blob: 43088b4a2019fb6b0d49af6235e29b7fd651c7d9 [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
John McCall4d9f1422011-02-15 22:21:29 +0000103 /// NextFieldOffset - Holds the next field offset.
104 CharUnits NextFieldOffset;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000105
Anders Carlsson1de2f572010-04-17 20:49:27 +0000106 /// LayoutUnionField - Will layout a field in an union and return the type
107 /// that the field will have.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000108 llvm::Type *LayoutUnionField(const FieldDecl *Field,
109 const ASTRecordLayout &Layout);
Anders Carlsson1de2f572010-04-17 20:49:27 +0000110
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000111 /// LayoutUnion - Will layout a union RecordDecl.
112 void LayoutUnion(const RecordDecl *D);
113
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000114 /// Lay out a sequence of contiguous bitfields.
115 bool LayoutBitfields(const ASTRecordLayout &Layout,
116 unsigned &FirstFieldNo,
117 RecordDecl::field_iterator &FI,
118 RecordDecl::field_iterator FE);
119
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000120 /// LayoutField - try to layout all fields in the record decl.
121 /// Returns false if the operation failed because the struct is not packed.
122 bool LayoutFields(const RecordDecl *D);
123
Anders Carlssona518b2a2010-12-04 23:59:48 +0000124 /// Layout a single base, virtual or non-virtual
Eli Friedman3c840aa2011-12-12 23:13:20 +0000125 bool LayoutBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000126 const CGRecordLayout &baseLayout,
127 CharUnits baseOffset);
Anders Carlssona518b2a2010-12-04 23:59:48 +0000128
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000129 /// LayoutVirtualBase - layout a single virtual base.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000130 bool LayoutVirtualBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000131 CharUnits baseOffset);
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000132
Anders Carlssona459adb2010-11-28 19:18:44 +0000133 /// LayoutVirtualBases - layout the virtual bases of a record decl.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000134 bool LayoutVirtualBases(const CXXRecordDecl *RD,
Anders Carlssona459adb2010-11-28 19:18:44 +0000135 const ASTRecordLayout &Layout);
John McCall0153cd32011-11-08 04:01:03 +0000136
137 /// MSLayoutVirtualBases - layout the virtual bases of a record decl,
138 /// like MSVC.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000139 bool MSLayoutVirtualBases(const CXXRecordDecl *RD,
John McCall0153cd32011-11-08 04:01:03 +0000140 const ASTRecordLayout &Layout);
Anders Carlssona459adb2010-11-28 19:18:44 +0000141
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000142 /// LayoutNonVirtualBase - layout a single non-virtual base.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000143 bool LayoutNonVirtualBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000144 CharUnits baseOffset);
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000145
Anders Carlssona459adb2010-11-28 19:18:44 +0000146 /// LayoutNonVirtualBases - layout the virtual bases of a record decl.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000147 bool LayoutNonVirtualBases(const CXXRecordDecl *RD,
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000148 const ASTRecordLayout &Layout);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000149
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000150 /// ComputeNonVirtualBaseType - Compute the non-virtual base field types.
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000151 bool ComputeNonVirtualBaseType(const CXXRecordDecl *RD);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000152
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000153 /// LayoutField - layout a single field. Returns false if the operation failed
154 /// because the current struct is not packed.
155 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
156
157 /// LayoutBitField - layout a single bit field.
158 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
159
160 /// AppendField - Appends a field with the given offset and type.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000161 void AppendField(CharUnits fieldOffset, llvm::Type *FieldTy);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000162
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000163 /// AppendPadding - Appends enough padding bytes so that the total
164 /// struct size is a multiple of the field alignment.
John McCall4d9f1422011-02-15 22:21:29 +0000165 void AppendPadding(CharUnits fieldOffset, CharUnits fieldAlignment);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000166
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000167 /// ResizeLastBaseFieldIfNecessary - Fields and bases can be laid out in the
168 /// tail padding of a previous base. If this happens, the type of the previous
169 /// base needs to be changed to an array of i8. Returns true if the last
170 /// laid out base was resized.
171 bool ResizeLastBaseFieldIfNecessary(CharUnits offset);
172
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000173 /// getByteArrayType - Returns a byte array type with the given number of
174 /// elements.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000175 llvm::Type *getByteArrayType(CharUnits NumBytes);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000176
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000177 /// AppendBytes - Append a given number of bytes to the record.
John McCall4d9f1422011-02-15 22:21:29 +0000178 void AppendBytes(CharUnits numBytes);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000179
180 /// AppendTailPadding - Append enough tail padding so that the type will have
181 /// the passed size.
Ken Dyck272b6fa2011-04-24 16:53:44 +0000182 void AppendTailPadding(CharUnits RecordSize);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000183
Chris Lattner2192fe52011-07-18 04:24:23 +0000184 CharUnits getTypeAlignment(llvm::Type *Ty) const;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000185
Anders Carlssonacf877b2010-11-28 23:06:23 +0000186 /// getAlignmentAsLLVMStruct - Returns the maximum alignment of all the
187 /// LLVM element types.
John McCall4d9f1422011-02-15 22:21:29 +0000188 CharUnits getAlignmentAsLLVMStruct() const;
Anders Carlssonacf877b2010-11-28 23:06:23 +0000189
John McCall614dbdc2010-08-22 21:01:12 +0000190 /// CheckZeroInitializable - Check if the given type contains a pointer
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000191 /// to data member.
John McCall614dbdc2010-08-22 21:01:12 +0000192 void CheckZeroInitializable(QualType T);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000193
194public:
195 CGRecordLayoutBuilder(CodeGenTypes &Types)
John McCall0217dfc22011-02-15 06:40:56 +0000196 : BaseSubobjectType(0),
197 IsZeroInitializable(true), IsZeroInitializableAsBase(true),
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000198 Packed(false), IsMsStruct(false),
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000199 Types(Types) { }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000200
201 /// Layout - Will layout a RecordDecl.
202 void Layout(const RecordDecl *D);
203};
204
205}
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000206
Anders Carlsson307846f2009-07-23 03:17:50 +0000207void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
John McCall4d9f1422011-02-15 22:21:29 +0000208 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment();
Anders Carlsson09a37742009-09-02 17:51:33 +0000209 Packed = D->hasAttr<PackedAttr>();
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000210
Eli Friedman9ee2d0472012-10-12 23:29:20 +0000211 IsMsStruct = D->isMsStruct(Types.getContext());
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000212
Anders Carlsson697f6592009-07-23 03:43:54 +0000213 if (D->isUnion()) {
214 LayoutUnion(D);
215 return;
216 }
Anders Carlsson68e0b682009-08-08 18:23:56 +0000217
Anders Carlsson307846f2009-07-23 03:17:50 +0000218 if (LayoutFields(D))
219 return;
Mike Stump11289f42009-09-09 15:08:12 +0000220
Anders Carlsson307846f2009-07-23 03:17:50 +0000221 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlssond78fc892009-07-23 17:24:40 +0000222 Packed = true;
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000223 LastLaidOutBase.invalidate();
John McCall4d9f1422011-02-15 22:21:29 +0000224 NextFieldOffset = CharUnits::Zero();
Anders Carlsson307846f2009-07-23 03:17:50 +0000225 FieldTypes.clear();
John McCall0217dfc22011-02-15 06:40:56 +0000226 Fields.clear();
227 BitFields.clear();
228 NonVirtualBases.clear();
229 VirtualBases.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000230
Anders Carlsson307846f2009-07-23 03:17:50 +0000231 LayoutFields(D);
232}
233
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000234CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000235 const FieldDecl *FD,
236 uint64_t Offset, uint64_t Size,
237 uint64_t StorageSize,
238 uint64_t StorageAlignment) {
Chris Lattner2192fe52011-07-18 04:24:23 +0000239 llvm::Type *Ty = Types.ConvertTypeForMem(FD->getType());
John McCall8a3c5552011-02-26 08:41:59 +0000240 CharUnits TypeSizeInBytes =
Micah Villmowdd31ca12012-10-08 16:25:52 +0000241 CharUnits::fromQuantity(Types.getDataLayout().getTypeAllocSize(Ty));
John McCall8a3c5552011-02-26 08:41:59 +0000242 uint64_t TypeSizeInBits = Types.getContext().toBits(TypeSizeInBytes);
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000243
Douglas Gregor6ab2fa82011-05-20 16:38:50 +0000244 bool IsSigned = FD->getType()->isSignedIntegerOrEnumerationType();
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000245
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000246 if (Size > TypeSizeInBits) {
Anders Carlssond5f27b02010-04-17 22:54:57 +0000247 // We have a wide bit-field. The extra bits are only used for padding, so
248 // if we have a bitfield of type T, with size N:
249 //
250 // T t : N;
251 //
252 // We can just assume that it's:
253 //
254 // T t : sizeof(T);
255 //
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000256 Size = TypeSizeInBits;
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000257 }
258
Chandler Carruthfd8eca22012-12-09 07:26:04 +0000259 // Reverse the bit offsets for big endian machines. Because we represent
260 // a bitfield as a single large integer load, we can imagine the bits
261 // counting from the most-significant-bit instead of the
262 // least-significant-bit.
263 if (Types.getDataLayout().isBigEndian()) {
264 Offset = StorageSize - (Offset + Size);
265 }
Chris Lattnerfb59c7c2011-02-17 22:09:58 +0000266
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000267 return CGBitFieldInfo(Offset, Size, IsSigned, StorageSize, StorageAlignment);
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000268}
269
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000270/// \brief Layout the range of bitfields from BFI to BFE as contiguous storage.
271bool CGRecordLayoutBuilder::LayoutBitfields(const ASTRecordLayout &Layout,
272 unsigned &FirstFieldNo,
273 RecordDecl::field_iterator &FI,
274 RecordDecl::field_iterator FE) {
275 assert(FI != FE);
276 uint64_t FirstFieldOffset = Layout.getFieldOffset(FirstFieldNo);
277 uint64_t NextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset);
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000278
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000279 unsigned CharAlign = Types.getContext().getTargetInfo().getCharAlign();
280 assert(FirstFieldOffset % CharAlign == 0 &&
281 "First field offset is misaligned");
282 CharUnits FirstFieldOffsetInBytes
283 = Types.getContext().toCharUnitsFromBits(FirstFieldOffset);
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000284
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000285 unsigned StorageAlignment
286 = llvm::MinAlign(Alignment.getQuantity(),
287 FirstFieldOffsetInBytes.getQuantity());
Mike Stump11289f42009-09-09 15:08:12 +0000288
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000289 if (FirstFieldOffset < NextFieldOffsetInBits) {
290 CharUnits FieldOffsetInCharUnits =
291 Types.getContext().toCharUnitsFromBits(FirstFieldOffset);
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000292
293 // Try to resize the last base field.
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000294 if (!ResizeLastBaseFieldIfNecessary(FieldOffsetInCharUnits))
295 llvm_unreachable("We must be able to resize the last base if we need to "
296 "pack bits into it.");
297
298 NextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset);
299 assert(FirstFieldOffset >= NextFieldOffsetInBits);
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000300 }
301
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000302 // Append padding if necessary.
303 AppendPadding(Types.getContext().toCharUnitsFromBits(FirstFieldOffset),
304 CharUnits::One());
Mike Stump11289f42009-09-09 15:08:12 +0000305
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000306 // Find the last bitfield in a contiguous run of bitfields.
307 RecordDecl::field_iterator BFI = FI;
308 unsigned LastFieldNo = FirstFieldNo;
309 uint64_t NextContiguousFieldOffset = FirstFieldOffset;
310 for (RecordDecl::field_iterator FJ = FI;
311 (FJ != FE && (*FJ)->isBitField() &&
312 NextContiguousFieldOffset == Layout.getFieldOffset(LastFieldNo) &&
313 (*FJ)->getBitWidthValue(Types.getContext()) != 0); FI = FJ++) {
314 NextContiguousFieldOffset += (*FJ)->getBitWidthValue(Types.getContext());
315 ++LastFieldNo;
Anders Carlsson307846f2009-07-23 03:17:50 +0000316
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000317 // We must use packed structs for packed fields, and also unnamed bit
318 // fields since they don't affect the struct alignment.
319 if (!Packed && ((*FJ)->hasAttr<PackedAttr>() || !(*FJ)->getDeclName()))
320 return false;
321 }
322 RecordDecl::field_iterator BFE = llvm::next(FI);
323 --LastFieldNo;
324 assert(LastFieldNo >= FirstFieldNo && "Empty run of contiguous bitfields");
325 FieldDecl *LastFD = *FI;
Mike Stump11289f42009-09-09 15:08:12 +0000326
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000327 // Find the last bitfield's offset, add its size, and round it up to the
328 // character alignment to compute the storage required.
329 uint64_t LastFieldOffset = Layout.getFieldOffset(LastFieldNo);
330 uint64_t LastFieldSize = LastFD->getBitWidthValue(Types.getContext());
331 uint64_t TotalBits = (LastFieldOffset + LastFieldSize) - FirstFieldOffset;
332 CharUnits StorageBytes = Types.getContext().toCharUnitsFromBits(
333 llvm::RoundUpToAlignment(TotalBits, CharAlign));
334 uint64_t StorageBits = Types.getContext().toBits(StorageBytes);
Mike Stump11289f42009-09-09 15:08:12 +0000335
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000336 // Grow the storage to encompass any known padding in the layout when doing
337 // so will make the storage a power-of-two. There are two cases when we can
338 // do this. The first is when we have a subsequent field and can widen up to
339 // its offset. The second is when the data size of the AST record layout is
340 // past the end of the current storage. The latter is true when there is tail
341 // padding on a struct and no members of a super class can be packed into it.
342 //
343 // Note that we widen the storage as much as possible here to express the
344 // maximum latitude the language provides, and rely on the backend to lower
345 // these in conjunction with shifts and masks to narrower operations where
346 // beneficial.
347 uint64_t EndOffset = Types.getContext().toBits(Layout.getDataSize());
348 if (BFE != FE)
349 // If there are more fields to be laid out, the offset at the end of the
350 // bitfield is the offset of the next field in the record.
351 EndOffset = Layout.getFieldOffset(LastFieldNo + 1);
352 assert(EndOffset >= (FirstFieldOffset + TotalBits) &&
353 "End offset is not past the end of the known storage bits.");
354 uint64_t SpaceBits = EndOffset - FirstFieldOffset;
355 uint64_t LongBits = Types.getContext().getTargetInfo().getLongWidth();
356 uint64_t WidenedBits = (StorageBits / LongBits) * LongBits +
357 llvm::NextPowerOf2(StorageBits % LongBits - 1);
358 assert(WidenedBits >= StorageBits && "Widening shrunk the bits!");
359 if (WidenedBits <= SpaceBits) {
360 StorageBits = WidenedBits;
361 StorageBytes = Types.getContext().toCharUnitsFromBits(StorageBits);
362 assert(StorageBits == (uint64_t)Types.getContext().toBits(StorageBytes));
Anders Carlsson307846f2009-07-23 03:17:50 +0000363 }
364
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000365 unsigned FieldIndex = FieldTypes.size();
366 AppendBytes(StorageBytes);
Mike Stump11289f42009-09-09 15:08:12 +0000367
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000368 // Now walk the bitfields associating them with this field of storage and
369 // building up the bitfield specific info.
370 unsigned FieldNo = FirstFieldNo;
371 for (; BFI != BFE; ++BFI, ++FieldNo) {
372 FieldDecl *FD = *BFI;
373 uint64_t FieldOffset = Layout.getFieldOffset(FieldNo) - FirstFieldOffset;
374 uint64_t FieldSize = FD->getBitWidthValue(Types.getContext());
375 Fields[FD] = FieldIndex;
376 BitFields[FD] = CGBitFieldInfo::MakeInfo(Types, FD, FieldOffset, FieldSize,
377 StorageBits, StorageAlignment);
378 }
379 FirstFieldNo = LastFieldNo;
380 return true;
Anders Carlsson307846f2009-07-23 03:17:50 +0000381}
382
383bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
John McCall4d9f1422011-02-15 22:21:29 +0000384 uint64_t fieldOffset) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000385 // If the field is packed, then we need a packed struct.
Anders Carlsson68e0b682009-08-08 18:23:56 +0000386 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson307846f2009-07-23 03:17:50 +0000387 return false;
388
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000389 assert(!D->isBitField() && "Bitfields should be laid out seperately.");
Mike Stump11289f42009-09-09 15:08:12 +0000390
John McCall614dbdc2010-08-22 21:01:12 +0000391 CheckZeroInitializable(D->getType());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000392
John McCall8a3c5552011-02-26 08:41:59 +0000393 assert(fieldOffset % Types.getTarget().getCharWidth() == 0
394 && "field offset is not on a byte boundary!");
395 CharUnits fieldOffsetInBytes
396 = Types.getContext().toCharUnitsFromBits(fieldOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000397
Chris Lattnera5f58b02011-07-09 17:41:47 +0000398 llvm::Type *Ty = Types.ConvertTypeForMem(D->getType());
John McCall4d9f1422011-02-15 22:21:29 +0000399 CharUnits typeAlignment = getTypeAlignment(Ty);
Anders Carlsson19702bb2009-08-04 16:29:15 +0000400
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000401 // If the type alignment is larger then the struct alignment, we must use
402 // a packed struct.
John McCall4d9f1422011-02-15 22:21:29 +0000403 if (typeAlignment > Alignment) {
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000404 assert(!Packed && "Alignment is wrong even with packed struct!");
405 return false;
406 }
Mike Stump11289f42009-09-09 15:08:12 +0000407
John McCall4d9f1422011-02-15 22:21:29 +0000408 if (!Packed) {
409 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
410 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
411 if (const MaxFieldAlignmentAttr *MFAA =
412 RD->getAttr<MaxFieldAlignmentAttr>()) {
John McCall8a3c5552011-02-26 08:41:59 +0000413 if (MFAA->getAlignment() != Types.getContext().toBits(typeAlignment))
John McCall4d9f1422011-02-15 22:21:29 +0000414 return false;
415 }
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000416 }
417 }
418
Anders Carlsson19702bb2009-08-04 16:29:15 +0000419 // Round up the field offset to the alignment of the field type.
John McCall4d9f1422011-02-15 22:21:29 +0000420 CharUnits alignedNextFieldOffsetInBytes =
421 NextFieldOffset.RoundUpToAlignment(typeAlignment);
Anders Carlsson19702bb2009-08-04 16:29:15 +0000422
John McCall4d9f1422011-02-15 22:21:29 +0000423 if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000424 // Try to resize the last base field.
425 if (ResizeLastBaseFieldIfNecessary(fieldOffsetInBytes)) {
426 alignedNextFieldOffsetInBytes =
427 NextFieldOffset.RoundUpToAlignment(typeAlignment);
428 }
429 }
430
431 if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
Anders Carlsson19702bb2009-08-04 16:29:15 +0000432 assert(!Packed && "Could not place field even with packed struct!");
433 return false;
434 }
Mike Stump11289f42009-09-09 15:08:12 +0000435
John McCall4d9f1422011-02-15 22:21:29 +0000436 AppendPadding(fieldOffsetInBytes, typeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +0000437
Anders Carlsson307846f2009-07-23 03:17:50 +0000438 // Now append the field.
John McCall0217dfc22011-02-15 06:40:56 +0000439 Fields[D] = FieldTypes.size();
John McCall4d9f1422011-02-15 22:21:29 +0000440 AppendField(fieldOffsetInBytes, Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000441
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000442 LastLaidOutBase.invalidate();
Anders Carlsson307846f2009-07-23 03:17:50 +0000443 return true;
444}
445
Chris Lattnera5f58b02011-07-09 17:41:47 +0000446llvm::Type *
Anders Carlsson1de2f572010-04-17 20:49:27 +0000447CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
448 const ASTRecordLayout &Layout) {
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000449 Fields[Field] = 0;
Anders Carlsson1de2f572010-04-17 20:49:27 +0000450 if (Field->isBitField()) {
Richard Smithcaf33902011-10-10 18:28:20 +0000451 uint64_t FieldSize = Field->getBitWidthValue(Types.getContext());
Anders Carlsson1de2f572010-04-17 20:49:27 +0000452
453 // Ignore zero sized bit fields.
454 if (FieldSize == 0)
455 return 0;
456
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000457 unsigned StorageBits = llvm::RoundUpToAlignment(
458 FieldSize, Types.getContext().getTargetInfo().getCharAlign());
459 CharUnits NumBytesToAppend
460 = Types.getContext().toCharUnitsFromBits(StorageBits);
Anders Carlsson2295f132010-04-17 21:04:52 +0000461
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000462 llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
Ken Dyck7a0b19f2011-04-24 16:47:33 +0000463 if (NumBytesToAppend > CharUnits::One())
464 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend.getQuantity());
Anders Carlsson2295f132010-04-17 21:04:52 +0000465
Anders Carlsson1de2f572010-04-17 20:49:27 +0000466 // Add the bit field info.
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000467 BitFields[Field] = CGBitFieldInfo::MakeInfo(Types, Field, 0, FieldSize,
468 StorageBits,
469 Alignment.getQuantity());
Anders Carlsson2295f132010-04-17 21:04:52 +0000470 return FieldTy;
Anders Carlsson1de2f572010-04-17 20:49:27 +0000471 }
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000472
Anders Carlsson1de2f572010-04-17 20:49:27 +0000473 // This is a regular union field.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000474 return Types.ConvertTypeForMem(Field->getType());
Anders Carlsson1de2f572010-04-17 20:49:27 +0000475}
476
Anders Carlsson697f6592009-07-23 03:43:54 +0000477void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
478 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump11289f42009-09-09 15:08:12 +0000479
John McCall4d9f1422011-02-15 22:21:29 +0000480 const ASTRecordLayout &layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000481
Chris Lattnera5f58b02011-07-09 17:41:47 +0000482 llvm::Type *unionType = 0;
John McCall4d9f1422011-02-15 22:21:29 +0000483 CharUnits unionSize = CharUnits::Zero();
484 CharUnits unionAlign = CharUnits::Zero();
Mike Stump11289f42009-09-09 15:08:12 +0000485
John McCall4d9f1422011-02-15 22:21:29 +0000486 bool hasOnlyZeroSizedBitFields = true;
Eli Friedmandae858a2011-12-07 01:30:11 +0000487 bool checkedFirstFieldZeroInit = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000488
John McCall4d9f1422011-02-15 22:21:29 +0000489 unsigned fieldNo = 0;
490 for (RecordDecl::field_iterator field = D->field_begin(),
491 fieldEnd = D->field_end(); field != fieldEnd; ++field, ++fieldNo) {
492 assert(layout.getFieldOffset(fieldNo) == 0 &&
Anders Carlsson697f6592009-07-23 03:43:54 +0000493 "Union field offset did not start at the beginning of record!");
David Blaikie40ed2972012-06-06 20:45:41 +0000494 llvm::Type *fieldType = LayoutUnionField(*field, layout);
Anders Carlssonf814ee62009-07-23 04:00:39 +0000495
John McCall4d9f1422011-02-15 22:21:29 +0000496 if (!fieldType)
Anders Carlsson1de2f572010-04-17 20:49:27 +0000497 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000498
Eli Friedmandae858a2011-12-07 01:30:11 +0000499 if (field->getDeclName() && !checkedFirstFieldZeroInit) {
500 CheckZeroInitializable(field->getType());
501 checkedFirstFieldZeroInit = true;
502 }
503
John McCall4d9f1422011-02-15 22:21:29 +0000504 hasOnlyZeroSizedBitFields = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000505
John McCall4d9f1422011-02-15 22:21:29 +0000506 CharUnits fieldAlign = CharUnits::fromQuantity(
Micah Villmowdd31ca12012-10-08 16:25:52 +0000507 Types.getDataLayout().getABITypeAlignment(fieldType));
John McCall4d9f1422011-02-15 22:21:29 +0000508 CharUnits fieldSize = CharUnits::fromQuantity(
Micah Villmowdd31ca12012-10-08 16:25:52 +0000509 Types.getDataLayout().getTypeAllocSize(fieldType));
Mike Stump11289f42009-09-09 15:08:12 +0000510
John McCall4d9f1422011-02-15 22:21:29 +0000511 if (fieldAlign < unionAlign)
Anders Carlsson697f6592009-07-23 03:43:54 +0000512 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000513
John McCall4d9f1422011-02-15 22:21:29 +0000514 if (fieldAlign > unionAlign || fieldSize > unionSize) {
515 unionType = fieldType;
516 unionAlign = fieldAlign;
517 unionSize = fieldSize;
Anders Carlsson697f6592009-07-23 03:43:54 +0000518 }
519 }
Mike Stump11289f42009-09-09 15:08:12 +0000520
Anders Carlsson697f6592009-07-23 03:43:54 +0000521 // Now add our field.
John McCall4d9f1422011-02-15 22:21:29 +0000522 if (unionType) {
523 AppendField(CharUnits::Zero(), unionType);
Anders Carlsson0e912752009-09-03 22:56:02 +0000524
John McCall4d9f1422011-02-15 22:21:29 +0000525 if (getTypeAlignment(unionType) > layout.getAlignment()) {
Anders Carlsson0e912752009-09-03 22:56:02 +0000526 // We need a packed struct.
527 Packed = true;
John McCall4d9f1422011-02-15 22:21:29 +0000528 unionAlign = CharUnits::One();
Anders Carlsson0e912752009-09-03 22:56:02 +0000529 }
530 }
John McCall4d9f1422011-02-15 22:21:29 +0000531 if (unionAlign.isZero()) {
Chandler Carruth52b6ac22012-03-04 12:16:40 +0000532 (void)hasOnlyZeroSizedBitFields;
John McCall4d9f1422011-02-15 22:21:29 +0000533 assert(hasOnlyZeroSizedBitFields &&
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000534 "0-align record did not have all zero-sized bit-fields!");
John McCall4d9f1422011-02-15 22:21:29 +0000535 unionAlign = CharUnits::One();
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000536 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000537
Anders Carlsson697f6592009-07-23 03:43:54 +0000538 // Append tail padding.
John McCall4d9f1422011-02-15 22:21:29 +0000539 CharUnits recordSize = layout.getSize();
540 if (recordSize > unionSize)
541 AppendPadding(recordSize, unionAlign);
Anders Carlsson697f6592009-07-23 03:43:54 +0000542}
543
Eli Friedman3c840aa2011-12-12 23:13:20 +0000544bool CGRecordLayoutBuilder::LayoutBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000545 const CGRecordLayout &baseLayout,
546 CharUnits baseOffset) {
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000547 ResizeLastBaseFieldIfNecessary(baseOffset);
548
John McCall4d9f1422011-02-15 22:21:29 +0000549 AppendPadding(baseOffset, CharUnits::One());
Anders Carlssona518b2a2010-12-04 23:59:48 +0000550
John McCall0217dfc22011-02-15 06:40:56 +0000551 const ASTRecordLayout &baseASTLayout
552 = Types.getContext().getASTRecordLayout(base);
553
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000554 LastLaidOutBase.Offset = NextFieldOffset;
555 LastLaidOutBase.NonVirtualSize = baseASTLayout.getNonVirtualSize();
556
Chris Lattnera5f58b02011-07-09 17:41:47 +0000557 llvm::StructType *subobjectType = baseLayout.getBaseSubobjectLLVMType();
Eli Friedman3c840aa2011-12-12 23:13:20 +0000558 if (getTypeAlignment(subobjectType) > Alignment)
559 return false;
560
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000561 AppendField(baseOffset, subobjectType);
Eli Friedman3c840aa2011-12-12 23:13:20 +0000562 return true;
John McCall0217dfc22011-02-15 06:40:56 +0000563}
564
Eli Friedman3c840aa2011-12-12 23:13:20 +0000565bool CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000566 CharUnits baseOffset) {
John McCall0217dfc22011-02-15 06:40:56 +0000567 // Ignore empty bases.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000568 if (base->isEmpty()) return true;
John McCall0217dfc22011-02-15 06:40:56 +0000569
570 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
571 if (IsZeroInitializableAsBase) {
572 assert(IsZeroInitializable &&
573 "class zero-initializable as base but not as complete object");
574
575 IsZeroInitializable = IsZeroInitializableAsBase =
576 baseLayout.isZeroInitializableAsBase();
577 }
578
Eli Friedman3c840aa2011-12-12 23:13:20 +0000579 if (!LayoutBase(base, baseLayout, baseOffset))
580 return false;
John McCall0217dfc22011-02-15 06:40:56 +0000581 NonVirtualBases[base] = (FieldTypes.size() - 1);
Eli Friedman3c840aa2011-12-12 23:13:20 +0000582 return true;
Anders Carlssona518b2a2010-12-04 23:59:48 +0000583}
584
Eli Friedman3c840aa2011-12-12 23:13:20 +0000585bool
John McCall0217dfc22011-02-15 06:40:56 +0000586CGRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000587 CharUnits baseOffset) {
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000588 // Ignore empty bases.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000589 if (base->isEmpty()) return true;
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000590
John McCall0217dfc22011-02-15 06:40:56 +0000591 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
592 if (IsZeroInitializable)
593 IsZeroInitializable = baseLayout.isZeroInitializableAsBase();
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000594
Eli Friedman3c840aa2011-12-12 23:13:20 +0000595 if (!LayoutBase(base, baseLayout, baseOffset))
596 return false;
John McCall0217dfc22011-02-15 06:40:56 +0000597 VirtualBases[base] = (FieldTypes.size() - 1);
Eli Friedman3c840aa2011-12-12 23:13:20 +0000598 return true;
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000599}
600
Eli Friedman3c840aa2011-12-12 23:13:20 +0000601bool
John McCall0153cd32011-11-08 04:01:03 +0000602CGRecordLayoutBuilder::MSLayoutVirtualBases(const CXXRecordDecl *RD,
603 const ASTRecordLayout &Layout) {
604 if (!RD->getNumVBases())
Eli Friedman3c840aa2011-12-12 23:13:20 +0000605 return true;
John McCall0153cd32011-11-08 04:01:03 +0000606
607 // The vbases list is uniqued and ordered by a depth-first
608 // traversal, which is what we need here.
609 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
610 E = RD->vbases_end(); I != E; ++I) {
611
612 const CXXRecordDecl *BaseDecl =
613 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
614
615 CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl);
Eli Friedman3c840aa2011-12-12 23:13:20 +0000616 if (!LayoutVirtualBase(BaseDecl, vbaseOffset))
617 return false;
John McCall0153cd32011-11-08 04:01:03 +0000618 }
Eli Friedman3c840aa2011-12-12 23:13:20 +0000619 return true;
John McCall0153cd32011-11-08 04:01:03 +0000620}
621
Anders Carlssona459adb2010-11-28 19:18:44 +0000622/// LayoutVirtualBases - layout the non-virtual bases of a record decl.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000623bool
Anders Carlssona459adb2010-11-28 19:18:44 +0000624CGRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
625 const ASTRecordLayout &Layout) {
626 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
627 E = RD->bases_end(); I != E; ++I) {
628 const CXXRecordDecl *BaseDecl =
629 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
630
631 // We only want to lay out virtual bases that aren't indirect primary bases
632 // of some other base.
633 if (I->isVirtual() && !IndirectPrimaryBases.count(BaseDecl)) {
634 // Only lay out the base once.
635 if (!LaidOutVirtualBases.insert(BaseDecl))
636 continue;
637
John McCall4d9f1422011-02-15 22:21:29 +0000638 CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl);
Eli Friedman3c840aa2011-12-12 23:13:20 +0000639 if (!LayoutVirtualBase(BaseDecl, vbaseOffset))
640 return false;
Anders Carlssona459adb2010-11-28 19:18:44 +0000641 }
642
643 if (!BaseDecl->getNumVBases()) {
644 // This base isn't interesting since it doesn't have any virtual bases.
645 continue;
646 }
647
Eli Friedman3c840aa2011-12-12 23:13:20 +0000648 if (!LayoutVirtualBases(BaseDecl, Layout))
649 return false;
Anders Carlssona459adb2010-11-28 19:18:44 +0000650 }
Eli Friedman3c840aa2011-12-12 23:13:20 +0000651 return true;
Anders Carlssona459adb2010-11-28 19:18:44 +0000652}
653
Eli Friedman3c840aa2011-12-12 23:13:20 +0000654bool
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000655CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD,
656 const ASTRecordLayout &Layout) {
657 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
658
John McCall0153cd32011-11-08 04:01:03 +0000659 // If we have a primary base, lay it out first.
660 if (PrimaryBase) {
Eli Friedman3c840aa2011-12-12 23:13:20 +0000661 if (!Layout.isPrimaryBaseVirtual()) {
662 if (!LayoutNonVirtualBase(PrimaryBase, CharUnits::Zero()))
663 return false;
664 } else {
665 if (!LayoutVirtualBase(PrimaryBase, CharUnits::Zero()))
666 return false;
667 }
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000668
John McCall0153cd32011-11-08 04:01:03 +0000669 // Otherwise, add a vtable / vf-table if the layout says to do so.
John McCalle42a3362012-05-01 08:55:32 +0000670 } else if (Layout.hasOwnVFPtr()) {
John McCall0153cd32011-11-08 04:01:03 +0000671 llvm::Type *FunctionType =
672 llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()),
673 /*isVarArg=*/true);
674 llvm::Type *VTableTy = FunctionType->getPointerTo();
Eli Friedmanf927b8b2012-04-27 02:34:46 +0000675
676 if (getTypeAlignment(VTableTy) > Alignment) {
677 // FIXME: Should we allow this to happen in Sema?
678 assert(!Packed && "Alignment is wrong even with packed struct!");
679 return false;
680 }
681
John McCall0153cd32011-11-08 04:01:03 +0000682 assert(NextFieldOffset.isZero() &&
683 "VTable pointer must come first!");
684 AppendField(CharUnits::Zero(), VTableTy->getPointerTo());
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000685 }
686
687 // Layout the non-virtual bases.
688 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
689 E = RD->bases_end(); I != E; ++I) {
690 if (I->isVirtual())
691 continue;
692
693 const CXXRecordDecl *BaseDecl =
694 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
695
696 // We've already laid out the primary base.
Anders Carlsson7f95cd12010-11-24 23:12:57 +0000697 if (BaseDecl == PrimaryBase && !Layout.isPrimaryBaseVirtual())
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000698 continue;
699
Eli Friedman3c840aa2011-12-12 23:13:20 +0000700 if (!LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffset(BaseDecl)))
701 return false;
Anders Carlssond681a292009-12-16 17:27:20 +0000702 }
John McCall0153cd32011-11-08 04:01:03 +0000703
704 // Add a vb-table pointer if the layout insists.
705 if (Layout.getVBPtrOffset() != CharUnits::fromQuantity(-1)) {
706 CharUnits VBPtrOffset = Layout.getVBPtrOffset();
707 llvm::Type *Vbptr = llvm::Type::getInt32PtrTy(Types.getLLVMContext());
708 AppendPadding(VBPtrOffset, getTypeAlignment(Vbptr));
709 AppendField(VBPtrOffset, Vbptr);
710 }
Eli Friedman3c840aa2011-12-12 23:13:20 +0000711
712 return true;
Anders Carlssond681a292009-12-16 17:27:20 +0000713}
714
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000715bool
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000716CGRecordLayoutBuilder::ComputeNonVirtualBaseType(const CXXRecordDecl *RD) {
717 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(RD);
718
Ken Dyckbec02852011-02-08 02:02:47 +0000719 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
720 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
John McCall4d9f1422011-02-15 22:21:29 +0000721 CharUnits AlignedNonVirtualTypeSize =
722 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000723
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000724 // First check if we can use the same fields as for the complete class.
John McCall4d9f1422011-02-15 22:21:29 +0000725 CharUnits RecordSize = Layout.getSize();
John McCall0217dfc22011-02-15 06:40:56 +0000726 if (AlignedNonVirtualTypeSize == RecordSize)
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000727 return true;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000728
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000729 // Check if we need padding.
John McCall4d9f1422011-02-15 22:21:29 +0000730 CharUnits AlignedNextFieldOffset =
731 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000732
John McCall0217dfc22011-02-15 06:40:56 +0000733 if (AlignedNextFieldOffset > AlignedNonVirtualTypeSize) {
734 assert(!Packed && "cannot layout even as packed struct");
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000735 return false; // Needs packing.
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000736 }
737
John McCall0217dfc22011-02-15 06:40:56 +0000738 bool needsPadding = (AlignedNonVirtualTypeSize != AlignedNextFieldOffset);
739 if (needsPadding) {
John McCall4d9f1422011-02-15 22:21:29 +0000740 CharUnits NumBytes = AlignedNonVirtualTypeSize - AlignedNextFieldOffset;
John McCall0217dfc22011-02-15 06:40:56 +0000741 FieldTypes.push_back(getByteArrayType(NumBytes));
742 }
Chris Lattnera5f58b02011-07-09 17:41:47 +0000743
Chris Lattner5ec04a52011-08-12 17:43:31 +0000744 BaseSubobjectType = llvm::StructType::create(Types.getLLVMContext(),
745 FieldTypes, "", Packed);
Chris Lattnera5f58b02011-07-09 17:41:47 +0000746 Types.addRecordTypeName(RD, BaseSubobjectType, ".base");
John McCall0217dfc22011-02-15 06:40:56 +0000747
Chris Lattnera5f58b02011-07-09 17:41:47 +0000748 // Pull the padding back off.
749 if (needsPadding)
John McCall0217dfc22011-02-15 06:40:56 +0000750 FieldTypes.pop_back();
John McCall0217dfc22011-02-15 06:40:56 +0000751
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000752 return true;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000753}
754
Anders Carlsson307846f2009-07-23 03:17:50 +0000755bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
756 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
John McCall4d9f1422011-02-15 22:21:29 +0000757 assert(!Alignment.isZero() && "Did not set alignment!");
Mike Stump11289f42009-09-09 15:08:12 +0000758
Anders Carlsson697f6592009-07-23 03:43:54 +0000759 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000760
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000761 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
762 if (RD)
Eli Friedman3c840aa2011-12-12 23:13:20 +0000763 if (!LayoutNonVirtualBases(RD, Layout))
764 return false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000765
Anders Carlsson307846f2009-07-23 03:17:50 +0000766 unsigned FieldNo = 0;
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000767 const FieldDecl *LastFD = 0;
768
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000769 for (RecordDecl::field_iterator FI = D->field_begin(), FE = D->field_end();
770 FI != FE; ++FI, ++FieldNo) {
771 FieldDecl *FD = *FI;
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000772 if (IsMsStruct) {
773 // Zero-length bitfields following non-bitfield members are
774 // ignored:
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +0000775 if (Types.getContext().ZeroBitfieldFollowsNonBitfield(FD, LastFD)) {
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000776 --FieldNo;
777 continue;
778 }
779 LastFD = FD;
780 }
Chandler Carruthff0e3a12012-12-06 11:14:44 +0000781
782 // If this field is a bitfield, layout all of the consecutive
783 // non-zero-length bitfields and the last zero-length bitfield; these will
784 // all share storage.
785 if (FD->isBitField()) {
786 // If all we have is a zero-width bitfield, skip it.
787 if (FD->getBitWidthValue(Types.getContext()) == 0)
788 continue;
789
790 // Layout this range of bitfields.
791 if (!LayoutBitfields(Layout, FieldNo, FI, FE)) {
792 assert(!Packed &&
793 "Could not layout bitfields even with a packed LLVM struct!");
794 return false;
795 }
796 assert(FI != FE && "Advanced past the last bitfield");
797 continue;
798 }
799
800 if (!LayoutField(FD, Layout.getFieldOffset(FieldNo))) {
Mike Stump11289f42009-09-09 15:08:12 +0000801 assert(!Packed &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000802 "Could not layout fields even with a packed LLVM struct!");
803 return false;
804 }
805 }
806
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000807 if (RD) {
Anders Carlssona459adb2010-11-28 19:18:44 +0000808 // We've laid out the non-virtual bases and the fields, now compute the
809 // non-virtual base field types.
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000810 if (!ComputeNonVirtualBaseType(RD)) {
811 assert(!Packed && "Could not layout even with a packed LLVM struct!");
812 return false;
813 }
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000814
John McCall0153cd32011-11-08 04:01:03 +0000815 // Lay out the virtual bases. The MS ABI uses a different
816 // algorithm here due to the lack of primary virtual bases.
817 if (Types.getContext().getTargetInfo().getCXXABI() != CXXABI_Microsoft) {
818 RD->getIndirectPrimaryBases(IndirectPrimaryBases);
819 if (Layout.isPrimaryBaseVirtual())
820 IndirectPrimaryBases.insert(Layout.getPrimaryBase());
821
Eli Friedman3c840aa2011-12-12 23:13:20 +0000822 if (!LayoutVirtualBases(RD, Layout))
823 return false;
John McCall0153cd32011-11-08 04:01:03 +0000824 } else {
Eli Friedman3c840aa2011-12-12 23:13:20 +0000825 if (!MSLayoutVirtualBases(RD, Layout))
826 return false;
John McCall0153cd32011-11-08 04:01:03 +0000827 }
Anders Carlssona459adb2010-11-28 19:18:44 +0000828 }
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000829
Anders Carlsson307846f2009-07-23 03:17:50 +0000830 // Append tail padding if necessary.
Ken Dyck272b6fa2011-04-24 16:53:44 +0000831 AppendTailPadding(Layout.getSize());
Mike Stump11289f42009-09-09 15:08:12 +0000832
Anders Carlsson307846f2009-07-23 03:17:50 +0000833 return true;
834}
835
Ken Dyck272b6fa2011-04-24 16:53:44 +0000836void CGRecordLayoutBuilder::AppendTailPadding(CharUnits RecordSize) {
837 ResizeLastBaseFieldIfNecessary(RecordSize);
Mike Stump11289f42009-09-09 15:08:12 +0000838
Ken Dyck272b6fa2011-04-24 16:53:44 +0000839 assert(NextFieldOffset <= RecordSize && "Size mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000840
John McCall4d9f1422011-02-15 22:21:29 +0000841 CharUnits AlignedNextFieldOffset =
842 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
Anders Carlsson220bf4f2009-12-08 01:24:23 +0000843
Ken Dyck272b6fa2011-04-24 16:53:44 +0000844 if (AlignedNextFieldOffset == RecordSize) {
Anders Carlsson220bf4f2009-12-08 01:24:23 +0000845 // We don't need any padding.
846 return;
847 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000848
Ken Dyck272b6fa2011-04-24 16:53:44 +0000849 CharUnits NumPadBytes = RecordSize - NextFieldOffset;
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000850 AppendBytes(NumPadBytes);
851}
852
John McCall4d9f1422011-02-15 22:21:29 +0000853void CGRecordLayoutBuilder::AppendField(CharUnits fieldOffset,
Chris Lattnera5f58b02011-07-09 17:41:47 +0000854 llvm::Type *fieldType) {
John McCall4d9f1422011-02-15 22:21:29 +0000855 CharUnits fieldSize =
Micah Villmowdd31ca12012-10-08 16:25:52 +0000856 CharUnits::fromQuantity(Types.getDataLayout().getTypeAllocSize(fieldType));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000857
John McCall4d9f1422011-02-15 22:21:29 +0000858 FieldTypes.push_back(fieldType);
Anders Carlsson307846f2009-07-23 03:17:50 +0000859
John McCall4d9f1422011-02-15 22:21:29 +0000860 NextFieldOffset = fieldOffset + fieldSize;
Anders Carlsson307846f2009-07-23 03:17:50 +0000861}
862
John McCall4d9f1422011-02-15 22:21:29 +0000863void CGRecordLayoutBuilder::AppendPadding(CharUnits fieldOffset,
864 CharUnits fieldAlignment) {
865 assert(NextFieldOffset <= fieldOffset &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000866 "Incorrect field layout!");
Mike Stump11289f42009-09-09 15:08:12 +0000867
John McCall0153cd32011-11-08 04:01:03 +0000868 // Do nothing if we're already at the right offset.
869 if (fieldOffset == NextFieldOffset) return;
Anders Carlsson307846f2009-07-23 03:17:50 +0000870
John McCall0153cd32011-11-08 04:01:03 +0000871 // If we're not emitting a packed LLVM type, try to avoid adding
872 // unnecessary padding fields.
873 if (!Packed) {
874 // Round up the field offset to the alignment of the field type.
875 CharUnits alignedNextFieldOffset =
876 NextFieldOffset.RoundUpToAlignment(fieldAlignment);
877 assert(alignedNextFieldOffset <= fieldOffset);
Anders Carlsson307846f2009-07-23 03:17:50 +0000878
John McCall0153cd32011-11-08 04:01:03 +0000879 // If that's the right offset, we're done.
880 if (alignedNextFieldOffset == fieldOffset) return;
Anders Carlsson307846f2009-07-23 03:17:50 +0000881 }
John McCall0153cd32011-11-08 04:01:03 +0000882
883 // Otherwise we need explicit padding.
884 CharUnits padding = fieldOffset - NextFieldOffset;
885 AppendBytes(padding);
Anders Carlsson307846f2009-07-23 03:17:50 +0000886}
887
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000888bool CGRecordLayoutBuilder::ResizeLastBaseFieldIfNecessary(CharUnits offset) {
889 // Check if we have a base to resize.
890 if (!LastLaidOutBase.isValid())
891 return false;
892
893 // This offset does not overlap with the tail padding.
894 if (offset >= NextFieldOffset)
895 return false;
896
897 // Restore the field offset and append an i8 array instead.
898 FieldTypes.pop_back();
899 NextFieldOffset = LastLaidOutBase.Offset;
900 AppendBytes(LastLaidOutBase.NonVirtualSize);
901 LastLaidOutBase.invalidate();
902
903 return true;
904}
905
Chris Lattnera5f58b02011-07-09 17:41:47 +0000906llvm::Type *CGRecordLayoutBuilder::getByteArrayType(CharUnits numBytes) {
John McCall4d9f1422011-02-15 22:21:29 +0000907 assert(!numBytes.isZero() && "Empty byte arrays aren't allowed.");
Mike Stump11289f42009-09-09 15:08:12 +0000908
Chris Lattnera5f58b02011-07-09 17:41:47 +0000909 llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
John McCall4d9f1422011-02-15 22:21:29 +0000910 if (numBytes > CharUnits::One())
911 Ty = llvm::ArrayType::get(Ty, numBytes.getQuantity());
Mike Stump11289f42009-09-09 15:08:12 +0000912
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000913 return Ty;
914}
915
John McCall4d9f1422011-02-15 22:21:29 +0000916void CGRecordLayoutBuilder::AppendBytes(CharUnits numBytes) {
917 if (numBytes.isZero())
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000918 return;
919
Anders Carlsson307846f2009-07-23 03:17:50 +0000920 // Append the padding field
John McCall4d9f1422011-02-15 22:21:29 +0000921 AppendField(NextFieldOffset, getByteArrayType(numBytes));
Anders Carlsson307846f2009-07-23 03:17:50 +0000922}
923
Chris Lattner2192fe52011-07-18 04:24:23 +0000924CharUnits CGRecordLayoutBuilder::getTypeAlignment(llvm::Type *Ty) const {
Anders Carlssond78fc892009-07-23 17:24:40 +0000925 if (Packed)
John McCall4d9f1422011-02-15 22:21:29 +0000926 return CharUnits::One();
Mike Stump11289f42009-09-09 15:08:12 +0000927
Micah Villmowdd31ca12012-10-08 16:25:52 +0000928 return CharUnits::fromQuantity(Types.getDataLayout().getABITypeAlignment(Ty));
Anders Carlsson307846f2009-07-23 03:17:50 +0000929}
930
John McCall4d9f1422011-02-15 22:21:29 +0000931CharUnits CGRecordLayoutBuilder::getAlignmentAsLLVMStruct() const {
Anders Carlssonacf877b2010-11-28 23:06:23 +0000932 if (Packed)
John McCall4d9f1422011-02-15 22:21:29 +0000933 return CharUnits::One();
Anders Carlssonacf877b2010-11-28 23:06:23 +0000934
John McCall4d9f1422011-02-15 22:21:29 +0000935 CharUnits maxAlignment = CharUnits::One();
Anders Carlssonacf877b2010-11-28 23:06:23 +0000936 for (size_t i = 0; i != FieldTypes.size(); ++i)
John McCall4d9f1422011-02-15 22:21:29 +0000937 maxAlignment = std::max(maxAlignment, getTypeAlignment(FieldTypes[i]));
Anders Carlssonacf877b2010-11-28 23:06:23 +0000938
John McCall4d9f1422011-02-15 22:21:29 +0000939 return maxAlignment;
Anders Carlssonacf877b2010-11-28 23:06:23 +0000940}
941
John McCall0217dfc22011-02-15 06:40:56 +0000942/// Merge in whether a field of the given type is zero-initializable.
John McCall614dbdc2010-08-22 21:01:12 +0000943void CGRecordLayoutBuilder::CheckZeroInitializable(QualType T) {
Anders Carlssond606de72009-08-23 01:25:01 +0000944 // This record already contains a member pointer.
John McCall0217dfc22011-02-15 06:40:56 +0000945 if (!IsZeroInitializableAsBase)
Anders Carlssond606de72009-08-23 01:25:01 +0000946 return;
Mike Stump11289f42009-09-09 15:08:12 +0000947
Anders Carlssond606de72009-08-23 01:25:01 +0000948 // Can only have member pointers if we're compiling C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000949 if (!Types.getContext().getLangOpts().CPlusPlus)
Anders Carlssond606de72009-08-23 01:25:01 +0000950 return;
Mike Stump11289f42009-09-09 15:08:12 +0000951
John McCall0217dfc22011-02-15 06:40:56 +0000952 const Type *elementType = T->getBaseElementTypeUnsafe();
Mike Stump11289f42009-09-09 15:08:12 +0000953
John McCall0217dfc22011-02-15 06:40:56 +0000954 if (const MemberPointerType *MPT = elementType->getAs<MemberPointerType>()) {
John McCall614dbdc2010-08-22 21:01:12 +0000955 if (!Types.getCXXABI().isZeroInitializable(MPT))
John McCall0217dfc22011-02-15 06:40:56 +0000956 IsZeroInitializable = IsZeroInitializableAsBase = false;
957 } else if (const RecordType *RT = elementType->getAs<RecordType>()) {
Anders Carlssone8bfe412010-02-02 05:17:25 +0000958 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
John McCall0217dfc22011-02-15 06:40:56 +0000959 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
960 if (!Layout.isZeroInitializable())
961 IsZeroInitializable = IsZeroInitializableAsBase = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000962 }
Anders Carlssond606de72009-08-23 01:25:01 +0000963}
964
Chris Lattnera5f58b02011-07-09 17:41:47 +0000965CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D,
966 llvm::StructType *Ty) {
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000967 CGRecordLayoutBuilder Builder(*this);
Mike Stump11289f42009-09-09 15:08:12 +0000968
Anders Carlsson307846f2009-07-23 03:17:50 +0000969 Builder.Layout(D);
Anders Carlssone1d5ca52009-07-24 15:20:52 +0000970
Chris Lattnera5f58b02011-07-09 17:41:47 +0000971 Ty->setBody(Builder.FieldTypes, Builder.Packed);
Mike Stump11289f42009-09-09 15:08:12 +0000972
John McCall0217dfc22011-02-15 06:40:56 +0000973 // If we're in C++, compute the base subobject type.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000974 llvm::StructType *BaseTy = 0;
Eli Friedman09d272d2012-01-13 03:58:31 +0000975 if (isa<CXXRecordDecl>(D) && !D->isUnion()) {
John McCall0217dfc22011-02-15 06:40:56 +0000976 BaseTy = Builder.BaseSubobjectType;
977 if (!BaseTy) BaseTy = Ty;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000978 }
979
Daniel Dunbar034299e2010-03-31 01:09:11 +0000980 CGRecordLayout *RL =
John McCall0217dfc22011-02-15 06:40:56 +0000981 new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable,
982 Builder.IsZeroInitializableAsBase);
Daniel Dunbar034299e2010-03-31 01:09:11 +0000983
John McCall0217dfc22011-02-15 06:40:56 +0000984 RL->NonVirtualBases.swap(Builder.NonVirtualBases);
985 RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases);
Anders Carlsson061ca522010-05-18 05:22:06 +0000986
Anders Carlsson307846f2009-07-23 03:17:50 +0000987 // Add all the field numbers.
John McCall0217dfc22011-02-15 06:40:56 +0000988 RL->FieldInfo.swap(Builder.Fields);
Anders Carlsson307846f2009-07-23 03:17:50 +0000989
990 // Add bitfield info.
John McCall0217dfc22011-02-15 06:40:56 +0000991 RL->BitFields.swap(Builder.BitFields);
Mike Stump11289f42009-09-09 15:08:12 +0000992
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000993 // Dump the layout, if requested.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000994 if (getContext().getLangOpts().DumpRecordLayouts) {
Daniel Dunbarccabe482010-04-19 20:44:53 +0000995 llvm::errs() << "\n*** Dumping IRgen Record Layout\n";
Daniel Dunbarb935b932010-04-13 20:58:55 +0000996 llvm::errs() << "Record: ";
997 D->dump();
998 llvm::errs() << "\nLayout: ";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000999 RL->dump();
Daniel Dunbarb935b932010-04-13 20:58:55 +00001000 }
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001001
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001002#ifndef NDEBUG
Daniel Dunbar2ea51832010-04-19 20:44:47 +00001003 // Verify that the computed LLVM struct size matches the AST layout size.
Anders Carlssonc1351ca2010-11-09 05:25:47 +00001004 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
1005
Ken Dyckb0fcc592011-02-11 01:54:29 +00001006 uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize());
Micah Villmowdd31ca12012-10-08 16:25:52 +00001007 assert(TypeSizeInBits == getDataLayout().getTypeAllocSizeInBits(Ty) &&
Daniel Dunbar2ea51832010-04-19 20:44:47 +00001008 "Type size mismatch!");
1009
Anders Carlssonc1351ca2010-11-09 05:25:47 +00001010 if (BaseTy) {
Ken Dyckbec02852011-02-08 02:02:47 +00001011 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
1012 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
1013 CharUnits AlignedNonVirtualTypeSize =
1014 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
1015
1016 uint64_t AlignedNonVirtualTypeSizeInBits =
Ken Dyckb0fcc592011-02-11 01:54:29 +00001017 getContext().toBits(AlignedNonVirtualTypeSize);
Anders Carlssonc1351ca2010-11-09 05:25:47 +00001018
1019 assert(AlignedNonVirtualTypeSizeInBits ==
Micah Villmowdd31ca12012-10-08 16:25:52 +00001020 getDataLayout().getTypeAllocSizeInBits(BaseTy) &&
Anders Carlssonc1351ca2010-11-09 05:25:47 +00001021 "Type size mismatch!");
1022 }
1023
Daniel Dunbar2ba67442010-04-21 19:10:49 +00001024 // Verify that the LLVM and AST field offsets agree.
Chris Lattner2192fe52011-07-18 04:24:23 +00001025 llvm::StructType *ST =
Daniel Dunbar2ba67442010-04-21 19:10:49 +00001026 dyn_cast<llvm::StructType>(RL->getLLVMType());
Micah Villmowdd31ca12012-10-08 16:25:52 +00001027 const llvm::StructLayout *SL = getDataLayout().getStructLayout(ST);
Daniel Dunbar2ba67442010-04-21 19:10:49 +00001028
1029 const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
1030 RecordDecl::field_iterator it = D->field_begin();
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001031 const FieldDecl *LastFD = 0;
Eli Friedman9ee2d0472012-10-12 23:29:20 +00001032 bool IsMsStruct = D->isMsStruct(getContext());
Daniel Dunbar2ba67442010-04-21 19:10:49 +00001033 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
David Blaikie40ed2972012-06-06 20:45:41 +00001034 const FieldDecl *FD = *it;
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001035
1036 // For non-bit-fields, just check that the LLVM struct offset matches the
1037 // AST offset.
1038 if (!FD->isBitField()) {
Daniel Dunbar2ba67442010-04-21 19:10:49 +00001039 unsigned FieldNo = RL->getLLVMFieldNo(FD);
1040 assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
1041 "Invalid field offset!");
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001042 LastFD = FD;
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001043 continue;
1044 }
1045
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001046 if (IsMsStruct) {
1047 // Zero-length bitfields following non-bitfield members are
1048 // ignored:
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001049 if (getContext().ZeroBitfieldFollowsNonBitfield(FD, LastFD)) {
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001050 --i;
1051 continue;
1052 }
1053 LastFD = FD;
1054 }
1055
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001056 // Ignore unnamed bit-fields.
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001057 if (!FD->getDeclName()) {
1058 LastFD = FD;
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001059 continue;
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001060 }
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001061
Chandler Carruthff0e3a12012-12-06 11:14:44 +00001062 // Don't inspect zero-length bitfields.
1063 if (FD->getBitWidthValue(getContext()) == 0)
1064 continue;
1065
Chandler Carruthff0e3a12012-12-06 11:14:44 +00001066 const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
Chandler Carruthed72cdc2012-12-09 10:33:27 +00001067 llvm::Type *ElementTy = ST->getTypeAtIndex(RL->getLLVMFieldNo(FD));
1068
Chandler Carruthff0e3a12012-12-06 11:14:44 +00001069 // Unions have overlapping elements dictating their layout, but for
1070 // non-unions we can verify that this section of the layout is the exact
Chandler Carruthed72cdc2012-12-09 10:33:27 +00001071 // expected size.
Chandler Carruthff0e3a12012-12-06 11:14:44 +00001072 if (D->isUnion()) {
Chandler Carruthed72cdc2012-12-09 10:33:27 +00001073 // For unions we verify that the start is zero and the size
1074 // is in-bounds. However, on BE systems, the offset may be non-zero, but
1075 // the size + offset should match the storage size in that case as it
1076 // "starts" at the back.
1077 if (getDataLayout().isBigEndian())
1078 assert((Info.Offset + Info.Size) == Info.StorageSize &&
1079 "Big endian union bitfield does not end at the back");
1080 else
1081 assert(Info.Offset == 0 &&
1082 "Little endian union bitfield with a non-zero offset");
Chandler Carruthff0e3a12012-12-06 11:14:44 +00001083 assert(Info.StorageSize <= SL->getSizeInBits() &&
1084 "Union not large enough for bitfield storage");
1085 } else {
1086 assert(Info.StorageSize ==
1087 getDataLayout().getTypeAllocSizeInBits(ElementTy) &&
1088 "Storage size does not match the element type size");
Daniel Dunbar2ba67442010-04-21 19:10:49 +00001089 }
Chandler Carruthff0e3a12012-12-06 11:14:44 +00001090 assert(Info.Size > 0 && "Empty bitfield!");
1091 assert(Info.Offset + Info.Size <= Info.StorageSize &&
1092 "Bitfield outside of its allocated storage");
Daniel Dunbar2ba67442010-04-21 19:10:49 +00001093 }
1094#endif
Daniel Dunbar2ea51832010-04-19 20:44:47 +00001095
Daniel Dunbar034299e2010-03-31 01:09:11 +00001096 return RL;
Anders Carlsson307846f2009-07-23 03:17:50 +00001097}
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001098
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001099void CGRecordLayout::print(raw_ostream &OS) const {
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001100 OS << "<CGRecordLayout\n";
John McCall0217dfc22011-02-15 06:40:56 +00001101 OS << " LLVMType:" << *CompleteObjectType << "\n";
1102 if (BaseSubobjectType)
1103 OS << " NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n";
John McCall614dbdc2010-08-22 21:01:12 +00001104 OS << " IsZeroInitializable:" << IsZeroInitializable << "\n";
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001105 OS << " BitFields:[\n";
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001106
1107 // Print bit-field infos in declaration order.
1108 std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001109 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
1110 it = BitFields.begin(), ie = BitFields.end();
1111 it != ie; ++it) {
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001112 const RecordDecl *RD = it->first->getParent();
1113 unsigned Index = 0;
1114 for (RecordDecl::field_iterator
David Blaikie40ed2972012-06-06 20:45:41 +00001115 it2 = RD->field_begin(); *it2 != it->first; ++it2)
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001116 ++Index;
1117 BFIs.push_back(std::make_pair(Index, &it->second));
1118 }
1119 llvm::array_pod_sort(BFIs.begin(), BFIs.end());
1120 for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
Daniel Dunbarb935b932010-04-13 20:58:55 +00001121 OS.indent(4);
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001122 BFIs[i].second->print(OS);
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001123 OS << "\n";
1124 }
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001125
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001126 OS << "]>\n";
1127}
1128
1129void CGRecordLayout::dump() const {
1130 print(llvm::errs());
1131}
1132
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001133void CGBitFieldInfo::print(raw_ostream &OS) const {
Chandler Carruthff0e3a12012-12-06 11:14:44 +00001134 OS << "<CGBitFieldInfo"
1135 << " Offset:" << Offset
1136 << " Size:" << Size
1137 << " IsSigned:" << IsSigned
1138 << " StorageSize:" << StorageSize
1139 << " StorageAlignment:" << StorageAlignment << ">";
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001140}
1141
1142void CGBitFieldInfo::dump() const {
1143 print(llvm::errs());
1144}