blob: 106ce10f512a456ed903df39f5b6b0b32a10f2d1 [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 Lattner5f9e2722011-07-23 10:55:15 +000038 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);
John McCall9da23522011-11-08 04:01:03 +0000134
135 /// MSLayoutVirtualBases - layout the virtual bases of a record decl,
136 /// like MSVC.
137 void MSLayoutVirtualBases(const CXXRecordDecl *RD,
138 const ASTRecordLayout &Layout);
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000139
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000140 /// LayoutNonVirtualBase - layout a single non-virtual base.
John McCallfd577d62011-02-15 22:21:29 +0000141 void LayoutNonVirtualBase(const CXXRecordDecl *base,
142 CharUnits baseOffset);
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000143
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000144 /// LayoutNonVirtualBases - layout the virtual bases of a record decl.
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000145 void LayoutNonVirtualBases(const CXXRecordDecl *RD,
146 const ASTRecordLayout &Layout);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000147
Anders Carlsson3d155e62010-11-09 05:25:47 +0000148 /// ComputeNonVirtualBaseType - Compute the non-virtual base field types.
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000149 bool ComputeNonVirtualBaseType(const CXXRecordDecl *RD);
Anders Carlsson3d155e62010-11-09 05:25:47 +0000150
Daniel Dunbar270e2032010-03-31 00:11:27 +0000151 /// LayoutField - layout a single field. Returns false if the operation failed
152 /// because the current struct is not packed.
153 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
154
155 /// LayoutBitField - layout a single bit field.
156 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
157
158 /// AppendField - Appends a field with the given offset and type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000159 void AppendField(CharUnits fieldOffset, llvm::Type *FieldTy);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000160
Daniel Dunbar270e2032010-03-31 00:11:27 +0000161 /// AppendPadding - Appends enough padding bytes so that the total
162 /// struct size is a multiple of the field alignment.
John McCallfd577d62011-02-15 22:21:29 +0000163 void AppendPadding(CharUnits fieldOffset, CharUnits fieldAlignment);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000164
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000165 /// ResizeLastBaseFieldIfNecessary - Fields and bases can be laid out in the
166 /// tail padding of a previous base. If this happens, the type of the previous
167 /// base needs to be changed to an array of i8. Returns true if the last
168 /// laid out base was resized.
169 bool ResizeLastBaseFieldIfNecessary(CharUnits offset);
170
Anders Carlsson3d155e62010-11-09 05:25:47 +0000171 /// getByteArrayType - Returns a byte array type with the given number of
172 /// elements.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000173 llvm::Type *getByteArrayType(CharUnits NumBytes);
Anders Carlsson3d155e62010-11-09 05:25:47 +0000174
Daniel Dunbar270e2032010-03-31 00:11:27 +0000175 /// AppendBytes - Append a given number of bytes to the record.
John McCallfd577d62011-02-15 22:21:29 +0000176 void AppendBytes(CharUnits numBytes);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000177
178 /// AppendTailPadding - Append enough tail padding so that the type will have
179 /// the passed size.
Ken Dyck3256de72011-04-24 16:53:44 +0000180 void AppendTailPadding(CharUnits RecordSize);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000181
Chris Lattner2acc6e32011-07-18 04:24:23 +0000182 CharUnits getTypeAlignment(llvm::Type *Ty) const;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000183
Anders Carlssonfc86d552010-11-28 23:06:23 +0000184 /// getAlignmentAsLLVMStruct - Returns the maximum alignment of all the
185 /// LLVM element types.
John McCallfd577d62011-02-15 22:21:29 +0000186 CharUnits getAlignmentAsLLVMStruct() const;
Anders Carlssonfc86d552010-11-28 23:06:23 +0000187
John McCallf16aa102010-08-22 21:01:12 +0000188 /// CheckZeroInitializable - Check if the given type contains a pointer
Daniel Dunbar270e2032010-03-31 00:11:27 +0000189 /// to data member.
John McCallf16aa102010-08-22 21:01:12 +0000190 void CheckZeroInitializable(QualType T);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000191
192public:
193 CGRecordLayoutBuilder(CodeGenTypes &Types)
John McCall9b7da1c2011-02-15 06:40:56 +0000194 : BaseSubobjectType(0),
195 IsZeroInitializable(true), IsZeroInitializableAsBase(true),
Fariborz Jahanian62055b02011-04-26 23:52:16 +0000196 Packed(false), IsMsStruct(false),
197 Types(Types), BitsAvailableInLastField(0) { }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000198
199 /// Layout - Will layout a RecordDecl.
200 void Layout(const RecordDecl *D);
201};
202
203}
Daniel Dunbar270e2032010-03-31 00:11:27 +0000204
Anders Carlsson45372a62009-07-23 03:17:50 +0000205void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
John McCallfd577d62011-02-15 22:21:29 +0000206 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment();
Anders Carlssond0eb3b92009-09-02 17:51:33 +0000207 Packed = D->hasAttr<PackedAttr>();
Fariborz Jahanian62055b02011-04-26 23:52:16 +0000208
209 IsMsStruct = D->hasAttr<MsStructAttr>();
Anders Carlssona5dd7222009-08-08 19:38:24 +0000210
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000211 if (D->isUnion()) {
212 LayoutUnion(D);
213 return;
214 }
Anders Carlssona860e752009-08-08 18:23:56 +0000215
Anders Carlsson45372a62009-07-23 03:17:50 +0000216 if (LayoutFields(D))
217 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Anders Carlsson45372a62009-07-23 03:17:50 +0000219 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000220 Packed = true;
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000221 LastLaidOutBase.invalidate();
John McCallfd577d62011-02-15 22:21:29 +0000222 NextFieldOffset = CharUnits::Zero();
Anders Carlsson45372a62009-07-23 03:17:50 +0000223 FieldTypes.clear();
John McCall9b7da1c2011-02-15 06:40:56 +0000224 Fields.clear();
225 BitFields.clear();
226 NonVirtualBases.clear();
227 VirtualBases.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Anders Carlsson45372a62009-07-23 03:17:50 +0000229 LayoutFields(D);
230}
231
Daniel Dunbare7a80bd2010-09-02 23:53:28 +0000232CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
233 const FieldDecl *FD,
234 uint64_t FieldOffset,
235 uint64_t FieldSize,
236 uint64_t ContainingTypeSizeInBits,
237 unsigned ContainingTypeAlign) {
Chris Lattner2acc6e32011-07-18 04:24:23 +0000238 llvm::Type *Ty = Types.ConvertTypeForMem(FD->getType());
John McCall92ee7ca2011-02-26 08:41:59 +0000239 CharUnits TypeSizeInBytes =
240 CharUnits::fromQuantity(Types.getTargetData().getTypeAllocSize(Ty));
241 uint64_t TypeSizeInBits = Types.getContext().toBits(TypeSizeInBytes);
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000242
Douglas Gregor575a1c92011-05-20 16:38:50 +0000243 bool IsSigned = FD->getType()->isSignedIntegerOrEnumerationType();
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000244
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000245 if (FieldSize > TypeSizeInBits) {
Anders Carlsson6ba38152010-04-17 22:54:57 +0000246 // We have a wide bit-field. The extra bits are only used for padding, so
247 // if we have a bitfield of type T, with size N:
248 //
249 // T t : N;
250 //
251 // We can just assume that it's:
252 //
253 // T t : sizeof(T);
254 //
255 FieldSize = TypeSizeInBits;
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000256 }
257
Chris Lattnerd8df5b62011-02-17 22:09:58 +0000258 // in big-endian machines the first fields are in higher bit positions,
259 // so revert the offset. The byte offsets are reversed(back) later.
260 if (Types.getTargetData().isBigEndian()) {
261 FieldOffset = ((ContainingTypeSizeInBits)-FieldOffset-FieldSize);
262 }
263
Daniel Dunbare1467a42010-04-22 02:35:46 +0000264 // Compute the access components. The policy we use is to start by attempting
265 // to access using the width of the bit-field type itself and to always access
266 // at aligned indices of that type. If such an access would fail because it
267 // extends past the bound of the type, then we reduce size to the next smaller
268 // power of two and retry. The current algorithm assumes pow2 sized types,
269 // although this is easy to fix.
270 //
Daniel Dunbare1467a42010-04-22 02:35:46 +0000271 assert(llvm::isPowerOf2_32(TypeSizeInBits) && "Unexpected type size!");
272 CGBitFieldInfo::AccessInfo Components[3];
273 unsigned NumComponents = 0;
Nick Lewyckyc3e49402011-03-22 17:35:47 +0000274 unsigned AccessedTargetBits = 0; // The number of target bits accessed.
Daniel Dunbare1467a42010-04-22 02:35:46 +0000275 unsigned AccessWidth = TypeSizeInBits; // The current access width to attempt.
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000276
Daniel Dunbare26bdb92011-06-21 18:54:46 +0000277 // If requested, widen the initial bit-field access to be register sized. The
278 // theory is that this is most likely to allow multiple accesses into the same
279 // structure to be coalesced, and that the backend should be smart enough to
280 // narrow the store if no coalescing is ever done.
281 //
282 // The subsequent code will handle align these access to common boundaries and
283 // guaranteeing that we do not access past the end of the structure.
284 if (Types.getCodeGenOpts().UseRegisterSizedBitfieldAccess) {
285 if (AccessWidth < Types.getTarget().getRegisterWidth())
286 AccessWidth = Types.getTarget().getRegisterWidth();
287 }
288
Daniel Dunbare1467a42010-04-22 02:35:46 +0000289 // Round down from the field offset to find the first access position that is
290 // at an aligned offset of the initial access type.
Daniel Dunbar52968a12010-04-22 15:22:33 +0000291 uint64_t AccessStart = FieldOffset - (FieldOffset % AccessWidth);
292
293 // Adjust initial access size to fit within record.
John McCall92ee7ca2011-02-26 08:41:59 +0000294 while (AccessWidth > Types.getTarget().getCharWidth() &&
Daniel Dunbar52968a12010-04-22 15:22:33 +0000295 AccessStart + AccessWidth > ContainingTypeSizeInBits) {
296 AccessWidth >>= 1;
297 AccessStart = FieldOffset - (FieldOffset % AccessWidth);
298 }
Daniel Dunbar2df25692010-04-15 05:09:32 +0000299
Daniel Dunbare1467a42010-04-22 02:35:46 +0000300 while (AccessedTargetBits < FieldSize) {
301 // Check that we can access using a type of this size, without reading off
302 // the end of the structure. This can occur with packed structures and
303 // -fno-bitfield-type-align, for example.
304 if (AccessStart + AccessWidth > ContainingTypeSizeInBits) {
305 // If so, reduce access size to the next smaller power-of-two and retry.
306 AccessWidth >>= 1;
John McCall92ee7ca2011-02-26 08:41:59 +0000307 assert(AccessWidth >= Types.getTarget().getCharWidth()
308 && "Cannot access under byte size!");
Daniel Dunbare1467a42010-04-22 02:35:46 +0000309 continue;
310 }
Daniel Dunbarab970f92010-04-13 20:58:55 +0000311
Daniel Dunbare1467a42010-04-22 02:35:46 +0000312 // Otherwise, add an access component.
Daniel Dunbarab970f92010-04-13 20:58:55 +0000313
Daniel Dunbare1467a42010-04-22 02:35:46 +0000314 // First, compute the bits inside this access which are part of the
315 // target. We are reading bits [AccessStart, AccessStart + AccessWidth); the
316 // intersection with [FieldOffset, FieldOffset + FieldSize) gives the bits
317 // in the target that we are reading.
Daniel Dunbar52968a12010-04-22 15:22:33 +0000318 assert(FieldOffset < AccessStart + AccessWidth && "Invalid access start!");
319 assert(AccessStart < FieldOffset + FieldSize && "Invalid access start!");
Daniel Dunbare1467a42010-04-22 02:35:46 +0000320 uint64_t AccessBitsInFieldStart = std::max(AccessStart, FieldOffset);
321 uint64_t AccessBitsInFieldSize =
Daniel Dunbar52968a12010-04-22 15:22:33 +0000322 std::min(AccessWidth + AccessStart,
323 FieldOffset + FieldSize) - AccessBitsInFieldStart;
Daniel Dunbar4651efb2010-04-22 14:56:10 +0000324
Daniel Dunbare1467a42010-04-22 02:35:46 +0000325 assert(NumComponents < 3 && "Unexpected number of components!");
326 CGBitFieldInfo::AccessInfo &AI = Components[NumComponents++];
327 AI.FieldIndex = 0;
328 // FIXME: We still follow the old access pattern of only using the field
329 // byte offset. We should switch this once we fix the struct layout to be
330 // pretty.
Chris Lattnerd8df5b62011-02-17 22:09:58 +0000331
332 // on big-endian machines we reverted the bit offset because first fields are
333 // in higher bits. But this also reverts the bytes, so fix this here by reverting
334 // the byte offset on big-endian machines.
335 if (Types.getTargetData().isBigEndian()) {
Ken Dyck28ebde52011-04-24 10:04:59 +0000336 AI.FieldByteOffset = Types.getContext().toCharUnitsFromBits(
337 ContainingTypeSizeInBits - AccessStart - AccessWidth);
Chris Lattnerd8df5b62011-02-17 22:09:58 +0000338 } else {
Ken Dyck28ebde52011-04-24 10:04:59 +0000339 AI.FieldByteOffset = Types.getContext().toCharUnitsFromBits(AccessStart);
Chris Lattnerd8df5b62011-02-17 22:09:58 +0000340 }
Daniel Dunbare1467a42010-04-22 02:35:46 +0000341 AI.FieldBitStart = AccessBitsInFieldStart - AccessStart;
342 AI.AccessWidth = AccessWidth;
Ken Dyckb9e6b2c2011-04-24 10:13:17 +0000343 AI.AccessAlignment = Types.getContext().toCharUnitsFromBits(
344 llvm::MinAlign(ContainingTypeAlign, AccessStart));
Daniel Dunbare1467a42010-04-22 02:35:46 +0000345 AI.TargetBitOffset = AccessedTargetBits;
346 AI.TargetBitWidth = AccessBitsInFieldSize;
347
348 AccessStart += AccessWidth;
349 AccessedTargetBits += AI.TargetBitWidth;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000350 }
351
Daniel Dunbare1467a42010-04-22 02:35:46 +0000352 assert(AccessedTargetBits == FieldSize && "Invalid bit-field access!");
Daniel Dunbar2df25692010-04-15 05:09:32 +0000353 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000354}
355
Daniel Dunbare7a80bd2010-09-02 23:53:28 +0000356CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
357 const FieldDecl *FD,
358 uint64_t FieldOffset,
359 uint64_t FieldSize) {
360 const RecordDecl *RD = FD->getParent();
361 const ASTRecordLayout &RL = Types.getContext().getASTRecordLayout(RD);
Ken Dyckdd76a9a2011-02-11 01:54:29 +0000362 uint64_t ContainingTypeSizeInBits = Types.getContext().toBits(RL.getSize());
Ken Dyckdac54c12011-02-15 02:32:40 +0000363 unsigned ContainingTypeAlign = Types.getContext().toBits(RL.getAlignment());
Daniel Dunbare7a80bd2010-09-02 23:53:28 +0000364
365 return MakeInfo(Types, FD, FieldOffset, FieldSize, ContainingTypeSizeInBits,
366 ContainingTypeAlign);
367}
368
Anders Carlsson45372a62009-07-23 03:17:50 +0000369void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
John McCallfd577d62011-02-15 22:21:29 +0000370 uint64_t fieldOffset) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000371 uint64_t fieldSize = D->getBitWidthValue(Types.getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000372
John McCallfd577d62011-02-15 22:21:29 +0000373 if (fieldSize == 0)
Anders Carlsson45372a62009-07-23 03:17:50 +0000374 return;
375
John McCall92ee7ca2011-02-26 08:41:59 +0000376 uint64_t nextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset);
Ken Dyckedda6e42011-04-24 16:40:29 +0000377 CharUnits numBytesToAppend;
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000378 unsigned charAlign = Types.getContext().getTargetInfo().getCharAlign();
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000380 if (fieldOffset < nextFieldOffsetInBits && !BitsAvailableInLastField) {
Ken Dyckedda6e42011-04-24 16:40:29 +0000381 assert(fieldOffset % charAlign == 0 &&
382 "Field offset not aligned correctly");
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000383
384 CharUnits fieldOffsetInCharUnits =
385 Types.getContext().toCharUnitsFromBits(fieldOffset);
386
387 // Try to resize the last base field.
388 if (ResizeLastBaseFieldIfNecessary(fieldOffsetInCharUnits))
389 nextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset);
390 }
391
John McCallfd577d62011-02-15 22:21:29 +0000392 if (fieldOffset < nextFieldOffsetInBits) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000393 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
John McCallfd577d62011-02-15 22:21:29 +0000394 assert(!NextFieldOffset.isZero() && "Must have laid out at least one byte");
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Anders Carlsson45372a62009-07-23 03:17:50 +0000396 // The bitfield begins in the previous bit-field.
Ken Dyckedda6e42011-04-24 16:40:29 +0000397 numBytesToAppend = Types.getContext().toCharUnitsFromBits(
398 llvm::RoundUpToAlignment(fieldSize - BitsAvailableInLastField,
399 charAlign));
Anders Carlsson45372a62009-07-23 03:17:50 +0000400 } else {
Ken Dyckedda6e42011-04-24 16:40:29 +0000401 assert(fieldOffset % charAlign == 0 &&
402 "Field offset not aligned correctly");
Anders Carlsson45372a62009-07-23 03:17:50 +0000403
404 // Append padding if necessary.
Ken Dyckedda6e42011-04-24 16:40:29 +0000405 AppendPadding(Types.getContext().toCharUnitsFromBits(fieldOffset),
406 CharUnits::One());
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Ken Dyckedda6e42011-04-24 16:40:29 +0000408 numBytesToAppend = Types.getContext().toCharUnitsFromBits(
409 llvm::RoundUpToAlignment(fieldSize, charAlign));
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Ken Dyckedda6e42011-04-24 16:40:29 +0000411 assert(!numBytesToAppend.isZero() && "No bytes to append!");
Anders Carlsson45372a62009-07-23 03:17:50 +0000412 }
413
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000414 // Add the bit field info.
John McCall9b7da1c2011-02-15 06:40:56 +0000415 BitFields.insert(std::make_pair(D,
John McCallfd577d62011-02-15 22:21:29 +0000416 CGBitFieldInfo::MakeInfo(Types, D, fieldOffset, fieldSize)));
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Ken Dyckedda6e42011-04-24 16:40:29 +0000418 AppendBytes(numBytesToAppend);
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Mike Stump1eb44332009-09-09 15:08:12 +0000420 BitsAvailableInLastField =
Ken Dyckedda6e42011-04-24 16:40:29 +0000421 Types.getContext().toBits(NextFieldOffset) - (fieldOffset + fieldSize);
Anders Carlsson45372a62009-07-23 03:17:50 +0000422}
423
424bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
John McCallfd577d62011-02-15 22:21:29 +0000425 uint64_t fieldOffset) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000426 // If the field is packed, then we need a packed struct.
Anders Carlssona860e752009-08-08 18:23:56 +0000427 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson45372a62009-07-23 03:17:50 +0000428 return false;
429
430 if (D->isBitField()) {
431 // We must use packed structs for unnamed bit fields since they
432 // don't affect the struct alignment.
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000433 if (!Packed && !D->getDeclName())
Anders Carlsson45372a62009-07-23 03:17:50 +0000434 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000435
John McCallfd577d62011-02-15 22:21:29 +0000436 LayoutBitField(D, fieldOffset);
Anders Carlsson45372a62009-07-23 03:17:50 +0000437 return true;
438 }
Mike Stump1eb44332009-09-09 15:08:12 +0000439
John McCallf16aa102010-08-22 21:01:12 +0000440 CheckZeroInitializable(D->getType());
Daniel Dunbar270e2032010-03-31 00:11:27 +0000441
John McCall92ee7ca2011-02-26 08:41:59 +0000442 assert(fieldOffset % Types.getTarget().getCharWidth() == 0
443 && "field offset is not on a byte boundary!");
444 CharUnits fieldOffsetInBytes
445 = Types.getContext().toCharUnitsFromBits(fieldOffset);
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000447 llvm::Type *Ty = Types.ConvertTypeForMem(D->getType());
John McCallfd577d62011-02-15 22:21:29 +0000448 CharUnits typeAlignment = getTypeAlignment(Ty);
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000449
Anders Carlssona5dd7222009-08-08 19:38:24 +0000450 // If the type alignment is larger then the struct alignment, we must use
451 // a packed struct.
John McCallfd577d62011-02-15 22:21:29 +0000452 if (typeAlignment > Alignment) {
Anders Carlssona5dd7222009-08-08 19:38:24 +0000453 assert(!Packed && "Alignment is wrong even with packed struct!");
454 return false;
455 }
Mike Stump1eb44332009-09-09 15:08:12 +0000456
John McCallfd577d62011-02-15 22:21:29 +0000457 if (!Packed) {
458 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
459 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
460 if (const MaxFieldAlignmentAttr *MFAA =
461 RD->getAttr<MaxFieldAlignmentAttr>()) {
John McCall92ee7ca2011-02-26 08:41:59 +0000462 if (MFAA->getAlignment() != Types.getContext().toBits(typeAlignment))
John McCallfd577d62011-02-15 22:21:29 +0000463 return false;
464 }
Anders Carlssona5dd7222009-08-08 19:38:24 +0000465 }
466 }
467
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000468 // Round up the field offset to the alignment of the field type.
John McCallfd577d62011-02-15 22:21:29 +0000469 CharUnits alignedNextFieldOffsetInBytes =
470 NextFieldOffset.RoundUpToAlignment(typeAlignment);
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000471
John McCallfd577d62011-02-15 22:21:29 +0000472 if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000473 // Try to resize the last base field.
474 if (ResizeLastBaseFieldIfNecessary(fieldOffsetInBytes)) {
475 alignedNextFieldOffsetInBytes =
476 NextFieldOffset.RoundUpToAlignment(typeAlignment);
477 }
478 }
479
480 if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000481 assert(!Packed && "Could not place field even with packed struct!");
482 return false;
483 }
Mike Stump1eb44332009-09-09 15:08:12 +0000484
John McCallfd577d62011-02-15 22:21:29 +0000485 AppendPadding(fieldOffsetInBytes, typeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Anders Carlsson45372a62009-07-23 03:17:50 +0000487 // Now append the field.
John McCall9b7da1c2011-02-15 06:40:56 +0000488 Fields[D] = FieldTypes.size();
John McCallfd577d62011-02-15 22:21:29 +0000489 AppendField(fieldOffsetInBytes, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000491 LastLaidOutBase.invalidate();
Anders Carlsson45372a62009-07-23 03:17:50 +0000492 return true;
493}
494
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000495llvm::Type *
Anders Carlsson86664462010-04-17 20:49:27 +0000496CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
497 const ASTRecordLayout &Layout) {
498 if (Field->isBitField()) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000499 uint64_t FieldSize = Field->getBitWidthValue(Types.getContext());
Anders Carlsson86664462010-04-17 20:49:27 +0000500
501 // Ignore zero sized bit fields.
502 if (FieldSize == 0)
503 return 0;
504
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000505 llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
Ken Dyck737978d2011-04-24 16:47:33 +0000506 CharUnits NumBytesToAppend = Types.getContext().toCharUnitsFromBits(
507 llvm::RoundUpToAlignment(FieldSize,
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000508 Types.getContext().getTargetInfo().getCharAlign()));
Anders Carlssond62328e2010-04-17 21:04:52 +0000509
Ken Dyck737978d2011-04-24 16:47:33 +0000510 if (NumBytesToAppend > CharUnits::One())
511 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend.getQuantity());
Anders Carlssond62328e2010-04-17 21:04:52 +0000512
Anders Carlsson86664462010-04-17 20:49:27 +0000513 // Add the bit field info.
John McCall9b7da1c2011-02-15 06:40:56 +0000514 BitFields.insert(std::make_pair(Field,
515 CGBitFieldInfo::MakeInfo(Types, Field, 0, FieldSize)));
Anders Carlssond62328e2010-04-17 21:04:52 +0000516 return FieldTy;
Anders Carlsson86664462010-04-17 20:49:27 +0000517 }
Daniel Dunbar8ab78a72010-04-20 17:52:30 +0000518
Anders Carlsson86664462010-04-17 20:49:27 +0000519 // This is a regular union field.
John McCall9b7da1c2011-02-15 06:40:56 +0000520 Fields[Field] = 0;
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000521 return Types.ConvertTypeForMem(Field->getType());
Anders Carlsson86664462010-04-17 20:49:27 +0000522}
523
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000524void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
525 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump1eb44332009-09-09 15:08:12 +0000526
John McCallfd577d62011-02-15 22:21:29 +0000527 const ASTRecordLayout &layout = Types.getContext().getASTRecordLayout(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000529 llvm::Type *unionType = 0;
John McCallfd577d62011-02-15 22:21:29 +0000530 CharUnits unionSize = CharUnits::Zero();
531 CharUnits unionAlign = CharUnits::Zero();
Mike Stump1eb44332009-09-09 15:08:12 +0000532
John McCallfd577d62011-02-15 22:21:29 +0000533 bool hasOnlyZeroSizedBitFields = true;
Eli Friedman26e80cd2011-12-07 01:30:11 +0000534 bool checkedFirstFieldZeroInit = false;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000535
John McCallfd577d62011-02-15 22:21:29 +0000536 unsigned fieldNo = 0;
537 for (RecordDecl::field_iterator field = D->field_begin(),
538 fieldEnd = D->field_end(); field != fieldEnd; ++field, ++fieldNo) {
539 assert(layout.getFieldOffset(fieldNo) == 0 &&
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000540 "Union field offset did not start at the beginning of record!");
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000541 llvm::Type *fieldType = LayoutUnionField(*field, layout);
Anders Carlsson2cc8f172009-07-23 04:00:39 +0000542
John McCallfd577d62011-02-15 22:21:29 +0000543 if (!fieldType)
Anders Carlsson86664462010-04-17 20:49:27 +0000544 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Eli Friedman26e80cd2011-12-07 01:30:11 +0000546 if (field->getDeclName() && !checkedFirstFieldZeroInit) {
547 CheckZeroInitializable(field->getType());
548 checkedFirstFieldZeroInit = true;
549 }
550
John McCallfd577d62011-02-15 22:21:29 +0000551 hasOnlyZeroSizedBitFields = false;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000552
John McCallfd577d62011-02-15 22:21:29 +0000553 CharUnits fieldAlign = CharUnits::fromQuantity(
554 Types.getTargetData().getABITypeAlignment(fieldType));
555 CharUnits fieldSize = CharUnits::fromQuantity(
556 Types.getTargetData().getTypeAllocSize(fieldType));
Mike Stump1eb44332009-09-09 15:08:12 +0000557
John McCallfd577d62011-02-15 22:21:29 +0000558 if (fieldAlign < unionAlign)
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000559 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000560
John McCallfd577d62011-02-15 22:21:29 +0000561 if (fieldAlign > unionAlign || fieldSize > unionSize) {
562 unionType = fieldType;
563 unionAlign = fieldAlign;
564 unionSize = fieldSize;
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000565 }
566 }
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000568 // Now add our field.
John McCallfd577d62011-02-15 22:21:29 +0000569 if (unionType) {
570 AppendField(CharUnits::Zero(), unionType);
Anders Carlsson36620002009-09-03 22:56:02 +0000571
John McCallfd577d62011-02-15 22:21:29 +0000572 if (getTypeAlignment(unionType) > layout.getAlignment()) {
Anders Carlsson36620002009-09-03 22:56:02 +0000573 // We need a packed struct.
574 Packed = true;
John McCallfd577d62011-02-15 22:21:29 +0000575 unionAlign = CharUnits::One();
Anders Carlsson36620002009-09-03 22:56:02 +0000576 }
577 }
John McCallfd577d62011-02-15 22:21:29 +0000578 if (unionAlign.isZero()) {
579 assert(hasOnlyZeroSizedBitFields &&
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000580 "0-align record did not have all zero-sized bit-fields!");
John McCallfd577d62011-02-15 22:21:29 +0000581 unionAlign = CharUnits::One();
Fariborz Jahaniane5041702009-11-06 20:47:40 +0000582 }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000583
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000584 // Append tail padding.
John McCallfd577d62011-02-15 22:21:29 +0000585 CharUnits recordSize = layout.getSize();
586 if (recordSize > unionSize)
587 AppendPadding(recordSize, unionAlign);
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000588}
589
John McCall9b7da1c2011-02-15 06:40:56 +0000590void CGRecordLayoutBuilder::LayoutBase(const CXXRecordDecl *base,
John McCallfd577d62011-02-15 22:21:29 +0000591 const CGRecordLayout &baseLayout,
592 CharUnits baseOffset) {
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000593 ResizeLastBaseFieldIfNecessary(baseOffset);
594
John McCallfd577d62011-02-15 22:21:29 +0000595 AppendPadding(baseOffset, CharUnits::One());
Anders Carlsson860453c2010-12-04 23:59:48 +0000596
John McCall9b7da1c2011-02-15 06:40:56 +0000597 const ASTRecordLayout &baseASTLayout
598 = Types.getContext().getASTRecordLayout(base);
599
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000600 LastLaidOutBase.Offset = NextFieldOffset;
601 LastLaidOutBase.NonVirtualSize = baseASTLayout.getNonVirtualSize();
602
John McCallfd577d62011-02-15 22:21:29 +0000603 // Fields and bases can be laid out in the tail padding of previous
604 // bases. If this happens, we need to allocate the base as an i8
605 // array; otherwise, we can use the subobject type. However,
606 // actually doing that would require knowledge of what immediately
607 // follows this base in the layout, so instead we do a conservative
608 // approximation, which is to use the base subobject type if it
609 // has the same LLVM storage size as the nvsize.
610
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000611 llvm::StructType *subobjectType = baseLayout.getBaseSubobjectLLVMType();
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000612 AppendField(baseOffset, subobjectType);
John McCall9b7da1c2011-02-15 06:40:56 +0000613}
614
615void CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *base,
John McCallfd577d62011-02-15 22:21:29 +0000616 CharUnits baseOffset) {
John McCall9b7da1c2011-02-15 06:40:56 +0000617 // Ignore empty bases.
618 if (base->isEmpty()) return;
619
620 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
621 if (IsZeroInitializableAsBase) {
622 assert(IsZeroInitializable &&
623 "class zero-initializable as base but not as complete object");
624
625 IsZeroInitializable = IsZeroInitializableAsBase =
626 baseLayout.isZeroInitializableAsBase();
627 }
628
John McCallfd577d62011-02-15 22:21:29 +0000629 LayoutBase(base, baseLayout, baseOffset);
John McCall9b7da1c2011-02-15 06:40:56 +0000630 NonVirtualBases[base] = (FieldTypes.size() - 1);
Anders Carlsson860453c2010-12-04 23:59:48 +0000631}
632
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000633void
John McCall9b7da1c2011-02-15 06:40:56 +0000634CGRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *base,
John McCallfd577d62011-02-15 22:21:29 +0000635 CharUnits baseOffset) {
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000636 // Ignore empty bases.
John McCall9b7da1c2011-02-15 06:40:56 +0000637 if (base->isEmpty()) return;
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000638
John McCall9b7da1c2011-02-15 06:40:56 +0000639 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
640 if (IsZeroInitializable)
641 IsZeroInitializable = baseLayout.isZeroInitializableAsBase();
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000642
John McCallfd577d62011-02-15 22:21:29 +0000643 LayoutBase(base, baseLayout, baseOffset);
John McCall9b7da1c2011-02-15 06:40:56 +0000644 VirtualBases[base] = (FieldTypes.size() - 1);
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000645}
646
John McCall9da23522011-11-08 04:01:03 +0000647void
648CGRecordLayoutBuilder::MSLayoutVirtualBases(const CXXRecordDecl *RD,
649 const ASTRecordLayout &Layout) {
650 if (!RD->getNumVBases())
651 return;
652
653 // The vbases list is uniqued and ordered by a depth-first
654 // traversal, which is what we need here.
655 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
656 E = RD->vbases_end(); I != E; ++I) {
657
658 const CXXRecordDecl *BaseDecl =
659 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
660
661 CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl);
662 LayoutVirtualBase(BaseDecl, vbaseOffset);
663 }
664}
665
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000666/// LayoutVirtualBases - layout the non-virtual bases of a record decl.
667void
668CGRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
669 const ASTRecordLayout &Layout) {
670 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
671 E = RD->bases_end(); I != E; ++I) {
672 const CXXRecordDecl *BaseDecl =
673 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
674
675 // We only want to lay out virtual bases that aren't indirect primary bases
676 // of some other base.
677 if (I->isVirtual() && !IndirectPrimaryBases.count(BaseDecl)) {
678 // Only lay out the base once.
679 if (!LaidOutVirtualBases.insert(BaseDecl))
680 continue;
681
John McCallfd577d62011-02-15 22:21:29 +0000682 CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl);
683 LayoutVirtualBase(BaseDecl, vbaseOffset);
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000684 }
685
686 if (!BaseDecl->getNumVBases()) {
687 // This base isn't interesting since it doesn't have any virtual bases.
688 continue;
689 }
690
691 LayoutVirtualBases(BaseDecl, Layout);
692 }
693}
694
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000695void
696CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD,
697 const ASTRecordLayout &Layout) {
698 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
699
John McCall9da23522011-11-08 04:01:03 +0000700 // If we have a primary base, lay it out first.
701 if (PrimaryBase) {
702 if (!Layout.isPrimaryBaseVirtual())
703 LayoutNonVirtualBase(PrimaryBase, CharUnits::Zero());
704 else
705 LayoutVirtualBase(PrimaryBase, CharUnits::Zero());
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000706
John McCall9da23522011-11-08 04:01:03 +0000707 // Otherwise, add a vtable / vf-table if the layout says to do so.
708 } else if (Types.getContext().getTargetInfo().getCXXABI() == CXXABI_Microsoft
709 ? Layout.getVFPtrOffset() != CharUnits::fromQuantity(-1)
710 : RD->isDynamicClass()) {
711 llvm::Type *FunctionType =
712 llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()),
713 /*isVarArg=*/true);
714 llvm::Type *VTableTy = FunctionType->getPointerTo();
715
716 assert(NextFieldOffset.isZero() &&
717 "VTable pointer must come first!");
718 AppendField(CharUnits::Zero(), VTableTy->getPointerTo());
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000719 }
720
721 // Layout the non-virtual bases.
722 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
723 E = RD->bases_end(); I != E; ++I) {
724 if (I->isVirtual())
725 continue;
726
727 const CXXRecordDecl *BaseDecl =
728 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
729
730 // We've already laid out the primary base.
Anders Carlssonc9e814b2010-11-24 23:12:57 +0000731 if (BaseDecl == PrimaryBase && !Layout.isPrimaryBaseVirtual())
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000732 continue;
733
John McCallfd577d62011-02-15 22:21:29 +0000734 LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffset(BaseDecl));
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000735 }
John McCall9da23522011-11-08 04:01:03 +0000736
737 // Add a vb-table pointer if the layout insists.
738 if (Layout.getVBPtrOffset() != CharUnits::fromQuantity(-1)) {
739 CharUnits VBPtrOffset = Layout.getVBPtrOffset();
740 llvm::Type *Vbptr = llvm::Type::getInt32PtrTy(Types.getLLVMContext());
741 AppendPadding(VBPtrOffset, getTypeAlignment(Vbptr));
742 AppendField(VBPtrOffset, Vbptr);
743 }
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000744}
745
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000746bool
Anders Carlsson3d155e62010-11-09 05:25:47 +0000747CGRecordLayoutBuilder::ComputeNonVirtualBaseType(const CXXRecordDecl *RD) {
748 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(RD);
749
Ken Dyck68cf1a52011-02-08 02:02:47 +0000750 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
751 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
John McCallfd577d62011-02-15 22:21:29 +0000752 CharUnits AlignedNonVirtualTypeSize =
753 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
Anders Carlsson3d155e62010-11-09 05:25:47 +0000754
Anders Carlsson3d155e62010-11-09 05:25:47 +0000755 // First check if we can use the same fields as for the complete class.
John McCallfd577d62011-02-15 22:21:29 +0000756 CharUnits RecordSize = Layout.getSize();
John McCall9b7da1c2011-02-15 06:40:56 +0000757 if (AlignedNonVirtualTypeSize == RecordSize)
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000758 return true;
Anders Carlsson3d155e62010-11-09 05:25:47 +0000759
Anders Carlsson3d155e62010-11-09 05:25:47 +0000760 // Check if we need padding.
John McCallfd577d62011-02-15 22:21:29 +0000761 CharUnits AlignedNextFieldOffset =
762 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
Anders Carlsson3d155e62010-11-09 05:25:47 +0000763
John McCall9b7da1c2011-02-15 06:40:56 +0000764 if (AlignedNextFieldOffset > AlignedNonVirtualTypeSize) {
765 assert(!Packed && "cannot layout even as packed struct");
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000766 return false; // Needs packing.
Anders Carlsson3d155e62010-11-09 05:25:47 +0000767 }
768
John McCall9b7da1c2011-02-15 06:40:56 +0000769 bool needsPadding = (AlignedNonVirtualTypeSize != AlignedNextFieldOffset);
770 if (needsPadding) {
John McCallfd577d62011-02-15 22:21:29 +0000771 CharUnits NumBytes = AlignedNonVirtualTypeSize - AlignedNextFieldOffset;
John McCall9b7da1c2011-02-15 06:40:56 +0000772 FieldTypes.push_back(getByteArrayType(NumBytes));
773 }
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000774
Chris Lattnerc1c20112011-08-12 17:43:31 +0000775 BaseSubobjectType = llvm::StructType::create(Types.getLLVMContext(),
776 FieldTypes, "", Packed);
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000777 Types.addRecordTypeName(RD, BaseSubobjectType, ".base");
John McCall9b7da1c2011-02-15 06:40:56 +0000778
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000779 // Pull the padding back off.
780 if (needsPadding)
John McCall9b7da1c2011-02-15 06:40:56 +0000781 FieldTypes.pop_back();
John McCall9b7da1c2011-02-15 06:40:56 +0000782
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000783 return true;
Anders Carlsson3d155e62010-11-09 05:25:47 +0000784}
785
Anders Carlsson45372a62009-07-23 03:17:50 +0000786bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
787 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
John McCallfd577d62011-02-15 22:21:29 +0000788 assert(!Alignment.isZero() && "Did not set alignment!");
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000790 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Anders Carlsson3d155e62010-11-09 05:25:47 +0000792 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
793 if (RD)
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000794 LayoutNonVirtualBases(RD, Layout);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000795
Anders Carlsson45372a62009-07-23 03:17:50 +0000796 unsigned FieldNo = 0;
Fariborz Jahanian62055b02011-04-26 23:52:16 +0000797 const FieldDecl *LastFD = 0;
798
Mike Stump1eb44332009-09-09 15:08:12 +0000799 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson45372a62009-07-23 03:17:50 +0000800 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
Fariborz Jahanian62055b02011-04-26 23:52:16 +0000801 if (IsMsStruct) {
802 // Zero-length bitfields following non-bitfield members are
803 // ignored:
804 const FieldDecl *FD = (*Field);
Fariborz Jahanian855a8e72011-05-03 20:21:04 +0000805 if (Types.getContext().ZeroBitfieldFollowsNonBitfield(FD, LastFD)) {
Fariborz Jahanian62055b02011-04-26 23:52:16 +0000806 --FieldNo;
807 continue;
808 }
809 LastFD = FD;
810 }
811
Anders Carlsson45372a62009-07-23 03:17:50 +0000812 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000813 assert(!Packed &&
Anders Carlsson45372a62009-07-23 03:17:50 +0000814 "Could not layout fields even with a packed LLVM struct!");
815 return false;
816 }
817 }
818
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000819 if (RD) {
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000820 // We've laid out the non-virtual bases and the fields, now compute the
821 // non-virtual base field types.
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000822 if (!ComputeNonVirtualBaseType(RD)) {
823 assert(!Packed && "Could not layout even with a packed LLVM struct!");
824 return false;
825 }
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000826
John McCall9da23522011-11-08 04:01:03 +0000827 // Lay out the virtual bases. The MS ABI uses a different
828 // algorithm here due to the lack of primary virtual bases.
829 if (Types.getContext().getTargetInfo().getCXXABI() != CXXABI_Microsoft) {
830 RD->getIndirectPrimaryBases(IndirectPrimaryBases);
831 if (Layout.isPrimaryBaseVirtual())
832 IndirectPrimaryBases.insert(Layout.getPrimaryBase());
833
834 LayoutVirtualBases(RD, Layout);
835 } else {
836 MSLayoutVirtualBases(RD, Layout);
837 }
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000838 }
Anders Carlsson3d155e62010-11-09 05:25:47 +0000839
Anders Carlsson45372a62009-07-23 03:17:50 +0000840 // Append tail padding if necessary.
Ken Dyck3256de72011-04-24 16:53:44 +0000841 AppendTailPadding(Layout.getSize());
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Anders Carlsson45372a62009-07-23 03:17:50 +0000843 return true;
844}
845
Ken Dyck3256de72011-04-24 16:53:44 +0000846void CGRecordLayoutBuilder::AppendTailPadding(CharUnits RecordSize) {
847 ResizeLastBaseFieldIfNecessary(RecordSize);
Mike Stump1eb44332009-09-09 15:08:12 +0000848
Ken Dyck3256de72011-04-24 16:53:44 +0000849 assert(NextFieldOffset <= RecordSize && "Size mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000850
John McCallfd577d62011-02-15 22:21:29 +0000851 CharUnits AlignedNextFieldOffset =
852 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
Anders Carlssonc2456822009-12-08 01:24:23 +0000853
Ken Dyck3256de72011-04-24 16:53:44 +0000854 if (AlignedNextFieldOffset == RecordSize) {
Anders Carlssonc2456822009-12-08 01:24:23 +0000855 // We don't need any padding.
856 return;
857 }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000858
Ken Dyck3256de72011-04-24 16:53:44 +0000859 CharUnits NumPadBytes = RecordSize - NextFieldOffset;
Anders Carlssonc1efe362009-07-27 14:55:54 +0000860 AppendBytes(NumPadBytes);
861}
862
John McCallfd577d62011-02-15 22:21:29 +0000863void CGRecordLayoutBuilder::AppendField(CharUnits fieldOffset,
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000864 llvm::Type *fieldType) {
John McCallfd577d62011-02-15 22:21:29 +0000865 CharUnits fieldSize =
866 CharUnits::fromQuantity(Types.getTargetData().getTypeAllocSize(fieldType));
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000867
John McCallfd577d62011-02-15 22:21:29 +0000868 FieldTypes.push_back(fieldType);
Anders Carlsson45372a62009-07-23 03:17:50 +0000869
John McCallfd577d62011-02-15 22:21:29 +0000870 NextFieldOffset = fieldOffset + fieldSize;
Anders Carlsson45372a62009-07-23 03:17:50 +0000871 BitsAvailableInLastField = 0;
872}
873
John McCallfd577d62011-02-15 22:21:29 +0000874void CGRecordLayoutBuilder::AppendPadding(CharUnits fieldOffset,
875 CharUnits fieldAlignment) {
876 assert(NextFieldOffset <= fieldOffset &&
Anders Carlsson45372a62009-07-23 03:17:50 +0000877 "Incorrect field layout!");
Mike Stump1eb44332009-09-09 15:08:12 +0000878
John McCall9da23522011-11-08 04:01:03 +0000879 // Do nothing if we're already at the right offset.
880 if (fieldOffset == NextFieldOffset) return;
Anders Carlsson45372a62009-07-23 03:17:50 +0000881
John McCall9da23522011-11-08 04:01:03 +0000882 // If we're not emitting a packed LLVM type, try to avoid adding
883 // unnecessary padding fields.
884 if (!Packed) {
885 // Round up the field offset to the alignment of the field type.
886 CharUnits alignedNextFieldOffset =
887 NextFieldOffset.RoundUpToAlignment(fieldAlignment);
888 assert(alignedNextFieldOffset <= fieldOffset);
Anders Carlsson45372a62009-07-23 03:17:50 +0000889
John McCall9da23522011-11-08 04:01:03 +0000890 // If that's the right offset, we're done.
891 if (alignedNextFieldOffset == fieldOffset) return;
Anders Carlsson45372a62009-07-23 03:17:50 +0000892 }
John McCall9da23522011-11-08 04:01:03 +0000893
894 // Otherwise we need explicit padding.
895 CharUnits padding = fieldOffset - NextFieldOffset;
896 AppendBytes(padding);
Anders Carlsson45372a62009-07-23 03:17:50 +0000897}
898
Anders Carlssoneb9d81d2011-04-17 21:56:13 +0000899bool CGRecordLayoutBuilder::ResizeLastBaseFieldIfNecessary(CharUnits offset) {
900 // Check if we have a base to resize.
901 if (!LastLaidOutBase.isValid())
902 return false;
903
904 // This offset does not overlap with the tail padding.
905 if (offset >= NextFieldOffset)
906 return false;
907
908 // Restore the field offset and append an i8 array instead.
909 FieldTypes.pop_back();
910 NextFieldOffset = LastLaidOutBase.Offset;
911 AppendBytes(LastLaidOutBase.NonVirtualSize);
912 LastLaidOutBase.invalidate();
913
914 return true;
915}
916
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000917llvm::Type *CGRecordLayoutBuilder::getByteArrayType(CharUnits numBytes) {
John McCallfd577d62011-02-15 22:21:29 +0000918 assert(!numBytes.isZero() && "Empty byte arrays aren't allowed.");
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000920 llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
John McCallfd577d62011-02-15 22:21:29 +0000921 if (numBytes > CharUnits::One())
922 Ty = llvm::ArrayType::get(Ty, numBytes.getQuantity());
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Anders Carlsson3d155e62010-11-09 05:25:47 +0000924 return Ty;
925}
926
John McCallfd577d62011-02-15 22:21:29 +0000927void CGRecordLayoutBuilder::AppendBytes(CharUnits numBytes) {
928 if (numBytes.isZero())
Anders Carlsson3d155e62010-11-09 05:25:47 +0000929 return;
930
Anders Carlsson45372a62009-07-23 03:17:50 +0000931 // Append the padding field
John McCallfd577d62011-02-15 22:21:29 +0000932 AppendField(NextFieldOffset, getByteArrayType(numBytes));
Anders Carlsson45372a62009-07-23 03:17:50 +0000933}
934
Chris Lattner2acc6e32011-07-18 04:24:23 +0000935CharUnits CGRecordLayoutBuilder::getTypeAlignment(llvm::Type *Ty) const {
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000936 if (Packed)
John McCallfd577d62011-02-15 22:21:29 +0000937 return CharUnits::One();
Mike Stump1eb44332009-09-09 15:08:12 +0000938
John McCallfd577d62011-02-15 22:21:29 +0000939 return CharUnits::fromQuantity(Types.getTargetData().getABITypeAlignment(Ty));
Anders Carlsson45372a62009-07-23 03:17:50 +0000940}
941
John McCallfd577d62011-02-15 22:21:29 +0000942CharUnits CGRecordLayoutBuilder::getAlignmentAsLLVMStruct() const {
Anders Carlssonfc86d552010-11-28 23:06:23 +0000943 if (Packed)
John McCallfd577d62011-02-15 22:21:29 +0000944 return CharUnits::One();
Anders Carlssonfc86d552010-11-28 23:06:23 +0000945
John McCallfd577d62011-02-15 22:21:29 +0000946 CharUnits maxAlignment = CharUnits::One();
Anders Carlssonfc86d552010-11-28 23:06:23 +0000947 for (size_t i = 0; i != FieldTypes.size(); ++i)
John McCallfd577d62011-02-15 22:21:29 +0000948 maxAlignment = std::max(maxAlignment, getTypeAlignment(FieldTypes[i]));
Anders Carlssonfc86d552010-11-28 23:06:23 +0000949
John McCallfd577d62011-02-15 22:21:29 +0000950 return maxAlignment;
Anders Carlssonfc86d552010-11-28 23:06:23 +0000951}
952
John McCall9b7da1c2011-02-15 06:40:56 +0000953/// Merge in whether a field of the given type is zero-initializable.
John McCallf16aa102010-08-22 21:01:12 +0000954void CGRecordLayoutBuilder::CheckZeroInitializable(QualType T) {
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000955 // This record already contains a member pointer.
John McCall9b7da1c2011-02-15 06:40:56 +0000956 if (!IsZeroInitializableAsBase)
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000957 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000959 // Can only have member pointers if we're compiling C++.
960 if (!Types.getContext().getLangOptions().CPlusPlus)
961 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000962
John McCall9b7da1c2011-02-15 06:40:56 +0000963 const Type *elementType = T->getBaseElementTypeUnsafe();
Mike Stump1eb44332009-09-09 15:08:12 +0000964
John McCall9b7da1c2011-02-15 06:40:56 +0000965 if (const MemberPointerType *MPT = elementType->getAs<MemberPointerType>()) {
John McCallf16aa102010-08-22 21:01:12 +0000966 if (!Types.getCXXABI().isZeroInitializable(MPT))
John McCall9b7da1c2011-02-15 06:40:56 +0000967 IsZeroInitializable = IsZeroInitializableAsBase = false;
968 } else if (const RecordType *RT = elementType->getAs<RecordType>()) {
Anders Carlsson2c12d032010-02-02 05:17:25 +0000969 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
John McCall9b7da1c2011-02-15 06:40:56 +0000970 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
971 if (!Layout.isZeroInitializable())
972 IsZeroInitializable = IsZeroInitializableAsBase = false;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000973 }
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000974}
975
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000976CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D,
977 llvm::StructType *Ty) {
Daniel Dunbar270e2032010-03-31 00:11:27 +0000978 CGRecordLayoutBuilder Builder(*this);
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Anders Carlsson45372a62009-07-23 03:17:50 +0000980 Builder.Layout(D);
Anders Carlsson4c98efd2009-07-24 15:20:52 +0000981
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000982 Ty->setBody(Builder.FieldTypes, Builder.Packed);
Mike Stump1eb44332009-09-09 15:08:12 +0000983
John McCall9b7da1c2011-02-15 06:40:56 +0000984 // If we're in C++, compute the base subobject type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000985 llvm::StructType *BaseTy = 0;
Anders Carlsson3d155e62010-11-09 05:25:47 +0000986 if (isa<CXXRecordDecl>(D)) {
John McCall9b7da1c2011-02-15 06:40:56 +0000987 BaseTy = Builder.BaseSubobjectType;
988 if (!BaseTy) BaseTy = Ty;
Anders Carlsson3d155e62010-11-09 05:25:47 +0000989 }
990
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000991 CGRecordLayout *RL =
John McCall9b7da1c2011-02-15 06:40:56 +0000992 new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable,
993 Builder.IsZeroInitializableAsBase);
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000994
John McCall9b7da1c2011-02-15 06:40:56 +0000995 RL->NonVirtualBases.swap(Builder.NonVirtualBases);
996 RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases);
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000997
Anders Carlsson45372a62009-07-23 03:17:50 +0000998 // Add all the field numbers.
John McCall9b7da1c2011-02-15 06:40:56 +0000999 RL->FieldInfo.swap(Builder.Fields);
Anders Carlsson45372a62009-07-23 03:17:50 +00001000
1001 // Add bitfield info.
John McCall9b7da1c2011-02-15 06:40:56 +00001002 RL->BitFields.swap(Builder.BitFields);
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +00001004 // Dump the layout, if requested.
Daniel Dunbarab970f92010-04-13 20:58:55 +00001005 if (getContext().getLangOptions().DumpRecordLayouts) {
Daniel Dunbar8d8ab742010-04-19 20:44:53 +00001006 llvm::errs() << "\n*** Dumping IRgen Record Layout\n";
Daniel Dunbarab970f92010-04-13 20:58:55 +00001007 llvm::errs() << "Record: ";
1008 D->dump();
1009 llvm::errs() << "\nLayout: ";
Daniel Dunbar93c62962010-04-12 18:14:18 +00001010 RL->dump();
Daniel Dunbarab970f92010-04-13 20:58:55 +00001011 }
Daniel Dunbar93c62962010-04-12 18:14:18 +00001012
Daniel Dunbare1467a42010-04-22 02:35:46 +00001013#ifndef NDEBUG
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +00001014 // Verify that the computed LLVM struct size matches the AST layout size.
Anders Carlsson3d155e62010-11-09 05:25:47 +00001015 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
1016
Ken Dyckdd76a9a2011-02-11 01:54:29 +00001017 uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize());
Daniel Dunbare1467a42010-04-22 02:35:46 +00001018 assert(TypeSizeInBits == getTargetData().getTypeAllocSizeInBits(Ty) &&
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +00001019 "Type size mismatch!");
1020
Anders Carlsson3d155e62010-11-09 05:25:47 +00001021 if (BaseTy) {
Ken Dyck68cf1a52011-02-08 02:02:47 +00001022 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
1023 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
1024 CharUnits AlignedNonVirtualTypeSize =
1025 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
1026
1027 uint64_t AlignedNonVirtualTypeSizeInBits =
Ken Dyckdd76a9a2011-02-11 01:54:29 +00001028 getContext().toBits(AlignedNonVirtualTypeSize);
Anders Carlsson3d155e62010-11-09 05:25:47 +00001029
1030 assert(AlignedNonVirtualTypeSizeInBits ==
1031 getTargetData().getTypeAllocSizeInBits(BaseTy) &&
1032 "Type size mismatch!");
1033 }
1034
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +00001035 // Verify that the LLVM and AST field offsets agree.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001036 llvm::StructType *ST =
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +00001037 dyn_cast<llvm::StructType>(RL->getLLVMType());
1038 const llvm::StructLayout *SL = getTargetData().getStructLayout(ST);
1039
1040 const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
1041 RecordDecl::field_iterator it = D->field_begin();
Fariborz Jahanian62055b02011-04-26 23:52:16 +00001042 const FieldDecl *LastFD = 0;
1043 bool IsMsStruct = D->hasAttr<MsStructAttr>();
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +00001044 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
1045 const FieldDecl *FD = *it;
Daniel Dunbare1467a42010-04-22 02:35:46 +00001046
1047 // For non-bit-fields, just check that the LLVM struct offset matches the
1048 // AST offset.
1049 if (!FD->isBitField()) {
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +00001050 unsigned FieldNo = RL->getLLVMFieldNo(FD);
1051 assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
1052 "Invalid field offset!");
Fariborz Jahanian62055b02011-04-26 23:52:16 +00001053 LastFD = FD;
Daniel Dunbare1467a42010-04-22 02:35:46 +00001054 continue;
1055 }
1056
Fariborz Jahanian62055b02011-04-26 23:52:16 +00001057 if (IsMsStruct) {
1058 // Zero-length bitfields following non-bitfield members are
1059 // ignored:
Fariborz Jahanian855a8e72011-05-03 20:21:04 +00001060 if (getContext().ZeroBitfieldFollowsNonBitfield(FD, LastFD)) {
Fariborz Jahanian62055b02011-04-26 23:52:16 +00001061 --i;
1062 continue;
1063 }
1064 LastFD = FD;
1065 }
1066
Daniel Dunbare1467a42010-04-22 02:35:46 +00001067 // Ignore unnamed bit-fields.
Fariborz Jahanian62055b02011-04-26 23:52:16 +00001068 if (!FD->getDeclName()) {
1069 LastFD = FD;
Daniel Dunbare1467a42010-04-22 02:35:46 +00001070 continue;
Fariborz Jahanian62055b02011-04-26 23:52:16 +00001071 }
1072
Daniel Dunbare1467a42010-04-22 02:35:46 +00001073 const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
1074 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
1075 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
1076
1077 // Verify that every component access is within the structure.
1078 uint64_t FieldOffset = SL->getElementOffsetInBits(AI.FieldIndex);
John McCall92ee7ca2011-02-26 08:41:59 +00001079 uint64_t AccessBitOffset = FieldOffset +
Ken Dyck28ebde52011-04-24 10:04:59 +00001080 getContext().toBits(AI.FieldByteOffset);
Daniel Dunbare1467a42010-04-22 02:35:46 +00001081 assert(AccessBitOffset + AI.AccessWidth <= TypeSizeInBits &&
1082 "Invalid bit-field access (out of range)!");
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +00001083 }
1084 }
1085#endif
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +00001086
Daniel Dunbar198bcb42010-03-31 01:09:11 +00001087 return RL;
Anders Carlsson45372a62009-07-23 03:17:50 +00001088}
Daniel Dunbar93c62962010-04-12 18:14:18 +00001089
Chris Lattner5f9e2722011-07-23 10:55:15 +00001090void CGRecordLayout::print(raw_ostream &OS) const {
Daniel Dunbar93c62962010-04-12 18:14:18 +00001091 OS << "<CGRecordLayout\n";
John McCall9b7da1c2011-02-15 06:40:56 +00001092 OS << " LLVMType:" << *CompleteObjectType << "\n";
1093 if (BaseSubobjectType)
1094 OS << " NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n";
John McCallf16aa102010-08-22 21:01:12 +00001095 OS << " IsZeroInitializable:" << IsZeroInitializable << "\n";
Daniel Dunbar93c62962010-04-12 18:14:18 +00001096 OS << " BitFields:[\n";
Daniel Dunbarad759532010-04-22 02:35:36 +00001097
1098 // Print bit-field infos in declaration order.
1099 std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
Daniel Dunbar93c62962010-04-12 18:14:18 +00001100 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
1101 it = BitFields.begin(), ie = BitFields.end();
1102 it != ie; ++it) {
Daniel Dunbarad759532010-04-22 02:35:36 +00001103 const RecordDecl *RD = it->first->getParent();
1104 unsigned Index = 0;
1105 for (RecordDecl::field_iterator
1106 it2 = RD->field_begin(); *it2 != it->first; ++it2)
1107 ++Index;
1108 BFIs.push_back(std::make_pair(Index, &it->second));
1109 }
1110 llvm::array_pod_sort(BFIs.begin(), BFIs.end());
1111 for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
Daniel Dunbarab970f92010-04-13 20:58:55 +00001112 OS.indent(4);
Daniel Dunbarad759532010-04-22 02:35:36 +00001113 BFIs[i].second->print(OS);
Daniel Dunbar93c62962010-04-12 18:14:18 +00001114 OS << "\n";
1115 }
Daniel Dunbarad759532010-04-22 02:35:36 +00001116
Daniel Dunbar93c62962010-04-12 18:14:18 +00001117 OS << "]>\n";
1118}
1119
1120void CGRecordLayout::dump() const {
1121 print(llvm::errs());
1122}
1123
Chris Lattner5f9e2722011-07-23 10:55:15 +00001124void CGBitFieldInfo::print(raw_ostream &OS) const {
Daniel Dunbar93c62962010-04-12 18:14:18 +00001125 OS << "<CGBitFieldInfo";
Daniel Dunbar93c62962010-04-12 18:14:18 +00001126 OS << " Size:" << Size;
Daniel Dunbarab970f92010-04-13 20:58:55 +00001127 OS << " IsSigned:" << IsSigned << "\n";
1128
1129 OS.indent(4 + strlen("<CGBitFieldInfo"));
1130 OS << " NumComponents:" << getNumComponents();
1131 OS << " Components: [";
1132 if (getNumComponents()) {
1133 OS << "\n";
1134 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
1135 const AccessInfo &AI = getComponent(i);
1136 OS.indent(8);
1137 OS << "<AccessInfo"
1138 << " FieldIndex:" << AI.FieldIndex
Ken Dyck28ebde52011-04-24 10:04:59 +00001139 << " FieldByteOffset:" << AI.FieldByteOffset.getQuantity()
Daniel Dunbarab970f92010-04-13 20:58:55 +00001140 << " FieldBitStart:" << AI.FieldBitStart
1141 << " AccessWidth:" << AI.AccessWidth << "\n";
1142 OS.indent(8 + strlen("<AccessInfo"));
Ken Dyckb9e6b2c2011-04-24 10:13:17 +00001143 OS << " AccessAlignment:" << AI.AccessAlignment.getQuantity()
Daniel Dunbarab970f92010-04-13 20:58:55 +00001144 << " TargetBitOffset:" << AI.TargetBitOffset
1145 << " TargetBitWidth:" << AI.TargetBitWidth
1146 << ">\n";
1147 }
1148 OS.indent(4);
1149 }
1150 OS << "]>";
Daniel Dunbar93c62962010-04-12 18:14:18 +00001151}
1152
1153void CGBitFieldInfo::dump() const {
1154 print(llvm::errs());
1155}