blob: 1193e974e19504ea72a861696e895c445a07d93d [file] [log] [blame]
Daniel Dunbar23ee4b72010-03-31 00:11:27 +00001//===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder ----*- C++ -*-===//
Anders Carlsson307846f2009-07-23 03:17:50 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000010// Builder implementation for CGRecordLayout objects.
Anders Carlsson307846f2009-07-23 03:17:50 +000011//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000014#include "CGRecordLayout.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/Attr.h"
Anders Carlsson4131f002010-11-24 22:50:27 +000017#include "clang/AST/CXXInheritance.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000018#include "clang/AST/DeclCXX.h"
19#include "clang/AST/Expr.h"
20#include "clang/AST/RecordLayout.h"
Daniel Dunbard3f3d932011-06-21 18:54:46 +000021#include "clang/Frontend/CodeGenOptions.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000022#include "CodeGenTypes.h"
John McCall614dbdc2010-08-22 21:01:12 +000023#include "CGCXXABI.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000024#include "llvm/DerivedTypes.h"
Daniel Dunbarb97bff92010-04-12 18:14:18 +000025#include "llvm/Type.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"
Anders Carlsson307846f2009-07-23 03:17:50 +000028#include "llvm/Target/TargetData.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000029using namespace clang;
30using namespace CodeGen;
31
John McCallbcd38212010-11-30 23:17:27 +000032namespace {
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000033
34class CGRecordLayoutBuilder {
35public:
36 /// FieldTypes - Holds the LLVM types that the struct is created from.
John McCall0217dfc22011-02-15 06:40:56 +000037 ///
Chris Lattner0e62c1c2011-07-23 10:55:15 +000038 SmallVector<llvm::Type *, 16> FieldTypes;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000039
John McCall0217dfc22011-02-15 06:40:56 +000040 /// BaseSubobjectType - Holds the LLVM type for the non-virtual part
Anders Carlssonc1351ca2010-11-09 05:25:47 +000041 /// of the struct. For example, consider:
42 ///
43 /// struct A { int i; };
44 /// struct B { void *v; };
45 /// struct C : virtual A, B { };
46 ///
47 /// The LLVM type of C will be
48 /// %struct.C = type { i32 (...)**, %struct.A, i32, %struct.B }
49 ///
50 /// And the LLVM type of the non-virtual base struct will be
51 /// %struct.C.base = type { i32 (...)**, %struct.A, i32 }
John McCall0217dfc22011-02-15 06:40:56 +000052 ///
53 /// This only gets initialized if the base subobject type is
54 /// different from the complete-object type.
Chris Lattnera5f58b02011-07-09 17:41:47 +000055 llvm::StructType *BaseSubobjectType;
Anders Carlssonc1351ca2010-11-09 05:25:47 +000056
John McCall0217dfc22011-02-15 06:40:56 +000057 /// FieldInfo - Holds a field and its corresponding LLVM field number.
58 llvm::DenseMap<const FieldDecl *, unsigned> Fields;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000059
John McCall0217dfc22011-02-15 06:40:56 +000060 /// BitFieldInfo - Holds location and size information about a bit field.
61 llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000062
John McCall0217dfc22011-02-15 06:40:56 +000063 llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases;
64 llvm::DenseMap<const CXXRecordDecl *, unsigned> VirtualBases;
Anders Carlsson4131f002010-11-24 22:50:27 +000065
66 /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are
67 /// primary base classes for some other direct or indirect base class.
68 CXXIndirectPrimaryBaseSet IndirectPrimaryBases;
69
Anders Carlssona459adb2010-11-28 19:18:44 +000070 /// LaidOutVirtualBases - A set of all laid out virtual bases, used to avoid
71 /// avoid laying out virtual bases more than once.
72 llvm::SmallPtrSet<const CXXRecordDecl *, 4> LaidOutVirtualBases;
73
John McCall614dbdc2010-08-22 21:01:12 +000074 /// IsZeroInitializable - Whether this struct can be C++
75 /// zero-initialized with an LLVM zeroinitializer.
76 bool IsZeroInitializable;
John McCall0217dfc22011-02-15 06:40:56 +000077 bool IsZeroInitializableAsBase;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000078
79 /// Packed - Whether the resulting LLVM struct will be packed or not.
80 bool Packed;
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +000081
82 /// IsMsStruct - Whether ms_struct is in effect or not
83 bool IsMsStruct;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000084
85private:
86 CodeGenTypes &Types;
87
Anders Carlssonfcaaa692011-04-17 21:56:13 +000088 /// LastLaidOutBaseInfo - Contains the offset and non-virtual size of the
89 /// last base laid out. Used so that we can replace the last laid out base
90 /// type with an i8 array if needed.
91 struct LastLaidOutBaseInfo {
92 CharUnits Offset;
93 CharUnits NonVirtualSize;
94
95 bool isValid() const { return !NonVirtualSize.isZero(); }
96 void invalidate() { NonVirtualSize = CharUnits::Zero(); }
97
98 } LastLaidOutBase;
99
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000100 /// Alignment - Contains the alignment of the RecordDecl.
John McCall4d9f1422011-02-15 22:21:29 +0000101 CharUnits Alignment;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000102
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000103 /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field,
104 /// this will have the number of bits still available in the field.
105 char BitsAvailableInLastField;
106
John McCall4d9f1422011-02-15 22:21:29 +0000107 /// NextFieldOffset - Holds the next field offset.
108 CharUnits NextFieldOffset;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000109
Anders Carlsson1de2f572010-04-17 20:49:27 +0000110 /// LayoutUnionField - Will layout a field in an union and return the type
111 /// that the field will have.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000112 llvm::Type *LayoutUnionField(const FieldDecl *Field,
113 const ASTRecordLayout &Layout);
Anders Carlsson1de2f572010-04-17 20:49:27 +0000114
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000115 /// LayoutUnion - Will layout a union RecordDecl.
116 void LayoutUnion(const RecordDecl *D);
117
118 /// LayoutField - try to layout all fields in the record decl.
119 /// Returns false if the operation failed because the struct is not packed.
120 bool LayoutFields(const RecordDecl *D);
121
Anders Carlssona518b2a2010-12-04 23:59:48 +0000122 /// Layout a single base, virtual or non-virtual
Eli Friedman3c840aa2011-12-12 23:13:20 +0000123 bool LayoutBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000124 const CGRecordLayout &baseLayout,
125 CharUnits baseOffset);
Anders Carlssona518b2a2010-12-04 23:59:48 +0000126
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000127 /// LayoutVirtualBase - layout a single virtual base.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000128 bool LayoutVirtualBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000129 CharUnits baseOffset);
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000130
Anders Carlssona459adb2010-11-28 19:18:44 +0000131 /// LayoutVirtualBases - layout the virtual bases of a record decl.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000132 bool LayoutVirtualBases(const CXXRecordDecl *RD,
Anders Carlssona459adb2010-11-28 19:18:44 +0000133 const ASTRecordLayout &Layout);
John McCall0153cd32011-11-08 04:01:03 +0000134
135 /// MSLayoutVirtualBases - layout the virtual bases of a record decl,
136 /// like MSVC.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000137 bool MSLayoutVirtualBases(const CXXRecordDecl *RD,
John McCall0153cd32011-11-08 04:01:03 +0000138 const ASTRecordLayout &Layout);
Anders Carlssona459adb2010-11-28 19:18:44 +0000139
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000140 /// LayoutNonVirtualBase - layout a single non-virtual base.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000141 bool LayoutNonVirtualBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000142 CharUnits baseOffset);
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000143
Anders Carlssona459adb2010-11-28 19:18:44 +0000144 /// LayoutNonVirtualBases - layout the virtual bases of a record decl.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000145 bool LayoutNonVirtualBases(const CXXRecordDecl *RD,
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000146 const ASTRecordLayout &Layout);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000147
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000148 /// ComputeNonVirtualBaseType - Compute the non-virtual base field types.
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000149 bool ComputeNonVirtualBaseType(const CXXRecordDecl *RD);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000150
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000151 /// LayoutField - layout a single field. Returns false if the operation failed
152 /// because the current struct is not packed.
153 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
154
155 /// LayoutBitField - layout a single bit field.
156 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
157
158 /// AppendField - Appends a field with the given offset and type.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000159 void AppendField(CharUnits fieldOffset, llvm::Type *FieldTy);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000160
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000161 /// AppendPadding - Appends enough padding bytes so that the total
162 /// struct size is a multiple of the field alignment.
John McCall4d9f1422011-02-15 22:21:29 +0000163 void AppendPadding(CharUnits fieldOffset, CharUnits fieldAlignment);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000164
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000165 /// ResizeLastBaseFieldIfNecessary - Fields and bases can be laid out in the
166 /// tail padding of a previous base. If this happens, the type of the previous
167 /// base needs to be changed to an array of i8. Returns true if the last
168 /// laid out base was resized.
169 bool ResizeLastBaseFieldIfNecessary(CharUnits offset);
170
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000171 /// getByteArrayType - Returns a byte array type with the given number of
172 /// elements.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000173 llvm::Type *getByteArrayType(CharUnits NumBytes);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000174
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000175 /// AppendBytes - Append a given number of bytes to the record.
John McCall4d9f1422011-02-15 22:21:29 +0000176 void AppendBytes(CharUnits numBytes);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000177
178 /// AppendTailPadding - Append enough tail padding so that the type will have
179 /// the passed size.
Ken Dyck272b6fa2011-04-24 16:53:44 +0000180 void AppendTailPadding(CharUnits RecordSize);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000181
Chris Lattner2192fe52011-07-18 04:24:23 +0000182 CharUnits getTypeAlignment(llvm::Type *Ty) const;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000183
Anders Carlssonacf877b2010-11-28 23:06:23 +0000184 /// getAlignmentAsLLVMStruct - Returns the maximum alignment of all the
185 /// LLVM element types.
John McCall4d9f1422011-02-15 22:21:29 +0000186 CharUnits getAlignmentAsLLVMStruct() const;
Anders Carlssonacf877b2010-11-28 23:06:23 +0000187
John McCall614dbdc2010-08-22 21:01:12 +0000188 /// CheckZeroInitializable - Check if the given type contains a pointer
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000189 /// to data member.
John McCall614dbdc2010-08-22 21:01:12 +0000190 void CheckZeroInitializable(QualType T);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000191
192public:
193 CGRecordLayoutBuilder(CodeGenTypes &Types)
John McCall0217dfc22011-02-15 06:40:56 +0000194 : BaseSubobjectType(0),
195 IsZeroInitializable(true), IsZeroInitializableAsBase(true),
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000196 Packed(false), IsMsStruct(false),
197 Types(Types), BitsAvailableInLastField(0) { }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000198
199 /// Layout - Will layout a RecordDecl.
200 void Layout(const RecordDecl *D);
201};
202
203}
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000204
Anders Carlsson307846f2009-07-23 03:17:50 +0000205void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
John McCall4d9f1422011-02-15 22:21:29 +0000206 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment();
Anders Carlsson09a37742009-09-02 17:51:33 +0000207 Packed = D->hasAttr<PackedAttr>();
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000208
209 IsMsStruct = D->hasAttr<MsStructAttr>();
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000210
Anders Carlsson697f6592009-07-23 03:43:54 +0000211 if (D->isUnion()) {
212 LayoutUnion(D);
213 return;
214 }
Anders Carlsson68e0b682009-08-08 18:23:56 +0000215
Anders Carlsson307846f2009-07-23 03:17:50 +0000216 if (LayoutFields(D))
217 return;
Mike Stump11289f42009-09-09 15:08:12 +0000218
Anders Carlsson307846f2009-07-23 03:17:50 +0000219 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlssond78fc892009-07-23 17:24:40 +0000220 Packed = true;
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000221 LastLaidOutBase.invalidate();
John McCall4d9f1422011-02-15 22:21:29 +0000222 NextFieldOffset = CharUnits::Zero();
Anders Carlsson307846f2009-07-23 03:17:50 +0000223 FieldTypes.clear();
John McCall0217dfc22011-02-15 06:40:56 +0000224 Fields.clear();
225 BitFields.clear();
226 NonVirtualBases.clear();
227 VirtualBases.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000228
Anders Carlsson307846f2009-07-23 03:17:50 +0000229 LayoutFields(D);
230}
231
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000232CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
233 const FieldDecl *FD,
234 uint64_t FieldOffset,
235 uint64_t FieldSize,
236 uint64_t ContainingTypeSizeInBits,
237 unsigned ContainingTypeAlign) {
Chris Lattner2192fe52011-07-18 04:24:23 +0000238 llvm::Type *Ty = Types.ConvertTypeForMem(FD->getType());
John McCall8a3c5552011-02-26 08:41:59 +0000239 CharUnits TypeSizeInBytes =
240 CharUnits::fromQuantity(Types.getTargetData().getTypeAllocSize(Ty));
241 uint64_t TypeSizeInBits = Types.getContext().toBits(TypeSizeInBytes);
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000242
Douglas Gregor6ab2fa82011-05-20 16:38:50 +0000243 bool IsSigned = FD->getType()->isSignedIntegerOrEnumerationType();
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000244
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000245 if (FieldSize > TypeSizeInBits) {
Anders Carlssond5f27b02010-04-17 22:54:57 +0000246 // We have a wide bit-field. The extra bits are only used for padding, so
247 // if we have a bitfield of type T, with size N:
248 //
249 // T t : N;
250 //
251 // We can just assume that it's:
252 //
253 // T t : sizeof(T);
254 //
255 FieldSize = TypeSizeInBits;
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000256 }
257
Chris Lattnerfb59c7c2011-02-17 22:09:58 +0000258 // in big-endian machines the first fields are in higher bit positions,
259 // so revert the offset. The byte offsets are reversed(back) later.
260 if (Types.getTargetData().isBigEndian()) {
261 FieldOffset = ((ContainingTypeSizeInBits)-FieldOffset-FieldSize);
262 }
263
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000264 // Compute the access components. The policy we use is to start by attempting
265 // to access using the width of the bit-field type itself and to always access
266 // at aligned indices of that type. If such an access would fail because it
267 // extends past the bound of the type, then we reduce size to the next smaller
268 // power of two and retry. The current algorithm assumes pow2 sized types,
269 // although this is easy to fix.
270 //
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000271 assert(llvm::isPowerOf2_32(TypeSizeInBits) && "Unexpected type size!");
272 CGBitFieldInfo::AccessInfo Components[3];
273 unsigned NumComponents = 0;
Nick Lewyckyd2348d82011-03-22 17:35:47 +0000274 unsigned AccessedTargetBits = 0; // The number of target bits accessed.
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000275 unsigned AccessWidth = TypeSizeInBits; // The current access width to attempt.
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000276
Daniel Dunbard3f3d932011-06-21 18:54:46 +0000277 // If requested, widen the initial bit-field access to be register sized. The
278 // theory is that this is most likely to allow multiple accesses into the same
279 // structure to be coalesced, and that the backend should be smart enough to
280 // narrow the store if no coalescing is ever done.
281 //
282 // The subsequent code will handle align these access to common boundaries and
283 // guaranteeing that we do not access past the end of the structure.
284 if (Types.getCodeGenOpts().UseRegisterSizedBitfieldAccess) {
285 if (AccessWidth < Types.getTarget().getRegisterWidth())
286 AccessWidth = Types.getTarget().getRegisterWidth();
287 }
288
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000289 // Round down from the field offset to find the first access position that is
290 // at an aligned offset of the initial access type.
Daniel Dunbar59813772010-04-22 15:22:33 +0000291 uint64_t AccessStart = FieldOffset - (FieldOffset % AccessWidth);
292
293 // Adjust initial access size to fit within record.
John McCall8a3c5552011-02-26 08:41:59 +0000294 while (AccessWidth > Types.getTarget().getCharWidth() &&
Daniel Dunbar59813772010-04-22 15:22:33 +0000295 AccessStart + AccessWidth > ContainingTypeSizeInBits) {
296 AccessWidth >>= 1;
297 AccessStart = FieldOffset - (FieldOffset % AccessWidth);
298 }
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000299
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000300 while (AccessedTargetBits < FieldSize) {
301 // Check that we can access using a type of this size, without reading off
302 // the end of the structure. This can occur with packed structures and
303 // -fno-bitfield-type-align, for example.
304 if (AccessStart + AccessWidth > ContainingTypeSizeInBits) {
305 // If so, reduce access size to the next smaller power-of-two and retry.
306 AccessWidth >>= 1;
John McCall8a3c5552011-02-26 08:41:59 +0000307 assert(AccessWidth >= Types.getTarget().getCharWidth()
308 && "Cannot access under byte size!");
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000309 continue;
310 }
Daniel Dunbarb935b932010-04-13 20:58:55 +0000311
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000312 // Otherwise, add an access component.
Daniel Dunbarb935b932010-04-13 20:58:55 +0000313
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000314 // First, compute the bits inside this access which are part of the
315 // target. We are reading bits [AccessStart, AccessStart + AccessWidth); the
316 // intersection with [FieldOffset, FieldOffset + FieldSize) gives the bits
317 // in the target that we are reading.
Daniel Dunbar59813772010-04-22 15:22:33 +0000318 assert(FieldOffset < AccessStart + AccessWidth && "Invalid access start!");
319 assert(AccessStart < FieldOffset + FieldSize && "Invalid access start!");
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000320 uint64_t AccessBitsInFieldStart = std::max(AccessStart, FieldOffset);
321 uint64_t AccessBitsInFieldSize =
Daniel Dunbar59813772010-04-22 15:22:33 +0000322 std::min(AccessWidth + AccessStart,
323 FieldOffset + FieldSize) - AccessBitsInFieldStart;
Daniel Dunbar5d6c07e2010-04-22 14:56:10 +0000324
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000325 assert(NumComponents < 3 && "Unexpected number of components!");
326 CGBitFieldInfo::AccessInfo &AI = Components[NumComponents++];
327 AI.FieldIndex = 0;
328 // FIXME: We still follow the old access pattern of only using the field
329 // byte offset. We should switch this once we fix the struct layout to be
330 // pretty.
Chris Lattnerfb59c7c2011-02-17 22:09:58 +0000331
332 // on big-endian machines we reverted the bit offset because first fields are
333 // in higher bits. But this also reverts the bytes, so fix this here by reverting
334 // the byte offset on big-endian machines.
335 if (Types.getTargetData().isBigEndian()) {
Ken Dyckf76759c2011-04-24 10:04:59 +0000336 AI.FieldByteOffset = Types.getContext().toCharUnitsFromBits(
337 ContainingTypeSizeInBits - AccessStart - AccessWidth);
Chris Lattnerfb59c7c2011-02-17 22:09:58 +0000338 } else {
Ken Dyckf76759c2011-04-24 10:04:59 +0000339 AI.FieldByteOffset = Types.getContext().toCharUnitsFromBits(AccessStart);
Chris Lattnerfb59c7c2011-02-17 22:09:58 +0000340 }
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000341 AI.FieldBitStart = AccessBitsInFieldStart - AccessStart;
342 AI.AccessWidth = AccessWidth;
Ken Dyck27337a82011-04-24 10:13:17 +0000343 AI.AccessAlignment = Types.getContext().toCharUnitsFromBits(
344 llvm::MinAlign(ContainingTypeAlign, AccessStart));
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000345 AI.TargetBitOffset = AccessedTargetBits;
346 AI.TargetBitWidth = AccessBitsInFieldSize;
347
348 AccessStart += AccessWidth;
349 AccessedTargetBits += AI.TargetBitWidth;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000350 }
351
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000352 assert(AccessedTargetBits == FieldSize && "Invalid bit-field access!");
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000353 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000354}
355
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000356CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
357 const FieldDecl *FD,
358 uint64_t FieldOffset,
359 uint64_t FieldSize) {
360 const RecordDecl *RD = FD->getParent();
361 const ASTRecordLayout &RL = Types.getContext().getASTRecordLayout(RD);
Ken Dyckb0fcc592011-02-11 01:54:29 +0000362 uint64_t ContainingTypeSizeInBits = Types.getContext().toBits(RL.getSize());
Ken Dyck7ad11e72011-02-15 02:32:40 +0000363 unsigned ContainingTypeAlign = Types.getContext().toBits(RL.getAlignment());
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000364
365 return MakeInfo(Types, FD, FieldOffset, FieldSize, ContainingTypeSizeInBits,
366 ContainingTypeAlign);
367}
368
Anders Carlsson307846f2009-07-23 03:17:50 +0000369void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
John McCall4d9f1422011-02-15 22:21:29 +0000370 uint64_t fieldOffset) {
Richard Smithcaf33902011-10-10 18:28:20 +0000371 uint64_t fieldSize = D->getBitWidthValue(Types.getContext());
Mike Stump11289f42009-09-09 15:08:12 +0000372
John McCall4d9f1422011-02-15 22:21:29 +0000373 if (fieldSize == 0)
Anders Carlsson307846f2009-07-23 03:17:50 +0000374 return;
375
John McCall8a3c5552011-02-26 08:41:59 +0000376 uint64_t nextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset);
Ken Dyck345a6de2011-04-24 16:40:29 +0000377 CharUnits numBytesToAppend;
Douglas Gregore8bbc122011-09-02 00:18:52 +0000378 unsigned charAlign = Types.getContext().getTargetInfo().getCharAlign();
Mike Stump11289f42009-09-09 15:08:12 +0000379
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000380 if (fieldOffset < nextFieldOffsetInBits && !BitsAvailableInLastField) {
Ken Dyck345a6de2011-04-24 16:40:29 +0000381 assert(fieldOffset % charAlign == 0 &&
382 "Field offset not aligned correctly");
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000383
384 CharUnits fieldOffsetInCharUnits =
385 Types.getContext().toCharUnitsFromBits(fieldOffset);
386
387 // Try to resize the last base field.
388 if (ResizeLastBaseFieldIfNecessary(fieldOffsetInCharUnits))
389 nextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset);
390 }
391
John McCall4d9f1422011-02-15 22:21:29 +0000392 if (fieldOffset < nextFieldOffsetInBits) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000393 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
John McCall4d9f1422011-02-15 22:21:29 +0000394 assert(!NextFieldOffset.isZero() && "Must have laid out at least one byte");
Mike Stump11289f42009-09-09 15:08:12 +0000395
Anders Carlsson307846f2009-07-23 03:17:50 +0000396 // The bitfield begins in the previous bit-field.
Ken Dyck345a6de2011-04-24 16:40:29 +0000397 numBytesToAppend = Types.getContext().toCharUnitsFromBits(
398 llvm::RoundUpToAlignment(fieldSize - BitsAvailableInLastField,
399 charAlign));
Anders Carlsson307846f2009-07-23 03:17:50 +0000400 } else {
Ken Dyck345a6de2011-04-24 16:40:29 +0000401 assert(fieldOffset % charAlign == 0 &&
402 "Field offset not aligned correctly");
Anders Carlsson307846f2009-07-23 03:17:50 +0000403
404 // Append padding if necessary.
Ken Dyck345a6de2011-04-24 16:40:29 +0000405 AppendPadding(Types.getContext().toCharUnitsFromBits(fieldOffset),
406 CharUnits::One());
Mike Stump11289f42009-09-09 15:08:12 +0000407
Ken Dyck345a6de2011-04-24 16:40:29 +0000408 numBytesToAppend = Types.getContext().toCharUnitsFromBits(
409 llvm::RoundUpToAlignment(fieldSize, charAlign));
Mike Stump11289f42009-09-09 15:08:12 +0000410
Ken Dyck345a6de2011-04-24 16:40:29 +0000411 assert(!numBytesToAppend.isZero() && "No bytes to append!");
Anders Carlsson307846f2009-07-23 03:17:50 +0000412 }
413
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000414 // Add the bit field info.
John McCall0217dfc22011-02-15 06:40:56 +0000415 BitFields.insert(std::make_pair(D,
John McCall4d9f1422011-02-15 22:21:29 +0000416 CGBitFieldInfo::MakeInfo(Types, D, fieldOffset, fieldSize)));
Mike Stump11289f42009-09-09 15:08:12 +0000417
Ken Dyck345a6de2011-04-24 16:40:29 +0000418 AppendBytes(numBytesToAppend);
Mike Stump11289f42009-09-09 15:08:12 +0000419
Mike Stump11289f42009-09-09 15:08:12 +0000420 BitsAvailableInLastField =
Ken Dyck345a6de2011-04-24 16:40:29 +0000421 Types.getContext().toBits(NextFieldOffset) - (fieldOffset + fieldSize);
Anders Carlsson307846f2009-07-23 03:17:50 +0000422}
423
424bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
John McCall4d9f1422011-02-15 22:21:29 +0000425 uint64_t fieldOffset) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000426 // If the field is packed, then we need a packed struct.
Anders Carlsson68e0b682009-08-08 18:23:56 +0000427 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson307846f2009-07-23 03:17:50 +0000428 return false;
429
430 if (D->isBitField()) {
431 // We must use packed structs for unnamed bit fields since they
432 // don't affect the struct alignment.
Anders Carlssond78fc892009-07-23 17:24:40 +0000433 if (!Packed && !D->getDeclName())
Anders Carlsson307846f2009-07-23 03:17:50 +0000434 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000435
John McCall4d9f1422011-02-15 22:21:29 +0000436 LayoutBitField(D, fieldOffset);
Anders Carlsson307846f2009-07-23 03:17:50 +0000437 return true;
438 }
Mike Stump11289f42009-09-09 15:08:12 +0000439
John McCall614dbdc2010-08-22 21:01:12 +0000440 CheckZeroInitializable(D->getType());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000441
John McCall8a3c5552011-02-26 08:41:59 +0000442 assert(fieldOffset % Types.getTarget().getCharWidth() == 0
443 && "field offset is not on a byte boundary!");
444 CharUnits fieldOffsetInBytes
445 = Types.getContext().toCharUnitsFromBits(fieldOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000446
Chris Lattnera5f58b02011-07-09 17:41:47 +0000447 llvm::Type *Ty = Types.ConvertTypeForMem(D->getType());
John McCall4d9f1422011-02-15 22:21:29 +0000448 CharUnits typeAlignment = getTypeAlignment(Ty);
Anders Carlsson19702bb2009-08-04 16:29:15 +0000449
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000450 // If the type alignment is larger then the struct alignment, we must use
451 // a packed struct.
John McCall4d9f1422011-02-15 22:21:29 +0000452 if (typeAlignment > Alignment) {
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000453 assert(!Packed && "Alignment is wrong even with packed struct!");
454 return false;
455 }
Mike Stump11289f42009-09-09 15:08:12 +0000456
John McCall4d9f1422011-02-15 22:21:29 +0000457 if (!Packed) {
458 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
459 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
460 if (const MaxFieldAlignmentAttr *MFAA =
461 RD->getAttr<MaxFieldAlignmentAttr>()) {
John McCall8a3c5552011-02-26 08:41:59 +0000462 if (MFAA->getAlignment() != Types.getContext().toBits(typeAlignment))
John McCall4d9f1422011-02-15 22:21:29 +0000463 return false;
464 }
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000465 }
466 }
467
Anders Carlsson19702bb2009-08-04 16:29:15 +0000468 // Round up the field offset to the alignment of the field type.
John McCall4d9f1422011-02-15 22:21:29 +0000469 CharUnits alignedNextFieldOffsetInBytes =
470 NextFieldOffset.RoundUpToAlignment(typeAlignment);
Anders Carlsson19702bb2009-08-04 16:29:15 +0000471
John McCall4d9f1422011-02-15 22:21:29 +0000472 if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000473 // Try to resize the last base field.
474 if (ResizeLastBaseFieldIfNecessary(fieldOffsetInBytes)) {
475 alignedNextFieldOffsetInBytes =
476 NextFieldOffset.RoundUpToAlignment(typeAlignment);
477 }
478 }
479
480 if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
Anders Carlsson19702bb2009-08-04 16:29:15 +0000481 assert(!Packed && "Could not place field even with packed struct!");
482 return false;
483 }
Mike Stump11289f42009-09-09 15:08:12 +0000484
John McCall4d9f1422011-02-15 22:21:29 +0000485 AppendPadding(fieldOffsetInBytes, typeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +0000486
Anders Carlsson307846f2009-07-23 03:17:50 +0000487 // Now append the field.
John McCall0217dfc22011-02-15 06:40:56 +0000488 Fields[D] = FieldTypes.size();
John McCall4d9f1422011-02-15 22:21:29 +0000489 AppendField(fieldOffsetInBytes, Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000490
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000491 LastLaidOutBase.invalidate();
Anders Carlsson307846f2009-07-23 03:17:50 +0000492 return true;
493}
494
Chris Lattnera5f58b02011-07-09 17:41:47 +0000495llvm::Type *
Anders Carlsson1de2f572010-04-17 20:49:27 +0000496CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
497 const ASTRecordLayout &Layout) {
498 if (Field->isBitField()) {
Richard Smithcaf33902011-10-10 18:28:20 +0000499 uint64_t FieldSize = Field->getBitWidthValue(Types.getContext());
Anders Carlsson1de2f572010-04-17 20:49:27 +0000500
501 // Ignore zero sized bit fields.
502 if (FieldSize == 0)
503 return 0;
504
Chris Lattnera5f58b02011-07-09 17:41:47 +0000505 llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
Ken Dyck7a0b19f2011-04-24 16:47:33 +0000506 CharUnits NumBytesToAppend = Types.getContext().toCharUnitsFromBits(
507 llvm::RoundUpToAlignment(FieldSize,
Douglas Gregore8bbc122011-09-02 00:18:52 +0000508 Types.getContext().getTargetInfo().getCharAlign()));
Anders Carlsson2295f132010-04-17 21:04:52 +0000509
Ken Dyck7a0b19f2011-04-24 16:47:33 +0000510 if (NumBytesToAppend > CharUnits::One())
511 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend.getQuantity());
Anders Carlsson2295f132010-04-17 21:04:52 +0000512
Anders Carlsson1de2f572010-04-17 20:49:27 +0000513 // Add the bit field info.
John McCall0217dfc22011-02-15 06:40:56 +0000514 BitFields.insert(std::make_pair(Field,
515 CGBitFieldInfo::MakeInfo(Types, Field, 0, FieldSize)));
Anders Carlsson2295f132010-04-17 21:04:52 +0000516 return FieldTy;
Anders Carlsson1de2f572010-04-17 20:49:27 +0000517 }
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000518
Anders Carlsson1de2f572010-04-17 20:49:27 +0000519 // This is a regular union field.
John McCall0217dfc22011-02-15 06:40:56 +0000520 Fields[Field] = 0;
Chris Lattnera5f58b02011-07-09 17:41:47 +0000521 return Types.ConvertTypeForMem(Field->getType());
Anders Carlsson1de2f572010-04-17 20:49:27 +0000522}
523
Anders Carlsson697f6592009-07-23 03:43:54 +0000524void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
525 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump11289f42009-09-09 15:08:12 +0000526
John McCall4d9f1422011-02-15 22:21:29 +0000527 const ASTRecordLayout &layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000528
Chris Lattnera5f58b02011-07-09 17:41:47 +0000529 llvm::Type *unionType = 0;
John McCall4d9f1422011-02-15 22:21:29 +0000530 CharUnits unionSize = CharUnits::Zero();
531 CharUnits unionAlign = CharUnits::Zero();
Mike Stump11289f42009-09-09 15:08:12 +0000532
John McCall4d9f1422011-02-15 22:21:29 +0000533 bool hasOnlyZeroSizedBitFields = true;
Eli Friedmandae858a2011-12-07 01:30:11 +0000534 bool checkedFirstFieldZeroInit = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000535
John McCall4d9f1422011-02-15 22:21:29 +0000536 unsigned fieldNo = 0;
537 for (RecordDecl::field_iterator field = D->field_begin(),
538 fieldEnd = D->field_end(); field != fieldEnd; ++field, ++fieldNo) {
539 assert(layout.getFieldOffset(fieldNo) == 0 &&
Anders Carlsson697f6592009-07-23 03:43:54 +0000540 "Union field offset did not start at the beginning of record!");
Chris Lattnera5f58b02011-07-09 17:41:47 +0000541 llvm::Type *fieldType = LayoutUnionField(*field, layout);
Anders Carlssonf814ee62009-07-23 04:00:39 +0000542
John McCall4d9f1422011-02-15 22:21:29 +0000543 if (!fieldType)
Anders Carlsson1de2f572010-04-17 20:49:27 +0000544 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000545
Eli Friedmandae858a2011-12-07 01:30:11 +0000546 if (field->getDeclName() && !checkedFirstFieldZeroInit) {
547 CheckZeroInitializable(field->getType());
548 checkedFirstFieldZeroInit = true;
549 }
550
John McCall4d9f1422011-02-15 22:21:29 +0000551 hasOnlyZeroSizedBitFields = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000552
John McCall4d9f1422011-02-15 22:21:29 +0000553 CharUnits fieldAlign = CharUnits::fromQuantity(
554 Types.getTargetData().getABITypeAlignment(fieldType));
555 CharUnits fieldSize = CharUnits::fromQuantity(
556 Types.getTargetData().getTypeAllocSize(fieldType));
Mike Stump11289f42009-09-09 15:08:12 +0000557
John McCall4d9f1422011-02-15 22:21:29 +0000558 if (fieldAlign < unionAlign)
Anders Carlsson697f6592009-07-23 03:43:54 +0000559 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000560
John McCall4d9f1422011-02-15 22:21:29 +0000561 if (fieldAlign > unionAlign || fieldSize > unionSize) {
562 unionType = fieldType;
563 unionAlign = fieldAlign;
564 unionSize = fieldSize;
Anders Carlsson697f6592009-07-23 03:43:54 +0000565 }
566 }
Mike Stump11289f42009-09-09 15:08:12 +0000567
Anders Carlsson697f6592009-07-23 03:43:54 +0000568 // Now add our field.
John McCall4d9f1422011-02-15 22:21:29 +0000569 if (unionType) {
570 AppendField(CharUnits::Zero(), unionType);
Anders Carlsson0e912752009-09-03 22:56:02 +0000571
John McCall4d9f1422011-02-15 22:21:29 +0000572 if (getTypeAlignment(unionType) > layout.getAlignment()) {
Anders Carlsson0e912752009-09-03 22:56:02 +0000573 // We need a packed struct.
574 Packed = true;
John McCall4d9f1422011-02-15 22:21:29 +0000575 unionAlign = CharUnits::One();
Anders Carlsson0e912752009-09-03 22:56:02 +0000576 }
577 }
John McCall4d9f1422011-02-15 22:21:29 +0000578 if (unionAlign.isZero()) {
Chandler Carruth52b6ac22012-03-04 12:16:40 +0000579 (void)hasOnlyZeroSizedBitFields;
John McCall4d9f1422011-02-15 22:21:29 +0000580 assert(hasOnlyZeroSizedBitFields &&
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000581 "0-align record did not have all zero-sized bit-fields!");
John McCall4d9f1422011-02-15 22:21:29 +0000582 unionAlign = CharUnits::One();
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000583 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000584
Anders Carlsson697f6592009-07-23 03:43:54 +0000585 // Append tail padding.
John McCall4d9f1422011-02-15 22:21:29 +0000586 CharUnits recordSize = layout.getSize();
587 if (recordSize > unionSize)
588 AppendPadding(recordSize, unionAlign);
Anders Carlsson697f6592009-07-23 03:43:54 +0000589}
590
Eli Friedman3c840aa2011-12-12 23:13:20 +0000591bool CGRecordLayoutBuilder::LayoutBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000592 const CGRecordLayout &baseLayout,
593 CharUnits baseOffset) {
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000594 ResizeLastBaseFieldIfNecessary(baseOffset);
595
John McCall4d9f1422011-02-15 22:21:29 +0000596 AppendPadding(baseOffset, CharUnits::One());
Anders Carlssona518b2a2010-12-04 23:59:48 +0000597
John McCall0217dfc22011-02-15 06:40:56 +0000598 const ASTRecordLayout &baseASTLayout
599 = Types.getContext().getASTRecordLayout(base);
600
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000601 LastLaidOutBase.Offset = NextFieldOffset;
602 LastLaidOutBase.NonVirtualSize = baseASTLayout.getNonVirtualSize();
603
Chris Lattnera5f58b02011-07-09 17:41:47 +0000604 llvm::StructType *subobjectType = baseLayout.getBaseSubobjectLLVMType();
Eli Friedman3c840aa2011-12-12 23:13:20 +0000605 if (getTypeAlignment(subobjectType) > Alignment)
606 return false;
607
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000608 AppendField(baseOffset, subobjectType);
Eli Friedman3c840aa2011-12-12 23:13:20 +0000609 return true;
John McCall0217dfc22011-02-15 06:40:56 +0000610}
611
Eli Friedman3c840aa2011-12-12 23:13:20 +0000612bool CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000613 CharUnits baseOffset) {
John McCall0217dfc22011-02-15 06:40:56 +0000614 // Ignore empty bases.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000615 if (base->isEmpty()) return true;
John McCall0217dfc22011-02-15 06:40:56 +0000616
617 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
618 if (IsZeroInitializableAsBase) {
619 assert(IsZeroInitializable &&
620 "class zero-initializable as base but not as complete object");
621
622 IsZeroInitializable = IsZeroInitializableAsBase =
623 baseLayout.isZeroInitializableAsBase();
624 }
625
Eli Friedman3c840aa2011-12-12 23:13:20 +0000626 if (!LayoutBase(base, baseLayout, baseOffset))
627 return false;
John McCall0217dfc22011-02-15 06:40:56 +0000628 NonVirtualBases[base] = (FieldTypes.size() - 1);
Eli Friedman3c840aa2011-12-12 23:13:20 +0000629 return true;
Anders Carlssona518b2a2010-12-04 23:59:48 +0000630}
631
Eli Friedman3c840aa2011-12-12 23:13:20 +0000632bool
John McCall0217dfc22011-02-15 06:40:56 +0000633CGRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000634 CharUnits baseOffset) {
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000635 // Ignore empty bases.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000636 if (base->isEmpty()) return true;
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000637
John McCall0217dfc22011-02-15 06:40:56 +0000638 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
639 if (IsZeroInitializable)
640 IsZeroInitializable = baseLayout.isZeroInitializableAsBase();
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000641
Eli Friedman3c840aa2011-12-12 23:13:20 +0000642 if (!LayoutBase(base, baseLayout, baseOffset))
643 return false;
John McCall0217dfc22011-02-15 06:40:56 +0000644 VirtualBases[base] = (FieldTypes.size() - 1);
Eli Friedman3c840aa2011-12-12 23:13:20 +0000645 return true;
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000646}
647
Eli Friedman3c840aa2011-12-12 23:13:20 +0000648bool
John McCall0153cd32011-11-08 04:01:03 +0000649CGRecordLayoutBuilder::MSLayoutVirtualBases(const CXXRecordDecl *RD,
650 const ASTRecordLayout &Layout) {
651 if (!RD->getNumVBases())
Eli Friedman3c840aa2011-12-12 23:13:20 +0000652 return true;
John McCall0153cd32011-11-08 04:01:03 +0000653
654 // The vbases list is uniqued and ordered by a depth-first
655 // traversal, which is what we need here.
656 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
657 E = RD->vbases_end(); I != E; ++I) {
658
659 const CXXRecordDecl *BaseDecl =
660 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
661
662 CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl);
Eli Friedman3c840aa2011-12-12 23:13:20 +0000663 if (!LayoutVirtualBase(BaseDecl, vbaseOffset))
664 return false;
John McCall0153cd32011-11-08 04:01:03 +0000665 }
Eli Friedman3c840aa2011-12-12 23:13:20 +0000666 return true;
John McCall0153cd32011-11-08 04:01:03 +0000667}
668
Anders Carlssona459adb2010-11-28 19:18:44 +0000669/// LayoutVirtualBases - layout the non-virtual bases of a record decl.
Eli Friedman3c840aa2011-12-12 23:13:20 +0000670bool
Anders Carlssona459adb2010-11-28 19:18:44 +0000671CGRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
672 const ASTRecordLayout &Layout) {
673 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
674 E = RD->bases_end(); I != E; ++I) {
675 const CXXRecordDecl *BaseDecl =
676 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
677
678 // We only want to lay out virtual bases that aren't indirect primary bases
679 // of some other base.
680 if (I->isVirtual() && !IndirectPrimaryBases.count(BaseDecl)) {
681 // Only lay out the base once.
682 if (!LaidOutVirtualBases.insert(BaseDecl))
683 continue;
684
John McCall4d9f1422011-02-15 22:21:29 +0000685 CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl);
Eli Friedman3c840aa2011-12-12 23:13:20 +0000686 if (!LayoutVirtualBase(BaseDecl, vbaseOffset))
687 return false;
Anders Carlssona459adb2010-11-28 19:18:44 +0000688 }
689
690 if (!BaseDecl->getNumVBases()) {
691 // This base isn't interesting since it doesn't have any virtual bases.
692 continue;
693 }
694
Eli Friedman3c840aa2011-12-12 23:13:20 +0000695 if (!LayoutVirtualBases(BaseDecl, Layout))
696 return false;
Anders Carlssona459adb2010-11-28 19:18:44 +0000697 }
Eli Friedman3c840aa2011-12-12 23:13:20 +0000698 return true;
Anders Carlssona459adb2010-11-28 19:18:44 +0000699}
700
Eli Friedman3c840aa2011-12-12 23:13:20 +0000701bool
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000702CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD,
703 const ASTRecordLayout &Layout) {
704 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
705
John McCall0153cd32011-11-08 04:01:03 +0000706 // If we have a primary base, lay it out first.
707 if (PrimaryBase) {
Eli Friedman3c840aa2011-12-12 23:13:20 +0000708 if (!Layout.isPrimaryBaseVirtual()) {
709 if (!LayoutNonVirtualBase(PrimaryBase, CharUnits::Zero()))
710 return false;
711 } else {
712 if (!LayoutVirtualBase(PrimaryBase, CharUnits::Zero()))
713 return false;
714 }
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000715
John McCall0153cd32011-11-08 04:01:03 +0000716 // Otherwise, add a vtable / vf-table if the layout says to do so.
717 } else if (Types.getContext().getTargetInfo().getCXXABI() == CXXABI_Microsoft
718 ? Layout.getVFPtrOffset() != CharUnits::fromQuantity(-1)
719 : RD->isDynamicClass()) {
720 llvm::Type *FunctionType =
721 llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()),
722 /*isVarArg=*/true);
723 llvm::Type *VTableTy = FunctionType->getPointerTo();
724
725 assert(NextFieldOffset.isZero() &&
726 "VTable pointer must come first!");
727 AppendField(CharUnits::Zero(), VTableTy->getPointerTo());
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000728 }
729
730 // Layout the non-virtual bases.
731 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
732 E = RD->bases_end(); I != E; ++I) {
733 if (I->isVirtual())
734 continue;
735
736 const CXXRecordDecl *BaseDecl =
737 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
738
739 // We've already laid out the primary base.
Anders Carlsson7f95cd12010-11-24 23:12:57 +0000740 if (BaseDecl == PrimaryBase && !Layout.isPrimaryBaseVirtual())
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000741 continue;
742
Eli Friedman3c840aa2011-12-12 23:13:20 +0000743 if (!LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffset(BaseDecl)))
744 return false;
Anders Carlssond681a292009-12-16 17:27:20 +0000745 }
John McCall0153cd32011-11-08 04:01:03 +0000746
747 // Add a vb-table pointer if the layout insists.
748 if (Layout.getVBPtrOffset() != CharUnits::fromQuantity(-1)) {
749 CharUnits VBPtrOffset = Layout.getVBPtrOffset();
750 llvm::Type *Vbptr = llvm::Type::getInt32PtrTy(Types.getLLVMContext());
751 AppendPadding(VBPtrOffset, getTypeAlignment(Vbptr));
752 AppendField(VBPtrOffset, Vbptr);
753 }
Eli Friedman3c840aa2011-12-12 23:13:20 +0000754
755 return true;
Anders Carlssond681a292009-12-16 17:27:20 +0000756}
757
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000758bool
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000759CGRecordLayoutBuilder::ComputeNonVirtualBaseType(const CXXRecordDecl *RD) {
760 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(RD);
761
Ken Dyckbec02852011-02-08 02:02:47 +0000762 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
763 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
John McCall4d9f1422011-02-15 22:21:29 +0000764 CharUnits AlignedNonVirtualTypeSize =
765 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000766
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000767 // First check if we can use the same fields as for the complete class.
John McCall4d9f1422011-02-15 22:21:29 +0000768 CharUnits RecordSize = Layout.getSize();
John McCall0217dfc22011-02-15 06:40:56 +0000769 if (AlignedNonVirtualTypeSize == RecordSize)
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000770 return true;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000771
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000772 // Check if we need padding.
John McCall4d9f1422011-02-15 22:21:29 +0000773 CharUnits AlignedNextFieldOffset =
774 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000775
John McCall0217dfc22011-02-15 06:40:56 +0000776 if (AlignedNextFieldOffset > AlignedNonVirtualTypeSize) {
777 assert(!Packed && "cannot layout even as packed struct");
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000778 return false; // Needs packing.
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000779 }
780
John McCall0217dfc22011-02-15 06:40:56 +0000781 bool needsPadding = (AlignedNonVirtualTypeSize != AlignedNextFieldOffset);
782 if (needsPadding) {
John McCall4d9f1422011-02-15 22:21:29 +0000783 CharUnits NumBytes = AlignedNonVirtualTypeSize - AlignedNextFieldOffset;
John McCall0217dfc22011-02-15 06:40:56 +0000784 FieldTypes.push_back(getByteArrayType(NumBytes));
785 }
Chris Lattnera5f58b02011-07-09 17:41:47 +0000786
Chris Lattner5ec04a52011-08-12 17:43:31 +0000787 BaseSubobjectType = llvm::StructType::create(Types.getLLVMContext(),
788 FieldTypes, "", Packed);
Chris Lattnera5f58b02011-07-09 17:41:47 +0000789 Types.addRecordTypeName(RD, BaseSubobjectType, ".base");
John McCall0217dfc22011-02-15 06:40:56 +0000790
Chris Lattnera5f58b02011-07-09 17:41:47 +0000791 // Pull the padding back off.
792 if (needsPadding)
John McCall0217dfc22011-02-15 06:40:56 +0000793 FieldTypes.pop_back();
John McCall0217dfc22011-02-15 06:40:56 +0000794
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000795 return true;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000796}
797
Anders Carlsson307846f2009-07-23 03:17:50 +0000798bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
799 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
John McCall4d9f1422011-02-15 22:21:29 +0000800 assert(!Alignment.isZero() && "Did not set alignment!");
Mike Stump11289f42009-09-09 15:08:12 +0000801
Anders Carlsson697f6592009-07-23 03:43:54 +0000802 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000803
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000804 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
805 if (RD)
Eli Friedman3c840aa2011-12-12 23:13:20 +0000806 if (!LayoutNonVirtualBases(RD, Layout))
807 return false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000808
Anders Carlsson307846f2009-07-23 03:17:50 +0000809 unsigned FieldNo = 0;
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000810 const FieldDecl *LastFD = 0;
811
Mike Stump11289f42009-09-09 15:08:12 +0000812 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson307846f2009-07-23 03:17:50 +0000813 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000814 if (IsMsStruct) {
815 // Zero-length bitfields following non-bitfield members are
816 // ignored:
817 const FieldDecl *FD = (*Field);
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +0000818 if (Types.getContext().ZeroBitfieldFollowsNonBitfield(FD, LastFD)) {
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000819 --FieldNo;
820 continue;
821 }
822 LastFD = FD;
823 }
824
Anders Carlsson307846f2009-07-23 03:17:50 +0000825 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump11289f42009-09-09 15:08:12 +0000826 assert(!Packed &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000827 "Could not layout fields even with a packed LLVM struct!");
828 return false;
829 }
830 }
831
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000832 if (RD) {
Anders Carlssona459adb2010-11-28 19:18:44 +0000833 // We've laid out the non-virtual bases and the fields, now compute the
834 // non-virtual base field types.
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000835 if (!ComputeNonVirtualBaseType(RD)) {
836 assert(!Packed && "Could not layout even with a packed LLVM struct!");
837 return false;
838 }
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000839
John McCall0153cd32011-11-08 04:01:03 +0000840 // Lay out the virtual bases. The MS ABI uses a different
841 // algorithm here due to the lack of primary virtual bases.
842 if (Types.getContext().getTargetInfo().getCXXABI() != CXXABI_Microsoft) {
843 RD->getIndirectPrimaryBases(IndirectPrimaryBases);
844 if (Layout.isPrimaryBaseVirtual())
845 IndirectPrimaryBases.insert(Layout.getPrimaryBase());
846
Eli Friedman3c840aa2011-12-12 23:13:20 +0000847 if (!LayoutVirtualBases(RD, Layout))
848 return false;
John McCall0153cd32011-11-08 04:01:03 +0000849 } else {
Eli Friedman3c840aa2011-12-12 23:13:20 +0000850 if (!MSLayoutVirtualBases(RD, Layout))
851 return false;
John McCall0153cd32011-11-08 04:01:03 +0000852 }
Anders Carlssona459adb2010-11-28 19:18:44 +0000853 }
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000854
Anders Carlsson307846f2009-07-23 03:17:50 +0000855 // Append tail padding if necessary.
Ken Dyck272b6fa2011-04-24 16:53:44 +0000856 AppendTailPadding(Layout.getSize());
Mike Stump11289f42009-09-09 15:08:12 +0000857
Anders Carlsson307846f2009-07-23 03:17:50 +0000858 return true;
859}
860
Ken Dyck272b6fa2011-04-24 16:53:44 +0000861void CGRecordLayoutBuilder::AppendTailPadding(CharUnits RecordSize) {
862 ResizeLastBaseFieldIfNecessary(RecordSize);
Mike Stump11289f42009-09-09 15:08:12 +0000863
Ken Dyck272b6fa2011-04-24 16:53:44 +0000864 assert(NextFieldOffset <= RecordSize && "Size mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000865
John McCall4d9f1422011-02-15 22:21:29 +0000866 CharUnits AlignedNextFieldOffset =
867 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
Anders Carlsson220bf4f2009-12-08 01:24:23 +0000868
Ken Dyck272b6fa2011-04-24 16:53:44 +0000869 if (AlignedNextFieldOffset == RecordSize) {
Anders Carlsson220bf4f2009-12-08 01:24:23 +0000870 // We don't need any padding.
871 return;
872 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000873
Ken Dyck272b6fa2011-04-24 16:53:44 +0000874 CharUnits NumPadBytes = RecordSize - NextFieldOffset;
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000875 AppendBytes(NumPadBytes);
876}
877
John McCall4d9f1422011-02-15 22:21:29 +0000878void CGRecordLayoutBuilder::AppendField(CharUnits fieldOffset,
Chris Lattnera5f58b02011-07-09 17:41:47 +0000879 llvm::Type *fieldType) {
John McCall4d9f1422011-02-15 22:21:29 +0000880 CharUnits fieldSize =
881 CharUnits::fromQuantity(Types.getTargetData().getTypeAllocSize(fieldType));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000882
John McCall4d9f1422011-02-15 22:21:29 +0000883 FieldTypes.push_back(fieldType);
Anders Carlsson307846f2009-07-23 03:17:50 +0000884
John McCall4d9f1422011-02-15 22:21:29 +0000885 NextFieldOffset = fieldOffset + fieldSize;
Anders Carlsson307846f2009-07-23 03:17:50 +0000886 BitsAvailableInLastField = 0;
887}
888
John McCall4d9f1422011-02-15 22:21:29 +0000889void CGRecordLayoutBuilder::AppendPadding(CharUnits fieldOffset,
890 CharUnits fieldAlignment) {
891 assert(NextFieldOffset <= fieldOffset &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000892 "Incorrect field layout!");
Mike Stump11289f42009-09-09 15:08:12 +0000893
John McCall0153cd32011-11-08 04:01:03 +0000894 // Do nothing if we're already at the right offset.
895 if (fieldOffset == NextFieldOffset) return;
Anders Carlsson307846f2009-07-23 03:17:50 +0000896
John McCall0153cd32011-11-08 04:01:03 +0000897 // If we're not emitting a packed LLVM type, try to avoid adding
898 // unnecessary padding fields.
899 if (!Packed) {
900 // Round up the field offset to the alignment of the field type.
901 CharUnits alignedNextFieldOffset =
902 NextFieldOffset.RoundUpToAlignment(fieldAlignment);
903 assert(alignedNextFieldOffset <= fieldOffset);
Anders Carlsson307846f2009-07-23 03:17:50 +0000904
John McCall0153cd32011-11-08 04:01:03 +0000905 // If that's the right offset, we're done.
906 if (alignedNextFieldOffset == fieldOffset) return;
Anders Carlsson307846f2009-07-23 03:17:50 +0000907 }
John McCall0153cd32011-11-08 04:01:03 +0000908
909 // Otherwise we need explicit padding.
910 CharUnits padding = fieldOffset - NextFieldOffset;
911 AppendBytes(padding);
Anders Carlsson307846f2009-07-23 03:17:50 +0000912}
913
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000914bool CGRecordLayoutBuilder::ResizeLastBaseFieldIfNecessary(CharUnits offset) {
915 // Check if we have a base to resize.
916 if (!LastLaidOutBase.isValid())
917 return false;
918
919 // This offset does not overlap with the tail padding.
920 if (offset >= NextFieldOffset)
921 return false;
922
923 // Restore the field offset and append an i8 array instead.
924 FieldTypes.pop_back();
925 NextFieldOffset = LastLaidOutBase.Offset;
926 AppendBytes(LastLaidOutBase.NonVirtualSize);
927 LastLaidOutBase.invalidate();
928
929 return true;
930}
931
Chris Lattnera5f58b02011-07-09 17:41:47 +0000932llvm::Type *CGRecordLayoutBuilder::getByteArrayType(CharUnits numBytes) {
John McCall4d9f1422011-02-15 22:21:29 +0000933 assert(!numBytes.isZero() && "Empty byte arrays aren't allowed.");
Mike Stump11289f42009-09-09 15:08:12 +0000934
Chris Lattnera5f58b02011-07-09 17:41:47 +0000935 llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
John McCall4d9f1422011-02-15 22:21:29 +0000936 if (numBytes > CharUnits::One())
937 Ty = llvm::ArrayType::get(Ty, numBytes.getQuantity());
Mike Stump11289f42009-09-09 15:08:12 +0000938
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000939 return Ty;
940}
941
John McCall4d9f1422011-02-15 22:21:29 +0000942void CGRecordLayoutBuilder::AppendBytes(CharUnits numBytes) {
943 if (numBytes.isZero())
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000944 return;
945
Anders Carlsson307846f2009-07-23 03:17:50 +0000946 // Append the padding field
John McCall4d9f1422011-02-15 22:21:29 +0000947 AppendField(NextFieldOffset, getByteArrayType(numBytes));
Anders Carlsson307846f2009-07-23 03:17:50 +0000948}
949
Chris Lattner2192fe52011-07-18 04:24:23 +0000950CharUnits CGRecordLayoutBuilder::getTypeAlignment(llvm::Type *Ty) const {
Anders Carlssond78fc892009-07-23 17:24:40 +0000951 if (Packed)
John McCall4d9f1422011-02-15 22:21:29 +0000952 return CharUnits::One();
Mike Stump11289f42009-09-09 15:08:12 +0000953
John McCall4d9f1422011-02-15 22:21:29 +0000954 return CharUnits::fromQuantity(Types.getTargetData().getABITypeAlignment(Ty));
Anders Carlsson307846f2009-07-23 03:17:50 +0000955}
956
John McCall4d9f1422011-02-15 22:21:29 +0000957CharUnits CGRecordLayoutBuilder::getAlignmentAsLLVMStruct() const {
Anders Carlssonacf877b2010-11-28 23:06:23 +0000958 if (Packed)
John McCall4d9f1422011-02-15 22:21:29 +0000959 return CharUnits::One();
Anders Carlssonacf877b2010-11-28 23:06:23 +0000960
John McCall4d9f1422011-02-15 22:21:29 +0000961 CharUnits maxAlignment = CharUnits::One();
Anders Carlssonacf877b2010-11-28 23:06:23 +0000962 for (size_t i = 0; i != FieldTypes.size(); ++i)
John McCall4d9f1422011-02-15 22:21:29 +0000963 maxAlignment = std::max(maxAlignment, getTypeAlignment(FieldTypes[i]));
Anders Carlssonacf877b2010-11-28 23:06:23 +0000964
John McCall4d9f1422011-02-15 22:21:29 +0000965 return maxAlignment;
Anders Carlssonacf877b2010-11-28 23:06:23 +0000966}
967
John McCall0217dfc22011-02-15 06:40:56 +0000968/// Merge in whether a field of the given type is zero-initializable.
John McCall614dbdc2010-08-22 21:01:12 +0000969void CGRecordLayoutBuilder::CheckZeroInitializable(QualType T) {
Anders Carlssond606de72009-08-23 01:25:01 +0000970 // This record already contains a member pointer.
John McCall0217dfc22011-02-15 06:40:56 +0000971 if (!IsZeroInitializableAsBase)
Anders Carlssond606de72009-08-23 01:25:01 +0000972 return;
Mike Stump11289f42009-09-09 15:08:12 +0000973
Anders Carlssond606de72009-08-23 01:25:01 +0000974 // Can only have member pointers if we're compiling C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000975 if (!Types.getContext().getLangOpts().CPlusPlus)
Anders Carlssond606de72009-08-23 01:25:01 +0000976 return;
Mike Stump11289f42009-09-09 15:08:12 +0000977
John McCall0217dfc22011-02-15 06:40:56 +0000978 const Type *elementType = T->getBaseElementTypeUnsafe();
Mike Stump11289f42009-09-09 15:08:12 +0000979
John McCall0217dfc22011-02-15 06:40:56 +0000980 if (const MemberPointerType *MPT = elementType->getAs<MemberPointerType>()) {
John McCall614dbdc2010-08-22 21:01:12 +0000981 if (!Types.getCXXABI().isZeroInitializable(MPT))
John McCall0217dfc22011-02-15 06:40:56 +0000982 IsZeroInitializable = IsZeroInitializableAsBase = false;
983 } else if (const RecordType *RT = elementType->getAs<RecordType>()) {
Anders Carlssone8bfe412010-02-02 05:17:25 +0000984 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
John McCall0217dfc22011-02-15 06:40:56 +0000985 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
986 if (!Layout.isZeroInitializable())
987 IsZeroInitializable = IsZeroInitializableAsBase = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000988 }
Anders Carlssond606de72009-08-23 01:25:01 +0000989}
990
Chris Lattnera5f58b02011-07-09 17:41:47 +0000991CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D,
992 llvm::StructType *Ty) {
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000993 CGRecordLayoutBuilder Builder(*this);
Mike Stump11289f42009-09-09 15:08:12 +0000994
Anders Carlsson307846f2009-07-23 03:17:50 +0000995 Builder.Layout(D);
Anders Carlssone1d5ca52009-07-24 15:20:52 +0000996
Chris Lattnera5f58b02011-07-09 17:41:47 +0000997 Ty->setBody(Builder.FieldTypes, Builder.Packed);
Mike Stump11289f42009-09-09 15:08:12 +0000998
John McCall0217dfc22011-02-15 06:40:56 +0000999 // If we're in C++, compute the base subobject type.
Chris Lattnera5f58b02011-07-09 17:41:47 +00001000 llvm::StructType *BaseTy = 0;
Eli Friedman09d272d2012-01-13 03:58:31 +00001001 if (isa<CXXRecordDecl>(D) && !D->isUnion()) {
John McCall0217dfc22011-02-15 06:40:56 +00001002 BaseTy = Builder.BaseSubobjectType;
1003 if (!BaseTy) BaseTy = Ty;
Anders Carlssonc1351ca2010-11-09 05:25:47 +00001004 }
1005
Daniel Dunbar034299e2010-03-31 01:09:11 +00001006 CGRecordLayout *RL =
John McCall0217dfc22011-02-15 06:40:56 +00001007 new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable,
1008 Builder.IsZeroInitializableAsBase);
Daniel Dunbar034299e2010-03-31 01:09:11 +00001009
John McCall0217dfc22011-02-15 06:40:56 +00001010 RL->NonVirtualBases.swap(Builder.NonVirtualBases);
1011 RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases);
Anders Carlsson061ca522010-05-18 05:22:06 +00001012
Anders Carlsson307846f2009-07-23 03:17:50 +00001013 // Add all the field numbers.
John McCall0217dfc22011-02-15 06:40:56 +00001014 RL->FieldInfo.swap(Builder.Fields);
Anders Carlsson307846f2009-07-23 03:17:50 +00001015
1016 // Add bitfield info.
John McCall0217dfc22011-02-15 06:40:56 +00001017 RL->BitFields.swap(Builder.BitFields);
Mike Stump11289f42009-09-09 15:08:12 +00001018
Daniel Dunbar2ea51832010-04-19 20:44:47 +00001019 // Dump the layout, if requested.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001020 if (getContext().getLangOpts().DumpRecordLayouts) {
Daniel Dunbarccabe482010-04-19 20:44:53 +00001021 llvm::errs() << "\n*** Dumping IRgen Record Layout\n";
Daniel Dunbarb935b932010-04-13 20:58:55 +00001022 llvm::errs() << "Record: ";
1023 D->dump();
1024 llvm::errs() << "\nLayout: ";
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001025 RL->dump();
Daniel Dunbarb935b932010-04-13 20:58:55 +00001026 }
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001027
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001028#ifndef NDEBUG
Daniel Dunbar2ea51832010-04-19 20:44:47 +00001029 // Verify that the computed LLVM struct size matches the AST layout size.
Anders Carlssonc1351ca2010-11-09 05:25:47 +00001030 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
1031
Ken Dyckb0fcc592011-02-11 01:54:29 +00001032 uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize());
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001033 assert(TypeSizeInBits == getTargetData().getTypeAllocSizeInBits(Ty) &&
Daniel Dunbar2ea51832010-04-19 20:44:47 +00001034 "Type size mismatch!");
1035
Anders Carlssonc1351ca2010-11-09 05:25:47 +00001036 if (BaseTy) {
Ken Dyckbec02852011-02-08 02:02:47 +00001037 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
1038 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
1039 CharUnits AlignedNonVirtualTypeSize =
1040 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
1041
1042 uint64_t AlignedNonVirtualTypeSizeInBits =
Ken Dyckb0fcc592011-02-11 01:54:29 +00001043 getContext().toBits(AlignedNonVirtualTypeSize);
Anders Carlssonc1351ca2010-11-09 05:25:47 +00001044
1045 assert(AlignedNonVirtualTypeSizeInBits ==
1046 getTargetData().getTypeAllocSizeInBits(BaseTy) &&
1047 "Type size mismatch!");
1048 }
1049
Daniel Dunbar2ba67442010-04-21 19:10:49 +00001050 // Verify that the LLVM and AST field offsets agree.
Chris Lattner2192fe52011-07-18 04:24:23 +00001051 llvm::StructType *ST =
Daniel Dunbar2ba67442010-04-21 19:10:49 +00001052 dyn_cast<llvm::StructType>(RL->getLLVMType());
1053 const llvm::StructLayout *SL = getTargetData().getStructLayout(ST);
1054
1055 const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
1056 RecordDecl::field_iterator it = D->field_begin();
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001057 const FieldDecl *LastFD = 0;
1058 bool IsMsStruct = D->hasAttr<MsStructAttr>();
Daniel Dunbar2ba67442010-04-21 19:10:49 +00001059 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
1060 const FieldDecl *FD = *it;
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001061
1062 // For non-bit-fields, just check that the LLVM struct offset matches the
1063 // AST offset.
1064 if (!FD->isBitField()) {
Daniel Dunbar2ba67442010-04-21 19:10:49 +00001065 unsigned FieldNo = RL->getLLVMFieldNo(FD);
1066 assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
1067 "Invalid field offset!");
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001068 LastFD = FD;
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001069 continue;
1070 }
1071
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001072 if (IsMsStruct) {
1073 // Zero-length bitfields following non-bitfield members are
1074 // ignored:
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001075 if (getContext().ZeroBitfieldFollowsNonBitfield(FD, LastFD)) {
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001076 --i;
1077 continue;
1078 }
1079 LastFD = FD;
1080 }
1081
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001082 // Ignore unnamed bit-fields.
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001083 if (!FD->getDeclName()) {
1084 LastFD = FD;
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001085 continue;
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001086 }
1087
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001088 const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
1089 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
1090 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
1091
1092 // Verify that every component access is within the structure.
1093 uint64_t FieldOffset = SL->getElementOffsetInBits(AI.FieldIndex);
John McCall8a3c5552011-02-26 08:41:59 +00001094 uint64_t AccessBitOffset = FieldOffset +
Ken Dyckf76759c2011-04-24 10:04:59 +00001095 getContext().toBits(AI.FieldByteOffset);
Daniel Dunbar488f55c2010-04-22 02:35:46 +00001096 assert(AccessBitOffset + AI.AccessWidth <= TypeSizeInBits &&
1097 "Invalid bit-field access (out of range)!");
Daniel Dunbar2ba67442010-04-21 19:10:49 +00001098 }
1099 }
1100#endif
Daniel Dunbar2ea51832010-04-19 20:44:47 +00001101
Daniel Dunbar034299e2010-03-31 01:09:11 +00001102 return RL;
Anders Carlsson307846f2009-07-23 03:17:50 +00001103}
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001104
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001105void CGRecordLayout::print(raw_ostream &OS) const {
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001106 OS << "<CGRecordLayout\n";
John McCall0217dfc22011-02-15 06:40:56 +00001107 OS << " LLVMType:" << *CompleteObjectType << "\n";
1108 if (BaseSubobjectType)
1109 OS << " NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n";
John McCall614dbdc2010-08-22 21:01:12 +00001110 OS << " IsZeroInitializable:" << IsZeroInitializable << "\n";
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001111 OS << " BitFields:[\n";
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001112
1113 // Print bit-field infos in declaration order.
1114 std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001115 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
1116 it = BitFields.begin(), ie = BitFields.end();
1117 it != ie; ++it) {
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001118 const RecordDecl *RD = it->first->getParent();
1119 unsigned Index = 0;
1120 for (RecordDecl::field_iterator
1121 it2 = RD->field_begin(); *it2 != it->first; ++it2)
1122 ++Index;
1123 BFIs.push_back(std::make_pair(Index, &it->second));
1124 }
1125 llvm::array_pod_sort(BFIs.begin(), BFIs.end());
1126 for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
Daniel Dunbarb935b932010-04-13 20:58:55 +00001127 OS.indent(4);
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001128 BFIs[i].second->print(OS);
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001129 OS << "\n";
1130 }
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001131
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001132 OS << "]>\n";
1133}
1134
1135void CGRecordLayout::dump() const {
1136 print(llvm::errs());
1137}
1138
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001139void CGBitFieldInfo::print(raw_ostream &OS) const {
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001140 OS << "<CGBitFieldInfo";
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001141 OS << " Size:" << Size;
Daniel Dunbarb935b932010-04-13 20:58:55 +00001142 OS << " IsSigned:" << IsSigned << "\n";
1143
1144 OS.indent(4 + strlen("<CGBitFieldInfo"));
1145 OS << " NumComponents:" << getNumComponents();
1146 OS << " Components: [";
1147 if (getNumComponents()) {
1148 OS << "\n";
1149 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
1150 const AccessInfo &AI = getComponent(i);
1151 OS.indent(8);
1152 OS << "<AccessInfo"
1153 << " FieldIndex:" << AI.FieldIndex
Ken Dyckf76759c2011-04-24 10:04:59 +00001154 << " FieldByteOffset:" << AI.FieldByteOffset.getQuantity()
Daniel Dunbarb935b932010-04-13 20:58:55 +00001155 << " FieldBitStart:" << AI.FieldBitStart
1156 << " AccessWidth:" << AI.AccessWidth << "\n";
1157 OS.indent(8 + strlen("<AccessInfo"));
Ken Dyck27337a82011-04-24 10:13:17 +00001158 OS << " AccessAlignment:" << AI.AccessAlignment.getQuantity()
Daniel Dunbarb935b932010-04-13 20:58:55 +00001159 << " TargetBitOffset:" << AI.TargetBitOffset
1160 << " TargetBitWidth:" << AI.TargetBitWidth
1161 << ">\n";
1162 }
1163 OS.indent(4);
1164 }
1165 OS << "]>";
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001166}
1167
1168void CGBitFieldInfo::dump() const {
1169 print(llvm::errs());
1170}