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_LAYOUTINFO_H
15 #define LLVM_CLANG_AST_LAYOUTINFO_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 uint64_t *FieldOffsets;
76 // FieldCount - Number of fields.
79 /// CXXRecordLayoutInfo - Contains C++ specific layout information.
80 struct CXXRecordLayoutInfo {
81 /// NonVirtualSize - The non-virtual size (in chars) of an object, which is
82 /// the size of the object without virtual bases.
83 CharUnits NonVirtualSize;
85 /// NonVirtualAlignment - The non-virtual alignment (in chars) of an object,
86 /// which is the alignment of the object without virtual bases.
87 CharUnits NonVirtualAlignment;
89 /// SizeOfLargestEmptySubobject - The size of the largest empty subobject
90 /// (either a base or a member). Will be zero if the class doesn't contain
91 /// any empty subobjects.
92 CharUnits SizeOfLargestEmptySubobject;
94 /// VBPtrOffset - Virtual base table offset (Microsoft-only).
95 CharUnits VBPtrOffset;
97 /// HasOwnVFPtr - Does this class provide a virtual function table
98 /// (vtable in Itanium, vftbl in Microsoft) that is independent from
100 bool HasOwnVFPtr : 1;
102 /// HasVFPtr - Does this class have a vftable that could be extended by
103 /// a derived class. The class may have inherited this pointer from
104 /// a primary base class.
105 bool HasExtendableVFPtr : 1;
107 /// HasZeroSizedSubObject - True if this class contains a zero sized member
108 /// or base or a base with a zero sized member or base. Only used for
110 bool HasZeroSizedSubObject : 1;
112 /// \brief True if this class is zero sized or first base is zero sized or
113 /// has this property. Only used for MS-ABI.
114 bool LeadsWithZeroSizedBase : 1;
116 /// PrimaryBase - The primary base info for this record.
117 llvm::PointerIntPair<const CXXRecordDecl *, 1, bool> PrimaryBase;
119 /// BaseSharingVBPtr - The base we share vbptr with.
120 const CXXRecordDecl *BaseSharingVBPtr;
122 /// FIXME: This should really use a SmallPtrMap, once we have one in LLVM :)
123 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetsMapTy;
125 /// BaseOffsets - Contains a map from base classes to their offset.
126 BaseOffsetsMapTy BaseOffsets;
128 /// VBaseOffsets - Contains a map from vbase classes to their offset.
129 VBaseOffsetsMapTy VBaseOffsets;
132 /// CXXInfo - If the record layout is for a C++ record, this will have
133 /// C++ specific information about the record.
134 CXXRecordLayoutInfo *CXXInfo;
136 friend class ASTContext;
138 ASTRecordLayout(const ASTContext &Ctx, CharUnits size, CharUnits alignment,
139 CharUnits requiredAlignment,
140 CharUnits datasize, const uint64_t *fieldoffsets,
141 unsigned fieldcount);
143 // Constructor for C++ records.
144 typedef CXXRecordLayoutInfo::BaseOffsetsMapTy BaseOffsetsMapTy;
145 ASTRecordLayout(const ASTContext &Ctx,
146 CharUnits size, CharUnits alignment,
147 CharUnits requiredAlignment,
148 bool hasOwnVFPtr, bool hasExtendableVFPtr,
149 CharUnits vbptroffset,
151 const uint64_t *fieldoffsets, unsigned fieldcount,
152 CharUnits nonvirtualsize, CharUnits nonvirtualalignment,
153 CharUnits SizeOfLargestEmptySubobject,
154 const CXXRecordDecl *PrimaryBase,
155 bool IsPrimaryBaseVirtual,
156 const CXXRecordDecl *BaseSharingVBPtr,
157 bool HasZeroSizedSubObject,
158 bool LeadsWithZeroSizedBase,
159 const BaseOffsetsMapTy& BaseOffsets,
160 const VBaseOffsetsMapTy& VBaseOffsets);
162 ~ASTRecordLayout() {}
164 void Destroy(ASTContext &Ctx);
166 ASTRecordLayout(const ASTRecordLayout &) LLVM_DELETED_FUNCTION;
167 void operator=(const ASTRecordLayout &) LLVM_DELETED_FUNCTION;
170 /// getAlignment - Get the record alignment in characters.
171 CharUnits getAlignment() const { return Alignment; }
173 /// getSize - Get the record size in characters.
174 CharUnits getSize() const { return Size; }
176 /// getFieldCount - Get the number of fields in the layout.
177 unsigned getFieldCount() const { return FieldCount; }
179 /// getFieldOffset - Get the offset of the given field index, in
181 uint64_t getFieldOffset(unsigned FieldNo) const {
182 assert (FieldNo < FieldCount && "Invalid Field No");
183 return FieldOffsets[FieldNo];
186 /// getDataSize() - Get the record data size, which is the record size
187 /// without tail padding, in characters.
188 CharUnits getDataSize() const {
192 /// getNonVirtualSize - Get the non-virtual size (in chars) of an object,
193 /// which is the size of the object without virtual bases.
194 CharUnits getNonVirtualSize() const {
195 assert(CXXInfo && "Record layout does not have C++ specific info!");
197 return CXXInfo->NonVirtualSize;
200 /// getNonVirtualSize - Get the non-virtual alignment (in chars) of an object,
201 /// which is the alignment of the object without virtual bases.
202 CharUnits getNonVirtualAlignment() const {
203 assert(CXXInfo && "Record layout does not have C++ specific info!");
205 return CXXInfo->NonVirtualAlignment;
208 /// getPrimaryBase - Get the primary base for this record.
209 const CXXRecordDecl *getPrimaryBase() const {
210 assert(CXXInfo && "Record layout does not have C++ specific info!");
212 return CXXInfo->PrimaryBase.getPointer();
215 /// isPrimaryBaseVirtual - Get whether the primary base for this record
216 /// is virtual or not.
217 bool isPrimaryBaseVirtual() const {
218 assert(CXXInfo && "Record layout does not have C++ specific info!");
220 return CXXInfo->PrimaryBase.getInt();
223 /// getBaseClassOffset - Get the offset, in chars, for the given base class.
224 CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const {
225 assert(CXXInfo && "Record layout does not have C++ specific info!");
226 assert(CXXInfo->BaseOffsets.count(Base) && "Did not find base!");
228 return CXXInfo->BaseOffsets[Base];
231 /// getVBaseClassOffset - Get the offset, in chars, for the given base class.
232 CharUnits getVBaseClassOffset(const CXXRecordDecl *VBase) const {
233 assert(CXXInfo && "Record layout does not have C++ specific info!");
234 assert(CXXInfo->VBaseOffsets.count(VBase) && "Did not find base!");
236 return CXXInfo->VBaseOffsets[VBase].VBaseOffset;
239 CharUnits getSizeOfLargestEmptySubobject() const {
240 assert(CXXInfo && "Record layout does not have C++ specific info!");
241 return CXXInfo->SizeOfLargestEmptySubobject;
244 /// hasOwnVFPtr - Does this class provide its own virtual-function
245 /// table pointer, rather than inheriting one from a primary base
246 /// class? If so, it is at offset zero.
248 /// This implies that the ABI has no primary base class, meaning
249 /// that it has no base classes that are suitable under the conditions
251 bool hasOwnVFPtr() const {
252 assert(CXXInfo && "Record layout does not have C++ specific info!");
253 return CXXInfo->HasOwnVFPtr;
256 /// hasVFPtr - Does this class have a virtual function table pointer
257 /// that can be extended by a derived class? This is synonymous with
258 /// this class having a VFPtr at offset zero.
259 bool hasExtendableVFPtr() const {
260 assert(CXXInfo && "Record layout does not have C++ specific info!");
261 return CXXInfo->HasExtendableVFPtr;
264 /// hasOwnVBPtr - Does this class provide its own virtual-base
265 /// table pointer, rather than inheriting one from a primary base
268 /// This implies that the ABI has no primary base class, meaning
269 /// that it has no base classes that are suitable under the conditions
271 bool hasOwnVBPtr() const {
272 assert(CXXInfo && "Record layout does not have C++ specific info!");
273 return hasVBPtr() && !CXXInfo->BaseSharingVBPtr;
276 /// hasVBPtr - Does this class have a virtual function table pointer.
277 bool hasVBPtr() const {
278 assert(CXXInfo && "Record layout does not have C++ specific info!");
279 return !CXXInfo->VBPtrOffset.isNegative();
282 CharUnits getRequiredAlignment() const {
283 return RequiredAlignment;
286 bool hasZeroSizedSubObject() const {
287 return CXXInfo && CXXInfo->HasZeroSizedSubObject;
290 bool leadsWithZeroSizedBase() const {
291 assert(CXXInfo && "Record layout does not have C++ specific info!");
292 return CXXInfo->LeadsWithZeroSizedBase;
295 /// getVBPtrOffset - Get the offset for virtual base table pointer.
296 /// This is only meaningful with the Microsoft ABI.
297 CharUnits getVBPtrOffset() const {
298 assert(CXXInfo && "Record layout does not have C++ specific info!");
299 return CXXInfo->VBPtrOffset;
302 const CXXRecordDecl *getBaseSharingVBPtr() const {
303 assert(CXXInfo && "Record layout does not have C++ specific info!");
304 return CXXInfo->BaseSharingVBPtr;
307 const VBaseOffsetsMapTy &getVBaseOffsetsMap() const {
308 assert(CXXInfo && "Record layout does not have C++ specific info!");
309 return CXXInfo->VBaseOffsets;
313 } // end namespace clang