RecordLayout.h revision 243a68551ac9ec71bf341e062418e33eb4f286ff
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 "llvm/Support/DataTypes.h"
18
19namespace clang {
20  class ASTContext;
21  class FieldDecl;
22  class RecordDecl;
23
24/// ASTRecordLayout -
25/// This class contains layout information for one RecordDecl,
26/// which is a struct/union/class.  The decl represented must be a definition,
27/// not a forward declaration.
28/// This class is also used to contain layout information for one
29/// ObjCInterfaceDecl. FIXME - Find appropriate name.
30/// These objects are managed by ASTContext.
31class ASTRecordLayout {
32  uint64_t Size;        // Size of record in bits.
33  uint64_t DataSize;    // Size of record in bits without tail padding.
34  uint64_t *FieldOffsets;
35  unsigned Alignment;   // Alignment of record in bits.
36  unsigned FieldCount;  // Number of fields
37  friend class ASTContext;
38  friend class ASTRecordLayoutBuilder;
39
40  ASTRecordLayout(uint64_t size, unsigned alignment, unsigned datasize,
41                  const uint64_t *fieldoffsets, unsigned fieldcount)
42  : Size(size), DataSize(datasize), FieldOffsets(0), Alignment(alignment),
43    FieldCount(fieldcount) {
44    if (FieldCount > 0)  {
45      FieldOffsets = new uint64_t[FieldCount];
46      for (unsigned i = 0; i < FieldCount; ++i)
47        FieldOffsets[i] = fieldoffsets[i];
48    }
49  }
50  ~ASTRecordLayout() {
51    delete [] FieldOffsets;
52  }
53
54  ASTRecordLayout(const ASTRecordLayout&);   // DO NOT IMPLEMENT
55  void operator=(const ASTRecordLayout&); // DO NOT IMPLEMENT
56public:
57
58  /// getAlignment - Get the record alignment in bits.
59  unsigned getAlignment() const { return Alignment; }
60
61  /// getSize - Get the record size in bits.
62  uint64_t getSize() const { return Size; }
63
64  /// getFieldCount - Get the number of fields in the layout.
65  unsigned getFieldCount() const { return FieldCount; }
66
67  /// getFieldOffset - Get the offset of the given field index, in
68  /// bits.
69  uint64_t getFieldOffset(unsigned FieldNo) const {
70    assert (FieldNo < FieldCount && "Invalid Field No");
71    return FieldOffsets[FieldNo];
72  }
73
74  /// getDataSize() - Get the record data size, which is the record size
75  /// without tail padding, in bits.
76  uint64_t getDataSize() const {
77    return DataSize;
78  }
79};
80
81}  // end namespace clang
82
83#endif
84