blob: ac542abf5d77c2be72dee4c1c463af1c5d7bc894 [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"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "CGCXXABI.h"
16#include "CodeGenTypes.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Attr.h"
Anders Carlsson4131f002010-11-24 22:50:27 +000019#include "clang/AST/CXXInheritance.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000020#include "clang/AST/DeclCXX.h"
21#include "clang/AST/Expr.h"
22#include "clang/AST/RecordLayout.h"
Daniel Dunbard3f3d932011-06-21 18:54:46 +000023#include "clang/Frontend/CodeGenOptions.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "llvm/DataLayout.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000025#include "llvm/DerivedTypes.h"
Daniel Dunbar2ba67442010-04-21 19:10:49 +000026#include "llvm/Support/Debug.h"
Daniel Dunbarb97bff92010-04-12 18:14:18 +000027#include "llvm/Support/raw_ostream.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000028#include "llvm/Type.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000029using namespace clang;
30using namespace CodeGen;
31
John McCallbcd38212010-11-30 23:17:27 +000032namespace {
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000033
34class CGRecordLayoutBuilder {
35public:
36 /// FieldTypes - Holds the LLVM types that the struct is created from.
John McCall0217dfc22011-02-15 06:40:56 +000037 ///
Chris Lattner0e62c1c2011-07-23 10:55:15 +000038 SmallVector<llvm::Type *, 16> FieldTypes;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000039
John McCall0217dfc22011-02-15 06:40:56 +000040 /// BaseSubobjectType - Holds the LLVM type for the non-virtual part
Anders Carlssonc1351ca2010-11-09 05:25:47 +000041 /// of the struct. For example, consider:
42 ///
43 /// struct A { int i; };
44 /// struct B { void *v; };
45 /// struct C : virtual A, B { };
46 ///
47 /// The LLVM type of C will be
48 /// %struct.C = type { i32 (...)**, %struct.A, i32, %struct.B }
49 ///
50 /// And the LLVM type of the non-virtual base struct will be
51 /// %struct.C.base = type { i32 (...)**, %struct.A, i32 }
John McCall0217dfc22011-02-15 06:40:56 +000052 ///
53 /// This only gets initialized if the base subobject type is
54 /// different from the complete-object type.
Chris Lattnera5f58b02011-07-09 17:41:47 +000055 llvm::StructType *BaseSubobjectType;
Anders Carlssonc1351ca2010-11-09 05:25:47 +000056
John McCall0217dfc22011-02-15 06:40:56 +000057 /// FieldInfo - Holds a field and its corresponding LLVM field number.
58 llvm::DenseMap<const FieldDecl *, unsigned> Fields;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000059
John McCall0217dfc22011-02-15 06:40:56 +000060 /// BitFieldInfo - Holds location and size information about a bit field.
61 llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000062
John McCall0217dfc22011-02-15 06:40:56 +000063 llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases;
64 llvm::DenseMap<const CXXRecordDecl *, unsigned> VirtualBases;
Anders Carlsson4131f002010-11-24 22:50:27 +000065
66 /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are
67 /// primary base classes for some other direct or indirect base class.
68 CXXIndirectPrimaryBaseSet IndirectPrimaryBases;
69
Anders Carlssona459adb2010-11-28 19:18:44 +000070 /// LaidOutVirtualBases - A set of all laid out virtual bases, used to avoid
71 /// avoid laying out virtual bases more than once.
72 llvm::SmallPtrSet<const CXXRecordDecl *, 4> LaidOutVirtualBases;
73
John McCall614dbdc2010-08-22 21:01:12 +000074 /// IsZeroInitializable - Whether this struct can be C++
75 /// zero-initialized with an LLVM zeroinitializer.
76 bool IsZeroInitializable;
John McCall0217dfc22011-02-15 06:40:56 +000077 bool IsZeroInitializableAsBase;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000078
79 /// Packed - Whether the resulting LLVM struct will be packed or not.
80 bool Packed;
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +000081
82 /// IsMsStruct - Whether ms_struct is in effect or not
83 bool IsMsStruct;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000084
85private:
86 CodeGenTypes &Types;
87
Anders Carlssonfcaaa692011-04-17 21:56:13 +000088 /// LastLaidOutBaseInfo - Contains the offset and non-virtual size of the
89 /// last base laid out. Used so that we can replace the last laid out base
90 /// type with an i8 array if needed.
91 struct LastLaidOutBaseInfo {
92 CharUnits Offset;
93 CharUnits NonVirtualSize;
94
95 bool isValid() const { return !NonVirtualSize.isZero(); }
96 void invalidate() { NonVirtualSize = CharUnits::Zero(); }
97
98 } LastLaidOutBase;
99
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000100 /// Alignment - Contains the alignment of the RecordDecl.
John McCall4d9f1422011-02-15 22:21:29 +0000101 CharUnits Alignment;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000102
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000103 /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field,
104 /// this will have the number of bits still available in the field.
105 char BitsAvailableInLastField;
106
John McCall4d9f1422011-02-15 22:21:29 +0000107 /// NextFieldOffset - Holds the next field offset.
108 CharUnits NextFieldOffset;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000109
Anders Carlsson1de2f572010-04-17 20:49:27 +0000110 /// LayoutUnionField - Will layout a field in an union and return the type
111 /// that the field will have.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000112 llvm::Type *LayoutUnionField(const FieldDecl *Field,
113 const ASTRecordLayout &Layout);
Anders Carlsson1de2f572010-04-17 20:49:27 +0000114
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000115 /// LayoutUnion - Will layout a union RecordDecl.
116 void LayoutUnion(const RecordDecl *D);
117
118 /// LayoutField - try to layout all fields in the record decl.
119 /// Returns false if the operation failed because the struct is not packed.
120 bool LayoutFields(const RecordDecl *D);
121
Anders Carlssona518b2a2010-12-04 23:59:48 +0000122 /// Layout a single base, virtual or non-virtual
Eli Friedman3c840aa2011-12-12 23:13:20 +0000123 bool LayoutBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000124 const CGRecordLayout &baseLayout,
125 CharUnits baseOffset);
Anders Carlssona518b2a2010-12-04 23:59:48 +0000126
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000127 /// LayoutVirtualBase - layout a single virtual base.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000128 bool LayoutVirtualBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000129 CharUnits baseOffset);
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000130
Anders Carlssona459adb2010-11-28 19:18:44 +0000131 /// LayoutVirtualBases - layout the virtual bases of a record decl.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000132 bool LayoutVirtualBases(const CXXRecordDecl *RD,
Anders Carlssona459adb2010-11-28 19:18:44 +0000133 const ASTRecordLayout &Layout);
John McCall0153cd32011-11-08 04:01:03 +0000134
135 /// MSLayoutVirtualBases - layout the virtual bases of a record decl,
136 /// like MSVC.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000137 bool MSLayoutVirtualBases(const CXXRecordDecl *RD,
John McCall0153cd32011-11-08 04:01:03 +0000138 const ASTRecordLayout &Layout);
Anders Carlssona459adb2010-11-28 19:18:44 +0000139
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000140 /// LayoutNonVirtualBase - layout a single non-virtual base.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000141 bool LayoutNonVirtualBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000142 CharUnits baseOffset);
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000143
Anders Carlssona459adb2010-11-28 19:18:44 +0000144 /// LayoutNonVirtualBases - layout the virtual bases of a record decl.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000145 bool LayoutNonVirtualBases(const CXXRecordDecl *RD,
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000146 const ASTRecordLayout &Layout);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000147
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000148 /// ComputeNonVirtualBaseType - Compute the non-virtual base field types.
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000149 bool ComputeNonVirtualBaseType(const CXXRecordDecl *RD);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000150
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000151 /// LayoutField - layout a single field. Returns false if the operation failed
152 /// because the current struct is not packed.
153 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
154
155 /// LayoutBitField - layout a single bit field.
156 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
157
158 /// AppendField - Appends a field with the given offset and type.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000159 void AppendField(CharUnits fieldOffset, llvm::Type *FieldTy);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000160
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000161 /// AppendPadding - Appends enough padding bytes so that the total
162 /// struct size is a multiple of the field alignment.
John McCall4d9f1422011-02-15 22:21:29 +0000163 void AppendPadding(CharUnits fieldOffset, CharUnits fieldAlignment);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000164
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000165 /// ResizeLastBaseFieldIfNecessary - Fields and bases can be laid out in the
166 /// tail padding of a previous base. If this happens, the type of the previous
167 /// base needs to be changed to an array of i8. Returns true if the last
168 /// laid out base was resized.
169 bool ResizeLastBaseFieldIfNecessary(CharUnits offset);
170
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000171 /// getByteArrayType - Returns a byte array type with the given number of
172 /// elements.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000173 llvm::Type *getByteArrayType(CharUnits NumBytes);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000174
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000175 /// AppendBytes - Append a given number of bytes to the record.
John McCall4d9f1422011-02-15 22:21:29 +0000176 void AppendBytes(CharUnits numBytes);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000177
178 /// AppendTailPadding - Append enough tail padding so that the type will have
179 /// the passed size.
Ken Dyck272b6fa2011-04-24 16:53:44 +0000180 void AppendTailPadding(CharUnits RecordSize);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000181
Chris Lattner2192fe52011-07-18 04:24:23 +0000182 CharUnits getTypeAlignment(llvm::Type *Ty) const;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000183
Anders Carlssonacf877b2010-11-28 23:06:23 +0000184 /// getAlignmentAsLLVMStruct - Returns the maximum alignment of all the
185 /// LLVM element types.
John McCall4d9f1422011-02-15 22:21:29 +0000186 CharUnits getAlignmentAsLLVMStruct() const;
Anders Carlssonacf877b2010-11-28 23:06:23 +0000187
John McCall614dbdc2010-08-22 21:01:12 +0000188 /// CheckZeroInitializable - Check if the given type contains a pointer
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000189 /// to data member.
John McCall614dbdc2010-08-22 21:01:12 +0000190 void CheckZeroInitializable(QualType T);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000191
192public:
193 CGRecordLayoutBuilder(CodeGenTypes &Types)
John McCall0217dfc22011-02-15 06:40:56 +0000194 : BaseSubobjectType(0),
195 IsZeroInitializable(true), IsZeroInitializableAsBase(true),
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000196 Packed(false), IsMsStruct(false),
197 Types(Types), BitsAvailableInLastField(0) { }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000198
199 /// Layout - Will layout a RecordDecl.
200 void Layout(const RecordDecl *D);
201};
202
203}
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000204
Anders Carlsson307846f2009-07-23 03:17:50 +0000205void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
John McCall4d9f1422011-02-15 22:21:29 +0000206 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment();
Anders Carlsson09a37742009-09-02 17:51:33 +0000207 Packed = D->hasAttr<PackedAttr>();
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000208
Eli Friedman9ee2d0472012-10-12 23:29:20 +0000209 IsMsStruct = D->isMsStruct(Types.getContext());
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000210
Anders Carlsson697f6592009-07-23 03:43:54 +0000211 if (D->isUnion()) {
212 LayoutUnion(D);
213 return;
214 }
Anders Carlsson68e0b682009-08-08 18:23:56 +0000215
Anders Carlsson307846f2009-07-23 03:17:50 +0000216 if (LayoutFields(D))
217 return;
Mike Stump11289f42009-09-09 15:08:12 +0000218
Anders Carlsson307846f2009-07-23 03:17:50 +0000219 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlssond78fc892009-07-23 17:24:40 +0000220 Packed = true;
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000221 LastLaidOutBase.invalidate();
John McCall4d9f1422011-02-15 22:21:29 +0000222 NextFieldOffset = CharUnits::Zero();
Anders Carlsson307846f2009-07-23 03:17:50 +0000223 FieldTypes.clear();
John McCall0217dfc22011-02-15 06:40:56 +0000224 Fields.clear();
225 BitFields.clear();
226 NonVirtualBases.clear();
227 VirtualBases.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000228
Anders Carlsson307846f2009-07-23 03:17:50 +0000229 LayoutFields(D);
230}
231
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000232CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
233 const FieldDecl *FD,
234 uint64_t FieldOffset,
235 uint64_t FieldSize,
236 uint64_t ContainingTypeSizeInBits,
237 unsigned ContainingTypeAlign) {
Eli Friedmanc24e2fb2012-06-27 21:19:48 +0000238 assert(ContainingTypeAlign && "Expected alignment to be specified");
239
Chris Lattner2192fe52011-07-18 04:24:23 +0000240 llvm::Type *Ty = Types.ConvertTypeForMem(FD->getType());
John McCall8a3c5552011-02-26 08:41:59 +0000241 CharUnits TypeSizeInBytes =
Micah Villmowdd31ca12012-10-08 16:25:52 +0000242 CharUnits::fromQuantity(Types.getDataLayout().getTypeAllocSize(Ty));
John McCall8a3c5552011-02-26 08:41:59 +0000243 uint64_t TypeSizeInBits = Types.getContext().toBits(TypeSizeInBytes);
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000244
Douglas Gregor6ab2fa82011-05-20 16:38:50 +0000245 bool IsSigned = FD->getType()->isSignedIntegerOrEnumerationType();
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000246
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000247 if (FieldSize > TypeSizeInBits) {
Anders Carlssond5f27b02010-04-17 22:54:57 +0000248 // We have a wide bit-field. The extra bits are only used for padding, so
249 // if we have a bitfield of type T, with size N:
250 //
251 // T t : N;
252 //
253 // We can just assume that it's:
254 //
255 // T t : sizeof(T);
256 //
257 FieldSize = TypeSizeInBits;
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000258 }
259
Chris Lattnerfb59c7c2011-02-17 22:09:58 +0000260 // in big-endian machines the first fields are in higher bit positions,
261 // so revert the offset. The byte offsets are reversed(back) later.
Micah Villmowdd31ca12012-10-08 16:25:52 +0000262 if (Types.getDataLayout().isBigEndian()) {
Chris Lattnerfb59c7c2011-02-17 22:09:58 +0000263 FieldOffset = ((ContainingTypeSizeInBits)-FieldOffset-FieldSize);
264 }
265
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000266 // Compute the access components. The policy we use is to start by attempting
267 // to access using the width of the bit-field type itself and to always access
268 // at aligned indices of that type. If such an access would fail because it
269 // extends past the bound of the type, then we reduce size to the next smaller
270 // power of two and retry. The current algorithm assumes pow2 sized types,
271 // although this is easy to fix.
272 //
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000273 assert(llvm::isPowerOf2_32(TypeSizeInBits) && "Unexpected type size!");
274 CGBitFieldInfo::AccessInfo Components[3];
275 unsigned NumComponents = 0;
Nick Lewyckyd2348d82011-03-22 17:35:47 +0000276 unsigned AccessedTargetBits = 0; // The number of target bits accessed.
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000277 unsigned AccessWidth = TypeSizeInBits; // The current access width to attempt.
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000278
Daniel Dunbard3f3d932011-06-21 18:54:46 +0000279 // If requested, widen the initial bit-field access to be register sized. The
280 // theory is that this is most likely to allow multiple accesses into the same
281 // structure to be coalesced, and that the backend should be smart enough to
282 // narrow the store if no coalescing is ever done.
283 //
284 // The subsequent code will handle align these access to common boundaries and
285 // guaranteeing that we do not access past the end of the structure.
286 if (Types.getCodeGenOpts().UseRegisterSizedBitfieldAccess) {
287 if (AccessWidth < Types.getTarget().getRegisterWidth())
288 AccessWidth = Types.getTarget().getRegisterWidth();
289 }
290
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000291 // Round down from the field offset to find the first access position that is
292 // at an aligned offset of the initial access type.
Daniel Dunbar59813772010-04-22 15:22:33 +0000293 uint64_t AccessStart = FieldOffset - (FieldOffset % AccessWidth);
294
295 // Adjust initial access size to fit within record.
John McCall8a3c5552011-02-26 08:41:59 +0000296 while (AccessWidth > Types.getTarget().getCharWidth() &&
Daniel Dunbar59813772010-04-22 15:22:33 +0000297 AccessStart + AccessWidth > ContainingTypeSizeInBits) {
298 AccessWidth >>= 1;
299 AccessStart = FieldOffset - (FieldOffset % AccessWidth);
300 }
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000301
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000302 while (AccessedTargetBits < FieldSize) {
303 // Check that we can access using a type of this size, without reading off
304 // the end of the structure. This can occur with packed structures and
305 // -fno-bitfield-type-align, for example.
306 if (AccessStart + AccessWidth > ContainingTypeSizeInBits) {
307 // If so, reduce access size to the next smaller power-of-two and retry.
308 AccessWidth >>= 1;
John McCall8a3c5552011-02-26 08:41:59 +0000309 assert(AccessWidth >= Types.getTarget().getCharWidth()
310 && "Cannot access under byte size!");
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000311 continue;
312 }
Daniel Dunbarb935b932010-04-13 20:58:55 +0000313
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000314 // Otherwise, add an access component.
Daniel Dunbarb935b932010-04-13 20:58:55 +0000315
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000316 // First, compute the bits inside this access which are part of the
317 // target. We are reading bits [AccessStart, AccessStart + AccessWidth); the
318 // intersection with [FieldOffset, FieldOffset + FieldSize) gives the bits
319 // in the target that we are reading.
Daniel Dunbar59813772010-04-22 15:22:33 +0000320 assert(FieldOffset < AccessStart + AccessWidth && "Invalid access start!");
321 assert(AccessStart < FieldOffset + FieldSize && "Invalid access start!");
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000322 uint64_t AccessBitsInFieldStart = std::max(AccessStart, FieldOffset);
323 uint64_t AccessBitsInFieldSize =
Daniel Dunbar59813772010-04-22 15:22:33 +0000324 std::min(AccessWidth + AccessStart,
325 FieldOffset + FieldSize) - AccessBitsInFieldStart;
Daniel Dunbar5d6c07e2010-04-22 14:56:10 +0000326
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000327 assert(NumComponents < 3 && "Unexpected number of components!");
328 CGBitFieldInfo::AccessInfo &AI = Components[NumComponents++];
329 AI.FieldIndex = 0;
330 // FIXME: We still follow the old access pattern of only using the field
331 // byte offset. We should switch this once we fix the struct layout to be
332 // pretty.
Chris Lattnerfb59c7c2011-02-17 22:09:58 +0000333
334 // on big-endian machines we reverted the bit offset because first fields are
335 // in higher bits. But this also reverts the bytes, so fix this here by reverting
336 // the byte offset on big-endian machines.
Micah Villmowdd31ca12012-10-08 16:25:52 +0000337 if (Types.getDataLayout().isBigEndian()) {
Ken Dyckf76759c2011-04-24 10:04:59 +0000338 AI.FieldByteOffset = Types.getContext().toCharUnitsFromBits(
339 ContainingTypeSizeInBits - AccessStart - AccessWidth);
Chris Lattnerfb59c7c2011-02-17 22:09:58 +0000340 } else {
Ken Dyckf76759c2011-04-24 10:04:59 +0000341 AI.FieldByteOffset = Types.getContext().toCharUnitsFromBits(AccessStart);
Chris Lattnerfb59c7c2011-02-17 22:09:58 +0000342 }
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000343 AI.FieldBitStart = AccessBitsInFieldStart - AccessStart;
344 AI.AccessWidth = AccessWidth;
Ken Dyck27337a82011-04-24 10:13:17 +0000345 AI.AccessAlignment = Types.getContext().toCharUnitsFromBits(
346 llvm::MinAlign(ContainingTypeAlign, AccessStart));
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000347 AI.TargetBitOffset = AccessedTargetBits;
348 AI.TargetBitWidth = AccessBitsInFieldSize;
349
350 AccessStart += AccessWidth;
351 AccessedTargetBits += AI.TargetBitWidth;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000352 }
353
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000354 assert(AccessedTargetBits == FieldSize && "Invalid bit-field access!");
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000355 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000356}
357
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000358CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
359 const FieldDecl *FD,
360 uint64_t FieldOffset,
361 uint64_t FieldSize) {
362 const RecordDecl *RD = FD->getParent();
363 const ASTRecordLayout &RL = Types.getContext().getASTRecordLayout(RD);
Ken Dyckb0fcc592011-02-11 01:54:29 +0000364 uint64_t ContainingTypeSizeInBits = Types.getContext().toBits(RL.getSize());
Ken Dyck7ad11e72011-02-15 02:32:40 +0000365 unsigned ContainingTypeAlign = Types.getContext().toBits(RL.getAlignment());
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000366
367 return MakeInfo(Types, FD, FieldOffset, FieldSize, ContainingTypeSizeInBits,
368 ContainingTypeAlign);
369}
370
Anders Carlsson307846f2009-07-23 03:17:50 +0000371void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
John McCall4d9f1422011-02-15 22:21:29 +0000372 uint64_t fieldOffset) {
Richard Smithcaf33902011-10-10 18:28:20 +0000373 uint64_t fieldSize = D->getBitWidthValue(Types.getContext());
Mike Stump11289f42009-09-09 15:08:12 +0000374
John McCall4d9f1422011-02-15 22:21:29 +0000375 if (fieldSize == 0)
Anders Carlsson307846f2009-07-23 03:17:50 +0000376 return;
377
John McCall8a3c5552011-02-26 08:41:59 +0000378 uint64_t nextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset);
Ken Dyck345a6de2011-04-24 16:40:29 +0000379 CharUnits numBytesToAppend;
Douglas Gregore8bbc122011-09-02 00:18:52 +0000380 unsigned charAlign = Types.getContext().getTargetInfo().getCharAlign();
Mike Stump11289f42009-09-09 15:08:12 +0000381
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000382 if (fieldOffset < nextFieldOffsetInBits && !BitsAvailableInLastField) {
Ken Dyck345a6de2011-04-24 16:40:29 +0000383 assert(fieldOffset % charAlign == 0 &&
384 "Field offset not aligned correctly");
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000385
386 CharUnits fieldOffsetInCharUnits =
387 Types.getContext().toCharUnitsFromBits(fieldOffset);
388
389 // Try to resize the last base field.
390 if (ResizeLastBaseFieldIfNecessary(fieldOffsetInCharUnits))
391 nextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset);
392 }
393
John McCall4d9f1422011-02-15 22:21:29 +0000394 if (fieldOffset < nextFieldOffsetInBits) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000395 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
John McCall4d9f1422011-02-15 22:21:29 +0000396 assert(!NextFieldOffset.isZero() && "Must have laid out at least one byte");
Mike Stump11289f42009-09-09 15:08:12 +0000397
Anders Carlsson307846f2009-07-23 03:17:50 +0000398 // The bitfield begins in the previous bit-field.
Ken Dyck345a6de2011-04-24 16:40:29 +0000399 numBytesToAppend = Types.getContext().toCharUnitsFromBits(
400 llvm::RoundUpToAlignment(fieldSize - BitsAvailableInLastField,
401 charAlign));
Anders Carlsson307846f2009-07-23 03:17:50 +0000402 } else {
Ken Dyck345a6de2011-04-24 16:40:29 +0000403 assert(fieldOffset % charAlign == 0 &&
404 "Field offset not aligned correctly");
Anders Carlsson307846f2009-07-23 03:17:50 +0000405
406 // Append padding if necessary.
Ken Dyck345a6de2011-04-24 16:40:29 +0000407 AppendPadding(Types.getContext().toCharUnitsFromBits(fieldOffset),
408 CharUnits::One());
Mike Stump11289f42009-09-09 15:08:12 +0000409
Ken Dyck345a6de2011-04-24 16:40:29 +0000410 numBytesToAppend = Types.getContext().toCharUnitsFromBits(
411 llvm::RoundUpToAlignment(fieldSize, charAlign));
Mike Stump11289f42009-09-09 15:08:12 +0000412
Ken Dyck345a6de2011-04-24 16:40:29 +0000413 assert(!numBytesToAppend.isZero() && "No bytes to append!");
Anders Carlsson307846f2009-07-23 03:17:50 +0000414 }
415
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000416 // Add the bit field info.
John McCall0217dfc22011-02-15 06:40:56 +0000417 BitFields.insert(std::make_pair(D,
John McCall4d9f1422011-02-15 22:21:29 +0000418 CGBitFieldInfo::MakeInfo(Types, D, fieldOffset, fieldSize)));
Mike Stump11289f42009-09-09 15:08:12 +0000419
Ken Dyck345a6de2011-04-24 16:40:29 +0000420 AppendBytes(numBytesToAppend);
Mike Stump11289f42009-09-09 15:08:12 +0000421
Mike Stump11289f42009-09-09 15:08:12 +0000422 BitsAvailableInLastField =
Ken Dyck345a6de2011-04-24 16:40:29 +0000423 Types.getContext().toBits(NextFieldOffset) - (fieldOffset + fieldSize);
Anders Carlsson307846f2009-07-23 03:17:50 +0000424}
425
426bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
John McCall4d9f1422011-02-15 22:21:29 +0000427 uint64_t fieldOffset) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000428 // If the field is packed, then we need a packed struct.
Anders Carlsson68e0b682009-08-08 18:23:56 +0000429 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson307846f2009-07-23 03:17:50 +0000430 return false;
431
432 if (D->isBitField()) {
433 // We must use packed structs for unnamed bit fields since they
434 // don't affect the struct alignment.
Anders Carlssond78fc892009-07-23 17:24:40 +0000435 if (!Packed && !D->getDeclName())
Anders Carlsson307846f2009-07-23 03:17:50 +0000436 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000437
John McCall4d9f1422011-02-15 22:21:29 +0000438 LayoutBitField(D, fieldOffset);
Anders Carlsson307846f2009-07-23 03:17:50 +0000439 return true;
440 }
Mike Stump11289f42009-09-09 15:08:12 +0000441
John McCall614dbdc2010-08-22 21:01:12 +0000442 CheckZeroInitializable(D->getType());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000443
John McCall8a3c5552011-02-26 08:41:59 +0000444 assert(fieldOffset % Types.getTarget().getCharWidth() == 0
445 && "field offset is not on a byte boundary!");
446 CharUnits fieldOffsetInBytes
447 = Types.getContext().toCharUnitsFromBits(fieldOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000448
Chris Lattnera5f58b02011-07-09 17:41:47 +0000449 llvm::Type *Ty = Types.ConvertTypeForMem(D->getType());
John McCall4d9f1422011-02-15 22:21:29 +0000450 CharUnits typeAlignment = getTypeAlignment(Ty);
Anders Carlsson19702bb2009-08-04 16:29:15 +0000451
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000452 // If the type alignment is larger then the struct alignment, we must use
453 // a packed struct.
John McCall4d9f1422011-02-15 22:21:29 +0000454 if (typeAlignment > Alignment) {
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000455 assert(!Packed && "Alignment is wrong even with packed struct!");
456 return false;
457 }
Mike Stump11289f42009-09-09 15:08:12 +0000458
John McCall4d9f1422011-02-15 22:21:29 +0000459 if (!Packed) {
460 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
461 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
462 if (const MaxFieldAlignmentAttr *MFAA =
463 RD->getAttr<MaxFieldAlignmentAttr>()) {
John McCall8a3c5552011-02-26 08:41:59 +0000464 if (MFAA->getAlignment() != Types.getContext().toBits(typeAlignment))
John McCall4d9f1422011-02-15 22:21:29 +0000465 return false;
466 }
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000467 }
468 }
469
Anders Carlsson19702bb2009-08-04 16:29:15 +0000470 // Round up the field offset to the alignment of the field type.
John McCall4d9f1422011-02-15 22:21:29 +0000471 CharUnits alignedNextFieldOffsetInBytes =
472 NextFieldOffset.RoundUpToAlignment(typeAlignment);
Anders Carlsson19702bb2009-08-04 16:29:15 +0000473
John McCall4d9f1422011-02-15 22:21:29 +0000474 if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000475 // Try to resize the last base field.
476 if (ResizeLastBaseFieldIfNecessary(fieldOffsetInBytes)) {
477 alignedNextFieldOffsetInBytes =
478 NextFieldOffset.RoundUpToAlignment(typeAlignment);
479 }
480 }
481
482 if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
Anders Carlsson19702bb2009-08-04 16:29:15 +0000483 assert(!Packed && "Could not place field even with packed struct!");
484 return false;
485 }
Mike Stump11289f42009-09-09 15:08:12 +0000486
John McCall4d9f1422011-02-15 22:21:29 +0000487 AppendPadding(fieldOffsetInBytes, typeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +0000488
Anders Carlsson307846f2009-07-23 03:17:50 +0000489 // Now append the field.
John McCall0217dfc22011-02-15 06:40:56 +0000490 Fields[D] = FieldTypes.size();
John McCall4d9f1422011-02-15 22:21:29 +0000491 AppendField(fieldOffsetInBytes, Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000492
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000493 LastLaidOutBase.invalidate();
Anders Carlsson307846f2009-07-23 03:17:50 +0000494 return true;
495}
496
Chris Lattnera5f58b02011-07-09 17:41:47 +0000497llvm::Type *
Anders Carlsson1de2f572010-04-17 20:49:27 +0000498CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
499 const ASTRecordLayout &Layout) {
500 if (Field->isBitField()) {
Richard Smithcaf33902011-10-10 18:28:20 +0000501 uint64_t FieldSize = Field->getBitWidthValue(Types.getContext());
Anders Carlsson1de2f572010-04-17 20:49:27 +0000502
503 // Ignore zero sized bit fields.
504 if (FieldSize == 0)
505 return 0;
506
Chris Lattnera5f58b02011-07-09 17:41:47 +0000507 llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
Ken Dyck7a0b19f2011-04-24 16:47:33 +0000508 CharUnits NumBytesToAppend = Types.getContext().toCharUnitsFromBits(
509 llvm::RoundUpToAlignment(FieldSize,
Douglas Gregore8bbc122011-09-02 00:18:52 +0000510 Types.getContext().getTargetInfo().getCharAlign()));
Anders Carlsson2295f132010-04-17 21:04:52 +0000511
Ken Dyck7a0b19f2011-04-24 16:47:33 +0000512 if (NumBytesToAppend > CharUnits::One())
513 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend.getQuantity());
Anders Carlsson2295f132010-04-17 21:04:52 +0000514
Anders Carlsson1de2f572010-04-17 20:49:27 +0000515 // Add the bit field info.
John McCall0217dfc22011-02-15 06:40:56 +0000516 BitFields.insert(std::make_pair(Field,
517 CGBitFieldInfo::MakeInfo(Types, Field, 0, FieldSize)));
Anders Carlsson2295f132010-04-17 21:04:52 +0000518 return FieldTy;
Anders Carlsson1de2f572010-04-17 20:49:27 +0000519 }
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000520
Anders Carlsson1de2f572010-04-17 20:49:27 +0000521 // This is a regular union field.
John McCall0217dfc22011-02-15 06:40:56 +0000522 Fields[Field] = 0;
Chris Lattnera5f58b02011-07-09 17:41:47 +0000523 return Types.ConvertTypeForMem(Field->getType());
Anders Carlsson1de2f572010-04-17 20:49:27 +0000524}
525
Anders Carlsson697f6592009-07-23 03:43:54 +0000526void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
527 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump11289f42009-09-09 15:08:12 +0000528
John McCall4d9f1422011-02-15 22:21:29 +0000529 const ASTRecordLayout &layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000530
Chris Lattnera5f58b02011-07-09 17:41:47 +0000531 llvm::Type *unionType = 0;
John McCall4d9f1422011-02-15 22:21:29 +0000532 CharUnits unionSize = CharUnits::Zero();
533 CharUnits unionAlign = CharUnits::Zero();
Mike Stump11289f42009-09-09 15:08:12 +0000534
John McCall4d9f1422011-02-15 22:21:29 +0000535 bool hasOnlyZeroSizedBitFields = true;
Eli Friedmandae858a2011-12-07 01:30:11 +0000536 bool checkedFirstFieldZeroInit = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000537
John McCall4d9f1422011-02-15 22:21:29 +0000538 unsigned fieldNo = 0;
539 for (RecordDecl::field_iterator field = D->field_begin(),
540 fieldEnd = D->field_end(); field != fieldEnd; ++field, ++fieldNo) {
541 assert(layout.getFieldOffset(fieldNo) == 0 &&
Anders Carlsson697f6592009-07-23 03:43:54 +0000542 "Union field offset did not start at the beginning of record!");
David Blaikie40ed2972012-06-06 20:45:41 +0000543 llvm::Type *fieldType = LayoutUnionField(*field, layout);
Anders Carlssonf814ee62009-07-23 04:00:39 +0000544
John McCall4d9f1422011-02-15 22:21:29 +0000545 if (!fieldType)
Anders Carlsson1de2f572010-04-17 20:49:27 +0000546 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000547
Eli Friedmandae858a2011-12-07 01:30:11 +0000548 if (field->getDeclName() && !checkedFirstFieldZeroInit) {
549 CheckZeroInitializable(field->getType());
550 checkedFirstFieldZeroInit = true;
551 }
552
John McCall4d9f1422011-02-15 22:21:29 +0000553 hasOnlyZeroSizedBitFields = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000554
John McCall4d9f1422011-02-15 22:21:29 +0000555 CharUnits fieldAlign = CharUnits::fromQuantity(
Micah Villmowdd31ca12012-10-08 16:25:52 +0000556 Types.getDataLayout().getABITypeAlignment(fieldType));
John McCall4d9f1422011-02-15 22:21:29 +0000557 CharUnits fieldSize = CharUnits::fromQuantity(
Micah Villmowdd31ca12012-10-08 16:25:52 +0000558 Types.getDataLayout().getTypeAllocSize(fieldType));
Mike Stump11289f42009-09-09 15:08:12 +0000559
John McCall4d9f1422011-02-15 22:21:29 +0000560 if (fieldAlign < unionAlign)
Anders Carlsson697f6592009-07-23 03:43:54 +0000561 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000562
John McCall4d9f1422011-02-15 22:21:29 +0000563 if (fieldAlign > unionAlign || fieldSize > unionSize) {
564 unionType = fieldType;
565 unionAlign = fieldAlign;
566 unionSize = fieldSize;
Anders Carlsson697f6592009-07-23 03:43:54 +0000567 }
568 }
Mike Stump11289f42009-09-09 15:08:12 +0000569
Anders Carlsson697f6592009-07-23 03:43:54 +0000570 // Now add our field.
John McCall4d9f1422011-02-15 22:21:29 +0000571 if (unionType) {
572 AppendField(CharUnits::Zero(), unionType);
Anders Carlsson0e912752009-09-03 22:56:02 +0000573
John McCall4d9f1422011-02-15 22:21:29 +0000574 if (getTypeAlignment(unionType) > layout.getAlignment()) {
Anders Carlsson0e912752009-09-03 22:56:02 +0000575 // We need a packed struct.
576 Packed = true;
John McCall4d9f1422011-02-15 22:21:29 +0000577 unionAlign = CharUnits::One();
Anders Carlsson0e912752009-09-03 22:56:02 +0000578 }
579 }
John McCall4d9f1422011-02-15 22:21:29 +0000580 if (unionAlign.isZero()) {
Chandler Carruth52b6ac22012-03-04 12:16:40 +0000581 (void)hasOnlyZeroSizedBitFields;
John McCall4d9f1422011-02-15 22:21:29 +0000582 assert(hasOnlyZeroSizedBitFields &&
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000583 "0-align record did not have all zero-sized bit-fields!");
John McCall4d9f1422011-02-15 22:21:29 +0000584 unionAlign = CharUnits::One();
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000585 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000586
Anders Carlsson697f6592009-07-23 03:43:54 +0000587 // Append tail padding.
John McCall4d9f1422011-02-15 22:21:29 +0000588 CharUnits recordSize = layout.getSize();
589 if (recordSize > unionSize)
590 AppendPadding(recordSize, unionAlign);
Anders Carlsson697f6592009-07-23 03:43:54 +0000591}
592
Eli Friedman3c840aa2011-12-12 23:13:20 +0000593bool CGRecordLayoutBuilder::LayoutBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000594 const CGRecordLayout &baseLayout,
595 CharUnits baseOffset) {
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000596 ResizeLastBaseFieldIfNecessary(baseOffset);
597
John McCall4d9f1422011-02-15 22:21:29 +0000598 AppendPadding(baseOffset, CharUnits::One());
Anders Carlssona518b2a2010-12-04 23:59:48 +0000599
John McCall0217dfc22011-02-15 06:40:56 +0000600 const ASTRecordLayout &baseASTLayout
601 = Types.getContext().getASTRecordLayout(base);
602
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000603 LastLaidOutBase.Offset = NextFieldOffset;
604 LastLaidOutBase.NonVirtualSize = baseASTLayout.getNonVirtualSize();
605
Chris Lattnera5f58b02011-07-09 17:41:47 +0000606 llvm::StructType *subobjectType = baseLayout.getBaseSubobjectLLVMType();
Eli Friedman3c840aa2011-12-12 23:13:20 +0000607 if (getTypeAlignment(subobjectType) > Alignment)
608 return false;
609
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000610 AppendField(baseOffset, subobjectType);
Eli Friedman3c840aa2011-12-12 23:13:20 +0000611 return true;
John McCall0217dfc22011-02-15 06:40:56 +0000612}
613
Eli Friedman3c840aa2011-12-12 23:13:20 +0000614bool CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000615 CharUnits baseOffset) {
John McCall0217dfc22011-02-15 06:40:56 +0000616 // Ignore empty bases.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000617 if (base->isEmpty()) return true;
John McCall0217dfc22011-02-15 06:40:56 +0000618
619 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
620 if (IsZeroInitializableAsBase) {
621 assert(IsZeroInitializable &&
622 "class zero-initializable as base but not as complete object");
623
624 IsZeroInitializable = IsZeroInitializableAsBase =
625 baseLayout.isZeroInitializableAsBase();
626 }
627
Eli Friedman3c840aa2011-12-12 23:13:20 +0000628 if (!LayoutBase(base, baseLayout, baseOffset))
629 return false;
John McCall0217dfc22011-02-15 06:40:56 +0000630 NonVirtualBases[base] = (FieldTypes.size() - 1);
Eli Friedman3c840aa2011-12-12 23:13:20 +0000631 return true;
Anders Carlssona518b2a2010-12-04 23:59:48 +0000632}
633
Eli Friedman3c840aa2011-12-12 23:13:20 +0000634bool
John McCall0217dfc22011-02-15 06:40:56 +0000635CGRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000636 CharUnits baseOffset) {
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000637 // Ignore empty bases.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000638 if (base->isEmpty()) return true;
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000639
John McCall0217dfc22011-02-15 06:40:56 +0000640 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
641 if (IsZeroInitializable)
642 IsZeroInitializable = baseLayout.isZeroInitializableAsBase();
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000643
Eli Friedman3c840aa2011-12-12 23:13:20 +0000644 if (!LayoutBase(base, baseLayout, baseOffset))
645 return false;
John McCall0217dfc22011-02-15 06:40:56 +0000646 VirtualBases[base] = (FieldTypes.size() - 1);
Eli Friedman3c840aa2011-12-12 23:13:20 +0000647 return true;
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000648}
649
Eli Friedman3c840aa2011-12-12 23:13:20 +0000650bool
John McCall0153cd32011-11-08 04:01:03 +0000651CGRecordLayoutBuilder::MSLayoutVirtualBases(const CXXRecordDecl *RD,
652 const ASTRecordLayout &Layout) {
653 if (!RD->getNumVBases())
Eli Friedman3c840aa2011-12-12 23:13:20 +0000654 return true;
John McCall0153cd32011-11-08 04:01:03 +0000655
656 // The vbases list is uniqued and ordered by a depth-first
657 // traversal, which is what we need here.
658 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
659 E = RD->vbases_end(); I != E; ++I) {
660
661 const CXXRecordDecl *BaseDecl =
662 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
663
664 CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl);
Eli Friedman3c840aa2011-12-12 23:13:20 +0000665 if (!LayoutVirtualBase(BaseDecl, vbaseOffset))
666 return false;
John McCall0153cd32011-11-08 04:01:03 +0000667 }
Eli Friedman3c840aa2011-12-12 23:13:20 +0000668 return true;
John McCall0153cd32011-11-08 04:01:03 +0000669}
670
Anders Carlssona459adb2010-11-28 19:18:44 +0000671/// LayoutVirtualBases - layout the non-virtual bases of a record decl.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000672bool
Anders Carlssona459adb2010-11-28 19:18:44 +0000673CGRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
674 const ASTRecordLayout &Layout) {
675 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
676 E = RD->bases_end(); I != E; ++I) {
677 const CXXRecordDecl *BaseDecl =
678 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
679
680 // We only want to lay out virtual bases that aren't indirect primary bases
681 // of some other base.
682 if (I->isVirtual() && !IndirectPrimaryBases.count(BaseDecl)) {
683 // Only lay out the base once.
684 if (!LaidOutVirtualBases.insert(BaseDecl))
685 continue;
686
John McCall4d9f1422011-02-15 22:21:29 +0000687 CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl);
Eli Friedman3c840aa2011-12-12 23:13:20 +0000688 if (!LayoutVirtualBase(BaseDecl, vbaseOffset))
689 return false;
Anders Carlssona459adb2010-11-28 19:18:44 +0000690 }
691
692 if (!BaseDecl->getNumVBases()) {
693 // This base isn't interesting since it doesn't have any virtual bases.
694 continue;
695 }
696
Eli Friedman3c840aa2011-12-12 23:13:20 +0000697 if (!LayoutVirtualBases(BaseDecl, Layout))
698 return false;
Anders Carlssona459adb2010-11-28 19:18:44 +0000699 }
Eli Friedman3c840aa2011-12-12 23:13:20 +0000700 return true;
Anders Carlssona459adb2010-11-28 19:18:44 +0000701}
702
Eli Friedman3c840aa2011-12-12 23:13:20 +0000703bool
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000704CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD,
705 const ASTRecordLayout &Layout) {
706 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
707
John McCall0153cd32011-11-08 04:01:03 +0000708 // If we have a primary base, lay it out first.
709 if (PrimaryBase) {
Eli Friedman3c840aa2011-12-12 23:13:20 +0000710 if (!Layout.isPrimaryBaseVirtual()) {
711 if (!LayoutNonVirtualBase(PrimaryBase, CharUnits::Zero()))
712 return false;
713 } else {
714 if (!LayoutVirtualBase(PrimaryBase, CharUnits::Zero()))
715 return false;
716 }
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000717
John McCall0153cd32011-11-08 04:01:03 +0000718 // Otherwise, add a vtable / vf-table if the layout says to do so.
John McCalle42a3362012-05-01 08:55:32 +0000719 } else if (Layout.hasOwnVFPtr()) {
John McCall0153cd32011-11-08 04:01:03 +0000720 llvm::Type *FunctionType =
721 llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()),
722 /*isVarArg=*/true);
723 llvm::Type *VTableTy = FunctionType->getPointerTo();
Eli Friedmanf927b8b2012-04-27 02:34:46 +0000724
725 if (getTypeAlignment(VTableTy) > Alignment) {
726 // FIXME: Should we allow this to happen in Sema?
727 assert(!Packed && "Alignment is wrong even with packed struct!");
728 return false;
729 }
730
John McCall0153cd32011-11-08 04:01:03 +0000731 assert(NextFieldOffset.isZero() &&
732 "VTable pointer must come first!");
733 AppendField(CharUnits::Zero(), VTableTy->getPointerTo());
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000734 }
735
736 // Layout the non-virtual bases.
737 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
738 E = RD->bases_end(); I != E; ++I) {
739 if (I->isVirtual())
740 continue;
741
742 const CXXRecordDecl *BaseDecl =
743 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
744
745 // We've already laid out the primary base.
Anders Carlsson7f95cd12010-11-24 23:12:57 +0000746 if (BaseDecl == PrimaryBase && !Layout.isPrimaryBaseVirtual())
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000747 continue;
748
Eli Friedman3c840aa2011-12-12 23:13:20 +0000749 if (!LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffset(BaseDecl)))
750 return false;
Anders Carlssond681a292009-12-16 17:27:20 +0000751 }
John McCall0153cd32011-11-08 04:01:03 +0000752
753 // Add a vb-table pointer if the layout insists.
754 if (Layout.getVBPtrOffset() != CharUnits::fromQuantity(-1)) {
755 CharUnits VBPtrOffset = Layout.getVBPtrOffset();
756 llvm::Type *Vbptr = llvm::Type::getInt32PtrTy(Types.getLLVMContext());
757 AppendPadding(VBPtrOffset, getTypeAlignment(Vbptr));
758 AppendField(VBPtrOffset, Vbptr);
759 }
Eli Friedman3c840aa2011-12-12 23:13:20 +0000760
761 return true;
Anders Carlssond681a292009-12-16 17:27:20 +0000762}
763
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000764bool
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000765CGRecordLayoutBuilder::ComputeNonVirtualBaseType(const CXXRecordDecl *RD) {
766 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(RD);
767
Ken Dyckbec02852011-02-08 02:02:47 +0000768 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
769 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
John McCall4d9f1422011-02-15 22:21:29 +0000770 CharUnits AlignedNonVirtualTypeSize =
771 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000772
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000773 // First check if we can use the same fields as for the complete class.
John McCall4d9f1422011-02-15 22:21:29 +0000774 CharUnits RecordSize = Layout.getSize();
John McCall0217dfc22011-02-15 06:40:56 +0000775 if (AlignedNonVirtualTypeSize == RecordSize)
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000776 return true;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000777
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000778 // Check if we need padding.
John McCall4d9f1422011-02-15 22:21:29 +0000779 CharUnits AlignedNextFieldOffset =
780 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000781
John McCall0217dfc22011-02-15 06:40:56 +0000782 if (AlignedNextFieldOffset > AlignedNonVirtualTypeSize) {
783 assert(!Packed && "cannot layout even as packed struct");
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000784 return false; // Needs packing.
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000785 }
786
John McCall0217dfc22011-02-15 06:40:56 +0000787 bool needsPadding = (AlignedNonVirtualTypeSize != AlignedNextFieldOffset);
788 if (needsPadding) {
John McCall4d9f1422011-02-15 22:21:29 +0000789 CharUnits NumBytes = AlignedNonVirtualTypeSize - AlignedNextFieldOffset;
John McCall0217dfc22011-02-15 06:40:56 +0000790 FieldTypes.push_back(getByteArrayType(NumBytes));
791 }
Chris Lattnera5f58b02011-07-09 17:41:47 +0000792
Chris Lattner5ec04a52011-08-12 17:43:31 +0000793 BaseSubobjectType = llvm::StructType::create(Types.getLLVMContext(),
794 FieldTypes, "", Packed);
Chris Lattnera5f58b02011-07-09 17:41:47 +0000795 Types.addRecordTypeName(RD, BaseSubobjectType, ".base");
John McCall0217dfc22011-02-15 06:40:56 +0000796
Chris Lattnera5f58b02011-07-09 17:41:47 +0000797 // Pull the padding back off.
798 if (needsPadding)
John McCall0217dfc22011-02-15 06:40:56 +0000799 FieldTypes.pop_back();
John McCall0217dfc22011-02-15 06:40:56 +0000800
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000801 return true;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000802}
803
Anders Carlsson307846f2009-07-23 03:17:50 +0000804bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
805 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
John McCall4d9f1422011-02-15 22:21:29 +0000806 assert(!Alignment.isZero() && "Did not set alignment!");
Mike Stump11289f42009-09-09 15:08:12 +0000807
Anders Carlsson697f6592009-07-23 03:43:54 +0000808 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000809
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000810 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
811 if (RD)
Eli Friedman3c840aa2011-12-12 23:13:20 +0000812 if (!LayoutNonVirtualBases(RD, Layout))
813 return false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000814
Anders Carlsson307846f2009-07-23 03:17:50 +0000815 unsigned FieldNo = 0;
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000816 const FieldDecl *LastFD = 0;
817
Mike Stump11289f42009-09-09 15:08:12 +0000818 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson307846f2009-07-23 03:17:50 +0000819 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000820 if (IsMsStruct) {
821 // Zero-length bitfields following non-bitfield members are
822 // ignored:
David Blaikie40ed2972012-06-06 20:45:41 +0000823 const FieldDecl *FD = *Field;
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +0000824 if (Types.getContext().ZeroBitfieldFollowsNonBitfield(FD, LastFD)) {
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000825 --FieldNo;
826 continue;
827 }
828 LastFD = FD;
829 }
830
David Blaikie40ed2972012-06-06 20:45:41 +0000831 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump11289f42009-09-09 15:08:12 +0000832 assert(!Packed &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000833 "Could not layout fields even with a packed LLVM struct!");
834 return false;
835 }
836 }
837
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000838 if (RD) {
Anders Carlssona459adb2010-11-28 19:18:44 +0000839 // We've laid out the non-virtual bases and the fields, now compute the
840 // non-virtual base field types.
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000841 if (!ComputeNonVirtualBaseType(RD)) {
842 assert(!Packed && "Could not layout even with a packed LLVM struct!");
843 return false;
844 }
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000845
John McCall0153cd32011-11-08 04:01:03 +0000846 // Lay out the virtual bases. The MS ABI uses a different
847 // algorithm here due to the lack of primary virtual bases.
848 if (Types.getContext().getTargetInfo().getCXXABI() != CXXABI_Microsoft) {
849 RD->getIndirectPrimaryBases(IndirectPrimaryBases);
850 if (Layout.isPrimaryBaseVirtual())
851 IndirectPrimaryBases.insert(Layout.getPrimaryBase());
852
Eli Friedman3c840aa2011-12-12 23:13:20 +0000853 if (!LayoutVirtualBases(RD, Layout))
854 return false;
John McCall0153cd32011-11-08 04:01:03 +0000855 } else {
Eli Friedman3c840aa2011-12-12 23:13:20 +0000856 if (!MSLayoutVirtualBases(RD, Layout))
857 return false;
John McCall0153cd32011-11-08 04:01:03 +0000858 }
Anders Carlssona459adb2010-11-28 19:18:44 +0000859 }
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000860
Anders Carlsson307846f2009-07-23 03:17:50 +0000861 // Append tail padding if necessary.
Ken Dyck272b6fa2011-04-24 16:53:44 +0000862 AppendTailPadding(Layout.getSize());
Mike Stump11289f42009-09-09 15:08:12 +0000863
Anders Carlsson307846f2009-07-23 03:17:50 +0000864 return true;
865}
866
Ken Dyck272b6fa2011-04-24 16:53:44 +0000867void CGRecordLayoutBuilder::AppendTailPadding(CharUnits RecordSize) {
868 ResizeLastBaseFieldIfNecessary(RecordSize);
Mike Stump11289f42009-09-09 15:08:12 +0000869
Ken Dyck272b6fa2011-04-24 16:53:44 +0000870 assert(NextFieldOffset <= RecordSize && "Size mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000871
John McCall4d9f1422011-02-15 22:21:29 +0000872 CharUnits AlignedNextFieldOffset =
873 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
Anders Carlsson220bf4f2009-12-08 01:24:23 +0000874
Ken Dyck272b6fa2011-04-24 16:53:44 +0000875 if (AlignedNextFieldOffset == RecordSize) {
Anders Carlsson220bf4f2009-12-08 01:24:23 +0000876 // We don't need any padding.
877 return;
878 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000879
Ken Dyck272b6fa2011-04-24 16:53:44 +0000880 CharUnits NumPadBytes = RecordSize - NextFieldOffset;
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000881 AppendBytes(NumPadBytes);
882}
883
John McCall4d9f1422011-02-15 22:21:29 +0000884void CGRecordLayoutBuilder::AppendField(CharUnits fieldOffset,
Chris Lattnera5f58b02011-07-09 17:41:47 +0000885 llvm::Type *fieldType) {
John McCall4d9f1422011-02-15 22:21:29 +0000886 CharUnits fieldSize =
Micah Villmowdd31ca12012-10-08 16:25:52 +0000887 CharUnits::fromQuantity(Types.getDataLayout().getTypeAllocSize(fieldType));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000888
John McCall4d9f1422011-02-15 22:21:29 +0000889 FieldTypes.push_back(fieldType);
Anders Carlsson307846f2009-07-23 03:17:50 +0000890
John McCall4d9f1422011-02-15 22:21:29 +0000891 NextFieldOffset = fieldOffset + fieldSize;
Anders Carlsson307846f2009-07-23 03:17:50 +0000892 BitsAvailableInLastField = 0;
893}
894
John McCall4d9f1422011-02-15 22:21:29 +0000895void CGRecordLayoutBuilder::AppendPadding(CharUnits fieldOffset,
896 CharUnits fieldAlignment) {
897 assert(NextFieldOffset <= fieldOffset &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000898 "Incorrect field layout!");
Mike Stump11289f42009-09-09 15:08:12 +0000899
John McCall0153cd32011-11-08 04:01:03 +0000900 // Do nothing if we're already at the right offset.
901 if (fieldOffset == NextFieldOffset) return;
Anders Carlsson307846f2009-07-23 03:17:50 +0000902
John McCall0153cd32011-11-08 04:01:03 +0000903 // If we're not emitting a packed LLVM type, try to avoid adding
904 // unnecessary padding fields.
905 if (!Packed) {
906 // Round up the field offset to the alignment of the field type.
907 CharUnits alignedNextFieldOffset =
908 NextFieldOffset.RoundUpToAlignment(fieldAlignment);
909 assert(alignedNextFieldOffset <= fieldOffset);
Anders Carlsson307846f2009-07-23 03:17:50 +0000910
John McCall0153cd32011-11-08 04:01:03 +0000911 // If that's the right offset, we're done.
912 if (alignedNextFieldOffset == fieldOffset) return;
Anders Carlsson307846f2009-07-23 03:17:50 +0000913 }
John McCall0153cd32011-11-08 04:01:03 +0000914
915 // Otherwise we need explicit padding.
916 CharUnits padding = fieldOffset - NextFieldOffset;
917 AppendBytes(padding);
Anders Carlsson307846f2009-07-23 03:17:50 +0000918}
919
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000920bool CGRecordLayoutBuilder::ResizeLastBaseFieldIfNecessary(CharUnits offset) {
921 // Check if we have a base to resize.
922 if (!LastLaidOutBase.isValid())
923 return false;
924
925 // This offset does not overlap with the tail padding.
926 if (offset >= NextFieldOffset)
927 return false;
928
929 // Restore the field offset and append an i8 array instead.
930 FieldTypes.pop_back();
931 NextFieldOffset = LastLaidOutBase.Offset;
932 AppendBytes(LastLaidOutBase.NonVirtualSize);
933 LastLaidOutBase.invalidate();
934
935 return true;
936}
937
Chris Lattnera5f58b02011-07-09 17:41:47 +0000938llvm::Type *CGRecordLayoutBuilder::getByteArrayType(CharUnits numBytes) {
John McCall4d9f1422011-02-15 22:21:29 +0000939 assert(!numBytes.isZero() && "Empty byte arrays aren't allowed.");
Mike Stump11289f42009-09-09 15:08:12 +0000940
Chris Lattnera5f58b02011-07-09 17:41:47 +0000941 llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
John McCall4d9f1422011-02-15 22:21:29 +0000942 if (numBytes > CharUnits::One())
943 Ty = llvm::ArrayType::get(Ty, numBytes.getQuantity());
Mike Stump11289f42009-09-09 15:08:12 +0000944
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000945 return Ty;
946}
947
John McCall4d9f1422011-02-15 22:21:29 +0000948void CGRecordLayoutBuilder::AppendBytes(CharUnits numBytes) {
949 if (numBytes.isZero())
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000950 return;
951
Anders Carlsson307846f2009-07-23 03:17:50 +0000952 // Append the padding field
John McCall4d9f1422011-02-15 22:21:29 +0000953 AppendField(NextFieldOffset, getByteArrayType(numBytes));
Anders Carlsson307846f2009-07-23 03:17:50 +0000954}
955
Chris Lattner2192fe52011-07-18 04:24:23 +0000956CharUnits CGRecordLayoutBuilder::getTypeAlignment(llvm::Type *Ty) const {
Anders Carlssond78fc892009-07-23 17:24:40 +0000957 if (Packed)
John McCall4d9f1422011-02-15 22:21:29 +0000958 return CharUnits::One();
Mike Stump11289f42009-09-09 15:08:12 +0000959
Micah Villmowdd31ca12012-10-08 16:25:52 +0000960 return CharUnits::fromQuantity(Types.getDataLayout().getABITypeAlignment(Ty));
Anders Carlsson307846f2009-07-23 03:17:50 +0000961}
962
John McCall4d9f1422011-02-15 22:21:29 +0000963CharUnits CGRecordLayoutBuilder::getAlignmentAsLLVMStruct() const {
Anders Carlssonacf877b2010-11-28 23:06:23 +0000964 if (Packed)
John McCall4d9f1422011-02-15 22:21:29 +0000965 return CharUnits::One();
Anders Carlssonacf877b2010-11-28 23:06:23 +0000966
John McCall4d9f1422011-02-15 22:21:29 +0000967 CharUnits maxAlignment = CharUnits::One();
Anders Carlssonacf877b2010-11-28 23:06:23 +0000968 for (size_t i = 0; i != FieldTypes.size(); ++i)
John McCall4d9f1422011-02-15 22:21:29 +0000969 maxAlignment = std::max(maxAlignment, getTypeAlignment(FieldTypes[i]));
Anders Carlssonacf877b2010-11-28 23:06:23 +0000970
John McCall4d9f1422011-02-15 22:21:29 +0000971 return maxAlignment;
Anders Carlssonacf877b2010-11-28 23:06:23 +0000972}
973
John McCall0217dfc22011-02-15 06:40:56 +0000974/// Merge in whether a field of the given type is zero-initializable.
John McCall614dbdc2010-08-22 21:01:12 +0000975void CGRecordLayoutBuilder::CheckZeroInitializable(QualType T) {
Anders Carlssond606de72009-08-23 01:25:01 +0000976 // This record already contains a member pointer.
John McCall0217dfc22011-02-15 06:40:56 +0000977 if (!IsZeroInitializableAsBase)
Anders Carlssond606de72009-08-23 01:25:01 +0000978 return;
Mike Stump11289f42009-09-09 15:08:12 +0000979
Anders Carlssond606de72009-08-23 01:25:01 +0000980 // Can only have member pointers if we're compiling C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000981 if (!Types.getContext().getLangOpts().CPlusPlus)
Anders Carlssond606de72009-08-23 01:25:01 +0000982 return;
Mike Stump11289f42009-09-09 15:08:12 +0000983
John McCall0217dfc22011-02-15 06:40:56 +0000984 const Type *elementType = T->getBaseElementTypeUnsafe();
Mike Stump11289f42009-09-09 15:08:12 +0000985
John McCall0217dfc22011-02-15 06:40:56 +0000986 if (const MemberPointerType *MPT = elementType->getAs<MemberPointerType>()) {
John McCall614dbdc2010-08-22 21:01:12 +0000987 if (!Types.getCXXABI().isZeroInitializable(MPT))
John McCall0217dfc22011-02-15 06:40:56 +0000988 IsZeroInitializable = IsZeroInitializableAsBase = false;
989 } else if (const RecordType *RT = elementType->getAs<RecordType>()) {
Anders Carlssone8bfe412010-02-02 05:17:25 +0000990 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
John McCall0217dfc22011-02-15 06:40:56 +0000991 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
992 if (!Layout.isZeroInitializable())
993 IsZeroInitializable = IsZeroInitializableAsBase = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000994 }
Anders Carlssond606de72009-08-23 01:25:01 +0000995}
996
Chris Lattnera5f58b02011-07-09 17:41:47 +0000997CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D,
998 llvm::StructType *Ty) {
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000999 CGRecordLayoutBuilder Builder(*this);
Mike Stump11289f42009-09-09 15:08:12 +00001000
Anders Carlsson307846f2009-07-23 03:17:50 +00001001 Builder.Layout(D);
Anders Carlssone1d5ca52009-07-24 15:20:52 +00001002
Chris Lattnera5f58b02011-07-09 17:41:47 +00001003 Ty->setBody(Builder.FieldTypes, Builder.Packed);
Mike Stump11289f42009-09-09 15:08:12 +00001004
John McCall0217dfc22011-02-15 06:40:56 +00001005 // If we're in C++, compute the base subobject type.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001006 llvm::StructType *BaseTy = 0;
Eli Friedman09d272d2012-01-13 03:58:31 +00001007 if (isa<CXXRecordDecl>(D) && !D->isUnion()) {
John McCall0217dfc22011-02-15 06:40:56 +00001008 BaseTy = Builder.BaseSubobjectType;
1009 if (!BaseTy) BaseTy = Ty;
Anders Carlssonc1351ca2010-11-09 05:25:47 +00001010 }
1011
Daniel Dunbar034299e2010-03-31 01:09:11 +00001012 CGRecordLayout *RL =
John McCall0217dfc22011-02-15 06:40:56 +00001013 new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable,
1014 Builder.IsZeroInitializableAsBase);
Daniel Dunbar034299e2010-03-31 01:09:11 +00001015
John McCall0217dfc22011-02-15 06:40:56 +00001016 RL->NonVirtualBases.swap(Builder.NonVirtualBases);
1017 RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases);
Anders Carlsson061ca522010-05-18 05:22:06 +00001018
Anders Carlsson307846f2009-07-23 03:17:50 +00001019 // Add all the field numbers.
John McCall0217dfc22011-02-15 06:40:56 +00001020 RL->FieldInfo.swap(Builder.Fields);
Anders Carlsson307846f2009-07-23 03:17:50 +00001021
1022 // Add bitfield info.
John McCall0217dfc22011-02-15 06:40:56 +00001023 RL->BitFields.swap(Builder.BitFields);
Mike Stump11289f42009-09-09 15:08:12 +00001024
Daniel Dunbar2ea51832010-04-19 20:44:47 +00001025 // Dump the layout, if requested.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001026 if (getContext().getLangOpts().DumpRecordLayouts) {
Daniel Dunbarccabe482010-04-19 20:44:53 +00001027 llvm::errs() << "\n*** Dumping IRgen Record Layout\n";
Daniel Dunbarb935b932010-04-13 20:58:55 +00001028 llvm::errs() << "Record: ";
1029 D->dump();
1030 llvm::errs() << "\nLayout: ";
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001031 RL->dump();
Daniel Dunbarb935b932010-04-13 20:58:55 +00001032 }
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001033
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001034#ifndef NDEBUG
Daniel Dunbar2ea51832010-04-19 20:44:47 +00001035 // Verify that the computed LLVM struct size matches the AST layout size.
Anders Carlssonc1351ca2010-11-09 05:25:47 +00001036 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
1037
Ken Dyckb0fcc592011-02-11 01:54:29 +00001038 uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize());
Micah Villmowdd31ca12012-10-08 16:25:52 +00001039 assert(TypeSizeInBits == getDataLayout().getTypeAllocSizeInBits(Ty) &&
Daniel Dunbar2ea51832010-04-19 20:44:47 +00001040 "Type size mismatch!");
1041
Anders Carlssonc1351ca2010-11-09 05:25:47 +00001042 if (BaseTy) {
Ken Dyckbec02852011-02-08 02:02:47 +00001043 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
1044 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
1045 CharUnits AlignedNonVirtualTypeSize =
1046 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
1047
1048 uint64_t AlignedNonVirtualTypeSizeInBits =
Ken Dyckb0fcc592011-02-11 01:54:29 +00001049 getContext().toBits(AlignedNonVirtualTypeSize);
Anders Carlssonc1351ca2010-11-09 05:25:47 +00001050
1051 assert(AlignedNonVirtualTypeSizeInBits ==
Micah Villmowdd31ca12012-10-08 16:25:52 +00001052 getDataLayout().getTypeAllocSizeInBits(BaseTy) &&
Anders Carlssonc1351ca2010-11-09 05:25:47 +00001053 "Type size mismatch!");
1054 }
1055
Daniel Dunbar2ba67442010-04-21 19:10:49 +00001056 // Verify that the LLVM and AST field offsets agree.
Chris Lattner2192fe52011-07-18 04:24:23 +00001057 llvm::StructType *ST =
Daniel Dunbar2ba67442010-04-21 19:10:49 +00001058 dyn_cast<llvm::StructType>(RL->getLLVMType());
Micah Villmowdd31ca12012-10-08 16:25:52 +00001059 const llvm::StructLayout *SL = getDataLayout().getStructLayout(ST);
Daniel Dunbar2ba67442010-04-21 19:10:49 +00001060
1061 const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
1062 RecordDecl::field_iterator it = D->field_begin();
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001063 const FieldDecl *LastFD = 0;
Eli Friedman9ee2d0472012-10-12 23:29:20 +00001064 bool IsMsStruct = D->isMsStruct(getContext());
Daniel Dunbar2ba67442010-04-21 19:10:49 +00001065 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
David Blaikie40ed2972012-06-06 20:45:41 +00001066 const FieldDecl *FD = *it;
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001067
1068 // For non-bit-fields, just check that the LLVM struct offset matches the
1069 // AST offset.
1070 if (!FD->isBitField()) {
Daniel Dunbar2ba67442010-04-21 19:10:49 +00001071 unsigned FieldNo = RL->getLLVMFieldNo(FD);
1072 assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
1073 "Invalid field offset!");
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001074 LastFD = FD;
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001075 continue;
1076 }
1077
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001078 if (IsMsStruct) {
1079 // Zero-length bitfields following non-bitfield members are
1080 // ignored:
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001081 if (getContext().ZeroBitfieldFollowsNonBitfield(FD, LastFD)) {
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001082 --i;
1083 continue;
1084 }
1085 LastFD = FD;
1086 }
1087
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001088 // Ignore unnamed bit-fields.
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001089 if (!FD->getDeclName()) {
1090 LastFD = FD;
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001091 continue;
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001092 }
1093
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001094 const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
1095 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
1096 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
1097
1098 // Verify that every component access is within the structure.
1099 uint64_t FieldOffset = SL->getElementOffsetInBits(AI.FieldIndex);
John McCall8a3c5552011-02-26 08:41:59 +00001100 uint64_t AccessBitOffset = FieldOffset +
Ken Dyckf76759c2011-04-24 10:04:59 +00001101 getContext().toBits(AI.FieldByteOffset);
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001102 assert(AccessBitOffset + AI.AccessWidth <= TypeSizeInBits &&
1103 "Invalid bit-field access (out of range)!");
Daniel Dunbar2ba67442010-04-21 19:10:49 +00001104 }
1105 }
1106#endif
Daniel Dunbar2ea51832010-04-19 20:44:47 +00001107
Daniel Dunbar034299e2010-03-31 01:09:11 +00001108 return RL;
Anders Carlsson307846f2009-07-23 03:17:50 +00001109}
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001110
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001111void CGRecordLayout::print(raw_ostream &OS) const {
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001112 OS << "<CGRecordLayout\n";
John McCall0217dfc22011-02-15 06:40:56 +00001113 OS << " LLVMType:" << *CompleteObjectType << "\n";
1114 if (BaseSubobjectType)
1115 OS << " NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n";
John McCall614dbdc2010-08-22 21:01:12 +00001116 OS << " IsZeroInitializable:" << IsZeroInitializable << "\n";
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001117 OS << " BitFields:[\n";
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001118
1119 // Print bit-field infos in declaration order.
1120 std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001121 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
1122 it = BitFields.begin(), ie = BitFields.end();
1123 it != ie; ++it) {
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001124 const RecordDecl *RD = it->first->getParent();
1125 unsigned Index = 0;
1126 for (RecordDecl::field_iterator
David Blaikie40ed2972012-06-06 20:45:41 +00001127 it2 = RD->field_begin(); *it2 != it->first; ++it2)
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001128 ++Index;
1129 BFIs.push_back(std::make_pair(Index, &it->second));
1130 }
1131 llvm::array_pod_sort(BFIs.begin(), BFIs.end());
1132 for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
Daniel Dunbarb935b932010-04-13 20:58:55 +00001133 OS.indent(4);
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001134 BFIs[i].second->print(OS);
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001135 OS << "\n";
1136 }
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001137
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001138 OS << "]>\n";
1139}
1140
1141void CGRecordLayout::dump() const {
1142 print(llvm::errs());
1143}
1144
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001145void CGBitFieldInfo::print(raw_ostream &OS) const {
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001146 OS << "<CGBitFieldInfo";
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001147 OS << " Size:" << Size;
Daniel Dunbarb935b932010-04-13 20:58:55 +00001148 OS << " IsSigned:" << IsSigned << "\n";
1149
1150 OS.indent(4 + strlen("<CGBitFieldInfo"));
1151 OS << " NumComponents:" << getNumComponents();
1152 OS << " Components: [";
1153 if (getNumComponents()) {
1154 OS << "\n";
1155 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
1156 const AccessInfo &AI = getComponent(i);
1157 OS.indent(8);
1158 OS << "<AccessInfo"
1159 << " FieldIndex:" << AI.FieldIndex
Ken Dyckf76759c2011-04-24 10:04:59 +00001160 << " FieldByteOffset:" << AI.FieldByteOffset.getQuantity()
Daniel Dunbarb935b932010-04-13 20:58:55 +00001161 << " FieldBitStart:" << AI.FieldBitStart
1162 << " AccessWidth:" << AI.AccessWidth << "\n";
1163 OS.indent(8 + strlen("<AccessInfo"));
Ken Dyck27337a82011-04-24 10:13:17 +00001164 OS << " AccessAlignment:" << AI.AccessAlignment.getQuantity()
Daniel Dunbarb935b932010-04-13 20:58:55 +00001165 << " TargetBitOffset:" << AI.TargetBitOffset
1166 << " TargetBitWidth:" << AI.TargetBitWidth
1167 << ">\n";
1168 }
1169 OS.indent(4);
1170 }
1171 OS << "]>";
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001172}
1173
1174void CGBitFieldInfo::dump() const {
1175 print(llvm::errs());
1176}