blob: 0d72f854ba3e0e5e8387c50c014fa13f82047937 [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"
21#include "CodeGenTypes.h"
John McCall614dbdc2010-08-22 21:01:12 +000022#include "CGCXXABI.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000023#include "llvm/DerivedTypes.h"
Daniel Dunbarb97bff92010-04-12 18:14:18 +000024#include "llvm/Type.h"
Daniel Dunbar2ba67442010-04-21 19:10:49 +000025#include "llvm/Support/Debug.h"
Daniel Dunbarb97bff92010-04-12 18:14:18 +000026#include "llvm/Support/raw_ostream.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000027#include "llvm/Target/TargetData.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000028using namespace clang;
29using namespace CodeGen;
30
John McCallbcd38212010-11-30 23:17:27 +000031namespace {
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000032
33class CGRecordLayoutBuilder {
34public:
35 /// FieldTypes - Holds the LLVM types that the struct is created from.
John McCall0217dfc22011-02-15 06:40:56 +000036 ///
Anders Carlssonb6d31e72011-04-17 21:32:41 +000037 llvm::SmallVector<const llvm::Type *, 16> FieldTypes;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000038
John McCall0217dfc22011-02-15 06:40:56 +000039 /// BaseSubobjectType - Holds the LLVM type for the non-virtual part
Anders Carlssonc1351ca2010-11-09 05:25:47 +000040 /// of the struct. For example, consider:
41 ///
42 /// struct A { int i; };
43 /// struct B { void *v; };
44 /// struct C : virtual A, B { };
45 ///
46 /// The LLVM type of C will be
47 /// %struct.C = type { i32 (...)**, %struct.A, i32, %struct.B }
48 ///
49 /// And the LLVM type of the non-virtual base struct will be
50 /// %struct.C.base = type { i32 (...)**, %struct.A, i32 }
John McCall0217dfc22011-02-15 06:40:56 +000051 ///
52 /// This only gets initialized if the base subobject type is
53 /// different from the complete-object type.
54 const llvm::StructType *BaseSubobjectType;
Anders Carlssonc1351ca2010-11-09 05:25:47 +000055
John McCall0217dfc22011-02-15 06:40:56 +000056 /// FieldInfo - Holds a field and its corresponding LLVM field number.
57 llvm::DenseMap<const FieldDecl *, unsigned> Fields;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000058
John McCall0217dfc22011-02-15 06:40:56 +000059 /// BitFieldInfo - Holds location and size information about a bit field.
60 llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000061
John McCall0217dfc22011-02-15 06:40:56 +000062 llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases;
63 llvm::DenseMap<const CXXRecordDecl *, unsigned> VirtualBases;
Anders Carlsson4131f002010-11-24 22:50:27 +000064
65 /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are
66 /// primary base classes for some other direct or indirect base class.
67 CXXIndirectPrimaryBaseSet IndirectPrimaryBases;
68
Anders Carlssona459adb2010-11-28 19:18:44 +000069 /// LaidOutVirtualBases - A set of all laid out virtual bases, used to avoid
70 /// avoid laying out virtual bases more than once.
71 llvm::SmallPtrSet<const CXXRecordDecl *, 4> LaidOutVirtualBases;
72
John McCall614dbdc2010-08-22 21:01:12 +000073 /// IsZeroInitializable - Whether this struct can be C++
74 /// zero-initialized with an LLVM zeroinitializer.
75 bool IsZeroInitializable;
John McCall0217dfc22011-02-15 06:40:56 +000076 bool IsZeroInitializableAsBase;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000077
78 /// Packed - Whether the resulting LLVM struct will be packed or not.
79 bool Packed;
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +000080
81 /// IsMsStruct - Whether ms_struct is in effect or not
82 bool IsMsStruct;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000083
84private:
85 CodeGenTypes &Types;
86
Anders Carlssonfcaaa692011-04-17 21:56:13 +000087 /// LastLaidOutBaseInfo - Contains the offset and non-virtual size of the
88 /// last base laid out. Used so that we can replace the last laid out base
89 /// type with an i8 array if needed.
90 struct LastLaidOutBaseInfo {
91 CharUnits Offset;
92 CharUnits NonVirtualSize;
93
94 bool isValid() const { return !NonVirtualSize.isZero(); }
95 void invalidate() { NonVirtualSize = CharUnits::Zero(); }
96
97 } LastLaidOutBase;
98
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000099 /// Alignment - Contains the alignment of the RecordDecl.
John McCall4d9f1422011-02-15 22:21:29 +0000100 CharUnits Alignment;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000101
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000102 /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field,
103 /// this will have the number of bits still available in the field.
104 char BitsAvailableInLastField;
105
John McCall4d9f1422011-02-15 22:21:29 +0000106 /// NextFieldOffset - Holds the next field offset.
107 CharUnits NextFieldOffset;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000108
Anders Carlsson1de2f572010-04-17 20:49:27 +0000109 /// LayoutUnionField - Will layout a field in an union and return the type
110 /// that the field will have.
111 const llvm::Type *LayoutUnionField(const FieldDecl *Field,
112 const ASTRecordLayout &Layout);
113
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000114 /// LayoutUnion - Will layout a union RecordDecl.
115 void LayoutUnion(const RecordDecl *D);
116
117 /// LayoutField - try to layout all fields in the record decl.
118 /// Returns false if the operation failed because the struct is not packed.
119 bool LayoutFields(const RecordDecl *D);
120
Anders Carlssona518b2a2010-12-04 23:59:48 +0000121 /// Layout a single base, virtual or non-virtual
John McCall4d9f1422011-02-15 22:21:29 +0000122 void LayoutBase(const CXXRecordDecl *base,
123 const CGRecordLayout &baseLayout,
124 CharUnits baseOffset);
Anders Carlssona518b2a2010-12-04 23:59:48 +0000125
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000126 /// LayoutVirtualBase - layout a single virtual base.
John McCall4d9f1422011-02-15 22:21:29 +0000127 void LayoutVirtualBase(const CXXRecordDecl *base,
128 CharUnits baseOffset);
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000129
Anders Carlssona459adb2010-11-28 19:18:44 +0000130 /// LayoutVirtualBases - layout the virtual bases of a record decl.
131 void LayoutVirtualBases(const CXXRecordDecl *RD,
132 const ASTRecordLayout &Layout);
133
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000134 /// LayoutNonVirtualBase - layout a single non-virtual base.
John McCall4d9f1422011-02-15 22:21:29 +0000135 void LayoutNonVirtualBase(const CXXRecordDecl *base,
136 CharUnits baseOffset);
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000137
Anders Carlssona459adb2010-11-28 19:18:44 +0000138 /// LayoutNonVirtualBases - layout the virtual bases of a record decl.
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000139 void LayoutNonVirtualBases(const CXXRecordDecl *RD,
140 const ASTRecordLayout &Layout);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000141
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000142 /// ComputeNonVirtualBaseType - Compute the non-virtual base field types.
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000143 bool ComputeNonVirtualBaseType(const CXXRecordDecl *RD);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000144
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000145 /// LayoutField - layout a single field. Returns false if the operation failed
146 /// because the current struct is not packed.
147 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
148
149 /// LayoutBitField - layout a single bit field.
150 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
151
152 /// AppendField - Appends a field with the given offset and type.
John McCall4d9f1422011-02-15 22:21:29 +0000153 void AppendField(CharUnits fieldOffset, const llvm::Type *FieldTy);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000154
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000155 /// AppendPadding - Appends enough padding bytes so that the total
156 /// struct size is a multiple of the field alignment.
John McCall4d9f1422011-02-15 22:21:29 +0000157 void AppendPadding(CharUnits fieldOffset, CharUnits fieldAlignment);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000158
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000159 /// ResizeLastBaseFieldIfNecessary - Fields and bases can be laid out in the
160 /// tail padding of a previous base. If this happens, the type of the previous
161 /// base needs to be changed to an array of i8. Returns true if the last
162 /// laid out base was resized.
163 bool ResizeLastBaseFieldIfNecessary(CharUnits offset);
164
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000165 /// getByteArrayType - Returns a byte array type with the given number of
166 /// elements.
John McCall4d9f1422011-02-15 22:21:29 +0000167 const llvm::Type *getByteArrayType(CharUnits NumBytes);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000168
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000169 /// AppendBytes - Append a given number of bytes to the record.
John McCall4d9f1422011-02-15 22:21:29 +0000170 void AppendBytes(CharUnits numBytes);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000171
172 /// AppendTailPadding - Append enough tail padding so that the type will have
173 /// the passed size.
Ken Dyck272b6fa2011-04-24 16:53:44 +0000174 void AppendTailPadding(CharUnits RecordSize);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000175
John McCall4d9f1422011-02-15 22:21:29 +0000176 CharUnits getTypeAlignment(const llvm::Type *Ty) const;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000177
Anders Carlssonacf877b2010-11-28 23:06:23 +0000178 /// getAlignmentAsLLVMStruct - Returns the maximum alignment of all the
179 /// LLVM element types.
John McCall4d9f1422011-02-15 22:21:29 +0000180 CharUnits getAlignmentAsLLVMStruct() const;
Anders Carlssonacf877b2010-11-28 23:06:23 +0000181
John McCall614dbdc2010-08-22 21:01:12 +0000182 /// CheckZeroInitializable - Check if the given type contains a pointer
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000183 /// to data member.
John McCall614dbdc2010-08-22 21:01:12 +0000184 void CheckZeroInitializable(QualType T);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000185
186public:
187 CGRecordLayoutBuilder(CodeGenTypes &Types)
John McCall0217dfc22011-02-15 06:40:56 +0000188 : BaseSubobjectType(0),
189 IsZeroInitializable(true), IsZeroInitializableAsBase(true),
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000190 Packed(false), IsMsStruct(false),
191 Types(Types), BitsAvailableInLastField(0) { }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000192
193 /// Layout - Will layout a RecordDecl.
194 void Layout(const RecordDecl *D);
195};
196
197}
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000198
Anders Carlsson307846f2009-07-23 03:17:50 +0000199void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
John McCall4d9f1422011-02-15 22:21:29 +0000200 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment();
Anders Carlsson09a37742009-09-02 17:51:33 +0000201 Packed = D->hasAttr<PackedAttr>();
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000202
203 IsMsStruct = D->hasAttr<MsStructAttr>();
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000204
Anders Carlsson697f6592009-07-23 03:43:54 +0000205 if (D->isUnion()) {
206 LayoutUnion(D);
207 return;
208 }
Anders Carlsson68e0b682009-08-08 18:23:56 +0000209
Anders Carlsson307846f2009-07-23 03:17:50 +0000210 if (LayoutFields(D))
211 return;
Mike Stump11289f42009-09-09 15:08:12 +0000212
Anders Carlsson307846f2009-07-23 03:17:50 +0000213 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlssond78fc892009-07-23 17:24:40 +0000214 Packed = true;
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000215 LastLaidOutBase.invalidate();
John McCall4d9f1422011-02-15 22:21:29 +0000216 NextFieldOffset = CharUnits::Zero();
Anders Carlsson307846f2009-07-23 03:17:50 +0000217 FieldTypes.clear();
John McCall0217dfc22011-02-15 06:40:56 +0000218 Fields.clear();
219 BitFields.clear();
220 NonVirtualBases.clear();
221 VirtualBases.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000222
Anders Carlsson307846f2009-07-23 03:17:50 +0000223 LayoutFields(D);
224}
225
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000226CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
227 const FieldDecl *FD,
228 uint64_t FieldOffset,
229 uint64_t FieldSize,
230 uint64_t ContainingTypeSizeInBits,
231 unsigned ContainingTypeAlign) {
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000232 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(FD->getType());
John McCall8a3c5552011-02-26 08:41:59 +0000233 CharUnits TypeSizeInBytes =
234 CharUnits::fromQuantity(Types.getTargetData().getTypeAllocSize(Ty));
235 uint64_t TypeSizeInBits = Types.getContext().toBits(TypeSizeInBytes);
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000236
Douglas Gregor6ab2fa82011-05-20 16:38:50 +0000237 bool IsSigned = FD->getType()->isSignedIntegerOrEnumerationType();
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000238
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000239 if (FieldSize > TypeSizeInBits) {
Anders Carlssond5f27b02010-04-17 22:54:57 +0000240 // We have a wide bit-field. The extra bits are only used for padding, so
241 // if we have a bitfield of type T, with size N:
242 //
243 // T t : N;
244 //
245 // We can just assume that it's:
246 //
247 // T t : sizeof(T);
248 //
249 FieldSize = TypeSizeInBits;
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000250 }
251
Chris Lattnerfb59c7c2011-02-17 22:09:58 +0000252 // in big-endian machines the first fields are in higher bit positions,
253 // so revert the offset. The byte offsets are reversed(back) later.
254 if (Types.getTargetData().isBigEndian()) {
255 FieldOffset = ((ContainingTypeSizeInBits)-FieldOffset-FieldSize);
256 }
257
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000258 // Compute the access components. The policy we use is to start by attempting
259 // to access using the width of the bit-field type itself and to always access
260 // at aligned indices of that type. If such an access would fail because it
261 // extends past the bound of the type, then we reduce size to the next smaller
262 // power of two and retry. The current algorithm assumes pow2 sized types,
263 // although this is easy to fix.
264 //
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000265 assert(llvm::isPowerOf2_32(TypeSizeInBits) && "Unexpected type size!");
266 CGBitFieldInfo::AccessInfo Components[3];
267 unsigned NumComponents = 0;
Nick Lewyckyd2348d82011-03-22 17:35:47 +0000268 unsigned AccessedTargetBits = 0; // The number of target bits accessed.
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000269 unsigned AccessWidth = TypeSizeInBits; // The current access width to attempt.
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000270
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000271 // Round down from the field offset to find the first access position that is
272 // at an aligned offset of the initial access type.
Daniel Dunbar59813772010-04-22 15:22:33 +0000273 uint64_t AccessStart = FieldOffset - (FieldOffset % AccessWidth);
274
275 // Adjust initial access size to fit within record.
John McCall8a3c5552011-02-26 08:41:59 +0000276 while (AccessWidth > Types.getTarget().getCharWidth() &&
Daniel Dunbar59813772010-04-22 15:22:33 +0000277 AccessStart + AccessWidth > ContainingTypeSizeInBits) {
278 AccessWidth >>= 1;
279 AccessStart = FieldOffset - (FieldOffset % AccessWidth);
280 }
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000281
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000282 while (AccessedTargetBits < FieldSize) {
283 // Check that we can access using a type of this size, without reading off
284 // the end of the structure. This can occur with packed structures and
285 // -fno-bitfield-type-align, for example.
286 if (AccessStart + AccessWidth > ContainingTypeSizeInBits) {
287 // If so, reduce access size to the next smaller power-of-two and retry.
288 AccessWidth >>= 1;
John McCall8a3c5552011-02-26 08:41:59 +0000289 assert(AccessWidth >= Types.getTarget().getCharWidth()
290 && "Cannot access under byte size!");
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000291 continue;
292 }
Daniel Dunbarb935b932010-04-13 20:58:55 +0000293
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000294 // Otherwise, add an access component.
Daniel Dunbarb935b932010-04-13 20:58:55 +0000295
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000296 // First, compute the bits inside this access which are part of the
297 // target. We are reading bits [AccessStart, AccessStart + AccessWidth); the
298 // intersection with [FieldOffset, FieldOffset + FieldSize) gives the bits
299 // in the target that we are reading.
Daniel Dunbar59813772010-04-22 15:22:33 +0000300 assert(FieldOffset < AccessStart + AccessWidth && "Invalid access start!");
301 assert(AccessStart < FieldOffset + FieldSize && "Invalid access start!");
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000302 uint64_t AccessBitsInFieldStart = std::max(AccessStart, FieldOffset);
303 uint64_t AccessBitsInFieldSize =
Daniel Dunbar59813772010-04-22 15:22:33 +0000304 std::min(AccessWidth + AccessStart,
305 FieldOffset + FieldSize) - AccessBitsInFieldStart;
Daniel Dunbar5d6c07e2010-04-22 14:56:10 +0000306
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000307 assert(NumComponents < 3 && "Unexpected number of components!");
308 CGBitFieldInfo::AccessInfo &AI = Components[NumComponents++];
309 AI.FieldIndex = 0;
310 // FIXME: We still follow the old access pattern of only using the field
311 // byte offset. We should switch this once we fix the struct layout to be
312 // pretty.
Chris Lattnerfb59c7c2011-02-17 22:09:58 +0000313
314 // on big-endian machines we reverted the bit offset because first fields are
315 // in higher bits. But this also reverts the bytes, so fix this here by reverting
316 // the byte offset on big-endian machines.
317 if (Types.getTargetData().isBigEndian()) {
Ken Dyckf76759c2011-04-24 10:04:59 +0000318 AI.FieldByteOffset = Types.getContext().toCharUnitsFromBits(
319 ContainingTypeSizeInBits - AccessStart - AccessWidth);
Chris Lattnerfb59c7c2011-02-17 22:09:58 +0000320 } else {
Ken Dyckf76759c2011-04-24 10:04:59 +0000321 AI.FieldByteOffset = Types.getContext().toCharUnitsFromBits(AccessStart);
Chris Lattnerfb59c7c2011-02-17 22:09:58 +0000322 }
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000323 AI.FieldBitStart = AccessBitsInFieldStart - AccessStart;
324 AI.AccessWidth = AccessWidth;
Ken Dyck27337a82011-04-24 10:13:17 +0000325 AI.AccessAlignment = Types.getContext().toCharUnitsFromBits(
326 llvm::MinAlign(ContainingTypeAlign, AccessStart));
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000327 AI.TargetBitOffset = AccessedTargetBits;
328 AI.TargetBitWidth = AccessBitsInFieldSize;
329
330 AccessStart += AccessWidth;
331 AccessedTargetBits += AI.TargetBitWidth;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000332 }
333
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000334 assert(AccessedTargetBits == FieldSize && "Invalid bit-field access!");
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000335 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000336}
337
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000338CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
339 const FieldDecl *FD,
340 uint64_t FieldOffset,
341 uint64_t FieldSize) {
342 const RecordDecl *RD = FD->getParent();
343 const ASTRecordLayout &RL = Types.getContext().getASTRecordLayout(RD);
Ken Dyckb0fcc592011-02-11 01:54:29 +0000344 uint64_t ContainingTypeSizeInBits = Types.getContext().toBits(RL.getSize());
Ken Dyck7ad11e72011-02-15 02:32:40 +0000345 unsigned ContainingTypeAlign = Types.getContext().toBits(RL.getAlignment());
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000346
347 return MakeInfo(Types, FD, FieldOffset, FieldSize, ContainingTypeSizeInBits,
348 ContainingTypeAlign);
349}
350
Anders Carlsson307846f2009-07-23 03:17:50 +0000351void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
John McCall4d9f1422011-02-15 22:21:29 +0000352 uint64_t fieldOffset) {
353 uint64_t fieldSize =
Anders Carlsson307846f2009-07-23 03:17:50 +0000354 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump11289f42009-09-09 15:08:12 +0000355
John McCall4d9f1422011-02-15 22:21:29 +0000356 if (fieldSize == 0)
Anders Carlsson307846f2009-07-23 03:17:50 +0000357 return;
358
John McCall8a3c5552011-02-26 08:41:59 +0000359 uint64_t nextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset);
Ken Dyck345a6de2011-04-24 16:40:29 +0000360 CharUnits numBytesToAppend;
361 unsigned charAlign = Types.getContext().Target.getCharAlign();
Mike Stump11289f42009-09-09 15:08:12 +0000362
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000363 if (fieldOffset < nextFieldOffsetInBits && !BitsAvailableInLastField) {
Ken Dyck345a6de2011-04-24 16:40:29 +0000364 assert(fieldOffset % charAlign == 0 &&
365 "Field offset not aligned correctly");
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000366
367 CharUnits fieldOffsetInCharUnits =
368 Types.getContext().toCharUnitsFromBits(fieldOffset);
369
370 // Try to resize the last base field.
371 if (ResizeLastBaseFieldIfNecessary(fieldOffsetInCharUnits))
372 nextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset);
373 }
374
John McCall4d9f1422011-02-15 22:21:29 +0000375 if (fieldOffset < nextFieldOffsetInBits) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000376 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
John McCall4d9f1422011-02-15 22:21:29 +0000377 assert(!NextFieldOffset.isZero() && "Must have laid out at least one byte");
Mike Stump11289f42009-09-09 15:08:12 +0000378
Anders Carlsson307846f2009-07-23 03:17:50 +0000379 // The bitfield begins in the previous bit-field.
Ken Dyck345a6de2011-04-24 16:40:29 +0000380 numBytesToAppend = Types.getContext().toCharUnitsFromBits(
381 llvm::RoundUpToAlignment(fieldSize - BitsAvailableInLastField,
382 charAlign));
Anders Carlsson307846f2009-07-23 03:17:50 +0000383 } else {
Ken Dyck345a6de2011-04-24 16:40:29 +0000384 assert(fieldOffset % charAlign == 0 &&
385 "Field offset not aligned correctly");
Anders Carlsson307846f2009-07-23 03:17:50 +0000386
387 // Append padding if necessary.
Ken Dyck345a6de2011-04-24 16:40:29 +0000388 AppendPadding(Types.getContext().toCharUnitsFromBits(fieldOffset),
389 CharUnits::One());
Mike Stump11289f42009-09-09 15:08:12 +0000390
Ken Dyck345a6de2011-04-24 16:40:29 +0000391 numBytesToAppend = Types.getContext().toCharUnitsFromBits(
392 llvm::RoundUpToAlignment(fieldSize, charAlign));
Mike Stump11289f42009-09-09 15:08:12 +0000393
Ken Dyck345a6de2011-04-24 16:40:29 +0000394 assert(!numBytesToAppend.isZero() && "No bytes to append!");
Anders Carlsson307846f2009-07-23 03:17:50 +0000395 }
396
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000397 // Add the bit field info.
John McCall0217dfc22011-02-15 06:40:56 +0000398 BitFields.insert(std::make_pair(D,
John McCall4d9f1422011-02-15 22:21:29 +0000399 CGBitFieldInfo::MakeInfo(Types, D, fieldOffset, fieldSize)));
Mike Stump11289f42009-09-09 15:08:12 +0000400
Ken Dyck345a6de2011-04-24 16:40:29 +0000401 AppendBytes(numBytesToAppend);
Mike Stump11289f42009-09-09 15:08:12 +0000402
Mike Stump11289f42009-09-09 15:08:12 +0000403 BitsAvailableInLastField =
Ken Dyck345a6de2011-04-24 16:40:29 +0000404 Types.getContext().toBits(NextFieldOffset) - (fieldOffset + fieldSize);
Anders Carlsson307846f2009-07-23 03:17:50 +0000405}
406
407bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
John McCall4d9f1422011-02-15 22:21:29 +0000408 uint64_t fieldOffset) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000409 // If the field is packed, then we need a packed struct.
Anders Carlsson68e0b682009-08-08 18:23:56 +0000410 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson307846f2009-07-23 03:17:50 +0000411 return false;
412
413 if (D->isBitField()) {
414 // We must use packed structs for unnamed bit fields since they
415 // don't affect the struct alignment.
Anders Carlssond78fc892009-07-23 17:24:40 +0000416 if (!Packed && !D->getDeclName())
Anders Carlsson307846f2009-07-23 03:17:50 +0000417 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000418
John McCall4d9f1422011-02-15 22:21:29 +0000419 LayoutBitField(D, fieldOffset);
Anders Carlsson307846f2009-07-23 03:17:50 +0000420 return true;
421 }
Mike Stump11289f42009-09-09 15:08:12 +0000422
John McCall614dbdc2010-08-22 21:01:12 +0000423 CheckZeroInitializable(D->getType());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000424
John McCall8a3c5552011-02-26 08:41:59 +0000425 assert(fieldOffset % Types.getTarget().getCharWidth() == 0
426 && "field offset is not on a byte boundary!");
427 CharUnits fieldOffsetInBytes
428 = Types.getContext().toCharUnitsFromBits(fieldOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000429
Anders Carlsson19702bb2009-08-04 16:29:15 +0000430 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
John McCall4d9f1422011-02-15 22:21:29 +0000431 CharUnits typeAlignment = getTypeAlignment(Ty);
Anders Carlsson19702bb2009-08-04 16:29:15 +0000432
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000433 // If the type alignment is larger then the struct alignment, we must use
434 // a packed struct.
John McCall4d9f1422011-02-15 22:21:29 +0000435 if (typeAlignment > Alignment) {
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000436 assert(!Packed && "Alignment is wrong even with packed struct!");
437 return false;
438 }
Mike Stump11289f42009-09-09 15:08:12 +0000439
John McCall4d9f1422011-02-15 22:21:29 +0000440 if (!Packed) {
441 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
442 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
443 if (const MaxFieldAlignmentAttr *MFAA =
444 RD->getAttr<MaxFieldAlignmentAttr>()) {
John McCall8a3c5552011-02-26 08:41:59 +0000445 if (MFAA->getAlignment() != Types.getContext().toBits(typeAlignment))
John McCall4d9f1422011-02-15 22:21:29 +0000446 return false;
447 }
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000448 }
449 }
450
Anders Carlsson19702bb2009-08-04 16:29:15 +0000451 // Round up the field offset to the alignment of the field type.
John McCall4d9f1422011-02-15 22:21:29 +0000452 CharUnits alignedNextFieldOffsetInBytes =
453 NextFieldOffset.RoundUpToAlignment(typeAlignment);
Anders Carlsson19702bb2009-08-04 16:29:15 +0000454
John McCall4d9f1422011-02-15 22:21:29 +0000455 if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000456 // Try to resize the last base field.
457 if (ResizeLastBaseFieldIfNecessary(fieldOffsetInBytes)) {
458 alignedNextFieldOffsetInBytes =
459 NextFieldOffset.RoundUpToAlignment(typeAlignment);
460 }
461 }
462
463 if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
Anders Carlsson19702bb2009-08-04 16:29:15 +0000464 assert(!Packed && "Could not place field even with packed struct!");
465 return false;
466 }
Mike Stump11289f42009-09-09 15:08:12 +0000467
John McCall4d9f1422011-02-15 22:21:29 +0000468 AppendPadding(fieldOffsetInBytes, typeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +0000469
Anders Carlsson307846f2009-07-23 03:17:50 +0000470 // Now append the field.
John McCall0217dfc22011-02-15 06:40:56 +0000471 Fields[D] = FieldTypes.size();
John McCall4d9f1422011-02-15 22:21:29 +0000472 AppendField(fieldOffsetInBytes, Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000473
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000474 LastLaidOutBase.invalidate();
Anders Carlsson307846f2009-07-23 03:17:50 +0000475 return true;
476}
477
Anders Carlsson1de2f572010-04-17 20:49:27 +0000478const llvm::Type *
479CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
480 const ASTRecordLayout &Layout) {
481 if (Field->isBitField()) {
482 uint64_t FieldSize =
483 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
484
485 // Ignore zero sized bit fields.
486 if (FieldSize == 0)
487 return 0;
488
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000489 const llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
Ken Dyck7a0b19f2011-04-24 16:47:33 +0000490 CharUnits NumBytesToAppend = Types.getContext().toCharUnitsFromBits(
491 llvm::RoundUpToAlignment(FieldSize,
492 Types.getContext().Target.getCharAlign()));
Anders Carlsson2295f132010-04-17 21:04:52 +0000493
Ken Dyck7a0b19f2011-04-24 16:47:33 +0000494 if (NumBytesToAppend > CharUnits::One())
495 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend.getQuantity());
Anders Carlsson2295f132010-04-17 21:04:52 +0000496
Anders Carlsson1de2f572010-04-17 20:49:27 +0000497 // Add the bit field info.
John McCall0217dfc22011-02-15 06:40:56 +0000498 BitFields.insert(std::make_pair(Field,
499 CGBitFieldInfo::MakeInfo(Types, Field, 0, FieldSize)));
Anders Carlsson2295f132010-04-17 21:04:52 +0000500 return FieldTy;
Anders Carlsson1de2f572010-04-17 20:49:27 +0000501 }
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000502
Anders Carlsson1de2f572010-04-17 20:49:27 +0000503 // This is a regular union field.
John McCall0217dfc22011-02-15 06:40:56 +0000504 Fields[Field] = 0;
Anders Carlsson1de2f572010-04-17 20:49:27 +0000505 return Types.ConvertTypeForMemRecursive(Field->getType());
506}
507
Anders Carlsson697f6592009-07-23 03:43:54 +0000508void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
509 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump11289f42009-09-09 15:08:12 +0000510
John McCall4d9f1422011-02-15 22:21:29 +0000511 const ASTRecordLayout &layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000512
John McCall4d9f1422011-02-15 22:21:29 +0000513 const llvm::Type *unionType = 0;
514 CharUnits unionSize = CharUnits::Zero();
515 CharUnits unionAlign = CharUnits::Zero();
Mike Stump11289f42009-09-09 15:08:12 +0000516
John McCall4d9f1422011-02-15 22:21:29 +0000517 bool hasOnlyZeroSizedBitFields = true;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000518
John McCall4d9f1422011-02-15 22:21:29 +0000519 unsigned fieldNo = 0;
520 for (RecordDecl::field_iterator field = D->field_begin(),
521 fieldEnd = D->field_end(); field != fieldEnd; ++field, ++fieldNo) {
522 assert(layout.getFieldOffset(fieldNo) == 0 &&
Anders Carlsson697f6592009-07-23 03:43:54 +0000523 "Union field offset did not start at the beginning of record!");
John McCall4d9f1422011-02-15 22:21:29 +0000524 const llvm::Type *fieldType = LayoutUnionField(*field, layout);
Anders Carlssonf814ee62009-07-23 04:00:39 +0000525
John McCall4d9f1422011-02-15 22:21:29 +0000526 if (!fieldType)
Anders Carlsson1de2f572010-04-17 20:49:27 +0000527 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000528
John McCall4d9f1422011-02-15 22:21:29 +0000529 hasOnlyZeroSizedBitFields = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000530
John McCall4d9f1422011-02-15 22:21:29 +0000531 CharUnits fieldAlign = CharUnits::fromQuantity(
532 Types.getTargetData().getABITypeAlignment(fieldType));
533 CharUnits fieldSize = CharUnits::fromQuantity(
534 Types.getTargetData().getTypeAllocSize(fieldType));
Mike Stump11289f42009-09-09 15:08:12 +0000535
John McCall4d9f1422011-02-15 22:21:29 +0000536 if (fieldAlign < unionAlign)
Anders Carlsson697f6592009-07-23 03:43:54 +0000537 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000538
John McCall4d9f1422011-02-15 22:21:29 +0000539 if (fieldAlign > unionAlign || fieldSize > unionSize) {
540 unionType = fieldType;
541 unionAlign = fieldAlign;
542 unionSize = fieldSize;
Anders Carlsson697f6592009-07-23 03:43:54 +0000543 }
544 }
Mike Stump11289f42009-09-09 15:08:12 +0000545
Anders Carlsson697f6592009-07-23 03:43:54 +0000546 // Now add our field.
John McCall4d9f1422011-02-15 22:21:29 +0000547 if (unionType) {
548 AppendField(CharUnits::Zero(), unionType);
Anders Carlsson0e912752009-09-03 22:56:02 +0000549
John McCall4d9f1422011-02-15 22:21:29 +0000550 if (getTypeAlignment(unionType) > layout.getAlignment()) {
Anders Carlsson0e912752009-09-03 22:56:02 +0000551 // We need a packed struct.
552 Packed = true;
John McCall4d9f1422011-02-15 22:21:29 +0000553 unionAlign = CharUnits::One();
Anders Carlsson0e912752009-09-03 22:56:02 +0000554 }
555 }
John McCall4d9f1422011-02-15 22:21:29 +0000556 if (unionAlign.isZero()) {
557 assert(hasOnlyZeroSizedBitFields &&
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000558 "0-align record did not have all zero-sized bit-fields!");
John McCall4d9f1422011-02-15 22:21:29 +0000559 unionAlign = CharUnits::One();
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000560 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000561
Anders Carlsson697f6592009-07-23 03:43:54 +0000562 // Append tail padding.
John McCall4d9f1422011-02-15 22:21:29 +0000563 CharUnits recordSize = layout.getSize();
564 if (recordSize > unionSize)
565 AppendPadding(recordSize, unionAlign);
Anders Carlsson697f6592009-07-23 03:43:54 +0000566}
567
John McCall0217dfc22011-02-15 06:40:56 +0000568void CGRecordLayoutBuilder::LayoutBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000569 const CGRecordLayout &baseLayout,
570 CharUnits baseOffset) {
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000571 ResizeLastBaseFieldIfNecessary(baseOffset);
572
John McCall4d9f1422011-02-15 22:21:29 +0000573 AppendPadding(baseOffset, CharUnits::One());
Anders Carlssona518b2a2010-12-04 23:59:48 +0000574
John McCall0217dfc22011-02-15 06:40:56 +0000575 const ASTRecordLayout &baseASTLayout
576 = Types.getContext().getASTRecordLayout(base);
577
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000578 LastLaidOutBase.Offset = NextFieldOffset;
579 LastLaidOutBase.NonVirtualSize = baseASTLayout.getNonVirtualSize();
580
John McCall4d9f1422011-02-15 22:21:29 +0000581 // Fields and bases can be laid out in the tail padding of previous
582 // bases. If this happens, we need to allocate the base as an i8
583 // array; otherwise, we can use the subobject type. However,
584 // actually doing that would require knowledge of what immediately
585 // follows this base in the layout, so instead we do a conservative
586 // approximation, which is to use the base subobject type if it
587 // has the same LLVM storage size as the nvsize.
588
John McCall4d9f1422011-02-15 22:21:29 +0000589 const llvm::StructType *subobjectType = baseLayout.getBaseSubobjectLLVMType();
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000590 AppendField(baseOffset, subobjectType);
John McCall4d9f1422011-02-15 22:21:29 +0000591
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000592 Types.addBaseSubobjectTypeName(base, baseLayout);
John McCall0217dfc22011-02-15 06:40:56 +0000593}
594
595void CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000596 CharUnits baseOffset) {
John McCall0217dfc22011-02-15 06:40:56 +0000597 // Ignore empty bases.
598 if (base->isEmpty()) return;
599
600 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
601 if (IsZeroInitializableAsBase) {
602 assert(IsZeroInitializable &&
603 "class zero-initializable as base but not as complete object");
604
605 IsZeroInitializable = IsZeroInitializableAsBase =
606 baseLayout.isZeroInitializableAsBase();
607 }
608
John McCall4d9f1422011-02-15 22:21:29 +0000609 LayoutBase(base, baseLayout, baseOffset);
John McCall0217dfc22011-02-15 06:40:56 +0000610 NonVirtualBases[base] = (FieldTypes.size() - 1);
Anders Carlssona518b2a2010-12-04 23:59:48 +0000611}
612
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000613void
John McCall0217dfc22011-02-15 06:40:56 +0000614CGRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000615 CharUnits baseOffset) {
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000616 // Ignore empty bases.
John McCall0217dfc22011-02-15 06:40:56 +0000617 if (base->isEmpty()) return;
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000618
John McCall0217dfc22011-02-15 06:40:56 +0000619 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
620 if (IsZeroInitializable)
621 IsZeroInitializable = baseLayout.isZeroInitializableAsBase();
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000622
John McCall4d9f1422011-02-15 22:21:29 +0000623 LayoutBase(base, baseLayout, baseOffset);
John McCall0217dfc22011-02-15 06:40:56 +0000624 VirtualBases[base] = (FieldTypes.size() - 1);
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000625}
626
Anders Carlssona459adb2010-11-28 19:18:44 +0000627/// LayoutVirtualBases - layout the non-virtual bases of a record decl.
628void
629CGRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
630 const ASTRecordLayout &Layout) {
631 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
632 E = RD->bases_end(); I != E; ++I) {
633 const CXXRecordDecl *BaseDecl =
634 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
635
636 // We only want to lay out virtual bases that aren't indirect primary bases
637 // of some other base.
638 if (I->isVirtual() && !IndirectPrimaryBases.count(BaseDecl)) {
639 // Only lay out the base once.
640 if (!LaidOutVirtualBases.insert(BaseDecl))
641 continue;
642
John McCall4d9f1422011-02-15 22:21:29 +0000643 CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl);
644 LayoutVirtualBase(BaseDecl, vbaseOffset);
Anders Carlssona459adb2010-11-28 19:18:44 +0000645 }
646
647 if (!BaseDecl->getNumVBases()) {
648 // This base isn't interesting since it doesn't have any virtual bases.
649 continue;
650 }
651
652 LayoutVirtualBases(BaseDecl, Layout);
653 }
654}
655
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000656void
657CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD,
658 const ASTRecordLayout &Layout) {
659 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
660
661 // Check if we need to add a vtable pointer.
662 if (RD->isDynamicClass()) {
663 if (!PrimaryBase) {
664 const llvm::Type *FunctionType =
665 llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()),
666 /*isVarArg=*/true);
667 const llvm::Type *VTableTy = FunctionType->getPointerTo();
668
John McCall4d9f1422011-02-15 22:21:29 +0000669 assert(NextFieldOffset.isZero() &&
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000670 "VTable pointer must come first!");
John McCall4d9f1422011-02-15 22:21:29 +0000671 AppendField(CharUnits::Zero(), VTableTy->getPointerTo());
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000672 } else {
Anders Carlsson7f95cd12010-11-24 23:12:57 +0000673 if (!Layout.isPrimaryBaseVirtual())
John McCall4d9f1422011-02-15 22:21:29 +0000674 LayoutNonVirtualBase(PrimaryBase, CharUnits::Zero());
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000675 else
John McCall4d9f1422011-02-15 22:21:29 +0000676 LayoutVirtualBase(PrimaryBase, CharUnits::Zero());
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000677 }
678 }
679
680 // Layout the non-virtual bases.
681 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
682 E = RD->bases_end(); I != E; ++I) {
683 if (I->isVirtual())
684 continue;
685
686 const CXXRecordDecl *BaseDecl =
687 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
688
689 // We've already laid out the primary base.
Anders Carlsson7f95cd12010-11-24 23:12:57 +0000690 if (BaseDecl == PrimaryBase && !Layout.isPrimaryBaseVirtual())
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000691 continue;
692
John McCall4d9f1422011-02-15 22:21:29 +0000693 LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffset(BaseDecl));
Anders Carlssond681a292009-12-16 17:27:20 +0000694 }
695}
696
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000697bool
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000698CGRecordLayoutBuilder::ComputeNonVirtualBaseType(const CXXRecordDecl *RD) {
699 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(RD);
700
Ken Dyckbec02852011-02-08 02:02:47 +0000701 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
702 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
John McCall4d9f1422011-02-15 22:21:29 +0000703 CharUnits AlignedNonVirtualTypeSize =
704 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000705
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000706 // First check if we can use the same fields as for the complete class.
John McCall4d9f1422011-02-15 22:21:29 +0000707 CharUnits RecordSize = Layout.getSize();
John McCall0217dfc22011-02-15 06:40:56 +0000708 if (AlignedNonVirtualTypeSize == RecordSize)
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000709 return true;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000710
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000711 // Check if we need padding.
John McCall4d9f1422011-02-15 22:21:29 +0000712 CharUnits AlignedNextFieldOffset =
713 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000714
John McCall0217dfc22011-02-15 06:40:56 +0000715 if (AlignedNextFieldOffset > AlignedNonVirtualTypeSize) {
716 assert(!Packed && "cannot layout even as packed struct");
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000717 return false; // Needs packing.
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000718 }
719
John McCall0217dfc22011-02-15 06:40:56 +0000720 bool needsPadding = (AlignedNonVirtualTypeSize != AlignedNextFieldOffset);
721 if (needsPadding) {
John McCall4d9f1422011-02-15 22:21:29 +0000722 CharUnits NumBytes = AlignedNonVirtualTypeSize - AlignedNextFieldOffset;
John McCall0217dfc22011-02-15 06:40:56 +0000723 FieldTypes.push_back(getByteArrayType(NumBytes));
724 }
725
726 BaseSubobjectType = llvm::StructType::get(Types.getLLVMContext(),
727 FieldTypes, Packed);
728
729 if (needsPadding) {
730 // Pull the padding back off.
731 FieldTypes.pop_back();
732 }
733
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000734 return true;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000735}
736
Anders Carlsson307846f2009-07-23 03:17:50 +0000737bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
738 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
John McCall4d9f1422011-02-15 22:21:29 +0000739 assert(!Alignment.isZero() && "Did not set alignment!");
Mike Stump11289f42009-09-09 15:08:12 +0000740
Anders Carlsson697f6592009-07-23 03:43:54 +0000741 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000742
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000743 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
744 if (RD)
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000745 LayoutNonVirtualBases(RD, Layout);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000746
Anders Carlsson307846f2009-07-23 03:17:50 +0000747 unsigned FieldNo = 0;
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000748 const FieldDecl *LastFD = 0;
749
Mike Stump11289f42009-09-09 15:08:12 +0000750 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson307846f2009-07-23 03:17:50 +0000751 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000752 if (IsMsStruct) {
753 // Zero-length bitfields following non-bitfield members are
754 // ignored:
755 const FieldDecl *FD = (*Field);
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +0000756 if (Types.getContext().ZeroBitfieldFollowsNonBitfield(FD, LastFD)) {
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000757 --FieldNo;
758 continue;
759 }
760 LastFD = FD;
761 }
762
Anders Carlsson307846f2009-07-23 03:17:50 +0000763 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump11289f42009-09-09 15:08:12 +0000764 assert(!Packed &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000765 "Could not layout fields even with a packed LLVM struct!");
766 return false;
767 }
768 }
769
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000770 if (RD) {
Anders Carlssona459adb2010-11-28 19:18:44 +0000771 // We've laid out the non-virtual bases and the fields, now compute the
772 // non-virtual base field types.
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000773 if (!ComputeNonVirtualBaseType(RD)) {
774 assert(!Packed && "Could not layout even with a packed LLVM struct!");
775 return false;
776 }
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000777
Anders Carlssona459adb2010-11-28 19:18:44 +0000778 // And lay out the virtual bases.
779 RD->getIndirectPrimaryBases(IndirectPrimaryBases);
780 if (Layout.isPrimaryBaseVirtual())
781 IndirectPrimaryBases.insert(Layout.getPrimaryBase());
782 LayoutVirtualBases(RD, Layout);
783 }
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000784
Anders Carlsson307846f2009-07-23 03:17:50 +0000785 // Append tail padding if necessary.
Ken Dyck272b6fa2011-04-24 16:53:44 +0000786 AppendTailPadding(Layout.getSize());
Mike Stump11289f42009-09-09 15:08:12 +0000787
Anders Carlsson307846f2009-07-23 03:17:50 +0000788 return true;
789}
790
Ken Dyck272b6fa2011-04-24 16:53:44 +0000791void CGRecordLayoutBuilder::AppendTailPadding(CharUnits RecordSize) {
792 ResizeLastBaseFieldIfNecessary(RecordSize);
Mike Stump11289f42009-09-09 15:08:12 +0000793
Ken Dyck272b6fa2011-04-24 16:53:44 +0000794 assert(NextFieldOffset <= RecordSize && "Size mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000795
John McCall4d9f1422011-02-15 22:21:29 +0000796 CharUnits AlignedNextFieldOffset =
797 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
Anders Carlsson220bf4f2009-12-08 01:24:23 +0000798
Ken Dyck272b6fa2011-04-24 16:53:44 +0000799 if (AlignedNextFieldOffset == RecordSize) {
Anders Carlsson220bf4f2009-12-08 01:24:23 +0000800 // We don't need any padding.
801 return;
802 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000803
Ken Dyck272b6fa2011-04-24 16:53:44 +0000804 CharUnits NumPadBytes = RecordSize - NextFieldOffset;
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000805 AppendBytes(NumPadBytes);
806}
807
John McCall4d9f1422011-02-15 22:21:29 +0000808void CGRecordLayoutBuilder::AppendField(CharUnits fieldOffset,
809 const llvm::Type *fieldType) {
810 CharUnits fieldSize =
811 CharUnits::fromQuantity(Types.getTargetData().getTypeAllocSize(fieldType));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000812
John McCall4d9f1422011-02-15 22:21:29 +0000813 FieldTypes.push_back(fieldType);
Anders Carlsson307846f2009-07-23 03:17:50 +0000814
John McCall4d9f1422011-02-15 22:21:29 +0000815 NextFieldOffset = fieldOffset + fieldSize;
Anders Carlsson307846f2009-07-23 03:17:50 +0000816 BitsAvailableInLastField = 0;
817}
818
John McCall4d9f1422011-02-15 22:21:29 +0000819void CGRecordLayoutBuilder::AppendPadding(CharUnits fieldOffset,
820 CharUnits fieldAlignment) {
821 assert(NextFieldOffset <= fieldOffset &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000822 "Incorrect field layout!");
Mike Stump11289f42009-09-09 15:08:12 +0000823
Anders Carlsson307846f2009-07-23 03:17:50 +0000824 // Round up the field offset to the alignment of the field type.
John McCall4d9f1422011-02-15 22:21:29 +0000825 CharUnits alignedNextFieldOffset =
826 NextFieldOffset.RoundUpToAlignment(fieldAlignment);
Anders Carlsson307846f2009-07-23 03:17:50 +0000827
John McCall4d9f1422011-02-15 22:21:29 +0000828 if (alignedNextFieldOffset < fieldOffset) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000829 // Even with alignment, the field offset is not at the right place,
830 // insert padding.
John McCall4d9f1422011-02-15 22:21:29 +0000831 CharUnits padding = fieldOffset - NextFieldOffset;
Anders Carlsson307846f2009-07-23 03:17:50 +0000832
John McCall4d9f1422011-02-15 22:21:29 +0000833 AppendBytes(padding);
Anders Carlsson307846f2009-07-23 03:17:50 +0000834 }
835}
836
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000837bool CGRecordLayoutBuilder::ResizeLastBaseFieldIfNecessary(CharUnits offset) {
838 // Check if we have a base to resize.
839 if (!LastLaidOutBase.isValid())
840 return false;
841
842 // This offset does not overlap with the tail padding.
843 if (offset >= NextFieldOffset)
844 return false;
845
846 // Restore the field offset and append an i8 array instead.
847 FieldTypes.pop_back();
848 NextFieldOffset = LastLaidOutBase.Offset;
849 AppendBytes(LastLaidOutBase.NonVirtualSize);
850 LastLaidOutBase.invalidate();
851
852 return true;
853}
854
John McCall4d9f1422011-02-15 22:21:29 +0000855const llvm::Type *CGRecordLayoutBuilder::getByteArrayType(CharUnits numBytes) {
856 assert(!numBytes.isZero() && "Empty byte arrays aren't allowed.");
Mike Stump11289f42009-09-09 15:08:12 +0000857
Owen Anderson41a75022009-08-13 21:57:51 +0000858 const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
John McCall4d9f1422011-02-15 22:21:29 +0000859 if (numBytes > CharUnits::One())
860 Ty = llvm::ArrayType::get(Ty, numBytes.getQuantity());
Mike Stump11289f42009-09-09 15:08:12 +0000861
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000862 return Ty;
863}
864
John McCall4d9f1422011-02-15 22:21:29 +0000865void CGRecordLayoutBuilder::AppendBytes(CharUnits numBytes) {
866 if (numBytes.isZero())
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000867 return;
868
Anders Carlsson307846f2009-07-23 03:17:50 +0000869 // Append the padding field
John McCall4d9f1422011-02-15 22:21:29 +0000870 AppendField(NextFieldOffset, getByteArrayType(numBytes));
Anders Carlsson307846f2009-07-23 03:17:50 +0000871}
872
John McCall4d9f1422011-02-15 22:21:29 +0000873CharUnits CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlssond78fc892009-07-23 17:24:40 +0000874 if (Packed)
John McCall4d9f1422011-02-15 22:21:29 +0000875 return CharUnits::One();
Mike Stump11289f42009-09-09 15:08:12 +0000876
John McCall4d9f1422011-02-15 22:21:29 +0000877 return CharUnits::fromQuantity(Types.getTargetData().getABITypeAlignment(Ty));
Anders Carlsson307846f2009-07-23 03:17:50 +0000878}
879
John McCall4d9f1422011-02-15 22:21:29 +0000880CharUnits CGRecordLayoutBuilder::getAlignmentAsLLVMStruct() const {
Anders Carlssonacf877b2010-11-28 23:06:23 +0000881 if (Packed)
John McCall4d9f1422011-02-15 22:21:29 +0000882 return CharUnits::One();
Anders Carlssonacf877b2010-11-28 23:06:23 +0000883
John McCall4d9f1422011-02-15 22:21:29 +0000884 CharUnits maxAlignment = CharUnits::One();
Anders Carlssonacf877b2010-11-28 23:06:23 +0000885 for (size_t i = 0; i != FieldTypes.size(); ++i)
John McCall4d9f1422011-02-15 22:21:29 +0000886 maxAlignment = std::max(maxAlignment, getTypeAlignment(FieldTypes[i]));
Anders Carlssonacf877b2010-11-28 23:06:23 +0000887
John McCall4d9f1422011-02-15 22:21:29 +0000888 return maxAlignment;
Anders Carlssonacf877b2010-11-28 23:06:23 +0000889}
890
John McCall0217dfc22011-02-15 06:40:56 +0000891/// Merge in whether a field of the given type is zero-initializable.
John McCall614dbdc2010-08-22 21:01:12 +0000892void CGRecordLayoutBuilder::CheckZeroInitializable(QualType T) {
Anders Carlssond606de72009-08-23 01:25:01 +0000893 // This record already contains a member pointer.
John McCall0217dfc22011-02-15 06:40:56 +0000894 if (!IsZeroInitializableAsBase)
Anders Carlssond606de72009-08-23 01:25:01 +0000895 return;
Mike Stump11289f42009-09-09 15:08:12 +0000896
Anders Carlssond606de72009-08-23 01:25:01 +0000897 // Can only have member pointers if we're compiling C++.
898 if (!Types.getContext().getLangOptions().CPlusPlus)
899 return;
Mike Stump11289f42009-09-09 15:08:12 +0000900
John McCall0217dfc22011-02-15 06:40:56 +0000901 const Type *elementType = T->getBaseElementTypeUnsafe();
Mike Stump11289f42009-09-09 15:08:12 +0000902
John McCall0217dfc22011-02-15 06:40:56 +0000903 if (const MemberPointerType *MPT = elementType->getAs<MemberPointerType>()) {
John McCall614dbdc2010-08-22 21:01:12 +0000904 if (!Types.getCXXABI().isZeroInitializable(MPT))
John McCall0217dfc22011-02-15 06:40:56 +0000905 IsZeroInitializable = IsZeroInitializableAsBase = false;
906 } else if (const RecordType *RT = elementType->getAs<RecordType>()) {
Anders Carlssone8bfe412010-02-02 05:17:25 +0000907 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
John McCall0217dfc22011-02-15 06:40:56 +0000908 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
909 if (!Layout.isZeroInitializable())
910 IsZeroInitializable = IsZeroInitializableAsBase = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000911 }
Anders Carlssond606de72009-08-23 01:25:01 +0000912}
913
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000914CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) {
915 CGRecordLayoutBuilder Builder(*this);
Mike Stump11289f42009-09-09 15:08:12 +0000916
Anders Carlsson307846f2009-07-23 03:17:50 +0000917 Builder.Layout(D);
Anders Carlssone1d5ca52009-07-24 15:20:52 +0000918
Anders Carlsson36e2fa82010-11-24 19:37:16 +0000919 const llvm::StructType *Ty = llvm::StructType::get(getLLVMContext(),
920 Builder.FieldTypes,
921 Builder.Packed);
Mike Stump11289f42009-09-09 15:08:12 +0000922
John McCall0217dfc22011-02-15 06:40:56 +0000923 // If we're in C++, compute the base subobject type.
Anders Carlsson36e2fa82010-11-24 19:37:16 +0000924 const llvm::StructType *BaseTy = 0;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000925 if (isa<CXXRecordDecl>(D)) {
John McCall0217dfc22011-02-15 06:40:56 +0000926 BaseTy = Builder.BaseSubobjectType;
927 if (!BaseTy) BaseTy = Ty;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000928 }
929
Daniel Dunbar034299e2010-03-31 01:09:11 +0000930 CGRecordLayout *RL =
John McCall0217dfc22011-02-15 06:40:56 +0000931 new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable,
932 Builder.IsZeroInitializableAsBase);
Daniel Dunbar034299e2010-03-31 01:09:11 +0000933
John McCall0217dfc22011-02-15 06:40:56 +0000934 RL->NonVirtualBases.swap(Builder.NonVirtualBases);
935 RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases);
Anders Carlsson061ca522010-05-18 05:22:06 +0000936
Anders Carlsson307846f2009-07-23 03:17:50 +0000937 // Add all the field numbers.
John McCall0217dfc22011-02-15 06:40:56 +0000938 RL->FieldInfo.swap(Builder.Fields);
Anders Carlsson307846f2009-07-23 03:17:50 +0000939
940 // Add bitfield info.
John McCall0217dfc22011-02-15 06:40:56 +0000941 RL->BitFields.swap(Builder.BitFields);
Mike Stump11289f42009-09-09 15:08:12 +0000942
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000943 // Dump the layout, if requested.
Daniel Dunbarb935b932010-04-13 20:58:55 +0000944 if (getContext().getLangOptions().DumpRecordLayouts) {
Daniel Dunbarccabe482010-04-19 20:44:53 +0000945 llvm::errs() << "\n*** Dumping IRgen Record Layout\n";
Daniel Dunbarb935b932010-04-13 20:58:55 +0000946 llvm::errs() << "Record: ";
947 D->dump();
948 llvm::errs() << "\nLayout: ";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000949 RL->dump();
Daniel Dunbarb935b932010-04-13 20:58:55 +0000950 }
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000951
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000952#ifndef NDEBUG
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000953 // Verify that the computed LLVM struct size matches the AST layout size.
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000954 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
955
Ken Dyckb0fcc592011-02-11 01:54:29 +0000956 uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize());
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000957 assert(TypeSizeInBits == getTargetData().getTypeAllocSizeInBits(Ty) &&
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000958 "Type size mismatch!");
959
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000960 if (BaseTy) {
Ken Dyckbec02852011-02-08 02:02:47 +0000961 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
962 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
963 CharUnits AlignedNonVirtualTypeSize =
964 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
965
966 uint64_t AlignedNonVirtualTypeSizeInBits =
Ken Dyckb0fcc592011-02-11 01:54:29 +0000967 getContext().toBits(AlignedNonVirtualTypeSize);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000968
969 assert(AlignedNonVirtualTypeSizeInBits ==
970 getTargetData().getTypeAllocSizeInBits(BaseTy) &&
971 "Type size mismatch!");
972 }
973
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000974 // Verify that the LLVM and AST field offsets agree.
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000975 const llvm::StructType *ST =
976 dyn_cast<llvm::StructType>(RL->getLLVMType());
977 const llvm::StructLayout *SL = getTargetData().getStructLayout(ST);
978
979 const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
980 RecordDecl::field_iterator it = D->field_begin();
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000981 const FieldDecl *LastFD = 0;
982 bool IsMsStruct = D->hasAttr<MsStructAttr>();
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000983 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
984 const FieldDecl *FD = *it;
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000985
986 // For non-bit-fields, just check that the LLVM struct offset matches the
987 // AST offset.
988 if (!FD->isBitField()) {
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000989 unsigned FieldNo = RL->getLLVMFieldNo(FD);
990 assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
991 "Invalid field offset!");
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000992 LastFD = FD;
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000993 continue;
994 }
995
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000996 if (IsMsStruct) {
997 // Zero-length bitfields following non-bitfield members are
998 // ignored:
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +0000999 if (getContext().ZeroBitfieldFollowsNonBitfield(FD, LastFD)) {
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001000 --i;
1001 continue;
1002 }
1003 LastFD = FD;
1004 }
1005
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001006 // Ignore unnamed bit-fields.
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001007 if (!FD->getDeclName()) {
1008 LastFD = FD;
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001009 continue;
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001010 }
1011
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001012 const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
1013 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
1014 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
1015
1016 // Verify that every component access is within the structure.
1017 uint64_t FieldOffset = SL->getElementOffsetInBits(AI.FieldIndex);
John McCall8a3c5552011-02-26 08:41:59 +00001018 uint64_t AccessBitOffset = FieldOffset +
Ken Dyckf76759c2011-04-24 10:04:59 +00001019 getContext().toBits(AI.FieldByteOffset);
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001020 assert(AccessBitOffset + AI.AccessWidth <= TypeSizeInBits &&
1021 "Invalid bit-field access (out of range)!");
Daniel Dunbar2ba67442010-04-21 19:10:49 +00001022 }
1023 }
1024#endif
Daniel Dunbar2ea51832010-04-19 20:44:47 +00001025
Daniel Dunbar034299e2010-03-31 01:09:11 +00001026 return RL;
Anders Carlsson307846f2009-07-23 03:17:50 +00001027}
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001028
1029void CGRecordLayout::print(llvm::raw_ostream &OS) const {
1030 OS << "<CGRecordLayout\n";
John McCall0217dfc22011-02-15 06:40:56 +00001031 OS << " LLVMType:" << *CompleteObjectType << "\n";
1032 if (BaseSubobjectType)
1033 OS << " NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n";
John McCall614dbdc2010-08-22 21:01:12 +00001034 OS << " IsZeroInitializable:" << IsZeroInitializable << "\n";
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001035 OS << " BitFields:[\n";
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001036
1037 // Print bit-field infos in declaration order.
1038 std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001039 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
1040 it = BitFields.begin(), ie = BitFields.end();
1041 it != ie; ++it) {
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001042 const RecordDecl *RD = it->first->getParent();
1043 unsigned Index = 0;
1044 for (RecordDecl::field_iterator
1045 it2 = RD->field_begin(); *it2 != it->first; ++it2)
1046 ++Index;
1047 BFIs.push_back(std::make_pair(Index, &it->second));
1048 }
1049 llvm::array_pod_sort(BFIs.begin(), BFIs.end());
1050 for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
Daniel Dunbarb935b932010-04-13 20:58:55 +00001051 OS.indent(4);
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001052 BFIs[i].second->print(OS);
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001053 OS << "\n";
1054 }
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001055
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001056 OS << "]>\n";
1057}
1058
1059void CGRecordLayout::dump() const {
1060 print(llvm::errs());
1061}
1062
1063void CGBitFieldInfo::print(llvm::raw_ostream &OS) const {
1064 OS << "<CGBitFieldInfo";
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001065 OS << " Size:" << Size;
Daniel Dunbarb935b932010-04-13 20:58:55 +00001066 OS << " IsSigned:" << IsSigned << "\n";
1067
1068 OS.indent(4 + strlen("<CGBitFieldInfo"));
1069 OS << " NumComponents:" << getNumComponents();
1070 OS << " Components: [";
1071 if (getNumComponents()) {
1072 OS << "\n";
1073 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
1074 const AccessInfo &AI = getComponent(i);
1075 OS.indent(8);
1076 OS << "<AccessInfo"
1077 << " FieldIndex:" << AI.FieldIndex
Ken Dyckf76759c2011-04-24 10:04:59 +00001078 << " FieldByteOffset:" << AI.FieldByteOffset.getQuantity()
Daniel Dunbarb935b932010-04-13 20:58:55 +00001079 << " FieldBitStart:" << AI.FieldBitStart
1080 << " AccessWidth:" << AI.AccessWidth << "\n";
1081 OS.indent(8 + strlen("<AccessInfo"));
Ken Dyck27337a82011-04-24 10:13:17 +00001082 OS << " AccessAlignment:" << AI.AccessAlignment.getQuantity()
Daniel Dunbarb935b932010-04-13 20:58:55 +00001083 << " TargetBitOffset:" << AI.TargetBitOffset
1084 << " TargetBitWidth:" << AI.TargetBitWidth
1085 << ">\n";
1086 }
1087 OS.indent(4);
1088 }
1089 OS << "]>";
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001090}
1091
1092void CGBitFieldInfo::dump() const {
1093 print(llvm::errs());
1094}