blob: 05b5ee977cc9ac87882638c0a1222267600a60f1 [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 Carlsson28a5fa22009-08-08 19:38:24 +000026 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 Stumpc2f591b2009-08-13 22:53:07 +000029void ASTRecordLayoutBuilder::LayoutVtable(const CXXRecordDecl *RD,
30 llvm::SmallSet<const CXXRecordDecl*, 32> &IndirectPrimary) {
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 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
Mike Stumpc2f591b2009-08-13 22:53:07 +000037 SelectPrimaryBase(RD, IndirectPrimary);
Mike Stumpd8fe7b22009-08-05 22:37:18 +000038 if (PrimaryBase == 0) {
39 int AS = 0;
40 UpdateAlignment(Ctx.Target.getPointerAlign(AS));
41 Size += Ctx.Target.getPointerWidth(AS);
42 NextOffset = Size;
43 }
Mike Stump3dc7eb92009-07-30 00:22:38 +000044}
45
Mike Stump11289f42009-09-09 15:08:12 +000046void
Anders Carlsson6d9f6f32009-07-19 00:18:47 +000047ASTRecordLayoutBuilder::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()) {
Mike Stump11289f42009-09-09 15:08:12 +000051 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.
Mike Stumpbcf756c2009-08-14 01:44:03 +000054 if (Base != PrimaryBase || PrimaryBaseWasVirtual)
55 LayoutBaseNonVirtually(Base, false);
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) {
Mike Stump78696a72009-08-11 04:03:59 +000077 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD);
78 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
79 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
Mike Stumpbcf756c2009-08-14 01:44:03 +000080
81 if (PrimaryBaseWasVirtual)
Mike Stump78696a72009-08-11 04:03:59 +000082 IndirectPrimary.insert(PrimaryBase);
Mike Stumpbcf756c2009-08-14 01:44:03 +000083
Mike Stumpd8fe7b22009-08-05 22:37:18 +000084 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
85 e = RD->bases_end(); i != e; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +000086 const CXXRecordDecl *Base =
Mike Stump78696a72009-08-11 04:03:59 +000087 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
88 // 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())
91 SelectPrimaryForBase(Base, IndirectPrimary);
Mike Stumpd8fe7b22009-08-05 22:37:18 +000092 }
93}
94
Mike Stump6f3793b2009-08-12 21:50:08 +000095void ASTRecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD,
96 const CXXRecordDecl *&FirstPrimary,
97 llvm::SmallSet<const CXXRecordDecl*, 32> &IndirectPrimary) {
98 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()) {
103 SelectPrimaryVBase(Base, FirstPrimary, IndirectPrimary);
104 if (PrimaryBase)
105 return;
106 continue;
107 }
108 if (IsNearlyEmpty(Base)) {
109 if (FirstPrimary==0)
110 FirstPrimary = Base;
111 if (!IndirectPrimary.count(Base)) {
112 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.
Mike Stumpc2f591b2009-08-13 22:53:07 +0000121void ASTRecordLayoutBuilder::SelectPrimaryBase(const CXXRecordDecl *RD,
122 llvm::SmallSet<const CXXRecordDecl*, 32> &IndirectPrimary) {
Mike Stump590a7c72009-08-13 23:26:06 +0000123 // We compute all the primary virtual bases for all of our direct and
124 // indirect bases, and record all their primary virtual base classes.
125 const CXXRecordDecl *FirstPrimary = 0;
126 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
127 e = RD->bases_end(); i != e; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +0000128 const CXXRecordDecl *Base =
Mike Stump590a7c72009-08-13 23:26:06 +0000129 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
130 SelectPrimaryForBase(Base, IndirectPrimary);
131 }
132
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000133 // The primary base is the first non-virtual indirect or direct base class,
134 // if one exists.
135 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
136 e = RD->bases_end(); i != e; ++i) {
137 if (!i->isVirtual()) {
Mike Stump11289f42009-09-09 15:08:12 +0000138 const CXXRecordDecl *Base =
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000139 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
140 if (Base->isDynamicClass()) {
Mike Stumpc266c6d2009-08-07 19:00:50 +0000141 setPrimaryBase(Base, false);
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000142 return;
143 }
144 }
145 }
146
Mike Stump6f3793b2009-08-12 21:50:08 +0000147 setPrimaryBase(0, false);
148
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000149 // Otherwise, it is the first nearly empty virtual base that is not an
Mike Stump78696a72009-08-11 04:03:59 +0000150 // indirect primary virtual base class, if one exists.
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000151
152 // If we have no virtual bases at this point, bail out as the searching below
153 // is expensive.
Mike Stump590a7c72009-08-13 23:26:06 +0000154 if (RD->getNumVBases() == 0)
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000155 return;
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000156
157 // Then we can search for the first nearly empty virtual base itself.
Mike Stump6f3793b2009-08-12 21:50:08 +0000158 SelectPrimaryVBase(RD, FirstPrimary, IndirectPrimary);
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000159
Mike Stumpc266c6d2009-08-07 19:00:50 +0000160 // Otherwise if is the first nearly empty virtual base, if one exists,
161 // otherwise there is no primary base class.
162 setPrimaryBase(FirstPrimary, true);
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000163 return;
164}
165
Mike Stump6b2556f2009-08-06 13:41:24 +0000166void ASTRecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *RD) {
Mike Stumpbcf756c2009-08-14 01:44:03 +0000167 LayoutBaseNonVirtually(RD, true);
Mike Stump6b2556f2009-08-06 13:41:24 +0000168}
169
Mike Stump13007542009-08-13 02:02:14 +0000170void ASTRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
Mike Stump996576f32009-08-16 19:04:13 +0000171 const CXXRecordDecl *PB,
Mike Stump22ea1f82009-08-16 01:46:26 +0000172 int64_t Offset,
173 llvm::SmallSet<const CXXRecordDecl*, 32> &mark,
Mike Stump13007542009-08-13 02:02:14 +0000174 llvm::SmallSet<const CXXRecordDecl*, 32> &IndirectPrimary) {
Mike Stumpc2f591b2009-08-13 22:53:07 +0000175 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
176 e = RD->bases_end(); i != e; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +0000177 const CXXRecordDecl *Base =
Mike Stump6b2556f2009-08-06 13:41:24 +0000178 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stump22ea1f82009-08-16 01:46:26 +0000179#if 0
180 const ASTRecordLayout &L = Ctx.getASTRecordLayout(Base);
181 const CXXRecordDecl *PB = L.getPrimaryBase();
182 if (PB && L.getPrimaryBaseWasVirtual()
183 && IndirectPrimary.count(PB)) {
184 int64_t BaseOffset;
185 // FIXME: calculate this.
186 BaseOffset = (1<<63) | (1<<31);
187 VBases.push_back(PB);
188 VBaseOffsets.push_back(BaseOffset);
189 }
190#endif
Mike Stump996576f32009-08-16 19:04:13 +0000191 int64_t BaseOffset = Offset;;
192 // FIXME: Calculate BaseOffset.
Mike Stump22ea1f82009-08-16 01:46:26 +0000193 if (i->isVirtual()) {
Mike Stump996576f32009-08-16 19:04:13 +0000194 if (Base == PB) {
195 // Only lay things out once.
196 if (mark.count(Base))
197 continue;
198 // Mark it so we don't lay it out twice.
199 mark.insert(Base);
200 assert (IndirectPrimary.count(Base) && "IndirectPrimary was wrong");
Mike Stump22ea1f82009-08-16 01:46:26 +0000201 VBases.push_back(Base);
Mike Stump996576f32009-08-16 19:04:13 +0000202 VBaseOffsets.push_back(Offset);
203 } else if (IndirectPrimary.count(Base)) {
204 // Someone else will eventually lay this out.
205 ;
206 } else {
207 // Only lay things out once.
208 if (mark.count(Base))
209 continue;
210 // Mark it so we don't lay it out twice.
211 mark.insert(Base);
Mike Stump22ea1f82009-08-16 01:46:26 +0000212 LayoutVirtualBase(Base);
Mike Stump996576f32009-08-16 19:04:13 +0000213 BaseOffset = *(VBaseOffsets.end()-1);
214 }
Mike Stumpc2f591b2009-08-13 22:53:07 +0000215 }
Mike Stump996576f32009-08-16 19:04:13 +0000216 if (Base->getNumVBases()) {
217 const ASTRecordLayout &L = Ctx.getASTRecordLayout(Base);
218 const CXXRecordDecl *PB = L.getPrimaryBase();
219 LayoutVirtualBases(Base, PB, BaseOffset, mark, IndirectPrimary);
220 }
Mike Stump6b2556f2009-08-06 13:41:24 +0000221 }
222}
223
Mike Stumpbcf756c2009-08-14 01:44:03 +0000224void ASTRecordLayoutBuilder::LayoutBaseNonVirtually(const CXXRecordDecl *RD,
225 bool IsVirtualBase) {
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000226 const ASTRecordLayout &BaseInfo = Ctx.getASTRecordLayout(RD);
Mike Stump11289f42009-09-09 15:08:12 +0000227 assert(BaseInfo.getDataSize() > 0 &&
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000228 "FIXME: Handle empty classes.");
Mike Stump11289f42009-09-09 15:08:12 +0000229
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000230 unsigned BaseAlign = BaseInfo.getNonVirtualAlign();
231 uint64_t BaseSize = BaseInfo.getNonVirtualSize();
Mike Stump11289f42009-09-09 15:08:12 +0000232
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000233 // Round up the current record size to the base's alignment boundary.
234 Size = (Size + (BaseAlign-1)) & ~(BaseAlign-1);
235
Mike Stump22ea1f82009-08-16 01:46:26 +0000236 // Add base class offsets.
Mike Stumpbcf756c2009-08-14 01:44:03 +0000237 if (IsVirtualBase) {
238 VBases.push_back(RD);
239 VBaseOffsets.push_back(Size);
240 } else {
241 Bases.push_back(RD);
242 BaseOffsets.push_back(Size);
243 }
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000244
Mike Stump996576f32009-08-16 19:04:13 +0000245#if 0
Mike Stump22ea1f82009-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 Stump996576f32009-08-16 19:04:13 +0000259#endif
Mike Stump22ea1f82009-08-16 01:46:26 +0000260
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000261 // Reserve space for this base.
262 Size += BaseSize;
Mike Stump11289f42009-09-09 15:08:12 +0000263
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000264 // Remember the next available offset.
265 NextOffset = Size;
Mike Stump11289f42009-09-09 15:08:12 +0000266
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000267 // Remember max struct/class alignment.
268 UpdateAlignment(BaseAlign);
269}
270
Anders Carlsson79474332009-07-18 20:20:21 +0000271void ASTRecordLayoutBuilder::Layout(const RecordDecl *D) {
272 IsUnion = D->isUnion();
Anders Carlsson68e0b682009-08-08 18:23:56 +0000273
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000274 Packed = D->hasAttr<PackedAttr>();
275
276 // The #pragma pack attribute specifies the maximum field alignment.
Anders Carlsson68e0b682009-08-08 18:23:56 +0000277 if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>())
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000278 MaxFieldAlignment = PPA->getAlignment();
Mike Stump11289f42009-09-09 15:08:12 +0000279
Anders Carlsson79474332009-07-18 20:20:21 +0000280 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
281 UpdateAlignment(AA->getAlignment());
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000282
Mike Stump13007542009-08-13 02:02:14 +0000283 llvm::SmallSet<const CXXRecordDecl*, 32> IndirectPrimary;
284
Mike Stump22ea1f82009-08-16 01:46:26 +0000285 // If this is a C++ class, lay out the vtable and the non-virtual bases.
Mike Stump6b2556f2009-08-06 13:41:24 +0000286 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
287 if (RD) {
Mike Stumpc2f591b2009-08-13 22:53:07 +0000288 LayoutVtable(RD, IndirectPrimary);
Mike Stump25094802009-08-06 00:38:46 +0000289 // PrimaryBase goes first.
Mike Stump13007542009-08-13 02:02:14 +0000290 if (PrimaryBase) {
Mike Stump13007542009-08-13 02:02:14 +0000291 if (PrimaryBaseWasVirtual)
292 IndirectPrimary.insert(PrimaryBase);
Mike Stumpbcf756c2009-08-14 01:44:03 +0000293 LayoutBaseNonVirtually(PrimaryBase, PrimaryBaseWasVirtual);
Mike Stump13007542009-08-13 02:02:14 +0000294 }
Mike Stump3dc7eb92009-07-30 00:22:38 +0000295 LayoutNonVirtualBases(RD);
Mike Stump3dc7eb92009-07-30 00:22:38 +0000296 }
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000297
Anders Carlsson118ce162009-07-18 21:48:39 +0000298 LayoutFields(D);
Mike Stump11289f42009-09-09 15:08:12 +0000299
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000300 NonVirtualSize = Size;
301 NonVirtualAlignment = Alignment;
Mike Stump3dc7eb92009-07-30 00:22:38 +0000302
Mike Stump22ea1f82009-08-16 01:46:26 +0000303 if (RD) {
304 llvm::SmallSet<const CXXRecordDecl*, 32> mark;
Mike Stump996576f32009-08-16 19:04:13 +0000305 LayoutVirtualBases(RD, PrimaryBase, 0, mark, IndirectPrimary);
Mike Stump22ea1f82009-08-16 01:46:26 +0000306 }
Mike Stump6b2556f2009-08-06 13:41:24 +0000307
Anders Carlsson79474332009-07-18 20:20:21 +0000308 // Finally, round the size of the total struct up to the alignment of the
309 // struct itself.
310 FinishLayout();
311}
312
Anders Carlsson4f516282009-07-18 20:50:59 +0000313void ASTRecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D,
314 const ObjCImplementationDecl *Impl) {
315 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
316 const ASTRecordLayout &SL = Ctx.getASTObjCInterfaceLayout(SD);
317
318 UpdateAlignment(SL.getAlignment());
Mike Stump11289f42009-09-09 15:08:12 +0000319
Anders Carlsson4f516282009-07-18 20:50:59 +0000320 // We start laying out ivars not at the end of the superclass
321 // structure, but at the next byte following the last field.
Anders Carlsson27b50132009-07-18 21:26:44 +0000322 Size = llvm::RoundUpToAlignment(SL.getDataSize(), 8);
Anders Carlsson4f516282009-07-18 20:50:59 +0000323 NextOffset = Size;
324 }
Mike Stump11289f42009-09-09 15:08:12 +0000325
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000326 Packed = D->hasAttr<PackedAttr>();
Mike Stump11289f42009-09-09 15:08:12 +0000327
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000328 // The #pragma pack attribute specifies the maximum field alignment.
329 if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>())
330 MaxFieldAlignment = PPA->getAlignment();
Mike Stump11289f42009-09-09 15:08:12 +0000331
Anders Carlsson4f516282009-07-18 20:50:59 +0000332 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
333 UpdateAlignment(AA->getAlignment());
Mike Stump11289f42009-09-09 15:08:12 +0000334
Anders Carlsson4f516282009-07-18 20:50:59 +0000335 // Layout each ivar sequentially.
336 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
337 Ctx.ShallowCollectObjCIvars(D, Ivars, Impl);
338 for (unsigned i = 0, e = Ivars.size(); i != e; ++i)
339 LayoutField(Ivars[i]);
Mike Stump11289f42009-09-09 15:08:12 +0000340
Anders Carlsson4f516282009-07-18 20:50:59 +0000341 // Finally, round the size of the total struct up to the alignment of the
342 // struct itself.
343 FinishLayout();
344}
345
Anders Carlsson118ce162009-07-18 21:48:39 +0000346void ASTRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
347 // Layout each field, for now, just sequentially, respecting alignment. In
348 // the future, this will need to be tweakable by targets.
Mike Stump11289f42009-09-09 15:08:12 +0000349 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson118ce162009-07-18 21:48:39 +0000350 FieldEnd = D->field_end(); Field != FieldEnd; ++Field)
351 LayoutField(*Field);
352}
353
Anders Carlsson79474332009-07-18 20:20:21 +0000354void ASTRecordLayoutBuilder::LayoutField(const FieldDecl *D) {
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000355 bool FieldPacked = Packed;
Anders Carlsson79474332009-07-18 20:20:21 +0000356 uint64_t FieldOffset = IsUnion ? 0 : Size;
357 uint64_t FieldSize;
358 unsigned FieldAlign;
Mike Stump11289f42009-09-09 15:08:12 +0000359
360 FieldPacked |= D->hasAttr<PackedAttr>();
361
Anders Carlsson79474332009-07-18 20:20:21 +0000362 if (const Expr *BitWidthExpr = D->getBitWidth()) {
363 // TODO: Need to check this algorithm on other targets!
364 // (tested on Linux-X86)
365 FieldSize = BitWidthExpr->EvaluateAsInt(Ctx).getZExtValue();
Mike Stump11289f42009-09-09 15:08:12 +0000366
Anders Carlsson79474332009-07-18 20:20:21 +0000367 std::pair<uint64_t, unsigned> FieldInfo = Ctx.getTypeInfo(D->getType());
368 uint64_t TypeSize = FieldInfo.first;
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000369
Anders Carlsson79474332009-07-18 20:20:21 +0000370 FieldAlign = FieldInfo.second;
Mike Stump11289f42009-09-09 15:08:12 +0000371
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000372 if (FieldPacked)
373 FieldAlign = 1;
Anders Carlsson79474332009-07-18 20:20:21 +0000374 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
375 FieldAlign = std::max(FieldAlign, AA->getAlignment());
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000376 // The maximum field alignment overrides the aligned attribute.
377 if (MaxFieldAlignment)
378 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
379
Anders Carlsson79474332009-07-18 20:20:21 +0000380 // Check if we need to add padding to give the field the correct
381 // alignment.
382 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
383 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
Mike Stump11289f42009-09-09 15:08:12 +0000384
Anders Carlsson79474332009-07-18 20:20:21 +0000385 // Padding members don't affect overall alignment
386 if (!D->getIdentifier())
387 FieldAlign = 1;
388 } else {
389 if (D->getType()->isIncompleteArrayType()) {
390 // This is a flexible array member; we can't directly
391 // query getTypeInfo about these, so we figure it out here.
392 // Flexible array members don't have any size, but they
393 // have to be aligned appropriately for their element type.
394 FieldSize = 0;
395 const ArrayType* ATy = Ctx.getAsArrayType(D->getType());
396 FieldAlign = Ctx.getTypeAlign(ATy->getElementType());
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000397 } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) {
Anders Carlsson79474332009-07-18 20:20:21 +0000398 unsigned AS = RT->getPointeeType().getAddressSpace();
399 FieldSize = Ctx.Target.getPointerWidth(AS);
400 FieldAlign = Ctx.Target.getPointerAlign(AS);
401 } else {
402 std::pair<uint64_t, unsigned> FieldInfo = Ctx.getTypeInfo(D->getType());
403 FieldSize = FieldInfo.first;
404 FieldAlign = FieldInfo.second;
405 }
Mike Stump11289f42009-09-09 15:08:12 +0000406
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000407 if (FieldPacked)
408 FieldAlign = 8;
Anders Carlsson79474332009-07-18 20:20:21 +0000409 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
410 FieldAlign = std::max(FieldAlign, AA->getAlignment());
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000411 // The maximum field alignment overrides the aligned attribute.
412 if (MaxFieldAlignment)
413 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
Mike Stump11289f42009-09-09 15:08:12 +0000414
Anders Carlsson79474332009-07-18 20:20:21 +0000415 // Round up the current record size to the field's alignment boundary.
416 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
417 }
Mike Stump11289f42009-09-09 15:08:12 +0000418
Anders Carlsson79474332009-07-18 20:20:21 +0000419 // Place this field at the current location.
420 FieldOffsets.push_back(FieldOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000421
Anders Carlsson79474332009-07-18 20:20:21 +0000422 // Reserve space for this field.
423 if (IsUnion)
424 Size = std::max(Size, FieldSize);
425 else
426 Size = FieldOffset + FieldSize;
Mike Stump11289f42009-09-09 15:08:12 +0000427
Anders Carlsson79474332009-07-18 20:20:21 +0000428 // Remember the next available offset.
429 NextOffset = Size;
Mike Stump11289f42009-09-09 15:08:12 +0000430
Anders Carlsson79474332009-07-18 20:20:21 +0000431 // Remember max struct/class alignment.
432 UpdateAlignment(FieldAlign);
433}
434
435void ASTRecordLayoutBuilder::FinishLayout() {
436 // In C++, records cannot be of size 0.
437 if (Ctx.getLangOptions().CPlusPlus && Size == 0)
438 Size = 8;
439 // Finally, round the size of the record up to the alignment of the
440 // record itself.
441 Size = (Size + (Alignment-1)) & ~(Alignment-1);
442}
443
444void ASTRecordLayoutBuilder::UpdateAlignment(unsigned NewAlignment) {
445 if (NewAlignment <= Alignment)
446 return;
Mike Stump11289f42009-09-09 15:08:12 +0000447
Anders Carlsson79474332009-07-18 20:20:21 +0000448 assert(llvm::isPowerOf2_32(NewAlignment && "Alignment not a power of 2"));
Mike Stump11289f42009-09-09 15:08:12 +0000449
Anders Carlsson79474332009-07-18 20:20:21 +0000450 Alignment = NewAlignment;
451}
Mike Stump11289f42009-09-09 15:08:12 +0000452
Anders Carlsson79474332009-07-18 20:20:21 +0000453const ASTRecordLayout *
Mike Stump11289f42009-09-09 15:08:12 +0000454ASTRecordLayoutBuilder::ComputeLayout(ASTContext &Ctx,
Anders Carlsson79474332009-07-18 20:20:21 +0000455 const RecordDecl *D) {
456 ASTRecordLayoutBuilder Builder(Ctx);
457
458 Builder.Layout(D);
459
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000460 if (!isa<CXXRecordDecl>(D))
461 return new ASTRecordLayout(Builder.Size, Builder.Alignment, Builder.Size,
Mike Stump11289f42009-09-09 15:08:12 +0000462 Builder.FieldOffsets.data(),
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000463 Builder.FieldOffsets.size());
Mike Stump11289f42009-09-09 15:08:12 +0000464
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000465 // FIXME: This is not always correct. See the part about bitfields at
466 // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info.
467 // FIXME: IsPODForThePurposeOfLayout should be stored in the record layout.
468 bool IsPODForThePurposeOfLayout = cast<CXXRecordDecl>(D)->isPOD();
Mike Stump11289f42009-09-09 15:08:12 +0000469
470 assert(Builder.Bases.size() == Builder.BaseOffsets.size() &&
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000471 "Base offsets vector must be same size as bases vector!");
Mike Stump11289f42009-09-09 15:08:12 +0000472 assert(Builder.VBases.size() == Builder.VBaseOffsets.size() &&
Mike Stumpbcf756c2009-08-14 01:44:03 +0000473 "Base offsets vector must be same size as bases vector!");
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000474
475 // FIXME: This should be done in FinalizeLayout.
Mike Stump11289f42009-09-09 15:08:12 +0000476 uint64_t DataSize =
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000477 IsPODForThePurposeOfLayout ? Builder.Size : Builder.NextOffset;
Mike Stump11289f42009-09-09 15:08:12 +0000478 uint64_t NonVirtualSize =
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000479 IsPODForThePurposeOfLayout ? DataSize : Builder.NonVirtualSize;
Mike Stump11289f42009-09-09 15:08:12 +0000480
Anders Carlsson6d9f6f32009-07-19 00:18:47 +0000481 return new ASTRecordLayout(Builder.Size, Builder.Alignment, DataSize,
Mike Stump11289f42009-09-09 15:08:12 +0000482 Builder.FieldOffsets.data(),
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000483 Builder.FieldOffsets.size(),
484 NonVirtualSize,
485 Builder.NonVirtualAlignment,
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000486 Builder.PrimaryBase,
Mike Stumpc266c6d2009-08-07 19:00:50 +0000487 Builder.PrimaryBaseWasVirtual,
Anders Carlssonfc8cfa82009-07-28 19:24:15 +0000488 Builder.Bases.data(),
489 Builder.BaseOffsets.data(),
Mike Stumpbcf756c2009-08-14 01:44:03 +0000490 Builder.Bases.size(),
491 Builder.VBases.data(),
492 Builder.VBaseOffsets.data(),
493 Builder.VBases.size());
Anders Carlsson4f516282009-07-18 20:50:59 +0000494}
495
496const ASTRecordLayout *
497ASTRecordLayoutBuilder::ComputeLayout(ASTContext &Ctx,
498 const ObjCInterfaceDecl *D,
499 const ObjCImplementationDecl *Impl) {
500 ASTRecordLayoutBuilder Builder(Ctx);
Mike Stump11289f42009-09-09 15:08:12 +0000501
Anders Carlsson4f516282009-07-18 20:50:59 +0000502 Builder.Layout(D, Impl);
Mike Stump11289f42009-09-09 15:08:12 +0000503
Anders Carlsson4f516282009-07-18 20:50:59 +0000504 return new ASTRecordLayout(Builder.Size, Builder.Alignment,
505 Builder.NextOffset,
Mike Stump11289f42009-09-09 15:08:12 +0000506 Builder.FieldOffsets.data(),
Anders Carlsson79474332009-07-18 20:20:21 +0000507 Builder.FieldOffsets.size());
508}