blob: ab92563b21f3b5a9541b7135d95a49df1e35f369 [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 Carruthffd55512013-01-02 11:45:17 +000024#include "llvm/IR/DataLayout.h"
25#include "llvm/IR/DerivedTypes.h"
26#include "llvm/IR/Type.h"
Daniel Dunbar2ba67442010-04-21 19:10:49 +000027#include "llvm/Support/Debug.h"
Daniel Dunbarb97bff92010-04-12 18:14:18 +000028#include "llvm/Support/raw_ostream.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;
81
82private:
83 CodeGenTypes &Types;
84
Anders Carlssonfcaaa692011-04-17 21:56:13 +000085 /// LastLaidOutBaseInfo - Contains the offset and non-virtual size of the
86 /// last base laid out. Used so that we can replace the last laid out base
87 /// type with an i8 array if needed.
88 struct LastLaidOutBaseInfo {
89 CharUnits Offset;
90 CharUnits NonVirtualSize;
91
92 bool isValid() const { return !NonVirtualSize.isZero(); }
93 void invalidate() { NonVirtualSize = CharUnits::Zero(); }
94
95 } LastLaidOutBase;
96
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000097 /// Alignment - Contains the alignment of the RecordDecl.
John McCall4d9f1422011-02-15 22:21:29 +000098 CharUnits Alignment;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000099
John McCall4d9f1422011-02-15 22:21:29 +0000100 /// NextFieldOffset - Holds the next field offset.
101 CharUnits NextFieldOffset;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000102
Anders Carlsson1de2f572010-04-17 20:49:27 +0000103 /// LayoutUnionField - Will layout a field in an union and return the type
104 /// that the field will have.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000105 llvm::Type *LayoutUnionField(const FieldDecl *Field,
106 const ASTRecordLayout &Layout);
Anders Carlsson1de2f572010-04-17 20:49:27 +0000107
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000108 /// LayoutUnion - Will layout a union RecordDecl.
109 void LayoutUnion(const RecordDecl *D);
110
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000111 /// Lay out a sequence of contiguous bitfields.
112 bool LayoutBitfields(const ASTRecordLayout &Layout,
113 unsigned &FirstFieldNo,
114 RecordDecl::field_iterator &FI,
115 RecordDecl::field_iterator FE);
116
Hans Wennborg96655c02013-11-15 22:31:11 +0000117 /// LayoutFields - try to layout all fields in the record decl.
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000118 /// Returns false if the operation failed because the struct is not packed.
119 bool LayoutFields(const RecordDecl *D);
120
Anders Carlssona518b2a2010-12-04 23:59:48 +0000121 /// Layout a single base, virtual or non-virtual
Eli Friedman3c840aa2011-12-12 23:13:20 +0000122 bool LayoutBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000123 const CGRecordLayout &baseLayout,
124 CharUnits baseOffset);
Anders Carlssona518b2a2010-12-04 23:59:48 +0000125
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000126 /// LayoutVirtualBase - layout a single virtual base.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000127 bool LayoutVirtualBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000128 CharUnits baseOffset);
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000129
Anders Carlssona459adb2010-11-28 19:18:44 +0000130 /// LayoutVirtualBases - layout the virtual bases of a record decl.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000131 bool LayoutVirtualBases(const CXXRecordDecl *RD,
Anders Carlssona459adb2010-11-28 19:18:44 +0000132 const ASTRecordLayout &Layout);
John McCall0153cd32011-11-08 04:01:03 +0000133
134 /// MSLayoutVirtualBases - layout the virtual bases of a record decl,
135 /// like MSVC.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000136 bool MSLayoutVirtualBases(const CXXRecordDecl *RD,
John McCall0153cd32011-11-08 04:01:03 +0000137 const ASTRecordLayout &Layout);
Anders Carlssona459adb2010-11-28 19:18:44 +0000138
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000139 /// LayoutNonVirtualBase - layout a single non-virtual base.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000140 bool LayoutNonVirtualBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000141 CharUnits baseOffset);
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000142
Anders Carlssona459adb2010-11-28 19:18:44 +0000143 /// LayoutNonVirtualBases - layout the virtual bases of a record decl.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000144 bool LayoutNonVirtualBases(const CXXRecordDecl *RD,
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000145 const ASTRecordLayout &Layout);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000146
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000147 /// ComputeNonVirtualBaseType - Compute the non-virtual base field types.
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000148 bool ComputeNonVirtualBaseType(const CXXRecordDecl *RD);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000149
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000150 /// LayoutField - layout a single field. Returns false if the operation failed
151 /// because the current struct is not packed.
152 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
153
154 /// LayoutBitField - layout a single bit field.
155 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
156
157 /// AppendField - Appends a field with the given offset and type.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000158 void AppendField(CharUnits fieldOffset, llvm::Type *FieldTy);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000159
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000160 /// AppendPadding - Appends enough padding bytes so that the total
161 /// struct size is a multiple of the field alignment.
John McCall4d9f1422011-02-15 22:21:29 +0000162 void AppendPadding(CharUnits fieldOffset, CharUnits fieldAlignment);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000163
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000164 /// ResizeLastBaseFieldIfNecessary - Fields and bases can be laid out in the
165 /// tail padding of a previous base. If this happens, the type of the previous
166 /// base needs to be changed to an array of i8. Returns true if the last
167 /// laid out base was resized.
168 bool ResizeLastBaseFieldIfNecessary(CharUnits offset);
169
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000170 /// getByteArrayType - Returns a byte array type with the given number of
171 /// elements.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000172 llvm::Type *getByteArrayType(CharUnits NumBytes);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000173
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000174 /// AppendBytes - Append a given number of bytes to the record.
John McCall4d9f1422011-02-15 22:21:29 +0000175 void AppendBytes(CharUnits numBytes);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000176
177 /// AppendTailPadding - Append enough tail padding so that the type will have
178 /// the passed size.
Ken Dyck272b6fa2011-04-24 16:53:44 +0000179 void AppendTailPadding(CharUnits RecordSize);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000180
Chris Lattner2192fe52011-07-18 04:24:23 +0000181 CharUnits getTypeAlignment(llvm::Type *Ty) const;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000182
Anders Carlssonacf877b2010-11-28 23:06:23 +0000183 /// getAlignmentAsLLVMStruct - Returns the maximum alignment of all the
184 /// LLVM element types.
John McCall4d9f1422011-02-15 22:21:29 +0000185 CharUnits getAlignmentAsLLVMStruct() const;
Anders Carlssonacf877b2010-11-28 23:06:23 +0000186
John McCall614dbdc2010-08-22 21:01:12 +0000187 /// CheckZeroInitializable - Check if the given type contains a pointer
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000188 /// to data member.
John McCall614dbdc2010-08-22 21:01:12 +0000189 void CheckZeroInitializable(QualType T);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000190
191public:
192 CGRecordLayoutBuilder(CodeGenTypes &Types)
John McCall0217dfc22011-02-15 06:40:56 +0000193 : BaseSubobjectType(0),
194 IsZeroInitializable(true), IsZeroInitializableAsBase(true),
Eli Friedman2782dac2013-06-26 20:50:34 +0000195 Packed(false), Types(Types) { }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000196
197 /// Layout - Will layout a RecordDecl.
198 void Layout(const RecordDecl *D);
199};
200
201}
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000202
Anders Carlsson307846f2009-07-23 03:17:50 +0000203void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
Warren Huntdbbc5df2013-10-29 23:49:26 +0000204 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Warren Hunt7c55e7e2013-10-30 00:14:55 +0000205 Alignment = Layout.getAlignment();
206 Packed = D->hasAttr<PackedAttr>() || Layout.getSize() % Alignment != 0;
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000207
Anders Carlsson697f6592009-07-23 03:43:54 +0000208 if (D->isUnion()) {
209 LayoutUnion(D);
210 return;
211 }
Anders Carlsson68e0b682009-08-08 18:23:56 +0000212
Anders Carlsson307846f2009-07-23 03:17:50 +0000213 if (LayoutFields(D))
214 return;
Mike Stump11289f42009-09-09 15:08:12 +0000215
Anders Carlsson307846f2009-07-23 03:17:50 +0000216 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlssond78fc892009-07-23 17:24:40 +0000217 Packed = true;
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000218 LastLaidOutBase.invalidate();
John McCall4d9f1422011-02-15 22:21:29 +0000219 NextFieldOffset = CharUnits::Zero();
Anders Carlsson307846f2009-07-23 03:17:50 +0000220 FieldTypes.clear();
John McCall0217dfc22011-02-15 06:40:56 +0000221 Fields.clear();
222 BitFields.clear();
223 NonVirtualBases.clear();
224 VirtualBases.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000225
Anders Carlsson307846f2009-07-23 03:17:50 +0000226 LayoutFields(D);
227}
228
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000229CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000230 const FieldDecl *FD,
231 uint64_t Offset, uint64_t Size,
232 uint64_t StorageSize,
233 uint64_t StorageAlignment) {
Chris Lattner2192fe52011-07-18 04:24:23 +0000234 llvm::Type *Ty = Types.ConvertTypeForMem(FD->getType());
John McCall8a3c5552011-02-26 08:41:59 +0000235 CharUnits TypeSizeInBytes =
Micah Villmowdd31ca12012-10-08 16:25:52 +0000236 CharUnits::fromQuantity(Types.getDataLayout().getTypeAllocSize(Ty));
John McCall8a3c5552011-02-26 08:41:59 +0000237 uint64_t TypeSizeInBits = Types.getContext().toBits(TypeSizeInBytes);
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000238
Douglas Gregor6ab2fa82011-05-20 16:38:50 +0000239 bool IsSigned = FD->getType()->isSignedIntegerOrEnumerationType();
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000240
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000241 if (Size > TypeSizeInBits) {
Anders Carlssond5f27b02010-04-17 22:54:57 +0000242 // We have a wide bit-field. The extra bits are only used for padding, so
243 // if we have a bitfield of type T, with size N:
244 //
245 // T t : N;
246 //
247 // We can just assume that it's:
248 //
249 // T t : sizeof(T);
250 //
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000251 Size = TypeSizeInBits;
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000252 }
253
Chandler Carruthfd8eca22012-12-09 07:26:04 +0000254 // Reverse the bit offsets for big endian machines. Because we represent
255 // a bitfield as a single large integer load, we can imagine the bits
256 // counting from the most-significant-bit instead of the
257 // least-significant-bit.
258 if (Types.getDataLayout().isBigEndian()) {
259 Offset = StorageSize - (Offset + Size);
260 }
Chris Lattnerfb59c7c2011-02-17 22:09:58 +0000261
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000262 return CGBitFieldInfo(Offset, Size, IsSigned, StorageSize, StorageAlignment);
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000263}
264
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000265/// \brief Layout the range of bitfields from BFI to BFE as contiguous storage.
266bool CGRecordLayoutBuilder::LayoutBitfields(const ASTRecordLayout &Layout,
267 unsigned &FirstFieldNo,
268 RecordDecl::field_iterator &FI,
269 RecordDecl::field_iterator FE) {
270 assert(FI != FE);
271 uint64_t FirstFieldOffset = Layout.getFieldOffset(FirstFieldNo);
272 uint64_t NextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset);
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000273
John McCallc8e01702013-04-16 22:48:15 +0000274 unsigned CharAlign = Types.getTarget().getCharAlign();
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000275 assert(FirstFieldOffset % CharAlign == 0 &&
276 "First field offset is misaligned");
277 CharUnits FirstFieldOffsetInBytes
278 = Types.getContext().toCharUnitsFromBits(FirstFieldOffset);
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000279
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000280 unsigned StorageAlignment
281 = llvm::MinAlign(Alignment.getQuantity(),
282 FirstFieldOffsetInBytes.getQuantity());
Mike Stump11289f42009-09-09 15:08:12 +0000283
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000284 if (FirstFieldOffset < NextFieldOffsetInBits) {
285 CharUnits FieldOffsetInCharUnits =
286 Types.getContext().toCharUnitsFromBits(FirstFieldOffset);
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000287
288 // Try to resize the last base field.
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000289 if (!ResizeLastBaseFieldIfNecessary(FieldOffsetInCharUnits))
290 llvm_unreachable("We must be able to resize the last base if we need to "
291 "pack bits into it.");
292
293 NextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset);
294 assert(FirstFieldOffset >= NextFieldOffsetInBits);
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000295 }
296
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000297 // Append padding if necessary.
298 AppendPadding(Types.getContext().toCharUnitsFromBits(FirstFieldOffset),
299 CharUnits::One());
Mike Stump11289f42009-09-09 15:08:12 +0000300
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000301 // Find the last bitfield in a contiguous run of bitfields.
302 RecordDecl::field_iterator BFI = FI;
303 unsigned LastFieldNo = FirstFieldNo;
304 uint64_t NextContiguousFieldOffset = FirstFieldOffset;
305 for (RecordDecl::field_iterator FJ = FI;
306 (FJ != FE && (*FJ)->isBitField() &&
307 NextContiguousFieldOffset == Layout.getFieldOffset(LastFieldNo) &&
308 (*FJ)->getBitWidthValue(Types.getContext()) != 0); FI = FJ++) {
309 NextContiguousFieldOffset += (*FJ)->getBitWidthValue(Types.getContext());
310 ++LastFieldNo;
Anders Carlsson307846f2009-07-23 03:17:50 +0000311
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000312 // We must use packed structs for packed fields, and also unnamed bit
313 // fields since they don't affect the struct alignment.
314 if (!Packed && ((*FJ)->hasAttr<PackedAttr>() || !(*FJ)->getDeclName()))
315 return false;
316 }
317 RecordDecl::field_iterator BFE = llvm::next(FI);
318 --LastFieldNo;
319 assert(LastFieldNo >= FirstFieldNo && "Empty run of contiguous bitfields");
320 FieldDecl *LastFD = *FI;
Mike Stump11289f42009-09-09 15:08:12 +0000321
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000322 // Find the last bitfield's offset, add its size, and round it up to the
323 // character alignment to compute the storage required.
324 uint64_t LastFieldOffset = Layout.getFieldOffset(LastFieldNo);
325 uint64_t LastFieldSize = LastFD->getBitWidthValue(Types.getContext());
326 uint64_t TotalBits = (LastFieldOffset + LastFieldSize) - FirstFieldOffset;
327 CharUnits StorageBytes = Types.getContext().toCharUnitsFromBits(
328 llvm::RoundUpToAlignment(TotalBits, CharAlign));
329 uint64_t StorageBits = Types.getContext().toBits(StorageBytes);
Mike Stump11289f42009-09-09 15:08:12 +0000330
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000331 // Grow the storage to encompass any known padding in the layout when doing
332 // so will make the storage a power-of-two. There are two cases when we can
333 // do this. The first is when we have a subsequent field and can widen up to
334 // its offset. The second is when the data size of the AST record layout is
335 // past the end of the current storage. The latter is true when there is tail
336 // padding on a struct and no members of a super class can be packed into it.
337 //
338 // Note that we widen the storage as much as possible here to express the
339 // maximum latitude the language provides, and rely on the backend to lower
340 // these in conjunction with shifts and masks to narrower operations where
341 // beneficial.
342 uint64_t EndOffset = Types.getContext().toBits(Layout.getDataSize());
343 if (BFE != FE)
344 // If there are more fields to be laid out, the offset at the end of the
345 // bitfield is the offset of the next field in the record.
346 EndOffset = Layout.getFieldOffset(LastFieldNo + 1);
347 assert(EndOffset >= (FirstFieldOffset + TotalBits) &&
348 "End offset is not past the end of the known storage bits.");
349 uint64_t SpaceBits = EndOffset - FirstFieldOffset;
John McCallc8e01702013-04-16 22:48:15 +0000350 uint64_t LongBits = Types.getTarget().getLongWidth();
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000351 uint64_t WidenedBits = (StorageBits / LongBits) * LongBits +
352 llvm::NextPowerOf2(StorageBits % LongBits - 1);
353 assert(WidenedBits >= StorageBits && "Widening shrunk the bits!");
354 if (WidenedBits <= SpaceBits) {
355 StorageBits = WidenedBits;
356 StorageBytes = Types.getContext().toCharUnitsFromBits(StorageBits);
357 assert(StorageBits == (uint64_t)Types.getContext().toBits(StorageBytes));
Anders Carlsson307846f2009-07-23 03:17:50 +0000358 }
359
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000360 unsigned FieldIndex = FieldTypes.size();
361 AppendBytes(StorageBytes);
Mike Stump11289f42009-09-09 15:08:12 +0000362
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000363 // Now walk the bitfields associating them with this field of storage and
364 // building up the bitfield specific info.
365 unsigned FieldNo = FirstFieldNo;
366 for (; BFI != BFE; ++BFI, ++FieldNo) {
367 FieldDecl *FD = *BFI;
368 uint64_t FieldOffset = Layout.getFieldOffset(FieldNo) - FirstFieldOffset;
369 uint64_t FieldSize = FD->getBitWidthValue(Types.getContext());
370 Fields[FD] = FieldIndex;
371 BitFields[FD] = CGBitFieldInfo::MakeInfo(Types, FD, FieldOffset, FieldSize,
372 StorageBits, StorageAlignment);
373 }
374 FirstFieldNo = LastFieldNo;
375 return true;
Anders Carlsson307846f2009-07-23 03:17:50 +0000376}
377
378bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
John McCall4d9f1422011-02-15 22:21:29 +0000379 uint64_t fieldOffset) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000380 // If the field is packed, then we need a packed struct.
Anders Carlsson68e0b682009-08-08 18:23:56 +0000381 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson307846f2009-07-23 03:17:50 +0000382 return false;
383
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000384 assert(!D->isBitField() && "Bitfields should be laid out seperately.");
Mike Stump11289f42009-09-09 15:08:12 +0000385
John McCall614dbdc2010-08-22 21:01:12 +0000386 CheckZeroInitializable(D->getType());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000387
John McCall8a3c5552011-02-26 08:41:59 +0000388 assert(fieldOffset % Types.getTarget().getCharWidth() == 0
389 && "field offset is not on a byte boundary!");
390 CharUnits fieldOffsetInBytes
391 = Types.getContext().toCharUnitsFromBits(fieldOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000392
Chris Lattnera5f58b02011-07-09 17:41:47 +0000393 llvm::Type *Ty = Types.ConvertTypeForMem(D->getType());
John McCall4d9f1422011-02-15 22:21:29 +0000394 CharUnits typeAlignment = getTypeAlignment(Ty);
Anders Carlsson19702bb2009-08-04 16:29:15 +0000395
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000396 // If the type alignment is larger then the struct alignment, we must use
397 // a packed struct.
John McCall4d9f1422011-02-15 22:21:29 +0000398 if (typeAlignment > Alignment) {
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000399 assert(!Packed && "Alignment is wrong even with packed struct!");
400 return false;
401 }
Mike Stump11289f42009-09-09 15:08:12 +0000402
John McCall4d9f1422011-02-15 22:21:29 +0000403 if (!Packed) {
404 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
405 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
406 if (const MaxFieldAlignmentAttr *MFAA =
407 RD->getAttr<MaxFieldAlignmentAttr>()) {
John McCall8a3c5552011-02-26 08:41:59 +0000408 if (MFAA->getAlignment() != Types.getContext().toBits(typeAlignment))
John McCall4d9f1422011-02-15 22:21:29 +0000409 return false;
410 }
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000411 }
412 }
413
Anders Carlsson19702bb2009-08-04 16:29:15 +0000414 // Round up the field offset to the alignment of the field type.
John McCall4d9f1422011-02-15 22:21:29 +0000415 CharUnits alignedNextFieldOffsetInBytes =
416 NextFieldOffset.RoundUpToAlignment(typeAlignment);
Anders Carlsson19702bb2009-08-04 16:29:15 +0000417
John McCall4d9f1422011-02-15 22:21:29 +0000418 if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000419 // Try to resize the last base field.
420 if (ResizeLastBaseFieldIfNecessary(fieldOffsetInBytes)) {
421 alignedNextFieldOffsetInBytes =
422 NextFieldOffset.RoundUpToAlignment(typeAlignment);
423 }
424 }
425
426 if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
Anders Carlsson19702bb2009-08-04 16:29:15 +0000427 assert(!Packed && "Could not place field even with packed struct!");
428 return false;
429 }
Mike Stump11289f42009-09-09 15:08:12 +0000430
John McCall4d9f1422011-02-15 22:21:29 +0000431 AppendPadding(fieldOffsetInBytes, typeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +0000432
Anders Carlsson307846f2009-07-23 03:17:50 +0000433 // Now append the field.
John McCall0217dfc22011-02-15 06:40:56 +0000434 Fields[D] = FieldTypes.size();
John McCall4d9f1422011-02-15 22:21:29 +0000435 AppendField(fieldOffsetInBytes, Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000436
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000437 LastLaidOutBase.invalidate();
Anders Carlsson307846f2009-07-23 03:17:50 +0000438 return true;
439}
440
Chris Lattnera5f58b02011-07-09 17:41:47 +0000441llvm::Type *
Anders Carlsson1de2f572010-04-17 20:49:27 +0000442CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
443 const ASTRecordLayout &Layout) {
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000444 Fields[Field] = 0;
Anders Carlsson1de2f572010-04-17 20:49:27 +0000445 if (Field->isBitField()) {
Richard Smithcaf33902011-10-10 18:28:20 +0000446 uint64_t FieldSize = Field->getBitWidthValue(Types.getContext());
Anders Carlsson1de2f572010-04-17 20:49:27 +0000447
448 // Ignore zero sized bit fields.
449 if (FieldSize == 0)
450 return 0;
451
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000452 unsigned StorageBits = llvm::RoundUpToAlignment(
John McCallc8e01702013-04-16 22:48:15 +0000453 FieldSize, Types.getTarget().getCharAlign());
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000454 CharUnits NumBytesToAppend
455 = Types.getContext().toCharUnitsFromBits(StorageBits);
Anders Carlsson2295f132010-04-17 21:04:52 +0000456
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000457 llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
Ken Dyck7a0b19f2011-04-24 16:47:33 +0000458 if (NumBytesToAppend > CharUnits::One())
459 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend.getQuantity());
Anders Carlsson2295f132010-04-17 21:04:52 +0000460
Anders Carlsson1de2f572010-04-17 20:49:27 +0000461 // Add the bit field info.
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000462 BitFields[Field] = CGBitFieldInfo::MakeInfo(Types, Field, 0, FieldSize,
463 StorageBits,
464 Alignment.getQuantity());
Anders Carlsson2295f132010-04-17 21:04:52 +0000465 return FieldTy;
Anders Carlsson1de2f572010-04-17 20:49:27 +0000466 }
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000467
Anders Carlsson1de2f572010-04-17 20:49:27 +0000468 // This is a regular union field.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000469 return Types.ConvertTypeForMem(Field->getType());
Anders Carlsson1de2f572010-04-17 20:49:27 +0000470}
471
Anders Carlsson697f6592009-07-23 03:43:54 +0000472void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
473 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump11289f42009-09-09 15:08:12 +0000474
John McCall4d9f1422011-02-15 22:21:29 +0000475 const ASTRecordLayout &layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000476
Chris Lattnera5f58b02011-07-09 17:41:47 +0000477 llvm::Type *unionType = 0;
John McCall4d9f1422011-02-15 22:21:29 +0000478 CharUnits unionSize = CharUnits::Zero();
479 CharUnits unionAlign = CharUnits::Zero();
Mike Stump11289f42009-09-09 15:08:12 +0000480
John McCall4d9f1422011-02-15 22:21:29 +0000481 bool hasOnlyZeroSizedBitFields = true;
Eli Friedmandae858a2011-12-07 01:30:11 +0000482 bool checkedFirstFieldZeroInit = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000483
John McCall4d9f1422011-02-15 22:21:29 +0000484 unsigned fieldNo = 0;
485 for (RecordDecl::field_iterator field = D->field_begin(),
486 fieldEnd = D->field_end(); field != fieldEnd; ++field, ++fieldNo) {
487 assert(layout.getFieldOffset(fieldNo) == 0 &&
Anders Carlsson697f6592009-07-23 03:43:54 +0000488 "Union field offset did not start at the beginning of record!");
David Blaikie40ed2972012-06-06 20:45:41 +0000489 llvm::Type *fieldType = LayoutUnionField(*field, layout);
Anders Carlssonf814ee62009-07-23 04:00:39 +0000490
John McCall4d9f1422011-02-15 22:21:29 +0000491 if (!fieldType)
Anders Carlsson1de2f572010-04-17 20:49:27 +0000492 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000493
Eli Friedmandae858a2011-12-07 01:30:11 +0000494 if (field->getDeclName() && !checkedFirstFieldZeroInit) {
495 CheckZeroInitializable(field->getType());
496 checkedFirstFieldZeroInit = true;
497 }
498
John McCall4d9f1422011-02-15 22:21:29 +0000499 hasOnlyZeroSizedBitFields = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000500
John McCall4d9f1422011-02-15 22:21:29 +0000501 CharUnits fieldAlign = CharUnits::fromQuantity(
Micah Villmowdd31ca12012-10-08 16:25:52 +0000502 Types.getDataLayout().getABITypeAlignment(fieldType));
John McCall4d9f1422011-02-15 22:21:29 +0000503 CharUnits fieldSize = CharUnits::fromQuantity(
Micah Villmowdd31ca12012-10-08 16:25:52 +0000504 Types.getDataLayout().getTypeAllocSize(fieldType));
Mike Stump11289f42009-09-09 15:08:12 +0000505
John McCall4d9f1422011-02-15 22:21:29 +0000506 if (fieldAlign < unionAlign)
Anders Carlsson697f6592009-07-23 03:43:54 +0000507 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000508
John McCall4d9f1422011-02-15 22:21:29 +0000509 if (fieldAlign > unionAlign || fieldSize > unionSize) {
510 unionType = fieldType;
511 unionAlign = fieldAlign;
512 unionSize = fieldSize;
Anders Carlsson697f6592009-07-23 03:43:54 +0000513 }
514 }
Mike Stump11289f42009-09-09 15:08:12 +0000515
Anders Carlsson697f6592009-07-23 03:43:54 +0000516 // Now add our field.
John McCall4d9f1422011-02-15 22:21:29 +0000517 if (unionType) {
518 AppendField(CharUnits::Zero(), unionType);
Anders Carlsson0e912752009-09-03 22:56:02 +0000519
John McCall4d9f1422011-02-15 22:21:29 +0000520 if (getTypeAlignment(unionType) > layout.getAlignment()) {
Anders Carlsson0e912752009-09-03 22:56:02 +0000521 // We need a packed struct.
522 Packed = true;
John McCall4d9f1422011-02-15 22:21:29 +0000523 unionAlign = CharUnits::One();
Anders Carlsson0e912752009-09-03 22:56:02 +0000524 }
525 }
John McCall4d9f1422011-02-15 22:21:29 +0000526 if (unionAlign.isZero()) {
Chandler Carruth52b6ac22012-03-04 12:16:40 +0000527 (void)hasOnlyZeroSizedBitFields;
John McCall4d9f1422011-02-15 22:21:29 +0000528 assert(hasOnlyZeroSizedBitFields &&
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000529 "0-align record did not have all zero-sized bit-fields!");
John McCall4d9f1422011-02-15 22:21:29 +0000530 unionAlign = CharUnits::One();
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000531 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000532
Anders Carlsson697f6592009-07-23 03:43:54 +0000533 // Append tail padding.
John McCall4d9f1422011-02-15 22:21:29 +0000534 CharUnits recordSize = layout.getSize();
535 if (recordSize > unionSize)
536 AppendPadding(recordSize, unionAlign);
Anders Carlsson697f6592009-07-23 03:43:54 +0000537}
538
Eli Friedman3c840aa2011-12-12 23:13:20 +0000539bool CGRecordLayoutBuilder::LayoutBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000540 const CGRecordLayout &baseLayout,
541 CharUnits baseOffset) {
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000542 ResizeLastBaseFieldIfNecessary(baseOffset);
543
John McCall4d9f1422011-02-15 22:21:29 +0000544 AppendPadding(baseOffset, CharUnits::One());
Anders Carlssona518b2a2010-12-04 23:59:48 +0000545
John McCall0217dfc22011-02-15 06:40:56 +0000546 const ASTRecordLayout &baseASTLayout
547 = Types.getContext().getASTRecordLayout(base);
548
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000549 LastLaidOutBase.Offset = NextFieldOffset;
550 LastLaidOutBase.NonVirtualSize = baseASTLayout.getNonVirtualSize();
551
Chris Lattnera5f58b02011-07-09 17:41:47 +0000552 llvm::StructType *subobjectType = baseLayout.getBaseSubobjectLLVMType();
Eli Friedman3c840aa2011-12-12 23:13:20 +0000553 if (getTypeAlignment(subobjectType) > Alignment)
554 return false;
555
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000556 AppendField(baseOffset, subobjectType);
Eli Friedman3c840aa2011-12-12 23:13:20 +0000557 return true;
John McCall0217dfc22011-02-15 06:40:56 +0000558}
559
Eli Friedman3c840aa2011-12-12 23:13:20 +0000560bool CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000561 CharUnits baseOffset) {
John McCall0217dfc22011-02-15 06:40:56 +0000562 // Ignore empty bases.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000563 if (base->isEmpty()) return true;
John McCall0217dfc22011-02-15 06:40:56 +0000564
565 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
566 if (IsZeroInitializableAsBase) {
567 assert(IsZeroInitializable &&
568 "class zero-initializable as base but not as complete object");
569
570 IsZeroInitializable = IsZeroInitializableAsBase =
571 baseLayout.isZeroInitializableAsBase();
572 }
573
Eli Friedman3c840aa2011-12-12 23:13:20 +0000574 if (!LayoutBase(base, baseLayout, baseOffset))
575 return false;
John McCall0217dfc22011-02-15 06:40:56 +0000576 NonVirtualBases[base] = (FieldTypes.size() - 1);
Eli Friedman3c840aa2011-12-12 23:13:20 +0000577 return true;
Anders Carlssona518b2a2010-12-04 23:59:48 +0000578}
579
Eli Friedman3c840aa2011-12-12 23:13:20 +0000580bool
John McCall0217dfc22011-02-15 06:40:56 +0000581CGRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000582 CharUnits baseOffset) {
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000583 // Ignore empty bases.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000584 if (base->isEmpty()) return true;
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000585
John McCall0217dfc22011-02-15 06:40:56 +0000586 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
587 if (IsZeroInitializable)
588 IsZeroInitializable = baseLayout.isZeroInitializableAsBase();
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000589
Eli Friedman3c840aa2011-12-12 23:13:20 +0000590 if (!LayoutBase(base, baseLayout, baseOffset))
591 return false;
John McCall0217dfc22011-02-15 06:40:56 +0000592 VirtualBases[base] = (FieldTypes.size() - 1);
Eli Friedman3c840aa2011-12-12 23:13:20 +0000593 return true;
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000594}
595
Eli Friedman3c840aa2011-12-12 23:13:20 +0000596bool
John McCall0153cd32011-11-08 04:01:03 +0000597CGRecordLayoutBuilder::MSLayoutVirtualBases(const CXXRecordDecl *RD,
598 const ASTRecordLayout &Layout) {
599 if (!RD->getNumVBases())
Eli Friedman3c840aa2011-12-12 23:13:20 +0000600 return true;
John McCall0153cd32011-11-08 04:01:03 +0000601
602 // The vbases list is uniqued and ordered by a depth-first
603 // traversal, which is what we need here.
604 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
605 E = RD->vbases_end(); I != E; ++I) {
606
607 const CXXRecordDecl *BaseDecl =
608 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
609
610 CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl);
Eli Friedman3c840aa2011-12-12 23:13:20 +0000611 if (!LayoutVirtualBase(BaseDecl, vbaseOffset))
612 return false;
John McCall0153cd32011-11-08 04:01:03 +0000613 }
Eli Friedman3c840aa2011-12-12 23:13:20 +0000614 return true;
John McCall0153cd32011-11-08 04:01:03 +0000615}
616
Anders Carlssona459adb2010-11-28 19:18:44 +0000617/// LayoutVirtualBases - layout the non-virtual bases of a record decl.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000618bool
Anders Carlssona459adb2010-11-28 19:18:44 +0000619CGRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
620 const ASTRecordLayout &Layout) {
621 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
622 E = RD->bases_end(); I != E; ++I) {
623 const CXXRecordDecl *BaseDecl =
624 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
625
626 // We only want to lay out virtual bases that aren't indirect primary bases
627 // of some other base.
628 if (I->isVirtual() && !IndirectPrimaryBases.count(BaseDecl)) {
629 // Only lay out the base once.
630 if (!LaidOutVirtualBases.insert(BaseDecl))
631 continue;
632
John McCall4d9f1422011-02-15 22:21:29 +0000633 CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl);
Eli Friedman3c840aa2011-12-12 23:13:20 +0000634 if (!LayoutVirtualBase(BaseDecl, vbaseOffset))
635 return false;
Anders Carlssona459adb2010-11-28 19:18:44 +0000636 }
637
638 if (!BaseDecl->getNumVBases()) {
639 // This base isn't interesting since it doesn't have any virtual bases.
640 continue;
641 }
642
Eli Friedman3c840aa2011-12-12 23:13:20 +0000643 if (!LayoutVirtualBases(BaseDecl, Layout))
644 return false;
Anders Carlssona459adb2010-11-28 19:18:44 +0000645 }
Eli Friedman3c840aa2011-12-12 23:13:20 +0000646 return true;
Anders Carlssona459adb2010-11-28 19:18:44 +0000647}
648
Eli Friedman3c840aa2011-12-12 23:13:20 +0000649bool
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000650CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD,
651 const ASTRecordLayout &Layout) {
652 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
653
John McCall0153cd32011-11-08 04:01:03 +0000654 // If we have a primary base, lay it out first.
655 if (PrimaryBase) {
Eli Friedman3c840aa2011-12-12 23:13:20 +0000656 if (!Layout.isPrimaryBaseVirtual()) {
657 if (!LayoutNonVirtualBase(PrimaryBase, CharUnits::Zero()))
658 return false;
659 } else {
660 if (!LayoutVirtualBase(PrimaryBase, CharUnits::Zero()))
661 return false;
662 }
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000663
John McCall0153cd32011-11-08 04:01:03 +0000664 // Otherwise, add a vtable / vf-table if the layout says to do so.
John McCalle42a3362012-05-01 08:55:32 +0000665 } else if (Layout.hasOwnVFPtr()) {
John McCall0153cd32011-11-08 04:01:03 +0000666 llvm::Type *FunctionType =
667 llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()),
668 /*isVarArg=*/true);
669 llvm::Type *VTableTy = FunctionType->getPointerTo();
Eli Friedmanf927b8b2012-04-27 02:34:46 +0000670
671 if (getTypeAlignment(VTableTy) > Alignment) {
672 // FIXME: Should we allow this to happen in Sema?
673 assert(!Packed && "Alignment is wrong even with packed struct!");
674 return false;
675 }
676
John McCall0153cd32011-11-08 04:01:03 +0000677 assert(NextFieldOffset.isZero() &&
678 "VTable pointer must come first!");
679 AppendField(CharUnits::Zero(), VTableTy->getPointerTo());
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000680 }
681
682 // Layout the non-virtual bases.
683 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
684 E = RD->bases_end(); I != E; ++I) {
685 if (I->isVirtual())
686 continue;
687
688 const CXXRecordDecl *BaseDecl =
689 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
690
691 // We've already laid out the primary base.
Anders Carlsson7f95cd12010-11-24 23:12:57 +0000692 if (BaseDecl == PrimaryBase && !Layout.isPrimaryBaseVirtual())
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000693 continue;
694
Eli Friedman3c840aa2011-12-12 23:13:20 +0000695 if (!LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffset(BaseDecl)))
696 return false;
Anders Carlssond681a292009-12-16 17:27:20 +0000697 }
John McCall0153cd32011-11-08 04:01:03 +0000698
699 // Add a vb-table pointer if the layout insists.
Warren Hunt8f8bad72013-10-11 20:19:00 +0000700 if (Layout.hasOwnVBPtr()) {
John McCall0153cd32011-11-08 04:01:03 +0000701 CharUnits VBPtrOffset = Layout.getVBPtrOffset();
702 llvm::Type *Vbptr = llvm::Type::getInt32PtrTy(Types.getLLVMContext());
703 AppendPadding(VBPtrOffset, getTypeAlignment(Vbptr));
704 AppendField(VBPtrOffset, Vbptr);
705 }
Eli Friedman3c840aa2011-12-12 23:13:20 +0000706
707 return true;
Anders Carlssond681a292009-12-16 17:27:20 +0000708}
709
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000710bool
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000711CGRecordLayoutBuilder::ComputeNonVirtualBaseType(const CXXRecordDecl *RD) {
712 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(RD);
713
Ken Dyckbec02852011-02-08 02:02:47 +0000714 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
715 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
John McCall4d9f1422011-02-15 22:21:29 +0000716 CharUnits AlignedNonVirtualTypeSize =
717 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000718
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000719 // First check if we can use the same fields as for the complete class.
John McCall4d9f1422011-02-15 22:21:29 +0000720 CharUnits RecordSize = Layout.getSize();
John McCall0217dfc22011-02-15 06:40:56 +0000721 if (AlignedNonVirtualTypeSize == RecordSize)
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000722 return true;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000723
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000724 // Check if we need padding.
John McCall4d9f1422011-02-15 22:21:29 +0000725 CharUnits AlignedNextFieldOffset =
726 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000727
John McCall0217dfc22011-02-15 06:40:56 +0000728 if (AlignedNextFieldOffset > AlignedNonVirtualTypeSize) {
729 assert(!Packed && "cannot layout even as packed struct");
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000730 return false; // Needs packing.
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000731 }
732
John McCall0217dfc22011-02-15 06:40:56 +0000733 bool needsPadding = (AlignedNonVirtualTypeSize != AlignedNextFieldOffset);
734 if (needsPadding) {
John McCall4d9f1422011-02-15 22:21:29 +0000735 CharUnits NumBytes = AlignedNonVirtualTypeSize - AlignedNextFieldOffset;
John McCall0217dfc22011-02-15 06:40:56 +0000736 FieldTypes.push_back(getByteArrayType(NumBytes));
737 }
Chris Lattnera5f58b02011-07-09 17:41:47 +0000738
Chris Lattner5ec04a52011-08-12 17:43:31 +0000739 BaseSubobjectType = llvm::StructType::create(Types.getLLVMContext(),
740 FieldTypes, "", Packed);
Chris Lattnera5f58b02011-07-09 17:41:47 +0000741 Types.addRecordTypeName(RD, BaseSubobjectType, ".base");
John McCall0217dfc22011-02-15 06:40:56 +0000742
Chris Lattnera5f58b02011-07-09 17:41:47 +0000743 // Pull the padding back off.
744 if (needsPadding)
John McCall0217dfc22011-02-15 06:40:56 +0000745 FieldTypes.pop_back();
John McCall0217dfc22011-02-15 06:40:56 +0000746
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000747 return true;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000748}
749
Anders Carlsson307846f2009-07-23 03:17:50 +0000750bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
751 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
John McCall4d9f1422011-02-15 22:21:29 +0000752 assert(!Alignment.isZero() && "Did not set alignment!");
Mike Stump11289f42009-09-09 15:08:12 +0000753
Anders Carlsson697f6592009-07-23 03:43:54 +0000754 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000755
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000756 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
757 if (RD)
Eli Friedman3c840aa2011-12-12 23:13:20 +0000758 if (!LayoutNonVirtualBases(RD, Layout))
759 return false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000760
Anders Carlsson307846f2009-07-23 03:17:50 +0000761 unsigned FieldNo = 0;
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000762
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000763 for (RecordDecl::field_iterator FI = D->field_begin(), FE = D->field_end();
764 FI != FE; ++FI, ++FieldNo) {
765 FieldDecl *FD = *FI;
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000766
767 // If this field is a bitfield, layout all of the consecutive
768 // non-zero-length bitfields and the last zero-length bitfield; these will
769 // all share storage.
770 if (FD->isBitField()) {
771 // If all we have is a zero-width bitfield, skip it.
772 if (FD->getBitWidthValue(Types.getContext()) == 0)
773 continue;
774
775 // Layout this range of bitfields.
776 if (!LayoutBitfields(Layout, FieldNo, FI, FE)) {
777 assert(!Packed &&
778 "Could not layout bitfields even with a packed LLVM struct!");
779 return false;
780 }
781 assert(FI != FE && "Advanced past the last bitfield");
782 continue;
783 }
784
785 if (!LayoutField(FD, Layout.getFieldOffset(FieldNo))) {
Mike Stump11289f42009-09-09 15:08:12 +0000786 assert(!Packed &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000787 "Could not layout fields even with a packed LLVM struct!");
788 return false;
789 }
790 }
791
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000792 if (RD) {
Anders Carlssona459adb2010-11-28 19:18:44 +0000793 // We've laid out the non-virtual bases and the fields, now compute the
794 // non-virtual base field types.
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000795 if (!ComputeNonVirtualBaseType(RD)) {
796 assert(!Packed && "Could not layout even with a packed LLVM struct!");
797 return false;
798 }
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000799
John McCall0153cd32011-11-08 04:01:03 +0000800 // Lay out the virtual bases. The MS ABI uses a different
801 // algorithm here due to the lack of primary virtual bases.
John McCallc8e01702013-04-16 22:48:15 +0000802 if (Types.getTarget().getCXXABI().hasPrimaryVBases()) {
John McCall0153cd32011-11-08 04:01:03 +0000803 RD->getIndirectPrimaryBases(IndirectPrimaryBases);
804 if (Layout.isPrimaryBaseVirtual())
805 IndirectPrimaryBases.insert(Layout.getPrimaryBase());
806
Eli Friedman3c840aa2011-12-12 23:13:20 +0000807 if (!LayoutVirtualBases(RD, Layout))
808 return false;
John McCall0153cd32011-11-08 04:01:03 +0000809 } else {
Eli Friedman3c840aa2011-12-12 23:13:20 +0000810 if (!MSLayoutVirtualBases(RD, Layout))
811 return false;
John McCall0153cd32011-11-08 04:01:03 +0000812 }
Anders Carlssona459adb2010-11-28 19:18:44 +0000813 }
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000814
Anders Carlsson307846f2009-07-23 03:17:50 +0000815 // Append tail padding if necessary.
Ken Dyck272b6fa2011-04-24 16:53:44 +0000816 AppendTailPadding(Layout.getSize());
Mike Stump11289f42009-09-09 15:08:12 +0000817
Anders Carlsson307846f2009-07-23 03:17:50 +0000818 return true;
819}
820
Ken Dyck272b6fa2011-04-24 16:53:44 +0000821void CGRecordLayoutBuilder::AppendTailPadding(CharUnits RecordSize) {
822 ResizeLastBaseFieldIfNecessary(RecordSize);
Mike Stump11289f42009-09-09 15:08:12 +0000823
Ken Dyck272b6fa2011-04-24 16:53:44 +0000824 assert(NextFieldOffset <= RecordSize && "Size mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000825
John McCall4d9f1422011-02-15 22:21:29 +0000826 CharUnits AlignedNextFieldOffset =
827 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
Anders Carlsson220bf4f2009-12-08 01:24:23 +0000828
Ken Dyck272b6fa2011-04-24 16:53:44 +0000829 if (AlignedNextFieldOffset == RecordSize) {
Anders Carlsson220bf4f2009-12-08 01:24:23 +0000830 // We don't need any padding.
831 return;
832 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000833
Ken Dyck272b6fa2011-04-24 16:53:44 +0000834 CharUnits NumPadBytes = RecordSize - NextFieldOffset;
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000835 AppendBytes(NumPadBytes);
836}
837
John McCall4d9f1422011-02-15 22:21:29 +0000838void CGRecordLayoutBuilder::AppendField(CharUnits fieldOffset,
Chris Lattnera5f58b02011-07-09 17:41:47 +0000839 llvm::Type *fieldType) {
John McCall4d9f1422011-02-15 22:21:29 +0000840 CharUnits fieldSize =
Micah Villmowdd31ca12012-10-08 16:25:52 +0000841 CharUnits::fromQuantity(Types.getDataLayout().getTypeAllocSize(fieldType));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000842
John McCall4d9f1422011-02-15 22:21:29 +0000843 FieldTypes.push_back(fieldType);
Anders Carlsson307846f2009-07-23 03:17:50 +0000844
John McCall4d9f1422011-02-15 22:21:29 +0000845 NextFieldOffset = fieldOffset + fieldSize;
Anders Carlsson307846f2009-07-23 03:17:50 +0000846}
847
John McCall4d9f1422011-02-15 22:21:29 +0000848void CGRecordLayoutBuilder::AppendPadding(CharUnits fieldOffset,
849 CharUnits fieldAlignment) {
850 assert(NextFieldOffset <= fieldOffset &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000851 "Incorrect field layout!");
Mike Stump11289f42009-09-09 15:08:12 +0000852
John McCall0153cd32011-11-08 04:01:03 +0000853 // Do nothing if we're already at the right offset.
854 if (fieldOffset == NextFieldOffset) return;
Anders Carlsson307846f2009-07-23 03:17:50 +0000855
John McCall0153cd32011-11-08 04:01:03 +0000856 // If we're not emitting a packed LLVM type, try to avoid adding
857 // unnecessary padding fields.
858 if (!Packed) {
859 // Round up the field offset to the alignment of the field type.
860 CharUnits alignedNextFieldOffset =
861 NextFieldOffset.RoundUpToAlignment(fieldAlignment);
862 assert(alignedNextFieldOffset <= fieldOffset);
Anders Carlsson307846f2009-07-23 03:17:50 +0000863
John McCall0153cd32011-11-08 04:01:03 +0000864 // If that's the right offset, we're done.
865 if (alignedNextFieldOffset == fieldOffset) return;
Anders Carlsson307846f2009-07-23 03:17:50 +0000866 }
John McCall0153cd32011-11-08 04:01:03 +0000867
868 // Otherwise we need explicit padding.
869 CharUnits padding = fieldOffset - NextFieldOffset;
870 AppendBytes(padding);
Anders Carlsson307846f2009-07-23 03:17:50 +0000871}
872
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000873bool CGRecordLayoutBuilder::ResizeLastBaseFieldIfNecessary(CharUnits offset) {
874 // Check if we have a base to resize.
875 if (!LastLaidOutBase.isValid())
876 return false;
877
878 // This offset does not overlap with the tail padding.
879 if (offset >= NextFieldOffset)
880 return false;
881
882 // Restore the field offset and append an i8 array instead.
883 FieldTypes.pop_back();
884 NextFieldOffset = LastLaidOutBase.Offset;
885 AppendBytes(LastLaidOutBase.NonVirtualSize);
886 LastLaidOutBase.invalidate();
887
888 return true;
889}
890
Chris Lattnera5f58b02011-07-09 17:41:47 +0000891llvm::Type *CGRecordLayoutBuilder::getByteArrayType(CharUnits numBytes) {
John McCall4d9f1422011-02-15 22:21:29 +0000892 assert(!numBytes.isZero() && "Empty byte arrays aren't allowed.");
Mike Stump11289f42009-09-09 15:08:12 +0000893
Chris Lattnera5f58b02011-07-09 17:41:47 +0000894 llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
John McCall4d9f1422011-02-15 22:21:29 +0000895 if (numBytes > CharUnits::One())
896 Ty = llvm::ArrayType::get(Ty, numBytes.getQuantity());
Mike Stump11289f42009-09-09 15:08:12 +0000897
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000898 return Ty;
899}
900
John McCall4d9f1422011-02-15 22:21:29 +0000901void CGRecordLayoutBuilder::AppendBytes(CharUnits numBytes) {
902 if (numBytes.isZero())
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000903 return;
904
Anders Carlsson307846f2009-07-23 03:17:50 +0000905 // Append the padding field
John McCall4d9f1422011-02-15 22:21:29 +0000906 AppendField(NextFieldOffset, getByteArrayType(numBytes));
Anders Carlsson307846f2009-07-23 03:17:50 +0000907}
908
Chris Lattner2192fe52011-07-18 04:24:23 +0000909CharUnits CGRecordLayoutBuilder::getTypeAlignment(llvm::Type *Ty) const {
Anders Carlssond78fc892009-07-23 17:24:40 +0000910 if (Packed)
John McCall4d9f1422011-02-15 22:21:29 +0000911 return CharUnits::One();
Mike Stump11289f42009-09-09 15:08:12 +0000912
Micah Villmowdd31ca12012-10-08 16:25:52 +0000913 return CharUnits::fromQuantity(Types.getDataLayout().getABITypeAlignment(Ty));
Anders Carlsson307846f2009-07-23 03:17:50 +0000914}
915
John McCall4d9f1422011-02-15 22:21:29 +0000916CharUnits CGRecordLayoutBuilder::getAlignmentAsLLVMStruct() const {
Anders Carlssonacf877b2010-11-28 23:06:23 +0000917 if (Packed)
John McCall4d9f1422011-02-15 22:21:29 +0000918 return CharUnits::One();
Anders Carlssonacf877b2010-11-28 23:06:23 +0000919
John McCall4d9f1422011-02-15 22:21:29 +0000920 CharUnits maxAlignment = CharUnits::One();
Anders Carlssonacf877b2010-11-28 23:06:23 +0000921 for (size_t i = 0; i != FieldTypes.size(); ++i)
John McCall4d9f1422011-02-15 22:21:29 +0000922 maxAlignment = std::max(maxAlignment, getTypeAlignment(FieldTypes[i]));
Anders Carlssonacf877b2010-11-28 23:06:23 +0000923
John McCall4d9f1422011-02-15 22:21:29 +0000924 return maxAlignment;
Anders Carlssonacf877b2010-11-28 23:06:23 +0000925}
926
John McCall0217dfc22011-02-15 06:40:56 +0000927/// Merge in whether a field of the given type is zero-initializable.
John McCall614dbdc2010-08-22 21:01:12 +0000928void CGRecordLayoutBuilder::CheckZeroInitializable(QualType T) {
Anders Carlssond606de72009-08-23 01:25:01 +0000929 // This record already contains a member pointer.
John McCall0217dfc22011-02-15 06:40:56 +0000930 if (!IsZeroInitializableAsBase)
Anders Carlssond606de72009-08-23 01:25:01 +0000931 return;
Mike Stump11289f42009-09-09 15:08:12 +0000932
Anders Carlssond606de72009-08-23 01:25:01 +0000933 // Can only have member pointers if we're compiling C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000934 if (!Types.getContext().getLangOpts().CPlusPlus)
Anders Carlssond606de72009-08-23 01:25:01 +0000935 return;
Mike Stump11289f42009-09-09 15:08:12 +0000936
John McCall0217dfc22011-02-15 06:40:56 +0000937 const Type *elementType = T->getBaseElementTypeUnsafe();
Mike Stump11289f42009-09-09 15:08:12 +0000938
John McCall0217dfc22011-02-15 06:40:56 +0000939 if (const MemberPointerType *MPT = elementType->getAs<MemberPointerType>()) {
John McCall614dbdc2010-08-22 21:01:12 +0000940 if (!Types.getCXXABI().isZeroInitializable(MPT))
John McCall0217dfc22011-02-15 06:40:56 +0000941 IsZeroInitializable = IsZeroInitializableAsBase = false;
942 } else if (const RecordType *RT = elementType->getAs<RecordType>()) {
Anders Carlssone8bfe412010-02-02 05:17:25 +0000943 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
John McCall0217dfc22011-02-15 06:40:56 +0000944 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
945 if (!Layout.isZeroInitializable())
946 IsZeroInitializable = IsZeroInitializableAsBase = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000947 }
Anders Carlssond606de72009-08-23 01:25:01 +0000948}
949
Chris Lattnera5f58b02011-07-09 17:41:47 +0000950CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D,
951 llvm::StructType *Ty) {
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000952 CGRecordLayoutBuilder Builder(*this);
Mike Stump11289f42009-09-09 15:08:12 +0000953
Anders Carlsson307846f2009-07-23 03:17:50 +0000954 Builder.Layout(D);
Anders Carlssone1d5ca52009-07-24 15:20:52 +0000955
Chris Lattnera5f58b02011-07-09 17:41:47 +0000956 Ty->setBody(Builder.FieldTypes, Builder.Packed);
Mike Stump11289f42009-09-09 15:08:12 +0000957
John McCall0217dfc22011-02-15 06:40:56 +0000958 // If we're in C++, compute the base subobject type.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000959 llvm::StructType *BaseTy = 0;
Eli Friedman09d272d2012-01-13 03:58:31 +0000960 if (isa<CXXRecordDecl>(D) && !D->isUnion()) {
John McCall0217dfc22011-02-15 06:40:56 +0000961 BaseTy = Builder.BaseSubobjectType;
962 if (!BaseTy) BaseTy = Ty;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000963 }
964
Daniel Dunbar034299e2010-03-31 01:09:11 +0000965 CGRecordLayout *RL =
John McCall0217dfc22011-02-15 06:40:56 +0000966 new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable,
967 Builder.IsZeroInitializableAsBase);
Daniel Dunbar034299e2010-03-31 01:09:11 +0000968
John McCall0217dfc22011-02-15 06:40:56 +0000969 RL->NonVirtualBases.swap(Builder.NonVirtualBases);
970 RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases);
Anders Carlsson061ca522010-05-18 05:22:06 +0000971
Anders Carlsson307846f2009-07-23 03:17:50 +0000972 // Add all the field numbers.
John McCall0217dfc22011-02-15 06:40:56 +0000973 RL->FieldInfo.swap(Builder.Fields);
Anders Carlsson307846f2009-07-23 03:17:50 +0000974
975 // Add bitfield info.
John McCall0217dfc22011-02-15 06:40:56 +0000976 RL->BitFields.swap(Builder.BitFields);
Mike Stump11289f42009-09-09 15:08:12 +0000977
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000978 // Dump the layout, if requested.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000979 if (getContext().getLangOpts().DumpRecordLayouts) {
Argyrios Kyrtzidis8ade08e2013-07-12 22:30:03 +0000980 llvm::outs() << "\n*** Dumping IRgen Record Layout\n";
981 llvm::outs() << "Record: ";
982 D->dump(llvm::outs());
983 llvm::outs() << "\nLayout: ";
984 RL->print(llvm::outs());
Daniel Dunbarb935b932010-04-13 20:58:55 +0000985 }
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000986
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000987#ifndef NDEBUG
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000988 // Verify that the computed LLVM struct size matches the AST layout size.
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000989 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
990
Ken Dyckb0fcc592011-02-11 01:54:29 +0000991 uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize());
Micah Villmowdd31ca12012-10-08 16:25:52 +0000992 assert(TypeSizeInBits == getDataLayout().getTypeAllocSizeInBits(Ty) &&
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000993 "Type size mismatch!");
994
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000995 if (BaseTy) {
Ken Dyckbec02852011-02-08 02:02:47 +0000996 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
997 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
998 CharUnits AlignedNonVirtualTypeSize =
999 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
1000
1001 uint64_t AlignedNonVirtualTypeSizeInBits =
Ken Dyckb0fcc592011-02-11 01:54:29 +00001002 getContext().toBits(AlignedNonVirtualTypeSize);
Anders Carlssonc1351ca2010-11-09 05:25:47 +00001003
1004 assert(AlignedNonVirtualTypeSizeInBits ==
Micah Villmowdd31ca12012-10-08 16:25:52 +00001005 getDataLayout().getTypeAllocSizeInBits(BaseTy) &&
Anders Carlssonc1351ca2010-11-09 05:25:47 +00001006 "Type size mismatch!");
1007 }
1008
Daniel Dunbar2ba67442010-04-21 19:10:49 +00001009 // Verify that the LLVM and AST field offsets agree.
Chris Lattner2192fe52011-07-18 04:24:23 +00001010 llvm::StructType *ST =
Daniel Dunbar2ba67442010-04-21 19:10:49 +00001011 dyn_cast<llvm::StructType>(RL->getLLVMType());
Micah Villmowdd31ca12012-10-08 16:25:52 +00001012 const llvm::StructLayout *SL = getDataLayout().getStructLayout(ST);
Daniel Dunbar2ba67442010-04-21 19:10:49 +00001013
1014 const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
1015 RecordDecl::field_iterator it = D->field_begin();
1016 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
David Blaikie40ed2972012-06-06 20:45:41 +00001017 const FieldDecl *FD = *it;
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001018
1019 // For non-bit-fields, just check that the LLVM struct offset matches the
1020 // AST offset.
1021 if (!FD->isBitField()) {
Daniel Dunbar2ba67442010-04-21 19:10:49 +00001022 unsigned FieldNo = RL->getLLVMFieldNo(FD);
1023 assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
1024 "Invalid field offset!");
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001025 continue;
1026 }
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001027
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001028 // Ignore unnamed bit-fields.
Eli Friedman2782dac2013-06-26 20:50:34 +00001029 if (!FD->getDeclName())
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001030 continue;
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001031
Chandler Carruthff0e3a12012-12-06 11:14:44 +00001032 // Don't inspect zero-length bitfields.
1033 if (FD->getBitWidthValue(getContext()) == 0)
1034 continue;
1035
Chandler Carruthff0e3a12012-12-06 11:14:44 +00001036 const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
Chandler Carruthed72cdc2012-12-09 10:33:27 +00001037 llvm::Type *ElementTy = ST->getTypeAtIndex(RL->getLLVMFieldNo(FD));
1038
Chandler Carruthff0e3a12012-12-06 11:14:44 +00001039 // Unions have overlapping elements dictating their layout, but for
1040 // non-unions we can verify that this section of the layout is the exact
Chandler Carruthed72cdc2012-12-09 10:33:27 +00001041 // expected size.
Chandler Carruthff0e3a12012-12-06 11:14:44 +00001042 if (D->isUnion()) {
Chandler Carruthed72cdc2012-12-09 10:33:27 +00001043 // For unions we verify that the start is zero and the size
1044 // is in-bounds. However, on BE systems, the offset may be non-zero, but
1045 // the size + offset should match the storage size in that case as it
1046 // "starts" at the back.
1047 if (getDataLayout().isBigEndian())
David Greene464d2192013-01-15 23:13:49 +00001048 assert(static_cast<unsigned>(Info.Offset + Info.Size) ==
1049 Info.StorageSize &&
Chandler Carruthed72cdc2012-12-09 10:33:27 +00001050 "Big endian union bitfield does not end at the back");
1051 else
1052 assert(Info.Offset == 0 &&
1053 "Little endian union bitfield with a non-zero offset");
Chandler Carruthff0e3a12012-12-06 11:14:44 +00001054 assert(Info.StorageSize <= SL->getSizeInBits() &&
1055 "Union not large enough for bitfield storage");
1056 } else {
1057 assert(Info.StorageSize ==
1058 getDataLayout().getTypeAllocSizeInBits(ElementTy) &&
1059 "Storage size does not match the element type size");
Daniel Dunbar2ba67442010-04-21 19:10:49 +00001060 }
Chandler Carruthff0e3a12012-12-06 11:14:44 +00001061 assert(Info.Size > 0 && "Empty bitfield!");
Eli Bendersky76bd3d82012-12-18 18:53:14 +00001062 assert(static_cast<unsigned>(Info.Offset) + Info.Size <= Info.StorageSize &&
Chandler Carruthff0e3a12012-12-06 11:14:44 +00001063 "Bitfield outside of its allocated storage");
Daniel Dunbar2ba67442010-04-21 19:10:49 +00001064 }
1065#endif
Daniel Dunbar2ea51832010-04-19 20:44:47 +00001066
Daniel Dunbar034299e2010-03-31 01:09:11 +00001067 return RL;
Anders Carlsson307846f2009-07-23 03:17:50 +00001068}
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001069
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001070void CGRecordLayout::print(raw_ostream &OS) const {
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001071 OS << "<CGRecordLayout\n";
John McCall0217dfc22011-02-15 06:40:56 +00001072 OS << " LLVMType:" << *CompleteObjectType << "\n";
1073 if (BaseSubobjectType)
1074 OS << " NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n";
John McCall614dbdc2010-08-22 21:01:12 +00001075 OS << " IsZeroInitializable:" << IsZeroInitializable << "\n";
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001076 OS << " BitFields:[\n";
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001077
1078 // Print bit-field infos in declaration order.
1079 std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001080 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
1081 it = BitFields.begin(), ie = BitFields.end();
1082 it != ie; ++it) {
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001083 const RecordDecl *RD = it->first->getParent();
1084 unsigned Index = 0;
1085 for (RecordDecl::field_iterator
David Blaikie40ed2972012-06-06 20:45:41 +00001086 it2 = RD->field_begin(); *it2 != it->first; ++it2)
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001087 ++Index;
1088 BFIs.push_back(std::make_pair(Index, &it->second));
1089 }
1090 llvm::array_pod_sort(BFIs.begin(), BFIs.end());
1091 for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
Daniel Dunbarb935b932010-04-13 20:58:55 +00001092 OS.indent(4);
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001093 BFIs[i].second->print(OS);
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001094 OS << "\n";
1095 }
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001096
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001097 OS << "]>\n";
1098}
1099
1100void CGRecordLayout::dump() const {
1101 print(llvm::errs());
1102}
1103
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001104void CGBitFieldInfo::print(raw_ostream &OS) const {
Chandler Carruthff0e3a12012-12-06 11:14:44 +00001105 OS << "<CGBitFieldInfo"
1106 << " Offset:" << Offset
1107 << " Size:" << Size
1108 << " IsSigned:" << IsSigned
1109 << " StorageSize:" << StorageSize
1110 << " StorageAlignment:" << StorageAlignment << ">";
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001111}
1112
1113void CGBitFieldInfo::dump() const {
1114 print(llvm::errs());
1115}