RecordLayout.h revision 6acb04f755cfc73d05772457d70aeb5adfaf6d8f
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  /// FieldOffsets - Array of field offsets in bits.
70  uint64_t *FieldOffsets;
71
72  // FieldCount - Number of fields.
73  unsigned FieldCount;
74
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;
80
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;
84
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;
89
90    /// VBPtrOffset - Virtual base table offset (Microsoft-only).
91    CharUnits VBPtrOffset;
92
93    /// HasOwnVFPtr - Does this class provide a virtual function table
94    /// (vtable in Itanium, vftbl in Microsoft) that is independent from
95    /// its base classes?
96    bool HasOwnVFPtr : 1;
97
98    /// HasVFPtr - Does this class have a vftable at all (could be inherited
99    /// from its primary base.)
100    bool HasVFPtr : 1;
101
102    /// AlignAfterVBases - Force appropriate alignment after virtual bases are
103    /// laid out in MS-C++-ABI.
104    bool AlignAfterVBases : 1;
105
106    /// PrimaryBase - The primary base info for this record.
107    llvm::PointerIntPair<const CXXRecordDecl *, 1, bool> PrimaryBase;
108
109    /// BaseSharingVBPtr - The base we share vbptr with.
110    const CXXRecordDecl *BaseSharingVBPtr;
111
112    /// FIXME: This should really use a SmallPtrMap, once we have one in LLVM :)
113    typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetsMapTy;
114
115    /// BaseOffsets - Contains a map from base classes to their offset.
116    BaseOffsetsMapTy BaseOffsets;
117
118    /// VBaseOffsets - Contains a map from vbase classes to their offset.
119    VBaseOffsetsMapTy VBaseOffsets;
120  };
121
122  /// CXXInfo - If the record layout is for a C++ record, this will have
123  /// C++ specific information about the record.
124  CXXRecordLayoutInfo *CXXInfo;
125
126  friend class ASTContext;
127
128  ASTRecordLayout(const ASTContext &Ctx, CharUnits size, CharUnits alignment,
129                  CharUnits datasize, const uint64_t *fieldoffsets,
130                  unsigned fieldcount);
131
132  // Constructor for C++ records.
133  typedef CXXRecordLayoutInfo::BaseOffsetsMapTy BaseOffsetsMapTy;
134  ASTRecordLayout(const ASTContext &Ctx,
135                  CharUnits size, CharUnits alignment,
136                  bool hasOwnVFPtr, bool hasVFPtr,
137                  CharUnits vbptroffset,
138                  CharUnits datasize,
139                  const uint64_t *fieldoffsets, unsigned fieldcount,
140                  CharUnits nonvirtualsize, CharUnits nonvirtualalign,
141                  CharUnits SizeOfLargestEmptySubobject,
142                  const CXXRecordDecl *PrimaryBase,
143                  bool IsPrimaryBaseVirtual,
144                  const CXXRecordDecl *BaseSharingVBPtr,
145                  bool ForceAlign,
146                  const BaseOffsetsMapTy& BaseOffsets,
147                  const VBaseOffsetsMapTy& VBaseOffsets);
148
149  ~ASTRecordLayout() {}
150
151  void Destroy(ASTContext &Ctx);
152
153  ASTRecordLayout(const ASTRecordLayout &) LLVM_DELETED_FUNCTION;
154  void operator=(const ASTRecordLayout &) LLVM_DELETED_FUNCTION;
155public:
156
157  /// getAlignment - Get the record alignment in characters.
158  CharUnits getAlignment() const { return Alignment; }
159
160  /// getSize - Get the record size in characters.
161  CharUnits getSize() const { return Size; }
162
163  /// getFieldCount - Get the number of fields in the layout.
164  unsigned getFieldCount() const { return FieldCount; }
165
166  /// getFieldOffset - Get the offset of the given field index, in
167  /// bits.
168  uint64_t getFieldOffset(unsigned FieldNo) const {
169    assert (FieldNo < FieldCount && "Invalid Field No");
170    return FieldOffsets[FieldNo];
171  }
172
173  /// getDataSize() - Get the record data size, which is the record size
174  /// without tail padding, in characters.
175  CharUnits getDataSize() const {
176    return DataSize;
177  }
178
179  /// getNonVirtualSize - Get the non-virtual size (in chars) of an object,
180  /// which is the size of the object without virtual bases.
181  CharUnits getNonVirtualSize() const {
182    assert(CXXInfo && "Record layout does not have C++ specific info!");
183
184    return CXXInfo->NonVirtualSize;
185  }
186
187  /// getNonVirtualSize - Get the non-virtual alignment (in chars) of an object,
188  /// which is the alignment of the object without virtual bases.
189  CharUnits getNonVirtualAlign() const {
190    assert(CXXInfo && "Record layout does not have C++ specific info!");
191
192    return CXXInfo->NonVirtualAlign;
193  }
194
195  /// getPrimaryBase - Get the primary base for this record.
196  const CXXRecordDecl *getPrimaryBase() const {
197    assert(CXXInfo && "Record layout does not have C++ specific info!");
198
199    return CXXInfo->PrimaryBase.getPointer();
200  }
201
202  /// isPrimaryBaseVirtual - Get whether the primary base for this record
203  /// is virtual or not.
204  bool isPrimaryBaseVirtual() const {
205    assert(CXXInfo && "Record layout does not have C++ specific info!");
206
207    return CXXInfo->PrimaryBase.getInt();
208  }
209
210  /// getBaseClassOffset - Get the offset, in chars, for the given base class.
211  CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const {
212    assert(CXXInfo && "Record layout does not have C++ specific info!");
213    assert(CXXInfo->BaseOffsets.count(Base) && "Did not find base!");
214
215    return CXXInfo->BaseOffsets[Base];
216  }
217
218  /// getVBaseClassOffset - Get the offset, in chars, for the given base class.
219  CharUnits getVBaseClassOffset(const CXXRecordDecl *VBase) const {
220    assert(CXXInfo && "Record layout does not have C++ specific info!");
221    assert(CXXInfo->VBaseOffsets.count(VBase) && "Did not find base!");
222
223    return CXXInfo->VBaseOffsets[VBase].VBaseOffset;
224  }
225
226  CharUnits getSizeOfLargestEmptySubobject() const {
227    assert(CXXInfo && "Record layout does not have C++ specific info!");
228    return CXXInfo->SizeOfLargestEmptySubobject;
229  }
230
231  /// hasOwnVFPtr - Does this class provide its own virtual-function
232  /// table pointer, rather than inheriting one from a primary base
233  /// class?  If so, it is at offset zero.
234  ///
235  /// This implies that the ABI has no primary base class, meaning
236  /// that it has no base classes that are suitable under the conditions
237  /// of the ABI.
238  bool hasOwnVFPtr() const {
239    assert(CXXInfo && "Record layout does not have C++ specific info!");
240    return CXXInfo->HasOwnVFPtr;
241  }
242
243  /// hasVFPtr - Does this class have a virtual function table pointer.
244  bool hasVFPtr() const {
245    assert(CXXInfo && "Record layout does not have C++ specific info!");
246    return CXXInfo->HasVFPtr;
247  }
248
249  /// hasOwnVBPtr - Does this class provide its own virtual-base
250  /// table pointer, rather than inheriting one from a primary base
251  /// class?
252  ///
253  /// This implies that the ABI has no primary base class, meaning
254  /// that it has no base classes that are suitable under the conditions
255  /// of the ABI.
256  bool hasOwnVBPtr() const {
257    assert(CXXInfo && "Record layout does not have C++ specific info!");
258    return hasVBPtr() && !CXXInfo->BaseSharingVBPtr;
259  }
260
261  /// hasVBPtr - Does this class have a virtual function table pointer.
262  bool hasVBPtr() const {
263    assert(CXXInfo && "Record layout does not have C++ specific info!");
264    return !CXXInfo->VBPtrOffset.isNegative();
265  }
266
267  bool getAlignAfterVBases() const {
268    assert(CXXInfo && "Record layout does not have C++ specific info!");
269    return CXXInfo->AlignAfterVBases;
270  }
271
272  /// getVBPtrOffset - Get the offset for virtual base table pointer.
273  /// This is only meaningful with the Microsoft ABI.
274  CharUnits getVBPtrOffset() const {
275    assert(CXXInfo && "Record layout does not have C++ specific info!");
276    return CXXInfo->VBPtrOffset;
277  }
278
279  const CXXRecordDecl *getBaseSharingVBPtr() const {
280    assert(CXXInfo && "Record layout does not have C++ specific info!");
281    return CXXInfo->BaseSharingVBPtr;
282  }
283
284  const VBaseOffsetsMapTy &getVBaseOffsetsMap() const {
285    assert(CXXInfo && "Record layout does not have C++ specific info!");
286    return CXXInfo->VBaseOffsets;
287  }
288};
289
290}  // end namespace clang
291
292#endif
293