blob: b1a2d51b66f3957c5501f31bec8eb2b220b50ce2 [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"
21#include "llvm/DerivedTypes.h"
Daniel Dunbar93c62962010-04-12 18:14:18 +000022#include "llvm/Type.h"
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +000023#include "llvm/Support/Debug.h"
Daniel Dunbar93c62962010-04-12 18:14:18 +000024#include "llvm/Support/raw_ostream.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000025#include "llvm/Target/TargetData.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000026using namespace clang;
27using namespace CodeGen;
28
Daniel Dunbar270e2032010-03-31 00:11:27 +000029namespace clang {
30namespace CodeGen {
31
32class CGRecordLayoutBuilder {
33public:
34 /// FieldTypes - Holds the LLVM types that the struct is created from.
35 std::vector<const llvm::Type *> FieldTypes;
36
37 /// LLVMFieldInfo - Holds a field and its corresponding LLVM field number.
38 typedef std::pair<const FieldDecl *, unsigned> LLVMFieldInfo;
39 llvm::SmallVector<LLVMFieldInfo, 16> LLVMFields;
40
41 /// LLVMBitFieldInfo - Holds location and size information about a bit field.
Daniel Dunbarc7a984a2010-04-06 01:07:41 +000042 typedef std::pair<const FieldDecl *, CGBitFieldInfo> LLVMBitFieldInfo;
Daniel Dunbar270e2032010-03-31 00:11:27 +000043 llvm::SmallVector<LLVMBitFieldInfo, 16> LLVMBitFields;
44
Anders Carlssonc6772ce2010-05-18 05:22:06 +000045 typedef std::pair<const CXXRecordDecl *, unsigned> LLVMBaseInfo;
46 llvm::SmallVector<LLVMBaseInfo, 16> LLVMNonVirtualBases;
47
Daniel Dunbar270e2032010-03-31 00:11:27 +000048 /// ContainsPointerToDataMember - Whether one of the fields in this record
49 /// layout is a pointer to data member, or a struct that contains pointer to
50 /// data member.
51 bool ContainsPointerToDataMember;
52
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
118 /// CheckForPointerToDataMember - Check if the given type contains a pointer
119 /// to data member.
120 void CheckForPointerToDataMember(QualType T);
121
122public:
123 CGRecordLayoutBuilder(CodeGenTypes &Types)
124 : ContainsPointerToDataMember(false), Packed(false), Types(Types),
125 Alignment(0), AlignmentAsLLVMStruct(1),
126 BitsAvailableInLastField(0), NextFieldOffsetInBytes(0) { }
127
128 /// Layout - Will layout a RecordDecl.
129 void Layout(const RecordDecl *D);
130};
131
132}
133}
134
Anders Carlsson45372a62009-07-23 03:17:50 +0000135void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
Anders Carlssona5dd7222009-08-08 19:38:24 +0000136 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment() / 8;
Anders Carlssond0eb3b92009-09-02 17:51:33 +0000137 Packed = D->hasAttr<PackedAttr>();
Anders Carlssona5dd7222009-08-08 19:38:24 +0000138
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000139 if (D->isUnion()) {
140 LayoutUnion(D);
141 return;
142 }
Anders Carlssona860e752009-08-08 18:23:56 +0000143
Anders Carlsson45372a62009-07-23 03:17:50 +0000144 if (LayoutFields(D))
145 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Anders Carlsson45372a62009-07-23 03:17:50 +0000147 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000148 Packed = true;
Anders Carlsson45372a62009-07-23 03:17:50 +0000149 AlignmentAsLLVMStruct = 1;
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000150 NextFieldOffsetInBytes = 0;
Anders Carlsson45372a62009-07-23 03:17:50 +0000151 FieldTypes.clear();
Anders Carlsson45372a62009-07-23 03:17:50 +0000152 LLVMFields.clear();
153 LLVMBitFields.clear();
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000154 LLVMNonVirtualBases.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Anders Carlsson45372a62009-07-23 03:17:50 +0000156 LayoutFields(D);
157}
158
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000159static CGBitFieldInfo ComputeBitFieldInfo(CodeGenTypes &Types,
160 const FieldDecl *FD,
161 uint64_t FieldOffset,
162 uint64_t FieldSize) {
Daniel Dunbare1467a42010-04-22 02:35:46 +0000163 const RecordDecl *RD = FD->getParent();
Daniel Dunbar89da8742010-04-22 03:17:04 +0000164 const ASTRecordLayout &RL = Types.getContext().getASTRecordLayout(RD);
165 uint64_t ContainingTypeSizeInBits = RL.getSize();
166 unsigned ContainingTypeAlign = RL.getAlignment();
Daniel Dunbare1467a42010-04-22 02:35:46 +0000167
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000168 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(FD->getType());
Daniel Dunbarab970f92010-04-13 20:58:55 +0000169 uint64_t TypeSizeInBytes = Types.getTargetData().getTypeAllocSize(Ty);
170 uint64_t TypeSizeInBits = TypeSizeInBytes * 8;
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000171
172 bool IsSigned = FD->getType()->isSignedIntegerType();
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000173
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000174 if (FieldSize > TypeSizeInBits) {
Anders Carlsson6ba38152010-04-17 22:54:57 +0000175 // We have a wide bit-field. The extra bits are only used for padding, so
176 // if we have a bitfield of type T, with size N:
177 //
178 // T t : N;
179 //
180 // We can just assume that it's:
181 //
182 // T t : sizeof(T);
183 //
184 FieldSize = TypeSizeInBits;
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000185 }
186
Daniel Dunbare1467a42010-04-22 02:35:46 +0000187 // Compute the access components. The policy we use is to start by attempting
188 // to access using the width of the bit-field type itself and to always access
189 // at aligned indices of that type. If such an access would fail because it
190 // extends past the bound of the type, then we reduce size to the next smaller
191 // power of two and retry. The current algorithm assumes pow2 sized types,
192 // although this is easy to fix.
193 //
194 // FIXME: This algorithm is wrong on big-endian systems, I think.
195 assert(llvm::isPowerOf2_32(TypeSizeInBits) && "Unexpected type size!");
196 CGBitFieldInfo::AccessInfo Components[3];
197 unsigned NumComponents = 0;
198 unsigned AccessedTargetBits = 0; // The tumber of target bits accessed.
199 unsigned AccessWidth = TypeSizeInBits; // The current access width to attempt.
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000200
Daniel Dunbare1467a42010-04-22 02:35:46 +0000201 // Round down from the field offset to find the first access position that is
202 // at an aligned offset of the initial access type.
Daniel Dunbar52968a12010-04-22 15:22:33 +0000203 uint64_t AccessStart = FieldOffset - (FieldOffset % AccessWidth);
204
205 // Adjust initial access size to fit within record.
206 while (AccessWidth > 8 &&
207 AccessStart + AccessWidth > ContainingTypeSizeInBits) {
208 AccessWidth >>= 1;
209 AccessStart = FieldOffset - (FieldOffset % AccessWidth);
210 }
Daniel Dunbar2df25692010-04-15 05:09:32 +0000211
Daniel Dunbare1467a42010-04-22 02:35:46 +0000212 while (AccessedTargetBits < FieldSize) {
213 // Check that we can access using a type of this size, without reading off
214 // the end of the structure. This can occur with packed structures and
215 // -fno-bitfield-type-align, for example.
216 if (AccessStart + AccessWidth > ContainingTypeSizeInBits) {
217 // If so, reduce access size to the next smaller power-of-two and retry.
218 AccessWidth >>= 1;
219 assert(AccessWidth >= 8 && "Cannot access under byte size!");
220 continue;
221 }
Daniel Dunbarab970f92010-04-13 20:58:55 +0000222
Daniel Dunbare1467a42010-04-22 02:35:46 +0000223 // Otherwise, add an access component.
Daniel Dunbarab970f92010-04-13 20:58:55 +0000224
Daniel Dunbare1467a42010-04-22 02:35:46 +0000225 // First, compute the bits inside this access which are part of the
226 // target. We are reading bits [AccessStart, AccessStart + AccessWidth); the
227 // intersection with [FieldOffset, FieldOffset + FieldSize) gives the bits
228 // in the target that we are reading.
Daniel Dunbar52968a12010-04-22 15:22:33 +0000229 assert(FieldOffset < AccessStart + AccessWidth && "Invalid access start!");
230 assert(AccessStart < FieldOffset + FieldSize && "Invalid access start!");
Daniel Dunbare1467a42010-04-22 02:35:46 +0000231 uint64_t AccessBitsInFieldStart = std::max(AccessStart, FieldOffset);
232 uint64_t AccessBitsInFieldSize =
Daniel Dunbar52968a12010-04-22 15:22:33 +0000233 std::min(AccessWidth + AccessStart,
234 FieldOffset + FieldSize) - AccessBitsInFieldStart;
Daniel Dunbar4651efb2010-04-22 14:56:10 +0000235
Daniel Dunbare1467a42010-04-22 02:35:46 +0000236 assert(NumComponents < 3 && "Unexpected number of components!");
237 CGBitFieldInfo::AccessInfo &AI = Components[NumComponents++];
238 AI.FieldIndex = 0;
239 // FIXME: We still follow the old access pattern of only using the field
240 // byte offset. We should switch this once we fix the struct layout to be
241 // pretty.
242 AI.FieldByteOffset = AccessStart / 8;
243 AI.FieldBitStart = AccessBitsInFieldStart - AccessStart;
244 AI.AccessWidth = AccessWidth;
Daniel Dunbar89da8742010-04-22 03:17:04 +0000245 AI.AccessAlignment = llvm::MinAlign(ContainingTypeAlign, AccessStart) / 8;
Daniel Dunbare1467a42010-04-22 02:35:46 +0000246 AI.TargetBitOffset = AccessedTargetBits;
247 AI.TargetBitWidth = AccessBitsInFieldSize;
248
249 AccessStart += AccessWidth;
250 AccessedTargetBits += AI.TargetBitWidth;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000251 }
252
Daniel Dunbare1467a42010-04-22 02:35:46 +0000253 assert(AccessedTargetBits == FieldSize && "Invalid bit-field access!");
Daniel Dunbar2df25692010-04-15 05:09:32 +0000254 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000255}
256
Anders Carlsson45372a62009-07-23 03:17:50 +0000257void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
258 uint64_t FieldOffset) {
Mike Stump1eb44332009-09-09 15:08:12 +0000259 uint64_t FieldSize =
Anders Carlsson45372a62009-07-23 03:17:50 +0000260 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Anders Carlsson45372a62009-07-23 03:17:50 +0000262 if (FieldSize == 0)
263 return;
264
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000265 uint64_t NextFieldOffset = NextFieldOffsetInBytes * 8;
Anders Carlsson45372a62009-07-23 03:17:50 +0000266 unsigned NumBytesToAppend;
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Anders Carlsson45372a62009-07-23 03:17:50 +0000268 if (FieldOffset < NextFieldOffset) {
269 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000270 assert(NextFieldOffsetInBytes && "Must have laid out at least one byte!");
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Anders Carlsson45372a62009-07-23 03:17:50 +0000272 // The bitfield begins in the previous bit-field.
Mike Stump1eb44332009-09-09 15:08:12 +0000273 NumBytesToAppend =
Anders Carlsson45372a62009-07-23 03:17:50 +0000274 llvm::RoundUpToAlignment(FieldSize - BitsAvailableInLastField, 8) / 8;
275 } else {
276 assert(FieldOffset % 8 == 0 && "Field offset not aligned correctly");
277
278 // Append padding if necessary.
279 AppendBytes((FieldOffset - NextFieldOffset) / 8);
Mike Stump1eb44332009-09-09 15:08:12 +0000280
281 NumBytesToAppend =
Anders Carlsson45372a62009-07-23 03:17:50 +0000282 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Anders Carlsson45372a62009-07-23 03:17:50 +0000284 assert(NumBytesToAppend && "No bytes to append!");
285 }
286
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000287 // Add the bit field info.
288 LLVMBitFields.push_back(
289 LLVMBitFieldInfo(D, ComputeBitFieldInfo(Types, D, FieldOffset, FieldSize)));
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Anders Carlsson45372a62009-07-23 03:17:50 +0000291 AppendBytes(NumBytesToAppend);
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Mike Stump1eb44332009-09-09 15:08:12 +0000293 BitsAvailableInLastField =
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000294 NextFieldOffsetInBytes * 8 - (FieldOffset + FieldSize);
Anders Carlsson45372a62009-07-23 03:17:50 +0000295}
296
297bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
298 uint64_t FieldOffset) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000299 // If the field is packed, then we need a packed struct.
Anders Carlssona860e752009-08-08 18:23:56 +0000300 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson45372a62009-07-23 03:17:50 +0000301 return false;
302
303 if (D->isBitField()) {
304 // We must use packed structs for unnamed bit fields since they
305 // don't affect the struct alignment.
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000306 if (!Packed && !D->getDeclName())
Anders Carlsson45372a62009-07-23 03:17:50 +0000307 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Anders Carlsson45372a62009-07-23 03:17:50 +0000309 LayoutBitField(D, FieldOffset);
310 return true;
311 }
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Anders Carlsson2c12d032010-02-02 05:17:25 +0000313 // Check if we have a pointer to data member in this field.
314 CheckForPointerToDataMember(D->getType());
Daniel Dunbar270e2032010-03-31 00:11:27 +0000315
Anders Carlsson45372a62009-07-23 03:17:50 +0000316 assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!");
Anders Carlsson45372a62009-07-23 03:17:50 +0000317 uint64_t FieldOffsetInBytes = FieldOffset / 8;
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000319 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
320 unsigned TypeAlignment = getTypeAlignment(Ty);
321
Anders Carlssona5dd7222009-08-08 19:38:24 +0000322 // If the type alignment is larger then the struct alignment, we must use
323 // a packed struct.
324 if (TypeAlignment > Alignment) {
325 assert(!Packed && "Alignment is wrong even with packed struct!");
326 return false;
327 }
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Anders Carlssona5dd7222009-08-08 19:38:24 +0000329 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
330 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
331 if (const PragmaPackAttr *PPA = RD->getAttr<PragmaPackAttr>()) {
332 if (PPA->getAlignment() != TypeAlignment * 8 && !Packed)
333 return false;
334 }
335 }
336
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000337 // Round up the field offset to the alignment of the field type.
Mike Stump1eb44332009-09-09 15:08:12 +0000338 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000339 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, TypeAlignment);
340
341 if (FieldOffsetInBytes < AlignedNextFieldOffsetInBytes) {
342 assert(!Packed && "Could not place field even with packed struct!");
343 return false;
344 }
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000346 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
347 // Even with alignment, the field offset is not at the right place,
348 // insert padding.
349 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000351 AppendBytes(PaddingInBytes);
352 }
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Anders Carlsson45372a62009-07-23 03:17:50 +0000354 // Now append the field.
355 LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size()));
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000356 AppendField(FieldOffsetInBytes, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Anders Carlsson45372a62009-07-23 03:17:50 +0000358 return true;
359}
360
Anders Carlsson86664462010-04-17 20:49:27 +0000361const llvm::Type *
362CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
363 const ASTRecordLayout &Layout) {
364 if (Field->isBitField()) {
365 uint64_t FieldSize =
366 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
367
368 // Ignore zero sized bit fields.
369 if (FieldSize == 0)
370 return 0;
371
Daniel Dunbar8ab78a72010-04-20 17:52:30 +0000372 const llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
373 unsigned NumBytesToAppend =
374 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Anders Carlssond62328e2010-04-17 21:04:52 +0000375
Daniel Dunbar8ab78a72010-04-20 17:52:30 +0000376 if (NumBytesToAppend > 1)
377 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend);
Anders Carlssond62328e2010-04-17 21:04:52 +0000378
Anders Carlsson86664462010-04-17 20:49:27 +0000379 // Add the bit field info.
380 LLVMBitFields.push_back(
381 LLVMBitFieldInfo(Field, ComputeBitFieldInfo(Types, Field, 0, FieldSize)));
Anders Carlssond62328e2010-04-17 21:04:52 +0000382 return FieldTy;
Anders Carlsson86664462010-04-17 20:49:27 +0000383 }
Daniel Dunbar8ab78a72010-04-20 17:52:30 +0000384
Anders Carlsson86664462010-04-17 20:49:27 +0000385 // This is a regular union field.
386 LLVMFields.push_back(LLVMFieldInfo(Field, 0));
387 return Types.ConvertTypeForMemRecursive(Field->getType());
388}
389
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000390void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
391 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000393 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000395 const llvm::Type *Ty = 0;
396 uint64_t Size = 0;
397 unsigned Align = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000399 bool HasOnlyZeroSizedBitFields = true;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000400
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000401 unsigned FieldNo = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000402 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000403 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
Mike Stump1eb44332009-09-09 15:08:12 +0000404 assert(Layout.getFieldOffset(FieldNo) == 0 &&
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000405 "Union field offset did not start at the beginning of record!");
Anders Carlsson86664462010-04-17 20:49:27 +0000406 const llvm::Type *FieldTy = LayoutUnionField(*Field, Layout);
Anders Carlsson2cc8f172009-07-23 04:00:39 +0000407
Anders Carlsson86664462010-04-17 20:49:27 +0000408 if (!FieldTy)
409 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000411 HasOnlyZeroSizedBitFields = false;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000412
Anders Carlsson177d4d82009-07-23 21:52:03 +0000413 unsigned FieldAlign = Types.getTargetData().getABITypeAlignment(FieldTy);
414 uint64_t FieldSize = Types.getTargetData().getTypeAllocSize(FieldTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000416 if (FieldAlign < Align)
417 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000419 if (FieldAlign > Align || FieldSize > Size) {
420 Ty = FieldTy;
421 Align = FieldAlign;
422 Size = FieldSize;
423 }
424 }
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000426 // Now add our field.
Anders Carlsson36620002009-09-03 22:56:02 +0000427 if (Ty) {
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000428 AppendField(0, Ty);
Anders Carlsson36620002009-09-03 22:56:02 +0000429
430 if (getTypeAlignment(Ty) > Layout.getAlignment() / 8) {
431 // We need a packed struct.
432 Packed = true;
433 Align = 1;
434 }
435 }
Fariborz Jahaniane5041702009-11-06 20:47:40 +0000436 if (!Align) {
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000437 assert(HasOnlyZeroSizedBitFields &&
438 "0-align record did not have all zero-sized bit-fields!");
Fariborz Jahaniane5041702009-11-06 20:47:40 +0000439 Align = 1;
440 }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000441
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000442 // Append tail padding.
443 if (Layout.getSize() / 8 > Size)
444 AppendPadding(Layout.getSize() / 8, Align);
445}
446
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000447void CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *BaseDecl,
448 uint64_t BaseOffset) {
449 const ASTRecordLayout &Layout =
450 Types.getContext().getASTRecordLayout(BaseDecl);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000451
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000452 uint64_t NonVirtualSize = Layout.getNonVirtualSize();
453
454 if (BaseDecl->isEmpty()) {
455 // FIXME: Lay out empty bases.
456 return;
457 }
458
459 // FIXME: Actually use a better type than [sizeof(BaseDecl) x i8] when we can.
460 AppendPadding(BaseOffset / 8, 1);
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000461
462 // Append the base field.
463 LLVMNonVirtualBases.push_back(LLVMBaseInfo(BaseDecl, FieldTypes.size()));
464
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000465 AppendBytes(NonVirtualSize / 8);
466}
467
468void
469CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD,
470 const ASTRecordLayout &Layout) {
471 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
472
473 // Check if we need to add a vtable pointer.
474 if (RD->isDynamicClass()) {
475 if (!PrimaryBase) {
476 const llvm::Type *FunctionType =
477 llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()),
478 /*isVarArg=*/true);
479 const llvm::Type *VTableTy = FunctionType->getPointerTo();
480
481 assert(NextFieldOffsetInBytes == 0 &&
482 "VTable pointer must come first!");
483 AppendField(NextFieldOffsetInBytes, VTableTy->getPointerTo());
484 } else {
485 // FIXME: Handle a virtual primary base.
486 if (!Layout.getPrimaryBaseWasVirtual())
487 LayoutNonVirtualBase(PrimaryBase, 0);
488 }
489 }
490
491 // Layout the non-virtual bases.
492 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
493 E = RD->bases_end(); I != E; ++I) {
494 if (I->isVirtual())
495 continue;
496
497 const CXXRecordDecl *BaseDecl =
498 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
499
500 // We've already laid out the primary base.
501 if (BaseDecl == PrimaryBase && !Layout.getPrimaryBaseWasVirtual())
502 continue;
503
504 LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffset(BaseDecl));
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000505 }
506}
507
Anders Carlsson45372a62009-07-23 03:17:50 +0000508bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
509 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
Anders Carlssona5dd7222009-08-08 19:38:24 +0000510 assert(Alignment && "Did not set alignment!");
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000512 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000514 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D))
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000515 LayoutNonVirtualBases(RD, Layout);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000516
Anders Carlsson45372a62009-07-23 03:17:50 +0000517 unsigned FieldNo = 0;
Fariborz Jahaniancad86652009-07-27 20:57:45 +0000518
Mike Stump1eb44332009-09-09 15:08:12 +0000519 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson45372a62009-07-23 03:17:50 +0000520 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
521 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000522 assert(!Packed &&
Anders Carlsson45372a62009-07-23 03:17:50 +0000523 "Could not layout fields even with a packed LLVM struct!");
524 return false;
525 }
526 }
527
528 // Append tail padding if necessary.
Anders Carlssonc1efe362009-07-27 14:55:54 +0000529 AppendTailPadding(Layout.getSize());
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Anders Carlsson45372a62009-07-23 03:17:50 +0000531 return true;
532}
533
Anders Carlssonc1efe362009-07-27 14:55:54 +0000534void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) {
535 assert(RecordSize % 8 == 0 && "Invalid record size!");
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Anders Carlssonc1efe362009-07-27 14:55:54 +0000537 uint64_t RecordSizeInBytes = RecordSize / 8;
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000538 assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Daniel Dunbar270e2032010-03-31 00:11:27 +0000540 uint64_t AlignedNextFieldOffset =
Anders Carlssonc2456822009-12-08 01:24:23 +0000541 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, AlignmentAsLLVMStruct);
542
543 if (AlignedNextFieldOffset == RecordSizeInBytes) {
544 // We don't need any padding.
545 return;
546 }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000547
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000548 unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes;
Anders Carlssonc1efe362009-07-27 14:55:54 +0000549 AppendBytes(NumPadBytes);
550}
551
Mike Stump1eb44332009-09-09 15:08:12 +0000552void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes,
Anders Carlsson45372a62009-07-23 03:17:50 +0000553 const llvm::Type *FieldTy) {
554 AlignmentAsLLVMStruct = std::max(AlignmentAsLLVMStruct,
555 getTypeAlignment(FieldTy));
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000556
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000557 uint64_t FieldSizeInBytes = Types.getTargetData().getTypeAllocSize(FieldTy);
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000558
Anders Carlsson45372a62009-07-23 03:17:50 +0000559 FieldTypes.push_back(FieldTy);
Anders Carlsson45372a62009-07-23 03:17:50 +0000560
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000561 NextFieldOffsetInBytes = FieldOffsetInBytes + FieldSizeInBytes;
Anders Carlsson45372a62009-07-23 03:17:50 +0000562 BitsAvailableInLastField = 0;
563}
564
Mike Stump1eb44332009-09-09 15:08:12 +0000565void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
Anders Carlsson45372a62009-07-23 03:17:50 +0000566 unsigned FieldAlignment) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000567 assert(NextFieldOffsetInBytes <= FieldOffsetInBytes &&
568 "Incorrect field layout!");
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Anders Carlsson45372a62009-07-23 03:17:50 +0000570 // Round up the field offset to the alignment of the field type.
Mike Stump1eb44332009-09-09 15:08:12 +0000571 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson45372a62009-07-23 03:17:50 +0000572 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment);
573
574 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
575 // Even with alignment, the field offset is not at the right place,
576 // insert padding.
577 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
578
579 AppendBytes(PaddingInBytes);
580 }
581}
582
583void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) {
584 if (NumBytes == 0)
585 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Owen Anderson0032b272009-08-13 21:57:51 +0000587 const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
Anders Carlssonc1efe362009-07-27 14:55:54 +0000588 if (NumBytes > 1)
Anders Carlsson45372a62009-07-23 03:17:50 +0000589 Ty = llvm::ArrayType::get(Ty, NumBytes);
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Anders Carlsson45372a62009-07-23 03:17:50 +0000591 // Append the padding field
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000592 AppendField(NextFieldOffsetInBytes, Ty);
Anders Carlsson45372a62009-07-23 03:17:50 +0000593}
594
595unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000596 if (Packed)
Anders Carlsson45372a62009-07-23 03:17:50 +0000597 return 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Anders Carlsson45372a62009-07-23 03:17:50 +0000599 return Types.getTargetData().getABITypeAlignment(Ty);
600}
601
Anders Carlsson2c12d032010-02-02 05:17:25 +0000602void CGRecordLayoutBuilder::CheckForPointerToDataMember(QualType T) {
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000603 // This record already contains a member pointer.
Anders Carlsson2c12d032010-02-02 05:17:25 +0000604 if (ContainsPointerToDataMember)
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000605 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000606
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000607 // Can only have member pointers if we're compiling C++.
608 if (!Types.getContext().getLangOptions().CPlusPlus)
609 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Anders Carlsson2c12d032010-02-02 05:17:25 +0000611 T = Types.getContext().getBaseElementType(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Anders Carlsson2c12d032010-02-02 05:17:25 +0000613 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) {
614 if (!MPT->getPointeeType()->isFunctionType()) {
615 // We have a pointer to data member.
616 ContainsPointerToDataMember = true;
617 }
618 } else if (const RecordType *RT = T->getAs<RecordType>()) {
619 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Daniel Dunbar270e2032010-03-31 00:11:27 +0000620
Anders Carlsson2c12d032010-02-02 05:17:25 +0000621 // FIXME: It would be better if there was a way to explicitly compute the
622 // record layout instead of converting to a type.
623 Types.ConvertTagDeclType(RD);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000624
Anders Carlsson2c12d032010-02-02 05:17:25 +0000625 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000626
Anders Carlsson2c12d032010-02-02 05:17:25 +0000627 if (Layout.containsPointerToDataMember())
628 ContainsPointerToDataMember = true;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000629 }
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000630}
631
Daniel Dunbar270e2032010-03-31 00:11:27 +0000632CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) {
633 CGRecordLayoutBuilder Builder(*this);
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Anders Carlsson45372a62009-07-23 03:17:50 +0000635 Builder.Layout(D);
Anders Carlsson4c98efd2009-07-24 15:20:52 +0000636
Daniel Dunbar270e2032010-03-31 00:11:27 +0000637 const llvm::Type *Ty = llvm::StructType::get(getLLVMContext(),
Owen Anderson47a434f2009-08-05 23:18:46 +0000638 Builder.FieldTypes,
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000639 Builder.Packed);
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000641 CGRecordLayout *RL =
642 new CGRecordLayout(Ty, Builder.ContainsPointerToDataMember);
643
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000644 // Add all the non-virtual base field numbers.
645 RL->NonVirtualBaseFields.insert(Builder.LLVMNonVirtualBases.begin(),
646 Builder.LLVMNonVirtualBases.end());
647
Anders Carlsson45372a62009-07-23 03:17:50 +0000648 // Add all the field numbers.
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000649 RL->FieldInfo.insert(Builder.LLVMFields.begin(),
650 Builder.LLVMFields.end());
Anders Carlsson45372a62009-07-23 03:17:50 +0000651
652 // Add bitfield info.
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000653 RL->BitFields.insert(Builder.LLVMBitFields.begin(),
654 Builder.LLVMBitFields.end());
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000656 // Dump the layout, if requested.
Daniel Dunbarab970f92010-04-13 20:58:55 +0000657 if (getContext().getLangOptions().DumpRecordLayouts) {
Daniel Dunbar8d8ab742010-04-19 20:44:53 +0000658 llvm::errs() << "\n*** Dumping IRgen Record Layout\n";
Daniel Dunbarab970f92010-04-13 20:58:55 +0000659 llvm::errs() << "Record: ";
660 D->dump();
661 llvm::errs() << "\nLayout: ";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000662 RL->dump();
Daniel Dunbarab970f92010-04-13 20:58:55 +0000663 }
Daniel Dunbar93c62962010-04-12 18:14:18 +0000664
Daniel Dunbare1467a42010-04-22 02:35:46 +0000665#ifndef NDEBUG
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000666 // Verify that the computed LLVM struct size matches the AST layout size.
Daniel Dunbare1467a42010-04-22 02:35:46 +0000667 uint64_t TypeSizeInBits = getContext().getASTRecordLayout(D).getSize();
668 assert(TypeSizeInBits == getTargetData().getTypeAllocSizeInBits(Ty) &&
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000669 "Type size mismatch!");
670
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000671 // Verify that the LLVM and AST field offsets agree.
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000672 const llvm::StructType *ST =
673 dyn_cast<llvm::StructType>(RL->getLLVMType());
674 const llvm::StructLayout *SL = getTargetData().getStructLayout(ST);
675
676 const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
677 RecordDecl::field_iterator it = D->field_begin();
678 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
679 const FieldDecl *FD = *it;
Daniel Dunbare1467a42010-04-22 02:35:46 +0000680
681 // For non-bit-fields, just check that the LLVM struct offset matches the
682 // AST offset.
683 if (!FD->isBitField()) {
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000684 unsigned FieldNo = RL->getLLVMFieldNo(FD);
685 assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
686 "Invalid field offset!");
Daniel Dunbare1467a42010-04-22 02:35:46 +0000687 continue;
688 }
689
690 // Ignore unnamed bit-fields.
691 if (!FD->getDeclName())
692 continue;
693
694 const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
695 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
696 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
697
698 // Verify that every component access is within the structure.
699 uint64_t FieldOffset = SL->getElementOffsetInBits(AI.FieldIndex);
700 uint64_t AccessBitOffset = FieldOffset + AI.FieldByteOffset * 8;
701 assert(AccessBitOffset + AI.AccessWidth <= TypeSizeInBits &&
702 "Invalid bit-field access (out of range)!");
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000703 }
704 }
705#endif
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000706
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000707 return RL;
Anders Carlsson45372a62009-07-23 03:17:50 +0000708}
Daniel Dunbar93c62962010-04-12 18:14:18 +0000709
710void CGRecordLayout::print(llvm::raw_ostream &OS) const {
711 OS << "<CGRecordLayout\n";
712 OS << " LLVMType:" << *LLVMType << "\n";
713 OS << " ContainsPointerToDataMember:" << ContainsPointerToDataMember << "\n";
714 OS << " BitFields:[\n";
Daniel Dunbarad759532010-04-22 02:35:36 +0000715
716 // Print bit-field infos in declaration order.
717 std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
Daniel Dunbar93c62962010-04-12 18:14:18 +0000718 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
719 it = BitFields.begin(), ie = BitFields.end();
720 it != ie; ++it) {
Daniel Dunbarad759532010-04-22 02:35:36 +0000721 const RecordDecl *RD = it->first->getParent();
722 unsigned Index = 0;
723 for (RecordDecl::field_iterator
724 it2 = RD->field_begin(); *it2 != it->first; ++it2)
725 ++Index;
726 BFIs.push_back(std::make_pair(Index, &it->second));
727 }
728 llvm::array_pod_sort(BFIs.begin(), BFIs.end());
729 for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
Daniel Dunbarab970f92010-04-13 20:58:55 +0000730 OS.indent(4);
Daniel Dunbarad759532010-04-22 02:35:36 +0000731 BFIs[i].second->print(OS);
Daniel Dunbar93c62962010-04-12 18:14:18 +0000732 OS << "\n";
733 }
Daniel Dunbarad759532010-04-22 02:35:36 +0000734
Daniel Dunbar93c62962010-04-12 18:14:18 +0000735 OS << "]>\n";
736}
737
738void CGRecordLayout::dump() const {
739 print(llvm::errs());
740}
741
742void CGBitFieldInfo::print(llvm::raw_ostream &OS) const {
743 OS << "<CGBitFieldInfo";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000744 OS << " Size:" << Size;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000745 OS << " IsSigned:" << IsSigned << "\n";
746
747 OS.indent(4 + strlen("<CGBitFieldInfo"));
748 OS << " NumComponents:" << getNumComponents();
749 OS << " Components: [";
750 if (getNumComponents()) {
751 OS << "\n";
752 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
753 const AccessInfo &AI = getComponent(i);
754 OS.indent(8);
755 OS << "<AccessInfo"
756 << " FieldIndex:" << AI.FieldIndex
757 << " FieldByteOffset:" << AI.FieldByteOffset
758 << " FieldBitStart:" << AI.FieldBitStart
759 << " AccessWidth:" << AI.AccessWidth << "\n";
760 OS.indent(8 + strlen("<AccessInfo"));
761 OS << " AccessAlignment:" << AI.AccessAlignment
762 << " TargetBitOffset:" << AI.TargetBitOffset
763 << " TargetBitWidth:" << AI.TargetBitWidth
764 << ">\n";
765 }
766 OS.indent(4);
767 }
768 OS << "]>";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000769}
770
771void CGBitFieldInfo::dump() const {
772 print(llvm::errs());
773}