clang  14.0.0git
CGVTables.h
Go to the documentation of this file.
1 //===--- CGVTables.h - Emit LLVM Code for C++ vtables -----------*- C++ -*-===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This contains code dealing with C++ code generation of virtual tables.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_LIB_CODEGEN_CGVTABLES_H
14 #define LLVM_CLANG_LIB_CODEGEN_CGVTABLES_H
15 
17 #include "clang/AST/CharUnits.h"
18 #include "clang/AST/GlobalDecl.h"
20 #include "clang/Basic/ABI.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/IR/GlobalVariable.h"
23 
24 namespace clang {
25  class CXXRecordDecl;
26 
27 namespace CodeGen {
28  class CodeGenModule;
29  class ConstantArrayBuilder;
30  class ConstantStructBuilder;
31 
33  CodeGenModule &CGM;
34 
35  VTableContextBase *VTContext;
36 
37  /// VTableAddressPointsMapTy - Address points for a single vtable.
38  typedef VTableLayout::AddressPointsMapTy VTableAddressPointsMapTy;
39 
40  typedef std::pair<const CXXRecordDecl *, BaseSubobject> BaseSubobjectPairTy;
41  typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> SubVTTIndiciesMapTy;
42 
43  /// SubVTTIndicies - Contains indices into the various sub-VTTs.
44  SubVTTIndiciesMapTy SubVTTIndicies;
45 
46  typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t>
47  SecondaryVirtualPointerIndicesMapTy;
48 
49  /// SecondaryVirtualPointerIndices - Contains the secondary virtual pointer
50  /// indices.
51  SecondaryVirtualPointerIndicesMapTy SecondaryVirtualPointerIndices;
52 
53  /// 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 
59  /// Get the address of a thunk and emit it if necessary.
60  llvm::Constant *maybeEmitThunk(GlobalDecl GD,
61  const ThunkInfo &ThunkAdjustments,
62  bool ForVTable);
63 
64  void addVTableComponent(ConstantArrayBuilder &builder,
65  const VTableLayout &layout, unsigned componentIndex,
66  llvm::Constant *rtti, unsigned &nextVTableThunkIndex,
67  unsigned vtableAddressPoint,
68  bool vtableHasLocalLinkage);
69 
70  /// Add a 32-bit offset to a component relative to the vtable when using the
71  /// relative vtables ABI. The array builder points to the start of the vtable.
72  void addRelativeComponent(ConstantArrayBuilder &builder,
73  llvm::Constant *component,
74  unsigned vtableAddressPoint,
75  bool vtableHasLocalLinkage,
76  bool isCompleteDtor) const;
77 
78  /// Create a dso_local stub that will be used for a relative reference in the
79  /// relative vtable layout. This stub will just be a tail call to the original
80  /// function and propagate any function attributes from the original. If the
81  /// original function is already dso_local, the original is returned instead
82  /// and a stub is not created.
83  llvm::Function *
84  getOrCreateRelativeStub(llvm::Function *func,
85  llvm::GlobalValue::LinkageTypes stubLinkage,
86  bool isCompleteDtor) const;
87 
88  bool useRelativeLayout() const;
89 
90  llvm::Type *getVTableComponentType() const;
91 
92 public:
93  /// Add vtable components for the given vtable layout to the given
94  /// global initializer.
96  const VTableLayout &layout, llvm::Constant *rtti,
97  bool vtableHasLocalLinkage);
98 
100 
102  return *cast<ItaniumVTableContext>(VTContext);
103  }
104 
106  return *cast<MicrosoftVTableContext>(VTContext);
107  }
108 
109  /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the
110  /// given record decl.
111  uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base);
112 
113  /// getSecondaryVirtualPointerIndex - Return the index in the VTT where the
114  /// virtual pointer for the given subobject is located.
117 
118  /// GenerateConstructionVTable - Generate a construction vtable for the given
119  /// base subobject.
120  llvm::GlobalVariable *
122  bool BaseIsVirtual,
123  llvm::GlobalVariable::LinkageTypes Linkage,
124  VTableAddressPointsMapTy& AddressPoints);
125 
126 
127  /// GetAddrOfVTT - Get the address of the VTT for the given record decl.
128  llvm::GlobalVariable *GetAddrOfVTT(const CXXRecordDecl *RD);
129 
130  /// EmitVTTDefinition - Emit the definition of the given vtable.
131  void EmitVTTDefinition(llvm::GlobalVariable *VTT,
132  llvm::GlobalVariable::LinkageTypes Linkage,
133  const CXXRecordDecl *RD);
134 
135  /// EmitThunks - Emit the associated thunks for the given global decl.
136  void EmitThunks(GlobalDecl GD);
137 
138  /// GenerateClassData - Generate all the class data required to be
139  /// generated upon definition of a KeyFunction. This includes the
140  /// vtable, the RTTI data structure (if RTTI is enabled) and the VTT
141  /// (if the class has virtual bases).
142  void GenerateClassData(const CXXRecordDecl *RD);
143 
144  bool isVTableExternal(const CXXRecordDecl *RD);
145 
146  /// Returns the type of a vtable with the given layout. Normally a struct of
147  /// arrays of pointers, with one struct element for each vtable in the vtable
148  /// group.
149  llvm::Type *getVTableType(const VTableLayout &layout);
150 
151  /// Generate a public facing alias for the vtable and make the vtable either
152  /// hidden or private. The alias will have the original linkage and visibility
153  /// of the vtable. This is used for cases under the relative vtables ABI
154  /// when a vtable may not be dso_local.
155  void GenerateRelativeVTableAlias(llvm::GlobalVariable *VTable,
156  llvm::StringRef AliasNameRef);
157 };
158 
159 } // end namespace CodeGen
160 } // end namespace clang
161 #endif
clang::CodeGen::CodeGenVTables::GenerateRelativeVTableAlias
void GenerateRelativeVTableAlias(llvm::GlobalVariable *VTable, llvm::StringRef AliasNameRef)
Generate a public facing alias for the vtable and make the vtable either hidden or private.
Definition: CGVTables.cpp:934
clang::CodeGen::ConstantArrayBuilder
A helper class of ConstantInitBuilder, used for building constant array initializers.
Definition: ConstantInitBuilder.h:544
clang::CodeGen::CodeGenVTables::GenerateConstructionVTable
llvm::GlobalVariable * GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base, bool BaseIsVirtual, llvm::GlobalVariable::LinkageTypes Linkage, VTableAddressPointsMapTy &AddressPoints)
GenerateConstructionVTable - Generate a construction vtable for the given base subobject.
Definition: CGVTables.cpp:856
clang::CodeGen::CodeGenVTables::GetAddrOfVTT
llvm::GlobalVariable * GetAddrOfVTT(const CXXRecordDecl *RD)
GetAddrOfVTT - Get the address of the VTT for the given record decl.
Definition: CGVTT.cpp:104
clang::CodeGen::CodeGenVTables::CodeGenVTables
CodeGenVTables(CodeGenModule &CGM)
Definition: CGVTables.cpp:31
clang::CodeGen::ConstantStructBuilder
A helper class of ConstantInitBuilder, used for building constant struct initializers.
Definition: ConstantInitBuilder.h:561
ABI.h
clang::GlobalDecl
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:55
clang::CodeGen::CodeGenVTables::EmitThunks
void EmitThunks(GlobalDecl GD)
EmitThunks - Emit the associated thunks for the given global decl.
Definition: CGVTables.cpp:605
clang::Linkage
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have.
Definition: Linkage.h:23
clang::CodeGen::CodeGenVTables::getSecondaryVirtualPointerIndex
uint64_t getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD, BaseSubobject Base)
getSecondaryVirtualPointerIndex - Return the index in the VTT where the virtual pointer for the given...
Definition: CGVTT.cpp:154
clang::BaseSubobject
Definition: BaseSubobject.h:30
clang::CodeGen::CodeGenVTables::GenerateClassData
void GenerateClassData(const CXXRecordDecl *RD)
GenerateClassData - Generate all the class data required to be generated upon definition of a KeyFunc...
Definition: CGVTables.cpp:1091
clang::CodeGen::CodeGenVTables::getMicrosoftVTableContext
MicrosoftVTableContext & getMicrosoftVTableContext()
Definition: CGVTables.h:105
Base
clang::CodeGen::CodeGenVTables::createVTableInitializer
void createVTableInitializer(ConstantStructBuilder &builder, const VTableLayout &layout, llvm::Constant *rtti, bool vtableHasLocalLinkage)
Add vtable components for the given vtable layout to the given global initializer.
Definition: CGVTables.cpp:832
CharUnits.h
clang::VTableLayout::AddressPointsMapTy
llvm::DenseMap< BaseSubobject, AddressPointLocation > AddressPointsMapTy
Definition: VTableBuilder.h:240
clang::CodeGen::CodeGenVTables::EmitVTTDefinition
void EmitVTTDefinition(llvm::GlobalVariable *VTT, llvm::GlobalVariable::LinkageTypes Linkage, const CXXRecordDecl *RD)
EmitVTTDefinition - Emit the definition of the given vtable.
Definition: CGVTT.cpp:41
BaseSubobject.h
clang::CXXRecordDecl
Represents a C++ struct/union/class.
Definition: DeclCXX.h:255
VTableBuilder.h
clang::CodeGen::CodeGenModule
This class organizes the cross-function state that is used while generating LLVM code.
Definition: CodeGenModule.h:284
clang::ItaniumVTableContext
Definition: VTableBuilder.h:363
clang::ThunkInfo
The this pointer adjustment as well as an optional return adjustment for a thunk.
Definition: Thunk.h:156
clang::MicrosoftVTableContext
Definition: VTableBuilder.h:544
clang::CodeGen::CodeGenVTables::getSubVTTIndex
uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base)
getSubVTTIndex - Return the index of the sub-VTT for the base class of the given record decl.
Definition: CGVTT.cpp:128
clang::CodeGen::CodeGenVTables::getVTableType
llvm::Type * getVTableType(const VTableLayout &layout)
Returns the type of a vtable with the given layout.
Definition: CGVTables.cpp:823
clang::VTableLayout
Definition: VTableBuilder.h:233
clang
Definition: CalledOnceCheck.h:17
clang::CodeGen::CodeGenVTables
Definition: CGVTables.h:32
GlobalDecl.h
clang::CodeGen::CodeGenVTables::isVTableExternal
bool isVTableExternal(const CXXRecordDecl *RD)
At this point in the translation unit, does it appear that can we rely on the vtable being defined el...
Definition: CGVTables.cpp:1111
clang::CodeGen::CodeGenVTables::getItaniumVTableContext
ItaniumVTableContext & getItaniumVTableContext()
Definition: CGVTables.h:101
clang::VTableContextBase
Definition: VTableBuilder.h:321