blob: a87c8bcbbc2b9db8412e21d2f234f3068e8a0a6f [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"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/Expr.h"
19#include "clang/AST/RecordLayout.h"
20#include "CodeGenTypes.h"
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000021#include "llvm/Type.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000022#include "llvm/DerivedTypes.h"
23#include "llvm/Target/TargetData.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000024using namespace clang;
25using namespace CodeGen;
26
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000027namespace clang {
28namespace CodeGen {
29
30class CGRecordLayoutBuilder {
31public:
32 /// FieldTypes - Holds the LLVM types that the struct is created from.
33 std::vector<const llvm::Type *> FieldTypes;
34
35 /// LLVMFieldInfo - Holds a field and its corresponding LLVM field number.
36 typedef std::pair<const FieldDecl *, unsigned> LLVMFieldInfo;
37 llvm::SmallVector<LLVMFieldInfo, 16> LLVMFields;
38
39 /// LLVMBitFieldInfo - Holds location and size information about a bit field.
40 struct LLVMBitFieldInfo {
41 LLVMBitFieldInfo(const FieldDecl *FD, unsigned FieldNo, unsigned Start,
42 unsigned Size)
43 : FD(FD), FieldNo(FieldNo), Start(Start), Size(Size) { }
44
45 const FieldDecl *FD;
46
47 unsigned FieldNo;
48 unsigned Start;
49 unsigned Size;
50 };
51 llvm::SmallVector<LLVMBitFieldInfo, 16> LLVMBitFields;
52
53 /// ContainsPointerToDataMember - Whether one of the fields in this record
54 /// layout is a pointer to data member, or a struct that contains pointer to
55 /// data member.
56 bool ContainsPointerToDataMember;
57
58 /// Packed - Whether the resulting LLVM struct will be packed or not.
59 bool Packed;
60
61private:
62 CodeGenTypes &Types;
63
64 /// Alignment - Contains the alignment of the RecordDecl.
65 //
66 // FIXME: This is not needed and should be removed.
67 unsigned Alignment;
68
69 /// AlignmentAsLLVMStruct - Will contain the maximum alignment of all the
70 /// LLVM types.
71 unsigned AlignmentAsLLVMStruct;
72
73 /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field,
74 /// this will have the number of bits still available in the field.
75 char BitsAvailableInLastField;
76
77 /// NextFieldOffsetInBytes - Holds the next field offset in bytes.
78 uint64_t NextFieldOffsetInBytes;
79
80 /// LayoutUnion - Will layout a union RecordDecl.
81 void LayoutUnion(const RecordDecl *D);
82
83 /// LayoutField - try to layout all fields in the record decl.
84 /// Returns false if the operation failed because the struct is not packed.
85 bool LayoutFields(const RecordDecl *D);
86
87 /// LayoutBases - layout the bases and vtable pointer of a record decl.
88 void LayoutBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout);
89
90 /// LayoutField - layout a single field. Returns false if the operation failed
91 /// because the current struct is not packed.
92 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
93
94 /// LayoutBitField - layout a single bit field.
95 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
96
97 /// AppendField - Appends a field with the given offset and type.
98 void AppendField(uint64_t FieldOffsetInBytes, const llvm::Type *FieldTy);
99
100 /// AppendPadding - Appends enough padding bytes so that the total struct
101 /// size matches the alignment of the passed in type.
102 void AppendPadding(uint64_t FieldOffsetInBytes, const llvm::Type *FieldTy);
103
104 /// AppendPadding - Appends enough padding bytes so that the total
105 /// struct size is a multiple of the field alignment.
106 void AppendPadding(uint64_t FieldOffsetInBytes, unsigned FieldAlignment);
107
108 /// AppendBytes - Append a given number of bytes to the record.
109 void AppendBytes(uint64_t NumBytes);
110
111 /// AppendTailPadding - Append enough tail padding so that the type will have
112 /// the passed size.
113 void AppendTailPadding(uint64_t RecordSize);
114
115 unsigned getTypeAlignment(const llvm::Type *Ty) const;
116 uint64_t getTypeSizeInBytes(const llvm::Type *Ty) const;
117
118 /// CheckForPointerToDataMember - Check if the given type contains a pointer
119 /// to data member.
120 void CheckForPointerToDataMember(QualType T);
121
122public:
123 CGRecordLayoutBuilder(CodeGenTypes &Types)
124 : ContainsPointerToDataMember(false), Packed(false), Types(Types),
125 Alignment(0), AlignmentAsLLVMStruct(1),
126 BitsAvailableInLastField(0), NextFieldOffsetInBytes(0) { }
127
128 /// Layout - Will layout a RecordDecl.
129 void Layout(const RecordDecl *D);
130};
131
132}
133}
134
Anders Carlsson307846f2009-07-23 03:17:50 +0000135void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000136 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment() / 8;
Anders Carlsson09a37742009-09-02 17:51:33 +0000137 Packed = D->hasAttr<PackedAttr>();
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000138
Anders Carlsson697f6592009-07-23 03:43:54 +0000139 if (D->isUnion()) {
140 LayoutUnion(D);
141 return;
142 }
Anders Carlsson68e0b682009-08-08 18:23:56 +0000143
Anders Carlsson307846f2009-07-23 03:17:50 +0000144 if (LayoutFields(D))
145 return;
Mike Stump11289f42009-09-09 15:08:12 +0000146
Anders Carlsson307846f2009-07-23 03:17:50 +0000147 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlssond78fc892009-07-23 17:24:40 +0000148 Packed = true;
Anders Carlsson307846f2009-07-23 03:17:50 +0000149 AlignmentAsLLVMStruct = 1;
Anders Carlssond5d64132009-07-28 17:56:36 +0000150 NextFieldOffsetInBytes = 0;
Anders Carlsson307846f2009-07-23 03:17:50 +0000151 FieldTypes.clear();
Anders Carlsson307846f2009-07-23 03:17:50 +0000152 LLVMFields.clear();
153 LLVMBitFields.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000154
Anders Carlsson307846f2009-07-23 03:17:50 +0000155 LayoutFields(D);
156}
157
158void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
159 uint64_t FieldOffset) {
Mike Stump11289f42009-09-09 15:08:12 +0000160 uint64_t FieldSize =
Anders Carlsson307846f2009-07-23 03:17:50 +0000161 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump11289f42009-09-09 15:08:12 +0000162
Anders Carlsson307846f2009-07-23 03:17:50 +0000163 if (FieldSize == 0)
164 return;
165
Anders Carlssond5d64132009-07-28 17:56:36 +0000166 uint64_t NextFieldOffset = NextFieldOffsetInBytes * 8;
Anders Carlsson307846f2009-07-23 03:17:50 +0000167 unsigned NumBytesToAppend;
Mike Stump11289f42009-09-09 15:08:12 +0000168
Anders Carlsson307846f2009-07-23 03:17:50 +0000169 if (FieldOffset < NextFieldOffset) {
170 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
Anders Carlssond5d64132009-07-28 17:56:36 +0000171 assert(NextFieldOffsetInBytes && "Must have laid out at least one byte!");
Mike Stump11289f42009-09-09 15:08:12 +0000172
Anders Carlsson307846f2009-07-23 03:17:50 +0000173 // The bitfield begins in the previous bit-field.
Mike Stump11289f42009-09-09 15:08:12 +0000174 NumBytesToAppend =
Anders Carlsson307846f2009-07-23 03:17:50 +0000175 llvm::RoundUpToAlignment(FieldSize - BitsAvailableInLastField, 8) / 8;
176 } else {
177 assert(FieldOffset % 8 == 0 && "Field offset not aligned correctly");
178
179 // Append padding if necessary.
180 AppendBytes((FieldOffset - NextFieldOffset) / 8);
Mike Stump11289f42009-09-09 15:08:12 +0000181
182 NumBytesToAppend =
Anders Carlsson307846f2009-07-23 03:17:50 +0000183 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Mike Stump11289f42009-09-09 15:08:12 +0000184
Anders Carlsson307846f2009-07-23 03:17:50 +0000185 assert(NumBytesToAppend && "No bytes to append!");
186 }
187
188 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
189 uint64_t TypeSizeInBits = getTypeSizeInBytes(Ty) * 8;
Mike Stump11289f42009-09-09 15:08:12 +0000190
Anders Carlsson8af896c2009-07-23 17:01:21 +0000191 LLVMBitFields.push_back(LLVMBitFieldInfo(D, FieldOffset / TypeSizeInBits,
Mike Stump11289f42009-09-09 15:08:12 +0000192 FieldOffset % TypeSizeInBits,
Anders Carlsson307846f2009-07-23 03:17:50 +0000193 FieldSize));
Mike Stump11289f42009-09-09 15:08:12 +0000194
Anders Carlsson307846f2009-07-23 03:17:50 +0000195 AppendBytes(NumBytesToAppend);
Mike Stump11289f42009-09-09 15:08:12 +0000196
Mike Stump11289f42009-09-09 15:08:12 +0000197 BitsAvailableInLastField =
Anders Carlssond5d64132009-07-28 17:56:36 +0000198 NextFieldOffsetInBytes * 8 - (FieldOffset + FieldSize);
Anders Carlsson307846f2009-07-23 03:17:50 +0000199}
200
201bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
202 uint64_t FieldOffset) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000203 // If the field is packed, then we need a packed struct.
Anders Carlsson68e0b682009-08-08 18:23:56 +0000204 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson307846f2009-07-23 03:17:50 +0000205 return false;
206
207 if (D->isBitField()) {
208 // We must use packed structs for unnamed bit fields since they
209 // don't affect the struct alignment.
Anders Carlssond78fc892009-07-23 17:24:40 +0000210 if (!Packed && !D->getDeclName())
Anders Carlsson307846f2009-07-23 03:17:50 +0000211 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000212
Anders Carlsson307846f2009-07-23 03:17:50 +0000213 LayoutBitField(D, FieldOffset);
214 return true;
215 }
Mike Stump11289f42009-09-09 15:08:12 +0000216
Anders Carlssone8bfe412010-02-02 05:17:25 +0000217 // Check if we have a pointer to data member in this field.
218 CheckForPointerToDataMember(D->getType());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000219
Anders Carlsson307846f2009-07-23 03:17:50 +0000220 assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!");
Anders Carlsson307846f2009-07-23 03:17:50 +0000221 uint64_t FieldOffsetInBytes = FieldOffset / 8;
Mike Stump11289f42009-09-09 15:08:12 +0000222
Anders Carlsson19702bb2009-08-04 16:29:15 +0000223 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
224 unsigned TypeAlignment = getTypeAlignment(Ty);
225
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000226 // If the type alignment is larger then the struct alignment, we must use
227 // a packed struct.
228 if (TypeAlignment > Alignment) {
229 assert(!Packed && "Alignment is wrong even with packed struct!");
230 return false;
231 }
Mike Stump11289f42009-09-09 15:08:12 +0000232
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000233 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
234 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
235 if (const PragmaPackAttr *PPA = RD->getAttr<PragmaPackAttr>()) {
236 if (PPA->getAlignment() != TypeAlignment * 8 && !Packed)
237 return false;
238 }
239 }
240
Anders Carlsson19702bb2009-08-04 16:29:15 +0000241 // Round up the field offset to the alignment of the field type.
Mike Stump11289f42009-09-09 15:08:12 +0000242 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson19702bb2009-08-04 16:29:15 +0000243 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, TypeAlignment);
244
245 if (FieldOffsetInBytes < AlignedNextFieldOffsetInBytes) {
246 assert(!Packed && "Could not place field even with packed struct!");
247 return false;
248 }
Mike Stump11289f42009-09-09 15:08:12 +0000249
Anders Carlsson19702bb2009-08-04 16:29:15 +0000250 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
251 // Even with alignment, the field offset is not at the right place,
252 // insert padding.
253 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
Mike Stump11289f42009-09-09 15:08:12 +0000254
Anders Carlsson19702bb2009-08-04 16:29:15 +0000255 AppendBytes(PaddingInBytes);
256 }
Mike Stump11289f42009-09-09 15:08:12 +0000257
Anders Carlsson307846f2009-07-23 03:17:50 +0000258 // Now append the field.
259 LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size()));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000260 AppendField(FieldOffsetInBytes, Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000261
Anders Carlsson307846f2009-07-23 03:17:50 +0000262 return true;
263}
264
Anders Carlsson697f6592009-07-23 03:43:54 +0000265void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
266 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump11289f42009-09-09 15:08:12 +0000267
Anders Carlsson697f6592009-07-23 03:43:54 +0000268 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000269
Anders Carlsson697f6592009-07-23 03:43:54 +0000270 const llvm::Type *Ty = 0;
271 uint64_t Size = 0;
272 unsigned Align = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000273
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000274 bool HasOnlyZeroSizedBitFields = true;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000275
Anders Carlsson697f6592009-07-23 03:43:54 +0000276 unsigned FieldNo = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000277 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson697f6592009-07-23 03:43:54 +0000278 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
Mike Stump11289f42009-09-09 15:08:12 +0000279 assert(Layout.getFieldOffset(FieldNo) == 0 &&
Anders Carlsson697f6592009-07-23 03:43:54 +0000280 "Union field offset did not start at the beginning of record!");
Anders Carlssonf814ee62009-07-23 04:00:39 +0000281
282 if (Field->isBitField()) {
Mike Stump11289f42009-09-09 15:08:12 +0000283 uint64_t FieldSize =
Anders Carlssonf814ee62009-07-23 04:00:39 +0000284 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump11289f42009-09-09 15:08:12 +0000285
Anders Carlssonf814ee62009-07-23 04:00:39 +0000286 // Ignore zero sized bit fields.
287 if (FieldSize == 0)
288 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000289
Anders Carlsson08539542009-07-23 22:52:34 +0000290 // Add the bit field info.
291 Types.addBitFieldInfo(*Field, 0, 0, FieldSize);
292 } else
293 Types.addFieldInfo(*Field, 0);
Mike Stump11289f42009-09-09 15:08:12 +0000294
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000295 HasOnlyZeroSizedBitFields = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000296
Mike Stump11289f42009-09-09 15:08:12 +0000297 const llvm::Type *FieldTy =
Anders Carlsson697f6592009-07-23 03:43:54 +0000298 Types.ConvertTypeForMemRecursive(Field->getType());
Anders Carlssone2accf42009-07-23 21:52:03 +0000299 unsigned FieldAlign = Types.getTargetData().getABITypeAlignment(FieldTy);
300 uint64_t FieldSize = Types.getTargetData().getTypeAllocSize(FieldTy);
Mike Stump11289f42009-09-09 15:08:12 +0000301
Anders Carlsson697f6592009-07-23 03:43:54 +0000302 if (FieldAlign < Align)
303 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000304
Anders Carlsson697f6592009-07-23 03:43:54 +0000305 if (FieldAlign > Align || FieldSize > Size) {
306 Ty = FieldTy;
307 Align = FieldAlign;
308 Size = FieldSize;
309 }
310 }
Mike Stump11289f42009-09-09 15:08:12 +0000311
Anders Carlsson697f6592009-07-23 03:43:54 +0000312 // Now add our field.
Anders Carlsson0e912752009-09-03 22:56:02 +0000313 if (Ty) {
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000314 AppendField(0, Ty);
Anders Carlsson0e912752009-09-03 22:56:02 +0000315
316 if (getTypeAlignment(Ty) > Layout.getAlignment() / 8) {
317 // We need a packed struct.
318 Packed = true;
319 Align = 1;
320 }
321 }
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000322 if (!Align) {
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000323 assert(HasOnlyZeroSizedBitFields &&
324 "0-align record did not have all zero-sized bit-fields!");
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000325 Align = 1;
326 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000327
Anders Carlsson697f6592009-07-23 03:43:54 +0000328 // Append tail padding.
329 if (Layout.getSize() / 8 > Size)
330 AppendPadding(Layout.getSize() / 8, Align);
331}
332
Anders Carlssond681a292009-12-16 17:27:20 +0000333void CGRecordLayoutBuilder::LayoutBases(const CXXRecordDecl *RD,
334 const ASTRecordLayout &Layout) {
335 // Check if we need to add a vtable pointer.
336 if (RD->isDynamicClass() && !Layout.getPrimaryBase()) {
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000337 const llvm::Type *Int8PtrTy =
Anders Carlssond681a292009-12-16 17:27:20 +0000338 llvm::Type::getInt8PtrTy(Types.getLLVMContext());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000339
Anders Carlssond681a292009-12-16 17:27:20 +0000340 assert(NextFieldOffsetInBytes == 0 &&
341 "Vtable pointer must come first!");
342 AppendField(NextFieldOffsetInBytes, Int8PtrTy->getPointerTo());
343 }
344}
345
Anders Carlsson307846f2009-07-23 03:17:50 +0000346bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
347 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000348 assert(Alignment && "Did not set alignment!");
Mike Stump11289f42009-09-09 15:08:12 +0000349
Anders Carlsson697f6592009-07-23 03:43:54 +0000350 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000351
Anders Carlssond681a292009-12-16 17:27:20 +0000352 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D))
353 LayoutBases(RD, Layout);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000354
Anders Carlsson307846f2009-07-23 03:17:50 +0000355 unsigned FieldNo = 0;
Fariborz Jahanian7b2b1ec2009-07-27 20:57:45 +0000356
Mike Stump11289f42009-09-09 15:08:12 +0000357 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson307846f2009-07-23 03:17:50 +0000358 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
359 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump11289f42009-09-09 15:08:12 +0000360 assert(!Packed &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000361 "Could not layout fields even with a packed LLVM struct!");
362 return false;
363 }
364 }
365
366 // Append tail padding if necessary.
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000367 AppendTailPadding(Layout.getSize());
Mike Stump11289f42009-09-09 15:08:12 +0000368
Anders Carlsson307846f2009-07-23 03:17:50 +0000369 return true;
370}
371
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000372void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) {
373 assert(RecordSize % 8 == 0 && "Invalid record size!");
Mike Stump11289f42009-09-09 15:08:12 +0000374
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000375 uint64_t RecordSizeInBytes = RecordSize / 8;
Anders Carlssond5d64132009-07-28 17:56:36 +0000376 assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000377
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000378 uint64_t AlignedNextFieldOffset =
Anders Carlsson220bf4f2009-12-08 01:24:23 +0000379 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, AlignmentAsLLVMStruct);
380
381 if (AlignedNextFieldOffset == RecordSizeInBytes) {
382 // We don't need any padding.
383 return;
384 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000385
Anders Carlssond5d64132009-07-28 17:56:36 +0000386 unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes;
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000387 AppendBytes(NumPadBytes);
388}
389
Mike Stump11289f42009-09-09 15:08:12 +0000390void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes,
Anders Carlsson307846f2009-07-23 03:17:50 +0000391 const llvm::Type *FieldTy) {
392 AlignmentAsLLVMStruct = std::max(AlignmentAsLLVMStruct,
393 getTypeAlignment(FieldTy));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000394
395 uint64_t FieldSizeInBytes = getTypeSizeInBytes(FieldTy);
396
Anders Carlsson307846f2009-07-23 03:17:50 +0000397 FieldTypes.push_back(FieldTy);
Anders Carlsson307846f2009-07-23 03:17:50 +0000398
Anders Carlssond5d64132009-07-28 17:56:36 +0000399 NextFieldOffsetInBytes = FieldOffsetInBytes + FieldSizeInBytes;
Anders Carlsson307846f2009-07-23 03:17:50 +0000400 BitsAvailableInLastField = 0;
401}
402
Mike Stump11289f42009-09-09 15:08:12 +0000403void
Anders Carlsson307846f2009-07-23 03:17:50 +0000404CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
405 const llvm::Type *FieldTy) {
406 AppendPadding(FieldOffsetInBytes, getTypeAlignment(FieldTy));
407}
408
Mike Stump11289f42009-09-09 15:08:12 +0000409void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
Anders Carlsson307846f2009-07-23 03:17:50 +0000410 unsigned FieldAlignment) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000411 assert(NextFieldOffsetInBytes <= FieldOffsetInBytes &&
412 "Incorrect field layout!");
Mike Stump11289f42009-09-09 15:08:12 +0000413
Anders Carlsson307846f2009-07-23 03:17:50 +0000414 // Round up the field offset to the alignment of the field type.
Mike Stump11289f42009-09-09 15:08:12 +0000415 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson307846f2009-07-23 03:17:50 +0000416 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment);
417
418 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
419 // Even with alignment, the field offset is not at the right place,
420 // insert padding.
421 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
422
423 AppendBytes(PaddingInBytes);
424 }
425}
426
427void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) {
428 if (NumBytes == 0)
429 return;
Mike Stump11289f42009-09-09 15:08:12 +0000430
Owen Anderson41a75022009-08-13 21:57:51 +0000431 const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000432 if (NumBytes > 1)
Anders Carlsson307846f2009-07-23 03:17:50 +0000433 Ty = llvm::ArrayType::get(Ty, NumBytes);
Mike Stump11289f42009-09-09 15:08:12 +0000434
Anders Carlsson307846f2009-07-23 03:17:50 +0000435 // Append the padding field
Anders Carlssond5d64132009-07-28 17:56:36 +0000436 AppendField(NextFieldOffsetInBytes, Ty);
Anders Carlsson307846f2009-07-23 03:17:50 +0000437}
438
439unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlssond78fc892009-07-23 17:24:40 +0000440 if (Packed)
Anders Carlsson307846f2009-07-23 03:17:50 +0000441 return 1;
Mike Stump11289f42009-09-09 15:08:12 +0000442
Anders Carlsson307846f2009-07-23 03:17:50 +0000443 return Types.getTargetData().getABITypeAlignment(Ty);
444}
445
446uint64_t CGRecordLayoutBuilder::getTypeSizeInBytes(const llvm::Type *Ty) const {
447 return Types.getTargetData().getTypeAllocSize(Ty);
448}
449
Anders Carlssone8bfe412010-02-02 05:17:25 +0000450void CGRecordLayoutBuilder::CheckForPointerToDataMember(QualType T) {
Anders Carlssond606de72009-08-23 01:25:01 +0000451 // This record already contains a member pointer.
Anders Carlssone8bfe412010-02-02 05:17:25 +0000452 if (ContainsPointerToDataMember)
Anders Carlssond606de72009-08-23 01:25:01 +0000453 return;
Mike Stump11289f42009-09-09 15:08:12 +0000454
Anders Carlssond606de72009-08-23 01:25:01 +0000455 // Can only have member pointers if we're compiling C++.
456 if (!Types.getContext().getLangOptions().CPlusPlus)
457 return;
Mike Stump11289f42009-09-09 15:08:12 +0000458
Anders Carlssone8bfe412010-02-02 05:17:25 +0000459 T = Types.getContext().getBaseElementType(T);
Mike Stump11289f42009-09-09 15:08:12 +0000460
Anders Carlssone8bfe412010-02-02 05:17:25 +0000461 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) {
462 if (!MPT->getPointeeType()->isFunctionType()) {
463 // We have a pointer to data member.
464 ContainsPointerToDataMember = true;
465 }
466 } else if (const RecordType *RT = T->getAs<RecordType>()) {
467 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000468
Anders Carlssone8bfe412010-02-02 05:17:25 +0000469 // FIXME: It would be better if there was a way to explicitly compute the
470 // record layout instead of converting to a type.
471 Types.ConvertTagDeclType(RD);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000472
Anders Carlssone8bfe412010-02-02 05:17:25 +0000473 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000474
Anders Carlssone8bfe412010-02-02 05:17:25 +0000475 if (Layout.containsPointerToDataMember())
476 ContainsPointerToDataMember = true;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000477 }
Anders Carlssond606de72009-08-23 01:25:01 +0000478}
479
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000480CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) {
481 CGRecordLayoutBuilder Builder(*this);
Mike Stump11289f42009-09-09 15:08:12 +0000482
Anders Carlsson307846f2009-07-23 03:17:50 +0000483 Builder.Layout(D);
Anders Carlssone1d5ca52009-07-24 15:20:52 +0000484
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000485 const llvm::Type *Ty = llvm::StructType::get(getLLVMContext(),
Owen Anderson758428f2009-08-05 23:18:46 +0000486 Builder.FieldTypes,
Anders Carlssond78fc892009-07-23 17:24:40 +0000487 Builder.Packed);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000488 assert(getContext().getASTRecordLayout(D).getSize() / 8 ==
489 getTargetData().getTypeAllocSize(Ty) &&
Anders Carlsson453878b2009-08-08 18:01:57 +0000490 "Type size mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000491
Anders Carlsson307846f2009-07-23 03:17:50 +0000492 // Add all the field numbers.
493 for (unsigned i = 0, e = Builder.LLVMFields.size(); i != e; ++i) {
494 const FieldDecl *FD = Builder.LLVMFields[i].first;
495 unsigned FieldNo = Builder.LLVMFields[i].second;
496
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000497 addFieldInfo(FD, FieldNo);
Anders Carlsson307846f2009-07-23 03:17:50 +0000498 }
499
500 // Add bitfield info.
501 for (unsigned i = 0, e = Builder.LLVMBitFields.size(); i != e; ++i) {
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000502 const CGRecordLayoutBuilder::LLVMBitFieldInfo &Info =
503 Builder.LLVMBitFields[i];
Mike Stump11289f42009-09-09 15:08:12 +0000504
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000505 addBitFieldInfo(Info.FD, Info.FieldNo, Info.Start, Info.Size);
Anders Carlsson307846f2009-07-23 03:17:50 +0000506 }
Mike Stump11289f42009-09-09 15:08:12 +0000507
Anders Carlssone8bfe412010-02-02 05:17:25 +0000508 return new CGRecordLayout(Ty, Builder.ContainsPointerToDataMember);
Anders Carlsson307846f2009-07-23 03:17:50 +0000509}