blob: ceae66f3849e37f63497ebbbbf37cfd008da62c6 [file] [log] [blame]
Daniel Dunbar270e2032010-03-31 00:11:27 +00001//===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder ----*- C++ -*-===//
Anders Carlsson45372a62009-07-23 03:17:50 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Daniel Dunbar270e2032010-03-31 00:11:27 +000010// Builder implementation for CGRecordLayout objects.
Anders Carlsson45372a62009-07-23 03:17:50 +000011//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar2924ade2010-03-30 22:26:10 +000014#include "CGRecordLayout.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/Attr.h"
Anders Carlsson46170f92010-11-24 22:50:27 +000017#include "clang/AST/CXXInheritance.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000018#include "clang/AST/DeclCXX.h"
19#include "clang/AST/Expr.h"
20#include "clang/AST/RecordLayout.h"
21#include "CodeGenTypes.h"
John McCallf16aa102010-08-22 21:01:12 +000022#include "CGCXXABI.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000023#include "llvm/DerivedTypes.h"
Daniel Dunbar93c62962010-04-12 18:14:18 +000024#include "llvm/Type.h"
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +000025#include "llvm/Support/Debug.h"
Daniel Dunbar93c62962010-04-12 18:14:18 +000026#include "llvm/Support/raw_ostream.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000027#include "llvm/Target/TargetData.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000028using namespace clang;
29using namespace CodeGen;
30
John McCalld0de0ce2010-11-30 23:17:27 +000031namespace {
Daniel Dunbar270e2032010-03-31 00:11:27 +000032
33class CGRecordLayoutBuilder {
34public:
35 /// FieldTypes - Holds the LLVM types that the struct is created from.
John McCall9b7da1c2011-02-15 06:40:56 +000036 ///
Daniel Dunbar270e2032010-03-31 00:11:27 +000037 std::vector<const llvm::Type *> FieldTypes;
38
John McCall9b7da1c2011-02-15 06:40:56 +000039 /// BaseSubobjectType - Holds the LLVM type for the non-virtual part
Anders Carlsson3d155e62010-11-09 05:25:47 +000040 /// of the struct. For example, consider:
41 ///
42 /// struct A { int i; };
43 /// struct B { void *v; };
44 /// struct C : virtual A, B { };
45 ///
46 /// The LLVM type of C will be
47 /// %struct.C = type { i32 (...)**, %struct.A, i32, %struct.B }
48 ///
49 /// And the LLVM type of the non-virtual base struct will be
50 /// %struct.C.base = type { i32 (...)**, %struct.A, i32 }
John McCall9b7da1c2011-02-15 06:40:56 +000051 ///
52 /// This only gets initialized if the base subobject type is
53 /// different from the complete-object type.
54 const llvm::StructType *BaseSubobjectType;
Anders Carlsson3d155e62010-11-09 05:25:47 +000055
John McCall9b7da1c2011-02-15 06:40:56 +000056 /// FieldInfo - Holds a field and its corresponding LLVM field number.
57 llvm::DenseMap<const FieldDecl *, unsigned> Fields;
Daniel Dunbar270e2032010-03-31 00:11:27 +000058
John McCall9b7da1c2011-02-15 06:40:56 +000059 /// BitFieldInfo - Holds location and size information about a bit field.
60 llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields;
Daniel Dunbar270e2032010-03-31 00:11:27 +000061
John McCall9b7da1c2011-02-15 06:40:56 +000062 llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases;
63 llvm::DenseMap<const CXXRecordDecl *, unsigned> VirtualBases;
Anders Carlsson46170f92010-11-24 22:50:27 +000064
65 /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are
66 /// primary base classes for some other direct or indirect base class.
67 CXXIndirectPrimaryBaseSet IndirectPrimaryBases;
68
Anders Carlsson1d7dc222010-11-28 19:18:44 +000069 /// LaidOutVirtualBases - A set of all laid out virtual bases, used to avoid
70 /// avoid laying out virtual bases more than once.
71 llvm::SmallPtrSet<const CXXRecordDecl *, 4> LaidOutVirtualBases;
72
John McCallf16aa102010-08-22 21:01:12 +000073 /// IsZeroInitializable - Whether this struct can be C++
74 /// zero-initialized with an LLVM zeroinitializer.
75 bool IsZeroInitializable;
John McCall9b7da1c2011-02-15 06:40:56 +000076 bool IsZeroInitializableAsBase;
Daniel Dunbar270e2032010-03-31 00:11:27 +000077
78 /// Packed - Whether the resulting LLVM struct will be packed or not.
79 bool Packed;
80
81private:
82 CodeGenTypes &Types;
83
84 /// Alignment - Contains the alignment of the RecordDecl.
85 //
86 // FIXME: This is not needed and should be removed.
John McCallfd577d62011-02-15 22:21:29 +000087 CharUnits Alignment;
Daniel Dunbar270e2032010-03-31 00:11:27 +000088
Daniel Dunbar270e2032010-03-31 00:11:27 +000089 /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field,
90 /// this will have the number of bits still available in the field.
91 char BitsAvailableInLastField;
92
John McCallfd577d62011-02-15 22:21:29 +000093 /// NextFieldOffset - Holds the next field offset.
94 CharUnits NextFieldOffset;
Daniel Dunbar270e2032010-03-31 00:11:27 +000095
Anders Carlsson86664462010-04-17 20:49:27 +000096 /// LayoutUnionField - Will layout a field in an union and return the type
97 /// that the field will have.
98 const llvm::Type *LayoutUnionField(const FieldDecl *Field,
99 const ASTRecordLayout &Layout);
100
Daniel Dunbar270e2032010-03-31 00:11:27 +0000101 /// LayoutUnion - Will layout a union RecordDecl.
102 void LayoutUnion(const RecordDecl *D);
103
104 /// LayoutField - try to layout all fields in the record decl.
105 /// Returns false if the operation failed because the struct is not packed.
106 bool LayoutFields(const RecordDecl *D);
107
Anders Carlsson860453c2010-12-04 23:59:48 +0000108 /// Layout a single base, virtual or non-virtual
John McCallfd577d62011-02-15 22:21:29 +0000109 void LayoutBase(const CXXRecordDecl *base,
110 const CGRecordLayout &baseLayout,
111 CharUnits baseOffset);
Anders Carlsson860453c2010-12-04 23:59:48 +0000112
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000113 /// LayoutVirtualBase - layout a single virtual base.
John McCallfd577d62011-02-15 22:21:29 +0000114 void LayoutVirtualBase(const CXXRecordDecl *base,
115 CharUnits baseOffset);
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000116
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000117 /// LayoutVirtualBases - layout the virtual bases of a record decl.
118 void LayoutVirtualBases(const CXXRecordDecl *RD,
119 const ASTRecordLayout &Layout);
120
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000121 /// LayoutNonVirtualBase - layout a single non-virtual base.
John McCallfd577d62011-02-15 22:21:29 +0000122 void LayoutNonVirtualBase(const CXXRecordDecl *base,
123 CharUnits baseOffset);
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000124
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000125 /// LayoutNonVirtualBases - layout the virtual bases of a record decl.
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000126 void LayoutNonVirtualBases(const CXXRecordDecl *RD,
127 const ASTRecordLayout &Layout);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000128
Anders Carlsson3d155e62010-11-09 05:25:47 +0000129 /// ComputeNonVirtualBaseType - Compute the non-virtual base field types.
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000130 bool ComputeNonVirtualBaseType(const CXXRecordDecl *RD);
Anders Carlsson3d155e62010-11-09 05:25:47 +0000131
Daniel Dunbar270e2032010-03-31 00:11:27 +0000132 /// LayoutField - layout a single field. Returns false if the operation failed
133 /// because the current struct is not packed.
134 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
135
136 /// LayoutBitField - layout a single bit field.
137 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
138
139 /// AppendField - Appends a field with the given offset and type.
John McCallfd577d62011-02-15 22:21:29 +0000140 void AppendField(CharUnits fieldOffset, const llvm::Type *FieldTy);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000141
Daniel Dunbar270e2032010-03-31 00:11:27 +0000142 /// AppendPadding - Appends enough padding bytes so that the total
143 /// struct size is a multiple of the field alignment.
John McCallfd577d62011-02-15 22:21:29 +0000144 void AppendPadding(CharUnits fieldOffset, CharUnits fieldAlignment);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000145
Anders Carlsson3d155e62010-11-09 05:25:47 +0000146 /// getByteArrayType - Returns a byte array type with the given number of
147 /// elements.
John McCallfd577d62011-02-15 22:21:29 +0000148 const llvm::Type *getByteArrayType(CharUnits NumBytes);
Anders Carlsson3d155e62010-11-09 05:25:47 +0000149
Daniel Dunbar270e2032010-03-31 00:11:27 +0000150 /// AppendBytes - Append a given number of bytes to the record.
John McCallfd577d62011-02-15 22:21:29 +0000151 void AppendBytes(CharUnits numBytes);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000152
153 /// AppendTailPadding - Append enough tail padding so that the type will have
154 /// the passed size.
155 void AppendTailPadding(uint64_t RecordSize);
156
John McCallfd577d62011-02-15 22:21:29 +0000157 CharUnits getTypeAlignment(const llvm::Type *Ty) const;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000158
Anders Carlssonfc86d552010-11-28 23:06:23 +0000159 /// getAlignmentAsLLVMStruct - Returns the maximum alignment of all the
160 /// LLVM element types.
John McCallfd577d62011-02-15 22:21:29 +0000161 CharUnits getAlignmentAsLLVMStruct() const;
Anders Carlssonfc86d552010-11-28 23:06:23 +0000162
John McCallf16aa102010-08-22 21:01:12 +0000163 /// CheckZeroInitializable - Check if the given type contains a pointer
Daniel Dunbar270e2032010-03-31 00:11:27 +0000164 /// to data member.
John McCallf16aa102010-08-22 21:01:12 +0000165 void CheckZeroInitializable(QualType T);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000166
167public:
168 CGRecordLayoutBuilder(CodeGenTypes &Types)
John McCall9b7da1c2011-02-15 06:40:56 +0000169 : BaseSubobjectType(0),
170 IsZeroInitializable(true), IsZeroInitializableAsBase(true),
John McCallfd577d62011-02-15 22:21:29 +0000171 Packed(false), Types(Types), BitsAvailableInLastField(0) { }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000172
173 /// Layout - Will layout a RecordDecl.
174 void Layout(const RecordDecl *D);
175};
176
177}
Daniel Dunbar270e2032010-03-31 00:11:27 +0000178
Anders Carlsson45372a62009-07-23 03:17:50 +0000179void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
John McCallfd577d62011-02-15 22:21:29 +0000180 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment();
Anders Carlssond0eb3b92009-09-02 17:51:33 +0000181 Packed = D->hasAttr<PackedAttr>();
Anders Carlssona5dd7222009-08-08 19:38:24 +0000182
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000183 if (D->isUnion()) {
184 LayoutUnion(D);
185 return;
186 }
Anders Carlssona860e752009-08-08 18:23:56 +0000187
Anders Carlsson45372a62009-07-23 03:17:50 +0000188 if (LayoutFields(D))
189 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Anders Carlsson45372a62009-07-23 03:17:50 +0000191 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000192 Packed = true;
John McCallfd577d62011-02-15 22:21:29 +0000193 NextFieldOffset = CharUnits::Zero();
Anders Carlsson45372a62009-07-23 03:17:50 +0000194 FieldTypes.clear();
John McCall9b7da1c2011-02-15 06:40:56 +0000195 Fields.clear();
196 BitFields.clear();
197 NonVirtualBases.clear();
198 VirtualBases.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Anders Carlsson45372a62009-07-23 03:17:50 +0000200 LayoutFields(D);
201}
202
Daniel Dunbare7a80bd2010-09-02 23:53:28 +0000203CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
204 const FieldDecl *FD,
205 uint64_t FieldOffset,
206 uint64_t FieldSize,
207 uint64_t ContainingTypeSizeInBits,
208 unsigned ContainingTypeAlign) {
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000209 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(FD->getType());
John McCall92ee7ca2011-02-26 08:41:59 +0000210 CharUnits TypeSizeInBytes =
211 CharUnits::fromQuantity(Types.getTargetData().getTypeAllocSize(Ty));
212 uint64_t TypeSizeInBits = Types.getContext().toBits(TypeSizeInBytes);
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000213
214 bool IsSigned = FD->getType()->isSignedIntegerType();
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000215
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000216 if (FieldSize > TypeSizeInBits) {
Anders Carlsson6ba38152010-04-17 22:54:57 +0000217 // We have a wide bit-field. The extra bits are only used for padding, so
218 // if we have a bitfield of type T, with size N:
219 //
220 // T t : N;
221 //
222 // We can just assume that it's:
223 //
224 // T t : sizeof(T);
225 //
226 FieldSize = TypeSizeInBits;
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000227 }
228
Chris Lattnerd8df5b62011-02-17 22:09:58 +0000229 // in big-endian machines the first fields are in higher bit positions,
230 // so revert the offset. The byte offsets are reversed(back) later.
231 if (Types.getTargetData().isBigEndian()) {
232 FieldOffset = ((ContainingTypeSizeInBits)-FieldOffset-FieldSize);
233 }
234
Daniel Dunbare1467a42010-04-22 02:35:46 +0000235 // Compute the access components. The policy we use is to start by attempting
236 // to access using the width of the bit-field type itself and to always access
237 // at aligned indices of that type. If such an access would fail because it
238 // extends past the bound of the type, then we reduce size to the next smaller
239 // power of two and retry. The current algorithm assumes pow2 sized types,
240 // although this is easy to fix.
241 //
Daniel Dunbare1467a42010-04-22 02:35:46 +0000242 assert(llvm::isPowerOf2_32(TypeSizeInBits) && "Unexpected type size!");
243 CGBitFieldInfo::AccessInfo Components[3];
244 unsigned NumComponents = 0;
245 unsigned AccessedTargetBits = 0; // The tumber of target bits accessed.
246 unsigned AccessWidth = TypeSizeInBits; // The current access width to attempt.
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000247
Daniel Dunbare1467a42010-04-22 02:35:46 +0000248 // Round down from the field offset to find the first access position that is
249 // at an aligned offset of the initial access type.
Daniel Dunbar52968a12010-04-22 15:22:33 +0000250 uint64_t AccessStart = FieldOffset - (FieldOffset % AccessWidth);
251
252 // Adjust initial access size to fit within record.
John McCall92ee7ca2011-02-26 08:41:59 +0000253 while (AccessWidth > Types.getTarget().getCharWidth() &&
Daniel Dunbar52968a12010-04-22 15:22:33 +0000254 AccessStart + AccessWidth > ContainingTypeSizeInBits) {
255 AccessWidth >>= 1;
256 AccessStart = FieldOffset - (FieldOffset % AccessWidth);
257 }
Daniel Dunbar2df25692010-04-15 05:09:32 +0000258
Daniel Dunbare1467a42010-04-22 02:35:46 +0000259 while (AccessedTargetBits < FieldSize) {
260 // Check that we can access using a type of this size, without reading off
261 // the end of the structure. This can occur with packed structures and
262 // -fno-bitfield-type-align, for example.
263 if (AccessStart + AccessWidth > ContainingTypeSizeInBits) {
264 // If so, reduce access size to the next smaller power-of-two and retry.
265 AccessWidth >>= 1;
John McCall92ee7ca2011-02-26 08:41:59 +0000266 assert(AccessWidth >= Types.getTarget().getCharWidth()
267 && "Cannot access under byte size!");
Daniel Dunbare1467a42010-04-22 02:35:46 +0000268 continue;
269 }
Daniel Dunbarab970f92010-04-13 20:58:55 +0000270
Daniel Dunbare1467a42010-04-22 02:35:46 +0000271 // Otherwise, add an access component.
Daniel Dunbarab970f92010-04-13 20:58:55 +0000272
Daniel Dunbare1467a42010-04-22 02:35:46 +0000273 // First, compute the bits inside this access which are part of the
274 // target. We are reading bits [AccessStart, AccessStart + AccessWidth); the
275 // intersection with [FieldOffset, FieldOffset + FieldSize) gives the bits
276 // in the target that we are reading.
Daniel Dunbar52968a12010-04-22 15:22:33 +0000277 assert(FieldOffset < AccessStart + AccessWidth && "Invalid access start!");
278 assert(AccessStart < FieldOffset + FieldSize && "Invalid access start!");
Daniel Dunbare1467a42010-04-22 02:35:46 +0000279 uint64_t AccessBitsInFieldStart = std::max(AccessStart, FieldOffset);
280 uint64_t AccessBitsInFieldSize =
Daniel Dunbar52968a12010-04-22 15:22:33 +0000281 std::min(AccessWidth + AccessStart,
282 FieldOffset + FieldSize) - AccessBitsInFieldStart;
Daniel Dunbar4651efb2010-04-22 14:56:10 +0000283
Daniel Dunbare1467a42010-04-22 02:35:46 +0000284 assert(NumComponents < 3 && "Unexpected number of components!");
285 CGBitFieldInfo::AccessInfo &AI = Components[NumComponents++];
286 AI.FieldIndex = 0;
287 // FIXME: We still follow the old access pattern of only using the field
288 // byte offset. We should switch this once we fix the struct layout to be
289 // pretty.
Chris Lattnerd8df5b62011-02-17 22:09:58 +0000290
291 // on big-endian machines we reverted the bit offset because first fields are
292 // in higher bits. But this also reverts the bytes, so fix this here by reverting
293 // the byte offset on big-endian machines.
294 if (Types.getTargetData().isBigEndian()) {
295 AI.FieldByteOffset = (ContainingTypeSizeInBits - AccessStart - AccessWidth )/8;
296 } else {
297 AI.FieldByteOffset = AccessStart / 8;
298 }
Daniel Dunbare1467a42010-04-22 02:35:46 +0000299 AI.FieldBitStart = AccessBitsInFieldStart - AccessStart;
300 AI.AccessWidth = AccessWidth;
Daniel Dunbar89da8742010-04-22 03:17:04 +0000301 AI.AccessAlignment = llvm::MinAlign(ContainingTypeAlign, AccessStart) / 8;
Daniel Dunbare1467a42010-04-22 02:35:46 +0000302 AI.TargetBitOffset = AccessedTargetBits;
303 AI.TargetBitWidth = AccessBitsInFieldSize;
304
305 AccessStart += AccessWidth;
306 AccessedTargetBits += AI.TargetBitWidth;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000307 }
308
Daniel Dunbare1467a42010-04-22 02:35:46 +0000309 assert(AccessedTargetBits == FieldSize && "Invalid bit-field access!");
Daniel Dunbar2df25692010-04-15 05:09:32 +0000310 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000311}
312
Daniel Dunbare7a80bd2010-09-02 23:53:28 +0000313CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
314 const FieldDecl *FD,
315 uint64_t FieldOffset,
316 uint64_t FieldSize) {
317 const RecordDecl *RD = FD->getParent();
318 const ASTRecordLayout &RL = Types.getContext().getASTRecordLayout(RD);
Ken Dyckdd76a9a2011-02-11 01:54:29 +0000319 uint64_t ContainingTypeSizeInBits = Types.getContext().toBits(RL.getSize());
Ken Dyckdac54c12011-02-15 02:32:40 +0000320 unsigned ContainingTypeAlign = Types.getContext().toBits(RL.getAlignment());
Daniel Dunbare7a80bd2010-09-02 23:53:28 +0000321
322 return MakeInfo(Types, FD, FieldOffset, FieldSize, ContainingTypeSizeInBits,
323 ContainingTypeAlign);
324}
325
Anders Carlsson45372a62009-07-23 03:17:50 +0000326void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
John McCallfd577d62011-02-15 22:21:29 +0000327 uint64_t fieldOffset) {
328 uint64_t fieldSize =
Anders Carlsson45372a62009-07-23 03:17:50 +0000329 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000330
John McCallfd577d62011-02-15 22:21:29 +0000331 if (fieldSize == 0)
Anders Carlsson45372a62009-07-23 03:17:50 +0000332 return;
333
John McCall92ee7ca2011-02-26 08:41:59 +0000334 uint64_t nextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset);
John McCallfd577d62011-02-15 22:21:29 +0000335 unsigned numBytesToAppend;
Mike Stump1eb44332009-09-09 15:08:12 +0000336
John McCallfd577d62011-02-15 22:21:29 +0000337 if (fieldOffset < nextFieldOffsetInBits) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000338 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
John McCallfd577d62011-02-15 22:21:29 +0000339 assert(!NextFieldOffset.isZero() && "Must have laid out at least one byte");
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Anders Carlsson45372a62009-07-23 03:17:50 +0000341 // The bitfield begins in the previous bit-field.
John McCallfd577d62011-02-15 22:21:29 +0000342 numBytesToAppend =
343 llvm::RoundUpToAlignment(fieldSize - BitsAvailableInLastField, 8) / 8;
Anders Carlsson45372a62009-07-23 03:17:50 +0000344 } else {
John McCallfd577d62011-02-15 22:21:29 +0000345 assert(fieldOffset % 8 == 0 && "Field offset not aligned correctly");
Anders Carlsson45372a62009-07-23 03:17:50 +0000346
347 // Append padding if necessary.
John McCallfd577d62011-02-15 22:21:29 +0000348 AppendPadding(CharUnits::fromQuantity(fieldOffset / 8), CharUnits::One());
Mike Stump1eb44332009-09-09 15:08:12 +0000349
John McCallfd577d62011-02-15 22:21:29 +0000350 numBytesToAppend = llvm::RoundUpToAlignment(fieldSize, 8) / 8;
Mike Stump1eb44332009-09-09 15:08:12 +0000351
John McCallfd577d62011-02-15 22:21:29 +0000352 assert(numBytesToAppend && "No bytes to append!");
Anders Carlsson45372a62009-07-23 03:17:50 +0000353 }
354
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000355 // Add the bit field info.
John McCall9b7da1c2011-02-15 06:40:56 +0000356 BitFields.insert(std::make_pair(D,
John McCallfd577d62011-02-15 22:21:29 +0000357 CGBitFieldInfo::MakeInfo(Types, D, fieldOffset, fieldSize)));
Mike Stump1eb44332009-09-09 15:08:12 +0000358
John McCallfd577d62011-02-15 22:21:29 +0000359 AppendBytes(CharUnits::fromQuantity(numBytesToAppend));
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Mike Stump1eb44332009-09-09 15:08:12 +0000361 BitsAvailableInLastField =
John McCallfd577d62011-02-15 22:21:29 +0000362 NextFieldOffset.getQuantity() * 8 - (fieldOffset + fieldSize);
Anders Carlsson45372a62009-07-23 03:17:50 +0000363}
364
365bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
John McCallfd577d62011-02-15 22:21:29 +0000366 uint64_t fieldOffset) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000367 // If the field is packed, then we need a packed struct.
Anders Carlssona860e752009-08-08 18:23:56 +0000368 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson45372a62009-07-23 03:17:50 +0000369 return false;
370
371 if (D->isBitField()) {
372 // We must use packed structs for unnamed bit fields since they
373 // don't affect the struct alignment.
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000374 if (!Packed && !D->getDeclName())
Anders Carlsson45372a62009-07-23 03:17:50 +0000375 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000376
John McCallfd577d62011-02-15 22:21:29 +0000377 LayoutBitField(D, fieldOffset);
Anders Carlsson45372a62009-07-23 03:17:50 +0000378 return true;
379 }
Mike Stump1eb44332009-09-09 15:08:12 +0000380
John McCallf16aa102010-08-22 21:01:12 +0000381 CheckZeroInitializable(D->getType());
Daniel Dunbar270e2032010-03-31 00:11:27 +0000382
John McCall92ee7ca2011-02-26 08:41:59 +0000383 assert(fieldOffset % Types.getTarget().getCharWidth() == 0
384 && "field offset is not on a byte boundary!");
385 CharUnits fieldOffsetInBytes
386 = Types.getContext().toCharUnitsFromBits(fieldOffset);
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000388 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
John McCallfd577d62011-02-15 22:21:29 +0000389 CharUnits typeAlignment = getTypeAlignment(Ty);
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000390
Anders Carlssona5dd7222009-08-08 19:38:24 +0000391 // If the type alignment is larger then the struct alignment, we must use
392 // a packed struct.
John McCallfd577d62011-02-15 22:21:29 +0000393 if (typeAlignment > Alignment) {
Anders Carlssona5dd7222009-08-08 19:38:24 +0000394 assert(!Packed && "Alignment is wrong even with packed struct!");
395 return false;
396 }
Mike Stump1eb44332009-09-09 15:08:12 +0000397
John McCallfd577d62011-02-15 22:21:29 +0000398 if (!Packed) {
399 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
400 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
401 if (const MaxFieldAlignmentAttr *MFAA =
402 RD->getAttr<MaxFieldAlignmentAttr>()) {
John McCall92ee7ca2011-02-26 08:41:59 +0000403 if (MFAA->getAlignment() != Types.getContext().toBits(typeAlignment))
John McCallfd577d62011-02-15 22:21:29 +0000404 return false;
405 }
Anders Carlssona5dd7222009-08-08 19:38:24 +0000406 }
407 }
408
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000409 // Round up the field offset to the alignment of the field type.
John McCallfd577d62011-02-15 22:21:29 +0000410 CharUnits alignedNextFieldOffsetInBytes =
411 NextFieldOffset.RoundUpToAlignment(typeAlignment);
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000412
John McCallfd577d62011-02-15 22:21:29 +0000413 if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000414 assert(!Packed && "Could not place field even with packed struct!");
415 return false;
416 }
Mike Stump1eb44332009-09-09 15:08:12 +0000417
John McCallfd577d62011-02-15 22:21:29 +0000418 AppendPadding(fieldOffsetInBytes, typeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Anders Carlsson45372a62009-07-23 03:17:50 +0000420 // Now append the field.
John McCall9b7da1c2011-02-15 06:40:56 +0000421 Fields[D] = FieldTypes.size();
John McCallfd577d62011-02-15 22:21:29 +0000422 AppendField(fieldOffsetInBytes, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Anders Carlsson45372a62009-07-23 03:17:50 +0000424 return true;
425}
426
Anders Carlsson86664462010-04-17 20:49:27 +0000427const llvm::Type *
428CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
429 const ASTRecordLayout &Layout) {
430 if (Field->isBitField()) {
431 uint64_t FieldSize =
432 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
433
434 // Ignore zero sized bit fields.
435 if (FieldSize == 0)
436 return 0;
437
Daniel Dunbar8ab78a72010-04-20 17:52:30 +0000438 const llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
439 unsigned NumBytesToAppend =
440 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Anders Carlssond62328e2010-04-17 21:04:52 +0000441
Daniel Dunbar8ab78a72010-04-20 17:52:30 +0000442 if (NumBytesToAppend > 1)
443 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend);
Anders Carlssond62328e2010-04-17 21:04:52 +0000444
Anders Carlsson86664462010-04-17 20:49:27 +0000445 // Add the bit field info.
John McCall9b7da1c2011-02-15 06:40:56 +0000446 BitFields.insert(std::make_pair(Field,
447 CGBitFieldInfo::MakeInfo(Types, Field, 0, FieldSize)));
Anders Carlssond62328e2010-04-17 21:04:52 +0000448 return FieldTy;
Anders Carlsson86664462010-04-17 20:49:27 +0000449 }
Daniel Dunbar8ab78a72010-04-20 17:52:30 +0000450
Anders Carlsson86664462010-04-17 20:49:27 +0000451 // This is a regular union field.
John McCall9b7da1c2011-02-15 06:40:56 +0000452 Fields[Field] = 0;
Anders Carlsson86664462010-04-17 20:49:27 +0000453 return Types.ConvertTypeForMemRecursive(Field->getType());
454}
455
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000456void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
457 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump1eb44332009-09-09 15:08:12 +0000458
John McCallfd577d62011-02-15 22:21:29 +0000459 const ASTRecordLayout &layout = Types.getContext().getASTRecordLayout(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000460
John McCallfd577d62011-02-15 22:21:29 +0000461 const llvm::Type *unionType = 0;
462 CharUnits unionSize = CharUnits::Zero();
463 CharUnits unionAlign = CharUnits::Zero();
Mike Stump1eb44332009-09-09 15:08:12 +0000464
John McCallfd577d62011-02-15 22:21:29 +0000465 bool hasOnlyZeroSizedBitFields = true;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000466
John McCallfd577d62011-02-15 22:21:29 +0000467 unsigned fieldNo = 0;
468 for (RecordDecl::field_iterator field = D->field_begin(),
469 fieldEnd = D->field_end(); field != fieldEnd; ++field, ++fieldNo) {
470 assert(layout.getFieldOffset(fieldNo) == 0 &&
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000471 "Union field offset did not start at the beginning of record!");
John McCallfd577d62011-02-15 22:21:29 +0000472 const llvm::Type *fieldType = LayoutUnionField(*field, layout);
Anders Carlsson2cc8f172009-07-23 04:00:39 +0000473
John McCallfd577d62011-02-15 22:21:29 +0000474 if (!fieldType)
Anders Carlsson86664462010-04-17 20:49:27 +0000475 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000476
John McCallfd577d62011-02-15 22:21:29 +0000477 hasOnlyZeroSizedBitFields = false;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000478
John McCallfd577d62011-02-15 22:21:29 +0000479 CharUnits fieldAlign = CharUnits::fromQuantity(
480 Types.getTargetData().getABITypeAlignment(fieldType));
481 CharUnits fieldSize = CharUnits::fromQuantity(
482 Types.getTargetData().getTypeAllocSize(fieldType));
Mike Stump1eb44332009-09-09 15:08:12 +0000483
John McCallfd577d62011-02-15 22:21:29 +0000484 if (fieldAlign < unionAlign)
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000485 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000486
John McCallfd577d62011-02-15 22:21:29 +0000487 if (fieldAlign > unionAlign || fieldSize > unionSize) {
488 unionType = fieldType;
489 unionAlign = fieldAlign;
490 unionSize = fieldSize;
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000491 }
492 }
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000494 // Now add our field.
John McCallfd577d62011-02-15 22:21:29 +0000495 if (unionType) {
496 AppendField(CharUnits::Zero(), unionType);
Anders Carlsson36620002009-09-03 22:56:02 +0000497
John McCallfd577d62011-02-15 22:21:29 +0000498 if (getTypeAlignment(unionType) > layout.getAlignment()) {
Anders Carlsson36620002009-09-03 22:56:02 +0000499 // We need a packed struct.
500 Packed = true;
John McCallfd577d62011-02-15 22:21:29 +0000501 unionAlign = CharUnits::One();
Anders Carlsson36620002009-09-03 22:56:02 +0000502 }
503 }
John McCallfd577d62011-02-15 22:21:29 +0000504 if (unionAlign.isZero()) {
505 assert(hasOnlyZeroSizedBitFields &&
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000506 "0-align record did not have all zero-sized bit-fields!");
John McCallfd577d62011-02-15 22:21:29 +0000507 unionAlign = CharUnits::One();
Fariborz Jahaniane5041702009-11-06 20:47:40 +0000508 }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000509
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000510 // Append tail padding.
John McCallfd577d62011-02-15 22:21:29 +0000511 CharUnits recordSize = layout.getSize();
512 if (recordSize > unionSize)
513 AppendPadding(recordSize, unionAlign);
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000514}
515
John McCall9b7da1c2011-02-15 06:40:56 +0000516void CGRecordLayoutBuilder::LayoutBase(const CXXRecordDecl *base,
John McCallfd577d62011-02-15 22:21:29 +0000517 const CGRecordLayout &baseLayout,
518 CharUnits baseOffset) {
519 AppendPadding(baseOffset, CharUnits::One());
Anders Carlsson860453c2010-12-04 23:59:48 +0000520
John McCall9b7da1c2011-02-15 06:40:56 +0000521 const ASTRecordLayout &baseASTLayout
522 = Types.getContext().getASTRecordLayout(base);
523
John McCallfd577d62011-02-15 22:21:29 +0000524 // Fields and bases can be laid out in the tail padding of previous
525 // bases. If this happens, we need to allocate the base as an i8
526 // array; otherwise, we can use the subobject type. However,
527 // actually doing that would require knowledge of what immediately
528 // follows this base in the layout, so instead we do a conservative
529 // approximation, which is to use the base subobject type if it
530 // has the same LLVM storage size as the nvsize.
531
532 // The nvsize, i.e. the unpadded size of the base class.
533 CharUnits nvsize = baseASTLayout.getNonVirtualSize();
534
535#if 0
536 const llvm::StructType *subobjectType = baseLayout.getBaseSubobjectLLVMType();
537 const llvm::StructLayout *baseLLVMLayout =
538 Types.getTargetData().getStructLayout(subobjectType);
539 CharUnits stsize = CharUnits::fromQuantity(baseLLVMLayout->getSizeInBytes());
540
541 if (nvsize == stsize)
542 AppendField(baseOffset, subobjectType);
543 else
544#endif
545 AppendBytes(nvsize);
John McCall9b7da1c2011-02-15 06:40:56 +0000546}
547
548void CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *base,
John McCallfd577d62011-02-15 22:21:29 +0000549 CharUnits baseOffset) {
John McCall9b7da1c2011-02-15 06:40:56 +0000550 // Ignore empty bases.
551 if (base->isEmpty()) return;
552
553 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
554 if (IsZeroInitializableAsBase) {
555 assert(IsZeroInitializable &&
556 "class zero-initializable as base but not as complete object");
557
558 IsZeroInitializable = IsZeroInitializableAsBase =
559 baseLayout.isZeroInitializableAsBase();
560 }
561
John McCallfd577d62011-02-15 22:21:29 +0000562 LayoutBase(base, baseLayout, baseOffset);
John McCall9b7da1c2011-02-15 06:40:56 +0000563 NonVirtualBases[base] = (FieldTypes.size() - 1);
Anders Carlsson860453c2010-12-04 23:59:48 +0000564}
565
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000566void
John McCall9b7da1c2011-02-15 06:40:56 +0000567CGRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *base,
John McCallfd577d62011-02-15 22:21:29 +0000568 CharUnits baseOffset) {
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000569 // Ignore empty bases.
John McCall9b7da1c2011-02-15 06:40:56 +0000570 if (base->isEmpty()) return;
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000571
John McCall9b7da1c2011-02-15 06:40:56 +0000572 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
573 if (IsZeroInitializable)
574 IsZeroInitializable = baseLayout.isZeroInitializableAsBase();
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000575
John McCallfd577d62011-02-15 22:21:29 +0000576 LayoutBase(base, baseLayout, baseOffset);
John McCall9b7da1c2011-02-15 06:40:56 +0000577 VirtualBases[base] = (FieldTypes.size() - 1);
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000578}
579
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000580/// LayoutVirtualBases - layout the non-virtual bases of a record decl.
581void
582CGRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
583 const ASTRecordLayout &Layout) {
584 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
585 E = RD->bases_end(); I != E; ++I) {
586 const CXXRecordDecl *BaseDecl =
587 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
588
589 // We only want to lay out virtual bases that aren't indirect primary bases
590 // of some other base.
591 if (I->isVirtual() && !IndirectPrimaryBases.count(BaseDecl)) {
592 // Only lay out the base once.
593 if (!LaidOutVirtualBases.insert(BaseDecl))
594 continue;
595
John McCallfd577d62011-02-15 22:21:29 +0000596 CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl);
597 LayoutVirtualBase(BaseDecl, vbaseOffset);
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000598 }
599
600 if (!BaseDecl->getNumVBases()) {
601 // This base isn't interesting since it doesn't have any virtual bases.
602 continue;
603 }
604
605 LayoutVirtualBases(BaseDecl, Layout);
606 }
607}
608
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000609void
610CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD,
611 const ASTRecordLayout &Layout) {
612 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
613
614 // Check if we need to add a vtable pointer.
615 if (RD->isDynamicClass()) {
616 if (!PrimaryBase) {
617 const llvm::Type *FunctionType =
618 llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()),
619 /*isVarArg=*/true);
620 const llvm::Type *VTableTy = FunctionType->getPointerTo();
621
John McCallfd577d62011-02-15 22:21:29 +0000622 assert(NextFieldOffset.isZero() &&
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000623 "VTable pointer must come first!");
John McCallfd577d62011-02-15 22:21:29 +0000624 AppendField(CharUnits::Zero(), VTableTy->getPointerTo());
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000625 } else {
Anders Carlssonc9e814b2010-11-24 23:12:57 +0000626 if (!Layout.isPrimaryBaseVirtual())
John McCallfd577d62011-02-15 22:21:29 +0000627 LayoutNonVirtualBase(PrimaryBase, CharUnits::Zero());
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000628 else
John McCallfd577d62011-02-15 22:21:29 +0000629 LayoutVirtualBase(PrimaryBase, CharUnits::Zero());
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000630 }
631 }
632
633 // Layout the non-virtual bases.
634 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
635 E = RD->bases_end(); I != E; ++I) {
636 if (I->isVirtual())
637 continue;
638
639 const CXXRecordDecl *BaseDecl =
640 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
641
642 // We've already laid out the primary base.
Anders Carlssonc9e814b2010-11-24 23:12:57 +0000643 if (BaseDecl == PrimaryBase && !Layout.isPrimaryBaseVirtual())
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000644 continue;
645
John McCallfd577d62011-02-15 22:21:29 +0000646 LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffset(BaseDecl));
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000647 }
648}
649
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000650bool
Anders Carlsson3d155e62010-11-09 05:25:47 +0000651CGRecordLayoutBuilder::ComputeNonVirtualBaseType(const CXXRecordDecl *RD) {
652 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(RD);
653
Ken Dyck68cf1a52011-02-08 02:02:47 +0000654 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
655 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
John McCallfd577d62011-02-15 22:21:29 +0000656 CharUnits AlignedNonVirtualTypeSize =
657 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
Anders Carlsson3d155e62010-11-09 05:25:47 +0000658
Anders Carlsson3d155e62010-11-09 05:25:47 +0000659 // First check if we can use the same fields as for the complete class.
John McCallfd577d62011-02-15 22:21:29 +0000660 CharUnits RecordSize = Layout.getSize();
John McCall9b7da1c2011-02-15 06:40:56 +0000661 if (AlignedNonVirtualTypeSize == RecordSize)
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000662 return true;
Anders Carlsson3d155e62010-11-09 05:25:47 +0000663
Anders Carlsson3d155e62010-11-09 05:25:47 +0000664 // Check if we need padding.
John McCallfd577d62011-02-15 22:21:29 +0000665 CharUnits AlignedNextFieldOffset =
666 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
Anders Carlsson3d155e62010-11-09 05:25:47 +0000667
John McCall9b7da1c2011-02-15 06:40:56 +0000668 if (AlignedNextFieldOffset > AlignedNonVirtualTypeSize) {
669 assert(!Packed && "cannot layout even as packed struct");
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000670 return false; // Needs packing.
Anders Carlsson3d155e62010-11-09 05:25:47 +0000671 }
672
John McCall9b7da1c2011-02-15 06:40:56 +0000673 bool needsPadding = (AlignedNonVirtualTypeSize != AlignedNextFieldOffset);
674 if (needsPadding) {
John McCallfd577d62011-02-15 22:21:29 +0000675 CharUnits NumBytes = AlignedNonVirtualTypeSize - AlignedNextFieldOffset;
John McCall9b7da1c2011-02-15 06:40:56 +0000676 FieldTypes.push_back(getByteArrayType(NumBytes));
677 }
678
679 BaseSubobjectType = llvm::StructType::get(Types.getLLVMContext(),
680 FieldTypes, Packed);
681
682 if (needsPadding) {
683 // Pull the padding back off.
684 FieldTypes.pop_back();
685 }
686
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000687 return true;
Anders Carlsson3d155e62010-11-09 05:25:47 +0000688}
689
Anders Carlsson45372a62009-07-23 03:17:50 +0000690bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
691 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
John McCallfd577d62011-02-15 22:21:29 +0000692 assert(!Alignment.isZero() && "Did not set alignment!");
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000694 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000695
Anders Carlsson3d155e62010-11-09 05:25:47 +0000696 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
697 if (RD)
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000698 LayoutNonVirtualBases(RD, Layout);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000699
Anders Carlsson45372a62009-07-23 03:17:50 +0000700 unsigned FieldNo = 0;
Fariborz Jahaniancad86652009-07-27 20:57:45 +0000701
Mike Stump1eb44332009-09-09 15:08:12 +0000702 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson45372a62009-07-23 03:17:50 +0000703 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
704 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000705 assert(!Packed &&
Anders Carlsson45372a62009-07-23 03:17:50 +0000706 "Could not layout fields even with a packed LLVM struct!");
707 return false;
708 }
709 }
710
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000711 if (RD) {
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000712 // We've laid out the non-virtual bases and the fields, now compute the
713 // non-virtual base field types.
Argyrios Kyrtzidisdb2b42f2010-12-10 00:11:00 +0000714 if (!ComputeNonVirtualBaseType(RD)) {
715 assert(!Packed && "Could not layout even with a packed LLVM struct!");
716 return false;
717 }
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000718
Anders Carlsson1d7dc222010-11-28 19:18:44 +0000719 // And lay out the virtual bases.
720 RD->getIndirectPrimaryBases(IndirectPrimaryBases);
721 if (Layout.isPrimaryBaseVirtual())
722 IndirectPrimaryBases.insert(Layout.getPrimaryBase());
723 LayoutVirtualBases(RD, Layout);
724 }
Anders Carlsson3d155e62010-11-09 05:25:47 +0000725
Anders Carlsson45372a62009-07-23 03:17:50 +0000726 // Append tail padding if necessary.
Ken Dyckdd76a9a2011-02-11 01:54:29 +0000727 AppendTailPadding(Types.getContext().toBits(Layout.getSize()));
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Anders Carlsson45372a62009-07-23 03:17:50 +0000729 return true;
730}
731
Anders Carlssonc1efe362009-07-27 14:55:54 +0000732void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) {
733 assert(RecordSize % 8 == 0 && "Invalid record size!");
Mike Stump1eb44332009-09-09 15:08:12 +0000734
John McCall92ee7ca2011-02-26 08:41:59 +0000735 CharUnits RecordSizeInBytes =
736 Types.getContext().toCharUnitsFromBits(RecordSize);
John McCallfd577d62011-02-15 22:21:29 +0000737 assert(NextFieldOffset <= RecordSizeInBytes && "Size mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000738
John McCallfd577d62011-02-15 22:21:29 +0000739 CharUnits AlignedNextFieldOffset =
740 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
Anders Carlssonc2456822009-12-08 01:24:23 +0000741
742 if (AlignedNextFieldOffset == RecordSizeInBytes) {
743 // We don't need any padding.
744 return;
745 }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000746
John McCallfd577d62011-02-15 22:21:29 +0000747 CharUnits NumPadBytes = RecordSizeInBytes - NextFieldOffset;
Anders Carlssonc1efe362009-07-27 14:55:54 +0000748 AppendBytes(NumPadBytes);
749}
750
John McCallfd577d62011-02-15 22:21:29 +0000751void CGRecordLayoutBuilder::AppendField(CharUnits fieldOffset,
752 const llvm::Type *fieldType) {
753 CharUnits fieldSize =
754 CharUnits::fromQuantity(Types.getTargetData().getTypeAllocSize(fieldType));
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000755
John McCallfd577d62011-02-15 22:21:29 +0000756 FieldTypes.push_back(fieldType);
Anders Carlsson45372a62009-07-23 03:17:50 +0000757
John McCallfd577d62011-02-15 22:21:29 +0000758 NextFieldOffset = fieldOffset + fieldSize;
Anders Carlsson45372a62009-07-23 03:17:50 +0000759 BitsAvailableInLastField = 0;
760}
761
John McCallfd577d62011-02-15 22:21:29 +0000762void CGRecordLayoutBuilder::AppendPadding(CharUnits fieldOffset,
763 CharUnits fieldAlignment) {
764 assert(NextFieldOffset <= fieldOffset &&
Anders Carlsson45372a62009-07-23 03:17:50 +0000765 "Incorrect field layout!");
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Anders Carlsson45372a62009-07-23 03:17:50 +0000767 // Round up the field offset to the alignment of the field type.
John McCallfd577d62011-02-15 22:21:29 +0000768 CharUnits alignedNextFieldOffset =
769 NextFieldOffset.RoundUpToAlignment(fieldAlignment);
Anders Carlsson45372a62009-07-23 03:17:50 +0000770
John McCallfd577d62011-02-15 22:21:29 +0000771 if (alignedNextFieldOffset < fieldOffset) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000772 // Even with alignment, the field offset is not at the right place,
773 // insert padding.
John McCallfd577d62011-02-15 22:21:29 +0000774 CharUnits padding = fieldOffset - NextFieldOffset;
Anders Carlsson45372a62009-07-23 03:17:50 +0000775
John McCallfd577d62011-02-15 22:21:29 +0000776 AppendBytes(padding);
Anders Carlsson45372a62009-07-23 03:17:50 +0000777 }
778}
779
John McCallfd577d62011-02-15 22:21:29 +0000780const llvm::Type *CGRecordLayoutBuilder::getByteArrayType(CharUnits numBytes) {
781 assert(!numBytes.isZero() && "Empty byte arrays aren't allowed.");
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Owen Anderson0032b272009-08-13 21:57:51 +0000783 const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
John McCallfd577d62011-02-15 22:21:29 +0000784 if (numBytes > CharUnits::One())
785 Ty = llvm::ArrayType::get(Ty, numBytes.getQuantity());
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Anders Carlsson3d155e62010-11-09 05:25:47 +0000787 return Ty;
788}
789
John McCallfd577d62011-02-15 22:21:29 +0000790void CGRecordLayoutBuilder::AppendBytes(CharUnits numBytes) {
791 if (numBytes.isZero())
Anders Carlsson3d155e62010-11-09 05:25:47 +0000792 return;
793
Anders Carlsson45372a62009-07-23 03:17:50 +0000794 // Append the padding field
John McCallfd577d62011-02-15 22:21:29 +0000795 AppendField(NextFieldOffset, getByteArrayType(numBytes));
Anders Carlsson45372a62009-07-23 03:17:50 +0000796}
797
John McCallfd577d62011-02-15 22:21:29 +0000798CharUnits CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000799 if (Packed)
John McCallfd577d62011-02-15 22:21:29 +0000800 return CharUnits::One();
Mike Stump1eb44332009-09-09 15:08:12 +0000801
John McCallfd577d62011-02-15 22:21:29 +0000802 return CharUnits::fromQuantity(Types.getTargetData().getABITypeAlignment(Ty));
Anders Carlsson45372a62009-07-23 03:17:50 +0000803}
804
John McCallfd577d62011-02-15 22:21:29 +0000805CharUnits CGRecordLayoutBuilder::getAlignmentAsLLVMStruct() const {
Anders Carlssonfc86d552010-11-28 23:06:23 +0000806 if (Packed)
John McCallfd577d62011-02-15 22:21:29 +0000807 return CharUnits::One();
Anders Carlssonfc86d552010-11-28 23:06:23 +0000808
John McCallfd577d62011-02-15 22:21:29 +0000809 CharUnits maxAlignment = CharUnits::One();
Anders Carlssonfc86d552010-11-28 23:06:23 +0000810 for (size_t i = 0; i != FieldTypes.size(); ++i)
John McCallfd577d62011-02-15 22:21:29 +0000811 maxAlignment = std::max(maxAlignment, getTypeAlignment(FieldTypes[i]));
Anders Carlssonfc86d552010-11-28 23:06:23 +0000812
John McCallfd577d62011-02-15 22:21:29 +0000813 return maxAlignment;
Anders Carlssonfc86d552010-11-28 23:06:23 +0000814}
815
John McCall9b7da1c2011-02-15 06:40:56 +0000816/// Merge in whether a field of the given type is zero-initializable.
John McCallf16aa102010-08-22 21:01:12 +0000817void CGRecordLayoutBuilder::CheckZeroInitializable(QualType T) {
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000818 // This record already contains a member pointer.
John McCall9b7da1c2011-02-15 06:40:56 +0000819 if (!IsZeroInitializableAsBase)
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000820 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000822 // Can only have member pointers if we're compiling C++.
823 if (!Types.getContext().getLangOptions().CPlusPlus)
824 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000825
John McCall9b7da1c2011-02-15 06:40:56 +0000826 const Type *elementType = T->getBaseElementTypeUnsafe();
Mike Stump1eb44332009-09-09 15:08:12 +0000827
John McCall9b7da1c2011-02-15 06:40:56 +0000828 if (const MemberPointerType *MPT = elementType->getAs<MemberPointerType>()) {
John McCallf16aa102010-08-22 21:01:12 +0000829 if (!Types.getCXXABI().isZeroInitializable(MPT))
John McCall9b7da1c2011-02-15 06:40:56 +0000830 IsZeroInitializable = IsZeroInitializableAsBase = false;
831 } else if (const RecordType *RT = elementType->getAs<RecordType>()) {
Anders Carlsson2c12d032010-02-02 05:17:25 +0000832 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
John McCall9b7da1c2011-02-15 06:40:56 +0000833 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
834 if (!Layout.isZeroInitializable())
835 IsZeroInitializable = IsZeroInitializableAsBase = false;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000836 }
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000837}
838
Daniel Dunbar270e2032010-03-31 00:11:27 +0000839CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) {
840 CGRecordLayoutBuilder Builder(*this);
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Anders Carlsson45372a62009-07-23 03:17:50 +0000842 Builder.Layout(D);
Anders Carlsson4c98efd2009-07-24 15:20:52 +0000843
Anders Carlssonba2c2ee2010-11-24 19:37:16 +0000844 const llvm::StructType *Ty = llvm::StructType::get(getLLVMContext(),
845 Builder.FieldTypes,
846 Builder.Packed);
Mike Stump1eb44332009-09-09 15:08:12 +0000847
John McCall9b7da1c2011-02-15 06:40:56 +0000848 // If we're in C++, compute the base subobject type.
Anders Carlssonba2c2ee2010-11-24 19:37:16 +0000849 const llvm::StructType *BaseTy = 0;
Anders Carlsson3d155e62010-11-09 05:25:47 +0000850 if (isa<CXXRecordDecl>(D)) {
John McCall9b7da1c2011-02-15 06:40:56 +0000851 BaseTy = Builder.BaseSubobjectType;
852 if (!BaseTy) BaseTy = Ty;
Anders Carlsson3d155e62010-11-09 05:25:47 +0000853 }
854
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000855 CGRecordLayout *RL =
John McCall9b7da1c2011-02-15 06:40:56 +0000856 new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable,
857 Builder.IsZeroInitializableAsBase);
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000858
John McCall9b7da1c2011-02-15 06:40:56 +0000859 RL->NonVirtualBases.swap(Builder.NonVirtualBases);
860 RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases);
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000861
Anders Carlsson45372a62009-07-23 03:17:50 +0000862 // Add all the field numbers.
John McCall9b7da1c2011-02-15 06:40:56 +0000863 RL->FieldInfo.swap(Builder.Fields);
Anders Carlsson45372a62009-07-23 03:17:50 +0000864
865 // Add bitfield info.
John McCall9b7da1c2011-02-15 06:40:56 +0000866 RL->BitFields.swap(Builder.BitFields);
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000868 // Dump the layout, if requested.
Daniel Dunbarab970f92010-04-13 20:58:55 +0000869 if (getContext().getLangOptions().DumpRecordLayouts) {
Daniel Dunbar8d8ab742010-04-19 20:44:53 +0000870 llvm::errs() << "\n*** Dumping IRgen Record Layout\n";
Daniel Dunbarab970f92010-04-13 20:58:55 +0000871 llvm::errs() << "Record: ";
872 D->dump();
873 llvm::errs() << "\nLayout: ";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000874 RL->dump();
Daniel Dunbarab970f92010-04-13 20:58:55 +0000875 }
Daniel Dunbar93c62962010-04-12 18:14:18 +0000876
Daniel Dunbare1467a42010-04-22 02:35:46 +0000877#ifndef NDEBUG
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000878 // Verify that the computed LLVM struct size matches the AST layout size.
Anders Carlsson3d155e62010-11-09 05:25:47 +0000879 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
880
Ken Dyckdd76a9a2011-02-11 01:54:29 +0000881 uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize());
Daniel Dunbare1467a42010-04-22 02:35:46 +0000882 assert(TypeSizeInBits == getTargetData().getTypeAllocSizeInBits(Ty) &&
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000883 "Type size mismatch!");
884
Anders Carlsson3d155e62010-11-09 05:25:47 +0000885 if (BaseTy) {
Ken Dyck68cf1a52011-02-08 02:02:47 +0000886 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
887 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
888 CharUnits AlignedNonVirtualTypeSize =
889 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
890
891 uint64_t AlignedNonVirtualTypeSizeInBits =
Ken Dyckdd76a9a2011-02-11 01:54:29 +0000892 getContext().toBits(AlignedNonVirtualTypeSize);
Anders Carlsson3d155e62010-11-09 05:25:47 +0000893
894 assert(AlignedNonVirtualTypeSizeInBits ==
895 getTargetData().getTypeAllocSizeInBits(BaseTy) &&
896 "Type size mismatch!");
897 }
898
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000899 // Verify that the LLVM and AST field offsets agree.
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000900 const llvm::StructType *ST =
901 dyn_cast<llvm::StructType>(RL->getLLVMType());
902 const llvm::StructLayout *SL = getTargetData().getStructLayout(ST);
903
904 const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
905 RecordDecl::field_iterator it = D->field_begin();
906 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
907 const FieldDecl *FD = *it;
Daniel Dunbare1467a42010-04-22 02:35:46 +0000908
909 // For non-bit-fields, just check that the LLVM struct offset matches the
910 // AST offset.
911 if (!FD->isBitField()) {
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000912 unsigned FieldNo = RL->getLLVMFieldNo(FD);
913 assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
914 "Invalid field offset!");
Daniel Dunbare1467a42010-04-22 02:35:46 +0000915 continue;
916 }
917
918 // Ignore unnamed bit-fields.
919 if (!FD->getDeclName())
920 continue;
921
922 const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
923 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
924 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
925
926 // Verify that every component access is within the structure.
927 uint64_t FieldOffset = SL->getElementOffsetInBits(AI.FieldIndex);
John McCall92ee7ca2011-02-26 08:41:59 +0000928 uint64_t AccessBitOffset = FieldOffset +
929 getContext().toBits(CharUnits::fromQuantity(AI.FieldByteOffset));
Daniel Dunbare1467a42010-04-22 02:35:46 +0000930 assert(AccessBitOffset + AI.AccessWidth <= TypeSizeInBits &&
931 "Invalid bit-field access (out of range)!");
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000932 }
933 }
934#endif
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000935
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000936 return RL;
Anders Carlsson45372a62009-07-23 03:17:50 +0000937}
Daniel Dunbar93c62962010-04-12 18:14:18 +0000938
939void CGRecordLayout::print(llvm::raw_ostream &OS) const {
940 OS << "<CGRecordLayout\n";
John McCall9b7da1c2011-02-15 06:40:56 +0000941 OS << " LLVMType:" << *CompleteObjectType << "\n";
942 if (BaseSubobjectType)
943 OS << " NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n";
John McCallf16aa102010-08-22 21:01:12 +0000944 OS << " IsZeroInitializable:" << IsZeroInitializable << "\n";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000945 OS << " BitFields:[\n";
Daniel Dunbarad759532010-04-22 02:35:36 +0000946
947 // Print bit-field infos in declaration order.
948 std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
Daniel Dunbar93c62962010-04-12 18:14:18 +0000949 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
950 it = BitFields.begin(), ie = BitFields.end();
951 it != ie; ++it) {
Daniel Dunbarad759532010-04-22 02:35:36 +0000952 const RecordDecl *RD = it->first->getParent();
953 unsigned Index = 0;
954 for (RecordDecl::field_iterator
955 it2 = RD->field_begin(); *it2 != it->first; ++it2)
956 ++Index;
957 BFIs.push_back(std::make_pair(Index, &it->second));
958 }
959 llvm::array_pod_sort(BFIs.begin(), BFIs.end());
960 for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
Daniel Dunbarab970f92010-04-13 20:58:55 +0000961 OS.indent(4);
Daniel Dunbarad759532010-04-22 02:35:36 +0000962 BFIs[i].second->print(OS);
Daniel Dunbar93c62962010-04-12 18:14:18 +0000963 OS << "\n";
964 }
Daniel Dunbarad759532010-04-22 02:35:36 +0000965
Daniel Dunbar93c62962010-04-12 18:14:18 +0000966 OS << "]>\n";
967}
968
969void CGRecordLayout::dump() const {
970 print(llvm::errs());
971}
972
973void CGBitFieldInfo::print(llvm::raw_ostream &OS) const {
974 OS << "<CGBitFieldInfo";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000975 OS << " Size:" << Size;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000976 OS << " IsSigned:" << IsSigned << "\n";
977
978 OS.indent(4 + strlen("<CGBitFieldInfo"));
979 OS << " NumComponents:" << getNumComponents();
980 OS << " Components: [";
981 if (getNumComponents()) {
982 OS << "\n";
983 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
984 const AccessInfo &AI = getComponent(i);
985 OS.indent(8);
986 OS << "<AccessInfo"
987 << " FieldIndex:" << AI.FieldIndex
988 << " FieldByteOffset:" << AI.FieldByteOffset
989 << " FieldBitStart:" << AI.FieldBitStart
990 << " AccessWidth:" << AI.AccessWidth << "\n";
991 OS.indent(8 + strlen("<AccessInfo"));
992 OS << " AccessAlignment:" << AI.AccessAlignment
993 << " TargetBitOffset:" << AI.TargetBitOffset
994 << " TargetBitWidth:" << AI.TargetBitWidth
995 << ">\n";
996 }
997 OS.indent(4);
998 }
999 OS << "]>";
Daniel Dunbar93c62962010-04-12 18:14:18 +00001000}
1001
1002void CGBitFieldInfo::dump() const {
1003 print(llvm::errs());
1004}