clang 24.0.0git
RecordLayout.h
Go to the documentation of this file.
1//===- RecordLayout.h - Layout information for a struct/union ---*- 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 file defines the RecordLayout interface.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_AST_RECORDLAYOUT_H
14#define LLVM_CLANG_AST_RECORDLAYOUT_H
15
16#include "clang/AST/ASTVector.h"
17#include "clang/AST/CharUnits.h"
18#include "clang/AST/DeclCXX.h"
19#include "clang/Basic/LLVM.h"
20#include "llvm/ADT/ArrayRef.h"
21#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/PointerIntPair.h"
23#include <cassert>
24#include <cstdint>
25
26namespace clang {
27
28class ASTContext;
29class CXXRecordDecl;
30
31/// ASTRecordLayout -
32/// This class contains layout information for one RecordDecl,
33/// which is a struct/union/class. The decl represented must be a definition,
34/// not a forward declaration.
35/// This class is also used to contain layout information for one
36/// ObjCInterfaceDecl. FIXME - Find appropriate name.
37/// These objects are managed by ASTContext.
38class ASTRecordLayout {
39public:
40 struct VBaseInfo {
41 /// The offset to this virtual base in the complete-object layout
42 /// of this class.
44
45 private:
46 /// Whether this virtual base requires a vtordisp field in the
47 /// Microsoft ABI. These fields are required for certain operations
48 /// in constructors and destructors.
49 bool HasVtorDisp = false;
50
51 public:
52 VBaseInfo() = default;
55
56 bool hasVtorDisp() const { return HasVtorDisp; }
57 };
58
59 using VBaseOffsetsMapTy = llvm::DenseMap<const CXXRecordDecl *, VBaseInfo>;
60
61private:
62 friend class ASTContext;
63
64 /// Size - Size of record in characters.
65 CharUnits Size;
66
67 /// DataSize - Size of record in characters without tail padding.
68 CharUnits DataSize;
69
70 // Alignment - Alignment of record in characters.
71 CharUnits Alignment;
72
73 // PreferredAlignment - Preferred alignment of record in characters. This
74 // can be different than Alignment in cases where it is beneficial for
75 // performance or backwards compatibility preserving (e.g. AIX-ABI).
76 CharUnits PreferredAlignment;
77
78 // UnadjustedAlignment - Alignment of record in characters before alignment
79 // adjustments. Maximum of the alignments of the record members and base
80 // classes in characters.
81 CharUnits UnadjustedAlignment;
82
83 /// RequiredAlignment - The required alignment of the object. In the MS-ABI
84 /// the __declspec(align()) trumps #pramga pack and must always be obeyed.
85 CharUnits RequiredAlignment;
86
87 /// FieldOffsets - Array of field offsets in bits.
88 ASTVector<uint64_t> FieldOffsets;
89
90 /// CXXRecordLayoutInfo - Contains C++ specific layout information.
91 struct CXXRecordLayoutInfo {
92 /// NonVirtualSize - The non-virtual size (in chars) of an object, which is
93 /// the size of the object without virtual bases.
94 CharUnits NonVirtualSize;
95
96 /// NonVirtualAlignment - The non-virtual alignment (in chars) of an object,
97 /// which is the alignment of the object without virtual bases.
98 CharUnits NonVirtualAlignment;
99
100 /// NonRequiredNVAlignment - The non-virtual alignment (in chars) of an
101 /// object ignoring any over-alignment imposed via `alignas` /
102 /// `__declspec(align)` (i.e. the record's required alignment) on the record
103 /// or its bases.
104 CharUnits NonRequiredNVAlignment;
105
106 /// PreferredNVAlignment - The preferred non-virtual alignment (in chars) of
107 /// an object, which is the preferred alignment of the object without
108 /// virtual bases.
109 CharUnits PreferredNVAlignment;
110
111 /// SizeOfLargestEmptySubobject - The size of the largest empty subobject
112 /// (either a base or a member). Will be zero if the class doesn't contain
113 /// any empty subobjects.
114 CharUnits SizeOfLargestEmptySubobject;
115
116 /// VBPtrOffset - Virtual base table offset (Microsoft-only).
117 CharUnits VBPtrOffset;
118
119 /// HasOwnVFPtr - Does this class provide a virtual function table
120 /// (vtable in Itanium, vftbl in Microsoft) that is independent from
121 /// its base classes?
122 bool HasOwnVFPtr : 1;
123
124 /// HasVFPtr - Does this class have a vftable that could be extended by
125 /// a derived class. The class may have inherited this pointer from
126 /// a primary base class.
127 bool HasExtendableVFPtr : 1;
128
129 /// EndsWithZeroSizedObject - True if this class contains a zero sized
130 /// member or base or a base with a zero sized member or base.
131 /// Only used for MS-ABI.
132 bool EndsWithZeroSizedObject : 1;
133
134 /// True if this class is zero sized or first base is zero sized or
135 /// has this property. Only used for MS-ABI.
136 bool LeadsWithZeroSizedBase : 1;
137
138 /// PrimaryBase - The primary base info for this record.
139 llvm::PointerIntPair<const CXXRecordDecl *, 1, bool> PrimaryBase;
140
141 /// BaseSharingVBPtr - The base we share vbptr with.
142 const CXXRecordDecl *BaseSharingVBPtr;
143
144 /// FIXME: This should really use a SmallPtrMap, once we have one in LLVM :)
145 using BaseOffsetsMapTy = llvm::DenseMap<const CXXRecordDecl *, CharUnits>;
146
147 /// BaseOffsets - Contains a map from base classes to their offset.
148 BaseOffsetsMapTy BaseOffsets;
149
150 /// VBaseOffsets - Contains a map from vbase classes to their offset.
151 VBaseOffsetsMapTy VBaseOffsets;
152 };
153
154 /// CXXInfo - If the record layout is for a C++ record, this will have
155 /// C++ specific information about the record.
156 CXXRecordLayoutInfo *CXXInfo = nullptr;
157
158 ASTRecordLayout(const ASTContext &Ctx, CharUnits size, CharUnits alignment,
159 CharUnits preferredAlignment, CharUnits unadjustedAlignment,
160 CharUnits requiredAlignment, CharUnits datasize,
161 ArrayRef<uint64_t> fieldoffsets);
162
163 using BaseOffsetsMapTy = CXXRecordLayoutInfo::BaseOffsetsMapTy;
164
165 // Constructor for C++ records.
166 ASTRecordLayout(const ASTContext &Ctx, CharUnits size, CharUnits alignment,
167 CharUnits preferredAlignment, CharUnits unadjustedAlignment,
168 CharUnits requiredAlignment, bool hasOwnVFPtr,
169 bool hasExtendableVFPtr, CharUnits vbptroffset,
170 CharUnits datasize, ArrayRef<uint64_t> fieldoffsets,
171 CharUnits nonvirtualsize, CharUnits nonvirtualalignment,
172 CharUnits preferrednvalignment,
173 CharUnits nonrequirednvalignment,
174 CharUnits SizeOfLargestEmptySubobject,
175 const CXXRecordDecl *PrimaryBase, bool IsPrimaryBaseVirtual,
176 const CXXRecordDecl *BaseSharingVBPtr,
177 bool EndsWithZeroSizedObject, bool LeadsWithZeroSizedBase,
178 const BaseOffsetsMapTy &BaseOffsets,
179 const VBaseOffsetsMapTy &VBaseOffsets);
180
181 ~ASTRecordLayout() = default;
182
183 void Destroy(ASTContext &Ctx);
184
185public:
186 ASTRecordLayout(const ASTRecordLayout &) = delete;
187 ASTRecordLayout &operator=(const ASTRecordLayout &) = delete;
188
189 /// getAlignment - Get the record alignment in characters.
190 CharUnits getAlignment() const { return Alignment; }
191
192 /// getPreferredFieldAlignment - Get the record preferred alignment in
193 /// characters.
194 CharUnits getPreferredAlignment() const { return PreferredAlignment; }
195
196 /// getUnadjustedAlignment - Get the record alignment in characters, before
197 /// alignment adjustment.
198 CharUnits getUnadjustedAlignment() const { return UnadjustedAlignment; }
199
200 /// getSize - Get the record size in characters.
201 CharUnits getSize() const { return Size; }
202
203 /// getFieldCount - Get the number of fields in the layout.
204 unsigned getFieldCount() const { return FieldOffsets.size(); }
205
206 /// getFieldOffset - Get the offset of the given field index, in
207 /// bits.
208 uint64_t getFieldOffset(unsigned FieldNo) const {
209 return FieldOffsets[FieldNo];
210 }
211
212 /// getDataSize() - Get the record data size, which is the record size
213 /// without tail padding, in characters.
214 CharUnits getDataSize() const { return DataSize; }
215
216 /// getNonVirtualSize - Get the non-virtual size (in chars) of an object,
217 /// which is the size of the object without virtual bases.
219 assert(CXXInfo && "Record layout does not have C++ specific info!");
220
221 return CXXInfo->NonVirtualSize;
222 }
223
224 /// getNonVirtualAlignment - Get the non-virtual alignment (in chars) of an
225 /// object, which is the alignment of the object without virtual bases.
227 assert(CXXInfo && "Record layout does not have C++ specific info!");
228
229 return CXXInfo->NonVirtualAlignment;
230 }
231
232 /// getNonRequiredNVAlignment - Get the non-virtual alignment (in chars) of an
233 /// object ignoring over-alignment imposed via `alignas` / `__declspec(align)`
234 /// on the object or its bases. Only meaningful for the Microsoft ABI.
236 assert(CXXInfo && "Record layout does not have C++ specific info!");
237
238 return CXXInfo->NonRequiredNVAlignment;
239 }
240
241 /// getPreferredNVAlignment - Get the preferred non-virtual alignment (in
242 /// chars) of an object, which is the preferred alignment of the object
243 /// without virtual bases.
245 assert(CXXInfo && "Record layout does not have C++ specific info!");
246
247 return CXXInfo->PreferredNVAlignment;
248 }
249
250 /// getPrimaryBase - Get the primary base for this record.
252 assert(CXXInfo && "Record layout does not have C++ specific info!");
253
254 return CXXInfo->PrimaryBase.getPointer();
255 }
256
257 /// isPrimaryBaseVirtual - Get whether the primary base for this record
258 /// is virtual or not.
259 bool isPrimaryBaseVirtual() const {
260 assert(CXXInfo && "Record layout does not have C++ specific info!");
261
262 return CXXInfo->PrimaryBase.getInt();
263 }
264
265 /// getBaseClassOffset - Get the offset, in chars, for the given base class.
267 assert(CXXInfo && "Record layout does not have C++ specific info!");
268
269 Base = Base->getDefinition();
270 assert(CXXInfo->BaseOffsets.count(Base) && "Did not find base!");
271
272 return CXXInfo->BaseOffsets[Base];
273 }
274
275 /// getVBaseClassOffset - Get the offset, in chars, for the given base class.
277 assert(CXXInfo && "Record layout does not have C++ specific info!");
278
279 VBase = VBase->getDefinition();
280 assert(CXXInfo->VBaseOffsets.count(VBase) && "Did not find base!");
281
282 return CXXInfo->VBaseOffsets[VBase].VBaseOffset;
283 }
284
286 assert(CXXInfo && "Record layout does not have C++ specific info!");
287 return CXXInfo->SizeOfLargestEmptySubobject;
288 }
289
290 /// hasOwnVFPtr - Does this class provide its own virtual-function
291 /// table pointer, rather than inheriting one from a primary base
292 /// class? If so, it is at offset zero.
293 ///
294 /// This implies that the ABI has no primary base class, meaning
295 /// that it has no base classes that are suitable under the conditions
296 /// of the ABI.
297 bool hasOwnVFPtr() const {
298 assert(CXXInfo && "Record layout does not have C++ specific info!");
299 return CXXInfo->HasOwnVFPtr;
300 }
301
302 /// hasVFPtr - Does this class have a virtual function table pointer
303 /// that can be extended by a derived class? This is synonymous with
304 /// this class having a VFPtr at offset zero.
305 bool hasExtendableVFPtr() const {
306 assert(CXXInfo && "Record layout does not have C++ specific info!");
307 return CXXInfo->HasExtendableVFPtr;
308 }
309
310 /// hasOwnVBPtr - Does this class provide its own virtual-base
311 /// table pointer, rather than inheriting one from a primary base
312 /// class?
313 ///
314 /// This implies that the ABI has no primary base class, meaning
315 /// that it has no base classes that are suitable under the conditions
316 /// of the ABI.
317 bool hasOwnVBPtr() const {
318 assert(CXXInfo && "Record layout does not have C++ specific info!");
319 return hasVBPtr() && !CXXInfo->BaseSharingVBPtr;
320 }
321
322 /// hasVBPtr - Does this class have a virtual function table pointer.
323 bool hasVBPtr() const {
324 assert(CXXInfo && "Record layout does not have C++ specific info!");
325 return !CXXInfo->VBPtrOffset.isNegative();
326 }
327
328 CharUnits getRequiredAlignment() const { return RequiredAlignment; }
329
331 return CXXInfo && CXXInfo->EndsWithZeroSizedObject;
332 }
333
335 assert(CXXInfo && "Record layout does not have C++ specific info!");
336 return CXXInfo->LeadsWithZeroSizedBase;
337 }
338
339 /// getVBPtrOffset - Get the offset for virtual base table pointer.
340 /// This is only meaningful with the Microsoft ABI.
342 assert(CXXInfo && "Record layout does not have C++ specific info!");
343 return CXXInfo->VBPtrOffset;
344 }
345
347 assert(CXXInfo && "Record layout does not have C++ specific info!");
348 return CXXInfo->BaseSharingVBPtr;
349 }
350
352 assert(CXXInfo && "Record layout does not have C++ specific info!");
353 return CXXInfo->VBaseOffsets;
354 }
355};
356
357} // namespace clang
358
359#endif // LLVM_CLANG_AST_RECORDLAYOUT_H
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
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:223
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
bool endsWithZeroSizedObject() const
bool hasOwnVFPtr() const
hasOwnVFPtr - Does this class provide its own virtual-function table pointer, rather than inheriting ...
CharUnits getAlignment() const
getAlignment - Get the record alignment in characters.
const CXXRecordDecl * getBaseSharingVBPtr() const
CharUnits getPreferredAlignment() const
getPreferredFieldAlignment - Get the record preferred alignment in characters.
bool hasOwnVBPtr() const
hasOwnVBPtr - Does this class provide its own virtual-base table pointer, rather than inheriting one ...
llvm::DenseMap< const CXXRecordDecl *, VBaseInfo > VBaseOffsetsMapTy
ASTRecordLayout & operator=(const ASTRecordLayout &)=delete
CharUnits getNonRequiredNVAlignment() const
getNonRequiredNVAlignment - Get the non-virtual alignment (in chars) of an object ignoring over-align...
CharUnits getSize() const
getSize - Get the record size in characters.
unsigned getFieldCount() const
getFieldCount - Get the number of fields in the layout.
bool hasVBPtr() const
hasVBPtr - Does this class have a virtual function table pointer.
friend class ASTContext
bool leadsWithZeroSizedBase() const
uint64_t getFieldOffset(unsigned FieldNo) const
getFieldOffset - Get the offset of the given field index, in bits.
CharUnits getNonVirtualAlignment() const
getNonVirtualAlignment - Get the non-virtual alignment (in chars) of an object, which is the alignmen...
CharUnits getVBPtrOffset() const
getVBPtrOffset - Get the offset for virtual base table pointer.
CharUnits getDataSize() const
getDataSize() - Get the record data size, which is the record size without tail padding,...
ASTRecordLayout(const ASTRecordLayout &)=delete
CharUnits getRequiredAlignment() const
CharUnits getSizeOfLargestEmptySubobject() const
CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const
getBaseClassOffset - Get the offset, in chars, for the given base class.
CharUnits getPreferredNVAlignment() const
getPreferredNVAlignment - Get the preferred non-virtual alignment (in chars) of an object,...
CharUnits getVBaseClassOffset(const CXXRecordDecl *VBase) const
getVBaseClassOffset - Get the offset, in chars, for the given base class.
const VBaseOffsetsMapTy & getVBaseOffsetsMap() const
const CXXRecordDecl * getPrimaryBase() const
getPrimaryBase - Get the primary base for this record.
bool hasExtendableVFPtr() const
hasVFPtr - Does this class have a virtual function table pointer that can be extended by a derived cl...
bool isPrimaryBaseVirtual() const
isPrimaryBaseVirtual - Get whether the primary base for this record is virtual or not.
CharUnits getNonVirtualSize() const
getNonVirtualSize - Get the non-virtual size (in chars) of an object, which is the size of the object...
CharUnits getUnadjustedAlignment() const
getUnadjustedAlignment - Get the record alignment in characters, before alignment adjustment.
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
CXXRecordDecl * getDefinition() const
Definition DeclCXX.h:548
CharUnits - This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
Top level wrappers for InstallAPI frontend operations.
VBaseInfo(CharUnits VBaseOffset, bool hasVtorDisp)
CharUnits VBaseOffset
The offset to this virtual base in the complete-object layout of this class.