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