blob: 7988106de5986b95d32dd366655a535b4d1895f6 [file] [log] [blame]
Anders Carlssonbda4c102009-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 Carlsson74cbe222009-07-19 00:18:47 +000014#include "clang/AST/DeclCXX.h"
Anders Carlsson93fab9d2009-07-18 20:50:59 +000015#include "clang/AST/DeclObjC.h"
Anders Carlssonbda4c102009-07-18 20:20:21 +000016#include "clang/AST/Expr.h"
17#include "clang/AST/RecordLayout.h"
18#include "clang/Basic/TargetInfo.h"
Mike Stump6f376332009-08-05 22:37:18 +000019#include <llvm/ADT/SmallSet.h>
Anders Carlssonbda4c102009-07-18 20:20:21 +000020#include <llvm/Support/MathExtras.h>
21
22using namespace clang;
23
24ASTRecordLayoutBuilder::ASTRecordLayoutBuilder(ASTContext &Ctx)
Anders Carlssona5dd7222009-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 Carlssonbda4c102009-07-18 20:20:21 +000027
Mike Stump2effeca2009-08-06 00:38:46 +000028/// LayoutVtable - Lay out the vtable and set PrimaryBase.
Mike Stump4ef98092009-08-13 22:53:07 +000029void ASTRecordLayoutBuilder::LayoutVtable(const CXXRecordDecl *RD,
30 llvm::SmallSet<const CXXRecordDecl*, 32> &IndirectPrimary) {
Mike Stump02b16232009-08-12 22:06:55 +000031 if (!RD->isDynamicClass()) {
Mike Stump2effeca2009-08-06 00:38:46 +000032 // There is no primary base in this case.
Mike Stump928f1502009-08-07 19:00:50 +000033 setPrimaryBase(0, false);
Mike Stump6f376332009-08-05 22:37:18 +000034 return;
Mike Stump2effeca2009-08-06 00:38:46 +000035 }
Mike Stump6f376332009-08-05 22:37:18 +000036
Mike Stump4ef98092009-08-13 22:53:07 +000037 SelectPrimaryBase(RD, IndirectPrimary);
Mike Stump6f376332009-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 Stump3dee6ef2009-07-30 00:22:38 +000044}
45
Anders Carlsson74cbe222009-07-19 00:18:47 +000046void
47ASTRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) {
Anders Carlsson74cbe222009-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 Kremenek6217b802009-07-29 21:53:49 +000052 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stump2effeca2009-08-06 00:38:46 +000053 // Skip the PrimaryBase here, as it is laid down first.
54 if (Base != PrimaryBase)
Mike Stumpeb19fa92009-08-06 13:41:24 +000055 LayoutBaseNonVirtually(Base);
Anders Carlsson74cbe222009-07-19 00:18:47 +000056 }
57 }
58}
59
Mike Stump6f376332009-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 Stump49520942009-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 Stump6f376332009-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 Stump49520942009-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 Stump4ef98092009-08-13 22:53:07 +000089 if (Base->getNumVBases())
90 SelectPrimaryForBase(Base, IndirectPrimary);
Mike Stump6f376332009-08-05 22:37:18 +000091 }
92}
93
Mike Stumpd76264e2009-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 Stump6f376332009-08-05 22:37:18 +0000118/// SelectPrimaryBase - Selects the primary base for the given class and
Mike Stump49520942009-08-11 04:03:59 +0000119/// record that with setPrimaryBase.
Mike Stump4ef98092009-08-13 22:53:07 +0000120void ASTRecordLayoutBuilder::SelectPrimaryBase(const CXXRecordDecl *RD,
121 llvm::SmallSet<const CXXRecordDecl*, 32> &IndirectPrimary) {
Mike Stump6f376332009-08-05 22:37:18 +0000122 // The primary base is the first non-virtual indirect or direct base class,
123 // if one exists.
124 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
125 e = RD->bases_end(); i != e; ++i) {
126 if (!i->isVirtual()) {
127 const CXXRecordDecl *Base =
128 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
129 if (Base->isDynamicClass()) {
Mike Stump928f1502009-08-07 19:00:50 +0000130 setPrimaryBase(Base, false);
Mike Stump6f376332009-08-05 22:37:18 +0000131 return;
132 }
133 }
134 }
135
Mike Stumpd76264e2009-08-12 21:50:08 +0000136 setPrimaryBase(0, false);
137
Mike Stump6f376332009-08-05 22:37:18 +0000138 // Otherwise, it is the first nearly empty virtual base that is not an
Mike Stump49520942009-08-11 04:03:59 +0000139 // indirect primary virtual base class, if one exists.
Mike Stump6f376332009-08-05 22:37:18 +0000140
141 // If we have no virtual bases at this point, bail out as the searching below
142 // is expensive.
Mike Stump6f376332009-08-05 22:37:18 +0000143 if (RD->getNumVBases() == 0) {
Mike Stump6f376332009-08-05 22:37:18 +0000144 return;
145 }
146
Mike Stump49520942009-08-11 04:03:59 +0000147 // First, we compute all the primary virtual bases for all of our direct and
148 // indirect bases, and record all their primary virtual base classes.
Mike Stump6f376332009-08-05 22:37:18 +0000149 const CXXRecordDecl *FirstPrimary = 0;
Mike Stump6f376332009-08-05 22:37:18 +0000150 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
151 e = RD->bases_end(); i != e; ++i) {
Mike Stump49520942009-08-11 04:03:59 +0000152 const CXXRecordDecl *Base =
153 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
154 SelectPrimaryForBase(Base, IndirectPrimary);
Mike Stump6f376332009-08-05 22:37:18 +0000155 }
156
157 // Then we can search for the first nearly empty virtual base itself.
Mike Stumpd76264e2009-08-12 21:50:08 +0000158 SelectPrimaryVBase(RD, FirstPrimary, IndirectPrimary);
Mike Stump6f376332009-08-05 22:37:18 +0000159
Mike Stump928f1502009-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 Stump6f376332009-08-05 22:37:18 +0000163 return;
164}
165
Mike Stumpeb19fa92009-08-06 13:41:24 +0000166void ASTRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *RD) {
167 LayoutBaseNonVirtually(RD);
168}
169
Mike Stumpd53cef12009-08-13 02:02:14 +0000170void ASTRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
171 llvm::SmallSet<const CXXRecordDecl*, 32> &IndirectPrimary) {
Mike Stump4ef98092009-08-13 22:53:07 +0000172 // FIXME: Calculate IndirectPrimary when !PrimaryBaseWasVirtual
173 // assert(PrimaryBaseWasVirtual && "FIXME: calculate IndirectPrimary");
174 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
175 e = RD->bases_end(); i != e; ++i) {
Mike Stumpeb19fa92009-08-06 13:41:24 +0000176 const CXXRecordDecl *Base =
177 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stump4ef98092009-08-13 22:53:07 +0000178 if (i->isVirtual() && !IndirectPrimary.count(Base)) {
179 // Mark it so we don't output it twice.
180 IndirectPrimary.insert(Base);
Mike Stumpeb19fa92009-08-06 13:41:24 +0000181 LayoutVirtualBase(Base);
Mike Stump4ef98092009-08-13 22:53:07 +0000182 }
183 if (Base->getNumVBases())
184 LayoutVirtualBases(Base, IndirectPrimary);
Mike Stumpeb19fa92009-08-06 13:41:24 +0000185 }
186}
187
188void ASTRecordLayoutBuilder::LayoutBaseNonVirtually(const CXXRecordDecl *RD) {
Anders Carlsson74cbe222009-07-19 00:18:47 +0000189 const ASTRecordLayout &BaseInfo = Ctx.getASTRecordLayout(RD);
190 assert(BaseInfo.getDataSize() > 0 &&
191 "FIXME: Handle empty classes.");
192
Anders Carlssonb2fafd42009-07-28 19:24:15 +0000193 unsigned BaseAlign = BaseInfo.getNonVirtualAlign();
194 uint64_t BaseSize = BaseInfo.getNonVirtualSize();
Anders Carlsson74cbe222009-07-19 00:18:47 +0000195
196 // Round up the current record size to the base's alignment boundary.
197 Size = (Size + (BaseAlign-1)) & ~(BaseAlign-1);
198
Anders Carlssonb2fafd42009-07-28 19:24:15 +0000199 // Add base class offsets.
200 Bases.push_back(RD);
201 BaseOffsets.push_back(Size);
202
Anders Carlsson74cbe222009-07-19 00:18:47 +0000203 // Reserve space for this base.
204 Size += BaseSize;
205
206 // Remember the next available offset.
207 NextOffset = Size;
208
209 // Remember max struct/class alignment.
210 UpdateAlignment(BaseAlign);
211}
212
Anders Carlssonbda4c102009-07-18 20:20:21 +0000213void ASTRecordLayoutBuilder::Layout(const RecordDecl *D) {
214 IsUnion = D->isUnion();
Anders Carlssona860e752009-08-08 18:23:56 +0000215
Anders Carlssona5dd7222009-08-08 19:38:24 +0000216 Packed = D->hasAttr<PackedAttr>();
217
218 // The #pragma pack attribute specifies the maximum field alignment.
Anders Carlssona860e752009-08-08 18:23:56 +0000219 if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>())
Anders Carlssona5dd7222009-08-08 19:38:24 +0000220 MaxFieldAlignment = PPA->getAlignment();
Anders Carlssonbda4c102009-07-18 20:20:21 +0000221
222 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
223 UpdateAlignment(AA->getAlignment());
Anders Carlsson74cbe222009-07-19 00:18:47 +0000224
Mike Stumpd53cef12009-08-13 02:02:14 +0000225 llvm::SmallSet<const CXXRecordDecl*, 32> IndirectPrimary;
226
Anders Carlsson74cbe222009-07-19 00:18:47 +0000227 // If this is a C++ class, lay out the nonvirtual bases.
Mike Stumpeb19fa92009-08-06 13:41:24 +0000228 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
229 if (RD) {
Mike Stump4ef98092009-08-13 22:53:07 +0000230 LayoutVtable(RD, IndirectPrimary);
Mike Stump2effeca2009-08-06 00:38:46 +0000231 // PrimaryBase goes first.
Mike Stumpd53cef12009-08-13 02:02:14 +0000232 if (PrimaryBase) {
233 // FIXME: We need all the primaries.
234 if (PrimaryBaseWasVirtual)
235 IndirectPrimary.insert(PrimaryBase);
Mike Stumpeb19fa92009-08-06 13:41:24 +0000236 LayoutBaseNonVirtually(PrimaryBase);
Mike Stumpd53cef12009-08-13 02:02:14 +0000237 }
Mike Stump3dee6ef2009-07-30 00:22:38 +0000238 LayoutNonVirtualBases(RD);
Mike Stump3dee6ef2009-07-30 00:22:38 +0000239 }
Anders Carlsson74cbe222009-07-19 00:18:47 +0000240
Anders Carlssona2df41c2009-07-18 21:48:39 +0000241 LayoutFields(D);
Anders Carlssonbda4c102009-07-18 20:20:21 +0000242
Anders Carlssonb2fafd42009-07-28 19:24:15 +0000243 NonVirtualSize = Size;
244 NonVirtualAlignment = Alignment;
Mike Stump3dee6ef2009-07-30 00:22:38 +0000245
Mike Stumpeb19fa92009-08-06 13:41:24 +0000246 if (RD)
Mike Stumpd53cef12009-08-13 02:02:14 +0000247 LayoutVirtualBases(RD, IndirectPrimary);
Mike Stumpeb19fa92009-08-06 13:41:24 +0000248
Anders Carlssonbda4c102009-07-18 20:20:21 +0000249 // Finally, round the size of the total struct up to the alignment of the
250 // struct itself.
251 FinishLayout();
252}
253
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000254void ASTRecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D,
255 const ObjCImplementationDecl *Impl) {
256 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
257 const ASTRecordLayout &SL = Ctx.getASTObjCInterfaceLayout(SD);
258
259 UpdateAlignment(SL.getAlignment());
260
261 // We start laying out ivars not at the end of the superclass
262 // structure, but at the next byte following the last field.
Anders Carlsson243a6852009-07-18 21:26:44 +0000263 Size = llvm::RoundUpToAlignment(SL.getDataSize(), 8);
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000264 NextOffset = Size;
265 }
266
Anders Carlssona5dd7222009-08-08 19:38:24 +0000267 Packed = D->hasAttr<PackedAttr>();
268
269 // The #pragma pack attribute specifies the maximum field alignment.
270 if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>())
271 MaxFieldAlignment = PPA->getAlignment();
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000272
273 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
274 UpdateAlignment(AA->getAlignment());
275
276 // Layout each ivar sequentially.
277 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
278 Ctx.ShallowCollectObjCIvars(D, Ivars, Impl);
279 for (unsigned i = 0, e = Ivars.size(); i != e; ++i)
280 LayoutField(Ivars[i]);
281
282 // Finally, round the size of the total struct up to the alignment of the
283 // struct itself.
284 FinishLayout();
285}
286
Anders Carlssona2df41c2009-07-18 21:48:39 +0000287void ASTRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
288 // Layout each field, for now, just sequentially, respecting alignment. In
289 // the future, this will need to be tweakable by targets.
290 for (RecordDecl::field_iterator Field = D->field_begin(),
291 FieldEnd = D->field_end(); Field != FieldEnd; ++Field)
292 LayoutField(*Field);
293}
294
Anders Carlssonbda4c102009-07-18 20:20:21 +0000295void ASTRecordLayoutBuilder::LayoutField(const FieldDecl *D) {
Anders Carlssona5dd7222009-08-08 19:38:24 +0000296 bool FieldPacked = Packed;
Anders Carlssonbda4c102009-07-18 20:20:21 +0000297 uint64_t FieldOffset = IsUnion ? 0 : Size;
298 uint64_t FieldSize;
299 unsigned FieldAlign;
300
Anders Carlssona5dd7222009-08-08 19:38:24 +0000301 FieldPacked |= D->hasAttr<PackedAttr>();
Anders Carlssonbda4c102009-07-18 20:20:21 +0000302
303 if (const Expr *BitWidthExpr = D->getBitWidth()) {
304 // TODO: Need to check this algorithm on other targets!
305 // (tested on Linux-X86)
306 FieldSize = BitWidthExpr->EvaluateAsInt(Ctx).getZExtValue();
307
308 std::pair<uint64_t, unsigned> FieldInfo = Ctx.getTypeInfo(D->getType());
309 uint64_t TypeSize = FieldInfo.first;
Anders Carlssona5dd7222009-08-08 19:38:24 +0000310
Anders Carlssonbda4c102009-07-18 20:20:21 +0000311 FieldAlign = FieldInfo.second;
Anders Carlssona5dd7222009-08-08 19:38:24 +0000312
313 if (FieldPacked)
314 FieldAlign = 1;
Anders Carlssonbda4c102009-07-18 20:20:21 +0000315 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
316 FieldAlign = std::max(FieldAlign, AA->getAlignment());
Anders Carlssona5dd7222009-08-08 19:38:24 +0000317 // The maximum field alignment overrides the aligned attribute.
318 if (MaxFieldAlignment)
319 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
320
Anders Carlssonbda4c102009-07-18 20:20:21 +0000321 // Check if we need to add padding to give the field the correct
322 // alignment.
323 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
324 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
325
326 // Padding members don't affect overall alignment
327 if (!D->getIdentifier())
328 FieldAlign = 1;
329 } else {
330 if (D->getType()->isIncompleteArrayType()) {
331 // This is a flexible array member; we can't directly
332 // query getTypeInfo about these, so we figure it out here.
333 // Flexible array members don't have any size, but they
334 // have to be aligned appropriately for their element type.
335 FieldSize = 0;
336 const ArrayType* ATy = Ctx.getAsArrayType(D->getType());
337 FieldAlign = Ctx.getTypeAlign(ATy->getElementType());
Ted Kremenek6217b802009-07-29 21:53:49 +0000338 } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) {
Anders Carlssonbda4c102009-07-18 20:20:21 +0000339 unsigned AS = RT->getPointeeType().getAddressSpace();
340 FieldSize = Ctx.Target.getPointerWidth(AS);
341 FieldAlign = Ctx.Target.getPointerAlign(AS);
342 } else {
343 std::pair<uint64_t, unsigned> FieldInfo = Ctx.getTypeInfo(D->getType());
344 FieldSize = FieldInfo.first;
345 FieldAlign = FieldInfo.second;
346 }
347
Anders Carlssona5dd7222009-08-08 19:38:24 +0000348 if (FieldPacked)
349 FieldAlign = 8;
Anders Carlssonbda4c102009-07-18 20:20:21 +0000350 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
351 FieldAlign = std::max(FieldAlign, AA->getAlignment());
Anders Carlssona5dd7222009-08-08 19:38:24 +0000352 // The maximum field alignment overrides the aligned attribute.
353 if (MaxFieldAlignment)
354 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
Anders Carlssonbda4c102009-07-18 20:20:21 +0000355
356 // Round up the current record size to the field's alignment boundary.
357 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
358 }
359
360 // Place this field at the current location.
361 FieldOffsets.push_back(FieldOffset);
362
363 // Reserve space for this field.
364 if (IsUnion)
365 Size = std::max(Size, FieldSize);
366 else
367 Size = FieldOffset + FieldSize;
368
369 // Remember the next available offset.
370 NextOffset = Size;
371
372 // Remember max struct/class alignment.
373 UpdateAlignment(FieldAlign);
374}
375
376void ASTRecordLayoutBuilder::FinishLayout() {
377 // In C++, records cannot be of size 0.
378 if (Ctx.getLangOptions().CPlusPlus && Size == 0)
379 Size = 8;
380 // Finally, round the size of the record up to the alignment of the
381 // record itself.
382 Size = (Size + (Alignment-1)) & ~(Alignment-1);
383}
384
385void ASTRecordLayoutBuilder::UpdateAlignment(unsigned NewAlignment) {
386 if (NewAlignment <= Alignment)
387 return;
388
389 assert(llvm::isPowerOf2_32(NewAlignment && "Alignment not a power of 2"));
390
391 Alignment = NewAlignment;
392}
393
394const ASTRecordLayout *
395ASTRecordLayoutBuilder::ComputeLayout(ASTContext &Ctx,
396 const RecordDecl *D) {
397 ASTRecordLayoutBuilder Builder(Ctx);
398
399 Builder.Layout(D);
400
Anders Carlssonb2fafd42009-07-28 19:24:15 +0000401 if (!isa<CXXRecordDecl>(D))
402 return new ASTRecordLayout(Builder.Size, Builder.Alignment, Builder.Size,
403 Builder.FieldOffsets.data(),
404 Builder.FieldOffsets.size());
Anders Carlsson74cbe222009-07-19 00:18:47 +0000405
Anders Carlssonb2fafd42009-07-28 19:24:15 +0000406 // FIXME: This is not always correct. See the part about bitfields at
407 // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info.
408 // FIXME: IsPODForThePurposeOfLayout should be stored in the record layout.
409 bool IsPODForThePurposeOfLayout = cast<CXXRecordDecl>(D)->isPOD();
410
411 assert(Builder.Bases.size() == Builder.BaseOffsets.size() &&
412 "Base offsets vector must be same size as bases vector!");
413
414 // FIXME: This should be done in FinalizeLayout.
Anders Carlsson74cbe222009-07-19 00:18:47 +0000415 uint64_t DataSize =
416 IsPODForThePurposeOfLayout ? Builder.Size : Builder.NextOffset;
Anders Carlssonb2fafd42009-07-28 19:24:15 +0000417 uint64_t NonVirtualSize =
418 IsPODForThePurposeOfLayout ? DataSize : Builder.NonVirtualSize;
Anders Carlsson74cbe222009-07-19 00:18:47 +0000419
420 return new ASTRecordLayout(Builder.Size, Builder.Alignment, DataSize,
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000421 Builder.FieldOffsets.data(),
Anders Carlssonb2fafd42009-07-28 19:24:15 +0000422 Builder.FieldOffsets.size(),
423 NonVirtualSize,
424 Builder.NonVirtualAlignment,
Mike Stump6f376332009-08-05 22:37:18 +0000425 Builder.PrimaryBase,
Mike Stump928f1502009-08-07 19:00:50 +0000426 Builder.PrimaryBaseWasVirtual,
Anders Carlssonb2fafd42009-07-28 19:24:15 +0000427 Builder.Bases.data(),
428 Builder.BaseOffsets.data(),
429 Builder.Bases.size());
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000430}
431
432const ASTRecordLayout *
433ASTRecordLayoutBuilder::ComputeLayout(ASTContext &Ctx,
434 const ObjCInterfaceDecl *D,
435 const ObjCImplementationDecl *Impl) {
436 ASTRecordLayoutBuilder Builder(Ctx);
437
438 Builder.Layout(D, Impl);
439
440 return new ASTRecordLayout(Builder.Size, Builder.Alignment,
441 Builder.NextOffset,
Anders Carlssonbda4c102009-07-18 20:20:21 +0000442 Builder.FieldOffsets.data(),
443 Builder.FieldOffsets.size());
444}