clang  10.0.0svn
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 idx,
66  llvm::Constant *rtti,
67  unsigned &nextVTableThunkIndex);
68 
69 public:
70  /// Add vtable components for the given vtable layout to the given
71  /// global initializer.
73  const VTableLayout &layout,
74  llvm::Constant *rtti);
75 
77 
79  return *cast<ItaniumVTableContext>(VTContext);
80  }
81 
83  return *cast<MicrosoftVTableContext>(VTContext);
84  }
85 
86  /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the
87  /// given record decl.
88  uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base);
89 
90  /// getSecondaryVirtualPointerIndex - Return the index in the VTT where the
91  /// virtual pointer for the given subobject is located.
93  BaseSubobject Base);
94 
95  /// GenerateConstructionVTable - Generate a construction vtable for the given
96  /// base subobject.
97  llvm::GlobalVariable *
99  bool BaseIsVirtual,
100  llvm::GlobalVariable::LinkageTypes Linkage,
101  VTableAddressPointsMapTy& AddressPoints);
102 
103 
104  /// GetAddrOfVTT - 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);
111 
112  /// EmitThunks - Emit the associated thunks for the given global decl.
113  void EmitThunks(GlobalDecl GD);
114 
115  /// 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);
122 
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);
127 };
128 
129 } // end namespace CodeGen
130 } // end namespace clang
131 #endif
void EmitVTTDefinition(llvm::GlobalVariable *VTT, llvm::GlobalVariable::LinkageTypes Linkage, const CXXRecordDecl *RD)
EmitVTTDefinition - Emit the definition of the given vtable.
Definition: CGVTT.cpp:41
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have...
Definition: Linkage.h:23
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:129
llvm::Type * getVTableType(const VTableLayout &layout)
Returns the type of a vtable with the given layout.
Definition: CGVTables.cpp:730
The this pointer adjustment as well as an optional return adjustment for a thunk. ...
Definition: ABI.h:178
Enums/classes describing ABI related information about constructors, destructors and thunks...
ItaniumVTableContext & getItaniumVTableContext()
Definition: CGVTables.h:78
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:943
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:40
The l-value was considered opaque, so the alignment was determined from a type.
llvm::GlobalVariable * GetAddrOfVTT(const CXXRecordDecl *RD)
GetAddrOfVTT - Get the address of the VTT for the given record decl.
Definition: CGVTT.cpp:105
CodeGenVTables(CodeGenModule &CGM)
Definition: CGVTables.cpp:30
void createVTableInitializer(ConstantStructBuilder &builder, const VTableLayout &layout, llvm::Constant *rtti)
Add vtable components for the given vtable layout to the given global initializer.
Definition: CGVTables.cpp:739
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:755
This class organizes the cross-function state that is used while generating LLVM code.
Dataflow Directional Tag Classes.
void EmitThunks(GlobalDecl GD)
EmitThunks - Emit the associated thunks for the given global decl.
Definition: CGVTables.cpp:600
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:155
void GenerateClassData(const CXXRecordDecl *RD)
GenerateClassData - Generate all the class data required to be generated upon definition of a KeyFunc...
Definition: CGVTables.cpp:923
MicrosoftVTableContext & getMicrosoftVTableContext()
Definition: CGVTables.h:82
llvm::DenseMap< BaseSubobject, AddressPointLocation > AddressPointsMapTy
Represents a C++ struct/union/class.
Definition: DeclCXX.h:255
A helper class of ConstantInitBuilder, used for building constant struct initializers.
A helper class of ConstantInitBuilder, used for building constant array initializers.