blob: 24ddea84d766e6e43a61a630690d4e56a982b7be [file] [log] [blame]
Anders Carlsson35a36eb2010-05-26 05:41:04 +00001//=== RecordLayoutBuilder.cpp - Helper class for building record layouts ---==//
Anders Carlsson79474332009-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 Carlsson79474332009-07-18 20:20:21 +000010#include "clang/AST/Attr.h"
11#include "clang/AST/Decl.h"
Anders Carlsson6d9f6f32009-07-19 00:18:47 +000012#include "clang/AST/DeclCXX.h"
Anders Carlsson4f516282009-07-18 20:50:59 +000013#include "clang/AST/DeclObjC.h"
Anders Carlsson79474332009-07-18 20:20:21 +000014#include "clang/AST/Expr.h"
Anders Carlsson35a36eb2010-05-26 05:41:04 +000015#include "clang/AST/RecordLayout.h"
Anders Carlsson79474332009-07-18 20:20:21 +000016#include "clang/Basic/TargetInfo.h"
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +000017#include "clang/Sema/SemaDiagnostic.h"
Daniel Dunbaraa423af2010-04-08 02:59:49 +000018#include "llvm/Support/Format.h"
19#include "llvm/ADT/SmallSet.h"
20#include "llvm/Support/MathExtras.h"
Anders Carlsson35a36eb2010-05-26 05:41:04 +000021#include <map>
Anders Carlsson79474332009-07-18 20:20:21 +000022
23using namespace clang;
24
Benjamin Kramerc7656cd2010-05-26 09:58:31 +000025namespace {
Anders Carlssonf58de112010-05-26 15:32:58 +000026
Anders Carlssona7f3cdb2010-05-28 21:24:37 +000027/// BaseSubobjectInfo - Represents a single base subobject in a complete class.
28/// For a class hierarchy like
29///
30/// class A { };
31/// class B : A { };
32/// class C : A, B { };
33///
34/// The BaseSubobjectInfo graph for C will have three BaseSubobjectInfo
35/// instances, one for B and two for A.
36///
37/// If a base is virtual, it will only have one BaseSubobjectInfo allocated.
38struct BaseSubobjectInfo {
39 /// Class - The class for this base info.
Anders Carlsson056818f2010-05-28 21:13:31 +000040 const CXXRecordDecl *Class;
Anders Carlssona7f3cdb2010-05-28 21:24:37 +000041
42 /// IsVirtual - Whether the BaseInfo represents a virtual base or not.
Anders Carlsson056818f2010-05-28 21:13:31 +000043 bool IsVirtual;
44
Anders Carlssona7f3cdb2010-05-28 21:24:37 +000045 /// Bases - Information about the base subobjects.
46 llvm::SmallVector<BaseSubobjectInfo*, 4> Bases;
47
Anders Carlssone3c24c72010-05-29 17:35:14 +000048 /// PrimaryVirtualBaseInfo - Holds the base info for the primary virtual base
49 /// of this base info (if one exists).
50 BaseSubobjectInfo *PrimaryVirtualBaseInfo;
Anders Carlssona7f3cdb2010-05-28 21:24:37 +000051
52 // FIXME: Document.
53 const BaseSubobjectInfo *Derived;
Anders Carlsson056818f2010-05-28 21:13:31 +000054};
55
Anders Carlssonf58de112010-05-26 15:32:58 +000056/// EmptySubobjectMap - Keeps track of which empty subobjects exist at different
57/// offsets while laying out a C++ class.
58class EmptySubobjectMap {
59 ASTContext &Context;
Daniel Dunbar592a85c2010-05-27 02:25:46 +000060
Anders Carlssonf58de112010-05-26 15:32:58 +000061 /// Class - The class whose empty entries we're keeping track of.
62 const CXXRecordDecl *Class;
Daniel Dunbar592a85c2010-05-27 02:25:46 +000063
Anders Carlsson439edd12010-05-27 05:41:06 +000064 /// EmptyClassOffsets - A map from offsets to empty record decls.
65 typedef llvm::SmallVector<const CXXRecordDecl *, 1> ClassVectorTy;
Anders Carlssonf8f756d2010-10-31 21:22:43 +000066 typedef llvm::DenseMap<CharUnits, ClassVectorTy> EmptyClassOffsetsMapTy;
Anders Carlsson439edd12010-05-27 05:41:06 +000067 EmptyClassOffsetsMapTy EmptyClassOffsets;
68
Anders Carlssoncc5de092010-06-08 15:56:03 +000069 /// MaxEmptyClassOffset - The highest offset known to contain an empty
70 /// base subobject.
71 uint64_t MaxEmptyClassOffset;
72
Daniel Dunbar592a85c2010-05-27 02:25:46 +000073 /// ComputeEmptySubobjectSizes - Compute the size of the largest base or
Anders Carlssonc5ca1f72010-05-26 15:54:25 +000074 /// member subobject that is empty.
75 void ComputeEmptySubobjectSizes();
Anders Carlsson439edd12010-05-27 05:41:06 +000076
Anders Carlssondb319762010-05-27 18:20:57 +000077 void AddSubobjectAtOffset(const CXXRecordDecl *RD, uint64_t Offset);
78
Anders Carlssona7f3cdb2010-05-28 21:24:37 +000079 void UpdateEmptyBaseSubobjects(const BaseSubobjectInfo *Info,
Anders Carlssoncc59cc52010-06-13 18:00:18 +000080 uint64_t Offset, bool PlacingEmptyBase);
Anders Carlsson439edd12010-05-27 05:41:06 +000081
Anders Carlssondb319762010-05-27 18:20:57 +000082 void UpdateEmptyFieldSubobjects(const CXXRecordDecl *RD,
83 const CXXRecordDecl *Class,
84 uint64_t Offset);
85 void UpdateEmptyFieldSubobjects(const FieldDecl *FD, uint64_t Offset);
86
Anders Carlssoncc5de092010-06-08 15:56:03 +000087 /// AnyEmptySubobjectsBeyondOffset - Returns whether there are any empty
88 /// subobjects beyond the given offset.
89 bool AnyEmptySubobjectsBeyondOffset(uint64_t Offset) const {
90 return Offset <= MaxEmptyClassOffset;
91 }
92
Anders Carlssonf8f756d2010-10-31 21:22:43 +000093 // FIXME: Remove these.
94 CharUnits toCharUnits(uint64_t Offset) const {
95 return CharUnits::fromQuantity(Offset / Context.getCharWidth());
96 }
97 uint64_t toOffset(CharUnits Offset) const {
98 return Offset.getQuantity() * Context.getCharWidth();
99 }
100
Charles Davisc2c576a2010-08-19 00:55:19 +0000101protected:
102 bool CanPlaceSubobjectAtOffset(const CXXRecordDecl *RD,
103 uint64_t Offset) const;
104
105 bool CanPlaceBaseSubobjectAtOffset(const BaseSubobjectInfo *Info,
106 uint64_t Offset);
107
108 bool CanPlaceFieldSubobjectAtOffset(const CXXRecordDecl *RD,
109 const CXXRecordDecl *Class,
110 uint64_t Offset) const;
111 bool CanPlaceFieldSubobjectAtOffset(const FieldDecl *FD,
112 uint64_t Offset) const;
113
Anders Carlssonf58de112010-05-26 15:32:58 +0000114public:
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000115 /// This holds the size of the largest empty subobject (either a base
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000116 /// or a member). Will be zero if the record being built doesn't contain
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000117 /// any empty classes.
118 uint64_t SizeOfLargestEmptySubobject;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000119
Anders Carlssonf58de112010-05-26 15:32:58 +0000120 EmptySubobjectMap(ASTContext &Context, const CXXRecordDecl *Class)
Anders Carlsson45c1d282010-06-08 16:20:35 +0000121 : Context(Context), Class(Class), MaxEmptyClassOffset(0),
122 SizeOfLargestEmptySubobject(0) {
Anders Carlssonc121b4e2010-05-27 00:07:01 +0000123 ComputeEmptySubobjectSizes();
124 }
125
126 /// CanPlaceBaseAtOffset - Return whether the given base class can be placed
127 /// at the given offset.
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000128 /// Returns false if placing the record will result in two components
Anders Carlssonc121b4e2010-05-27 00:07:01 +0000129 /// (direct or indirect) of the same type having the same offset.
Anders Carlssoncc5de092010-06-08 15:56:03 +0000130 bool CanPlaceBaseAtOffset(const BaseSubobjectInfo *Info,
131 uint64_t Offset);
Anders Carlssondb319762010-05-27 18:20:57 +0000132
133 /// CanPlaceFieldAtOffset - Return whether a field can be placed at the given
134 /// offset.
135 bool CanPlaceFieldAtOffset(const FieldDecl *FD, uint64_t Offset);
Anders Carlssonf58de112010-05-26 15:32:58 +0000136};
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000137
138void EmptySubobjectMap::ComputeEmptySubobjectSizes() {
139 // Check the bases.
140 for (CXXRecordDecl::base_class_const_iterator I = Class->bases_begin(),
141 E = Class->bases_end(); I != E; ++I) {
142 const CXXRecordDecl *BaseDecl =
143 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
144
145 uint64_t EmptySize = 0;
146 const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl);
147 if (BaseDecl->isEmpty()) {
148 // If the class decl is empty, get its size.
149 EmptySize = Layout.getSize();
150 } else {
151 // Otherwise, we get the largest empty subobject for the decl.
152 EmptySize = Layout.getSizeOfLargestEmptySubobject();
153 }
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000154
155 SizeOfLargestEmptySubobject = std::max(SizeOfLargestEmptySubobject,
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000156 EmptySize);
157 }
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000158
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000159 // Check the fields.
160 for (CXXRecordDecl::field_iterator I = Class->field_begin(),
161 E = Class->field_end(); I != E; ++I) {
162 const FieldDecl *FD = *I;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000163
164 const RecordType *RT =
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000165 Context.getBaseElementType(FD->getType())->getAs<RecordType>();
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000166
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000167 // We only care about record types.
168 if (!RT)
169 continue;
170
171 uint64_t EmptySize = 0;
172 const CXXRecordDecl *MemberDecl = cast<CXXRecordDecl>(RT->getDecl());
173 const ASTRecordLayout &Layout = Context.getASTRecordLayout(MemberDecl);
174 if (MemberDecl->isEmpty()) {
175 // If the class decl is empty, get its size.
176 EmptySize = Layout.getSize();
177 } else {
178 // Otherwise, we get the largest empty subobject for the decl.
179 EmptySize = Layout.getSizeOfLargestEmptySubobject();
180 }
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000181
182 SizeOfLargestEmptySubobject = std::max(SizeOfLargestEmptySubobject,
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000183 EmptySize);
184 }
185}
186
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000187bool
Anders Carlssondb319762010-05-27 18:20:57 +0000188EmptySubobjectMap::CanPlaceSubobjectAtOffset(const CXXRecordDecl *RD,
Anders Carlssoncc5de092010-06-08 15:56:03 +0000189 uint64_t Offset) const {
Anders Carlssondb319762010-05-27 18:20:57 +0000190 // We only need to check empty bases.
191 if (!RD->isEmpty())
192 return true;
193
Anders Carlssonf8f756d2010-10-31 21:22:43 +0000194 EmptyClassOffsetsMapTy::const_iterator I =
195 EmptyClassOffsets.find(toCharUnits(Offset));
Anders Carlssondb319762010-05-27 18:20:57 +0000196 if (I == EmptyClassOffsets.end())
197 return true;
198
199 const ClassVectorTy& Classes = I->second;
200 if (std::find(Classes.begin(), Classes.end(), RD) == Classes.end())
201 return true;
202
203 // There is already an empty class of the same type at this offset.
204 return false;
205}
206
207void EmptySubobjectMap::AddSubobjectAtOffset(const CXXRecordDecl *RD,
208 uint64_t Offset) {
209 // We only care about empty bases.
210 if (!RD->isEmpty())
211 return;
212
Anders Carlssonf8f756d2010-10-31 21:22:43 +0000213 ClassVectorTy& Classes = EmptyClassOffsets[toCharUnits(Offset)];
Anders Carlssondb319762010-05-27 18:20:57 +0000214 assert(std::find(Classes.begin(), Classes.end(), RD) == Classes.end() &&
215 "Duplicate empty class detected!");
Anders Carlsson44687202010-06-08 19:09:24 +0000216
Anders Carlssondb319762010-05-27 18:20:57 +0000217 Classes.push_back(RD);
Anders Carlssoncc5de092010-06-08 15:56:03 +0000218
219 // Update the empty class offset.
220 MaxEmptyClassOffset = std::max(MaxEmptyClassOffset, Offset);
Anders Carlssondb319762010-05-27 18:20:57 +0000221}
222
223bool
Anders Carlssona7f3cdb2010-05-28 21:24:37 +0000224EmptySubobjectMap::CanPlaceBaseSubobjectAtOffset(const BaseSubobjectInfo *Info,
Anders Carlsson439edd12010-05-27 05:41:06 +0000225 uint64_t Offset) {
Anders Carlsson45c1d282010-06-08 16:20:35 +0000226 // We don't have to keep looking past the maximum offset that's known to
227 // contain an empty class.
228 if (!AnyEmptySubobjectsBeyondOffset(Offset))
229 return true;
230
Anders Carlssondb319762010-05-27 18:20:57 +0000231 if (!CanPlaceSubobjectAtOffset(Info->Class, Offset))
232 return false;
233
Anders Carlsson439edd12010-05-27 05:41:06 +0000234 // Traverse all non-virtual bases.
Anders Carlssona7774a62010-05-29 21:10:24 +0000235 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class);
Anders Carlsson439edd12010-05-27 05:41:06 +0000236 for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) {
Anders Carlssona7f3cdb2010-05-28 21:24:37 +0000237 BaseSubobjectInfo* Base = Info->Bases[I];
Anders Carlsson439edd12010-05-27 05:41:06 +0000238 if (Base->IsVirtual)
239 continue;
240
Anders Carlsson439edd12010-05-27 05:41:06 +0000241 uint64_t BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class);
242
243 if (!CanPlaceBaseSubobjectAtOffset(Base, BaseOffset))
244 return false;
245 }
246
Anders Carlssone3c24c72010-05-29 17:35:14 +0000247 if (Info->PrimaryVirtualBaseInfo) {
248 BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo;
Anders Carlsson439edd12010-05-27 05:41:06 +0000249
250 if (Info == PrimaryVirtualBaseInfo->Derived) {
251 if (!CanPlaceBaseSubobjectAtOffset(PrimaryVirtualBaseInfo, Offset))
252 return false;
253 }
254 }
255
Anders Carlssondb319762010-05-27 18:20:57 +0000256 // Traverse all member variables.
257 unsigned FieldNo = 0;
258 for (CXXRecordDecl::field_iterator I = Info->Class->field_begin(),
259 E = Info->Class->field_end(); I != E; ++I, ++FieldNo) {
260 const FieldDecl *FD = *I;
Anders Carlssona7774a62010-05-29 21:10:24 +0000261
Anders Carlssondb319762010-05-27 18:20:57 +0000262 uint64_t FieldOffset = Offset + Layout.getFieldOffset(FieldNo);
Anders Carlssondb319762010-05-27 18:20:57 +0000263 if (!CanPlaceFieldSubobjectAtOffset(FD, FieldOffset))
264 return false;
265 }
266
Anders Carlsson439edd12010-05-27 05:41:06 +0000267 return true;
268}
269
Anders Carlssona7f3cdb2010-05-28 21:24:37 +0000270void EmptySubobjectMap::UpdateEmptyBaseSubobjects(const BaseSubobjectInfo *Info,
Anders Carlssoncc59cc52010-06-13 18:00:18 +0000271 uint64_t Offset,
272 bool PlacingEmptyBase) {
273 if (!PlacingEmptyBase && Offset >= SizeOfLargestEmptySubobject) {
274 // We know that the only empty subobjects that can conflict with empty
275 // subobject of non-empty bases, are empty bases that can be placed at
276 // offset zero. Because of this, we only need to keep track of empty base
277 // subobjects with offsets less than the size of the largest empty
278 // subobject for our class.
279 return;
280 }
281
Anders Carlssondb319762010-05-27 18:20:57 +0000282 AddSubobjectAtOffset(Info->Class, Offset);
Anders Carlssona7774a62010-05-29 21:10:24 +0000283
Anders Carlsson439edd12010-05-27 05:41:06 +0000284 // Traverse all non-virtual bases.
Anders Carlssona7774a62010-05-29 21:10:24 +0000285 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class);
Anders Carlsson439edd12010-05-27 05:41:06 +0000286 for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) {
Anders Carlssona7f3cdb2010-05-28 21:24:37 +0000287 BaseSubobjectInfo* Base = Info->Bases[I];
Anders Carlsson439edd12010-05-27 05:41:06 +0000288 if (Base->IsVirtual)
289 continue;
Anders Carlssona7774a62010-05-29 21:10:24 +0000290
Anders Carlsson439edd12010-05-27 05:41:06 +0000291 uint64_t BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class);
Anders Carlssoncc59cc52010-06-13 18:00:18 +0000292 UpdateEmptyBaseSubobjects(Base, BaseOffset, PlacingEmptyBase);
Anders Carlsson439edd12010-05-27 05:41:06 +0000293 }
294
Anders Carlssone3c24c72010-05-29 17:35:14 +0000295 if (Info->PrimaryVirtualBaseInfo) {
296 BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo;
Anders Carlsson439edd12010-05-27 05:41:06 +0000297
298 if (Info == PrimaryVirtualBaseInfo->Derived)
Anders Carlssoncc59cc52010-06-13 18:00:18 +0000299 UpdateEmptyBaseSubobjects(PrimaryVirtualBaseInfo, Offset,
300 PlacingEmptyBase);
Anders Carlsson439edd12010-05-27 05:41:06 +0000301 }
Anders Carlssondb319762010-05-27 18:20:57 +0000302
Anders Carlssondb319762010-05-27 18:20:57 +0000303 // Traverse all member variables.
304 unsigned FieldNo = 0;
305 for (CXXRecordDecl::field_iterator I = Info->Class->field_begin(),
306 E = Info->Class->field_end(); I != E; ++I, ++FieldNo) {
307 const FieldDecl *FD = *I;
Anders Carlssona7774a62010-05-29 21:10:24 +0000308
Anders Carlssondb319762010-05-27 18:20:57 +0000309 uint64_t FieldOffset = Offset + Layout.getFieldOffset(FieldNo);
Anders Carlssondb319762010-05-27 18:20:57 +0000310 UpdateEmptyFieldSubobjects(FD, FieldOffset);
311 }
Anders Carlsson439edd12010-05-27 05:41:06 +0000312}
313
Anders Carlssona60b86a2010-05-29 20:49:49 +0000314bool EmptySubobjectMap::CanPlaceBaseAtOffset(const BaseSubobjectInfo *Info,
Anders Carlsson439edd12010-05-27 05:41:06 +0000315 uint64_t Offset) {
Anders Carlssonc121b4e2010-05-27 00:07:01 +0000316 // If we know this class doesn't have any empty subobjects we don't need to
317 // bother checking.
318 if (!SizeOfLargestEmptySubobject)
319 return true;
320
Anders Carlsson439edd12010-05-27 05:41:06 +0000321 if (!CanPlaceBaseSubobjectAtOffset(Info, Offset))
322 return false;
Anders Carlssondb319762010-05-27 18:20:57 +0000323
324 // We are able to place the base at this offset. Make sure to update the
325 // empty base subobject map.
Anders Carlssoncc59cc52010-06-13 18:00:18 +0000326 UpdateEmptyBaseSubobjects(Info, Offset, Info->Class->isEmpty());
Anders Carlssonc121b4e2010-05-27 00:07:01 +0000327 return true;
328}
329
Anders Carlssondb319762010-05-27 18:20:57 +0000330bool
331EmptySubobjectMap::CanPlaceFieldSubobjectAtOffset(const CXXRecordDecl *RD,
332 const CXXRecordDecl *Class,
Anders Carlssoncc5de092010-06-08 15:56:03 +0000333 uint64_t Offset) const {
Anders Carlsson45c1d282010-06-08 16:20:35 +0000334 // We don't have to keep looking past the maximum offset that's known to
335 // contain an empty class.
336 if (!AnyEmptySubobjectsBeyondOffset(Offset))
337 return true;
338
Anders Carlssondb319762010-05-27 18:20:57 +0000339 if (!CanPlaceSubobjectAtOffset(RD, Offset))
340 return false;
341
342 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
343
344 // Traverse all non-virtual bases.
345 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
346 E = RD->bases_end(); I != E; ++I) {
347 if (I->isVirtual())
348 continue;
349
350 const CXXRecordDecl *BaseDecl =
351 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
352
353 uint64_t BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl);
354 if (!CanPlaceFieldSubobjectAtOffset(BaseDecl, Class, BaseOffset))
355 return false;
356 }
357
Anders Carlsson44687202010-06-08 19:09:24 +0000358 if (RD == Class) {
359 // This is the most derived class, traverse virtual bases as well.
360 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
361 E = RD->vbases_end(); I != E; ++I) {
362 const CXXRecordDecl *VBaseDecl =
363 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
364
365 uint64_t VBaseOffset = Offset + Layout.getVBaseClassOffset(VBaseDecl);
366 if (!CanPlaceFieldSubobjectAtOffset(VBaseDecl, Class, VBaseOffset))
367 return false;
368 }
369 }
370
Anders Carlssondb319762010-05-27 18:20:57 +0000371 // Traverse all member variables.
372 unsigned FieldNo = 0;
373 for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
374 I != E; ++I, ++FieldNo) {
375 const FieldDecl *FD = *I;
376
377 uint64_t FieldOffset = Offset + Layout.getFieldOffset(FieldNo);
378
379 if (!CanPlaceFieldSubobjectAtOffset(FD, FieldOffset))
380 return false;
381 }
382
383 return true;
384}
385
386bool EmptySubobjectMap::CanPlaceFieldSubobjectAtOffset(const FieldDecl *FD,
Anders Carlssoncc5de092010-06-08 15:56:03 +0000387 uint64_t Offset) const {
Anders Carlsson45c1d282010-06-08 16:20:35 +0000388 // We don't have to keep looking past the maximum offset that's known to
389 // contain an empty class.
390 if (!AnyEmptySubobjectsBeyondOffset(Offset))
391 return true;
392
Anders Carlssondb319762010-05-27 18:20:57 +0000393 QualType T = FD->getType();
394 if (const RecordType *RT = T->getAs<RecordType>()) {
395 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
396 return CanPlaceFieldSubobjectAtOffset(RD, RD, Offset);
397 }
398
399 // If we have an array type we need to look at every element.
400 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) {
401 QualType ElemTy = Context.getBaseElementType(AT);
402 const RecordType *RT = ElemTy->getAs<RecordType>();
403 if (!RT)
404 return true;
405
406 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
407 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
408
409 uint64_t NumElements = Context.getConstantArrayElementCount(AT);
410 uint64_t ElementOffset = Offset;
411 for (uint64_t I = 0; I != NumElements; ++I) {
Anders Carlsson45c1d282010-06-08 16:20:35 +0000412 // We don't have to keep looking past the maximum offset that's known to
413 // contain an empty class.
414 if (!AnyEmptySubobjectsBeyondOffset(ElementOffset))
415 return true;
416
Anders Carlssondb319762010-05-27 18:20:57 +0000417 if (!CanPlaceFieldSubobjectAtOffset(RD, RD, ElementOffset))
418 return false;
419
420 ElementOffset += Layout.getSize();
421 }
422 }
423
424 return true;
425}
426
427bool
428EmptySubobjectMap::CanPlaceFieldAtOffset(const FieldDecl *FD, uint64_t Offset) {
429 if (!CanPlaceFieldSubobjectAtOffset(FD, Offset))
430 return false;
431
432 // We are able to place the member variable at this offset.
433 // Make sure to update the empty base subobject map.
434 UpdateEmptyFieldSubobjects(FD, Offset);
435 return true;
436}
437
438void EmptySubobjectMap::UpdateEmptyFieldSubobjects(const CXXRecordDecl *RD,
439 const CXXRecordDecl *Class,
440 uint64_t Offset) {
Anders Carlssonae111dc2010-06-13 17:49:16 +0000441 // We know that the only empty subobjects that can conflict with empty
Anders Carlssoncc59cc52010-06-13 18:00:18 +0000442 // field subobjects are subobjects of empty bases that can be placed at offset
Anders Carlssonae111dc2010-06-13 17:49:16 +0000443 // zero. Because of this, we only need to keep track of empty field
444 // subobjects with offsets less than the size of the largest empty
445 // subobject for our class.
446 if (Offset >= SizeOfLargestEmptySubobject)
447 return;
448
Anders Carlssondb319762010-05-27 18:20:57 +0000449 AddSubobjectAtOffset(RD, Offset);
450
451 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
452
453 // Traverse all non-virtual bases.
454 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
455 E = RD->bases_end(); I != E; ++I) {
456 if (I->isVirtual())
457 continue;
458
459 const CXXRecordDecl *BaseDecl =
460 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
461
462 uint64_t BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl);
463 UpdateEmptyFieldSubobjects(BaseDecl, Class, BaseOffset);
464 }
465
Anders Carlsson44687202010-06-08 19:09:24 +0000466 if (RD == Class) {
467 // This is the most derived class, traverse virtual bases as well.
468 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
469 E = RD->vbases_end(); I != E; ++I) {
470 const CXXRecordDecl *VBaseDecl =
471 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
472
473 uint64_t VBaseOffset = Offset + Layout.getVBaseClassOffset(VBaseDecl);
474 UpdateEmptyFieldSubobjects(VBaseDecl, Class, VBaseOffset);
475 }
476 }
477
Anders Carlssondb319762010-05-27 18:20:57 +0000478 // Traverse all member variables.
479 unsigned FieldNo = 0;
480 for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
481 I != E; ++I, ++FieldNo) {
482 const FieldDecl *FD = *I;
483
484 uint64_t FieldOffset = Offset + Layout.getFieldOffset(FieldNo);
485
486 UpdateEmptyFieldSubobjects(FD, FieldOffset);
487 }
488}
489
490void EmptySubobjectMap::UpdateEmptyFieldSubobjects(const FieldDecl *FD,
491 uint64_t Offset) {
492 QualType T = FD->getType();
493 if (const RecordType *RT = T->getAs<RecordType>()) {
494 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
495 UpdateEmptyFieldSubobjects(RD, RD, Offset);
496 return;
497 }
498
499 // If we have an array type we need to update every element.
500 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) {
501 QualType ElemTy = Context.getBaseElementType(AT);
502 const RecordType *RT = ElemTy->getAs<RecordType>();
503 if (!RT)
504 return;
505
506 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
507 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
508
509 uint64_t NumElements = Context.getConstantArrayElementCount(AT);
510 uint64_t ElementOffset = Offset;
511
512 for (uint64_t I = 0; I != NumElements; ++I) {
Anders Carlssonae111dc2010-06-13 17:49:16 +0000513 // We know that the only empty subobjects that can conflict with empty
Anders Carlssoncc59cc52010-06-13 18:00:18 +0000514 // field subobjects are subobjects of empty bases that can be placed at
Anders Carlssonae111dc2010-06-13 17:49:16 +0000515 // offset zero. Because of this, we only need to keep track of empty field
516 // subobjects with offsets less than the size of the largest empty
517 // subobject for our class.
518 if (ElementOffset >= SizeOfLargestEmptySubobject)
519 return;
520
Anders Carlssondb319762010-05-27 18:20:57 +0000521 UpdateEmptyFieldSubobjects(RD, RD, ElementOffset);
522 ElementOffset += Layout.getSize();
523 }
524 }
525}
526
Anders Carlssonc2226202010-05-26 05:58:59 +0000527class RecordLayoutBuilder {
Charles Davisc2c576a2010-08-19 00:55:19 +0000528protected:
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000529 // FIXME: Remove this and make the appropriate fields public.
530 friend class clang::ASTContext;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000531
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000532 ASTContext &Context;
533
Anders Carlssonf58de112010-05-26 15:32:58 +0000534 EmptySubobjectMap *EmptySubobjects;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000535
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000536 /// Size - The current size of the record layout.
537 uint64_t Size;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000538
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000539 /// Alignment - The current alignment of the record layout.
540 unsigned Alignment;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000541
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000542 /// \brief The alignment if attribute packed is not used.
543 unsigned UnpackedAlignment;
544
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000545 llvm::SmallVector<uint64_t, 16> FieldOffsets;
546
547 /// Packed - Whether the record is packed or not.
Daniel Dunbar6da10982010-05-27 05:45:51 +0000548 unsigned Packed : 1;
549
550 unsigned IsUnion : 1;
551
552 unsigned IsMac68kAlign : 1;
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000553
554 /// UnfilledBitsInLastByte - If the last field laid out was a bitfield,
555 /// this contains the number of bits in the last byte that can be used for
556 /// an adjacent bitfield if necessary.
557 unsigned char UnfilledBitsInLastByte;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000558
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000559 /// MaxFieldAlignment - The maximum allowed field alignment. This is set by
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000560 /// #pragma pack.
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000561 unsigned MaxFieldAlignment;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000562
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000563 /// DataSize - The data size of the record being laid out.
564 uint64_t DataSize;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000565
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000566 uint64_t NonVirtualSize;
567 unsigned NonVirtualAlignment;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000568
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000569 /// PrimaryBase - the primary base class (if one exists) of the class
570 /// we're laying out.
571 const CXXRecordDecl *PrimaryBase;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000572
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000573 /// PrimaryBaseIsVirtual - Whether the primary base of the class we're laying
574 /// out is virtual.
575 bool PrimaryBaseIsVirtual;
576
Anders Carlsson22f57202010-10-31 21:01:46 +0000577 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetsMapTy;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000578
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000579 /// Bases - base classes and their offsets in the record.
580 BaseOffsetsMapTy Bases;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000581
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000582 // VBases - virtual base classes and their offsets in the record.
583 BaseOffsetsMapTy VBases;
584
585 /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are
586 /// primary base classes for some other direct or indirect base class.
587 llvm::SmallSet<const CXXRecordDecl*, 32> IndirectPrimaryBases;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000588
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000589 /// FirstNearlyEmptyVBase - The first nearly empty virtual base class in
590 /// inheritance graph order. Used for determining the primary base class.
591 const CXXRecordDecl *FirstNearlyEmptyVBase;
592
593 /// VisitedVirtualBases - A set of all the visited virtual bases, used to
594 /// avoid visiting virtual bases more than once.
595 llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000596
Anders Carlssonc121b4e2010-05-27 00:07:01 +0000597 RecordLayoutBuilder(ASTContext &Context, EmptySubobjectMap *EmptySubobjects)
598 : Context(Context), EmptySubobjects(EmptySubobjects), Size(0), Alignment(8),
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000599 UnpackedAlignment(Alignment), Packed(false), IsUnion(false),
600 IsMac68kAlign(false), UnfilledBitsInLastByte(0), MaxFieldAlignment(0),
601 DataSize(0), NonVirtualSize(0), NonVirtualAlignment(8), PrimaryBase(0),
Daniel Dunbar6da10982010-05-27 05:45:51 +0000602 PrimaryBaseIsVirtual(false), FirstNearlyEmptyVBase(0) { }
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000603
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000604 void Layout(const RecordDecl *D);
Anders Carlssonc28a6c92010-05-26 15:10:00 +0000605 void Layout(const CXXRecordDecl *D);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000606 void Layout(const ObjCInterfaceDecl *D);
607
608 void LayoutFields(const RecordDecl *D);
609 void LayoutField(const FieldDecl *D);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000610 void LayoutWideBitField(uint64_t FieldSize, uint64_t TypeSize,
611 bool FieldPacked, const FieldDecl *D);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000612 void LayoutBitField(const FieldDecl *D);
613
Anders Carlssone3c24c72010-05-29 17:35:14 +0000614 /// BaseSubobjectInfoAllocator - Allocator for BaseSubobjectInfo objects.
615 llvm::SpecificBumpPtrAllocator<BaseSubobjectInfo> BaseSubobjectInfoAllocator;
616
617 typedef llvm::DenseMap<const CXXRecordDecl *, BaseSubobjectInfo *>
618 BaseSubobjectInfoMapTy;
619
620 /// VirtualBaseInfo - Map from all the (direct or indirect) virtual bases
621 /// of the class we're laying out to their base subobject info.
622 BaseSubobjectInfoMapTy VirtualBaseInfo;
623
624 /// NonVirtualBaseInfo - Map from all the direct non-virtual bases of the
625 /// class we're laying out to their base subobject info.
626 BaseSubobjectInfoMapTy NonVirtualBaseInfo;
627
628 /// ComputeBaseSubobjectInfo - Compute the base subobject information for the
629 /// bases of the given class.
630 void ComputeBaseSubobjectInfo(const CXXRecordDecl *RD);
631
632 /// ComputeBaseSubobjectInfo - Compute the base subobject information for a
633 /// single class and all of its base classes.
634 BaseSubobjectInfo *ComputeBaseSubobjectInfo(const CXXRecordDecl *RD,
635 bool IsVirtual,
636 BaseSubobjectInfo *Derived);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000637
638 /// DeterminePrimaryBase - Determine the primary base of the given class.
639 void DeterminePrimaryBase(const CXXRecordDecl *RD);
640
641 void SelectPrimaryVBase(const CXXRecordDecl *RD);
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000642
Charles Davisc2c576a2010-08-19 00:55:19 +0000643 virtual uint64_t GetVirtualPointersSize(const CXXRecordDecl *RD) const;
644
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000645 /// IdentifyPrimaryBases - Identify all virtual base classes, direct or
646 /// indirect, that are primary base classes for some other direct or indirect
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000647 /// base class.
648 void IdentifyPrimaryBases(const CXXRecordDecl *RD);
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000649
Charles Davisc2c576a2010-08-19 00:55:19 +0000650 virtual bool IsNearlyEmpty(const CXXRecordDecl *RD) const;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000651
652 /// LayoutNonVirtualBases - Determines the primary base class (if any) and
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000653 /// lays it out. Will then proceed to lay out all non-virtual base clasess.
654 void LayoutNonVirtualBases(const CXXRecordDecl *RD);
655
656 /// LayoutNonVirtualBase - Lays out a single non-virtual base.
Anders Carlssonbb0e6782010-05-29 17:42:25 +0000657 void LayoutNonVirtualBase(const BaseSubobjectInfo *Base);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000658
Anders Carlsson6b0d9142010-05-29 19:44:50 +0000659 void AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info,
Charles Davisc2c576a2010-08-19 00:55:19 +0000660 uint64_t Offset);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000661
662 /// LayoutVirtualBases - Lays out all the virtual bases.
663 void LayoutVirtualBases(const CXXRecordDecl *RD,
664 const CXXRecordDecl *MostDerivedClass);
665
666 /// LayoutVirtualBase - Lays out a single virtual base.
Anders Carlssond6ff5d72010-05-29 17:48:36 +0000667 void LayoutVirtualBase(const BaseSubobjectInfo *Base);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000668
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000669 /// LayoutBase - Will lay out a base and return the offset where it was
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000670 /// placed, in bits.
Anders Carlssond7f3fcf2010-05-29 20:47:33 +0000671 uint64_t LayoutBase(const BaseSubobjectInfo *Base);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000672
Anders Carlssonc28a6c92010-05-26 15:10:00 +0000673 /// InitializeLayout - Initialize record layout for the given record decl.
Daniel Dunbar6da10982010-05-27 05:45:51 +0000674 void InitializeLayout(const Decl *D);
Anders Carlssonc28a6c92010-05-26 15:10:00 +0000675
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000676 /// FinishLayout - Finalize record layout. Adjust record size based on the
677 /// alignment.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000678 void FinishLayout(const NamedDecl *D);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000679
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000680 void UpdateAlignment(unsigned NewAlignment, unsigned UnpackedNewAlignment);
681 void UpdateAlignment(unsigned NewAlignment) {
682 UpdateAlignment(NewAlignment, NewAlignment);
683 }
684
685 void CheckFieldPadding(uint64_t Offset, uint64_t UnpaddedOffset,
686 uint64_t UnpackedOffset, unsigned UnpackedAlign,
687 bool isPacked, const FieldDecl *D);
688
689 DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000690
Argyrios Kyrtzidis499e6e42010-08-15 10:17:39 +0000691 RecordLayoutBuilder(const RecordLayoutBuilder&); // DO NOT IMPLEMENT
692 void operator=(const RecordLayoutBuilder&); // DO NOT IMPLEMENT
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000693public:
694 static const CXXMethodDecl *ComputeKeyFunction(const CXXRecordDecl *RD);
Argyrios Kyrtzidis3c6cd172010-08-25 00:32:14 +0000695
696 virtual ~RecordLayoutBuilder() { }
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000697};
Benjamin Kramerc7656cd2010-05-26 09:58:31 +0000698} // end anonymous namespace
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000699
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000700/// IsNearlyEmpty - Indicates when a class has a vtable pointer, but
701/// no other data.
Anders Carlssonc2226202010-05-26 05:58:59 +0000702bool RecordLayoutBuilder::IsNearlyEmpty(const CXXRecordDecl *RD) const {
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000703 // FIXME: Audit the corners
704 if (!RD->isDynamicClass())
705 return false;
Anders Carlsson5efc56e2010-04-16 15:07:51 +0000706 const ASTRecordLayout &BaseInfo = Context.getASTRecordLayout(RD);
707 if (BaseInfo.getNonVirtualSize() == Context.Target.getPointerWidth(0))
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000708 return true;
709 return false;
710}
711
Anders Carlssonc2226202010-05-26 05:58:59 +0000712void RecordLayoutBuilder::IdentifyPrimaryBases(const CXXRecordDecl *RD) {
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000713 const ASTRecordLayout::PrimaryBaseInfo &BaseInfo =
Anders Carlsson5efc56e2010-04-16 15:07:51 +0000714 Context.getASTRecordLayout(RD).getPrimaryBaseInfo();
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000715
Anders Carlsson81430692009-09-22 03:02:06 +0000716 // If the record has a primary base class that is virtual, add it to the set
717 // of primary bases.
Anders Carlssona30c0d32009-11-27 22:14:40 +0000718 if (BaseInfo.isVirtual())
719 IndirectPrimaryBases.insert(BaseInfo.getBase());
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000720
Anders Carlsson81430692009-09-22 03:02:06 +0000721 // Now traverse all bases and find primary bases for them.
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000722 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000723 e = RD->bases_end(); i != e; ++i) {
Sebastian Redl1054fae2009-10-25 17:03:50 +0000724 assert(!i->getType()->isDependentType() &&
725 "Cannot layout class with dependent bases.");
Mike Stump11289f42009-09-09 15:08:12 +0000726 const CXXRecordDecl *Base =
Mike Stump78696a72009-08-11 04:03:59 +0000727 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000728
Mike Stump78696a72009-08-11 04:03:59 +0000729 // Only bases with virtual bases participate in computing the
730 // indirect primary virtual base classes.
Mike Stumpc2f591b2009-08-13 22:53:07 +0000731 if (Base->getNumVBases())
Anders Carlsson81430692009-09-22 03:02:06 +0000732 IdentifyPrimaryBases(Base);
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000733 }
734}
735
Anders Carlsson81430692009-09-22 03:02:06 +0000736void
Anders Carlssonc2226202010-05-26 05:58:59 +0000737RecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD) {
Anders Carlssonf2fa75b2010-03-11 03:39:12 +0000738 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000739 E = RD->bases_end(); I != E; ++I) {
Anders Carlssonf2fa75b2010-03-11 03:39:12 +0000740 assert(!I->getType()->isDependentType() &&
Sebastian Redl1054fae2009-10-25 17:03:50 +0000741 "Cannot layout class with dependent bases.");
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000742
Mike Stump11289f42009-09-09 15:08:12 +0000743 const CXXRecordDecl *Base =
Anders Carlssonf2fa75b2010-03-11 03:39:12 +0000744 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000745
Anders Carlssonf2fa75b2010-03-11 03:39:12 +0000746 // Check if this is a nearly empty virtual base.
747 if (I->isVirtual() && IsNearlyEmpty(Base)) {
748 // If it's not an indirect primary base, then we've found our primary
749 // base.
Anders Carlsson81430692009-09-22 03:02:06 +0000750 if (!IndirectPrimaryBases.count(Base)) {
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000751 PrimaryBase = Base;
752 PrimaryBaseIsVirtual = true;
Mike Stump6f3793b2009-08-12 21:50:08 +0000753 return;
754 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000755
Anders Carlssonf2fa75b2010-03-11 03:39:12 +0000756 // Is this the first nearly empty virtual base?
757 if (!FirstNearlyEmptyVBase)
758 FirstNearlyEmptyVBase = Base;
Mike Stump6f3793b2009-08-12 21:50:08 +0000759 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000760
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000761 SelectPrimaryVBase(Base);
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000762 if (PrimaryBase)
Zhongxing Xuec345b72010-02-15 04:28:35 +0000763 return;
Mike Stump6f3793b2009-08-12 21:50:08 +0000764 }
765}
766
Charles Davisc2c576a2010-08-19 00:55:19 +0000767uint64_t
768RecordLayoutBuilder::GetVirtualPointersSize(const CXXRecordDecl *RD) const {
769 return Context.Target.getPointerWidth(0);
770}
771
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000772/// DeterminePrimaryBase - Determine the primary base of the given class.
Anders Carlssonc2226202010-05-26 05:58:59 +0000773void RecordLayoutBuilder::DeterminePrimaryBase(const CXXRecordDecl *RD) {
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000774 // If the class isn't dynamic, it won't have a primary base.
775 if (!RD->isDynamicClass())
776 return;
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000777
Anders Carlsson81430692009-09-22 03:02:06 +0000778 // Compute all the primary virtual bases for all of our direct and
Mike Stump590a7c72009-08-13 23:26:06 +0000779 // indirect bases, and record all their primary virtual base classes.
Mike Stump590a7c72009-08-13 23:26:06 +0000780 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000781 e = RD->bases_end(); i != e; ++i) {
Sebastian Redl1054fae2009-10-25 17:03:50 +0000782 assert(!i->getType()->isDependentType() &&
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000783 "Cannot lay out class with dependent bases.");
Mike Stump11289f42009-09-09 15:08:12 +0000784 const CXXRecordDecl *Base =
Mike Stump590a7c72009-08-13 23:26:06 +0000785 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson81430692009-09-22 03:02:06 +0000786 IdentifyPrimaryBases(Base);
Mike Stump590a7c72009-08-13 23:26:06 +0000787 }
788
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000789 // If the record has a dynamic base class, attempt to choose a primary base
790 // class. It is the first (in direct base class order) non-virtual dynamic
Anders Carlsson81430692009-09-22 03:02:06 +0000791 // base class, if one exists.
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000792 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000793 e = RD->bases_end(); i != e; ++i) {
Anders Carlsson03ff3792009-11-27 22:05:05 +0000794 // Ignore virtual bases.
795 if (i->isVirtual())
796 continue;
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000797
Anders Carlsson03ff3792009-11-27 22:05:05 +0000798 const CXXRecordDecl *Base =
799 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
800
801 if (Base->isDynamicClass()) {
802 // We found it.
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000803 PrimaryBase = Base;
804 PrimaryBaseIsVirtual = false;
Anders Carlsson03ff3792009-11-27 22:05:05 +0000805 return;
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000806 }
807 }
808
809 // Otherwise, it is the first nearly empty virtual base that is not an
Mike Stump78696a72009-08-11 04:03:59 +0000810 // indirect primary virtual base class, if one exists.
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000811 if (RD->getNumVBases() != 0) {
812 SelectPrimaryVBase(RD);
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000813 if (PrimaryBase)
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000814 return;
815 }
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000816
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000817 // Otherwise, it is the first nearly empty virtual base that is not an
818 // indirect primary virtual base class, if one exists.
819 if (FirstNearlyEmptyVBase) {
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000820 PrimaryBase = FirstNearlyEmptyVBase;
821 PrimaryBaseIsVirtual = true;
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000822 return;
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000823 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000824
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000825 // Otherwise there is no primary base class.
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000826 assert(!PrimaryBase && "Should not get here with a primary base!");
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000827
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000828 // Allocate the virtual table pointer at offset zero.
829 assert(DataSize == 0 && "Vtable pointer must be at offset zero!");
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000830
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000831 // Update the size.
Charles Davisc2c576a2010-08-19 00:55:19 +0000832 Size += GetVirtualPointersSize(RD);
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000833 DataSize = Size;
834
835 // Update the alignment.
Anders Carlsson5efc56e2010-04-16 15:07:51 +0000836 UpdateAlignment(Context.Target.getPointerAlign(0));
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000837}
838
Anders Carlssone3c24c72010-05-29 17:35:14 +0000839BaseSubobjectInfo *
840RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD,
841 bool IsVirtual,
842 BaseSubobjectInfo *Derived) {
843 BaseSubobjectInfo *Info;
844
845 if (IsVirtual) {
846 // Check if we already have info about this virtual base.
847 BaseSubobjectInfo *&InfoSlot = VirtualBaseInfo[RD];
848 if (InfoSlot) {
849 assert(InfoSlot->Class == RD && "Wrong class for virtual base info!");
850 return InfoSlot;
851 }
852
853 // We don't, create it.
854 InfoSlot = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo;
855 Info = InfoSlot;
856 } else {
857 Info = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo;
858 }
859
860 Info->Class = RD;
861 Info->IsVirtual = IsVirtual;
862 Info->Derived = 0;
863 Info->PrimaryVirtualBaseInfo = 0;
864
865 const CXXRecordDecl *PrimaryVirtualBase = 0;
866 BaseSubobjectInfo *PrimaryVirtualBaseInfo = 0;
867
868 // Check if this base has a primary virtual base.
869 if (RD->getNumVBases()) {
870 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
871 if (Layout.getPrimaryBaseWasVirtual()) {
872 // This base does have a primary virtual base.
873 PrimaryVirtualBase = Layout.getPrimaryBase();
874 assert(PrimaryVirtualBase && "Didn't have a primary virtual base!");
875
876 // Now check if we have base subobject info about this primary base.
877 PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase);
878
879 if (PrimaryVirtualBaseInfo) {
880 if (PrimaryVirtualBaseInfo->Derived) {
881 // We did have info about this primary base, and it turns out that it
882 // has already been claimed as a primary virtual base for another
883 // base.
884 PrimaryVirtualBase = 0;
885 } else {
886 // We can claim this base as our primary base.
887 Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo;
888 PrimaryVirtualBaseInfo->Derived = Info;
889 }
890 }
891 }
892 }
893
894 // Now go through all direct bases.
895 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
896 E = RD->bases_end(); I != E; ++I) {
897 bool IsVirtual = I->isVirtual();
898
899 const CXXRecordDecl *BaseDecl =
900 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
901
902 Info->Bases.push_back(ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, Info));
903 }
904
905 if (PrimaryVirtualBase && !PrimaryVirtualBaseInfo) {
906 // Traversing the bases must have created the base info for our primary
907 // virtual base.
908 PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase);
909 assert(PrimaryVirtualBaseInfo &&
910 "Did not create a primary virtual base!");
911
912 // Claim the primary virtual base as our primary virtual base.
913 Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo;
914 PrimaryVirtualBaseInfo->Derived = Info;
915 }
916
917 return Info;
918}
919
920void RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD) {
921 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
922 E = RD->bases_end(); I != E; ++I) {
923 bool IsVirtual = I->isVirtual();
924
925 const CXXRecordDecl *BaseDecl =
926 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
927
928 // Compute the base subobject info for this base.
929 BaseSubobjectInfo *Info = ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, 0);
930
931 if (IsVirtual) {
932 // ComputeBaseInfo has already added this base for us.
933 assert(VirtualBaseInfo.count(BaseDecl) &&
934 "Did not add virtual base!");
935 } else {
936 // Add the base info to the map of non-virtual bases.
937 assert(!NonVirtualBaseInfo.count(BaseDecl) &&
938 "Non-virtual base already exists!");
939 NonVirtualBaseInfo.insert(std::make_pair(BaseDecl, Info));
940 }
941 }
942}
943
Anders Carlsson09ffa322010-03-10 22:21:28 +0000944void
Anders Carlssonc2226202010-05-26 05:58:59 +0000945RecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) {
Anders Carlssone3c24c72010-05-29 17:35:14 +0000946 // Then, determine the primary base class.
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000947 DeterminePrimaryBase(RD);
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000948
Anders Carlssone3c24c72010-05-29 17:35:14 +0000949 // Compute base subobject info.
950 ComputeBaseSubobjectInfo(RD);
951
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000952 // If we have a primary base class, lay it out.
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000953 if (PrimaryBase) {
954 if (PrimaryBaseIsVirtual) {
Anders Carlssone3c24c72010-05-29 17:35:14 +0000955 // If the primary virtual base was a primary virtual base of some other
956 // base class we'll have to steal it.
957 BaseSubobjectInfo *PrimaryBaseInfo = VirtualBaseInfo.lookup(PrimaryBase);
958 PrimaryBaseInfo->Derived = 0;
959
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000960 // We have a virtual primary base, insert it as an indirect primary base.
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000961 IndirectPrimaryBases.insert(PrimaryBase);
Anders Carlssonfe900962010-03-11 05:42:17 +0000962
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000963 assert(!VisitedVirtualBases.count(PrimaryBase) &&
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000964 "vbase already visited!");
965 VisitedVirtualBases.insert(PrimaryBase);
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000966
Anders Carlssond6ff5d72010-05-29 17:48:36 +0000967 LayoutVirtualBase(PrimaryBaseInfo);
Anders Carlssonbb0e6782010-05-29 17:42:25 +0000968 } else {
969 BaseSubobjectInfo *PrimaryBaseInfo =
970 NonVirtualBaseInfo.lookup(PrimaryBase);
971 assert(PrimaryBaseInfo &&
972 "Did not find base info for non-virtual primary base!");
973
974 LayoutNonVirtualBase(PrimaryBaseInfo);
975 }
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000976 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000977
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000978 // Now lay out the non-virtual bases.
979 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000980 E = RD->bases_end(); I != E; ++I) {
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000981
982 // Ignore virtual bases.
983 if (I->isVirtual())
984 continue;
985
Anders Carlssonbb0e6782010-05-29 17:42:25 +0000986 const CXXRecordDecl *BaseDecl =
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000987 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
988
989 // Skip the primary base.
Anders Carlssonbb0e6782010-05-29 17:42:25 +0000990 if (BaseDecl == PrimaryBase && !PrimaryBaseIsVirtual)
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000991 continue;
992
993 // Lay out the base.
Anders Carlssonbb0e6782010-05-29 17:42:25 +0000994 BaseSubobjectInfo *BaseInfo = NonVirtualBaseInfo.lookup(BaseDecl);
995 assert(BaseInfo && "Did not find base info for non-virtual base!");
996
997 LayoutNonVirtualBase(BaseInfo);
Anders Carlsson09ffa322010-03-10 22:21:28 +0000998 }
999}
1000
Anders Carlssonbb0e6782010-05-29 17:42:25 +00001001void RecordLayoutBuilder::LayoutNonVirtualBase(const BaseSubobjectInfo *Base) {
Anders Carlsson0d0b5882010-03-10 22:26:24 +00001002 // Layout the base.
Anders Carlssond7f3fcf2010-05-29 20:47:33 +00001003 uint64_t Offset = LayoutBase(Base);
Anders Carlsson22f57202010-10-31 21:01:46 +00001004 CharUnits OffsetInChars =
1005 CharUnits::fromQuantity(Offset / Context.getCharWidth());
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001006
Anders Carlsson0d0b5882010-03-10 22:26:24 +00001007 // Add its base class offset.
Anders Carlssonbb0e6782010-05-29 17:42:25 +00001008 assert(!Bases.count(Base->Class) && "base offset already exists!");
Anders Carlsson22f57202010-10-31 21:01:46 +00001009 Bases.insert(std::make_pair(Base->Class, OffsetInChars));
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001010
1011 AddPrimaryVirtualBaseOffsets(Base, Offset);
Anders Carlsson09ffa322010-03-10 22:21:28 +00001012}
Mike Stump2b84dd32009-11-05 04:02:15 +00001013
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001014void
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001015RecordLayoutBuilder::AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info,
1016 uint64_t Offset) {
1017 // This base isn't interesting, it has no virtual bases.
1018 if (!Info->Class->getNumVBases())
1019 return;
1020
1021 // First, check if we have a virtual primary base to add offsets for.
1022 if (Info->PrimaryVirtualBaseInfo) {
1023 assert(Info->PrimaryVirtualBaseInfo->IsVirtual &&
1024 "Primary virtual base is not virtual!");
1025 if (Info->PrimaryVirtualBaseInfo->Derived == Info) {
1026 // Add the offset.
1027 assert(!VBases.count(Info->PrimaryVirtualBaseInfo->Class) &&
1028 "primary vbase offset already exists!");
Anders Carlsson22f57202010-10-31 21:01:46 +00001029 CharUnits OffsetInChars =
1030 CharUnits::fromQuantity(Offset / Context.getCharWidth());
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001031 VBases.insert(std::make_pair(Info->PrimaryVirtualBaseInfo->Class,
Anders Carlsson22f57202010-10-31 21:01:46 +00001032 OffsetInChars));
Anders Carlssonea7b1822010-04-15 16:12:58 +00001033
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001034 // Traverse the primary virtual base.
1035 AddPrimaryVirtualBaseOffsets(Info->PrimaryVirtualBaseInfo, Offset);
1036 }
Anders Carlssonea7b1822010-04-15 16:12:58 +00001037 }
1038
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001039 // Now go through all direct non-virtual bases.
1040 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class);
1041 for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) {
1042 const BaseSubobjectInfo *Base = Info->Bases[I];
1043 if (Base->IsVirtual)
Anders Carlssonea7b1822010-04-15 16:12:58 +00001044 continue;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001045
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001046 uint64_t BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class);
1047 AddPrimaryVirtualBaseOffsets(Base, BaseOffset);
Anders Carlssonea7b1822010-04-15 16:12:58 +00001048 }
1049}
1050
1051void
Anders Carlssonc2226202010-05-26 05:58:59 +00001052RecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
Anders Carlssonea7b1822010-04-15 16:12:58 +00001053 const CXXRecordDecl *MostDerivedClass) {
Anders Carlssonde710c92010-03-11 04:33:54 +00001054 const CXXRecordDecl *PrimaryBase;
Anders Carlsson291279e2010-04-10 18:42:27 +00001055 bool PrimaryBaseIsVirtual;
Anders Carlssonfe900962010-03-11 05:42:17 +00001056
Anders Carlsson291279e2010-04-10 18:42:27 +00001057 if (MostDerivedClass == RD) {
Anders Carlssond20e7cd2010-05-26 05:20:58 +00001058 PrimaryBase = this->PrimaryBase;
1059 PrimaryBaseIsVirtual = this->PrimaryBaseIsVirtual;
Anders Carlsson291279e2010-04-10 18:42:27 +00001060 } else {
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001061 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlssonde710c92010-03-11 04:33:54 +00001062 PrimaryBase = Layout.getPrimaryBase();
Anders Carlsson291279e2010-04-10 18:42:27 +00001063 PrimaryBaseIsVirtual = Layout.getPrimaryBaseWasVirtual();
1064 }
1065
Anders Carlssonf7b7a1e2010-03-11 04:24:02 +00001066 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1067 E = RD->bases_end(); I != E; ++I) {
1068 assert(!I->getType()->isDependentType() &&
Sebastian Redl1054fae2009-10-25 17:03:50 +00001069 "Cannot layout class with dependent bases.");
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001070
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001071 const CXXRecordDecl *BaseDecl =
Anders Carlssonf7b7a1e2010-03-11 04:24:02 +00001072 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1073
1074 if (I->isVirtual()) {
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001075 if (PrimaryBase != BaseDecl || !PrimaryBaseIsVirtual) {
1076 bool IndirectPrimaryBase = IndirectPrimaryBases.count(BaseDecl);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001077
Anders Carlsson291279e2010-04-10 18:42:27 +00001078 // Only lay out the virtual base if it's not an indirect primary base.
1079 if (!IndirectPrimaryBase) {
1080 // Only visit virtual bases once.
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001081 if (!VisitedVirtualBases.insert(BaseDecl))
Anders Carlsson291279e2010-04-10 18:42:27 +00001082 continue;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001083
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001084 const BaseSubobjectInfo *BaseInfo = VirtualBaseInfo.lookup(BaseDecl);
1085 assert(BaseInfo && "Did not find virtual base info!");
1086 LayoutVirtualBase(BaseInfo);
Anders Carlsson6a848892010-03-11 04:10:39 +00001087 }
Mike Stump2b84dd32009-11-05 04:02:15 +00001088 }
Mike Stumpc2f591b2009-08-13 22:53:07 +00001089 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001090
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001091 if (!BaseDecl->getNumVBases()) {
Anders Carlssonf7b7a1e2010-03-11 04:24:02 +00001092 // This base isn't interesting since it doesn't have any virtual bases.
1093 continue;
Mike Stump996576f32009-08-16 19:04:13 +00001094 }
Anders Carlssonf7b7a1e2010-03-11 04:24:02 +00001095
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001096 LayoutVirtualBases(BaseDecl, MostDerivedClass);
Mike Stump6b2556f2009-08-06 13:41:24 +00001097 }
1098}
1099
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001100void RecordLayoutBuilder::LayoutVirtualBase(const BaseSubobjectInfo *Base) {
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001101 assert(!Base->Derived && "Trying to lay out a primary virtual base!");
1102
Anders Carlsson0d0b5882010-03-10 22:26:24 +00001103 // Layout the base.
Anders Carlssond7f3fcf2010-05-29 20:47:33 +00001104 uint64_t Offset = LayoutBase(Base);
Anders Carlsson22f57202010-10-31 21:01:46 +00001105 CharUnits OffsetInChars =
1106 CharUnits::fromQuantity(Offset / Context.getCharWidth());
Anders Carlsson0d0b5882010-03-10 22:26:24 +00001107
1108 // Add its base class offset.
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001109 assert(!VBases.count(Base->Class) && "vbase offset already exists!");
Anders Carlsson22f57202010-10-31 21:01:46 +00001110 VBases.insert(std::make_pair(Base->Class, OffsetInChars));
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001111
1112 AddPrimaryVirtualBaseOffsets(Base, Offset);
Anders Carlsson09ffa322010-03-10 22:21:28 +00001113}
1114
Anders Carlssond7f3fcf2010-05-29 20:47:33 +00001115uint64_t RecordLayoutBuilder::LayoutBase(const BaseSubobjectInfo *Base) {
1116 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base->Class);
Anders Carlsson09ffa322010-03-10 22:21:28 +00001117
1118 // If we have an empty base class, try to place it at offset 0.
Anders Carlssond7f3fcf2010-05-29 20:47:33 +00001119 if (Base->Class->isEmpty() &&
Anders Carlssonb1fcdd02010-05-30 06:52:33 +00001120 EmptySubobjects->CanPlaceBaseAtOffset(Base, 0)) {
Anders Carlsson2357bfd2010-05-08 22:35:05 +00001121 Size = std::max(Size, Layout.getSize());
Anders Carlsson09ffa322010-03-10 22:21:28 +00001122
1123 return 0;
1124 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001125
Anders Carlsson2357bfd2010-05-08 22:35:05 +00001126 unsigned BaseAlign = Layout.getNonVirtualAlign();
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001127
Anders Carlsson09ffa322010-03-10 22:21:28 +00001128 // Round up the current record size to the base's alignment boundary.
1129 uint64_t Offset = llvm::RoundUpToAlignment(DataSize, BaseAlign);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001130
Anders Carlsson09ffa322010-03-10 22:21:28 +00001131 // Try to place the base.
Anders Carlssonb1fcdd02010-05-30 06:52:33 +00001132 while (!EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset))
Anders Carlsson09ffa322010-03-10 22:21:28 +00001133 Offset += BaseAlign;
Anders Carlsson09ffa322010-03-10 22:21:28 +00001134
Anders Carlssond7f3fcf2010-05-29 20:47:33 +00001135 if (!Base->Class->isEmpty()) {
Anders Carlsson09ffa322010-03-10 22:21:28 +00001136 // Update the data size.
Anders Carlsson2357bfd2010-05-08 22:35:05 +00001137 DataSize = Offset + Layout.getNonVirtualSize();
Anders Carlsson09ffa322010-03-10 22:21:28 +00001138
1139 Size = std::max(Size, DataSize);
1140 } else
Anders Carlsson2357bfd2010-05-08 22:35:05 +00001141 Size = std::max(Size, Offset + Layout.getSize());
Anders Carlsson09ffa322010-03-10 22:21:28 +00001142
1143 // Remember max struct/class alignment.
1144 UpdateAlignment(BaseAlign);
1145
Anders Carlsson09ffa322010-03-10 22:21:28 +00001146 return Offset;
1147}
1148
Daniel Dunbar6da10982010-05-27 05:45:51 +00001149void RecordLayoutBuilder::InitializeLayout(const Decl *D) {
1150 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
1151 IsUnion = RD->isUnion();
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001152
Anders Carlsson28a5fa22009-08-08 19:38:24 +00001153 Packed = D->hasAttr<PackedAttr>();
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001154
Daniel Dunbar6da10982010-05-27 05:45:51 +00001155 // mac68k alignment supersedes maximum field alignment and attribute aligned,
1156 // and forces all structures to have 2-byte alignment. The IBM docs on it
1157 // allude to additional (more complicated) semantics, especially with regard
1158 // to bit-fields, but gcc appears not to follow that.
1159 if (D->hasAttr<AlignMac68kAttr>()) {
1160 IsMac68kAlign = true;
1161 MaxFieldAlignment = 2 * 8;
1162 Alignment = 2 * 8;
1163 } else {
1164 if (const MaxFieldAlignmentAttr *MFAA = D->getAttr<MaxFieldAlignmentAttr>())
1165 MaxFieldAlignment = MFAA->getAlignment();
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001166
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001167 if (unsigned MaxAlign = D->getMaxAlignment())
1168 UpdateAlignment(MaxAlign);
Daniel Dunbar6da10982010-05-27 05:45:51 +00001169 }
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001170}
Anders Carlsson6d9f6f32009-07-19 00:18:47 +00001171
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001172void RecordLayoutBuilder::Layout(const RecordDecl *D) {
1173 InitializeLayout(D);
Anders Carlsson118ce162009-07-18 21:48:39 +00001174 LayoutFields(D);
Mike Stump11289f42009-09-09 15:08:12 +00001175
Anders Carlsson79474332009-07-18 20:20:21 +00001176 // Finally, round the size of the total struct up to the alignment of the
1177 // struct itself.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001178 FinishLayout(D);
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001179}
1180
1181void RecordLayoutBuilder::Layout(const CXXRecordDecl *RD) {
1182 InitializeLayout(RD);
1183
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001184 // Lay out the vtable and the non-virtual bases.
1185 LayoutNonVirtualBases(RD);
1186
1187 LayoutFields(RD);
1188
1189 NonVirtualSize = Size;
1190 NonVirtualAlignment = Alignment;
1191
1192 // Lay out the virtual bases and add the primary virtual base offsets.
1193 LayoutVirtualBases(RD, RD);
1194
1195 VisitedVirtualBases.clear();
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001196
1197 // Finally, round the size of the total struct up to the alignment of the
1198 // struct itself.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001199 FinishLayout(RD);
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001200
Anders Carlsson5b441d72010-04-10 21:24:48 +00001201#ifndef NDEBUG
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001202 // Check that we have base offsets for all bases.
1203 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1204 E = RD->bases_end(); I != E; ++I) {
1205 if (I->isVirtual())
1206 continue;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001207
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001208 const CXXRecordDecl *BaseDecl =
1209 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1210
1211 assert(Bases.count(BaseDecl) && "Did not find base offset!");
1212 }
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001213
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001214 // And all virtual bases.
1215 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
1216 E = RD->vbases_end(); I != E; ++I) {
1217 const CXXRecordDecl *BaseDecl =
1218 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001219
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001220 assert(VBases.count(BaseDecl) && "Did not find base offset!");
Anders Carlsson5b441d72010-04-10 21:24:48 +00001221 }
1222#endif
Anders Carlsson79474332009-07-18 20:20:21 +00001223}
1224
Anders Carlssonc2226202010-05-26 05:58:59 +00001225void RecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D) {
Anders Carlsson4f516282009-07-18 20:50:59 +00001226 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001227 const ASTRecordLayout &SL = Context.getASTObjCInterfaceLayout(SD);
Anders Carlsson4f516282009-07-18 20:50:59 +00001228
1229 UpdateAlignment(SL.getAlignment());
Mike Stump11289f42009-09-09 15:08:12 +00001230
Anders Carlsson4f516282009-07-18 20:50:59 +00001231 // We start laying out ivars not at the end of the superclass
1232 // structure, but at the next byte following the last field.
Anders Carlsson27b50132009-07-18 21:26:44 +00001233 Size = llvm::RoundUpToAlignment(SL.getDataSize(), 8);
Anders Carlsson47680d82009-09-26 01:34:51 +00001234 DataSize = Size;
Anders Carlsson4f516282009-07-18 20:50:59 +00001235 }
Mike Stump11289f42009-09-09 15:08:12 +00001236
Daniel Dunbar6da10982010-05-27 05:45:51 +00001237 InitializeLayout(D);
Daniel Dunbar40130442010-05-27 01:12:46 +00001238
Anders Carlsson4f516282009-07-18 20:50:59 +00001239 // Layout each ivar sequentially.
1240 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001241 Context.ShallowCollectObjCIvars(D, Ivars);
Anders Carlsson4f516282009-07-18 20:50:59 +00001242 for (unsigned i = 0, e = Ivars.size(); i != e; ++i)
1243 LayoutField(Ivars[i]);
Mike Stump11289f42009-09-09 15:08:12 +00001244
Anders Carlsson4f516282009-07-18 20:50:59 +00001245 // Finally, round the size of the total struct up to the alignment of the
1246 // struct itself.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001247 FinishLayout(D);
Anders Carlsson4f516282009-07-18 20:50:59 +00001248}
1249
Anders Carlssonc2226202010-05-26 05:58:59 +00001250void RecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
Anders Carlsson118ce162009-07-18 21:48:39 +00001251 // Layout each field, for now, just sequentially, respecting alignment. In
1252 // the future, this will need to be tweakable by targets.
Mike Stump11289f42009-09-09 15:08:12 +00001253 for (RecordDecl::field_iterator Field = D->field_begin(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001254 FieldEnd = D->field_end(); Field != FieldEnd; ++Field)
Anders Carlsson118ce162009-07-18 21:48:39 +00001255 LayoutField(*Field);
1256}
1257
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001258void RecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize,
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001259 uint64_t TypeSize,
1260 bool FieldPacked,
1261 const FieldDecl *D) {
Anders Carlsson57235162010-04-16 15:57:11 +00001262 assert(Context.getLangOptions().CPlusPlus &&
1263 "Can only have wide bit-fields in C++!");
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001264
Anders Carlsson57235162010-04-16 15:57:11 +00001265 // Itanium C++ ABI 2.4:
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001266 // If sizeof(T)*8 < n, let T' be the largest integral POD type with
Anders Carlsson57235162010-04-16 15:57:11 +00001267 // sizeof(T')*8 <= n.
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001268
Anders Carlsson57235162010-04-16 15:57:11 +00001269 QualType IntegralPODTypes[] = {
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001270 Context.UnsignedCharTy, Context.UnsignedShortTy, Context.UnsignedIntTy,
Anders Carlsson57235162010-04-16 15:57:11 +00001271 Context.UnsignedLongTy, Context.UnsignedLongLongTy
1272 };
1273
Anders Carlsson57235162010-04-16 15:57:11 +00001274 QualType Type;
1275 for (unsigned I = 0, E = llvm::array_lengthof(IntegralPODTypes);
1276 I != E; ++I) {
1277 uint64_t Size = Context.getTypeSize(IntegralPODTypes[I]);
Anders Carlsson57235162010-04-16 15:57:11 +00001278
1279 if (Size > FieldSize)
1280 break;
1281
1282 Type = IntegralPODTypes[I];
1283 }
1284 assert(!Type.isNull() && "Did not find a type!");
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001285
Anders Carlsson57235162010-04-16 15:57:11 +00001286 unsigned TypeAlign = Context.getTypeAlign(Type);
1287
1288 // We're not going to use any of the unfilled bits in the last byte.
1289 UnfilledBitsInLastByte = 0;
1290
Anders Carlssonaad5fa82010-04-17 20:21:41 +00001291 uint64_t FieldOffset;
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001292 uint64_t UnpaddedFieldOffset = DataSize - UnfilledBitsInLastByte;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001293
Anders Carlsson57235162010-04-16 15:57:11 +00001294 if (IsUnion) {
1295 DataSize = std::max(DataSize, FieldSize);
Anders Carlssonaad5fa82010-04-17 20:21:41 +00001296 FieldOffset = 0;
Anders Carlsson57235162010-04-16 15:57:11 +00001297 } else {
Anders Carlssonaad5fa82010-04-17 20:21:41 +00001298 // The bitfield is allocated starting at the next offset aligned appropriately
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001299 // for T', with length n bits.
Anders Carlssonaad5fa82010-04-17 20:21:41 +00001300 FieldOffset = llvm::RoundUpToAlignment(DataSize, TypeAlign);
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001301
Anders Carlsson57235162010-04-16 15:57:11 +00001302 uint64_t NewSizeInBits = FieldOffset + FieldSize;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001303
Anders Carlsson57235162010-04-16 15:57:11 +00001304 DataSize = llvm::RoundUpToAlignment(NewSizeInBits, 8);
1305 UnfilledBitsInLastByte = DataSize - NewSizeInBits;
1306 }
1307
1308 // Place this field at the current location.
1309 FieldOffsets.push_back(FieldOffset);
1310
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001311 CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, FieldOffset,
1312 TypeAlign, FieldPacked, D);
1313
Anders Carlsson57235162010-04-16 15:57:11 +00001314 // Update the size.
1315 Size = std::max(Size, DataSize);
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001316
Anders Carlsson57235162010-04-16 15:57:11 +00001317 // Remember max struct/class alignment.
1318 UpdateAlignment(TypeAlign);
1319}
1320
Anders Carlssonc2226202010-05-26 05:58:59 +00001321void RecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
Anders Carlsson07209442009-11-22 17:37:31 +00001322 bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001323 uint64_t UnpaddedFieldOffset = DataSize - UnfilledBitsInLastByte;
1324 uint64_t FieldOffset = IsUnion ? 0 : UnpaddedFieldOffset;
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001325 uint64_t FieldSize = D->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001326
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001327 std::pair<uint64_t, unsigned> FieldInfo = Context.getTypeInfo(D->getType());
Anders Carlsson07209442009-11-22 17:37:31 +00001328 uint64_t TypeSize = FieldInfo.first;
1329 unsigned FieldAlign = FieldInfo.second;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001330
Anders Carlsson57235162010-04-16 15:57:11 +00001331 if (FieldSize > TypeSize) {
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001332 LayoutWideBitField(FieldSize, TypeSize, FieldPacked, D);
Anders Carlsson57235162010-04-16 15:57:11 +00001333 return;
1334 }
1335
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001336 // The align if the field is not packed. This is to check if the attribute
1337 // was unnecessary (-Wpacked).
1338 unsigned UnpackedFieldAlign = FieldAlign;
1339 uint64_t UnpackedFieldOffset = FieldOffset;
1340 if (!Context.Target.useBitFieldTypeAlignment())
1341 UnpackedFieldAlign = 1;
1342
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001343 if (FieldPacked || !Context.Target.useBitFieldTypeAlignment())
Anders Carlsson07209442009-11-22 17:37:31 +00001344 FieldAlign = 1;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001345 FieldAlign = std::max(FieldAlign, D->getMaxAlignment());
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001346 UnpackedFieldAlign = std::max(UnpackedFieldAlign, D->getMaxAlignment());
Anders Carlsson07209442009-11-22 17:37:31 +00001347
1348 // The maximum field alignment overrides the aligned attribute.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001349 if (MaxFieldAlignment) {
Anders Carlsson07209442009-11-22 17:37:31 +00001350 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001351 UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignment);
1352 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001353
Daniel Dunbar3d9289c2010-04-15 06:18:39 +00001354 // Check if we need to add padding to give the field the correct alignment.
Anders Carlsson07209442009-11-22 17:37:31 +00001355 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
Daniel Dunbarf35e7652010-06-29 18:34:35 +00001356 FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001357
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001358 if (FieldSize == 0 ||
1359 (UnpackedFieldOffset & (UnpackedFieldAlign-1)) + FieldSize > TypeSize)
1360 UnpackedFieldOffset = llvm::RoundUpToAlignment(UnpackedFieldOffset,
1361 UnpackedFieldAlign);
1362
Daniel Dunbar3d9289c2010-04-15 06:18:39 +00001363 // Padding members don't affect overall alignment.
Anders Carlsson07209442009-11-22 17:37:31 +00001364 if (!D->getIdentifier())
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001365 FieldAlign = UnpackedFieldAlign = 1;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001366
Anders Carlsson07209442009-11-22 17:37:31 +00001367 // Place this field at the current location.
1368 FieldOffsets.push_back(FieldOffset);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001369
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001370 CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, UnpackedFieldOffset,
1371 UnpackedFieldAlign, FieldPacked, D);
1372
Anders Carlssonba958402009-11-22 19:13:51 +00001373 // Update DataSize to include the last byte containing (part of) the bitfield.
1374 if (IsUnion) {
1375 // FIXME: I think FieldSize should be TypeSize here.
1376 DataSize = std::max(DataSize, FieldSize);
1377 } else {
1378 uint64_t NewSizeInBits = FieldOffset + FieldSize;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001379
Anders Carlssonba958402009-11-22 19:13:51 +00001380 DataSize = llvm::RoundUpToAlignment(NewSizeInBits, 8);
1381 UnfilledBitsInLastByte = DataSize - NewSizeInBits;
1382 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001383
Anders Carlssonba958402009-11-22 19:13:51 +00001384 // Update the size.
1385 Size = std::max(Size, DataSize);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001386
Anders Carlsson07209442009-11-22 17:37:31 +00001387 // Remember max struct/class alignment.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001388 UpdateAlignment(FieldAlign, UnpackedFieldAlign);
Anders Carlsson07209442009-11-22 17:37:31 +00001389}
1390
Anders Carlssonc2226202010-05-26 05:58:59 +00001391void RecordLayoutBuilder::LayoutField(const FieldDecl *D) {
Anders Carlsson07209442009-11-22 17:37:31 +00001392 if (D->isBitField()) {
1393 LayoutBitField(D);
1394 return;
1395 }
1396
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001397 uint64_t UnpaddedFieldOffset = DataSize - UnfilledBitsInLastByte;
1398
Anders Carlssonba958402009-11-22 19:13:51 +00001399 // Reset the unfilled bits.
1400 UnfilledBitsInLastByte = 0;
1401
Anders Carlsson07209442009-11-22 17:37:31 +00001402 bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
Anders Carlsson47680d82009-09-26 01:34:51 +00001403 uint64_t FieldOffset = IsUnion ? 0 : DataSize;
Anders Carlsson79474332009-07-18 20:20:21 +00001404 uint64_t FieldSize;
1405 unsigned FieldAlign;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001406
Anders Carlsson07209442009-11-22 17:37:31 +00001407 if (D->getType()->isIncompleteArrayType()) {
1408 // This is a flexible array member; we can't directly
1409 // query getTypeInfo about these, so we figure it out here.
1410 // Flexible array members don't have any size, but they
1411 // have to be aligned appropriately for their element type.
1412 FieldSize = 0;
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001413 const ArrayType* ATy = Context.getAsArrayType(D->getType());
1414 FieldAlign = Context.getTypeAlign(ATy->getElementType());
Anders Carlsson07209442009-11-22 17:37:31 +00001415 } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) {
1416 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001417 FieldSize = Context.Target.getPointerWidth(AS);
1418 FieldAlign = Context.Target.getPointerAlign(AS);
Anders Carlsson79474332009-07-18 20:20:21 +00001419 } else {
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001420 std::pair<uint64_t, unsigned> FieldInfo = Context.getTypeInfo(D->getType());
Anders Carlsson07209442009-11-22 17:37:31 +00001421 FieldSize = FieldInfo.first;
1422 FieldAlign = FieldInfo.second;
Anders Carlsson79474332009-07-18 20:20:21 +00001423 }
Mike Stump11289f42009-09-09 15:08:12 +00001424
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001425 // The align if the field is not packed. This is to check if the attribute
1426 // was unnecessary (-Wpacked).
1427 unsigned UnpackedFieldAlign = FieldAlign;
1428 uint64_t UnpackedFieldOffset = FieldOffset;
1429
Anders Carlsson07209442009-11-22 17:37:31 +00001430 if (FieldPacked)
1431 FieldAlign = 8;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001432 FieldAlign = std::max(FieldAlign, D->getMaxAlignment());
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001433 UnpackedFieldAlign = std::max(UnpackedFieldAlign, D->getMaxAlignment());
Anders Carlsson07209442009-11-22 17:37:31 +00001434
1435 // The maximum field alignment overrides the aligned attribute.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001436 if (MaxFieldAlignment) {
Anders Carlsson07209442009-11-22 17:37:31 +00001437 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001438 UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignment);
1439 }
Anders Carlsson07209442009-11-22 17:37:31 +00001440
1441 // Round up the current record size to the field's alignment boundary.
1442 FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001443 UnpackedFieldOffset = llvm::RoundUpToAlignment(UnpackedFieldOffset,
1444 UnpackedFieldAlign);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001445
Anders Carlssonb1fcdd02010-05-30 06:52:33 +00001446 if (!IsUnion && EmptySubobjects) {
1447 // Check if we can place the field at this offset.
1448 while (!EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset)) {
Anders Carlsson07209442009-11-22 17:37:31 +00001449 // We couldn't place the field at the offset. Try again at a new offset.
1450 FieldOffset += FieldAlign;
1451 }
Anders Carlsson07209442009-11-22 17:37:31 +00001452 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001453
Anders Carlsson79474332009-07-18 20:20:21 +00001454 // Place this field at the current location.
1455 FieldOffsets.push_back(FieldOffset);
Mike Stump11289f42009-09-09 15:08:12 +00001456
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001457 CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, UnpackedFieldOffset,
1458 UnpackedFieldAlign, FieldPacked, D);
1459
Anders Carlsson79474332009-07-18 20:20:21 +00001460 // Reserve space for this field.
1461 if (IsUnion)
1462 Size = std::max(Size, FieldSize);
1463 else
1464 Size = FieldOffset + FieldSize;
Mike Stump11289f42009-09-09 15:08:12 +00001465
Anders Carlsson47680d82009-09-26 01:34:51 +00001466 // Update the data size.
1467 DataSize = Size;
Mike Stump11289f42009-09-09 15:08:12 +00001468
Anders Carlsson79474332009-07-18 20:20:21 +00001469 // Remember max struct/class alignment.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001470 UpdateAlignment(FieldAlign, UnpackedFieldAlign);
Anders Carlsson79474332009-07-18 20:20:21 +00001471}
1472
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001473void RecordLayoutBuilder::FinishLayout(const NamedDecl *D) {
Anders Carlsson79474332009-07-18 20:20:21 +00001474 // In C++, records cannot be of size 0.
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001475 if (Context.getLangOptions().CPlusPlus && Size == 0)
Anders Carlsson79474332009-07-18 20:20:21 +00001476 Size = 8;
1477 // Finally, round the size of the record up to the alignment of the
1478 // record itself.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001479 uint64_t UnpaddedSize = Size - UnfilledBitsInLastByte;
1480 uint64_t UnpackedSize = llvm::RoundUpToAlignment(Size, UnpackedAlignment);
Anders Carlsson07209442009-11-22 17:37:31 +00001481 Size = llvm::RoundUpToAlignment(Size, Alignment);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001482
1483 unsigned CharBitNum = Context.Target.getCharWidth();
1484 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) {
1485 // Warn if padding was introduced to the struct/class/union.
1486 if (Size > UnpaddedSize) {
1487 unsigned PadSize = Size - UnpaddedSize;
1488 bool InBits = true;
1489 if (PadSize % CharBitNum == 0) {
1490 PadSize = PadSize / CharBitNum;
1491 InBits = false;
1492 }
1493 Diag(RD->getLocation(), diag::warn_padded_struct_size)
1494 << Context.getTypeDeclType(RD)
1495 << PadSize
1496 << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not
1497 }
1498
1499 // Warn if we packed it unnecessarily. If the alignment is 1 byte don't
1500 // bother since there won't be alignment issues.
1501 if (Packed && UnpackedAlignment > CharBitNum && Size == UnpackedSize)
1502 Diag(D->getLocation(), diag::warn_unnecessary_packed)
1503 << Context.getTypeDeclType(RD);
1504 }
Anders Carlsson79474332009-07-18 20:20:21 +00001505}
1506
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001507void RecordLayoutBuilder::UpdateAlignment(unsigned NewAlignment,
1508 unsigned UnpackedNewAlignment) {
Daniel Dunbar6da10982010-05-27 05:45:51 +00001509 // The alignment is not modified when using 'mac68k' alignment.
1510 if (IsMac68kAlign)
1511 return;
1512
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001513 if (NewAlignment > Alignment) {
1514 assert(llvm::isPowerOf2_32(NewAlignment && "Alignment not a power of 2"));
1515 Alignment = NewAlignment;
1516 }
1517
1518 if (UnpackedNewAlignment > UnpackedAlignment) {
1519 assert(llvm::isPowerOf2_32(UnpackedNewAlignment &&
1520 "Alignment not a power of 2"));
1521 UnpackedAlignment = UnpackedNewAlignment;
1522 }
1523}
1524
1525void RecordLayoutBuilder::CheckFieldPadding(uint64_t Offset,
1526 uint64_t UnpaddedOffset,
1527 uint64_t UnpackedOffset,
1528 unsigned UnpackedAlign,
1529 bool isPacked,
1530 const FieldDecl *D) {
1531 // We let objc ivars without warning, objc interfaces generally are not used
1532 // for padding tricks.
1533 if (isa<ObjCIvarDecl>(D))
Anders Carlsson79474332009-07-18 20:20:21 +00001534 return;
Mike Stump11289f42009-09-09 15:08:12 +00001535
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001536 unsigned CharBitNum = Context.Target.getCharWidth();
Mike Stump11289f42009-09-09 15:08:12 +00001537
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001538 // Warn if padding was introduced to the struct/class.
1539 if (!IsUnion && Offset > UnpaddedOffset) {
1540 unsigned PadSize = Offset - UnpaddedOffset;
1541 bool InBits = true;
1542 if (PadSize % CharBitNum == 0) {
1543 PadSize = PadSize / CharBitNum;
1544 InBits = false;
1545 }
1546 if (D->getIdentifier())
1547 Diag(D->getLocation(), diag::warn_padded_struct_field)
1548 << (D->getParent()->isStruct() ? 0 : 1) // struct|class
1549 << Context.getTypeDeclType(D->getParent())
1550 << PadSize
1551 << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1) // plural or not
1552 << D->getIdentifier();
1553 else
1554 Diag(D->getLocation(), diag::warn_padded_struct_anon_field)
1555 << (D->getParent()->isStruct() ? 0 : 1) // struct|class
1556 << Context.getTypeDeclType(D->getParent())
1557 << PadSize
1558 << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not
1559 }
1560
1561 // Warn if we packed it unnecessarily. If the alignment is 1 byte don't
1562 // bother since there won't be alignment issues.
1563 if (isPacked && UnpackedAlign > CharBitNum && Offset == UnpackedOffset)
1564 Diag(D->getLocation(), diag::warn_unnecessary_packed)
1565 << D->getIdentifier();
Anders Carlsson79474332009-07-18 20:20:21 +00001566}
Mike Stump11289f42009-09-09 15:08:12 +00001567
Anders Carlsson5ebf8b42009-12-07 04:35:11 +00001568const CXXMethodDecl *
Anders Carlssonc2226202010-05-26 05:58:59 +00001569RecordLayoutBuilder::ComputeKeyFunction(const CXXRecordDecl *RD) {
Daniel Dunbarccabe482010-04-19 20:44:53 +00001570 // If a class isn't polymorphic it doesn't have a key function.
Anders Carlsson5ebf8b42009-12-07 04:35:11 +00001571 if (!RD->isPolymorphic())
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001572 return 0;
Eli Friedmanf2c79b62009-12-08 03:56:49 +00001573
1574 // A class inside an anonymous namespace doesn't have a key function. (Or
1575 // at least, there's no point to assigning a key function to such a class;
1576 // this doesn't affect the ABI.)
1577 if (RD->isInAnonymousNamespace())
1578 return 0;
1579
Argyrios Kyrtzidis8c64bbe2010-10-13 02:39:41 +00001580 // Template instantiations don't have key functions,see Itanium C++ ABI 5.2.6.
1581 // Same behavior as GCC.
1582 TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
1583 if (TSK == TSK_ImplicitInstantiation ||
1584 TSK == TSK_ExplicitInstantiationDefinition)
1585 return 0;
1586
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001587 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
1588 E = RD->method_end(); I != E; ++I) {
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001589 const CXXMethodDecl *MD = *I;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001590
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001591 if (!MD->isVirtual())
1592 continue;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001593
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001594 if (MD->isPure())
1595 continue;
Eli Friedmanf2c79b62009-12-08 03:56:49 +00001596
Anders Carlssonf98849e2009-12-02 17:15:43 +00001597 // Ignore implicit member functions, they are always marked as inline, but
1598 // they don't have a body until they're defined.
1599 if (MD->isImplicit())
1600 continue;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001601
Douglas Gregora318efd2010-01-05 19:06:31 +00001602 if (MD->isInlineSpecified())
1603 continue;
Eli Friedman71a26d82009-12-06 20:50:05 +00001604
1605 if (MD->hasInlineBody())
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001606 continue;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001607
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001608 // We found it.
1609 return MD;
1610 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001611
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001612 return 0;
1613}
1614
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001615DiagnosticBuilder
1616RecordLayoutBuilder::Diag(SourceLocation Loc, unsigned DiagID) {
1617 return Context.getDiagnostics().Report(
1618 FullSourceLoc(Loc, Context.getSourceManager()), DiagID);
1619}
1620
Benjamin Kramer2fc373e2010-10-22 16:33:16 +00001621namespace {
1622 // This class implements layout specific to the Microsoft ABI.
1623 class MSRecordLayoutBuilder : public RecordLayoutBuilder {
1624 public:
1625 MSRecordLayoutBuilder(ASTContext& Ctx, EmptySubobjectMap *EmptySubobjects) :
1626 RecordLayoutBuilder(Ctx, EmptySubobjects) {}
Charles Davisc2c576a2010-08-19 00:55:19 +00001627
Benjamin Kramer2fc373e2010-10-22 16:33:16 +00001628 virtual bool IsNearlyEmpty(const CXXRecordDecl *RD) const;
1629 virtual uint64_t GetVirtualPointersSize(const CXXRecordDecl *RD) const;
1630 };
1631}
Charles Davisc2c576a2010-08-19 00:55:19 +00001632
1633bool MSRecordLayoutBuilder::IsNearlyEmpty(const CXXRecordDecl *RD) const {
1634 // FIXME: Audit the corners
1635 if (!RD->isDynamicClass())
1636 return false;
1637 const ASTRecordLayout &BaseInfo = Context.getASTRecordLayout(RD);
1638 // In the Microsoft ABI, classes can have one or two vtable pointers.
1639 if (BaseInfo.getNonVirtualSize() == Context.Target.getPointerWidth(0) ||
1640 BaseInfo.getNonVirtualSize() == Context.Target.getPointerWidth(0) * 2)
1641 return true;
1642 return false;
1643}
1644
1645uint64_t
1646MSRecordLayoutBuilder::GetVirtualPointersSize(const CXXRecordDecl *RD) const {
1647 // We should reserve space for two pointers if the class has both
1648 // virtual functions and virtual bases.
1649 if (RD->isPolymorphic() && RD->getNumVBases() > 0)
1650 return 2 * Context.Target.getPointerWidth(0);
1651 return Context.Target.getPointerWidth(0);
1652}
1653
Anders Carlssondf291d82010-05-26 04:56:53 +00001654/// getASTRecordLayout - Get or compute information about the layout of the
1655/// specified record (struct/union/class), which indicates its size and field
1656/// position information.
1657const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
1658 D = D->getDefinition();
1659 assert(D && "Cannot get layout of forward declarations!");
1660
1661 // Look up this layout, if already laid out, return what we have.
1662 // Note that we can't save a reference to the entry because this function
1663 // is recursive.
1664 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
1665 if (Entry) return *Entry;
1666
Anders Carlssond2954862010-05-26 05:10:47 +00001667 const ASTRecordLayout *NewEntry;
1668
1669 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
Anders Carlssonc121b4e2010-05-27 00:07:01 +00001670 EmptySubobjectMap EmptySubobjects(*this, RD);
Anders Carlsson439edd12010-05-27 05:41:06 +00001671
Charles Davisc2c576a2010-08-19 00:55:19 +00001672 // When compiling for Microsoft, use the special MS builder.
Argyrios Kyrtzidis3c6cd172010-08-25 00:32:14 +00001673 llvm::OwningPtr<RecordLayoutBuilder> Builder;
Charles Davis6bcb07a2010-08-19 02:18:14 +00001674 switch (Target.getCXXABI()) {
1675 default:
Argyrios Kyrtzidis3c6cd172010-08-25 00:32:14 +00001676 Builder.reset(new RecordLayoutBuilder(*this, &EmptySubobjects));
Charles Davis6bcb07a2010-08-19 02:18:14 +00001677 break;
1678 case CXXABI_Microsoft:
Argyrios Kyrtzidis3c6cd172010-08-25 00:32:14 +00001679 Builder.reset(new MSRecordLayoutBuilder(*this, &EmptySubobjects));
Charles Davis6bcb07a2010-08-19 02:18:14 +00001680 }
Charles Davisc2c576a2010-08-19 00:55:19 +00001681 Builder->Layout(RD);
Anders Carlssond2954862010-05-26 05:10:47 +00001682
1683 // FIXME: This is not always correct. See the part about bitfields at
1684 // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info.
1685 // FIXME: IsPODForThePurposeOfLayout should be stored in the record layout.
1686 bool IsPODForThePurposeOfLayout = cast<CXXRecordDecl>(D)->isPOD();
1687
1688 // FIXME: This should be done in FinalizeLayout.
1689 uint64_t DataSize =
Charles Davisc2c576a2010-08-19 00:55:19 +00001690 IsPODForThePurposeOfLayout ? Builder->Size : Builder->DataSize;
Anders Carlssond2954862010-05-26 05:10:47 +00001691 uint64_t NonVirtualSize =
Charles Davisc2c576a2010-08-19 00:55:19 +00001692 IsPODForThePurposeOfLayout ? DataSize : Builder->NonVirtualSize;
Anders Carlssond2954862010-05-26 05:10:47 +00001693
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001694 NewEntry =
Charles Davisc2c576a2010-08-19 00:55:19 +00001695 new (*this) ASTRecordLayout(*this, Builder->Size, Builder->Alignment,
1696 DataSize, Builder->FieldOffsets.data(),
1697 Builder->FieldOffsets.size(),
Anders Carlssond2954862010-05-26 05:10:47 +00001698 NonVirtualSize,
Charles Davisc2c576a2010-08-19 00:55:19 +00001699 Builder->NonVirtualAlignment,
Anders Carlssonc121b4e2010-05-27 00:07:01 +00001700 EmptySubobjects.SizeOfLargestEmptySubobject,
Charles Davisc2c576a2010-08-19 00:55:19 +00001701 Builder->PrimaryBase,
1702 Builder->PrimaryBaseIsVirtual,
1703 Builder->Bases, Builder->VBases);
Anders Carlssond2954862010-05-26 05:10:47 +00001704 } else {
Anders Carlssonc121b4e2010-05-27 00:07:01 +00001705 RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0);
Anders Carlssond2954862010-05-26 05:10:47 +00001706 Builder.Layout(D);
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001707
Anders Carlssond2954862010-05-26 05:10:47 +00001708 NewEntry =
1709 new (*this) ASTRecordLayout(*this, Builder.Size, Builder.Alignment,
1710 Builder.Size,
1711 Builder.FieldOffsets.data(),
1712 Builder.FieldOffsets.size());
1713 }
1714
Anders Carlssondf291d82010-05-26 04:56:53 +00001715 ASTRecordLayouts[D] = NewEntry;
1716
1717 if (getLangOptions().DumpRecordLayouts) {
1718 llvm::errs() << "\n*** Dumping AST Record Layout\n";
1719 DumpRecordLayout(D, llvm::errs());
1720 }
1721
1722 return *NewEntry;
1723}
1724
1725const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) {
1726 RD = cast<CXXRecordDecl>(RD->getDefinition());
1727 assert(RD && "Cannot get key function for forward declarations!");
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001728
Anders Carlssondf291d82010-05-26 04:56:53 +00001729 const CXXMethodDecl *&Entry = KeyFunctions[RD];
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001730 if (!Entry)
Anders Carlssonc2226202010-05-26 05:58:59 +00001731 Entry = RecordLayoutBuilder::ComputeKeyFunction(RD);
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001732
Anders Carlssondf291d82010-05-26 04:56:53 +00001733 return Entry;
1734}
1735
1736/// getInterfaceLayoutImpl - Get or compute information about the
1737/// layout of the given interface.
1738///
1739/// \param Impl - If given, also include the layout of the interface's
1740/// implementation. This may differ by including synthesized ivars.
1741const ASTRecordLayout &
1742ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
1743 const ObjCImplementationDecl *Impl) {
1744 assert(!D->isForwardDecl() && "Invalid interface decl!");
1745
1746 // Look up this layout, if already laid out, return what we have.
1747 ObjCContainerDecl *Key =
1748 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
1749 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
1750 return *Entry;
1751
1752 // Add in synthesized ivar count if laying out an implementation.
1753 if (Impl) {
1754 unsigned SynthCount = CountNonClassIvars(D);
1755 // If there aren't any sythesized ivars then reuse the interface
1756 // entry. Note we can't cache this because we simply free all
1757 // entries later; however we shouldn't look up implementations
1758 // frequently.
1759 if (SynthCount == 0)
1760 return getObjCLayout(D, 0);
1761 }
1762
Anders Carlssonc121b4e2010-05-27 00:07:01 +00001763 RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0);
Anders Carlsson6ed3a9a2010-05-26 05:04:25 +00001764 Builder.Layout(D);
1765
Anders Carlssondf291d82010-05-26 04:56:53 +00001766 const ASTRecordLayout *NewEntry =
Anders Carlsson6ed3a9a2010-05-26 05:04:25 +00001767 new (*this) ASTRecordLayout(*this, Builder.Size, Builder.Alignment,
1768 Builder.DataSize,
1769 Builder.FieldOffsets.data(),
1770 Builder.FieldOffsets.size());
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001771
Anders Carlssondf291d82010-05-26 04:56:53 +00001772 ObjCLayouts[Key] = NewEntry;
1773
1774 return *NewEntry;
1775}
1776
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001777static void PrintOffset(llvm::raw_ostream &OS,
1778 uint64_t Offset, unsigned IndentLevel) {
1779 OS << llvm::format("%4d | ", Offset);
1780 OS.indent(IndentLevel * 2);
1781}
1782
1783static void DumpCXXRecordLayout(llvm::raw_ostream &OS,
1784 const CXXRecordDecl *RD, ASTContext &C,
1785 uint64_t Offset,
1786 unsigned IndentLevel,
1787 const char* Description,
1788 bool IncludeVirtualBases) {
1789 const ASTRecordLayout &Info = C.getASTRecordLayout(RD);
1790
1791 PrintOffset(OS, Offset, IndentLevel);
Dan Gohman145f3f12010-04-19 16:39:44 +00001792 OS << C.getTypeDeclType(const_cast<CXXRecordDecl *>(RD)).getAsString();
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001793 if (Description)
1794 OS << ' ' << Description;
1795 if (RD->isEmpty())
1796 OS << " (empty)";
1797 OS << '\n';
1798
1799 IndentLevel++;
1800
1801 const CXXRecordDecl *PrimaryBase = Info.getPrimaryBase();
1802
1803 // Vtable pointer.
1804 if (RD->isDynamicClass() && !PrimaryBase) {
1805 PrintOffset(OS, Offset, IndentLevel);
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001806 OS << '(' << RD << " vtable pointer)\n";
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001807 }
1808 // Dump (non-virtual) bases
1809 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1810 E = RD->bases_end(); I != E; ++I) {
1811 assert(!I->getType()->isDependentType() &&
1812 "Cannot layout class with dependent bases.");
1813 if (I->isVirtual())
1814 continue;
1815
1816 const CXXRecordDecl *Base =
1817 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1818
1819 uint64_t BaseOffset = Offset + Info.getBaseClassOffset(Base) / 8;
1820
1821 DumpCXXRecordLayout(OS, Base, C, BaseOffset, IndentLevel,
1822 Base == PrimaryBase ? "(primary base)" : "(base)",
1823 /*IncludeVirtualBases=*/false);
1824 }
1825
1826 // Dump fields.
1827 uint64_t FieldNo = 0;
1828 for (CXXRecordDecl::field_iterator I = RD->field_begin(),
1829 E = RD->field_end(); I != E; ++I, ++FieldNo) {
1830 const FieldDecl *Field = *I;
1831 uint64_t FieldOffset = Offset + Info.getFieldOffset(FieldNo) / 8;
1832
1833 if (const RecordType *RT = Field->getType()->getAs<RecordType>()) {
1834 if (const CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
1835 DumpCXXRecordLayout(OS, D, C, FieldOffset, IndentLevel,
Daniel Dunbar56df9772010-08-17 22:39:59 +00001836 Field->getName().data(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001837 /*IncludeVirtualBases=*/true);
1838 continue;
1839 }
1840 }
1841
1842 PrintOffset(OS, FieldOffset, IndentLevel);
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001843 OS << Field->getType().getAsString() << ' ' << Field << '\n';
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001844 }
1845
1846 if (!IncludeVirtualBases)
1847 return;
1848
1849 // Dump virtual bases.
1850 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
1851 E = RD->vbases_end(); I != E; ++I) {
1852 assert(I->isVirtual() && "Found non-virtual class!");
1853 const CXXRecordDecl *VBase =
1854 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1855
1856 uint64_t VBaseOffset = Offset + Info.getVBaseClassOffset(VBase) / 8;
1857 DumpCXXRecordLayout(OS, VBase, C, VBaseOffset, IndentLevel,
1858 VBase == PrimaryBase ?
1859 "(primary virtual base)" : "(virtual base)",
1860 /*IncludeVirtualBases=*/false);
1861 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001862
1863 OS << " sizeof=" << Info.getSize() / 8;
1864 OS << ", dsize=" << Info.getDataSize() / 8;
1865 OS << ", align=" << Info.getAlignment() / 8 << '\n';
1866 OS << " nvsize=" << Info.getNonVirtualSize() / 8;
1867 OS << ", nvalign=" << Info.getNonVirtualAlign() / 8 << '\n';
1868 OS << '\n';
1869}
Daniel Dunbarccabe482010-04-19 20:44:53 +00001870
1871void ASTContext::DumpRecordLayout(const RecordDecl *RD,
1872 llvm::raw_ostream &OS) {
1873 const ASTRecordLayout &Info = getASTRecordLayout(RD);
1874
1875 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
1876 return DumpCXXRecordLayout(OS, CXXRD, *this, 0, 0, 0,
1877 /*IncludeVirtualBases=*/true);
1878
1879 OS << "Type: " << getTypeDeclType(RD).getAsString() << "\n";
1880 OS << "Record: ";
1881 RD->dump();
1882 OS << "\nLayout: ";
1883 OS << "<ASTRecordLayout\n";
1884 OS << " Size:" << Info.getSize() << "\n";
1885 OS << " DataSize:" << Info.getDataSize() << "\n";
1886 OS << " Alignment:" << Info.getAlignment() << "\n";
1887 OS << " FieldOffsets: [";
1888 for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i) {
1889 if (i) OS << ", ";
1890 OS << Info.getFieldOffset(i);
1891 }
1892 OS << "]>\n";
1893}