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 /// FieldOffsets - Array of field offsets in bits.
70 uint64_t *FieldOffsets;
72 // FieldCount - Number of fields.
75 /// CXXRecordLayoutInfo - Contains C++ specific layout information.
76 struct CXXRecordLayoutInfo {
77 /// NonVirtualSize - The non-virtual size (in chars) of an object, which is
78 /// the size of the object without virtual bases.
79 CharUnits NonVirtualSize;
81 /// NonVirtualAlign - The non-virtual alignment (in chars) of an object,
82 /// which is the alignment of the object without virtual bases.
83 CharUnits NonVirtualAlign;
85 /// SizeOfLargestEmptySubobject - The size of the largest empty subobject
86 /// (either a base or a member). Will be zero if the class doesn't contain
87 /// any empty subobjects.
88 CharUnits SizeOfLargestEmptySubobject;
90 /// VBPtrOffset - Virtual base table offset (Microsoft-only).
91 CharUnits VBPtrOffset;
93 /// HasOwnVFPtr - Does this class provide a virtual function table
94 /// (vtable in Itanium, vftbl in Microsoft) that is independent from
96 bool HasOwnVFPtr; // TODO: stash this somewhere more efficient
98 /// PrimaryBase - The primary base info for this record.
99 llvm::PointerIntPair<const CXXRecordDecl *, 1, bool> PrimaryBase;
101 /// FIXME: This should really use a SmallPtrMap, once we have one in LLVM :)
102 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetsMapTy;
104 /// BaseOffsets - Contains a map from base classes to their offset.
105 BaseOffsetsMapTy BaseOffsets;
107 /// VBaseOffsets - Contains a map from vbase classes to their offset.
108 VBaseOffsetsMapTy VBaseOffsets;
111 /// CXXInfo - If the record layout is for a C++ record, this will have
112 /// C++ specific information about the record.
113 CXXRecordLayoutInfo *CXXInfo;
115 friend class ASTContext;
117 ASTRecordLayout(const ASTContext &Ctx, CharUnits size, CharUnits alignment,
118 CharUnits datasize, const uint64_t *fieldoffsets,
119 unsigned fieldcount);
121 // Constructor for C++ records.
122 typedef CXXRecordLayoutInfo::BaseOffsetsMapTy BaseOffsetsMapTy;
123 ASTRecordLayout(const ASTContext &Ctx,
124 CharUnits size, CharUnits alignment,
125 bool hasOwnVFPtr, CharUnits vbptroffset,
127 const uint64_t *fieldoffsets, unsigned fieldcount,
128 CharUnits nonvirtualsize, CharUnits nonvirtualalign,
129 CharUnits SizeOfLargestEmptySubobject,
130 const CXXRecordDecl *PrimaryBase,
131 bool IsPrimaryBaseVirtual,
132 const BaseOffsetsMapTy& BaseOffsets,
133 const VBaseOffsetsMapTy& VBaseOffsets);
135 ~ASTRecordLayout() {}
137 void Destroy(ASTContext &Ctx);
139 ASTRecordLayout(const ASTRecordLayout &) LLVM_DELETED_FUNCTION;
140 void operator=(const ASTRecordLayout &) LLVM_DELETED_FUNCTION;
143 /// getAlignment - Get the record alignment in characters.
144 CharUnits getAlignment() const { return Alignment; }
146 /// getSize - Get the record size in characters.
147 CharUnits getSize() const { return Size; }
149 /// getFieldCount - Get the number of fields in the layout.
150 unsigned getFieldCount() const { return FieldCount; }
152 /// getFieldOffset - Get the offset of the given field index, in
154 uint64_t getFieldOffset(unsigned FieldNo) const {
155 assert (FieldNo < FieldCount && "Invalid Field No");
156 return FieldOffsets[FieldNo];
159 /// getDataSize() - Get the record data size, which is the record size
160 /// without tail padding, in characters.
161 CharUnits getDataSize() const {
165 /// getNonVirtualSize - Get the non-virtual size (in chars) of an object,
166 /// which is the size of the object without virtual bases.
167 CharUnits getNonVirtualSize() const {
168 assert(CXXInfo && "Record layout does not have C++ specific info!");
170 return CXXInfo->NonVirtualSize;
173 /// getNonVirtualSize - Get the non-virtual alignment (in chars) of an object,
174 /// which is the alignment of the object without virtual bases.
175 CharUnits getNonVirtualAlign() const {
176 assert(CXXInfo && "Record layout does not have C++ specific info!");
178 return CXXInfo->NonVirtualAlign;
181 /// getPrimaryBase - Get the primary base for this record.
182 const CXXRecordDecl *getPrimaryBase() const {
183 assert(CXXInfo && "Record layout does not have C++ specific info!");
185 return CXXInfo->PrimaryBase.getPointer();
188 /// isPrimaryBaseVirtual - Get whether the primary base for this record
189 /// is virtual or not.
190 bool isPrimaryBaseVirtual() const {
191 assert(CXXInfo && "Record layout does not have C++ specific info!");
193 return CXXInfo->PrimaryBase.getInt();
196 /// getBaseClassOffset - Get the offset, in chars, for the given base class.
197 CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const {
198 assert(CXXInfo && "Record layout does not have C++ specific info!");
199 assert(CXXInfo->BaseOffsets.count(Base) && "Did not find base!");
201 return CXXInfo->BaseOffsets[Base];
204 /// getVBaseClassOffset - Get the offset, in chars, for the given base class.
205 CharUnits getVBaseClassOffset(const CXXRecordDecl *VBase) const {
206 assert(CXXInfo && "Record layout does not have C++ specific info!");
207 assert(CXXInfo->VBaseOffsets.count(VBase) && "Did not find base!");
209 return CXXInfo->VBaseOffsets[VBase].VBaseOffset;
212 CharUnits getSizeOfLargestEmptySubobject() const {
213 assert(CXXInfo && "Record layout does not have C++ specific info!");
214 return CXXInfo->SizeOfLargestEmptySubobject;
217 /// hasOwnVFPtr - Does this class provide its own virtual-function
218 /// table pointer, rather than inheriting one from a primary base
219 /// class? If so, it is at offset zero.
221 /// This implies that the ABI has no primary base class, meaning
222 /// that it has no base classes that are suitable under the conditions
224 bool hasOwnVFPtr() const {
225 assert(CXXInfo && "Record layout does not have C++ specific info!");
226 return CXXInfo->HasOwnVFPtr;
229 /// getVBPtrOffset - Get the offset for virtual base table pointer.
230 /// This is only meaningful with the Microsoft ABI.
231 CharUnits getVBPtrOffset() const {
232 assert(CXXInfo && "Record layout does not have C++ specific info!");
233 return CXXInfo->VBPtrOffset;
236 const VBaseOffsetsMapTy &getVBaseOffsetsMap() const {
237 assert(CXXInfo && "Record layout does not have C++ specific info!");
238 return CXXInfo->VBaseOffsets;
242 } // end namespace clang