blob: dc58faca1959af1b82d4cb2ee720fb2bad60924d [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"
Anders Carlsson46170f92010-11-24 22:50:27 +000017#include "clang/AST/CXXInheritance.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000018#include "clang/AST/DeclCXX.h"
19#include "clang/AST/Expr.h"
20#include "clang/AST/RecordLayout.h"
21#include "CodeGenTypes.h"
John McCallf16aa102010-08-22 21:01:12 +000022#include "CGCXXABI.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000023#include "llvm/DerivedTypes.h"
Daniel Dunbar93c62962010-04-12 18:14:18 +000024#include "llvm/Type.h"
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +000025#include "llvm/Support/Debug.h"
Daniel Dunbar93c62962010-04-12 18:14:18 +000026#include "llvm/Support/raw_ostream.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000027#include "llvm/Target/TargetData.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000028using namespace clang;
29using namespace CodeGen;
30
Daniel Dunbar270e2032010-03-31 00:11:27 +000031namespace clang {
32namespace CodeGen {
33
34class CGRecordLayoutBuilder {
35public:
36 /// FieldTypes - Holds the LLVM types that the struct is created from.
37 std::vector<const llvm::Type *> FieldTypes;
38
Anders Carlsson3d155e62010-11-09 05:25:47 +000039 /// NonVirtualBaseFieldTypes - Holds the LLVM types for the non-virtual part
40 /// of the struct. For example, consider:
41 ///
42 /// struct A { int i; };
43 /// struct B { void *v; };
44 /// struct C : virtual A, B { };
45 ///
46 /// The LLVM type of C will be
47 /// %struct.C = type { i32 (...)**, %struct.A, i32, %struct.B }
48 ///
49 /// And the LLVM type of the non-virtual base struct will be
50 /// %struct.C.base = type { i32 (...)**, %struct.A, i32 }
51 std::vector<const llvm::Type *> NonVirtualBaseFieldTypes;
52
53 /// NonVirtualBaseTypeIsSameAsCompleteType - Whether the non-virtual part of
54 /// the struct is equivalent to the complete struct.
55 bool NonVirtualBaseTypeIsSameAsCompleteType;
56
Daniel Dunbar270e2032010-03-31 00:11:27 +000057 /// LLVMFieldInfo - Holds a field and its corresponding LLVM field number.
58 typedef std::pair<const FieldDecl *, unsigned> LLVMFieldInfo;
59 llvm::SmallVector<LLVMFieldInfo, 16> LLVMFields;
60
61 /// LLVMBitFieldInfo - Holds location and size information about a bit field.
Daniel Dunbarc7a984a2010-04-06 01:07:41 +000062 typedef std::pair<const FieldDecl *, CGBitFieldInfo> LLVMBitFieldInfo;
Daniel Dunbar270e2032010-03-31 00:11:27 +000063 llvm::SmallVector<LLVMBitFieldInfo, 16> LLVMBitFields;
64
Anders Carlssonc6772ce2010-05-18 05:22:06 +000065 typedef std::pair<const CXXRecordDecl *, unsigned> LLVMBaseInfo;
66 llvm::SmallVector<LLVMBaseInfo, 16> LLVMNonVirtualBases;
Anders Carlsson46170f92010-11-24 22:50:27 +000067
68 /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are
69 /// primary base classes for some other direct or indirect base class.
70 CXXIndirectPrimaryBaseSet IndirectPrimaryBases;
71
John McCallf16aa102010-08-22 21:01:12 +000072 /// IsZeroInitializable - Whether this struct can be C++
73 /// zero-initialized with an LLVM zeroinitializer.
74 bool IsZeroInitializable;
Daniel Dunbar270e2032010-03-31 00:11:27 +000075
76 /// Packed - Whether the resulting LLVM struct will be packed or not.
77 bool Packed;
78
79private:
80 CodeGenTypes &Types;
81
82 /// Alignment - Contains the alignment of the RecordDecl.
83 //
84 // FIXME: This is not needed and should be removed.
85 unsigned Alignment;
86
87 /// AlignmentAsLLVMStruct - Will contain the maximum alignment of all the
88 /// LLVM types.
89 unsigned AlignmentAsLLVMStruct;
90
91 /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field,
92 /// this will have the number of bits still available in the field.
93 char BitsAvailableInLastField;
94
95 /// NextFieldOffsetInBytes - Holds the next field offset in bytes.
96 uint64_t NextFieldOffsetInBytes;
97
Anders Carlsson86664462010-04-17 20:49:27 +000098 /// LayoutUnionField - Will layout a field in an union and return the type
99 /// that the field will have.
100 const llvm::Type *LayoutUnionField(const FieldDecl *Field,
101 const ASTRecordLayout &Layout);
102
Daniel Dunbar270e2032010-03-31 00:11:27 +0000103 /// LayoutUnion - Will layout a union RecordDecl.
104 void LayoutUnion(const RecordDecl *D);
105
106 /// LayoutField - try to layout all fields in the record decl.
107 /// Returns false if the operation failed because the struct is not packed.
108 bool LayoutFields(const RecordDecl *D);
109
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000110 /// LayoutVirtualBase - layout a single virtual base.
111 void LayoutVirtualBase(const CXXRecordDecl *BaseDecl, uint64_t BaseOffset);
112
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000113 /// LayoutNonVirtualBase - layout a single non-virtual base.
114 void LayoutNonVirtualBase(const CXXRecordDecl *BaseDecl,
115 uint64_t BaseOffset);
116
117 /// LayoutNonVirtualBases - layout the non-virtual bases of a record decl.
118 void LayoutNonVirtualBases(const CXXRecordDecl *RD,
119 const ASTRecordLayout &Layout);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000120
Anders Carlsson3d155e62010-11-09 05:25:47 +0000121 /// ComputeNonVirtualBaseType - Compute the non-virtual base field types.
122 void ComputeNonVirtualBaseType(const CXXRecordDecl *RD);
123
Daniel Dunbar270e2032010-03-31 00:11:27 +0000124 /// LayoutField - layout a single field. Returns false if the operation failed
125 /// because the current struct is not packed.
126 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
127
128 /// LayoutBitField - layout a single bit field.
129 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
130
131 /// AppendField - Appends a field with the given offset and type.
132 void AppendField(uint64_t FieldOffsetInBytes, const llvm::Type *FieldTy);
133
Daniel Dunbar270e2032010-03-31 00:11:27 +0000134 /// AppendPadding - Appends enough padding bytes so that the total
135 /// struct size is a multiple of the field alignment.
136 void AppendPadding(uint64_t FieldOffsetInBytes, unsigned FieldAlignment);
137
Anders Carlsson3d155e62010-11-09 05:25:47 +0000138 /// getByteArrayType - Returns a byte array type with the given number of
139 /// elements.
140 const llvm::Type *getByteArrayType(uint64_t NumBytes);
141
Daniel Dunbar270e2032010-03-31 00:11:27 +0000142 /// AppendBytes - Append a given number of bytes to the record.
143 void AppendBytes(uint64_t NumBytes);
144
145 /// AppendTailPadding - Append enough tail padding so that the type will have
146 /// the passed size.
147 void AppendTailPadding(uint64_t RecordSize);
148
149 unsigned getTypeAlignment(const llvm::Type *Ty) const;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000150
John McCallf16aa102010-08-22 21:01:12 +0000151 /// CheckZeroInitializable - Check if the given type contains a pointer
Daniel Dunbar270e2032010-03-31 00:11:27 +0000152 /// to data member.
John McCallf16aa102010-08-22 21:01:12 +0000153 void CheckZeroInitializable(QualType T);
154 void CheckZeroInitializable(const CXXRecordDecl *RD);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000155
156public:
157 CGRecordLayoutBuilder(CodeGenTypes &Types)
Anders Carlsson3d155e62010-11-09 05:25:47 +0000158 : NonVirtualBaseTypeIsSameAsCompleteType(false), IsZeroInitializable(true),
159 Packed(false), Types(Types), Alignment(0), AlignmentAsLLVMStruct(1),
Daniel Dunbar270e2032010-03-31 00:11:27 +0000160 BitsAvailableInLastField(0), NextFieldOffsetInBytes(0) { }
161
162 /// Layout - Will layout a RecordDecl.
163 void Layout(const RecordDecl *D);
164};
165
166}
167}
168
Anders Carlsson45372a62009-07-23 03:17:50 +0000169void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
Anders Carlssona5dd7222009-08-08 19:38:24 +0000170 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment() / 8;
Anders Carlssond0eb3b92009-09-02 17:51:33 +0000171 Packed = D->hasAttr<PackedAttr>();
Anders Carlssona5dd7222009-08-08 19:38:24 +0000172
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000173 if (D->isUnion()) {
174 LayoutUnion(D);
175 return;
176 }
Anders Carlssona860e752009-08-08 18:23:56 +0000177
Anders Carlsson45372a62009-07-23 03:17:50 +0000178 if (LayoutFields(D))
179 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Anders Carlsson45372a62009-07-23 03:17:50 +0000181 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000182 Packed = true;
Anders Carlsson45372a62009-07-23 03:17:50 +0000183 AlignmentAsLLVMStruct = 1;
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000184 NextFieldOffsetInBytes = 0;
Anders Carlsson45372a62009-07-23 03:17:50 +0000185 FieldTypes.clear();
Anders Carlsson45372a62009-07-23 03:17:50 +0000186 LLVMFields.clear();
187 LLVMBitFields.clear();
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000188 LLVMNonVirtualBases.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Anders Carlsson45372a62009-07-23 03:17:50 +0000190 LayoutFields(D);
191}
192
Daniel Dunbare7a80bd2010-09-02 23:53:28 +0000193CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
194 const FieldDecl *FD,
195 uint64_t FieldOffset,
196 uint64_t FieldSize,
197 uint64_t ContainingTypeSizeInBits,
198 unsigned ContainingTypeAlign) {
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000199 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(FD->getType());
Daniel Dunbarab970f92010-04-13 20:58:55 +0000200 uint64_t TypeSizeInBytes = Types.getTargetData().getTypeAllocSize(Ty);
201 uint64_t TypeSizeInBits = TypeSizeInBytes * 8;
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000202
203 bool IsSigned = FD->getType()->isSignedIntegerType();
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000204
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000205 if (FieldSize > TypeSizeInBits) {
Anders Carlsson6ba38152010-04-17 22:54:57 +0000206 // We have a wide bit-field. The extra bits are only used for padding, so
207 // if we have a bitfield of type T, with size N:
208 //
209 // T t : N;
210 //
211 // We can just assume that it's:
212 //
213 // T t : sizeof(T);
214 //
215 FieldSize = TypeSizeInBits;
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000216 }
217
Daniel Dunbare1467a42010-04-22 02:35:46 +0000218 // Compute the access components. The policy we use is to start by attempting
219 // to access using the width of the bit-field type itself and to always access
220 // at aligned indices of that type. If such an access would fail because it
221 // extends past the bound of the type, then we reduce size to the next smaller
222 // power of two and retry. The current algorithm assumes pow2 sized types,
223 // although this is easy to fix.
224 //
225 // FIXME: This algorithm is wrong on big-endian systems, I think.
226 assert(llvm::isPowerOf2_32(TypeSizeInBits) && "Unexpected type size!");
227 CGBitFieldInfo::AccessInfo Components[3];
228 unsigned NumComponents = 0;
229 unsigned AccessedTargetBits = 0; // The tumber of target bits accessed.
230 unsigned AccessWidth = TypeSizeInBits; // The current access width to attempt.
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000231
Daniel Dunbare1467a42010-04-22 02:35:46 +0000232 // Round down from the field offset to find the first access position that is
233 // at an aligned offset of the initial access type.
Daniel Dunbar52968a12010-04-22 15:22:33 +0000234 uint64_t AccessStart = FieldOffset - (FieldOffset % AccessWidth);
235
236 // Adjust initial access size to fit within record.
237 while (AccessWidth > 8 &&
238 AccessStart + AccessWidth > ContainingTypeSizeInBits) {
239 AccessWidth >>= 1;
240 AccessStart = FieldOffset - (FieldOffset % AccessWidth);
241 }
Daniel Dunbar2df25692010-04-15 05:09:32 +0000242
Daniel Dunbare1467a42010-04-22 02:35:46 +0000243 while (AccessedTargetBits < FieldSize) {
244 // Check that we can access using a type of this size, without reading off
245 // the end of the structure. This can occur with packed structures and
246 // -fno-bitfield-type-align, for example.
247 if (AccessStart + AccessWidth > ContainingTypeSizeInBits) {
248 // If so, reduce access size to the next smaller power-of-two and retry.
249 AccessWidth >>= 1;
250 assert(AccessWidth >= 8 && "Cannot access under byte size!");
251 continue;
252 }
Daniel Dunbarab970f92010-04-13 20:58:55 +0000253
Daniel Dunbare1467a42010-04-22 02:35:46 +0000254 // Otherwise, add an access component.
Daniel Dunbarab970f92010-04-13 20:58:55 +0000255
Daniel Dunbare1467a42010-04-22 02:35:46 +0000256 // First, compute the bits inside this access which are part of the
257 // target. We are reading bits [AccessStart, AccessStart + AccessWidth); the
258 // intersection with [FieldOffset, FieldOffset + FieldSize) gives the bits
259 // in the target that we are reading.
Daniel Dunbar52968a12010-04-22 15:22:33 +0000260 assert(FieldOffset < AccessStart + AccessWidth && "Invalid access start!");
261 assert(AccessStart < FieldOffset + FieldSize && "Invalid access start!");
Daniel Dunbare1467a42010-04-22 02:35:46 +0000262 uint64_t AccessBitsInFieldStart = std::max(AccessStart, FieldOffset);
263 uint64_t AccessBitsInFieldSize =
Daniel Dunbar52968a12010-04-22 15:22:33 +0000264 std::min(AccessWidth + AccessStart,
265 FieldOffset + FieldSize) - AccessBitsInFieldStart;
Daniel Dunbar4651efb2010-04-22 14:56:10 +0000266
Daniel Dunbare1467a42010-04-22 02:35:46 +0000267 assert(NumComponents < 3 && "Unexpected number of components!");
268 CGBitFieldInfo::AccessInfo &AI = Components[NumComponents++];
269 AI.FieldIndex = 0;
270 // FIXME: We still follow the old access pattern of only using the field
271 // byte offset. We should switch this once we fix the struct layout to be
272 // pretty.
273 AI.FieldByteOffset = AccessStart / 8;
274 AI.FieldBitStart = AccessBitsInFieldStart - AccessStart;
275 AI.AccessWidth = AccessWidth;
Daniel Dunbar89da8742010-04-22 03:17:04 +0000276 AI.AccessAlignment = llvm::MinAlign(ContainingTypeAlign, AccessStart) / 8;
Daniel Dunbare1467a42010-04-22 02:35:46 +0000277 AI.TargetBitOffset = AccessedTargetBits;
278 AI.TargetBitWidth = AccessBitsInFieldSize;
279
280 AccessStart += AccessWidth;
281 AccessedTargetBits += AI.TargetBitWidth;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000282 }
283
Daniel Dunbare1467a42010-04-22 02:35:46 +0000284 assert(AccessedTargetBits == FieldSize && "Invalid bit-field access!");
Daniel Dunbar2df25692010-04-15 05:09:32 +0000285 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000286}
287
Daniel Dunbare7a80bd2010-09-02 23:53:28 +0000288CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
289 const FieldDecl *FD,
290 uint64_t FieldOffset,
291 uint64_t FieldSize) {
292 const RecordDecl *RD = FD->getParent();
293 const ASTRecordLayout &RL = Types.getContext().getASTRecordLayout(RD);
294 uint64_t ContainingTypeSizeInBits = RL.getSize();
295 unsigned ContainingTypeAlign = RL.getAlignment();
296
297 return MakeInfo(Types, FD, FieldOffset, FieldSize, ContainingTypeSizeInBits,
298 ContainingTypeAlign);
299}
300
Anders Carlsson45372a62009-07-23 03:17:50 +0000301void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
302 uint64_t FieldOffset) {
Mike Stump1eb44332009-09-09 15:08:12 +0000303 uint64_t FieldSize =
Anders Carlsson45372a62009-07-23 03:17:50 +0000304 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Anders Carlsson45372a62009-07-23 03:17:50 +0000306 if (FieldSize == 0)
307 return;
308
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000309 uint64_t NextFieldOffset = NextFieldOffsetInBytes * 8;
Anders Carlsson45372a62009-07-23 03:17:50 +0000310 unsigned NumBytesToAppend;
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Anders Carlsson45372a62009-07-23 03:17:50 +0000312 if (FieldOffset < NextFieldOffset) {
313 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000314 assert(NextFieldOffsetInBytes && "Must have laid out at least one byte!");
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Anders Carlsson45372a62009-07-23 03:17:50 +0000316 // The bitfield begins in the previous bit-field.
Mike Stump1eb44332009-09-09 15:08:12 +0000317 NumBytesToAppend =
Anders Carlsson45372a62009-07-23 03:17:50 +0000318 llvm::RoundUpToAlignment(FieldSize - BitsAvailableInLastField, 8) / 8;
319 } else {
320 assert(FieldOffset % 8 == 0 && "Field offset not aligned correctly");
321
322 // Append padding if necessary.
323 AppendBytes((FieldOffset - NextFieldOffset) / 8);
Mike Stump1eb44332009-09-09 15:08:12 +0000324
325 NumBytesToAppend =
Anders Carlsson45372a62009-07-23 03:17:50 +0000326 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Anders Carlsson45372a62009-07-23 03:17:50 +0000328 assert(NumBytesToAppend && "No bytes to append!");
329 }
330
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000331 // Add the bit field info.
332 LLVMBitFields.push_back(
Daniel Dunbare7a80bd2010-09-02 23:53:28 +0000333 LLVMBitFieldInfo(D, CGBitFieldInfo::MakeInfo(Types, D, FieldOffset,
334 FieldSize)));
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Anders Carlsson45372a62009-07-23 03:17:50 +0000336 AppendBytes(NumBytesToAppend);
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Mike Stump1eb44332009-09-09 15:08:12 +0000338 BitsAvailableInLastField =
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000339 NextFieldOffsetInBytes * 8 - (FieldOffset + FieldSize);
Anders Carlsson45372a62009-07-23 03:17:50 +0000340}
341
342bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
343 uint64_t FieldOffset) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000344 // If the field is packed, then we need a packed struct.
Anders Carlssona860e752009-08-08 18:23:56 +0000345 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson45372a62009-07-23 03:17:50 +0000346 return false;
347
348 if (D->isBitField()) {
349 // We must use packed structs for unnamed bit fields since they
350 // don't affect the struct alignment.
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000351 if (!Packed && !D->getDeclName())
Anders Carlsson45372a62009-07-23 03:17:50 +0000352 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Anders Carlsson45372a62009-07-23 03:17:50 +0000354 LayoutBitField(D, FieldOffset);
355 return true;
356 }
Mike Stump1eb44332009-09-09 15:08:12 +0000357
John McCallf16aa102010-08-22 21:01:12 +0000358 CheckZeroInitializable(D->getType());
Daniel Dunbar270e2032010-03-31 00:11:27 +0000359
Anders Carlsson45372a62009-07-23 03:17:50 +0000360 assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!");
Anders Carlsson45372a62009-07-23 03:17:50 +0000361 uint64_t FieldOffsetInBytes = FieldOffset / 8;
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000363 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
364 unsigned TypeAlignment = getTypeAlignment(Ty);
365
Anders Carlssona5dd7222009-08-08 19:38:24 +0000366 // If the type alignment is larger then the struct alignment, we must use
367 // a packed struct.
368 if (TypeAlignment > Alignment) {
369 assert(!Packed && "Alignment is wrong even with packed struct!");
370 return false;
371 }
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Anders Carlssona5dd7222009-08-08 19:38:24 +0000373 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
374 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
Daniel Dunbar8a2c92c2010-05-27 01:12:46 +0000375 if (const MaxFieldAlignmentAttr *MFAA =
376 RD->getAttr<MaxFieldAlignmentAttr>()) {
377 if (MFAA->getAlignment() != TypeAlignment * 8 && !Packed)
Anders Carlssona5dd7222009-08-08 19:38:24 +0000378 return false;
379 }
380 }
381
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000382 // Round up the field offset to the alignment of the field type.
Mike Stump1eb44332009-09-09 15:08:12 +0000383 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000384 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, TypeAlignment);
385
386 if (FieldOffsetInBytes < AlignedNextFieldOffsetInBytes) {
387 assert(!Packed && "Could not place field even with packed struct!");
388 return false;
389 }
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000391 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
392 // Even with alignment, the field offset is not at the right place,
393 // insert padding.
394 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000396 AppendBytes(PaddingInBytes);
397 }
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Anders Carlsson45372a62009-07-23 03:17:50 +0000399 // Now append the field.
400 LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size()));
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000401 AppendField(FieldOffsetInBytes, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Anders Carlsson45372a62009-07-23 03:17:50 +0000403 return true;
404}
405
Anders Carlsson86664462010-04-17 20:49:27 +0000406const llvm::Type *
407CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
408 const ASTRecordLayout &Layout) {
409 if (Field->isBitField()) {
410 uint64_t FieldSize =
411 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
412
413 // Ignore zero sized bit fields.
414 if (FieldSize == 0)
415 return 0;
416
Daniel Dunbar8ab78a72010-04-20 17:52:30 +0000417 const llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
418 unsigned NumBytesToAppend =
419 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Anders Carlssond62328e2010-04-17 21:04:52 +0000420
Daniel Dunbar8ab78a72010-04-20 17:52:30 +0000421 if (NumBytesToAppend > 1)
422 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend);
Anders Carlssond62328e2010-04-17 21:04:52 +0000423
Anders Carlsson86664462010-04-17 20:49:27 +0000424 // Add the bit field info.
425 LLVMBitFields.push_back(
Daniel Dunbare7a80bd2010-09-02 23:53:28 +0000426 LLVMBitFieldInfo(Field, CGBitFieldInfo::MakeInfo(Types, Field,
427 0, FieldSize)));
Anders Carlssond62328e2010-04-17 21:04:52 +0000428 return FieldTy;
Anders Carlsson86664462010-04-17 20:49:27 +0000429 }
Daniel Dunbar8ab78a72010-04-20 17:52:30 +0000430
Anders Carlsson86664462010-04-17 20:49:27 +0000431 // This is a regular union field.
432 LLVMFields.push_back(LLVMFieldInfo(Field, 0));
433 return Types.ConvertTypeForMemRecursive(Field->getType());
434}
435
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000436void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
437 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000439 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000441 const llvm::Type *Ty = 0;
442 uint64_t Size = 0;
443 unsigned Align = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000445 bool HasOnlyZeroSizedBitFields = true;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000446
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000447 unsigned FieldNo = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000448 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000449 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
Mike Stump1eb44332009-09-09 15:08:12 +0000450 assert(Layout.getFieldOffset(FieldNo) == 0 &&
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000451 "Union field offset did not start at the beginning of record!");
Anders Carlsson86664462010-04-17 20:49:27 +0000452 const llvm::Type *FieldTy = LayoutUnionField(*Field, Layout);
Anders Carlsson2cc8f172009-07-23 04:00:39 +0000453
Anders Carlsson86664462010-04-17 20:49:27 +0000454 if (!FieldTy)
455 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000457 HasOnlyZeroSizedBitFields = false;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000458
Anders Carlsson177d4d82009-07-23 21:52:03 +0000459 unsigned FieldAlign = Types.getTargetData().getABITypeAlignment(FieldTy);
460 uint64_t FieldSize = Types.getTargetData().getTypeAllocSize(FieldTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000462 if (FieldAlign < Align)
463 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000465 if (FieldAlign > Align || FieldSize > Size) {
466 Ty = FieldTy;
467 Align = FieldAlign;
468 Size = FieldSize;
469 }
470 }
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000472 // Now add our field.
Anders Carlsson36620002009-09-03 22:56:02 +0000473 if (Ty) {
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000474 AppendField(0, Ty);
Anders Carlsson36620002009-09-03 22:56:02 +0000475
476 if (getTypeAlignment(Ty) > Layout.getAlignment() / 8) {
477 // We need a packed struct.
478 Packed = true;
479 Align = 1;
480 }
481 }
Fariborz Jahaniane5041702009-11-06 20:47:40 +0000482 if (!Align) {
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000483 assert(HasOnlyZeroSizedBitFields &&
484 "0-align record did not have all zero-sized bit-fields!");
Fariborz Jahaniane5041702009-11-06 20:47:40 +0000485 Align = 1;
486 }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000487
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000488 // Append tail padding.
489 if (Layout.getSize() / 8 > Size)
490 AppendPadding(Layout.getSize() / 8, Align);
491}
492
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000493void
494CGRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *BaseDecl,
495 uint64_t BaseOffset) {
496 // Ignore empty bases.
497 if (BaseDecl->isEmpty())
498 return;
499
500 CheckZeroInitializable(BaseDecl);
501
502 const ASTRecordLayout &Layout =
503 Types.getContext().getASTRecordLayout(BaseDecl);
504
505 uint64_t NonVirtualSize = Layout.getNonVirtualSize();
506
507 // FIXME: Actually use a better type than [sizeof(BaseDecl) x i8] when we can.
508 AppendPadding(BaseOffset / 8, 1);
509
510 // FIXME: Add the vbase field info.
511
512 AppendBytes(NonVirtualSize / 8);
513
514}
515
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000516void CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *BaseDecl,
517 uint64_t BaseOffset) {
Anders Carlssona03613d2010-11-22 00:03:08 +0000518 // Ignore empty bases.
519 if (BaseDecl->isEmpty())
520 return;
521
522 CheckZeroInitializable(BaseDecl);
523
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000524 const ASTRecordLayout &Layout =
525 Types.getContext().getASTRecordLayout(BaseDecl);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000526
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000527 uint64_t NonVirtualSize = Layout.getNonVirtualSize();
528
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000529 // FIXME: Actually use a better type than [sizeof(BaseDecl) x i8] when we can.
530 AppendPadding(BaseOffset / 8, 1);
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000531
532 // Append the base field.
533 LLVMNonVirtualBases.push_back(LLVMBaseInfo(BaseDecl, FieldTypes.size()));
534
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000535 AppendBytes(NonVirtualSize / 8);
536}
537
538void
539CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD,
540 const ASTRecordLayout &Layout) {
541 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
542
543 // Check if we need to add a vtable pointer.
544 if (RD->isDynamicClass()) {
545 if (!PrimaryBase) {
546 const llvm::Type *FunctionType =
547 llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()),
548 /*isVarArg=*/true);
549 const llvm::Type *VTableTy = FunctionType->getPointerTo();
550
551 assert(NextFieldOffsetInBytes == 0 &&
552 "VTable pointer must come first!");
553 AppendField(NextFieldOffsetInBytes, VTableTy->getPointerTo());
554 } else {
Anders Carlssonc9e814b2010-11-24 23:12:57 +0000555 if (!Layout.isPrimaryBaseVirtual())
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000556 LayoutNonVirtualBase(PrimaryBase, 0);
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000557 else
558 LayoutVirtualBase(PrimaryBase, 0);
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000559 }
560 }
561
562 // Layout the non-virtual bases.
563 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
564 E = RD->bases_end(); I != E; ++I) {
565 if (I->isVirtual())
566 continue;
567
568 const CXXRecordDecl *BaseDecl =
569 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
570
571 // We've already laid out the primary base.
Anders Carlssonc9e814b2010-11-24 23:12:57 +0000572 if (BaseDecl == PrimaryBase && !Layout.isPrimaryBaseVirtual())
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000573 continue;
574
Anders Carlssona14f5972010-10-31 23:22:37 +0000575 LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffsetInBits(BaseDecl));
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000576 }
577}
578
Anders Carlsson3d155e62010-11-09 05:25:47 +0000579void
580CGRecordLayoutBuilder::ComputeNonVirtualBaseType(const CXXRecordDecl *RD) {
581 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(RD);
582
583 uint64_t AlignedNonVirtualTypeSize =
584 llvm::RoundUpToAlignment(Layout.getNonVirtualSize(),
585 Layout.getNonVirtualAlign()) / 8;
586
587
588 // First check if we can use the same fields as for the complete class.
589 if (AlignedNonVirtualTypeSize == Layout.getSize() / 8) {
590 NonVirtualBaseTypeIsSameAsCompleteType = true;
591 return;
592 }
593
594 NonVirtualBaseFieldTypes = FieldTypes;
595
596 // Check if we need padding.
597 uint64_t AlignedNextFieldOffset =
598 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, AlignmentAsLLVMStruct);
599
600 assert(AlignedNextFieldOffset <= AlignedNonVirtualTypeSize &&
601 "Size mismatch!");
602
603 if (AlignedNonVirtualTypeSize == AlignedNextFieldOffset) {
604 // We don't need any padding.
605 return;
606 }
607
608 uint64_t NumBytes = AlignedNonVirtualTypeSize - AlignedNextFieldOffset;
609 NonVirtualBaseFieldTypes.push_back(getByteArrayType(NumBytes));
Anders Carlsson3d155e62010-11-09 05:25:47 +0000610}
611
Anders Carlsson45372a62009-07-23 03:17:50 +0000612bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
613 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
Anders Carlssona5dd7222009-08-08 19:38:24 +0000614 assert(Alignment && "Did not set alignment!");
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000616 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Anders Carlsson3d155e62010-11-09 05:25:47 +0000618 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
619 if (RD)
Anders Carlsson15ddfdc2010-05-18 05:12:20 +0000620 LayoutNonVirtualBases(RD, Layout);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000621
Anders Carlsson45372a62009-07-23 03:17:50 +0000622 unsigned FieldNo = 0;
Fariborz Jahaniancad86652009-07-27 20:57:45 +0000623
Mike Stump1eb44332009-09-09 15:08:12 +0000624 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson45372a62009-07-23 03:17:50 +0000625 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
626 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000627 assert(!Packed &&
Anders Carlsson45372a62009-07-23 03:17:50 +0000628 "Could not layout fields even with a packed LLVM struct!");
629 return false;
630 }
631 }
632
Anders Carlsson3d155e62010-11-09 05:25:47 +0000633 // We've laid out the non-virtual bases and the fields, now compute the
634 // non-virtual base field types.
635 if (RD)
636 ComputeNonVirtualBaseType(RD);
637
Anders Carlsson8f2c6892010-11-25 01:59:35 +0000638 if (RD) {
639 RD->getIndirectPrimaryBases(IndirectPrimaryBases);
640 }
641
Anders Carlsson3d155e62010-11-09 05:25:47 +0000642 // FIXME: Lay out the virtual bases instead of just treating them as tail
643 // padding.
644
Anders Carlsson45372a62009-07-23 03:17:50 +0000645 // Append tail padding if necessary.
Anders Carlssonc1efe362009-07-27 14:55:54 +0000646 AppendTailPadding(Layout.getSize());
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Anders Carlsson45372a62009-07-23 03:17:50 +0000648 return true;
649}
650
Anders Carlssonc1efe362009-07-27 14:55:54 +0000651void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) {
652 assert(RecordSize % 8 == 0 && "Invalid record size!");
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Anders Carlssonc1efe362009-07-27 14:55:54 +0000654 uint64_t RecordSizeInBytes = RecordSize / 8;
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000655 assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Daniel Dunbar270e2032010-03-31 00:11:27 +0000657 uint64_t AlignedNextFieldOffset =
Anders Carlssonc2456822009-12-08 01:24:23 +0000658 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, AlignmentAsLLVMStruct);
659
660 if (AlignedNextFieldOffset == RecordSizeInBytes) {
661 // We don't need any padding.
662 return;
663 }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000664
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000665 unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes;
Anders Carlssonc1efe362009-07-27 14:55:54 +0000666 AppendBytes(NumPadBytes);
667}
668
Mike Stump1eb44332009-09-09 15:08:12 +0000669void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes,
Anders Carlsson45372a62009-07-23 03:17:50 +0000670 const llvm::Type *FieldTy) {
671 AlignmentAsLLVMStruct = std::max(AlignmentAsLLVMStruct,
672 getTypeAlignment(FieldTy));
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000673
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000674 uint64_t FieldSizeInBytes = Types.getTargetData().getTypeAllocSize(FieldTy);
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000675
Anders Carlsson45372a62009-07-23 03:17:50 +0000676 FieldTypes.push_back(FieldTy);
Anders Carlsson45372a62009-07-23 03:17:50 +0000677
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000678 NextFieldOffsetInBytes = FieldOffsetInBytes + FieldSizeInBytes;
Anders Carlsson45372a62009-07-23 03:17:50 +0000679 BitsAvailableInLastField = 0;
680}
681
Mike Stump1eb44332009-09-09 15:08:12 +0000682void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
Anders Carlsson45372a62009-07-23 03:17:50 +0000683 unsigned FieldAlignment) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000684 assert(NextFieldOffsetInBytes <= FieldOffsetInBytes &&
685 "Incorrect field layout!");
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Anders Carlsson45372a62009-07-23 03:17:50 +0000687 // Round up the field offset to the alignment of the field type.
Mike Stump1eb44332009-09-09 15:08:12 +0000688 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson45372a62009-07-23 03:17:50 +0000689 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment);
690
691 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
692 // Even with alignment, the field offset is not at the right place,
693 // insert padding.
694 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
695
696 AppendBytes(PaddingInBytes);
697 }
698}
699
Anders Carlsson3d155e62010-11-09 05:25:47 +0000700const llvm::Type *CGRecordLayoutBuilder::getByteArrayType(uint64_t NumBytes) {
701 assert(NumBytes != 0 && "Empty byte array's aren't allowed.");
Mike Stump1eb44332009-09-09 15:08:12 +0000702
Owen Anderson0032b272009-08-13 21:57:51 +0000703 const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
Anders Carlssonc1efe362009-07-27 14:55:54 +0000704 if (NumBytes > 1)
Anders Carlsson45372a62009-07-23 03:17:50 +0000705 Ty = llvm::ArrayType::get(Ty, NumBytes);
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Anders Carlsson3d155e62010-11-09 05:25:47 +0000707 return Ty;
708}
709
710void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) {
711 if (NumBytes == 0)
712 return;
713
Anders Carlsson45372a62009-07-23 03:17:50 +0000714 // Append the padding field
Anders Carlsson3d155e62010-11-09 05:25:47 +0000715 AppendField(NextFieldOffsetInBytes, getByteArrayType(NumBytes));
Anders Carlsson45372a62009-07-23 03:17:50 +0000716}
717
718unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000719 if (Packed)
Anders Carlsson45372a62009-07-23 03:17:50 +0000720 return 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Anders Carlsson45372a62009-07-23 03:17:50 +0000722 return Types.getTargetData().getABITypeAlignment(Ty);
723}
724
John McCallf16aa102010-08-22 21:01:12 +0000725void CGRecordLayoutBuilder::CheckZeroInitializable(QualType T) {
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000726 // This record already contains a member pointer.
John McCallf16aa102010-08-22 21:01:12 +0000727 if (!IsZeroInitializable)
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000728 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000730 // Can only have member pointers if we're compiling C++.
731 if (!Types.getContext().getLangOptions().CPlusPlus)
732 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Anders Carlsson2c12d032010-02-02 05:17:25 +0000734 T = Types.getContext().getBaseElementType(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Anders Carlsson2c12d032010-02-02 05:17:25 +0000736 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) {
John McCallf16aa102010-08-22 21:01:12 +0000737 if (!Types.getCXXABI().isZeroInitializable(MPT))
738 IsZeroInitializable = false;
Anders Carlsson2c12d032010-02-02 05:17:25 +0000739 } else if (const RecordType *RT = T->getAs<RecordType>()) {
740 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
John McCallf16aa102010-08-22 21:01:12 +0000741 CheckZeroInitializable(RD);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000742 }
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000743}
744
John McCallf16aa102010-08-22 21:01:12 +0000745void CGRecordLayoutBuilder::CheckZeroInitializable(const CXXRecordDecl *RD) {
Anders Carlssona83fb4b2010-05-18 16:51:41 +0000746 // This record already contains a member pointer.
John McCallf16aa102010-08-22 21:01:12 +0000747 if (!IsZeroInitializable)
Anders Carlssona83fb4b2010-05-18 16:51:41 +0000748 return;
749
Anders Carlsson3379e9b2010-11-24 19:57:04 +0000750 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
John McCallf16aa102010-08-22 21:01:12 +0000751 if (!Layout.isZeroInitializable())
752 IsZeroInitializable = false;
Anders Carlssona83fb4b2010-05-18 16:51:41 +0000753}
754
Daniel Dunbar270e2032010-03-31 00:11:27 +0000755CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) {
756 CGRecordLayoutBuilder Builder(*this);
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Anders Carlsson45372a62009-07-23 03:17:50 +0000758 Builder.Layout(D);
Anders Carlsson4c98efd2009-07-24 15:20:52 +0000759
Anders Carlssonba2c2ee2010-11-24 19:37:16 +0000760 const llvm::StructType *Ty = llvm::StructType::get(getLLVMContext(),
761 Builder.FieldTypes,
762 Builder.Packed);
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Anders Carlssonba2c2ee2010-11-24 19:37:16 +0000764 const llvm::StructType *BaseTy = 0;
Anders Carlsson3d155e62010-11-09 05:25:47 +0000765 if (isa<CXXRecordDecl>(D)) {
766 if (Builder.NonVirtualBaseTypeIsSameAsCompleteType)
767 BaseTy = Ty;
768 else if (!Builder.NonVirtualBaseFieldTypes.empty())
769 BaseTy = llvm::StructType::get(getLLVMContext(),
770 Builder.NonVirtualBaseFieldTypes,
771 Builder.Packed);
772 }
773
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000774 CGRecordLayout *RL =
Anders Carlsson3d155e62010-11-09 05:25:47 +0000775 new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable);
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000776
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000777 // Add all the non-virtual base field numbers.
778 RL->NonVirtualBaseFields.insert(Builder.LLVMNonVirtualBases.begin(),
779 Builder.LLVMNonVirtualBases.end());
780
Anders Carlsson45372a62009-07-23 03:17:50 +0000781 // Add all the field numbers.
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000782 RL->FieldInfo.insert(Builder.LLVMFields.begin(),
783 Builder.LLVMFields.end());
Anders Carlsson45372a62009-07-23 03:17:50 +0000784
785 // Add bitfield info.
Anders Carlssonc6772ce2010-05-18 05:22:06 +0000786 RL->BitFields.insert(Builder.LLVMBitFields.begin(),
787 Builder.LLVMBitFields.end());
Mike Stump1eb44332009-09-09 15:08:12 +0000788
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000789 // Dump the layout, if requested.
Daniel Dunbarab970f92010-04-13 20:58:55 +0000790 if (getContext().getLangOptions().DumpRecordLayouts) {
Daniel Dunbar8d8ab742010-04-19 20:44:53 +0000791 llvm::errs() << "\n*** Dumping IRgen Record Layout\n";
Daniel Dunbarab970f92010-04-13 20:58:55 +0000792 llvm::errs() << "Record: ";
793 D->dump();
794 llvm::errs() << "\nLayout: ";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000795 RL->dump();
Daniel Dunbarab970f92010-04-13 20:58:55 +0000796 }
Daniel Dunbar93c62962010-04-12 18:14:18 +0000797
Daniel Dunbare1467a42010-04-22 02:35:46 +0000798#ifndef NDEBUG
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000799 // Verify that the computed LLVM struct size matches the AST layout size.
Anders Carlsson3d155e62010-11-09 05:25:47 +0000800 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
801
802 uint64_t TypeSizeInBits = Layout.getSize();
Daniel Dunbare1467a42010-04-22 02:35:46 +0000803 assert(TypeSizeInBits == getTargetData().getTypeAllocSizeInBits(Ty) &&
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000804 "Type size mismatch!");
805
Anders Carlsson3d155e62010-11-09 05:25:47 +0000806 if (BaseTy) {
807 uint64_t AlignedNonVirtualTypeSizeInBits =
808 llvm::RoundUpToAlignment(Layout.getNonVirtualSize(),
809 Layout.getNonVirtualAlign());
810
811 assert(AlignedNonVirtualTypeSizeInBits ==
812 getTargetData().getTypeAllocSizeInBits(BaseTy) &&
813 "Type size mismatch!");
814 }
815
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000816 // Verify that the LLVM and AST field offsets agree.
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000817 const llvm::StructType *ST =
818 dyn_cast<llvm::StructType>(RL->getLLVMType());
819 const llvm::StructLayout *SL = getTargetData().getStructLayout(ST);
820
821 const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
822 RecordDecl::field_iterator it = D->field_begin();
823 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
824 const FieldDecl *FD = *it;
Daniel Dunbare1467a42010-04-22 02:35:46 +0000825
826 // For non-bit-fields, just check that the LLVM struct offset matches the
827 // AST offset.
828 if (!FD->isBitField()) {
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000829 unsigned FieldNo = RL->getLLVMFieldNo(FD);
830 assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
831 "Invalid field offset!");
Daniel Dunbare1467a42010-04-22 02:35:46 +0000832 continue;
833 }
834
835 // Ignore unnamed bit-fields.
836 if (!FD->getDeclName())
837 continue;
838
839 const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
840 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
841 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
842
843 // Verify that every component access is within the structure.
844 uint64_t FieldOffset = SL->getElementOffsetInBits(AI.FieldIndex);
845 uint64_t AccessBitOffset = FieldOffset + AI.FieldByteOffset * 8;
846 assert(AccessBitOffset + AI.AccessWidth <= TypeSizeInBits &&
847 "Invalid bit-field access (out of range)!");
Daniel Dunbar3b2ae7a2010-04-21 19:10:49 +0000848 }
849 }
850#endif
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000851
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000852 return RL;
Anders Carlsson45372a62009-07-23 03:17:50 +0000853}
Daniel Dunbar93c62962010-04-12 18:14:18 +0000854
855void CGRecordLayout::print(llvm::raw_ostream &OS) const {
856 OS << "<CGRecordLayout\n";
857 OS << " LLVMType:" << *LLVMType << "\n";
Anders Carlsson9a5a3f22010-11-21 23:59:45 +0000858 if (NonVirtualBaseLLVMType)
859 OS << " NonVirtualBaseLLVMType:" << *NonVirtualBaseLLVMType << "\n";
John McCallf16aa102010-08-22 21:01:12 +0000860 OS << " IsZeroInitializable:" << IsZeroInitializable << "\n";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000861 OS << " BitFields:[\n";
Daniel Dunbarad759532010-04-22 02:35:36 +0000862
863 // Print bit-field infos in declaration order.
864 std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
Daniel Dunbar93c62962010-04-12 18:14:18 +0000865 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
866 it = BitFields.begin(), ie = BitFields.end();
867 it != ie; ++it) {
Daniel Dunbarad759532010-04-22 02:35:36 +0000868 const RecordDecl *RD = it->first->getParent();
869 unsigned Index = 0;
870 for (RecordDecl::field_iterator
871 it2 = RD->field_begin(); *it2 != it->first; ++it2)
872 ++Index;
873 BFIs.push_back(std::make_pair(Index, &it->second));
874 }
875 llvm::array_pod_sort(BFIs.begin(), BFIs.end());
876 for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
Daniel Dunbarab970f92010-04-13 20:58:55 +0000877 OS.indent(4);
Daniel Dunbarad759532010-04-22 02:35:36 +0000878 BFIs[i].second->print(OS);
Daniel Dunbar93c62962010-04-12 18:14:18 +0000879 OS << "\n";
880 }
Daniel Dunbarad759532010-04-22 02:35:36 +0000881
Daniel Dunbar93c62962010-04-12 18:14:18 +0000882 OS << "]>\n";
883}
884
885void CGRecordLayout::dump() const {
886 print(llvm::errs());
887}
888
889void CGBitFieldInfo::print(llvm::raw_ostream &OS) const {
890 OS << "<CGBitFieldInfo";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000891 OS << " Size:" << Size;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000892 OS << " IsSigned:" << IsSigned << "\n";
893
894 OS.indent(4 + strlen("<CGBitFieldInfo"));
895 OS << " NumComponents:" << getNumComponents();
896 OS << " Components: [";
897 if (getNumComponents()) {
898 OS << "\n";
899 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
900 const AccessInfo &AI = getComponent(i);
901 OS.indent(8);
902 OS << "<AccessInfo"
903 << " FieldIndex:" << AI.FieldIndex
904 << " FieldByteOffset:" << AI.FieldByteOffset
905 << " FieldBitStart:" << AI.FieldBitStart
906 << " AccessWidth:" << AI.AccessWidth << "\n";
907 OS.indent(8 + strlen("<AccessInfo"));
908 OS << " AccessAlignment:" << AI.AccessAlignment
909 << " TargetBitOffset:" << AI.TargetBitOffset
910 << " TargetBitWidth:" << AI.TargetBitWidth
911 << ">\n";
912 }
913 OS.indent(4);
914 }
915 OS << "]>";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000916}
917
918void CGBitFieldInfo::dump() const {
919 print(llvm::errs());
920}