blob: 24caf68d76b71e2e01813d19d4de9e26d46cd7a7 [file] [log] [blame]
Daniel Dunbar23ee4b72010-03-31 00:11:27 +00001//===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder ----*- C++ -*-===//
Anders Carlsson307846f2009-07-23 03:17:50 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000010// Builder implementation for CGRecordLayout objects.
Anders Carlsson307846f2009-07-23 03:17:50 +000011//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000014#include "CGRecordLayout.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/Attr.h"
Anders Carlsson4131f002010-11-24 22:50:27 +000017#include "clang/AST/CXXInheritance.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000018#include "clang/AST/DeclCXX.h"
19#include "clang/AST/Expr.h"
20#include "clang/AST/RecordLayout.h"
21#include "CodeGenTypes.h"
John McCall614dbdc2010-08-22 21:01:12 +000022#include "CGCXXABI.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000023#include "llvm/DerivedTypes.h"
Daniel Dunbarb97bff92010-04-12 18:14:18 +000024#include "llvm/Type.h"
Daniel Dunbar2ba67442010-04-21 19:10:49 +000025#include "llvm/Support/Debug.h"
Daniel Dunbarb97bff92010-04-12 18:14:18 +000026#include "llvm/Support/raw_ostream.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000027#include "llvm/Target/TargetData.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000028using namespace clang;
29using namespace CodeGen;
30
John McCallbcd38212010-11-30 23:17:27 +000031namespace {
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000032
33class CGRecordLayoutBuilder {
34public:
35 /// FieldTypes - Holds the LLVM types that the struct is created from.
John McCall0217dfc22011-02-15 06:40:56 +000036 ///
Anders Carlssonb6d31e72011-04-17 21:32:41 +000037 llvm::SmallVector<const llvm::Type *, 16> FieldTypes;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000038
John McCall0217dfc22011-02-15 06:40:56 +000039 /// BaseSubobjectType - Holds the LLVM type for the non-virtual part
Anders Carlssonc1351ca2010-11-09 05:25:47 +000040 /// of the struct. For example, consider:
41 ///
42 /// struct A { int i; };
43 /// struct B { void *v; };
44 /// struct C : virtual A, B { };
45 ///
46 /// The LLVM type of C will be
47 /// %struct.C = type { i32 (...)**, %struct.A, i32, %struct.B }
48 ///
49 /// And the LLVM type of the non-virtual base struct will be
50 /// %struct.C.base = type { i32 (...)**, %struct.A, i32 }
John McCall0217dfc22011-02-15 06:40:56 +000051 ///
52 /// This only gets initialized if the base subobject type is
53 /// different from the complete-object type.
54 const llvm::StructType *BaseSubobjectType;
Anders Carlssonc1351ca2010-11-09 05:25:47 +000055
John McCall0217dfc22011-02-15 06:40:56 +000056 /// FieldInfo - Holds a field and its corresponding LLVM field number.
57 llvm::DenseMap<const FieldDecl *, unsigned> Fields;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000058
John McCall0217dfc22011-02-15 06:40:56 +000059 /// BitFieldInfo - Holds location and size information about a bit field.
60 llvm::DenseMap<const FieldDecl *, CGBitFieldInfo> BitFields;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000061
John McCall0217dfc22011-02-15 06:40:56 +000062 llvm::DenseMap<const CXXRecordDecl *, unsigned> NonVirtualBases;
63 llvm::DenseMap<const CXXRecordDecl *, unsigned> VirtualBases;
Anders Carlsson4131f002010-11-24 22:50:27 +000064
65 /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are
66 /// primary base classes for some other direct or indirect base class.
67 CXXIndirectPrimaryBaseSet IndirectPrimaryBases;
68
Anders Carlssona459adb2010-11-28 19:18:44 +000069 /// LaidOutVirtualBases - A set of all laid out virtual bases, used to avoid
70 /// avoid laying out virtual bases more than once.
71 llvm::SmallPtrSet<const CXXRecordDecl *, 4> LaidOutVirtualBases;
72
John McCall614dbdc2010-08-22 21:01:12 +000073 /// IsZeroInitializable - Whether this struct can be C++
74 /// zero-initialized with an LLVM zeroinitializer.
75 bool IsZeroInitializable;
John McCall0217dfc22011-02-15 06:40:56 +000076 bool IsZeroInitializableAsBase;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000077
78 /// Packed - Whether the resulting LLVM struct will be packed or not.
79 bool Packed;
80
81private:
82 CodeGenTypes &Types;
83
Anders Carlssonfcaaa692011-04-17 21:56:13 +000084 /// LastLaidOutBaseInfo - Contains the offset and non-virtual size of the
85 /// last base laid out. Used so that we can replace the last laid out base
86 /// type with an i8 array if needed.
87 struct LastLaidOutBaseInfo {
88 CharUnits Offset;
89 CharUnits NonVirtualSize;
90
91 bool isValid() const { return !NonVirtualSize.isZero(); }
92 void invalidate() { NonVirtualSize = CharUnits::Zero(); }
93
94 } LastLaidOutBase;
95
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000096 /// Alignment - Contains the alignment of the RecordDecl.
John McCall4d9f1422011-02-15 22:21:29 +000097 CharUnits Alignment;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000098
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000099 /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field,
100 /// this will have the number of bits still available in the field.
101 char BitsAvailableInLastField;
102
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.
108 const llvm::Type *LayoutUnionField(const FieldDecl *Field,
109 const ASTRecordLayout &Layout);
110
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000111 /// LayoutUnion - Will layout a union RecordDecl.
112 void LayoutUnion(const RecordDecl *D);
113
114 /// LayoutField - try to layout all fields in the record decl.
115 /// Returns false if the operation failed because the struct is not packed.
116 bool LayoutFields(const RecordDecl *D);
117
Anders Carlssona518b2a2010-12-04 23:59:48 +0000118 /// Layout a single base, virtual or non-virtual
John McCall4d9f1422011-02-15 22:21:29 +0000119 void LayoutBase(const CXXRecordDecl *base,
120 const CGRecordLayout &baseLayout,
121 CharUnits baseOffset);
Anders Carlssona518b2a2010-12-04 23:59:48 +0000122
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000123 /// LayoutVirtualBase - layout a single virtual base.
John McCall4d9f1422011-02-15 22:21:29 +0000124 void LayoutVirtualBase(const CXXRecordDecl *base,
125 CharUnits baseOffset);
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000126
Anders Carlssona459adb2010-11-28 19:18:44 +0000127 /// LayoutVirtualBases - layout the virtual bases of a record decl.
128 void LayoutVirtualBases(const CXXRecordDecl *RD,
129 const ASTRecordLayout &Layout);
130
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000131 /// LayoutNonVirtualBase - layout a single non-virtual base.
John McCall4d9f1422011-02-15 22:21:29 +0000132 void LayoutNonVirtualBase(const CXXRecordDecl *base,
133 CharUnits baseOffset);
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000134
Anders Carlssona459adb2010-11-28 19:18:44 +0000135 /// LayoutNonVirtualBases - layout the virtual bases of a record decl.
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000136 void LayoutNonVirtualBases(const CXXRecordDecl *RD,
137 const ASTRecordLayout &Layout);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000138
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000139 /// ComputeNonVirtualBaseType - Compute the non-virtual base field types.
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000140 bool ComputeNonVirtualBaseType(const CXXRecordDecl *RD);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000141
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000142 /// LayoutField - layout a single field. Returns false if the operation failed
143 /// because the current struct is not packed.
144 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
145
146 /// LayoutBitField - layout a single bit field.
147 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
148
149 /// AppendField - Appends a field with the given offset and type.
John McCall4d9f1422011-02-15 22:21:29 +0000150 void AppendField(CharUnits fieldOffset, const llvm::Type *FieldTy);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000151
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000152 /// AppendPadding - Appends enough padding bytes so that the total
153 /// struct size is a multiple of the field alignment.
John McCall4d9f1422011-02-15 22:21:29 +0000154 void AppendPadding(CharUnits fieldOffset, CharUnits fieldAlignment);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000155
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000156 /// ResizeLastBaseFieldIfNecessary - Fields and bases can be laid out in the
157 /// tail padding of a previous base. If this happens, the type of the previous
158 /// base needs to be changed to an array of i8. Returns true if the last
159 /// laid out base was resized.
160 bool ResizeLastBaseFieldIfNecessary(CharUnits offset);
161
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000162 /// getByteArrayType - Returns a byte array type with the given number of
163 /// elements.
John McCall4d9f1422011-02-15 22:21:29 +0000164 const llvm::Type *getByteArrayType(CharUnits NumBytes);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000165
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000166 /// AppendBytes - Append a given number of bytes to the record.
John McCall4d9f1422011-02-15 22:21:29 +0000167 void AppendBytes(CharUnits numBytes);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000168
169 /// AppendTailPadding - Append enough tail padding so that the type will have
170 /// the passed size.
Ken Dyck272b6fa2011-04-24 16:53:44 +0000171 void AppendTailPadding(CharUnits RecordSize);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000172
John McCall4d9f1422011-02-15 22:21:29 +0000173 CharUnits getTypeAlignment(const llvm::Type *Ty) const;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000174
Anders Carlssonacf877b2010-11-28 23:06:23 +0000175 /// getAlignmentAsLLVMStruct - Returns the maximum alignment of all the
176 /// LLVM element types.
John McCall4d9f1422011-02-15 22:21:29 +0000177 CharUnits getAlignmentAsLLVMStruct() const;
Anders Carlssonacf877b2010-11-28 23:06:23 +0000178
John McCall614dbdc2010-08-22 21:01:12 +0000179 /// CheckZeroInitializable - Check if the given type contains a pointer
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000180 /// to data member.
John McCall614dbdc2010-08-22 21:01:12 +0000181 void CheckZeroInitializable(QualType T);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000182
183public:
184 CGRecordLayoutBuilder(CodeGenTypes &Types)
John McCall0217dfc22011-02-15 06:40:56 +0000185 : BaseSubobjectType(0),
186 IsZeroInitializable(true), IsZeroInitializableAsBase(true),
John McCall4d9f1422011-02-15 22:21:29 +0000187 Packed(false), Types(Types), BitsAvailableInLastField(0) { }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000188
189 /// Layout - Will layout a RecordDecl.
190 void Layout(const RecordDecl *D);
191};
192
193}
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000194
Anders Carlsson307846f2009-07-23 03:17:50 +0000195void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
John McCall4d9f1422011-02-15 22:21:29 +0000196 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment();
Anders Carlsson09a37742009-09-02 17:51:33 +0000197 Packed = D->hasAttr<PackedAttr>();
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000198
Anders Carlsson697f6592009-07-23 03:43:54 +0000199 if (D->isUnion()) {
200 LayoutUnion(D);
201 return;
202 }
Anders Carlsson68e0b682009-08-08 18:23:56 +0000203
Anders Carlsson307846f2009-07-23 03:17:50 +0000204 if (LayoutFields(D))
205 return;
Mike Stump11289f42009-09-09 15:08:12 +0000206
Anders Carlsson307846f2009-07-23 03:17:50 +0000207 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlssond78fc892009-07-23 17:24:40 +0000208 Packed = true;
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000209 LastLaidOutBase.invalidate();
John McCall4d9f1422011-02-15 22:21:29 +0000210 NextFieldOffset = CharUnits::Zero();
Anders Carlsson307846f2009-07-23 03:17:50 +0000211 FieldTypes.clear();
John McCall0217dfc22011-02-15 06:40:56 +0000212 Fields.clear();
213 BitFields.clear();
214 NonVirtualBases.clear();
215 VirtualBases.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000216
Anders Carlsson307846f2009-07-23 03:17:50 +0000217 LayoutFields(D);
218}
219
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000220CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
221 const FieldDecl *FD,
222 uint64_t FieldOffset,
223 uint64_t FieldSize,
224 uint64_t ContainingTypeSizeInBits,
225 unsigned ContainingTypeAlign) {
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000226 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(FD->getType());
John McCall8a3c5552011-02-26 08:41:59 +0000227 CharUnits TypeSizeInBytes =
228 CharUnits::fromQuantity(Types.getTargetData().getTypeAllocSize(Ty));
229 uint64_t TypeSizeInBits = Types.getContext().toBits(TypeSizeInBytes);
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000230
231 bool IsSigned = FD->getType()->isSignedIntegerType();
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000232
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000233 if (FieldSize > TypeSizeInBits) {
Anders Carlssond5f27b02010-04-17 22:54:57 +0000234 // We have a wide bit-field. The extra bits are only used for padding, so
235 // if we have a bitfield of type T, with size N:
236 //
237 // T t : N;
238 //
239 // We can just assume that it's:
240 //
241 // T t : sizeof(T);
242 //
243 FieldSize = TypeSizeInBits;
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000244 }
245
Chris Lattnerfb59c7c2011-02-17 22:09:58 +0000246 // in big-endian machines the first fields are in higher bit positions,
247 // so revert the offset. The byte offsets are reversed(back) later.
248 if (Types.getTargetData().isBigEndian()) {
249 FieldOffset = ((ContainingTypeSizeInBits)-FieldOffset-FieldSize);
250 }
251
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000252 // Compute the access components. The policy we use is to start by attempting
253 // to access using the width of the bit-field type itself and to always access
254 // at aligned indices of that type. If such an access would fail because it
255 // extends past the bound of the type, then we reduce size to the next smaller
256 // power of two and retry. The current algorithm assumes pow2 sized types,
257 // although this is easy to fix.
258 //
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000259 assert(llvm::isPowerOf2_32(TypeSizeInBits) && "Unexpected type size!");
260 CGBitFieldInfo::AccessInfo Components[3];
261 unsigned NumComponents = 0;
Nick Lewyckyd2348d82011-03-22 17:35:47 +0000262 unsigned AccessedTargetBits = 0; // The number of target bits accessed.
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000263 unsigned AccessWidth = TypeSizeInBits; // The current access width to attempt.
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000264
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000265 // Round down from the field offset to find the first access position that is
266 // at an aligned offset of the initial access type.
Daniel Dunbar59813772010-04-22 15:22:33 +0000267 uint64_t AccessStart = FieldOffset - (FieldOffset % AccessWidth);
268
269 // Adjust initial access size to fit within record.
John McCall8a3c5552011-02-26 08:41:59 +0000270 while (AccessWidth > Types.getTarget().getCharWidth() &&
Daniel Dunbar59813772010-04-22 15:22:33 +0000271 AccessStart + AccessWidth > ContainingTypeSizeInBits) {
272 AccessWidth >>= 1;
273 AccessStart = FieldOffset - (FieldOffset % AccessWidth);
274 }
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000275
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000276 while (AccessedTargetBits < FieldSize) {
277 // Check that we can access using a type of this size, without reading off
278 // the end of the structure. This can occur with packed structures and
279 // -fno-bitfield-type-align, for example.
280 if (AccessStart + AccessWidth > ContainingTypeSizeInBits) {
281 // If so, reduce access size to the next smaller power-of-two and retry.
282 AccessWidth >>= 1;
John McCall8a3c5552011-02-26 08:41:59 +0000283 assert(AccessWidth >= Types.getTarget().getCharWidth()
284 && "Cannot access under byte size!");
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000285 continue;
286 }
Daniel Dunbarb935b932010-04-13 20:58:55 +0000287
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000288 // Otherwise, add an access component.
Daniel Dunbarb935b932010-04-13 20:58:55 +0000289
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000290 // First, compute the bits inside this access which are part of the
291 // target. We are reading bits [AccessStart, AccessStart + AccessWidth); the
292 // intersection with [FieldOffset, FieldOffset + FieldSize) gives the bits
293 // in the target that we are reading.
Daniel Dunbar59813772010-04-22 15:22:33 +0000294 assert(FieldOffset < AccessStart + AccessWidth && "Invalid access start!");
295 assert(AccessStart < FieldOffset + FieldSize && "Invalid access start!");
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000296 uint64_t AccessBitsInFieldStart = std::max(AccessStart, FieldOffset);
297 uint64_t AccessBitsInFieldSize =
Daniel Dunbar59813772010-04-22 15:22:33 +0000298 std::min(AccessWidth + AccessStart,
299 FieldOffset + FieldSize) - AccessBitsInFieldStart;
Daniel Dunbar5d6c07e2010-04-22 14:56:10 +0000300
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000301 assert(NumComponents < 3 && "Unexpected number of components!");
302 CGBitFieldInfo::AccessInfo &AI = Components[NumComponents++];
303 AI.FieldIndex = 0;
304 // FIXME: We still follow the old access pattern of only using the field
305 // byte offset. We should switch this once we fix the struct layout to be
306 // pretty.
Chris Lattnerfb59c7c2011-02-17 22:09:58 +0000307
308 // on big-endian machines we reverted the bit offset because first fields are
309 // in higher bits. But this also reverts the bytes, so fix this here by reverting
310 // the byte offset on big-endian machines.
311 if (Types.getTargetData().isBigEndian()) {
Ken Dyckf76759c2011-04-24 10:04:59 +0000312 AI.FieldByteOffset = Types.getContext().toCharUnitsFromBits(
313 ContainingTypeSizeInBits - AccessStart - AccessWidth);
Chris Lattnerfb59c7c2011-02-17 22:09:58 +0000314 } else {
Ken Dyckf76759c2011-04-24 10:04:59 +0000315 AI.FieldByteOffset = Types.getContext().toCharUnitsFromBits(AccessStart);
Chris Lattnerfb59c7c2011-02-17 22:09:58 +0000316 }
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000317 AI.FieldBitStart = AccessBitsInFieldStart - AccessStart;
318 AI.AccessWidth = AccessWidth;
Ken Dyck27337a82011-04-24 10:13:17 +0000319 AI.AccessAlignment = Types.getContext().toCharUnitsFromBits(
320 llvm::MinAlign(ContainingTypeAlign, AccessStart));
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000321 AI.TargetBitOffset = AccessedTargetBits;
322 AI.TargetBitWidth = AccessBitsInFieldSize;
323
324 AccessStart += AccessWidth;
325 AccessedTargetBits += AI.TargetBitWidth;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000326 }
327
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000328 assert(AccessedTargetBits == FieldSize && "Invalid bit-field access!");
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000329 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000330}
331
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000332CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
333 const FieldDecl *FD,
334 uint64_t FieldOffset,
335 uint64_t FieldSize) {
336 const RecordDecl *RD = FD->getParent();
337 const ASTRecordLayout &RL = Types.getContext().getASTRecordLayout(RD);
Ken Dyckb0fcc592011-02-11 01:54:29 +0000338 uint64_t ContainingTypeSizeInBits = Types.getContext().toBits(RL.getSize());
Ken Dyck7ad11e72011-02-15 02:32:40 +0000339 unsigned ContainingTypeAlign = Types.getContext().toBits(RL.getAlignment());
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000340
341 return MakeInfo(Types, FD, FieldOffset, FieldSize, ContainingTypeSizeInBits,
342 ContainingTypeAlign);
343}
344
Anders Carlsson307846f2009-07-23 03:17:50 +0000345void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
John McCall4d9f1422011-02-15 22:21:29 +0000346 uint64_t fieldOffset) {
347 uint64_t fieldSize =
Anders Carlsson307846f2009-07-23 03:17:50 +0000348 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump11289f42009-09-09 15:08:12 +0000349
John McCall4d9f1422011-02-15 22:21:29 +0000350 if (fieldSize == 0)
Anders Carlsson307846f2009-07-23 03:17:50 +0000351 return;
352
John McCall8a3c5552011-02-26 08:41:59 +0000353 uint64_t nextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset);
Ken Dyck345a6de2011-04-24 16:40:29 +0000354 CharUnits numBytesToAppend;
355 unsigned charAlign = Types.getContext().Target.getCharAlign();
Mike Stump11289f42009-09-09 15:08:12 +0000356
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000357 if (fieldOffset < nextFieldOffsetInBits && !BitsAvailableInLastField) {
Ken Dyck345a6de2011-04-24 16:40:29 +0000358 assert(fieldOffset % charAlign == 0 &&
359 "Field offset not aligned correctly");
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000360
361 CharUnits fieldOffsetInCharUnits =
362 Types.getContext().toCharUnitsFromBits(fieldOffset);
363
364 // Try to resize the last base field.
365 if (ResizeLastBaseFieldIfNecessary(fieldOffsetInCharUnits))
366 nextFieldOffsetInBits = Types.getContext().toBits(NextFieldOffset);
367 }
368
John McCall4d9f1422011-02-15 22:21:29 +0000369 if (fieldOffset < nextFieldOffsetInBits) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000370 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
John McCall4d9f1422011-02-15 22:21:29 +0000371 assert(!NextFieldOffset.isZero() && "Must have laid out at least one byte");
Mike Stump11289f42009-09-09 15:08:12 +0000372
Anders Carlsson307846f2009-07-23 03:17:50 +0000373 // The bitfield begins in the previous bit-field.
Ken Dyck345a6de2011-04-24 16:40:29 +0000374 numBytesToAppend = Types.getContext().toCharUnitsFromBits(
375 llvm::RoundUpToAlignment(fieldSize - BitsAvailableInLastField,
376 charAlign));
Anders Carlsson307846f2009-07-23 03:17:50 +0000377 } else {
Ken Dyck345a6de2011-04-24 16:40:29 +0000378 assert(fieldOffset % charAlign == 0 &&
379 "Field offset not aligned correctly");
Anders Carlsson307846f2009-07-23 03:17:50 +0000380
381 // Append padding if necessary.
Ken Dyck345a6de2011-04-24 16:40:29 +0000382 AppendPadding(Types.getContext().toCharUnitsFromBits(fieldOffset),
383 CharUnits::One());
Mike Stump11289f42009-09-09 15:08:12 +0000384
Ken Dyck345a6de2011-04-24 16:40:29 +0000385 numBytesToAppend = Types.getContext().toCharUnitsFromBits(
386 llvm::RoundUpToAlignment(fieldSize, charAlign));
Mike Stump11289f42009-09-09 15:08:12 +0000387
Ken Dyck345a6de2011-04-24 16:40:29 +0000388 assert(!numBytesToAppend.isZero() && "No bytes to append!");
Anders Carlsson307846f2009-07-23 03:17:50 +0000389 }
390
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000391 // Add the bit field info.
John McCall0217dfc22011-02-15 06:40:56 +0000392 BitFields.insert(std::make_pair(D,
John McCall4d9f1422011-02-15 22:21:29 +0000393 CGBitFieldInfo::MakeInfo(Types, D, fieldOffset, fieldSize)));
Mike Stump11289f42009-09-09 15:08:12 +0000394
Ken Dyck345a6de2011-04-24 16:40:29 +0000395 AppendBytes(numBytesToAppend);
Mike Stump11289f42009-09-09 15:08:12 +0000396
Mike Stump11289f42009-09-09 15:08:12 +0000397 BitsAvailableInLastField =
Ken Dyck345a6de2011-04-24 16:40:29 +0000398 Types.getContext().toBits(NextFieldOffset) - (fieldOffset + fieldSize);
Anders Carlsson307846f2009-07-23 03:17:50 +0000399}
400
401bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
John McCall4d9f1422011-02-15 22:21:29 +0000402 uint64_t fieldOffset) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000403 // If the field is packed, then we need a packed struct.
Anders Carlsson68e0b682009-08-08 18:23:56 +0000404 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson307846f2009-07-23 03:17:50 +0000405 return false;
406
407 if (D->isBitField()) {
408 // We must use packed structs for unnamed bit fields since they
409 // don't affect the struct alignment.
Anders Carlssond78fc892009-07-23 17:24:40 +0000410 if (!Packed && !D->getDeclName())
Anders Carlsson307846f2009-07-23 03:17:50 +0000411 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000412
John McCall4d9f1422011-02-15 22:21:29 +0000413 LayoutBitField(D, fieldOffset);
Anders Carlsson307846f2009-07-23 03:17:50 +0000414 return true;
415 }
Mike Stump11289f42009-09-09 15:08:12 +0000416
John McCall614dbdc2010-08-22 21:01:12 +0000417 CheckZeroInitializable(D->getType());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000418
John McCall8a3c5552011-02-26 08:41:59 +0000419 assert(fieldOffset % Types.getTarget().getCharWidth() == 0
420 && "field offset is not on a byte boundary!");
421 CharUnits fieldOffsetInBytes
422 = Types.getContext().toCharUnitsFromBits(fieldOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000423
Anders Carlsson19702bb2009-08-04 16:29:15 +0000424 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
John McCall4d9f1422011-02-15 22:21:29 +0000425 CharUnits typeAlignment = getTypeAlignment(Ty);
Anders Carlsson19702bb2009-08-04 16:29:15 +0000426
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000427 // If the type alignment is larger then the struct alignment, we must use
428 // a packed struct.
John McCall4d9f1422011-02-15 22:21:29 +0000429 if (typeAlignment > Alignment) {
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000430 assert(!Packed && "Alignment is wrong even with packed struct!");
431 return false;
432 }
Mike Stump11289f42009-09-09 15:08:12 +0000433
John McCall4d9f1422011-02-15 22:21:29 +0000434 if (!Packed) {
435 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
436 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
437 if (const MaxFieldAlignmentAttr *MFAA =
438 RD->getAttr<MaxFieldAlignmentAttr>()) {
John McCall8a3c5552011-02-26 08:41:59 +0000439 if (MFAA->getAlignment() != Types.getContext().toBits(typeAlignment))
John McCall4d9f1422011-02-15 22:21:29 +0000440 return false;
441 }
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000442 }
443 }
444
Anders Carlsson19702bb2009-08-04 16:29:15 +0000445 // Round up the field offset to the alignment of the field type.
John McCall4d9f1422011-02-15 22:21:29 +0000446 CharUnits alignedNextFieldOffsetInBytes =
447 NextFieldOffset.RoundUpToAlignment(typeAlignment);
Anders Carlsson19702bb2009-08-04 16:29:15 +0000448
John McCall4d9f1422011-02-15 22:21:29 +0000449 if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000450 // Try to resize the last base field.
451 if (ResizeLastBaseFieldIfNecessary(fieldOffsetInBytes)) {
452 alignedNextFieldOffsetInBytes =
453 NextFieldOffset.RoundUpToAlignment(typeAlignment);
454 }
455 }
456
457 if (fieldOffsetInBytes < alignedNextFieldOffsetInBytes) {
Anders Carlsson19702bb2009-08-04 16:29:15 +0000458 assert(!Packed && "Could not place field even with packed struct!");
459 return false;
460 }
Mike Stump11289f42009-09-09 15:08:12 +0000461
John McCall4d9f1422011-02-15 22:21:29 +0000462 AppendPadding(fieldOffsetInBytes, typeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +0000463
Anders Carlsson307846f2009-07-23 03:17:50 +0000464 // Now append the field.
John McCall0217dfc22011-02-15 06:40:56 +0000465 Fields[D] = FieldTypes.size();
John McCall4d9f1422011-02-15 22:21:29 +0000466 AppendField(fieldOffsetInBytes, Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000467
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000468 LastLaidOutBase.invalidate();
Anders Carlsson307846f2009-07-23 03:17:50 +0000469 return true;
470}
471
Anders Carlsson1de2f572010-04-17 20:49:27 +0000472const llvm::Type *
473CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
474 const ASTRecordLayout &Layout) {
475 if (Field->isBitField()) {
476 uint64_t FieldSize =
477 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
478
479 // Ignore zero sized bit fields.
480 if (FieldSize == 0)
481 return 0;
482
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000483 const llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
Ken Dyck7a0b19f2011-04-24 16:47:33 +0000484 CharUnits NumBytesToAppend = Types.getContext().toCharUnitsFromBits(
485 llvm::RoundUpToAlignment(FieldSize,
486 Types.getContext().Target.getCharAlign()));
Anders Carlsson2295f132010-04-17 21:04:52 +0000487
Ken Dyck7a0b19f2011-04-24 16:47:33 +0000488 if (NumBytesToAppend > CharUnits::One())
489 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend.getQuantity());
Anders Carlsson2295f132010-04-17 21:04:52 +0000490
Anders Carlsson1de2f572010-04-17 20:49:27 +0000491 // Add the bit field info.
John McCall0217dfc22011-02-15 06:40:56 +0000492 BitFields.insert(std::make_pair(Field,
493 CGBitFieldInfo::MakeInfo(Types, Field, 0, FieldSize)));
Anders Carlsson2295f132010-04-17 21:04:52 +0000494 return FieldTy;
Anders Carlsson1de2f572010-04-17 20:49:27 +0000495 }
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000496
Anders Carlsson1de2f572010-04-17 20:49:27 +0000497 // This is a regular union field.
John McCall0217dfc22011-02-15 06:40:56 +0000498 Fields[Field] = 0;
Anders Carlsson1de2f572010-04-17 20:49:27 +0000499 return Types.ConvertTypeForMemRecursive(Field->getType());
500}
501
Anders Carlsson697f6592009-07-23 03:43:54 +0000502void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
503 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump11289f42009-09-09 15:08:12 +0000504
John McCall4d9f1422011-02-15 22:21:29 +0000505 const ASTRecordLayout &layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000506
John McCall4d9f1422011-02-15 22:21:29 +0000507 const llvm::Type *unionType = 0;
508 CharUnits unionSize = CharUnits::Zero();
509 CharUnits unionAlign = CharUnits::Zero();
Mike Stump11289f42009-09-09 15:08:12 +0000510
John McCall4d9f1422011-02-15 22:21:29 +0000511 bool hasOnlyZeroSizedBitFields = true;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000512
John McCall4d9f1422011-02-15 22:21:29 +0000513 unsigned fieldNo = 0;
514 for (RecordDecl::field_iterator field = D->field_begin(),
515 fieldEnd = D->field_end(); field != fieldEnd; ++field, ++fieldNo) {
516 assert(layout.getFieldOffset(fieldNo) == 0 &&
Anders Carlsson697f6592009-07-23 03:43:54 +0000517 "Union field offset did not start at the beginning of record!");
John McCall4d9f1422011-02-15 22:21:29 +0000518 const llvm::Type *fieldType = LayoutUnionField(*field, layout);
Anders Carlssonf814ee62009-07-23 04:00:39 +0000519
John McCall4d9f1422011-02-15 22:21:29 +0000520 if (!fieldType)
Anders Carlsson1de2f572010-04-17 20:49:27 +0000521 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000522
John McCall4d9f1422011-02-15 22:21:29 +0000523 hasOnlyZeroSizedBitFields = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000524
John McCall4d9f1422011-02-15 22:21:29 +0000525 CharUnits fieldAlign = CharUnits::fromQuantity(
526 Types.getTargetData().getABITypeAlignment(fieldType));
527 CharUnits fieldSize = CharUnits::fromQuantity(
528 Types.getTargetData().getTypeAllocSize(fieldType));
Mike Stump11289f42009-09-09 15:08:12 +0000529
John McCall4d9f1422011-02-15 22:21:29 +0000530 if (fieldAlign < unionAlign)
Anders Carlsson697f6592009-07-23 03:43:54 +0000531 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000532
John McCall4d9f1422011-02-15 22:21:29 +0000533 if (fieldAlign > unionAlign || fieldSize > unionSize) {
534 unionType = fieldType;
535 unionAlign = fieldAlign;
536 unionSize = fieldSize;
Anders Carlsson697f6592009-07-23 03:43:54 +0000537 }
538 }
Mike Stump11289f42009-09-09 15:08:12 +0000539
Anders Carlsson697f6592009-07-23 03:43:54 +0000540 // Now add our field.
John McCall4d9f1422011-02-15 22:21:29 +0000541 if (unionType) {
542 AppendField(CharUnits::Zero(), unionType);
Anders Carlsson0e912752009-09-03 22:56:02 +0000543
John McCall4d9f1422011-02-15 22:21:29 +0000544 if (getTypeAlignment(unionType) > layout.getAlignment()) {
Anders Carlsson0e912752009-09-03 22:56:02 +0000545 // We need a packed struct.
546 Packed = true;
John McCall4d9f1422011-02-15 22:21:29 +0000547 unionAlign = CharUnits::One();
Anders Carlsson0e912752009-09-03 22:56:02 +0000548 }
549 }
John McCall4d9f1422011-02-15 22:21:29 +0000550 if (unionAlign.isZero()) {
551 assert(hasOnlyZeroSizedBitFields &&
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000552 "0-align record did not have all zero-sized bit-fields!");
John McCall4d9f1422011-02-15 22:21:29 +0000553 unionAlign = CharUnits::One();
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000554 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000555
Anders Carlsson697f6592009-07-23 03:43:54 +0000556 // Append tail padding.
John McCall4d9f1422011-02-15 22:21:29 +0000557 CharUnits recordSize = layout.getSize();
558 if (recordSize > unionSize)
559 AppendPadding(recordSize, unionAlign);
Anders Carlsson697f6592009-07-23 03:43:54 +0000560}
561
John McCall0217dfc22011-02-15 06:40:56 +0000562void CGRecordLayoutBuilder::LayoutBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000563 const CGRecordLayout &baseLayout,
564 CharUnits baseOffset) {
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000565 ResizeLastBaseFieldIfNecessary(baseOffset);
566
John McCall4d9f1422011-02-15 22:21:29 +0000567 AppendPadding(baseOffset, CharUnits::One());
Anders Carlssona518b2a2010-12-04 23:59:48 +0000568
John McCall0217dfc22011-02-15 06:40:56 +0000569 const ASTRecordLayout &baseASTLayout
570 = Types.getContext().getASTRecordLayout(base);
571
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000572 LastLaidOutBase.Offset = NextFieldOffset;
573 LastLaidOutBase.NonVirtualSize = baseASTLayout.getNonVirtualSize();
574
John McCall4d9f1422011-02-15 22:21:29 +0000575 // Fields and bases can be laid out in the tail padding of previous
576 // bases. If this happens, we need to allocate the base as an i8
577 // array; otherwise, we can use the subobject type. However,
578 // actually doing that would require knowledge of what immediately
579 // follows this base in the layout, so instead we do a conservative
580 // approximation, which is to use the base subobject type if it
581 // has the same LLVM storage size as the nvsize.
582
John McCall4d9f1422011-02-15 22:21:29 +0000583 const llvm::StructType *subobjectType = baseLayout.getBaseSubobjectLLVMType();
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000584 AppendField(baseOffset, subobjectType);
John McCall4d9f1422011-02-15 22:21:29 +0000585
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000586 Types.addBaseSubobjectTypeName(base, baseLayout);
John McCall0217dfc22011-02-15 06:40:56 +0000587}
588
589void CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000590 CharUnits baseOffset) {
John McCall0217dfc22011-02-15 06:40:56 +0000591 // Ignore empty bases.
592 if (base->isEmpty()) return;
593
594 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
595 if (IsZeroInitializableAsBase) {
596 assert(IsZeroInitializable &&
597 "class zero-initializable as base but not as complete object");
598
599 IsZeroInitializable = IsZeroInitializableAsBase =
600 baseLayout.isZeroInitializableAsBase();
601 }
602
John McCall4d9f1422011-02-15 22:21:29 +0000603 LayoutBase(base, baseLayout, baseOffset);
John McCall0217dfc22011-02-15 06:40:56 +0000604 NonVirtualBases[base] = (FieldTypes.size() - 1);
Anders Carlssona518b2a2010-12-04 23:59:48 +0000605}
606
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000607void
John McCall0217dfc22011-02-15 06:40:56 +0000608CGRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *base,
John McCall4d9f1422011-02-15 22:21:29 +0000609 CharUnits baseOffset) {
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000610 // Ignore empty bases.
John McCall0217dfc22011-02-15 06:40:56 +0000611 if (base->isEmpty()) return;
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000612
John McCall0217dfc22011-02-15 06:40:56 +0000613 const CGRecordLayout &baseLayout = Types.getCGRecordLayout(base);
614 if (IsZeroInitializable)
615 IsZeroInitializable = baseLayout.isZeroInitializableAsBase();
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000616
John McCall4d9f1422011-02-15 22:21:29 +0000617 LayoutBase(base, baseLayout, baseOffset);
John McCall0217dfc22011-02-15 06:40:56 +0000618 VirtualBases[base] = (FieldTypes.size() - 1);
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000619}
620
Anders Carlssona459adb2010-11-28 19:18:44 +0000621/// LayoutVirtualBases - layout the non-virtual bases of a record decl.
622void
623CGRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
624 const ASTRecordLayout &Layout) {
625 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
626 E = RD->bases_end(); I != E; ++I) {
627 const CXXRecordDecl *BaseDecl =
628 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
629
630 // We only want to lay out virtual bases that aren't indirect primary bases
631 // of some other base.
632 if (I->isVirtual() && !IndirectPrimaryBases.count(BaseDecl)) {
633 // Only lay out the base once.
634 if (!LaidOutVirtualBases.insert(BaseDecl))
635 continue;
636
John McCall4d9f1422011-02-15 22:21:29 +0000637 CharUnits vbaseOffset = Layout.getVBaseClassOffset(BaseDecl);
638 LayoutVirtualBase(BaseDecl, vbaseOffset);
Anders Carlssona459adb2010-11-28 19:18:44 +0000639 }
640
641 if (!BaseDecl->getNumVBases()) {
642 // This base isn't interesting since it doesn't have any virtual bases.
643 continue;
644 }
645
646 LayoutVirtualBases(BaseDecl, Layout);
647 }
648}
649
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000650void
651CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD,
652 const ASTRecordLayout &Layout) {
653 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
654
655 // Check if we need to add a vtable pointer.
656 if (RD->isDynamicClass()) {
657 if (!PrimaryBase) {
658 const llvm::Type *FunctionType =
659 llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()),
660 /*isVarArg=*/true);
661 const llvm::Type *VTableTy = FunctionType->getPointerTo();
662
John McCall4d9f1422011-02-15 22:21:29 +0000663 assert(NextFieldOffset.isZero() &&
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000664 "VTable pointer must come first!");
John McCall4d9f1422011-02-15 22:21:29 +0000665 AppendField(CharUnits::Zero(), VTableTy->getPointerTo());
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000666 } else {
Anders Carlsson7f95cd12010-11-24 23:12:57 +0000667 if (!Layout.isPrimaryBaseVirtual())
John McCall4d9f1422011-02-15 22:21:29 +0000668 LayoutNonVirtualBase(PrimaryBase, CharUnits::Zero());
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000669 else
John McCall4d9f1422011-02-15 22:21:29 +0000670 LayoutVirtualBase(PrimaryBase, CharUnits::Zero());
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000671 }
672 }
673
674 // Layout the non-virtual bases.
675 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
676 E = RD->bases_end(); I != E; ++I) {
677 if (I->isVirtual())
678 continue;
679
680 const CXXRecordDecl *BaseDecl =
681 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
682
683 // We've already laid out the primary base.
Anders Carlsson7f95cd12010-11-24 23:12:57 +0000684 if (BaseDecl == PrimaryBase && !Layout.isPrimaryBaseVirtual())
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000685 continue;
686
John McCall4d9f1422011-02-15 22:21:29 +0000687 LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffset(BaseDecl));
Anders Carlssond681a292009-12-16 17:27:20 +0000688 }
689}
690
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000691bool
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000692CGRecordLayoutBuilder::ComputeNonVirtualBaseType(const CXXRecordDecl *RD) {
693 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(RD);
694
Ken Dyckbec02852011-02-08 02:02:47 +0000695 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
696 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
John McCall4d9f1422011-02-15 22:21:29 +0000697 CharUnits AlignedNonVirtualTypeSize =
698 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000699
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000700 // First check if we can use the same fields as for the complete class.
John McCall4d9f1422011-02-15 22:21:29 +0000701 CharUnits RecordSize = Layout.getSize();
John McCall0217dfc22011-02-15 06:40:56 +0000702 if (AlignedNonVirtualTypeSize == RecordSize)
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000703 return true;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000704
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000705 // Check if we need padding.
John McCall4d9f1422011-02-15 22:21:29 +0000706 CharUnits AlignedNextFieldOffset =
707 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000708
John McCall0217dfc22011-02-15 06:40:56 +0000709 if (AlignedNextFieldOffset > AlignedNonVirtualTypeSize) {
710 assert(!Packed && "cannot layout even as packed struct");
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000711 return false; // Needs packing.
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000712 }
713
John McCall0217dfc22011-02-15 06:40:56 +0000714 bool needsPadding = (AlignedNonVirtualTypeSize != AlignedNextFieldOffset);
715 if (needsPadding) {
John McCall4d9f1422011-02-15 22:21:29 +0000716 CharUnits NumBytes = AlignedNonVirtualTypeSize - AlignedNextFieldOffset;
John McCall0217dfc22011-02-15 06:40:56 +0000717 FieldTypes.push_back(getByteArrayType(NumBytes));
718 }
719
720 BaseSubobjectType = llvm::StructType::get(Types.getLLVMContext(),
721 FieldTypes, Packed);
722
723 if (needsPadding) {
724 // Pull the padding back off.
725 FieldTypes.pop_back();
726 }
727
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000728 return true;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000729}
730
Anders Carlsson307846f2009-07-23 03:17:50 +0000731bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
732 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
John McCall4d9f1422011-02-15 22:21:29 +0000733 assert(!Alignment.isZero() && "Did not set alignment!");
Mike Stump11289f42009-09-09 15:08:12 +0000734
Anders Carlsson697f6592009-07-23 03:43:54 +0000735 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000736
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000737 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
738 if (RD)
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000739 LayoutNonVirtualBases(RD, Layout);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000740
Anders Carlsson307846f2009-07-23 03:17:50 +0000741 unsigned FieldNo = 0;
Fariborz Jahanian7b2b1ec2009-07-27 20:57:45 +0000742
Mike Stump11289f42009-09-09 15:08:12 +0000743 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson307846f2009-07-23 03:17:50 +0000744 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
745 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump11289f42009-09-09 15:08:12 +0000746 assert(!Packed &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000747 "Could not layout fields even with a packed LLVM struct!");
748 return false;
749 }
750 }
751
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000752 if (RD) {
Anders Carlssona459adb2010-11-28 19:18:44 +0000753 // We've laid out the non-virtual bases and the fields, now compute the
754 // non-virtual base field types.
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000755 if (!ComputeNonVirtualBaseType(RD)) {
756 assert(!Packed && "Could not layout even with a packed LLVM struct!");
757 return false;
758 }
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000759
Anders Carlssona459adb2010-11-28 19:18:44 +0000760 // And lay out the virtual bases.
761 RD->getIndirectPrimaryBases(IndirectPrimaryBases);
762 if (Layout.isPrimaryBaseVirtual())
763 IndirectPrimaryBases.insert(Layout.getPrimaryBase());
764 LayoutVirtualBases(RD, Layout);
765 }
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000766
Anders Carlsson307846f2009-07-23 03:17:50 +0000767 // Append tail padding if necessary.
Ken Dyck272b6fa2011-04-24 16:53:44 +0000768 AppendTailPadding(Layout.getSize());
Mike Stump11289f42009-09-09 15:08:12 +0000769
Anders Carlsson307846f2009-07-23 03:17:50 +0000770 return true;
771}
772
Ken Dyck272b6fa2011-04-24 16:53:44 +0000773void CGRecordLayoutBuilder::AppendTailPadding(CharUnits RecordSize) {
774 ResizeLastBaseFieldIfNecessary(RecordSize);
Mike Stump11289f42009-09-09 15:08:12 +0000775
Ken Dyck272b6fa2011-04-24 16:53:44 +0000776 assert(NextFieldOffset <= RecordSize && "Size mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000777
John McCall4d9f1422011-02-15 22:21:29 +0000778 CharUnits AlignedNextFieldOffset =
779 NextFieldOffset.RoundUpToAlignment(getAlignmentAsLLVMStruct());
Anders Carlsson220bf4f2009-12-08 01:24:23 +0000780
Ken Dyck272b6fa2011-04-24 16:53:44 +0000781 if (AlignedNextFieldOffset == RecordSize) {
Anders Carlsson220bf4f2009-12-08 01:24:23 +0000782 // We don't need any padding.
783 return;
784 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000785
Ken Dyck272b6fa2011-04-24 16:53:44 +0000786 CharUnits NumPadBytes = RecordSize - NextFieldOffset;
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000787 AppendBytes(NumPadBytes);
788}
789
John McCall4d9f1422011-02-15 22:21:29 +0000790void CGRecordLayoutBuilder::AppendField(CharUnits fieldOffset,
791 const llvm::Type *fieldType) {
792 CharUnits fieldSize =
793 CharUnits::fromQuantity(Types.getTargetData().getTypeAllocSize(fieldType));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000794
John McCall4d9f1422011-02-15 22:21:29 +0000795 FieldTypes.push_back(fieldType);
Anders Carlsson307846f2009-07-23 03:17:50 +0000796
John McCall4d9f1422011-02-15 22:21:29 +0000797 NextFieldOffset = fieldOffset + fieldSize;
Anders Carlsson307846f2009-07-23 03:17:50 +0000798 BitsAvailableInLastField = 0;
799}
800
John McCall4d9f1422011-02-15 22:21:29 +0000801void CGRecordLayoutBuilder::AppendPadding(CharUnits fieldOffset,
802 CharUnits fieldAlignment) {
803 assert(NextFieldOffset <= fieldOffset &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000804 "Incorrect field layout!");
Mike Stump11289f42009-09-09 15:08:12 +0000805
Anders Carlsson307846f2009-07-23 03:17:50 +0000806 // Round up the field offset to the alignment of the field type.
John McCall4d9f1422011-02-15 22:21:29 +0000807 CharUnits alignedNextFieldOffset =
808 NextFieldOffset.RoundUpToAlignment(fieldAlignment);
Anders Carlsson307846f2009-07-23 03:17:50 +0000809
John McCall4d9f1422011-02-15 22:21:29 +0000810 if (alignedNextFieldOffset < fieldOffset) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000811 // Even with alignment, the field offset is not at the right place,
812 // insert padding.
John McCall4d9f1422011-02-15 22:21:29 +0000813 CharUnits padding = fieldOffset - NextFieldOffset;
Anders Carlsson307846f2009-07-23 03:17:50 +0000814
John McCall4d9f1422011-02-15 22:21:29 +0000815 AppendBytes(padding);
Anders Carlsson307846f2009-07-23 03:17:50 +0000816 }
817}
818
Anders Carlssonfcaaa692011-04-17 21:56:13 +0000819bool CGRecordLayoutBuilder::ResizeLastBaseFieldIfNecessary(CharUnits offset) {
820 // Check if we have a base to resize.
821 if (!LastLaidOutBase.isValid())
822 return false;
823
824 // This offset does not overlap with the tail padding.
825 if (offset >= NextFieldOffset)
826 return false;
827
828 // Restore the field offset and append an i8 array instead.
829 FieldTypes.pop_back();
830 NextFieldOffset = LastLaidOutBase.Offset;
831 AppendBytes(LastLaidOutBase.NonVirtualSize);
832 LastLaidOutBase.invalidate();
833
834 return true;
835}
836
John McCall4d9f1422011-02-15 22:21:29 +0000837const llvm::Type *CGRecordLayoutBuilder::getByteArrayType(CharUnits numBytes) {
838 assert(!numBytes.isZero() && "Empty byte arrays aren't allowed.");
Mike Stump11289f42009-09-09 15:08:12 +0000839
Owen Anderson41a75022009-08-13 21:57:51 +0000840 const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
John McCall4d9f1422011-02-15 22:21:29 +0000841 if (numBytes > CharUnits::One())
842 Ty = llvm::ArrayType::get(Ty, numBytes.getQuantity());
Mike Stump11289f42009-09-09 15:08:12 +0000843
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000844 return Ty;
845}
846
John McCall4d9f1422011-02-15 22:21:29 +0000847void CGRecordLayoutBuilder::AppendBytes(CharUnits numBytes) {
848 if (numBytes.isZero())
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000849 return;
850
Anders Carlsson307846f2009-07-23 03:17:50 +0000851 // Append the padding field
John McCall4d9f1422011-02-15 22:21:29 +0000852 AppendField(NextFieldOffset, getByteArrayType(numBytes));
Anders Carlsson307846f2009-07-23 03:17:50 +0000853}
854
John McCall4d9f1422011-02-15 22:21:29 +0000855CharUnits CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlssond78fc892009-07-23 17:24:40 +0000856 if (Packed)
John McCall4d9f1422011-02-15 22:21:29 +0000857 return CharUnits::One();
Mike Stump11289f42009-09-09 15:08:12 +0000858
John McCall4d9f1422011-02-15 22:21:29 +0000859 return CharUnits::fromQuantity(Types.getTargetData().getABITypeAlignment(Ty));
Anders Carlsson307846f2009-07-23 03:17:50 +0000860}
861
John McCall4d9f1422011-02-15 22:21:29 +0000862CharUnits CGRecordLayoutBuilder::getAlignmentAsLLVMStruct() const {
Anders Carlssonacf877b2010-11-28 23:06:23 +0000863 if (Packed)
John McCall4d9f1422011-02-15 22:21:29 +0000864 return CharUnits::One();
Anders Carlssonacf877b2010-11-28 23:06:23 +0000865
John McCall4d9f1422011-02-15 22:21:29 +0000866 CharUnits maxAlignment = CharUnits::One();
Anders Carlssonacf877b2010-11-28 23:06:23 +0000867 for (size_t i = 0; i != FieldTypes.size(); ++i)
John McCall4d9f1422011-02-15 22:21:29 +0000868 maxAlignment = std::max(maxAlignment, getTypeAlignment(FieldTypes[i]));
Anders Carlssonacf877b2010-11-28 23:06:23 +0000869
John McCall4d9f1422011-02-15 22:21:29 +0000870 return maxAlignment;
Anders Carlssonacf877b2010-11-28 23:06:23 +0000871}
872
John McCall0217dfc22011-02-15 06:40:56 +0000873/// Merge in whether a field of the given type is zero-initializable.
John McCall614dbdc2010-08-22 21:01:12 +0000874void CGRecordLayoutBuilder::CheckZeroInitializable(QualType T) {
Anders Carlssond606de72009-08-23 01:25:01 +0000875 // This record already contains a member pointer.
John McCall0217dfc22011-02-15 06:40:56 +0000876 if (!IsZeroInitializableAsBase)
Anders Carlssond606de72009-08-23 01:25:01 +0000877 return;
Mike Stump11289f42009-09-09 15:08:12 +0000878
Anders Carlssond606de72009-08-23 01:25:01 +0000879 // Can only have member pointers if we're compiling C++.
880 if (!Types.getContext().getLangOptions().CPlusPlus)
881 return;
Mike Stump11289f42009-09-09 15:08:12 +0000882
John McCall0217dfc22011-02-15 06:40:56 +0000883 const Type *elementType = T->getBaseElementTypeUnsafe();
Mike Stump11289f42009-09-09 15:08:12 +0000884
John McCall0217dfc22011-02-15 06:40:56 +0000885 if (const MemberPointerType *MPT = elementType->getAs<MemberPointerType>()) {
John McCall614dbdc2010-08-22 21:01:12 +0000886 if (!Types.getCXXABI().isZeroInitializable(MPT))
John McCall0217dfc22011-02-15 06:40:56 +0000887 IsZeroInitializable = IsZeroInitializableAsBase = false;
888 } else if (const RecordType *RT = elementType->getAs<RecordType>()) {
Anders Carlssone8bfe412010-02-02 05:17:25 +0000889 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
John McCall0217dfc22011-02-15 06:40:56 +0000890 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
891 if (!Layout.isZeroInitializable())
892 IsZeroInitializable = IsZeroInitializableAsBase = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000893 }
Anders Carlssond606de72009-08-23 01:25:01 +0000894}
895
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000896CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) {
897 CGRecordLayoutBuilder Builder(*this);
Mike Stump11289f42009-09-09 15:08:12 +0000898
Anders Carlsson307846f2009-07-23 03:17:50 +0000899 Builder.Layout(D);
Anders Carlssone1d5ca52009-07-24 15:20:52 +0000900
Anders Carlsson36e2fa82010-11-24 19:37:16 +0000901 const llvm::StructType *Ty = llvm::StructType::get(getLLVMContext(),
902 Builder.FieldTypes,
903 Builder.Packed);
Mike Stump11289f42009-09-09 15:08:12 +0000904
John McCall0217dfc22011-02-15 06:40:56 +0000905 // If we're in C++, compute the base subobject type.
Anders Carlsson36e2fa82010-11-24 19:37:16 +0000906 const llvm::StructType *BaseTy = 0;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000907 if (isa<CXXRecordDecl>(D)) {
John McCall0217dfc22011-02-15 06:40:56 +0000908 BaseTy = Builder.BaseSubobjectType;
909 if (!BaseTy) BaseTy = Ty;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000910 }
911
Daniel Dunbar034299e2010-03-31 01:09:11 +0000912 CGRecordLayout *RL =
John McCall0217dfc22011-02-15 06:40:56 +0000913 new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable,
914 Builder.IsZeroInitializableAsBase);
Daniel Dunbar034299e2010-03-31 01:09:11 +0000915
John McCall0217dfc22011-02-15 06:40:56 +0000916 RL->NonVirtualBases.swap(Builder.NonVirtualBases);
917 RL->CompleteObjectVirtualBases.swap(Builder.VirtualBases);
Anders Carlsson061ca522010-05-18 05:22:06 +0000918
Anders Carlsson307846f2009-07-23 03:17:50 +0000919 // Add all the field numbers.
John McCall0217dfc22011-02-15 06:40:56 +0000920 RL->FieldInfo.swap(Builder.Fields);
Anders Carlsson307846f2009-07-23 03:17:50 +0000921
922 // Add bitfield info.
John McCall0217dfc22011-02-15 06:40:56 +0000923 RL->BitFields.swap(Builder.BitFields);
Mike Stump11289f42009-09-09 15:08:12 +0000924
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000925 // Dump the layout, if requested.
Daniel Dunbarb935b932010-04-13 20:58:55 +0000926 if (getContext().getLangOptions().DumpRecordLayouts) {
Daniel Dunbarccabe482010-04-19 20:44:53 +0000927 llvm::errs() << "\n*** Dumping IRgen Record Layout\n";
Daniel Dunbarb935b932010-04-13 20:58:55 +0000928 llvm::errs() << "Record: ";
929 D->dump();
930 llvm::errs() << "\nLayout: ";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000931 RL->dump();
Daniel Dunbarb935b932010-04-13 20:58:55 +0000932 }
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000933
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000934#ifndef NDEBUG
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000935 // Verify that the computed LLVM struct size matches the AST layout size.
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000936 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
937
Ken Dyckb0fcc592011-02-11 01:54:29 +0000938 uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize());
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000939 assert(TypeSizeInBits == getTargetData().getTypeAllocSizeInBits(Ty) &&
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000940 "Type size mismatch!");
941
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000942 if (BaseTy) {
Ken Dyckbec02852011-02-08 02:02:47 +0000943 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
944 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
945 CharUnits AlignedNonVirtualTypeSize =
946 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
947
948 uint64_t AlignedNonVirtualTypeSizeInBits =
Ken Dyckb0fcc592011-02-11 01:54:29 +0000949 getContext().toBits(AlignedNonVirtualTypeSize);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000950
951 assert(AlignedNonVirtualTypeSizeInBits ==
952 getTargetData().getTypeAllocSizeInBits(BaseTy) &&
953 "Type size mismatch!");
954 }
955
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000956 // Verify that the LLVM and AST field offsets agree.
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000957 const llvm::StructType *ST =
958 dyn_cast<llvm::StructType>(RL->getLLVMType());
959 const llvm::StructLayout *SL = getTargetData().getStructLayout(ST);
960
961 const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
962 RecordDecl::field_iterator it = D->field_begin();
963 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
964 const FieldDecl *FD = *it;
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000965
966 // For non-bit-fields, just check that the LLVM struct offset matches the
967 // AST offset.
968 if (!FD->isBitField()) {
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000969 unsigned FieldNo = RL->getLLVMFieldNo(FD);
970 assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
971 "Invalid field offset!");
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000972 continue;
973 }
974
975 // Ignore unnamed bit-fields.
976 if (!FD->getDeclName())
977 continue;
978
979 const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
980 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
981 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
982
983 // Verify that every component access is within the structure.
984 uint64_t FieldOffset = SL->getElementOffsetInBits(AI.FieldIndex);
John McCall8a3c5552011-02-26 08:41:59 +0000985 uint64_t AccessBitOffset = FieldOffset +
Ken Dyckf76759c2011-04-24 10:04:59 +0000986 getContext().toBits(AI.FieldByteOffset);
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000987 assert(AccessBitOffset + AI.AccessWidth <= TypeSizeInBits &&
988 "Invalid bit-field access (out of range)!");
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000989 }
990 }
991#endif
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000992
Daniel Dunbar034299e2010-03-31 01:09:11 +0000993 return RL;
Anders Carlsson307846f2009-07-23 03:17:50 +0000994}
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000995
996void CGRecordLayout::print(llvm::raw_ostream &OS) const {
997 OS << "<CGRecordLayout\n";
John McCall0217dfc22011-02-15 06:40:56 +0000998 OS << " LLVMType:" << *CompleteObjectType << "\n";
999 if (BaseSubobjectType)
1000 OS << " NonVirtualBaseLLVMType:" << *BaseSubobjectType << "\n";
John McCall614dbdc2010-08-22 21:01:12 +00001001 OS << " IsZeroInitializable:" << IsZeroInitializable << "\n";
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001002 OS << " BitFields:[\n";
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001003
1004 // Print bit-field infos in declaration order.
1005 std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001006 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
1007 it = BitFields.begin(), ie = BitFields.end();
1008 it != ie; ++it) {
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001009 const RecordDecl *RD = it->first->getParent();
1010 unsigned Index = 0;
1011 for (RecordDecl::field_iterator
1012 it2 = RD->field_begin(); *it2 != it->first; ++it2)
1013 ++Index;
1014 BFIs.push_back(std::make_pair(Index, &it->second));
1015 }
1016 llvm::array_pod_sort(BFIs.begin(), BFIs.end());
1017 for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
Daniel Dunbarb935b932010-04-13 20:58:55 +00001018 OS.indent(4);
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001019 BFIs[i].second->print(OS);
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001020 OS << "\n";
1021 }
Daniel Dunbarb6f4b052010-04-22 02:35:36 +00001022
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001023 OS << "]>\n";
1024}
1025
1026void CGRecordLayout::dump() const {
1027 print(llvm::errs());
1028}
1029
1030void CGBitFieldInfo::print(llvm::raw_ostream &OS) const {
1031 OS << "<CGBitFieldInfo";
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001032 OS << " Size:" << Size;
Daniel Dunbarb935b932010-04-13 20:58:55 +00001033 OS << " IsSigned:" << IsSigned << "\n";
1034
1035 OS.indent(4 + strlen("<CGBitFieldInfo"));
1036 OS << " NumComponents:" << getNumComponents();
1037 OS << " Components: [";
1038 if (getNumComponents()) {
1039 OS << "\n";
1040 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
1041 const AccessInfo &AI = getComponent(i);
1042 OS.indent(8);
1043 OS << "<AccessInfo"
1044 << " FieldIndex:" << AI.FieldIndex
Ken Dyckf76759c2011-04-24 10:04:59 +00001045 << " FieldByteOffset:" << AI.FieldByteOffset.getQuantity()
Daniel Dunbarb935b932010-04-13 20:58:55 +00001046 << " FieldBitStart:" << AI.FieldBitStart
1047 << " AccessWidth:" << AI.AccessWidth << "\n";
1048 OS.indent(8 + strlen("<AccessInfo"));
Ken Dyck27337a82011-04-24 10:13:17 +00001049 OS << " AccessAlignment:" << AI.AccessAlignment.getQuantity()
Daniel Dunbarb935b932010-04-13 20:58:55 +00001050 << " TargetBitOffset:" << AI.TargetBitOffset
1051 << " TargetBitWidth:" << AI.TargetBitWidth
1052 << ">\n";
1053 }
1054 OS.indent(4);
1055 }
1056 OS << "]>";
Daniel Dunbarb97bff92010-04-12 18:14:18 +00001057}
1058
1059void CGBitFieldInfo::dump() const {
1060 print(llvm::errs());
1061}