blob: 36dc2b2248d888c3a49cb23a61a835ed30369d09 [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);
99 void Layout(const ObjCInterfaceDecl *D);
100
101 void LayoutFields(const RecordDecl *D);
102 void LayoutField(const FieldDecl *D);
103 void LayoutWideBitField(uint64_t FieldSize, uint64_t TypeSize);
104 void LayoutBitField(const FieldDecl *D);
105
106 /// ComputeEmptySubobjectSizes - Compute the size of the largest base or
107 /// member subobject that is empty.
108 void ComputeEmptySubobjectSizes(const CXXRecordDecl *RD);
109
110 /// DeterminePrimaryBase - Determine the primary base of the given class.
111 void DeterminePrimaryBase(const CXXRecordDecl *RD);
112
113 void SelectPrimaryVBase(const CXXRecordDecl *RD);
114
115 /// IdentifyPrimaryBases - Identify all virtual base classes, direct or
116 /// indirect, that are primary base classes for some other direct or indirect
117 /// base class.
118 void IdentifyPrimaryBases(const CXXRecordDecl *RD);
119
120 bool IsNearlyEmpty(const CXXRecordDecl *RD) const;
121
122 /// LayoutNonVirtualBases - Determines the primary base class (if any) and
123 /// lays it out. Will then proceed to lay out all non-virtual base clasess.
124 void LayoutNonVirtualBases(const CXXRecordDecl *RD);
125
126 /// LayoutNonVirtualBase - Lays out a single non-virtual base.
127 void LayoutNonVirtualBase(const CXXRecordDecl *RD);
128
129 void AddPrimaryVirtualBaseOffsets(const CXXRecordDecl *RD, uint64_t Offset,
130 const CXXRecordDecl *MostDerivedClass);
131
132 /// LayoutVirtualBases - Lays out all the virtual bases.
133 void LayoutVirtualBases(const CXXRecordDecl *RD,
134 const CXXRecordDecl *MostDerivedClass);
135
136 /// LayoutVirtualBase - Lays out a single virtual base.
137 void LayoutVirtualBase(const CXXRecordDecl *RD);
138
139 /// LayoutBase - Will lay out a base and return the offset where it was
140 /// placed, in bits.
141 uint64_t LayoutBase(const CXXRecordDecl *RD);
142
143 /// canPlaceRecordAtOffset - Return whether a record (either a base class
144 /// or a field) can be placed at the given offset.
145 /// Returns false if placing the record will result in two components
146 /// (direct or indirect) of the same type having the same offset.
147 bool canPlaceRecordAtOffset(const CXXRecordDecl *RD, uint64_t Offset,
148 bool CheckVBases) const;
149
150 /// canPlaceFieldAtOffset - Return whether a field can be placed at the given
151 /// offset.
152 bool canPlaceFieldAtOffset(const FieldDecl *FD, uint64_t Offset) const;
153
154 /// UpdateEmptyClassOffsets - Called after a record (either a base class
155 /// or a field) has been placed at the given offset. Will update the
156 /// EmptyClassOffsets map if the class is empty or has any empty bases or
157 /// fields.
158 void UpdateEmptyClassOffsets(const CXXRecordDecl *RD, uint64_t Offset,
159 bool UpdateVBases);
160
161 /// UpdateEmptyClassOffsets - Called after a field has been placed at the
162 /// given offset.
163 void UpdateEmptyClassOffsets(const FieldDecl *FD, uint64_t Offset);
164
165 /// FinishLayout - Finalize record layout. Adjust record size based on the
166 /// alignment.
167 void FinishLayout();
168
169 void UpdateAlignment(unsigned NewAlignment);
170
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000171 RecordLayoutBuilder(const RecordLayoutBuilder&); // DO NOT IMPLEMENT
172 void operator=(const RecordLayoutBuilder&); // DO NOT IMPLEMENT
Anders Carlsson9392fa62010-05-26 05:41:04 +0000173public:
174 static const CXXMethodDecl *ComputeKeyFunction(const CXXRecordDecl *RD);
175};
Benjamin Kramer7e220282010-05-26 09:58:31 +0000176} // end anonymous namespace
Anders Carlsson9392fa62010-05-26 05:41:04 +0000177
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000178RecordLayoutBuilder::RecordLayoutBuilder(ASTContext &Context)
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000179 : Context(Context), Size(0), Alignment(8), Packed(false),
180 UnfilledBitsInLastByte(0), MaxFieldAlignment(0), DataSize(0), IsUnion(false),
Anders Carlsson48317102010-05-26 05:25:15 +0000181 NonVirtualSize(0), NonVirtualAlignment(8), PrimaryBase(0),
182 PrimaryBaseIsVirtual(false), FirstNearlyEmptyVBase(0),
Anders Carlsson836fc142010-05-08 23:06:26 +0000183 SizeOfLargestEmptySubobject(0) { }
Mike Stump3dee6ef2009-07-30 00:22:38 +0000184
Mike Stump6f376332009-08-05 22:37:18 +0000185/// IsNearlyEmpty - Indicates when a class has a vtable pointer, but
186/// no other data.
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000187bool RecordLayoutBuilder::IsNearlyEmpty(const CXXRecordDecl *RD) const {
Mike Stump6f376332009-08-05 22:37:18 +0000188 // FIXME: Audit the corners
189 if (!RD->isDynamicClass())
190 return false;
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000191 const ASTRecordLayout &BaseInfo = Context.getASTRecordLayout(RD);
192 if (BaseInfo.getNonVirtualSize() == Context.Target.getPointerWidth(0))
Mike Stump6f376332009-08-05 22:37:18 +0000193 return true;
194 return false;
195}
196
Anders Carlsson836fc142010-05-08 23:06:26 +0000197void
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000198RecordLayoutBuilder::ComputeEmptySubobjectSizes(const CXXRecordDecl *RD) {
Anders Carlsson836fc142010-05-08 23:06:26 +0000199 // Check the bases.
200 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
201 E = RD->bases_end(); I != E; ++I) {
202 const CXXRecordDecl *BaseDecl =
203 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
204
205 uint64_t EmptySize = 0;
206 const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl);
207 if (BaseDecl->isEmpty()) {
208 // If the class decl is empty, get its size.
209 EmptySize = Layout.getSize();
210 } else {
211 // Otherwise, we get the largest empty subobject for the decl.
212 EmptySize = Layout.getSizeOfLargestEmptySubobject();
213 }
214
215 SizeOfLargestEmptySubobject = std::max(SizeOfLargestEmptySubobject,
216 EmptySize);
217 }
218
219 // Check the fields.
220 for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
221 I != E; ++I) {
222 const FieldDecl *FD = *I;
223
224 const RecordType *RT =
225 Context.getBaseElementType(FD->getType())->getAs<RecordType>();
226
227 // We only care about record types.
228 if (!RT)
229 continue;
230
231 uint64_t EmptySize = 0;
232 const CXXRecordDecl *MemberDecl = cast<CXXRecordDecl>(RT->getDecl());
233 const ASTRecordLayout &Layout = Context.getASTRecordLayout(MemberDecl);
234 if (MemberDecl->isEmpty()) {
235 // If the class decl is empty, get its size.
236 EmptySize = Layout.getSize();
237 } else {
238 // Otherwise, we get the largest empty subobject for the decl.
239 EmptySize = Layout.getSizeOfLargestEmptySubobject();
240 }
241
242 SizeOfLargestEmptySubobject = std::max(SizeOfLargestEmptySubobject,
243 EmptySize);
244 }
245}
246
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000247void RecordLayoutBuilder::IdentifyPrimaryBases(const CXXRecordDecl *RD) {
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000248 const ASTRecordLayout::PrimaryBaseInfo &BaseInfo =
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000249 Context.getASTRecordLayout(RD).getPrimaryBaseInfo();
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000250
Anders Carlsson3f066522009-09-22 03:02:06 +0000251 // If the record has a primary base class that is virtual, add it to the set
252 // of primary bases.
Anders Carlsson261fba62009-11-27 22:14:40 +0000253 if (BaseInfo.isVirtual())
254 IndirectPrimaryBases.insert(BaseInfo.getBase());
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000255
Anders Carlsson3f066522009-09-22 03:02:06 +0000256 // Now traverse all bases and find primary bases for them.
Mike Stump6f376332009-08-05 22:37:18 +0000257 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000258 e = RD->bases_end(); i != e; ++i) {
Sebastian Redl9994a342009-10-25 17:03:50 +0000259 assert(!i->getType()->isDependentType() &&
260 "Cannot layout class with dependent bases.");
Mike Stump1eb44332009-09-09 15:08:12 +0000261 const CXXRecordDecl *Base =
Mike Stump49520942009-08-11 04:03:59 +0000262 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000263
Mike Stump49520942009-08-11 04:03:59 +0000264 // Only bases with virtual bases participate in computing the
265 // indirect primary virtual base classes.
Mike Stump4ef98092009-08-13 22:53:07 +0000266 if (Base->getNumVBases())
Anders Carlsson3f066522009-09-22 03:02:06 +0000267 IdentifyPrimaryBases(Base);
Mike Stump6f376332009-08-05 22:37:18 +0000268 }
269}
270
Anders Carlsson3f066522009-09-22 03:02:06 +0000271void
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000272RecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD) {
Anders Carlsson584e1df2010-03-11 03:39:12 +0000273 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000274 E = RD->bases_end(); I != E; ++I) {
Anders Carlsson584e1df2010-03-11 03:39:12 +0000275 assert(!I->getType()->isDependentType() &&
Sebastian Redl9994a342009-10-25 17:03:50 +0000276 "Cannot layout class with dependent bases.");
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000277
Mike Stump1eb44332009-09-09 15:08:12 +0000278 const CXXRecordDecl *Base =
Anders Carlsson584e1df2010-03-11 03:39:12 +0000279 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson200c5c22010-03-11 00:15:35 +0000280
Anders Carlsson584e1df2010-03-11 03:39:12 +0000281 // Check if this is a nearly empty virtual base.
282 if (I->isVirtual() && IsNearlyEmpty(Base)) {
283 // If it's not an indirect primary base, then we've found our primary
284 // base.
Anders Carlsson3f066522009-09-22 03:02:06 +0000285 if (!IndirectPrimaryBases.count(Base)) {
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000286 PrimaryBase = Base;
287 PrimaryBaseIsVirtual = true;
Mike Stumpd76264e2009-08-12 21:50:08 +0000288 return;
289 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000290
Anders Carlsson584e1df2010-03-11 03:39:12 +0000291 // Is this the first nearly empty virtual base?
292 if (!FirstNearlyEmptyVBase)
293 FirstNearlyEmptyVBase = Base;
Mike Stumpd76264e2009-08-12 21:50:08 +0000294 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000295
Anders Carlsson200c5c22010-03-11 00:15:35 +0000296 SelectPrimaryVBase(Base);
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000297 if (PrimaryBase)
Zhongxing Xu94ba3802010-02-15 04:28:35 +0000298 return;
Mike Stumpd76264e2009-08-12 21:50:08 +0000299 }
300}
301
Anders Carlsson200c5c22010-03-11 00:15:35 +0000302/// DeterminePrimaryBase - Determine the primary base of the given class.
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000303void RecordLayoutBuilder::DeterminePrimaryBase(const CXXRecordDecl *RD) {
Anders Carlsson200c5c22010-03-11 00:15:35 +0000304 // If the class isn't dynamic, it won't have a primary base.
305 if (!RD->isDynamicClass())
306 return;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000307
Anders Carlsson3f066522009-09-22 03:02:06 +0000308 // Compute all the primary virtual bases for all of our direct and
Mike Stump0880e752009-08-13 23:26:06 +0000309 // indirect bases, and record all their primary virtual base classes.
Mike Stump0880e752009-08-13 23:26:06 +0000310 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000311 e = RD->bases_end(); i != e; ++i) {
Sebastian Redl9994a342009-10-25 17:03:50 +0000312 assert(!i->getType()->isDependentType() &&
Anders Carlsson200c5c22010-03-11 00:15:35 +0000313 "Cannot lay out class with dependent bases.");
Mike Stump1eb44332009-09-09 15:08:12 +0000314 const CXXRecordDecl *Base =
Mike Stump0880e752009-08-13 23:26:06 +0000315 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson3f066522009-09-22 03:02:06 +0000316 IdentifyPrimaryBases(Base);
Mike Stump0880e752009-08-13 23:26:06 +0000317 }
318
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000319 // If the record has a dynamic base class, attempt to choose a primary base
320 // class. It is the first (in direct base class order) non-virtual dynamic
Anders Carlsson3f066522009-09-22 03:02:06 +0000321 // base class, if one exists.
Mike Stump6f376332009-08-05 22:37:18 +0000322 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000323 e = RD->bases_end(); i != e; ++i) {
Anders Carlssonce2009a2009-11-27 22:05:05 +0000324 // Ignore virtual bases.
325 if (i->isVirtual())
326 continue;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000327
Anders Carlssonce2009a2009-11-27 22:05:05 +0000328 const CXXRecordDecl *Base =
329 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
330
331 if (Base->isDynamicClass()) {
332 // We found it.
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000333 PrimaryBase = Base;
334 PrimaryBaseIsVirtual = false;
Anders Carlssonce2009a2009-11-27 22:05:05 +0000335 return;
Mike Stump6f376332009-08-05 22:37:18 +0000336 }
337 }
338
339 // Otherwise, it is the first nearly empty virtual base that is not an
Mike Stump49520942009-08-11 04:03:59 +0000340 // indirect primary virtual base class, if one exists.
Anders Carlsson200c5c22010-03-11 00:15:35 +0000341 if (RD->getNumVBases() != 0) {
342 SelectPrimaryVBase(RD);
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000343 if (PrimaryBase)
Anders Carlsson200c5c22010-03-11 00:15:35 +0000344 return;
345 }
Mike Stump6f376332009-08-05 22:37:18 +0000346
Anders Carlsson200c5c22010-03-11 00:15:35 +0000347 // Otherwise, it is the first nearly empty virtual base that is not an
348 // indirect primary virtual base class, if one exists.
349 if (FirstNearlyEmptyVBase) {
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000350 PrimaryBase = FirstNearlyEmptyVBase;
351 PrimaryBaseIsVirtual = true;
Mike Stump6f376332009-08-05 22:37:18 +0000352 return;
Anders Carlsson200c5c22010-03-11 00:15:35 +0000353 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000354
Anders Carlsson200c5c22010-03-11 00:15:35 +0000355 // Otherwise there is no primary base class.
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000356 assert(!PrimaryBase && "Should not get here with a primary base!");
Mike Stump6f376332009-08-05 22:37:18 +0000357
Anders Carlsson200c5c22010-03-11 00:15:35 +0000358 // Allocate the virtual table pointer at offset zero.
359 assert(DataSize == 0 && "Vtable pointer must be at offset zero!");
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000360
Anders Carlsson200c5c22010-03-11 00:15:35 +0000361 // Update the size.
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000362 Size += Context.Target.getPointerWidth(0);
Anders Carlsson200c5c22010-03-11 00:15:35 +0000363 DataSize = Size;
364
365 // Update the alignment.
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000366 UpdateAlignment(Context.Target.getPointerAlign(0));
Mike Stump6f376332009-08-05 22:37:18 +0000367}
368
Anders Carlssone239b9d2010-03-10 22:21:28 +0000369void
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000370RecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) {
Anders Carlsson200c5c22010-03-11 00:15:35 +0000371 // First, determine the primary base class.
372 DeterminePrimaryBase(RD);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000373
Anders Carlsson200c5c22010-03-11 00:15:35 +0000374 // If we have a primary base class, lay it out.
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000375 if (PrimaryBase) {
376 if (PrimaryBaseIsVirtual) {
Anders Carlsson200c5c22010-03-11 00:15:35 +0000377 // We have a virtual primary base, insert it as an indirect primary base.
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000378 IndirectPrimaryBases.insert(PrimaryBase);
Anders Carlsson37147ea2010-03-11 05:42:17 +0000379
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000380 assert(!VisitedVirtualBases.count(PrimaryBase) &&
381 "vbase already visited!");
382 VisitedVirtualBases.insert(PrimaryBase);
Anders Carlssonbdda6c12010-04-10 18:42:27 +0000383
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000384 LayoutVirtualBase(PrimaryBase);
Anders Carlsson200c5c22010-03-11 00:15:35 +0000385 } else
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000386 LayoutNonVirtualBase(PrimaryBase);
Anders Carlsson200c5c22010-03-11 00:15:35 +0000387 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000388
Anders Carlsson200c5c22010-03-11 00:15:35 +0000389 // Now lay out the non-virtual bases.
390 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000391 E = RD->bases_end(); I != E; ++I) {
Anders Carlsson200c5c22010-03-11 00:15:35 +0000392
393 // Ignore virtual bases.
394 if (I->isVirtual())
395 continue;
396
397 const CXXRecordDecl *Base =
398 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
399
400 // Skip the primary base.
Anders Carlssonaa230b72010-05-26 05:31:23 +0000401 if (Base == PrimaryBase && !PrimaryBaseIsVirtual)
Anders Carlsson200c5c22010-03-11 00:15:35 +0000402 continue;
403
404 // Lay out the base.
405 LayoutNonVirtualBase(Base);
Anders Carlssone239b9d2010-03-10 22:21:28 +0000406 }
407}
408
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000409void RecordLayoutBuilder::LayoutNonVirtualBase(const CXXRecordDecl *RD) {
Anders Carlssone3bdbee2010-03-10 22:26:24 +0000410 // Layout the base.
411 uint64_t Offset = LayoutBase(RD);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000412
Anders Carlssone3bdbee2010-03-10 22:26:24 +0000413 // Add its base class offset.
Anders Carlsson147b5dd2010-03-11 04:10:39 +0000414 if (!Bases.insert(std::make_pair(RD, Offset)).second)
415 assert(false && "Added same base offset more than once!");
Anders Carlssone239b9d2010-03-10 22:21:28 +0000416}
Mike Stump968db332009-11-05 04:02:15 +0000417
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000418void
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000419RecordLayoutBuilder::AddPrimaryVirtualBaseOffsets(const CXXRecordDecl *RD,
Anders Carlsson97913572010-04-15 16:12:58 +0000420 uint64_t Offset,
421 const CXXRecordDecl *MostDerivedClass) {
422 // We already have the offset for the primary base of the most derived class.
423 if (RD != MostDerivedClass) {
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000424 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlsson97913572010-04-15 16:12:58 +0000425 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
426
427 // If this is a primary virtual base and we haven't seen it before, add it.
428 if (PrimaryBase && Layout.getPrimaryBaseWasVirtual() &&
429 !VBases.count(PrimaryBase))
430 VBases.insert(std::make_pair(PrimaryBase, Offset));
431 }
432
433 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
434 E = RD->bases_end(); I != E; ++I) {
435 assert(!I->getType()->isDependentType() &&
436 "Cannot layout class with dependent bases.");
437
438 const CXXRecordDecl *BaseDecl =
439 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
440
441 if (!BaseDecl->getNumVBases()) {
442 // This base isn't interesting since it doesn't have any virtual bases.
443 continue;
444 }
445
446 // Compute the offset of this base.
447 uint64_t BaseOffset;
448
449 if (I->isVirtual()) {
450 // If we don't know this vbase yet, don't visit it. It will be visited
451 // later.
452 if (!VBases.count(BaseDecl)) {
453 continue;
454 }
455
456 // Check if we've already visited this base.
457 if (!VisitedVirtualBases.insert(BaseDecl))
458 continue;
459
460 // We want the vbase offset from the class we're currently laying out.
461 BaseOffset = VBases[BaseDecl];
462 } else if (RD == MostDerivedClass) {
463 // We want the base offset from the class we're currently laying out.
464 assert(Bases.count(BaseDecl) && "Did not find base!");
465 BaseOffset = Bases[BaseDecl];
466 } else {
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000467 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlsson97913572010-04-15 16:12:58 +0000468 BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl);
469 }
470
471 AddPrimaryVirtualBaseOffsets(BaseDecl, BaseOffset, MostDerivedClass);
472 }
473}
474
475void
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000476RecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
Anders Carlsson97913572010-04-15 16:12:58 +0000477 const CXXRecordDecl *MostDerivedClass) {
Anders Carlsson88f42962010-03-11 04:33:54 +0000478 const CXXRecordDecl *PrimaryBase;
Anders Carlssonbdda6c12010-04-10 18:42:27 +0000479 bool PrimaryBaseIsVirtual;
Anders Carlsson37147ea2010-03-11 05:42:17 +0000480
Anders Carlssonbdda6c12010-04-10 18:42:27 +0000481 if (MostDerivedClass == RD) {
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000482 PrimaryBase = this->PrimaryBase;
483 PrimaryBaseIsVirtual = this->PrimaryBaseIsVirtual;
Anders Carlssonbdda6c12010-04-10 18:42:27 +0000484 } else {
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000485 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlsson88f42962010-03-11 04:33:54 +0000486 PrimaryBase = Layout.getPrimaryBase();
Anders Carlssonbdda6c12010-04-10 18:42:27 +0000487 PrimaryBaseIsVirtual = Layout.getPrimaryBaseWasVirtual();
488 }
489
Anders Carlsson622e2472010-03-11 04:24:02 +0000490 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
491 E = RD->bases_end(); I != E; ++I) {
492 assert(!I->getType()->isDependentType() &&
Sebastian Redl9994a342009-10-25 17:03:50 +0000493 "Cannot layout class with dependent bases.");
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000494
Mike Stump1eb44332009-09-09 15:08:12 +0000495 const CXXRecordDecl *Base =
Anders Carlsson622e2472010-03-11 04:24:02 +0000496 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
497
498 if (I->isVirtual()) {
Anders Carlssonbdda6c12010-04-10 18:42:27 +0000499 if (PrimaryBase != Base || !PrimaryBaseIsVirtual) {
500 bool IndirectPrimaryBase = IndirectPrimaryBases.count(Base);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000501
Anders Carlssonbdda6c12010-04-10 18:42:27 +0000502 // Only lay out the virtual base if it's not an indirect primary base.
503 if (!IndirectPrimaryBase) {
504 // Only visit virtual bases once.
505 if (!VisitedVirtualBases.insert(Base))
506 continue;
507
Anders Carlsson37147ea2010-03-11 05:42:17 +0000508 LayoutVirtualBase(Base);
Anders Carlsson147b5dd2010-03-11 04:10:39 +0000509 }
Mike Stump968db332009-11-05 04:02:15 +0000510 }
Mike Stump4ef98092009-08-13 22:53:07 +0000511 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000512
Anders Carlsson622e2472010-03-11 04:24:02 +0000513 if (!Base->getNumVBases()) {
514 // This base isn't interesting since it doesn't have any virtual bases.
515 continue;
Mike Stumpfe3010d2009-08-16 19:04:13 +0000516 }
Anders Carlsson622e2472010-03-11 04:24:02 +0000517
Anders Carlsson97913572010-04-15 16:12:58 +0000518 LayoutVirtualBases(Base, MostDerivedClass);
Mike Stumpeb19fa92009-08-06 13:41:24 +0000519 }
520}
521
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000522void RecordLayoutBuilder::LayoutVirtualBase(const CXXRecordDecl *RD) {
Anders Carlssone3bdbee2010-03-10 22:26:24 +0000523 // Layout the base.
524 uint64_t Offset = LayoutBase(RD);
525
526 // Add its base class offset.
Anders Carlsson147b5dd2010-03-11 04:10:39 +0000527 if (!VBases.insert(std::make_pair(RD, Offset)).second)
528 assert(false && "Added same vbase offset more than once!");
Anders Carlssone239b9d2010-03-10 22:21:28 +0000529}
530
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000531uint64_t RecordLayoutBuilder::LayoutBase(const CXXRecordDecl *RD) {
Anders Carlssonc3fddeb2010-05-08 22:35:05 +0000532 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlssone239b9d2010-03-10 22:21:28 +0000533
534 // If we have an empty base class, try to place it at offset 0.
Anders Carlsson3159ffe2010-05-10 15:26:14 +0000535 if (RD->isEmpty() && canPlaceRecordAtOffset(RD, 0, /*CheckVBases=*/false)) {
Anders Carlssone239b9d2010-03-10 22:21:28 +0000536 // We were able to place the class at offset 0.
Anders Carlssonecafebe2010-05-10 15:28:59 +0000537 UpdateEmptyClassOffsets(RD, 0, /*UpdateVBases=*/false);
Anders Carlssone239b9d2010-03-10 22:21:28 +0000538
Anders Carlssonc3fddeb2010-05-08 22:35:05 +0000539 Size = std::max(Size, Layout.getSize());
Anders Carlssone239b9d2010-03-10 22:21:28 +0000540
541 return 0;
542 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000543
Anders Carlssonc3fddeb2010-05-08 22:35:05 +0000544 unsigned BaseAlign = Layout.getNonVirtualAlign();
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000545
Anders Carlssone239b9d2010-03-10 22:21:28 +0000546 // Round up the current record size to the base's alignment boundary.
547 uint64_t Offset = llvm::RoundUpToAlignment(DataSize, BaseAlign);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000548
Anders Carlssone239b9d2010-03-10 22:21:28 +0000549 // Try to place the base.
550 while (true) {
Anders Carlsson3159ffe2010-05-10 15:26:14 +0000551 if (canPlaceRecordAtOffset(RD, Offset, /*CheckVBases=*/false))
Anders Carlssone239b9d2010-03-10 22:21:28 +0000552 break;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000553
Anders Carlssone239b9d2010-03-10 22:21:28 +0000554 Offset += BaseAlign;
555 }
556
557 if (!RD->isEmpty()) {
558 // Update the data size.
Anders Carlssonc3fddeb2010-05-08 22:35:05 +0000559 DataSize = Offset + Layout.getNonVirtualSize();
Anders Carlssone239b9d2010-03-10 22:21:28 +0000560
561 Size = std::max(Size, DataSize);
562 } else
Anders Carlssonc3fddeb2010-05-08 22:35:05 +0000563 Size = std::max(Size, Offset + Layout.getSize());
Anders Carlssone239b9d2010-03-10 22:21:28 +0000564
565 // Remember max struct/class alignment.
566 UpdateAlignment(BaseAlign);
567
Anders Carlssonecafebe2010-05-10 15:28:59 +0000568 UpdateEmptyClassOffsets(RD, Offset, /*UpdateVBases=*/false);
Anders Carlssone239b9d2010-03-10 22:21:28 +0000569 return Offset;
570}
571
Anders Carlsson3159ffe2010-05-10 15:26:14 +0000572bool
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000573RecordLayoutBuilder::canPlaceRecordAtOffset(const CXXRecordDecl *RD,
Anders Carlsson3159ffe2010-05-10 15:26:14 +0000574 uint64_t Offset,
575 bool CheckVBases) const {
Anders Carlsson1345bd22009-09-24 03:22:10 +0000576 // Look for an empty class with the same type at the same offset.
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000577 for (EmptyClassOffsetsTy::const_iterator I =
578 EmptyClassOffsets.lower_bound(Offset),
579 E = EmptyClassOffsets.upper_bound(Offset); I != E; ++I) {
580
Anders Carlsson1345bd22009-09-24 03:22:10 +0000581 if (I->second == RD)
582 return false;
583 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000584
Anders Carlssonddae8772010-05-09 05:03:38 +0000585 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlssonffbdefc2009-09-24 05:21:31 +0000586
587 // Check bases.
588 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000589 E = RD->bases_end(); I != E; ++I) {
Sebastian Redl9994a342009-10-25 17:03:50 +0000590 assert(!I->getType()->isDependentType() &&
591 "Cannot layout class with dependent bases.");
Anders Carlssonffbdefc2009-09-24 05:21:31 +0000592 if (I->isVirtual())
593 continue;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000594
Anders Carlssonddae8772010-05-09 05:03:38 +0000595 const CXXRecordDecl *BaseDecl =
Anders Carlssonffbdefc2009-09-24 05:21:31 +0000596 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
597
Anders Carlssonddae8772010-05-09 05:03:38 +0000598 uint64_t BaseOffset = Layout.getBaseClassOffset(BaseDecl);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000599
Anders Carlsson3159ffe2010-05-10 15:26:14 +0000600 if (!canPlaceRecordAtOffset(BaseDecl, Offset + BaseOffset,
601 /*CheckVBases=*/false))
Anders Carlssonffbdefc2009-09-24 05:21:31 +0000602 return false;
603 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000604
Anders Carlsson1eca99b2009-09-25 15:39:00 +0000605 // Check fields.
606 unsigned FieldNo = 0;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000607 for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
Anders Carlsson1eca99b2009-09-25 15:39:00 +0000608 I != E; ++I, ++FieldNo) {
609 const FieldDecl *FD = *I;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000610
Anders Carlssonddae8772010-05-09 05:03:38 +0000611 uint64_t FieldOffset = Layout.getFieldOffset(FieldNo);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000612
Anders Carlsson1eca99b2009-09-25 15:39:00 +0000613 if (!canPlaceFieldAtOffset(FD, Offset + FieldOffset))
614 return false;
615 }
616
Anders Carlsson3159ffe2010-05-10 15:26:14 +0000617 if (CheckVBases) {
618 // FIXME: virtual bases.
619 }
620
Anders Carlsson96061492009-09-24 03:13:30 +0000621 return true;
622}
623
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000624bool RecordLayoutBuilder::canPlaceFieldAtOffset(const FieldDecl *FD,
Anders Carlsson60265042009-09-25 00:02:51 +0000625 uint64_t Offset) const {
Anders Carlssonfbbce492009-09-25 01:23:32 +0000626 QualType T = FD->getType();
627 if (const RecordType *RT = T->getAs<RecordType>()) {
Anders Carlsson60265042009-09-25 00:02:51 +0000628 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
Anders Carlsson3159ffe2010-05-10 15:26:14 +0000629 return canPlaceRecordAtOffset(RD, Offset, /*CheckVBases=*/true);
Anders Carlsson60265042009-09-25 00:02:51 +0000630 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000631
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000632 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) {
633 QualType ElemTy = Context.getBaseElementType(AT);
Anders Carlssonfbbce492009-09-25 01:23:32 +0000634 const RecordType *RT = ElemTy->getAs<RecordType>();
635 if (!RT)
636 return true;
637 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
638 if (!RD)
639 return true;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000640
Anders Carlssonc3fddeb2010-05-08 22:35:05 +0000641 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlssonfbbce492009-09-25 01:23:32 +0000642
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000643 uint64_t NumElements = Context.getConstantArrayElementCount(AT);
Mike Stump968db332009-11-05 04:02:15 +0000644 uint64_t ElementOffset = Offset;
Anders Carlssonfbbce492009-09-25 01:23:32 +0000645 for (uint64_t I = 0; I != NumElements; ++I) {
Anders Carlsson3159ffe2010-05-10 15:26:14 +0000646 if (!canPlaceRecordAtOffset(RD, ElementOffset, /*CheckVBases=*/true))
Anders Carlssonfbbce492009-09-25 01:23:32 +0000647 return false;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000648
Anders Carlssonc3fddeb2010-05-08 22:35:05 +0000649 ElementOffset += Layout.getSize();
Anders Carlssonfbbce492009-09-25 01:23:32 +0000650 }
651 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000652
Anders Carlsson60265042009-09-25 00:02:51 +0000653 return true;
654}
655
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000656void RecordLayoutBuilder::UpdateEmptyClassOffsets(const CXXRecordDecl *RD,
Anders Carlssonecafebe2010-05-10 15:28:59 +0000657 uint64_t Offset,
658 bool UpdateVBases) {
Anders Carlsson1345bd22009-09-24 03:22:10 +0000659 if (RD->isEmpty())
660 EmptyClassOffsets.insert(std::make_pair(Offset, RD));
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000661
Anders Carlssonc3fddeb2010-05-08 22:35:05 +0000662 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlssonffbdefc2009-09-24 05:21:31 +0000663
664 // Update bases.
665 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000666 E = RD->bases_end(); I != E; ++I) {
Sebastian Redl9994a342009-10-25 17:03:50 +0000667 assert(!I->getType()->isDependentType() &&
668 "Cannot layout class with dependent bases.");
Anders Carlssonffbdefc2009-09-24 05:21:31 +0000669 if (I->isVirtual())
670 continue;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000671
Anders Carlssonffbdefc2009-09-24 05:21:31 +0000672 const CXXRecordDecl *Base =
673 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000674
Anders Carlssonc3fddeb2010-05-08 22:35:05 +0000675 uint64_t BaseClassOffset = Layout.getBaseClassOffset(Base);
Anders Carlssonecafebe2010-05-10 15:28:59 +0000676 UpdateEmptyClassOffsets(Base, Offset + BaseClassOffset,
677 /*UpdateVBases=*/false);
Anders Carlssonffbdefc2009-09-24 05:21:31 +0000678 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000679
Anders Carlsson1eca99b2009-09-25 15:39:00 +0000680 // Update fields.
681 unsigned FieldNo = 0;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000682 for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
Anders Carlsson1eca99b2009-09-25 15:39:00 +0000683 I != E; ++I, ++FieldNo) {
684 const FieldDecl *FD = *I;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000685
Anders Carlssonc3fddeb2010-05-08 22:35:05 +0000686 uint64_t FieldOffset = Layout.getFieldOffset(FieldNo);
Anders Carlsson1eca99b2009-09-25 15:39:00 +0000687 UpdateEmptyClassOffsets(FD, Offset + FieldOffset);
688 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000689
Anders Carlssonbfcdc402010-05-23 18:14:24 +0000690 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
691
Anders Carlssonecafebe2010-05-10 15:28:59 +0000692 if (UpdateVBases) {
693 // FIXME: Update virtual bases.
Anders Carlssonbfcdc402010-05-23 18:14:24 +0000694 } else if (PrimaryBase && Layout.getPrimaryBaseWasVirtual()) {
695 // We always want to update the offsets of a primary virtual base.
696 assert(Layout.getVBaseClassOffset(PrimaryBase) == 0 &&
697 "primary base class offset must always be 0!");
698 UpdateEmptyClassOffsets(PrimaryBase, Offset, /*UpdateVBases=*/false);
Anders Carlssonecafebe2010-05-10 15:28:59 +0000699 }
Anders Carlsson96061492009-09-24 03:13:30 +0000700}
701
Anders Carlssona4c60812009-09-25 01:54:38 +0000702void
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000703RecordLayoutBuilder::UpdateEmptyClassOffsets(const FieldDecl *FD,
Anders Carlssona4c60812009-09-25 01:54:38 +0000704 uint64_t Offset) {
705 QualType T = FD->getType();
706
707 if (const RecordType *RT = T->getAs<RecordType>()) {
708 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
Anders Carlssonecafebe2010-05-10 15:28:59 +0000709 UpdateEmptyClassOffsets(RD, Offset, /*UpdateVBases=*/true);
Anders Carlssona4c60812009-09-25 01:54:38 +0000710 return;
711 }
712 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000713
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000714 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) {
715 QualType ElemTy = Context.getBaseElementType(AT);
Anders Carlssona4c60812009-09-25 01:54:38 +0000716 const RecordType *RT = ElemTy->getAs<RecordType>();
717 if (!RT)
718 return;
719 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
720 if (!RD)
721 return;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000722
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000723 const ASTRecordLayout &Info = Context.getASTRecordLayout(RD);
Anders Carlssona4c60812009-09-25 01:54:38 +0000724
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000725 uint64_t NumElements = Context.getConstantArrayElementCount(AT);
Mike Stump968db332009-11-05 04:02:15 +0000726 uint64_t ElementOffset = Offset;
Anders Carlssona4c60812009-09-25 01:54:38 +0000727
728 for (uint64_t I = 0; I != NumElements; ++I) {
Anders Carlssonecafebe2010-05-10 15:28:59 +0000729 UpdateEmptyClassOffsets(RD, ElementOffset, /*UpdateVBases=*/true);
Anders Carlssona4c60812009-09-25 01:54:38 +0000730 ElementOffset += Info.getSize();
731 }
732 }
733}
734
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000735void RecordLayoutBuilder::Layout(const RecordDecl *D) {
Anders Carlssonbda4c102009-07-18 20:20:21 +0000736 IsUnion = D->isUnion();
Anders Carlssona860e752009-08-08 18:23:56 +0000737
Anders Carlssona5dd7222009-08-08 19:38:24 +0000738 Packed = D->hasAttr<PackedAttr>();
739
740 // The #pragma pack attribute specifies the maximum field alignment.
Anders Carlssona860e752009-08-08 18:23:56 +0000741 if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>())
Anders Carlssona5dd7222009-08-08 19:38:24 +0000742 MaxFieldAlignment = PPA->getAlignment();
Mike Stump1eb44332009-09-09 15:08:12 +0000743
Anders Carlssonbda4c102009-07-18 20:20:21 +0000744 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Sean Huntbbd37c62009-11-21 08:43:09 +0000745 UpdateAlignment(AA->getMaxAlignment());
Anders Carlsson74cbe222009-07-19 00:18:47 +0000746
Mike Stump276b9f12009-08-16 01:46:26 +0000747 // If this is a C++ class, lay out the vtable and the non-virtual bases.
Mike Stumpeb19fa92009-08-06 13:41:24 +0000748 const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
Anders Carlsson836fc142010-05-08 23:06:26 +0000749 if (RD) {
750 ComputeEmptySubobjectSizes(RD);
Mike Stump3dee6ef2009-07-30 00:22:38 +0000751 LayoutNonVirtualBases(RD);
Anders Carlsson836fc142010-05-08 23:06:26 +0000752 }
Anders Carlsson74cbe222009-07-19 00:18:47 +0000753
Anders Carlssona2df41c2009-07-18 21:48:39 +0000754 LayoutFields(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Anders Carlssonb2fafd42009-07-28 19:24:15 +0000756 NonVirtualSize = Size;
757 NonVirtualAlignment = Alignment;
Mike Stump3dee6ef2009-07-30 00:22:38 +0000758
Anders Carlsson97913572010-04-15 16:12:58 +0000759 // If this is a C++ class, lay out its virtual bases and add its primary
760 // virtual base offsets.
761 if (RD) {
762 LayoutVirtualBases(RD, RD);
763
764 VisitedVirtualBases.clear();
765 AddPrimaryVirtualBaseOffsets(RD, 0, RD);
766 }
Mike Stumpeb19fa92009-08-06 13:41:24 +0000767
Anders Carlssonbda4c102009-07-18 20:20:21 +0000768 // Finally, round the size of the total struct up to the alignment of the
769 // struct itself.
770 FinishLayout();
Anders Carlssona1e87162010-04-10 21:24:48 +0000771
772#ifndef NDEBUG
773 if (RD) {
774 // Check that we have base offsets for all bases.
775 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
776 E = RD->bases_end(); I != E; ++I) {
777 if (I->isVirtual())
778 continue;
779
780 const CXXRecordDecl *BaseDecl =
781 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
782
783 assert(Bases.count(BaseDecl) && "Did not find base offset!");
784 }
785
786 // And all virtual bases.
787 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
788 E = RD->vbases_end(); I != E; ++I) {
789 const CXXRecordDecl *BaseDecl =
790 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
791
792 assert(VBases.count(BaseDecl) && "Did not find base offset!");
793 }
794 }
795#endif
Anders Carlssonbda4c102009-07-18 20:20:21 +0000796}
797
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000798void RecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D) {
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000799 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000800 const ASTRecordLayout &SL = Context.getASTObjCInterfaceLayout(SD);
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000801
802 UpdateAlignment(SL.getAlignment());
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000804 // We start laying out ivars not at the end of the superclass
805 // structure, but at the next byte following the last field.
Anders Carlsson243a6852009-07-18 21:26:44 +0000806 Size = llvm::RoundUpToAlignment(SL.getDataSize(), 8);
Anders Carlssona2239352009-09-26 01:34:51 +0000807 DataSize = Size;
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000808 }
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Anders Carlssona5dd7222009-08-08 19:38:24 +0000810 Packed = D->hasAttr<PackedAttr>();
Mike Stump1eb44332009-09-09 15:08:12 +0000811
Anders Carlssona5dd7222009-08-08 19:38:24 +0000812 // The #pragma pack attribute specifies the maximum field alignment.
813 if (const PragmaPackAttr *PPA = D->getAttr<PragmaPackAttr>())
814 MaxFieldAlignment = PPA->getAlignment();
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000816 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Sean Huntbbd37c62009-11-21 08:43:09 +0000817 UpdateAlignment(AA->getMaxAlignment());
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000818 // Layout each ivar sequentially.
819 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000820 Context.ShallowCollectObjCIvars(D, Ivars);
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000821 for (unsigned i = 0, e = Ivars.size(); i != e; ++i)
822 LayoutField(Ivars[i]);
Mike Stump1eb44332009-09-09 15:08:12 +0000823
Anders Carlsson93fab9d2009-07-18 20:50:59 +0000824 // Finally, round the size of the total struct up to the alignment of the
825 // struct itself.
826 FinishLayout();
827}
828
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000829void RecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
Anders Carlssona2df41c2009-07-18 21:48:39 +0000830 // Layout each field, for now, just sequentially, respecting alignment. In
831 // the future, this will need to be tweakable by targets.
Mike Stump1eb44332009-09-09 15:08:12 +0000832 for (RecordDecl::field_iterator Field = D->field_begin(),
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000833 FieldEnd = D->field_end(); Field != FieldEnd; ++Field)
Anders Carlssona2df41c2009-07-18 21:48:39 +0000834 LayoutField(*Field);
835}
836
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000837void RecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize,
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +0000838 uint64_t TypeSize) {
839 assert(Context.getLangOptions().CPlusPlus &&
840 "Can only have wide bit-fields in C++!");
841
842 // Itanium C++ ABI 2.4:
843 // If sizeof(T)*8 < n, let T' be the largest integral POD type with
844 // sizeof(T')*8 <= n.
845
846 QualType IntegralPODTypes[] = {
847 Context.UnsignedCharTy, Context.UnsignedShortTy, Context.UnsignedIntTy,
848 Context.UnsignedLongTy, Context.UnsignedLongLongTy
849 };
850
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +0000851 QualType Type;
852 for (unsigned I = 0, E = llvm::array_lengthof(IntegralPODTypes);
853 I != E; ++I) {
854 uint64_t Size = Context.getTypeSize(IntegralPODTypes[I]);
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +0000855
856 if (Size > FieldSize)
857 break;
858
859 Type = IntegralPODTypes[I];
860 }
861 assert(!Type.isNull() && "Did not find a type!");
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +0000862
863 unsigned TypeAlign = Context.getTypeAlign(Type);
864
865 // We're not going to use any of the unfilled bits in the last byte.
866 UnfilledBitsInLastByte = 0;
867
Anders Carlssonde9f1532010-04-17 20:21:41 +0000868 uint64_t FieldOffset;
869
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +0000870 if (IsUnion) {
871 DataSize = std::max(DataSize, FieldSize);
Anders Carlssonde9f1532010-04-17 20:21:41 +0000872 FieldOffset = 0;
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +0000873 } else {
Anders Carlssonde9f1532010-04-17 20:21:41 +0000874 // The bitfield is allocated starting at the next offset aligned appropriately
875 // for T', with length n bits.
876 FieldOffset = llvm::RoundUpToAlignment(DataSize, TypeAlign);
877
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +0000878 uint64_t NewSizeInBits = FieldOffset + FieldSize;
879
880 DataSize = llvm::RoundUpToAlignment(NewSizeInBits, 8);
881 UnfilledBitsInLastByte = DataSize - NewSizeInBits;
882 }
883
884 // Place this field at the current location.
885 FieldOffsets.push_back(FieldOffset);
886
887 // Update the size.
888 Size = std::max(Size, DataSize);
889
890 // Remember max struct/class alignment.
891 UpdateAlignment(TypeAlign);
892}
893
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000894void RecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
Anders Carlsson42dbcc42009-11-22 17:37:31 +0000895 bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
Anders Carlssone4fc0d92009-11-22 19:13:51 +0000896 uint64_t FieldOffset = IsUnion ? 0 : (DataSize - UnfilledBitsInLastByte);
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000897 uint64_t FieldSize = D->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000898
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000899 std::pair<uint64_t, unsigned> FieldInfo = Context.getTypeInfo(D->getType());
Anders Carlsson42dbcc42009-11-22 17:37:31 +0000900 uint64_t TypeSize = FieldInfo.first;
901 unsigned FieldAlign = FieldInfo.second;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000902
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +0000903 if (FieldSize > TypeSize) {
904 LayoutWideBitField(FieldSize, TypeSize);
905 return;
906 }
907
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000908 if (FieldPacked || !Context.Target.useBitFieldTypeAlignment())
Anders Carlsson42dbcc42009-11-22 17:37:31 +0000909 FieldAlign = 1;
910 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
911 FieldAlign = std::max(FieldAlign, AA->getMaxAlignment());
912
913 // The maximum field alignment overrides the aligned attribute.
914 if (MaxFieldAlignment)
915 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000916
Daniel Dunbarb6a16932010-04-15 06:18:39 +0000917 // Check if we need to add padding to give the field the correct alignment.
Anders Carlsson42dbcc42009-11-22 17:37:31 +0000918 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
919 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000920
Daniel Dunbarb6a16932010-04-15 06:18:39 +0000921 // Padding members don't affect overall alignment.
Anders Carlsson42dbcc42009-11-22 17:37:31 +0000922 if (!D->getIdentifier())
923 FieldAlign = 1;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000924
Anders Carlsson42dbcc42009-11-22 17:37:31 +0000925 // Place this field at the current location.
926 FieldOffsets.push_back(FieldOffset);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000927
Anders Carlssone4fc0d92009-11-22 19:13:51 +0000928 // Update DataSize to include the last byte containing (part of) the bitfield.
929 if (IsUnion) {
930 // FIXME: I think FieldSize should be TypeSize here.
931 DataSize = std::max(DataSize, FieldSize);
932 } else {
933 uint64_t NewSizeInBits = FieldOffset + FieldSize;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000934
Anders Carlssone4fc0d92009-11-22 19:13:51 +0000935 DataSize = llvm::RoundUpToAlignment(NewSizeInBits, 8);
936 UnfilledBitsInLastByte = DataSize - NewSizeInBits;
937 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000938
Anders Carlssone4fc0d92009-11-22 19:13:51 +0000939 // Update the size.
940 Size = std::max(Size, DataSize);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000941
Anders Carlsson42dbcc42009-11-22 17:37:31 +0000942 // Remember max struct/class alignment.
943 UpdateAlignment(FieldAlign);
944}
945
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000946void RecordLayoutBuilder::LayoutField(const FieldDecl *D) {
Anders Carlsson42dbcc42009-11-22 17:37:31 +0000947 if (D->isBitField()) {
948 LayoutBitField(D);
949 return;
950 }
951
Anders Carlssone4fc0d92009-11-22 19:13:51 +0000952 // Reset the unfilled bits.
953 UnfilledBitsInLastByte = 0;
954
Anders Carlsson42dbcc42009-11-22 17:37:31 +0000955 bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
Anders Carlssona2239352009-09-26 01:34:51 +0000956 uint64_t FieldOffset = IsUnion ? 0 : DataSize;
Anders Carlssonbda4c102009-07-18 20:20:21 +0000957 uint64_t FieldSize;
958 unsigned FieldAlign;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000959
Anders Carlsson42dbcc42009-11-22 17:37:31 +0000960 if (D->getType()->isIncompleteArrayType()) {
961 // This is a flexible array member; we can't directly
962 // query getTypeInfo about these, so we figure it out here.
963 // Flexible array members don't have any size, but they
964 // have to be aligned appropriately for their element type.
965 FieldSize = 0;
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000966 const ArrayType* ATy = Context.getAsArrayType(D->getType());
967 FieldAlign = Context.getTypeAlign(ATy->getElementType());
Anders Carlsson42dbcc42009-11-22 17:37:31 +0000968 } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) {
969 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000970 FieldSize = Context.Target.getPointerWidth(AS);
971 FieldAlign = Context.Target.getPointerAlign(AS);
Anders Carlssonbda4c102009-07-18 20:20:21 +0000972 } else {
Anders Carlsson0f0e9b02010-04-16 15:07:51 +0000973 std::pair<uint64_t, unsigned> FieldInfo = Context.getTypeInfo(D->getType());
Anders Carlsson42dbcc42009-11-22 17:37:31 +0000974 FieldSize = FieldInfo.first;
975 FieldAlign = FieldInfo.second;
Anders Carlssonbda4c102009-07-18 20:20:21 +0000976 }
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Anders Carlsson42dbcc42009-11-22 17:37:31 +0000978 if (FieldPacked)
979 FieldAlign = 8;
980 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
981 FieldAlign = std::max(FieldAlign, AA->getMaxAlignment());
982
983 // The maximum field alignment overrides the aligned attribute.
984 if (MaxFieldAlignment)
985 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
986
987 // Round up the current record size to the field's alignment boundary.
988 FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000989
Anders Carlsson42dbcc42009-11-22 17:37:31 +0000990 if (!IsUnion) {
991 while (true) {
992 // Check if we can place the field at this offset.
993 if (canPlaceFieldAtOffset(D, FieldOffset))
994 break;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000995
Anders Carlsson42dbcc42009-11-22 17:37:31 +0000996 // We couldn't place the field at the offset. Try again at a new offset.
997 FieldOffset += FieldAlign;
998 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000999
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001000 UpdateEmptyClassOffsets(D, FieldOffset);
1001 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001002
Anders Carlssonbda4c102009-07-18 20:20:21 +00001003 // Place this field at the current location.
1004 FieldOffsets.push_back(FieldOffset);
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Anders Carlssonbda4c102009-07-18 20:20:21 +00001006 // Reserve space for this field.
1007 if (IsUnion)
1008 Size = std::max(Size, FieldSize);
1009 else
1010 Size = FieldOffset + FieldSize;
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Anders Carlssona2239352009-09-26 01:34:51 +00001012 // Update the data size.
1013 DataSize = Size;
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Anders Carlssonbda4c102009-07-18 20:20:21 +00001015 // Remember max struct/class alignment.
1016 UpdateAlignment(FieldAlign);
1017}
1018
Anders Carlsson7d0918a2010-05-26 05:58:59 +00001019void RecordLayoutBuilder::FinishLayout() {
Anders Carlssonbda4c102009-07-18 20:20:21 +00001020 // In C++, records cannot be of size 0.
Anders Carlsson0f0e9b02010-04-16 15:07:51 +00001021 if (Context.getLangOptions().CPlusPlus && Size == 0)
Anders Carlssonbda4c102009-07-18 20:20:21 +00001022 Size = 8;
1023 // Finally, round the size of the record up to the alignment of the
1024 // record itself.
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001025 Size = llvm::RoundUpToAlignment(Size, Alignment);
Anders Carlssonbda4c102009-07-18 20:20:21 +00001026}
1027
Anders Carlsson7d0918a2010-05-26 05:58:59 +00001028void RecordLayoutBuilder::UpdateAlignment(unsigned NewAlignment) {
Anders Carlssonbda4c102009-07-18 20:20:21 +00001029 if (NewAlignment <= Alignment)
1030 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001031
Anders Carlssonbda4c102009-07-18 20:20:21 +00001032 assert(llvm::isPowerOf2_32(NewAlignment && "Alignment not a power of 2"));
Mike Stump1eb44332009-09-09 15:08:12 +00001033
Anders Carlssonbda4c102009-07-18 20:20:21 +00001034 Alignment = NewAlignment;
1035}
Mike Stump1eb44332009-09-09 15:08:12 +00001036
Anders Carlssonf53df232009-12-07 04:35:11 +00001037const CXXMethodDecl *
Anders Carlsson7d0918a2010-05-26 05:58:59 +00001038RecordLayoutBuilder::ComputeKeyFunction(const CXXRecordDecl *RD) {
Anders Carlssonf53df232009-12-07 04:35:11 +00001039 assert(RD->isDynamicClass() && "Class does not have any virtual methods!");
1040
Daniel Dunbar8d8ab742010-04-19 20:44:53 +00001041 // If a class isn't polymorphic it doesn't have a key function.
Anders Carlssonf53df232009-12-07 04:35:11 +00001042 if (!RD->isPolymorphic())
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00001043 return 0;
Eli Friedman61eab882009-12-08 03:56:49 +00001044
1045 // A class inside an anonymous namespace doesn't have a key function. (Or
1046 // at least, there's no point to assigning a key function to such a class;
1047 // this doesn't affect the ABI.)
1048 if (RD->isInAnonymousNamespace())
1049 return 0;
1050
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001051 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
1052 E = RD->method_end(); I != E; ++I) {
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00001053 const CXXMethodDecl *MD = *I;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001054
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00001055 if (!MD->isVirtual())
1056 continue;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001057
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00001058 if (MD->isPure())
1059 continue;
Eli Friedman61eab882009-12-08 03:56:49 +00001060
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001061 // Ignore implicit member functions, they are always marked as inline, but
1062 // they don't have a body until they're defined.
1063 if (MD->isImplicit())
1064 continue;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001065
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001066 if (MD->isInlineSpecified())
1067 continue;
Eli Friedmand7d7f672009-12-06 20:50:05 +00001068
1069 if (MD->hasInlineBody())
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00001070 continue;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001071
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00001072 // We found it.
1073 return MD;
1074 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001075
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00001076 return 0;
1077}
1078
Anders Carlsson1e641ce2010-05-26 04:56:53 +00001079/// getASTRecordLayout - Get or compute information about the layout of the
1080/// specified record (struct/union/class), which indicates its size and field
1081/// position information.
1082const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
1083 D = D->getDefinition();
1084 assert(D && "Cannot get layout of forward declarations!");
1085
1086 // Look up this layout, if already laid out, return what we have.
1087 // Note that we can't save a reference to the entry because this function
1088 // is recursive.
1089 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
1090 if (Entry) return *Entry;
1091
Anders Carlsson2f64e372010-05-26 05:10:47 +00001092 const ASTRecordLayout *NewEntry;
1093
1094 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
Anders Carlsson7d0918a2010-05-26 05:58:59 +00001095 RecordLayoutBuilder Builder(*this);
Anders Carlsson2f64e372010-05-26 05:10:47 +00001096 Builder.Layout(RD);
1097
1098 // FIXME: This is not always correct. See the part about bitfields at
1099 // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info.
1100 // FIXME: IsPODForThePurposeOfLayout should be stored in the record layout.
1101 bool IsPODForThePurposeOfLayout = cast<CXXRecordDecl>(D)->isPOD();
1102
1103 // FIXME: This should be done in FinalizeLayout.
1104 uint64_t DataSize =
1105 IsPODForThePurposeOfLayout ? Builder.Size : Builder.DataSize;
1106 uint64_t NonVirtualSize =
1107 IsPODForThePurposeOfLayout ? DataSize : Builder.NonVirtualSize;
1108
1109 NewEntry =
1110 new (*this) ASTRecordLayout(*this, Builder.Size, Builder.Alignment,
1111 DataSize, Builder.FieldOffsets.data(),
1112 Builder.FieldOffsets.size(),
1113 NonVirtualSize,
1114 Builder.NonVirtualAlignment,
1115 Builder.SizeOfLargestEmptySubobject,
1116 Builder.PrimaryBase,
Anders Carlsson48317102010-05-26 05:25:15 +00001117 Builder.PrimaryBaseIsVirtual,
Anders Carlsson2f64e372010-05-26 05:10:47 +00001118 Builder.Bases, Builder.VBases);
1119 } else {
Anders Carlsson7d0918a2010-05-26 05:58:59 +00001120 RecordLayoutBuilder Builder(*this);
Anders Carlsson2f64e372010-05-26 05:10:47 +00001121 Builder.Layout(D);
1122
1123 NewEntry =
1124 new (*this) ASTRecordLayout(*this, Builder.Size, Builder.Alignment,
1125 Builder.Size,
1126 Builder.FieldOffsets.data(),
1127 Builder.FieldOffsets.size());
1128 }
1129
Anders Carlsson1e641ce2010-05-26 04:56:53 +00001130 ASTRecordLayouts[D] = NewEntry;
1131
1132 if (getLangOptions().DumpRecordLayouts) {
1133 llvm::errs() << "\n*** Dumping AST Record Layout\n";
1134 DumpRecordLayout(D, llvm::errs());
1135 }
1136
1137 return *NewEntry;
1138}
1139
1140const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) {
1141 RD = cast<CXXRecordDecl>(RD->getDefinition());
1142 assert(RD && "Cannot get key function for forward declarations!");
1143
1144 const CXXMethodDecl *&Entry = KeyFunctions[RD];
1145 if (!Entry)
Anders Carlsson7d0918a2010-05-26 05:58:59 +00001146 Entry = RecordLayoutBuilder::ComputeKeyFunction(RD);
Anders Carlsson1e641ce2010-05-26 04:56:53 +00001147 else
Anders Carlsson7d0918a2010-05-26 05:58:59 +00001148 assert(Entry == RecordLayoutBuilder::ComputeKeyFunction(RD) &&
Anders Carlsson1e641ce2010-05-26 04:56:53 +00001149 "Key function changed!");
1150
1151 return Entry;
1152}
1153
1154/// getInterfaceLayoutImpl - Get or compute information about the
1155/// layout of the given interface.
1156///
1157/// \param Impl - If given, also include the layout of the interface's
1158/// implementation. This may differ by including synthesized ivars.
1159const ASTRecordLayout &
1160ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
1161 const ObjCImplementationDecl *Impl) {
1162 assert(!D->isForwardDecl() && "Invalid interface decl!");
1163
1164 // Look up this layout, if already laid out, return what we have.
1165 ObjCContainerDecl *Key =
1166 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
1167 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
1168 return *Entry;
1169
1170 // Add in synthesized ivar count if laying out an implementation.
1171 if (Impl) {
1172 unsigned SynthCount = CountNonClassIvars(D);
1173 // If there aren't any sythesized ivars then reuse the interface
1174 // entry. Note we can't cache this because we simply free all
1175 // entries later; however we shouldn't look up implementations
1176 // frequently.
1177 if (SynthCount == 0)
1178 return getObjCLayout(D, 0);
1179 }
1180
Anders Carlsson7d0918a2010-05-26 05:58:59 +00001181 RecordLayoutBuilder Builder(*this);
Anders Carlsson36cdc612010-05-26 05:04:25 +00001182 Builder.Layout(D);
1183
Anders Carlsson1e641ce2010-05-26 04:56:53 +00001184 const ASTRecordLayout *NewEntry =
Anders Carlsson36cdc612010-05-26 05:04:25 +00001185 new (*this) ASTRecordLayout(*this, Builder.Size, Builder.Alignment,
1186 Builder.DataSize,
1187 Builder.FieldOffsets.data(),
1188 Builder.FieldOffsets.size());
1189
Anders Carlsson1e641ce2010-05-26 04:56:53 +00001190 ObjCLayouts[Key] = NewEntry;
1191
1192 return *NewEntry;
1193}
1194
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001195static void PrintOffset(llvm::raw_ostream &OS,
1196 uint64_t Offset, unsigned IndentLevel) {
1197 OS << llvm::format("%4d | ", Offset);
1198 OS.indent(IndentLevel * 2);
1199}
1200
1201static void DumpCXXRecordLayout(llvm::raw_ostream &OS,
1202 const CXXRecordDecl *RD, ASTContext &C,
1203 uint64_t Offset,
1204 unsigned IndentLevel,
1205 const char* Description,
1206 bool IncludeVirtualBases) {
1207 const ASTRecordLayout &Info = C.getASTRecordLayout(RD);
1208
1209 PrintOffset(OS, Offset, IndentLevel);
Dan Gohmancb421fa2010-04-19 16:39:44 +00001210 OS << C.getTypeDeclType(const_cast<CXXRecordDecl *>(RD)).getAsString();
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001211 if (Description)
1212 OS << ' ' << Description;
1213 if (RD->isEmpty())
1214 OS << " (empty)";
1215 OS << '\n';
1216
1217 IndentLevel++;
1218
1219 const CXXRecordDecl *PrimaryBase = Info.getPrimaryBase();
1220
1221 // Vtable pointer.
1222 if (RD->isDynamicClass() && !PrimaryBase) {
1223 PrintOffset(OS, Offset, IndentLevel);
Benjamin Kramer900fc632010-04-17 09:33:03 +00001224 OS << '(' << RD << " vtable pointer)\n";
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001225 }
1226 // Dump (non-virtual) bases
1227 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1228 E = RD->bases_end(); I != E; ++I) {
1229 assert(!I->getType()->isDependentType() &&
1230 "Cannot layout class with dependent bases.");
1231 if (I->isVirtual())
1232 continue;
1233
1234 const CXXRecordDecl *Base =
1235 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1236
1237 uint64_t BaseOffset = Offset + Info.getBaseClassOffset(Base) / 8;
1238
1239 DumpCXXRecordLayout(OS, Base, C, BaseOffset, IndentLevel,
1240 Base == PrimaryBase ? "(primary base)" : "(base)",
1241 /*IncludeVirtualBases=*/false);
1242 }
1243
1244 // Dump fields.
1245 uint64_t FieldNo = 0;
1246 for (CXXRecordDecl::field_iterator I = RD->field_begin(),
1247 E = RD->field_end(); I != E; ++I, ++FieldNo) {
1248 const FieldDecl *Field = *I;
1249 uint64_t FieldOffset = Offset + Info.getFieldOffset(FieldNo) / 8;
1250
1251 if (const RecordType *RT = Field->getType()->getAs<RecordType>()) {
1252 if (const CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
1253 DumpCXXRecordLayout(OS, D, C, FieldOffset, IndentLevel,
1254 Field->getNameAsCString(),
1255 /*IncludeVirtualBases=*/true);
1256 continue;
1257 }
1258 }
1259
1260 PrintOffset(OS, FieldOffset, IndentLevel);
Benjamin Kramer900fc632010-04-17 09:33:03 +00001261 OS << Field->getType().getAsString() << ' ' << Field << '\n';
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001262 }
1263
1264 if (!IncludeVirtualBases)
1265 return;
1266
1267 // Dump virtual bases.
1268 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
1269 E = RD->vbases_end(); I != E; ++I) {
1270 assert(I->isVirtual() && "Found non-virtual class!");
1271 const CXXRecordDecl *VBase =
1272 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1273
1274 uint64_t VBaseOffset = Offset + Info.getVBaseClassOffset(VBase) / 8;
1275 DumpCXXRecordLayout(OS, VBase, C, VBaseOffset, IndentLevel,
1276 VBase == PrimaryBase ?
1277 "(primary virtual base)" : "(virtual base)",
1278 /*IncludeVirtualBases=*/false);
1279 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001280
1281 OS << " sizeof=" << Info.getSize() / 8;
1282 OS << ", dsize=" << Info.getDataSize() / 8;
1283 OS << ", align=" << Info.getAlignment() / 8 << '\n';
1284 OS << " nvsize=" << Info.getNonVirtualSize() / 8;
1285 OS << ", nvalign=" << Info.getNonVirtualAlign() / 8 << '\n';
1286 OS << '\n';
1287}
Daniel Dunbar8d8ab742010-04-19 20:44:53 +00001288
1289void ASTContext::DumpRecordLayout(const RecordDecl *RD,
1290 llvm::raw_ostream &OS) {
1291 const ASTRecordLayout &Info = getASTRecordLayout(RD);
1292
1293 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
1294 return DumpCXXRecordLayout(OS, CXXRD, *this, 0, 0, 0,
1295 /*IncludeVirtualBases=*/true);
1296
1297 OS << "Type: " << getTypeDeclType(RD).getAsString() << "\n";
1298 OS << "Record: ";
1299 RD->dump();
1300 OS << "\nLayout: ";
1301 OS << "<ASTRecordLayout\n";
1302 OS << " Size:" << Info.getSize() << "\n";
1303 OS << " DataSize:" << Info.getDataSize() << "\n";
1304 OS << " Alignment:" << Info.getAlignment() << "\n";
1305 OS << " FieldOffsets: [";
1306 for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i) {
1307 if (i) OS << ", ";
1308 OS << Info.getFieldOffset(i);
1309 }
1310 OS << "]>\n";
1311}