blob: 1c5887626cc82aadd32f891d10ecbe5309c4658d [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
Mike Stump1eb44332009-09-09 15:08:12 +000024ASTRecordLayoutBuilder::ASTRecordLayoutBuilder(ASTContext &Ctx)
25 : Ctx(Ctx), Size(0), Alignment(8), Packed(false), MaxFieldAlignment(0),
Anders Carlssona5dd7222009-08-08 19:38:24 +000026 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.
Anders Carlsson3f066522009-09-22 03:02:06 +000029void ASTRecordLayoutBuilder::LayoutVtable(const CXXRecordDecl *RD) {
Mike Stump02b16232009-08-12 22:06:55 +000030 if (!RD->isDynamicClass()) {
Mike Stump2effeca2009-08-06 00:38:46 +000031 // There is no primary base in this case.
Mike Stump928f1502009-08-07 19:00:50 +000032 setPrimaryBase(0, false);
Mike Stump6f376332009-08-05 22:37:18 +000033 return;
Mike Stump2effeca2009-08-06 00:38:46 +000034 }
Mike Stump6f376332009-08-05 22:37:18 +000035
Anders Carlsson3f066522009-09-22 03:02:06 +000036 SelectPrimaryBase(RD);
Mike Stump6f376332009-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 Stump3dee6ef2009-07-30 00:22:38 +000043}
44
Mike Stump1eb44332009-09-09 15:08:12 +000045void
Anders Carlsson74cbe222009-07-19 00:18:47 +000046ASTRecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) {
Anders Carlsson74cbe222009-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 Stump1eb44332009-09-09 15:08:12 +000050 const CXXRecordDecl *Base =
Ted Kremenek6217b802009-07-29 21:53:49 +000051 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stump2effeca2009-08-06 00:38:46 +000052 // Skip the PrimaryBase here, as it is laid down first.
Mike Stump21538912009-08-14 01:44:03 +000053 if (Base != PrimaryBase || PrimaryBaseWasVirtual)
54 LayoutBaseNonVirtually(Base, false);
Anders Carlsson74cbe222009-07-19 00:18:47 +000055 }
56 }
57}
58
Mike Stump6f376332009-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 Carlsson3f066522009-09-22 03:02:06 +000064bool ASTRecordLayoutBuilder::IsNearlyEmpty(const CXXRecordDecl *RD) const {
Mike Stump6f376332009-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 Carlsson3f066522009-09-22 03:02:06 +000074void ASTRecordLayoutBuilder::IdentifyPrimaryBases(const CXXRecordDecl *RD) {
Mike Stump49520942009-08-11 04:03:59 +000075 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD);
Anders Carlsson3f066522009-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 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 Stump1eb44332009-09-09 15:08:12 +000085 const CXXRecordDecl *Base =
Mike Stump49520942009-08-11 04:03:59 +000086 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson3f066522009-09-22 03:02:06 +000087
Mike Stump49520942009-08-11 04:03:59 +000088 // Only bases with virtual bases participate in computing the
89 // indirect primary virtual base classes.
Anders Carlsson3f066522009-09-22 03:02:06 +000090 // FIXME: Is this correct?
Mike Stump4ef98092009-08-13 22:53:07 +000091 if (Base->getNumVBases())
Anders Carlsson3f066522009-09-22 03:02:06 +000092 IdentifyPrimaryBases(Base);
Mike Stump6f376332009-08-05 22:37:18 +000093 }
94}
95
Anders Carlsson3f066522009-09-22 03:02:06 +000096void
97ASTRecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD,
98 const CXXRecordDecl *&FirstPrimary) {
Mike Stumpd76264e2009-08-12 21:50:08 +000099 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
100 e = RD->bases_end(); i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000101 const CXXRecordDecl *Base =
Mike Stumpd76264e2009-08-12 21:50:08 +0000102 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
103 if (!i->isVirtual()) {
Anders Carlsson3f066522009-09-22 03:02:06 +0000104 SelectPrimaryVBase(Base, FirstPrimary);
Mike Stumpd76264e2009-08-12 21:50:08 +0000105 if (PrimaryBase)
106 return;
107 continue;
108 }
109 if (IsNearlyEmpty(Base)) {
110 if (FirstPrimary==0)
111 FirstPrimary = Base;
Anders Carlsson3f066522009-09-22 03:02:06 +0000112 if (!IndirectPrimaryBases.count(Base)) {
Mike Stumpd76264e2009-08-12 21:50:08 +0000113 setPrimaryBase(Base, true);
114 return;
115 }
116 }
117 }
118}
119
Mike Stump6f376332009-08-05 22:37:18 +0000120/// SelectPrimaryBase - Selects the primary base for the given class and
Mike Stump0880e752009-08-13 23:26:06 +0000121/// record that with setPrimaryBase. We also calculate the IndirectPrimaries.
Anders Carlsson3f066522009-09-22 03:02:06 +0000122void ASTRecordLayoutBuilder::SelectPrimaryBase(const CXXRecordDecl *RD) {
123 // Compute all the primary virtual bases for all of our direct and
Mike Stump0880e752009-08-13 23:26:06 +0000124 // indirect bases, and record all their primary virtual base classes.
Mike Stump0880e752009-08-13 23:26:06 +0000125 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
126 e = RD->bases_end(); i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000127 const CXXRecordDecl *Base =
Mike Stump0880e752009-08-13 23:26:06 +0000128 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson3f066522009-09-22 03:02:06 +0000129 IdentifyPrimaryBases(Base);
Mike Stump0880e752009-08-13 23:26:06 +0000130 }
131
Anders Carlsson3f066522009-09-22 03:02:06 +0000132 // If the record has a dynamic base class, attempt to choose a primary base
133 // class. It is the first (in direct base class order) non-virtual dynamic
134 // base class, if one exists.
Mike Stump6f376332009-08-05 22:37:18 +0000135 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
136 e = RD->bases_end(); i != e; ++i) {
137 if (!i->isVirtual()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000138 const CXXRecordDecl *Base =
Mike Stump6f376332009-08-05 22:37:18 +0000139 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
140 if (Base->isDynamicClass()) {
Anders Carlsson3f066522009-09-22 03:02:06 +0000141 // We found it.
Mike Stump928f1502009-08-07 19:00:50 +0000142 setPrimaryBase(Base, false);
Mike Stump6f376332009-08-05 22:37:18 +0000143 return;
144 }
145 }
146 }
147
148 // Otherwise, it is the first nearly empty virtual base that is not an
Mike Stump49520942009-08-11 04:03:59 +0000149 // indirect primary virtual base class, if one exists.
Mike Stump6f376332009-08-05 22:37:18 +0000150
151 // If we have no virtual bases at this point, bail out as the searching below
152 // is expensive.
Anders Carlsson3f066522009-09-22 03:02:06 +0000153 if (RD->getNumVBases() == 0) {
154 setPrimaryBase(0, false);
Mike Stump6f376332009-08-05 22:37:18 +0000155 return;
Anders Carlsson3f066522009-09-22 03:02:06 +0000156 }
157
Mike Stump6f376332009-08-05 22:37:18 +0000158 // Then we can search for the first nearly empty virtual base itself.
Anders Carlsson3f066522009-09-22 03:02:06 +0000159 const CXXRecordDecl *FirstPrimary = 0;
160 SelectPrimaryVBase(RD, FirstPrimary);
Mike Stump6f376332009-08-05 22:37:18 +0000161
Mike Stump928f1502009-08-07 19:00:50 +0000162 // Otherwise if is the first nearly empty virtual base, if one exists,
163 // otherwise there is no primary base class.
164 setPrimaryBase(FirstPrimary, true);
Mike Stump6f376332009-08-05 22:37:18 +0000165 return;
166}
167
Mike Stumpeb19fa92009-08-06 13:41:24 +0000168void ASTRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *RD) {
Mike Stump21538912009-08-14 01:44:03 +0000169 LayoutBaseNonVirtually(RD, true);
Mike Stumpeb19fa92009-08-06 13:41:24 +0000170}
171
Mike Stumpd53cef12009-08-13 02:02:14 +0000172void ASTRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
Mike Stumpfe3010d2009-08-16 19:04:13 +0000173 const CXXRecordDecl *PB,
Mike Stump276b9f12009-08-16 01:46:26 +0000174 int64_t Offset,
175 llvm::SmallSet<const CXXRecordDecl*, 32> &mark,
Mike Stumpd53cef12009-08-13 02:02:14 +0000176 llvm::SmallSet<const CXXRecordDecl*, 32> &IndirectPrimary) {
Mike Stump4ef98092009-08-13 22:53:07 +0000177 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
178 e = RD->bases_end(); i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000179 const CXXRecordDecl *Base =
Mike Stumpeb19fa92009-08-06 13:41:24 +0000180 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stump276b9f12009-08-16 01:46:26 +0000181#if 0
182 const ASTRecordLayout &L = Ctx.getASTRecordLayout(Base);
183 const CXXRecordDecl *PB = L.getPrimaryBase();
184 if (PB && L.getPrimaryBaseWasVirtual()
185 && IndirectPrimary.count(PB)) {
186 int64_t BaseOffset;
187 // FIXME: calculate this.
188 BaseOffset = (1<<63) | (1<<31);
189 VBases.push_back(PB);
190 VBaseOffsets.push_back(BaseOffset);
191 }
192#endif
Mike Stumpfe3010d2009-08-16 19:04:13 +0000193 int64_t BaseOffset = Offset;;
194 // FIXME: Calculate BaseOffset.
Mike Stump276b9f12009-08-16 01:46:26 +0000195 if (i->isVirtual()) {
Mike Stumpfe3010d2009-08-16 19:04:13 +0000196 if (Base == PB) {
197 // Only lay things out once.
198 if (mark.count(Base))
199 continue;
200 // Mark it so we don't lay it out twice.
201 mark.insert(Base);
202 assert (IndirectPrimary.count(Base) && "IndirectPrimary was wrong");
Anders Carlssone4feb832009-09-22 00:04:45 +0000203 VBases.push_back(std::make_pair(Base, Offset));
Mike Stumpfe3010d2009-08-16 19:04:13 +0000204 } else if (IndirectPrimary.count(Base)) {
205 // Someone else will eventually lay this out.
206 ;
207 } else {
208 // Only lay things out once.
209 if (mark.count(Base))
210 continue;
211 // Mark it so we don't lay it out twice.
212 mark.insert(Base);
Mike Stump276b9f12009-08-16 01:46:26 +0000213 LayoutVirtualBase(Base);
Anders Carlssone4feb832009-09-22 00:04:45 +0000214 BaseOffset = VBases.back().second;
Mike Stumpfe3010d2009-08-16 19:04:13 +0000215 }
Mike Stump4ef98092009-08-13 22:53:07 +0000216 }
Mike Stumpfe3010d2009-08-16 19:04:13 +0000217 if (Base->getNumVBases()) {
218 const ASTRecordLayout &L = Ctx.getASTRecordLayout(Base);
219 const CXXRecordDecl *PB = L.getPrimaryBase();
220 LayoutVirtualBases(Base, PB, BaseOffset, mark, IndirectPrimary);
221 }
Mike Stumpeb19fa92009-08-06 13:41:24 +0000222 }
223}
224
Mike Stump21538912009-08-14 01:44:03 +0000225void ASTRecordLayoutBuilder::LayoutBaseNonVirtually(const CXXRecordDecl *RD,
226 bool IsVirtualBase) {
Anders Carlsson74cbe222009-07-19 00:18:47 +0000227 const ASTRecordLayout &BaseInfo = Ctx.getASTRecordLayout(RD);
Anders Carlssonb237fd62009-09-17 04:42:56 +0000228 if (!Bases.empty()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000229 assert(BaseInfo.getDataSize() > 0 &&
Anders Carlsson74cbe222009-07-19 00:18:47 +0000230 "FIXME: Handle empty classes.");
Anders Carlssonb237fd62009-09-17 04:42:56 +0000231 }
232
Anders Carlssonb2fafd42009-07-28 19:24:15 +0000233 unsigned BaseAlign = BaseInfo.getNonVirtualAlign();
234 uint64_t BaseSize = BaseInfo.getNonVirtualSize();
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Anders Carlsson74cbe222009-07-19 00:18:47 +0000236 // Round up the current record size to the base's alignment boundary.
237 Size = (Size + (BaseAlign-1)) & ~(BaseAlign-1);
238
Mike Stump276b9f12009-08-16 01:46:26 +0000239 // Add base class offsets.
Anders Carlssone4feb832009-09-22 00:04:45 +0000240 if (IsVirtualBase)
241 VBases.push_back(std::make_pair(RD, Size));
242 else
243 Bases.push_back(std::make_pair(RD, Size));
Anders Carlssonb2fafd42009-07-28 19:24:15 +0000244
Mike Stumpfe3010d2009-08-16 19:04:13 +0000245#if 0
Mike Stump276b9f12009-08-16 01:46:26 +0000246 // And now add offsets for all our primary virtual bases as well, so
247 // they all have offsets.
248 const ASTRecordLayout *L = &BaseInfo;
249 const CXXRecordDecl *PB = L->getPrimaryBase();
250 while (PB) {
251 if (L->getPrimaryBaseWasVirtual()) {
252 VBases.push_back(PB);
253 VBaseOffsets.push_back(Size);
254 }
255 PB = L->getPrimaryBase();
256 if (PB)
257 L = &Ctx.getASTRecordLayout(PB);
258 }
Mike Stumpfe3010d2009-08-16 19:04:13 +0000259#endif
Mike Stump276b9f12009-08-16 01:46:26 +0000260
Anders Carlsson74cbe222009-07-19 00:18:47 +0000261 // Reserve space for this base.
262 Size += BaseSize;
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Anders Carlsson74cbe222009-07-19 00:18:47 +0000264 // Remember the next available offset.
265 NextOffset = Size;
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Anders Carlsson74cbe222009-07-19 00:18:47 +0000267 // Remember max struct/class alignment.
268 UpdateAlignment(BaseAlign);
269}
270
Anders Carlssonbda4c102009-07-18 20:20:21 +0000271void ASTRecordLayoutBuilder::Layout(const RecordDecl *D) {
272 IsUnion = D->isUnion();
Anders Carlssona860e752009-08-08 18:23:56 +0000273
Anders Carlssona5dd7222009-08-08 19:38:24 +0000274 Packed = D->hasAttr<PackedAttr>();
275
276 // The #pragma pack attribute specifies the maximum field alignment.
Anders Carlssona860e752009-08-08 18:23:56 +0000277 if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>())
Anders Carlssona5dd7222009-08-08 19:38:24 +0000278 MaxFieldAlignment = PPA->getAlignment();
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Anders Carlssonbda4c102009-07-18 20:20:21 +0000280 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
281 UpdateAlignment(AA->getAlignment());
Anders Carlsson74cbe222009-07-19 00:18:47 +0000282
Mike Stump276b9f12009-08-16 01:46:26 +0000283 // If this is a C++ class, lay out the vtable and the non-virtual bases.
Mike Stumpeb19fa92009-08-06 13:41:24 +0000284 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
285 if (RD) {
Anders Carlsson3f066522009-09-22 03:02:06 +0000286 LayoutVtable(RD);
Mike Stump2effeca2009-08-06 00:38:46 +0000287 // PrimaryBase goes first.
Mike Stumpd53cef12009-08-13 02:02:14 +0000288 if (PrimaryBase) {
Mike Stumpd53cef12009-08-13 02:02:14 +0000289 if (PrimaryBaseWasVirtual)
Anders Carlsson3f066522009-09-22 03:02:06 +0000290 IndirectPrimaryBases.insert(PrimaryBase);
Mike Stump21538912009-08-14 01:44:03 +0000291 LayoutBaseNonVirtually(PrimaryBase, PrimaryBaseWasVirtual);
Mike Stumpd53cef12009-08-13 02:02:14 +0000292 }
Mike Stump3dee6ef2009-07-30 00:22:38 +0000293 LayoutNonVirtualBases(RD);
Mike Stump3dee6ef2009-07-30 00:22:38 +0000294 }
Anders Carlsson74cbe222009-07-19 00:18:47 +0000295
Anders Carlssona2df41c2009-07-18 21:48:39 +0000296 LayoutFields(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Anders Carlssonb2fafd42009-07-28 19:24:15 +0000298 NonVirtualSize = Size;
299 NonVirtualAlignment = Alignment;
Mike Stump3dee6ef2009-07-30 00:22:38 +0000300
Mike Stump276b9f12009-08-16 01:46:26 +0000301 if (RD) {
302 llvm::SmallSet<const CXXRecordDecl*, 32> mark;
Anders Carlsson3f066522009-09-22 03:02:06 +0000303 LayoutVirtualBases(RD, PrimaryBase, 0, mark, IndirectPrimaryBases);
Mike Stump276b9f12009-08-16 01:46:26 +0000304 }
Mike Stumpeb19fa92009-08-06 13:41:24 +0000305
Anders Carlssonbda4c102009-07-18 20:20:21 +0000306 // Finally, round the size of the total struct up to the alignment of the
307 // struct itself.
308 FinishLayout();
309}
310
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000311void ASTRecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D,
312 const ObjCImplementationDecl *Impl) {
313 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
314 const ASTRecordLayout &SL = Ctx.getASTObjCInterfaceLayout(SD);
315
316 UpdateAlignment(SL.getAlignment());
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000318 // We start laying out ivars not at the end of the superclass
319 // structure, but at the next byte following the last field.
Anders Carlsson243a6852009-07-18 21:26:44 +0000320 Size = llvm::RoundUpToAlignment(SL.getDataSize(), 8);
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000321 NextOffset = Size;
322 }
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Anders Carlssona5dd7222009-08-08 19:38:24 +0000324 Packed = D->hasAttr<PackedAttr>();
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Anders Carlssona5dd7222009-08-08 19:38:24 +0000326 // The #pragma pack attribute specifies the maximum field alignment.
327 if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>())
328 MaxFieldAlignment = PPA->getAlignment();
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000330 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
331 UpdateAlignment(AA->getAlignment());
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000333 // Layout each ivar sequentially.
334 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
335 Ctx.ShallowCollectObjCIvars(D, Ivars, Impl);
336 for (unsigned i = 0, e = Ivars.size(); i != e; ++i)
337 LayoutField(Ivars[i]);
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000339 // Finally, round the size of the total struct up to the alignment of the
340 // struct itself.
341 FinishLayout();
342}
343
Anders Carlssona2df41c2009-07-18 21:48:39 +0000344void ASTRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
345 // Layout each field, for now, just sequentially, respecting alignment. In
346 // the future, this will need to be tweakable by targets.
Mike Stump1eb44332009-09-09 15:08:12 +0000347 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlssona2df41c2009-07-18 21:48:39 +0000348 FieldEnd = D->field_end(); Field != FieldEnd; ++Field)
349 LayoutField(*Field);
350}
351
Anders Carlssonbda4c102009-07-18 20:20:21 +0000352void ASTRecordLayoutBuilder::LayoutField(const FieldDecl *D) {
Anders Carlssona5dd7222009-08-08 19:38:24 +0000353 bool FieldPacked = Packed;
Anders Carlssonbda4c102009-07-18 20:20:21 +0000354 uint64_t FieldOffset = IsUnion ? 0 : Size;
355 uint64_t FieldSize;
356 unsigned FieldAlign;
Mike Stump1eb44332009-09-09 15:08:12 +0000357
358 FieldPacked |= D->hasAttr<PackedAttr>();
359
Anders Carlssonbda4c102009-07-18 20:20:21 +0000360 if (const Expr *BitWidthExpr = D->getBitWidth()) {
361 // TODO: Need to check this algorithm on other targets!
362 // (tested on Linux-X86)
363 FieldSize = BitWidthExpr->EvaluateAsInt(Ctx).getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Anders Carlssonbda4c102009-07-18 20:20:21 +0000365 std::pair<uint64_t, unsigned> FieldInfo = Ctx.getTypeInfo(D->getType());
366 uint64_t TypeSize = FieldInfo.first;
Anders Carlssona5dd7222009-08-08 19:38:24 +0000367
Anders Carlssonbda4c102009-07-18 20:20:21 +0000368 FieldAlign = FieldInfo.second;
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Anders Carlssona5dd7222009-08-08 19:38:24 +0000370 if (FieldPacked)
371 FieldAlign = 1;
Anders Carlssonbda4c102009-07-18 20:20:21 +0000372 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
373 FieldAlign = std::max(FieldAlign, AA->getAlignment());
Anders Carlssona5dd7222009-08-08 19:38:24 +0000374 // The maximum field alignment overrides the aligned attribute.
375 if (MaxFieldAlignment)
376 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
377
Anders Carlssonbda4c102009-07-18 20:20:21 +0000378 // Check if we need to add padding to give the field the correct
379 // alignment.
380 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
381 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Anders Carlssonbda4c102009-07-18 20:20:21 +0000383 // Padding members don't affect overall alignment
384 if (!D->getIdentifier())
385 FieldAlign = 1;
386 } else {
387 if (D->getType()->isIncompleteArrayType()) {
388 // This is a flexible array member; we can't directly
389 // query getTypeInfo about these, so we figure it out here.
390 // Flexible array members don't have any size, but they
391 // have to be aligned appropriately for their element type.
392 FieldSize = 0;
393 const ArrayType* ATy = Ctx.getAsArrayType(D->getType());
394 FieldAlign = Ctx.getTypeAlign(ATy->getElementType());
Ted Kremenek6217b802009-07-29 21:53:49 +0000395 } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) {
Anders Carlssonbda4c102009-07-18 20:20:21 +0000396 unsigned AS = RT->getPointeeType().getAddressSpace();
397 FieldSize = Ctx.Target.getPointerWidth(AS);
398 FieldAlign = Ctx.Target.getPointerAlign(AS);
399 } else {
400 std::pair<uint64_t, unsigned> FieldInfo = Ctx.getTypeInfo(D->getType());
401 FieldSize = FieldInfo.first;
402 FieldAlign = FieldInfo.second;
403 }
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Anders Carlssona5dd7222009-08-08 19:38:24 +0000405 if (FieldPacked)
406 FieldAlign = 8;
Anders Carlssonbda4c102009-07-18 20:20:21 +0000407 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
408 FieldAlign = std::max(FieldAlign, AA->getAlignment());
Anders Carlssona5dd7222009-08-08 19:38:24 +0000409 // The maximum field alignment overrides the aligned attribute.
410 if (MaxFieldAlignment)
411 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Anders Carlssonbda4c102009-07-18 20:20:21 +0000413 // Round up the current record size to the field's alignment boundary.
414 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
415 }
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Anders Carlssonbda4c102009-07-18 20:20:21 +0000417 // Place this field at the current location.
418 FieldOffsets.push_back(FieldOffset);
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Anders Carlssonbda4c102009-07-18 20:20:21 +0000420 // Reserve space for this field.
421 if (IsUnion)
422 Size = std::max(Size, FieldSize);
423 else
424 Size = FieldOffset + FieldSize;
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Anders Carlssonbda4c102009-07-18 20:20:21 +0000426 // Remember the next available offset.
427 NextOffset = Size;
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Anders Carlssonbda4c102009-07-18 20:20:21 +0000429 // Remember max struct/class alignment.
430 UpdateAlignment(FieldAlign);
431}
432
433void ASTRecordLayoutBuilder::FinishLayout() {
434 // In C++, records cannot be of size 0.
435 if (Ctx.getLangOptions().CPlusPlus && Size == 0)
436 Size = 8;
437 // Finally, round the size of the record up to the alignment of the
438 // record itself.
439 Size = (Size + (Alignment-1)) & ~(Alignment-1);
440}
441
442void ASTRecordLayoutBuilder::UpdateAlignment(unsigned NewAlignment) {
443 if (NewAlignment <= Alignment)
444 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Anders Carlssonbda4c102009-07-18 20:20:21 +0000446 assert(llvm::isPowerOf2_32(NewAlignment && "Alignment not a power of 2"));
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Anders Carlssonbda4c102009-07-18 20:20:21 +0000448 Alignment = NewAlignment;
449}
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Anders Carlssonbda4c102009-07-18 20:20:21 +0000451const ASTRecordLayout *
Mike Stump1eb44332009-09-09 15:08:12 +0000452ASTRecordLayoutBuilder::ComputeLayout(ASTContext &Ctx,
Anders Carlssonbda4c102009-07-18 20:20:21 +0000453 const RecordDecl *D) {
454 ASTRecordLayoutBuilder Builder(Ctx);
455
456 Builder.Layout(D);
457
Anders Carlssonb2fafd42009-07-28 19:24:15 +0000458 if (!isa<CXXRecordDecl>(D))
459 return new ASTRecordLayout(Builder.Size, Builder.Alignment, Builder.Size,
Mike Stump1eb44332009-09-09 15:08:12 +0000460 Builder.FieldOffsets.data(),
Anders Carlssonb2fafd42009-07-28 19:24:15 +0000461 Builder.FieldOffsets.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Anders Carlssonb2fafd42009-07-28 19:24:15 +0000463 // FIXME: This is not always correct. See the part about bitfields at
464 // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info.
465 // FIXME: IsPODForThePurposeOfLayout should be stored in the record layout.
466 bool IsPODForThePurposeOfLayout = cast<CXXRecordDecl>(D)->isPOD();
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Anders Carlssonb2fafd42009-07-28 19:24:15 +0000468 // FIXME: This should be done in FinalizeLayout.
Mike Stump1eb44332009-09-09 15:08:12 +0000469 uint64_t DataSize =
Anders Carlsson74cbe222009-07-19 00:18:47 +0000470 IsPODForThePurposeOfLayout ? Builder.Size : Builder.NextOffset;
Mike Stump1eb44332009-09-09 15:08:12 +0000471 uint64_t NonVirtualSize =
Anders Carlssonb2fafd42009-07-28 19:24:15 +0000472 IsPODForThePurposeOfLayout ? DataSize : Builder.NonVirtualSize;
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Anders Carlsson74cbe222009-07-19 00:18:47 +0000474 return new ASTRecordLayout(Builder.Size, Builder.Alignment, DataSize,
Mike Stump1eb44332009-09-09 15:08:12 +0000475 Builder.FieldOffsets.data(),
Anders Carlssonb2fafd42009-07-28 19:24:15 +0000476 Builder.FieldOffsets.size(),
477 NonVirtualSize,
478 Builder.NonVirtualAlignment,
Mike Stump6f376332009-08-05 22:37:18 +0000479 Builder.PrimaryBase,
Mike Stump928f1502009-08-07 19:00:50 +0000480 Builder.PrimaryBaseWasVirtual,
Anders Carlssonb2fafd42009-07-28 19:24:15 +0000481 Builder.Bases.data(),
Mike Stump21538912009-08-14 01:44:03 +0000482 Builder.Bases.size(),
483 Builder.VBases.data(),
Mike Stump21538912009-08-14 01:44:03 +0000484 Builder.VBases.size());
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000485}
486
487const ASTRecordLayout *
488ASTRecordLayoutBuilder::ComputeLayout(ASTContext &Ctx,
489 const ObjCInterfaceDecl *D,
490 const ObjCImplementationDecl *Impl) {
491 ASTRecordLayoutBuilder Builder(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000493 Builder.Layout(D, Impl);
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000495 return new ASTRecordLayout(Builder.Size, Builder.Alignment,
496 Builder.NextOffset,
Mike Stump1eb44332009-09-09 15:08:12 +0000497 Builder.FieldOffsets.data(),
Anders Carlssonbda4c102009-07-18 20:20:21 +0000498 Builder.FieldOffsets.size());
499}