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 | |
Benjamin Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 14 | #ifndef LLVM_CLANG_LIB_CODEGEN_CGVTABLES_H |
| 15 | #define LLVM_CLANG_LIB_CODEGEN_CGVTABLES_H |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 16 | |
Peter Collingbourne | 66c3a83 | 2011-09-26 01:56:16 +0000 | [diff] [blame] | 17 | #include "clang/AST/BaseSubobject.h" |
Ken Dyck | 16ffcac | 2011-03-24 01:21:01 +0000 | [diff] [blame] | 18 | #include "clang/AST/CharUnits.h" |
Peter Collingbourne | ee781d5 | 2011-06-14 04:02:39 +0000 | [diff] [blame] | 19 | #include "clang/AST/GlobalDecl.h" |
Peter Collingbourne | cfd2356 | 2011-09-26 01:57:12 +0000 | [diff] [blame] | 20 | #include "clang/AST/VTableBuilder.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/Basic/ABI.h" |
| 22 | #include "llvm/ADT/DenseMap.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 23 | #include "llvm/IR/GlobalVariable.h" |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 24 | |
| 25 | namespace clang { |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 26 | class CXXRecordDecl; |
Benjamin Kramer | 334af99 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 27 | |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 28 | namespace CodeGen { |
| 29 | class CodeGenModule; |
John McCall | 9c6cb76 | 2016-11-28 22:18:33 +0000 | [diff] [blame] | 30 | class ConstantArrayBuilder; |
Peter Collingbourne | 2849c4e | 2016-12-13 20:40:39 +0000 | [diff] [blame] | 31 | class ConstantStructBuilder; |
Anders Carlsson | c778540 | 2009-11-26 02:32:05 +0000 | [diff] [blame] | 32 | |
Peter Collingbourne | a834166 | 2011-09-26 01:56:30 +0000 | [diff] [blame] | 33 | class CodeGenVTables { |
| 34 | CodeGenModule &CGM; |
| 35 | |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 36 | VTableContextBase *VTContext; |
| 37 | |
Anders Carlsson | a208b39 | 2010-03-26 03:56:54 +0000 | [diff] [blame] | 38 | /// VTableAddressPointsMapTy - Address points for a single vtable. |
Peter Collingbourne | 2849c4e | 2016-12-13 20:40:39 +0000 | [diff] [blame] | 39 | typedef VTableLayout::AddressPointsMapTy VTableAddressPointsMapTy; |
Anders Carlsson | a208b39 | 2010-03-26 03:56:54 +0000 | [diff] [blame] | 40 | |
Peter Collingbourne | 5ee9ee4 | 2011-09-26 01:56:41 +0000 | [diff] [blame] | 41 | typedef std::pair<const CXXRecordDecl *, BaseSubobject> BaseSubobjectPairTy; |
Anders Carlsson | 8bdbb5b | 2010-05-03 00:55:11 +0000 | [diff] [blame] | 42 | typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> SubVTTIndiciesMapTy; |
Anders Carlsson | f1a994c | 2010-03-26 04:23:58 +0000 | [diff] [blame] | 43 | |
| 44 | /// SubVTTIndicies - Contains indices into the various sub-VTTs. |
| 45 | SubVTTIndiciesMapTy SubVTTIndicies; |
| 46 | |
Anders Carlsson | 8bdbb5b | 2010-05-03 00:55:11 +0000 | [diff] [blame] | 47 | typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> |
Anders Carlsson | f1a994c | 2010-03-26 04:23:58 +0000 | [diff] [blame] | 48 | SecondaryVirtualPointerIndicesMapTy; |
| 49 | |
| 50 | /// SecondaryVirtualPointerIndices - Contains the secondary virtual pointer |
| 51 | /// indices. |
| 52 | SecondaryVirtualPointerIndicesMapTy SecondaryVirtualPointerIndices; |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 53 | |
Peter Collingbourne | e53683f | 2016-09-08 01:14:39 +0000 | [diff] [blame] | 54 | /// Cache for the pure virtual member call function. |
| 55 | llvm::Constant *PureVirtualFn = nullptr; |
| 56 | |
| 57 | /// Cache for the deleted virtual member call function. |
| 58 | llvm::Constant *DeletedVirtualFn = nullptr; |
| 59 | |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 60 | /// emitThunk - Emit a single thunk. |
| 61 | void emitThunk(GlobalDecl GD, const ThunkInfo &Thunk, bool ForVTable); |
Anders Carlsson | 8b02183 | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 62 | |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 63 | /// maybeEmitThunkForVTable - Emit the given thunk for the vtable if needed by |
| 64 | /// the ABI. |
| 65 | void maybeEmitThunkForVTable(GlobalDecl GD, const ThunkInfo &Thunk); |
Anders Carlsson | 8b02183 | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 66 | |
John McCall | 9c6cb76 | 2016-11-28 22:18:33 +0000 | [diff] [blame] | 67 | void addVTableComponent(ConstantArrayBuilder &builder, |
| 68 | const VTableLayout &layout, unsigned idx, |
| 69 | llvm::Constant *rtti, |
| 70 | unsigned &nextVTableThunkIndex); |
Peter Collingbourne | e53683f | 2016-09-08 01:14:39 +0000 | [diff] [blame] | 71 | |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 72 | public: |
John McCall | 9c6cb76 | 2016-11-28 22:18:33 +0000 | [diff] [blame] | 73 | /// Add vtable components for the given vtable layout to the given |
| 74 | /// global initializer. |
Peter Collingbourne | 2849c4e | 2016-12-13 20:40:39 +0000 | [diff] [blame] | 75 | void createVTableInitializer(ConstantStructBuilder &builder, |
John McCall | 9c6cb76 | 2016-11-28 22:18:33 +0000 | [diff] [blame] | 76 | const VTableLayout &layout, |
| 77 | llvm::Constant *rtti); |
Anders Carlsson | a208b39 | 2010-03-26 03:56:54 +0000 | [diff] [blame] | 78 | |
Peter Collingbourne | a834166 | 2011-09-26 01:56:30 +0000 | [diff] [blame] | 79 | CodeGenVTables(CodeGenModule &CGM); |
| 80 | |
Reid Kleckner | b60a3d5 | 2013-12-20 23:58:52 +0000 | [diff] [blame] | 81 | ItaniumVTableContext &getItaniumVTableContext() { |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 82 | return *cast<ItaniumVTableContext>(VTContext); |
Reid Kleckner | b60a3d5 | 2013-12-20 23:58:52 +0000 | [diff] [blame] | 83 | } |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 84 | |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 85 | MicrosoftVTableContext &getMicrosoftVTableContext() { |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 86 | return *cast<MicrosoftVTableContext>(VTContext); |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 87 | } |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 88 | |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 89 | /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the |
| 90 | /// given record decl. |
Anders Carlsson | 859b306 | 2010-05-02 23:53:25 +0000 | [diff] [blame] | 91 | uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base); |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 92 | |
Anders Carlsson | f1a994c | 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 | 0534b02 | 2010-03-25 00:35:49 +0000 | [diff] [blame] | 98 | /// GenerateConstructionVTable - Generate a construction vtable for the given |
| 99 | /// base subobject. |
| 100 | llvm::GlobalVariable * |
| 101 | GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base, |
| 102 | bool BaseIsVirtual, |
John McCall | 358d056 | 2011-03-27 09:00:25 +0000 | [diff] [blame] | 103 | llvm::GlobalVariable::LinkageTypes Linkage, |
Anders Carlsson | a208b39 | 2010-03-26 03:56:54 +0000 | [diff] [blame] | 104 | VTableAddressPointsMapTy& AddressPoints); |
Anders Carlsson | 883fc72 | 2011-01-29 19:16:51 +0000 | [diff] [blame] | 105 | |
| 106 | |
Hans Wennborg | febdcb0 | 2014-06-02 18:50:54 +0000 | [diff] [blame] | 107 | /// GetAddrOfVTT - Get the address of the VTT for the given record decl. |
Anders Carlsson | 883fc72 | 2011-01-29 19:16:51 +0000 | [diff] [blame] | 108 | llvm::GlobalVariable *GetAddrOfVTT(const CXXRecordDecl *RD); |
| 109 | |
| 110 | /// EmitVTTDefinition - Emit the definition of the given vtable. |
| 111 | void EmitVTTDefinition(llvm::GlobalVariable *VTT, |
| 112 | llvm::GlobalVariable::LinkageTypes Linkage, |
| 113 | const CXXRecordDecl *RD); |
Rafael Espindola | e7113ca | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 114 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 115 | /// EmitThunks - Emit the associated thunks for the given global decl. |
| 116 | void EmitThunks(GlobalDecl GD); |
| 117 | |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 118 | /// GenerateClassData - Generate all the class data required to be |
| 119 | /// generated upon definition of a KeyFunction. This includes the |
| 120 | /// vtable, the RTTI data structure (if RTTI is enabled) and the VTT |
| 121 | /// (if the class has virtual bases). |
| 122 | void GenerateClassData(const CXXRecordDecl *RD); |
| 123 | |
| 124 | bool isVTableExternal(const CXXRecordDecl *RD); |
Peter Collingbourne | 2849c4e | 2016-12-13 20:40:39 +0000 | [diff] [blame] | 125 | |
| 126 | /// Returns the type of a vtable with the given layout. Normally a struct of |
| 127 | /// arrays of pointers, with one struct element for each vtable in the vtable |
| 128 | /// group. |
| 129 | llvm::Type *getVTableType(const VTableLayout &layout); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 130 | }; |
Benjamin Kramer | 334af99 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 131 | |
Anders Carlsson | 5f9a881 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 132 | } // end namespace CodeGen |
| 133 | } // end namespace clang |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 134 | #endif |