1//===--- RecordLayout.h - Layout information for a struct/union -*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file defines the RecordLayout interface.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_AST_LAYOUTINFO_H
15#define LLVM_CLANG_AST_LAYOUTINFO_H
16
17#include "clang/AST/CharUnits.h"
18#include "clang/AST/DeclCXX.h"
19#include "llvm/ADT/DenseMap.h"
20
21namespace clang {
22  class ASTContext;
23  class FieldDecl;
24  class RecordDecl;
25  class CXXRecordDecl;
26
27/// ASTRecordLayout -
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.
34class ASTRecordLayout {
35public:
36  struct VBaseInfo {
37    /// The offset to this virtual base in the complete-object layout
38    /// of this class.
39    CharUnits VBaseOffset;
40
41  private:
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.
45    bool HasVtorDisp;
46
47  public:
48    bool hasVtorDisp() const { return HasVtorDisp; }
49
50    VBaseInfo() : HasVtorDisp(false) {}
51
52    VBaseInfo(CharUnits VBaseOffset, bool hasVtorDisp) :
53     VBaseOffset(VBaseOffset), HasVtorDisp(hasVtorDisp) {}
54  };
55
56  typedef llvm::DenseMap<const CXXRecordDecl *, VBaseInfo>
57    VBaseOffsetsMapTy;
58
59private:
60  /// Size - Size of record in characters.
61  CharUnits Size;
62
63  /// DataSize - Size of record in characters without tail padding.
64  CharUnits DataSize;
65
66  // Alignment - Alignment of record in characters.
67  CharUnits Alignment;
68
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;
72
73  /// FieldOffsets - Array of field offsets in bits.
74  uint64_t *FieldOffsets;
75
76  // FieldCount - Number of fields.
77  unsigned FieldCount;
78
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;
84
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;
88
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;
93
94    /// VBPtrOffset - Virtual base table offset (Microsoft-only).
95    CharUnits VBPtrOffset;
96
97    /// HasOwnVFPtr - Does this class provide a virtual function table
98    /// (vtable in Itanium, vftbl in Microsoft) that is independent from
99    /// its base classes?
100    bool HasOwnVFPtr : 1;
101
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;
106
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
109    /// MS-ABI.
110    bool HasZeroSizedSubObject : 1;
111
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;
115
116    /// PrimaryBase - The primary base info for this record.
117    llvm::PointerIntPair<const CXXRecordDecl *, 1, bool> PrimaryBase;
118
119    /// BaseSharingVBPtr - The base we share vbptr with.
120    const CXXRecordDecl *BaseSharingVBPtr;
121
122    /// FIXME: This should really use a SmallPtrMap, once we have one in LLVM :)
123    typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetsMapTy;
124
125    /// BaseOffsets - Contains a map from base classes to their offset.
126    BaseOffsetsMapTy BaseOffsets;
127
128    /// VBaseOffsets - Contains a map from vbase classes to their offset.
129    VBaseOffsetsMapTy VBaseOffsets;
130  };
131
132  /// CXXInfo - If the record layout is for a C++ record, this will have
133  /// C++ specific information about the record.
134  CXXRecordLayoutInfo *CXXInfo;
135
136  friend class ASTContext;
137
138  ASTRecordLayout(const ASTContext &Ctx, CharUnits size, CharUnits alignment,
139                  CharUnits requiredAlignment,
140                  CharUnits datasize, const uint64_t *fieldoffsets,
141                  unsigned fieldcount);
142
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,
150                  CharUnits datasize,
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);
161
162  ~ASTRecordLayout() {}
163
164  void Destroy(ASTContext &Ctx);
165
166  ASTRecordLayout(const ASTRecordLayout &) LLVM_DELETED_FUNCTION;
167  void operator=(const ASTRecordLayout &) LLVM_DELETED_FUNCTION;
168public:
169
170  /// getAlignment - Get the record alignment in characters.
171  CharUnits getAlignment() const { return Alignment; }
172
173  /// getSize - Get the record size in characters.
174  CharUnits getSize() const { return Size; }
175
176  /// getFieldCount - Get the number of fields in the layout.
177  unsigned getFieldCount() const { return FieldCount; }
178
179  /// getFieldOffset - Get the offset of the given field index, in
180  /// bits.
181  uint64_t getFieldOffset(unsigned FieldNo) const {
182    assert (FieldNo < FieldCount && "Invalid Field No");
183    return FieldOffsets[FieldNo];
184  }
185
186  /// getDataSize() - Get the record data size, which is the record size
187  /// without tail padding, in characters.
188  CharUnits getDataSize() const {
189    return DataSize;
190  }
191
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!");
196
197    return CXXInfo->NonVirtualSize;
198  }
199
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!");
204
205    return CXXInfo->NonVirtualAlignment;
206  }
207
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!");
211
212    return CXXInfo->PrimaryBase.getPointer();
213  }
214
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!");
219
220    return CXXInfo->PrimaryBase.getInt();
221  }
222
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!");
227
228    return CXXInfo->BaseOffsets[Base];
229  }
230
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!");
235
236    return CXXInfo->VBaseOffsets[VBase].VBaseOffset;
237  }
238
239  CharUnits getSizeOfLargestEmptySubobject() const {
240    assert(CXXInfo && "Record layout does not have C++ specific info!");
241    return CXXInfo->SizeOfLargestEmptySubobject;
242  }
243
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.
247  ///
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
250  /// of the ABI.
251  bool hasOwnVFPtr() const {
252    assert(CXXInfo && "Record layout does not have C++ specific info!");
253    return CXXInfo->HasOwnVFPtr;
254  }
255
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;
262  }
263
264  /// hasOwnVBPtr - Does this class provide its own virtual-base
265  /// table pointer, rather than inheriting one from a primary base
266  /// class?
267  ///
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
270  /// of the ABI.
271  bool hasOwnVBPtr() const {
272    assert(CXXInfo && "Record layout does not have C++ specific info!");
273    return hasVBPtr() && !CXXInfo->BaseSharingVBPtr;
274  }
275
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();
280  }
281
282  CharUnits getRequiredAlignment() const {
283    return RequiredAlignment;
284  }
285
286  bool hasZeroSizedSubObject() const {
287    return CXXInfo && CXXInfo->HasZeroSizedSubObject;
288  }
289
290  bool leadsWithZeroSizedBase() const {
291    assert(CXXInfo && "Record layout does not have C++ specific info!");
292    return CXXInfo->LeadsWithZeroSizedBase;
293  }
294
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;
300  }
301
302  const CXXRecordDecl *getBaseSharingVBPtr() const {
303    assert(CXXInfo && "Record layout does not have C++ specific info!");
304    return CXXInfo->BaseSharingVBPtr;
305  }
306
307  const VBaseOffsetsMapTy &getVBaseOffsetsMap() const {
308    assert(CXXInfo && "Record layout does not have C++ specific info!");
309    return CXXInfo->VBaseOffsets;
310  }
311};
312
313}  // end namespace clang
314
315#endif
316