clang  19.0.0git
VTTBuilder.h
Go to the documentation of this file.
1 //===- VTTBuilder.h - C++ VTT layout builder --------------------*- 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 generation of the layout of virtual table
10 // tables (VTT).
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_AST_VTTBUILDER_H
15 #define LLVM_CLANG_AST_VTTBUILDER_H
16 
18 #include "clang/AST/CharUnits.h"
19 #include "clang/Basic/LLVM.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/PointerIntPair.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include <cstdint>
25 
26 namespace clang {
27 
28 class ASTContext;
29 class ASTRecordLayout;
30 class CXXRecordDecl;
31 
32 class VTTVTable {
33  llvm::PointerIntPair<const CXXRecordDecl *, 1, bool> BaseAndIsVirtual;
34  CharUnits BaseOffset;
35 
36 public:
37  VTTVTable() = default;
38  VTTVTable(const CXXRecordDecl *Base, CharUnits BaseOffset, bool BaseIsVirtual)
39  : BaseAndIsVirtual(Base, BaseIsVirtual), BaseOffset(BaseOffset) {}
40  VTTVTable(BaseSubobject Base, bool BaseIsVirtual)
41  : BaseAndIsVirtual(Base.getBase(), BaseIsVirtual),
42  BaseOffset(Base.getBaseOffset()) {}
43 
44  const CXXRecordDecl *getBase() const {
45  return BaseAndIsVirtual.getPointer();
46  }
47 
49  return BaseOffset;
50  }
51 
52  bool isVirtual() const {
53  return BaseAndIsVirtual.getInt();
54  }
55 
57  return BaseSubobject(getBase(), getBaseOffset());
58  }
59 };
60 
61 struct VTTComponent {
64 
65  VTTComponent() = default;
68 };
69 
70 /// Class for building VTT layout information.
71 class VTTBuilder {
72  ASTContext &Ctx;
73 
74  /// The most derived class for which we're building this vtable.
75  const CXXRecordDecl *MostDerivedClass;
76 
78 
79  /// The VTT vtables.
80  VTTVTablesVectorTy VTTVTables;
81 
83 
84  /// The VTT components.
85  VTTComponentsVectorTy VTTComponents;
86 
87  /// The AST record layout of the most derived class.
88  const ASTRecordLayout &MostDerivedClassLayout;
89 
91 
92  using AddressPointsMapTy = llvm::DenseMap<BaseSubobject, uint64_t>;
93 
94  /// The sub-VTT indices for the bases of the most derived class.
95  llvm::DenseMap<BaseSubobject, uint64_t> SubVTTIndices;
96 
97  /// The secondary virtual pointer indices of all subobjects of
98  /// the most derived class.
99  llvm::DenseMap<BaseSubobject, uint64_t> SecondaryVirtualPointerIndices;
100 
101  /// Whether the VTT builder should generate LLVM IR for the VTT.
102  bool GenerateDefinition;
103 
104  /// Add a vtable pointer to the VTT currently being built.
105  void AddVTablePointer(BaseSubobject Base, uint64_t VTableIndex,
106  const CXXRecordDecl *VTableClass);
107 
108  /// Lay out the secondary VTTs of the given base subobject.
109  void LayoutSecondaryVTTs(BaseSubobject Base);
110 
111  /// Lay out the secondary virtual pointers for the given base
112  /// subobject.
113  ///
114  /// \param BaseIsMorallyVirtual whether the base subobject is a virtual base
115  /// or a direct or indirect base of a virtual base.
116  void LayoutSecondaryVirtualPointers(BaseSubobject Base,
117  bool BaseIsMorallyVirtual,
118  uint64_t VTableIndex,
119  const CXXRecordDecl *VTableClass,
120  VisitedVirtualBasesSetTy &VBases);
121 
122  /// Lay out the secondary virtual pointers for the given base
123  /// subobject.
124  void LayoutSecondaryVirtualPointers(BaseSubobject Base,
125  uint64_t VTableIndex);
126 
127  /// Lay out the VTTs for the virtual base classes of the given
128  /// record declaration.
129  void LayoutVirtualVTTs(const CXXRecordDecl *RD,
130  VisitedVirtualBasesSetTy &VBases);
131 
132  /// Lay out the VTT for the given subobject, including any
133  /// secondary VTTs, secondary virtual pointers and virtual VTTs.
134  void LayoutVTT(BaseSubobject Base, bool BaseIsVirtual);
135 
136 public:
137  VTTBuilder(ASTContext &Ctx, const CXXRecordDecl *MostDerivedClass,
138  bool GenerateDefinition);
139 
140  // Returns a reference to the VTT components.
142  return VTTComponents;
143  }
144 
145  // Returns a reference to the VTT vtables.
147  return VTTVTables;
148  }
149 
150  /// Returns a reference to the sub-VTT indices.
151  const llvm::DenseMap<BaseSubobject, uint64_t> &getSubVTTIndices() const {
152  return SubVTTIndices;
153  }
154 
155  /// Returns a reference to the secondary virtual pointer indices.
156  const llvm::DenseMap<BaseSubobject, uint64_t> &
158  return SecondaryVirtualPointerIndices;
159  }
160 };
161 
162 } // namespace clang
163 
164 #endif // LLVM_CLANG_AST_VTTBUILDER_H
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:185
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
Definition: RecordLayout.h:38
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
Class for building VTT layout information.
Definition: VTTBuilder.h:71
const VTTVTablesVectorTy & getVTTVTables() const
Definition: VTTBuilder.h:146
VTTBuilder(ASTContext &Ctx, const CXXRecordDecl *MostDerivedClass, bool GenerateDefinition)
Definition: VTTBuilder.cpp:31
const llvm::DenseMap< BaseSubobject, uint64_t > & getSubVTTIndices() const
Returns a reference to the sub-VTT indices.
Definition: VTTBuilder.h:151
const llvm::DenseMap< BaseSubobject, uint64_t > & getSecondaryVirtualPointerIndices() const
Returns a reference to the secondary virtual pointer indices.
Definition: VTTBuilder.h:157
const VTTComponentsVectorTy & getVTTComponents() const
Definition: VTTBuilder.h:141
VTTVTable(BaseSubobject Base, bool BaseIsVirtual)
Definition: VTTBuilder.h:40
const CXXRecordDecl * getBase() const
Definition: VTTBuilder.h:44
VTTVTable(const CXXRecordDecl *Base, CharUnits BaseOffset, bool BaseIsVirtual)
Definition: VTTBuilder.h:38
CharUnits getBaseOffset() const
Definition: VTTBuilder.h:48
bool isVirtual() const
Definition: VTTBuilder.h:52
VTTVTable()=default
BaseSubobject getBaseSubobject() const
Definition: VTTBuilder.h:56
The JSON file list parser is used to communicate input to InstallAPI.
unsigned long uint64_t
VTTComponent()=default
uint64_t VTableIndex
Definition: VTTBuilder.h:62
BaseSubobject VTableBase
Definition: VTTBuilder.h:63
VTTComponent(uint64_t VTableIndex, BaseSubobject VTableBase)
Definition: VTTBuilder.h:66