blob: bd994143087ca3e4d7cfa34361030f78130d250f [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 Stump3dc7eb92009-07-30 00:22:38 +000029void ASTRecordLayoutBuilder::LayoutVtable(const CXXRecordDecl *RD) {
Mike Stumpaa08da72009-08-12 22:06:55 +000030 if (!RD->isDynamicClass()) {
Mike Stump25094802009-08-06 00:38:46 +000031 // There is no primary base in this case.
Mike Stumpc266c6d2009-08-07 19:00:50 +000032 setPrimaryBase(0, false);
Mike Stumpd8fe7b22009-08-05 22:37:18 +000033 return;
Mike Stump25094802009-08-06 00:38:46 +000034 }
Mike Stumpd8fe7b22009-08-05 22:37:18 +000035
36 SelectPrimaryBase(RD);
37 if (PrimaryBase == 0) {
38 int AS = 0;
39 UpdateAlignment(Ctx.Target.getPointerAlign(AS));
40 Size += Ctx.Target.getPointerWidth(AS);
41 NextOffset = Size;
42 }
Mike Stump3dc7eb92009-07-30 00:22:38 +000043}
44
Anders Carlsson6d9f6f32009-07-19 00:18:47 +000045void
46ASTRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) {
Anders Carlsson6d9f6f32009-07-19 00:18:47 +000047 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
48 e = RD->bases_end(); i != e; ++i) {
49 if (!i->isVirtual()) {
50 const CXXRecordDecl *Base =
Ted Kremenekc23c7e62009-07-29 21:53:49 +000051 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stump25094802009-08-06 00:38:46 +000052 // Skip the PrimaryBase here, as it is laid down first.
53 if (Base != PrimaryBase)
Mike Stump6b2556f2009-08-06 13:41:24 +000054 LayoutBaseNonVirtually(Base);
Anders Carlsson6d9f6f32009-07-19 00:18:47 +000055 }
56 }
57}
58
Mike Stumpd8fe7b22009-08-05 22:37:18 +000059// Helper routines related to the abi definition from:
60// http://www.codesourcery.com/public/cxx-abi/abi.html
61//
62/// IsNearlyEmpty - Indicates when a class has a vtable pointer, but
63/// no other data.
64bool ASTRecordLayoutBuilder::IsNearlyEmpty(const CXXRecordDecl *RD) {
65 // FIXME: Audit the corners
66 if (!RD->isDynamicClass())
67 return false;
68 const ASTRecordLayout &BaseInfo = Ctx.getASTRecordLayout(RD);
69 if (BaseInfo.getNonVirtualSize() == Ctx.Target.getPointerWidth(0))
70 return true;
71 return false;
72}
73
74void ASTRecordLayoutBuilder::SelectPrimaryForBase(const CXXRecordDecl *RD,
75 llvm::SmallSet<const CXXRecordDecl*, 32> &IndirectPrimary) {
Mike Stump78696a72009-08-11 04:03:59 +000076 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD);
77 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
78 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
79 if (PrimaryBaseWasVirtual) {
80 IndirectPrimary.insert(PrimaryBase);
81 }
Mike Stumpd8fe7b22009-08-05 22:37:18 +000082 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
83 e = RD->bases_end(); i != e; ++i) {
Mike Stump78696a72009-08-11 04:03:59 +000084 const CXXRecordDecl *Base =
85 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
86 // Only bases with virtual bases participate in computing the
87 // indirect primary virtual base classes.
Mike Stump78696a72009-08-11 04:03:59 +000088 if (Base->getNumVBases() == 0)
89 continue;
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 Stump78696a72009-08-11 04:03:59 +0000119/// record that with setPrimaryBase.
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000120void ASTRecordLayoutBuilder::SelectPrimaryBase(const CXXRecordDecl *RD) {
121 // The primary base is the first non-virtual indirect or direct base class,
122 // if one exists.
123 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
124 e = RD->bases_end(); i != e; ++i) {
125 if (!i->isVirtual()) {
126 const CXXRecordDecl *Base =
127 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
128 if (Base->isDynamicClass()) {
Mike Stumpc266c6d2009-08-07 19:00:50 +0000129 setPrimaryBase(Base, false);
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000130 return;
131 }
132 }
133 }
134
Mike Stump6f3793b2009-08-12 21:50:08 +0000135 setPrimaryBase(0, false);
136
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000137 // Otherwise, it is the first nearly empty virtual base that is not an
Mike Stump78696a72009-08-11 04:03:59 +0000138 // indirect primary virtual base class, if one exists.
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000139
140 // If we have no virtual bases at this point, bail out as the searching below
141 // is expensive.
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000142 if (RD->getNumVBases() == 0) {
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000143 return;
144 }
145
Mike Stump78696a72009-08-11 04:03:59 +0000146 // First, we compute all the primary virtual bases for all of our direct and
147 // indirect bases, and record all their primary virtual base classes.
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000148 const CXXRecordDecl *FirstPrimary = 0;
149 llvm::SmallSet<const CXXRecordDecl*, 32> IndirectPrimary;
150 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
151 e = RD->bases_end(); i != e; ++i) {
Mike Stump78696a72009-08-11 04:03:59 +0000152 const CXXRecordDecl *Base =
153 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
154 SelectPrimaryForBase(Base, IndirectPrimary);
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000155 }
156
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) {
167 LayoutBaseNonVirtually(RD);
168}
169
Mike Stump13007542009-08-13 02:02:14 +0000170void ASTRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
171 llvm::SmallSet<const CXXRecordDecl*, 32> &IndirectPrimary) {
172 // FIXME: Though complete, this is the wrong order
Mike Stump6b2556f2009-08-06 13:41:24 +0000173 for (CXXRecordDecl::base_class_const_iterator i = RD->vbases_begin(),
174 e = RD->vbases_end(); i != e; ++i) {
175 const CXXRecordDecl *Base =
176 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stump13007542009-08-13 02:02:14 +0000177 if (!IndirectPrimary.count(Base))
Mike Stump6b2556f2009-08-06 13:41:24 +0000178 LayoutVirtualBase(Base);
179 }
180}
181
182void ASTRecordLayoutBuilder::LayoutBaseNonVirtually(const CXXRecordDecl *RD) {
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000183 const ASTRecordLayout &BaseInfo = Ctx.getASTRecordLayout(RD);
184 assert(BaseInfo.getDataSize() > 0 &&
185 "FIXME: Handle empty classes.");
186
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000187 unsigned BaseAlign = BaseInfo.getNonVirtualAlign();
188 uint64_t BaseSize = BaseInfo.getNonVirtualSize();
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000189
190 // Round up the current record size to the base's alignment boundary.
191 Size = (Size + (BaseAlign-1)) & ~(BaseAlign-1);
192
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000193 // Add base class offsets.
194 Bases.push_back(RD);
195 BaseOffsets.push_back(Size);
196
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000197 // Reserve space for this base.
198 Size += BaseSize;
199
200 // Remember the next available offset.
201 NextOffset = Size;
202
203 // Remember max struct/class alignment.
204 UpdateAlignment(BaseAlign);
205}
206
Anders Carlsson79474332009-07-18 20:20:21 +0000207void ASTRecordLayoutBuilder::Layout(const RecordDecl *D) {
208 IsUnion = D->isUnion();
Anders Carlsson68e0b682009-08-08 18:23:56 +0000209
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000210 Packed = D->hasAttr<PackedAttr>();
211
212 // The #pragma pack attribute specifies the maximum field alignment.
Anders Carlsson68e0b682009-08-08 18:23:56 +0000213 if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>())
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000214 MaxFieldAlignment = PPA->getAlignment();
Anders Carlsson79474332009-07-18 20:20:21 +0000215
216 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
217 UpdateAlignment(AA->getAlignment());
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000218
Mike Stump13007542009-08-13 02:02:14 +0000219 // FIXME: Calculate this completely.
220 llvm::SmallSet<const CXXRecordDecl*, 32> IndirectPrimary;
221
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000222 // If this is a C++ class, lay out the nonvirtual bases.
Mike Stump6b2556f2009-08-06 13:41:24 +0000223 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
224 if (RD) {
Mike Stump3dc7eb92009-07-30 00:22:38 +0000225 LayoutVtable(RD);
Mike Stump25094802009-08-06 00:38:46 +0000226 // PrimaryBase goes first.
Mike Stump13007542009-08-13 02:02:14 +0000227 if (PrimaryBase) {
228 // FIXME: We need all the primaries.
229 if (PrimaryBaseWasVirtual)
230 IndirectPrimary.insert(PrimaryBase);
Mike Stump6b2556f2009-08-06 13:41:24 +0000231 LayoutBaseNonVirtually(PrimaryBase);
Mike Stump13007542009-08-13 02:02:14 +0000232 }
Mike Stump3dc7eb92009-07-30 00:22:38 +0000233 LayoutNonVirtualBases(RD);
Mike Stump3dc7eb92009-07-30 00:22:38 +0000234 }
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000235
Anders Carlsson118ce162009-07-18 21:48:39 +0000236 LayoutFields(D);
Anders Carlsson79474332009-07-18 20:20:21 +0000237
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000238 NonVirtualSize = Size;
239 NonVirtualAlignment = Alignment;
Mike Stump3dc7eb92009-07-30 00:22:38 +0000240
Mike Stump6b2556f2009-08-06 13:41:24 +0000241 if (RD)
Mike Stump13007542009-08-13 02:02:14 +0000242 LayoutVirtualBases(RD, IndirectPrimary);
Mike Stump6b2556f2009-08-06 13:41:24 +0000243
Anders Carlsson79474332009-07-18 20:20:21 +0000244 // Finally, round the size of the total struct up to the alignment of the
245 // struct itself.
246 FinishLayout();
247}
248
Anders Carlsson4f516282009-07-18 20:50:59 +0000249void ASTRecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D,
250 const ObjCImplementationDecl *Impl) {
251 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
252 const ASTRecordLayout &SL = Ctx.getASTObjCInterfaceLayout(SD);
253
254 UpdateAlignment(SL.getAlignment());
255
256 // We start laying out ivars not at the end of the superclass
257 // structure, but at the next byte following the last field.
Anders Carlsson27b50132009-07-18 21:26:44 +0000258 Size = llvm::RoundUpToAlignment(SL.getDataSize(), 8);
Anders Carlsson4f516282009-07-18 20:50:59 +0000259 NextOffset = Size;
260 }
261
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000262 Packed = D->hasAttr<PackedAttr>();
263
264 // The #pragma pack attribute specifies the maximum field alignment.
265 if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>())
266 MaxFieldAlignment = PPA->getAlignment();
Anders Carlsson4f516282009-07-18 20:50:59 +0000267
268 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
269 UpdateAlignment(AA->getAlignment());
270
271 // Layout each ivar sequentially.
272 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
273 Ctx.ShallowCollectObjCIvars(D, Ivars, Impl);
274 for (unsigned i = 0, e = Ivars.size(); i != e; ++i)
275 LayoutField(Ivars[i]);
276
277 // Finally, round the size of the total struct up to the alignment of the
278 // struct itself.
279 FinishLayout();
280}
281
Anders Carlsson118ce162009-07-18 21:48:39 +0000282void ASTRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
283 // Layout each field, for now, just sequentially, respecting alignment. In
284 // the future, this will need to be tweakable by targets.
285 for (RecordDecl::field_iterator Field = D->field_begin(),
286 FieldEnd = D->field_end(); Field != FieldEnd; ++Field)
287 LayoutField(*Field);
288}
289
Anders Carlsson79474332009-07-18 20:20:21 +0000290void ASTRecordLayoutBuilder::LayoutField(const FieldDecl *D) {
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000291 bool FieldPacked = Packed;
Anders Carlsson79474332009-07-18 20:20:21 +0000292 uint64_t FieldOffset = IsUnion ? 0 : Size;
293 uint64_t FieldSize;
294 unsigned FieldAlign;
295
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000296 FieldPacked |= D->hasAttr<PackedAttr>();
Anders Carlsson79474332009-07-18 20:20:21 +0000297
298 if (const Expr *BitWidthExpr = D->getBitWidth()) {
299 // TODO: Need to check this algorithm on other targets!
300 // (tested on Linux-X86)
301 FieldSize = BitWidthExpr->EvaluateAsInt(Ctx).getZExtValue();
302
303 std::pair<uint64_t, unsigned> FieldInfo = Ctx.getTypeInfo(D->getType());
304 uint64_t TypeSize = FieldInfo.first;
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000305
Anders Carlsson79474332009-07-18 20:20:21 +0000306 FieldAlign = FieldInfo.second;
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000307
308 if (FieldPacked)
309 FieldAlign = 1;
Anders Carlsson79474332009-07-18 20:20:21 +0000310 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
311 FieldAlign = std::max(FieldAlign, AA->getAlignment());
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000312 // The maximum field alignment overrides the aligned attribute.
313 if (MaxFieldAlignment)
314 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
315
Anders Carlsson79474332009-07-18 20:20:21 +0000316 // Check if we need to add padding to give the field the correct
317 // alignment.
318 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
319 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
320
321 // Padding members don't affect overall alignment
322 if (!D->getIdentifier())
323 FieldAlign = 1;
324 } else {
325 if (D->getType()->isIncompleteArrayType()) {
326 // This is a flexible array member; we can't directly
327 // query getTypeInfo about these, so we figure it out here.
328 // Flexible array members don't have any size, but they
329 // have to be aligned appropriately for their element type.
330 FieldSize = 0;
331 const ArrayType* ATy = Ctx.getAsArrayType(D->getType());
332 FieldAlign = Ctx.getTypeAlign(ATy->getElementType());
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000333 } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) {
Anders Carlsson79474332009-07-18 20:20:21 +0000334 unsigned AS = RT->getPointeeType().getAddressSpace();
335 FieldSize = Ctx.Target.getPointerWidth(AS);
336 FieldAlign = Ctx.Target.getPointerAlign(AS);
337 } else {
338 std::pair<uint64_t, unsigned> FieldInfo = Ctx.getTypeInfo(D->getType());
339 FieldSize = FieldInfo.first;
340 FieldAlign = FieldInfo.second;
341 }
342
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000343 if (FieldPacked)
344 FieldAlign = 8;
Anders Carlsson79474332009-07-18 20:20:21 +0000345 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
346 FieldAlign = std::max(FieldAlign, AA->getAlignment());
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000347 // The maximum field alignment overrides the aligned attribute.
348 if (MaxFieldAlignment)
349 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
Anders Carlsson79474332009-07-18 20:20:21 +0000350
351 // Round up the current record size to the field's alignment boundary.
352 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
353 }
354
355 // Place this field at the current location.
356 FieldOffsets.push_back(FieldOffset);
357
358 // Reserve space for this field.
359 if (IsUnion)
360 Size = std::max(Size, FieldSize);
361 else
362 Size = FieldOffset + FieldSize;
363
364 // Remember the next available offset.
365 NextOffset = Size;
366
367 // Remember max struct/class alignment.
368 UpdateAlignment(FieldAlign);
369}
370
371void ASTRecordLayoutBuilder::FinishLayout() {
372 // In C++, records cannot be of size 0.
373 if (Ctx.getLangOptions().CPlusPlus && Size == 0)
374 Size = 8;
375 // Finally, round the size of the record up to the alignment of the
376 // record itself.
377 Size = (Size + (Alignment-1)) & ~(Alignment-1);
378}
379
380void ASTRecordLayoutBuilder::UpdateAlignment(unsigned NewAlignment) {
381 if (NewAlignment <= Alignment)
382 return;
383
384 assert(llvm::isPowerOf2_32(NewAlignment && "Alignment not a power of 2"));
385
386 Alignment = NewAlignment;
387}
388
389const ASTRecordLayout *
390ASTRecordLayoutBuilder::ComputeLayout(ASTContext &Ctx,
391 const RecordDecl *D) {
392 ASTRecordLayoutBuilder Builder(Ctx);
393
394 Builder.Layout(D);
395
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000396 if (!isa<CXXRecordDecl>(D))
397 return new ASTRecordLayout(Builder.Size, Builder.Alignment, Builder.Size,
398 Builder.FieldOffsets.data(),
399 Builder.FieldOffsets.size());
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000400
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000401 // FIXME: This is not always correct. See the part about bitfields at
402 // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info.
403 // FIXME: IsPODForThePurposeOfLayout should be stored in the record layout.
404 bool IsPODForThePurposeOfLayout = cast<CXXRecordDecl>(D)->isPOD();
405
406 assert(Builder.Bases.size() == Builder.BaseOffsets.size() &&
407 "Base offsets vector must be same size as bases vector!");
408
409 // FIXME: This should be done in FinalizeLayout.
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000410 uint64_t DataSize =
411 IsPODForThePurposeOfLayout ? Builder.Size : Builder.NextOffset;
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000412 uint64_t NonVirtualSize =
413 IsPODForThePurposeOfLayout ? DataSize : Builder.NonVirtualSize;
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000414
415 return new ASTRecordLayout(Builder.Size, Builder.Alignment, DataSize,
Anders Carlsson4f516282009-07-18 20:50:59 +0000416 Builder.FieldOffsets.data(),
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000417 Builder.FieldOffsets.size(),
418 NonVirtualSize,
419 Builder.NonVirtualAlignment,
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000420 Builder.PrimaryBase,
Mike Stumpc266c6d2009-08-07 19:00:50 +0000421 Builder.PrimaryBaseWasVirtual,
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000422 Builder.Bases.data(),
423 Builder.BaseOffsets.data(),
424 Builder.Bases.size());
Anders Carlsson4f516282009-07-18 20:50:59 +0000425}
426
427const ASTRecordLayout *
428ASTRecordLayoutBuilder::ComputeLayout(ASTContext &Ctx,
429 const ObjCInterfaceDecl *D,
430 const ObjCImplementationDecl *Impl) {
431 ASTRecordLayoutBuilder Builder(Ctx);
432
433 Builder.Layout(D, Impl);
434
435 return new ASTRecordLayout(Builder.Size, Builder.Alignment,
436 Builder.NextOffset,
Anders Carlsson79474332009-07-18 20:20:21 +0000437 Builder.FieldOffsets.data(),
438 Builder.FieldOffsets.size());
439}