blob: 9c56121ee141a3a1265abbe12a0c19da08358b3b [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.
54 if (Base != PrimaryBase)
Mike Stump6b2556f2009-08-06 13:41:24 +000055 LayoutBaseNonVirtually(Base);
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();
80 if (PrimaryBaseWasVirtual) {
81 IndirectPrimary.insert(PrimaryBase);
82 }
Mike Stumpd8fe7b22009-08-05 22:37:18 +000083 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
84 e = RD->bases_end(); i != e; ++i) {
Mike Stump78696a72009-08-11 04:03:59 +000085 const CXXRecordDecl *Base =
86 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
87 // Only bases with virtual bases participate in computing the
88 // indirect primary virtual base classes.
Mike Stumpc2f591b2009-08-13 22:53:07 +000089 if (Base->getNumVBases())
90 SelectPrimaryForBase(Base, IndirectPrimary);
Mike Stumpd8fe7b22009-08-05 22:37:18 +000091 }
92}
93
Mike Stump6f3793b2009-08-12 21:50:08 +000094void ASTRecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD,
95 const CXXRecordDecl *&FirstPrimary,
96 llvm::SmallSet<const CXXRecordDecl*, 32> &IndirectPrimary) {
97 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
98 e = RD->bases_end(); i != e; ++i) {
99 const CXXRecordDecl *Base =
100 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
101 if (!i->isVirtual()) {
102 SelectPrimaryVBase(Base, FirstPrimary, IndirectPrimary);
103 if (PrimaryBase)
104 return;
105 continue;
106 }
107 if (IsNearlyEmpty(Base)) {
108 if (FirstPrimary==0)
109 FirstPrimary = Base;
110 if (!IndirectPrimary.count(Base)) {
111 setPrimaryBase(Base, true);
112 return;
113 }
114 }
115 }
116}
117
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000118/// SelectPrimaryBase - Selects the primary base for the given class and
Mike Stump590a7c72009-08-13 23:26:06 +0000119/// record that with setPrimaryBase. We also calculate the IndirectPrimaries.
Mike Stumpc2f591b2009-08-13 22:53:07 +0000120void ASTRecordLayoutBuilder::SelectPrimaryBase(const CXXRecordDecl *RD,
121 llvm::SmallSet<const CXXRecordDecl*, 32> &IndirectPrimary) {
Mike Stump590a7c72009-08-13 23:26:06 +0000122 // We compute all the primary virtual bases for all of our direct and
123 // indirect bases, and record all their primary virtual base classes.
124 const CXXRecordDecl *FirstPrimary = 0;
125 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
126 e = RD->bases_end(); i != e; ++i) {
127 const CXXRecordDecl *Base =
128 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
129 SelectPrimaryForBase(Base, IndirectPrimary);
130 }
131
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000132 // The primary base is the first non-virtual indirect or direct base class,
133 // if one exists.
134 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
135 e = RD->bases_end(); i != e; ++i) {
136 if (!i->isVirtual()) {
137 const CXXRecordDecl *Base =
138 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
139 if (Base->isDynamicClass()) {
Mike Stumpc266c6d2009-08-07 19:00:50 +0000140 setPrimaryBase(Base, false);
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000141 return;
142 }
143 }
144 }
145
Mike Stump6f3793b2009-08-12 21:50:08 +0000146 setPrimaryBase(0, false);
147
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000148 // Otherwise, it is the first nearly empty virtual base that is not an
Mike Stump78696a72009-08-11 04:03:59 +0000149 // indirect primary virtual base class, if one exists.
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000150
151 // If we have no virtual bases at this point, bail out as the searching below
152 // is expensive.
Mike Stump590a7c72009-08-13 23:26:06 +0000153 if (RD->getNumVBases() == 0)
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000154 return;
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000155
156 // Then we can search for the first nearly empty virtual base itself.
Mike Stump6f3793b2009-08-12 21:50:08 +0000157 SelectPrimaryVBase(RD, FirstPrimary, IndirectPrimary);
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000158
Mike Stumpc266c6d2009-08-07 19:00:50 +0000159 // Otherwise if is the first nearly empty virtual base, if one exists,
160 // otherwise there is no primary base class.
161 setPrimaryBase(FirstPrimary, true);
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000162 return;
163}
164
Mike Stump6b2556f2009-08-06 13:41:24 +0000165void ASTRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *RD) {
166 LayoutBaseNonVirtually(RD);
167}
168
Mike Stump13007542009-08-13 02:02:14 +0000169void ASTRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
170 llvm::SmallSet<const CXXRecordDecl*, 32> &IndirectPrimary) {
Mike Stumpc2f591b2009-08-13 22:53:07 +0000171 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
172 e = RD->bases_end(); i != e; ++i) {
Mike Stump6b2556f2009-08-06 13:41:24 +0000173 const CXXRecordDecl *Base =
174 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stumpc2f591b2009-08-13 22:53:07 +0000175 if (i->isVirtual() && !IndirectPrimary.count(Base)) {
176 // Mark it so we don't output it twice.
177 IndirectPrimary.insert(Base);
Mike Stump6b2556f2009-08-06 13:41:24 +0000178 LayoutVirtualBase(Base);
Mike Stumpc2f591b2009-08-13 22:53:07 +0000179 }
180 if (Base->getNumVBases())
181 LayoutVirtualBases(Base, IndirectPrimary);
Mike Stump6b2556f2009-08-06 13:41:24 +0000182 }
183}
184
185void ASTRecordLayoutBuilder::LayoutBaseNonVirtually(const CXXRecordDecl *RD) {
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000186 const ASTRecordLayout &BaseInfo = Ctx.getASTRecordLayout(RD);
187 assert(BaseInfo.getDataSize() > 0 &&
188 "FIXME: Handle empty classes.");
189
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000190 unsigned BaseAlign = BaseInfo.getNonVirtualAlign();
191 uint64_t BaseSize = BaseInfo.getNonVirtualSize();
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000192
193 // Round up the current record size to the base's alignment boundary.
194 Size = (Size + (BaseAlign-1)) & ~(BaseAlign-1);
195
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000196 // Add base class offsets.
197 Bases.push_back(RD);
198 BaseOffsets.push_back(Size);
199
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000200 // Reserve space for this base.
201 Size += BaseSize;
202
203 // Remember the next available offset.
204 NextOffset = Size;
205
206 // Remember max struct/class alignment.
207 UpdateAlignment(BaseAlign);
208}
209
Anders Carlsson79474332009-07-18 20:20:21 +0000210void ASTRecordLayoutBuilder::Layout(const RecordDecl *D) {
211 IsUnion = D->isUnion();
Anders Carlsson68e0b682009-08-08 18:23:56 +0000212
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000213 Packed = D->hasAttr<PackedAttr>();
214
215 // The #pragma pack attribute specifies the maximum field alignment.
Anders Carlsson68e0b682009-08-08 18:23:56 +0000216 if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>())
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000217 MaxFieldAlignment = PPA->getAlignment();
Anders Carlsson79474332009-07-18 20:20:21 +0000218
219 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
220 UpdateAlignment(AA->getAlignment());
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000221
Mike Stump13007542009-08-13 02:02:14 +0000222 llvm::SmallSet<const CXXRecordDecl*, 32> IndirectPrimary;
223
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000224 // If this is a C++ class, lay out the nonvirtual bases.
Mike Stump6b2556f2009-08-06 13:41:24 +0000225 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
226 if (RD) {
Mike Stumpc2f591b2009-08-13 22:53:07 +0000227 LayoutVtable(RD, IndirectPrimary);
Mike Stump25094802009-08-06 00:38:46 +0000228 // PrimaryBase goes first.
Mike Stump13007542009-08-13 02:02:14 +0000229 if (PrimaryBase) {
Mike Stump13007542009-08-13 02:02:14 +0000230 if (PrimaryBaseWasVirtual)
231 IndirectPrimary.insert(PrimaryBase);
Mike Stump6b2556f2009-08-06 13:41:24 +0000232 LayoutBaseNonVirtually(PrimaryBase);
Mike Stump13007542009-08-13 02:02:14 +0000233 }
Mike Stump3dc7eb92009-07-30 00:22:38 +0000234 LayoutNonVirtualBases(RD);
Mike Stump3dc7eb92009-07-30 00:22:38 +0000235 }
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000236
Anders Carlsson118ce162009-07-18 21:48:39 +0000237 LayoutFields(D);
Anders Carlsson79474332009-07-18 20:20:21 +0000238
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000239 NonVirtualSize = Size;
240 NonVirtualAlignment = Alignment;
Mike Stump3dc7eb92009-07-30 00:22:38 +0000241
Mike Stump6b2556f2009-08-06 13:41:24 +0000242 if (RD)
Mike Stump13007542009-08-13 02:02:14 +0000243 LayoutVirtualBases(RD, IndirectPrimary);
Mike Stump6b2556f2009-08-06 13:41:24 +0000244
Anders Carlsson79474332009-07-18 20:20:21 +0000245 // Finally, round the size of the total struct up to the alignment of the
246 // struct itself.
247 FinishLayout();
248}
249
Anders Carlsson4f516282009-07-18 20:50:59 +0000250void ASTRecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D,
251 const ObjCImplementationDecl *Impl) {
252 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
253 const ASTRecordLayout &SL = Ctx.getASTObjCInterfaceLayout(SD);
254
255 UpdateAlignment(SL.getAlignment());
256
257 // We start laying out ivars not at the end of the superclass
258 // structure, but at the next byte following the last field.
Anders Carlsson27b50132009-07-18 21:26:44 +0000259 Size = llvm::RoundUpToAlignment(SL.getDataSize(), 8);
Anders Carlsson4f516282009-07-18 20:50:59 +0000260 NextOffset = Size;
261 }
262
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000263 Packed = D->hasAttr<PackedAttr>();
264
265 // The #pragma pack attribute specifies the maximum field alignment.
266 if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>())
267 MaxFieldAlignment = PPA->getAlignment();
Anders Carlsson4f516282009-07-18 20:50:59 +0000268
269 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
270 UpdateAlignment(AA->getAlignment());
271
272 // Layout each ivar sequentially.
273 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
274 Ctx.ShallowCollectObjCIvars(D, Ivars, Impl);
275 for (unsigned i = 0, e = Ivars.size(); i != e; ++i)
276 LayoutField(Ivars[i]);
277
278 // Finally, round the size of the total struct up to the alignment of the
279 // struct itself.
280 FinishLayout();
281}
282
Anders Carlsson118ce162009-07-18 21:48:39 +0000283void ASTRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
284 // Layout each field, for now, just sequentially, respecting alignment. In
285 // the future, this will need to be tweakable by targets.
286 for (RecordDecl::field_iterator Field = D->field_begin(),
287 FieldEnd = D->field_end(); Field != FieldEnd; ++Field)
288 LayoutField(*Field);
289}
290
Anders Carlsson79474332009-07-18 20:20:21 +0000291void ASTRecordLayoutBuilder::LayoutField(const FieldDecl *D) {
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000292 bool FieldPacked = Packed;
Anders Carlsson79474332009-07-18 20:20:21 +0000293 uint64_t FieldOffset = IsUnion ? 0 : Size;
294 uint64_t FieldSize;
295 unsigned FieldAlign;
296
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000297 FieldPacked |= D->hasAttr<PackedAttr>();
Anders Carlsson79474332009-07-18 20:20:21 +0000298
299 if (const Expr *BitWidthExpr = D->getBitWidth()) {
300 // TODO: Need to check this algorithm on other targets!
301 // (tested on Linux-X86)
302 FieldSize = BitWidthExpr->EvaluateAsInt(Ctx).getZExtValue();
303
304 std::pair<uint64_t, unsigned> FieldInfo = Ctx.getTypeInfo(D->getType());
305 uint64_t TypeSize = FieldInfo.first;
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000306
Anders Carlsson79474332009-07-18 20:20:21 +0000307 FieldAlign = FieldInfo.second;
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000308
309 if (FieldPacked)
310 FieldAlign = 1;
Anders Carlsson79474332009-07-18 20:20:21 +0000311 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
312 FieldAlign = std::max(FieldAlign, AA->getAlignment());
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000313 // The maximum field alignment overrides the aligned attribute.
314 if (MaxFieldAlignment)
315 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
316
Anders Carlsson79474332009-07-18 20:20:21 +0000317 // Check if we need to add padding to give the field the correct
318 // alignment.
319 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
320 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
321
322 // Padding members don't affect overall alignment
323 if (!D->getIdentifier())
324 FieldAlign = 1;
325 } else {
326 if (D->getType()->isIncompleteArrayType()) {
327 // This is a flexible array member; we can't directly
328 // query getTypeInfo about these, so we figure it out here.
329 // Flexible array members don't have any size, but they
330 // have to be aligned appropriately for their element type.
331 FieldSize = 0;
332 const ArrayType* ATy = Ctx.getAsArrayType(D->getType());
333 FieldAlign = Ctx.getTypeAlign(ATy->getElementType());
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000334 } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) {
Anders Carlsson79474332009-07-18 20:20:21 +0000335 unsigned AS = RT->getPointeeType().getAddressSpace();
336 FieldSize = Ctx.Target.getPointerWidth(AS);
337 FieldAlign = Ctx.Target.getPointerAlign(AS);
338 } else {
339 std::pair<uint64_t, unsigned> FieldInfo = Ctx.getTypeInfo(D->getType());
340 FieldSize = FieldInfo.first;
341 FieldAlign = FieldInfo.second;
342 }
343
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000344 if (FieldPacked)
345 FieldAlign = 8;
Anders Carlsson79474332009-07-18 20:20:21 +0000346 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
347 FieldAlign = std::max(FieldAlign, AA->getAlignment());
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000348 // The maximum field alignment overrides the aligned attribute.
349 if (MaxFieldAlignment)
350 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
Anders Carlsson79474332009-07-18 20:20:21 +0000351
352 // Round up the current record size to the field's alignment boundary.
353 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
354 }
355
356 // Place this field at the current location.
357 FieldOffsets.push_back(FieldOffset);
358
359 // Reserve space for this field.
360 if (IsUnion)
361 Size = std::max(Size, FieldSize);
362 else
363 Size = FieldOffset + FieldSize;
364
365 // Remember the next available offset.
366 NextOffset = Size;
367
368 // Remember max struct/class alignment.
369 UpdateAlignment(FieldAlign);
370}
371
372void ASTRecordLayoutBuilder::FinishLayout() {
373 // In C++, records cannot be of size 0.
374 if (Ctx.getLangOptions().CPlusPlus && Size == 0)
375 Size = 8;
376 // Finally, round the size of the record up to the alignment of the
377 // record itself.
378 Size = (Size + (Alignment-1)) & ~(Alignment-1);
379}
380
381void ASTRecordLayoutBuilder::UpdateAlignment(unsigned NewAlignment) {
382 if (NewAlignment <= Alignment)
383 return;
384
385 assert(llvm::isPowerOf2_32(NewAlignment && "Alignment not a power of 2"));
386
387 Alignment = NewAlignment;
388}
389
390const ASTRecordLayout *
391ASTRecordLayoutBuilder::ComputeLayout(ASTContext &Ctx,
392 const RecordDecl *D) {
393 ASTRecordLayoutBuilder Builder(Ctx);
394
395 Builder.Layout(D);
396
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000397 if (!isa<CXXRecordDecl>(D))
398 return new ASTRecordLayout(Builder.Size, Builder.Alignment, Builder.Size,
399 Builder.FieldOffsets.data(),
400 Builder.FieldOffsets.size());
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000401
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000402 // FIXME: This is not always correct. See the part about bitfields at
403 // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info.
404 // FIXME: IsPODForThePurposeOfLayout should be stored in the record layout.
405 bool IsPODForThePurposeOfLayout = cast<CXXRecordDecl>(D)->isPOD();
406
407 assert(Builder.Bases.size() == Builder.BaseOffsets.size() &&
408 "Base offsets vector must be same size as bases vector!");
409
410 // FIXME: This should be done in FinalizeLayout.
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000411 uint64_t DataSize =
412 IsPODForThePurposeOfLayout ? Builder.Size : Builder.NextOffset;
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000413 uint64_t NonVirtualSize =
414 IsPODForThePurposeOfLayout ? DataSize : Builder.NonVirtualSize;
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000415
416 return new ASTRecordLayout(Builder.Size, Builder.Alignment, DataSize,
Anders Carlsson4f516282009-07-18 20:50:59 +0000417 Builder.FieldOffsets.data(),
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000418 Builder.FieldOffsets.size(),
419 NonVirtualSize,
420 Builder.NonVirtualAlignment,
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000421 Builder.PrimaryBase,
Mike Stumpc266c6d2009-08-07 19:00:50 +0000422 Builder.PrimaryBaseWasVirtual,
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000423 Builder.Bases.data(),
424 Builder.BaseOffsets.data(),
425 Builder.Bases.size());
Anders Carlsson4f516282009-07-18 20:50:59 +0000426}
427
428const ASTRecordLayout *
429ASTRecordLayoutBuilder::ComputeLayout(ASTContext &Ctx,
430 const ObjCInterfaceDecl *D,
431 const ObjCImplementationDecl *Impl) {
432 ASTRecordLayoutBuilder Builder(Ctx);
433
434 Builder.Layout(D, Impl);
435
436 return new ASTRecordLayout(Builder.Size, Builder.Alignment,
437 Builder.NextOffset,
Anders Carlsson79474332009-07-18 20:20:21 +0000438 Builder.FieldOffsets.data(),
439 Builder.FieldOffsets.size());
440}