blob: e554aebf4032bc33a87b861cac95496bf056410f [file] [log] [blame]
Anders Carlsson9392fa62010-05-26 05:41:04 +00001//=== RecordLayoutBuilder.cpp - Helper class for building record layouts ---==//
Anders Carlssonbda4c102009-07-18 20:20:21 +00002//
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
Anders Carlssonbda4c102009-07-18 20:20:21 +000010#include "clang/AST/Attr.h"
11#include "clang/AST/Decl.h"
Anders Carlsson74cbe222009-07-19 00:18:47 +000012#include "clang/AST/DeclCXX.h"
Anders Carlsson93fab9d2009-07-18 20:50:59 +000013#include "clang/AST/DeclObjC.h"
Anders Carlssonbda4c102009-07-18 20:20:21 +000014#include "clang/AST/Expr.h"
Anders Carlsson9392fa62010-05-26 05:41:04 +000015#include "clang/AST/RecordLayout.h"
Anders Carlssonbda4c102009-07-18 20:20:21 +000016#include "clang/Basic/TargetInfo.h"
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +000017#include "llvm/Support/Format.h"
18#include "llvm/ADT/SmallSet.h"
19#include "llvm/Support/MathExtras.h"
Anders Carlsson9392fa62010-05-26 05:41:04 +000020#include <map>
Anders Carlssonbda4c102009-07-18 20:20:21 +000021
22using namespace clang;
23
Benjamin Kramer7e220282010-05-26 09:58:31 +000024namespace {
Anders Carlsson7d0918a2010-05-26 05:58:59 +000025class RecordLayoutBuilder {
Anders Carlsson9392fa62010-05-26 05:41:04 +000026 // FIXME: Remove this and make the appropriate fields public.
27 friend class clang::ASTContext;
28
29 ASTContext &Context;
30
31 /// Size - The current size of the record layout.
32 uint64_t Size;
33
34 /// Alignment - The current alignment of the record layout.
35 unsigned Alignment;
36
37 llvm::SmallVector<uint64_t, 16> FieldOffsets;
38
39 /// Packed - Whether the record is packed or not.
40 bool Packed;
41
42 /// UnfilledBitsInLastByte - If the last field laid out was a bitfield,
43 /// this contains the number of bits in the last byte that can be used for
44 /// an adjacent bitfield if necessary.
45 unsigned char UnfilledBitsInLastByte;
46
47 /// MaxFieldAlignment - The maximum allowed field alignment. This is set by
48 /// #pragma pack.
49 unsigned MaxFieldAlignment;
50
51 /// DataSize - The data size of the record being laid out.
52 uint64_t DataSize;
53
54 bool IsUnion;
55
56 uint64_t NonVirtualSize;
57 unsigned NonVirtualAlignment;
58
59 /// PrimaryBase - the primary base class (if one exists) of the class
60 /// we're laying out.
61 const CXXRecordDecl *PrimaryBase;
62
63 /// PrimaryBaseIsVirtual - Whether the primary base of the class we're laying
64 /// out is virtual.
65 bool PrimaryBaseIsVirtual;
66
67 typedef llvm::DenseMap<const CXXRecordDecl *, uint64_t> BaseOffsetsMapTy;
68
69 /// Bases - base classes and their offsets in the record.
70 BaseOffsetsMapTy Bases;
71
72 // VBases - virtual base classes and their offsets in the record.
73 BaseOffsetsMapTy VBases;
74
75 /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are
76 /// primary base classes for some other direct or indirect base class.
77 llvm::SmallSet<const CXXRecordDecl*, 32> IndirectPrimaryBases;
78
79 /// FirstNearlyEmptyVBase - The first nearly empty virtual base class in
80 /// inheritance graph order. Used for determining the primary base class.
81 const CXXRecordDecl *FirstNearlyEmptyVBase;
82
83 /// VisitedVirtualBases - A set of all the visited virtual bases, used to
84 /// avoid visiting virtual bases more than once.
85 llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases;
86
87 /// SizeOfLargestEmptySubobject - When laying out C++ classes, this holds the
88 /// size of the largest empty subobject (either a base or a member).
89 /// Will be zero if the record being built doesn't contain any empty classes.
90 uint64_t SizeOfLargestEmptySubobject;
91
92 /// EmptyClassOffsets - A map from offsets to empty record decls.
93 typedef std::multimap<uint64_t, const CXXRecordDecl *> EmptyClassOffsetsTy;
94 EmptyClassOffsetsTy EmptyClassOffsets;
95
Anders Carlsson7d0918a2010-05-26 05:58:59 +000096 RecordLayoutBuilder(ASTContext &Ctx);
Anders Carlsson9392fa62010-05-26 05:41:04 +000097
98 void Layout(const RecordDecl *D);
Anders Carlssonc6cab682010-05-26 15:10:00 +000099 void Layout(const CXXRecordDecl *D);
Anders Carlsson9392fa62010-05-26 05:41:04 +0000100 void Layout(const ObjCInterfaceDecl *D);
101
102 void LayoutFields(const RecordDecl *D);
103 void LayoutField(const FieldDecl *D);
104 void LayoutWideBitField(uint64_t FieldSize, uint64_t TypeSize);
105 void LayoutBitField(const FieldDecl *D);
106
107 /// ComputeEmptySubobjectSizes - Compute the size of the largest base or
108 /// member subobject that is empty.
109 void ComputeEmptySubobjectSizes(const CXXRecordDecl *RD);
110
111 /// DeterminePrimaryBase - Determine the primary base of the given class.
112 void DeterminePrimaryBase(const CXXRecordDecl *RD);
113
114 void SelectPrimaryVBase(const CXXRecordDecl *RD);
115
116 /// IdentifyPrimaryBases - Identify all virtual base classes, direct or
117 /// indirect, that are primary base classes for some other direct or indirect
118 /// base class.
119 void IdentifyPrimaryBases(const CXXRecordDecl *RD);
120
121 bool IsNearlyEmpty(const CXXRecordDecl *RD) const;
122
123 /// LayoutNonVirtualBases - Determines the primary base class (if any) and
124 /// lays it out. Will then proceed to lay out all non-virtual base clasess.
125 void LayoutNonVirtualBases(const CXXRecordDecl *RD);
126
127 /// LayoutNonVirtualBase - Lays out a single non-virtual base.
128 void LayoutNonVirtualBase(const CXXRecordDecl *RD);
129
130 void AddPrimaryVirtualBaseOffsets(const CXXRecordDecl *RD, uint64_t Offset,
131 const CXXRecordDecl *MostDerivedClass);
132
133 /// LayoutVirtualBases - Lays out all the virtual bases.
134 void LayoutVirtualBases(const CXXRecordDecl *RD,
135 const CXXRecordDecl *MostDerivedClass);
136
137 /// LayoutVirtualBase - Lays out a single virtual base.
138 void LayoutVirtualBase(const CXXRecordDecl *RD);
139
140 /// LayoutBase - Will lay out a base and return the offset where it was
141 /// placed, in bits.
142 uint64_t LayoutBase(const CXXRecordDecl *RD);
143
144 /// canPlaceRecordAtOffset - Return whether a record (either a base class
145 /// or a field) can be placed at the given offset.
146 /// Returns false if placing the record will result in two components
147 /// (direct or indirect) of the same type having the same offset.
148 bool canPlaceRecordAtOffset(const CXXRecordDecl *RD, uint64_t Offset,
149 bool CheckVBases) const;
150
151 /// canPlaceFieldAtOffset - Return whether a field can be placed at the given
152 /// offset.
153 bool canPlaceFieldAtOffset(const FieldDecl *FD, uint64_t Offset) const;
154
155 /// UpdateEmptyClassOffsets - Called after a record (either a base class
156 /// or a field) has been placed at the given offset. Will update the
157 /// EmptyClassOffsets map if the class is empty or has any empty bases or
158 /// fields.
159 void UpdateEmptyClassOffsets(const CXXRecordDecl *RD, uint64_t Offset,
160 bool UpdateVBases);
161
162 /// UpdateEmptyClassOffsets - Called after a field has been placed at the
163 /// given offset.
164 void UpdateEmptyClassOffsets(const FieldDecl *FD, uint64_t Offset);
165
Anders Carlssonc6cab682010-05-26 15:10:00 +0000166 /// InitializeLayout - Initialize record layout for the given record decl.
167 void InitializeLayout(const RecordDecl *D);
168
Anders Carlsson9392fa62010-05-26 05:41:04 +0000169 /// FinishLayout - Finalize record layout. Adjust record size based on the
170 /// alignment.
171 void FinishLayout();
172
173 void UpdateAlignment(unsigned NewAlignment);
174
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000175 RecordLayoutBuilder(const RecordLayoutBuilder&); // DO NOT IMPLEMENT
176 void operator=(const RecordLayoutBuilder&); // DO NOT IMPLEMENT
Anders Carlsson9392fa62010-05-26 05:41:04 +0000177public:
178 static const CXXMethodDecl *ComputeKeyFunction(const CXXRecordDecl *RD);
179};
Benjamin Kramer7e220282010-05-26 09:58:31 +0000180} // end anonymous namespace
Anders Carlsson9392fa62010-05-26 05:41:04 +0000181
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000182RecordLayoutBuilder::RecordLayoutBuilder(ASTContext &Context)
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000183 : Context(Context), Size(0), Alignment(8), Packed(false),
184 UnfilledBitsInLastByte(0), MaxFieldAlignment(0), DataSize(0), IsUnion(false),
Anders Carlsson48317102010-05-26 05:25:15 +0000185 NonVirtualSize(0), NonVirtualAlignment(8), PrimaryBase(0),
186 PrimaryBaseIsVirtual(false), FirstNearlyEmptyVBase(0),
Anders Carlsson836fc142010-05-08 23:06:26 +0000187 SizeOfLargestEmptySubobject(0) { }
Mike Stump3dee6ef2009-07-30 00:22:38 +0000188
Mike Stump6f376332009-08-05 22:37:18 +0000189/// IsNearlyEmpty - Indicates when a class has a vtable pointer, but
190/// no other data.
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000191bool RecordLayoutBuilder::IsNearlyEmpty(const CXXRecordDecl *RD) const {
Mike Stump6f376332009-08-05 22:37:18 +0000192 // FIXME: Audit the corners
193 if (!RD->isDynamicClass())
194 return false;
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000195 const ASTRecordLayout &BaseInfo = Context.getASTRecordLayout(RD);
196 if (BaseInfo.getNonVirtualSize() == Context.Target.getPointerWidth(0))
Mike Stump6f376332009-08-05 22:37:18 +0000197 return true;
198 return false;
199}
200
Anders Carlsson836fc142010-05-08 23:06:26 +0000201void
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000202RecordLayoutBuilder::ComputeEmptySubobjectSizes(const CXXRecordDecl *RD) {
Anders Carlsson836fc142010-05-08 23:06:26 +0000203 // Check the bases.
204 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
205 E = RD->bases_end(); I != E; ++I) {
206 const CXXRecordDecl *BaseDecl =
207 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
208
209 uint64_t EmptySize = 0;
210 const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl);
211 if (BaseDecl->isEmpty()) {
212 // If the class decl is empty, get its size.
213 EmptySize = Layout.getSize();
214 } else {
215 // Otherwise, we get the largest empty subobject for the decl.
216 EmptySize = Layout.getSizeOfLargestEmptySubobject();
217 }
218
219 SizeOfLargestEmptySubobject = std::max(SizeOfLargestEmptySubobject,
220 EmptySize);
221 }
222
223 // Check the fields.
224 for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
225 I != E; ++I) {
226 const FieldDecl *FD = *I;
227
228 const RecordType *RT =
229 Context.getBaseElementType(FD->getType())->getAs<RecordType>();
230
231 // We only care about record types.
232 if (!RT)
233 continue;
234
235 uint64_t EmptySize = 0;
236 const CXXRecordDecl *MemberDecl = cast<CXXRecordDecl>(RT->getDecl());
237 const ASTRecordLayout &Layout = Context.getASTRecordLayout(MemberDecl);
238 if (MemberDecl->isEmpty()) {
239 // If the class decl is empty, get its size.
240 EmptySize = Layout.getSize();
241 } else {
242 // Otherwise, we get the largest empty subobject for the decl.
243 EmptySize = Layout.getSizeOfLargestEmptySubobject();
244 }
245
246 SizeOfLargestEmptySubobject = std::max(SizeOfLargestEmptySubobject,
247 EmptySize);
248 }
249}
250
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000251void RecordLayoutBuilder::IdentifyPrimaryBases(const CXXRecordDecl *RD) {
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000252 const ASTRecordLayout::PrimaryBaseInfo &BaseInfo =
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000253 Context.getASTRecordLayout(RD).getPrimaryBaseInfo();
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000254
Anders Carlsson3f066522009-09-22 03:02:06 +0000255 // If the record has a primary base class that is virtual, add it to the set
256 // of primary bases.
Anders Carlsson261fba62009-11-27 22:14:40 +0000257 if (BaseInfo.isVirtual())
258 IndirectPrimaryBases.insert(BaseInfo.getBase());
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000259
Anders Carlsson3f066522009-09-22 03:02:06 +0000260 // Now traverse all bases and find primary bases for them.
Mike Stump6f376332009-08-05 22:37:18 +0000261 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000262 e = RD->bases_end(); i != e; ++i) {
Sebastian Redl9994a342009-10-25 17:03:50 +0000263 assert(!i->getType()->isDependentType() &&
264 "Cannot layout class with dependent bases.");
Mike Stump1eb44332009-09-09 15:08:12 +0000265 const CXXRecordDecl *Base =
Mike Stump49520942009-08-11 04:03:59 +0000266 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000267
Mike Stump49520942009-08-11 04:03:59 +0000268 // Only bases with virtual bases participate in computing the
269 // indirect primary virtual base classes.
Mike Stump4ef98092009-08-13 22:53:07 +0000270 if (Base->getNumVBases())
Anders Carlsson3f066522009-09-22 03:02:06 +0000271 IdentifyPrimaryBases(Base);
Mike Stump6f376332009-08-05 22:37:18 +0000272 }
273}
274
Anders Carlsson3f066522009-09-22 03:02:06 +0000275void
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000276RecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD) {
Anders Carlsson584e1df2010-03-11 03:39:12 +0000277 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000278 E = RD->bases_end(); I != E; ++I) {
Anders Carlsson584e1df2010-03-11 03:39:12 +0000279 assert(!I->getType()->isDependentType() &&
Sebastian Redl9994a342009-10-25 17:03:50 +0000280 "Cannot layout class with dependent bases.");
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000281
Mike Stump1eb44332009-09-09 15:08:12 +0000282 const CXXRecordDecl *Base =
Anders Carlsson584e1df2010-03-11 03:39:12 +0000283 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson200c5c22010-03-11 00:15:35 +0000284
Anders Carlsson584e1df2010-03-11 03:39:12 +0000285 // Check if this is a nearly empty virtual base.
286 if (I->isVirtual() && IsNearlyEmpty(Base)) {
287 // If it's not an indirect primary base, then we've found our primary
288 // base.
Anders Carlsson3f066522009-09-22 03:02:06 +0000289 if (!IndirectPrimaryBases.count(Base)) {
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000290 PrimaryBase = Base;
291 PrimaryBaseIsVirtual = true;
Mike Stumpd76264e2009-08-12 21:50:08 +0000292 return;
293 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000294
Anders Carlsson584e1df2010-03-11 03:39:12 +0000295 // Is this the first nearly empty virtual base?
296 if (!FirstNearlyEmptyVBase)
297 FirstNearlyEmptyVBase = Base;
Mike Stumpd76264e2009-08-12 21:50:08 +0000298 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000299
Anders Carlsson200c5c22010-03-11 00:15:35 +0000300 SelectPrimaryVBase(Base);
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000301 if (PrimaryBase)
Zhongxing Xu94ba3802010-02-15 04:28:35 +0000302 return;
Mike Stumpd76264e2009-08-12 21:50:08 +0000303 }
304}
305
Anders Carlsson200c5c22010-03-11 00:15:35 +0000306/// DeterminePrimaryBase - Determine the primary base of the given class.
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000307void RecordLayoutBuilder::DeterminePrimaryBase(const CXXRecordDecl *RD) {
Anders Carlsson200c5c22010-03-11 00:15:35 +0000308 // If the class isn't dynamic, it won't have a primary base.
309 if (!RD->isDynamicClass())
310 return;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000311
Anders Carlsson3f066522009-09-22 03:02:06 +0000312 // Compute all the primary virtual bases for all of our direct and
Mike Stump0880e752009-08-13 23:26:06 +0000313 // indirect bases, and record all their primary virtual base classes.
Mike Stump0880e752009-08-13 23:26:06 +0000314 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000315 e = RD->bases_end(); i != e; ++i) {
Sebastian Redl9994a342009-10-25 17:03:50 +0000316 assert(!i->getType()->isDependentType() &&
Anders Carlsson200c5c22010-03-11 00:15:35 +0000317 "Cannot lay out class with dependent bases.");
Mike Stump1eb44332009-09-09 15:08:12 +0000318 const CXXRecordDecl *Base =
Mike Stump0880e752009-08-13 23:26:06 +0000319 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson3f066522009-09-22 03:02:06 +0000320 IdentifyPrimaryBases(Base);
Mike Stump0880e752009-08-13 23:26:06 +0000321 }
322
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000323 // If the record has a dynamic base class, attempt to choose a primary base
324 // class. It is the first (in direct base class order) non-virtual dynamic
Anders Carlsson3f066522009-09-22 03:02:06 +0000325 // base class, if one exists.
Mike Stump6f376332009-08-05 22:37:18 +0000326 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000327 e = RD->bases_end(); i != e; ++i) {
Anders Carlssonce2009a2009-11-27 22:05:05 +0000328 // Ignore virtual bases.
329 if (i->isVirtual())
330 continue;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000331
Anders Carlssonce2009a2009-11-27 22:05:05 +0000332 const CXXRecordDecl *Base =
333 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
334
335 if (Base->isDynamicClass()) {
336 // We found it.
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000337 PrimaryBase = Base;
338 PrimaryBaseIsVirtual = false;
Anders Carlssonce2009a2009-11-27 22:05:05 +0000339 return;
Mike Stump6f376332009-08-05 22:37:18 +0000340 }
341 }
342
343 // Otherwise, it is the first nearly empty virtual base that is not an
Mike Stump49520942009-08-11 04:03:59 +0000344 // indirect primary virtual base class, if one exists.
Anders Carlsson200c5c22010-03-11 00:15:35 +0000345 if (RD->getNumVBases() != 0) {
346 SelectPrimaryVBase(RD);
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000347 if (PrimaryBase)
Anders Carlsson200c5c22010-03-11 00:15:35 +0000348 return;
349 }
Mike Stump6f376332009-08-05 22:37:18 +0000350
Anders Carlsson200c5c22010-03-11 00:15:35 +0000351 // Otherwise, it is the first nearly empty virtual base that is not an
352 // indirect primary virtual base class, if one exists.
353 if (FirstNearlyEmptyVBase) {
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000354 PrimaryBase = FirstNearlyEmptyVBase;
355 PrimaryBaseIsVirtual = true;
Mike Stump6f376332009-08-05 22:37:18 +0000356 return;
Anders Carlsson200c5c22010-03-11 00:15:35 +0000357 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000358
Anders Carlsson200c5c22010-03-11 00:15:35 +0000359 // Otherwise there is no primary base class.
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000360 assert(!PrimaryBase && "Should not get here with a primary base!");
Mike Stump6f376332009-08-05 22:37:18 +0000361
Anders Carlsson200c5c22010-03-11 00:15:35 +0000362 // Allocate the virtual table pointer at offset zero.
363 assert(DataSize == 0 && "Vtable pointer must be at offset zero!");
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000364
Anders Carlsson200c5c22010-03-11 00:15:35 +0000365 // Update the size.
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000366 Size += Context.Target.getPointerWidth(0);
Anders Carlsson200c5c22010-03-11 00:15:35 +0000367 DataSize = Size;
368
369 // Update the alignment.
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000370 UpdateAlignment(Context.Target.getPointerAlign(0));
Mike Stump6f376332009-08-05 22:37:18 +0000371}
372
Anders Carlssone239b9d2010-03-10 22:21:28 +0000373void
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000374RecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) {
Anders Carlsson200c5c22010-03-11 00:15:35 +0000375 // First, determine the primary base class.
376 DeterminePrimaryBase(RD);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000377
Anders Carlsson200c5c22010-03-11 00:15:35 +0000378 // If we have a primary base class, lay it out.
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000379 if (PrimaryBase) {
380 if (PrimaryBaseIsVirtual) {
Anders Carlsson200c5c22010-03-11 00:15:35 +0000381 // We have a virtual primary base, insert it as an indirect primary base.
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000382 IndirectPrimaryBases.insert(PrimaryBase);
Anders Carlsson37147ea2010-03-11 05:42:17 +0000383
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000384 assert(!VisitedVirtualBases.count(PrimaryBase) &&
385 "vbase already visited!");
386 VisitedVirtualBases.insert(PrimaryBase);
Anders Carlssonbdda6c12010-04-10 18:42:27 +0000387
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000388 LayoutVirtualBase(PrimaryBase);
Anders Carlsson200c5c22010-03-11 00:15:35 +0000389 } else
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000390 LayoutNonVirtualBase(PrimaryBase);
Anders Carlsson200c5c22010-03-11 00:15:35 +0000391 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000392
Anders Carlsson200c5c22010-03-11 00:15:35 +0000393 // Now lay out the non-virtual bases.
394 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000395 E = RD->bases_end(); I != E; ++I) {
Anders Carlsson200c5c22010-03-11 00:15:35 +0000396
397 // Ignore virtual bases.
398 if (I->isVirtual())
399 continue;
400
401 const CXXRecordDecl *Base =
402 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
403
404 // Skip the primary base.
Anders Carlssonaa230b72010-05-26 05:31:23 +0000405 if (Base == PrimaryBase && !PrimaryBaseIsVirtual)
Anders Carlsson200c5c22010-03-11 00:15:35 +0000406 continue;
407
408 // Lay out the base.
409 LayoutNonVirtualBase(Base);
Anders Carlssone239b9d2010-03-10 22:21:28 +0000410 }
411}
412
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000413void RecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *RD) {
Anders Carlssone3bdbee2010-03-10 22:26:24 +0000414 // Layout the base.
415 uint64_t Offset = LayoutBase(RD);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000416
Anders Carlssone3bdbee2010-03-10 22:26:24 +0000417 // Add its base class offset.
Anders Carlsson147b5dd2010-03-11 04:10:39 +0000418 if (!Bases.insert(std::make_pair(RD, Offset)).second)
419 assert(false && "Added same base offset more than once!");
Anders Carlssone239b9d2010-03-10 22:21:28 +0000420}
Mike Stump968db332009-11-05 04:02:15 +0000421
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000422void
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000423RecordLayoutBuilder::AddPrimaryVirtualBaseOffsets(const CXXRecordDecl *RD,
Anders Carlsson97913572010-04-15 16:12:58 +0000424 uint64_t Offset,
425 const CXXRecordDecl *MostDerivedClass) {
426 // We already have the offset for the primary base of the most derived class.
427 if (RD != MostDerivedClass) {
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000428 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlsson97913572010-04-15 16:12:58 +0000429 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
430
431 // If this is a primary virtual base and we haven't seen it before, add it.
432 if (PrimaryBase && Layout.getPrimaryBaseWasVirtual() &&
433 !VBases.count(PrimaryBase))
434 VBases.insert(std::make_pair(PrimaryBase, Offset));
435 }
436
437 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
438 E = RD->bases_end(); I != E; ++I) {
439 assert(!I->getType()->isDependentType() &&
440 "Cannot layout class with dependent bases.");
441
442 const CXXRecordDecl *BaseDecl =
443 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
444
445 if (!BaseDecl->getNumVBases()) {
446 // This base isn't interesting since it doesn't have any virtual bases.
447 continue;
448 }
449
450 // Compute the offset of this base.
451 uint64_t BaseOffset;
452
453 if (I->isVirtual()) {
454 // If we don't know this vbase yet, don't visit it. It will be visited
455 // later.
456 if (!VBases.count(BaseDecl)) {
457 continue;
458 }
459
460 // Check if we've already visited this base.
461 if (!VisitedVirtualBases.insert(BaseDecl))
462 continue;
463
464 // We want the vbase offset from the class we're currently laying out.
465 BaseOffset = VBases[BaseDecl];
466 } else if (RD == MostDerivedClass) {
467 // We want the base offset from the class we're currently laying out.
468 assert(Bases.count(BaseDecl) && "Did not find base!");
469 BaseOffset = Bases[BaseDecl];
470 } else {
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000471 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlsson97913572010-04-15 16:12:58 +0000472 BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl);
473 }
474
475 AddPrimaryVirtualBaseOffsets(BaseDecl, BaseOffset, MostDerivedClass);
476 }
477}
478
479void
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000480RecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
Anders Carlsson97913572010-04-15 16:12:58 +0000481 const CXXRecordDecl *MostDerivedClass) {
Anders Carlsson88f42962010-03-11 04:33:54 +0000482 const CXXRecordDecl *PrimaryBase;
Anders Carlssonbdda6c12010-04-10 18:42:27 +0000483 bool PrimaryBaseIsVirtual;
Anders Carlsson37147ea2010-03-11 05:42:17 +0000484
Anders Carlssonbdda6c12010-04-10 18:42:27 +0000485 if (MostDerivedClass == RD) {
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000486 PrimaryBase = this->PrimaryBase;
487 PrimaryBaseIsVirtual = this->PrimaryBaseIsVirtual;
Anders Carlssonbdda6c12010-04-10 18:42:27 +0000488 } else {
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000489 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlsson88f42962010-03-11 04:33:54 +0000490 PrimaryBase = Layout.getPrimaryBase();
Anders Carlssonbdda6c12010-04-10 18:42:27 +0000491 PrimaryBaseIsVirtual = Layout.getPrimaryBaseWasVirtual();
492 }
493
Anders Carlsson622e2472010-03-11 04:24:02 +0000494 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
495 E = RD->bases_end(); I != E; ++I) {
496 assert(!I->getType()->isDependentType() &&
Sebastian Redl9994a342009-10-25 17:03:50 +0000497 "Cannot layout class with dependent bases.");
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000498
Mike Stump1eb44332009-09-09 15:08:12 +0000499 const CXXRecordDecl *Base =
Anders Carlsson622e2472010-03-11 04:24:02 +0000500 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
501
502 if (I->isVirtual()) {
Anders Carlssonbdda6c12010-04-10 18:42:27 +0000503 if (PrimaryBase != Base || !PrimaryBaseIsVirtual) {
504 bool IndirectPrimaryBase = IndirectPrimaryBases.count(Base);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000505
Anders Carlssonbdda6c12010-04-10 18:42:27 +0000506 // Only lay out the virtual base if it's not an indirect primary base.
507 if (!IndirectPrimaryBase) {
508 // Only visit virtual bases once.
509 if (!VisitedVirtualBases.insert(Base))
510 continue;
511
Anders Carlsson37147ea2010-03-11 05:42:17 +0000512 LayoutVirtualBase(Base);
Anders Carlsson147b5dd2010-03-11 04:10:39 +0000513 }
Mike Stump968db332009-11-05 04:02:15 +0000514 }
Mike Stump4ef98092009-08-13 22:53:07 +0000515 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000516
Anders Carlsson622e2472010-03-11 04:24:02 +0000517 if (!Base->getNumVBases()) {
518 // This base isn't interesting since it doesn't have any virtual bases.
519 continue;
Mike Stumpfe3010d2009-08-16 19:04:13 +0000520 }
Anders Carlsson622e2472010-03-11 04:24:02 +0000521
Anders Carlsson97913572010-04-15 16:12:58 +0000522 LayoutVirtualBases(Base, MostDerivedClass);
Mike Stumpeb19fa92009-08-06 13:41:24 +0000523 }
524}
525
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000526void RecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *RD) {
Anders Carlssone3bdbee2010-03-10 22:26:24 +0000527 // Layout the base.
528 uint64_t Offset = LayoutBase(RD);
529
530 // Add its base class offset.
Anders Carlsson147b5dd2010-03-11 04:10:39 +0000531 if (!VBases.insert(std::make_pair(RD, Offset)).second)
532 assert(false && "Added same vbase offset more than once!");
Anders Carlssone239b9d2010-03-10 22:21:28 +0000533}
534
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000535uint64_t RecordLayoutBuilder::LayoutBase(const CXXRecordDecl *RD) {
Anders Carlssonc3fddeb2010-05-08 22:35:05 +0000536 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlssone239b9d2010-03-10 22:21:28 +0000537
538 // If we have an empty base class, try to place it at offset 0.
Anders Carlsson3159ffe2010-05-10 15:26:14 +0000539 if (RD->isEmpty() && canPlaceRecordAtOffset(RD, 0, /*CheckVBases=*/false)) {
Anders Carlssone239b9d2010-03-10 22:21:28 +0000540 // We were able to place the class at offset 0.
Anders Carlssonecafebe2010-05-10 15:28:59 +0000541 UpdateEmptyClassOffsets(RD, 0, /*UpdateVBases=*/false);
Anders Carlssone239b9d2010-03-10 22:21:28 +0000542
Anders Carlssonc3fddeb2010-05-08 22:35:05 +0000543 Size = std::max(Size, Layout.getSize());
Anders Carlssone239b9d2010-03-10 22:21:28 +0000544
545 return 0;
546 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000547
Anders Carlssonc3fddeb2010-05-08 22:35:05 +0000548 unsigned BaseAlign = Layout.getNonVirtualAlign();
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000549
Anders Carlssone239b9d2010-03-10 22:21:28 +0000550 // Round up the current record size to the base's alignment boundary.
551 uint64_t Offset = llvm::RoundUpToAlignment(DataSize, BaseAlign);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000552
Anders Carlssone239b9d2010-03-10 22:21:28 +0000553 // Try to place the base.
554 while (true) {
Anders Carlsson3159ffe2010-05-10 15:26:14 +0000555 if (canPlaceRecordAtOffset(RD, Offset, /*CheckVBases=*/false))
Anders Carlssone239b9d2010-03-10 22:21:28 +0000556 break;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000557
Anders Carlssone239b9d2010-03-10 22:21:28 +0000558 Offset += BaseAlign;
559 }
560
561 if (!RD->isEmpty()) {
562 // Update the data size.
Anders Carlssonc3fddeb2010-05-08 22:35:05 +0000563 DataSize = Offset + Layout.getNonVirtualSize();
Anders Carlssone239b9d2010-03-10 22:21:28 +0000564
565 Size = std::max(Size, DataSize);
566 } else
Anders Carlssonc3fddeb2010-05-08 22:35:05 +0000567 Size = std::max(Size, Offset + Layout.getSize());
Anders Carlssone239b9d2010-03-10 22:21:28 +0000568
569 // Remember max struct/class alignment.
570 UpdateAlignment(BaseAlign);
571
Anders Carlssonecafebe2010-05-10 15:28:59 +0000572 UpdateEmptyClassOffsets(RD, Offset, /*UpdateVBases=*/false);
Anders Carlssone239b9d2010-03-10 22:21:28 +0000573 return Offset;
574}
575
Anders Carlsson3159ffe2010-05-10 15:26:14 +0000576bool
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000577RecordLayoutBuilder::canPlaceRecordAtOffset(const CXXRecordDecl *RD,
Anders Carlsson3159ffe2010-05-10 15:26:14 +0000578 uint64_t Offset,
579 bool CheckVBases) const {
Anders Carlsson1345bd22009-09-24 03:22:10 +0000580 // Look for an empty class with the same type at the same offset.
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000581 for (EmptyClassOffsetsTy::const_iterator I =
582 EmptyClassOffsets.lower_bound(Offset),
583 E = EmptyClassOffsets.upper_bound(Offset); I != E; ++I) {
584
Anders Carlsson1345bd22009-09-24 03:22:10 +0000585 if (I->second == RD)
586 return false;
587 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000588
Anders Carlssonddae8772010-05-09 05:03:38 +0000589 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlssonffbdefc2009-09-24 05:21:31 +0000590
591 // Check bases.
592 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000593 E = RD->bases_end(); I != E; ++I) {
Sebastian Redl9994a342009-10-25 17:03:50 +0000594 assert(!I->getType()->isDependentType() &&
595 "Cannot layout class with dependent bases.");
Anders Carlssonffbdefc2009-09-24 05:21:31 +0000596 if (I->isVirtual())
597 continue;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000598
Anders Carlssonddae8772010-05-09 05:03:38 +0000599 const CXXRecordDecl *BaseDecl =
Anders Carlssonffbdefc2009-09-24 05:21:31 +0000600 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
601
Anders Carlssonddae8772010-05-09 05:03:38 +0000602 uint64_t BaseOffset = Layout.getBaseClassOffset(BaseDecl);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000603
Anders Carlsson3159ffe2010-05-10 15:26:14 +0000604 if (!canPlaceRecordAtOffset(BaseDecl, Offset + BaseOffset,
605 /*CheckVBases=*/false))
Anders Carlssonffbdefc2009-09-24 05:21:31 +0000606 return false;
607 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000608
Anders Carlsson1eca99b2009-09-25 15:39:00 +0000609 // Check fields.
610 unsigned FieldNo = 0;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000611 for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
Anders Carlsson1eca99b2009-09-25 15:39:00 +0000612 I != E; ++I, ++FieldNo) {
613 const FieldDecl *FD = *I;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000614
Anders Carlssonddae8772010-05-09 05:03:38 +0000615 uint64_t FieldOffset = Layout.getFieldOffset(FieldNo);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000616
Anders Carlsson1eca99b2009-09-25 15:39:00 +0000617 if (!canPlaceFieldAtOffset(FD, Offset + FieldOffset))
618 return false;
619 }
620
Anders Carlsson3159ffe2010-05-10 15:26:14 +0000621 if (CheckVBases) {
622 // FIXME: virtual bases.
623 }
624
Anders Carlsson96061492009-09-24 03:13:30 +0000625 return true;
626}
627
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000628bool RecordLayoutBuilder::canPlaceFieldAtOffset(const FieldDecl *FD,
Anders Carlsson60265042009-09-25 00:02:51 +0000629 uint64_t Offset) const {
Anders Carlssonfbbce492009-09-25 01:23:32 +0000630 QualType T = FD->getType();
631 if (const RecordType *RT = T->getAs<RecordType>()) {
Anders Carlsson60265042009-09-25 00:02:51 +0000632 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
Anders Carlsson3159ffe2010-05-10 15:26:14 +0000633 return canPlaceRecordAtOffset(RD, Offset, /*CheckVBases=*/true);
Anders Carlsson60265042009-09-25 00:02:51 +0000634 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000635
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000636 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) {
637 QualType ElemTy = Context.getBaseElementType(AT);
Anders Carlssonfbbce492009-09-25 01:23:32 +0000638 const RecordType *RT = ElemTy->getAs<RecordType>();
639 if (!RT)
640 return true;
641 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
642 if (!RD)
643 return true;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000644
Anders Carlssonc3fddeb2010-05-08 22:35:05 +0000645 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlssonfbbce492009-09-25 01:23:32 +0000646
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000647 uint64_t NumElements = Context.getConstantArrayElementCount(AT);
Mike Stump968db332009-11-05 04:02:15 +0000648 uint64_t ElementOffset = Offset;
Anders Carlssonfbbce492009-09-25 01:23:32 +0000649 for (uint64_t I = 0; I != NumElements; ++I) {
Anders Carlsson3159ffe2010-05-10 15:26:14 +0000650 if (!canPlaceRecordAtOffset(RD, ElementOffset, /*CheckVBases=*/true))
Anders Carlssonfbbce492009-09-25 01:23:32 +0000651 return false;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000652
Anders Carlssonc3fddeb2010-05-08 22:35:05 +0000653 ElementOffset += Layout.getSize();
Anders Carlssonfbbce492009-09-25 01:23:32 +0000654 }
655 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000656
Anders Carlsson60265042009-09-25 00:02:51 +0000657 return true;
658}
659
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000660void RecordLayoutBuilder::UpdateEmptyClassOffsets(const CXXRecordDecl *RD,
Anders Carlssonecafebe2010-05-10 15:28:59 +0000661 uint64_t Offset,
662 bool UpdateVBases) {
Anders Carlsson1345bd22009-09-24 03:22:10 +0000663 if (RD->isEmpty())
664 EmptyClassOffsets.insert(std::make_pair(Offset, RD));
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000665
Anders Carlssonc3fddeb2010-05-08 22:35:05 +0000666 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlssonffbdefc2009-09-24 05:21:31 +0000667
668 // Update bases.
669 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000670 E = RD->bases_end(); I != E; ++I) {
Sebastian Redl9994a342009-10-25 17:03:50 +0000671 assert(!I->getType()->isDependentType() &&
672 "Cannot layout class with dependent bases.");
Anders Carlssonffbdefc2009-09-24 05:21:31 +0000673 if (I->isVirtual())
674 continue;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000675
Anders Carlssonffbdefc2009-09-24 05:21:31 +0000676 const CXXRecordDecl *Base =
677 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000678
Anders Carlssonc3fddeb2010-05-08 22:35:05 +0000679 uint64_t BaseClassOffset = Layout.getBaseClassOffset(Base);
Anders Carlssonecafebe2010-05-10 15:28:59 +0000680 UpdateEmptyClassOffsets(Base, Offset + BaseClassOffset,
681 /*UpdateVBases=*/false);
Anders Carlssonffbdefc2009-09-24 05:21:31 +0000682 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000683
Anders Carlsson1eca99b2009-09-25 15:39:00 +0000684 // Update fields.
685 unsigned FieldNo = 0;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000686 for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
Anders Carlsson1eca99b2009-09-25 15:39:00 +0000687 I != E; ++I, ++FieldNo) {
688 const FieldDecl *FD = *I;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000689
Anders Carlssonc3fddeb2010-05-08 22:35:05 +0000690 uint64_t FieldOffset = Layout.getFieldOffset(FieldNo);
Anders Carlsson1eca99b2009-09-25 15:39:00 +0000691 UpdateEmptyClassOffsets(FD, Offset + FieldOffset);
692 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000693
Anders Carlssonbfcdc402010-05-23 18:14:24 +0000694 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
695
Anders Carlssonecafebe2010-05-10 15:28:59 +0000696 if (UpdateVBases) {
697 // FIXME: Update virtual bases.
Anders Carlssonbfcdc402010-05-23 18:14:24 +0000698 } else if (PrimaryBase && Layout.getPrimaryBaseWasVirtual()) {
699 // We always want to update the offsets of a primary virtual base.
700 assert(Layout.getVBaseClassOffset(PrimaryBase) == 0 &&
701 "primary base class offset must always be 0!");
702 UpdateEmptyClassOffsets(PrimaryBase, Offset, /*UpdateVBases=*/false);
Anders Carlssonecafebe2010-05-10 15:28:59 +0000703 }
Anders Carlsson96061492009-09-24 03:13:30 +0000704}
705
Anders Carlssona4c60812009-09-25 01:54:38 +0000706void
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000707RecordLayoutBuilder::UpdateEmptyClassOffsets(const FieldDecl *FD,
Anders Carlssona4c60812009-09-25 01:54:38 +0000708 uint64_t Offset) {
709 QualType T = FD->getType();
710
711 if (const RecordType *RT = T->getAs<RecordType>()) {
712 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
Anders Carlssonecafebe2010-05-10 15:28:59 +0000713 UpdateEmptyClassOffsets(RD, Offset, /*UpdateVBases=*/true);
Anders Carlssona4c60812009-09-25 01:54:38 +0000714 return;
715 }
716 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000717
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000718 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) {
719 QualType ElemTy = Context.getBaseElementType(AT);
Anders Carlssona4c60812009-09-25 01:54:38 +0000720 const RecordType *RT = ElemTy->getAs<RecordType>();
721 if (!RT)
722 return;
723 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
724 if (!RD)
725 return;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000726
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000727 const ASTRecordLayout &Info = Context.getASTRecordLayout(RD);
Anders Carlssona4c60812009-09-25 01:54:38 +0000728
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000729 uint64_t NumElements = Context.getConstantArrayElementCount(AT);
Mike Stump968db332009-11-05 04:02:15 +0000730 uint64_t ElementOffset = Offset;
Anders Carlssona4c60812009-09-25 01:54:38 +0000731
732 for (uint64_t I = 0; I != NumElements; ++I) {
Anders Carlssonecafebe2010-05-10 15:28:59 +0000733 UpdateEmptyClassOffsets(RD, ElementOffset, /*UpdateVBases=*/true);
Anders Carlssona4c60812009-09-25 01:54:38 +0000734 ElementOffset += Info.getSize();
735 }
736 }
737}
738
Anders Carlssonc6cab682010-05-26 15:10:00 +0000739void RecordLayoutBuilder::InitializeLayout(const RecordDecl *D) {
Anders Carlssonbda4c102009-07-18 20:20:21 +0000740 IsUnion = D->isUnion();
Anders Carlssonc6cab682010-05-26 15:10:00 +0000741
Anders Carlssona5dd7222009-08-08 19:38:24 +0000742 Packed = D->hasAttr<PackedAttr>();
Anders Carlssonc6cab682010-05-26 15:10:00 +0000743
Anders Carlssona5dd7222009-08-08 19:38:24 +0000744 // The #pragma pack attribute specifies the maximum field alignment.
Anders Carlssona860e752009-08-08 18:23:56 +0000745 if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>())
Anders Carlssona5dd7222009-08-08 19:38:24 +0000746 MaxFieldAlignment = PPA->getAlignment();
Anders Carlssonc6cab682010-05-26 15:10:00 +0000747
Anders Carlssonbda4c102009-07-18 20:20:21 +0000748 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Sean Huntbbd37c62009-11-21 08:43:09 +0000749 UpdateAlignment(AA->getMaxAlignment());
Anders Carlssonc6cab682010-05-26 15:10:00 +0000750}
Anders Carlsson74cbe222009-07-19 00:18:47 +0000751
Anders Carlssonc6cab682010-05-26 15:10:00 +0000752void RecordLayoutBuilder::Layout(const RecordDecl *D) {
753 InitializeLayout(D);
Anders Carlssona2df41c2009-07-18 21:48:39 +0000754 LayoutFields(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Anders Carlssonbda4c102009-07-18 20:20:21 +0000756 // Finally, round the size of the total struct up to the alignment of the
757 // struct itself.
758 FinishLayout();
Anders Carlssonc6cab682010-05-26 15:10:00 +0000759}
760
761void RecordLayoutBuilder::Layout(const CXXRecordDecl *RD) {
762 InitializeLayout(RD);
763
764 ComputeEmptySubobjectSizes(RD);
765
766 // Lay out the vtable and the non-virtual bases.
767 LayoutNonVirtualBases(RD);
768
769 LayoutFields(RD);
770
771 NonVirtualSize = Size;
772 NonVirtualAlignment = Alignment;
773
774 // Lay out the virtual bases and add the primary virtual base offsets.
775 LayoutVirtualBases(RD, RD);
776
777 VisitedVirtualBases.clear();
778 AddPrimaryVirtualBaseOffsets(RD, 0, RD);
779
780 // Finally, round the size of the total struct up to the alignment of the
781 // struct itself.
782 FinishLayout();
783
Anders Carlssona1e87162010-04-10 21:24:48 +0000784#ifndef NDEBUG
Anders Carlssonc6cab682010-05-26 15:10:00 +0000785 // Check that we have base offsets for all bases.
786 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
787 E = RD->bases_end(); I != E; ++I) {
788 if (I->isVirtual())
789 continue;
Anders Carlssona1e87162010-04-10 21:24:48 +0000790
Anders Carlssonc6cab682010-05-26 15:10:00 +0000791 const CXXRecordDecl *BaseDecl =
792 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
793
794 assert(Bases.count(BaseDecl) && "Did not find base offset!");
795 }
796
797 // And all virtual bases.
798 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
799 E = RD->vbases_end(); I != E; ++I) {
800 const CXXRecordDecl *BaseDecl =
801 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
Anders Carlssona1e87162010-04-10 21:24:48 +0000802
Anders Carlssonc6cab682010-05-26 15:10:00 +0000803 assert(VBases.count(BaseDecl) && "Did not find base offset!");
Anders Carlssona1e87162010-04-10 21:24:48 +0000804 }
805#endif
Anders Carlssonbda4c102009-07-18 20:20:21 +0000806}
807
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000808void RecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D) {
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000809 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000810 const ASTRecordLayout &SL = Context.getASTObjCInterfaceLayout(SD);
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000811
812 UpdateAlignment(SL.getAlignment());
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000814 // We start laying out ivars not at the end of the superclass
815 // structure, but at the next byte following the last field.
Anders Carlsson243a6852009-07-18 21:26:44 +0000816 Size = llvm::RoundUpToAlignment(SL.getDataSize(), 8);
Anders Carlssona2239352009-09-26 01:34:51 +0000817 DataSize = Size;
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000818 }
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Anders Carlssona5dd7222009-08-08 19:38:24 +0000820 Packed = D->hasAttr<PackedAttr>();
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Anders Carlssona5dd7222009-08-08 19:38:24 +0000822 // The #pragma pack attribute specifies the maximum field alignment.
823 if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>())
824 MaxFieldAlignment = PPA->getAlignment();
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000826 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Sean Huntbbd37c62009-11-21 08:43:09 +0000827 UpdateAlignment(AA->getMaxAlignment());
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000828 // Layout each ivar sequentially.
829 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000830 Context.ShallowCollectObjCIvars(D, Ivars);
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000831 for (unsigned i = 0, e = Ivars.size(); i != e; ++i)
832 LayoutField(Ivars[i]);
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000834 // Finally, round the size of the total struct up to the alignment of the
835 // struct itself.
836 FinishLayout();
837}
838
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000839void RecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
Anders Carlssona2df41c2009-07-18 21:48:39 +0000840 // Layout each field, for now, just sequentially, respecting alignment. In
841 // the future, this will need to be tweakable by targets.
Mike Stump1eb44332009-09-09 15:08:12 +0000842 for (RecordDecl::field_iterator Field = D->field_begin(),
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000843 FieldEnd = D->field_end(); Field != FieldEnd; ++Field)
Anders Carlssona2df41c2009-07-18 21:48:39 +0000844 LayoutField(*Field);
845}
846
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000847void RecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize,
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +0000848 uint64_t TypeSize) {
849 assert(Context.getLangOptions().CPlusPlus &&
850 "Can only have wide bit-fields in C++!");
851
852 // Itanium C++ ABI 2.4:
853 // If sizeof(T)*8 < n, let T' be the largest integral POD type with
854 // sizeof(T')*8 <= n.
855
856 QualType IntegralPODTypes[] = {
857 Context.UnsignedCharTy, Context.UnsignedShortTy, Context.UnsignedIntTy,
858 Context.UnsignedLongTy, Context.UnsignedLongLongTy
859 };
860
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +0000861 QualType Type;
862 for (unsigned I = 0, E = llvm::array_lengthof(IntegralPODTypes);
863 I != E; ++I) {
864 uint64_t Size = Context.getTypeSize(IntegralPODTypes[I]);
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +0000865
866 if (Size > FieldSize)
867 break;
868
869 Type = IntegralPODTypes[I];
870 }
871 assert(!Type.isNull() && "Did not find a type!");
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +0000872
873 unsigned TypeAlign = Context.getTypeAlign(Type);
874
875 // We're not going to use any of the unfilled bits in the last byte.
876 UnfilledBitsInLastByte = 0;
877
Anders Carlssonde9f1532010-04-17 20:21:41 +0000878 uint64_t FieldOffset;
879
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +0000880 if (IsUnion) {
881 DataSize = std::max(DataSize, FieldSize);
Anders Carlssonde9f1532010-04-17 20:21:41 +0000882 FieldOffset = 0;
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +0000883 } else {
Anders Carlssonde9f1532010-04-17 20:21:41 +0000884 // The bitfield is allocated starting at the next offset aligned appropriately
885 // for T', with length n bits.
886 FieldOffset = llvm::RoundUpToAlignment(DataSize, TypeAlign);
887
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +0000888 uint64_t NewSizeInBits = FieldOffset + FieldSize;
889
890 DataSize = llvm::RoundUpToAlignment(NewSizeInBits, 8);
891 UnfilledBitsInLastByte = DataSize - NewSizeInBits;
892 }
893
894 // Place this field at the current location.
895 FieldOffsets.push_back(FieldOffset);
896
897 // Update the size.
898 Size = std::max(Size, DataSize);
899
900 // Remember max struct/class alignment.
901 UpdateAlignment(TypeAlign);
902}
903
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000904void RecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
Anders Carlsson42dbcc42009-11-22 17:37:31 +0000905 bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
Anders Carlssone4fc0d92009-11-22 19:13:51 +0000906 uint64_t FieldOffset = IsUnion ? 0 : (DataSize - UnfilledBitsInLastByte);
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000907 uint64_t FieldSize = D->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000908
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000909 std::pair<uint64_t, unsigned> FieldInfo = Context.getTypeInfo(D->getType());
Anders Carlsson42dbcc42009-11-22 17:37:31 +0000910 uint64_t TypeSize = FieldInfo.first;
911 unsigned FieldAlign = FieldInfo.second;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000912
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +0000913 if (FieldSize > TypeSize) {
914 LayoutWideBitField(FieldSize, TypeSize);
915 return;
916 }
917
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000918 if (FieldPacked || !Context.Target.useBitFieldTypeAlignment())
Anders Carlsson42dbcc42009-11-22 17:37:31 +0000919 FieldAlign = 1;
920 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
921 FieldAlign = std::max(FieldAlign, AA->getMaxAlignment());
922
923 // The maximum field alignment overrides the aligned attribute.
924 if (MaxFieldAlignment)
925 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000926
Daniel Dunbarb6a16932010-04-15 06:18:39 +0000927 // Check if we need to add padding to give the field the correct alignment.
Anders Carlsson42dbcc42009-11-22 17:37:31 +0000928 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
929 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000930
Daniel Dunbarb6a16932010-04-15 06:18:39 +0000931 // Padding members don't affect overall alignment.
Anders Carlsson42dbcc42009-11-22 17:37:31 +0000932 if (!D->getIdentifier())
933 FieldAlign = 1;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000934
Anders Carlsson42dbcc42009-11-22 17:37:31 +0000935 // Place this field at the current location.
936 FieldOffsets.push_back(FieldOffset);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000937
Anders Carlssone4fc0d92009-11-22 19:13:51 +0000938 // Update DataSize to include the last byte containing (part of) the bitfield.
939 if (IsUnion) {
940 // FIXME: I think FieldSize should be TypeSize here.
941 DataSize = std::max(DataSize, FieldSize);
942 } else {
943 uint64_t NewSizeInBits = FieldOffset + FieldSize;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000944
Anders Carlssone4fc0d92009-11-22 19:13:51 +0000945 DataSize = llvm::RoundUpToAlignment(NewSizeInBits, 8);
946 UnfilledBitsInLastByte = DataSize - NewSizeInBits;
947 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000948
Anders Carlssone4fc0d92009-11-22 19:13:51 +0000949 // Update the size.
950 Size = std::max(Size, DataSize);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000951
Anders Carlsson42dbcc42009-11-22 17:37:31 +0000952 // Remember max struct/class alignment.
953 UpdateAlignment(FieldAlign);
954}
955
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000956void RecordLayoutBuilder::LayoutField(const FieldDecl *D) {
Anders Carlsson42dbcc42009-11-22 17:37:31 +0000957 if (D->isBitField()) {
958 LayoutBitField(D);
959 return;
960 }
961
Anders Carlssone4fc0d92009-11-22 19:13:51 +0000962 // Reset the unfilled bits.
963 UnfilledBitsInLastByte = 0;
964
Anders Carlsson42dbcc42009-11-22 17:37:31 +0000965 bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
Anders Carlssona2239352009-09-26 01:34:51 +0000966 uint64_t FieldOffset = IsUnion ? 0 : DataSize;
Anders Carlssonbda4c102009-07-18 20:20:21 +0000967 uint64_t FieldSize;
968 unsigned FieldAlign;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000969
Anders Carlsson42dbcc42009-11-22 17:37:31 +0000970 if (D->getType()->isIncompleteArrayType()) {
971 // This is a flexible array member; we can't directly
972 // query getTypeInfo about these, so we figure it out here.
973 // Flexible array members don't have any size, but they
974 // have to be aligned appropriately for their element type.
975 FieldSize = 0;
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000976 const ArrayType* ATy = Context.getAsArrayType(D->getType());
977 FieldAlign = Context.getTypeAlign(ATy->getElementType());
Anders Carlsson42dbcc42009-11-22 17:37:31 +0000978 } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) {
979 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000980 FieldSize = Context.Target.getPointerWidth(AS);
981 FieldAlign = Context.Target.getPointerAlign(AS);
Anders Carlssonbda4c102009-07-18 20:20:21 +0000982 } else {
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000983 std::pair<uint64_t, unsigned> FieldInfo = Context.getTypeInfo(D->getType());
Anders Carlsson42dbcc42009-11-22 17:37:31 +0000984 FieldSize = FieldInfo.first;
985 FieldAlign = FieldInfo.second;
Anders Carlssonbda4c102009-07-18 20:20:21 +0000986 }
Mike Stump1eb44332009-09-09 15:08:12 +0000987
Anders Carlsson42dbcc42009-11-22 17:37:31 +0000988 if (FieldPacked)
989 FieldAlign = 8;
990 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
991 FieldAlign = std::max(FieldAlign, AA->getMaxAlignment());
992
993 // The maximum field alignment overrides the aligned attribute.
994 if (MaxFieldAlignment)
995 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
996
997 // Round up the current record size to the field's alignment boundary.
998 FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000999
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001000 if (!IsUnion) {
1001 while (true) {
1002 // Check if we can place the field at this offset.
1003 if (canPlaceFieldAtOffset(D, FieldOffset))
1004 break;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001005
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001006 // We couldn't place the field at the offset. Try again at a new offset.
1007 FieldOffset += FieldAlign;
1008 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001009
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001010 UpdateEmptyClassOffsets(D, FieldOffset);
1011 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001012
Anders Carlssonbda4c102009-07-18 20:20:21 +00001013 // Place this field at the current location.
1014 FieldOffsets.push_back(FieldOffset);
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Anders Carlssonbda4c102009-07-18 20:20:21 +00001016 // Reserve space for this field.
1017 if (IsUnion)
1018 Size = std::max(Size, FieldSize);
1019 else
1020 Size = FieldOffset + FieldSize;
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Anders Carlssona2239352009-09-26 01:34:51 +00001022 // Update the data size.
1023 DataSize = Size;
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Anders Carlssonbda4c102009-07-18 20:20:21 +00001025 // Remember max struct/class alignment.
1026 UpdateAlignment(FieldAlign);
1027}
1028
Anders Carlsson7d0918a2010-05-26 05:58:59 +00001029void RecordLayoutBuilder::FinishLayout() {
Anders Carlssonbda4c102009-07-18 20:20:21 +00001030 // In C++, records cannot be of size 0.
Anders Carlsson0f0e9b02010-04-16 15:07:51 +00001031 if (Context.getLangOptions().CPlusPlus && Size == 0)
Anders Carlssonbda4c102009-07-18 20:20:21 +00001032 Size = 8;
1033 // Finally, round the size of the record up to the alignment of the
1034 // record itself.
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001035 Size = llvm::RoundUpToAlignment(Size, Alignment);
Anders Carlssonbda4c102009-07-18 20:20:21 +00001036}
1037
Anders Carlsson7d0918a2010-05-26 05:58:59 +00001038void RecordLayoutBuilder::UpdateAlignment(unsigned NewAlignment) {
Anders Carlssonbda4c102009-07-18 20:20:21 +00001039 if (NewAlignment <= Alignment)
1040 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001041
Anders Carlssonbda4c102009-07-18 20:20:21 +00001042 assert(llvm::isPowerOf2_32(NewAlignment && "Alignment not a power of 2"));
Mike Stump1eb44332009-09-09 15:08:12 +00001043
Anders Carlssonbda4c102009-07-18 20:20:21 +00001044 Alignment = NewAlignment;
1045}
Mike Stump1eb44332009-09-09 15:08:12 +00001046
Anders Carlssonf53df232009-12-07 04:35:11 +00001047const CXXMethodDecl *
Anders Carlsson7d0918a2010-05-26 05:58:59 +00001048RecordLayoutBuilder::ComputeKeyFunction(const CXXRecordDecl *RD) {
Anders Carlssonf53df232009-12-07 04:35:11 +00001049 assert(RD->isDynamicClass() && "Class does not have any virtual methods!");
1050
Daniel Dunbar8d8ab742010-04-19 20:44:53 +00001051 // If a class isn't polymorphic it doesn't have a key function.
Anders Carlssonf53df232009-12-07 04:35:11 +00001052 if (!RD->isPolymorphic())
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00001053 return 0;
Eli Friedman61eab882009-12-08 03:56:49 +00001054
1055 // A class inside an anonymous namespace doesn't have a key function. (Or
1056 // at least, there's no point to assigning a key function to such a class;
1057 // this doesn't affect the ABI.)
1058 if (RD->isInAnonymousNamespace())
1059 return 0;
1060
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001061 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
1062 E = RD->method_end(); I != E; ++I) {
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00001063 const CXXMethodDecl *MD = *I;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001064
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00001065 if (!MD->isVirtual())
1066 continue;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001067
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00001068 if (MD->isPure())
1069 continue;
Eli Friedman61eab882009-12-08 03:56:49 +00001070
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001071 // Ignore implicit member functions, they are always marked as inline, but
1072 // they don't have a body until they're defined.
1073 if (MD->isImplicit())
1074 continue;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001075
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001076 if (MD->isInlineSpecified())
1077 continue;
Eli Friedmand7d7f672009-12-06 20:50:05 +00001078
1079 if (MD->hasInlineBody())
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00001080 continue;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001081
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00001082 // We found it.
1083 return MD;
1084 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001085
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00001086 return 0;
1087}
1088
Anders Carlsson1e641ce2010-05-26 04:56:53 +00001089/// getASTRecordLayout - Get or compute information about the layout of the
1090/// specified record (struct/union/class), which indicates its size and field
1091/// position information.
1092const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
1093 D = D->getDefinition();
1094 assert(D && "Cannot get layout of forward declarations!");
1095
1096 // Look up this layout, if already laid out, return what we have.
1097 // Note that we can't save a reference to the entry because this function
1098 // is recursive.
1099 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
1100 if (Entry) return *Entry;
1101
Anders Carlsson2f64e372010-05-26 05:10:47 +00001102 const ASTRecordLayout *NewEntry;
1103
1104 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
Anders Carlsson7d0918a2010-05-26 05:58:59 +00001105 RecordLayoutBuilder Builder(*this);
Anders Carlsson2f64e372010-05-26 05:10:47 +00001106 Builder.Layout(RD);
1107
1108 // FIXME: This is not always correct. See the part about bitfields at
1109 // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info.
1110 // FIXME: IsPODForThePurposeOfLayout should be stored in the record layout.
1111 bool IsPODForThePurposeOfLayout = cast<CXXRecordDecl>(D)->isPOD();
1112
1113 // FIXME: This should be done in FinalizeLayout.
1114 uint64_t DataSize =
1115 IsPODForThePurposeOfLayout ? Builder.Size : Builder.DataSize;
1116 uint64_t NonVirtualSize =
1117 IsPODForThePurposeOfLayout ? DataSize : Builder.NonVirtualSize;
1118
1119 NewEntry =
1120 new (*this) ASTRecordLayout(*this, Builder.Size, Builder.Alignment,
1121 DataSize, Builder.FieldOffsets.data(),
1122 Builder.FieldOffsets.size(),
1123 NonVirtualSize,
1124 Builder.NonVirtualAlignment,
1125 Builder.SizeOfLargestEmptySubobject,
1126 Builder.PrimaryBase,
Anders Carlsson48317102010-05-26 05:25:15 +00001127 Builder.PrimaryBaseIsVirtual,
Anders Carlsson2f64e372010-05-26 05:10:47 +00001128 Builder.Bases, Builder.VBases);
1129 } else {
Anders Carlsson7d0918a2010-05-26 05:58:59 +00001130 RecordLayoutBuilder Builder(*this);
Anders Carlsson2f64e372010-05-26 05:10:47 +00001131 Builder.Layout(D);
1132
1133 NewEntry =
1134 new (*this) ASTRecordLayout(*this, Builder.Size, Builder.Alignment,
1135 Builder.Size,
1136 Builder.FieldOffsets.data(),
1137 Builder.FieldOffsets.size());
1138 }
1139
Anders Carlsson1e641ce2010-05-26 04:56:53 +00001140 ASTRecordLayouts[D] = NewEntry;
1141
1142 if (getLangOptions().DumpRecordLayouts) {
1143 llvm::errs() << "\n*** Dumping AST Record Layout\n";
1144 DumpRecordLayout(D, llvm::errs());
1145 }
1146
1147 return *NewEntry;
1148}
1149
1150const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) {
1151 RD = cast<CXXRecordDecl>(RD->getDefinition());
1152 assert(RD && "Cannot get key function for forward declarations!");
1153
1154 const CXXMethodDecl *&Entry = KeyFunctions[RD];
1155 if (!Entry)
Anders Carlsson7d0918a2010-05-26 05:58:59 +00001156 Entry = RecordLayoutBuilder::ComputeKeyFunction(RD);
Anders Carlsson1e641ce2010-05-26 04:56:53 +00001157 else
Anders Carlsson7d0918a2010-05-26 05:58:59 +00001158 assert(Entry == RecordLayoutBuilder::ComputeKeyFunction(RD) &&
Anders Carlsson1e641ce2010-05-26 04:56:53 +00001159 "Key function changed!");
1160
1161 return Entry;
1162}
1163
1164/// getInterfaceLayoutImpl - Get or compute information about the
1165/// layout of the given interface.
1166///
1167/// \param Impl - If given, also include the layout of the interface's
1168/// implementation. This may differ by including synthesized ivars.
1169const ASTRecordLayout &
1170ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
1171 const ObjCImplementationDecl *Impl) {
1172 assert(!D->isForwardDecl() && "Invalid interface decl!");
1173
1174 // Look up this layout, if already laid out, return what we have.
1175 ObjCContainerDecl *Key =
1176 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
1177 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
1178 return *Entry;
1179
1180 // Add in synthesized ivar count if laying out an implementation.
1181 if (Impl) {
1182 unsigned SynthCount = CountNonClassIvars(D);
1183 // If there aren't any sythesized ivars then reuse the interface
1184 // entry. Note we can't cache this because we simply free all
1185 // entries later; however we shouldn't look up implementations
1186 // frequently.
1187 if (SynthCount == 0)
1188 return getObjCLayout(D, 0);
1189 }
1190
Anders Carlsson7d0918a2010-05-26 05:58:59 +00001191 RecordLayoutBuilder Builder(*this);
Anders Carlsson36cdc612010-05-26 05:04:25 +00001192 Builder.Layout(D);
1193
Anders Carlsson1e641ce2010-05-26 04:56:53 +00001194 const ASTRecordLayout *NewEntry =
Anders Carlsson36cdc612010-05-26 05:04:25 +00001195 new (*this) ASTRecordLayout(*this, Builder.Size, Builder.Alignment,
1196 Builder.DataSize,
1197 Builder.FieldOffsets.data(),
1198 Builder.FieldOffsets.size());
1199
Anders Carlsson1e641ce2010-05-26 04:56:53 +00001200 ObjCLayouts[Key] = NewEntry;
1201
1202 return *NewEntry;
1203}
1204
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001205static void PrintOffset(llvm::raw_ostream &OS,
1206 uint64_t Offset, unsigned IndentLevel) {
1207 OS << llvm::format("%4d | ", Offset);
1208 OS.indent(IndentLevel * 2);
1209}
1210
1211static void DumpCXXRecordLayout(llvm::raw_ostream &OS,
1212 const CXXRecordDecl *RD, ASTContext &C,
1213 uint64_t Offset,
1214 unsigned IndentLevel,
1215 const char* Description,
1216 bool IncludeVirtualBases) {
1217 const ASTRecordLayout &Info = C.getASTRecordLayout(RD);
1218
1219 PrintOffset(OS, Offset, IndentLevel);
Dan Gohmancb421fa2010-04-19 16:39:44 +00001220 OS << C.getTypeDeclType(const_cast<CXXRecordDecl *>(RD)).getAsString();
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001221 if (Description)
1222 OS << ' ' << Description;
1223 if (RD->isEmpty())
1224 OS << " (empty)";
1225 OS << '\n';
1226
1227 IndentLevel++;
1228
1229 const CXXRecordDecl *PrimaryBase = Info.getPrimaryBase();
1230
1231 // Vtable pointer.
1232 if (RD->isDynamicClass() && !PrimaryBase) {
1233 PrintOffset(OS, Offset, IndentLevel);
Benjamin Kramer900fc632010-04-17 09:33:03 +00001234 OS << '(' << RD << " vtable pointer)\n";
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001235 }
1236 // Dump (non-virtual) bases
1237 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1238 E = RD->bases_end(); I != E; ++I) {
1239 assert(!I->getType()->isDependentType() &&
1240 "Cannot layout class with dependent bases.");
1241 if (I->isVirtual())
1242 continue;
1243
1244 const CXXRecordDecl *Base =
1245 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1246
1247 uint64_t BaseOffset = Offset + Info.getBaseClassOffset(Base) / 8;
1248
1249 DumpCXXRecordLayout(OS, Base, C, BaseOffset, IndentLevel,
1250 Base == PrimaryBase ? "(primary base)" : "(base)",
1251 /*IncludeVirtualBases=*/false);
1252 }
1253
1254 // Dump fields.
1255 uint64_t FieldNo = 0;
1256 for (CXXRecordDecl::field_iterator I = RD->field_begin(),
1257 E = RD->field_end(); I != E; ++I, ++FieldNo) {
1258 const FieldDecl *Field = *I;
1259 uint64_t FieldOffset = Offset + Info.getFieldOffset(FieldNo) / 8;
1260
1261 if (const RecordType *RT = Field->getType()->getAs<RecordType>()) {
1262 if (const CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
1263 DumpCXXRecordLayout(OS, D, C, FieldOffset, IndentLevel,
1264 Field->getNameAsCString(),
1265 /*IncludeVirtualBases=*/true);
1266 continue;
1267 }
1268 }
1269
1270 PrintOffset(OS, FieldOffset, IndentLevel);
Benjamin Kramer900fc632010-04-17 09:33:03 +00001271 OS << Field->getType().getAsString() << ' ' << Field << '\n';
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001272 }
1273
1274 if (!IncludeVirtualBases)
1275 return;
1276
1277 // Dump virtual bases.
1278 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
1279 E = RD->vbases_end(); I != E; ++I) {
1280 assert(I->isVirtual() && "Found non-virtual class!");
1281 const CXXRecordDecl *VBase =
1282 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1283
1284 uint64_t VBaseOffset = Offset + Info.getVBaseClassOffset(VBase) / 8;
1285 DumpCXXRecordLayout(OS, VBase, C, VBaseOffset, IndentLevel,
1286 VBase == PrimaryBase ?
1287 "(primary virtual base)" : "(virtual base)",
1288 /*IncludeVirtualBases=*/false);
1289 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001290
1291 OS << " sizeof=" << Info.getSize() / 8;
1292 OS << ", dsize=" << Info.getDataSize() / 8;
1293 OS << ", align=" << Info.getAlignment() / 8 << '\n';
1294 OS << " nvsize=" << Info.getNonVirtualSize() / 8;
1295 OS << ", nvalign=" << Info.getNonVirtualAlign() / 8 << '\n';
1296 OS << '\n';
1297}
Daniel Dunbar8d8ab742010-04-19 20:44:53 +00001298
1299void ASTContext::DumpRecordLayout(const RecordDecl *RD,
1300 llvm::raw_ostream &OS) {
1301 const ASTRecordLayout &Info = getASTRecordLayout(RD);
1302
1303 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
1304 return DumpCXXRecordLayout(OS, CXXRD, *this, 0, 0, 0,
1305 /*IncludeVirtualBases=*/true);
1306
1307 OS << "Type: " << getTypeDeclType(RD).getAsString() << "\n";
1308 OS << "Record: ";
1309 RD->dump();
1310 OS << "\nLayout: ";
1311 OS << "<ASTRecordLayout\n";
1312 OS << " Size:" << Info.getSize() << "\n";
1313 OS << " DataSize:" << Info.getDataSize() << "\n";
1314 OS << " Alignment:" << Info.getAlignment() << "\n";
1315 OS << " FieldOffsets: [";
1316 for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i) {
1317 if (i) OS << ", ";
1318 OS << Info.getFieldOffset(i);
1319 }
1320 OS << "]>\n";
1321}