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