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/CharUnits.h"
18 #include "clang/AST/DeclCXX.h"
19 #include "llvm/ADT/DenseMap.h"
28 /// This class contains layout information for one RecordDecl,
29 /// which is a struct/union/class. The decl represented must be a definition,
30 /// not a forward declaration.
31 /// This class is also used to contain layout information for one
32 /// ObjCInterfaceDecl. FIXME - Find appropriate name.
33 /// These objects are managed by ASTContext.
34 class ASTRecordLayout {
37 /// The offset to this virtual base in the complete-object layout
39 CharUnits VBaseOffset;
42 /// Whether this virtual base requires a vtordisp field in the
43 /// Microsoft ABI. These fields are required for certain operations
44 /// in constructors and destructors.
48 bool hasVtorDisp() const { return HasVtorDisp; }
50 VBaseInfo() : HasVtorDisp(false) {}
52 VBaseInfo(CharUnits VBaseOffset, bool hasVtorDisp) :
53 VBaseOffset(VBaseOffset), HasVtorDisp(hasVtorDisp) {}
56 typedef llvm::DenseMap<const CXXRecordDecl *, VBaseInfo>
60 /// Size - Size of record in characters.
63 /// DataSize - Size of record in characters without tail padding.
66 // Alignment - Alignment of record in characters.
69 /// RequiredAlignment - The required alignment of the object. In the MS-ABI
70 /// the __declspec(align()) trumps #pramga pack and must always be obeyed.
71 CharUnits RequiredAlignment;
73 /// FieldOffsets - Array of field offsets in bits.
74 ASTVector<uint64_t> FieldOffsets;
76 /// CXXRecordLayoutInfo - Contains C++ specific layout information.
77 struct CXXRecordLayoutInfo {
78 /// NonVirtualSize - The non-virtual size (in chars) of an object, which is
79 /// the size of the object without virtual bases.
80 CharUnits NonVirtualSize;
82 /// NonVirtualAlignment - The non-virtual alignment (in chars) of an object,
83 /// which is the alignment of the object without virtual bases.
84 CharUnits NonVirtualAlignment;
86 /// SizeOfLargestEmptySubobject - The size of the largest empty subobject
87 /// (either a base or a member). Will be zero if the class doesn't contain
88 /// any empty subobjects.
89 CharUnits SizeOfLargestEmptySubobject;
91 /// VBPtrOffset - Virtual base table offset (Microsoft-only).
92 CharUnits VBPtrOffset;
94 /// HasOwnVFPtr - Does this class provide a virtual function table
95 /// (vtable in Itanium, vftbl in Microsoft) that is independent from
99 /// HasVFPtr - Does this class have a vftable that could be extended by
100 /// a derived class. The class may have inherited this pointer from
101 /// a primary base class.
102 bool HasExtendableVFPtr : 1;
104 /// EndsWithZeroSizedObject - True if this class contains a zero sized
105 /// member or base or a base with a zero sized member or base.
106 /// Only used for MS-ABI.
107 bool EndsWithZeroSizedObject : 1;
109 /// \brief True if this class is zero sized or first base is zero sized or
110 /// has this property. Only used for MS-ABI.
111 bool LeadsWithZeroSizedBase : 1;
113 /// PrimaryBase - The primary base info for this record.
114 llvm::PointerIntPair<const CXXRecordDecl *, 1, bool> PrimaryBase;
116 /// BaseSharingVBPtr - The base we share vbptr with.
117 const CXXRecordDecl *BaseSharingVBPtr;
119 /// FIXME: This should really use a SmallPtrMap, once we have one in LLVM :)
120 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetsMapTy;
122 /// BaseOffsets - Contains a map from base classes to their offset.
123 BaseOffsetsMapTy BaseOffsets;
125 /// VBaseOffsets - Contains a map from vbase classes to their offset.
126 VBaseOffsetsMapTy VBaseOffsets;
129 /// CXXInfo - If the record layout is for a C++ record, this will have
130 /// C++ specific information about the record.
131 CXXRecordLayoutInfo *CXXInfo;
133 friend class ASTContext;
135 ASTRecordLayout(const ASTContext &Ctx, CharUnits size, CharUnits alignment,
136 CharUnits requiredAlignment, CharUnits datasize,
137 ArrayRef<uint64_t> fieldoffsets);
139 // Constructor for C++ records.
140 typedef CXXRecordLayoutInfo::BaseOffsetsMapTy BaseOffsetsMapTy;
141 ASTRecordLayout(const ASTContext &Ctx,
142 CharUnits size, CharUnits alignment,
143 CharUnits requiredAlignment,
144 bool hasOwnVFPtr, bool hasExtendableVFPtr,
145 CharUnits vbptroffset,
147 ArrayRef<uint64_t> fieldoffsets,
148 CharUnits nonvirtualsize, CharUnits nonvirtualalignment,
149 CharUnits SizeOfLargestEmptySubobject,
150 const CXXRecordDecl *PrimaryBase,
151 bool IsPrimaryBaseVirtual,
152 const CXXRecordDecl *BaseSharingVBPtr,
153 bool EndsWithZeroSizedObject,
154 bool LeadsWithZeroSizedBase,
155 const BaseOffsetsMapTy& BaseOffsets,
156 const VBaseOffsetsMapTy& VBaseOffsets);
158 ~ASTRecordLayout() = default;
160 void Destroy(ASTContext &Ctx);
162 ASTRecordLayout(const ASTRecordLayout &) = delete;
163 void operator=(const ASTRecordLayout &) = delete;
166 /// getAlignment - Get the record alignment in characters.
167 CharUnits getAlignment() const { return Alignment; }
169 /// getSize - Get the record size in characters.
170 CharUnits getSize() const { return Size; }
172 /// getFieldCount - Get the number of fields in the layout.
173 unsigned getFieldCount() const { return FieldOffsets.size(); }
175 /// getFieldOffset - Get the offset of the given field index, in
177 uint64_t getFieldOffset(unsigned FieldNo) const {
178 return FieldOffsets[FieldNo];
181 /// getDataSize() - Get the record data size, which is the record size
182 /// without tail padding, in characters.
183 CharUnits getDataSize() const {
187 /// getNonVirtualSize - Get the non-virtual size (in chars) of an object,
188 /// which is the size of the object without virtual bases.
189 CharUnits getNonVirtualSize() const {
190 assert(CXXInfo && "Record layout does not have C++ specific info!");
192 return CXXInfo->NonVirtualSize;
195 /// getNonVirtualSize - Get the non-virtual alignment (in chars) of an object,
196 /// which is the alignment of the object without virtual bases.
197 CharUnits getNonVirtualAlignment() const {
198 assert(CXXInfo && "Record layout does not have C++ specific info!");
200 return CXXInfo->NonVirtualAlignment;
203 /// getPrimaryBase - Get the primary base for this record.
204 const CXXRecordDecl *getPrimaryBase() const {
205 assert(CXXInfo && "Record layout does not have C++ specific info!");
207 return CXXInfo->PrimaryBase.getPointer();
210 /// isPrimaryBaseVirtual - Get whether the primary base for this record
211 /// is virtual or not.
212 bool isPrimaryBaseVirtual() const {
213 assert(CXXInfo && "Record layout does not have C++ specific info!");
215 return CXXInfo->PrimaryBase.getInt();
218 /// getBaseClassOffset - Get the offset, in chars, for the given base class.
219 CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const {
220 assert(CXXInfo && "Record layout does not have C++ specific info!");
221 assert(CXXInfo->BaseOffsets.count(Base) && "Did not find base!");
223 return CXXInfo->BaseOffsets[Base];
226 /// getVBaseClassOffset - Get the offset, in chars, for the given base class.
227 CharUnits getVBaseClassOffset(const CXXRecordDecl *VBase) const {
228 assert(CXXInfo && "Record layout does not have C++ specific info!");
229 assert(CXXInfo->VBaseOffsets.count(VBase) && "Did not find base!");
231 return CXXInfo->VBaseOffsets[VBase].VBaseOffset;
234 CharUnits getSizeOfLargestEmptySubobject() const {
235 assert(CXXInfo && "Record layout does not have C++ specific info!");
236 return CXXInfo->SizeOfLargestEmptySubobject;
239 /// hasOwnVFPtr - Does this class provide its own virtual-function
240 /// table pointer, rather than inheriting one from a primary base
241 /// class? If so, it is at offset zero.
243 /// This implies that the ABI has no primary base class, meaning
244 /// that it has no base classes that are suitable under the conditions
246 bool hasOwnVFPtr() const {
247 assert(CXXInfo && "Record layout does not have C++ specific info!");
248 return CXXInfo->HasOwnVFPtr;
251 /// hasVFPtr - Does this class have a virtual function table pointer
252 /// that can be extended by a derived class? This is synonymous with
253 /// this class having a VFPtr at offset zero.
254 bool hasExtendableVFPtr() const {
255 assert(CXXInfo && "Record layout does not have C++ specific info!");
256 return CXXInfo->HasExtendableVFPtr;
259 /// hasOwnVBPtr - Does this class provide its own virtual-base
260 /// table pointer, rather than inheriting one from a primary base
263 /// This implies that the ABI has no primary base class, meaning
264 /// that it has no base classes that are suitable under the conditions
266 bool hasOwnVBPtr() const {
267 assert(CXXInfo && "Record layout does not have C++ specific info!");
268 return hasVBPtr() && !CXXInfo->BaseSharingVBPtr;
271 /// hasVBPtr - Does this class have a virtual function table pointer.
272 bool hasVBPtr() const {
273 assert(CXXInfo && "Record layout does not have C++ specific info!");
274 return !CXXInfo->VBPtrOffset.isNegative();
277 CharUnits getRequiredAlignment() const {
278 return RequiredAlignment;
281 bool endsWithZeroSizedObject() const {
282 return CXXInfo && CXXInfo->EndsWithZeroSizedObject;
285 bool leadsWithZeroSizedBase() const {
286 assert(CXXInfo && "Record layout does not have C++ specific info!");
287 return CXXInfo->LeadsWithZeroSizedBase;
290 /// getVBPtrOffset - Get the offset for virtual base table pointer.
291 /// This is only meaningful with the Microsoft ABI.
292 CharUnits getVBPtrOffset() const {
293 assert(CXXInfo && "Record layout does not have C++ specific info!");
294 return CXXInfo->VBPtrOffset;
297 const CXXRecordDecl *getBaseSharingVBPtr() const {
298 assert(CXXInfo && "Record layout does not have C++ specific info!");
299 return CXXInfo->BaseSharingVBPtr;
302 const VBaseOffsetsMapTy &getVBaseOffsetsMap() const {
303 assert(CXXInfo && "Record layout does not have C++ specific info!");
304 return CXXInfo->VBaseOffsets;
308 } // end namespace clang