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