blob: 78598b795626f395cc76f237b6d8122054689ab3 [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
Mike Stump11289f42009-09-09 15:08:12 +000024ASTRecordLayoutBuilder::ASTRecordLayoutBuilder(ASTContext &Ctx)
25 : Ctx(Ctx), Size(0), Alignment(8), Packed(false), MaxFieldAlignment(0),
Anders Carlsson96cff1f2009-09-22 18:21:58 +000026 NextOffset(0), IsUnion(false), NonVirtualSize(0), NonVirtualAlignment(8),
27 PrimaryBase(0), PrimaryBaseWasVirtual(false) {}
Anders Carlsson79474332009-07-18 20:20:21 +000028
Mike Stump25094802009-08-06 00:38:46 +000029/// LayoutVtable - Lay out the vtable and set PrimaryBase.
Anders Carlsson81430692009-09-22 03:02:06 +000030void ASTRecordLayoutBuilder::LayoutVtable(const CXXRecordDecl *RD) {
Mike Stumpaa08da72009-08-12 22:06:55 +000031 if (!RD->isDynamicClass()) {
Mike Stump25094802009-08-06 00:38:46 +000032 // There is no primary base in this case.
Mike Stumpd8fe7b22009-08-05 22:37:18 +000033 return;
Mike Stump25094802009-08-06 00:38:46 +000034 }
Mike Stumpd8fe7b22009-08-05 22:37:18 +000035
Anders Carlsson81430692009-09-22 03:02:06 +000036 SelectPrimaryBase(RD);
Mike Stumpd8fe7b22009-08-05 22:37:18 +000037 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
Mike Stump11289f42009-09-09 15:08:12 +000045void
Anders Carlsson6d9f6f32009-07-19 00:18:47 +000046ASTRecordLayoutBuilder::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()) {
Mike Stump11289f42009-09-09 15:08:12 +000050 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.
Mike Stumpbcf756c2009-08-14 01:44:03 +000053 if (Base != PrimaryBase || PrimaryBaseWasVirtual)
54 LayoutBaseNonVirtually(Base, false);
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.
Anders Carlsson81430692009-09-22 03:02:06 +000064bool ASTRecordLayoutBuilder::IsNearlyEmpty(const CXXRecordDecl *RD) const {
Mike Stumpd8fe7b22009-08-05 22:37:18 +000065 // 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
Anders Carlsson81430692009-09-22 03:02:06 +000074void ASTRecordLayoutBuilder::IdentifyPrimaryBases(const CXXRecordDecl *RD) {
Mike Stump78696a72009-08-11 04:03:59 +000075 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD);
Anders Carlsson81430692009-09-22 03:02:06 +000076
77 // If the record has a primary base class that is virtual, add it to the set
78 // of primary bases.
79 if (Layout.getPrimaryBaseWasVirtual())
80 IndirectPrimaryBases.insert(Layout.getPrimaryBase());
81
82 // Now traverse all bases and find primary bases for them.
Mike Stumpd8fe7b22009-08-05 22:37:18 +000083 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
84 e = RD->bases_end(); i != e; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +000085 const CXXRecordDecl *Base =
Mike Stump78696a72009-08-11 04:03:59 +000086 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson81430692009-09-22 03:02:06 +000087
Mike Stump78696a72009-08-11 04:03:59 +000088 // Only bases with virtual bases participate in computing the
89 // indirect primary virtual base classes.
Mike Stumpc2f591b2009-08-13 22:53:07 +000090 if (Base->getNumVBases())
Anders Carlsson81430692009-09-22 03:02:06 +000091 IdentifyPrimaryBases(Base);
Mike Stumpd8fe7b22009-08-05 22:37:18 +000092 }
93}
94
Anders Carlsson81430692009-09-22 03:02:06 +000095void
96ASTRecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD,
97 const CXXRecordDecl *&FirstPrimary) {
Mike Stump6f3793b2009-08-12 21:50:08 +000098 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
99 e = RD->bases_end(); i != e; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +0000100 const CXXRecordDecl *Base =
Mike Stump6f3793b2009-08-12 21:50:08 +0000101 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
102 if (!i->isVirtual()) {
Anders Carlsson81430692009-09-22 03:02:06 +0000103 SelectPrimaryVBase(Base, FirstPrimary);
Mike Stump6f3793b2009-08-12 21:50:08 +0000104 if (PrimaryBase)
105 return;
106 continue;
107 }
108 if (IsNearlyEmpty(Base)) {
109 if (FirstPrimary==0)
110 FirstPrimary = Base;
Anders Carlsson81430692009-09-22 03:02:06 +0000111 if (!IndirectPrimaryBases.count(Base)) {
Mike Stump6f3793b2009-08-12 21:50:08 +0000112 setPrimaryBase(Base, true);
113 return;
114 }
115 }
116 }
117}
118
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000119/// SelectPrimaryBase - Selects the primary base for the given class and
Mike Stump590a7c72009-08-13 23:26:06 +0000120/// record that with setPrimaryBase. We also calculate the IndirectPrimaries.
Anders Carlsson81430692009-09-22 03:02:06 +0000121void ASTRecordLayoutBuilder::SelectPrimaryBase(const CXXRecordDecl *RD) {
122 // Compute all the primary virtual bases for all of our direct and
Mike Stump590a7c72009-08-13 23:26:06 +0000123 // indirect bases, and record all their primary virtual base classes.
Mike Stump590a7c72009-08-13 23:26:06 +0000124 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
125 e = RD->bases_end(); i != e; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +0000126 const CXXRecordDecl *Base =
Mike Stump590a7c72009-08-13 23:26:06 +0000127 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson81430692009-09-22 03:02:06 +0000128 IdentifyPrimaryBases(Base);
Mike Stump590a7c72009-08-13 23:26:06 +0000129 }
130
Anders Carlsson81430692009-09-22 03:02:06 +0000131 // If the record has a dynamic base class, attempt to choose a primary base
132 // class. It is the first (in direct base class order) non-virtual dynamic
133 // base class, if one exists.
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000134 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
135 e = RD->bases_end(); i != e; ++i) {
136 if (!i->isVirtual()) {
Mike Stump11289f42009-09-09 15:08:12 +0000137 const CXXRecordDecl *Base =
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000138 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
139 if (Base->isDynamicClass()) {
Anders Carlsson81430692009-09-22 03:02:06 +0000140 // We found it.
Mike Stumpc266c6d2009-08-07 19:00:50 +0000141 setPrimaryBase(Base, false);
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000142 return;
143 }
144 }
145 }
146
147 // Otherwise, it is the first nearly empty virtual base that is not an
Mike Stump78696a72009-08-11 04:03:59 +0000148 // indirect primary virtual base class, if one exists.
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000149
150 // If we have no virtual bases at this point, bail out as the searching below
151 // is expensive.
Anders Carlsson96cff1f2009-09-22 18:21:58 +0000152 if (RD->getNumVBases() == 0)
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000153 return;
Anders Carlsson81430692009-09-22 03:02:06 +0000154
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000155 // Then we can search for the first nearly empty virtual base itself.
Anders Carlsson81430692009-09-22 03:02:06 +0000156 const CXXRecordDecl *FirstPrimary = 0;
157 SelectPrimaryVBase(RD, FirstPrimary);
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000158
Mike Stumpc266c6d2009-08-07 19:00:50 +0000159 // Otherwise if is the first nearly empty virtual base, if one exists,
160 // otherwise there is no primary base class.
Anders Carlsson55640542009-09-22 19:16:59 +0000161 if (!PrimaryBase)
162 setPrimaryBase(FirstPrimary, true);
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000163}
164
Mike Stump6b2556f2009-08-06 13:41:24 +0000165void ASTRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *RD) {
Mike Stumpbcf756c2009-08-14 01:44:03 +0000166 LayoutBaseNonVirtually(RD, true);
Mike Stump6b2556f2009-08-06 13:41:24 +0000167}
168
Mike Stump13007542009-08-13 02:02:14 +0000169void ASTRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
Mike Stump996576f32009-08-16 19:04:13 +0000170 const CXXRecordDecl *PB,
Mike Stump22ea1f82009-08-16 01:46:26 +0000171 int64_t Offset,
172 llvm::SmallSet<const CXXRecordDecl*, 32> &mark,
Mike Stump13007542009-08-13 02:02:14 +0000173 llvm::SmallSet<const CXXRecordDecl*, 32> &IndirectPrimary) {
Mike Stumpc2f591b2009-08-13 22:53:07 +0000174 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
175 e = RD->bases_end(); i != e; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +0000176 const CXXRecordDecl *Base =
Mike Stump6b2556f2009-08-06 13:41:24 +0000177 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stump22ea1f82009-08-16 01:46:26 +0000178#if 0
179 const ASTRecordLayout &L = Ctx.getASTRecordLayout(Base);
180 const CXXRecordDecl *PB = L.getPrimaryBase();
181 if (PB && L.getPrimaryBaseWasVirtual()
182 && IndirectPrimary.count(PB)) {
183 int64_t BaseOffset;
184 // FIXME: calculate this.
185 BaseOffset = (1<<63) | (1<<31);
186 VBases.push_back(PB);
187 VBaseOffsets.push_back(BaseOffset);
188 }
189#endif
Mike Stump996576f32009-08-16 19:04:13 +0000190 int64_t BaseOffset = Offset;;
191 // FIXME: Calculate BaseOffset.
Mike Stump22ea1f82009-08-16 01:46:26 +0000192 if (i->isVirtual()) {
Mike Stump996576f32009-08-16 19:04:13 +0000193 if (Base == PB) {
194 // Only lay things out once.
195 if (mark.count(Base))
196 continue;
197 // Mark it so we don't lay it out twice.
198 mark.insert(Base);
199 assert (IndirectPrimary.count(Base) && "IndirectPrimary was wrong");
Anders Carlssond6020c32009-09-22 00:04:45 +0000200 VBases.push_back(std::make_pair(Base, Offset));
Mike Stump996576f32009-08-16 19:04:13 +0000201 } else if (IndirectPrimary.count(Base)) {
202 // Someone else will eventually lay this out.
203 ;
204 } else {
205 // Only lay things out once.
206 if (mark.count(Base))
207 continue;
208 // Mark it so we don't lay it out twice.
209 mark.insert(Base);
Mike Stump22ea1f82009-08-16 01:46:26 +0000210 LayoutVirtualBase(Base);
Anders Carlssond6020c32009-09-22 00:04:45 +0000211 BaseOffset = VBases.back().second;
Mike Stump996576f32009-08-16 19:04:13 +0000212 }
Mike Stumpc2f591b2009-08-13 22:53:07 +0000213 }
Mike Stump996576f32009-08-16 19:04:13 +0000214 if (Base->getNumVBases()) {
215 const ASTRecordLayout &L = Ctx.getASTRecordLayout(Base);
216 const CXXRecordDecl *PB = L.getPrimaryBase();
217 LayoutVirtualBases(Base, PB, BaseOffset, mark, IndirectPrimary);
218 }
Mike Stump6b2556f2009-08-06 13:41:24 +0000219 }
220}
221
Anders Carlsson6522b052009-09-24 03:13:30 +0000222bool ASTRecordLayoutBuilder::canPlaceRecordAtOffset(const CXXRecordDecl *RD,
223 uint64_t Offset) const {
224 // FIXME: Implement.
225 return true;
226}
227
228void ASTRecordLayoutBuilder::UpdateEmptyClassOffsets(const CXXRecordDecl *RD,
229 uint64_t Offset) {
230 // FIXME: Implement.
231}
232
233uint64_t ASTRecordLayoutBuilder::LayoutBase(const CXXRecordDecl *RD) {
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000234 const ASTRecordLayout &BaseInfo = Ctx.getASTRecordLayout(RD);
Anders Carlsson93b6d5e2009-09-17 04:42:56 +0000235 if (!Bases.empty()) {
Mike Stump11289f42009-09-09 15:08:12 +0000236 assert(BaseInfo.getDataSize() > 0 &&
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000237 "FIXME: Handle empty classes.");
Anders Carlsson93b6d5e2009-09-17 04:42:56 +0000238 }
239
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000240 unsigned BaseAlign = BaseInfo.getNonVirtualAlign();
241 uint64_t BaseSize = BaseInfo.getNonVirtualSize();
Anders Carlsson6522b052009-09-24 03:13:30 +0000242
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000243 // Round up the current record size to the base's alignment boundary.
Anders Carlsson6522b052009-09-24 03:13:30 +0000244 uint64_t Offset = llvm::RoundUpToAlignment(Size, BaseAlign);
245
246 // Reserve space for this base.
247 Size = Offset + BaseSize;
248
249 // Remember the next available offset.
250 NextOffset = Size;
251
252 // Remember max struct/class alignment.
253 UpdateAlignment(BaseAlign);
254
255 return Offset;
256}
257
258void ASTRecordLayoutBuilder::LayoutBaseNonVirtually(const CXXRecordDecl *RD,
259 bool IsVirtualBase) {
260 // Layout the base.
261 unsigned Offset = LayoutBase(RD);
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000262
Mike Stump22ea1f82009-08-16 01:46:26 +0000263 // Add base class offsets.
Anders Carlssond6020c32009-09-22 00:04:45 +0000264 if (IsVirtualBase)
Anders Carlsson6522b052009-09-24 03:13:30 +0000265 VBases.push_back(std::make_pair(RD, Offset));
Anders Carlssond6020c32009-09-22 00:04:45 +0000266 else
Anders Carlsson6522b052009-09-24 03:13:30 +0000267 Bases.push_back(std::make_pair(RD, Offset));
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000268
Mike Stump996576f32009-08-16 19:04:13 +0000269#if 0
Mike Stump22ea1f82009-08-16 01:46:26 +0000270 // And now add offsets for all our primary virtual bases as well, so
271 // they all have offsets.
272 const ASTRecordLayout *L = &BaseInfo;
273 const CXXRecordDecl *PB = L->getPrimaryBase();
274 while (PB) {
275 if (L->getPrimaryBaseWasVirtual()) {
276 VBases.push_back(PB);
277 VBaseOffsets.push_back(Size);
278 }
279 PB = L->getPrimaryBase();
280 if (PB)
281 L = &Ctx.getASTRecordLayout(PB);
282 }
Mike Stump996576f32009-08-16 19:04:13 +0000283#endif
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000284}
285
Anders Carlsson79474332009-07-18 20:20:21 +0000286void ASTRecordLayoutBuilder::Layout(const RecordDecl *D) {
287 IsUnion = D->isUnion();
Anders Carlsson68e0b682009-08-08 18:23:56 +0000288
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000289 Packed = D->hasAttr<PackedAttr>();
290
291 // The #pragma pack attribute specifies the maximum field alignment.
Anders Carlsson68e0b682009-08-08 18:23:56 +0000292 if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>())
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000293 MaxFieldAlignment = PPA->getAlignment();
Mike Stump11289f42009-09-09 15:08:12 +0000294
Anders Carlsson79474332009-07-18 20:20:21 +0000295 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
296 UpdateAlignment(AA->getAlignment());
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000297
Mike Stump22ea1f82009-08-16 01:46:26 +0000298 // If this is a C++ class, lay out the vtable and the non-virtual bases.
Mike Stump6b2556f2009-08-06 13:41:24 +0000299 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
300 if (RD) {
Anders Carlsson81430692009-09-22 03:02:06 +0000301 LayoutVtable(RD);
Mike Stump25094802009-08-06 00:38:46 +0000302 // PrimaryBase goes first.
Mike Stump13007542009-08-13 02:02:14 +0000303 if (PrimaryBase) {
Mike Stump13007542009-08-13 02:02:14 +0000304 if (PrimaryBaseWasVirtual)
Anders Carlsson81430692009-09-22 03:02:06 +0000305 IndirectPrimaryBases.insert(PrimaryBase);
Mike Stumpbcf756c2009-08-14 01:44:03 +0000306 LayoutBaseNonVirtually(PrimaryBase, PrimaryBaseWasVirtual);
Mike Stump13007542009-08-13 02:02:14 +0000307 }
Mike Stump3dc7eb92009-07-30 00:22:38 +0000308 LayoutNonVirtualBases(RD);
Mike Stump3dc7eb92009-07-30 00:22:38 +0000309 }
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000310
Anders Carlsson118ce162009-07-18 21:48:39 +0000311 LayoutFields(D);
Mike Stump11289f42009-09-09 15:08:12 +0000312
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000313 NonVirtualSize = Size;
314 NonVirtualAlignment = Alignment;
Mike Stump3dc7eb92009-07-30 00:22:38 +0000315
Mike Stump22ea1f82009-08-16 01:46:26 +0000316 if (RD) {
317 llvm::SmallSet<const CXXRecordDecl*, 32> mark;
Anders Carlsson81430692009-09-22 03:02:06 +0000318 LayoutVirtualBases(RD, PrimaryBase, 0, mark, IndirectPrimaryBases);
Mike Stump22ea1f82009-08-16 01:46:26 +0000319 }
Mike Stump6b2556f2009-08-06 13:41:24 +0000320
Anders Carlsson79474332009-07-18 20:20:21 +0000321 // Finally, round the size of the total struct up to the alignment of the
322 // struct itself.
323 FinishLayout();
324}
325
Anders Carlsson4f516282009-07-18 20:50:59 +0000326void ASTRecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D,
327 const ObjCImplementationDecl *Impl) {
328 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
329 const ASTRecordLayout &SL = Ctx.getASTObjCInterfaceLayout(SD);
330
331 UpdateAlignment(SL.getAlignment());
Mike Stump11289f42009-09-09 15:08:12 +0000332
Anders Carlsson4f516282009-07-18 20:50:59 +0000333 // We start laying out ivars not at the end of the superclass
334 // structure, but at the next byte following the last field.
Anders Carlsson27b50132009-07-18 21:26:44 +0000335 Size = llvm::RoundUpToAlignment(SL.getDataSize(), 8);
Anders Carlsson4f516282009-07-18 20:50:59 +0000336 NextOffset = Size;
337 }
Mike Stump11289f42009-09-09 15:08:12 +0000338
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000339 Packed = D->hasAttr<PackedAttr>();
Mike Stump11289f42009-09-09 15:08:12 +0000340
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000341 // The #pragma pack attribute specifies the maximum field alignment.
342 if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>())
343 MaxFieldAlignment = PPA->getAlignment();
Mike Stump11289f42009-09-09 15:08:12 +0000344
Anders Carlsson4f516282009-07-18 20:50:59 +0000345 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
346 UpdateAlignment(AA->getAlignment());
Mike Stump11289f42009-09-09 15:08:12 +0000347
Anders Carlsson4f516282009-07-18 20:50:59 +0000348 // Layout each ivar sequentially.
349 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
350 Ctx.ShallowCollectObjCIvars(D, Ivars, Impl);
351 for (unsigned i = 0, e = Ivars.size(); i != e; ++i)
352 LayoutField(Ivars[i]);
Mike Stump11289f42009-09-09 15:08:12 +0000353
Anders Carlsson4f516282009-07-18 20:50:59 +0000354 // Finally, round the size of the total struct up to the alignment of the
355 // struct itself.
356 FinishLayout();
357}
358
Anders Carlsson118ce162009-07-18 21:48:39 +0000359void ASTRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
360 // Layout each field, for now, just sequentially, respecting alignment. In
361 // the future, this will need to be tweakable by targets.
Mike Stump11289f42009-09-09 15:08:12 +0000362 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson118ce162009-07-18 21:48:39 +0000363 FieldEnd = D->field_end(); Field != FieldEnd; ++Field)
364 LayoutField(*Field);
365}
366
Anders Carlsson79474332009-07-18 20:20:21 +0000367void ASTRecordLayoutBuilder::LayoutField(const FieldDecl *D) {
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000368 bool FieldPacked = Packed;
Anders Carlsson79474332009-07-18 20:20:21 +0000369 uint64_t FieldOffset = IsUnion ? 0 : Size;
370 uint64_t FieldSize;
371 unsigned FieldAlign;
Mike Stump11289f42009-09-09 15:08:12 +0000372
373 FieldPacked |= D->hasAttr<PackedAttr>();
374
Anders Carlsson79474332009-07-18 20:20:21 +0000375 if (const Expr *BitWidthExpr = D->getBitWidth()) {
376 // TODO: Need to check this algorithm on other targets!
377 // (tested on Linux-X86)
378 FieldSize = BitWidthExpr->EvaluateAsInt(Ctx).getZExtValue();
Mike Stump11289f42009-09-09 15:08:12 +0000379
Anders Carlsson79474332009-07-18 20:20:21 +0000380 std::pair<uint64_t, unsigned> FieldInfo = Ctx.getTypeInfo(D->getType());
381 uint64_t TypeSize = FieldInfo.first;
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000382
Anders Carlsson79474332009-07-18 20:20:21 +0000383 FieldAlign = FieldInfo.second;
Mike Stump11289f42009-09-09 15:08:12 +0000384
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000385 if (FieldPacked)
386 FieldAlign = 1;
Anders Carlsson79474332009-07-18 20:20:21 +0000387 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
388 FieldAlign = std::max(FieldAlign, AA->getAlignment());
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000389 // The maximum field alignment overrides the aligned attribute.
390 if (MaxFieldAlignment)
391 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
392
Anders Carlsson79474332009-07-18 20:20:21 +0000393 // Check if we need to add padding to give the field the correct
394 // alignment.
395 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
396 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
Mike Stump11289f42009-09-09 15:08:12 +0000397
Anders Carlsson79474332009-07-18 20:20:21 +0000398 // Padding members don't affect overall alignment
399 if (!D->getIdentifier())
400 FieldAlign = 1;
401 } else {
402 if (D->getType()->isIncompleteArrayType()) {
403 // This is a flexible array member; we can't directly
404 // query getTypeInfo about these, so we figure it out here.
405 // Flexible array members don't have any size, but they
406 // have to be aligned appropriately for their element type.
407 FieldSize = 0;
408 const ArrayType* ATy = Ctx.getAsArrayType(D->getType());
409 FieldAlign = Ctx.getTypeAlign(ATy->getElementType());
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000410 } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) {
Anders Carlsson79474332009-07-18 20:20:21 +0000411 unsigned AS = RT->getPointeeType().getAddressSpace();
412 FieldSize = Ctx.Target.getPointerWidth(AS);
413 FieldAlign = Ctx.Target.getPointerAlign(AS);
414 } else {
415 std::pair<uint64_t, unsigned> FieldInfo = Ctx.getTypeInfo(D->getType());
416 FieldSize = FieldInfo.first;
417 FieldAlign = FieldInfo.second;
418 }
Mike Stump11289f42009-09-09 15:08:12 +0000419
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000420 if (FieldPacked)
421 FieldAlign = 8;
Anders Carlsson79474332009-07-18 20:20:21 +0000422 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
423 FieldAlign = std::max(FieldAlign, AA->getAlignment());
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000424 // The maximum field alignment overrides the aligned attribute.
425 if (MaxFieldAlignment)
426 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
Mike Stump11289f42009-09-09 15:08:12 +0000427
Anders Carlsson79474332009-07-18 20:20:21 +0000428 // Round up the current record size to the field's alignment boundary.
Anders Carlsson6522b052009-09-24 03:13:30 +0000429 FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign);
Anders Carlsson79474332009-07-18 20:20:21 +0000430 }
Mike Stump11289f42009-09-09 15:08:12 +0000431
Anders Carlsson79474332009-07-18 20:20:21 +0000432 // Place this field at the current location.
433 FieldOffsets.push_back(FieldOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000434
Anders Carlsson79474332009-07-18 20:20:21 +0000435 // Reserve space for this field.
436 if (IsUnion)
437 Size = std::max(Size, FieldSize);
438 else
439 Size = FieldOffset + FieldSize;
Mike Stump11289f42009-09-09 15:08:12 +0000440
Anders Carlsson79474332009-07-18 20:20:21 +0000441 // Remember the next available offset.
442 NextOffset = Size;
Mike Stump11289f42009-09-09 15:08:12 +0000443
Anders Carlsson79474332009-07-18 20:20:21 +0000444 // Remember max struct/class alignment.
445 UpdateAlignment(FieldAlign);
446}
447
448void ASTRecordLayoutBuilder::FinishLayout() {
449 // In C++, records cannot be of size 0.
450 if (Ctx.getLangOptions().CPlusPlus && Size == 0)
451 Size = 8;
452 // Finally, round the size of the record up to the alignment of the
453 // record itself.
454 Size = (Size + (Alignment-1)) & ~(Alignment-1);
455}
456
457void ASTRecordLayoutBuilder::UpdateAlignment(unsigned NewAlignment) {
458 if (NewAlignment <= Alignment)
459 return;
Mike Stump11289f42009-09-09 15:08:12 +0000460
Anders Carlsson79474332009-07-18 20:20:21 +0000461 assert(llvm::isPowerOf2_32(NewAlignment && "Alignment not a power of 2"));
Mike Stump11289f42009-09-09 15:08:12 +0000462
Anders Carlsson79474332009-07-18 20:20:21 +0000463 Alignment = NewAlignment;
464}
Mike Stump11289f42009-09-09 15:08:12 +0000465
Anders Carlsson79474332009-07-18 20:20:21 +0000466const ASTRecordLayout *
Mike Stump11289f42009-09-09 15:08:12 +0000467ASTRecordLayoutBuilder::ComputeLayout(ASTContext &Ctx,
Anders Carlsson79474332009-07-18 20:20:21 +0000468 const RecordDecl *D) {
469 ASTRecordLayoutBuilder Builder(Ctx);
470
471 Builder.Layout(D);
472
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000473 if (!isa<CXXRecordDecl>(D))
474 return new ASTRecordLayout(Builder.Size, Builder.Alignment, Builder.Size,
Mike Stump11289f42009-09-09 15:08:12 +0000475 Builder.FieldOffsets.data(),
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000476 Builder.FieldOffsets.size());
Mike Stump11289f42009-09-09 15:08:12 +0000477
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000478 // FIXME: This is not always correct. See the part about bitfields at
479 // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info.
480 // FIXME: IsPODForThePurposeOfLayout should be stored in the record layout.
481 bool IsPODForThePurposeOfLayout = cast<CXXRecordDecl>(D)->isPOD();
Mike Stump11289f42009-09-09 15:08:12 +0000482
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000483 // FIXME: This should be done in FinalizeLayout.
Mike Stump11289f42009-09-09 15:08:12 +0000484 uint64_t DataSize =
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000485 IsPODForThePurposeOfLayout ? Builder.Size : Builder.NextOffset;
Mike Stump11289f42009-09-09 15:08:12 +0000486 uint64_t NonVirtualSize =
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000487 IsPODForThePurposeOfLayout ? DataSize : Builder.NonVirtualSize;
Mike Stump11289f42009-09-09 15:08:12 +0000488
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000489 return new ASTRecordLayout(Builder.Size, Builder.Alignment, DataSize,
Mike Stump11289f42009-09-09 15:08:12 +0000490 Builder.FieldOffsets.data(),
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000491 Builder.FieldOffsets.size(),
492 NonVirtualSize,
493 Builder.NonVirtualAlignment,
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000494 Builder.PrimaryBase,
Mike Stumpc266c6d2009-08-07 19:00:50 +0000495 Builder.PrimaryBaseWasVirtual,
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000496 Builder.Bases.data(),
Mike Stumpbcf756c2009-08-14 01:44:03 +0000497 Builder.Bases.size(),
498 Builder.VBases.data(),
Mike Stumpbcf756c2009-08-14 01:44:03 +0000499 Builder.VBases.size());
Anders Carlsson4f516282009-07-18 20:50:59 +0000500}
501
502const ASTRecordLayout *
503ASTRecordLayoutBuilder::ComputeLayout(ASTContext &Ctx,
504 const ObjCInterfaceDecl *D,
505 const ObjCImplementationDecl *Impl) {
506 ASTRecordLayoutBuilder Builder(Ctx);
Mike Stump11289f42009-09-09 15:08:12 +0000507
Anders Carlsson4f516282009-07-18 20:50:59 +0000508 Builder.Layout(D, Impl);
Mike Stump11289f42009-09-09 15:08:12 +0000509
Anders Carlsson4f516282009-07-18 20:50:59 +0000510 return new ASTRecordLayout(Builder.Size, Builder.Alignment,
511 Builder.NextOffset,
Mike Stump11289f42009-09-09 15:08:12 +0000512 Builder.FieldOffsets.data(),
Anders Carlsson79474332009-07-18 20:20:21 +0000513 Builder.FieldOffsets.size());
514}