blob: 0253327d09a3871e2a62a111787a2ba16aa17ca2 [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"
Anders Carlsson79474332009-07-18 20:20:21 +000017#include "clang/Basic/TargetInfo.h"
Mike Stumpd8fe7b22009-08-05 22:37:18 +000018#include <llvm/ADT/SmallSet.h>
Anders Carlsson79474332009-07-18 20:20:21 +000019#include <llvm/Support/MathExtras.h>
20
21using namespace clang;
22
Mike Stump11289f42009-09-09 15:08:12 +000023ASTRecordLayoutBuilder::ASTRecordLayoutBuilder(ASTContext &Ctx)
Anders Carlssonba958402009-11-22 19:13:51 +000024 : Ctx(Ctx), Size(0), Alignment(8), Packed(false), UnfilledBitsInLastByte(0),
25 MaxFieldAlignment(0), DataSize(0), IsUnion(false), NonVirtualSize(0),
Anders Carlsson8630b5b2010-03-11 00:15:35 +000026 NonVirtualAlignment(8), FirstNearlyEmptyVBase(0) { }
Mike Stump3dc7eb92009-07-30 00:22:38 +000027
Mike Stumpd8fe7b22009-08-05 22:37:18 +000028/// IsNearlyEmpty - Indicates when a class has a vtable pointer, but
29/// no other data.
Anders Carlsson81430692009-09-22 03:02:06 +000030bool ASTRecordLayoutBuilder::IsNearlyEmpty(const CXXRecordDecl *RD) const {
Mike Stumpd8fe7b22009-08-05 22:37:18 +000031 // FIXME: Audit the corners
32 if (!RD->isDynamicClass())
33 return false;
34 const ASTRecordLayout &BaseInfo = Ctx.getASTRecordLayout(RD);
35 if (BaseInfo.getNonVirtualSize() == Ctx.Target.getPointerWidth(0))
36 return true;
37 return false;
38}
39
Anders Carlsson81430692009-09-22 03:02:06 +000040void ASTRecordLayoutBuilder::IdentifyPrimaryBases(const CXXRecordDecl *RD) {
Anders Carlsson03ff3792009-11-27 22:05:05 +000041 const ASTRecordLayout::PrimaryBaseInfo &BaseInfo =
42 Ctx.getASTRecordLayout(RD).getPrimaryBaseInfo();
Anders Carlsson81430692009-09-22 03:02:06 +000043
44 // If the record has a primary base class that is virtual, add it to the set
45 // of primary bases.
Anders Carlssona30c0d32009-11-27 22:14:40 +000046 if (BaseInfo.isVirtual())
47 IndirectPrimaryBases.insert(BaseInfo.getBase());
Anders Carlsson81430692009-09-22 03:02:06 +000048
49 // Now traverse all bases and find primary bases for them.
Mike Stumpd8fe7b22009-08-05 22:37:18 +000050 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
51 e = RD->bases_end(); i != e; ++i) {
Sebastian Redl1054fae2009-10-25 17:03:50 +000052 assert(!i->getType()->isDependentType() &&
53 "Cannot layout class with dependent bases.");
Mike Stump11289f42009-09-09 15:08:12 +000054 const CXXRecordDecl *Base =
Mike Stump78696a72009-08-11 04:03:59 +000055 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson81430692009-09-22 03:02:06 +000056
Mike Stump78696a72009-08-11 04:03:59 +000057 // Only bases with virtual bases participate in computing the
58 // indirect primary virtual base classes.
Mike Stumpc2f591b2009-08-13 22:53:07 +000059 if (Base->getNumVBases())
Anders Carlsson81430692009-09-22 03:02:06 +000060 IdentifyPrimaryBases(Base);
Mike Stumpd8fe7b22009-08-05 22:37:18 +000061 }
62}
63
Anders Carlsson81430692009-09-22 03:02:06 +000064void
Anders Carlsson8630b5b2010-03-11 00:15:35 +000065ASTRecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD) {
66
Mike Stump6f3793b2009-08-12 21:50:08 +000067 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
68 e = RD->bases_end(); i != e; ++i) {
Sebastian Redl1054fae2009-10-25 17:03:50 +000069 assert(!i->getType()->isDependentType() &&
70 "Cannot layout class with dependent bases.");
Mike Stump11289f42009-09-09 15:08:12 +000071 const CXXRecordDecl *Base =
Mike Stump6f3793b2009-08-12 21:50:08 +000072 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
73 if (!i->isVirtual()) {
Anders Carlsson8630b5b2010-03-11 00:15:35 +000074 SelectPrimaryVBase(Base);
Anders Carlssona30c0d32009-11-27 22:14:40 +000075 if (PrimaryBase.getBase())
Mike Stump6f3793b2009-08-12 21:50:08 +000076 return;
77 continue;
78 }
79 if (IsNearlyEmpty(Base)) {
Anders Carlsson8630b5b2010-03-11 00:15:35 +000080 // Is this the first nearly empty primary virtual base?
81 if (!FirstNearlyEmptyVBase)
82 FirstNearlyEmptyVBase = Base;
83
Anders Carlsson81430692009-09-22 03:02:06 +000084 if (!IndirectPrimaryBases.count(Base)) {
Anders Carlsson8630b5b2010-03-11 00:15:35 +000085 PrimaryBase = ASTRecordLayout::PrimaryBaseInfo(Base,
86 /*IsVirtual=*/true);
Mike Stump6f3793b2009-08-12 21:50:08 +000087 return;
88 }
89 }
Anders Carlsson8630b5b2010-03-11 00:15:35 +000090
Zhongxing Xuec345b72010-02-15 04:28:35 +000091 assert(i->isVirtual());
Anders Carlsson8630b5b2010-03-11 00:15:35 +000092 SelectPrimaryVBase(Base);
Zhongxing Xuec345b72010-02-15 04:28:35 +000093 if (PrimaryBase.getBase())
94 return;
Mike Stump6f3793b2009-08-12 21:50:08 +000095 }
96}
97
Anders Carlsson8630b5b2010-03-11 00:15:35 +000098/// DeterminePrimaryBase - Determine the primary base of the given class.
99void ASTRecordLayoutBuilder::DeterminePrimaryBase(const CXXRecordDecl *RD) {
100 // If the class isn't dynamic, it won't have a primary base.
101 if (!RD->isDynamicClass())
102 return;
103
Anders Carlsson81430692009-09-22 03:02:06 +0000104 // Compute all the primary virtual bases for all of our direct and
Mike Stump590a7c72009-08-13 23:26:06 +0000105 // indirect bases, and record all their primary virtual base classes.
Mike Stump590a7c72009-08-13 23:26:06 +0000106 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
107 e = RD->bases_end(); i != e; ++i) {
Sebastian Redl1054fae2009-10-25 17:03:50 +0000108 assert(!i->getType()->isDependentType() &&
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000109 "Cannot lay out class with dependent bases.");
Mike Stump11289f42009-09-09 15:08:12 +0000110 const CXXRecordDecl *Base =
Mike Stump590a7c72009-08-13 23:26:06 +0000111 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson81430692009-09-22 03:02:06 +0000112 IdentifyPrimaryBases(Base);
Mike Stump590a7c72009-08-13 23:26:06 +0000113 }
114
Anders Carlsson81430692009-09-22 03:02:06 +0000115 // If the record has a dynamic base class, attempt to choose a primary base
116 // class. It is the first (in direct base class order) non-virtual dynamic
117 // base class, if one exists.
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000118 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
119 e = RD->bases_end(); i != e; ++i) {
Anders Carlsson03ff3792009-11-27 22:05:05 +0000120 // Ignore virtual bases.
121 if (i->isVirtual())
122 continue;
123
124 const CXXRecordDecl *Base =
125 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
126
127 if (Base->isDynamicClass()) {
128 // We found it.
129 PrimaryBase = ASTRecordLayout::PrimaryBaseInfo(Base, /*IsVirtual=*/false);
130 return;
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000131 }
132 }
133
134 // Otherwise, it is the first nearly empty virtual base that is not an
Mike Stump78696a72009-08-11 04:03:59 +0000135 // indirect primary virtual base class, if one exists.
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000136 if (RD->getNumVBases() != 0) {
137 SelectPrimaryVBase(RD);
138 if (PrimaryBase.getBase())
139 return;
140 }
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000141
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000142 // Otherwise, it is the first nearly empty virtual base that is not an
143 // indirect primary virtual base class, if one exists.
144 if (FirstNearlyEmptyVBase) {
145 PrimaryBase =
146 ASTRecordLayout::PrimaryBaseInfo(FirstNearlyEmptyVBase, /*IsVirtual=*/true);
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000147 return;
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000148 }
Anders Carlsson81430692009-09-22 03:02:06 +0000149
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000150 // Otherwise there is no primary base class.
151 assert(!PrimaryBase.getBase() && "Should not get here with a primary base!");
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000152
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000153 // Allocate the virtual table pointer at offset zero.
154 assert(DataSize == 0 && "Vtable pointer must be at offset zero!");
155
156 // Update the size.
157 Size += Ctx.Target.getPointerWidth(0);
158 DataSize = Size;
159
160 // Update the alignment.
161 UpdateAlignment(Ctx.Target.getPointerAlign(0));
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000162}
163
Mike Stump2b84dd32009-11-05 04:02:15 +0000164uint64_t ASTRecordLayoutBuilder::getBaseOffset(const CXXRecordDecl *Base) {
165 for (size_t i = 0; i < Bases.size(); ++i) {
166 if (Bases[i].first == Base)
167 return Bases[i].second;
168 }
169 for (size_t i = 0; i < VBases.size(); ++i) {
170 if (VBases[i].first == Base)
171 return VBases[i].second;
172 }
173 assert(0 && "missing base");
174 return 0;
175}
176
Anders Carlsson09ffa322010-03-10 22:21:28 +0000177void
178ASTRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) {
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000179 // First, determine the primary base class.
180 DeterminePrimaryBase(RD);
181
182 // If we have a primary base class, lay it out.
183 if (const CXXRecordDecl *Base = PrimaryBase.getBase()) {
184 printf("found primary base %s\n", Base->getQualifiedNameAsString().c_str());
185 if (PrimaryBase.isVirtual()) {
186 // We have a virtual primary base, insert it as an indirect primary base.
187 IndirectPrimaryBases.insert(Base);
188
189 LayoutVirtualBase(Base);
190 } else
191 LayoutNonVirtualBase(Base);
192 }
193
194 // Now lay out the non-virtual bases.
195 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
196 E = RD->bases_end(); I != E; ++I) {
197
198 // Ignore virtual bases.
199 if (I->isVirtual())
200 continue;
201
202 const CXXRecordDecl *Base =
203 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
204
205 // Skip the primary base.
206 if (Base == PrimaryBase.getBase() && !PrimaryBase.isVirtual())
207 continue;
208
209 // Lay out the base.
210 LayoutNonVirtualBase(Base);
Anders Carlsson09ffa322010-03-10 22:21:28 +0000211 }
212}
213
214void ASTRecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *RD) {
Anders Carlsson0d0b5882010-03-10 22:26:24 +0000215 // Layout the base.
216 uint64_t Offset = LayoutBase(RD);
217
218 // Add its base class offset.
219 Bases.push_back(std::make_pair(RD, Offset));
Anders Carlsson09ffa322010-03-10 22:21:28 +0000220}
Mike Stump2b84dd32009-11-05 04:02:15 +0000221
222void ASTRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *Class,
223 const CXXRecordDecl *RD,
Mike Stump996576f32009-08-16 19:04:13 +0000224 const CXXRecordDecl *PB,
Mike Stump2b84dd32009-11-05 04:02:15 +0000225 uint64_t Offset,
Mike Stump22ea1f82009-08-16 01:46:26 +0000226 llvm::SmallSet<const CXXRecordDecl*, 32> &mark,
Mike Stump13007542009-08-13 02:02:14 +0000227 llvm::SmallSet<const CXXRecordDecl*, 32> &IndirectPrimary) {
Mike Stumpc2f591b2009-08-13 22:53:07 +0000228 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
229 e = RD->bases_end(); i != e; ++i) {
Sebastian Redl1054fae2009-10-25 17:03:50 +0000230 assert(!i->getType()->isDependentType() &&
231 "Cannot layout class with dependent bases.");
Mike Stump11289f42009-09-09 15:08:12 +0000232 const CXXRecordDecl *Base =
Mike Stump6b2556f2009-08-06 13:41:24 +0000233 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stump2b84dd32009-11-05 04:02:15 +0000234 uint64_t BaseOffset = Offset;
Mike Stump22ea1f82009-08-16 01:46:26 +0000235 if (i->isVirtual()) {
Mike Stump996576f32009-08-16 19:04:13 +0000236 if (Base == PB) {
237 // Only lay things out once.
238 if (mark.count(Base))
239 continue;
240 // Mark it so we don't lay it out twice.
241 mark.insert(Base);
242 assert (IndirectPrimary.count(Base) && "IndirectPrimary was wrong");
Anders Carlssond6020c32009-09-22 00:04:45 +0000243 VBases.push_back(std::make_pair(Base, Offset));
Mike Stump996576f32009-08-16 19:04:13 +0000244 } else if (IndirectPrimary.count(Base)) {
245 // Someone else will eventually lay this out.
246 ;
247 } else {
248 // Only lay things out once.
249 if (mark.count(Base))
250 continue;
251 // Mark it so we don't lay it out twice.
252 mark.insert(Base);
Mike Stump22ea1f82009-08-16 01:46:26 +0000253 LayoutVirtualBase(Base);
Anders Carlssond6020c32009-09-22 00:04:45 +0000254 BaseOffset = VBases.back().second;
Mike Stump996576f32009-08-16 19:04:13 +0000255 }
Mike Stump2b84dd32009-11-05 04:02:15 +0000256 } else {
257 if (RD == Class)
258 BaseOffset = getBaseOffset(Base);
259 else {
Mike Stump4e16d052009-11-11 02:49:00 +0000260 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD);
Mike Stump2b84dd32009-11-05 04:02:15 +0000261 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
262 }
Mike Stumpc2f591b2009-08-13 22:53:07 +0000263 }
Mike Stump2b84dd32009-11-05 04:02:15 +0000264
Mike Stump996576f32009-08-16 19:04:13 +0000265 if (Base->getNumVBases()) {
Anders Carlsson03ff3792009-11-27 22:05:05 +0000266 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(Base);
Anders Carlssona30c0d32009-11-27 22:14:40 +0000267 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBaseInfo().getBase();
Anders Carlsson03ff3792009-11-27 22:05:05 +0000268 LayoutVirtualBases(Class, Base, PrimaryBase, BaseOffset, mark,
269 IndirectPrimary);
Mike Stump996576f32009-08-16 19:04:13 +0000270 }
Mike Stump6b2556f2009-08-06 13:41:24 +0000271 }
272}
273
Anders Carlsson09ffa322010-03-10 22:21:28 +0000274void ASTRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *RD) {
Anders Carlsson0d0b5882010-03-10 22:26:24 +0000275 // Layout the base.
276 uint64_t Offset = LayoutBase(RD);
277
278 // Add its base class offset.
279 VBases.push_back(std::make_pair(RD, Offset));
Anders Carlsson09ffa322010-03-10 22:21:28 +0000280}
281
282uint64_t ASTRecordLayoutBuilder::LayoutBase(const CXXRecordDecl *RD) {
283 const ASTRecordLayout &BaseInfo = Ctx.getASTRecordLayout(RD);
284
285 // If we have an empty base class, try to place it at offset 0.
286 if (RD->isEmpty() && canPlaceRecordAtOffset(RD, 0)) {
287 // We were able to place the class at offset 0.
288 UpdateEmptyClassOffsets(RD, 0);
289
290 Size = std::max(Size, BaseInfo.getSize());
291
292 return 0;
293 }
294
295 unsigned BaseAlign = BaseInfo.getNonVirtualAlign();
296
297 // Round up the current record size to the base's alignment boundary.
298 uint64_t Offset = llvm::RoundUpToAlignment(DataSize, BaseAlign);
299
300 // Try to place the base.
301 while (true) {
302 if (canPlaceRecordAtOffset(RD, Offset))
303 break;
304
305 Offset += BaseAlign;
306 }
307
308 if (!RD->isEmpty()) {
309 // Update the data size.
310 DataSize = Offset + BaseInfo.getNonVirtualSize();
311
312 Size = std::max(Size, DataSize);
313 } else
314 Size = std::max(Size, Offset + BaseInfo.getSize());
315
316 // Remember max struct/class alignment.
317 UpdateAlignment(BaseAlign);
318
319 UpdateEmptyClassOffsets(RD, Offset);
320 return Offset;
321}
322
Anders Carlsson6522b052009-09-24 03:13:30 +0000323bool ASTRecordLayoutBuilder::canPlaceRecordAtOffset(const CXXRecordDecl *RD,
324 uint64_t Offset) const {
Anders Carlssonf24b18f2009-09-24 03:22:10 +0000325 // Look for an empty class with the same type at the same offset.
326 for (EmptyClassOffsetsTy::const_iterator I =
327 EmptyClassOffsets.lower_bound(Offset),
328 E = EmptyClassOffsets.upper_bound(Offset); I != E; ++I) {
329
330 if (I->second == RD)
331 return false;
332 }
333
Anders Carlssonbb66bc82009-09-24 05:21:31 +0000334 const ASTRecordLayout &Info = Ctx.getASTRecordLayout(RD);
335
336 // Check bases.
337 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
338 E = RD->bases_end(); I != E; ++I) {
Sebastian Redl1054fae2009-10-25 17:03:50 +0000339 assert(!I->getType()->isDependentType() &&
340 "Cannot layout class with dependent bases.");
Anders Carlssonbb66bc82009-09-24 05:21:31 +0000341 if (I->isVirtual())
342 continue;
343
344 const CXXRecordDecl *Base =
345 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
346
347 uint64_t BaseClassOffset = Info.getBaseClassOffset(Base);
348
349 if (!canPlaceRecordAtOffset(Base, Offset + BaseClassOffset))
350 return false;
351 }
352
Anders Carlssond7d358a2009-09-25 15:39:00 +0000353 // Check fields.
354 unsigned FieldNo = 0;
355 for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
356 I != E; ++I, ++FieldNo) {
357 const FieldDecl *FD = *I;
358
359 uint64_t FieldOffset = Info.getFieldOffset(FieldNo);
360
361 if (!canPlaceFieldAtOffset(FD, Offset + FieldOffset))
362 return false;
363 }
364
Anders Carlssonbb66bc82009-09-24 05:21:31 +0000365 // FIXME: virtual bases.
Anders Carlsson6522b052009-09-24 03:13:30 +0000366 return true;
367}
368
Anders Carlsson6f95c702009-09-25 00:02:51 +0000369bool ASTRecordLayoutBuilder::canPlaceFieldAtOffset(const FieldDecl *FD,
370 uint64_t Offset) const {
Anders Carlsson4bf82142009-09-25 01:23:32 +0000371 QualType T = FD->getType();
372 if (const RecordType *RT = T->getAs<RecordType>()) {
Anders Carlsson6f95c702009-09-25 00:02:51 +0000373 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
374 return canPlaceRecordAtOffset(RD, Offset);
375 }
376
Anders Carlsson4bf82142009-09-25 01:23:32 +0000377 if (const ConstantArrayType *AT = Ctx.getAsConstantArrayType(T)) {
378 QualType ElemTy = Ctx.getBaseElementType(AT);
379 const RecordType *RT = ElemTy->getAs<RecordType>();
380 if (!RT)
381 return true;
382 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
383 if (!RD)
384 return true;
385
386 const ASTRecordLayout &Info = Ctx.getASTRecordLayout(RD);
387
388 uint64_t NumElements = Ctx.getConstantArrayElementCount(AT);
Mike Stump2b84dd32009-11-05 04:02:15 +0000389 uint64_t ElementOffset = Offset;
Anders Carlsson4bf82142009-09-25 01:23:32 +0000390 for (uint64_t I = 0; I != NumElements; ++I) {
391 if (!canPlaceRecordAtOffset(RD, ElementOffset))
392 return false;
393
394 ElementOffset += Info.getSize();
395 }
396 }
Anders Carlsson6f95c702009-09-25 00:02:51 +0000397
398 return true;
399}
400
Anders Carlsson6522b052009-09-24 03:13:30 +0000401void ASTRecordLayoutBuilder::UpdateEmptyClassOffsets(const CXXRecordDecl *RD,
402 uint64_t Offset) {
Anders Carlssonf24b18f2009-09-24 03:22:10 +0000403 if (RD->isEmpty())
404 EmptyClassOffsets.insert(std::make_pair(Offset, RD));
Anders Carlssonbb66bc82009-09-24 05:21:31 +0000405
406 const ASTRecordLayout &Info = Ctx.getASTRecordLayout(RD);
407
408 // Update bases.
409 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
410 E = RD->bases_end(); I != E; ++I) {
Sebastian Redl1054fae2009-10-25 17:03:50 +0000411 assert(!I->getType()->isDependentType() &&
412 "Cannot layout class with dependent bases.");
Anders Carlssonbb66bc82009-09-24 05:21:31 +0000413 if (I->isVirtual())
414 continue;
Anders Carlssonf24b18f2009-09-24 03:22:10 +0000415
Anders Carlssonbb66bc82009-09-24 05:21:31 +0000416 const CXXRecordDecl *Base =
417 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
418
419 uint64_t BaseClassOffset = Info.getBaseClassOffset(Base);
420 UpdateEmptyClassOffsets(Base, Offset + BaseClassOffset);
421 }
422
Anders Carlssond7d358a2009-09-25 15:39:00 +0000423 // Update fields.
424 unsigned FieldNo = 0;
425 for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
426 I != E; ++I, ++FieldNo) {
427 const FieldDecl *FD = *I;
428
429 uint64_t FieldOffset = Info.getFieldOffset(FieldNo);
430 UpdateEmptyClassOffsets(FD, Offset + FieldOffset);
431 }
432
433 // FIXME: Update virtual bases.
Anders Carlsson6522b052009-09-24 03:13:30 +0000434}
435
Anders Carlssone1883102009-09-25 01:54:38 +0000436void
437ASTRecordLayoutBuilder::UpdateEmptyClassOffsets(const FieldDecl *FD,
438 uint64_t Offset) {
439 QualType T = FD->getType();
440
441 if (const RecordType *RT = T->getAs<RecordType>()) {
442 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
443 UpdateEmptyClassOffsets(RD, Offset);
444 return;
445 }
446 }
447
448 if (const ConstantArrayType *AT = Ctx.getAsConstantArrayType(T)) {
449 QualType ElemTy = Ctx.getBaseElementType(AT);
450 const RecordType *RT = ElemTy->getAs<RecordType>();
451 if (!RT)
452 return;
453 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
454 if (!RD)
455 return;
456
457 const ASTRecordLayout &Info = Ctx.getASTRecordLayout(RD);
458
459 uint64_t NumElements = Ctx.getConstantArrayElementCount(AT);
Mike Stump2b84dd32009-11-05 04:02:15 +0000460 uint64_t ElementOffset = Offset;
Anders Carlssone1883102009-09-25 01:54:38 +0000461
462 for (uint64_t I = 0; I != NumElements; ++I) {
463 UpdateEmptyClassOffsets(RD, ElementOffset);
464 ElementOffset += Info.getSize();
465 }
466 }
467}
468
Anders Carlsson79474332009-07-18 20:20:21 +0000469void ASTRecordLayoutBuilder::Layout(const RecordDecl *D) {
470 IsUnion = D->isUnion();
Anders Carlsson68e0b682009-08-08 18:23:56 +0000471
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000472 Packed = D->hasAttr<PackedAttr>();
473
474 // The #pragma pack attribute specifies the maximum field alignment.
Anders Carlsson68e0b682009-08-08 18:23:56 +0000475 if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>())
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000476 MaxFieldAlignment = PPA->getAlignment();
Mike Stump11289f42009-09-09 15:08:12 +0000477
Anders Carlsson79474332009-07-18 20:20:21 +0000478 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Alexis Hunt96d5c762009-11-21 08:43:09 +0000479 UpdateAlignment(AA->getMaxAlignment());
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000480
Mike Stump22ea1f82009-08-16 01:46:26 +0000481 // If this is a C++ class, lay out the vtable and the non-virtual bases.
Mike Stump6b2556f2009-08-06 13:41:24 +0000482 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000483 if (RD)
Mike Stump3dc7eb92009-07-30 00:22:38 +0000484 LayoutNonVirtualBases(RD);
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000485
Anders Carlsson118ce162009-07-18 21:48:39 +0000486 LayoutFields(D);
Mike Stump11289f42009-09-09 15:08:12 +0000487
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000488 NonVirtualSize = Size;
489 NonVirtualAlignment = Alignment;
Mike Stump3dc7eb92009-07-30 00:22:38 +0000490
Mike Stump22ea1f82009-08-16 01:46:26 +0000491 if (RD) {
492 llvm::SmallSet<const CXXRecordDecl*, 32> mark;
Anders Carlssona30c0d32009-11-27 22:14:40 +0000493 LayoutVirtualBases(RD, RD, PrimaryBase.getBase(),
494 0, mark, IndirectPrimaryBases);
Mike Stump22ea1f82009-08-16 01:46:26 +0000495 }
Mike Stump6b2556f2009-08-06 13:41:24 +0000496
Anders Carlsson79474332009-07-18 20:20:21 +0000497 // Finally, round the size of the total struct up to the alignment of the
498 // struct itself.
499 FinishLayout();
500}
501
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000502// FIXME. Impl is no longer needed.
Anders Carlsson4f516282009-07-18 20:50:59 +0000503void ASTRecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D,
504 const ObjCImplementationDecl *Impl) {
505 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
506 const ASTRecordLayout &SL = Ctx.getASTObjCInterfaceLayout(SD);
507
508 UpdateAlignment(SL.getAlignment());
Mike Stump11289f42009-09-09 15:08:12 +0000509
Anders Carlsson4f516282009-07-18 20:50:59 +0000510 // We start laying out ivars not at the end of the superclass
511 // structure, but at the next byte following the last field.
Anders Carlsson27b50132009-07-18 21:26:44 +0000512 Size = llvm::RoundUpToAlignment(SL.getDataSize(), 8);
Anders Carlsson47680d82009-09-26 01:34:51 +0000513 DataSize = Size;
Anders Carlsson4f516282009-07-18 20:50:59 +0000514 }
Mike Stump11289f42009-09-09 15:08:12 +0000515
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000516 Packed = D->hasAttr<PackedAttr>();
Mike Stump11289f42009-09-09 15:08:12 +0000517
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000518 // The #pragma pack attribute specifies the maximum field alignment.
519 if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>())
520 MaxFieldAlignment = PPA->getAlignment();
Mike Stump11289f42009-09-09 15:08:12 +0000521
Anders Carlsson4f516282009-07-18 20:50:59 +0000522 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Alexis Hunt96d5c762009-11-21 08:43:09 +0000523 UpdateAlignment(AA->getMaxAlignment());
Anders Carlsson4f516282009-07-18 20:50:59 +0000524 // Layout each ivar sequentially.
525 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000526 Ctx.ShallowCollectObjCIvars(D, Ivars);
Anders Carlsson4f516282009-07-18 20:50:59 +0000527 for (unsigned i = 0, e = Ivars.size(); i != e; ++i)
528 LayoutField(Ivars[i]);
Mike Stump11289f42009-09-09 15:08:12 +0000529
Anders Carlsson4f516282009-07-18 20:50:59 +0000530 // Finally, round the size of the total struct up to the alignment of the
531 // struct itself.
532 FinishLayout();
533}
534
Anders Carlsson118ce162009-07-18 21:48:39 +0000535void ASTRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
536 // Layout each field, for now, just sequentially, respecting alignment. In
537 // the future, this will need to be tweakable by targets.
Mike Stump11289f42009-09-09 15:08:12 +0000538 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson118ce162009-07-18 21:48:39 +0000539 FieldEnd = D->field_end(); Field != FieldEnd; ++Field)
540 LayoutField(*Field);
541}
542
Anders Carlsson07209442009-11-22 17:37:31 +0000543void ASTRecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
544 bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
Anders Carlssonba958402009-11-22 19:13:51 +0000545 uint64_t FieldOffset = IsUnion ? 0 : (DataSize - UnfilledBitsInLastByte);
Anders Carlsson07209442009-11-22 17:37:31 +0000546 uint64_t FieldSize = D->getBitWidth()->EvaluateAsInt(Ctx).getZExtValue();
547
548 std::pair<uint64_t, unsigned> FieldInfo = Ctx.getTypeInfo(D->getType());
549 uint64_t TypeSize = FieldInfo.first;
550 unsigned FieldAlign = FieldInfo.second;
551
552 if (FieldPacked)
553 FieldAlign = 1;
554 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
555 FieldAlign = std::max(FieldAlign, AA->getMaxAlignment());
556
557 // The maximum field alignment overrides the aligned attribute.
558 if (MaxFieldAlignment)
559 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
560
561 // Check if we need to add padding to give the field the correct
562 // alignment.
563 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
564 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
565
566 // Padding members don't affect overall alignment
567 if (!D->getIdentifier())
568 FieldAlign = 1;
569
570 // Place this field at the current location.
571 FieldOffsets.push_back(FieldOffset);
572
Anders Carlssonba958402009-11-22 19:13:51 +0000573 // Update DataSize to include the last byte containing (part of) the bitfield.
574 if (IsUnion) {
575 // FIXME: I think FieldSize should be TypeSize here.
576 DataSize = std::max(DataSize, FieldSize);
577 } else {
578 uint64_t NewSizeInBits = FieldOffset + FieldSize;
579
580 DataSize = llvm::RoundUpToAlignment(NewSizeInBits, 8);
581 UnfilledBitsInLastByte = DataSize - NewSizeInBits;
582 }
Anders Carlsson07209442009-11-22 17:37:31 +0000583
Anders Carlssonba958402009-11-22 19:13:51 +0000584 // Update the size.
585 Size = std::max(Size, DataSize);
Anders Carlsson07209442009-11-22 17:37:31 +0000586
587 // Remember max struct/class alignment.
588 UpdateAlignment(FieldAlign);
589}
590
Anders Carlsson79474332009-07-18 20:20:21 +0000591void ASTRecordLayoutBuilder::LayoutField(const FieldDecl *D) {
Anders Carlsson07209442009-11-22 17:37:31 +0000592 if (D->isBitField()) {
593 LayoutBitField(D);
594 return;
595 }
596
Anders Carlssonba958402009-11-22 19:13:51 +0000597 // Reset the unfilled bits.
598 UnfilledBitsInLastByte = 0;
599
Anders Carlsson07209442009-11-22 17:37:31 +0000600 bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
Anders Carlsson47680d82009-09-26 01:34:51 +0000601 uint64_t FieldOffset = IsUnion ? 0 : DataSize;
Anders Carlsson79474332009-07-18 20:20:21 +0000602 uint64_t FieldSize;
603 unsigned FieldAlign;
Anders Carlsson07209442009-11-22 17:37:31 +0000604
605 if (D->getType()->isIncompleteArrayType()) {
606 // This is a flexible array member; we can't directly
607 // query getTypeInfo about these, so we figure it out here.
608 // Flexible array members don't have any size, but they
609 // have to be aligned appropriately for their element type.
610 FieldSize = 0;
611 const ArrayType* ATy = Ctx.getAsArrayType(D->getType());
612 FieldAlign = Ctx.getTypeAlign(ATy->getElementType());
613 } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) {
614 unsigned AS = RT->getPointeeType().getAddressSpace();
615 FieldSize = Ctx.Target.getPointerWidth(AS);
616 FieldAlign = Ctx.Target.getPointerAlign(AS);
Anders Carlsson79474332009-07-18 20:20:21 +0000617 } else {
Anders Carlsson07209442009-11-22 17:37:31 +0000618 std::pair<uint64_t, unsigned> FieldInfo = Ctx.getTypeInfo(D->getType());
619 FieldSize = FieldInfo.first;
620 FieldAlign = FieldInfo.second;
Anders Carlsson79474332009-07-18 20:20:21 +0000621 }
Mike Stump11289f42009-09-09 15:08:12 +0000622
Anders Carlsson07209442009-11-22 17:37:31 +0000623 if (FieldPacked)
624 FieldAlign = 8;
625 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
626 FieldAlign = std::max(FieldAlign, AA->getMaxAlignment());
627
628 // The maximum field alignment overrides the aligned attribute.
629 if (MaxFieldAlignment)
630 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
631
632 // Round up the current record size to the field's alignment boundary.
633 FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign);
634
635 if (!IsUnion) {
636 while (true) {
637 // Check if we can place the field at this offset.
638 if (canPlaceFieldAtOffset(D, FieldOffset))
639 break;
640
641 // We couldn't place the field at the offset. Try again at a new offset.
642 FieldOffset += FieldAlign;
643 }
644
645 UpdateEmptyClassOffsets(D, FieldOffset);
646 }
647
Anders Carlsson79474332009-07-18 20:20:21 +0000648 // Place this field at the current location.
649 FieldOffsets.push_back(FieldOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000650
Anders Carlsson79474332009-07-18 20:20:21 +0000651 // Reserve space for this field.
652 if (IsUnion)
653 Size = std::max(Size, FieldSize);
654 else
655 Size = FieldOffset + FieldSize;
Mike Stump11289f42009-09-09 15:08:12 +0000656
Anders Carlsson47680d82009-09-26 01:34:51 +0000657 // Update the data size.
658 DataSize = Size;
Mike Stump11289f42009-09-09 15:08:12 +0000659
Anders Carlsson79474332009-07-18 20:20:21 +0000660 // Remember max struct/class alignment.
661 UpdateAlignment(FieldAlign);
662}
663
664void ASTRecordLayoutBuilder::FinishLayout() {
665 // In C++, records cannot be of size 0.
666 if (Ctx.getLangOptions().CPlusPlus && Size == 0)
667 Size = 8;
668 // Finally, round the size of the record up to the alignment of the
669 // record itself.
Anders Carlsson07209442009-11-22 17:37:31 +0000670 Size = llvm::RoundUpToAlignment(Size, Alignment);
Anders Carlsson79474332009-07-18 20:20:21 +0000671}
672
673void ASTRecordLayoutBuilder::UpdateAlignment(unsigned NewAlignment) {
674 if (NewAlignment <= Alignment)
675 return;
Mike Stump11289f42009-09-09 15:08:12 +0000676
Anders Carlsson79474332009-07-18 20:20:21 +0000677 assert(llvm::isPowerOf2_32(NewAlignment && "Alignment not a power of 2"));
Mike Stump11289f42009-09-09 15:08:12 +0000678
Anders Carlsson79474332009-07-18 20:20:21 +0000679 Alignment = NewAlignment;
680}
Mike Stump11289f42009-09-09 15:08:12 +0000681
Anders Carlsson5ebf8b42009-12-07 04:35:11 +0000682const ASTRecordLayout *
683ASTRecordLayoutBuilder::ComputeLayout(ASTContext &Ctx,
684 const RecordDecl *D) {
685 ASTRecordLayoutBuilder Builder(Ctx);
686
687 Builder.Layout(D);
688
689 if (!isa<CXXRecordDecl>(D))
Ted Kremenekc3015a92010-03-08 20:56:29 +0000690 return new (Ctx) ASTRecordLayout(Ctx, Builder.Size, Builder.Alignment,
691 Builder.Size,
692 Builder.FieldOffsets.data(),
693 Builder.FieldOffsets.size());
Anders Carlsson5ebf8b42009-12-07 04:35:11 +0000694
695 // FIXME: This is not always correct. See the part about bitfields at
696 // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info.
697 // FIXME: IsPODForThePurposeOfLayout should be stored in the record layout.
698 bool IsPODForThePurposeOfLayout = cast<CXXRecordDecl>(D)->isPOD();
699
700 // FIXME: This should be done in FinalizeLayout.
701 uint64_t DataSize =
702 IsPODForThePurposeOfLayout ? Builder.Size : Builder.DataSize;
703 uint64_t NonVirtualSize =
704 IsPODForThePurposeOfLayout ? DataSize : Builder.NonVirtualSize;
705
Ted Kremenekc3015a92010-03-08 20:56:29 +0000706 return new (Ctx) ASTRecordLayout(Ctx, Builder.Size, Builder.Alignment,
707 DataSize, Builder.FieldOffsets.data(),
708 Builder.FieldOffsets.size(),
709 NonVirtualSize,
710 Builder.NonVirtualAlignment,
711 Builder.PrimaryBase,
712 Builder.Bases.data(),
713 Builder.Bases.size(),
714 Builder.VBases.data(),
715 Builder.VBases.size());
Anders Carlsson5ebf8b42009-12-07 04:35:11 +0000716}
717
718const ASTRecordLayout *
719ASTRecordLayoutBuilder::ComputeLayout(ASTContext &Ctx,
720 const ObjCInterfaceDecl *D,
721 const ObjCImplementationDecl *Impl) {
722 ASTRecordLayoutBuilder Builder(Ctx);
723
724 Builder.Layout(D, Impl);
725
Ted Kremenekc3015a92010-03-08 20:56:29 +0000726 return new (Ctx) ASTRecordLayout(Ctx, Builder.Size, Builder.Alignment,
727 Builder.DataSize,
728 Builder.FieldOffsets.data(),
729 Builder.FieldOffsets.size());
Anders Carlsson5ebf8b42009-12-07 04:35:11 +0000730}
731
732const CXXMethodDecl *
733ASTRecordLayoutBuilder::ComputeKeyFunction(const CXXRecordDecl *RD) {
734 assert(RD->isDynamicClass() && "Class does not have any virtual methods!");
735
736 // If a class isnt' polymorphic it doesn't have a key function.
737 if (!RD->isPolymorphic())
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +0000738 return 0;
Eli Friedmanf2c79b62009-12-08 03:56:49 +0000739
740 // A class inside an anonymous namespace doesn't have a key function. (Or
741 // at least, there's no point to assigning a key function to such a class;
742 // this doesn't affect the ABI.)
743 if (RD->isInAnonymousNamespace())
744 return 0;
745
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +0000746 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
747 E = RD->method_end(); I != E; ++I) {
748 const CXXMethodDecl *MD = *I;
749
750 if (!MD->isVirtual())
751 continue;
752
753 if (MD->isPure())
754 continue;
Eli Friedmanf2c79b62009-12-08 03:56:49 +0000755
Anders Carlssonf98849e2009-12-02 17:15:43 +0000756 // Ignore implicit member functions, they are always marked as inline, but
757 // they don't have a body until they're defined.
758 if (MD->isImplicit())
759 continue;
Douglas Gregora318efd2010-01-05 19:06:31 +0000760
761 if (MD->isInlineSpecified())
762 continue;
Eli Friedman71a26d82009-12-06 20:50:05 +0000763
764 if (MD->hasInlineBody())
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +0000765 continue;
766
767 // We found it.
768 return MD;
769 }
770
771 return 0;
772}
773