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 /// Natural alignment of pragma-pack-resistant fields (vectors, x87 fp80),
88 /// or zero if none.
89 CharUnits PragmaPackResistantAlignment;
90
91 /// FieldOffsets - Array of field offsets in bits.
92 ASTVector<uint64_t> FieldOffsets;
93
94 /// CXXRecordLayoutInfo - Contains C++ specific layout information.
95 struct CXXRecordLayoutInfo {
96 /// NonVirtualSize - The non-virtual size (in chars) of an object, which is
97 /// the size of the object without virtual bases.
98 CharUnits NonVirtualSize;
99
100 /// NonVirtualAlignment - The non-virtual alignment (in chars) of an object,
101 /// which is the alignment of the object without virtual bases.
102 CharUnits NonVirtualAlignment;
103
104 /// NonRequiredNVAlignment - The non-virtual alignment (in chars) of an
105 /// object ignoring any over-alignment imposed via `alignas` /
106 /// `__declspec(align)` (i.e. the record's required alignment) on the record
107 /// or its bases.
108 CharUnits NonRequiredNVAlignment;
109
110 /// PreferredNVAlignment - The preferred non-virtual alignment (in chars) of
111 /// an object, which is the preferred alignment of the object without
112 /// virtual bases.
113 CharUnits PreferredNVAlignment;
114
115 /// SizeOfLargestEmptySubobject - The size of the largest empty subobject
116 /// (either a base or a member). Will be zero if the class doesn't contain
117 /// any empty subobjects.
118 CharUnits SizeOfLargestEmptySubobject;
119
120 /// VBPtrOffset - Virtual base table offset (Microsoft-only).
121 CharUnits VBPtrOffset;
122
123 /// HasOwnVFPtr - Does this class provide a virtual function table
124 /// (vtable in Itanium, vftbl in Microsoft) that is independent from
125 /// its base classes?
126 bool HasOwnVFPtr : 1;
127
128 /// HasVFPtr - Does this class have a vftable that could be extended by
129 /// a derived class. The class may have inherited this pointer from
130 /// a primary base class.
131 bool HasExtendableVFPtr : 1;
132
133 /// EndsWithZeroSizedObject - True if this class contains a zero sized
134 /// member or base or a base with a zero sized member or base.
135 /// Only used for MS-ABI.
136 bool EndsWithZeroSizedObject : 1;
137
138 /// True if this class is zero sized or first base is zero sized or
139 /// has this property. Only used for MS-ABI.
140 bool LeadsWithZeroSizedBase : 1;
141
142 /// PrimaryBase - The primary base info for this record.
143 llvm::PointerIntPair<const CXXRecordDecl *, 1, bool> PrimaryBase;
144
145 /// BaseSharingVBPtr - The base we share vbptr with.
146 const CXXRecordDecl *BaseSharingVBPtr;
147
148 /// FIXME: This should really use a SmallPtrMap, once we have one in LLVM :)
149 using BaseOffsetsMapTy = llvm::DenseMap<const CXXRecordDecl *, CharUnits>;
150
151 /// BaseOffsets - Contains a map from base classes to their offset.
152 BaseOffsetsMapTy BaseOffsets;
153
154 /// VBaseOffsets - Contains a map from vbase classes to their offset.
155 VBaseOffsetsMapTy VBaseOffsets;
156 };
157
158 /// CXXInfo - If the record layout is for a C++ record, this will have
159 /// C++ specific information about the record.
160 CXXRecordLayoutInfo *CXXInfo = nullptr;
161
162 ASTRecordLayout(const ASTContext &Ctx, CharUnits size, CharUnits alignment,
163 CharUnits preferredAlignment, CharUnits unadjustedAlignment,
164 CharUnits requiredAlignment,
165 CharUnits pragmaPackResistantAlignment, CharUnits datasize,
166 ArrayRef<uint64_t> fieldoffsets);
167
168 using BaseOffsetsMapTy = CXXRecordLayoutInfo::BaseOffsetsMapTy;
169
170 // Constructor for C++ records.
171 ASTRecordLayout(const ASTContext &Ctx, CharUnits size, CharUnits alignment,
172 CharUnits preferredAlignment, CharUnits unadjustedAlignment,
173 CharUnits requiredAlignment,
174 CharUnits pragmaPackResistantAlignment, bool hasOwnVFPtr,
175 bool hasExtendableVFPtr, CharUnits vbptroffset,
176 CharUnits datasize, ArrayRef<uint64_t> fieldoffsets,
177 CharUnits nonvirtualsize, CharUnits nonvirtualalignment,
178 CharUnits preferrednvalignment,
179 CharUnits nonrequirednvalignment,
180 CharUnits SizeOfLargestEmptySubobject,
181 const CXXRecordDecl *PrimaryBase, bool IsPrimaryBaseVirtual,
182 const CXXRecordDecl *BaseSharingVBPtr,
183 bool EndsWithZeroSizedObject, bool LeadsWithZeroSizedBase,
184 const BaseOffsetsMapTy &BaseOffsets,
185 const VBaseOffsetsMapTy &VBaseOffsets);
186
187 ~ASTRecordLayout() = default;
188
189 void Destroy(ASTContext &Ctx);
190
191public:
192 ASTRecordLayout(const ASTRecordLayout &) = delete;
193 ASTRecordLayout &operator=(const ASTRecordLayout &) = delete;
194
195 /// getAlignment - Get the record alignment in characters.
196 CharUnits getAlignment() const { return Alignment; }
197
198 /// getPreferredFieldAlignment - Get the record preferred alignment in
199 /// characters.
200 CharUnits getPreferredAlignment() const { return PreferredAlignment; }
201
202 /// getUnadjustedAlignment - Get the record alignment in characters, before
203 /// alignment adjustment.
204 CharUnits getUnadjustedAlignment() const { return UnadjustedAlignment; }
205
206 /// getSize - Get the record size in characters.
207 CharUnits getSize() const { return Size; }
208
209 /// getFieldCount - Get the number of fields in the layout.
210 unsigned getFieldCount() const { return FieldOffsets.size(); }
211
212 /// getFieldOffset - Get the offset of the given field index, in
213 /// bits.
214 uint64_t getFieldOffset(unsigned FieldNo) const {
215 return FieldOffsets[FieldNo];
216 }
217
218 /// getDataSize() - Get the record data size, which is the record size
219 /// without tail padding, in characters.
220 CharUnits getDataSize() const { return DataSize; }
221
222 /// getNonVirtualSize - Get the non-virtual size (in chars) of an object,
223 /// which is the size of the object without virtual bases.
225 assert(CXXInfo && "Record layout does not have C++ specific info!");
226
227 return CXXInfo->NonVirtualSize;
228 }
229
230 /// getNonVirtualAlignment - Get the non-virtual alignment (in chars) of an
231 /// object, which is the alignment of the object without virtual bases.
233 assert(CXXInfo && "Record layout does not have C++ specific info!");
234
235 return CXXInfo->NonVirtualAlignment;
236 }
237
238 /// getNonRequiredNVAlignment - Get the non-virtual alignment (in chars) of an
239 /// object ignoring over-alignment imposed via `alignas` / `__declspec(align)`
240 /// on the object or its bases. Only meaningful for the Microsoft ABI.
242 assert(CXXInfo && "Record layout does not have C++ specific info!");
243
244 return CXXInfo->NonRequiredNVAlignment;
245 }
246
247 /// getPreferredNVAlignment - Get the preferred non-virtual alignment (in
248 /// chars) of an object, which is the preferred alignment of the object
249 /// without virtual bases.
251 assert(CXXInfo && "Record layout does not have C++ specific info!");
252
253 return CXXInfo->PreferredNVAlignment;
254 }
255
256 /// getPrimaryBase - Get the primary base for this record.
258 assert(CXXInfo && "Record layout does not have C++ specific info!");
259
260 return CXXInfo->PrimaryBase.getPointer();
261 }
262
263 /// isPrimaryBaseVirtual - Get whether the primary base for this record
264 /// is virtual or not.
265 bool isPrimaryBaseVirtual() const {
266 assert(CXXInfo && "Record layout does not have C++ specific info!");
267
268 return CXXInfo->PrimaryBase.getInt();
269 }
270
271 /// getBaseClassOffset - Get the offset, in chars, for the given base class.
273 assert(CXXInfo && "Record layout does not have C++ specific info!");
274
275 Base = Base->getDefinition();
276 assert(CXXInfo->BaseOffsets.count(Base) && "Did not find base!");
277
278 return CXXInfo->BaseOffsets[Base];
279 }
280
281 /// getVBaseClassOffset - Get the offset, in chars, for the given base class.
283 assert(CXXInfo && "Record layout does not have C++ specific info!");
284
285 VBase = VBase->getDefinition();
286 assert(CXXInfo->VBaseOffsets.count(VBase) && "Did not find base!");
287
288 return CXXInfo->VBaseOffsets[VBase].VBaseOffset;
289 }
290
292 assert(CXXInfo && "Record layout does not have C++ specific info!");
293 return CXXInfo->SizeOfLargestEmptySubobject;
294 }
295
296 /// hasOwnVFPtr - Does this class provide its own virtual-function
297 /// table pointer, rather than inheriting one from a primary base
298 /// class? If so, it is at offset zero.
299 ///
300 /// This implies that the ABI has no primary base class, meaning
301 /// that it has no base classes that are suitable under the conditions
302 /// of the ABI.
303 bool hasOwnVFPtr() const {
304 assert(CXXInfo && "Record layout does not have C++ specific info!");
305 return CXXInfo->HasOwnVFPtr;
306 }
307
308 /// hasVFPtr - Does this class have a virtual function table pointer
309 /// that can be extended by a derived class? This is synonymous with
310 /// this class having a VFPtr at offset zero.
311 bool hasExtendableVFPtr() const {
312 assert(CXXInfo && "Record layout does not have C++ specific info!");
313 return CXXInfo->HasExtendableVFPtr;
314 }
315
316 /// hasOwnVBPtr - Does this class provide its own virtual-base
317 /// table pointer, rather than inheriting one from a primary base
318 /// class?
319 ///
320 /// This implies that the ABI has no primary base class, meaning
321 /// that it has no base classes that are suitable under the conditions
322 /// of the ABI.
323 bool hasOwnVBPtr() const {
324 assert(CXXInfo && "Record layout does not have C++ specific info!");
325 return hasVBPtr() && !CXXInfo->BaseSharingVBPtr;
326 }
327
328 /// hasVBPtr - Does this class have a virtual function table pointer.
329 bool hasVBPtr() const {
330 assert(CXXInfo && "Record layout does not have C++ specific info!");
331 return !CXXInfo->VBPtrOffset.isNegative();
332 }
333
334 CharUnits getRequiredAlignment() const { return RequiredAlignment; }
335
336 /// Get the natural alignment of pragma-pack-resistant fields.
338 return PragmaPackResistantAlignment;
339 }
340
342 return CXXInfo && CXXInfo->EndsWithZeroSizedObject;
343 }
344
346 assert(CXXInfo && "Record layout does not have C++ specific info!");
347 return CXXInfo->LeadsWithZeroSizedBase;
348 }
349
350 /// getVBPtrOffset - Get the offset for virtual base table pointer.
351 /// This is only meaningful with the Microsoft ABI.
353 assert(CXXInfo && "Record layout does not have C++ specific info!");
354 return CXXInfo->VBPtrOffset;
355 }
356
358 assert(CXXInfo && "Record layout does not have C++ specific info!");
359 return CXXInfo->BaseSharingVBPtr;
360 }
361
363 assert(CXXInfo && "Record layout does not have C++ specific info!");
364 return CXXInfo->VBaseOffsets;
365 }
366};
367
368} // namespace clang
369
370#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:245
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 getPragmaPackResistantAlignment() const
Get the natural alignment of pragma-pack-resistant fields.
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:549
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.