blob: 4b19aefcf9c4e7d5c1f525c6f63e06b390839bf4 [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 ///
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000037 std::vector<const llvm::Type *> FieldTypes;
38
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;
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 McCall4d9f1422011-02-15 22:21:29 +000087 CharUnits Alignment;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000088
Daniel Dunbar23ee4b72010-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 McCall4d9f1422011-02-15 22:21:29 +000093 /// NextFieldOffset - Holds the next field offset.
94 CharUnits NextFieldOffset;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000095
Anders Carlsson1de2f572010-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 Dunbar23ee4b72010-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 Carlssona518b2a2010-12-04 23:59:48 +0000108 /// Layout a single base, virtual or non-virtual
John McCall4d9f1422011-02-15 22:21:29 +0000109 void LayoutBase(const CXXRecordDecl *base,
110 const CGRecordLayout &baseLayout,
111 CharUnits baseOffset);
Anders Carlssona518b2a2010-12-04 23:59:48 +0000112
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000113 /// LayoutVirtualBase - layout a single virtual base.
John McCall4d9f1422011-02-15 22:21:29 +0000114 void LayoutVirtualBase(const CXXRecordDecl *base,
115 CharUnits baseOffset);
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000116
Anders Carlssona459adb2010-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 Carlssonaf9e5af2010-05-18 05:12:20 +0000121 /// LayoutNonVirtualBase - layout a single non-virtual base.
John McCall4d9f1422011-02-15 22:21:29 +0000122 void LayoutNonVirtualBase(const CXXRecordDecl *base,
123 CharUnits baseOffset);
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000124
Anders Carlssona459adb2010-11-28 19:18:44 +0000125 /// LayoutNonVirtualBases - layout the virtual bases of a record decl.
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000126 void LayoutNonVirtualBases(const CXXRecordDecl *RD,
127 const ASTRecordLayout &Layout);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000128
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000129 /// ComputeNonVirtualBaseType - Compute the non-virtual base field types.
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000130 bool ComputeNonVirtualBaseType(const CXXRecordDecl *RD);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000131
Daniel Dunbar23ee4b72010-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 McCall4d9f1422011-02-15 22:21:29 +0000140 void AppendField(CharUnits fieldOffset, const llvm::Type *FieldTy);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000141
Daniel Dunbar23ee4b72010-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 McCall4d9f1422011-02-15 22:21:29 +0000144 void AppendPadding(CharUnits fieldOffset, CharUnits fieldAlignment);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000145
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000146 /// getByteArrayType - Returns a byte array type with the given number of
147 /// elements.
John McCall4d9f1422011-02-15 22:21:29 +0000148 const llvm::Type *getByteArrayType(CharUnits NumBytes);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000149
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000150 /// AppendBytes - Append a given number of bytes to the record.
John McCall4d9f1422011-02-15 22:21:29 +0000151 void AppendBytes(CharUnits numBytes);
Daniel Dunbar23ee4b72010-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 McCall4d9f1422011-02-15 22:21:29 +0000157 CharUnits getTypeAlignment(const llvm::Type *Ty) const;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000158
Anders Carlssonacf877b2010-11-28 23:06:23 +0000159 /// getAlignmentAsLLVMStruct - Returns the maximum alignment of all the
160 /// LLVM element types.
John McCall4d9f1422011-02-15 22:21:29 +0000161 CharUnits getAlignmentAsLLVMStruct() const;
Anders Carlssonacf877b2010-11-28 23:06:23 +0000162
John McCall614dbdc2010-08-22 21:01:12 +0000163 /// CheckZeroInitializable - Check if the given type contains a pointer
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000164 /// to data member.
John McCall614dbdc2010-08-22 21:01:12 +0000165 void CheckZeroInitializable(QualType T);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000166
167public:
168 CGRecordLayoutBuilder(CodeGenTypes &Types)
John McCall0217dfc22011-02-15 06:40:56 +0000169 : BaseSubobjectType(0),
170 IsZeroInitializable(true), IsZeroInitializableAsBase(true),
John McCall4d9f1422011-02-15 22:21:29 +0000171 Packed(false), Types(Types), BitsAvailableInLastField(0) { }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000172
173 /// Layout - Will layout a RecordDecl.
174 void Layout(const RecordDecl *D);
175};
176
177}
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000178
Anders Carlsson307846f2009-07-23 03:17:50 +0000179void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
John McCall4d9f1422011-02-15 22:21:29 +0000180 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment();
Anders Carlsson09a37742009-09-02 17:51:33 +0000181 Packed = D->hasAttr<PackedAttr>();
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000182
Anders Carlsson697f6592009-07-23 03:43:54 +0000183 if (D->isUnion()) {
184 LayoutUnion(D);
185 return;
186 }
Anders Carlsson68e0b682009-08-08 18:23:56 +0000187
Anders Carlsson307846f2009-07-23 03:17:50 +0000188 if (LayoutFields(D))
189 return;
Mike Stump11289f42009-09-09 15:08:12 +0000190
Anders Carlsson307846f2009-07-23 03:17:50 +0000191 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlssond78fc892009-07-23 17:24:40 +0000192 Packed = true;
John McCall4d9f1422011-02-15 22:21:29 +0000193 NextFieldOffset = CharUnits::Zero();
Anders Carlsson307846f2009-07-23 03:17:50 +0000194 FieldTypes.clear();
John McCall0217dfc22011-02-15 06:40:56 +0000195 Fields.clear();
196 BitFields.clear();
197 NonVirtualBases.clear();
198 VirtualBases.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000199
Anders Carlsson307846f2009-07-23 03:17:50 +0000200 LayoutFields(D);
201}
202
Daniel Dunbarc7f9bba2010-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 Dunbarf9c24f82010-04-12 21:01:28 +0000209 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(FD->getType());
Daniel Dunbarb935b932010-04-13 20:58:55 +0000210 uint64_t TypeSizeInBytes = Types.getTargetData().getTypeAllocSize(Ty);
211 uint64_t TypeSizeInBits = TypeSizeInBytes * 8;
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000212
213 bool IsSigned = FD->getType()->isSignedIntegerType();
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000214
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000215 if (FieldSize > TypeSizeInBits) {
Anders Carlssond5f27b02010-04-17 22:54:57 +0000216 // We have a wide bit-field. The extra bits are only used for padding, so
217 // if we have a bitfield of type T, with size N:
218 //
219 // T t : N;
220 //
221 // We can just assume that it's:
222 //
223 // T t : sizeof(T);
224 //
225 FieldSize = TypeSizeInBits;
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000226 }
227
Chris Lattnerfb59c7c2011-02-17 22:09:58 +0000228 // in big-endian machines the first fields are in higher bit positions,
229 // so revert the offset. The byte offsets are reversed(back) later.
230 if (Types.getTargetData().isBigEndian()) {
231 FieldOffset = ((ContainingTypeSizeInBits)-FieldOffset-FieldSize);
232 }
233
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000234 // Compute the access components. The policy we use is to start by attempting
235 // to access using the width of the bit-field type itself and to always access
236 // at aligned indices of that type. If such an access would fail because it
237 // extends past the bound of the type, then we reduce size to the next smaller
238 // power of two and retry. The current algorithm assumes pow2 sized types,
239 // although this is easy to fix.
240 //
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000241 assert(llvm::isPowerOf2_32(TypeSizeInBits) && "Unexpected type size!");
242 CGBitFieldInfo::AccessInfo Components[3];
243 unsigned NumComponents = 0;
244 unsigned AccessedTargetBits = 0; // The tumber of target bits accessed.
245 unsigned AccessWidth = TypeSizeInBits; // The current access width to attempt.
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000246
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000247 // Round down from the field offset to find the first access position that is
248 // at an aligned offset of the initial access type.
Daniel Dunbar59813772010-04-22 15:22:33 +0000249 uint64_t AccessStart = FieldOffset - (FieldOffset % AccessWidth);
250
251 // Adjust initial access size to fit within record.
252 while (AccessWidth > 8 &&
253 AccessStart + AccessWidth > ContainingTypeSizeInBits) {
254 AccessWidth >>= 1;
255 AccessStart = FieldOffset - (FieldOffset % AccessWidth);
256 }
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000257
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000258 while (AccessedTargetBits < FieldSize) {
259 // Check that we can access using a type of this size, without reading off
260 // the end of the structure. This can occur with packed structures and
261 // -fno-bitfield-type-align, for example.
262 if (AccessStart + AccessWidth > ContainingTypeSizeInBits) {
263 // If so, reduce access size to the next smaller power-of-two and retry.
264 AccessWidth >>= 1;
265 assert(AccessWidth >= 8 && "Cannot access under byte size!");
266 continue;
267 }
Daniel Dunbarb935b932010-04-13 20:58:55 +0000268
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000269 // Otherwise, add an access component.
Daniel Dunbarb935b932010-04-13 20:58:55 +0000270
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000271 // First, compute the bits inside this access which are part of the
272 // target. We are reading bits [AccessStart, AccessStart + AccessWidth); the
273 // intersection with [FieldOffset, FieldOffset + FieldSize) gives the bits
274 // in the target that we are reading.
Daniel Dunbar59813772010-04-22 15:22:33 +0000275 assert(FieldOffset < AccessStart + AccessWidth && "Invalid access start!");
276 assert(AccessStart < FieldOffset + FieldSize && "Invalid access start!");
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000277 uint64_t AccessBitsInFieldStart = std::max(AccessStart, FieldOffset);
278 uint64_t AccessBitsInFieldSize =
Daniel Dunbar59813772010-04-22 15:22:33 +0000279 std::min(AccessWidth + AccessStart,
280 FieldOffset + FieldSize) - AccessBitsInFieldStart;
Daniel Dunbar5d6c07e2010-04-22 14:56:10 +0000281
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000282 assert(NumComponents < 3 && "Unexpected number of components!");
283 CGBitFieldInfo::AccessInfo &AI = Components[NumComponents++];
284 AI.FieldIndex = 0;
285 // FIXME: We still follow the old access pattern of only using the field
286 // byte offset. We should switch this once we fix the struct layout to be
287 // pretty.
Chris Lattnerfb59c7c2011-02-17 22:09:58 +0000288
289 // on big-endian machines we reverted the bit offset because first fields are
290 // in higher bits. But this also reverts the bytes, so fix this here by reverting
291 // the byte offset on big-endian machines.
292 if (Types.getTargetData().isBigEndian()) {
293 AI.FieldByteOffset = (ContainingTypeSizeInBits - AccessStart - AccessWidth )/8;
294 } else {
295 AI.FieldByteOffset = AccessStart / 8;
296 }
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000297 AI.FieldBitStart = AccessBitsInFieldStart - AccessStart;
298 AI.AccessWidth = AccessWidth;
Daniel Dunbarfc66e0e2010-04-22 03:17:04 +0000299 AI.AccessAlignment = llvm::MinAlign(ContainingTypeAlign, AccessStart) / 8;
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000300 AI.TargetBitOffset = AccessedTargetBits;
301 AI.TargetBitWidth = AccessBitsInFieldSize;
302
303 AccessStart += AccessWidth;
304 AccessedTargetBits += AI.TargetBitWidth;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000305 }
306
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000307 assert(AccessedTargetBits == FieldSize && "Invalid bit-field access!");
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000308 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000309}
310
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000311CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
312 const FieldDecl *FD,
313 uint64_t FieldOffset,
314 uint64_t FieldSize) {
315 const RecordDecl *RD = FD->getParent();
316 const ASTRecordLayout &RL = Types.getContext().getASTRecordLayout(RD);
Ken Dyckb0fcc592011-02-11 01:54:29 +0000317 uint64_t ContainingTypeSizeInBits = Types.getContext().toBits(RL.getSize());
Ken Dyck7ad11e72011-02-15 02:32:40 +0000318 unsigned ContainingTypeAlign = Types.getContext().toBits(RL.getAlignment());
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000319
320 return MakeInfo(Types, FD, FieldOffset, FieldSize, ContainingTypeSizeInBits,
321 ContainingTypeAlign);
322}
323
Anders Carlsson307846f2009-07-23 03:17:50 +0000324void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
John McCall4d9f1422011-02-15 22:21:29 +0000325 uint64_t fieldOffset) {
326 uint64_t fieldSize =
Anders Carlsson307846f2009-07-23 03:17:50 +0000327 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump11289f42009-09-09 15:08:12 +0000328
John McCall4d9f1422011-02-15 22:21:29 +0000329 if (fieldSize == 0)
Anders Carlsson307846f2009-07-23 03:17:50 +0000330 return;
331
John McCall4d9f1422011-02-15 22:21:29 +0000332 uint64_t nextFieldOffsetInBits = NextFieldOffset.getQuantity() * 8;
333 unsigned numBytesToAppend;
Mike Stump11289f42009-09-09 15:08:12 +0000334
John McCall4d9f1422011-02-15 22:21:29 +0000335 if (fieldOffset < nextFieldOffsetInBits) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000336 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
John McCall4d9f1422011-02-15 22:21:29 +0000337 assert(!NextFieldOffset.isZero() && "Must have laid out at least one byte");
Mike Stump11289f42009-09-09 15:08:12 +0000338
Anders Carlsson307846f2009-07-23 03:17:50 +0000339 // The bitfield begins in the previous bit-field.
John McCall4d9f1422011-02-15 22:21:29 +0000340 numBytesToAppend =
341 llvm::RoundUpToAlignment(fieldSize - BitsAvailableInLastField, 8) / 8;
Anders Carlsson307846f2009-07-23 03:17:50 +0000342 } else {
John McCall4d9f1422011-02-15 22:21:29 +0000343 assert(fieldOffset % 8 == 0 && "Field offset not aligned correctly");
Anders Carlsson307846f2009-07-23 03:17:50 +0000344
345 // Append padding if necessary.
John McCall4d9f1422011-02-15 22:21:29 +0000346 AppendPadding(CharUnits::fromQuantity(fieldOffset / 8), CharUnits::One());
Mike Stump11289f42009-09-09 15:08:12 +0000347
John McCall4d9f1422011-02-15 22:21:29 +0000348 numBytesToAppend = llvm::RoundUpToAlignment(fieldSize, 8) / 8;
Mike Stump11289f42009-09-09 15:08:12 +0000349
John McCall4d9f1422011-02-15 22:21:29 +0000350 assert(numBytesToAppend && "No bytes to append!");
Anders Carlsson307846f2009-07-23 03:17:50 +0000351 }
352
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000353 // Add the bit field info.
John McCall0217dfc22011-02-15 06:40:56 +0000354 BitFields.insert(std::make_pair(D,
John McCall4d9f1422011-02-15 22:21:29 +0000355 CGBitFieldInfo::MakeInfo(Types, D, fieldOffset, fieldSize)));
Mike Stump11289f42009-09-09 15:08:12 +0000356
John McCall4d9f1422011-02-15 22:21:29 +0000357 AppendBytes(CharUnits::fromQuantity(numBytesToAppend));
Mike Stump11289f42009-09-09 15:08:12 +0000358
Mike Stump11289f42009-09-09 15:08:12 +0000359 BitsAvailableInLastField =
John McCall4d9f1422011-02-15 22:21:29 +0000360 NextFieldOffset.getQuantity() * 8 - (fieldOffset + fieldSize);
Anders Carlsson307846f2009-07-23 03:17:50 +0000361}
362
363bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
John McCall4d9f1422011-02-15 22:21:29 +0000364 uint64_t fieldOffset) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000365 // If the field is packed, then we need a packed struct.
Anders Carlsson68e0b682009-08-08 18:23:56 +0000366 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson307846f2009-07-23 03:17:50 +0000367 return false;
368
369 if (D->isBitField()) {
370 // We must use packed structs for unnamed bit fields since they
371 // don't affect the struct alignment.
Anders Carlssond78fc892009-07-23 17:24:40 +0000372 if (!Packed && !D->getDeclName())
Anders Carlsson307846f2009-07-23 03:17:50 +0000373 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000374
John McCall4d9f1422011-02-15 22:21:29 +0000375 LayoutBitField(D, fieldOffset);
Anders Carlsson307846f2009-07-23 03:17:50 +0000376 return true;
377 }
Mike Stump11289f42009-09-09 15:08:12 +0000378
John McCall614dbdc2010-08-22 21:01:12 +0000379 CheckZeroInitializable(D->getType());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000380
John McCall4d9f1422011-02-15 22:21:29 +0000381 assert(fieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!");
382 CharUnits fieldOffsetInBytes = CharUnits::fromQuantity(fieldOffset / 8);
Mike Stump11289f42009-09-09 15:08:12 +0000383
Anders Carlsson19702bb2009-08-04 16:29:15 +0000384 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
John McCall4d9f1422011-02-15 22:21:29 +0000385 CharUnits typeAlignment = getTypeAlignment(Ty);
Anders Carlsson19702bb2009-08-04 16:29:15 +0000386
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000387 // If the type alignment is larger then the struct alignment, we must use
388 // a packed struct.
John McCall4d9f1422011-02-15 22:21:29 +0000389 if (typeAlignment > Alignment) {
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000390 assert(!Packed && "Alignment is wrong even with packed struct!");
391 return false;
392 }
Mike Stump11289f42009-09-09 15:08:12 +0000393
John McCall4d9f1422011-02-15 22:21:29 +0000394 if (!Packed) {
395 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
396 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
397 if (const MaxFieldAlignmentAttr *MFAA =
398 RD->getAttr<MaxFieldAlignmentAttr>()) {
399 if (MFAA->getAlignment() != typeAlignment.getQuantity() * 8)
400 return false;
401 }
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000402 }
403 }
404
Anders Carlsson19702bb2009-08-04 16:29:15 +0000405 // Round up the field offset to the alignment of the field type.
John McCall4d9f1422011-02-15 22:21:29 +0000406 CharUnits alignedNextFieldOffsetInBytes =
407 NextFieldOffset.RoundUpToAlignment(typeAlignment);
Anders Carlsson19702bb2009-08-04 16:29:15 +0000408
John McCall4d9f1422011-02-15 22:21:29 +0000409 if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
Anders Carlsson19702bb2009-08-04 16:29:15 +0000410 assert(!Packed && "Could not place field even with packed struct!");
411 return false;
412 }
Mike Stump11289f42009-09-09 15:08:12 +0000413
John McCall4d9f1422011-02-15 22:21:29 +0000414 AppendPadding(fieldOffsetInBytes, typeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +0000415
Anders Carlsson307846f2009-07-23 03:17:50 +0000416 // Now append the field.
John McCall0217dfc22011-02-15 06:40:56 +0000417 Fields[D] = FieldTypes.size();
John McCall4d9f1422011-02-15 22:21:29 +0000418 AppendField(fieldOffsetInBytes, Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000419
Anders Carlsson307846f2009-07-23 03:17:50 +0000420 return true;
421}
422
Anders Carlsson1de2f572010-04-17 20:49:27 +0000423const llvm::Type *
424CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
425 const ASTRecordLayout &Layout) {
426 if (Field->isBitField()) {
427 uint64_t FieldSize =
428 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
429
430 // Ignore zero sized bit fields.
431 if (FieldSize == 0)
432 return 0;
433
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000434 const llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
435 unsigned NumBytesToAppend =
436 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Anders Carlsson2295f132010-04-17 21:04:52 +0000437
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000438 if (NumBytesToAppend > 1)
439 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend);
Anders Carlsson2295f132010-04-17 21:04:52 +0000440
Anders Carlsson1de2f572010-04-17 20:49:27 +0000441 // Add the bit field info.
John McCall0217dfc22011-02-15 06:40:56 +0000442 BitFields.insert(std::make_pair(Field,
443 CGBitFieldInfo::MakeInfo(Types, Field, 0, FieldSize)));
Anders Carlsson2295f132010-04-17 21:04:52 +0000444 return FieldTy;
Anders Carlsson1de2f572010-04-17 20:49:27 +0000445 }
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000446
Anders Carlsson1de2f572010-04-17 20:49:27 +0000447 // This is a regular union field.
John McCall0217dfc22011-02-15 06:40:56 +0000448 Fields[Field] = 0;
Anders Carlsson1de2f572010-04-17 20:49:27 +0000449 return Types.ConvertTypeForMemRecursive(Field->getType());
450}
451
Anders Carlsson697f6592009-07-23 03:43:54 +0000452void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
453 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump11289f42009-09-09 15:08:12 +0000454
John McCall4d9f1422011-02-15 22:21:29 +0000455 const ASTRecordLayout &layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000456
John McCall4d9f1422011-02-15 22:21:29 +0000457 const llvm::Type *unionType = 0;
458 CharUnits unionSize = CharUnits::Zero();
459 CharUnits unionAlign = CharUnits::Zero();
Mike Stump11289f42009-09-09 15:08:12 +0000460
John McCall4d9f1422011-02-15 22:21:29 +0000461 bool hasOnlyZeroSizedBitFields = true;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000462
John McCall4d9f1422011-02-15 22:21:29 +0000463 unsigned fieldNo = 0;
464 for (RecordDecl::field_iterator field = D->field_begin(),
465 fieldEnd = D->field_end(); field != fieldEnd; ++field, ++fieldNo) {
466 assert(layout.getFieldOffset(fieldNo) == 0 &&
Anders Carlsson697f6592009-07-23 03:43:54 +0000467 "Union field offset did not start at the beginning of record!");
John McCall4d9f1422011-02-15 22:21:29 +0000468 const llvm::Type *fieldType = LayoutUnionField(*field, layout);
Anders Carlssonf814ee62009-07-23 04:00:39 +0000469
John McCall4d9f1422011-02-15 22:21:29 +0000470 if (!fieldType)
Anders Carlsson1de2f572010-04-17 20:49:27 +0000471 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000472
John McCall4d9f1422011-02-15 22:21:29 +0000473 hasOnlyZeroSizedBitFields = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000474
John McCall4d9f1422011-02-15 22:21:29 +0000475 CharUnits fieldAlign = CharUnits::fromQuantity(
476 Types.getTargetData().getABITypeAlignment(fieldType));
477 CharUnits fieldSize = CharUnits::fromQuantity(
478 Types.getTargetData().getTypeAllocSize(fieldType));
Mike Stump11289f42009-09-09 15:08:12 +0000479
John McCall4d9f1422011-02-15 22:21:29 +0000480 if (fieldAlign < unionAlign)
Anders Carlsson697f6592009-07-23 03:43:54 +0000481 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000482
John McCall4d9f1422011-02-15 22:21:29 +0000483 if (fieldAlign > unionAlign || fieldSize > unionSize) {
484 unionType = fieldType;
485 unionAlign = fieldAlign;
486 unionSize = fieldSize;
Anders Carlsson697f6592009-07-23 03:43:54 +0000487 }
488 }
Mike Stump11289f42009-09-09 15:08:12 +0000489
Anders Carlsson697f6592009-07-23 03:43:54 +0000490 // Now add our field.
John McCall4d9f1422011-02-15 22:21:29 +0000491 if (unionType) {
492 AppendField(CharUnits::Zero(), unionType);
Anders Carlsson0e912752009-09-03 22:56:02 +0000493
John McCall4d9f1422011-02-15 22:21:29 +0000494 if (getTypeAlignment(unionType) > layout.getAlignment()) {
Anders Carlsson0e912752009-09-03 22:56:02 +0000495 // We need a packed struct.
496 Packed = true;
John McCall4d9f1422011-02-15 22:21:29 +0000497 unionAlign = CharUnits::One();
Anders Carlsson0e912752009-09-03 22:56:02 +0000498 }
499 }
John McCall4d9f1422011-02-15 22:21:29 +0000500 if (unionAlign.isZero()) {
501 assert(hasOnlyZeroSizedBitFields &&
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000502 "0-align record did not have all zero-sized bit-fields!");
John McCall4d9f1422011-02-15 22:21:29 +0000503 unionAlign = CharUnits::One();
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000504 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000505
Anders Carlsson697f6592009-07-23 03:43:54 +0000506 // Append tail padding.
John McCall4d9f1422011-02-15 22:21:29 +0000507 CharUnits recordSize = layout.getSize();
508 if (recordSize > unionSize)
509 AppendPadding(recordSize, unionAlign);
Anders Carlsson697f6592009-07-23 03:43:54 +0000510}
511
John McCall0217dfc22011-02-15 06:40:56 +0000512void CGRecordLayoutBuilder::LayoutBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000513 const CGRecordLayout &baseLayout,
514 CharUnits baseOffset) {
515 AppendPadding(baseOffset, CharUnits::One());
Anders Carlssona518b2a2010-12-04 23:59:48 +0000516
John McCall0217dfc22011-02-15 06:40:56 +0000517 const ASTRecordLayout &baseASTLayout
518 = Types.getContext().getASTRecordLayout(base);
519
John McCall4d9f1422011-02-15 22:21:29 +0000520 // Fields and bases can be laid out in the tail padding of previous
521 // bases. If this happens, we need to allocate the base as an i8
522 // array; otherwise, we can use the subobject type. However,
523 // actually doing that would require knowledge of what immediately
524 // follows this base in the layout, so instead we do a conservative
525 // approximation, which is to use the base subobject type if it
526 // has the same LLVM storage size as the nvsize.
527
528 // The nvsize, i.e. the unpadded size of the base class.
529 CharUnits nvsize = baseASTLayout.getNonVirtualSize();
530
531#if 0
532 const llvm::StructType *subobjectType = baseLayout.getBaseSubobjectLLVMType();
533 const llvm::StructLayout *baseLLVMLayout =
534 Types.getTargetData().getStructLayout(subobjectType);
535 CharUnits stsize = CharUnits::fromQuantity(baseLLVMLayout->getSizeInBytes());
536
537 if (nvsize == stsize)
538 AppendField(baseOffset, subobjectType);
539 else
540#endif
541 AppendBytes(nvsize);
John McCall0217dfc22011-02-15 06:40:56 +0000542}
543
544void CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000545 CharUnits baseOffset) {
John McCall0217dfc22011-02-15 06:40:56 +0000546 // Ignore empty bases.
547 if (base->isEmpty()) return;
548
549 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
550 if (IsZeroInitializableAsBase) {
551 assert(IsZeroInitializable &&
552 "class zero-initializable as base but not as complete object");
553
554 IsZeroInitializable = IsZeroInitializableAsBase =
555 baseLayout.isZeroInitializableAsBase();
556 }
557
John McCall4d9f1422011-02-15 22:21:29 +0000558 LayoutBase(base, baseLayout, baseOffset);
John McCall0217dfc22011-02-15 06:40:56 +0000559 NonVirtualBases[base] = (FieldTypes.size() - 1);
Anders Carlssona518b2a2010-12-04 23:59:48 +0000560}
561
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000562void
John McCall0217dfc22011-02-15 06:40:56 +0000563CGRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000564 CharUnits baseOffset) {
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000565 // Ignore empty bases.
John McCall0217dfc22011-02-15 06:40:56 +0000566 if (base->isEmpty()) return;
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000567
John McCall0217dfc22011-02-15 06:40:56 +0000568 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
569 if (IsZeroInitializable)
570 IsZeroInitializable = baseLayout.isZeroInitializableAsBase();
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000571
John McCall4d9f1422011-02-15 22:21:29 +0000572 LayoutBase(base, baseLayout, baseOffset);
John McCall0217dfc22011-02-15 06:40:56 +0000573 VirtualBases[base] = (FieldTypes.size() - 1);
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000574}
575
Anders Carlssona459adb2010-11-28 19:18:44 +0000576/// LayoutVirtualBases - layout the non-virtual bases of a record decl.
577void
578CGRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
579 const ASTRecordLayout &Layout) {
580 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
581 E = RD->bases_end(); I != E; ++I) {
582 const CXXRecordDecl *BaseDecl =
583 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
584
585 // We only want to lay out virtual bases that aren't indirect primary bases
586 // of some other base.
587 if (I->isVirtual() && !IndirectPrimaryBases.count(BaseDecl)) {
588 // Only lay out the base once.
589 if (!LaidOutVirtualBases.insert(BaseDecl))
590 continue;
591
John McCall4d9f1422011-02-15 22:21:29 +0000592 CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl);
593 LayoutVirtualBase(BaseDecl, vbaseOffset);
Anders Carlssona459adb2010-11-28 19:18:44 +0000594 }
595
596 if (!BaseDecl->getNumVBases()) {
597 // This base isn't interesting since it doesn't have any virtual bases.
598 continue;
599 }
600
601 LayoutVirtualBases(BaseDecl, Layout);
602 }
603}
604
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000605void
606CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD,
607 const ASTRecordLayout &Layout) {
608 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
609
610 // Check if we need to add a vtable pointer.
611 if (RD->isDynamicClass()) {
612 if (!PrimaryBase) {
613 const llvm::Type *FunctionType =
614 llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()),
615 /*isVarArg=*/true);
616 const llvm::Type *VTableTy = FunctionType->getPointerTo();
617
John McCall4d9f1422011-02-15 22:21:29 +0000618 assert(NextFieldOffset.isZero() &&
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000619 "VTable pointer must come first!");
John McCall4d9f1422011-02-15 22:21:29 +0000620 AppendField(CharUnits::Zero(), VTableTy->getPointerTo());
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000621 } else {
Anders Carlsson7f95cd12010-11-24 23:12:57 +0000622 if (!Layout.isPrimaryBaseVirtual())
John McCall4d9f1422011-02-15 22:21:29 +0000623 LayoutNonVirtualBase(PrimaryBase, CharUnits::Zero());
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000624 else
John McCall4d9f1422011-02-15 22:21:29 +0000625 LayoutVirtualBase(PrimaryBase, CharUnits::Zero());
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000626 }
627 }
628
629 // Layout the non-virtual bases.
630 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
631 E = RD->bases_end(); I != E; ++I) {
632 if (I->isVirtual())
633 continue;
634
635 const CXXRecordDecl *BaseDecl =
636 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
637
638 // We've already laid out the primary base.
Anders Carlsson7f95cd12010-11-24 23:12:57 +0000639 if (BaseDecl == PrimaryBase && !Layout.isPrimaryBaseVirtual())
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000640 continue;
641
John McCall4d9f1422011-02-15 22:21:29 +0000642 LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffset(BaseDecl));
Anders Carlssond681a292009-12-16 17:27:20 +0000643 }
644}
645
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000646bool
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000647CGRecordLayoutBuilder::ComputeNonVirtualBaseType(const CXXRecordDecl *RD) {
648 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(RD);
649
Ken Dyckbec02852011-02-08 02:02:47 +0000650 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
651 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
John McCall4d9f1422011-02-15 22:21:29 +0000652 CharUnits AlignedNonVirtualTypeSize =
653 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000654
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000655 // First check if we can use the same fields as for the complete class.
John McCall4d9f1422011-02-15 22:21:29 +0000656 CharUnits RecordSize = Layout.getSize();
John McCall0217dfc22011-02-15 06:40:56 +0000657 if (AlignedNonVirtualTypeSize == RecordSize)
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000658 return true;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000659
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000660 // Check if we need padding.
John McCall4d9f1422011-02-15 22:21:29 +0000661 CharUnits AlignedNextFieldOffset =
662 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000663
John McCall0217dfc22011-02-15 06:40:56 +0000664 if (AlignedNextFieldOffset > AlignedNonVirtualTypeSize) {
665 assert(!Packed && "cannot layout even as packed struct");
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000666 return false; // Needs packing.
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000667 }
668
John McCall0217dfc22011-02-15 06:40:56 +0000669 bool needsPadding = (AlignedNonVirtualTypeSize != AlignedNextFieldOffset);
670 if (needsPadding) {
John McCall4d9f1422011-02-15 22:21:29 +0000671 CharUnits NumBytes = AlignedNonVirtualTypeSize - AlignedNextFieldOffset;
John McCall0217dfc22011-02-15 06:40:56 +0000672 FieldTypes.push_back(getByteArrayType(NumBytes));
673 }
674
675 BaseSubobjectType = llvm::StructType::get(Types.getLLVMContext(),
676 FieldTypes, Packed);
677
678 if (needsPadding) {
679 // Pull the padding back off.
680 FieldTypes.pop_back();
681 }
682
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000683 return true;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000684}
685
Anders Carlsson307846f2009-07-23 03:17:50 +0000686bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
687 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
John McCall4d9f1422011-02-15 22:21:29 +0000688 assert(!Alignment.isZero() && "Did not set alignment!");
Mike Stump11289f42009-09-09 15:08:12 +0000689
Anders Carlsson697f6592009-07-23 03:43:54 +0000690 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000691
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000692 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
693 if (RD)
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000694 LayoutNonVirtualBases(RD, Layout);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000695
Anders Carlsson307846f2009-07-23 03:17:50 +0000696 unsigned FieldNo = 0;
Fariborz Jahanian7b2b1ec2009-07-27 20:57:45 +0000697
Mike Stump11289f42009-09-09 15:08:12 +0000698 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson307846f2009-07-23 03:17:50 +0000699 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
700 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump11289f42009-09-09 15:08:12 +0000701 assert(!Packed &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000702 "Could not layout fields even with a packed LLVM struct!");
703 return false;
704 }
705 }
706
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000707 if (RD) {
Anders Carlssona459adb2010-11-28 19:18:44 +0000708 // We've laid out the non-virtual bases and the fields, now compute the
709 // non-virtual base field types.
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000710 if (!ComputeNonVirtualBaseType(RD)) {
711 assert(!Packed && "Could not layout even with a packed LLVM struct!");
712 return false;
713 }
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000714
Anders Carlssona459adb2010-11-28 19:18:44 +0000715 // And lay out the virtual bases.
716 RD->getIndirectPrimaryBases(IndirectPrimaryBases);
717 if (Layout.isPrimaryBaseVirtual())
718 IndirectPrimaryBases.insert(Layout.getPrimaryBase());
719 LayoutVirtualBases(RD, Layout);
720 }
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000721
Anders Carlsson307846f2009-07-23 03:17:50 +0000722 // Append tail padding if necessary.
Ken Dyckb0fcc592011-02-11 01:54:29 +0000723 AppendTailPadding(Types.getContext().toBits(Layout.getSize()));
Mike Stump11289f42009-09-09 15:08:12 +0000724
Anders Carlsson307846f2009-07-23 03:17:50 +0000725 return true;
726}
727
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000728void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) {
729 assert(RecordSize % 8 == 0 && "Invalid record size!");
Mike Stump11289f42009-09-09 15:08:12 +0000730
John McCall4d9f1422011-02-15 22:21:29 +0000731 CharUnits RecordSizeInBytes = CharUnits::fromQuantity(RecordSize / 8);
732 assert(NextFieldOffset <= RecordSizeInBytes && "Size mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000733
John McCall4d9f1422011-02-15 22:21:29 +0000734 CharUnits AlignedNextFieldOffset =
735 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
Anders Carlsson220bf4f2009-12-08 01:24:23 +0000736
737 if (AlignedNextFieldOffset == RecordSizeInBytes) {
738 // We don't need any padding.
739 return;
740 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000741
John McCall4d9f1422011-02-15 22:21:29 +0000742 CharUnits NumPadBytes = RecordSizeInBytes - NextFieldOffset;
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000743 AppendBytes(NumPadBytes);
744}
745
John McCall4d9f1422011-02-15 22:21:29 +0000746void CGRecordLayoutBuilder::AppendField(CharUnits fieldOffset,
747 const llvm::Type *fieldType) {
748 CharUnits fieldSize =
749 CharUnits::fromQuantity(Types.getTargetData().getTypeAllocSize(fieldType));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000750
John McCall4d9f1422011-02-15 22:21:29 +0000751 FieldTypes.push_back(fieldType);
Anders Carlsson307846f2009-07-23 03:17:50 +0000752
John McCall4d9f1422011-02-15 22:21:29 +0000753 NextFieldOffset = fieldOffset + fieldSize;
Anders Carlsson307846f2009-07-23 03:17:50 +0000754 BitsAvailableInLastField = 0;
755}
756
John McCall4d9f1422011-02-15 22:21:29 +0000757void CGRecordLayoutBuilder::AppendPadding(CharUnits fieldOffset,
758 CharUnits fieldAlignment) {
759 assert(NextFieldOffset <= fieldOffset &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000760 "Incorrect field layout!");
Mike Stump11289f42009-09-09 15:08:12 +0000761
Anders Carlsson307846f2009-07-23 03:17:50 +0000762 // Round up the field offset to the alignment of the field type.
John McCall4d9f1422011-02-15 22:21:29 +0000763 CharUnits alignedNextFieldOffset =
764 NextFieldOffset.RoundUpToAlignment(fieldAlignment);
Anders Carlsson307846f2009-07-23 03:17:50 +0000765
John McCall4d9f1422011-02-15 22:21:29 +0000766 if (alignedNextFieldOffset < fieldOffset) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000767 // Even with alignment, the field offset is not at the right place,
768 // insert padding.
John McCall4d9f1422011-02-15 22:21:29 +0000769 CharUnits padding = fieldOffset - NextFieldOffset;
Anders Carlsson307846f2009-07-23 03:17:50 +0000770
John McCall4d9f1422011-02-15 22:21:29 +0000771 AppendBytes(padding);
Anders Carlsson307846f2009-07-23 03:17:50 +0000772 }
773}
774
John McCall4d9f1422011-02-15 22:21:29 +0000775const llvm::Type *CGRecordLayoutBuilder::getByteArrayType(CharUnits numBytes) {
776 assert(!numBytes.isZero() && "Empty byte arrays aren't allowed.");
Mike Stump11289f42009-09-09 15:08:12 +0000777
Owen Anderson41a75022009-08-13 21:57:51 +0000778 const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
John McCall4d9f1422011-02-15 22:21:29 +0000779 if (numBytes > CharUnits::One())
780 Ty = llvm::ArrayType::get(Ty, numBytes.getQuantity());
Mike Stump11289f42009-09-09 15:08:12 +0000781
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000782 return Ty;
783}
784
John McCall4d9f1422011-02-15 22:21:29 +0000785void CGRecordLayoutBuilder::AppendBytes(CharUnits numBytes) {
786 if (numBytes.isZero())
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000787 return;
788
Anders Carlsson307846f2009-07-23 03:17:50 +0000789 // Append the padding field
John McCall4d9f1422011-02-15 22:21:29 +0000790 AppendField(NextFieldOffset, getByteArrayType(numBytes));
Anders Carlsson307846f2009-07-23 03:17:50 +0000791}
792
John McCall4d9f1422011-02-15 22:21:29 +0000793CharUnits CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlssond78fc892009-07-23 17:24:40 +0000794 if (Packed)
John McCall4d9f1422011-02-15 22:21:29 +0000795 return CharUnits::One();
Mike Stump11289f42009-09-09 15:08:12 +0000796
John McCall4d9f1422011-02-15 22:21:29 +0000797 return CharUnits::fromQuantity(Types.getTargetData().getABITypeAlignment(Ty));
Anders Carlsson307846f2009-07-23 03:17:50 +0000798}
799
John McCall4d9f1422011-02-15 22:21:29 +0000800CharUnits CGRecordLayoutBuilder::getAlignmentAsLLVMStruct() const {
Anders Carlssonacf877b2010-11-28 23:06:23 +0000801 if (Packed)
John McCall4d9f1422011-02-15 22:21:29 +0000802 return CharUnits::One();
Anders Carlssonacf877b2010-11-28 23:06:23 +0000803
John McCall4d9f1422011-02-15 22:21:29 +0000804 CharUnits maxAlignment = CharUnits::One();
Anders Carlssonacf877b2010-11-28 23:06:23 +0000805 for (size_t i = 0; i != FieldTypes.size(); ++i)
John McCall4d9f1422011-02-15 22:21:29 +0000806 maxAlignment = std::max(maxAlignment, getTypeAlignment(FieldTypes[i]));
Anders Carlssonacf877b2010-11-28 23:06:23 +0000807
John McCall4d9f1422011-02-15 22:21:29 +0000808 return maxAlignment;
Anders Carlssonacf877b2010-11-28 23:06:23 +0000809}
810
John McCall0217dfc22011-02-15 06:40:56 +0000811/// Merge in whether a field of the given type is zero-initializable.
John McCall614dbdc2010-08-22 21:01:12 +0000812void CGRecordLayoutBuilder::CheckZeroInitializable(QualType T) {
Anders Carlssond606de72009-08-23 01:25:01 +0000813 // This record already contains a member pointer.
John McCall0217dfc22011-02-15 06:40:56 +0000814 if (!IsZeroInitializableAsBase)
Anders Carlssond606de72009-08-23 01:25:01 +0000815 return;
Mike Stump11289f42009-09-09 15:08:12 +0000816
Anders Carlssond606de72009-08-23 01:25:01 +0000817 // Can only have member pointers if we're compiling C++.
818 if (!Types.getContext().getLangOptions().CPlusPlus)
819 return;
Mike Stump11289f42009-09-09 15:08:12 +0000820
John McCall0217dfc22011-02-15 06:40:56 +0000821 const Type *elementType = T->getBaseElementTypeUnsafe();
Mike Stump11289f42009-09-09 15:08:12 +0000822
John McCall0217dfc22011-02-15 06:40:56 +0000823 if (const MemberPointerType *MPT = elementType->getAs<MemberPointerType>()) {
John McCall614dbdc2010-08-22 21:01:12 +0000824 if (!Types.getCXXABI().isZeroInitializable(MPT))
John McCall0217dfc22011-02-15 06:40:56 +0000825 IsZeroInitializable = IsZeroInitializableAsBase = false;
826 } else if (const RecordType *RT = elementType->getAs<RecordType>()) {
Anders Carlssone8bfe412010-02-02 05:17:25 +0000827 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
John McCall0217dfc22011-02-15 06:40:56 +0000828 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
829 if (!Layout.isZeroInitializable())
830 IsZeroInitializable = IsZeroInitializableAsBase = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000831 }
Anders Carlssond606de72009-08-23 01:25:01 +0000832}
833
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000834CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) {
835 CGRecordLayoutBuilder Builder(*this);
Mike Stump11289f42009-09-09 15:08:12 +0000836
Anders Carlsson307846f2009-07-23 03:17:50 +0000837 Builder.Layout(D);
Anders Carlssone1d5ca52009-07-24 15:20:52 +0000838
Anders Carlsson36e2fa82010-11-24 19:37:16 +0000839 const llvm::StructType *Ty = llvm::StructType::get(getLLVMContext(),
840 Builder.FieldTypes,
841 Builder.Packed);
Mike Stump11289f42009-09-09 15:08:12 +0000842
John McCall0217dfc22011-02-15 06:40:56 +0000843 // If we're in C++, compute the base subobject type.
Anders Carlsson36e2fa82010-11-24 19:37:16 +0000844 const llvm::StructType *BaseTy = 0;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000845 if (isa<CXXRecordDecl>(D)) {
John McCall0217dfc22011-02-15 06:40:56 +0000846 BaseTy = Builder.BaseSubobjectType;
847 if (!BaseTy) BaseTy = Ty;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000848 }
849
Daniel Dunbar034299e2010-03-31 01:09:11 +0000850 CGRecordLayout *RL =
John McCall0217dfc22011-02-15 06:40:56 +0000851 new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable,
852 Builder.IsZeroInitializableAsBase);
Daniel Dunbar034299e2010-03-31 01:09:11 +0000853
John McCall0217dfc22011-02-15 06:40:56 +0000854 RL->NonVirtualBases.swap(Builder.NonVirtualBases);
855 RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases);
Anders Carlsson061ca522010-05-18 05:22:06 +0000856
Anders Carlsson307846f2009-07-23 03:17:50 +0000857 // Add all the field numbers.
John McCall0217dfc22011-02-15 06:40:56 +0000858 RL->FieldInfo.swap(Builder.Fields);
Anders Carlsson307846f2009-07-23 03:17:50 +0000859
860 // Add bitfield info.
John McCall0217dfc22011-02-15 06:40:56 +0000861 RL->BitFields.swap(Builder.BitFields);
Mike Stump11289f42009-09-09 15:08:12 +0000862
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000863 // Dump the layout, if requested.
Daniel Dunbarb935b932010-04-13 20:58:55 +0000864 if (getContext().getLangOptions().DumpRecordLayouts) {
Daniel Dunbarccabe482010-04-19 20:44:53 +0000865 llvm::errs() << "\n*** Dumping IRgen Record Layout\n";
Daniel Dunbarb935b932010-04-13 20:58:55 +0000866 llvm::errs() << "Record: ";
867 D->dump();
868 llvm::errs() << "\nLayout: ";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000869 RL->dump();
Daniel Dunbarb935b932010-04-13 20:58:55 +0000870 }
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000871
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000872#ifndef NDEBUG
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000873 // Verify that the computed LLVM struct size matches the AST layout size.
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000874 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
875
Ken Dyckb0fcc592011-02-11 01:54:29 +0000876 uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize());
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000877 assert(TypeSizeInBits == getTargetData().getTypeAllocSizeInBits(Ty) &&
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000878 "Type size mismatch!");
879
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000880 if (BaseTy) {
Ken Dyckbec02852011-02-08 02:02:47 +0000881 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
882 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
883 CharUnits AlignedNonVirtualTypeSize =
884 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
885
886 uint64_t AlignedNonVirtualTypeSizeInBits =
Ken Dyckb0fcc592011-02-11 01:54:29 +0000887 getContext().toBits(AlignedNonVirtualTypeSize);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000888
889 assert(AlignedNonVirtualTypeSizeInBits ==
890 getTargetData().getTypeAllocSizeInBits(BaseTy) &&
891 "Type size mismatch!");
892 }
893
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000894 // Verify that the LLVM and AST field offsets agree.
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000895 const llvm::StructType *ST =
896 dyn_cast<llvm::StructType>(RL->getLLVMType());
897 const llvm::StructLayout *SL = getTargetData().getStructLayout(ST);
898
899 const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
900 RecordDecl::field_iterator it = D->field_begin();
901 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
902 const FieldDecl *FD = *it;
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000903
904 // For non-bit-fields, just check that the LLVM struct offset matches the
905 // AST offset.
906 if (!FD->isBitField()) {
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000907 unsigned FieldNo = RL->getLLVMFieldNo(FD);
908 assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
909 "Invalid field offset!");
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000910 continue;
911 }
912
913 // Ignore unnamed bit-fields.
914 if (!FD->getDeclName())
915 continue;
916
917 const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
918 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
919 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
920
921 // Verify that every component access is within the structure.
922 uint64_t FieldOffset = SL->getElementOffsetInBits(AI.FieldIndex);
923 uint64_t AccessBitOffset = FieldOffset + AI.FieldByteOffset * 8;
924 assert(AccessBitOffset + AI.AccessWidth <= TypeSizeInBits &&
925 "Invalid bit-field access (out of range)!");
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000926 }
927 }
928#endif
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000929
Daniel Dunbar034299e2010-03-31 01:09:11 +0000930 return RL;
Anders Carlsson307846f2009-07-23 03:17:50 +0000931}
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000932
933void CGRecordLayout::print(llvm::raw_ostream &OS) const {
934 OS << "<CGRecordLayout\n";
John McCall0217dfc22011-02-15 06:40:56 +0000935 OS << " LLVMType:" << *CompleteObjectType << "\n";
936 if (BaseSubobjectType)
937 OS << " NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n";
John McCall614dbdc2010-08-22 21:01:12 +0000938 OS << " IsZeroInitializable:" << IsZeroInitializable << "\n";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000939 OS << " BitFields:[\n";
Daniel Dunbarb6f4b052010-04-22 02:35:36 +0000940
941 // Print bit-field infos in declaration order.
942 std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000943 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
944 it = BitFields.begin(), ie = BitFields.end();
945 it != ie; ++it) {
Daniel Dunbarb6f4b052010-04-22 02:35:36 +0000946 const RecordDecl *RD = it->first->getParent();
947 unsigned Index = 0;
948 for (RecordDecl::field_iterator
949 it2 = RD->field_begin(); *it2 != it->first; ++it2)
950 ++Index;
951 BFIs.push_back(std::make_pair(Index, &it->second));
952 }
953 llvm::array_pod_sort(BFIs.begin(), BFIs.end());
954 for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
Daniel Dunbarb935b932010-04-13 20:58:55 +0000955 OS.indent(4);
Daniel Dunbarb6f4b052010-04-22 02:35:36 +0000956 BFIs[i].second->print(OS);
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000957 OS << "\n";
958 }
Daniel Dunbarb6f4b052010-04-22 02:35:36 +0000959
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000960 OS << "]>\n";
961}
962
963void CGRecordLayout::dump() const {
964 print(llvm::errs());
965}
966
967void CGBitFieldInfo::print(llvm::raw_ostream &OS) const {
968 OS << "<CGBitFieldInfo";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000969 OS << " Size:" << Size;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000970 OS << " IsSigned:" << IsSigned << "\n";
971
972 OS.indent(4 + strlen("<CGBitFieldInfo"));
973 OS << " NumComponents:" << getNumComponents();
974 OS << " Components: [";
975 if (getNumComponents()) {
976 OS << "\n";
977 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
978 const AccessInfo &AI = getComponent(i);
979 OS.indent(8);
980 OS << "<AccessInfo"
981 << " FieldIndex:" << AI.FieldIndex
982 << " FieldByteOffset:" << AI.FieldByteOffset
983 << " FieldBitStart:" << AI.FieldBitStart
984 << " AccessWidth:" << AI.AccessWidth << "\n";
985 OS.indent(8 + strlen("<AccessInfo"));
986 OS << " AccessAlignment:" << AI.AccessAlignment
987 << " TargetBitOffset:" << AI.TargetBitOffset
988 << " TargetBitWidth:" << AI.TargetBitWidth
989 << ">\n";
990 }
991 OS.indent(4);
992 }
993 OS << "]>";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000994}
995
996void CGBitFieldInfo::dump() const {
997 print(llvm::errs());
998}