blob: b92212c368a983f729fd93525f4fa941dce7dcf8 [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//
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
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000014#ifndef LLVM_CLANG_LIB_CODEGEN_CGVTABLES_H
15#define LLVM_CLANG_LIB_CODEGEN_CGVTABLES_H
Anders Carlsson2bb27f52009-10-11 22:13:54 +000016
Peter Collingbourne66c3a832011-09-26 01:56:16 +000017#include "clang/AST/BaseSubobject.h"
Ken Dyck16ffcac2011-03-24 01:21:01 +000018#include "clang/AST/CharUnits.h"
Peter Collingbourneee781d52011-06-14 04:02:39 +000019#include "clang/AST/GlobalDecl.h"
Peter Collingbournecfd23562011-09-26 01:57:12 +000020#include "clang/AST/VTableBuilder.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Basic/ABI.h"
22#include "llvm/ADT/DenseMap.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000023#include "llvm/IR/GlobalVariable.h"
Anders Carlsson2bb27f52009-10-11 22:13:54 +000024
25namespace clang {
Anders Carlsson2bb27f52009-10-11 22:13:54 +000026 class CXXRecordDecl;
Benjamin Kramer334af992009-11-26 13:09:03 +000027
Anders Carlsson2bb27f52009-10-11 22:13:54 +000028namespace CodeGen {
29 class CodeGenModule;
John McCall9c6cb762016-11-28 22:18:33 +000030 class ConstantArrayBuilder;
Peter Collingbourne2849c4e2016-12-13 20:40:39 +000031 class ConstantStructBuilder;
Anders Carlssonc7785402009-11-26 02:32:05 +000032
Peter Collingbournea8341662011-09-26 01:56:30 +000033class CodeGenVTables {
34 CodeGenModule &CGM;
35
Reid Kleckner96f8f932014-02-05 17:27:08 +000036 VTableContextBase *VTContext;
37
Anders Carlssona208b392010-03-26 03:56:54 +000038 /// VTableAddressPointsMapTy - Address points for a single vtable.
Peter Collingbourne2849c4e2016-12-13 20:40:39 +000039 typedef VTableLayout::AddressPointsMapTy VTableAddressPointsMapTy;
Anders Carlssona208b392010-03-26 03:56:54 +000040
Peter Collingbourne5ee9ee42011-09-26 01:56:41 +000041 typedef std::pair<const CXXRecordDecl *, BaseSubobject> BaseSubobjectPairTy;
Anders Carlsson8bdbb5b2010-05-03 00:55:11 +000042 typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> SubVTTIndiciesMapTy;
Anders Carlssonf1a994c2010-03-26 04:23:58 +000043
44 /// SubVTTIndicies - Contains indices into the various sub-VTTs.
45 SubVTTIndiciesMapTy SubVTTIndicies;
46
Anders Carlsson8bdbb5b2010-05-03 00:55:11 +000047 typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t>
Anders Carlssonf1a994c2010-03-26 04:23:58 +000048 SecondaryVirtualPointerIndicesMapTy;
49
50 /// SecondaryVirtualPointerIndices - Contains the secondary virtual pointer
51 /// indices.
52 SecondaryVirtualPointerIndicesMapTy SecondaryVirtualPointerIndices;
Anders Carlssone36a6b32010-01-02 01:01:18 +000053
Peter Collingbournee53683f2016-09-08 01:14:39 +000054 /// Cache for the pure virtual member call function.
55 llvm::Constant *PureVirtualFn = nullptr;
56
57 /// Cache for the deleted virtual member call function.
58 llvm::Constant *DeletedVirtualFn = nullptr;
59
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +000060 /// emitThunk - Emit a single thunk.
61 void emitThunk(GlobalDecl GD, const ThunkInfo &Thunk, bool ForVTable);
Anders Carlsson8b021832011-02-06 18:31:40 +000062
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +000063 /// maybeEmitThunkForVTable - Emit the given thunk for the vtable if needed by
64 /// the ABI.
65 void maybeEmitThunkForVTable(GlobalDecl GD, const ThunkInfo &Thunk);
Anders Carlsson8b021832011-02-06 18:31:40 +000066
John McCall9c6cb762016-11-28 22:18:33 +000067 void addVTableComponent(ConstantArrayBuilder &builder,
68 const VTableLayout &layout, unsigned idx,
69 llvm::Constant *rtti,
70 unsigned &nextVTableThunkIndex);
Peter Collingbournee53683f2016-09-08 01:14:39 +000071
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +000072public:
John McCall9c6cb762016-11-28 22:18:33 +000073 /// Add vtable components for the given vtable layout to the given
74 /// global initializer.
Peter Collingbourne2849c4e2016-12-13 20:40:39 +000075 void createVTableInitializer(ConstantStructBuilder &builder,
John McCall9c6cb762016-11-28 22:18:33 +000076 const VTableLayout &layout,
77 llvm::Constant *rtti);
Anders Carlssona208b392010-03-26 03:56:54 +000078
Peter Collingbournea8341662011-09-26 01:56:30 +000079 CodeGenVTables(CodeGenModule &CGM);
80
Reid Klecknerb60a3d52013-12-20 23:58:52 +000081 ItaniumVTableContext &getItaniumVTableContext() {
Reid Kleckner96f8f932014-02-05 17:27:08 +000082 return *cast<ItaniumVTableContext>(VTContext);
Reid Klecknerb60a3d52013-12-20 23:58:52 +000083 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +000084
Timur Iskhodzhanov58776632013-11-05 15:54:58 +000085 MicrosoftVTableContext &getMicrosoftVTableContext() {
Reid Kleckner96f8f932014-02-05 17:27:08 +000086 return *cast<MicrosoftVTableContext>(VTContext);
Timur Iskhodzhanov58776632013-11-05 15:54:58 +000087 }
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +000088
Anders Carlssone36a6b32010-01-02 01:01:18 +000089 /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the
90 /// given record decl.
Anders Carlsson859b3062010-05-02 23:53:25 +000091 uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base);
Anders Carlssone36a6b32010-01-02 01:01:18 +000092
Anders Carlssonf1a994c2010-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 Carlsson0534b022010-03-25 00:35:49 +000098 /// GenerateConstructionVTable - Generate a construction vtable for the given
99 /// base subobject.
100 llvm::GlobalVariable *
101 GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base,
102 bool BaseIsVirtual,
John McCall358d0562011-03-27 09:00:25 +0000103 llvm::GlobalVariable::LinkageTypes Linkage,
Anders Carlssona208b392010-03-26 03:56:54 +0000104 VTableAddressPointsMapTy& AddressPoints);
Anders Carlsson883fc722011-01-29 19:16:51 +0000105
106
Hans Wennborgfebdcb02014-06-02 18:50:54 +0000107 /// GetAddrOfVTT - Get the address of the VTT for the given record decl.
Anders Carlsson883fc722011-01-29 19:16:51 +0000108 llvm::GlobalVariable *GetAddrOfVTT(const CXXRecordDecl *RD);
109
110 /// EmitVTTDefinition - Emit the definition of the given vtable.
111 void EmitVTTDefinition(llvm::GlobalVariable *VTT,
112 llvm::GlobalVariable::LinkageTypes Linkage,
113 const CXXRecordDecl *RD);
Rafael Espindolae7113ca2010-03-10 02:19:29 +0000114
Douglas Gregor88d292c2010-05-13 16:44:06 +0000115 /// EmitThunks - Emit the associated thunks for the given global decl.
116 void EmitThunks(GlobalDecl GD);
117
John McCall6bd2a892013-01-25 22:31:03 +0000118 /// GenerateClassData - Generate all the class data required to be
119 /// generated upon definition of a KeyFunction. This includes the
120 /// vtable, the RTTI data structure (if RTTI is enabled) and the VTT
121 /// (if the class has virtual bases).
122 void GenerateClassData(const CXXRecordDecl *RD);
123
124 bool isVTableExternal(const CXXRecordDecl *RD);
Peter Collingbourne2849c4e2016-12-13 20:40:39 +0000125
126 /// Returns the type of a vtable with the given layout. Normally a struct of
127 /// arrays of pointers, with one struct element for each vtable in the vtable
128 /// group.
129 llvm::Type *getVTableType(const VTableLayout &layout);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000130};
Benjamin Kramer334af992009-11-26 13:09:03 +0000131
Anders Carlsson5f9a8812010-01-14 02:29:07 +0000132} // end namespace CodeGen
133} // end namespace clang
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000134#endif