blob: af0c9ed499a5c0ef4ca639834ad112026bda2bae [file] [log] [blame]
Daniel Dunbar23ee4b72010-03-31 00:11:27 +00001//===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder ----*- C++ -*-===//
Anders Carlsson307846f2009-07-23 03:17:50 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000010// Builder implementation for CGRecordLayout objects.
Anders Carlsson307846f2009-07-23 03:17:50 +000011//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000014#include "CGRecordLayout.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/Attr.h"
Anders Carlsson4131f002010-11-24 22:50:27 +000017#include "clang/AST/CXXInheritance.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000018#include "clang/AST/DeclCXX.h"
19#include "clang/AST/Expr.h"
20#include "clang/AST/RecordLayout.h"
21#include "CodeGenTypes.h"
John McCall614dbdc2010-08-22 21:01:12 +000022#include "CGCXXABI.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000023#include "llvm/DerivedTypes.h"
Daniel Dunbarb97bff92010-04-12 18:14:18 +000024#include "llvm/Type.h"
Daniel Dunbar2ba67442010-04-21 19:10:49 +000025#include "llvm/Support/Debug.h"
Daniel Dunbarb97bff92010-04-12 18:14:18 +000026#include "llvm/Support/raw_ostream.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000027#include "llvm/Target/TargetData.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000028using namespace clang;
29using namespace CodeGen;
30
John McCallbcd38212010-11-30 23:17:27 +000031namespace {
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000032
33class CGRecordLayoutBuilder {
34public:
35 /// FieldTypes - Holds the LLVM types that the struct is created from.
36 std::vector<const llvm::Type *> FieldTypes;
37
Anders Carlssonc1351ca2010-11-09 05:25:47 +000038 /// NonVirtualBaseFieldTypes - Holds the LLVM types for the non-virtual part
39 /// of the struct. For example, consider:
40 ///
41 /// struct A { int i; };
42 /// struct B { void *v; };
43 /// struct C : virtual A, B { };
44 ///
45 /// The LLVM type of C will be
46 /// %struct.C = type { i32 (...)**, %struct.A, i32, %struct.B }
47 ///
48 /// And the LLVM type of the non-virtual base struct will be
49 /// %struct.C.base = type { i32 (...)**, %struct.A, i32 }
50 std::vector<const llvm::Type *> NonVirtualBaseFieldTypes;
51
52 /// NonVirtualBaseTypeIsSameAsCompleteType - Whether the non-virtual part of
53 /// the struct is equivalent to the complete struct.
54 bool NonVirtualBaseTypeIsSameAsCompleteType;
55
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000056 /// LLVMFieldInfo - Holds a field and its corresponding LLVM field number.
57 typedef std::pair<const FieldDecl *, unsigned> LLVMFieldInfo;
58 llvm::SmallVector<LLVMFieldInfo, 16> LLVMFields;
59
60 /// LLVMBitFieldInfo - Holds location and size information about a bit field.
Daniel Dunbard4549102010-04-06 01:07:41 +000061 typedef std::pair<const FieldDecl *, CGBitFieldInfo> LLVMBitFieldInfo;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000062 llvm::SmallVector<LLVMBitFieldInfo, 16> LLVMBitFields;
63
Anders Carlsson061ca522010-05-18 05:22:06 +000064 typedef std::pair<const CXXRecordDecl *, unsigned> LLVMBaseInfo;
65 llvm::SmallVector<LLVMBaseInfo, 16> LLVMNonVirtualBases;
Anders Carlsson4131f002010-11-24 22:50:27 +000066
67 /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are
68 /// primary base classes for some other direct or indirect base class.
69 CXXIndirectPrimaryBaseSet IndirectPrimaryBases;
70
Anders Carlssona459adb2010-11-28 19:18:44 +000071 /// LaidOutVirtualBases - A set of all laid out virtual bases, used to avoid
72 /// avoid laying out virtual bases more than once.
73 llvm::SmallPtrSet<const CXXRecordDecl *, 4> LaidOutVirtualBases;
74
John McCall614dbdc2010-08-22 21:01:12 +000075 /// IsZeroInitializable - Whether this struct can be C++
76 /// zero-initialized with an LLVM zeroinitializer.
77 bool IsZeroInitializable;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000078
79 /// Packed - Whether the resulting LLVM struct will be packed or not.
80 bool Packed;
81
82private:
83 CodeGenTypes &Types;
84
85 /// Alignment - Contains the alignment of the RecordDecl.
86 //
87 // FIXME: This is not needed and should be removed.
88 unsigned Alignment;
89
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000090 /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field,
91 /// this will have the number of bits still available in the field.
92 char BitsAvailableInLastField;
93
94 /// NextFieldOffsetInBytes - Holds the next field offset in bytes.
95 uint64_t NextFieldOffsetInBytes;
96
Anders Carlsson1de2f572010-04-17 20:49:27 +000097 /// LayoutUnionField - Will layout a field in an union and return the type
98 /// that the field will have.
99 const llvm::Type *LayoutUnionField(const FieldDecl *Field,
100 const ASTRecordLayout &Layout);
101
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000102 /// LayoutUnion - Will layout a union RecordDecl.
103 void LayoutUnion(const RecordDecl *D);
104
105 /// LayoutField - try to layout all fields in the record decl.
106 /// Returns false if the operation failed because the struct is not packed.
107 bool LayoutFields(const RecordDecl *D);
108
Anders Carlssona518b2a2010-12-04 23:59:48 +0000109 /// Layout a single base, virtual or non-virtual
110 void LayoutBase(const CXXRecordDecl *BaseDecl, uint64_t BaseOffset);
111
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000112 /// LayoutVirtualBase - layout a single virtual base.
113 void LayoutVirtualBase(const CXXRecordDecl *BaseDecl, uint64_t BaseOffset);
114
Anders Carlssona459adb2010-11-28 19:18:44 +0000115 /// LayoutVirtualBases - layout the virtual bases of a record decl.
116 void LayoutVirtualBases(const CXXRecordDecl *RD,
117 const ASTRecordLayout &Layout);
118
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000119 /// LayoutNonVirtualBase - layout a single non-virtual base.
120 void LayoutNonVirtualBase(const CXXRecordDecl *BaseDecl,
121 uint64_t BaseOffset);
122
Anders Carlssona459adb2010-11-28 19:18:44 +0000123 /// LayoutNonVirtualBases - layout the virtual bases of a record decl.
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000124 void LayoutNonVirtualBases(const CXXRecordDecl *RD,
125 const ASTRecordLayout &Layout);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000126
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000127 /// ComputeNonVirtualBaseType - Compute the non-virtual base field types.
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000128 bool ComputeNonVirtualBaseType(const CXXRecordDecl *RD);
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000129
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000130 /// LayoutField - layout a single field. Returns false if the operation failed
131 /// because the current struct is not packed.
132 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
133
134 /// LayoutBitField - layout a single bit field.
135 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
136
137 /// AppendField - Appends a field with the given offset and type.
138 void AppendField(uint64_t FieldOffsetInBytes, const llvm::Type *FieldTy);
139
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000140 /// AppendPadding - Appends enough padding bytes so that the total
141 /// struct size is a multiple of the field alignment.
Anders Carlssond74cad82010-12-04 23:53:18 +0000142 void AppendPadding(uint64_t FieldOffsetInBytes,
143 unsigned FieldAlignmentInBytes);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000144
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000145 /// getByteArrayType - Returns a byte array type with the given number of
146 /// elements.
147 const llvm::Type *getByteArrayType(uint64_t NumBytes);
148
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000149 /// AppendBytes - Append a given number of bytes to the record.
150 void AppendBytes(uint64_t NumBytes);
151
152 /// AppendTailPadding - Append enough tail padding so that the type will have
153 /// the passed size.
154 void AppendTailPadding(uint64_t RecordSize);
155
156 unsigned getTypeAlignment(const llvm::Type *Ty) const;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000157
Anders Carlssonacf877b2010-11-28 23:06:23 +0000158 /// getAlignmentAsLLVMStruct - Returns the maximum alignment of all the
159 /// LLVM element types.
160 unsigned getAlignmentAsLLVMStruct() const;
161
John McCall614dbdc2010-08-22 21:01:12 +0000162 /// CheckZeroInitializable - Check if the given type contains a pointer
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000163 /// to data member.
John McCall614dbdc2010-08-22 21:01:12 +0000164 void CheckZeroInitializable(QualType T);
165 void CheckZeroInitializable(const CXXRecordDecl *RD);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000166
167public:
168 CGRecordLayoutBuilder(CodeGenTypes &Types)
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000169 : NonVirtualBaseTypeIsSameAsCompleteType(false), IsZeroInitializable(true),
Anders Carlssonacf877b2010-11-28 23:06:23 +0000170 Packed(false), Types(Types), Alignment(0), BitsAvailableInLastField(0),
171 NextFieldOffsetInBytes(0) { }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000172
173 /// Layout - Will layout a RecordDecl.
174 void Layout(const RecordDecl *D);
175};
176
177}
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000178
Anders Carlsson307846f2009-07-23 03:17:50 +0000179void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000180 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment() / 8;
Anders Carlsson09a37742009-09-02 17:51:33 +0000181 Packed = D->hasAttr<PackedAttr>();
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000182
Anders Carlsson697f6592009-07-23 03:43:54 +0000183 if (D->isUnion()) {
184 LayoutUnion(D);
185 return;
186 }
Anders Carlsson68e0b682009-08-08 18:23:56 +0000187
Anders Carlsson307846f2009-07-23 03:17:50 +0000188 if (LayoutFields(D))
189 return;
Mike Stump11289f42009-09-09 15:08:12 +0000190
Anders Carlsson307846f2009-07-23 03:17:50 +0000191 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlssond78fc892009-07-23 17:24:40 +0000192 Packed = true;
Anders Carlssond5d64132009-07-28 17:56:36 +0000193 NextFieldOffsetInBytes = 0;
Anders Carlsson307846f2009-07-23 03:17:50 +0000194 FieldTypes.clear();
Anders Carlsson307846f2009-07-23 03:17:50 +0000195 LLVMFields.clear();
196 LLVMBitFields.clear();
Anders Carlsson061ca522010-05-18 05:22:06 +0000197 LLVMNonVirtualBases.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000198
Anders Carlsson307846f2009-07-23 03:17:50 +0000199 LayoutFields(D);
200}
201
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000202CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
203 const FieldDecl *FD,
204 uint64_t FieldOffset,
205 uint64_t FieldSize,
206 uint64_t ContainingTypeSizeInBits,
207 unsigned ContainingTypeAlign) {
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000208 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(FD->getType());
Daniel Dunbarb935b932010-04-13 20:58:55 +0000209 uint64_t TypeSizeInBytes = Types.getTargetData().getTypeAllocSize(Ty);
210 uint64_t TypeSizeInBits = TypeSizeInBytes * 8;
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000211
212 bool IsSigned = FD->getType()->isSignedIntegerType();
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000213
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000214 if (FieldSize > TypeSizeInBits) {
Anders Carlssond5f27b02010-04-17 22:54:57 +0000215 // We have a wide bit-field. The extra bits are only used for padding, so
216 // if we have a bitfield of type T, with size N:
217 //
218 // T t : N;
219 //
220 // We can just assume that it's:
221 //
222 // T t : sizeof(T);
223 //
224 FieldSize = TypeSizeInBits;
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000225 }
226
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000227 // Compute the access components. The policy we use is to start by attempting
228 // to access using the width of the bit-field type itself and to always access
229 // at aligned indices of that type. If such an access would fail because it
230 // extends past the bound of the type, then we reduce size to the next smaller
231 // power of two and retry. The current algorithm assumes pow2 sized types,
232 // although this is easy to fix.
233 //
234 // FIXME: This algorithm is wrong on big-endian systems, I think.
235 assert(llvm::isPowerOf2_32(TypeSizeInBits) && "Unexpected type size!");
236 CGBitFieldInfo::AccessInfo Components[3];
237 unsigned NumComponents = 0;
238 unsigned AccessedTargetBits = 0; // The tumber of target bits accessed.
239 unsigned AccessWidth = TypeSizeInBits; // The current access width to attempt.
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000240
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000241 // Round down from the field offset to find the first access position that is
242 // at an aligned offset of the initial access type.
Daniel Dunbar59813772010-04-22 15:22:33 +0000243 uint64_t AccessStart = FieldOffset - (FieldOffset % AccessWidth);
244
245 // Adjust initial access size to fit within record.
246 while (AccessWidth > 8 &&
247 AccessStart + AccessWidth > ContainingTypeSizeInBits) {
248 AccessWidth >>= 1;
249 AccessStart = FieldOffset - (FieldOffset % AccessWidth);
250 }
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000251
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000252 while (AccessedTargetBits < FieldSize) {
253 // Check that we can access using a type of this size, without reading off
254 // the end of the structure. This can occur with packed structures and
255 // -fno-bitfield-type-align, for example.
256 if (AccessStart + AccessWidth > ContainingTypeSizeInBits) {
257 // If so, reduce access size to the next smaller power-of-two and retry.
258 AccessWidth >>= 1;
259 assert(AccessWidth >= 8 && "Cannot access under byte size!");
260 continue;
261 }
Daniel Dunbarb935b932010-04-13 20:58:55 +0000262
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000263 // Otherwise, add an access component.
Daniel Dunbarb935b932010-04-13 20:58:55 +0000264
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000265 // First, compute the bits inside this access which are part of the
266 // target. We are reading bits [AccessStart, AccessStart + AccessWidth); the
267 // intersection with [FieldOffset, FieldOffset + FieldSize) gives the bits
268 // in the target that we are reading.
Daniel Dunbar59813772010-04-22 15:22:33 +0000269 assert(FieldOffset < AccessStart + AccessWidth && "Invalid access start!");
270 assert(AccessStart < FieldOffset + FieldSize && "Invalid access start!");
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000271 uint64_t AccessBitsInFieldStart = std::max(AccessStart, FieldOffset);
272 uint64_t AccessBitsInFieldSize =
Daniel Dunbar59813772010-04-22 15:22:33 +0000273 std::min(AccessWidth + AccessStart,
274 FieldOffset + FieldSize) - AccessBitsInFieldStart;
Daniel Dunbar5d6c07e2010-04-22 14:56:10 +0000275
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000276 assert(NumComponents < 3 && "Unexpected number of components!");
277 CGBitFieldInfo::AccessInfo &AI = Components[NumComponents++];
278 AI.FieldIndex = 0;
279 // FIXME: We still follow the old access pattern of only using the field
280 // byte offset. We should switch this once we fix the struct layout to be
281 // pretty.
282 AI.FieldByteOffset = AccessStart / 8;
283 AI.FieldBitStart = AccessBitsInFieldStart - AccessStart;
284 AI.AccessWidth = AccessWidth;
Daniel Dunbarfc66e0e2010-04-22 03:17:04 +0000285 AI.AccessAlignment = llvm::MinAlign(ContainingTypeAlign, AccessStart) / 8;
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000286 AI.TargetBitOffset = AccessedTargetBits;
287 AI.TargetBitWidth = AccessBitsInFieldSize;
288
289 AccessStart += AccessWidth;
290 AccessedTargetBits += AI.TargetBitWidth;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000291 }
292
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000293 assert(AccessedTargetBits == FieldSize && "Invalid bit-field access!");
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000294 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000295}
296
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000297CGBitFieldInfo CGBitFieldInfo::MakeInfo(CodeGenTypes &Types,
298 const FieldDecl *FD,
299 uint64_t FieldOffset,
300 uint64_t FieldSize) {
301 const RecordDecl *RD = FD->getParent();
302 const ASTRecordLayout &RL = Types.getContext().getASTRecordLayout(RD);
Ken Dyckc8ae5502011-02-09 01:59:34 +0000303 uint64_t ContainingTypeSizeInBits =
304 RL.getSize().getQuantity() * Types.getContext().getCharWidth();
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000305 unsigned ContainingTypeAlign = RL.getAlignment();
306
307 return MakeInfo(Types, FD, FieldOffset, FieldSize, ContainingTypeSizeInBits,
308 ContainingTypeAlign);
309}
310
Anders Carlsson307846f2009-07-23 03:17:50 +0000311void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
312 uint64_t FieldOffset) {
Mike Stump11289f42009-09-09 15:08:12 +0000313 uint64_t FieldSize =
Anders Carlsson307846f2009-07-23 03:17:50 +0000314 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump11289f42009-09-09 15:08:12 +0000315
Anders Carlsson307846f2009-07-23 03:17:50 +0000316 if (FieldSize == 0)
317 return;
318
Anders Carlssond5d64132009-07-28 17:56:36 +0000319 uint64_t NextFieldOffset = NextFieldOffsetInBytes * 8;
Anders Carlsson307846f2009-07-23 03:17:50 +0000320 unsigned NumBytesToAppend;
Mike Stump11289f42009-09-09 15:08:12 +0000321
Anders Carlsson307846f2009-07-23 03:17:50 +0000322 if (FieldOffset < NextFieldOffset) {
323 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
Anders Carlssond5d64132009-07-28 17:56:36 +0000324 assert(NextFieldOffsetInBytes && "Must have laid out at least one byte!");
Mike Stump11289f42009-09-09 15:08:12 +0000325
Anders Carlsson307846f2009-07-23 03:17:50 +0000326 // The bitfield begins in the previous bit-field.
Mike Stump11289f42009-09-09 15:08:12 +0000327 NumBytesToAppend =
Anders Carlsson307846f2009-07-23 03:17:50 +0000328 llvm::RoundUpToAlignment(FieldSize - BitsAvailableInLastField, 8) / 8;
329 } else {
330 assert(FieldOffset % 8 == 0 && "Field offset not aligned correctly");
331
332 // Append padding if necessary.
Anders Carlssond74cad82010-12-04 23:53:18 +0000333 AppendPadding(FieldOffset / 8, 1);
Mike Stump11289f42009-09-09 15:08:12 +0000334
335 NumBytesToAppend =
Anders Carlsson307846f2009-07-23 03:17:50 +0000336 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Mike Stump11289f42009-09-09 15:08:12 +0000337
Anders Carlsson307846f2009-07-23 03:17:50 +0000338 assert(NumBytesToAppend && "No bytes to append!");
339 }
340
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000341 // Add the bit field info.
342 LLVMBitFields.push_back(
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000343 LLVMBitFieldInfo(D, CGBitFieldInfo::MakeInfo(Types, D, FieldOffset,
344 FieldSize)));
Mike Stump11289f42009-09-09 15:08:12 +0000345
Anders Carlsson307846f2009-07-23 03:17:50 +0000346 AppendBytes(NumBytesToAppend);
Mike Stump11289f42009-09-09 15:08:12 +0000347
Mike Stump11289f42009-09-09 15:08:12 +0000348 BitsAvailableInLastField =
Anders Carlssond5d64132009-07-28 17:56:36 +0000349 NextFieldOffsetInBytes * 8 - (FieldOffset + FieldSize);
Anders Carlsson307846f2009-07-23 03:17:50 +0000350}
351
352bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
353 uint64_t FieldOffset) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000354 // If the field is packed, then we need a packed struct.
Anders Carlsson68e0b682009-08-08 18:23:56 +0000355 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson307846f2009-07-23 03:17:50 +0000356 return false;
357
358 if (D->isBitField()) {
359 // We must use packed structs for unnamed bit fields since they
360 // don't affect the struct alignment.
Anders Carlssond78fc892009-07-23 17:24:40 +0000361 if (!Packed && !D->getDeclName())
Anders Carlsson307846f2009-07-23 03:17:50 +0000362 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000363
Anders Carlsson307846f2009-07-23 03:17:50 +0000364 LayoutBitField(D, FieldOffset);
365 return true;
366 }
Mike Stump11289f42009-09-09 15:08:12 +0000367
John McCall614dbdc2010-08-22 21:01:12 +0000368 CheckZeroInitializable(D->getType());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000369
Anders Carlsson307846f2009-07-23 03:17:50 +0000370 assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!");
Anders Carlsson307846f2009-07-23 03:17:50 +0000371 uint64_t FieldOffsetInBytes = FieldOffset / 8;
Mike Stump11289f42009-09-09 15:08:12 +0000372
Anders Carlsson19702bb2009-08-04 16:29:15 +0000373 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
374 unsigned TypeAlignment = getTypeAlignment(Ty);
375
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000376 // If the type alignment is larger then the struct alignment, we must use
377 // a packed struct.
378 if (TypeAlignment > Alignment) {
379 assert(!Packed && "Alignment is wrong even with packed struct!");
380 return false;
381 }
Mike Stump11289f42009-09-09 15:08:12 +0000382
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000383 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
384 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
Daniel Dunbar40130442010-05-27 01:12:46 +0000385 if (const MaxFieldAlignmentAttr *MFAA =
386 RD->getAttr<MaxFieldAlignmentAttr>()) {
387 if (MFAA->getAlignment() != TypeAlignment * 8 && !Packed)
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000388 return false;
389 }
390 }
391
Anders Carlsson19702bb2009-08-04 16:29:15 +0000392 // Round up the field offset to the alignment of the field type.
Mike Stump11289f42009-09-09 15:08:12 +0000393 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson19702bb2009-08-04 16:29:15 +0000394 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, TypeAlignment);
395
396 if (FieldOffsetInBytes < AlignedNextFieldOffsetInBytes) {
397 assert(!Packed && "Could not place field even with packed struct!");
398 return false;
399 }
Mike Stump11289f42009-09-09 15:08:12 +0000400
Anders Carlssond74cad82010-12-04 23:53:18 +0000401 AppendPadding(FieldOffsetInBytes, TypeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +0000402
Anders Carlsson307846f2009-07-23 03:17:50 +0000403 // Now append the field.
404 LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size()));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000405 AppendField(FieldOffsetInBytes, Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000406
Anders Carlsson307846f2009-07-23 03:17:50 +0000407 return true;
408}
409
Anders Carlsson1de2f572010-04-17 20:49:27 +0000410const llvm::Type *
411CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
412 const ASTRecordLayout &Layout) {
413 if (Field->isBitField()) {
414 uint64_t FieldSize =
415 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
416
417 // Ignore zero sized bit fields.
418 if (FieldSize == 0)
419 return 0;
420
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000421 const llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
422 unsigned NumBytesToAppend =
423 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Anders Carlsson2295f132010-04-17 21:04:52 +0000424
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000425 if (NumBytesToAppend > 1)
426 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend);
Anders Carlsson2295f132010-04-17 21:04:52 +0000427
Anders Carlsson1de2f572010-04-17 20:49:27 +0000428 // Add the bit field info.
429 LLVMBitFields.push_back(
Daniel Dunbarc7f9bba2010-09-02 23:53:28 +0000430 LLVMBitFieldInfo(Field, CGBitFieldInfo::MakeInfo(Types, Field,
431 0, FieldSize)));
Anders Carlsson2295f132010-04-17 21:04:52 +0000432 return FieldTy;
Anders Carlsson1de2f572010-04-17 20:49:27 +0000433 }
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000434
Anders Carlsson1de2f572010-04-17 20:49:27 +0000435 // This is a regular union field.
436 LLVMFields.push_back(LLVMFieldInfo(Field, 0));
437 return Types.ConvertTypeForMemRecursive(Field->getType());
438}
439
Anders Carlsson697f6592009-07-23 03:43:54 +0000440void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
441 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump11289f42009-09-09 15:08:12 +0000442
Anders Carlsson697f6592009-07-23 03:43:54 +0000443 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000444
Anders Carlsson697f6592009-07-23 03:43:54 +0000445 const llvm::Type *Ty = 0;
446 uint64_t Size = 0;
447 unsigned Align = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000448
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000449 bool HasOnlyZeroSizedBitFields = true;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000450
Anders Carlsson697f6592009-07-23 03:43:54 +0000451 unsigned FieldNo = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000452 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson697f6592009-07-23 03:43:54 +0000453 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
Mike Stump11289f42009-09-09 15:08:12 +0000454 assert(Layout.getFieldOffset(FieldNo) == 0 &&
Anders Carlsson697f6592009-07-23 03:43:54 +0000455 "Union field offset did not start at the beginning of record!");
Anders Carlsson1de2f572010-04-17 20:49:27 +0000456 const llvm::Type *FieldTy = LayoutUnionField(*Field, Layout);
Anders Carlssonf814ee62009-07-23 04:00:39 +0000457
Anders Carlsson1de2f572010-04-17 20:49:27 +0000458 if (!FieldTy)
459 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000460
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000461 HasOnlyZeroSizedBitFields = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000462
Anders Carlssone2accf42009-07-23 21:52:03 +0000463 unsigned FieldAlign = Types.getTargetData().getABITypeAlignment(FieldTy);
464 uint64_t FieldSize = Types.getTargetData().getTypeAllocSize(FieldTy);
Mike Stump11289f42009-09-09 15:08:12 +0000465
Anders Carlsson697f6592009-07-23 03:43:54 +0000466 if (FieldAlign < Align)
467 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000468
Anders Carlsson697f6592009-07-23 03:43:54 +0000469 if (FieldAlign > Align || FieldSize > Size) {
470 Ty = FieldTy;
471 Align = FieldAlign;
472 Size = FieldSize;
473 }
474 }
Mike Stump11289f42009-09-09 15:08:12 +0000475
Anders Carlsson697f6592009-07-23 03:43:54 +0000476 // Now add our field.
Anders Carlsson0e912752009-09-03 22:56:02 +0000477 if (Ty) {
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000478 AppendField(0, Ty);
Anders Carlsson0e912752009-09-03 22:56:02 +0000479
480 if (getTypeAlignment(Ty) > Layout.getAlignment() / 8) {
481 // We need a packed struct.
482 Packed = true;
483 Align = 1;
484 }
485 }
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000486 if (!Align) {
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000487 assert(HasOnlyZeroSizedBitFields &&
488 "0-align record did not have all zero-sized bit-fields!");
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000489 Align = 1;
490 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000491
Anders Carlsson697f6592009-07-23 03:43:54 +0000492 // Append tail padding.
Ken Dyck89d9f362011-02-10 12:36:29 +0000493 uint64_t RecordSize = Layout.getSize().getQuantity();
494 if (RecordSize > Size)
495 AppendPadding(RecordSize, Align);
Anders Carlsson697f6592009-07-23 03:43:54 +0000496}
497
Anders Carlssona518b2a2010-12-04 23:59:48 +0000498void CGRecordLayoutBuilder::LayoutBase(const CXXRecordDecl *BaseDecl,
499 uint64_t BaseOffset) {
500 CheckZeroInitializable(BaseDecl);
501
502 const ASTRecordLayout &Layout =
503 Types.getContext().getASTRecordLayout(BaseDecl);
504
Ken Dyck316d6f62011-02-01 01:52:10 +0000505 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
Anders Carlssona518b2a2010-12-04 23:59:48 +0000506
507 AppendPadding(BaseOffset / 8, 1);
508
509 // FIXME: Actually use a better type than [sizeof(BaseDecl) x i8] when we can.
Ken Dyck316d6f62011-02-01 01:52:10 +0000510 AppendBytes(NonVirtualSize.getQuantity());
Anders Carlssona518b2a2010-12-04 23:59:48 +0000511}
512
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000513void
514CGRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *BaseDecl,
515 uint64_t BaseOffset) {
516 // Ignore empty bases.
517 if (BaseDecl->isEmpty())
518 return;
519
520 CheckZeroInitializable(BaseDecl);
521
522 const ASTRecordLayout &Layout =
523 Types.getContext().getASTRecordLayout(BaseDecl);
524
Ken Dyck316d6f62011-02-01 01:52:10 +0000525 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000526
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000527 AppendPadding(BaseOffset / 8, 1);
528
Anders Carlssona518b2a2010-12-04 23:59:48 +0000529 // FIXME: Actually use a better type than [sizeof(BaseDecl) x i8] when we can.
Ken Dyck316d6f62011-02-01 01:52:10 +0000530 AppendBytes(NonVirtualSize.getQuantity());
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000531
Anders Carlssona518b2a2010-12-04 23:59:48 +0000532 // FIXME: Add the vbase field info.
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000533}
534
Anders Carlssona459adb2010-11-28 19:18:44 +0000535/// LayoutVirtualBases - layout the non-virtual bases of a record decl.
536void
537CGRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
538 const ASTRecordLayout &Layout) {
539 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
540 E = RD->bases_end(); I != E; ++I) {
541 const CXXRecordDecl *BaseDecl =
542 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
543
544 // We only want to lay out virtual bases that aren't indirect primary bases
545 // of some other base.
546 if (I->isVirtual() && !IndirectPrimaryBases.count(BaseDecl)) {
547 // Only lay out the base once.
548 if (!LaidOutVirtualBases.insert(BaseDecl))
549 continue;
550
551 uint64_t VBaseOffset = Layout.getVBaseClassOffsetInBits(BaseDecl);
552 LayoutVirtualBase(BaseDecl, VBaseOffset);
553 }
554
555 if (!BaseDecl->getNumVBases()) {
556 // This base isn't interesting since it doesn't have any virtual bases.
557 continue;
558 }
559
560 LayoutVirtualBases(BaseDecl, Layout);
561 }
562}
563
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000564void CGRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *BaseDecl,
565 uint64_t BaseOffset) {
Anders Carlsson39a6b222010-11-22 00:03:08 +0000566 // Ignore empty bases.
567 if (BaseDecl->isEmpty())
568 return;
569
Anders Carlssona518b2a2010-12-04 23:59:48 +0000570 LayoutBase(BaseDecl, BaseOffset);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000571
Anders Carlsson061ca522010-05-18 05:22:06 +0000572 // Append the base field.
Anders Carlssona518b2a2010-12-04 23:59:48 +0000573 LLVMNonVirtualBases.push_back(LLVMBaseInfo(BaseDecl, FieldTypes.size() - 1));
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000574}
575
576void
577CGRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD,
578 const ASTRecordLayout &Layout) {
579 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
580
581 // Check if we need to add a vtable pointer.
582 if (RD->isDynamicClass()) {
583 if (!PrimaryBase) {
584 const llvm::Type *FunctionType =
585 llvm::FunctionType::get(llvm::Type::getInt32Ty(Types.getLLVMContext()),
586 /*isVarArg=*/true);
587 const llvm::Type *VTableTy = FunctionType->getPointerTo();
588
589 assert(NextFieldOffsetInBytes == 0 &&
590 "VTable pointer must come first!");
591 AppendField(NextFieldOffsetInBytes, VTableTy->getPointerTo());
592 } else {
Anders Carlsson7f95cd12010-11-24 23:12:57 +0000593 if (!Layout.isPrimaryBaseVirtual())
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000594 LayoutNonVirtualBase(PrimaryBase, 0);
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000595 else
596 LayoutVirtualBase(PrimaryBase, 0);
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000597 }
598 }
599
600 // Layout the non-virtual bases.
601 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
602 E = RD->bases_end(); I != E; ++I) {
603 if (I->isVirtual())
604 continue;
605
606 const CXXRecordDecl *BaseDecl =
607 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
608
609 // We've already laid out the primary base.
Anders Carlsson7f95cd12010-11-24 23:12:57 +0000610 if (BaseDecl == PrimaryBase && !Layout.isPrimaryBaseVirtual())
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000611 continue;
612
Anders Carlssonfd88a612010-10-31 23:22:37 +0000613 LayoutNonVirtualBase(BaseDecl, Layout.getBaseClassOffsetInBits(BaseDecl));
Anders Carlssond681a292009-12-16 17:27:20 +0000614 }
615}
616
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000617bool
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000618CGRecordLayoutBuilder::ComputeNonVirtualBaseType(const CXXRecordDecl *RD) {
619 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(RD);
620
Ken Dyck316d6f62011-02-01 01:52:10 +0000621
Ken Dyckbec02852011-02-08 02:02:47 +0000622 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
623 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000624 uint64_t AlignedNonVirtualTypeSize =
Ken Dyckbec02852011-02-08 02:02:47 +0000625 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign).getQuantity();
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000626
627
628 // First check if we can use the same fields as for the complete class.
Ken Dyck89d9f362011-02-10 12:36:29 +0000629 uint64_t RecordSize = Layout.getSize().getQuantity();
630 if (AlignedNonVirtualTypeSize == RecordSize) {
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000631 NonVirtualBaseTypeIsSameAsCompleteType = true;
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000632 return true;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000633 }
634
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000635 // Check if we need padding.
636 uint64_t AlignedNextFieldOffset =
Anders Carlssonacf877b2010-11-28 23:06:23 +0000637 llvm::RoundUpToAlignment(NextFieldOffsetInBytes,
638 getAlignmentAsLLVMStruct());
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000639
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000640 if (AlignedNextFieldOffset > AlignedNonVirtualTypeSize)
641 return false; // Needs packing.
642
643 NonVirtualBaseFieldTypes = FieldTypes;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000644
645 if (AlignedNonVirtualTypeSize == AlignedNextFieldOffset) {
646 // We don't need any padding.
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000647 return true;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000648 }
649
650 uint64_t NumBytes = AlignedNonVirtualTypeSize - AlignedNextFieldOffset;
651 NonVirtualBaseFieldTypes.push_back(getByteArrayType(NumBytes));
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000652 return true;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000653}
654
Anders Carlsson307846f2009-07-23 03:17:50 +0000655bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
656 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000657 assert(Alignment && "Did not set alignment!");
Mike Stump11289f42009-09-09 15:08:12 +0000658
Anders Carlsson697f6592009-07-23 03:43:54 +0000659 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000660
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000661 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
662 if (RD)
Anders Carlssonaf9e5af2010-05-18 05:12:20 +0000663 LayoutNonVirtualBases(RD, Layout);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000664
Anders Carlsson307846f2009-07-23 03:17:50 +0000665 unsigned FieldNo = 0;
Fariborz Jahanian7b2b1ec2009-07-27 20:57:45 +0000666
Mike Stump11289f42009-09-09 15:08:12 +0000667 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson307846f2009-07-23 03:17:50 +0000668 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
669 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump11289f42009-09-09 15:08:12 +0000670 assert(!Packed &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000671 "Could not layout fields even with a packed LLVM struct!");
672 return false;
673 }
674 }
675
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000676 if (RD) {
Anders Carlssona459adb2010-11-28 19:18:44 +0000677 // We've laid out the non-virtual bases and the fields, now compute the
678 // non-virtual base field types.
Argyrios Kyrtzidis648fcbe2010-12-10 00:11:00 +0000679 if (!ComputeNonVirtualBaseType(RD)) {
680 assert(!Packed && "Could not layout even with a packed LLVM struct!");
681 return false;
682 }
Anders Carlsson1f95ee32010-11-25 01:59:35 +0000683
Anders Carlssona459adb2010-11-28 19:18:44 +0000684 // And lay out the virtual bases.
685 RD->getIndirectPrimaryBases(IndirectPrimaryBases);
686 if (Layout.isPrimaryBaseVirtual())
687 IndirectPrimaryBases.insert(Layout.getPrimaryBase());
688 LayoutVirtualBases(RD, Layout);
689 }
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000690
Anders Carlsson307846f2009-07-23 03:17:50 +0000691 // Append tail padding if necessary.
Ken Dyckc8ae5502011-02-09 01:59:34 +0000692 AppendTailPadding(
693 Layout.getSize().getQuantity() * Types.getContext().getCharWidth());
Mike Stump11289f42009-09-09 15:08:12 +0000694
Anders Carlsson307846f2009-07-23 03:17:50 +0000695 return true;
696}
697
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000698void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) {
699 assert(RecordSize % 8 == 0 && "Invalid record size!");
Mike Stump11289f42009-09-09 15:08:12 +0000700
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000701 uint64_t RecordSizeInBytes = RecordSize / 8;
Anders Carlssond5d64132009-07-28 17:56:36 +0000702 assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000703
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000704 uint64_t AlignedNextFieldOffset =
Anders Carlssonacf877b2010-11-28 23:06:23 +0000705 llvm::RoundUpToAlignment(NextFieldOffsetInBytes,
706 getAlignmentAsLLVMStruct());
Anders Carlsson220bf4f2009-12-08 01:24:23 +0000707
708 if (AlignedNextFieldOffset == RecordSizeInBytes) {
709 // We don't need any padding.
710 return;
711 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000712
Anders Carlssond5d64132009-07-28 17:56:36 +0000713 unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes;
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000714 AppendBytes(NumPadBytes);
715}
716
Mike Stump11289f42009-09-09 15:08:12 +0000717void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes,
Anders Carlsson307846f2009-07-23 03:17:50 +0000718 const llvm::Type *FieldTy) {
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000719 uint64_t FieldSizeInBytes = Types.getTargetData().getTypeAllocSize(FieldTy);
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000720
Anders Carlsson307846f2009-07-23 03:17:50 +0000721 FieldTypes.push_back(FieldTy);
Anders Carlsson307846f2009-07-23 03:17:50 +0000722
Anders Carlssond5d64132009-07-28 17:56:36 +0000723 NextFieldOffsetInBytes = FieldOffsetInBytes + FieldSizeInBytes;
Anders Carlsson307846f2009-07-23 03:17:50 +0000724 BitsAvailableInLastField = 0;
725}
726
Mike Stump11289f42009-09-09 15:08:12 +0000727void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
Anders Carlssond74cad82010-12-04 23:53:18 +0000728 unsigned FieldAlignmentInBytes) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000729 assert(NextFieldOffsetInBytes <= FieldOffsetInBytes &&
730 "Incorrect field layout!");
Mike Stump11289f42009-09-09 15:08:12 +0000731
Anders Carlsson307846f2009-07-23 03:17:50 +0000732 // Round up the field offset to the alignment of the field type.
Mike Stump11289f42009-09-09 15:08:12 +0000733 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlssond74cad82010-12-04 23:53:18 +0000734 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignmentInBytes);
Anders Carlsson307846f2009-07-23 03:17:50 +0000735
736 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
737 // Even with alignment, the field offset is not at the right place,
738 // insert padding.
739 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
740
741 AppendBytes(PaddingInBytes);
742 }
743}
744
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000745const llvm::Type *CGRecordLayoutBuilder::getByteArrayType(uint64_t NumBytes) {
746 assert(NumBytes != 0 && "Empty byte array's aren't allowed.");
Mike Stump11289f42009-09-09 15:08:12 +0000747
Owen Anderson41a75022009-08-13 21:57:51 +0000748 const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000749 if (NumBytes > 1)
Anders Carlsson307846f2009-07-23 03:17:50 +0000750 Ty = llvm::ArrayType::get(Ty, NumBytes);
Mike Stump11289f42009-09-09 15:08:12 +0000751
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000752 return Ty;
753}
754
755void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) {
756 if (NumBytes == 0)
757 return;
758
Anders Carlsson307846f2009-07-23 03:17:50 +0000759 // Append the padding field
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000760 AppendField(NextFieldOffsetInBytes, getByteArrayType(NumBytes));
Anders Carlsson307846f2009-07-23 03:17:50 +0000761}
762
763unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlssond78fc892009-07-23 17:24:40 +0000764 if (Packed)
Anders Carlsson307846f2009-07-23 03:17:50 +0000765 return 1;
Mike Stump11289f42009-09-09 15:08:12 +0000766
Anders Carlsson307846f2009-07-23 03:17:50 +0000767 return Types.getTargetData().getABITypeAlignment(Ty);
768}
769
Anders Carlssonacf877b2010-11-28 23:06:23 +0000770unsigned CGRecordLayoutBuilder::getAlignmentAsLLVMStruct() const {
771 if (Packed)
772 return 1;
773
774 unsigned MaxAlignment = 1;
775 for (size_t i = 0; i != FieldTypes.size(); ++i)
776 MaxAlignment = std::max(MaxAlignment, getTypeAlignment(FieldTypes[i]));
777
778 return MaxAlignment;
779}
780
John McCall614dbdc2010-08-22 21:01:12 +0000781void CGRecordLayoutBuilder::CheckZeroInitializable(QualType T) {
Anders Carlssond606de72009-08-23 01:25:01 +0000782 // This record already contains a member pointer.
John McCall614dbdc2010-08-22 21:01:12 +0000783 if (!IsZeroInitializable)
Anders Carlssond606de72009-08-23 01:25:01 +0000784 return;
Mike Stump11289f42009-09-09 15:08:12 +0000785
Anders Carlssond606de72009-08-23 01:25:01 +0000786 // Can only have member pointers if we're compiling C++.
787 if (!Types.getContext().getLangOptions().CPlusPlus)
788 return;
Mike Stump11289f42009-09-09 15:08:12 +0000789
Anders Carlssone8bfe412010-02-02 05:17:25 +0000790 T = Types.getContext().getBaseElementType(T);
Mike Stump11289f42009-09-09 15:08:12 +0000791
Anders Carlssone8bfe412010-02-02 05:17:25 +0000792 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) {
John McCall614dbdc2010-08-22 21:01:12 +0000793 if (!Types.getCXXABI().isZeroInitializable(MPT))
794 IsZeroInitializable = false;
Anders Carlssone8bfe412010-02-02 05:17:25 +0000795 } else if (const RecordType *RT = T->getAs<RecordType>()) {
796 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
John McCall614dbdc2010-08-22 21:01:12 +0000797 CheckZeroInitializable(RD);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000798 }
Anders Carlssond606de72009-08-23 01:25:01 +0000799}
800
John McCall614dbdc2010-08-22 21:01:12 +0000801void CGRecordLayoutBuilder::CheckZeroInitializable(const CXXRecordDecl *RD) {
Anders Carlssonbe48c542010-05-18 16:51:41 +0000802 // This record already contains a member pointer.
John McCall614dbdc2010-08-22 21:01:12 +0000803 if (!IsZeroInitializable)
Anders Carlssonbe48c542010-05-18 16:51:41 +0000804 return;
805
Anders Carlsson62776152010-11-24 19:57:04 +0000806 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
John McCall614dbdc2010-08-22 21:01:12 +0000807 if (!Layout.isZeroInitializable())
808 IsZeroInitializable = false;
Anders Carlssonbe48c542010-05-18 16:51:41 +0000809}
810
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000811CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) {
812 CGRecordLayoutBuilder Builder(*this);
Mike Stump11289f42009-09-09 15:08:12 +0000813
Anders Carlsson307846f2009-07-23 03:17:50 +0000814 Builder.Layout(D);
Anders Carlssone1d5ca52009-07-24 15:20:52 +0000815
Anders Carlsson36e2fa82010-11-24 19:37:16 +0000816 const llvm::StructType *Ty = llvm::StructType::get(getLLVMContext(),
817 Builder.FieldTypes,
818 Builder.Packed);
Mike Stump11289f42009-09-09 15:08:12 +0000819
Anders Carlsson36e2fa82010-11-24 19:37:16 +0000820 const llvm::StructType *BaseTy = 0;
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000821 if (isa<CXXRecordDecl>(D)) {
822 if (Builder.NonVirtualBaseTypeIsSameAsCompleteType)
823 BaseTy = Ty;
824 else if (!Builder.NonVirtualBaseFieldTypes.empty())
825 BaseTy = llvm::StructType::get(getLLVMContext(),
826 Builder.NonVirtualBaseFieldTypes,
827 Builder.Packed);
828 }
829
Daniel Dunbar034299e2010-03-31 01:09:11 +0000830 CGRecordLayout *RL =
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000831 new CGRecordLayout(Ty, BaseTy, Builder.IsZeroInitializable);
Daniel Dunbar034299e2010-03-31 01:09:11 +0000832
Anders Carlsson061ca522010-05-18 05:22:06 +0000833 // Add all the non-virtual base field numbers.
834 RL->NonVirtualBaseFields.insert(Builder.LLVMNonVirtualBases.begin(),
835 Builder.LLVMNonVirtualBases.end());
836
Anders Carlsson307846f2009-07-23 03:17:50 +0000837 // Add all the field numbers.
Anders Carlsson061ca522010-05-18 05:22:06 +0000838 RL->FieldInfo.insert(Builder.LLVMFields.begin(),
839 Builder.LLVMFields.end());
Anders Carlsson307846f2009-07-23 03:17:50 +0000840
841 // Add bitfield info.
Anders Carlsson061ca522010-05-18 05:22:06 +0000842 RL->BitFields.insert(Builder.LLVMBitFields.begin(),
843 Builder.LLVMBitFields.end());
Mike Stump11289f42009-09-09 15:08:12 +0000844
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000845 // Dump the layout, if requested.
Daniel Dunbarb935b932010-04-13 20:58:55 +0000846 if (getContext().getLangOptions().DumpRecordLayouts) {
Daniel Dunbarccabe482010-04-19 20:44:53 +0000847 llvm::errs() << "\n*** Dumping IRgen Record Layout\n";
Daniel Dunbarb935b932010-04-13 20:58:55 +0000848 llvm::errs() << "Record: ";
849 D->dump();
850 llvm::errs() << "\nLayout: ";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000851 RL->dump();
Daniel Dunbarb935b932010-04-13 20:58:55 +0000852 }
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000853
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000854#ifndef NDEBUG
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000855 // Verify that the computed LLVM struct size matches the AST layout size.
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000856 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
857
Ken Dyckc8ae5502011-02-09 01:59:34 +0000858 uint64_t TypeSizeInBits =
859 Layout.getSize().getQuantity() * getContext().getCharWidth();
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000860 assert(TypeSizeInBits == getTargetData().getTypeAllocSizeInBits(Ty) &&
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000861 "Type size mismatch!");
862
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000863 if (BaseTy) {
Ken Dyckbec02852011-02-08 02:02:47 +0000864 CharUnits NonVirtualSize = Layout.getNonVirtualSize();
865 CharUnits NonVirtualAlign = Layout.getNonVirtualAlign();
866 CharUnits AlignedNonVirtualTypeSize =
867 NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
868
869 uint64_t AlignedNonVirtualTypeSizeInBits =
870 AlignedNonVirtualTypeSize.getQuantity() * getContext().getCharWidth();
Anders Carlssonc1351ca2010-11-09 05:25:47 +0000871
872 assert(AlignedNonVirtualTypeSizeInBits ==
873 getTargetData().getTypeAllocSizeInBits(BaseTy) &&
874 "Type size mismatch!");
875 }
876
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000877 // Verify that the LLVM and AST field offsets agree.
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000878 const llvm::StructType *ST =
879 dyn_cast<llvm::StructType>(RL->getLLVMType());
880 const llvm::StructLayout *SL = getTargetData().getStructLayout(ST);
881
882 const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
883 RecordDecl::field_iterator it = D->field_begin();
884 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
885 const FieldDecl *FD = *it;
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000886
887 // For non-bit-fields, just check that the LLVM struct offset matches the
888 // AST offset.
889 if (!FD->isBitField()) {
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000890 unsigned FieldNo = RL->getLLVMFieldNo(FD);
891 assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
892 "Invalid field offset!");
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000893 continue;
894 }
895
896 // Ignore unnamed bit-fields.
897 if (!FD->getDeclName())
898 continue;
899
900 const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
901 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
902 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
903
904 // Verify that every component access is within the structure.
905 uint64_t FieldOffset = SL->getElementOffsetInBits(AI.FieldIndex);
906 uint64_t AccessBitOffset = FieldOffset + AI.FieldByteOffset * 8;
907 assert(AccessBitOffset + AI.AccessWidth <= TypeSizeInBits &&
908 "Invalid bit-field access (out of range)!");
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000909 }
910 }
911#endif
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000912
Daniel Dunbar034299e2010-03-31 01:09:11 +0000913 return RL;
Anders Carlsson307846f2009-07-23 03:17:50 +0000914}
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000915
916void CGRecordLayout::print(llvm::raw_ostream &OS) const {
917 OS << "<CGRecordLayout\n";
918 OS << " LLVMType:" << *LLVMType << "\n";
Anders Carlssona7dd96c2010-11-21 23:59:45 +0000919 if (NonVirtualBaseLLVMType)
920 OS << " NonVirtualBaseLLVMType:" << *NonVirtualBaseLLVMType << "\n";
John McCall614dbdc2010-08-22 21:01:12 +0000921 OS << " IsZeroInitializable:" << IsZeroInitializable << "\n";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000922 OS << " BitFields:[\n";
Daniel Dunbarb6f4b052010-04-22 02:35:36 +0000923
924 // Print bit-field infos in declaration order.
925 std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000926 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
927 it = BitFields.begin(), ie = BitFields.end();
928 it != ie; ++it) {
Daniel Dunbarb6f4b052010-04-22 02:35:36 +0000929 const RecordDecl *RD = it->first->getParent();
930 unsigned Index = 0;
931 for (RecordDecl::field_iterator
932 it2 = RD->field_begin(); *it2 != it->first; ++it2)
933 ++Index;
934 BFIs.push_back(std::make_pair(Index, &it->second));
935 }
936 llvm::array_pod_sort(BFIs.begin(), BFIs.end());
937 for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
Daniel Dunbarb935b932010-04-13 20:58:55 +0000938 OS.indent(4);
Daniel Dunbarb6f4b052010-04-22 02:35:36 +0000939 BFIs[i].second->print(OS);
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000940 OS << "\n";
941 }
Daniel Dunbarb6f4b052010-04-22 02:35:36 +0000942
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000943 OS << "]>\n";
944}
945
946void CGRecordLayout::dump() const {
947 print(llvm::errs());
948}
949
950void CGBitFieldInfo::print(llvm::raw_ostream &OS) const {
951 OS << "<CGBitFieldInfo";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000952 OS << " Size:" << Size;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000953 OS << " IsSigned:" << IsSigned << "\n";
954
955 OS.indent(4 + strlen("<CGBitFieldInfo"));
956 OS << " NumComponents:" << getNumComponents();
957 OS << " Components: [";
958 if (getNumComponents()) {
959 OS << "\n";
960 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
961 const AccessInfo &AI = getComponent(i);
962 OS.indent(8);
963 OS << "<AccessInfo"
964 << " FieldIndex:" << AI.FieldIndex
965 << " FieldByteOffset:" << AI.FieldByteOffset
966 << " FieldBitStart:" << AI.FieldBitStart
967 << " AccessWidth:" << AI.AccessWidth << "\n";
968 OS.indent(8 + strlen("<AccessInfo"));
969 OS << " AccessAlignment:" << AI.AccessAlignment
970 << " TargetBitOffset:" << AI.TargetBitOffset
971 << " TargetBitWidth:" << AI.TargetBitWidth
972 << ">\n";
973 }
974 OS.indent(4);
975 }
976 OS << "]>";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000977}
978
979void CGBitFieldInfo::dump() const {
980 print(llvm::errs());
981}