blob: 828330e5e3c4a3bcc414f20bbdc5f13edf6977c7 [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
17#include "llvm/ADT/DenseMap.h"
Anders Carlsson35272252009-12-06 00:23:49 +000018#include "llvm/GlobalVariable.h"
Peter Collingbourne14110472011-01-13 18:57:25 +000019#include "clang/Basic/ABI.h"
Peter Collingbourne33446f12011-09-26 01:56:16 +000020#include "clang/AST/BaseSubobject.h"
Ken Dyck4230d522011-03-24 01:21:01 +000021#include "clang/AST/CharUnits.h"
Peter Collingbournefd05ca02011-06-14 04:02:39 +000022#include "clang/AST/GlobalDecl.h"
Peter Collingbourne24018462011-09-26 01:57:12 +000023#include "clang/AST/VTableBuilder.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
34 VTableContext VTContext;
35
Anders Carlsson046c2942010-04-17 20:15:18 +000036 /// VTables - All the vtables which have been defined.
37 llvm::DenseMap<const CXXRecordDecl *, llvm::GlobalVariable *> VTables;
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.
61 /// This will be done iff optimizations are enabled and the member function
62 /// doesn't contain any incomplete types.
63 void MaybeEmitThunkAvailableExternally(GlobalDecl GD, const ThunkInfo &Thunk);
64
Anders Carlsson0d1407e2010-03-25 15:26:28 +000065 /// CreateVTableInitializer - Create a vtable initializer for the given record
66 /// decl.
67 /// \param Components - The vtable components; this is really an array of
68 /// VTableComponents.
69 llvm::Constant *CreateVTableInitializer(const CXXRecordDecl *RD,
Peter Collingbournee09cdf42011-09-26 01:56:50 +000070 const VTableComponent *Components,
Anders Carlsson0d1407e2010-03-25 15:26:28 +000071 unsigned NumComponents,
Peter Collingbournee09cdf42011-09-26 01:56:50 +000072 const VTableLayout::VTableThunkTy *VTableThunks,
73 unsigned NumVTableThunks);
Anders Carlsson2c822f12010-03-26 03:56:54 +000074
Anders Carlssondbd920c2009-10-11 22:13:54 +000075public:
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
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +000080 /// \brief True if the VTable of this record must be emitted in the
81 /// translation unit.
82 bool ShouldEmitVTableInThisTU(const CXXRecordDecl *RD);
Rafael Espindolab8cab182010-04-19 00:44:22 +000083
Anders Carlssonc997d422010-01-02 01:01:18 +000084 /// needsVTTParameter - Return whether the given global decl needs a VTT
85 /// parameter, which it does if it's a base constructor or destructor with
86 /// virtual bases.
87 static bool needsVTTParameter(GlobalDecl GD);
88
89 /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the
90 /// given record decl.
Anders Carlssonc11bb212010-05-02 23:53:25 +000091 uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base);
Anders Carlssonc997d422010-01-02 01:01:18 +000092
Anders Carlssone1dcc222010-03-26 04:23:58 +000093 /// 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 Carlsson64c9eca2010-03-29 02:08:26 +000098 /// getAddressPoint - Get the address point of the given subobject in the
99 /// class decl.
100 uint64_t getAddressPoint(BaseSubobject Base, const CXXRecordDecl *RD);
101
Anders Carlsson5eea8762010-03-24 05:32:05 +0000102 /// GetAddrOfVTable - Get the address of the vtable for the given record decl.
Anders Carlsson9dc338a2010-03-30 03:35:35 +0000103 llvm::GlobalVariable *GetAddrOfVTable(const CXXRecordDecl *RD);
Anders Carlsson5c6c1d92010-03-24 03:57:14 +0000104
Anders Carlssona7cde3b2010-03-29 03:38:52 +0000105 /// EmitVTableDefinition - Emit the definition of the given vtable.
106 void EmitVTableDefinition(llvm::GlobalVariable *VTable,
107 llvm::GlobalVariable::LinkageTypes Linkage,
108 const CXXRecordDecl *RD);
109
Anders Carlssonff143f82010-03-25 00:35:49 +0000110 /// GenerateConstructionVTable - Generate a construction vtable for the given
111 /// base subobject.
112 llvm::GlobalVariable *
113 GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base,
114 bool BaseIsVirtual,
John McCallbda0d6b2011-03-27 09:00:25 +0000115 llvm::GlobalVariable::LinkageTypes Linkage,
Anders Carlsson2c822f12010-03-26 03:56:54 +0000116 VTableAddressPointsMapTy& AddressPoints);
Anders Carlsson1cbce122011-01-29 19:16:51 +0000117
118
119 /// GetAddrOfVTable - Get the address of the VTT for the given record decl.
120 llvm::GlobalVariable *GetAddrOfVTT(const CXXRecordDecl *RD);
121
122 /// EmitVTTDefinition - Emit the definition of the given vtable.
123 void EmitVTTDefinition(llvm::GlobalVariable *VTT,
124 llvm::GlobalVariable::LinkageTypes Linkage,
125 const CXXRecordDecl *RD);
Rafael Espindolabbf58bb2010-03-10 02:19:29 +0000126
Douglas Gregor6fb745b2010-05-13 16:44:06 +0000127 /// EmitThunks - Emit the associated thunks for the given global decl.
128 void EmitThunks(GlobalDecl GD);
129
Anders Carlsson7986ad52010-03-23 18:18:41 +0000130 /// GenerateClassData - Generate all the class data required to be generated
Rafael Espindolabbf58bb2010-03-10 02:19:29 +0000131 /// upon definition of a KeyFunction. This includes the vtable, the
132 /// rtti data structure and the VTT.
133 ///
134 /// \param Linkage - The desired linkage of the vtable, the RTTI and the VTT.
135 void GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
136 const CXXRecordDecl *RD);
Anders Carlssondbd920c2009-10-11 22:13:54 +0000137};
Benjamin Kramer39411b92009-11-26 13:09:03 +0000138
Anders Carlsson1bb60992010-01-14 02:29:07 +0000139} // end namespace CodeGen
140} // end namespace clang
Anders Carlssondbd920c2009-10-11 22:13:54 +0000141#endif