blob: ba2897ceef59228738f17f9e1c34dbf4353dc130 [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
45 /// ContainsPointerToDataMember - Whether one of the fields in this record
46 /// layout is a pointer to data member, or a struct that contains pointer to
47 /// data member.
48 bool ContainsPointerToDataMember;
49
50 /// Packed - Whether the resulting LLVM struct will be packed or not.
51 bool Packed;
52
53private:
54 CodeGenTypes &Types;
55
56 /// Alignment - Contains the alignment of the RecordDecl.
57 //
58 // FIXME: This is not needed and should be removed.
59 unsigned Alignment;
60
61 /// AlignmentAsLLVMStruct - Will contain the maximum alignment of all the
62 /// LLVM types.
63 unsigned AlignmentAsLLVMStruct;
64
65 /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field,
66 /// this will have the number of bits still available in the field.
67 char BitsAvailableInLastField;
68
69 /// NextFieldOffsetInBytes - Holds the next field offset in bytes.
70 uint64_t NextFieldOffsetInBytes;
71
Anders Carlsson86664462010-04-17 20:49:27 +000072 /// LayoutUnionField - Will layout a field in an union and return the type
73 /// that the field will have.
74 const llvm::Type *LayoutUnionField(const FieldDecl *Field,
75 const ASTRecordLayout &Layout);
76
Daniel Dunbar270e2032010-03-31 00:11:27 +000077 /// LayoutUnion - Will layout a union RecordDecl.
78 void LayoutUnion(const RecordDecl *D);
79
80 /// LayoutField - try to layout all fields in the record decl.
81 /// Returns false if the operation failed because the struct is not packed.
82 bool LayoutFields(const RecordDecl *D);
83
Anders Carlsson15ddfdc2010-05-18 05:12:20 +000084 /// LayoutNonVirtualBase - layout a single non-virtual base.
85 void LayoutNonVirtualBase(const CXXRecordDecl *BaseDecl,
86 uint64_t BaseOffset);
87
88 /// LayoutNonVirtualBases - layout the non-virtual bases of a record decl.
89 void LayoutNonVirtualBases(const CXXRecordDecl *RD,
90 const ASTRecordLayout &Layout);
Daniel Dunbar270e2032010-03-31 00:11:27 +000091
92 /// LayoutField - layout a single field. Returns false if the operation failed
93 /// because the current struct is not packed.
94 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
95
96 /// LayoutBitField - layout a single bit field.
97 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
98
99 /// AppendField - Appends a field with the given offset and type.
100 void AppendField(uint64_t FieldOffsetInBytes, const llvm::Type *FieldTy);
101
Daniel Dunbar270e2032010-03-31 00:11:27 +0000102 /// AppendPadding - Appends enough padding bytes so that the total
103 /// struct size is a multiple of the field alignment.
104 void AppendPadding(uint64_t FieldOffsetInBytes, unsigned FieldAlignment);
105
106 /// AppendBytes - Append a given number of bytes to the record.
107 void AppendBytes(uint64_t NumBytes);
108
109 /// AppendTailPadding - Append enough tail padding so that the type will have
110 /// the passed size.
111 void AppendTailPadding(uint64_t RecordSize);
112
113 unsigned getTypeAlignment(const llvm::Type *Ty) const;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000114
115 /// CheckForPointerToDataMember - Check if the given type contains a pointer
116 /// to data member.
117 void CheckForPointerToDataMember(QualType T);
118
119public:
120 CGRecordLayoutBuilder(CodeGenTypes &Types)
121 : ContainsPointerToDataMember(false), Packed(false), Types(Types),
122 Alignment(0), AlignmentAsLLVMStruct(1),
123 BitsAvailableInLastField(0), NextFieldOffsetInBytes(0) { }
124
125 /// Layout - Will layout a RecordDecl.
126 void Layout(const RecordDecl *D);
127};
128
129}
130}
131
Anders Carlsson45372a62009-07-23 03:17:50 +0000132void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
Anders Carlssona5dd7222009-08-08 19:38:24 +0000133 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment() / 8;
Anders Carlssond0eb3b92009-09-02 17:51:33 +0000134 Packed = D->hasAttr<PackedAttr>();
Anders Carlssona5dd7222009-08-08 19:38:24 +0000135
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000136 if (D->isUnion()) {
137 LayoutUnion(D);
138 return;
139 }
Anders Carlssona860e752009-08-08 18:23:56 +0000140
Anders Carlsson45372a62009-07-23 03:17:50 +0000141 if (LayoutFields(D))
142 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Anders Carlsson45372a62009-07-23 03:17:50 +0000144 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000145 Packed = true;
Anders Carlsson45372a62009-07-23 03:17:50 +0000146 AlignmentAsLLVMStruct = 1;
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000147 NextFieldOffsetInBytes = 0;
Anders Carlsson45372a62009-07-23 03:17:50 +0000148 FieldTypes.clear();
Anders Carlsson45372a62009-07-23 03:17:50 +0000149 LLVMFields.clear();
150 LLVMBitFields.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Anders Carlsson45372a62009-07-23 03:17:50 +0000152 LayoutFields(D);
153}
154
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000155static CGBitFieldInfo ComputeBitFieldInfo(CodeGenTypes &Types,
156 const FieldDecl *FD,
157 uint64_t FieldOffset,
158 uint64_t FieldSize) {
Daniel Dunbare1467a42010-04-22 02:35:46 +0000159 const RecordDecl *RD = FD->getParent();
Daniel Dunbar89da8742010-04-22 03:17:04 +0000160 const ASTRecordLayout &RL = Types.getContext().getASTRecordLayout(RD);
161 uint64_t ContainingTypeSizeInBits = RL.getSize();
162 unsigned ContainingTypeAlign = RL.getAlignment();
Daniel Dunbare1467a42010-04-22 02:35:46 +0000163
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000164 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(FD->getType());
Daniel Dunbarab970f92010-04-13 20:58:55 +0000165 uint64_t TypeSizeInBytes = Types.getTargetData().getTypeAllocSize(Ty);
166 uint64_t TypeSizeInBits = TypeSizeInBytes * 8;
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000167
168 bool IsSigned = FD->getType()->isSignedIntegerType();
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000169
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000170 if (FieldSize > TypeSizeInBits) {
Anders Carlsson6ba38152010-04-17 22:54:57 +0000171 // We have a wide bit-field. The extra bits are only used for padding, so
172 // if we have a bitfield of type T, with size N:
173 //
174 // T t : N;
175 //
176 // We can just assume that it's:
177 //
178 // T t : sizeof(T);
179 //
180 FieldSize = TypeSizeInBits;
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000181 }
182
Daniel Dunbare1467a42010-04-22 02:35:46 +0000183 // Compute the access components. The policy we use is to start by attempting
184 // to access using the width of the bit-field type itself and to always access
185 // at aligned indices of that type. If such an access would fail because it
186 // extends past the bound of the type, then we reduce size to the next smaller
187 // power of two and retry. The current algorithm assumes pow2 sized types,
188 // although this is easy to fix.
189 //
190 // FIXME: This algorithm is wrong on big-endian systems, I think.
191 assert(llvm::isPowerOf2_32(TypeSizeInBits) && "Unexpected type size!");
192 CGBitFieldInfo::AccessInfo Components[3];
193 unsigned NumComponents = 0;
194 unsigned AccessedTargetBits = 0; // The tumber of target bits accessed.
195 unsigned AccessWidth = TypeSizeInBits; // The current access width to attempt.
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000196
Daniel Dunbare1467a42010-04-22 02:35:46 +0000197 // Round down from the field offset to find the first access position that is
198 // at an aligned offset of the initial access type.
Daniel Dunbar52968a12010-04-22 15:22:33 +0000199 uint64_t AccessStart = FieldOffset - (FieldOffset % AccessWidth);
200
201 // Adjust initial access size to fit within record.
202 while (AccessWidth > 8 &&
203 AccessStart + AccessWidth > ContainingTypeSizeInBits) {
204 AccessWidth >>= 1;
205 AccessStart = FieldOffset - (FieldOffset % AccessWidth);
206 }
Daniel Dunbar2df25692010-04-15 05:09:32 +0000207
Daniel Dunbare1467a42010-04-22 02:35:46 +0000208 while (AccessedTargetBits < FieldSize) {
209 // Check that we can access using a type of this size, without reading off
210 // the end of the structure. This can occur with packed structures and
211 // -fno-bitfield-type-align, for example.
212 if (AccessStart + AccessWidth > ContainingTypeSizeInBits) {
213 // If so, reduce access size to the next smaller power-of-two and retry.
214 AccessWidth >>= 1;
215 assert(AccessWidth >= 8 && "Cannot access under byte size!");
216 continue;
217 }
Daniel Dunbarab970f92010-04-13 20:58:55 +0000218
Daniel Dunbare1467a42010-04-22 02:35:46 +0000219 // Otherwise, add an access component.
Daniel Dunbarab970f92010-04-13 20:58:55 +0000220
Daniel Dunbare1467a42010-04-22 02:35:46 +0000221 // First, compute the bits inside this access which are part of the
222 // target. We are reading bits [AccessStart, AccessStart + AccessWidth); the
223 // intersection with [FieldOffset, FieldOffset + FieldSize) gives the bits
224 // in the target that we are reading.
Daniel Dunbar52968a12010-04-22 15:22:33 +0000225 assert(FieldOffset < AccessStart + AccessWidth && "Invalid access start!");
226 assert(AccessStart < FieldOffset + FieldSize && "Invalid access start!");
Daniel Dunbare1467a42010-04-22 02:35:46 +0000227 uint64_t AccessBitsInFieldStart = std::max(AccessStart, FieldOffset);
228 uint64_t AccessBitsInFieldSize =
Daniel Dunbar52968a12010-04-22 15:22:33 +0000229 std::min(AccessWidth + AccessStart,
230 FieldOffset + FieldSize) - AccessBitsInFieldStart;
Daniel Dunbar4651efb2010-04-22 14:56:10 +0000231
Daniel Dunbare1467a42010-04-22 02:35:46 +0000232 assert(NumComponents < 3 && "Unexpected number of components!");
233 CGBitFieldInfo::AccessInfo &AI = Components[NumComponents++];
234 AI.FieldIndex = 0;
235 // FIXME: We still follow the old access pattern of only using the field
236 // byte offset. We should switch this once we fix the struct layout to be
237 // pretty.
238 AI.FieldByteOffset = AccessStart / 8;
239 AI.FieldBitStart = AccessBitsInFieldStart - AccessStart;
240 AI.AccessWidth = AccessWidth;
Daniel Dunbar89da8742010-04-22 03:17:04 +0000241 AI.AccessAlignment = llvm::MinAlign(ContainingTypeAlign, AccessStart) / 8;
Daniel Dunbare1467a42010-04-22 02:35:46 +0000242 AI.TargetBitOffset = AccessedTargetBits;
243 AI.TargetBitWidth = AccessBitsInFieldSize;
244
245 AccessStart += AccessWidth;
246 AccessedTargetBits += AI.TargetBitWidth;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000247 }
248
Daniel Dunbare1467a42010-04-22 02:35:46 +0000249 assert(AccessedTargetBits == FieldSize && "Invalid bit-field access!");
Daniel Dunbar2df25692010-04-15 05:09:32 +0000250 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000251}
252
Anders Carlsson45372a62009-07-23 03:17:50 +0000253void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
254 uint64_t FieldOffset) {
Mike Stump1eb44332009-09-09 15:08:12 +0000255 uint64_t FieldSize =
Anders Carlsson45372a62009-07-23 03:17:50 +0000256 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Anders Carlsson45372a62009-07-23 03:17:50 +0000258 if (FieldSize == 0)
259 return;
260
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000261 uint64_t NextFieldOffset = NextFieldOffsetInBytes * 8;
Anders Carlsson45372a62009-07-23 03:17:50 +0000262 unsigned NumBytesToAppend;
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Anders Carlsson45372a62009-07-23 03:17:50 +0000264 if (FieldOffset < NextFieldOffset) {
265 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000266 assert(NextFieldOffsetInBytes && "Must have laid out at least one byte!");
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Anders Carlsson45372a62009-07-23 03:17:50 +0000268 // The bitfield begins in the previous bit-field.
Mike Stump1eb44332009-09-09 15:08:12 +0000269 NumBytesToAppend =
Anders Carlsson45372a62009-07-23 03:17:50 +0000270 llvm::RoundUpToAlignment(FieldSize - BitsAvailableInLastField, 8) / 8;
271 } else {
272 assert(FieldOffset % 8 == 0 && "Field offset not aligned correctly");
273
274 // Append padding if necessary.
275 AppendBytes((FieldOffset - NextFieldOffset) / 8);
Mike Stump1eb44332009-09-09 15:08:12 +0000276
277 NumBytesToAppend =
Anders Carlsson45372a62009-07-23 03:17:50 +0000278 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Anders Carlsson45372a62009-07-23 03:17:50 +0000280 assert(NumBytesToAppend && "No bytes to append!");
281 }
282
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000283 // Add the bit field info.
284 LLVMBitFields.push_back(
285 LLVMBitFieldInfo(D, ComputeBitFieldInfo(Types, D, FieldOffset, FieldSize)));
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Anders Carlsson45372a62009-07-23 03:17:50 +0000287 AppendBytes(NumBytesToAppend);
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Mike Stump1eb44332009-09-09 15:08:12 +0000289 BitsAvailableInLastField =
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000290 NextFieldOffsetInBytes * 8 - (FieldOffset + FieldSize);
Anders Carlsson45372a62009-07-23 03:17:50 +0000291}
292
293bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
294 uint64_t FieldOffset) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000295 // If the field is packed, then we need a packed struct.
Anders Carlssona860e752009-08-08 18:23:56 +0000296 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson45372a62009-07-23 03:17:50 +0000297 return false;
298
299 if (D->isBitField()) {
300 // We must use packed structs for unnamed bit fields since they
301 // don't affect the struct alignment.
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000302 if (!Packed && !D->getDeclName())
Anders Carlsson45372a62009-07-23 03:17:50 +0000303 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Anders Carlsson45372a62009-07-23 03:17:50 +0000305 LayoutBitField(D, FieldOffset);
306 return true;
307 }
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Anders Carlsson2c12d032010-02-02 05:17:25 +0000309 // Check if we have a pointer to data member in this field.
310 CheckForPointerToDataMember(D->getType());
Daniel Dunbar270e2032010-03-31 00:11:27 +0000311
Anders Carlsson45372a62009-07-23 03:17:50 +0000312 assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!");
Anders Carlsson45372a62009-07-23 03:17:50 +0000313 uint64_t FieldOffsetInBytes = FieldOffset / 8;
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000315 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
316 unsigned TypeAlignment = getTypeAlignment(Ty);
317
Anders Carlssona5dd7222009-08-08 19:38:24 +0000318 // If the type alignment is larger then the struct alignment, we must use
319 // a packed struct.
320 if (TypeAlignment > Alignment) {
321 assert(!Packed && "Alignment is wrong even with packed struct!");
322 return false;
323 }
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Anders Carlssona5dd7222009-08-08 19:38:24 +0000325 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
326 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
327 if (const PragmaPackAttr *PPA = RD->getAttr<PragmaPackAttr>()) {
328 if (PPA->getAlignment() != TypeAlignment * 8 && !Packed)
329 return false;
330 }
331 }
332
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000333 // Round up the field offset to the alignment of the field type.
Mike Stump1eb44332009-09-09 15:08:12 +0000334 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000335 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, TypeAlignment);
336
337 if (FieldOffsetInBytes < AlignedNextFieldOffsetInBytes) {
338 assert(!Packed && "Could not place field even with packed struct!");
339 return false;
340 }
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000342 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
343 // Even with alignment, the field offset is not at the right place,
344 // insert padding.
345 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000347 AppendBytes(PaddingInBytes);
348 }
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Anders Carlsson45372a62009-07-23 03:17:50 +0000350 // Now append the field.
351 LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size()));
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000352 AppendField(FieldOffsetInBytes, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Anders Carlsson45372a62009-07-23 03:17:50 +0000354 return true;
355}
356
Anders Carlsson86664462010-04-17 20:49:27 +0000357const llvm::Type *
358CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
359 const ASTRecordLayout &Layout) {
360 if (Field->isBitField()) {
361 uint64_t FieldSize =
362 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
363
364 // Ignore zero sized bit fields.
365 if (FieldSize == 0)
366 return 0;
367
Daniel Dunbar8ab78a72010-04-20 17:52:30 +0000368 const llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
369 unsigned NumBytesToAppend =
370 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Anders Carlssond62328e2010-04-17 21:04:52 +0000371
Daniel Dunbar8ab78a72010-04-20 17:52:30 +0000372 if (NumBytesToAppend > 1)
373 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend);
Anders Carlssond62328e2010-04-17 21:04:52 +0000374
Anders Carlsson86664462010-04-17 20:49:27 +0000375 // Add the bit field info.
376 LLVMBitFields.push_back(
377 LLVMBitFieldInfo(Field, ComputeBitFieldInfo(Types, Field, 0, FieldSize)));
Anders Carlssond62328e2010-04-17 21:04:52 +0000378 return FieldTy;
Anders Carlsson86664462010-04-17 20:49:27 +0000379 }
Daniel Dunbar8ab78a72010-04-20 17:52:30 +0000380
Anders Carlsson86664462010-04-17 20:49:27 +0000381 // This is a regular union field.
382 LLVMFields.push_back(LLVMFieldInfo(Field, 0));
383 return Types.ConvertTypeForMemRecursive(Field->getType());
384}
385
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000386void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
387 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000389 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000391 const llvm::Type *Ty = 0;
392 uint64_t Size = 0;
393 unsigned Align = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000395 bool HasOnlyZeroSizedBitFields = true;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000396
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000397 unsigned FieldNo = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000398 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000399 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
Mike Stump1eb44332009-09-09 15:08:12 +0000400 assert(Layout.getFieldOffset(FieldNo) == 0 &&
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000401 "Union field offset did not start at the beginning of record!");
Anders Carlsson86664462010-04-17 20:49:27 +0000402 const llvm::Type *FieldTy = LayoutUnionField(*Field, Layout);
Anders Carlsson2cc8f172009-07-23 04:00:39 +0000403
Anders Carlsson86664462010-04-17 20:49:27 +0000404 if (!FieldTy)
405 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000407 HasOnlyZeroSizedBitFields = false;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000408
Anders Carlsson177d4d82009-07-23 21:52:03 +0000409 unsigned FieldAlign = Types.getTargetData().getABITypeAlignment(FieldTy);
410 uint64_t FieldSize = Types.getTargetData().getTypeAllocSize(FieldTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000412 if (FieldAlign < Align)
413 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000415 if (FieldAlign > Align || FieldSize > Size) {
416 Ty = FieldTy;
417 Align = FieldAlign;
418 Size = FieldSize;
419 }
420 }
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000422 // Now add our field.
Anders Carlsson36620002009-09-03 22:56:02 +0000423 if (Ty) {
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000424 AppendField(0, Ty);
Anders Carlsson36620002009-09-03 22:56:02 +0000425
426 if (getTypeAlignment(Ty) > Layout.getAlignment() / 8) {
427 // We need a packed struct.
428 Packed = true;
429 Align = 1;
430 }
431 }
Fariborz Jahaniane5041702009-11-06 20:47:40 +0000432 if (!Align) {
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000433 assert(HasOnlyZeroSizedBitFields &&
434 "0-align record did not have all zero-sized bit-fields!");
Fariborz Jahaniane5041702009-11-06 20:47:40 +0000435 Align = 1;
436 }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000437
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000438 // Append tail padding.
439 if (Layout.getSize() / 8 > Size)
440 AppendPadding(Layout.getSize() / 8, Align);
441}
442
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000443void CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *BaseDecl,
444 uint64_t BaseOffset) {
445 const ASTRecordLayout &Layout =
446 Types.getContext().getASTRecordLayout(BaseDecl);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000447
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000448 uint64_t NonVirtualSize = Layout.getNonVirtualSize();
449
450 if (BaseDecl->isEmpty()) {
451 // FIXME: Lay out empty bases.
452 return;
453 }
454
455 // FIXME: Actually use a better type than [sizeof(BaseDecl) x i8] when we can.
456 AppendPadding(BaseOffset / 8, 1);
457 AppendBytes(NonVirtualSize / 8);
458}
459
460void
461CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD,
462 const ASTRecordLayout &Layout) {
463 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
464
465 // Check if we need to add a vtable pointer.
466 if (RD->isDynamicClass()) {
467 if (!PrimaryBase) {
468 const llvm::Type *FunctionType =
469 llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()),
470 /*isVarArg=*/true);
471 const llvm::Type *VTableTy = FunctionType->getPointerTo();
472
473 assert(NextFieldOffsetInBytes == 0 &&
474 "VTable pointer must come first!");
475 AppendField(NextFieldOffsetInBytes, VTableTy->getPointerTo());
476 } else {
477 // FIXME: Handle a virtual primary base.
478 if (!Layout.getPrimaryBaseWasVirtual())
479 LayoutNonVirtualBase(PrimaryBase, 0);
480 }
481 }
482
483 // Layout the non-virtual bases.
484 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
485 E = RD->bases_end(); I != E; ++I) {
486 if (I->isVirtual())
487 continue;
488
489 const CXXRecordDecl *BaseDecl =
490 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
491
492 // We've already laid out the primary base.
493 if (BaseDecl == PrimaryBase && !Layout.getPrimaryBaseWasVirtual())
494 continue;
495
496 LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffset(BaseDecl));
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000497 }
498}
499
Anders Carlsson45372a62009-07-23 03:17:50 +0000500bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
501 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
Anders Carlssona5dd7222009-08-08 19:38:24 +0000502 assert(Alignment && "Did not set alignment!");
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000504 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000506 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D))
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000507 LayoutNonVirtualBases(RD, Layout);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000508
Anders Carlsson45372a62009-07-23 03:17:50 +0000509 unsigned FieldNo = 0;
Fariborz Jahaniancad86652009-07-27 20:57:45 +0000510
Mike Stump1eb44332009-09-09 15:08:12 +0000511 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson45372a62009-07-23 03:17:50 +0000512 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
513 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000514 assert(!Packed &&
Anders Carlsson45372a62009-07-23 03:17:50 +0000515 "Could not layout fields even with a packed LLVM struct!");
516 return false;
517 }
518 }
519
520 // Append tail padding if necessary.
Anders Carlssonc1efe362009-07-27 14:55:54 +0000521 AppendTailPadding(Layout.getSize());
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Anders Carlsson45372a62009-07-23 03:17:50 +0000523 return true;
524}
525
Anders Carlssonc1efe362009-07-27 14:55:54 +0000526void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) {
527 assert(RecordSize % 8 == 0 && "Invalid record size!");
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Anders Carlssonc1efe362009-07-27 14:55:54 +0000529 uint64_t RecordSizeInBytes = RecordSize / 8;
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000530 assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Daniel Dunbar270e2032010-03-31 00:11:27 +0000532 uint64_t AlignedNextFieldOffset =
Anders Carlssonc2456822009-12-08 01:24:23 +0000533 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, AlignmentAsLLVMStruct);
534
535 if (AlignedNextFieldOffset == RecordSizeInBytes) {
536 // We don't need any padding.
537 return;
538 }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000539
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000540 unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes;
Anders Carlssonc1efe362009-07-27 14:55:54 +0000541 AppendBytes(NumPadBytes);
542}
543
Mike Stump1eb44332009-09-09 15:08:12 +0000544void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes,
Anders Carlsson45372a62009-07-23 03:17:50 +0000545 const llvm::Type *FieldTy) {
546 AlignmentAsLLVMStruct = std::max(AlignmentAsLLVMStruct,
547 getTypeAlignment(FieldTy));
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000548
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000549 uint64_t FieldSizeInBytes = Types.getTargetData().getTypeAllocSize(FieldTy);
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000550
Anders Carlsson45372a62009-07-23 03:17:50 +0000551 FieldTypes.push_back(FieldTy);
Anders Carlsson45372a62009-07-23 03:17:50 +0000552
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000553 NextFieldOffsetInBytes = FieldOffsetInBytes + FieldSizeInBytes;
Anders Carlsson45372a62009-07-23 03:17:50 +0000554 BitsAvailableInLastField = 0;
555}
556
Mike Stump1eb44332009-09-09 15:08:12 +0000557void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
Anders Carlsson45372a62009-07-23 03:17:50 +0000558 unsigned FieldAlignment) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000559 assert(NextFieldOffsetInBytes <= FieldOffsetInBytes &&
560 "Incorrect field layout!");
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Anders Carlsson45372a62009-07-23 03:17:50 +0000562 // Round up the field offset to the alignment of the field type.
Mike Stump1eb44332009-09-09 15:08:12 +0000563 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson45372a62009-07-23 03:17:50 +0000564 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment);
565
566 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
567 // Even with alignment, the field offset is not at the right place,
568 // insert padding.
569 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
570
571 AppendBytes(PaddingInBytes);
572 }
573}
574
575void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) {
576 if (NumBytes == 0)
577 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000578
Owen Anderson0032b272009-08-13 21:57:51 +0000579 const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
Anders Carlssonc1efe362009-07-27 14:55:54 +0000580 if (NumBytes > 1)
Anders Carlsson45372a62009-07-23 03:17:50 +0000581 Ty = llvm::ArrayType::get(Ty, NumBytes);
Mike Stump1eb44332009-09-09 15:08:12 +0000582
Anders Carlsson45372a62009-07-23 03:17:50 +0000583 // Append the padding field
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000584 AppendField(NextFieldOffsetInBytes, Ty);
Anders Carlsson45372a62009-07-23 03:17:50 +0000585}
586
587unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000588 if (Packed)
Anders Carlsson45372a62009-07-23 03:17:50 +0000589 return 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Anders Carlsson45372a62009-07-23 03:17:50 +0000591 return Types.getTargetData().getABITypeAlignment(Ty);
592}
593
Anders Carlsson2c12d032010-02-02 05:17:25 +0000594void CGRecordLayoutBuilder::CheckForPointerToDataMember(QualType T) {
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000595 // This record already contains a member pointer.
Anders Carlsson2c12d032010-02-02 05:17:25 +0000596 if (ContainsPointerToDataMember)
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000597 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000599 // Can only have member pointers if we're compiling C++.
600 if (!Types.getContext().getLangOptions().CPlusPlus)
601 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Anders Carlsson2c12d032010-02-02 05:17:25 +0000603 T = Types.getContext().getBaseElementType(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Anders Carlsson2c12d032010-02-02 05:17:25 +0000605 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) {
606 if (!MPT->getPointeeType()->isFunctionType()) {
607 // We have a pointer to data member.
608 ContainsPointerToDataMember = true;
609 }
610 } else if (const RecordType *RT = T->getAs<RecordType>()) {
611 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Daniel Dunbar270e2032010-03-31 00:11:27 +0000612
Anders Carlsson2c12d032010-02-02 05:17:25 +0000613 // FIXME: It would be better if there was a way to explicitly compute the
614 // record layout instead of converting to a type.
615 Types.ConvertTagDeclType(RD);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000616
Anders Carlsson2c12d032010-02-02 05:17:25 +0000617 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000618
Anders Carlsson2c12d032010-02-02 05:17:25 +0000619 if (Layout.containsPointerToDataMember())
620 ContainsPointerToDataMember = true;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000621 }
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000622}
623
Daniel Dunbar270e2032010-03-31 00:11:27 +0000624CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) {
625 CGRecordLayoutBuilder Builder(*this);
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Anders Carlsson45372a62009-07-23 03:17:50 +0000627 Builder.Layout(D);
Anders Carlsson4c98efd2009-07-24 15:20:52 +0000628
Daniel Dunbar270e2032010-03-31 00:11:27 +0000629 const llvm::Type *Ty = llvm::StructType::get(getLLVMContext(),
Owen Anderson47a434f2009-08-05 23:18:46 +0000630 Builder.FieldTypes,
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000631 Builder.Packed);
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000633 CGRecordLayout *RL =
634 new CGRecordLayout(Ty, Builder.ContainsPointerToDataMember);
635
Anders Carlsson45372a62009-07-23 03:17:50 +0000636 // Add all the field numbers.
Daniel Dunbarc7a984a2010-04-06 01:07:41 +0000637 for (unsigned i = 0, e = Builder.LLVMFields.size(); i != e; ++i)
638 RL->FieldInfo.insert(Builder.LLVMFields[i]);
Anders Carlsson45372a62009-07-23 03:17:50 +0000639
640 // Add bitfield info.
Daniel Dunbarc7a984a2010-04-06 01:07:41 +0000641 for (unsigned i = 0, e = Builder.LLVMBitFields.size(); i != e; ++i)
642 RL->BitFields.insert(Builder.LLVMBitFields[i]);
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000644 // Dump the layout, if requested.
Daniel Dunbarab970f92010-04-13 20:58:55 +0000645 if (getContext().getLangOptions().DumpRecordLayouts) {
Daniel Dunbar8d8ab742010-04-19 20:44:53 +0000646 llvm::errs() << "\n*** Dumping IRgen Record Layout\n";
Daniel Dunbarab970f92010-04-13 20:58:55 +0000647 llvm::errs() << "Record: ";
648 D->dump();
649 llvm::errs() << "\nLayout: ";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000650 RL->dump();
Daniel Dunbarab970f92010-04-13 20:58:55 +0000651 }
Daniel Dunbar93c62962010-04-12 18:14:18 +0000652
Daniel Dunbare1467a42010-04-22 02:35:46 +0000653#ifndef NDEBUG
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000654 // Verify that the computed LLVM struct size matches the AST layout size.
Daniel Dunbare1467a42010-04-22 02:35:46 +0000655 uint64_t TypeSizeInBits = getContext().getASTRecordLayout(D).getSize();
656 assert(TypeSizeInBits == getTargetData().getTypeAllocSizeInBits(Ty) &&
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000657 "Type size mismatch!");
658
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000659 // Verify that the LLVM and AST field offsets agree.
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000660 const llvm::StructType *ST =
661 dyn_cast<llvm::StructType>(RL->getLLVMType());
662 const llvm::StructLayout *SL = getTargetData().getStructLayout(ST);
663
664 const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
665 RecordDecl::field_iterator it = D->field_begin();
666 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
667 const FieldDecl *FD = *it;
Daniel Dunbare1467a42010-04-22 02:35:46 +0000668
669 // For non-bit-fields, just check that the LLVM struct offset matches the
670 // AST offset.
671 if (!FD->isBitField()) {
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000672 unsigned FieldNo = RL->getLLVMFieldNo(FD);
673 assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
674 "Invalid field offset!");
Daniel Dunbare1467a42010-04-22 02:35:46 +0000675 continue;
676 }
677
678 // Ignore unnamed bit-fields.
679 if (!FD->getDeclName())
680 continue;
681
682 const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
683 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
684 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
685
686 // Verify that every component access is within the structure.
687 uint64_t FieldOffset = SL->getElementOffsetInBits(AI.FieldIndex);
688 uint64_t AccessBitOffset = FieldOffset + AI.FieldByteOffset * 8;
689 assert(AccessBitOffset + AI.AccessWidth <= TypeSizeInBits &&
690 "Invalid bit-field access (out of range)!");
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000691 }
692 }
693#endif
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000694
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000695 return RL;
Anders Carlsson45372a62009-07-23 03:17:50 +0000696}
Daniel Dunbar93c62962010-04-12 18:14:18 +0000697
698void CGRecordLayout::print(llvm::raw_ostream &OS) const {
699 OS << "<CGRecordLayout\n";
700 OS << " LLVMType:" << *LLVMType << "\n";
701 OS << " ContainsPointerToDataMember:" << ContainsPointerToDataMember << "\n";
702 OS << " BitFields:[\n";
Daniel Dunbarad759532010-04-22 02:35:36 +0000703
704 // Print bit-field infos in declaration order.
705 std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
Daniel Dunbar93c62962010-04-12 18:14:18 +0000706 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
707 it = BitFields.begin(), ie = BitFields.end();
708 it != ie; ++it) {
Daniel Dunbarad759532010-04-22 02:35:36 +0000709 const RecordDecl *RD = it->first->getParent();
710 unsigned Index = 0;
711 for (RecordDecl::field_iterator
712 it2 = RD->field_begin(); *it2 != it->first; ++it2)
713 ++Index;
714 BFIs.push_back(std::make_pair(Index, &it->second));
715 }
716 llvm::array_pod_sort(BFIs.begin(), BFIs.end());
717 for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
Daniel Dunbarab970f92010-04-13 20:58:55 +0000718 OS.indent(4);
Daniel Dunbarad759532010-04-22 02:35:36 +0000719 BFIs[i].second->print(OS);
Daniel Dunbar93c62962010-04-12 18:14:18 +0000720 OS << "\n";
721 }
Daniel Dunbarad759532010-04-22 02:35:36 +0000722
Daniel Dunbar93c62962010-04-12 18:14:18 +0000723 OS << "]>\n";
724}
725
726void CGRecordLayout::dump() const {
727 print(llvm::errs());
728}
729
730void CGBitFieldInfo::print(llvm::raw_ostream &OS) const {
731 OS << "<CGBitFieldInfo";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000732 OS << " Size:" << Size;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000733 OS << " IsSigned:" << IsSigned << "\n";
734
735 OS.indent(4 + strlen("<CGBitFieldInfo"));
736 OS << " NumComponents:" << getNumComponents();
737 OS << " Components: [";
738 if (getNumComponents()) {
739 OS << "\n";
740 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
741 const AccessInfo &AI = getComponent(i);
742 OS.indent(8);
743 OS << "<AccessInfo"
744 << " FieldIndex:" << AI.FieldIndex
745 << " FieldByteOffset:" << AI.FieldByteOffset
746 << " FieldBitStart:" << AI.FieldBitStart
747 << " AccessWidth:" << AI.AccessWidth << "\n";
748 OS.indent(8 + strlen("<AccessInfo"));
749 OS << " AccessAlignment:" << AI.AccessAlignment
750 << " TargetBitOffset:" << AI.TargetBitOffset
751 << " TargetBitWidth:" << AI.TargetBitWidth
752 << ">\n";
753 }
754 OS.indent(4);
755 }
756 OS << "]>";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000757}
758
759void CGBitFieldInfo::dump() const {
760 print(llvm::errs());
761}