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 | |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 34 | // FIXME: Consider moving VTContext and VFTContext into respective CXXABI |
| 35 | // classes? |
Peter Collingbourne | 1d2b317 | 2011-09-26 01:56:30 +0000 | [diff] [blame] | 36 | VTableContext VTContext; |
Timur Iskhodzhanov | 635de28 | 2013-07-30 09:46:19 +0000 | [diff] [blame] | 37 | OwningPtr<MicrosoftVFTableContext> VFTContext; |
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. |
Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 61 | /// This will be done iff optimizations are enabled and the member function |
Anders Carlsson | 14e82fd | 2011-02-06 18:31:40 +0000 | [diff] [blame] | 62 | /// doesn't contain any incomplete types. |
| 63 | void MaybeEmitThunkAvailableExternally(GlobalDecl GD, const ThunkInfo &Thunk); |
| 64 | |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame^] | 65 | public: |
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 | |
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 | |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 80 | MicrosoftVFTableContext &getVFTableContext() { return *VFTContext.get(); } |
| 81 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 82 | /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the |
| 83 | /// given record decl. |
Anders Carlsson | c11bb21 | 2010-05-02 23:53:25 +0000 | [diff] [blame] | 84 | uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base); |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 85 | |
Anders Carlsson | e1dcc22 | 2010-03-26 04:23:58 +0000 | [diff] [blame] | 86 | /// getSecondaryVirtualPointerIndex - Return the index in the VTT where the |
| 87 | /// virtual pointer for the given subobject is located. |
| 88 | uint64_t getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD, |
| 89 | BaseSubobject Base); |
| 90 | |
Anders Carlsson | 64c9eca | 2010-03-29 02:08:26 +0000 | [diff] [blame] | 91 | /// getAddressPoint - Get the address point of the given subobject in the |
| 92 | /// class decl. |
| 93 | uint64_t getAddressPoint(BaseSubobject Base, const CXXRecordDecl *RD); |
| 94 | |
Anders Carlsson | ff143f8 | 2010-03-25 00:35:49 +0000 | [diff] [blame] | 95 | /// GenerateConstructionVTable - Generate a construction vtable for the given |
| 96 | /// base subobject. |
| 97 | llvm::GlobalVariable * |
| 98 | GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base, |
| 99 | bool BaseIsVirtual, |
John McCall | bda0d6b | 2011-03-27 09:00:25 +0000 | [diff] [blame] | 100 | llvm::GlobalVariable::LinkageTypes Linkage, |
Anders Carlsson | 2c822f1 | 2010-03-26 03:56:54 +0000 | [diff] [blame] | 101 | VTableAddressPointsMapTy& AddressPoints); |
Anders Carlsson | 1cbce12 | 2011-01-29 19:16:51 +0000 | [diff] [blame] | 102 | |
| 103 | |
| 104 | /// GetAddrOfVTable - Get the address of the VTT for the given record decl. |
| 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 | bbf58bb | 2010-03-10 02:19:29 +0000 | [diff] [blame] | 111 | |
Douglas Gregor | 6fb745b | 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); |
| 114 | |
John McCall | d5617ee | 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); |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 122 | }; |
Benjamin Kramer | 39411b9 | 2009-11-26 13:09:03 +0000 | [diff] [blame] | 123 | |
Anders Carlsson | 1bb6099 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 124 | } // end namespace CodeGen |
| 125 | } // end namespace clang |
Anders Carlsson | dbd920c | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 126 | #endif |