John McCall | bda0d6b | 2011-03-27 09:00:25 +0000 | [diff] [blame] | 1 | //===--- CGVTables.h - Emit LLVM Code for C++ vtables -----------*- C++ -*-===// |
Anders Carlsson | dbd920c | 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 | 3527225 | 2009-12-06 00:23:49 +0000 | [diff] [blame] | 18 | #include "llvm/GlobalVariable.h" |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 19 | #include "clang/Basic/ABI.h" |
Peter Collingbourne | 33446f1 | 2011-09-26 01:56:16 +0000 | [diff] [blame] | 20 | #include "clang/AST/BaseSubobject.h" |
Ken Dyck | 4230d52 | 2011-03-24 01:21:01 +0000 | [diff] [blame] | 21 | #include "clang/AST/CharUnits.h" |
Peter Collingbourne | fd05ca0 | 2011-06-14 04:02:39 +0000 | [diff] [blame] | 22 | #include "clang/AST/GlobalDecl.h" |
Peter Collingbourne | 2401846 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 23 | #include "clang/AST/VTableBuilder.h" |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 24 | |
| 25 | namespace clang { |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 26 | class CXXRecordDecl; |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 27 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 28 | namespace CodeGen { |
| 29 | class CodeGenModule; |
Anders Carlsson | a94822e | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 30 | |
Peter Collingbourne | 1d2b317 | 2011-09-26 01:56:30 +0000 | [diff] [blame] | 31 | class CodeGenVTables { |
| 32 | CodeGenModule &CGM; |
| 33 | |
| 34 | VTableContext VTContext; |
| 35 | |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 36 | /// VTables - All the vtables which have been defined. |
| 37 | llvm::DenseMap<const CXXRecordDecl *, llvm::GlobalVariable *> VTables; |
Anders Carlsson | d6b07fb | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 38 | |
Anders Carlsson | 2c822f1 | 2010-03-26 03:56:54 +0000 | [diff] [blame] | 39 | /// VTableAddressPointsMapTy - Address points for a single vtable. |
| 40 | typedef llvm::DenseMap<BaseSubobject, uint64_t> VTableAddressPointsMapTy; |
| 41 | |
Peter Collingbourne | 84fcc48 | 2011-09-26 01:56:41 +0000 | [diff] [blame] | 42 | typedef std::pair<const CXXRecordDecl *, BaseSubobject> BaseSubobjectPairTy; |
Anders Carlsson | 3855a07 | 2010-05-03 00:55:11 +0000 | [diff] [blame] | 43 | typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> SubVTTIndiciesMapTy; |
Anders Carlsson | e1dcc22 | 2010-03-26 04:23:58 +0000 | [diff] [blame] | 44 | |
| 45 | /// SubVTTIndicies - Contains indices into the various sub-VTTs. |
| 46 | SubVTTIndiciesMapTy SubVTTIndicies; |
| 47 | |
Anders Carlsson | 3855a07 | 2010-05-03 00:55:11 +0000 | [diff] [blame] | 48 | typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> |
Anders Carlsson | e1dcc22 | 2010-03-26 04:23:58 +0000 | [diff] [blame] | 49 | SecondaryVirtualPointerIndicesMapTy; |
| 50 | |
| 51 | /// SecondaryVirtualPointerIndices - Contains the secondary virtual pointer |
| 52 | /// indices. |
| 53 | SecondaryVirtualPointerIndicesMapTy SecondaryVirtualPointerIndices; |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 54 | |
Anders Carlsson | fbf6ed4 | 2010-03-23 16:36:50 +0000 | [diff] [blame] | 55 | /// EmitThunk - Emit a single thunk. |
Anders Carlsson | 14e82fd | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 56 | void EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk, |
| 57 | bool UseAvailableExternallyLinkage); |
| 58 | |
| 59 | /// MaybeEmitThunkAvailableExternally - Try to emit the given thunk with |
| 60 | /// available_externally linkage to allow for inlining of thunks. |
| 61 | /// This will be done iff optimizations are enabled and the member function |
| 62 | /// doesn't contain any incomplete types. |
| 63 | void MaybeEmitThunkAvailableExternally(GlobalDecl GD, const ThunkInfo &Thunk); |
| 64 | |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 65 | /// CreateVTableInitializer - Create a vtable initializer for the given record |
| 66 | /// decl. |
| 67 | /// \param Components - The vtable components; this is really an array of |
| 68 | /// VTableComponents. |
| 69 | llvm::Constant *CreateVTableInitializer(const CXXRecordDecl *RD, |
Peter Collingbourne | e09cdf4 | 2011-09-26 01:56:50 +0000 | [diff] [blame] | 70 | const VTableComponent *Components, |
Anders Carlsson | 0d1407e | 2010-03-25 15:26:28 +0000 | [diff] [blame] | 71 | unsigned NumComponents, |
Peter Collingbourne | e09cdf4 | 2011-09-26 01:56:50 +0000 | [diff] [blame] | 72 | const VTableLayout::VTableThunkTy *VTableThunks, |
| 73 | unsigned NumVTableThunks); |
Anders Carlsson | 2c822f1 | 2010-03-26 03:56:54 +0000 | [diff] [blame] | 74 | |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 75 | public: |
Peter Collingbourne | 1d2b317 | 2011-09-26 01:56:30 +0000 | [diff] [blame] | 76 | CodeGenVTables(CodeGenModule &CGM); |
| 77 | |
| 78 | VTableContext &getVTableContext() { return VTContext; } |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 79 | |
Argyrios Kyrtzidis | d2c47bd | 2010-10-11 03:25:57 +0000 | [diff] [blame] | 80 | /// \brief True if the VTable of this record must be emitted in the |
| 81 | /// translation unit. |
| 82 | bool ShouldEmitVTableInThisTU(const CXXRecordDecl *RD); |
Rafael Espindola | b8cab18 | 2010-04-19 00:44:22 +0000 | [diff] [blame] | 83 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 84 | /// needsVTTParameter - Return whether the given global decl needs a VTT |
| 85 | /// parameter, which it does if it's a base constructor or destructor with |
| 86 | /// virtual bases. |
| 87 | static bool needsVTTParameter(GlobalDecl GD); |
| 88 | |
| 89 | /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the |
| 90 | /// given record decl. |
Anders Carlsson | c11bb21 | 2010-05-02 23:53:25 +0000 | [diff] [blame] | 91 | uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base); |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 92 | |
Anders Carlsson | e1dcc22 | 2010-03-26 04:23:58 +0000 | [diff] [blame] | 93 | /// getSecondaryVirtualPointerIndex - Return the index in the VTT where the |
| 94 | /// virtual pointer for the given subobject is located. |
| 95 | uint64_t getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD, |
| 96 | BaseSubobject Base); |
| 97 | |
Anders Carlsson | 64c9eca | 2010-03-29 02:08:26 +0000 | [diff] [blame] | 98 | /// getAddressPoint - Get the address point of the given subobject in the |
| 99 | /// class decl. |
| 100 | uint64_t getAddressPoint(BaseSubobject Base, const CXXRecordDecl *RD); |
| 101 | |
Anders Carlsson | 5eea876 | 2010-03-24 05:32:05 +0000 | [diff] [blame] | 102 | /// GetAddrOfVTable - Get the address of the vtable for the given record decl. |
Anders Carlsson | 9dc338a | 2010-03-30 03:35:35 +0000 | [diff] [blame] | 103 | llvm::GlobalVariable *GetAddrOfVTable(const CXXRecordDecl *RD); |
Anders Carlsson | 5c6c1d9 | 2010-03-24 03:57:14 +0000 | [diff] [blame] | 104 | |
Anders Carlsson | a7cde3b | 2010-03-29 03:38:52 +0000 | [diff] [blame] | 105 | /// EmitVTableDefinition - Emit the definition of the given vtable. |
| 106 | void EmitVTableDefinition(llvm::GlobalVariable *VTable, |
| 107 | llvm::GlobalVariable::LinkageTypes Linkage, |
| 108 | const CXXRecordDecl *RD); |
| 109 | |
Anders Carlsson | ff143f8 | 2010-03-25 00:35:49 +0000 | [diff] [blame] | 110 | /// GenerateConstructionVTable - Generate a construction vtable for the given |
| 111 | /// base subobject. |
| 112 | llvm::GlobalVariable * |
| 113 | GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base, |
| 114 | bool BaseIsVirtual, |
John McCall | bda0d6b | 2011-03-27 09:00:25 +0000 | [diff] [blame] | 115 | llvm::GlobalVariable::LinkageTypes Linkage, |
Anders Carlsson | 2c822f1 | 2010-03-26 03:56:54 +0000 | [diff] [blame] | 116 | VTableAddressPointsMapTy& AddressPoints); |
Anders Carlsson | 1cbce12 | 2011-01-29 19:16:51 +0000 | [diff] [blame] | 117 | |
| 118 | |
| 119 | /// GetAddrOfVTable - Get the address of the VTT for the given record decl. |
| 120 | llvm::GlobalVariable *GetAddrOfVTT(const CXXRecordDecl *RD); |
| 121 | |
| 122 | /// EmitVTTDefinition - Emit the definition of the given vtable. |
| 123 | void EmitVTTDefinition(llvm::GlobalVariable *VTT, |
| 124 | llvm::GlobalVariable::LinkageTypes Linkage, |
| 125 | const CXXRecordDecl *RD); |
Rafael Espindola | bbf58bb | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 126 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 127 | /// EmitThunks - Emit the associated thunks for the given global decl. |
| 128 | void EmitThunks(GlobalDecl GD); |
| 129 | |
Anders Carlsson | 7986ad5 | 2010-03-23 18:18:41 +0000 | [diff] [blame] | 130 | /// GenerateClassData - Generate all the class data required to be generated |
Rafael Espindola | bbf58bb | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 131 | /// upon definition of a KeyFunction. This includes the vtable, the |
| 132 | /// rtti data structure and the VTT. |
| 133 | /// |
| 134 | /// \param Linkage - The desired linkage of the vtable, the RTTI and the VTT. |
| 135 | void GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage, |
| 136 | const CXXRecordDecl *RD); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 137 | }; |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 138 | |
Anders Carlsson | 1bb6099 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 139 | } // end namespace CodeGen |
| 140 | } // end namespace clang |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 141 | #endif |