blob: 77a319fa3ab1cd08d8366dddc4cf31c3b84db490 [file] [log] [blame]
Daniel Dunbar270e2032010-03-31 00:11:27 +00001//===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder ----*- C++ -*-===//
Anders Carlsson45372a62009-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 Dunbar270e2032010-03-31 00:11:27 +000010// Builder implementation for CGRecordLayout objects.
Anders Carlsson45372a62009-07-23 03:17:50 +000011//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar2924ade2010-03-30 22:26:10 +000014#include "CGRecordLayout.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/Attr.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/Expr.h"
19#include "clang/AST/RecordLayout.h"
20#include "CodeGenTypes.h"
John McCallf16aa102010-08-22 21:01:12 +000021#include "CGCXXABI.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000022#include "llvm/DerivedTypes.h"
Daniel Dunbar93c62962010-04-12 18:14:18 +000023#include "llvm/Type.h"
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +000024#include "llvm/Support/Debug.h"
Daniel Dunbar93c62962010-04-12 18:14:18 +000025#include "llvm/Support/raw_ostream.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000026#include "llvm/Target/TargetData.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000027using namespace clang;
28using namespace CodeGen;
29
Daniel Dunbar270e2032010-03-31 00:11:27 +000030namespace clang {
31namespace CodeGen {
32
33class CGRecordLayoutBuilder {
34public:
35 /// FieldTypes - Holds the LLVM types that the struct is created from.
36 std::vector<const llvm::Type *> FieldTypes;
37
38 /// LLVMFieldInfo - Holds a field and its corresponding LLVM field number.
39 typedef std::pair<const FieldDecl *, unsigned> LLVMFieldInfo;
40 llvm::SmallVector<LLVMFieldInfo, 16> LLVMFields;
41
42 /// LLVMBitFieldInfo - Holds location and size information about a bit field.
Daniel Dunbarc7a984a2010-04-06 01:07:41 +000043 typedef std::pair<const FieldDecl *, CGBitFieldInfo> LLVMBitFieldInfo;
Daniel Dunbar270e2032010-03-31 00:11:27 +000044 llvm::SmallVector<LLVMBitFieldInfo, 16> LLVMBitFields;
45
Anders Carlssonc6772ce2010-05-18 05:22:06 +000046 typedef std::pair<const CXXRecordDecl *, unsigned> LLVMBaseInfo;
47 llvm::SmallVector<LLVMBaseInfo, 16> LLVMNonVirtualBases;
48
John McCallf16aa102010-08-22 21:01:12 +000049 /// IsZeroInitializable - Whether this struct can be C++
50 /// zero-initialized with an LLVM zeroinitializer.
51 bool IsZeroInitializable;
Daniel Dunbar270e2032010-03-31 00:11:27 +000052
53 /// Packed - Whether the resulting LLVM struct will be packed or not.
54 bool Packed;
55
56private:
57 CodeGenTypes &Types;
58
59 /// Alignment - Contains the alignment of the RecordDecl.
60 //
61 // FIXME: This is not needed and should be removed.
62 unsigned Alignment;
63
64 /// AlignmentAsLLVMStruct - Will contain the maximum alignment of all the
65 /// LLVM types.
66 unsigned AlignmentAsLLVMStruct;
67
68 /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field,
69 /// this will have the number of bits still available in the field.
70 char BitsAvailableInLastField;
71
72 /// NextFieldOffsetInBytes - Holds the next field offset in bytes.
73 uint64_t NextFieldOffsetInBytes;
74
Anders Carlsson86664462010-04-17 20:49:27 +000075 /// LayoutUnionField - Will layout a field in an union and return the type
76 /// that the field will have.
77 const llvm::Type *LayoutUnionField(const FieldDecl *Field,
78 const ASTRecordLayout &Layout);
79
Daniel Dunbar270e2032010-03-31 00:11:27 +000080 /// LayoutUnion - Will layout a union RecordDecl.
81 void LayoutUnion(const RecordDecl *D);
82
83 /// LayoutField - try to layout all fields in the record decl.
84 /// Returns false if the operation failed because the struct is not packed.
85 bool LayoutFields(const RecordDecl *D);
86
Anders Carlsson15ddfdc2010-05-18 05:12:20 +000087 /// LayoutNonVirtualBase - layout a single non-virtual base.
88 void LayoutNonVirtualBase(const CXXRecordDecl *BaseDecl,
89 uint64_t BaseOffset);
90
91 /// LayoutNonVirtualBases - layout the non-virtual bases of a record decl.
92 void LayoutNonVirtualBases(const CXXRecordDecl *RD,
93 const ASTRecordLayout &Layout);
Daniel Dunbar270e2032010-03-31 00:11:27 +000094
95 /// LayoutField - layout a single field. Returns false if the operation failed
96 /// because the current struct is not packed.
97 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
98
99 /// LayoutBitField - layout a single bit field.
100 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
101
102 /// AppendField - Appends a field with the given offset and type.
103 void AppendField(uint64_t FieldOffsetInBytes, const llvm::Type *FieldTy);
104
Daniel Dunbar270e2032010-03-31 00:11:27 +0000105 /// AppendPadding - Appends enough padding bytes so that the total
106 /// struct size is a multiple of the field alignment.
107 void AppendPadding(uint64_t FieldOffsetInBytes, unsigned FieldAlignment);
108
109 /// AppendBytes - Append a given number of bytes to the record.
110 void AppendBytes(uint64_t NumBytes);
111
112 /// AppendTailPadding - Append enough tail padding so that the type will have
113 /// the passed size.
114 void AppendTailPadding(uint64_t RecordSize);
115
116 unsigned getTypeAlignment(const llvm::Type *Ty) const;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000117
John McCallf16aa102010-08-22 21:01:12 +0000118 /// CheckZeroInitializable - Check if the given type contains a pointer
Daniel Dunbar270e2032010-03-31 00:11:27 +0000119 /// to data member.
John McCallf16aa102010-08-22 21:01:12 +0000120 void CheckZeroInitializable(QualType T);
121 void CheckZeroInitializable(const CXXRecordDecl *RD);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000122
123public:
124 CGRecordLayoutBuilder(CodeGenTypes &Types)
John McCallf16aa102010-08-22 21:01:12 +0000125 : IsZeroInitializable(true), Packed(false), Types(Types),
Daniel Dunbar270e2032010-03-31 00:11:27 +0000126 Alignment(0), AlignmentAsLLVMStruct(1),
127 BitsAvailableInLastField(0), NextFieldOffsetInBytes(0) { }
128
129 /// Layout - Will layout a RecordDecl.
130 void Layout(const RecordDecl *D);
131};
132
133}
134}
135
Anders Carlsson45372a62009-07-23 03:17:50 +0000136void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
Anders Carlssona5dd7222009-08-08 19:38:24 +0000137 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment() / 8;
Anders Carlssond0eb3b92009-09-02 17:51:33 +0000138 Packed = D->hasAttr<PackedAttr>();
Anders Carlssona5dd7222009-08-08 19:38:24 +0000139
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000140 if (D->isUnion()) {
141 LayoutUnion(D);
142 return;
143 }
Anders Carlssona860e752009-08-08 18:23:56 +0000144
Anders Carlsson45372a62009-07-23 03:17:50 +0000145 if (LayoutFields(D))
146 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Anders Carlsson45372a62009-07-23 03:17:50 +0000148 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000149 Packed = true;
Anders Carlsson45372a62009-07-23 03:17:50 +0000150 AlignmentAsLLVMStruct = 1;
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000151 NextFieldOffsetInBytes = 0;
Anders Carlsson45372a62009-07-23 03:17:50 +0000152 FieldTypes.clear();
Anders Carlsson45372a62009-07-23 03:17:50 +0000153 LLVMFields.clear();
154 LLVMBitFields.clear();
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000155 LLVMNonVirtualBases.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Anders Carlsson45372a62009-07-23 03:17:50 +0000157 LayoutFields(D);
158}
159
Daniel Dunbare7a80bd2010-09-02 23:53:28 +0000160CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
161 const FieldDecl *FD,
162 uint64_t FieldOffset,
163 uint64_t FieldSize,
164 uint64_t ContainingTypeSizeInBits,
165 unsigned ContainingTypeAlign) {
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000166 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(FD->getType());
Daniel Dunbarab970f92010-04-13 20:58:55 +0000167 uint64_t TypeSizeInBytes = Types.getTargetData().getTypeAllocSize(Ty);
168 uint64_t TypeSizeInBits = TypeSizeInBytes * 8;
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000169
170 bool IsSigned = FD->getType()->isSignedIntegerType();
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000171
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000172 if (FieldSize > TypeSizeInBits) {
Anders Carlsson6ba38152010-04-17 22:54:57 +0000173 // We have a wide bit-field. The extra bits are only used for padding, so
174 // if we have a bitfield of type T, with size N:
175 //
176 // T t : N;
177 //
178 // We can just assume that it's:
179 //
180 // T t : sizeof(T);
181 //
182 FieldSize = TypeSizeInBits;
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000183 }
184
Daniel Dunbare1467a42010-04-22 02:35:46 +0000185 // Compute the access components. The policy we use is to start by attempting
186 // to access using the width of the bit-field type itself and to always access
187 // at aligned indices of that type. If such an access would fail because it
188 // extends past the bound of the type, then we reduce size to the next smaller
189 // power of two and retry. The current algorithm assumes pow2 sized types,
190 // although this is easy to fix.
191 //
192 // FIXME: This algorithm is wrong on big-endian systems, I think.
193 assert(llvm::isPowerOf2_32(TypeSizeInBits) && "Unexpected type size!");
194 CGBitFieldInfo::AccessInfo Components[3];
195 unsigned NumComponents = 0;
196 unsigned AccessedTargetBits = 0; // The tumber of target bits accessed.
197 unsigned AccessWidth = TypeSizeInBits; // The current access width to attempt.
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000198
Daniel Dunbare1467a42010-04-22 02:35:46 +0000199 // Round down from the field offset to find the first access position that is
200 // at an aligned offset of the initial access type.
Daniel Dunbar52968a12010-04-22 15:22:33 +0000201 uint64_t AccessStart = FieldOffset - (FieldOffset % AccessWidth);
202
203 // Adjust initial access size to fit within record.
204 while (AccessWidth > 8 &&
205 AccessStart + AccessWidth > ContainingTypeSizeInBits) {
206 AccessWidth >>= 1;
207 AccessStart = FieldOffset - (FieldOffset % AccessWidth);
208 }
Daniel Dunbar2df25692010-04-15 05:09:32 +0000209
Daniel Dunbare1467a42010-04-22 02:35:46 +0000210 while (AccessedTargetBits < FieldSize) {
211 // Check that we can access using a type of this size, without reading off
212 // the end of the structure. This can occur with packed structures and
213 // -fno-bitfield-type-align, for example.
214 if (AccessStart + AccessWidth > ContainingTypeSizeInBits) {
215 // If so, reduce access size to the next smaller power-of-two and retry.
216 AccessWidth >>= 1;
217 assert(AccessWidth >= 8 && "Cannot access under byte size!");
218 continue;
219 }
Daniel Dunbarab970f92010-04-13 20:58:55 +0000220
Daniel Dunbare1467a42010-04-22 02:35:46 +0000221 // Otherwise, add an access component.
Daniel Dunbarab970f92010-04-13 20:58:55 +0000222
Daniel Dunbare1467a42010-04-22 02:35:46 +0000223 // First, compute the bits inside this access which are part of the
224 // target. We are reading bits [AccessStart, AccessStart + AccessWidth); the
225 // intersection with [FieldOffset, FieldOffset + FieldSize) gives the bits
226 // in the target that we are reading.
Daniel Dunbar52968a12010-04-22 15:22:33 +0000227 assert(FieldOffset < AccessStart + AccessWidth && "Invalid access start!");
228 assert(AccessStart < FieldOffset + FieldSize && "Invalid access start!");
Daniel Dunbare1467a42010-04-22 02:35:46 +0000229 uint64_t AccessBitsInFieldStart = std::max(AccessStart, FieldOffset);
230 uint64_t AccessBitsInFieldSize =
Daniel Dunbar52968a12010-04-22 15:22:33 +0000231 std::min(AccessWidth + AccessStart,
232 FieldOffset + FieldSize) - AccessBitsInFieldStart;
Daniel Dunbar4651efb2010-04-22 14:56:10 +0000233
Daniel Dunbare1467a42010-04-22 02:35:46 +0000234 assert(NumComponents < 3 && "Unexpected number of components!");
235 CGBitFieldInfo::AccessInfo &AI = Components[NumComponents++];
236 AI.FieldIndex = 0;
237 // FIXME: We still follow the old access pattern of only using the field
238 // byte offset. We should switch this once we fix the struct layout to be
239 // pretty.
240 AI.FieldByteOffset = AccessStart / 8;
241 AI.FieldBitStart = AccessBitsInFieldStart - AccessStart;
242 AI.AccessWidth = AccessWidth;
Daniel Dunbar89da8742010-04-22 03:17:04 +0000243 AI.AccessAlignment = llvm::MinAlign(ContainingTypeAlign, AccessStart) / 8;
Daniel Dunbare1467a42010-04-22 02:35:46 +0000244 AI.TargetBitOffset = AccessedTargetBits;
245 AI.TargetBitWidth = AccessBitsInFieldSize;
246
247 AccessStart += AccessWidth;
248 AccessedTargetBits += AI.TargetBitWidth;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000249 }
250
Daniel Dunbare1467a42010-04-22 02:35:46 +0000251 assert(AccessedTargetBits == FieldSize && "Invalid bit-field access!");
Daniel Dunbar2df25692010-04-15 05:09:32 +0000252 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000253}
254
Daniel Dunbare7a80bd2010-09-02 23:53:28 +0000255CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
256 const FieldDecl *FD,
257 uint64_t FieldOffset,
258 uint64_t FieldSize) {
259 const RecordDecl *RD = FD->getParent();
260 const ASTRecordLayout &RL = Types.getContext().getASTRecordLayout(RD);
261 uint64_t ContainingTypeSizeInBits = RL.getSize();
262 unsigned ContainingTypeAlign = RL.getAlignment();
263
264 return MakeInfo(Types, FD, FieldOffset, FieldSize, ContainingTypeSizeInBits,
265 ContainingTypeAlign);
266}
267
Anders Carlsson45372a62009-07-23 03:17:50 +0000268void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
269 uint64_t FieldOffset) {
Mike Stump1eb44332009-09-09 15:08:12 +0000270 uint64_t FieldSize =
Anders Carlsson45372a62009-07-23 03:17:50 +0000271 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Anders Carlsson45372a62009-07-23 03:17:50 +0000273 if (FieldSize == 0)
274 return;
275
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000276 uint64_t NextFieldOffset = NextFieldOffsetInBytes * 8;
Anders Carlsson45372a62009-07-23 03:17:50 +0000277 unsigned NumBytesToAppend;
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Anders Carlsson45372a62009-07-23 03:17:50 +0000279 if (FieldOffset < NextFieldOffset) {
280 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000281 assert(NextFieldOffsetInBytes && "Must have laid out at least one byte!");
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Anders Carlsson45372a62009-07-23 03:17:50 +0000283 // The bitfield begins in the previous bit-field.
Mike Stump1eb44332009-09-09 15:08:12 +0000284 NumBytesToAppend =
Anders Carlsson45372a62009-07-23 03:17:50 +0000285 llvm::RoundUpToAlignment(FieldSize - BitsAvailableInLastField, 8) / 8;
286 } else {
287 assert(FieldOffset % 8 == 0 && "Field offset not aligned correctly");
288
289 // Append padding if necessary.
290 AppendBytes((FieldOffset - NextFieldOffset) / 8);
Mike Stump1eb44332009-09-09 15:08:12 +0000291
292 NumBytesToAppend =
Anders Carlsson45372a62009-07-23 03:17:50 +0000293 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Anders Carlsson45372a62009-07-23 03:17:50 +0000295 assert(NumBytesToAppend && "No bytes to append!");
296 }
297
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000298 // Add the bit field info.
299 LLVMBitFields.push_back(
Daniel Dunbare7a80bd2010-09-02 23:53:28 +0000300 LLVMBitFieldInfo(D, CGBitFieldInfo::MakeInfo(Types, D, FieldOffset,
301 FieldSize)));
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Anders Carlsson45372a62009-07-23 03:17:50 +0000303 AppendBytes(NumBytesToAppend);
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Mike Stump1eb44332009-09-09 15:08:12 +0000305 BitsAvailableInLastField =
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000306 NextFieldOffsetInBytes * 8 - (FieldOffset + FieldSize);
Anders Carlsson45372a62009-07-23 03:17:50 +0000307}
308
309bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
310 uint64_t FieldOffset) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000311 // If the field is packed, then we need a packed struct.
Anders Carlssona860e752009-08-08 18:23:56 +0000312 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson45372a62009-07-23 03:17:50 +0000313 return false;
314
315 if (D->isBitField()) {
316 // We must use packed structs for unnamed bit fields since they
317 // don't affect the struct alignment.
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000318 if (!Packed && !D->getDeclName())
Anders Carlsson45372a62009-07-23 03:17:50 +0000319 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Anders Carlsson45372a62009-07-23 03:17:50 +0000321 LayoutBitField(D, FieldOffset);
322 return true;
323 }
Mike Stump1eb44332009-09-09 15:08:12 +0000324
John McCallf16aa102010-08-22 21:01:12 +0000325 CheckZeroInitializable(D->getType());
Daniel Dunbar270e2032010-03-31 00:11:27 +0000326
Anders Carlsson45372a62009-07-23 03:17:50 +0000327 assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!");
Anders Carlsson45372a62009-07-23 03:17:50 +0000328 uint64_t FieldOffsetInBytes = FieldOffset / 8;
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000330 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
331 unsigned TypeAlignment = getTypeAlignment(Ty);
332
Anders Carlssona5dd7222009-08-08 19:38:24 +0000333 // If the type alignment is larger then the struct alignment, we must use
334 // a packed struct.
335 if (TypeAlignment > Alignment) {
336 assert(!Packed && "Alignment is wrong even with packed struct!");
337 return false;
338 }
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Anders Carlssona5dd7222009-08-08 19:38:24 +0000340 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
341 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
Daniel Dunbar8a2c92c2010-05-27 01:12:46 +0000342 if (const MaxFieldAlignmentAttr *MFAA =
343 RD->getAttr<MaxFieldAlignmentAttr>()) {
344 if (MFAA->getAlignment() != TypeAlignment * 8 && !Packed)
Anders Carlssona5dd7222009-08-08 19:38:24 +0000345 return false;
346 }
347 }
348
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000349 // Round up the field offset to the alignment of the field type.
Mike Stump1eb44332009-09-09 15:08:12 +0000350 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000351 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, TypeAlignment);
352
353 if (FieldOffsetInBytes < AlignedNextFieldOffsetInBytes) {
354 assert(!Packed && "Could not place field even with packed struct!");
355 return false;
356 }
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000358 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
359 // Even with alignment, the field offset is not at the right place,
360 // insert padding.
361 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000363 AppendBytes(PaddingInBytes);
364 }
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Anders Carlsson45372a62009-07-23 03:17:50 +0000366 // Now append the field.
367 LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size()));
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000368 AppendField(FieldOffsetInBytes, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Anders Carlsson45372a62009-07-23 03:17:50 +0000370 return true;
371}
372
Anders Carlsson86664462010-04-17 20:49:27 +0000373const llvm::Type *
374CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
375 const ASTRecordLayout &Layout) {
376 if (Field->isBitField()) {
377 uint64_t FieldSize =
378 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
379
380 // Ignore zero sized bit fields.
381 if (FieldSize == 0)
382 return 0;
383
Daniel Dunbar8ab78a72010-04-20 17:52:30 +0000384 const llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
385 unsigned NumBytesToAppend =
386 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Anders Carlssond62328e2010-04-17 21:04:52 +0000387
Daniel Dunbar8ab78a72010-04-20 17:52:30 +0000388 if (NumBytesToAppend > 1)
389 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend);
Anders Carlssond62328e2010-04-17 21:04:52 +0000390
Anders Carlsson86664462010-04-17 20:49:27 +0000391 // Add the bit field info.
392 LLVMBitFields.push_back(
Daniel Dunbare7a80bd2010-09-02 23:53:28 +0000393 LLVMBitFieldInfo(Field, CGBitFieldInfo::MakeInfo(Types, Field,
394 0, FieldSize)));
Anders Carlssond62328e2010-04-17 21:04:52 +0000395 return FieldTy;
Anders Carlsson86664462010-04-17 20:49:27 +0000396 }
Daniel Dunbar8ab78a72010-04-20 17:52:30 +0000397
Anders Carlsson86664462010-04-17 20:49:27 +0000398 // This is a regular union field.
399 LLVMFields.push_back(LLVMFieldInfo(Field, 0));
400 return Types.ConvertTypeForMemRecursive(Field->getType());
401}
402
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000403void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
404 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000406 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000408 const llvm::Type *Ty = 0;
409 uint64_t Size = 0;
410 unsigned Align = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000412 bool HasOnlyZeroSizedBitFields = true;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000413
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000414 unsigned FieldNo = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000415 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000416 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
Mike Stump1eb44332009-09-09 15:08:12 +0000417 assert(Layout.getFieldOffset(FieldNo) == 0 &&
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000418 "Union field offset did not start at the beginning of record!");
Anders Carlsson86664462010-04-17 20:49:27 +0000419 const llvm::Type *FieldTy = LayoutUnionField(*Field, Layout);
Anders Carlsson2cc8f172009-07-23 04:00:39 +0000420
Anders Carlsson86664462010-04-17 20:49:27 +0000421 if (!FieldTy)
422 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000424 HasOnlyZeroSizedBitFields = false;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000425
Anders Carlsson177d4d82009-07-23 21:52:03 +0000426 unsigned FieldAlign = Types.getTargetData().getABITypeAlignment(FieldTy);
427 uint64_t FieldSize = Types.getTargetData().getTypeAllocSize(FieldTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000429 if (FieldAlign < Align)
430 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000432 if (FieldAlign > Align || FieldSize > Size) {
433 Ty = FieldTy;
434 Align = FieldAlign;
435 Size = FieldSize;
436 }
437 }
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000439 // Now add our field.
Anders Carlsson36620002009-09-03 22:56:02 +0000440 if (Ty) {
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000441 AppendField(0, Ty);
Anders Carlsson36620002009-09-03 22:56:02 +0000442
443 if (getTypeAlignment(Ty) > Layout.getAlignment() / 8) {
444 // We need a packed struct.
445 Packed = true;
446 Align = 1;
447 }
448 }
Fariborz Jahaniane5041702009-11-06 20:47:40 +0000449 if (!Align) {
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000450 assert(HasOnlyZeroSizedBitFields &&
451 "0-align record did not have all zero-sized bit-fields!");
Fariborz Jahaniane5041702009-11-06 20:47:40 +0000452 Align = 1;
453 }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000454
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000455 // Append tail padding.
456 if (Layout.getSize() / 8 > Size)
457 AppendPadding(Layout.getSize() / 8, Align);
458}
459
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000460void CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *BaseDecl,
461 uint64_t BaseOffset) {
462 const ASTRecordLayout &Layout =
463 Types.getContext().getASTRecordLayout(BaseDecl);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000464
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000465 uint64_t NonVirtualSize = Layout.getNonVirtualSize();
466
467 if (BaseDecl->isEmpty()) {
468 // FIXME: Lay out empty bases.
469 return;
470 }
471
John McCallf16aa102010-08-22 21:01:12 +0000472 CheckZeroInitializable(BaseDecl);
Anders Carlssona83fb4b2010-05-18 16:51:41 +0000473
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000474 // FIXME: Actually use a better type than [sizeof(BaseDecl) x i8] when we can.
475 AppendPadding(BaseOffset / 8, 1);
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000476
477 // Append the base field.
478 LLVMNonVirtualBases.push_back(LLVMBaseInfo(BaseDecl, FieldTypes.size()));
479
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000480 AppendBytes(NonVirtualSize / 8);
481}
482
483void
484CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD,
485 const ASTRecordLayout &Layout) {
486 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
487
488 // Check if we need to add a vtable pointer.
489 if (RD->isDynamicClass()) {
490 if (!PrimaryBase) {
491 const llvm::Type *FunctionType =
492 llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()),
493 /*isVarArg=*/true);
494 const llvm::Type *VTableTy = FunctionType->getPointerTo();
495
496 assert(NextFieldOffsetInBytes == 0 &&
497 "VTable pointer must come first!");
498 AppendField(NextFieldOffsetInBytes, VTableTy->getPointerTo());
499 } else {
500 // FIXME: Handle a virtual primary base.
501 if (!Layout.getPrimaryBaseWasVirtual())
502 LayoutNonVirtualBase(PrimaryBase, 0);
503 }
504 }
505
506 // Layout the non-virtual bases.
507 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
508 E = RD->bases_end(); I != E; ++I) {
509 if (I->isVirtual())
510 continue;
511
512 const CXXRecordDecl *BaseDecl =
513 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
514
515 // We've already laid out the primary base.
516 if (BaseDecl == PrimaryBase && !Layout.getPrimaryBaseWasVirtual())
517 continue;
518
519 LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffset(BaseDecl));
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000520 }
521}
522
Anders Carlsson45372a62009-07-23 03:17:50 +0000523bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
524 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
Anders Carlssona5dd7222009-08-08 19:38:24 +0000525 assert(Alignment && "Did not set alignment!");
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000527 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000529 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D))
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000530 LayoutNonVirtualBases(RD, Layout);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000531
Anders Carlsson45372a62009-07-23 03:17:50 +0000532 unsigned FieldNo = 0;
Fariborz Jahaniancad86652009-07-27 20:57:45 +0000533
Mike Stump1eb44332009-09-09 15:08:12 +0000534 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson45372a62009-07-23 03:17:50 +0000535 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
536 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000537 assert(!Packed &&
Anders Carlsson45372a62009-07-23 03:17:50 +0000538 "Could not layout fields even with a packed LLVM struct!");
539 return false;
540 }
541 }
542
543 // Append tail padding if necessary.
Anders Carlssonc1efe362009-07-27 14:55:54 +0000544 AppendTailPadding(Layout.getSize());
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Anders Carlsson45372a62009-07-23 03:17:50 +0000546 return true;
547}
548
Anders Carlssonc1efe362009-07-27 14:55:54 +0000549void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) {
550 assert(RecordSize % 8 == 0 && "Invalid record size!");
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Anders Carlssonc1efe362009-07-27 14:55:54 +0000552 uint64_t RecordSizeInBytes = RecordSize / 8;
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000553 assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Daniel Dunbar270e2032010-03-31 00:11:27 +0000555 uint64_t AlignedNextFieldOffset =
Anders Carlssonc2456822009-12-08 01:24:23 +0000556 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, AlignmentAsLLVMStruct);
557
558 if (AlignedNextFieldOffset == RecordSizeInBytes) {
559 // We don't need any padding.
560 return;
561 }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000562
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000563 unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes;
Anders Carlssonc1efe362009-07-27 14:55:54 +0000564 AppendBytes(NumPadBytes);
565}
566
Mike Stump1eb44332009-09-09 15:08:12 +0000567void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes,
Anders Carlsson45372a62009-07-23 03:17:50 +0000568 const llvm::Type *FieldTy) {
569 AlignmentAsLLVMStruct = std::max(AlignmentAsLLVMStruct,
570 getTypeAlignment(FieldTy));
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000571
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000572 uint64_t FieldSizeInBytes = Types.getTargetData().getTypeAllocSize(FieldTy);
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000573
Anders Carlsson45372a62009-07-23 03:17:50 +0000574 FieldTypes.push_back(FieldTy);
Anders Carlsson45372a62009-07-23 03:17:50 +0000575
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000576 NextFieldOffsetInBytes = FieldOffsetInBytes + FieldSizeInBytes;
Anders Carlsson45372a62009-07-23 03:17:50 +0000577 BitsAvailableInLastField = 0;
578}
579
Mike Stump1eb44332009-09-09 15:08:12 +0000580void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
Anders Carlsson45372a62009-07-23 03:17:50 +0000581 unsigned FieldAlignment) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000582 assert(NextFieldOffsetInBytes <= FieldOffsetInBytes &&
583 "Incorrect field layout!");
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Anders Carlsson45372a62009-07-23 03:17:50 +0000585 // Round up the field offset to the alignment of the field type.
Mike Stump1eb44332009-09-09 15:08:12 +0000586 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson45372a62009-07-23 03:17:50 +0000587 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment);
588
589 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
590 // Even with alignment, the field offset is not at the right place,
591 // insert padding.
592 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
593
594 AppendBytes(PaddingInBytes);
595 }
596}
597
598void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) {
599 if (NumBytes == 0)
600 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000601
Owen Anderson0032b272009-08-13 21:57:51 +0000602 const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
Anders Carlssonc1efe362009-07-27 14:55:54 +0000603 if (NumBytes > 1)
Anders Carlsson45372a62009-07-23 03:17:50 +0000604 Ty = llvm::ArrayType::get(Ty, NumBytes);
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Anders Carlsson45372a62009-07-23 03:17:50 +0000606 // Append the padding field
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000607 AppendField(NextFieldOffsetInBytes, Ty);
Anders Carlsson45372a62009-07-23 03:17:50 +0000608}
609
610unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000611 if (Packed)
Anders Carlsson45372a62009-07-23 03:17:50 +0000612 return 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Anders Carlsson45372a62009-07-23 03:17:50 +0000614 return Types.getTargetData().getABITypeAlignment(Ty);
615}
616
John McCallf16aa102010-08-22 21:01:12 +0000617void CGRecordLayoutBuilder::CheckZeroInitializable(QualType T) {
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000618 // This record already contains a member pointer.
John McCallf16aa102010-08-22 21:01:12 +0000619 if (!IsZeroInitializable)
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000620 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000621
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000622 // Can only have member pointers if we're compiling C++.
623 if (!Types.getContext().getLangOptions().CPlusPlus)
624 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Anders Carlsson2c12d032010-02-02 05:17:25 +0000626 T = Types.getContext().getBaseElementType(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Anders Carlsson2c12d032010-02-02 05:17:25 +0000628 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) {
John McCallf16aa102010-08-22 21:01:12 +0000629 if (!Types.getCXXABI().isZeroInitializable(MPT))
630 IsZeroInitializable = false;
Anders Carlsson2c12d032010-02-02 05:17:25 +0000631 } else if (const RecordType *RT = T->getAs<RecordType>()) {
632 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
John McCallf16aa102010-08-22 21:01:12 +0000633 CheckZeroInitializable(RD);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000634 }
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000635}
636
John McCallf16aa102010-08-22 21:01:12 +0000637void CGRecordLayoutBuilder::CheckZeroInitializable(const CXXRecordDecl *RD) {
Anders Carlssona83fb4b2010-05-18 16:51:41 +0000638 // This record already contains a member pointer.
John McCallf16aa102010-08-22 21:01:12 +0000639 if (!IsZeroInitializable)
Anders Carlssona83fb4b2010-05-18 16:51:41 +0000640 return;
641
642 // FIXME: It would be better if there was a way to explicitly compute the
643 // record layout instead of converting to a type.
644 Types.ConvertTagDeclType(RD);
645
646 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
647
John McCallf16aa102010-08-22 21:01:12 +0000648 if (!Layout.isZeroInitializable())
649 IsZeroInitializable = false;
Anders Carlssona83fb4b2010-05-18 16:51:41 +0000650}
651
Daniel Dunbar270e2032010-03-31 00:11:27 +0000652CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) {
653 CGRecordLayoutBuilder Builder(*this);
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Anders Carlsson45372a62009-07-23 03:17:50 +0000655 Builder.Layout(D);
Anders Carlsson4c98efd2009-07-24 15:20:52 +0000656
Daniel Dunbar270e2032010-03-31 00:11:27 +0000657 const llvm::Type *Ty = llvm::StructType::get(getLLVMContext(),
Owen Anderson47a434f2009-08-05 23:18:46 +0000658 Builder.FieldTypes,
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000659 Builder.Packed);
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000661 CGRecordLayout *RL =
John McCallf16aa102010-08-22 21:01:12 +0000662 new CGRecordLayout(Ty, Builder.IsZeroInitializable);
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000663
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000664 // Add all the non-virtual base field numbers.
665 RL->NonVirtualBaseFields.insert(Builder.LLVMNonVirtualBases.begin(),
666 Builder.LLVMNonVirtualBases.end());
667
Anders Carlsson45372a62009-07-23 03:17:50 +0000668 // Add all the field numbers.
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000669 RL->FieldInfo.insert(Builder.LLVMFields.begin(),
670 Builder.LLVMFields.end());
Anders Carlsson45372a62009-07-23 03:17:50 +0000671
672 // Add bitfield info.
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000673 RL->BitFields.insert(Builder.LLVMBitFields.begin(),
674 Builder.LLVMBitFields.end());
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000676 // Dump the layout, if requested.
Daniel Dunbarab970f92010-04-13 20:58:55 +0000677 if (getContext().getLangOptions().DumpRecordLayouts) {
Daniel Dunbar8d8ab742010-04-19 20:44:53 +0000678 llvm::errs() << "\n*** Dumping IRgen Record Layout\n";
Daniel Dunbarab970f92010-04-13 20:58:55 +0000679 llvm::errs() << "Record: ";
680 D->dump();
681 llvm::errs() << "\nLayout: ";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000682 RL->dump();
Daniel Dunbarab970f92010-04-13 20:58:55 +0000683 }
Daniel Dunbar93c62962010-04-12 18:14:18 +0000684
Daniel Dunbare1467a42010-04-22 02:35:46 +0000685#ifndef NDEBUG
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000686 // Verify that the computed LLVM struct size matches the AST layout size.
Daniel Dunbare1467a42010-04-22 02:35:46 +0000687 uint64_t TypeSizeInBits = getContext().getASTRecordLayout(D).getSize();
688 assert(TypeSizeInBits == getTargetData().getTypeAllocSizeInBits(Ty) &&
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000689 "Type size mismatch!");
690
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000691 // Verify that the LLVM and AST field offsets agree.
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000692 const llvm::StructType *ST =
693 dyn_cast<llvm::StructType>(RL->getLLVMType());
694 const llvm::StructLayout *SL = getTargetData().getStructLayout(ST);
695
696 const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
697 RecordDecl::field_iterator it = D->field_begin();
698 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
699 const FieldDecl *FD = *it;
Daniel Dunbare1467a42010-04-22 02:35:46 +0000700
701 // For non-bit-fields, just check that the LLVM struct offset matches the
702 // AST offset.
703 if (!FD->isBitField()) {
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000704 unsigned FieldNo = RL->getLLVMFieldNo(FD);
705 assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
706 "Invalid field offset!");
Daniel Dunbare1467a42010-04-22 02:35:46 +0000707 continue;
708 }
709
710 // Ignore unnamed bit-fields.
711 if (!FD->getDeclName())
712 continue;
713
714 const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
715 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
716 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
717
718 // Verify that every component access is within the structure.
719 uint64_t FieldOffset = SL->getElementOffsetInBits(AI.FieldIndex);
720 uint64_t AccessBitOffset = FieldOffset + AI.FieldByteOffset * 8;
721 assert(AccessBitOffset + AI.AccessWidth <= TypeSizeInBits &&
722 "Invalid bit-field access (out of range)!");
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000723 }
724 }
725#endif
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000726
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000727 return RL;
Anders Carlsson45372a62009-07-23 03:17:50 +0000728}
Daniel Dunbar93c62962010-04-12 18:14:18 +0000729
730void CGRecordLayout::print(llvm::raw_ostream &OS) const {
731 OS << "<CGRecordLayout\n";
732 OS << " LLVMType:" << *LLVMType << "\n";
John McCallf16aa102010-08-22 21:01:12 +0000733 OS << " IsZeroInitializable:" << IsZeroInitializable << "\n";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000734 OS << " BitFields:[\n";
Daniel Dunbarad759532010-04-22 02:35:36 +0000735
736 // Print bit-field infos in declaration order.
737 std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
Daniel Dunbar93c62962010-04-12 18:14:18 +0000738 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
739 it = BitFields.begin(), ie = BitFields.end();
740 it != ie; ++it) {
Daniel Dunbarad759532010-04-22 02:35:36 +0000741 const RecordDecl *RD = it->first->getParent();
742 unsigned Index = 0;
743 for (RecordDecl::field_iterator
744 it2 = RD->field_begin(); *it2 != it->first; ++it2)
745 ++Index;
746 BFIs.push_back(std::make_pair(Index, &it->second));
747 }
748 llvm::array_pod_sort(BFIs.begin(), BFIs.end());
749 for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
Daniel Dunbarab970f92010-04-13 20:58:55 +0000750 OS.indent(4);
Daniel Dunbarad759532010-04-22 02:35:36 +0000751 BFIs[i].second->print(OS);
Daniel Dunbar93c62962010-04-12 18:14:18 +0000752 OS << "\n";
753 }
Daniel Dunbarad759532010-04-22 02:35:36 +0000754
Daniel Dunbar93c62962010-04-12 18:14:18 +0000755 OS << "]>\n";
756}
757
758void CGRecordLayout::dump() const {
759 print(llvm::errs());
760}
761
762void CGBitFieldInfo::print(llvm::raw_ostream &OS) const {
763 OS << "<CGBitFieldInfo";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000764 OS << " Size:" << Size;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000765 OS << " IsSigned:" << IsSigned << "\n";
766
767 OS.indent(4 + strlen("<CGBitFieldInfo"));
768 OS << " NumComponents:" << getNumComponents();
769 OS << " Components: [";
770 if (getNumComponents()) {
771 OS << "\n";
772 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
773 const AccessInfo &AI = getComponent(i);
774 OS.indent(8);
775 OS << "<AccessInfo"
776 << " FieldIndex:" << AI.FieldIndex
777 << " FieldByteOffset:" << AI.FieldByteOffset
778 << " FieldBitStart:" << AI.FieldBitStart
779 << " AccessWidth:" << AI.AccessWidth << "\n";
780 OS.indent(8 + strlen("<AccessInfo"));
781 OS << " AccessAlignment:" << AI.AccessAlignment
782 << " TargetBitOffset:" << AI.TargetBitOffset
783 << " TargetBitWidth:" << AI.TargetBitWidth
784 << ">\n";
785 }
786 OS.indent(4);
787 }
788 OS << "]>";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000789}
790
791void CGBitFieldInfo::dump() const {
792 print(llvm::errs());
793}