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 "llvm/Support/DataTypes.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "clang/AST/CharUnits.h"
20 #include "clang/AST/DeclCXX.h"
29 /// This class contains layout information for one RecordDecl,
30 /// which is a struct/union/class. The decl represented must be a definition,
31 /// not a forward declaration.
32 /// This class is also used to contain layout information for one
33 /// ObjCInterfaceDecl. FIXME - Find appropriate name.
34 /// These objects are managed by ASTContext.
35 class ASTRecordLayout {
36 /// Size - Size of record in characters.
39 /// DataSize - Size of record in characters without tail padding.
42 /// FieldOffsets - Array of field offsets in bits.
43 uint64_t *FieldOffsets;
45 // Alignment - Alignment of record in characters.
48 // FieldCount - Number of fields.
51 /// CXXRecordLayoutInfo - Contains C++ specific layout information.
52 struct CXXRecordLayoutInfo {
53 /// NonVirtualSize - The non-virtual size (in chars) of an object, which is
54 /// the size of the object without virtual bases.
55 CharUnits NonVirtualSize;
57 /// NonVirtualAlign - The non-virtual alignment (in chars) of an object,
58 /// which is the alignment of the object without virtual bases.
59 CharUnits NonVirtualAlign;
61 /// SizeOfLargestEmptySubobject - The size of the largest empty subobject
62 /// (either a base or a member). Will be zero if the class doesn't contain
63 /// any empty subobjects.
64 CharUnits SizeOfLargestEmptySubobject;
66 /// PrimaryBase - The primary base info for this record.
67 llvm::PointerIntPair<const CXXRecordDecl *, 1, bool> PrimaryBase;
69 /// FIXME: This should really use a SmallPtrMap, once we have one in LLVM :)
70 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetsMapTy;
72 /// BaseOffsets - Contains a map from base classes to their offset.
73 BaseOffsetsMapTy BaseOffsets;
75 /// VBaseOffsets - Contains a map from vbase classes to their offset.
76 BaseOffsetsMapTy VBaseOffsets;
79 /// CXXInfo - If the record layout is for a C++ record, this will have
80 /// C++ specific information about the record.
81 CXXRecordLayoutInfo *CXXInfo;
83 friend class ASTContext;
85 ASTRecordLayout(const ASTContext &Ctx, CharUnits size, CharUnits alignment,
86 CharUnits datasize, const uint64_t *fieldoffsets,
89 // Constructor for C++ records.
90 typedef CXXRecordLayoutInfo::BaseOffsetsMapTy BaseOffsetsMapTy;
91 ASTRecordLayout(const ASTContext &Ctx,
92 CharUnits size, CharUnits alignment, CharUnits datasize,
93 const uint64_t *fieldoffsets, unsigned fieldcount,
94 CharUnits nonvirtualsize, CharUnits nonvirtualalign,
95 CharUnits SizeOfLargestEmptySubobject,
96 const CXXRecordDecl *PrimaryBase,
97 bool IsPrimaryBaseVirtual,
98 const BaseOffsetsMapTy& BaseOffsets,
99 const BaseOffsetsMapTy& VBaseOffsets);
101 ~ASTRecordLayout() {}
103 void Destroy(ASTContext &Ctx);
105 ASTRecordLayout(const ASTRecordLayout&); // DO NOT IMPLEMENT
106 void operator=(const ASTRecordLayout&); // DO NOT IMPLEMENT
109 /// getAlignment - Get the record alignment in characters.
110 CharUnits getAlignment() const { return Alignment; }
112 /// getSize - Get the record size in characters.
113 CharUnits getSize() const { return Size; }
115 /// getFieldCount - Get the number of fields in the layout.
116 unsigned getFieldCount() const { return FieldCount; }
118 /// getFieldOffset - Get the offset of the given field index, in
120 uint64_t getFieldOffset(unsigned FieldNo) const {
121 assert (FieldNo < FieldCount && "Invalid Field No");
122 return FieldOffsets[FieldNo];
125 /// getDataSize() - Get the record data size, which is the record size
126 /// without tail padding, in characters.
127 CharUnits getDataSize() const {
131 /// getNonVirtualSize - Get the non-virtual size (in chars) of an object,
132 /// which is the size of the object without virtual bases.
133 CharUnits getNonVirtualSize() const {
134 assert(CXXInfo && "Record layout does not have C++ specific info!");
136 return CXXInfo->NonVirtualSize;
139 /// getNonVirtualSize - Get the non-virtual alignment (in chars) of an object,
140 /// which is the alignment of the object without virtual bases.
141 CharUnits getNonVirtualAlign() const {
142 assert(CXXInfo && "Record layout does not have C++ specific info!");
144 return CXXInfo->NonVirtualAlign;
147 /// getPrimaryBase - Get the primary base for this record.
148 const CXXRecordDecl *getPrimaryBase() const {
149 assert(CXXInfo && "Record layout does not have C++ specific info!");
151 return CXXInfo->PrimaryBase.getPointer();
154 /// isPrimaryBaseVirtual - Get whether the primary base for this record
155 /// is virtual or not.
156 bool isPrimaryBaseVirtual() const {
157 assert(CXXInfo && "Record layout does not have C++ specific info!");
159 return CXXInfo->PrimaryBase.getInt();
162 /// getBaseClassOffset - Get the offset, in chars, for the given base class.
163 CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const {
164 assert(CXXInfo && "Record layout does not have C++ specific info!");
165 assert(CXXInfo->BaseOffsets.count(Base) && "Did not find base!");
167 return CXXInfo->BaseOffsets[Base];
170 /// getVBaseClassOffset - Get the offset, in chars, for the given base class.
171 CharUnits getVBaseClassOffset(const CXXRecordDecl *VBase) const {
172 assert(CXXInfo && "Record layout does not have C++ specific info!");
173 assert(CXXInfo->VBaseOffsets.count(VBase) && "Did not find base!");
175 return CXXInfo->VBaseOffsets[VBase];
178 /// getBaseClassOffsetInBits - Get the offset, in bits, for the given
180 uint64_t getBaseClassOffsetInBits(const CXXRecordDecl *Base) const {
181 assert(CXXInfo && "Record layout does not have C++ specific info!");
182 assert(CXXInfo->BaseOffsets.count(Base) && "Did not find base!");
184 return getBaseClassOffset(Base).getQuantity() *
185 Base->getASTContext().getCharWidth();
188 /// getVBaseClassOffsetInBits - Get the offset, in bits, for the given
190 uint64_t getVBaseClassOffsetInBits(const CXXRecordDecl *VBase) const {
191 assert(CXXInfo && "Record layout does not have C++ specific info!");
192 assert(CXXInfo->VBaseOffsets.count(VBase) && "Did not find base!");
194 return getVBaseClassOffset(VBase).getQuantity() *
195 VBase->getASTContext().getCharWidth();
198 CharUnits getSizeOfLargestEmptySubobject() const {
199 assert(CXXInfo && "Record layout does not have C++ specific info!");
200 return CXXInfo->SizeOfLargestEmptySubobject;
204 } // end namespace clang