blob: 30682efbb8ad5313d5d6f2c683b87482af3c85a9 [file] [log] [blame]
Anders Carlsson79474332009-07-18 20:20:21 +00001//=== ASTRecordLayoutBuilder.cpp - Helper class for building record layouts ==//
2//
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
10#include "RecordLayoutBuilder.h"
11
12#include "clang/AST/Attr.h"
13#include "clang/AST/Decl.h"
Anders Carlsson6d9f6f32009-07-19 00:18:47 +000014#include "clang/AST/DeclCXX.h"
Anders Carlsson4f516282009-07-18 20:50:59 +000015#include "clang/AST/DeclObjC.h"
Anders Carlsson79474332009-07-18 20:20:21 +000016#include "clang/AST/Expr.h"
17#include "clang/AST/RecordLayout.h"
18#include "clang/Basic/TargetInfo.h"
Mike Stumpd8fe7b22009-08-05 22:37:18 +000019#include <llvm/ADT/SmallSet.h>
Anders Carlsson79474332009-07-18 20:20:21 +000020#include <llvm/Support/MathExtras.h>
21
22using namespace clang;
23
24ASTRecordLayoutBuilder::ASTRecordLayoutBuilder(ASTContext &Ctx)
Anders Carlsson28a5fa22009-08-08 19:38:24 +000025 : Ctx(Ctx), Size(0), Alignment(8), Packed(false), MaxFieldAlignment(0),
26 NextOffset(0), IsUnion(false), NonVirtualSize(0), NonVirtualAlignment(8) {}
Anders Carlsson79474332009-07-18 20:20:21 +000027
Mike Stump25094802009-08-06 00:38:46 +000028/// LayoutVtable - Lay out the vtable and set PrimaryBase.
Mike Stumpc2f591b2009-08-13 22:53:07 +000029void ASTRecordLayoutBuilder::LayoutVtable(const CXXRecordDecl *RD,
30 llvm::SmallSet<const CXXRecordDecl*, 32> &IndirectPrimary) {
Mike Stumpaa08da72009-08-12 22:06:55 +000031 if (!RD->isDynamicClass()) {
Mike Stump25094802009-08-06 00:38:46 +000032 // There is no primary base in this case.
Mike Stumpc266c6d2009-08-07 19:00:50 +000033 setPrimaryBase(0, false);
Mike Stumpd8fe7b22009-08-05 22:37:18 +000034 return;
Mike Stump25094802009-08-06 00:38:46 +000035 }
Mike Stumpd8fe7b22009-08-05 22:37:18 +000036
Mike Stumpc2f591b2009-08-13 22:53:07 +000037 SelectPrimaryBase(RD, IndirectPrimary);
Mike Stumpd8fe7b22009-08-05 22:37:18 +000038 if (PrimaryBase == 0) {
39 int AS = 0;
40 UpdateAlignment(Ctx.Target.getPointerAlign(AS));
41 Size += Ctx.Target.getPointerWidth(AS);
42 NextOffset = Size;
43 }
Mike Stump3dc7eb92009-07-30 00:22:38 +000044}
45
Anders Carlsson6d9f6f32009-07-19 00:18:47 +000046void
47ASTRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) {
Anders Carlsson6d9f6f32009-07-19 00:18:47 +000048 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
49 e = RD->bases_end(); i != e; ++i) {
50 if (!i->isVirtual()) {
51 const CXXRecordDecl *Base =
Ted Kremenekc23c7e62009-07-29 21:53:49 +000052 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stump25094802009-08-06 00:38:46 +000053 // Skip the PrimaryBase here, as it is laid down first.
Mike Stumpbcf756c2009-08-14 01:44:03 +000054 if (Base != PrimaryBase || PrimaryBaseWasVirtual)
55 LayoutBaseNonVirtually(Base, false);
Anders Carlsson6d9f6f32009-07-19 00:18:47 +000056 }
57 }
58}
59
Mike Stumpd8fe7b22009-08-05 22:37:18 +000060// Helper routines related to the abi definition from:
61// http://www.codesourcery.com/public/cxx-abi/abi.html
62//
63/// IsNearlyEmpty - Indicates when a class has a vtable pointer, but
64/// no other data.
65bool ASTRecordLayoutBuilder::IsNearlyEmpty(const CXXRecordDecl *RD) {
66 // FIXME: Audit the corners
67 if (!RD->isDynamicClass())
68 return false;
69 const ASTRecordLayout &BaseInfo = Ctx.getASTRecordLayout(RD);
70 if (BaseInfo.getNonVirtualSize() == Ctx.Target.getPointerWidth(0))
71 return true;
72 return false;
73}
74
75void ASTRecordLayoutBuilder::SelectPrimaryForBase(const CXXRecordDecl *RD,
76 llvm::SmallSet<const CXXRecordDecl*, 32> &IndirectPrimary) {
Mike Stump78696a72009-08-11 04:03:59 +000077 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD);
78 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
79 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
Mike Stumpbcf756c2009-08-14 01:44:03 +000080
81 if (PrimaryBaseWasVirtual)
Mike Stump78696a72009-08-11 04:03:59 +000082 IndirectPrimary.insert(PrimaryBase);
Mike Stumpbcf756c2009-08-14 01:44:03 +000083
Mike Stumpd8fe7b22009-08-05 22:37:18 +000084 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
85 e = RD->bases_end(); i != e; ++i) {
Mike Stump78696a72009-08-11 04:03:59 +000086 const CXXRecordDecl *Base =
87 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
88 // Only bases with virtual bases participate in computing the
89 // indirect primary virtual base classes.
Mike Stumpc2f591b2009-08-13 22:53:07 +000090 if (Base->getNumVBases())
91 SelectPrimaryForBase(Base, IndirectPrimary);
Mike Stumpd8fe7b22009-08-05 22:37:18 +000092 }
93}
94
Mike Stump6f3793b2009-08-12 21:50:08 +000095void ASTRecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD,
96 const CXXRecordDecl *&FirstPrimary,
97 llvm::SmallSet<const CXXRecordDecl*, 32> &IndirectPrimary) {
98 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
99 e = RD->bases_end(); i != e; ++i) {
100 const CXXRecordDecl *Base =
101 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
102 if (!i->isVirtual()) {
103 SelectPrimaryVBase(Base, FirstPrimary, IndirectPrimary);
104 if (PrimaryBase)
105 return;
106 continue;
107 }
108 if (IsNearlyEmpty(Base)) {
109 if (FirstPrimary==0)
110 FirstPrimary = Base;
111 if (!IndirectPrimary.count(Base)) {
112 setPrimaryBase(Base, true);
113 return;
114 }
115 }
116 }
117}
118
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000119/// SelectPrimaryBase - Selects the primary base for the given class and
Mike Stump590a7c72009-08-13 23:26:06 +0000120/// record that with setPrimaryBase. We also calculate the IndirectPrimaries.
Mike Stumpc2f591b2009-08-13 22:53:07 +0000121void ASTRecordLayoutBuilder::SelectPrimaryBase(const CXXRecordDecl *RD,
122 llvm::SmallSet<const CXXRecordDecl*, 32> &IndirectPrimary) {
Mike Stump590a7c72009-08-13 23:26:06 +0000123 // We compute all the primary virtual bases for all of our direct and
124 // indirect bases, and record all their primary virtual base classes.
125 const CXXRecordDecl *FirstPrimary = 0;
126 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
127 e = RD->bases_end(); i != e; ++i) {
128 const CXXRecordDecl *Base =
129 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
130 SelectPrimaryForBase(Base, IndirectPrimary);
131 }
132
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000133 // The primary base is the first non-virtual indirect or direct base class,
134 // if one exists.
135 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
136 e = RD->bases_end(); i != e; ++i) {
137 if (!i->isVirtual()) {
138 const CXXRecordDecl *Base =
139 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
140 if (Base->isDynamicClass()) {
Mike Stumpc266c6d2009-08-07 19:00:50 +0000141 setPrimaryBase(Base, false);
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000142 return;
143 }
144 }
145 }
146
Mike Stump6f3793b2009-08-12 21:50:08 +0000147 setPrimaryBase(0, false);
148
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000149 // Otherwise, it is the first nearly empty virtual base that is not an
Mike Stump78696a72009-08-11 04:03:59 +0000150 // indirect primary virtual base class, if one exists.
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000151
152 // If we have no virtual bases at this point, bail out as the searching below
153 // is expensive.
Mike Stump590a7c72009-08-13 23:26:06 +0000154 if (RD->getNumVBases() == 0)
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000155 return;
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000156
157 // Then we can search for the first nearly empty virtual base itself.
Mike Stump6f3793b2009-08-12 21:50:08 +0000158 SelectPrimaryVBase(RD, FirstPrimary, IndirectPrimary);
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000159
Mike Stumpc266c6d2009-08-07 19:00:50 +0000160 // Otherwise if is the first nearly empty virtual base, if one exists,
161 // otherwise there is no primary base class.
162 setPrimaryBase(FirstPrimary, true);
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000163 return;
164}
165
Mike Stump6b2556f2009-08-06 13:41:24 +0000166void ASTRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *RD) {
Mike Stumpbcf756c2009-08-14 01:44:03 +0000167 LayoutBaseNonVirtually(RD, true);
Mike Stump6b2556f2009-08-06 13:41:24 +0000168}
169
Mike Stump13007542009-08-13 02:02:14 +0000170void ASTRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
Mike Stump22ea1f82009-08-16 01:46:26 +0000171 int64_t Offset,
172 llvm::SmallSet<const CXXRecordDecl*, 32> &mark,
Mike Stump13007542009-08-13 02:02:14 +0000173 llvm::SmallSet<const CXXRecordDecl*, 32> &IndirectPrimary) {
Mike Stumpc2f591b2009-08-13 22:53:07 +0000174 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
175 e = RD->bases_end(); i != e; ++i) {
Mike Stump6b2556f2009-08-06 13:41:24 +0000176 const CXXRecordDecl *Base =
177 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stump22ea1f82009-08-16 01:46:26 +0000178#if 0
179 const ASTRecordLayout &L = Ctx.getASTRecordLayout(Base);
180 const CXXRecordDecl *PB = L.getPrimaryBase();
181 if (PB && L.getPrimaryBaseWasVirtual()
182 && IndirectPrimary.count(PB)) {
183 int64_t BaseOffset;
184 // FIXME: calculate this.
185 BaseOffset = (1<<63) | (1<<31);
186 VBases.push_back(PB);
187 VBaseOffsets.push_back(BaseOffset);
188 }
189#endif
190 if (i->isVirtual()) {
191 // Mark it so we don't lay it out twice.
192 if (mark.count(Base))
193 continue;
194 if (IndirectPrimary.count(Base)) {
195 int64_t BaseOffset;
196 // FIXME: audit
197 BaseOffset = Offset;
198 // BaseOffset = (1<<63) | (1<<31);
199 VBases.push_back(Base);
200 VBaseOffsets.push_back(BaseOffset);
201 } else
202 LayoutVirtualBase(Base);
Mike Stumpc2f591b2009-08-13 22:53:07 +0000203 }
204 if (Base->getNumVBases())
Mike Stump22ea1f82009-08-16 01:46:26 +0000205 LayoutVirtualBases(Base, Offset, mark, IndirectPrimary);
Mike Stump6b2556f2009-08-06 13:41:24 +0000206 }
207}
208
Mike Stumpbcf756c2009-08-14 01:44:03 +0000209void ASTRecordLayoutBuilder::LayoutBaseNonVirtually(const CXXRecordDecl *RD,
210 bool IsVirtualBase) {
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000211 const ASTRecordLayout &BaseInfo = Ctx.getASTRecordLayout(RD);
212 assert(BaseInfo.getDataSize() > 0 &&
213 "FIXME: Handle empty classes.");
214
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000215 unsigned BaseAlign = BaseInfo.getNonVirtualAlign();
216 uint64_t BaseSize = BaseInfo.getNonVirtualSize();
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000217
218 // Round up the current record size to the base's alignment boundary.
219 Size = (Size + (BaseAlign-1)) & ~(BaseAlign-1);
220
Mike Stump22ea1f82009-08-16 01:46:26 +0000221 // Add base class offsets.
Mike Stumpbcf756c2009-08-14 01:44:03 +0000222 if (IsVirtualBase) {
223 VBases.push_back(RD);
224 VBaseOffsets.push_back(Size);
225 } else {
226 Bases.push_back(RD);
227 BaseOffsets.push_back(Size);
228 }
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000229
Mike Stump22ea1f82009-08-16 01:46:26 +0000230 // And now add offsets for all our primary virtual bases as well, so
231 // they all have offsets.
232 const ASTRecordLayout *L = &BaseInfo;
233 const CXXRecordDecl *PB = L->getPrimaryBase();
234 while (PB) {
235 if (L->getPrimaryBaseWasVirtual()) {
236 VBases.push_back(PB);
237 VBaseOffsets.push_back(Size);
238 }
239 PB = L->getPrimaryBase();
240 if (PB)
241 L = &Ctx.getASTRecordLayout(PB);
242 }
243
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000244 // Reserve space for this base.
245 Size += BaseSize;
246
247 // Remember the next available offset.
248 NextOffset = Size;
249
250 // Remember max struct/class alignment.
251 UpdateAlignment(BaseAlign);
252}
253
Anders Carlsson79474332009-07-18 20:20:21 +0000254void ASTRecordLayoutBuilder::Layout(const RecordDecl *D) {
255 IsUnion = D->isUnion();
Anders Carlsson68e0b682009-08-08 18:23:56 +0000256
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000257 Packed = D->hasAttr<PackedAttr>();
258
259 // The #pragma pack attribute specifies the maximum field alignment.
Anders Carlsson68e0b682009-08-08 18:23:56 +0000260 if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>())
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000261 MaxFieldAlignment = PPA->getAlignment();
Anders Carlsson79474332009-07-18 20:20:21 +0000262
263 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
264 UpdateAlignment(AA->getAlignment());
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000265
Mike Stump13007542009-08-13 02:02:14 +0000266 llvm::SmallSet<const CXXRecordDecl*, 32> IndirectPrimary;
267
Mike Stump22ea1f82009-08-16 01:46:26 +0000268 // If this is a C++ class, lay out the vtable and the non-virtual bases.
Mike Stump6b2556f2009-08-06 13:41:24 +0000269 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
270 if (RD) {
Mike Stumpc2f591b2009-08-13 22:53:07 +0000271 LayoutVtable(RD, IndirectPrimary);
Mike Stump25094802009-08-06 00:38:46 +0000272 // PrimaryBase goes first.
Mike Stump13007542009-08-13 02:02:14 +0000273 if (PrimaryBase) {
Mike Stump13007542009-08-13 02:02:14 +0000274 if (PrimaryBaseWasVirtual)
275 IndirectPrimary.insert(PrimaryBase);
Mike Stumpbcf756c2009-08-14 01:44:03 +0000276 LayoutBaseNonVirtually(PrimaryBase, PrimaryBaseWasVirtual);
Mike Stump13007542009-08-13 02:02:14 +0000277 }
Mike Stump3dc7eb92009-07-30 00:22:38 +0000278 LayoutNonVirtualBases(RD);
Mike Stump3dc7eb92009-07-30 00:22:38 +0000279 }
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000280
Anders Carlsson118ce162009-07-18 21:48:39 +0000281 LayoutFields(D);
Anders Carlsson79474332009-07-18 20:20:21 +0000282
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000283 NonVirtualSize = Size;
284 NonVirtualAlignment = Alignment;
Mike Stump3dc7eb92009-07-30 00:22:38 +0000285
Mike Stump22ea1f82009-08-16 01:46:26 +0000286 if (RD) {
287 llvm::SmallSet<const CXXRecordDecl*, 32> mark;
288 LayoutVirtualBases(RD, 0, mark, IndirectPrimary);
289 }
Mike Stump6b2556f2009-08-06 13:41:24 +0000290
Anders Carlsson79474332009-07-18 20:20:21 +0000291 // Finally, round the size of the total struct up to the alignment of the
292 // struct itself.
293 FinishLayout();
294}
295
Anders Carlsson4f516282009-07-18 20:50:59 +0000296void ASTRecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D,
297 const ObjCImplementationDecl *Impl) {
298 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
299 const ASTRecordLayout &SL = Ctx.getASTObjCInterfaceLayout(SD);
300
301 UpdateAlignment(SL.getAlignment());
302
303 // We start laying out ivars not at the end of the superclass
304 // structure, but at the next byte following the last field.
Anders Carlsson27b50132009-07-18 21:26:44 +0000305 Size = llvm::RoundUpToAlignment(SL.getDataSize(), 8);
Anders Carlsson4f516282009-07-18 20:50:59 +0000306 NextOffset = Size;
307 }
308
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000309 Packed = D->hasAttr<PackedAttr>();
310
311 // The #pragma pack attribute specifies the maximum field alignment.
312 if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>())
313 MaxFieldAlignment = PPA->getAlignment();
Anders Carlsson4f516282009-07-18 20:50:59 +0000314
315 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
316 UpdateAlignment(AA->getAlignment());
317
318 // Layout each ivar sequentially.
319 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
320 Ctx.ShallowCollectObjCIvars(D, Ivars, Impl);
321 for (unsigned i = 0, e = Ivars.size(); i != e; ++i)
322 LayoutField(Ivars[i]);
323
324 // Finally, round the size of the total struct up to the alignment of the
325 // struct itself.
326 FinishLayout();
327}
328
Anders Carlsson118ce162009-07-18 21:48:39 +0000329void ASTRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
330 // Layout each field, for now, just sequentially, respecting alignment. In
331 // the future, this will need to be tweakable by targets.
332 for (RecordDecl::field_iterator Field = D->field_begin(),
333 FieldEnd = D->field_end(); Field != FieldEnd; ++Field)
334 LayoutField(*Field);
335}
336
Anders Carlsson79474332009-07-18 20:20:21 +0000337void ASTRecordLayoutBuilder::LayoutField(const FieldDecl *D) {
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000338 bool FieldPacked = Packed;
Anders Carlsson79474332009-07-18 20:20:21 +0000339 uint64_t FieldOffset = IsUnion ? 0 : Size;
340 uint64_t FieldSize;
341 unsigned FieldAlign;
342
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000343 FieldPacked |= D->hasAttr<PackedAttr>();
Anders Carlsson79474332009-07-18 20:20:21 +0000344
345 if (const Expr *BitWidthExpr = D->getBitWidth()) {
346 // TODO: Need to check this algorithm on other targets!
347 // (tested on Linux-X86)
348 FieldSize = BitWidthExpr->EvaluateAsInt(Ctx).getZExtValue();
349
350 std::pair<uint64_t, unsigned> FieldInfo = Ctx.getTypeInfo(D->getType());
351 uint64_t TypeSize = FieldInfo.first;
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000352
Anders Carlsson79474332009-07-18 20:20:21 +0000353 FieldAlign = FieldInfo.second;
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000354
355 if (FieldPacked)
356 FieldAlign = 1;
Anders Carlsson79474332009-07-18 20:20:21 +0000357 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
358 FieldAlign = std::max(FieldAlign, AA->getAlignment());
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000359 // The maximum field alignment overrides the aligned attribute.
360 if (MaxFieldAlignment)
361 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
362
Anders Carlsson79474332009-07-18 20:20:21 +0000363 // Check if we need to add padding to give the field the correct
364 // alignment.
365 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
366 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
367
368 // Padding members don't affect overall alignment
369 if (!D->getIdentifier())
370 FieldAlign = 1;
371 } else {
372 if (D->getType()->isIncompleteArrayType()) {
373 // This is a flexible array member; we can't directly
374 // query getTypeInfo about these, so we figure it out here.
375 // Flexible array members don't have any size, but they
376 // have to be aligned appropriately for their element type.
377 FieldSize = 0;
378 const ArrayType* ATy = Ctx.getAsArrayType(D->getType());
379 FieldAlign = Ctx.getTypeAlign(ATy->getElementType());
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000380 } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) {
Anders Carlsson79474332009-07-18 20:20:21 +0000381 unsigned AS = RT->getPointeeType().getAddressSpace();
382 FieldSize = Ctx.Target.getPointerWidth(AS);
383 FieldAlign = Ctx.Target.getPointerAlign(AS);
384 } else {
385 std::pair<uint64_t, unsigned> FieldInfo = Ctx.getTypeInfo(D->getType());
386 FieldSize = FieldInfo.first;
387 FieldAlign = FieldInfo.second;
388 }
389
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000390 if (FieldPacked)
391 FieldAlign = 8;
Anders Carlsson79474332009-07-18 20:20:21 +0000392 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
393 FieldAlign = std::max(FieldAlign, AA->getAlignment());
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000394 // The maximum field alignment overrides the aligned attribute.
395 if (MaxFieldAlignment)
396 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
Anders Carlsson79474332009-07-18 20:20:21 +0000397
398 // Round up the current record size to the field's alignment boundary.
399 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
400 }
401
402 // Place this field at the current location.
403 FieldOffsets.push_back(FieldOffset);
404
405 // Reserve space for this field.
406 if (IsUnion)
407 Size = std::max(Size, FieldSize);
408 else
409 Size = FieldOffset + FieldSize;
410
411 // Remember the next available offset.
412 NextOffset = Size;
413
414 // Remember max struct/class alignment.
415 UpdateAlignment(FieldAlign);
416}
417
418void ASTRecordLayoutBuilder::FinishLayout() {
419 // In C++, records cannot be of size 0.
420 if (Ctx.getLangOptions().CPlusPlus && Size == 0)
421 Size = 8;
422 // Finally, round the size of the record up to the alignment of the
423 // record itself.
424 Size = (Size + (Alignment-1)) & ~(Alignment-1);
425}
426
427void ASTRecordLayoutBuilder::UpdateAlignment(unsigned NewAlignment) {
428 if (NewAlignment <= Alignment)
429 return;
430
431 assert(llvm::isPowerOf2_32(NewAlignment && "Alignment not a power of 2"));
432
433 Alignment = NewAlignment;
434}
435
436const ASTRecordLayout *
437ASTRecordLayoutBuilder::ComputeLayout(ASTContext &Ctx,
438 const RecordDecl *D) {
439 ASTRecordLayoutBuilder Builder(Ctx);
440
441 Builder.Layout(D);
442
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000443 if (!isa<CXXRecordDecl>(D))
444 return new ASTRecordLayout(Builder.Size, Builder.Alignment, Builder.Size,
445 Builder.FieldOffsets.data(),
446 Builder.FieldOffsets.size());
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000447
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000448 // FIXME: This is not always correct. See the part about bitfields at
449 // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info.
450 // FIXME: IsPODForThePurposeOfLayout should be stored in the record layout.
451 bool IsPODForThePurposeOfLayout = cast<CXXRecordDecl>(D)->isPOD();
452
453 assert(Builder.Bases.size() == Builder.BaseOffsets.size() &&
454 "Base offsets vector must be same size as bases vector!");
Mike Stumpbcf756c2009-08-14 01:44:03 +0000455 assert(Builder.VBases.size() == Builder.VBaseOffsets.size() &&
456 "Base offsets vector must be same size as bases vector!");
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000457
458 // FIXME: This should be done in FinalizeLayout.
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000459 uint64_t DataSize =
460 IsPODForThePurposeOfLayout ? Builder.Size : Builder.NextOffset;
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000461 uint64_t NonVirtualSize =
462 IsPODForThePurposeOfLayout ? DataSize : Builder.NonVirtualSize;
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000463
464 return new ASTRecordLayout(Builder.Size, Builder.Alignment, DataSize,
Anders Carlsson4f516282009-07-18 20:50:59 +0000465 Builder.FieldOffsets.data(),
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000466 Builder.FieldOffsets.size(),
467 NonVirtualSize,
468 Builder.NonVirtualAlignment,
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000469 Builder.PrimaryBase,
Mike Stumpc266c6d2009-08-07 19:00:50 +0000470 Builder.PrimaryBaseWasVirtual,
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000471 Builder.Bases.data(),
472 Builder.BaseOffsets.data(),
Mike Stumpbcf756c2009-08-14 01:44:03 +0000473 Builder.Bases.size(),
474 Builder.VBases.data(),
475 Builder.VBaseOffsets.data(),
476 Builder.VBases.size());
Anders Carlsson4f516282009-07-18 20:50:59 +0000477}
478
479const ASTRecordLayout *
480ASTRecordLayoutBuilder::ComputeLayout(ASTContext &Ctx,
481 const ObjCInterfaceDecl *D,
482 const ObjCImplementationDecl *Impl) {
483 ASTRecordLayoutBuilder Builder(Ctx);
484
485 Builder.Layout(D, Impl);
486
487 return new ASTRecordLayout(Builder.Size, Builder.Alignment,
488 Builder.NextOffset,
Anders Carlsson79474332009-07-18 20:20:21 +0000489 Builder.FieldOffsets.data(),
490 Builder.FieldOffsets.size());
491}