blob: 6daf2ea371ce6c71ced58fc475530a5ca05bb1d3 [file] [log] [blame]
Daniel Dunbar23ee4b72010-03-31 00:11:27 +00001//===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder ----*- C++ -*-===//
Anders Carlsson307846f2009-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 Dunbar23ee4b72010-03-31 00:11:27 +000010// Builder implementation for CGRecordLayout objects.
Anders Carlsson307846f2009-07-23 03:17:50 +000011//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000014#include "CGRecordLayout.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/Attr.h"
Anders Carlsson4131f002010-11-24 22:50:27 +000017#include "clang/AST/CXXInheritance.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000018#include "clang/AST/DeclCXX.h"
19#include "clang/AST/Expr.h"
20#include "clang/AST/RecordLayout.h"
Daniel Dunbard3f3d932011-06-21 18:54:46 +000021#include "clang/Frontend/CodeGenOptions.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000022#include "CodeGenTypes.h"
John McCall614dbdc2010-08-22 21:01:12 +000023#include "CGCXXABI.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000024#include "llvm/DerivedTypes.h"
Daniel Dunbarb97bff92010-04-12 18:14:18 +000025#include "llvm/Type.h"
Daniel Dunbar2ba67442010-04-21 19:10:49 +000026#include "llvm/Support/Debug.h"
Daniel Dunbarb97bff92010-04-12 18:14:18 +000027#include "llvm/Support/raw_ostream.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000028#include "llvm/Target/TargetData.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000029using namespace clang;
30using namespace CodeGen;
31
John McCallbcd38212010-11-30 23:17:27 +000032namespace {
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000033
34class CGRecordLayoutBuilder {
35public:
36 /// FieldTypes - Holds the LLVM types that the struct is created from.
John McCall0217dfc22011-02-15 06:40:56 +000037 ///
Anders Carlssonb6d31e72011-04-17 21:32:41 +000038 llvm::SmallVector<const llvm::Type *, 16> FieldTypes;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000039
John McCall0217dfc22011-02-15 06:40:56 +000040 /// BaseSubobjectType - Holds the LLVM type for the non-virtual part
Anders Carlssonc1351ca2010-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 McCall0217dfc22011-02-15 06:40:56 +000052 ///
53 /// This only gets initialized if the base subobject type is
54 /// different from the complete-object type.
55 const llvm::StructType *BaseSubobjectType;
Anders Carlssonc1351ca2010-11-09 05:25:47 +000056
John McCall0217dfc22011-02-15 06:40:56 +000057 /// FieldInfo - Holds a field and its corresponding LLVM field number.
58 llvm::DenseMap<const FieldDecl *, unsigned> Fields;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000059
John McCall0217dfc22011-02-15 06:40:56 +000060 /// BitFieldInfo - Holds location and size information about a bit field.
61 llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000062
John McCall0217dfc22011-02-15 06:40:56 +000063 llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases;
64 llvm::DenseMap<const CXXRecordDecl *, unsigned> VirtualBases;
Anders Carlsson4131f002010-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 Carlssona459adb2010-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 McCall614dbdc2010-08-22 21:01:12 +000074 /// IsZeroInitializable - Whether this struct can be C++
75 /// zero-initialized with an LLVM zeroinitializer.
76 bool IsZeroInitializable;
John McCall0217dfc22011-02-15 06:40:56 +000077 bool IsZeroInitializableAsBase;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000078
79 /// Packed - Whether the resulting LLVM struct will be packed or not.
80 bool Packed;
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +000081
82 /// IsMsStruct - Whether ms_struct is in effect or not
83 bool IsMsStruct;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000084
85private:
86 CodeGenTypes &Types;
87
Anders Carlssonfcaaa692011-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 Dunbar23ee4b72010-03-31 00:11:27 +0000100 /// Alignment - Contains the alignment of the RecordDecl.
John McCall4d9f1422011-02-15 22:21:29 +0000101 CharUnits Alignment;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000102
Daniel Dunbar23ee4b72010-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 McCall4d9f1422011-02-15 22:21:29 +0000107 /// NextFieldOffset - Holds the next field offset.
108 CharUnits NextFieldOffset;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000109
Anders Carlsson1de2f572010-04-17 20:49:27 +0000110 /// LayoutUnionField - Will layout a field in an union and return the type
111 /// that the field will have.
112 const llvm::Type *LayoutUnionField(const FieldDecl *Field,
113 const ASTRecordLayout &Layout);
114
Daniel Dunbar23ee4b72010-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 Carlssona518b2a2010-12-04 23:59:48 +0000122 /// Layout a single base, virtual or non-virtual
John McCall4d9f1422011-02-15 22:21:29 +0000123 void LayoutBase(const CXXRecordDecl *base,
124 const CGRecordLayout &baseLayout,
125 CharUnits baseOffset);
Anders Carlssona518b2a2010-12-04 23:59:48 +0000126
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000127 /// LayoutVirtualBase - layout a single virtual base.
John McCall4d9f1422011-02-15 22:21:29 +0000128 void LayoutVirtualBase(const CXXRecordDecl *base,
129 CharUnits baseOffset);
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000130
Anders Carlssona459adb2010-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 Carlssonaf9e5af2010-05-18 05:12:20 +0000135 /// LayoutNonVirtualBase - layout a single non-virtual base.
John McCall4d9f1422011-02-15 22:21:29 +0000136 void LayoutNonVirtualBase(const CXXRecordDecl *base,
137 CharUnits baseOffset);
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000138
Anders Carlssona459adb2010-11-28 19:18:44 +0000139 /// LayoutNonVirtualBases - layout the virtual bases of a record decl.
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000140 void LayoutNonVirtualBases(const CXXRecordDecl *RD,
141 const ASTRecordLayout &Layout);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000142
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000143 /// ComputeNonVirtualBaseType - Compute the non-virtual base field types.
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000144 bool ComputeNonVirtualBaseType(const CXXRecordDecl *RD);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000145
Daniel Dunbar23ee4b72010-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.
John McCall4d9f1422011-02-15 22:21:29 +0000154 void AppendField(CharUnits fieldOffset, const llvm::Type *FieldTy);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000155
Daniel Dunbar23ee4b72010-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 McCall4d9f1422011-02-15 22:21:29 +0000158 void AppendPadding(CharUnits fieldOffset, CharUnits fieldAlignment);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000159
Anders Carlssonfcaaa692011-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 Carlssonc1351ca2010-11-09 05:25:47 +0000166 /// getByteArrayType - Returns a byte array type with the given number of
167 /// elements.
John McCall4d9f1422011-02-15 22:21:29 +0000168 const llvm::Type *getByteArrayType(CharUnits NumBytes);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000169
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000170 /// AppendBytes - Append a given number of bytes to the record.
John McCall4d9f1422011-02-15 22:21:29 +0000171 void AppendBytes(CharUnits numBytes);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000172
173 /// AppendTailPadding - Append enough tail padding so that the type will have
174 /// the passed size.
Ken Dyck272b6fa2011-04-24 16:53:44 +0000175 void AppendTailPadding(CharUnits RecordSize);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000176
John McCall4d9f1422011-02-15 22:21:29 +0000177 CharUnits getTypeAlignment(const llvm::Type *Ty) const;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000178
Anders Carlssonacf877b2010-11-28 23:06:23 +0000179 /// getAlignmentAsLLVMStruct - Returns the maximum alignment of all the
180 /// LLVM element types.
John McCall4d9f1422011-02-15 22:21:29 +0000181 CharUnits getAlignmentAsLLVMStruct() const;
Anders Carlssonacf877b2010-11-28 23:06:23 +0000182
John McCall614dbdc2010-08-22 21:01:12 +0000183 /// CheckZeroInitializable - Check if the given type contains a pointer
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000184 /// to data member.
John McCall614dbdc2010-08-22 21:01:12 +0000185 void CheckZeroInitializable(QualType T);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000186
187public:
188 CGRecordLayoutBuilder(CodeGenTypes &Types)
John McCall0217dfc22011-02-15 06:40:56 +0000189 : BaseSubobjectType(0),
190 IsZeroInitializable(true), IsZeroInitializableAsBase(true),
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000191 Packed(false), IsMsStruct(false),
192 Types(Types), BitsAvailableInLastField(0) { }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000193
194 /// Layout - Will layout a RecordDecl.
195 void Layout(const RecordDecl *D);
196};
197
198}
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000199
Anders Carlsson307846f2009-07-23 03:17:50 +0000200void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
John McCall4d9f1422011-02-15 22:21:29 +0000201 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment();
Anders Carlsson09a37742009-09-02 17:51:33 +0000202 Packed = D->hasAttr<PackedAttr>();
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000203
204 IsMsStruct = D->hasAttr<MsStructAttr>();
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000205
Anders Carlsson697f6592009-07-23 03:43:54 +0000206 if (D->isUnion()) {
207 LayoutUnion(D);
208 return;
209 }
Anders Carlsson68e0b682009-08-08 18:23:56 +0000210
Anders Carlsson307846f2009-07-23 03:17:50 +0000211 if (LayoutFields(D))
212 return;
Mike Stump11289f42009-09-09 15:08:12 +0000213
Anders Carlsson307846f2009-07-23 03:17:50 +0000214 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlssond78fc892009-07-23 17:24:40 +0000215 Packed = true;
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000216 LastLaidOutBase.invalidate();
John McCall4d9f1422011-02-15 22:21:29 +0000217 NextFieldOffset = CharUnits::Zero();
Anders Carlsson307846f2009-07-23 03:17:50 +0000218 FieldTypes.clear();
John McCall0217dfc22011-02-15 06:40:56 +0000219 Fields.clear();
220 BitFields.clear();
221 NonVirtualBases.clear();
222 VirtualBases.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000223
Anders Carlsson307846f2009-07-23 03:17:50 +0000224 LayoutFields(D);
225}
226
Daniel Dunbarc7f9bba2010-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) {
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000233 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(FD->getType());
John McCall8a3c5552011-02-26 08:41:59 +0000234 CharUnits TypeSizeInBytes =
235 CharUnits::fromQuantity(Types.getTargetData().getTypeAllocSize(Ty));
236 uint64_t TypeSizeInBits = Types.getContext().toBits(TypeSizeInBytes);
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000237
Douglas Gregor6ab2fa82011-05-20 16:38:50 +0000238 bool IsSigned = FD->getType()->isSignedIntegerOrEnumerationType();
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000239
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000240 if (FieldSize > TypeSizeInBits) {
Anders Carlssond5f27b02010-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 Carlssonbe6f3182010-04-16 16:23:02 +0000251 }
252
Chris Lattnerfb59c7c2011-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 Dunbar488f55c2010-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 Dunbar488f55c2010-04-22 02:35:46 +0000266 assert(llvm::isPowerOf2_32(TypeSizeInBits) && "Unexpected type size!");
267 CGBitFieldInfo::AccessInfo Components[3];
268 unsigned NumComponents = 0;
Nick Lewyckyd2348d82011-03-22 17:35:47 +0000269 unsigned AccessedTargetBits = 0; // The number of target bits accessed.
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000270 unsigned AccessWidth = TypeSizeInBits; // The current access width to attempt.
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000271
Daniel Dunbard3f3d932011-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 Dunbar488f55c2010-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 Dunbar59813772010-04-22 15:22:33 +0000286 uint64_t AccessStart = FieldOffset - (FieldOffset % AccessWidth);
287
288 // Adjust initial access size to fit within record.
John McCall8a3c5552011-02-26 08:41:59 +0000289 while (AccessWidth > Types.getTarget().getCharWidth() &&
Daniel Dunbar59813772010-04-22 15:22:33 +0000290 AccessStart + AccessWidth > ContainingTypeSizeInBits) {
291 AccessWidth >>= 1;
292 AccessStart = FieldOffset - (FieldOffset % AccessWidth);
293 }
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000294
Daniel Dunbar488f55c2010-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 McCall8a3c5552011-02-26 08:41:59 +0000302 assert(AccessWidth >= Types.getTarget().getCharWidth()
303 && "Cannot access under byte size!");
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000304 continue;
305 }
Daniel Dunbarb935b932010-04-13 20:58:55 +0000306
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000307 // Otherwise, add an access component.
Daniel Dunbarb935b932010-04-13 20:58:55 +0000308
Daniel Dunbar488f55c2010-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 Dunbar59813772010-04-22 15:22:33 +0000313 assert(FieldOffset < AccessStart + AccessWidth && "Invalid access start!");
314 assert(AccessStart < FieldOffset + FieldSize && "Invalid access start!");
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000315 uint64_t AccessBitsInFieldStart = std::max(AccessStart, FieldOffset);
316 uint64_t AccessBitsInFieldSize =
Daniel Dunbar59813772010-04-22 15:22:33 +0000317 std::min(AccessWidth + AccessStart,
318 FieldOffset + FieldSize) - AccessBitsInFieldStart;
Daniel Dunbar5d6c07e2010-04-22 14:56:10 +0000319
Daniel Dunbar488f55c2010-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 Lattnerfb59c7c2011-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 Dyckf76759c2011-04-24 10:04:59 +0000331 AI.FieldByteOffset = Types.getContext().toCharUnitsFromBits(
332 ContainingTypeSizeInBits - AccessStart - AccessWidth);
Chris Lattnerfb59c7c2011-02-17 22:09:58 +0000333 } else {
Ken Dyckf76759c2011-04-24 10:04:59 +0000334 AI.FieldByteOffset = Types.getContext().toCharUnitsFromBits(AccessStart);
Chris Lattnerfb59c7c2011-02-17 22:09:58 +0000335 }
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000336 AI.FieldBitStart = AccessBitsInFieldStart - AccessStart;
337 AI.AccessWidth = AccessWidth;
Ken Dyck27337a82011-04-24 10:13:17 +0000338 AI.AccessAlignment = Types.getContext().toCharUnitsFromBits(
339 llvm::MinAlign(ContainingTypeAlign, AccessStart));
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000340 AI.TargetBitOffset = AccessedTargetBits;
341 AI.TargetBitWidth = AccessBitsInFieldSize;
342
343 AccessStart += AccessWidth;
344 AccessedTargetBits += AI.TargetBitWidth;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000345 }
346
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000347 assert(AccessedTargetBits == FieldSize && "Invalid bit-field access!");
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000348 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000349}
350
Daniel Dunbarc7f9bba2010-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 Dyckb0fcc592011-02-11 01:54:29 +0000357 uint64_t ContainingTypeSizeInBits = Types.getContext().toBits(RL.getSize());
Ken Dyck7ad11e72011-02-15 02:32:40 +0000358 unsigned ContainingTypeAlign = Types.getContext().toBits(RL.getAlignment());
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000359
360 return MakeInfo(Types, FD, FieldOffset, FieldSize, ContainingTypeSizeInBits,
361 ContainingTypeAlign);
362}
363
Anders Carlsson307846f2009-07-23 03:17:50 +0000364void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
John McCall4d9f1422011-02-15 22:21:29 +0000365 uint64_t fieldOffset) {
366 uint64_t fieldSize =
Anders Carlsson307846f2009-07-23 03:17:50 +0000367 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump11289f42009-09-09 15:08:12 +0000368
John McCall4d9f1422011-02-15 22:21:29 +0000369 if (fieldSize == 0)
Anders Carlsson307846f2009-07-23 03:17:50 +0000370 return;
371
John McCall8a3c5552011-02-26 08:41:59 +0000372 uint64_t nextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset);
Ken Dyck345a6de2011-04-24 16:40:29 +0000373 CharUnits numBytesToAppend;
374 unsigned charAlign = Types.getContext().Target.getCharAlign();
Mike Stump11289f42009-09-09 15:08:12 +0000375
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000376 if (fieldOffset < nextFieldOffsetInBits && !BitsAvailableInLastField) {
Ken Dyck345a6de2011-04-24 16:40:29 +0000377 assert(fieldOffset % charAlign == 0 &&
378 "Field offset not aligned correctly");
Anders Carlssonfcaaa692011-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 McCall4d9f1422011-02-15 22:21:29 +0000388 if (fieldOffset < nextFieldOffsetInBits) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000389 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
John McCall4d9f1422011-02-15 22:21:29 +0000390 assert(!NextFieldOffset.isZero() && "Must have laid out at least one byte");
Mike Stump11289f42009-09-09 15:08:12 +0000391
Anders Carlsson307846f2009-07-23 03:17:50 +0000392 // The bitfield begins in the previous bit-field.
Ken Dyck345a6de2011-04-24 16:40:29 +0000393 numBytesToAppend = Types.getContext().toCharUnitsFromBits(
394 llvm::RoundUpToAlignment(fieldSize - BitsAvailableInLastField,
395 charAlign));
Anders Carlsson307846f2009-07-23 03:17:50 +0000396 } else {
Ken Dyck345a6de2011-04-24 16:40:29 +0000397 assert(fieldOffset % charAlign == 0 &&
398 "Field offset not aligned correctly");
Anders Carlsson307846f2009-07-23 03:17:50 +0000399
400 // Append padding if necessary.
Ken Dyck345a6de2011-04-24 16:40:29 +0000401 AppendPadding(Types.getContext().toCharUnitsFromBits(fieldOffset),
402 CharUnits::One());
Mike Stump11289f42009-09-09 15:08:12 +0000403
Ken Dyck345a6de2011-04-24 16:40:29 +0000404 numBytesToAppend = Types.getContext().toCharUnitsFromBits(
405 llvm::RoundUpToAlignment(fieldSize, charAlign));
Mike Stump11289f42009-09-09 15:08:12 +0000406
Ken Dyck345a6de2011-04-24 16:40:29 +0000407 assert(!numBytesToAppend.isZero() && "No bytes to append!");
Anders Carlsson307846f2009-07-23 03:17:50 +0000408 }
409
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000410 // Add the bit field info.
John McCall0217dfc22011-02-15 06:40:56 +0000411 BitFields.insert(std::make_pair(D,
John McCall4d9f1422011-02-15 22:21:29 +0000412 CGBitFieldInfo::MakeInfo(Types, D, fieldOffset, fieldSize)));
Mike Stump11289f42009-09-09 15:08:12 +0000413
Ken Dyck345a6de2011-04-24 16:40:29 +0000414 AppendBytes(numBytesToAppend);
Mike Stump11289f42009-09-09 15:08:12 +0000415
Mike Stump11289f42009-09-09 15:08:12 +0000416 BitsAvailableInLastField =
Ken Dyck345a6de2011-04-24 16:40:29 +0000417 Types.getContext().toBits(NextFieldOffset) - (fieldOffset + fieldSize);
Anders Carlsson307846f2009-07-23 03:17:50 +0000418}
419
420bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
John McCall4d9f1422011-02-15 22:21:29 +0000421 uint64_t fieldOffset) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000422 // If the field is packed, then we need a packed struct.
Anders Carlsson68e0b682009-08-08 18:23:56 +0000423 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson307846f2009-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 Carlssond78fc892009-07-23 17:24:40 +0000429 if (!Packed && !D->getDeclName())
Anders Carlsson307846f2009-07-23 03:17:50 +0000430 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000431
John McCall4d9f1422011-02-15 22:21:29 +0000432 LayoutBitField(D, fieldOffset);
Anders Carlsson307846f2009-07-23 03:17:50 +0000433 return true;
434 }
Mike Stump11289f42009-09-09 15:08:12 +0000435
John McCall614dbdc2010-08-22 21:01:12 +0000436 CheckZeroInitializable(D->getType());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000437
John McCall8a3c5552011-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 Stump11289f42009-09-09 15:08:12 +0000442
Anders Carlsson19702bb2009-08-04 16:29:15 +0000443 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
John McCall4d9f1422011-02-15 22:21:29 +0000444 CharUnits typeAlignment = getTypeAlignment(Ty);
Anders Carlsson19702bb2009-08-04 16:29:15 +0000445
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000446 // If the type alignment is larger then the struct alignment, we must use
447 // a packed struct.
John McCall4d9f1422011-02-15 22:21:29 +0000448 if (typeAlignment > Alignment) {
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000449 assert(!Packed && "Alignment is wrong even with packed struct!");
450 return false;
451 }
Mike Stump11289f42009-09-09 15:08:12 +0000452
John McCall4d9f1422011-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 McCall8a3c5552011-02-26 08:41:59 +0000458 if (MFAA->getAlignment() != Types.getContext().toBits(typeAlignment))
John McCall4d9f1422011-02-15 22:21:29 +0000459 return false;
460 }
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000461 }
462 }
463
Anders Carlsson19702bb2009-08-04 16:29:15 +0000464 // Round up the field offset to the alignment of the field type.
John McCall4d9f1422011-02-15 22:21:29 +0000465 CharUnits alignedNextFieldOffsetInBytes =
466 NextFieldOffset.RoundUpToAlignment(typeAlignment);
Anders Carlsson19702bb2009-08-04 16:29:15 +0000467
John McCall4d9f1422011-02-15 22:21:29 +0000468 if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
Anders Carlssonfcaaa692011-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 Carlsson19702bb2009-08-04 16:29:15 +0000477 assert(!Packed && "Could not place field even with packed struct!");
478 return false;
479 }
Mike Stump11289f42009-09-09 15:08:12 +0000480
John McCall4d9f1422011-02-15 22:21:29 +0000481 AppendPadding(fieldOffsetInBytes, typeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +0000482
Anders Carlsson307846f2009-07-23 03:17:50 +0000483 // Now append the field.
John McCall0217dfc22011-02-15 06:40:56 +0000484 Fields[D] = FieldTypes.size();
John McCall4d9f1422011-02-15 22:21:29 +0000485 AppendField(fieldOffsetInBytes, Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000486
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000487 LastLaidOutBase.invalidate();
Anders Carlsson307846f2009-07-23 03:17:50 +0000488 return true;
489}
490
Anders Carlsson1de2f572010-04-17 20:49:27 +0000491const llvm::Type *
492CGRecordLayoutBuilder::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
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000502 const llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
Ken Dyck7a0b19f2011-04-24 16:47:33 +0000503 CharUnits NumBytesToAppend = Types.getContext().toCharUnitsFromBits(
504 llvm::RoundUpToAlignment(FieldSize,
505 Types.getContext().Target.getCharAlign()));
Anders Carlsson2295f132010-04-17 21:04:52 +0000506
Ken Dyck7a0b19f2011-04-24 16:47:33 +0000507 if (NumBytesToAppend > CharUnits::One())
508 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend.getQuantity());
Anders Carlsson2295f132010-04-17 21:04:52 +0000509
Anders Carlsson1de2f572010-04-17 20:49:27 +0000510 // Add the bit field info.
John McCall0217dfc22011-02-15 06:40:56 +0000511 BitFields.insert(std::make_pair(Field,
512 CGBitFieldInfo::MakeInfo(Types, Field, 0, FieldSize)));
Anders Carlsson2295f132010-04-17 21:04:52 +0000513 return FieldTy;
Anders Carlsson1de2f572010-04-17 20:49:27 +0000514 }
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000515
Anders Carlsson1de2f572010-04-17 20:49:27 +0000516 // This is a regular union field.
John McCall0217dfc22011-02-15 06:40:56 +0000517 Fields[Field] = 0;
Anders Carlsson1de2f572010-04-17 20:49:27 +0000518 return Types.ConvertTypeForMemRecursive(Field->getType());
519}
520
Anders Carlsson697f6592009-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 Stump11289f42009-09-09 15:08:12 +0000523
John McCall4d9f1422011-02-15 22:21:29 +0000524 const ASTRecordLayout &layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000525
John McCall4d9f1422011-02-15 22:21:29 +0000526 const llvm::Type *unionType = 0;
527 CharUnits unionSize = CharUnits::Zero();
528 CharUnits unionAlign = CharUnits::Zero();
Mike Stump11289f42009-09-09 15:08:12 +0000529
John McCall4d9f1422011-02-15 22:21:29 +0000530 bool hasOnlyZeroSizedBitFields = true;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000531
John McCall4d9f1422011-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 Carlsson697f6592009-07-23 03:43:54 +0000536 "Union field offset did not start at the beginning of record!");
John McCall4d9f1422011-02-15 22:21:29 +0000537 const llvm::Type *fieldType = LayoutUnionField(*field, layout);
Anders Carlssonf814ee62009-07-23 04:00:39 +0000538
John McCall4d9f1422011-02-15 22:21:29 +0000539 if (!fieldType)
Anders Carlsson1de2f572010-04-17 20:49:27 +0000540 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000541
John McCall4d9f1422011-02-15 22:21:29 +0000542 hasOnlyZeroSizedBitFields = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000543
John McCall4d9f1422011-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 Stump11289f42009-09-09 15:08:12 +0000548
John McCall4d9f1422011-02-15 22:21:29 +0000549 if (fieldAlign < unionAlign)
Anders Carlsson697f6592009-07-23 03:43:54 +0000550 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000551
John McCall4d9f1422011-02-15 22:21:29 +0000552 if (fieldAlign > unionAlign || fieldSize > unionSize) {
553 unionType = fieldType;
554 unionAlign = fieldAlign;
555 unionSize = fieldSize;
Anders Carlsson697f6592009-07-23 03:43:54 +0000556 }
557 }
Mike Stump11289f42009-09-09 15:08:12 +0000558
Anders Carlsson697f6592009-07-23 03:43:54 +0000559 // Now add our field.
John McCall4d9f1422011-02-15 22:21:29 +0000560 if (unionType) {
561 AppendField(CharUnits::Zero(), unionType);
Anders Carlsson0e912752009-09-03 22:56:02 +0000562
John McCall4d9f1422011-02-15 22:21:29 +0000563 if (getTypeAlignment(unionType) > layout.getAlignment()) {
Anders Carlsson0e912752009-09-03 22:56:02 +0000564 // We need a packed struct.
565 Packed = true;
John McCall4d9f1422011-02-15 22:21:29 +0000566 unionAlign = CharUnits::One();
Anders Carlsson0e912752009-09-03 22:56:02 +0000567 }
568 }
John McCall4d9f1422011-02-15 22:21:29 +0000569 if (unionAlign.isZero()) {
570 assert(hasOnlyZeroSizedBitFields &&
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000571 "0-align record did not have all zero-sized bit-fields!");
John McCall4d9f1422011-02-15 22:21:29 +0000572 unionAlign = CharUnits::One();
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000573 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000574
Anders Carlsson697f6592009-07-23 03:43:54 +0000575 // Append tail padding.
John McCall4d9f1422011-02-15 22:21:29 +0000576 CharUnits recordSize = layout.getSize();
577 if (recordSize > unionSize)
578 AppendPadding(recordSize, unionAlign);
Anders Carlsson697f6592009-07-23 03:43:54 +0000579}
580
John McCall0217dfc22011-02-15 06:40:56 +0000581void CGRecordLayoutBuilder::LayoutBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000582 const CGRecordLayout &baseLayout,
583 CharUnits baseOffset) {
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000584 ResizeLastBaseFieldIfNecessary(baseOffset);
585
John McCall4d9f1422011-02-15 22:21:29 +0000586 AppendPadding(baseOffset, CharUnits::One());
Anders Carlssona518b2a2010-12-04 23:59:48 +0000587
John McCall0217dfc22011-02-15 06:40:56 +0000588 const ASTRecordLayout &baseASTLayout
589 = Types.getContext().getASTRecordLayout(base);
590
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000591 LastLaidOutBase.Offset = NextFieldOffset;
592 LastLaidOutBase.NonVirtualSize = baseASTLayout.getNonVirtualSize();
593
John McCall4d9f1422011-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
John McCall4d9f1422011-02-15 22:21:29 +0000602 const llvm::StructType *subobjectType = baseLayout.getBaseSubobjectLLVMType();
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000603 AppendField(baseOffset, subobjectType);
John McCall4d9f1422011-02-15 22:21:29 +0000604
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000605 Types.addBaseSubobjectTypeName(base, baseLayout);
John McCall0217dfc22011-02-15 06:40:56 +0000606}
607
608void CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000609 CharUnits baseOffset) {
John McCall0217dfc22011-02-15 06:40:56 +0000610 // Ignore empty bases.
611 if (base->isEmpty()) return;
612
613 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
614 if (IsZeroInitializableAsBase) {
615 assert(IsZeroInitializable &&
616 "class zero-initializable as base but not as complete object");
617
618 IsZeroInitializable = IsZeroInitializableAsBase =
619 baseLayout.isZeroInitializableAsBase();
620 }
621
John McCall4d9f1422011-02-15 22:21:29 +0000622 LayoutBase(base, baseLayout, baseOffset);
John McCall0217dfc22011-02-15 06:40:56 +0000623 NonVirtualBases[base] = (FieldTypes.size() - 1);
Anders Carlssona518b2a2010-12-04 23:59:48 +0000624}
625
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000626void
John McCall0217dfc22011-02-15 06:40:56 +0000627CGRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000628 CharUnits baseOffset) {
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000629 // Ignore empty bases.
John McCall0217dfc22011-02-15 06:40:56 +0000630 if (base->isEmpty()) return;
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000631
John McCall0217dfc22011-02-15 06:40:56 +0000632 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
633 if (IsZeroInitializable)
634 IsZeroInitializable = baseLayout.isZeroInitializableAsBase();
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000635
John McCall4d9f1422011-02-15 22:21:29 +0000636 LayoutBase(base, baseLayout, baseOffset);
John McCall0217dfc22011-02-15 06:40:56 +0000637 VirtualBases[base] = (FieldTypes.size() - 1);
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000638}
639
Anders Carlssona459adb2010-11-28 19:18:44 +0000640/// LayoutVirtualBases - layout the non-virtual bases of a record decl.
641void
642CGRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
643 const ASTRecordLayout &Layout) {
644 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
645 E = RD->bases_end(); I != E; ++I) {
646 const CXXRecordDecl *BaseDecl =
647 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
648
649 // We only want to lay out virtual bases that aren't indirect primary bases
650 // of some other base.
651 if (I->isVirtual() && !IndirectPrimaryBases.count(BaseDecl)) {
652 // Only lay out the base once.
653 if (!LaidOutVirtualBases.insert(BaseDecl))
654 continue;
655
John McCall4d9f1422011-02-15 22:21:29 +0000656 CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl);
657 LayoutVirtualBase(BaseDecl, vbaseOffset);
Anders Carlssona459adb2010-11-28 19:18:44 +0000658 }
659
660 if (!BaseDecl->getNumVBases()) {
661 // This base isn't interesting since it doesn't have any virtual bases.
662 continue;
663 }
664
665 LayoutVirtualBases(BaseDecl, Layout);
666 }
667}
668
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000669void
670CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD,
671 const ASTRecordLayout &Layout) {
672 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
673
674 // Check if we need to add a vtable pointer.
675 if (RD->isDynamicClass()) {
676 if (!PrimaryBase) {
677 const llvm::Type *FunctionType =
678 llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()),
679 /*isVarArg=*/true);
680 const llvm::Type *VTableTy = FunctionType->getPointerTo();
681
John McCall4d9f1422011-02-15 22:21:29 +0000682 assert(NextFieldOffset.isZero() &&
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000683 "VTable pointer must come first!");
John McCall4d9f1422011-02-15 22:21:29 +0000684 AppendField(CharUnits::Zero(), VTableTy->getPointerTo());
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000685 } else {
Anders Carlsson7f95cd12010-11-24 23:12:57 +0000686 if (!Layout.isPrimaryBaseVirtual())
John McCall4d9f1422011-02-15 22:21:29 +0000687 LayoutNonVirtualBase(PrimaryBase, CharUnits::Zero());
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000688 else
John McCall4d9f1422011-02-15 22:21:29 +0000689 LayoutVirtualBase(PrimaryBase, CharUnits::Zero());
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000690 }
691 }
692
693 // Layout the non-virtual bases.
694 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
695 E = RD->bases_end(); I != E; ++I) {
696 if (I->isVirtual())
697 continue;
698
699 const CXXRecordDecl *BaseDecl =
700 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
701
702 // We've already laid out the primary base.
Anders Carlsson7f95cd12010-11-24 23:12:57 +0000703 if (BaseDecl == PrimaryBase && !Layout.isPrimaryBaseVirtual())
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000704 continue;
705
John McCall4d9f1422011-02-15 22:21:29 +0000706 LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffset(BaseDecl));
Anders Carlssond681a292009-12-16 17:27:20 +0000707 }
708}
709
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000710bool
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000711CGRecordLayoutBuilder::ComputeNonVirtualBaseType(const CXXRecordDecl *RD) {
712 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(RD);
713
Ken Dyckbec02852011-02-08 02:02:47 +0000714 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
715 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
John McCall4d9f1422011-02-15 22:21:29 +0000716 CharUnits AlignedNonVirtualTypeSize =
717 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000718
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000719 // First check if we can use the same fields as for the complete class.
John McCall4d9f1422011-02-15 22:21:29 +0000720 CharUnits RecordSize = Layout.getSize();
John McCall0217dfc22011-02-15 06:40:56 +0000721 if (AlignedNonVirtualTypeSize == RecordSize)
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000722 return true;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000723
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000724 // Check if we need padding.
John McCall4d9f1422011-02-15 22:21:29 +0000725 CharUnits AlignedNextFieldOffset =
726 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000727
John McCall0217dfc22011-02-15 06:40:56 +0000728 if (AlignedNextFieldOffset > AlignedNonVirtualTypeSize) {
729 assert(!Packed && "cannot layout even as packed struct");
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000730 return false; // Needs packing.
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000731 }
732
John McCall0217dfc22011-02-15 06:40:56 +0000733 bool needsPadding = (AlignedNonVirtualTypeSize != AlignedNextFieldOffset);
734 if (needsPadding) {
John McCall4d9f1422011-02-15 22:21:29 +0000735 CharUnits NumBytes = AlignedNonVirtualTypeSize - AlignedNextFieldOffset;
John McCall0217dfc22011-02-15 06:40:56 +0000736 FieldTypes.push_back(getByteArrayType(NumBytes));
737 }
738
739 BaseSubobjectType = llvm::StructType::get(Types.getLLVMContext(),
740 FieldTypes, Packed);
741
742 if (needsPadding) {
743 // Pull the padding back off.
744 FieldTypes.pop_back();
745 }
746
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000747 return true;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000748}
749
Anders Carlsson307846f2009-07-23 03:17:50 +0000750bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
751 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
John McCall4d9f1422011-02-15 22:21:29 +0000752 assert(!Alignment.isZero() && "Did not set alignment!");
Mike Stump11289f42009-09-09 15:08:12 +0000753
Anders Carlsson697f6592009-07-23 03:43:54 +0000754 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000755
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000756 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
757 if (RD)
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000758 LayoutNonVirtualBases(RD, Layout);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000759
Anders Carlsson307846f2009-07-23 03:17:50 +0000760 unsigned FieldNo = 0;
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000761 const FieldDecl *LastFD = 0;
762
Mike Stump11289f42009-09-09 15:08:12 +0000763 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson307846f2009-07-23 03:17:50 +0000764 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000765 if (IsMsStruct) {
766 // Zero-length bitfields following non-bitfield members are
767 // ignored:
768 const FieldDecl *FD = (*Field);
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +0000769 if (Types.getContext().ZeroBitfieldFollowsNonBitfield(FD, LastFD)) {
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000770 --FieldNo;
771 continue;
772 }
773 LastFD = FD;
774 }
775
Anders Carlsson307846f2009-07-23 03:17:50 +0000776 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump11289f42009-09-09 15:08:12 +0000777 assert(!Packed &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000778 "Could not layout fields even with a packed LLVM struct!");
779 return false;
780 }
781 }
782
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000783 if (RD) {
Anders Carlssona459adb2010-11-28 19:18:44 +0000784 // We've laid out the non-virtual bases and the fields, now compute the
785 // non-virtual base field types.
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000786 if (!ComputeNonVirtualBaseType(RD)) {
787 assert(!Packed && "Could not layout even with a packed LLVM struct!");
788 return false;
789 }
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000790
Anders Carlssona459adb2010-11-28 19:18:44 +0000791 // And lay out the virtual bases.
792 RD->getIndirectPrimaryBases(IndirectPrimaryBases);
793 if (Layout.isPrimaryBaseVirtual())
794 IndirectPrimaryBases.insert(Layout.getPrimaryBase());
795 LayoutVirtualBases(RD, Layout);
796 }
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000797
Anders Carlsson307846f2009-07-23 03:17:50 +0000798 // Append tail padding if necessary.
Ken Dyck272b6fa2011-04-24 16:53:44 +0000799 AppendTailPadding(Layout.getSize());
Mike Stump11289f42009-09-09 15:08:12 +0000800
Anders Carlsson307846f2009-07-23 03:17:50 +0000801 return true;
802}
803
Ken Dyck272b6fa2011-04-24 16:53:44 +0000804void CGRecordLayoutBuilder::AppendTailPadding(CharUnits RecordSize) {
805 ResizeLastBaseFieldIfNecessary(RecordSize);
Mike Stump11289f42009-09-09 15:08:12 +0000806
Ken Dyck272b6fa2011-04-24 16:53:44 +0000807 assert(NextFieldOffset <= RecordSize && "Size mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000808
John McCall4d9f1422011-02-15 22:21:29 +0000809 CharUnits AlignedNextFieldOffset =
810 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
Anders Carlsson220bf4f2009-12-08 01:24:23 +0000811
Ken Dyck272b6fa2011-04-24 16:53:44 +0000812 if (AlignedNextFieldOffset == RecordSize) {
Anders Carlsson220bf4f2009-12-08 01:24:23 +0000813 // We don't need any padding.
814 return;
815 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000816
Ken Dyck272b6fa2011-04-24 16:53:44 +0000817 CharUnits NumPadBytes = RecordSize - NextFieldOffset;
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000818 AppendBytes(NumPadBytes);
819}
820
John McCall4d9f1422011-02-15 22:21:29 +0000821void CGRecordLayoutBuilder::AppendField(CharUnits fieldOffset,
822 const llvm::Type *fieldType) {
823 CharUnits fieldSize =
824 CharUnits::fromQuantity(Types.getTargetData().getTypeAllocSize(fieldType));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000825
John McCall4d9f1422011-02-15 22:21:29 +0000826 FieldTypes.push_back(fieldType);
Anders Carlsson307846f2009-07-23 03:17:50 +0000827
John McCall4d9f1422011-02-15 22:21:29 +0000828 NextFieldOffset = fieldOffset + fieldSize;
Anders Carlsson307846f2009-07-23 03:17:50 +0000829 BitsAvailableInLastField = 0;
830}
831
John McCall4d9f1422011-02-15 22:21:29 +0000832void CGRecordLayoutBuilder::AppendPadding(CharUnits fieldOffset,
833 CharUnits fieldAlignment) {
834 assert(NextFieldOffset <= fieldOffset &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000835 "Incorrect field layout!");
Mike Stump11289f42009-09-09 15:08:12 +0000836
Anders Carlsson307846f2009-07-23 03:17:50 +0000837 // Round up the field offset to the alignment of the field type.
John McCall4d9f1422011-02-15 22:21:29 +0000838 CharUnits alignedNextFieldOffset =
839 NextFieldOffset.RoundUpToAlignment(fieldAlignment);
Anders Carlsson307846f2009-07-23 03:17:50 +0000840
John McCall4d9f1422011-02-15 22:21:29 +0000841 if (alignedNextFieldOffset < fieldOffset) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000842 // Even with alignment, the field offset is not at the right place,
843 // insert padding.
John McCall4d9f1422011-02-15 22:21:29 +0000844 CharUnits padding = fieldOffset - NextFieldOffset;
Anders Carlsson307846f2009-07-23 03:17:50 +0000845
John McCall4d9f1422011-02-15 22:21:29 +0000846 AppendBytes(padding);
Anders Carlsson307846f2009-07-23 03:17:50 +0000847 }
848}
849
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000850bool CGRecordLayoutBuilder::ResizeLastBaseFieldIfNecessary(CharUnits offset) {
851 // Check if we have a base to resize.
852 if (!LastLaidOutBase.isValid())
853 return false;
854
855 // This offset does not overlap with the tail padding.
856 if (offset >= NextFieldOffset)
857 return false;
858
859 // Restore the field offset and append an i8 array instead.
860 FieldTypes.pop_back();
861 NextFieldOffset = LastLaidOutBase.Offset;
862 AppendBytes(LastLaidOutBase.NonVirtualSize);
863 LastLaidOutBase.invalidate();
864
865 return true;
866}
867
John McCall4d9f1422011-02-15 22:21:29 +0000868const llvm::Type *CGRecordLayoutBuilder::getByteArrayType(CharUnits numBytes) {
869 assert(!numBytes.isZero() && "Empty byte arrays aren't allowed.");
Mike Stump11289f42009-09-09 15:08:12 +0000870
Owen Anderson41a75022009-08-13 21:57:51 +0000871 const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
John McCall4d9f1422011-02-15 22:21:29 +0000872 if (numBytes > CharUnits::One())
873 Ty = llvm::ArrayType::get(Ty, numBytes.getQuantity());
Mike Stump11289f42009-09-09 15:08:12 +0000874
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000875 return Ty;
876}
877
John McCall4d9f1422011-02-15 22:21:29 +0000878void CGRecordLayoutBuilder::AppendBytes(CharUnits numBytes) {
879 if (numBytes.isZero())
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000880 return;
881
Anders Carlsson307846f2009-07-23 03:17:50 +0000882 // Append the padding field
John McCall4d9f1422011-02-15 22:21:29 +0000883 AppendField(NextFieldOffset, getByteArrayType(numBytes));
Anders Carlsson307846f2009-07-23 03:17:50 +0000884}
885
John McCall4d9f1422011-02-15 22:21:29 +0000886CharUnits CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlssond78fc892009-07-23 17:24:40 +0000887 if (Packed)
John McCall4d9f1422011-02-15 22:21:29 +0000888 return CharUnits::One();
Mike Stump11289f42009-09-09 15:08:12 +0000889
John McCall4d9f1422011-02-15 22:21:29 +0000890 return CharUnits::fromQuantity(Types.getTargetData().getABITypeAlignment(Ty));
Anders Carlsson307846f2009-07-23 03:17:50 +0000891}
892
John McCall4d9f1422011-02-15 22:21:29 +0000893CharUnits CGRecordLayoutBuilder::getAlignmentAsLLVMStruct() const {
Anders Carlssonacf877b2010-11-28 23:06:23 +0000894 if (Packed)
John McCall4d9f1422011-02-15 22:21:29 +0000895 return CharUnits::One();
Anders Carlssonacf877b2010-11-28 23:06:23 +0000896
John McCall4d9f1422011-02-15 22:21:29 +0000897 CharUnits maxAlignment = CharUnits::One();
Anders Carlssonacf877b2010-11-28 23:06:23 +0000898 for (size_t i = 0; i != FieldTypes.size(); ++i)
John McCall4d9f1422011-02-15 22:21:29 +0000899 maxAlignment = std::max(maxAlignment, getTypeAlignment(FieldTypes[i]));
Anders Carlssonacf877b2010-11-28 23:06:23 +0000900
John McCall4d9f1422011-02-15 22:21:29 +0000901 return maxAlignment;
Anders Carlssonacf877b2010-11-28 23:06:23 +0000902}
903
John McCall0217dfc22011-02-15 06:40:56 +0000904/// Merge in whether a field of the given type is zero-initializable.
John McCall614dbdc2010-08-22 21:01:12 +0000905void CGRecordLayoutBuilder::CheckZeroInitializable(QualType T) {
Anders Carlssond606de72009-08-23 01:25:01 +0000906 // This record already contains a member pointer.
John McCall0217dfc22011-02-15 06:40:56 +0000907 if (!IsZeroInitializableAsBase)
Anders Carlssond606de72009-08-23 01:25:01 +0000908 return;
Mike Stump11289f42009-09-09 15:08:12 +0000909
Anders Carlssond606de72009-08-23 01:25:01 +0000910 // Can only have member pointers if we're compiling C++.
911 if (!Types.getContext().getLangOptions().CPlusPlus)
912 return;
Mike Stump11289f42009-09-09 15:08:12 +0000913
John McCall0217dfc22011-02-15 06:40:56 +0000914 const Type *elementType = T->getBaseElementTypeUnsafe();
Mike Stump11289f42009-09-09 15:08:12 +0000915
John McCall0217dfc22011-02-15 06:40:56 +0000916 if (const MemberPointerType *MPT = elementType->getAs<MemberPointerType>()) {
John McCall614dbdc2010-08-22 21:01:12 +0000917 if (!Types.getCXXABI().isZeroInitializable(MPT))
John McCall0217dfc22011-02-15 06:40:56 +0000918 IsZeroInitializable = IsZeroInitializableAsBase = false;
919 } else if (const RecordType *RT = elementType->getAs<RecordType>()) {
Anders Carlssone8bfe412010-02-02 05:17:25 +0000920 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
John McCall0217dfc22011-02-15 06:40:56 +0000921 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
922 if (!Layout.isZeroInitializable())
923 IsZeroInitializable = IsZeroInitializableAsBase = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000924 }
Anders Carlssond606de72009-08-23 01:25:01 +0000925}
926
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000927CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) {
928 CGRecordLayoutBuilder Builder(*this);
Mike Stump11289f42009-09-09 15:08:12 +0000929
Anders Carlsson307846f2009-07-23 03:17:50 +0000930 Builder.Layout(D);
Anders Carlssone1d5ca52009-07-24 15:20:52 +0000931
Anders Carlsson36e2fa82010-11-24 19:37:16 +0000932 const llvm::StructType *Ty = llvm::StructType::get(getLLVMContext(),
933 Builder.FieldTypes,
934 Builder.Packed);
Mike Stump11289f42009-09-09 15:08:12 +0000935
John McCall0217dfc22011-02-15 06:40:56 +0000936 // If we're in C++, compute the base subobject type.
Anders Carlsson36e2fa82010-11-24 19:37:16 +0000937 const llvm::StructType *BaseTy = 0;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000938 if (isa<CXXRecordDecl>(D)) {
John McCall0217dfc22011-02-15 06:40:56 +0000939 BaseTy = Builder.BaseSubobjectType;
940 if (!BaseTy) BaseTy = Ty;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000941 }
942
Daniel Dunbar034299e2010-03-31 01:09:11 +0000943 CGRecordLayout *RL =
John McCall0217dfc22011-02-15 06:40:56 +0000944 new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable,
945 Builder.IsZeroInitializableAsBase);
Daniel Dunbar034299e2010-03-31 01:09:11 +0000946
John McCall0217dfc22011-02-15 06:40:56 +0000947 RL->NonVirtualBases.swap(Builder.NonVirtualBases);
948 RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases);
Anders Carlsson061ca522010-05-18 05:22:06 +0000949
Anders Carlsson307846f2009-07-23 03:17:50 +0000950 // Add all the field numbers.
John McCall0217dfc22011-02-15 06:40:56 +0000951 RL->FieldInfo.swap(Builder.Fields);
Anders Carlsson307846f2009-07-23 03:17:50 +0000952
953 // Add bitfield info.
John McCall0217dfc22011-02-15 06:40:56 +0000954 RL->BitFields.swap(Builder.BitFields);
Mike Stump11289f42009-09-09 15:08:12 +0000955
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000956 // Dump the layout, if requested.
Daniel Dunbarb935b932010-04-13 20:58:55 +0000957 if (getContext().getLangOptions().DumpRecordLayouts) {
Daniel Dunbarccabe482010-04-19 20:44:53 +0000958 llvm::errs() << "\n*** Dumping IRgen Record Layout\n";
Daniel Dunbarb935b932010-04-13 20:58:55 +0000959 llvm::errs() << "Record: ";
960 D->dump();
961 llvm::errs() << "\nLayout: ";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000962 RL->dump();
Daniel Dunbarb935b932010-04-13 20:58:55 +0000963 }
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000964
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000965#ifndef NDEBUG
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000966 // Verify that the computed LLVM struct size matches the AST layout size.
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000967 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
968
Ken Dyckb0fcc592011-02-11 01:54:29 +0000969 uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize());
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000970 assert(TypeSizeInBits == getTargetData().getTypeAllocSizeInBits(Ty) &&
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000971 "Type size mismatch!");
972
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000973 if (BaseTy) {
Ken Dyckbec02852011-02-08 02:02:47 +0000974 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
975 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
976 CharUnits AlignedNonVirtualTypeSize =
977 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
978
979 uint64_t AlignedNonVirtualTypeSizeInBits =
Ken Dyckb0fcc592011-02-11 01:54:29 +0000980 getContext().toBits(AlignedNonVirtualTypeSize);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000981
982 assert(AlignedNonVirtualTypeSizeInBits ==
983 getTargetData().getTypeAllocSizeInBits(BaseTy) &&
984 "Type size mismatch!");
985 }
986
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000987 // Verify that the LLVM and AST field offsets agree.
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000988 const llvm::StructType *ST =
989 dyn_cast<llvm::StructType>(RL->getLLVMType());
990 const llvm::StructLayout *SL = getTargetData().getStructLayout(ST);
991
992 const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
993 RecordDecl::field_iterator it = D->field_begin();
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000994 const FieldDecl *LastFD = 0;
995 bool IsMsStruct = D->hasAttr<MsStructAttr>();
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000996 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
997 const FieldDecl *FD = *it;
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000998
999 // For non-bit-fields, just check that the LLVM struct offset matches the
1000 // AST offset.
1001 if (!FD->isBitField()) {
Daniel Dunbar2ba67442010-04-21 19:10:49 +00001002 unsigned FieldNo = RL->getLLVMFieldNo(FD);
1003 assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
1004 "Invalid field offset!");
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001005 LastFD = FD;
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001006 continue;
1007 }
1008
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001009 if (IsMsStruct) {
1010 // Zero-length bitfields following non-bitfield members are
1011 // ignored:
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001012 if (getContext().ZeroBitfieldFollowsNonBitfield(FD, LastFD)) {
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001013 --i;
1014 continue;
1015 }
1016 LastFD = FD;
1017 }
1018
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001019 // Ignore unnamed bit-fields.
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001020 if (!FD->getDeclName()) {
1021 LastFD = FD;
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001022 continue;
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001023 }
1024
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001025 const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
1026 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
1027 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
1028
1029 // Verify that every component access is within the structure.
1030 uint64_t FieldOffset = SL->getElementOffsetInBits(AI.FieldIndex);
John McCall8a3c5552011-02-26 08:41:59 +00001031 uint64_t AccessBitOffset = FieldOffset +
Ken Dyckf76759c2011-04-24 10:04:59 +00001032 getContext().toBits(AI.FieldByteOffset);
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001033 assert(AccessBitOffset + AI.AccessWidth <= TypeSizeInBits &&
1034 "Invalid bit-field access (out of range)!");
Daniel Dunbar2ba67442010-04-21 19:10:49 +00001035 }
1036 }
1037#endif
Daniel Dunbar2ea51832010-04-19 20:44:47 +00001038
Daniel Dunbar034299e2010-03-31 01:09:11 +00001039 return RL;
Anders Carlsson307846f2009-07-23 03:17:50 +00001040}
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001041
1042void CGRecordLayout::print(llvm::raw_ostream &OS) const {
1043 OS << "<CGRecordLayout\n";
John McCall0217dfc22011-02-15 06:40:56 +00001044 OS << " LLVMType:" << *CompleteObjectType << "\n";
1045 if (BaseSubobjectType)
1046 OS << " NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n";
John McCall614dbdc2010-08-22 21:01:12 +00001047 OS << " IsZeroInitializable:" << IsZeroInitializable << "\n";
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001048 OS << " BitFields:[\n";
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001049
1050 // Print bit-field infos in declaration order.
1051 std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001052 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
1053 it = BitFields.begin(), ie = BitFields.end();
1054 it != ie; ++it) {
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001055 const RecordDecl *RD = it->first->getParent();
1056 unsigned Index = 0;
1057 for (RecordDecl::field_iterator
1058 it2 = RD->field_begin(); *it2 != it->first; ++it2)
1059 ++Index;
1060 BFIs.push_back(std::make_pair(Index, &it->second));
1061 }
1062 llvm::array_pod_sort(BFIs.begin(), BFIs.end());
1063 for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
Daniel Dunbarb935b932010-04-13 20:58:55 +00001064 OS.indent(4);
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001065 BFIs[i].second->print(OS);
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001066 OS << "\n";
1067 }
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001068
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001069 OS << "]>\n";
1070}
1071
1072void CGRecordLayout::dump() const {
1073 print(llvm::errs());
1074}
1075
1076void CGBitFieldInfo::print(llvm::raw_ostream &OS) const {
1077 OS << "<CGBitFieldInfo";
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001078 OS << " Size:" << Size;
Daniel Dunbarb935b932010-04-13 20:58:55 +00001079 OS << " IsSigned:" << IsSigned << "\n";
1080
1081 OS.indent(4 + strlen("<CGBitFieldInfo"));
1082 OS << " NumComponents:" << getNumComponents();
1083 OS << " Components: [";
1084 if (getNumComponents()) {
1085 OS << "\n";
1086 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
1087 const AccessInfo &AI = getComponent(i);
1088 OS.indent(8);
1089 OS << "<AccessInfo"
1090 << " FieldIndex:" << AI.FieldIndex
Ken Dyckf76759c2011-04-24 10:04:59 +00001091 << " FieldByteOffset:" << AI.FieldByteOffset.getQuantity()
Daniel Dunbarb935b932010-04-13 20:58:55 +00001092 << " FieldBitStart:" << AI.FieldBitStart
1093 << " AccessWidth:" << AI.AccessWidth << "\n";
1094 OS.indent(8 + strlen("<AccessInfo"));
Ken Dyck27337a82011-04-24 10:13:17 +00001095 OS << " AccessAlignment:" << AI.AccessAlignment.getQuantity()
Daniel Dunbarb935b932010-04-13 20:58:55 +00001096 << " TargetBitOffset:" << AI.TargetBitOffset
1097 << " TargetBitWidth:" << AI.TargetBitWidth
1098 << ">\n";
1099 }
1100 OS.indent(4);
1101 }
1102 OS << "]>";
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001103}
1104
1105void CGBitFieldInfo::dump() const {
1106 print(llvm::errs());
1107}