blob: 2b07bafa0096d9c0a92c754462c84479d7b8d903 [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"
Daniel Dunbare26bdb92011-06-21 18:54:46 +000021#include "clang/Frontend/CodeGenOptions.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000022#include "CodeGenTypes.h"
John McCallf16aa102010-08-22 21:01:12 +000023#include "CGCXXABI.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000024#include "llvm/DerivedTypes.h"
Daniel Dunbar93c62962010-04-12 18:14:18 +000025#include "llvm/Type.h"
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +000026#include "llvm/Support/Debug.h"
Daniel Dunbar93c62962010-04-12 18:14:18 +000027#include "llvm/Support/raw_ostream.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000028#include "llvm/Target/TargetData.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000029using namespace clang;
30using namespace CodeGen;
31
John McCalld0de0ce2010-11-30 23:17:27 +000032namespace {
Daniel Dunbar270e2032010-03-31 00:11:27 +000033
34class CGRecordLayoutBuilder {
35public:
36 /// FieldTypes - Holds the LLVM types that the struct is created from.
John McCall9b7da1c2011-02-15 06:40:56 +000037 ///
Chris Lattner9cbe4f02011-07-09 17:41:47 +000038 llvm::SmallVector<llvm::Type *, 16> FieldTypes;
Daniel Dunbar270e2032010-03-31 00:11:27 +000039
John McCall9b7da1c2011-02-15 06:40:56 +000040 /// BaseSubobjectType - Holds the LLVM type for the non-virtual part
Anders Carlsson3d155e62010-11-09 05:25:47 +000041 /// of the struct. For example, consider:
42 ///
43 /// struct A { int i; };
44 /// struct B { void *v; };
45 /// struct C : virtual A, B { };
46 ///
47 /// The LLVM type of C will be
48 /// %struct.C = type { i32 (...)**, %struct.A, i32, %struct.B }
49 ///
50 /// And the LLVM type of the non-virtual base struct will be
51 /// %struct.C.base = type { i32 (...)**, %struct.A, i32 }
John McCall9b7da1c2011-02-15 06:40:56 +000052 ///
53 /// This only gets initialized if the base subobject type is
54 /// different from the complete-object type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +000055 llvm::StructType *BaseSubobjectType;
Anders Carlsson3d155e62010-11-09 05:25:47 +000056
John McCall9b7da1c2011-02-15 06:40:56 +000057 /// FieldInfo - Holds a field and its corresponding LLVM field number.
58 llvm::DenseMap<const FieldDecl *, unsigned> Fields;
Daniel Dunbar270e2032010-03-31 00:11:27 +000059
John McCall9b7da1c2011-02-15 06:40:56 +000060 /// BitFieldInfo - Holds location and size information about a bit field.
61 llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields;
Daniel Dunbar270e2032010-03-31 00:11:27 +000062
John McCall9b7da1c2011-02-15 06:40:56 +000063 llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases;
64 llvm::DenseMap<const CXXRecordDecl *, unsigned> VirtualBases;
Anders Carlsson46170f92010-11-24 22:50:27 +000065
66 /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are
67 /// primary base classes for some other direct or indirect base class.
68 CXXIndirectPrimaryBaseSet IndirectPrimaryBases;
69
Anders Carlsson1d7dc222010-11-28 19:18:44 +000070 /// LaidOutVirtualBases - A set of all laid out virtual bases, used to avoid
71 /// avoid laying out virtual bases more than once.
72 llvm::SmallPtrSet<const CXXRecordDecl *, 4> LaidOutVirtualBases;
73
John McCallf16aa102010-08-22 21:01:12 +000074 /// IsZeroInitializable - Whether this struct can be C++
75 /// zero-initialized with an LLVM zeroinitializer.
76 bool IsZeroInitializable;
John McCall9b7da1c2011-02-15 06:40:56 +000077 bool IsZeroInitializableAsBase;
Daniel Dunbar270e2032010-03-31 00:11:27 +000078
79 /// Packed - Whether the resulting LLVM struct will be packed or not.
80 bool Packed;
Fariborz Jahanian62055b02011-04-26 23:52:16 +000081
82 /// IsMsStruct - Whether ms_struct is in effect or not
83 bool IsMsStruct;
Daniel Dunbar270e2032010-03-31 00:11:27 +000084
85private:
86 CodeGenTypes &Types;
87
Anders Carlssoneb9d81d2011-04-17 21:56:13 +000088 /// LastLaidOutBaseInfo - Contains the offset and non-virtual size of the
89 /// last base laid out. Used so that we can replace the last laid out base
90 /// type with an i8 array if needed.
91 struct LastLaidOutBaseInfo {
92 CharUnits Offset;
93 CharUnits NonVirtualSize;
94
95 bool isValid() const { return !NonVirtualSize.isZero(); }
96 void invalidate() { NonVirtualSize = CharUnits::Zero(); }
97
98 } LastLaidOutBase;
99
Daniel Dunbar270e2032010-03-31 00:11:27 +0000100 /// Alignment - Contains the alignment of the RecordDecl.
John McCallfd577d62011-02-15 22:21:29 +0000101 CharUnits Alignment;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000102
Daniel Dunbar270e2032010-03-31 00:11:27 +0000103 /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field,
104 /// this will have the number of bits still available in the field.
105 char BitsAvailableInLastField;
106
John McCallfd577d62011-02-15 22:21:29 +0000107 /// NextFieldOffset - Holds the next field offset.
108 CharUnits NextFieldOffset;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000109
Anders Carlsson86664462010-04-17 20:49:27 +0000110 /// LayoutUnionField - Will layout a field in an union and return the type
111 /// that the field will have.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000112 llvm::Type *LayoutUnionField(const FieldDecl *Field,
113 const ASTRecordLayout &Layout);
Anders Carlsson86664462010-04-17 20:49:27 +0000114
Daniel Dunbar270e2032010-03-31 00:11:27 +0000115 /// LayoutUnion - Will layout a union RecordDecl.
116 void LayoutUnion(const RecordDecl *D);
117
118 /// LayoutField - try to layout all fields in the record decl.
119 /// Returns false if the operation failed because the struct is not packed.
120 bool LayoutFields(const RecordDecl *D);
121
Anders Carlsson860453c2010-12-04 23:59:48 +0000122 /// Layout a single base, virtual or non-virtual
John McCallfd577d62011-02-15 22:21:29 +0000123 void LayoutBase(const CXXRecordDecl *base,
124 const CGRecordLayout &baseLayout,
125 CharUnits baseOffset);
Anders Carlsson860453c2010-12-04 23:59:48 +0000126
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000127 /// LayoutVirtualBase - layout a single virtual base.
John McCallfd577d62011-02-15 22:21:29 +0000128 void LayoutVirtualBase(const CXXRecordDecl *base,
129 CharUnits baseOffset);
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000130
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000131 /// LayoutVirtualBases - layout the virtual bases of a record decl.
132 void LayoutVirtualBases(const CXXRecordDecl *RD,
133 const ASTRecordLayout &Layout);
134
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000135 /// LayoutNonVirtualBase - layout a single non-virtual base.
John McCallfd577d62011-02-15 22:21:29 +0000136 void LayoutNonVirtualBase(const CXXRecordDecl *base,
137 CharUnits baseOffset);
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000138
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000139 /// LayoutNonVirtualBases - layout the virtual bases of a record decl.
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000140 void LayoutNonVirtualBases(const CXXRecordDecl *RD,
141 const ASTRecordLayout &Layout);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000142
Anders Carlsson3d155e62010-11-09 05:25:47 +0000143 /// ComputeNonVirtualBaseType - Compute the non-virtual base field types.
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000144 bool ComputeNonVirtualBaseType(const CXXRecordDecl *RD);
Anders Carlsson3d155e62010-11-09 05:25:47 +0000145
Daniel Dunbar270e2032010-03-31 00:11:27 +0000146 /// LayoutField - layout a single field. Returns false if the operation failed
147 /// because the current struct is not packed.
148 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
149
150 /// LayoutBitField - layout a single bit field.
151 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
152
153 /// AppendField - Appends a field with the given offset and type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000154 void AppendField(CharUnits fieldOffset, llvm::Type *FieldTy);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000155
Daniel Dunbar270e2032010-03-31 00:11:27 +0000156 /// AppendPadding - Appends enough padding bytes so that the total
157 /// struct size is a multiple of the field alignment.
John McCallfd577d62011-02-15 22:21:29 +0000158 void AppendPadding(CharUnits fieldOffset, CharUnits fieldAlignment);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000159
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000160 /// ResizeLastBaseFieldIfNecessary - Fields and bases can be laid out in the
161 /// tail padding of a previous base. If this happens, the type of the previous
162 /// base needs to be changed to an array of i8. Returns true if the last
163 /// laid out base was resized.
164 bool ResizeLastBaseFieldIfNecessary(CharUnits offset);
165
Anders Carlsson3d155e62010-11-09 05:25:47 +0000166 /// getByteArrayType - Returns a byte array type with the given number of
167 /// elements.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000168 llvm::Type *getByteArrayType(CharUnits NumBytes);
Anders Carlsson3d155e62010-11-09 05:25:47 +0000169
Daniel Dunbar270e2032010-03-31 00:11:27 +0000170 /// AppendBytes - Append a given number of bytes to the record.
John McCallfd577d62011-02-15 22:21:29 +0000171 void AppendBytes(CharUnits numBytes);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000172
173 /// AppendTailPadding - Append enough tail padding so that the type will have
174 /// the passed size.
Ken Dyck3256de72011-04-24 16:53:44 +0000175 void AppendTailPadding(CharUnits RecordSize);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000176
John McCallfd577d62011-02-15 22:21:29 +0000177 CharUnits getTypeAlignment(const llvm::Type *Ty) const;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000178
Anders Carlssonfc86d552010-11-28 23:06:23 +0000179 /// getAlignmentAsLLVMStruct - Returns the maximum alignment of all the
180 /// LLVM element types.
John McCallfd577d62011-02-15 22:21:29 +0000181 CharUnits getAlignmentAsLLVMStruct() const;
Anders Carlssonfc86d552010-11-28 23:06:23 +0000182
John McCallf16aa102010-08-22 21:01:12 +0000183 /// CheckZeroInitializable - Check if the given type contains a pointer
Daniel Dunbar270e2032010-03-31 00:11:27 +0000184 /// to data member.
John McCallf16aa102010-08-22 21:01:12 +0000185 void CheckZeroInitializable(QualType T);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000186
187public:
188 CGRecordLayoutBuilder(CodeGenTypes &Types)
John McCall9b7da1c2011-02-15 06:40:56 +0000189 : BaseSubobjectType(0),
190 IsZeroInitializable(true), IsZeroInitializableAsBase(true),
Fariborz Jahanian62055b02011-04-26 23:52:16 +0000191 Packed(false), IsMsStruct(false),
192 Types(Types), BitsAvailableInLastField(0) { }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000193
194 /// Layout - Will layout a RecordDecl.
195 void Layout(const RecordDecl *D);
196};
197
198}
Daniel Dunbar270e2032010-03-31 00:11:27 +0000199
Anders Carlsson45372a62009-07-23 03:17:50 +0000200void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
John McCallfd577d62011-02-15 22:21:29 +0000201 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment();
Anders Carlssond0eb3b92009-09-02 17:51:33 +0000202 Packed = D->hasAttr<PackedAttr>();
Fariborz Jahanian62055b02011-04-26 23:52:16 +0000203
204 IsMsStruct = D->hasAttr<MsStructAttr>();
Anders Carlssona5dd7222009-08-08 19:38:24 +0000205
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000206 if (D->isUnion()) {
207 LayoutUnion(D);
208 return;
209 }
Anders Carlssona860e752009-08-08 18:23:56 +0000210
Anders Carlsson45372a62009-07-23 03:17:50 +0000211 if (LayoutFields(D))
212 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Anders Carlsson45372a62009-07-23 03:17:50 +0000214 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000215 Packed = true;
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000216 LastLaidOutBase.invalidate();
John McCallfd577d62011-02-15 22:21:29 +0000217 NextFieldOffset = CharUnits::Zero();
Anders Carlsson45372a62009-07-23 03:17:50 +0000218 FieldTypes.clear();
John McCall9b7da1c2011-02-15 06:40:56 +0000219 Fields.clear();
220 BitFields.clear();
221 NonVirtualBases.clear();
222 VirtualBases.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Anders Carlsson45372a62009-07-23 03:17:50 +0000224 LayoutFields(D);
225}
226
Daniel Dunbare7a80bd2010-09-02 23:53:28 +0000227CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
228 const FieldDecl *FD,
229 uint64_t FieldOffset,
230 uint64_t FieldSize,
231 uint64_t ContainingTypeSizeInBits,
232 unsigned ContainingTypeAlign) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000233 const llvm::Type *Ty = Types.ConvertTypeForMem(FD->getType());
John McCall92ee7ca2011-02-26 08:41:59 +0000234 CharUnits TypeSizeInBytes =
235 CharUnits::fromQuantity(Types.getTargetData().getTypeAllocSize(Ty));
236 uint64_t TypeSizeInBits = Types.getContext().toBits(TypeSizeInBytes);
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000237
Douglas Gregor575a1c92011-05-20 16:38:50 +0000238 bool IsSigned = FD->getType()->isSignedIntegerOrEnumerationType();
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000239
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000240 if (FieldSize > TypeSizeInBits) {
Anders Carlsson6ba38152010-04-17 22:54:57 +0000241 // We have a wide bit-field. The extra bits are only used for padding, so
242 // if we have a bitfield of type T, with size N:
243 //
244 // T t : N;
245 //
246 // We can just assume that it's:
247 //
248 // T t : sizeof(T);
249 //
250 FieldSize = TypeSizeInBits;
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000251 }
252
Chris Lattnerd8df5b62011-02-17 22:09:58 +0000253 // in big-endian machines the first fields are in higher bit positions,
254 // so revert the offset. The byte offsets are reversed(back) later.
255 if (Types.getTargetData().isBigEndian()) {
256 FieldOffset = ((ContainingTypeSizeInBits)-FieldOffset-FieldSize);
257 }
258
Daniel Dunbare1467a42010-04-22 02:35:46 +0000259 // Compute the access components. The policy we use is to start by attempting
260 // to access using the width of the bit-field type itself and to always access
261 // at aligned indices of that type. If such an access would fail because it
262 // extends past the bound of the type, then we reduce size to the next smaller
263 // power of two and retry. The current algorithm assumes pow2 sized types,
264 // although this is easy to fix.
265 //
Daniel Dunbare1467a42010-04-22 02:35:46 +0000266 assert(llvm::isPowerOf2_32(TypeSizeInBits) && "Unexpected type size!");
267 CGBitFieldInfo::AccessInfo Components[3];
268 unsigned NumComponents = 0;
Nick Lewyckyc3e49402011-03-22 17:35:47 +0000269 unsigned AccessedTargetBits = 0; // The number of target bits accessed.
Daniel Dunbare1467a42010-04-22 02:35:46 +0000270 unsigned AccessWidth = TypeSizeInBits; // The current access width to attempt.
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000271
Daniel Dunbare26bdb92011-06-21 18:54:46 +0000272 // If requested, widen the initial bit-field access to be register sized. The
273 // theory is that this is most likely to allow multiple accesses into the same
274 // structure to be coalesced, and that the backend should be smart enough to
275 // narrow the store if no coalescing is ever done.
276 //
277 // The subsequent code will handle align these access to common boundaries and
278 // guaranteeing that we do not access past the end of the structure.
279 if (Types.getCodeGenOpts().UseRegisterSizedBitfieldAccess) {
280 if (AccessWidth < Types.getTarget().getRegisterWidth())
281 AccessWidth = Types.getTarget().getRegisterWidth();
282 }
283
Daniel Dunbare1467a42010-04-22 02:35:46 +0000284 // Round down from the field offset to find the first access position that is
285 // at an aligned offset of the initial access type.
Daniel Dunbar52968a12010-04-22 15:22:33 +0000286 uint64_t AccessStart = FieldOffset - (FieldOffset % AccessWidth);
287
288 // Adjust initial access size to fit within record.
John McCall92ee7ca2011-02-26 08:41:59 +0000289 while (AccessWidth > Types.getTarget().getCharWidth() &&
Daniel Dunbar52968a12010-04-22 15:22:33 +0000290 AccessStart + AccessWidth > ContainingTypeSizeInBits) {
291 AccessWidth >>= 1;
292 AccessStart = FieldOffset - (FieldOffset % AccessWidth);
293 }
Daniel Dunbar2df25692010-04-15 05:09:32 +0000294
Daniel Dunbare1467a42010-04-22 02:35:46 +0000295 while (AccessedTargetBits < FieldSize) {
296 // Check that we can access using a type of this size, without reading off
297 // the end of the structure. This can occur with packed structures and
298 // -fno-bitfield-type-align, for example.
299 if (AccessStart + AccessWidth > ContainingTypeSizeInBits) {
300 // If so, reduce access size to the next smaller power-of-two and retry.
301 AccessWidth >>= 1;
John McCall92ee7ca2011-02-26 08:41:59 +0000302 assert(AccessWidth >= Types.getTarget().getCharWidth()
303 && "Cannot access under byte size!");
Daniel Dunbare1467a42010-04-22 02:35:46 +0000304 continue;
305 }
Daniel Dunbarab970f92010-04-13 20:58:55 +0000306
Daniel Dunbare1467a42010-04-22 02:35:46 +0000307 // Otherwise, add an access component.
Daniel Dunbarab970f92010-04-13 20:58:55 +0000308
Daniel Dunbare1467a42010-04-22 02:35:46 +0000309 // First, compute the bits inside this access which are part of the
310 // target. We are reading bits [AccessStart, AccessStart + AccessWidth); the
311 // intersection with [FieldOffset, FieldOffset + FieldSize) gives the bits
312 // in the target that we are reading.
Daniel Dunbar52968a12010-04-22 15:22:33 +0000313 assert(FieldOffset < AccessStart + AccessWidth && "Invalid access start!");
314 assert(AccessStart < FieldOffset + FieldSize && "Invalid access start!");
Daniel Dunbare1467a42010-04-22 02:35:46 +0000315 uint64_t AccessBitsInFieldStart = std::max(AccessStart, FieldOffset);
316 uint64_t AccessBitsInFieldSize =
Daniel Dunbar52968a12010-04-22 15:22:33 +0000317 std::min(AccessWidth + AccessStart,
318 FieldOffset + FieldSize) - AccessBitsInFieldStart;
Daniel Dunbar4651efb2010-04-22 14:56:10 +0000319
Daniel Dunbare1467a42010-04-22 02:35:46 +0000320 assert(NumComponents < 3 && "Unexpected number of components!");
321 CGBitFieldInfo::AccessInfo &AI = Components[NumComponents++];
322 AI.FieldIndex = 0;
323 // FIXME: We still follow the old access pattern of only using the field
324 // byte offset. We should switch this once we fix the struct layout to be
325 // pretty.
Chris Lattnerd8df5b62011-02-17 22:09:58 +0000326
327 // on big-endian machines we reverted the bit offset because first fields are
328 // in higher bits. But this also reverts the bytes, so fix this here by reverting
329 // the byte offset on big-endian machines.
330 if (Types.getTargetData().isBigEndian()) {
Ken Dyck28ebde52011-04-24 10:04:59 +0000331 AI.FieldByteOffset = Types.getContext().toCharUnitsFromBits(
332 ContainingTypeSizeInBits - AccessStart - AccessWidth);
Chris Lattnerd8df5b62011-02-17 22:09:58 +0000333 } else {
Ken Dyck28ebde52011-04-24 10:04:59 +0000334 AI.FieldByteOffset = Types.getContext().toCharUnitsFromBits(AccessStart);
Chris Lattnerd8df5b62011-02-17 22:09:58 +0000335 }
Daniel Dunbare1467a42010-04-22 02:35:46 +0000336 AI.FieldBitStart = AccessBitsInFieldStart - AccessStart;
337 AI.AccessWidth = AccessWidth;
Ken Dyckb9e6b2c2011-04-24 10:13:17 +0000338 AI.AccessAlignment = Types.getContext().toCharUnitsFromBits(
339 llvm::MinAlign(ContainingTypeAlign, AccessStart));
Daniel Dunbare1467a42010-04-22 02:35:46 +0000340 AI.TargetBitOffset = AccessedTargetBits;
341 AI.TargetBitWidth = AccessBitsInFieldSize;
342
343 AccessStart += AccessWidth;
344 AccessedTargetBits += AI.TargetBitWidth;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000345 }
346
Daniel Dunbare1467a42010-04-22 02:35:46 +0000347 assert(AccessedTargetBits == FieldSize && "Invalid bit-field access!");
Daniel Dunbar2df25692010-04-15 05:09:32 +0000348 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000349}
350
Daniel Dunbare7a80bd2010-09-02 23:53:28 +0000351CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
352 const FieldDecl *FD,
353 uint64_t FieldOffset,
354 uint64_t FieldSize) {
355 const RecordDecl *RD = FD->getParent();
356 const ASTRecordLayout &RL = Types.getContext().getASTRecordLayout(RD);
Ken Dyckdd76a9a2011-02-11 01:54:29 +0000357 uint64_t ContainingTypeSizeInBits = Types.getContext().toBits(RL.getSize());
Ken Dyckdac54c12011-02-15 02:32:40 +0000358 unsigned ContainingTypeAlign = Types.getContext().toBits(RL.getAlignment());
Daniel Dunbare7a80bd2010-09-02 23:53:28 +0000359
360 return MakeInfo(Types, FD, FieldOffset, FieldSize, ContainingTypeSizeInBits,
361 ContainingTypeAlign);
362}
363
Anders Carlsson45372a62009-07-23 03:17:50 +0000364void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
John McCallfd577d62011-02-15 22:21:29 +0000365 uint64_t fieldOffset) {
366 uint64_t fieldSize =
Anders Carlsson45372a62009-07-23 03:17:50 +0000367 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000368
John McCallfd577d62011-02-15 22:21:29 +0000369 if (fieldSize == 0)
Anders Carlsson45372a62009-07-23 03:17:50 +0000370 return;
371
John McCall92ee7ca2011-02-26 08:41:59 +0000372 uint64_t nextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset);
Ken Dyckedda6e42011-04-24 16:40:29 +0000373 CharUnits numBytesToAppend;
374 unsigned charAlign = Types.getContext().Target.getCharAlign();
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000376 if (fieldOffset < nextFieldOffsetInBits && !BitsAvailableInLastField) {
Ken Dyckedda6e42011-04-24 16:40:29 +0000377 assert(fieldOffset % charAlign == 0 &&
378 "Field offset not aligned correctly");
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000379
380 CharUnits fieldOffsetInCharUnits =
381 Types.getContext().toCharUnitsFromBits(fieldOffset);
382
383 // Try to resize the last base field.
384 if (ResizeLastBaseFieldIfNecessary(fieldOffsetInCharUnits))
385 nextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset);
386 }
387
John McCallfd577d62011-02-15 22:21:29 +0000388 if (fieldOffset < nextFieldOffsetInBits) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000389 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
John McCallfd577d62011-02-15 22:21:29 +0000390 assert(!NextFieldOffset.isZero() && "Must have laid out at least one byte");
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Anders Carlsson45372a62009-07-23 03:17:50 +0000392 // The bitfield begins in the previous bit-field.
Ken Dyckedda6e42011-04-24 16:40:29 +0000393 numBytesToAppend = Types.getContext().toCharUnitsFromBits(
394 llvm::RoundUpToAlignment(fieldSize - BitsAvailableInLastField,
395 charAlign));
Anders Carlsson45372a62009-07-23 03:17:50 +0000396 } else {
Ken Dyckedda6e42011-04-24 16:40:29 +0000397 assert(fieldOffset % charAlign == 0 &&
398 "Field offset not aligned correctly");
Anders Carlsson45372a62009-07-23 03:17:50 +0000399
400 // Append padding if necessary.
Ken Dyckedda6e42011-04-24 16:40:29 +0000401 AppendPadding(Types.getContext().toCharUnitsFromBits(fieldOffset),
402 CharUnits::One());
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Ken Dyckedda6e42011-04-24 16:40:29 +0000404 numBytesToAppend = Types.getContext().toCharUnitsFromBits(
405 llvm::RoundUpToAlignment(fieldSize, charAlign));
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Ken Dyckedda6e42011-04-24 16:40:29 +0000407 assert(!numBytesToAppend.isZero() && "No bytes to append!");
Anders Carlsson45372a62009-07-23 03:17:50 +0000408 }
409
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000410 // Add the bit field info.
John McCall9b7da1c2011-02-15 06:40:56 +0000411 BitFields.insert(std::make_pair(D,
John McCallfd577d62011-02-15 22:21:29 +0000412 CGBitFieldInfo::MakeInfo(Types, D, fieldOffset, fieldSize)));
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Ken Dyckedda6e42011-04-24 16:40:29 +0000414 AppendBytes(numBytesToAppend);
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Mike Stump1eb44332009-09-09 15:08:12 +0000416 BitsAvailableInLastField =
Ken Dyckedda6e42011-04-24 16:40:29 +0000417 Types.getContext().toBits(NextFieldOffset) - (fieldOffset + fieldSize);
Anders Carlsson45372a62009-07-23 03:17:50 +0000418}
419
420bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
John McCallfd577d62011-02-15 22:21:29 +0000421 uint64_t fieldOffset) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000422 // If the field is packed, then we need a packed struct.
Anders Carlssona860e752009-08-08 18:23:56 +0000423 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson45372a62009-07-23 03:17:50 +0000424 return false;
425
426 if (D->isBitField()) {
427 // We must use packed structs for unnamed bit fields since they
428 // don't affect the struct alignment.
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000429 if (!Packed && !D->getDeclName())
Anders Carlsson45372a62009-07-23 03:17:50 +0000430 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000431
John McCallfd577d62011-02-15 22:21:29 +0000432 LayoutBitField(D, fieldOffset);
Anders Carlsson45372a62009-07-23 03:17:50 +0000433 return true;
434 }
Mike Stump1eb44332009-09-09 15:08:12 +0000435
John McCallf16aa102010-08-22 21:01:12 +0000436 CheckZeroInitializable(D->getType());
Daniel Dunbar270e2032010-03-31 00:11:27 +0000437
John McCall92ee7ca2011-02-26 08:41:59 +0000438 assert(fieldOffset % Types.getTarget().getCharWidth() == 0
439 && "field offset is not on a byte boundary!");
440 CharUnits fieldOffsetInBytes
441 = Types.getContext().toCharUnitsFromBits(fieldOffset);
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000443 llvm::Type *Ty = Types.ConvertTypeForMem(D->getType());
John McCallfd577d62011-02-15 22:21:29 +0000444 CharUnits typeAlignment = getTypeAlignment(Ty);
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000445
Anders Carlssona5dd7222009-08-08 19:38:24 +0000446 // If the type alignment is larger then the struct alignment, we must use
447 // a packed struct.
John McCallfd577d62011-02-15 22:21:29 +0000448 if (typeAlignment > Alignment) {
Anders Carlssona5dd7222009-08-08 19:38:24 +0000449 assert(!Packed && "Alignment is wrong even with packed struct!");
450 return false;
451 }
Mike Stump1eb44332009-09-09 15:08:12 +0000452
John McCallfd577d62011-02-15 22:21:29 +0000453 if (!Packed) {
454 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
455 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
456 if (const MaxFieldAlignmentAttr *MFAA =
457 RD->getAttr<MaxFieldAlignmentAttr>()) {
John McCall92ee7ca2011-02-26 08:41:59 +0000458 if (MFAA->getAlignment() != Types.getContext().toBits(typeAlignment))
John McCallfd577d62011-02-15 22:21:29 +0000459 return false;
460 }
Anders Carlssona5dd7222009-08-08 19:38:24 +0000461 }
462 }
463
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000464 // Round up the field offset to the alignment of the field type.
John McCallfd577d62011-02-15 22:21:29 +0000465 CharUnits alignedNextFieldOffsetInBytes =
466 NextFieldOffset.RoundUpToAlignment(typeAlignment);
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000467
John McCallfd577d62011-02-15 22:21:29 +0000468 if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000469 // Try to resize the last base field.
470 if (ResizeLastBaseFieldIfNecessary(fieldOffsetInBytes)) {
471 alignedNextFieldOffsetInBytes =
472 NextFieldOffset.RoundUpToAlignment(typeAlignment);
473 }
474 }
475
476 if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000477 assert(!Packed && "Could not place field even with packed struct!");
478 return false;
479 }
Mike Stump1eb44332009-09-09 15:08:12 +0000480
John McCallfd577d62011-02-15 22:21:29 +0000481 AppendPadding(fieldOffsetInBytes, typeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Anders Carlsson45372a62009-07-23 03:17:50 +0000483 // Now append the field.
John McCall9b7da1c2011-02-15 06:40:56 +0000484 Fields[D] = FieldTypes.size();
John McCallfd577d62011-02-15 22:21:29 +0000485 AppendField(fieldOffsetInBytes, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000487 LastLaidOutBase.invalidate();
Anders Carlsson45372a62009-07-23 03:17:50 +0000488 return true;
489}
490
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000491llvm::Type *
Anders Carlsson86664462010-04-17 20:49:27 +0000492CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
493 const ASTRecordLayout &Layout) {
494 if (Field->isBitField()) {
495 uint64_t FieldSize =
496 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
497
498 // Ignore zero sized bit fields.
499 if (FieldSize == 0)
500 return 0;
501
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000502 llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
Ken Dyck737978d2011-04-24 16:47:33 +0000503 CharUnits NumBytesToAppend = Types.getContext().toCharUnitsFromBits(
504 llvm::RoundUpToAlignment(FieldSize,
505 Types.getContext().Target.getCharAlign()));
Anders Carlssond62328e2010-04-17 21:04:52 +0000506
Ken Dyck737978d2011-04-24 16:47:33 +0000507 if (NumBytesToAppend > CharUnits::One())
508 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend.getQuantity());
Anders Carlssond62328e2010-04-17 21:04:52 +0000509
Anders Carlsson86664462010-04-17 20:49:27 +0000510 // Add the bit field info.
John McCall9b7da1c2011-02-15 06:40:56 +0000511 BitFields.insert(std::make_pair(Field,
512 CGBitFieldInfo::MakeInfo(Types, Field, 0, FieldSize)));
Anders Carlssond62328e2010-04-17 21:04:52 +0000513 return FieldTy;
Anders Carlsson86664462010-04-17 20:49:27 +0000514 }
Daniel Dunbar8ab78a72010-04-20 17:52:30 +0000515
Anders Carlsson86664462010-04-17 20:49:27 +0000516 // This is a regular union field.
John McCall9b7da1c2011-02-15 06:40:56 +0000517 Fields[Field] = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000518 return Types.ConvertTypeForMem(Field->getType());
Anders Carlsson86664462010-04-17 20:49:27 +0000519}
520
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000521void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
522 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump1eb44332009-09-09 15:08:12 +0000523
John McCallfd577d62011-02-15 22:21:29 +0000524 const ASTRecordLayout &layout = Types.getContext().getASTRecordLayout(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000526 llvm::Type *unionType = 0;
John McCallfd577d62011-02-15 22:21:29 +0000527 CharUnits unionSize = CharUnits::Zero();
528 CharUnits unionAlign = CharUnits::Zero();
Mike Stump1eb44332009-09-09 15:08:12 +0000529
John McCallfd577d62011-02-15 22:21:29 +0000530 bool hasOnlyZeroSizedBitFields = true;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000531
John McCallfd577d62011-02-15 22:21:29 +0000532 unsigned fieldNo = 0;
533 for (RecordDecl::field_iterator field = D->field_begin(),
534 fieldEnd = D->field_end(); field != fieldEnd; ++field, ++fieldNo) {
535 assert(layout.getFieldOffset(fieldNo) == 0 &&
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000536 "Union field offset did not start at the beginning of record!");
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000537 llvm::Type *fieldType = LayoutUnionField(*field, layout);
Anders Carlsson2cc8f172009-07-23 04:00:39 +0000538
John McCallfd577d62011-02-15 22:21:29 +0000539 if (!fieldType)
Anders Carlsson86664462010-04-17 20:49:27 +0000540 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000541
John McCallfd577d62011-02-15 22:21:29 +0000542 hasOnlyZeroSizedBitFields = false;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000543
John McCallfd577d62011-02-15 22:21:29 +0000544 CharUnits fieldAlign = CharUnits::fromQuantity(
545 Types.getTargetData().getABITypeAlignment(fieldType));
546 CharUnits fieldSize = CharUnits::fromQuantity(
547 Types.getTargetData().getTypeAllocSize(fieldType));
Mike Stump1eb44332009-09-09 15:08:12 +0000548
John McCallfd577d62011-02-15 22:21:29 +0000549 if (fieldAlign < unionAlign)
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000550 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000551
John McCallfd577d62011-02-15 22:21:29 +0000552 if (fieldAlign > unionAlign || fieldSize > unionSize) {
553 unionType = fieldType;
554 unionAlign = fieldAlign;
555 unionSize = fieldSize;
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000556 }
557 }
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000559 // Now add our field.
John McCallfd577d62011-02-15 22:21:29 +0000560 if (unionType) {
561 AppendField(CharUnits::Zero(), unionType);
Anders Carlsson36620002009-09-03 22:56:02 +0000562
John McCallfd577d62011-02-15 22:21:29 +0000563 if (getTypeAlignment(unionType) > layout.getAlignment()) {
Anders Carlsson36620002009-09-03 22:56:02 +0000564 // We need a packed struct.
565 Packed = true;
John McCallfd577d62011-02-15 22:21:29 +0000566 unionAlign = CharUnits::One();
Anders Carlsson36620002009-09-03 22:56:02 +0000567 }
568 }
John McCallfd577d62011-02-15 22:21:29 +0000569 if (unionAlign.isZero()) {
570 assert(hasOnlyZeroSizedBitFields &&
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000571 "0-align record did not have all zero-sized bit-fields!");
John McCallfd577d62011-02-15 22:21:29 +0000572 unionAlign = CharUnits::One();
Fariborz Jahaniane5041702009-11-06 20:47:40 +0000573 }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000574
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000575 // Append tail padding.
John McCallfd577d62011-02-15 22:21:29 +0000576 CharUnits recordSize = layout.getSize();
577 if (recordSize > unionSize)
578 AppendPadding(recordSize, unionAlign);
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000579}
580
John McCall9b7da1c2011-02-15 06:40:56 +0000581void CGRecordLayoutBuilder::LayoutBase(const CXXRecordDecl *base,
John McCallfd577d62011-02-15 22:21:29 +0000582 const CGRecordLayout &baseLayout,
583 CharUnits baseOffset) {
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000584 ResizeLastBaseFieldIfNecessary(baseOffset);
585
John McCallfd577d62011-02-15 22:21:29 +0000586 AppendPadding(baseOffset, CharUnits::One());
Anders Carlsson860453c2010-12-04 23:59:48 +0000587
John McCall9b7da1c2011-02-15 06:40:56 +0000588 const ASTRecordLayout &baseASTLayout
589 = Types.getContext().getASTRecordLayout(base);
590
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000591 LastLaidOutBase.Offset = NextFieldOffset;
592 LastLaidOutBase.NonVirtualSize = baseASTLayout.getNonVirtualSize();
593
John McCallfd577d62011-02-15 22:21:29 +0000594 // Fields and bases can be laid out in the tail padding of previous
595 // bases. If this happens, we need to allocate the base as an i8
596 // array; otherwise, we can use the subobject type. However,
597 // actually doing that would require knowledge of what immediately
598 // follows this base in the layout, so instead we do a conservative
599 // approximation, which is to use the base subobject type if it
600 // has the same LLVM storage size as the nvsize.
601
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000602 llvm::StructType *subobjectType = baseLayout.getBaseSubobjectLLVMType();
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000603 AppendField(baseOffset, subobjectType);
John McCall9b7da1c2011-02-15 06:40:56 +0000604}
605
606void CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *base,
John McCallfd577d62011-02-15 22:21:29 +0000607 CharUnits baseOffset) {
John McCall9b7da1c2011-02-15 06:40:56 +0000608 // Ignore empty bases.
609 if (base->isEmpty()) return;
610
611 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
612 if (IsZeroInitializableAsBase) {
613 assert(IsZeroInitializable &&
614 "class zero-initializable as base but not as complete object");
615
616 IsZeroInitializable = IsZeroInitializableAsBase =
617 baseLayout.isZeroInitializableAsBase();
618 }
619
John McCallfd577d62011-02-15 22:21:29 +0000620 LayoutBase(base, baseLayout, baseOffset);
John McCall9b7da1c2011-02-15 06:40:56 +0000621 NonVirtualBases[base] = (FieldTypes.size() - 1);
Anders Carlsson860453c2010-12-04 23:59:48 +0000622}
623
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000624void
John McCall9b7da1c2011-02-15 06:40:56 +0000625CGRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *base,
John McCallfd577d62011-02-15 22:21:29 +0000626 CharUnits baseOffset) {
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000627 // Ignore empty bases.
John McCall9b7da1c2011-02-15 06:40:56 +0000628 if (base->isEmpty()) return;
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000629
John McCall9b7da1c2011-02-15 06:40:56 +0000630 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
631 if (IsZeroInitializable)
632 IsZeroInitializable = baseLayout.isZeroInitializableAsBase();
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000633
John McCallfd577d62011-02-15 22:21:29 +0000634 LayoutBase(base, baseLayout, baseOffset);
John McCall9b7da1c2011-02-15 06:40:56 +0000635 VirtualBases[base] = (FieldTypes.size() - 1);
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000636}
637
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000638/// LayoutVirtualBases - layout the non-virtual bases of a record decl.
639void
640CGRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
641 const ASTRecordLayout &Layout) {
642 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
643 E = RD->bases_end(); I != E; ++I) {
644 const CXXRecordDecl *BaseDecl =
645 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
646
647 // We only want to lay out virtual bases that aren't indirect primary bases
648 // of some other base.
649 if (I->isVirtual() && !IndirectPrimaryBases.count(BaseDecl)) {
650 // Only lay out the base once.
651 if (!LaidOutVirtualBases.insert(BaseDecl))
652 continue;
653
John McCallfd577d62011-02-15 22:21:29 +0000654 CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl);
655 LayoutVirtualBase(BaseDecl, vbaseOffset);
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000656 }
657
658 if (!BaseDecl->getNumVBases()) {
659 // This base isn't interesting since it doesn't have any virtual bases.
660 continue;
661 }
662
663 LayoutVirtualBases(BaseDecl, Layout);
664 }
665}
666
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000667void
668CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD,
669 const ASTRecordLayout &Layout) {
670 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
671
672 // Check if we need to add a vtable pointer.
673 if (RD->isDynamicClass()) {
674 if (!PrimaryBase) {
675 const llvm::Type *FunctionType =
676 llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()),
677 /*isVarArg=*/true);
678 const llvm::Type *VTableTy = FunctionType->getPointerTo();
679
John McCallfd577d62011-02-15 22:21:29 +0000680 assert(NextFieldOffset.isZero() &&
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000681 "VTable pointer must come first!");
John McCallfd577d62011-02-15 22:21:29 +0000682 AppendField(CharUnits::Zero(), VTableTy->getPointerTo());
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000683 } else {
Anders Carlssonc9e814b2010-11-24 23:12:57 +0000684 if (!Layout.isPrimaryBaseVirtual())
John McCallfd577d62011-02-15 22:21:29 +0000685 LayoutNonVirtualBase(PrimaryBase, CharUnits::Zero());
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000686 else
John McCallfd577d62011-02-15 22:21:29 +0000687 LayoutVirtualBase(PrimaryBase, CharUnits::Zero());
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000688 }
689 }
690
691 // Layout the non-virtual bases.
692 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
693 E = RD->bases_end(); I != E; ++I) {
694 if (I->isVirtual())
695 continue;
696
697 const CXXRecordDecl *BaseDecl =
698 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
699
700 // We've already laid out the primary base.
Anders Carlssonc9e814b2010-11-24 23:12:57 +0000701 if (BaseDecl == PrimaryBase && !Layout.isPrimaryBaseVirtual())
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000702 continue;
703
John McCallfd577d62011-02-15 22:21:29 +0000704 LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffset(BaseDecl));
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000705 }
706}
707
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000708bool
Anders Carlsson3d155e62010-11-09 05:25:47 +0000709CGRecordLayoutBuilder::ComputeNonVirtualBaseType(const CXXRecordDecl *RD) {
710 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(RD);
711
Ken Dyck68cf1a52011-02-08 02:02:47 +0000712 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
713 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
John McCallfd577d62011-02-15 22:21:29 +0000714 CharUnits AlignedNonVirtualTypeSize =
715 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
Anders Carlsson3d155e62010-11-09 05:25:47 +0000716
Anders Carlsson3d155e62010-11-09 05:25:47 +0000717 // First check if we can use the same fields as for the complete class.
John McCallfd577d62011-02-15 22:21:29 +0000718 CharUnits RecordSize = Layout.getSize();
John McCall9b7da1c2011-02-15 06:40:56 +0000719 if (AlignedNonVirtualTypeSize == RecordSize)
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000720 return true;
Anders Carlsson3d155e62010-11-09 05:25:47 +0000721
Anders Carlsson3d155e62010-11-09 05:25:47 +0000722 // Check if we need padding.
John McCallfd577d62011-02-15 22:21:29 +0000723 CharUnits AlignedNextFieldOffset =
724 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
Anders Carlsson3d155e62010-11-09 05:25:47 +0000725
John McCall9b7da1c2011-02-15 06:40:56 +0000726 if (AlignedNextFieldOffset > AlignedNonVirtualTypeSize) {
727 assert(!Packed && "cannot layout even as packed struct");
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000728 return false; // Needs packing.
Anders Carlsson3d155e62010-11-09 05:25:47 +0000729 }
730
John McCall9b7da1c2011-02-15 06:40:56 +0000731 bool needsPadding = (AlignedNonVirtualTypeSize != AlignedNextFieldOffset);
732 if (needsPadding) {
John McCallfd577d62011-02-15 22:21:29 +0000733 CharUnits NumBytes = AlignedNonVirtualTypeSize - AlignedNextFieldOffset;
John McCall9b7da1c2011-02-15 06:40:56 +0000734 FieldTypes.push_back(getByteArrayType(NumBytes));
735 }
736
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000737
738 BaseSubobjectType = llvm::StructType::createNamed(Types.getLLVMContext(), "",
739 FieldTypes, Packed);
740 Types.addRecordTypeName(RD, BaseSubobjectType, ".base");
John McCall9b7da1c2011-02-15 06:40:56 +0000741
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000742 // Pull the padding back off.
743 if (needsPadding)
John McCall9b7da1c2011-02-15 06:40:56 +0000744 FieldTypes.pop_back();
John McCall9b7da1c2011-02-15 06:40:56 +0000745
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000746 return true;
Anders Carlsson3d155e62010-11-09 05:25:47 +0000747}
748
Anders Carlsson45372a62009-07-23 03:17:50 +0000749bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
750 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
John McCallfd577d62011-02-15 22:21:29 +0000751 assert(!Alignment.isZero() && "Did not set alignment!");
Mike Stump1eb44332009-09-09 15:08:12 +0000752
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000753 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Anders Carlsson3d155e62010-11-09 05:25:47 +0000755 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
756 if (RD)
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000757 LayoutNonVirtualBases(RD, Layout);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000758
Anders Carlsson45372a62009-07-23 03:17:50 +0000759 unsigned FieldNo = 0;
Fariborz Jahanian62055b02011-04-26 23:52:16 +0000760 const FieldDecl *LastFD = 0;
761
Mike Stump1eb44332009-09-09 15:08:12 +0000762 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson45372a62009-07-23 03:17:50 +0000763 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
Fariborz Jahanian62055b02011-04-26 23:52:16 +0000764 if (IsMsStruct) {
765 // Zero-length bitfields following non-bitfield members are
766 // ignored:
767 const FieldDecl *FD = (*Field);
Fariborz Jahanian855a8e72011-05-03 20:21:04 +0000768 if (Types.getContext().ZeroBitfieldFollowsNonBitfield(FD, LastFD)) {
Fariborz Jahanian62055b02011-04-26 23:52:16 +0000769 --FieldNo;
770 continue;
771 }
772 LastFD = FD;
773 }
774
Anders Carlsson45372a62009-07-23 03:17:50 +0000775 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000776 assert(!Packed &&
Anders Carlsson45372a62009-07-23 03:17:50 +0000777 "Could not layout fields even with a packed LLVM struct!");
778 return false;
779 }
780 }
781
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000782 if (RD) {
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000783 // We've laid out the non-virtual bases and the fields, now compute the
784 // non-virtual base field types.
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000785 if (!ComputeNonVirtualBaseType(RD)) {
786 assert(!Packed && "Could not layout even with a packed LLVM struct!");
787 return false;
788 }
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000789
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000790 // And lay out the virtual bases.
791 RD->getIndirectPrimaryBases(IndirectPrimaryBases);
792 if (Layout.isPrimaryBaseVirtual())
793 IndirectPrimaryBases.insert(Layout.getPrimaryBase());
794 LayoutVirtualBases(RD, Layout);
795 }
Anders Carlsson3d155e62010-11-09 05:25:47 +0000796
Anders Carlsson45372a62009-07-23 03:17:50 +0000797 // Append tail padding if necessary.
Ken Dyck3256de72011-04-24 16:53:44 +0000798 AppendTailPadding(Layout.getSize());
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Anders Carlsson45372a62009-07-23 03:17:50 +0000800 return true;
801}
802
Ken Dyck3256de72011-04-24 16:53:44 +0000803void CGRecordLayoutBuilder::AppendTailPadding(CharUnits RecordSize) {
804 ResizeLastBaseFieldIfNecessary(RecordSize);
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Ken Dyck3256de72011-04-24 16:53:44 +0000806 assert(NextFieldOffset <= RecordSize && "Size mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000807
John McCallfd577d62011-02-15 22:21:29 +0000808 CharUnits AlignedNextFieldOffset =
809 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
Anders Carlssonc2456822009-12-08 01:24:23 +0000810
Ken Dyck3256de72011-04-24 16:53:44 +0000811 if (AlignedNextFieldOffset == RecordSize) {
Anders Carlssonc2456822009-12-08 01:24:23 +0000812 // We don't need any padding.
813 return;
814 }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000815
Ken Dyck3256de72011-04-24 16:53:44 +0000816 CharUnits NumPadBytes = RecordSize - NextFieldOffset;
Anders Carlssonc1efe362009-07-27 14:55:54 +0000817 AppendBytes(NumPadBytes);
818}
819
John McCallfd577d62011-02-15 22:21:29 +0000820void CGRecordLayoutBuilder::AppendField(CharUnits fieldOffset,
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000821 llvm::Type *fieldType) {
John McCallfd577d62011-02-15 22:21:29 +0000822 CharUnits fieldSize =
823 CharUnits::fromQuantity(Types.getTargetData().getTypeAllocSize(fieldType));
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000824
John McCallfd577d62011-02-15 22:21:29 +0000825 FieldTypes.push_back(fieldType);
Anders Carlsson45372a62009-07-23 03:17:50 +0000826
John McCallfd577d62011-02-15 22:21:29 +0000827 NextFieldOffset = fieldOffset + fieldSize;
Anders Carlsson45372a62009-07-23 03:17:50 +0000828 BitsAvailableInLastField = 0;
829}
830
John McCallfd577d62011-02-15 22:21:29 +0000831void CGRecordLayoutBuilder::AppendPadding(CharUnits fieldOffset,
832 CharUnits fieldAlignment) {
833 assert(NextFieldOffset <= fieldOffset &&
Anders Carlsson45372a62009-07-23 03:17:50 +0000834 "Incorrect field layout!");
Mike Stump1eb44332009-09-09 15:08:12 +0000835
Anders Carlsson45372a62009-07-23 03:17:50 +0000836 // Round up the field offset to the alignment of the field type.
John McCallfd577d62011-02-15 22:21:29 +0000837 CharUnits alignedNextFieldOffset =
838 NextFieldOffset.RoundUpToAlignment(fieldAlignment);
Anders Carlsson45372a62009-07-23 03:17:50 +0000839
John McCallfd577d62011-02-15 22:21:29 +0000840 if (alignedNextFieldOffset < fieldOffset) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000841 // Even with alignment, the field offset is not at the right place,
842 // insert padding.
John McCallfd577d62011-02-15 22:21:29 +0000843 CharUnits padding = fieldOffset - NextFieldOffset;
Anders Carlsson45372a62009-07-23 03:17:50 +0000844
John McCallfd577d62011-02-15 22:21:29 +0000845 AppendBytes(padding);
Anders Carlsson45372a62009-07-23 03:17:50 +0000846 }
847}
848
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000849bool CGRecordLayoutBuilder::ResizeLastBaseFieldIfNecessary(CharUnits offset) {
850 // Check if we have a base to resize.
851 if (!LastLaidOutBase.isValid())
852 return false;
853
854 // This offset does not overlap with the tail padding.
855 if (offset >= NextFieldOffset)
856 return false;
857
858 // Restore the field offset and append an i8 array instead.
859 FieldTypes.pop_back();
860 NextFieldOffset = LastLaidOutBase.Offset;
861 AppendBytes(LastLaidOutBase.NonVirtualSize);
862 LastLaidOutBase.invalidate();
863
864 return true;
865}
866
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000867llvm::Type *CGRecordLayoutBuilder::getByteArrayType(CharUnits numBytes) {
John McCallfd577d62011-02-15 22:21:29 +0000868 assert(!numBytes.isZero() && "Empty byte arrays aren't allowed.");
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000870 llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
John McCallfd577d62011-02-15 22:21:29 +0000871 if (numBytes > CharUnits::One())
872 Ty = llvm::ArrayType::get(Ty, numBytes.getQuantity());
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Anders Carlsson3d155e62010-11-09 05:25:47 +0000874 return Ty;
875}
876
John McCallfd577d62011-02-15 22:21:29 +0000877void CGRecordLayoutBuilder::AppendBytes(CharUnits numBytes) {
878 if (numBytes.isZero())
Anders Carlsson3d155e62010-11-09 05:25:47 +0000879 return;
880
Anders Carlsson45372a62009-07-23 03:17:50 +0000881 // Append the padding field
John McCallfd577d62011-02-15 22:21:29 +0000882 AppendField(NextFieldOffset, getByteArrayType(numBytes));
Anders Carlsson45372a62009-07-23 03:17:50 +0000883}
884
John McCallfd577d62011-02-15 22:21:29 +0000885CharUnits CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000886 if (Packed)
John McCallfd577d62011-02-15 22:21:29 +0000887 return CharUnits::One();
Mike Stump1eb44332009-09-09 15:08:12 +0000888
John McCallfd577d62011-02-15 22:21:29 +0000889 return CharUnits::fromQuantity(Types.getTargetData().getABITypeAlignment(Ty));
Anders Carlsson45372a62009-07-23 03:17:50 +0000890}
891
John McCallfd577d62011-02-15 22:21:29 +0000892CharUnits CGRecordLayoutBuilder::getAlignmentAsLLVMStruct() const {
Anders Carlssonfc86d552010-11-28 23:06:23 +0000893 if (Packed)
John McCallfd577d62011-02-15 22:21:29 +0000894 return CharUnits::One();
Anders Carlssonfc86d552010-11-28 23:06:23 +0000895
John McCallfd577d62011-02-15 22:21:29 +0000896 CharUnits maxAlignment = CharUnits::One();
Anders Carlssonfc86d552010-11-28 23:06:23 +0000897 for (size_t i = 0; i != FieldTypes.size(); ++i)
John McCallfd577d62011-02-15 22:21:29 +0000898 maxAlignment = std::max(maxAlignment, getTypeAlignment(FieldTypes[i]));
Anders Carlssonfc86d552010-11-28 23:06:23 +0000899
John McCallfd577d62011-02-15 22:21:29 +0000900 return maxAlignment;
Anders Carlssonfc86d552010-11-28 23:06:23 +0000901}
902
John McCall9b7da1c2011-02-15 06:40:56 +0000903/// Merge in whether a field of the given type is zero-initializable.
John McCallf16aa102010-08-22 21:01:12 +0000904void CGRecordLayoutBuilder::CheckZeroInitializable(QualType T) {
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000905 // This record already contains a member pointer.
John McCall9b7da1c2011-02-15 06:40:56 +0000906 if (!IsZeroInitializableAsBase)
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000907 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000908
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000909 // Can only have member pointers if we're compiling C++.
910 if (!Types.getContext().getLangOptions().CPlusPlus)
911 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000912
John McCall9b7da1c2011-02-15 06:40:56 +0000913 const Type *elementType = T->getBaseElementTypeUnsafe();
Mike Stump1eb44332009-09-09 15:08:12 +0000914
John McCall9b7da1c2011-02-15 06:40:56 +0000915 if (const MemberPointerType *MPT = elementType->getAs<MemberPointerType>()) {
John McCallf16aa102010-08-22 21:01:12 +0000916 if (!Types.getCXXABI().isZeroInitializable(MPT))
John McCall9b7da1c2011-02-15 06:40:56 +0000917 IsZeroInitializable = IsZeroInitializableAsBase = false;
918 } else if (const RecordType *RT = elementType->getAs<RecordType>()) {
Anders Carlsson2c12d032010-02-02 05:17:25 +0000919 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
John McCall9b7da1c2011-02-15 06:40:56 +0000920 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
921 if (!Layout.isZeroInitializable())
922 IsZeroInitializable = IsZeroInitializableAsBase = false;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000923 }
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000924}
925
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000926CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D,
927 llvm::StructType *Ty) {
Daniel Dunbar270e2032010-03-31 00:11:27 +0000928 CGRecordLayoutBuilder Builder(*this);
Mike Stump1eb44332009-09-09 15:08:12 +0000929
Anders Carlsson45372a62009-07-23 03:17:50 +0000930 Builder.Layout(D);
Anders Carlsson4c98efd2009-07-24 15:20:52 +0000931
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000932 Ty->setBody(Builder.FieldTypes, Builder.Packed);
Mike Stump1eb44332009-09-09 15:08:12 +0000933
John McCall9b7da1c2011-02-15 06:40:56 +0000934 // If we're in C++, compute the base subobject type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000935 llvm::StructType *BaseTy = 0;
Anders Carlsson3d155e62010-11-09 05:25:47 +0000936 if (isa<CXXRecordDecl>(D)) {
John McCall9b7da1c2011-02-15 06:40:56 +0000937 BaseTy = Builder.BaseSubobjectType;
938 if (!BaseTy) BaseTy = Ty;
Anders Carlsson3d155e62010-11-09 05:25:47 +0000939 }
940
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000941 CGRecordLayout *RL =
John McCall9b7da1c2011-02-15 06:40:56 +0000942 new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable,
943 Builder.IsZeroInitializableAsBase);
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000944
John McCall9b7da1c2011-02-15 06:40:56 +0000945 RL->NonVirtualBases.swap(Builder.NonVirtualBases);
946 RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases);
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000947
Anders Carlsson45372a62009-07-23 03:17:50 +0000948 // Add all the field numbers.
John McCall9b7da1c2011-02-15 06:40:56 +0000949 RL->FieldInfo.swap(Builder.Fields);
Anders Carlsson45372a62009-07-23 03:17:50 +0000950
951 // Add bitfield info.
John McCall9b7da1c2011-02-15 06:40:56 +0000952 RL->BitFields.swap(Builder.BitFields);
Mike Stump1eb44332009-09-09 15:08:12 +0000953
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000954 // Dump the layout, if requested.
Daniel Dunbarab970f92010-04-13 20:58:55 +0000955 if (getContext().getLangOptions().DumpRecordLayouts) {
Daniel Dunbar8d8ab742010-04-19 20:44:53 +0000956 llvm::errs() << "\n*** Dumping IRgen Record Layout\n";
Daniel Dunbarab970f92010-04-13 20:58:55 +0000957 llvm::errs() << "Record: ";
958 D->dump();
959 llvm::errs() << "\nLayout: ";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000960 RL->dump();
Daniel Dunbarab970f92010-04-13 20:58:55 +0000961 }
Daniel Dunbar93c62962010-04-12 18:14:18 +0000962
Daniel Dunbare1467a42010-04-22 02:35:46 +0000963#ifndef NDEBUG
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000964 // Verify that the computed LLVM struct size matches the AST layout size.
Anders Carlsson3d155e62010-11-09 05:25:47 +0000965 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
966
Ken Dyckdd76a9a2011-02-11 01:54:29 +0000967 uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize());
Daniel Dunbare1467a42010-04-22 02:35:46 +0000968 assert(TypeSizeInBits == getTargetData().getTypeAllocSizeInBits(Ty) &&
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000969 "Type size mismatch!");
970
Anders Carlsson3d155e62010-11-09 05:25:47 +0000971 if (BaseTy) {
Ken Dyck68cf1a52011-02-08 02:02:47 +0000972 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
973 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
974 CharUnits AlignedNonVirtualTypeSize =
975 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
976
977 uint64_t AlignedNonVirtualTypeSizeInBits =
Ken Dyckdd76a9a2011-02-11 01:54:29 +0000978 getContext().toBits(AlignedNonVirtualTypeSize);
Anders Carlsson3d155e62010-11-09 05:25:47 +0000979
980 assert(AlignedNonVirtualTypeSizeInBits ==
981 getTargetData().getTypeAllocSizeInBits(BaseTy) &&
982 "Type size mismatch!");
983 }
984
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000985 // Verify that the LLVM and AST field offsets agree.
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000986 const llvm::StructType *ST =
987 dyn_cast<llvm::StructType>(RL->getLLVMType());
988 const llvm::StructLayout *SL = getTargetData().getStructLayout(ST);
989
990 const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
991 RecordDecl::field_iterator it = D->field_begin();
Fariborz Jahanian62055b02011-04-26 23:52:16 +0000992 const FieldDecl *LastFD = 0;
993 bool IsMsStruct = D->hasAttr<MsStructAttr>();
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000994 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
995 const FieldDecl *FD = *it;
Daniel Dunbare1467a42010-04-22 02:35:46 +0000996
997 // For non-bit-fields, just check that the LLVM struct offset matches the
998 // AST offset.
999 if (!FD->isBitField()) {
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +00001000 unsigned FieldNo = RL->getLLVMFieldNo(FD);
1001 assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
1002 "Invalid field offset!");
Fariborz Jahanian62055b02011-04-26 23:52:16 +00001003 LastFD = FD;
Daniel Dunbare1467a42010-04-22 02:35:46 +00001004 continue;
1005 }
1006
Fariborz Jahanian62055b02011-04-26 23:52:16 +00001007 if (IsMsStruct) {
1008 // Zero-length bitfields following non-bitfield members are
1009 // ignored:
Fariborz Jahanian855a8e72011-05-03 20:21:04 +00001010 if (getContext().ZeroBitfieldFollowsNonBitfield(FD, LastFD)) {
Fariborz Jahanian62055b02011-04-26 23:52:16 +00001011 --i;
1012 continue;
1013 }
1014 LastFD = FD;
1015 }
1016
Daniel Dunbare1467a42010-04-22 02:35:46 +00001017 // Ignore unnamed bit-fields.
Fariborz Jahanian62055b02011-04-26 23:52:16 +00001018 if (!FD->getDeclName()) {
1019 LastFD = FD;
Daniel Dunbare1467a42010-04-22 02:35:46 +00001020 continue;
Fariborz Jahanian62055b02011-04-26 23:52:16 +00001021 }
1022
Daniel Dunbare1467a42010-04-22 02:35:46 +00001023 const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
1024 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
1025 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
1026
1027 // Verify that every component access is within the structure.
1028 uint64_t FieldOffset = SL->getElementOffsetInBits(AI.FieldIndex);
John McCall92ee7ca2011-02-26 08:41:59 +00001029 uint64_t AccessBitOffset = FieldOffset +
Ken Dyck28ebde52011-04-24 10:04:59 +00001030 getContext().toBits(AI.FieldByteOffset);
Daniel Dunbare1467a42010-04-22 02:35:46 +00001031 assert(AccessBitOffset + AI.AccessWidth <= TypeSizeInBits &&
1032 "Invalid bit-field access (out of range)!");
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +00001033 }
1034 }
1035#endif
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +00001036
Daniel Dunbar198bcb42010-03-31 01:09:11 +00001037 return RL;
Anders Carlsson45372a62009-07-23 03:17:50 +00001038}
Daniel Dunbar93c62962010-04-12 18:14:18 +00001039
1040void CGRecordLayout::print(llvm::raw_ostream &OS) const {
1041 OS << "<CGRecordLayout\n";
John McCall9b7da1c2011-02-15 06:40:56 +00001042 OS << " LLVMType:" << *CompleteObjectType << "\n";
1043 if (BaseSubobjectType)
1044 OS << " NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n";
John McCallf16aa102010-08-22 21:01:12 +00001045 OS << " IsZeroInitializable:" << IsZeroInitializable << "\n";
Daniel Dunbar93c62962010-04-12 18:14:18 +00001046 OS << " BitFields:[\n";
Daniel Dunbarad759532010-04-22 02:35:36 +00001047
1048 // Print bit-field infos in declaration order.
1049 std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
Daniel Dunbar93c62962010-04-12 18:14:18 +00001050 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
1051 it = BitFields.begin(), ie = BitFields.end();
1052 it != ie; ++it) {
Daniel Dunbarad759532010-04-22 02:35:36 +00001053 const RecordDecl *RD = it->first->getParent();
1054 unsigned Index = 0;
1055 for (RecordDecl::field_iterator
1056 it2 = RD->field_begin(); *it2 != it->first; ++it2)
1057 ++Index;
1058 BFIs.push_back(std::make_pair(Index, &it->second));
1059 }
1060 llvm::array_pod_sort(BFIs.begin(), BFIs.end());
1061 for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
Daniel Dunbarab970f92010-04-13 20:58:55 +00001062 OS.indent(4);
Daniel Dunbarad759532010-04-22 02:35:36 +00001063 BFIs[i].second->print(OS);
Daniel Dunbar93c62962010-04-12 18:14:18 +00001064 OS << "\n";
1065 }
Daniel Dunbarad759532010-04-22 02:35:36 +00001066
Daniel Dunbar93c62962010-04-12 18:14:18 +00001067 OS << "]>\n";
1068}
1069
1070void CGRecordLayout::dump() const {
1071 print(llvm::errs());
1072}
1073
1074void CGBitFieldInfo::print(llvm::raw_ostream &OS) const {
1075 OS << "<CGBitFieldInfo";
Daniel Dunbar93c62962010-04-12 18:14:18 +00001076 OS << " Size:" << Size;
Daniel Dunbarab970f92010-04-13 20:58:55 +00001077 OS << " IsSigned:" << IsSigned << "\n";
1078
1079 OS.indent(4 + strlen("<CGBitFieldInfo"));
1080 OS << " NumComponents:" << getNumComponents();
1081 OS << " Components: [";
1082 if (getNumComponents()) {
1083 OS << "\n";
1084 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
1085 const AccessInfo &AI = getComponent(i);
1086 OS.indent(8);
1087 OS << "<AccessInfo"
1088 << " FieldIndex:" << AI.FieldIndex
Ken Dyck28ebde52011-04-24 10:04:59 +00001089 << " FieldByteOffset:" << AI.FieldByteOffset.getQuantity()
Daniel Dunbarab970f92010-04-13 20:58:55 +00001090 << " FieldBitStart:" << AI.FieldBitStart
1091 << " AccessWidth:" << AI.AccessWidth << "\n";
1092 OS.indent(8 + strlen("<AccessInfo"));
Ken Dyckb9e6b2c2011-04-24 10:13:17 +00001093 OS << " AccessAlignment:" << AI.AccessAlignment.getQuantity()
Daniel Dunbarab970f92010-04-13 20:58:55 +00001094 << " TargetBitOffset:" << AI.TargetBitOffset
1095 << " TargetBitWidth:" << AI.TargetBitWidth
1096 << ">\n";
1097 }
1098 OS.indent(4);
1099 }
1100 OS << "]>";
Daniel Dunbar93c62962010-04-12 18:14:18 +00001101}
1102
1103void CGBitFieldInfo::dump() const {
1104 print(llvm::errs());
1105}