blob: 68350daa6c4af85cc0bdb55c21e7d8a749f70f23 [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 Stumpd8fe7b22009-08-05 22:37:18 +000030 // FIXME: audit indirect virtual bases
Mike Stump25094802009-08-06 00:38:46 +000031 if (!RD->isPolymorphic() && !RD->getNumVBases()) {
32 // 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
37 SelectPrimaryBase(RD);
38 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) {
77 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
78 e = RD->bases_end(); i != e; ++i) {
79 if (!i->isVirtual()) {
80 const CXXRecordDecl *Base =
81 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
82 // Only bases with virtual bases participate in computing the
83 // indirect primary base classes.
84 // FIXME: audit indirect virtual bases
85 if (Base->getNumVBases() == 0)
86 return;
87 // FIXME: This information is recomputed a whole lot, cache it instead.
88 SelectPrimaryBase(Base);
89 IndirectPrimary.insert(PrimaryBase);
90 SelectPrimaryForBase(Base, IndirectPrimary);
91 }
92 }
93}
94
95/// SelectPrimaryBase - Selects the primary base for the given class and
96/// records that with setPrimaryBase.
97void ASTRecordLayoutBuilder::SelectPrimaryBase(const CXXRecordDecl *RD) {
98 // The primary base is the first non-virtual indirect or direct base class,
99 // if one exists.
100 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
101 e = RD->bases_end(); i != e; ++i) {
102 if (!i->isVirtual()) {
103 const CXXRecordDecl *Base =
104 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
105 if (Base->isDynamicClass()) {
Mike Stumpc266c6d2009-08-07 19:00:50 +0000106 setPrimaryBase(Base, false);
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000107 return;
108 }
109 }
110 }
111
112 // Otherwise, it is the first nearly empty virtual base that is not an
113 // indirect primary base class, if one exists.
114
115 // If we have no virtual bases at this point, bail out as the searching below
116 // is expensive.
117 // FIXME: audit indirect virtual bases
118 if (RD->getNumVBases() == 0) {
Mike Stumpc266c6d2009-08-07 19:00:50 +0000119 setPrimaryBase(0, false);
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000120 return;
121 }
122
Mike Stumpe9c85a72009-08-06 12:52:13 +0000123 // First, we compute all the primary bases for all of our direct and indirect
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000124 // non-virtual bases, and record all their primary base classes.
125 const CXXRecordDecl *FirstPrimary = 0;
126 llvm::SmallSet<const CXXRecordDecl*, 32> IndirectPrimary;
127 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
128 e = RD->bases_end(); i != e; ++i) {
129 if (!i->isVirtual()) {
130 const CXXRecordDecl *Base =
131 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
132 SelectPrimaryForBase(Base, IndirectPrimary);
133 }
134 }
135
136 // Then we can search for the first nearly empty virtual base itself.
137 // FIXME: audit indirect virtual bases
138 for (CXXRecordDecl::base_class_const_iterator i = RD->vbases_begin(),
139 e = RD->vbases_end(); i != e; ++i) {
140 const CXXRecordDecl *Base =
141 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
142 if (IsNearlyEmpty(Base)) {
143 if (FirstPrimary==0)
144 FirstPrimary = Base;
145 if (!IndirectPrimary.count(Base)) {
Mike Stumpc266c6d2009-08-07 19:00:50 +0000146 setPrimaryBase(Base, true);
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000147 return;
148 }
149 }
150 }
151
Mike Stumpc266c6d2009-08-07 19:00:50 +0000152 // Otherwise if is the first nearly empty virtual base, if one exists,
153 // otherwise there is no primary base class.
154 setPrimaryBase(FirstPrimary, true);
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000155 return;
156}
157
Mike Stump6b2556f2009-08-06 13:41:24 +0000158void ASTRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *RD) {
159 LayoutBaseNonVirtually(RD);
160}
161
162void ASTRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD) {
163 // FIXME: audit indirect virtual bases
164 for (CXXRecordDecl::base_class_const_iterator i = RD->vbases_begin(),
165 e = RD->vbases_end(); i != e; ++i) {
166 const CXXRecordDecl *Base =
167 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
168 if (Base != PrimaryBase)
169 LayoutVirtualBase(Base);
170 }
171}
172
173void ASTRecordLayoutBuilder::LayoutBaseNonVirtually(const CXXRecordDecl *RD) {
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000174 const ASTRecordLayout &BaseInfo = Ctx.getASTRecordLayout(RD);
175 assert(BaseInfo.getDataSize() > 0 &&
176 "FIXME: Handle empty classes.");
177
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000178 unsigned BaseAlign = BaseInfo.getNonVirtualAlign();
179 uint64_t BaseSize = BaseInfo.getNonVirtualSize();
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000180
181 // Round up the current record size to the base's alignment boundary.
182 Size = (Size + (BaseAlign-1)) & ~(BaseAlign-1);
183
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000184 // Add base class offsets.
185 Bases.push_back(RD);
186 BaseOffsets.push_back(Size);
187
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000188 // Reserve space for this base.
189 Size += BaseSize;
190
191 // Remember the next available offset.
192 NextOffset = Size;
193
194 // Remember max struct/class alignment.
195 UpdateAlignment(BaseAlign);
196}
197
Anders Carlsson79474332009-07-18 20:20:21 +0000198void ASTRecordLayoutBuilder::Layout(const RecordDecl *D) {
199 IsUnion = D->isUnion();
Anders Carlsson68e0b682009-08-08 18:23:56 +0000200
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000201 Packed = D->hasAttr<PackedAttr>();
202
203 // The #pragma pack attribute specifies the maximum field alignment.
Anders Carlsson68e0b682009-08-08 18:23:56 +0000204 if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>())
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000205 MaxFieldAlignment = PPA->getAlignment();
Anders Carlsson79474332009-07-18 20:20:21 +0000206
207 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
208 UpdateAlignment(AA->getAlignment());
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000209
210 // If this is a C++ class, lay out the nonvirtual bases.
Mike Stump6b2556f2009-08-06 13:41:24 +0000211 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
212 if (RD) {
Mike Stump3dc7eb92009-07-30 00:22:38 +0000213 LayoutVtable(RD);
Mike Stump25094802009-08-06 00:38:46 +0000214 // PrimaryBase goes first.
215 if (PrimaryBase)
Mike Stump6b2556f2009-08-06 13:41:24 +0000216 LayoutBaseNonVirtually(PrimaryBase);
Mike Stump3dc7eb92009-07-30 00:22:38 +0000217 LayoutNonVirtualBases(RD);
Mike Stump3dc7eb92009-07-30 00:22:38 +0000218 }
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000219
Anders Carlsson118ce162009-07-18 21:48:39 +0000220 LayoutFields(D);
Anders Carlsson79474332009-07-18 20:20:21 +0000221
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000222 NonVirtualSize = Size;
223 NonVirtualAlignment = Alignment;
Mike Stump3dc7eb92009-07-30 00:22:38 +0000224
Mike Stump6b2556f2009-08-06 13:41:24 +0000225 if (RD)
226 LayoutVirtualBases(RD);
227
Anders Carlsson79474332009-07-18 20:20:21 +0000228 // Finally, round the size of the total struct up to the alignment of the
229 // struct itself.
230 FinishLayout();
231}
232
Anders Carlsson4f516282009-07-18 20:50:59 +0000233void ASTRecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D,
234 const ObjCImplementationDecl *Impl) {
235 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
236 const ASTRecordLayout &SL = Ctx.getASTObjCInterfaceLayout(SD);
237
238 UpdateAlignment(SL.getAlignment());
239
240 // We start laying out ivars not at the end of the superclass
241 // structure, but at the next byte following the last field.
Anders Carlsson27b50132009-07-18 21:26:44 +0000242 Size = llvm::RoundUpToAlignment(SL.getDataSize(), 8);
Anders Carlsson4f516282009-07-18 20:50:59 +0000243 NextOffset = Size;
244 }
245
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000246 Packed = D->hasAttr<PackedAttr>();
247
248 // The #pragma pack attribute specifies the maximum field alignment.
249 if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>())
250 MaxFieldAlignment = PPA->getAlignment();
Anders Carlsson4f516282009-07-18 20:50:59 +0000251
252 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
253 UpdateAlignment(AA->getAlignment());
254
255 // Layout each ivar sequentially.
256 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
257 Ctx.ShallowCollectObjCIvars(D, Ivars, Impl);
258 for (unsigned i = 0, e = Ivars.size(); i != e; ++i)
259 LayoutField(Ivars[i]);
260
261 // Finally, round the size of the total struct up to the alignment of the
262 // struct itself.
263 FinishLayout();
264}
265
Anders Carlsson118ce162009-07-18 21:48:39 +0000266void ASTRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
267 // Layout each field, for now, just sequentially, respecting alignment. In
268 // the future, this will need to be tweakable by targets.
269 for (RecordDecl::field_iterator Field = D->field_begin(),
270 FieldEnd = D->field_end(); Field != FieldEnd; ++Field)
271 LayoutField(*Field);
272}
273
Anders Carlsson79474332009-07-18 20:20:21 +0000274void ASTRecordLayoutBuilder::LayoutField(const FieldDecl *D) {
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000275 bool FieldPacked = Packed;
Anders Carlsson79474332009-07-18 20:20:21 +0000276 uint64_t FieldOffset = IsUnion ? 0 : Size;
277 uint64_t FieldSize;
278 unsigned FieldAlign;
279
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000280 FieldPacked |= D->hasAttr<PackedAttr>();
Anders Carlsson79474332009-07-18 20:20:21 +0000281
282 if (const Expr *BitWidthExpr = D->getBitWidth()) {
283 // TODO: Need to check this algorithm on other targets!
284 // (tested on Linux-X86)
285 FieldSize = BitWidthExpr->EvaluateAsInt(Ctx).getZExtValue();
286
287 std::pair<uint64_t, unsigned> FieldInfo = Ctx.getTypeInfo(D->getType());
288 uint64_t TypeSize = FieldInfo.first;
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000289
Anders Carlsson79474332009-07-18 20:20:21 +0000290 FieldAlign = FieldInfo.second;
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000291
292 if (FieldPacked)
293 FieldAlign = 1;
Anders Carlsson79474332009-07-18 20:20:21 +0000294 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
295 FieldAlign = std::max(FieldAlign, AA->getAlignment());
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000296 // The maximum field alignment overrides the aligned attribute.
297 if (MaxFieldAlignment)
298 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
299
Anders Carlsson79474332009-07-18 20:20:21 +0000300 // Check if we need to add padding to give the field the correct
301 // alignment.
302 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
303 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
304
305 // Padding members don't affect overall alignment
306 if (!D->getIdentifier())
307 FieldAlign = 1;
308 } else {
309 if (D->getType()->isIncompleteArrayType()) {
310 // This is a flexible array member; we can't directly
311 // query getTypeInfo about these, so we figure it out here.
312 // Flexible array members don't have any size, but they
313 // have to be aligned appropriately for their element type.
314 FieldSize = 0;
315 const ArrayType* ATy = Ctx.getAsArrayType(D->getType());
316 FieldAlign = Ctx.getTypeAlign(ATy->getElementType());
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000317 } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) {
Anders Carlsson79474332009-07-18 20:20:21 +0000318 unsigned AS = RT->getPointeeType().getAddressSpace();
319 FieldSize = Ctx.Target.getPointerWidth(AS);
320 FieldAlign = Ctx.Target.getPointerAlign(AS);
321 } else {
322 std::pair<uint64_t, unsigned> FieldInfo = Ctx.getTypeInfo(D->getType());
323 FieldSize = FieldInfo.first;
324 FieldAlign = FieldInfo.second;
325 }
326
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000327 if (FieldPacked)
328 FieldAlign = 8;
Anders Carlsson79474332009-07-18 20:20:21 +0000329 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
330 FieldAlign = std::max(FieldAlign, AA->getAlignment());
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000331 // The maximum field alignment overrides the aligned attribute.
332 if (MaxFieldAlignment)
333 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
Anders Carlsson79474332009-07-18 20:20:21 +0000334
335 // Round up the current record size to the field's alignment boundary.
336 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
337 }
338
339 // Place this field at the current location.
340 FieldOffsets.push_back(FieldOffset);
341
342 // Reserve space for this field.
343 if (IsUnion)
344 Size = std::max(Size, FieldSize);
345 else
346 Size = FieldOffset + FieldSize;
347
348 // Remember the next available offset.
349 NextOffset = Size;
350
351 // Remember max struct/class alignment.
352 UpdateAlignment(FieldAlign);
353}
354
355void ASTRecordLayoutBuilder::FinishLayout() {
356 // In C++, records cannot be of size 0.
357 if (Ctx.getLangOptions().CPlusPlus && Size == 0)
358 Size = 8;
359 // Finally, round the size of the record up to the alignment of the
360 // record itself.
361 Size = (Size + (Alignment-1)) & ~(Alignment-1);
362}
363
364void ASTRecordLayoutBuilder::UpdateAlignment(unsigned NewAlignment) {
365 if (NewAlignment <= Alignment)
366 return;
367
368 assert(llvm::isPowerOf2_32(NewAlignment && "Alignment not a power of 2"));
369
370 Alignment = NewAlignment;
371}
372
373const ASTRecordLayout *
374ASTRecordLayoutBuilder::ComputeLayout(ASTContext &Ctx,
375 const RecordDecl *D) {
376 ASTRecordLayoutBuilder Builder(Ctx);
377
378 Builder.Layout(D);
379
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000380 if (!isa<CXXRecordDecl>(D))
381 return new ASTRecordLayout(Builder.Size, Builder.Alignment, Builder.Size,
382 Builder.FieldOffsets.data(),
383 Builder.FieldOffsets.size());
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000384
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000385 // FIXME: This is not always correct. See the part about bitfields at
386 // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info.
387 // FIXME: IsPODForThePurposeOfLayout should be stored in the record layout.
388 bool IsPODForThePurposeOfLayout = cast<CXXRecordDecl>(D)->isPOD();
389
390 assert(Builder.Bases.size() == Builder.BaseOffsets.size() &&
391 "Base offsets vector must be same size as bases vector!");
392
393 // FIXME: This should be done in FinalizeLayout.
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000394 uint64_t DataSize =
395 IsPODForThePurposeOfLayout ? Builder.Size : Builder.NextOffset;
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000396 uint64_t NonVirtualSize =
397 IsPODForThePurposeOfLayout ? DataSize : Builder.NonVirtualSize;
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000398
399 return new ASTRecordLayout(Builder.Size, Builder.Alignment, DataSize,
Anders Carlsson4f516282009-07-18 20:50:59 +0000400 Builder.FieldOffsets.data(),
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000401 Builder.FieldOffsets.size(),
402 NonVirtualSize,
403 Builder.NonVirtualAlignment,
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000404 Builder.PrimaryBase,
Mike Stumpc266c6d2009-08-07 19:00:50 +0000405 Builder.PrimaryBaseWasVirtual,
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000406 Builder.Bases.data(),
407 Builder.BaseOffsets.data(),
408 Builder.Bases.size());
Anders Carlsson4f516282009-07-18 20:50:59 +0000409}
410
411const ASTRecordLayout *
412ASTRecordLayoutBuilder::ComputeLayout(ASTContext &Ctx,
413 const ObjCInterfaceDecl *D,
414 const ObjCImplementationDecl *Impl) {
415 ASTRecordLayoutBuilder Builder(Ctx);
416
417 Builder.Layout(D, Impl);
418
419 return new ASTRecordLayout(Builder.Size, Builder.Alignment,
420 Builder.NextOffset,
Anders Carlsson79474332009-07-18 20:20:21 +0000421 Builder.FieldOffsets.data(),
422 Builder.FieldOffsets.size());
423}