blob: 4f5a23c24b0c3ba53b5911fdee464df3de7d9172 [file] [log] [blame]
Daniel Dunbar270e2032010-03-31 00:11:27 +00001//===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder ----*- C++ -*-===//
Anders Carlsson45372a62009-07-23 03:17:50 +00002//
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//
Daniel Dunbar270e2032010-03-31 00:11:27 +000010// Builder implementation for CGRecordLayout objects.
Anders Carlsson45372a62009-07-23 03:17:50 +000011//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar2924ade2010-03-30 22:26:10 +000014#include "CGRecordLayout.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/Attr.h"
Anders Carlsson46170f92010-11-24 22:50:27 +000017#include "clang/AST/CXXInheritance.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000018#include "clang/AST/DeclCXX.h"
19#include "clang/AST/Expr.h"
20#include "clang/AST/RecordLayout.h"
21#include "CodeGenTypes.h"
John McCallf16aa102010-08-22 21:01:12 +000022#include "CGCXXABI.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000023#include "llvm/DerivedTypes.h"
Daniel Dunbar93c62962010-04-12 18:14:18 +000024#include "llvm/Type.h"
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +000025#include "llvm/Support/Debug.h"
Daniel Dunbar93c62962010-04-12 18:14:18 +000026#include "llvm/Support/raw_ostream.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000027#include "llvm/Target/TargetData.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000028using namespace clang;
29using namespace CodeGen;
30
John McCalld0de0ce2010-11-30 23:17:27 +000031namespace {
Daniel Dunbar270e2032010-03-31 00:11:27 +000032
33class CGRecordLayoutBuilder {
34public:
35 /// FieldTypes - Holds the LLVM types that the struct is created from.
John McCall9b7da1c2011-02-15 06:40:56 +000036 ///
Anders Carlsson2786a812011-04-17 21:32:41 +000037 llvm::SmallVector<const llvm::Type *, 16> FieldTypes;
Daniel Dunbar270e2032010-03-31 00:11:27 +000038
John McCall9b7da1c2011-02-15 06:40:56 +000039 /// BaseSubobjectType - Holds the LLVM type for the non-virtual part
Anders Carlsson3d155e62010-11-09 05:25:47 +000040 /// of the struct. For example, consider:
41 ///
42 /// struct A { int i; };
43 /// struct B { void *v; };
44 /// struct C : virtual A, B { };
45 ///
46 /// The LLVM type of C will be
47 /// %struct.C = type { i32 (...)**, %struct.A, i32, %struct.B }
48 ///
49 /// And the LLVM type of the non-virtual base struct will be
50 /// %struct.C.base = type { i32 (...)**, %struct.A, i32 }
John McCall9b7da1c2011-02-15 06:40:56 +000051 ///
52 /// This only gets initialized if the base subobject type is
53 /// different from the complete-object type.
54 const llvm::StructType *BaseSubobjectType;
Anders Carlsson3d155e62010-11-09 05:25:47 +000055
John McCall9b7da1c2011-02-15 06:40:56 +000056 /// FieldInfo - Holds a field and its corresponding LLVM field number.
57 llvm::DenseMap<const FieldDecl *, unsigned> Fields;
Daniel Dunbar270e2032010-03-31 00:11:27 +000058
John McCall9b7da1c2011-02-15 06:40:56 +000059 /// BitFieldInfo - Holds location and size information about a bit field.
60 llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields;
Daniel Dunbar270e2032010-03-31 00:11:27 +000061
John McCall9b7da1c2011-02-15 06:40:56 +000062 llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases;
63 llvm::DenseMap<const CXXRecordDecl *, unsigned> VirtualBases;
Anders Carlsson46170f92010-11-24 22:50:27 +000064
65 /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are
66 /// primary base classes for some other direct or indirect base class.
67 CXXIndirectPrimaryBaseSet IndirectPrimaryBases;
68
Anders Carlsson1d7dc222010-11-28 19:18:44 +000069 /// LaidOutVirtualBases - A set of all laid out virtual bases, used to avoid
70 /// avoid laying out virtual bases more than once.
71 llvm::SmallPtrSet<const CXXRecordDecl *, 4> LaidOutVirtualBases;
72
John McCallf16aa102010-08-22 21:01:12 +000073 /// IsZeroInitializable - Whether this struct can be C++
74 /// zero-initialized with an LLVM zeroinitializer.
75 bool IsZeroInitializable;
John McCall9b7da1c2011-02-15 06:40:56 +000076 bool IsZeroInitializableAsBase;
Daniel Dunbar270e2032010-03-31 00:11:27 +000077
78 /// Packed - Whether the resulting LLVM struct will be packed or not.
79 bool Packed;
80
81private:
82 CodeGenTypes &Types;
83
Anders Carlssoneb9d81d2011-04-17 21:56:13 +000084 /// LastLaidOutBaseInfo - Contains the offset and non-virtual size of the
85 /// last base laid out. Used so that we can replace the last laid out base
86 /// type with an i8 array if needed.
87 struct LastLaidOutBaseInfo {
88 CharUnits Offset;
89 CharUnits NonVirtualSize;
90
91 bool isValid() const { return !NonVirtualSize.isZero(); }
92 void invalidate() { NonVirtualSize = CharUnits::Zero(); }
93
94 } LastLaidOutBase;
95
Daniel Dunbar270e2032010-03-31 00:11:27 +000096 /// Alignment - Contains the alignment of the RecordDecl.
John McCallfd577d62011-02-15 22:21:29 +000097 CharUnits Alignment;
Daniel Dunbar270e2032010-03-31 00:11:27 +000098
Daniel Dunbar270e2032010-03-31 00:11:27 +000099 /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field,
100 /// this will have the number of bits still available in the field.
101 char BitsAvailableInLastField;
102
John McCallfd577d62011-02-15 22:21:29 +0000103 /// NextFieldOffset - Holds the next field offset.
104 CharUnits NextFieldOffset;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000105
Anders Carlsson86664462010-04-17 20:49:27 +0000106 /// LayoutUnionField - Will layout a field in an union and return the type
107 /// that the field will have.
108 const llvm::Type *LayoutUnionField(const FieldDecl *Field,
109 const ASTRecordLayout &Layout);
110
Daniel Dunbar270e2032010-03-31 00:11:27 +0000111 /// LayoutUnion - Will layout a union RecordDecl.
112 void LayoutUnion(const RecordDecl *D);
113
114 /// LayoutField - try to layout all fields in the record decl.
115 /// Returns false if the operation failed because the struct is not packed.
116 bool LayoutFields(const RecordDecl *D);
117
Anders Carlsson860453c2010-12-04 23:59:48 +0000118 /// Layout a single base, virtual or non-virtual
John McCallfd577d62011-02-15 22:21:29 +0000119 void LayoutBase(const CXXRecordDecl *base,
120 const CGRecordLayout &baseLayout,
121 CharUnits baseOffset);
Anders Carlsson860453c2010-12-04 23:59:48 +0000122
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000123 /// LayoutVirtualBase - layout a single virtual base.
John McCallfd577d62011-02-15 22:21:29 +0000124 void LayoutVirtualBase(const CXXRecordDecl *base,
125 CharUnits baseOffset);
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000126
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000127 /// LayoutVirtualBases - layout the virtual bases of a record decl.
128 void LayoutVirtualBases(const CXXRecordDecl *RD,
129 const ASTRecordLayout &Layout);
130
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000131 /// LayoutNonVirtualBase - layout a single non-virtual base.
John McCallfd577d62011-02-15 22:21:29 +0000132 void LayoutNonVirtualBase(const CXXRecordDecl *base,
133 CharUnits baseOffset);
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000134
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000135 /// LayoutNonVirtualBases - layout the virtual bases of a record decl.
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000136 void LayoutNonVirtualBases(const CXXRecordDecl *RD,
137 const ASTRecordLayout &Layout);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000138
Anders Carlsson3d155e62010-11-09 05:25:47 +0000139 /// ComputeNonVirtualBaseType - Compute the non-virtual base field types.
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000140 bool ComputeNonVirtualBaseType(const CXXRecordDecl *RD);
Anders Carlsson3d155e62010-11-09 05:25:47 +0000141
Daniel Dunbar270e2032010-03-31 00:11:27 +0000142 /// LayoutField - layout a single field. Returns false if the operation failed
143 /// because the current struct is not packed.
144 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
145
146 /// LayoutBitField - layout a single bit field.
147 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
148
149 /// AppendField - Appends a field with the given offset and type.
John McCallfd577d62011-02-15 22:21:29 +0000150 void AppendField(CharUnits fieldOffset, const llvm::Type *FieldTy);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000151
Daniel Dunbar270e2032010-03-31 00:11:27 +0000152 /// AppendPadding - Appends enough padding bytes so that the total
153 /// struct size is a multiple of the field alignment.
John McCallfd577d62011-02-15 22:21:29 +0000154 void AppendPadding(CharUnits fieldOffset, CharUnits fieldAlignment);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000155
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000156 /// ResizeLastBaseFieldIfNecessary - Fields and bases can be laid out in the
157 /// tail padding of a previous base. If this happens, the type of the previous
158 /// base needs to be changed to an array of i8. Returns true if the last
159 /// laid out base was resized.
160 bool ResizeLastBaseFieldIfNecessary(CharUnits offset);
161
Anders Carlsson3d155e62010-11-09 05:25:47 +0000162 /// getByteArrayType - Returns a byte array type with the given number of
163 /// elements.
John McCallfd577d62011-02-15 22:21:29 +0000164 const llvm::Type *getByteArrayType(CharUnits NumBytes);
Anders Carlsson3d155e62010-11-09 05:25:47 +0000165
Daniel Dunbar270e2032010-03-31 00:11:27 +0000166 /// AppendBytes - Append a given number of bytes to the record.
John McCallfd577d62011-02-15 22:21:29 +0000167 void AppendBytes(CharUnits numBytes);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000168
169 /// AppendTailPadding - Append enough tail padding so that the type will have
170 /// the passed size.
171 void AppendTailPadding(uint64_t RecordSize);
172
John McCallfd577d62011-02-15 22:21:29 +0000173 CharUnits getTypeAlignment(const llvm::Type *Ty) const;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000174
Anders Carlssonfc86d552010-11-28 23:06:23 +0000175 /// getAlignmentAsLLVMStruct - Returns the maximum alignment of all the
176 /// LLVM element types.
John McCallfd577d62011-02-15 22:21:29 +0000177 CharUnits getAlignmentAsLLVMStruct() const;
Anders Carlssonfc86d552010-11-28 23:06:23 +0000178
John McCallf16aa102010-08-22 21:01:12 +0000179 /// CheckZeroInitializable - Check if the given type contains a pointer
Daniel Dunbar270e2032010-03-31 00:11:27 +0000180 /// to data member.
John McCallf16aa102010-08-22 21:01:12 +0000181 void CheckZeroInitializable(QualType T);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000182
183public:
184 CGRecordLayoutBuilder(CodeGenTypes &Types)
John McCall9b7da1c2011-02-15 06:40:56 +0000185 : BaseSubobjectType(0),
186 IsZeroInitializable(true), IsZeroInitializableAsBase(true),
John McCallfd577d62011-02-15 22:21:29 +0000187 Packed(false), Types(Types), BitsAvailableInLastField(0) { }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000188
189 /// Layout - Will layout a RecordDecl.
190 void Layout(const RecordDecl *D);
191};
192
193}
Daniel Dunbar270e2032010-03-31 00:11:27 +0000194
Anders Carlsson45372a62009-07-23 03:17:50 +0000195void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
John McCallfd577d62011-02-15 22:21:29 +0000196 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment();
Anders Carlssond0eb3b92009-09-02 17:51:33 +0000197 Packed = D->hasAttr<PackedAttr>();
Anders Carlssona5dd7222009-08-08 19:38:24 +0000198
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000199 if (D->isUnion()) {
200 LayoutUnion(D);
201 return;
202 }
Anders Carlssona860e752009-08-08 18:23:56 +0000203
Anders Carlsson45372a62009-07-23 03:17:50 +0000204 if (LayoutFields(D))
205 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Anders Carlsson45372a62009-07-23 03:17:50 +0000207 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000208 Packed = true;
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000209 LastLaidOutBase.invalidate();
John McCallfd577d62011-02-15 22:21:29 +0000210 NextFieldOffset = CharUnits::Zero();
Anders Carlsson45372a62009-07-23 03:17:50 +0000211 FieldTypes.clear();
John McCall9b7da1c2011-02-15 06:40:56 +0000212 Fields.clear();
213 BitFields.clear();
214 NonVirtualBases.clear();
215 VirtualBases.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Anders Carlsson45372a62009-07-23 03:17:50 +0000217 LayoutFields(D);
218}
219
Daniel Dunbare7a80bd2010-09-02 23:53:28 +0000220CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
221 const FieldDecl *FD,
222 uint64_t FieldOffset,
223 uint64_t FieldSize,
224 uint64_t ContainingTypeSizeInBits,
225 unsigned ContainingTypeAlign) {
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000226 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(FD->getType());
John McCall92ee7ca2011-02-26 08:41:59 +0000227 CharUnits TypeSizeInBytes =
228 CharUnits::fromQuantity(Types.getTargetData().getTypeAllocSize(Ty));
229 uint64_t TypeSizeInBits = Types.getContext().toBits(TypeSizeInBytes);
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000230
231 bool IsSigned = FD->getType()->isSignedIntegerType();
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000232
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000233 if (FieldSize > TypeSizeInBits) {
Anders Carlsson6ba38152010-04-17 22:54:57 +0000234 // We have a wide bit-field. The extra bits are only used for padding, so
235 // if we have a bitfield of type T, with size N:
236 //
237 // T t : N;
238 //
239 // We can just assume that it's:
240 //
241 // T t : sizeof(T);
242 //
243 FieldSize = TypeSizeInBits;
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000244 }
245
Chris Lattnerd8df5b62011-02-17 22:09:58 +0000246 // in big-endian machines the first fields are in higher bit positions,
247 // so revert the offset. The byte offsets are reversed(back) later.
248 if (Types.getTargetData().isBigEndian()) {
249 FieldOffset = ((ContainingTypeSizeInBits)-FieldOffset-FieldSize);
250 }
251
Daniel Dunbare1467a42010-04-22 02:35:46 +0000252 // Compute the access components. The policy we use is to start by attempting
253 // to access using the width of the bit-field type itself and to always access
254 // at aligned indices of that type. If such an access would fail because it
255 // extends past the bound of the type, then we reduce size to the next smaller
256 // power of two and retry. The current algorithm assumes pow2 sized types,
257 // although this is easy to fix.
258 //
Daniel Dunbare1467a42010-04-22 02:35:46 +0000259 assert(llvm::isPowerOf2_32(TypeSizeInBits) && "Unexpected type size!");
260 CGBitFieldInfo::AccessInfo Components[3];
261 unsigned NumComponents = 0;
Nick Lewyckyc3e49402011-03-22 17:35:47 +0000262 unsigned AccessedTargetBits = 0; // The number of target bits accessed.
Daniel Dunbare1467a42010-04-22 02:35:46 +0000263 unsigned AccessWidth = TypeSizeInBits; // The current access width to attempt.
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000264
Daniel Dunbare1467a42010-04-22 02:35:46 +0000265 // Round down from the field offset to find the first access position that is
266 // at an aligned offset of the initial access type.
Daniel Dunbar52968a12010-04-22 15:22:33 +0000267 uint64_t AccessStart = FieldOffset - (FieldOffset % AccessWidth);
268
269 // Adjust initial access size to fit within record.
John McCall92ee7ca2011-02-26 08:41:59 +0000270 while (AccessWidth > Types.getTarget().getCharWidth() &&
Daniel Dunbar52968a12010-04-22 15:22:33 +0000271 AccessStart + AccessWidth > ContainingTypeSizeInBits) {
272 AccessWidth >>= 1;
273 AccessStart = FieldOffset - (FieldOffset % AccessWidth);
274 }
Daniel Dunbar2df25692010-04-15 05:09:32 +0000275
Daniel Dunbare1467a42010-04-22 02:35:46 +0000276 while (AccessedTargetBits < FieldSize) {
277 // Check that we can access using a type of this size, without reading off
278 // the end of the structure. This can occur with packed structures and
279 // -fno-bitfield-type-align, for example.
280 if (AccessStart + AccessWidth > ContainingTypeSizeInBits) {
281 // If so, reduce access size to the next smaller power-of-two and retry.
282 AccessWidth >>= 1;
John McCall92ee7ca2011-02-26 08:41:59 +0000283 assert(AccessWidth >= Types.getTarget().getCharWidth()
284 && "Cannot access under byte size!");
Daniel Dunbare1467a42010-04-22 02:35:46 +0000285 continue;
286 }
Daniel Dunbarab970f92010-04-13 20:58:55 +0000287
Daniel Dunbare1467a42010-04-22 02:35:46 +0000288 // Otherwise, add an access component.
Daniel Dunbarab970f92010-04-13 20:58:55 +0000289
Daniel Dunbare1467a42010-04-22 02:35:46 +0000290 // First, compute the bits inside this access which are part of the
291 // target. We are reading bits [AccessStart, AccessStart + AccessWidth); the
292 // intersection with [FieldOffset, FieldOffset + FieldSize) gives the bits
293 // in the target that we are reading.
Daniel Dunbar52968a12010-04-22 15:22:33 +0000294 assert(FieldOffset < AccessStart + AccessWidth && "Invalid access start!");
295 assert(AccessStart < FieldOffset + FieldSize && "Invalid access start!");
Daniel Dunbare1467a42010-04-22 02:35:46 +0000296 uint64_t AccessBitsInFieldStart = std::max(AccessStart, FieldOffset);
297 uint64_t AccessBitsInFieldSize =
Daniel Dunbar52968a12010-04-22 15:22:33 +0000298 std::min(AccessWidth + AccessStart,
299 FieldOffset + FieldSize) - AccessBitsInFieldStart;
Daniel Dunbar4651efb2010-04-22 14:56:10 +0000300
Daniel Dunbare1467a42010-04-22 02:35:46 +0000301 assert(NumComponents < 3 && "Unexpected number of components!");
302 CGBitFieldInfo::AccessInfo &AI = Components[NumComponents++];
303 AI.FieldIndex = 0;
304 // FIXME: We still follow the old access pattern of only using the field
305 // byte offset. We should switch this once we fix the struct layout to be
306 // pretty.
Chris Lattnerd8df5b62011-02-17 22:09:58 +0000307
308 // on big-endian machines we reverted the bit offset because first fields are
309 // in higher bits. But this also reverts the bytes, so fix this here by reverting
310 // the byte offset on big-endian machines.
311 if (Types.getTargetData().isBigEndian()) {
Ken Dyck28ebde52011-04-24 10:04:59 +0000312 AI.FieldByteOffset = Types.getContext().toCharUnitsFromBits(
313 ContainingTypeSizeInBits - AccessStart - AccessWidth);
Chris Lattnerd8df5b62011-02-17 22:09:58 +0000314 } else {
Ken Dyck28ebde52011-04-24 10:04:59 +0000315 AI.FieldByteOffset = Types.getContext().toCharUnitsFromBits(AccessStart);
Chris Lattnerd8df5b62011-02-17 22:09:58 +0000316 }
Daniel Dunbare1467a42010-04-22 02:35:46 +0000317 AI.FieldBitStart = AccessBitsInFieldStart - AccessStart;
318 AI.AccessWidth = AccessWidth;
Ken Dyckb9e6b2c2011-04-24 10:13:17 +0000319 AI.AccessAlignment = Types.getContext().toCharUnitsFromBits(
320 llvm::MinAlign(ContainingTypeAlign, AccessStart));
Daniel Dunbare1467a42010-04-22 02:35:46 +0000321 AI.TargetBitOffset = AccessedTargetBits;
322 AI.TargetBitWidth = AccessBitsInFieldSize;
323
324 AccessStart += AccessWidth;
325 AccessedTargetBits += AI.TargetBitWidth;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000326 }
327
Daniel Dunbare1467a42010-04-22 02:35:46 +0000328 assert(AccessedTargetBits == FieldSize && "Invalid bit-field access!");
Daniel Dunbar2df25692010-04-15 05:09:32 +0000329 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000330}
331
Daniel Dunbare7a80bd2010-09-02 23:53:28 +0000332CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
333 const FieldDecl *FD,
334 uint64_t FieldOffset,
335 uint64_t FieldSize) {
336 const RecordDecl *RD = FD->getParent();
337 const ASTRecordLayout &RL = Types.getContext().getASTRecordLayout(RD);
Ken Dyckdd76a9a2011-02-11 01:54:29 +0000338 uint64_t ContainingTypeSizeInBits = Types.getContext().toBits(RL.getSize());
Ken Dyckdac54c12011-02-15 02:32:40 +0000339 unsigned ContainingTypeAlign = Types.getContext().toBits(RL.getAlignment());
Daniel Dunbare7a80bd2010-09-02 23:53:28 +0000340
341 return MakeInfo(Types, FD, FieldOffset, FieldSize, ContainingTypeSizeInBits,
342 ContainingTypeAlign);
343}
344
Anders Carlsson45372a62009-07-23 03:17:50 +0000345void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
John McCallfd577d62011-02-15 22:21:29 +0000346 uint64_t fieldOffset) {
347 uint64_t fieldSize =
Anders Carlsson45372a62009-07-23 03:17:50 +0000348 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000349
John McCallfd577d62011-02-15 22:21:29 +0000350 if (fieldSize == 0)
Anders Carlsson45372a62009-07-23 03:17:50 +0000351 return;
352
John McCall92ee7ca2011-02-26 08:41:59 +0000353 uint64_t nextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset);
John McCallfd577d62011-02-15 22:21:29 +0000354 unsigned numBytesToAppend;
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000356 if (fieldOffset < nextFieldOffsetInBits && !BitsAvailableInLastField) {
357 assert(fieldOffset % 8 == 0 && "Field offset not aligned correctly");
358
359 CharUnits fieldOffsetInCharUnits =
360 Types.getContext().toCharUnitsFromBits(fieldOffset);
361
362 // Try to resize the last base field.
363 if (ResizeLastBaseFieldIfNecessary(fieldOffsetInCharUnits))
364 nextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset);
365 }
366
John McCallfd577d62011-02-15 22:21:29 +0000367 if (fieldOffset < nextFieldOffsetInBits) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000368 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
John McCallfd577d62011-02-15 22:21:29 +0000369 assert(!NextFieldOffset.isZero() && "Must have laid out at least one byte");
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Anders Carlsson45372a62009-07-23 03:17:50 +0000371 // The bitfield begins in the previous bit-field.
John McCallfd577d62011-02-15 22:21:29 +0000372 numBytesToAppend =
373 llvm::RoundUpToAlignment(fieldSize - BitsAvailableInLastField, 8) / 8;
Anders Carlsson45372a62009-07-23 03:17:50 +0000374 } else {
John McCallfd577d62011-02-15 22:21:29 +0000375 assert(fieldOffset % 8 == 0 && "Field offset not aligned correctly");
Anders Carlsson45372a62009-07-23 03:17:50 +0000376
377 // Append padding if necessary.
John McCallfd577d62011-02-15 22:21:29 +0000378 AppendPadding(CharUnits::fromQuantity(fieldOffset / 8), CharUnits::One());
Mike Stump1eb44332009-09-09 15:08:12 +0000379
John McCallfd577d62011-02-15 22:21:29 +0000380 numBytesToAppend = llvm::RoundUpToAlignment(fieldSize, 8) / 8;
Mike Stump1eb44332009-09-09 15:08:12 +0000381
John McCallfd577d62011-02-15 22:21:29 +0000382 assert(numBytesToAppend && "No bytes to append!");
Anders Carlsson45372a62009-07-23 03:17:50 +0000383 }
384
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000385 // Add the bit field info.
John McCall9b7da1c2011-02-15 06:40:56 +0000386 BitFields.insert(std::make_pair(D,
John McCallfd577d62011-02-15 22:21:29 +0000387 CGBitFieldInfo::MakeInfo(Types, D, fieldOffset, fieldSize)));
Mike Stump1eb44332009-09-09 15:08:12 +0000388
John McCallfd577d62011-02-15 22:21:29 +0000389 AppendBytes(CharUnits::fromQuantity(numBytesToAppend));
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Mike Stump1eb44332009-09-09 15:08:12 +0000391 BitsAvailableInLastField =
John McCallfd577d62011-02-15 22:21:29 +0000392 NextFieldOffset.getQuantity() * 8 - (fieldOffset + fieldSize);
Anders Carlsson45372a62009-07-23 03:17:50 +0000393}
394
395bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
John McCallfd577d62011-02-15 22:21:29 +0000396 uint64_t fieldOffset) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000397 // If the field is packed, then we need a packed struct.
Anders Carlssona860e752009-08-08 18:23:56 +0000398 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson45372a62009-07-23 03:17:50 +0000399 return false;
400
401 if (D->isBitField()) {
402 // We must use packed structs for unnamed bit fields since they
403 // don't affect the struct alignment.
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000404 if (!Packed && !D->getDeclName())
Anders Carlsson45372a62009-07-23 03:17:50 +0000405 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000406
John McCallfd577d62011-02-15 22:21:29 +0000407 LayoutBitField(D, fieldOffset);
Anders Carlsson45372a62009-07-23 03:17:50 +0000408 return true;
409 }
Mike Stump1eb44332009-09-09 15:08:12 +0000410
John McCallf16aa102010-08-22 21:01:12 +0000411 CheckZeroInitializable(D->getType());
Daniel Dunbar270e2032010-03-31 00:11:27 +0000412
John McCall92ee7ca2011-02-26 08:41:59 +0000413 assert(fieldOffset % Types.getTarget().getCharWidth() == 0
414 && "field offset is not on a byte boundary!");
415 CharUnits fieldOffsetInBytes
416 = Types.getContext().toCharUnitsFromBits(fieldOffset);
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000418 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
John McCallfd577d62011-02-15 22:21:29 +0000419 CharUnits typeAlignment = getTypeAlignment(Ty);
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000420
Anders Carlssona5dd7222009-08-08 19:38:24 +0000421 // If the type alignment is larger then the struct alignment, we must use
422 // a packed struct.
John McCallfd577d62011-02-15 22:21:29 +0000423 if (typeAlignment > Alignment) {
Anders Carlssona5dd7222009-08-08 19:38:24 +0000424 assert(!Packed && "Alignment is wrong even with packed struct!");
425 return false;
426 }
Mike Stump1eb44332009-09-09 15:08:12 +0000427
John McCallfd577d62011-02-15 22:21:29 +0000428 if (!Packed) {
429 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
430 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
431 if (const MaxFieldAlignmentAttr *MFAA =
432 RD->getAttr<MaxFieldAlignmentAttr>()) {
John McCall92ee7ca2011-02-26 08:41:59 +0000433 if (MFAA->getAlignment() != Types.getContext().toBits(typeAlignment))
John McCallfd577d62011-02-15 22:21:29 +0000434 return false;
435 }
Anders Carlssona5dd7222009-08-08 19:38:24 +0000436 }
437 }
438
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000439 // Round up the field offset to the alignment of the field type.
John McCallfd577d62011-02-15 22:21:29 +0000440 CharUnits alignedNextFieldOffsetInBytes =
441 NextFieldOffset.RoundUpToAlignment(typeAlignment);
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000442
John McCallfd577d62011-02-15 22:21:29 +0000443 if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000444 // Try to resize the last base field.
445 if (ResizeLastBaseFieldIfNecessary(fieldOffsetInBytes)) {
446 alignedNextFieldOffsetInBytes =
447 NextFieldOffset.RoundUpToAlignment(typeAlignment);
448 }
449 }
450
451 if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000452 assert(!Packed && "Could not place field even with packed struct!");
453 return false;
454 }
Mike Stump1eb44332009-09-09 15:08:12 +0000455
John McCallfd577d62011-02-15 22:21:29 +0000456 AppendPadding(fieldOffsetInBytes, typeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Anders Carlsson45372a62009-07-23 03:17:50 +0000458 // Now append the field.
John McCall9b7da1c2011-02-15 06:40:56 +0000459 Fields[D] = FieldTypes.size();
John McCallfd577d62011-02-15 22:21:29 +0000460 AppendField(fieldOffsetInBytes, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000462 LastLaidOutBase.invalidate();
Anders Carlsson45372a62009-07-23 03:17:50 +0000463 return true;
464}
465
Anders Carlsson86664462010-04-17 20:49:27 +0000466const llvm::Type *
467CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
468 const ASTRecordLayout &Layout) {
469 if (Field->isBitField()) {
470 uint64_t FieldSize =
471 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
472
473 // Ignore zero sized bit fields.
474 if (FieldSize == 0)
475 return 0;
476
Daniel Dunbar8ab78a72010-04-20 17:52:30 +0000477 const llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
478 unsigned NumBytesToAppend =
479 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Anders Carlssond62328e2010-04-17 21:04:52 +0000480
Daniel Dunbar8ab78a72010-04-20 17:52:30 +0000481 if (NumBytesToAppend > 1)
482 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend);
Anders Carlssond62328e2010-04-17 21:04:52 +0000483
Anders Carlsson86664462010-04-17 20:49:27 +0000484 // Add the bit field info.
John McCall9b7da1c2011-02-15 06:40:56 +0000485 BitFields.insert(std::make_pair(Field,
486 CGBitFieldInfo::MakeInfo(Types, Field, 0, FieldSize)));
Anders Carlssond62328e2010-04-17 21:04:52 +0000487 return FieldTy;
Anders Carlsson86664462010-04-17 20:49:27 +0000488 }
Daniel Dunbar8ab78a72010-04-20 17:52:30 +0000489
Anders Carlsson86664462010-04-17 20:49:27 +0000490 // This is a regular union field.
John McCall9b7da1c2011-02-15 06:40:56 +0000491 Fields[Field] = 0;
Anders Carlsson86664462010-04-17 20:49:27 +0000492 return Types.ConvertTypeForMemRecursive(Field->getType());
493}
494
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000495void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
496 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump1eb44332009-09-09 15:08:12 +0000497
John McCallfd577d62011-02-15 22:21:29 +0000498 const ASTRecordLayout &layout = Types.getContext().getASTRecordLayout(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000499
John McCallfd577d62011-02-15 22:21:29 +0000500 const llvm::Type *unionType = 0;
501 CharUnits unionSize = CharUnits::Zero();
502 CharUnits unionAlign = CharUnits::Zero();
Mike Stump1eb44332009-09-09 15:08:12 +0000503
John McCallfd577d62011-02-15 22:21:29 +0000504 bool hasOnlyZeroSizedBitFields = true;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000505
John McCallfd577d62011-02-15 22:21:29 +0000506 unsigned fieldNo = 0;
507 for (RecordDecl::field_iterator field = D->field_begin(),
508 fieldEnd = D->field_end(); field != fieldEnd; ++field, ++fieldNo) {
509 assert(layout.getFieldOffset(fieldNo) == 0 &&
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000510 "Union field offset did not start at the beginning of record!");
John McCallfd577d62011-02-15 22:21:29 +0000511 const llvm::Type *fieldType = LayoutUnionField(*field, layout);
Anders Carlsson2cc8f172009-07-23 04:00:39 +0000512
John McCallfd577d62011-02-15 22:21:29 +0000513 if (!fieldType)
Anders Carlsson86664462010-04-17 20:49:27 +0000514 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000515
John McCallfd577d62011-02-15 22:21:29 +0000516 hasOnlyZeroSizedBitFields = false;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000517
John McCallfd577d62011-02-15 22:21:29 +0000518 CharUnits fieldAlign = CharUnits::fromQuantity(
519 Types.getTargetData().getABITypeAlignment(fieldType));
520 CharUnits fieldSize = CharUnits::fromQuantity(
521 Types.getTargetData().getTypeAllocSize(fieldType));
Mike Stump1eb44332009-09-09 15:08:12 +0000522
John McCallfd577d62011-02-15 22:21:29 +0000523 if (fieldAlign < unionAlign)
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000524 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000525
John McCallfd577d62011-02-15 22:21:29 +0000526 if (fieldAlign > unionAlign || fieldSize > unionSize) {
527 unionType = fieldType;
528 unionAlign = fieldAlign;
529 unionSize = fieldSize;
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000530 }
531 }
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000533 // Now add our field.
John McCallfd577d62011-02-15 22:21:29 +0000534 if (unionType) {
535 AppendField(CharUnits::Zero(), unionType);
Anders Carlsson36620002009-09-03 22:56:02 +0000536
John McCallfd577d62011-02-15 22:21:29 +0000537 if (getTypeAlignment(unionType) > layout.getAlignment()) {
Anders Carlsson36620002009-09-03 22:56:02 +0000538 // We need a packed struct.
539 Packed = true;
John McCallfd577d62011-02-15 22:21:29 +0000540 unionAlign = CharUnits::One();
Anders Carlsson36620002009-09-03 22:56:02 +0000541 }
542 }
John McCallfd577d62011-02-15 22:21:29 +0000543 if (unionAlign.isZero()) {
544 assert(hasOnlyZeroSizedBitFields &&
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000545 "0-align record did not have all zero-sized bit-fields!");
John McCallfd577d62011-02-15 22:21:29 +0000546 unionAlign = CharUnits::One();
Fariborz Jahaniane5041702009-11-06 20:47:40 +0000547 }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000548
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000549 // Append tail padding.
John McCallfd577d62011-02-15 22:21:29 +0000550 CharUnits recordSize = layout.getSize();
551 if (recordSize > unionSize)
552 AppendPadding(recordSize, unionAlign);
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000553}
554
John McCall9b7da1c2011-02-15 06:40:56 +0000555void CGRecordLayoutBuilder::LayoutBase(const CXXRecordDecl *base,
John McCallfd577d62011-02-15 22:21:29 +0000556 const CGRecordLayout &baseLayout,
557 CharUnits baseOffset) {
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000558 ResizeLastBaseFieldIfNecessary(baseOffset);
559
John McCallfd577d62011-02-15 22:21:29 +0000560 AppendPadding(baseOffset, CharUnits::One());
Anders Carlsson860453c2010-12-04 23:59:48 +0000561
John McCall9b7da1c2011-02-15 06:40:56 +0000562 const ASTRecordLayout &baseASTLayout
563 = Types.getContext().getASTRecordLayout(base);
564
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000565 LastLaidOutBase.Offset = NextFieldOffset;
566 LastLaidOutBase.NonVirtualSize = baseASTLayout.getNonVirtualSize();
567
John McCallfd577d62011-02-15 22:21:29 +0000568 // Fields and bases can be laid out in the tail padding of previous
569 // bases. If this happens, we need to allocate the base as an i8
570 // array; otherwise, we can use the subobject type. However,
571 // actually doing that would require knowledge of what immediately
572 // follows this base in the layout, so instead we do a conservative
573 // approximation, which is to use the base subobject type if it
574 // has the same LLVM storage size as the nvsize.
575
John McCallfd577d62011-02-15 22:21:29 +0000576 const llvm::StructType *subobjectType = baseLayout.getBaseSubobjectLLVMType();
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000577 AppendField(baseOffset, subobjectType);
John McCallfd577d62011-02-15 22:21:29 +0000578
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000579 Types.addBaseSubobjectTypeName(base, baseLayout);
John McCall9b7da1c2011-02-15 06:40:56 +0000580}
581
582void CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *base,
John McCallfd577d62011-02-15 22:21:29 +0000583 CharUnits baseOffset) {
John McCall9b7da1c2011-02-15 06:40:56 +0000584 // Ignore empty bases.
585 if (base->isEmpty()) return;
586
587 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
588 if (IsZeroInitializableAsBase) {
589 assert(IsZeroInitializable &&
590 "class zero-initializable as base but not as complete object");
591
592 IsZeroInitializable = IsZeroInitializableAsBase =
593 baseLayout.isZeroInitializableAsBase();
594 }
595
John McCallfd577d62011-02-15 22:21:29 +0000596 LayoutBase(base, baseLayout, baseOffset);
John McCall9b7da1c2011-02-15 06:40:56 +0000597 NonVirtualBases[base] = (FieldTypes.size() - 1);
Anders Carlsson860453c2010-12-04 23:59:48 +0000598}
599
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000600void
John McCall9b7da1c2011-02-15 06:40:56 +0000601CGRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *base,
John McCallfd577d62011-02-15 22:21:29 +0000602 CharUnits baseOffset) {
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000603 // Ignore empty bases.
John McCall9b7da1c2011-02-15 06:40:56 +0000604 if (base->isEmpty()) return;
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000605
John McCall9b7da1c2011-02-15 06:40:56 +0000606 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
607 if (IsZeroInitializable)
608 IsZeroInitializable = baseLayout.isZeroInitializableAsBase();
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000609
John McCallfd577d62011-02-15 22:21:29 +0000610 LayoutBase(base, baseLayout, baseOffset);
John McCall9b7da1c2011-02-15 06:40:56 +0000611 VirtualBases[base] = (FieldTypes.size() - 1);
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000612}
613
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000614/// LayoutVirtualBases - layout the non-virtual bases of a record decl.
615void
616CGRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
617 const ASTRecordLayout &Layout) {
618 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
619 E = RD->bases_end(); I != E; ++I) {
620 const CXXRecordDecl *BaseDecl =
621 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
622
623 // We only want to lay out virtual bases that aren't indirect primary bases
624 // of some other base.
625 if (I->isVirtual() && !IndirectPrimaryBases.count(BaseDecl)) {
626 // Only lay out the base once.
627 if (!LaidOutVirtualBases.insert(BaseDecl))
628 continue;
629
John McCallfd577d62011-02-15 22:21:29 +0000630 CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl);
631 LayoutVirtualBase(BaseDecl, vbaseOffset);
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000632 }
633
634 if (!BaseDecl->getNumVBases()) {
635 // This base isn't interesting since it doesn't have any virtual bases.
636 continue;
637 }
638
639 LayoutVirtualBases(BaseDecl, Layout);
640 }
641}
642
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000643void
644CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD,
645 const ASTRecordLayout &Layout) {
646 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
647
648 // Check if we need to add a vtable pointer.
649 if (RD->isDynamicClass()) {
650 if (!PrimaryBase) {
651 const llvm::Type *FunctionType =
652 llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()),
653 /*isVarArg=*/true);
654 const llvm::Type *VTableTy = FunctionType->getPointerTo();
655
John McCallfd577d62011-02-15 22:21:29 +0000656 assert(NextFieldOffset.isZero() &&
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000657 "VTable pointer must come first!");
John McCallfd577d62011-02-15 22:21:29 +0000658 AppendField(CharUnits::Zero(), VTableTy->getPointerTo());
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000659 } else {
Anders Carlssonc9e814b2010-11-24 23:12:57 +0000660 if (!Layout.isPrimaryBaseVirtual())
John McCallfd577d62011-02-15 22:21:29 +0000661 LayoutNonVirtualBase(PrimaryBase, CharUnits::Zero());
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000662 else
John McCallfd577d62011-02-15 22:21:29 +0000663 LayoutVirtualBase(PrimaryBase, CharUnits::Zero());
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000664 }
665 }
666
667 // Layout the non-virtual bases.
668 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
669 E = RD->bases_end(); I != E; ++I) {
670 if (I->isVirtual())
671 continue;
672
673 const CXXRecordDecl *BaseDecl =
674 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
675
676 // We've already laid out the primary base.
Anders Carlssonc9e814b2010-11-24 23:12:57 +0000677 if (BaseDecl == PrimaryBase && !Layout.isPrimaryBaseVirtual())
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000678 continue;
679
John McCallfd577d62011-02-15 22:21:29 +0000680 LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffset(BaseDecl));
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000681 }
682}
683
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000684bool
Anders Carlsson3d155e62010-11-09 05:25:47 +0000685CGRecordLayoutBuilder::ComputeNonVirtualBaseType(const CXXRecordDecl *RD) {
686 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(RD);
687
Ken Dyck68cf1a52011-02-08 02:02:47 +0000688 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
689 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
John McCallfd577d62011-02-15 22:21:29 +0000690 CharUnits AlignedNonVirtualTypeSize =
691 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
Anders Carlsson3d155e62010-11-09 05:25:47 +0000692
Anders Carlsson3d155e62010-11-09 05:25:47 +0000693 // First check if we can use the same fields as for the complete class.
John McCallfd577d62011-02-15 22:21:29 +0000694 CharUnits RecordSize = Layout.getSize();
John McCall9b7da1c2011-02-15 06:40:56 +0000695 if (AlignedNonVirtualTypeSize == RecordSize)
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000696 return true;
Anders Carlsson3d155e62010-11-09 05:25:47 +0000697
Anders Carlsson3d155e62010-11-09 05:25:47 +0000698 // Check if we need padding.
John McCallfd577d62011-02-15 22:21:29 +0000699 CharUnits AlignedNextFieldOffset =
700 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
Anders Carlsson3d155e62010-11-09 05:25:47 +0000701
John McCall9b7da1c2011-02-15 06:40:56 +0000702 if (AlignedNextFieldOffset > AlignedNonVirtualTypeSize) {
703 assert(!Packed && "cannot layout even as packed struct");
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000704 return false; // Needs packing.
Anders Carlsson3d155e62010-11-09 05:25:47 +0000705 }
706
John McCall9b7da1c2011-02-15 06:40:56 +0000707 bool needsPadding = (AlignedNonVirtualTypeSize != AlignedNextFieldOffset);
708 if (needsPadding) {
John McCallfd577d62011-02-15 22:21:29 +0000709 CharUnits NumBytes = AlignedNonVirtualTypeSize - AlignedNextFieldOffset;
John McCall9b7da1c2011-02-15 06:40:56 +0000710 FieldTypes.push_back(getByteArrayType(NumBytes));
711 }
712
713 BaseSubobjectType = llvm::StructType::get(Types.getLLVMContext(),
714 FieldTypes, Packed);
715
716 if (needsPadding) {
717 // Pull the padding back off.
718 FieldTypes.pop_back();
719 }
720
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000721 return true;
Anders Carlsson3d155e62010-11-09 05:25:47 +0000722}
723
Anders Carlsson45372a62009-07-23 03:17:50 +0000724bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
725 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
John McCallfd577d62011-02-15 22:21:29 +0000726 assert(!Alignment.isZero() && "Did not set alignment!");
Mike Stump1eb44332009-09-09 15:08:12 +0000727
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000728 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Anders Carlsson3d155e62010-11-09 05:25:47 +0000730 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
731 if (RD)
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000732 LayoutNonVirtualBases(RD, Layout);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000733
Anders Carlsson45372a62009-07-23 03:17:50 +0000734 unsigned FieldNo = 0;
Fariborz Jahaniancad86652009-07-27 20:57:45 +0000735
Mike Stump1eb44332009-09-09 15:08:12 +0000736 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson45372a62009-07-23 03:17:50 +0000737 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
738 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000739 assert(!Packed &&
Anders Carlsson45372a62009-07-23 03:17:50 +0000740 "Could not layout fields even with a packed LLVM struct!");
741 return false;
742 }
743 }
744
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000745 if (RD) {
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000746 // We've laid out the non-virtual bases and the fields, now compute the
747 // non-virtual base field types.
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000748 if (!ComputeNonVirtualBaseType(RD)) {
749 assert(!Packed && "Could not layout even with a packed LLVM struct!");
750 return false;
751 }
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000752
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000753 // And lay out the virtual bases.
754 RD->getIndirectPrimaryBases(IndirectPrimaryBases);
755 if (Layout.isPrimaryBaseVirtual())
756 IndirectPrimaryBases.insert(Layout.getPrimaryBase());
757 LayoutVirtualBases(RD, Layout);
758 }
Anders Carlsson3d155e62010-11-09 05:25:47 +0000759
Anders Carlsson45372a62009-07-23 03:17:50 +0000760 // Append tail padding if necessary.
Ken Dyckdd76a9a2011-02-11 01:54:29 +0000761 AppendTailPadding(Types.getContext().toBits(Layout.getSize()));
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Anders Carlsson45372a62009-07-23 03:17:50 +0000763 return true;
764}
765
Anders Carlssonc1efe362009-07-27 14:55:54 +0000766void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) {
767 assert(RecordSize % 8 == 0 && "Invalid record size!");
Mike Stump1eb44332009-09-09 15:08:12 +0000768
John McCall92ee7ca2011-02-26 08:41:59 +0000769 CharUnits RecordSizeInBytes =
770 Types.getContext().toCharUnitsFromBits(RecordSize);
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000771 ResizeLastBaseFieldIfNecessary(RecordSizeInBytes);
772
John McCallfd577d62011-02-15 22:21:29 +0000773 assert(NextFieldOffset <= RecordSizeInBytes && "Size mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000774
John McCallfd577d62011-02-15 22:21:29 +0000775 CharUnits AlignedNextFieldOffset =
776 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
Anders Carlssonc2456822009-12-08 01:24:23 +0000777
778 if (AlignedNextFieldOffset == RecordSizeInBytes) {
779 // We don't need any padding.
780 return;
781 }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000782
John McCallfd577d62011-02-15 22:21:29 +0000783 CharUnits NumPadBytes = RecordSizeInBytes - NextFieldOffset;
Anders Carlssonc1efe362009-07-27 14:55:54 +0000784 AppendBytes(NumPadBytes);
785}
786
John McCallfd577d62011-02-15 22:21:29 +0000787void CGRecordLayoutBuilder::AppendField(CharUnits fieldOffset,
788 const llvm::Type *fieldType) {
789 CharUnits fieldSize =
790 CharUnits::fromQuantity(Types.getTargetData().getTypeAllocSize(fieldType));
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000791
John McCallfd577d62011-02-15 22:21:29 +0000792 FieldTypes.push_back(fieldType);
Anders Carlsson45372a62009-07-23 03:17:50 +0000793
John McCallfd577d62011-02-15 22:21:29 +0000794 NextFieldOffset = fieldOffset + fieldSize;
Anders Carlsson45372a62009-07-23 03:17:50 +0000795 BitsAvailableInLastField = 0;
796}
797
John McCallfd577d62011-02-15 22:21:29 +0000798void CGRecordLayoutBuilder::AppendPadding(CharUnits fieldOffset,
799 CharUnits fieldAlignment) {
800 assert(NextFieldOffset <= fieldOffset &&
Anders Carlsson45372a62009-07-23 03:17:50 +0000801 "Incorrect field layout!");
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Anders Carlsson45372a62009-07-23 03:17:50 +0000803 // Round up the field offset to the alignment of the field type.
John McCallfd577d62011-02-15 22:21:29 +0000804 CharUnits alignedNextFieldOffset =
805 NextFieldOffset.RoundUpToAlignment(fieldAlignment);
Anders Carlsson45372a62009-07-23 03:17:50 +0000806
John McCallfd577d62011-02-15 22:21:29 +0000807 if (alignedNextFieldOffset < fieldOffset) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000808 // Even with alignment, the field offset is not at the right place,
809 // insert padding.
John McCallfd577d62011-02-15 22:21:29 +0000810 CharUnits padding = fieldOffset - NextFieldOffset;
Anders Carlsson45372a62009-07-23 03:17:50 +0000811
John McCallfd577d62011-02-15 22:21:29 +0000812 AppendBytes(padding);
Anders Carlsson45372a62009-07-23 03:17:50 +0000813 }
814}
815
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000816bool CGRecordLayoutBuilder::ResizeLastBaseFieldIfNecessary(CharUnits offset) {
817 // Check if we have a base to resize.
818 if (!LastLaidOutBase.isValid())
819 return false;
820
821 // This offset does not overlap with the tail padding.
822 if (offset >= NextFieldOffset)
823 return false;
824
825 // Restore the field offset and append an i8 array instead.
826 FieldTypes.pop_back();
827 NextFieldOffset = LastLaidOutBase.Offset;
828 AppendBytes(LastLaidOutBase.NonVirtualSize);
829 LastLaidOutBase.invalidate();
830
831 return true;
832}
833
John McCallfd577d62011-02-15 22:21:29 +0000834const llvm::Type *CGRecordLayoutBuilder::getByteArrayType(CharUnits numBytes) {
835 assert(!numBytes.isZero() && "Empty byte arrays aren't allowed.");
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Owen Anderson0032b272009-08-13 21:57:51 +0000837 const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
John McCallfd577d62011-02-15 22:21:29 +0000838 if (numBytes > CharUnits::One())
839 Ty = llvm::ArrayType::get(Ty, numBytes.getQuantity());
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Anders Carlsson3d155e62010-11-09 05:25:47 +0000841 return Ty;
842}
843
John McCallfd577d62011-02-15 22:21:29 +0000844void CGRecordLayoutBuilder::AppendBytes(CharUnits numBytes) {
845 if (numBytes.isZero())
Anders Carlsson3d155e62010-11-09 05:25:47 +0000846 return;
847
Anders Carlsson45372a62009-07-23 03:17:50 +0000848 // Append the padding field
John McCallfd577d62011-02-15 22:21:29 +0000849 AppendField(NextFieldOffset, getByteArrayType(numBytes));
Anders Carlsson45372a62009-07-23 03:17:50 +0000850}
851
John McCallfd577d62011-02-15 22:21:29 +0000852CharUnits CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000853 if (Packed)
John McCallfd577d62011-02-15 22:21:29 +0000854 return CharUnits::One();
Mike Stump1eb44332009-09-09 15:08:12 +0000855
John McCallfd577d62011-02-15 22:21:29 +0000856 return CharUnits::fromQuantity(Types.getTargetData().getABITypeAlignment(Ty));
Anders Carlsson45372a62009-07-23 03:17:50 +0000857}
858
John McCallfd577d62011-02-15 22:21:29 +0000859CharUnits CGRecordLayoutBuilder::getAlignmentAsLLVMStruct() const {
Anders Carlssonfc86d552010-11-28 23:06:23 +0000860 if (Packed)
John McCallfd577d62011-02-15 22:21:29 +0000861 return CharUnits::One();
Anders Carlssonfc86d552010-11-28 23:06:23 +0000862
John McCallfd577d62011-02-15 22:21:29 +0000863 CharUnits maxAlignment = CharUnits::One();
Anders Carlssonfc86d552010-11-28 23:06:23 +0000864 for (size_t i = 0; i != FieldTypes.size(); ++i)
John McCallfd577d62011-02-15 22:21:29 +0000865 maxAlignment = std::max(maxAlignment, getTypeAlignment(FieldTypes[i]));
Anders Carlssonfc86d552010-11-28 23:06:23 +0000866
John McCallfd577d62011-02-15 22:21:29 +0000867 return maxAlignment;
Anders Carlssonfc86d552010-11-28 23:06:23 +0000868}
869
John McCall9b7da1c2011-02-15 06:40:56 +0000870/// Merge in whether a field of the given type is zero-initializable.
John McCallf16aa102010-08-22 21:01:12 +0000871void CGRecordLayoutBuilder::CheckZeroInitializable(QualType T) {
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000872 // This record already contains a member pointer.
John McCall9b7da1c2011-02-15 06:40:56 +0000873 if (!IsZeroInitializableAsBase)
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000874 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000876 // Can only have member pointers if we're compiling C++.
877 if (!Types.getContext().getLangOptions().CPlusPlus)
878 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000879
John McCall9b7da1c2011-02-15 06:40:56 +0000880 const Type *elementType = T->getBaseElementTypeUnsafe();
Mike Stump1eb44332009-09-09 15:08:12 +0000881
John McCall9b7da1c2011-02-15 06:40:56 +0000882 if (const MemberPointerType *MPT = elementType->getAs<MemberPointerType>()) {
John McCallf16aa102010-08-22 21:01:12 +0000883 if (!Types.getCXXABI().isZeroInitializable(MPT))
John McCall9b7da1c2011-02-15 06:40:56 +0000884 IsZeroInitializable = IsZeroInitializableAsBase = false;
885 } else if (const RecordType *RT = elementType->getAs<RecordType>()) {
Anders Carlsson2c12d032010-02-02 05:17:25 +0000886 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
John McCall9b7da1c2011-02-15 06:40:56 +0000887 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
888 if (!Layout.isZeroInitializable())
889 IsZeroInitializable = IsZeroInitializableAsBase = false;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000890 }
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000891}
892
Daniel Dunbar270e2032010-03-31 00:11:27 +0000893CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) {
894 CGRecordLayoutBuilder Builder(*this);
Mike Stump1eb44332009-09-09 15:08:12 +0000895
Anders Carlsson45372a62009-07-23 03:17:50 +0000896 Builder.Layout(D);
Anders Carlsson4c98efd2009-07-24 15:20:52 +0000897
Anders Carlssonba2c2ee2010-11-24 19:37:16 +0000898 const llvm::StructType *Ty = llvm::StructType::get(getLLVMContext(),
899 Builder.FieldTypes,
900 Builder.Packed);
Mike Stump1eb44332009-09-09 15:08:12 +0000901
John McCall9b7da1c2011-02-15 06:40:56 +0000902 // If we're in C++, compute the base subobject type.
Anders Carlssonba2c2ee2010-11-24 19:37:16 +0000903 const llvm::StructType *BaseTy = 0;
Anders Carlsson3d155e62010-11-09 05:25:47 +0000904 if (isa<CXXRecordDecl>(D)) {
John McCall9b7da1c2011-02-15 06:40:56 +0000905 BaseTy = Builder.BaseSubobjectType;
906 if (!BaseTy) BaseTy = Ty;
Anders Carlsson3d155e62010-11-09 05:25:47 +0000907 }
908
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000909 CGRecordLayout *RL =
John McCall9b7da1c2011-02-15 06:40:56 +0000910 new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable,
911 Builder.IsZeroInitializableAsBase);
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000912
John McCall9b7da1c2011-02-15 06:40:56 +0000913 RL->NonVirtualBases.swap(Builder.NonVirtualBases);
914 RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases);
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000915
Anders Carlsson45372a62009-07-23 03:17:50 +0000916 // Add all the field numbers.
John McCall9b7da1c2011-02-15 06:40:56 +0000917 RL->FieldInfo.swap(Builder.Fields);
Anders Carlsson45372a62009-07-23 03:17:50 +0000918
919 // Add bitfield info.
John McCall9b7da1c2011-02-15 06:40:56 +0000920 RL->BitFields.swap(Builder.BitFields);
Mike Stump1eb44332009-09-09 15:08:12 +0000921
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000922 // Dump the layout, if requested.
Daniel Dunbarab970f92010-04-13 20:58:55 +0000923 if (getContext().getLangOptions().DumpRecordLayouts) {
Daniel Dunbar8d8ab742010-04-19 20:44:53 +0000924 llvm::errs() << "\n*** Dumping IRgen Record Layout\n";
Daniel Dunbarab970f92010-04-13 20:58:55 +0000925 llvm::errs() << "Record: ";
926 D->dump();
927 llvm::errs() << "\nLayout: ";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000928 RL->dump();
Daniel Dunbarab970f92010-04-13 20:58:55 +0000929 }
Daniel Dunbar93c62962010-04-12 18:14:18 +0000930
Daniel Dunbare1467a42010-04-22 02:35:46 +0000931#ifndef NDEBUG
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000932 // Verify that the computed LLVM struct size matches the AST layout size.
Anders Carlsson3d155e62010-11-09 05:25:47 +0000933 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
934
Ken Dyckdd76a9a2011-02-11 01:54:29 +0000935 uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize());
Daniel Dunbare1467a42010-04-22 02:35:46 +0000936 assert(TypeSizeInBits == getTargetData().getTypeAllocSizeInBits(Ty) &&
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000937 "Type size mismatch!");
938
Anders Carlsson3d155e62010-11-09 05:25:47 +0000939 if (BaseTy) {
Ken Dyck68cf1a52011-02-08 02:02:47 +0000940 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
941 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
942 CharUnits AlignedNonVirtualTypeSize =
943 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
944
945 uint64_t AlignedNonVirtualTypeSizeInBits =
Ken Dyckdd76a9a2011-02-11 01:54:29 +0000946 getContext().toBits(AlignedNonVirtualTypeSize);
Anders Carlsson3d155e62010-11-09 05:25:47 +0000947
948 assert(AlignedNonVirtualTypeSizeInBits ==
949 getTargetData().getTypeAllocSizeInBits(BaseTy) &&
950 "Type size mismatch!");
951 }
952
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000953 // Verify that the LLVM and AST field offsets agree.
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000954 const llvm::StructType *ST =
955 dyn_cast<llvm::StructType>(RL->getLLVMType());
956 const llvm::StructLayout *SL = getTargetData().getStructLayout(ST);
957
958 const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
959 RecordDecl::field_iterator it = D->field_begin();
960 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
961 const FieldDecl *FD = *it;
Daniel Dunbare1467a42010-04-22 02:35:46 +0000962
963 // For non-bit-fields, just check that the LLVM struct offset matches the
964 // AST offset.
965 if (!FD->isBitField()) {
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000966 unsigned FieldNo = RL->getLLVMFieldNo(FD);
967 assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
968 "Invalid field offset!");
Daniel Dunbare1467a42010-04-22 02:35:46 +0000969 continue;
970 }
971
972 // Ignore unnamed bit-fields.
973 if (!FD->getDeclName())
974 continue;
975
976 const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
977 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
978 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
979
980 // Verify that every component access is within the structure.
981 uint64_t FieldOffset = SL->getElementOffsetInBits(AI.FieldIndex);
John McCall92ee7ca2011-02-26 08:41:59 +0000982 uint64_t AccessBitOffset = FieldOffset +
Ken Dyck28ebde52011-04-24 10:04:59 +0000983 getContext().toBits(AI.FieldByteOffset);
Daniel Dunbare1467a42010-04-22 02:35:46 +0000984 assert(AccessBitOffset + AI.AccessWidth <= TypeSizeInBits &&
985 "Invalid bit-field access (out of range)!");
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000986 }
987 }
988#endif
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000989
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000990 return RL;
Anders Carlsson45372a62009-07-23 03:17:50 +0000991}
Daniel Dunbar93c62962010-04-12 18:14:18 +0000992
993void CGRecordLayout::print(llvm::raw_ostream &OS) const {
994 OS << "<CGRecordLayout\n";
John McCall9b7da1c2011-02-15 06:40:56 +0000995 OS << " LLVMType:" << *CompleteObjectType << "\n";
996 if (BaseSubobjectType)
997 OS << " NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n";
John McCallf16aa102010-08-22 21:01:12 +0000998 OS << " IsZeroInitializable:" << IsZeroInitializable << "\n";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000999 OS << " BitFields:[\n";
Daniel Dunbarad759532010-04-22 02:35:36 +00001000
1001 // Print bit-field infos in declaration order.
1002 std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
Daniel Dunbar93c62962010-04-12 18:14:18 +00001003 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
1004 it = BitFields.begin(), ie = BitFields.end();
1005 it != ie; ++it) {
Daniel Dunbarad759532010-04-22 02:35:36 +00001006 const RecordDecl *RD = it->first->getParent();
1007 unsigned Index = 0;
1008 for (RecordDecl::field_iterator
1009 it2 = RD->field_begin(); *it2 != it->first; ++it2)
1010 ++Index;
1011 BFIs.push_back(std::make_pair(Index, &it->second));
1012 }
1013 llvm::array_pod_sort(BFIs.begin(), BFIs.end());
1014 for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
Daniel Dunbarab970f92010-04-13 20:58:55 +00001015 OS.indent(4);
Daniel Dunbarad759532010-04-22 02:35:36 +00001016 BFIs[i].second->print(OS);
Daniel Dunbar93c62962010-04-12 18:14:18 +00001017 OS << "\n";
1018 }
Daniel Dunbarad759532010-04-22 02:35:36 +00001019
Daniel Dunbar93c62962010-04-12 18:14:18 +00001020 OS << "]>\n";
1021}
1022
1023void CGRecordLayout::dump() const {
1024 print(llvm::errs());
1025}
1026
1027void CGBitFieldInfo::print(llvm::raw_ostream &OS) const {
1028 OS << "<CGBitFieldInfo";
Daniel Dunbar93c62962010-04-12 18:14:18 +00001029 OS << " Size:" << Size;
Daniel Dunbarab970f92010-04-13 20:58:55 +00001030 OS << " IsSigned:" << IsSigned << "\n";
1031
1032 OS.indent(4 + strlen("<CGBitFieldInfo"));
1033 OS << " NumComponents:" << getNumComponents();
1034 OS << " Components: [";
1035 if (getNumComponents()) {
1036 OS << "\n";
1037 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
1038 const AccessInfo &AI = getComponent(i);
1039 OS.indent(8);
1040 OS << "<AccessInfo"
1041 << " FieldIndex:" << AI.FieldIndex
Ken Dyck28ebde52011-04-24 10:04:59 +00001042 << " FieldByteOffset:" << AI.FieldByteOffset.getQuantity()
Daniel Dunbarab970f92010-04-13 20:58:55 +00001043 << " FieldBitStart:" << AI.FieldBitStart
1044 << " AccessWidth:" << AI.AccessWidth << "\n";
1045 OS.indent(8 + strlen("<AccessInfo"));
Ken Dyckb9e6b2c2011-04-24 10:13:17 +00001046 OS << " AccessAlignment:" << AI.AccessAlignment.getQuantity()
Daniel Dunbarab970f92010-04-13 20:58:55 +00001047 << " TargetBitOffset:" << AI.TargetBitOffset
1048 << " TargetBitWidth:" << AI.TargetBitWidth
1049 << ">\n";
1050 }
1051 OS.indent(4);
1052 }
1053 OS << "]>";
Daniel Dunbar93c62962010-04-12 18:14:18 +00001054}
1055
1056void CGBitFieldInfo::dump() const {
1057 print(llvm::errs());
1058}