blob: 9b95952e5e82da1d8f94428a33729ff8c627c190 [file] [log] [blame]
John McCallbda0d6b2011-03-27 09:00:25 +00001//===--- CGVTables.h - Emit LLVM Code for C++ vtables -----------*- C++ -*-===//
Anders Carlssondbd920c2009-10-11 22:13:54 +00002//
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 Collingbourne33446f12011-09-26 01:56:16 +000017#include "clang/AST/BaseSubobject.h"
Ken Dyck4230d522011-03-24 01:21:01 +000018#include "clang/AST/CharUnits.h"
Peter Collingbournefd05ca02011-06-14 04:02:39 +000019#include "clang/AST/GlobalDecl.h"
Peter Collingbourne24018462011-09-26 01:57:12 +000020#include "clang/AST/VTableBuilder.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "clang/Basic/ABI.h"
22#include "llvm/ADT/DenseMap.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000023#include "llvm/IR/GlobalVariable.h"
Anders Carlssondbd920c2009-10-11 22:13:54 +000024
25namespace clang {
Anders Carlssondbd920c2009-10-11 22:13:54 +000026 class CXXRecordDecl;
Benjamin Kramer39411b92009-11-26 13:09:03 +000027
Anders Carlssondbd920c2009-10-11 22:13:54 +000028namespace CodeGen {
29 class CodeGenModule;
Anders Carlssona94822e2009-11-26 02:32:05 +000030
Peter Collingbourne1d2b3172011-09-26 01:56:30 +000031class CodeGenVTables {
32 CodeGenModule &CGM;
33
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +000034 // FIXME: Consider moving VTContext and VFTContext into respective CXXABI
35 // classes?
Peter Collingbourne1d2b3172011-09-26 01:56:30 +000036 VTableContext VTContext;
Timur Iskhodzhanov635de282013-07-30 09:46:19 +000037 OwningPtr<MicrosoftVFTableContext> VFTContext;
Anders Carlssond6b07fb2009-11-27 20:47:55 +000038
Anders Carlsson2c822f12010-03-26 03:56:54 +000039 /// VTableAddressPointsMapTy - Address points for a single vtable.
40 typedef llvm::DenseMap<BaseSubobject, uint64_t> VTableAddressPointsMapTy;
41
Peter Collingbourne84fcc482011-09-26 01:56:41 +000042 typedef std::pair<const CXXRecordDecl *, BaseSubobject> BaseSubobjectPairTy;
Anders Carlsson3855a072010-05-03 00:55:11 +000043 typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> SubVTTIndiciesMapTy;
Anders Carlssone1dcc222010-03-26 04:23:58 +000044
45 /// SubVTTIndicies - Contains indices into the various sub-VTTs.
46 SubVTTIndiciesMapTy SubVTTIndicies;
47
Anders Carlsson3855a072010-05-03 00:55:11 +000048 typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t>
Anders Carlssone1dcc222010-03-26 04:23:58 +000049 SecondaryVirtualPointerIndicesMapTy;
50
51 /// SecondaryVirtualPointerIndices - Contains the secondary virtual pointer
52 /// indices.
53 SecondaryVirtualPointerIndicesMapTy SecondaryVirtualPointerIndices;
Anders Carlssonc997d422010-01-02 01:01:18 +000054
Anders Carlssonfbf6ed42010-03-23 16:36:50 +000055 /// EmitThunk - Emit a single thunk.
Anders Carlsson14e82fd2011-02-06 18:31:40 +000056 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 Ledruf3477c12012-09-27 10:16:10 +000061 /// This will be done iff optimizations are enabled and the member function
Anders Carlsson14e82fd2011-02-06 18:31:40 +000062 /// doesn't contain any incomplete types.
63 void MaybeEmitThunkAvailableExternally(GlobalDecl GD, const ThunkInfo &Thunk);
64
Timur Iskhodzhanova53d7a02013-09-27 14:48:01 +000065public:
Anders Carlsson0d1407e2010-03-25 15:26:28 +000066 /// 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 Collingbournee09cdf42011-09-26 01:56:50 +000071 const VTableComponent *Components,
Anders Carlsson0d1407e2010-03-25 15:26:28 +000072 unsigned NumComponents,
Peter Collingbournee09cdf42011-09-26 01:56:50 +000073 const VTableLayout::VTableThunkTy *VTableThunks,
74 unsigned NumVTableThunks);
Anders Carlsson2c822f12010-03-26 03:56:54 +000075
Peter Collingbourne1d2b3172011-09-26 01:56:30 +000076 CodeGenVTables(CodeGenModule &CGM);
77
78 VTableContext &getVTableContext() { return VTContext; }
Anders Carlssondbd920c2009-10-11 22:13:54 +000079
Timur Iskhodzhanov8f189a92013-08-21 06:25:03 +000080 MicrosoftVFTableContext &getVFTableContext() { return *VFTContext.get(); }
81
Anders Carlssonc997d422010-01-02 01:01:18 +000082 /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the
83 /// given record decl.
Anders Carlssonc11bb212010-05-02 23:53:25 +000084 uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base);
Anders Carlssonc997d422010-01-02 01:01:18 +000085
Anders Carlssone1dcc222010-03-26 04:23:58 +000086 /// 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 Carlsson64c9eca2010-03-29 02:08:26 +000091 /// 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 Carlssonff143f82010-03-25 00:35:49 +000095 /// 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 McCallbda0d6b2011-03-27 09:00:25 +0000100 llvm::GlobalVariable::LinkageTypes Linkage,
Anders Carlsson2c822f12010-03-26 03:56:54 +0000101 VTableAddressPointsMapTy& AddressPoints);
Anders Carlsson1cbce122011-01-29 19:16:51 +0000102
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 Espindolabbf58bb2010-03-10 02:19:29 +0000111
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000112 /// EmitThunks - Emit the associated thunks for the given global decl.
113 void EmitThunks(GlobalDecl GD);
114
John McCalld5617ee2013-01-25 22:31:03 +0000115 /// 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 Carlssondbd920c2009-10-11 22:13:54 +0000122};
Benjamin Kramer39411b92009-11-26 13:09:03 +0000123
Anders Carlsson1bb60992010-01-14 02:29:07 +0000124} // end namespace CodeGen
125} // end namespace clang
Anders Carlssondbd920c2009-10-11 22:13:54 +0000126#endif