blob: a47841bfc6c3a67eb3838f0977325a122aa3fbe5 [file] [log] [blame]
John McCall358d0562011-03-27 09:00:25 +00001//===--- CGVTables.h - Emit LLVM Code for C++ vtables -----------*- C++ -*-===//
Anders Carlsson2bb27f52009-10-11 22:13:54 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Carlsson2bb27f52009-10-11 22:13:54 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This contains code dealing with C++ code generation of virtual tables.
10//
11//===----------------------------------------------------------------------===//
12
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000013#ifndef LLVM_CLANG_LIB_CODEGEN_CGVTABLES_H
14#define LLVM_CLANG_LIB_CODEGEN_CGVTABLES_H
Anders Carlsson2bb27f52009-10-11 22:13:54 +000015
Peter Collingbourne66c3a832011-09-26 01:56:16 +000016#include "clang/AST/BaseSubobject.h"
Ken Dyck16ffcac2011-03-24 01:21:01 +000017#include "clang/AST/CharUnits.h"
Peter Collingbourneee781d52011-06-14 04:02:39 +000018#include "clang/AST/GlobalDecl.h"
Peter Collingbournecfd23562011-09-26 01:57:12 +000019#include "clang/AST/VTableBuilder.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/Basic/ABI.h"
21#include "llvm/ADT/DenseMap.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000022#include "llvm/IR/GlobalVariable.h"
Anders Carlsson2bb27f52009-10-11 22:13:54 +000023
24namespace clang {
Anders Carlsson2bb27f52009-10-11 22:13:54 +000025 class CXXRecordDecl;
Benjamin Kramer334af992009-11-26 13:09:03 +000026
Anders Carlsson2bb27f52009-10-11 22:13:54 +000027namespace CodeGen {
28 class CodeGenModule;
John McCall9c6cb762016-11-28 22:18:33 +000029 class ConstantArrayBuilder;
Peter Collingbourne2849c4e2016-12-13 20:40:39 +000030 class ConstantStructBuilder;
Anders Carlssonc7785402009-11-26 02:32:05 +000031
Peter Collingbournea8341662011-09-26 01:56:30 +000032class CodeGenVTables {
33 CodeGenModule &CGM;
34
Reid Kleckner96f8f932014-02-05 17:27:08 +000035 VTableContextBase *VTContext;
36
Anders Carlssona208b392010-03-26 03:56:54 +000037 /// VTableAddressPointsMapTy - Address points for a single vtable.
Peter Collingbourne2849c4e2016-12-13 20:40:39 +000038 typedef VTableLayout::AddressPointsMapTy VTableAddressPointsMapTy;
Anders Carlssona208b392010-03-26 03:56:54 +000039
Peter Collingbourne5ee9ee42011-09-26 01:56:41 +000040 typedef std::pair<const CXXRecordDecl *, BaseSubobject> BaseSubobjectPairTy;
Anders Carlsson8bdbb5b2010-05-03 00:55:11 +000041 typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> SubVTTIndiciesMapTy;
Fangrui Song6907ce22018-07-30 19:24:48 +000042
Anders Carlssonf1a994c2010-03-26 04:23:58 +000043 /// SubVTTIndicies - Contains indices into the various sub-VTTs.
44 SubVTTIndiciesMapTy SubVTTIndicies;
45
Anders Carlsson8bdbb5b2010-05-03 00:55:11 +000046 typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t>
Anders Carlssonf1a994c2010-03-26 04:23:58 +000047 SecondaryVirtualPointerIndicesMapTy;
48
49 /// SecondaryVirtualPointerIndices - Contains the secondary virtual pointer
50 /// indices.
51 SecondaryVirtualPointerIndicesMapTy SecondaryVirtualPointerIndices;
Anders Carlssone36a6b32010-01-02 01:01:18 +000052
Peter Collingbournee53683f2016-09-08 01:14:39 +000053 /// 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 Kleckner399d96e2018-04-02 20:20:33 +000059 /// 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 Carlsson8b021832011-02-06 18:31:40 +000063
John McCall9c6cb762016-11-28 22:18:33 +000064 void addVTableComponent(ConstantArrayBuilder &builder,
65 const VTableLayout &layout, unsigned idx,
66 llvm::Constant *rtti,
67 unsigned &nextVTableThunkIndex);
Peter Collingbournee53683f2016-09-08 01:14:39 +000068
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +000069public:
John McCall9c6cb762016-11-28 22:18:33 +000070 /// Add vtable components for the given vtable layout to the given
71 /// global initializer.
Peter Collingbourne2849c4e2016-12-13 20:40:39 +000072 void createVTableInitializer(ConstantStructBuilder &builder,
John McCall9c6cb762016-11-28 22:18:33 +000073 const VTableLayout &layout,
74 llvm::Constant *rtti);
Anders Carlssona208b392010-03-26 03:56:54 +000075
Peter Collingbournea8341662011-09-26 01:56:30 +000076 CodeGenVTables(CodeGenModule &CGM);
77
Reid Klecknerb60a3d52013-12-20 23:58:52 +000078 ItaniumVTableContext &getItaniumVTableContext() {
Reid Kleckner96f8f932014-02-05 17:27:08 +000079 return *cast<ItaniumVTableContext>(VTContext);
Reid Klecknerb60a3d52013-12-20 23:58:52 +000080 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +000081
Timur Iskhodzhanov58776632013-11-05 15:54:58 +000082 MicrosoftVTableContext &getMicrosoftVTableContext() {
Reid Kleckner96f8f932014-02-05 17:27:08 +000083 return *cast<MicrosoftVTableContext>(VTContext);
Timur Iskhodzhanov58776632013-11-05 15:54:58 +000084 }
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +000085
Anders Carlssone36a6b32010-01-02 01:01:18 +000086 /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the
87 /// given record decl.
Anders Carlsson859b3062010-05-02 23:53:25 +000088 uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base);
Fangrui Song6907ce22018-07-30 19:24:48 +000089
Anders Carlssonf1a994c2010-03-26 04:23:58 +000090 /// 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 Song6907ce22018-07-30 19:24:48 +000095 /// GenerateConstructionVTable - Generate a construction vtable for the given
Anders Carlsson0534b022010-03-25 00:35:49 +000096 /// base subobject.
97 llvm::GlobalVariable *
Fangrui Song6907ce22018-07-30 19:24:48 +000098 GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base,
99 bool BaseIsVirtual,
John McCall358d0562011-03-27 09:00:25 +0000100 llvm::GlobalVariable::LinkageTypes Linkage,
Anders Carlssona208b392010-03-26 03:56:54 +0000101 VTableAddressPointsMapTy& AddressPoints);
Anders Carlsson883fc722011-01-29 19:16:51 +0000102
Fangrui Song6907ce22018-07-30 19:24:48 +0000103
Hans Wennborgfebdcb02014-06-02 18:50:54 +0000104 /// GetAddrOfVTT - Get the address of the VTT for the given record decl.
Anders Carlsson883fc722011-01-29 19:16:51 +0000105 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 Espindolae7113ca2010-03-10 02:19:29 +0000111
Douglas Gregor88d292c2010-05-13 16:44:06 +0000112 /// EmitThunks - Emit the associated thunks for the given global decl.
113 void EmitThunks(GlobalDecl GD);
Fangrui Song6907ce22018-07-30 19:24:48 +0000114
John McCall6bd2a892013-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);
Peter Collingbourne2849c4e2016-12-13 20:40:39 +0000122
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 Carlsson2bb27f52009-10-11 22:13:54 +0000127};
Benjamin Kramer334af992009-11-26 13:09:03 +0000128
Anders Carlsson5f9a8812010-01-14 02:29:07 +0000129} // end namespace CodeGen
130} // end namespace clang
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000131#endif