1 //===- RecordLayout.h - Layout information for a struct/union ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the RecordLayout interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_RECORDLAYOUT_H
15 #define LLVM_CLANG_AST_RECORDLAYOUT_H
17 #include "clang/AST/ASTVector.h"
18 #include "clang/AST/CharUnits.h"
19 #include "clang/AST/DeclCXX.h"
20 #include "clang/Basic/LLVM.h"
21 #include "llvm/ADT/ArrayRef.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/PointerIntPair.h"
33 /// This class contains layout information for one RecordDecl,
34 /// which is a struct/union/class. The decl represented must be a definition,
35 /// not a forward declaration.
36 /// This class is also used to contain layout information for one
37 /// ObjCInterfaceDecl. FIXME - Find appropriate name.
38 /// These objects are managed by ASTContext.
39 class ASTRecordLayout {
42 /// The offset to this virtual base in the complete-object layout
44 CharUnits VBaseOffset;
47 /// Whether this virtual base requires a vtordisp field in the
48 /// Microsoft ABI. These fields are required for certain operations
49 /// in constructors and destructors.
50 bool HasVtorDisp = false;
53 VBaseInfo() = default;
54 VBaseInfo(CharUnits VBaseOffset, bool hasVtorDisp)
55 : VBaseOffset(VBaseOffset), HasVtorDisp(hasVtorDisp) {}
57 bool hasVtorDisp() const { return HasVtorDisp; }
60 using VBaseOffsetsMapTy = llvm::DenseMap<const CXXRecordDecl *, VBaseInfo>;
63 friend class ASTContext;
65 /// Size - Size of record in characters.
68 /// DataSize - Size of record in characters without tail padding.
71 // Alignment - Alignment of record in characters.
74 // UnadjustedAlignment - Maximum of the alignments of the record members in
76 CharUnits UnadjustedAlignment;
78 /// RequiredAlignment - The required alignment of the object. In the MS-ABI
79 /// the __declspec(align()) trumps #pramga pack and must always be obeyed.
80 CharUnits RequiredAlignment;
82 /// FieldOffsets - Array of field offsets in bits.
83 ASTVector<uint64_t> FieldOffsets;
85 /// CXXRecordLayoutInfo - Contains C++ specific layout information.
86 struct CXXRecordLayoutInfo {
87 /// NonVirtualSize - The non-virtual size (in chars) of an object, which is
88 /// the size of the object without virtual bases.
89 CharUnits NonVirtualSize;
91 /// NonVirtualAlignment - The non-virtual alignment (in chars) of an object,
92 /// which is the alignment of the object without virtual bases.
93 CharUnits NonVirtualAlignment;
95 /// SizeOfLargestEmptySubobject - The size of the largest empty subobject
96 /// (either a base or a member). Will be zero if the class doesn't contain
97 /// any empty subobjects.
98 CharUnits SizeOfLargestEmptySubobject;
100 /// VBPtrOffset - Virtual base table offset (Microsoft-only).
101 CharUnits VBPtrOffset;
103 /// HasOwnVFPtr - Does this class provide a virtual function table
104 /// (vtable in Itanium, vftbl in Microsoft) that is independent from
105 /// its base classes?
106 bool HasOwnVFPtr : 1;
108 /// HasVFPtr - Does this class have a vftable that could be extended by
109 /// a derived class. The class may have inherited this pointer from
110 /// a primary base class.
111 bool HasExtendableVFPtr : 1;
113 /// EndsWithZeroSizedObject - True if this class contains a zero sized
114 /// member or base or a base with a zero sized member or base.
115 /// Only used for MS-ABI.
116 bool EndsWithZeroSizedObject : 1;
118 /// True if this class is zero sized or first base is zero sized or
119 /// has this property. Only used for MS-ABI.
120 bool LeadsWithZeroSizedBase : 1;
122 /// PrimaryBase - The primary base info for this record.
123 llvm::PointerIntPair<const CXXRecordDecl *, 1, bool> PrimaryBase;
125 /// BaseSharingVBPtr - The base we share vbptr with.
126 const CXXRecordDecl *BaseSharingVBPtr;
128 /// FIXME: This should really use a SmallPtrMap, once we have one in LLVM :)
129 using BaseOffsetsMapTy = llvm::DenseMap<const CXXRecordDecl *, CharUnits>;
131 /// BaseOffsets - Contains a map from base classes to their offset.
132 BaseOffsetsMapTy BaseOffsets;
134 /// VBaseOffsets - Contains a map from vbase classes to their offset.
135 VBaseOffsetsMapTy VBaseOffsets;
138 /// CXXInfo - If the record layout is for a C++ record, this will have
139 /// C++ specific information about the record.
140 CXXRecordLayoutInfo *CXXInfo = nullptr;
142 ASTRecordLayout(const ASTContext &Ctx, CharUnits size, CharUnits alignment,
143 CharUnits unadjustedAlignment,
144 CharUnits requiredAlignment, CharUnits datasize,
145 ArrayRef<uint64_t> fieldoffsets);
147 using BaseOffsetsMapTy = CXXRecordLayoutInfo::BaseOffsetsMapTy;
149 // Constructor for C++ records.
150 ASTRecordLayout(const ASTContext &Ctx,
151 CharUnits size, CharUnits alignment,
152 CharUnits unadjustedAlignment,
153 CharUnits requiredAlignment,
154 bool hasOwnVFPtr, bool hasExtendableVFPtr,
155 CharUnits vbptroffset,
157 ArrayRef<uint64_t> fieldoffsets,
158 CharUnits nonvirtualsize, CharUnits nonvirtualalignment,
159 CharUnits SizeOfLargestEmptySubobject,
160 const CXXRecordDecl *PrimaryBase,
161 bool IsPrimaryBaseVirtual,
162 const CXXRecordDecl *BaseSharingVBPtr,
163 bool EndsWithZeroSizedObject,
164 bool LeadsWithZeroSizedBase,
165 const BaseOffsetsMapTy& BaseOffsets,
166 const VBaseOffsetsMapTy& VBaseOffsets);
168 ~ASTRecordLayout() = default;
170 void Destroy(ASTContext &Ctx);
173 ASTRecordLayout(const ASTRecordLayout &) = delete;
174 ASTRecordLayout &operator=(const ASTRecordLayout &) = delete;
176 /// getAlignment - Get the record alignment in characters.
177 CharUnits getAlignment() const { return Alignment; }
179 /// getUnadjustedAlignment - Get the record alignment in characters, before
180 /// alignment adjustement.
181 CharUnits getUnadjustedAlignment() const { return UnadjustedAlignment; }
183 /// getSize - Get the record size in characters.
184 CharUnits getSize() const { return Size; }
186 /// getFieldCount - Get the number of fields in the layout.
187 unsigned getFieldCount() const { return FieldOffsets.size(); }
189 /// getFieldOffset - Get the offset of the given field index, in
191 uint64_t getFieldOffset(unsigned FieldNo) const {
192 return FieldOffsets[FieldNo];
195 /// getDataSize() - Get the record data size, which is the record size
196 /// without tail padding, in characters.
197 CharUnits getDataSize() const {
201 /// getNonVirtualSize - Get the non-virtual size (in chars) of an object,
202 /// which is the size of the object without virtual bases.
203 CharUnits getNonVirtualSize() const {
204 assert(CXXInfo && "Record layout does not have C++ specific info!");
206 return CXXInfo->NonVirtualSize;
209 /// getNonVirtualSize - Get the non-virtual alignment (in chars) of an object,
210 /// which is the alignment of the object without virtual bases.
211 CharUnits getNonVirtualAlignment() const {
212 assert(CXXInfo && "Record layout does not have C++ specific info!");
214 return CXXInfo->NonVirtualAlignment;
217 /// getPrimaryBase - Get the primary base for this record.
218 const CXXRecordDecl *getPrimaryBase() const {
219 assert(CXXInfo && "Record layout does not have C++ specific info!");
221 return CXXInfo->PrimaryBase.getPointer();
224 /// isPrimaryBaseVirtual - Get whether the primary base for this record
225 /// is virtual or not.
226 bool isPrimaryBaseVirtual() const {
227 assert(CXXInfo && "Record layout does not have C++ specific info!");
229 return CXXInfo->PrimaryBase.getInt();
232 /// getBaseClassOffset - Get the offset, in chars, for the given base class.
233 CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const {
234 assert(CXXInfo && "Record layout does not have C++ specific info!");
235 assert(CXXInfo->BaseOffsets.count(Base) && "Did not find base!");
237 return CXXInfo->BaseOffsets[Base];
240 /// getVBaseClassOffset - Get the offset, in chars, for the given base class.
241 CharUnits getVBaseClassOffset(const CXXRecordDecl *VBase) const {
242 assert(CXXInfo && "Record layout does not have C++ specific info!");
243 assert(CXXInfo->VBaseOffsets.count(VBase) && "Did not find base!");
245 return CXXInfo->VBaseOffsets[VBase].VBaseOffset;
248 CharUnits getSizeOfLargestEmptySubobject() const {
249 assert(CXXInfo && "Record layout does not have C++ specific info!");
250 return CXXInfo->SizeOfLargestEmptySubobject;
253 /// hasOwnVFPtr - Does this class provide its own virtual-function
254 /// table pointer, rather than inheriting one from a primary base
255 /// class? If so, it is at offset zero.
257 /// This implies that the ABI has no primary base class, meaning
258 /// that it has no base classes that are suitable under the conditions
260 bool hasOwnVFPtr() const {
261 assert(CXXInfo && "Record layout does not have C++ specific info!");
262 return CXXInfo->HasOwnVFPtr;
265 /// hasVFPtr - Does this class have a virtual function table pointer
266 /// that can be extended by a derived class? This is synonymous with
267 /// this class having a VFPtr at offset zero.
268 bool hasExtendableVFPtr() const {
269 assert(CXXInfo && "Record layout does not have C++ specific info!");
270 return CXXInfo->HasExtendableVFPtr;
273 /// hasOwnVBPtr - Does this class provide its own virtual-base
274 /// table pointer, rather than inheriting one from a primary base
277 /// This implies that the ABI has no primary base class, meaning
278 /// that it has no base classes that are suitable under the conditions
280 bool hasOwnVBPtr() const {
281 assert(CXXInfo && "Record layout does not have C++ specific info!");
282 return hasVBPtr() && !CXXInfo->BaseSharingVBPtr;
285 /// hasVBPtr - Does this class have a virtual function table pointer.
286 bool hasVBPtr() const {
287 assert(CXXInfo && "Record layout does not have C++ specific info!");
288 return !CXXInfo->VBPtrOffset.isNegative();
291 CharUnits getRequiredAlignment() const {
292 return RequiredAlignment;
295 bool endsWithZeroSizedObject() const {
296 return CXXInfo && CXXInfo->EndsWithZeroSizedObject;
299 bool leadsWithZeroSizedBase() const {
300 assert(CXXInfo && "Record layout does not have C++ specific info!");
301 return CXXInfo->LeadsWithZeroSizedBase;
304 /// getVBPtrOffset - Get the offset for virtual base table pointer.
305 /// This is only meaningful with the Microsoft ABI.
306 CharUnits getVBPtrOffset() const {
307 assert(CXXInfo && "Record layout does not have C++ specific info!");
308 return CXXInfo->VBPtrOffset;
311 const CXXRecordDecl *getBaseSharingVBPtr() const {
312 assert(CXXInfo && "Record layout does not have C++ specific info!");
313 return CXXInfo->BaseSharingVBPtr;
316 const VBaseOffsetsMapTy &getVBaseOffsetsMap() const {
317 assert(CXXInfo && "Record layout does not have C++ specific info!");
318 return CXXInfo->VBaseOffsets;
324 #endif // LLVM_CLANG_AST_RECORDLAYOUT_H