blob: c5465a5dca9882861f22f1a6646e28c2a451a202 [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;
66 typedef llvm::DenseMap<uint64_t, ClassVectorTy> EmptyClassOffsetsMapTy;
67 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
Charles Davisc2c576a2010-08-19 00:55:19 +000093protected:
94 bool CanPlaceSubobjectAtOffset(const CXXRecordDecl *RD,
95 uint64_t Offset) const;
96
97 bool CanPlaceBaseSubobjectAtOffset(const BaseSubobjectInfo *Info,
98 uint64_t Offset);
99
100 bool CanPlaceFieldSubobjectAtOffset(const CXXRecordDecl *RD,
101 const CXXRecordDecl *Class,
102 uint64_t Offset) const;
103 bool CanPlaceFieldSubobjectAtOffset(const FieldDecl *FD,
104 uint64_t Offset) const;
105
Anders Carlssonf58de112010-05-26 15:32:58 +0000106public:
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000107 /// This holds the size of the largest empty subobject (either a base
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000108 /// or a member). Will be zero if the record being built doesn't contain
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000109 /// any empty classes.
110 uint64_t SizeOfLargestEmptySubobject;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000111
Anders Carlssonf58de112010-05-26 15:32:58 +0000112 EmptySubobjectMap(ASTContext &Context, const CXXRecordDecl *Class)
Anders Carlsson45c1d282010-06-08 16:20:35 +0000113 : Context(Context), Class(Class), MaxEmptyClassOffset(0),
114 SizeOfLargestEmptySubobject(0) {
Anders Carlssonc121b4e2010-05-27 00:07:01 +0000115 ComputeEmptySubobjectSizes();
116 }
117
118 /// CanPlaceBaseAtOffset - Return whether the given base class can be placed
119 /// at the given offset.
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000120 /// Returns false if placing the record will result in two components
Anders Carlssonc121b4e2010-05-27 00:07:01 +0000121 /// (direct or indirect) of the same type having the same offset.
Anders Carlssoncc5de092010-06-08 15:56:03 +0000122 bool CanPlaceBaseAtOffset(const BaseSubobjectInfo *Info,
123 uint64_t Offset);
Anders Carlssondb319762010-05-27 18:20:57 +0000124
125 /// CanPlaceFieldAtOffset - Return whether a field can be placed at the given
126 /// offset.
127 bool CanPlaceFieldAtOffset(const FieldDecl *FD, uint64_t Offset);
Anders Carlssonf58de112010-05-26 15:32:58 +0000128};
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000129
130void EmptySubobjectMap::ComputeEmptySubobjectSizes() {
131 // Check the bases.
132 for (CXXRecordDecl::base_class_const_iterator I = Class->bases_begin(),
133 E = Class->bases_end(); I != E; ++I) {
134 const CXXRecordDecl *BaseDecl =
135 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
136
137 uint64_t EmptySize = 0;
138 const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl);
139 if (BaseDecl->isEmpty()) {
140 // If the class decl is empty, get its size.
141 EmptySize = Layout.getSize();
142 } else {
143 // Otherwise, we get the largest empty subobject for the decl.
144 EmptySize = Layout.getSizeOfLargestEmptySubobject();
145 }
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000146
147 SizeOfLargestEmptySubobject = std::max(SizeOfLargestEmptySubobject,
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000148 EmptySize);
149 }
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000150
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000151 // Check the fields.
152 for (CXXRecordDecl::field_iterator I = Class->field_begin(),
153 E = Class->field_end(); I != E; ++I) {
154 const FieldDecl *FD = *I;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000155
156 const RecordType *RT =
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000157 Context.getBaseElementType(FD->getType())->getAs<RecordType>();
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000158
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000159 // We only care about record types.
160 if (!RT)
161 continue;
162
163 uint64_t EmptySize = 0;
164 const CXXRecordDecl *MemberDecl = cast<CXXRecordDecl>(RT->getDecl());
165 const ASTRecordLayout &Layout = Context.getASTRecordLayout(MemberDecl);
166 if (MemberDecl->isEmpty()) {
167 // If the class decl is empty, get its size.
168 EmptySize = Layout.getSize();
169 } else {
170 // Otherwise, we get the largest empty subobject for the decl.
171 EmptySize = Layout.getSizeOfLargestEmptySubobject();
172 }
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000173
174 SizeOfLargestEmptySubobject = std::max(SizeOfLargestEmptySubobject,
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000175 EmptySize);
176 }
177}
178
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000179bool
Anders Carlssondb319762010-05-27 18:20:57 +0000180EmptySubobjectMap::CanPlaceSubobjectAtOffset(const CXXRecordDecl *RD,
Anders Carlssoncc5de092010-06-08 15:56:03 +0000181 uint64_t Offset) const {
Anders Carlssondb319762010-05-27 18:20:57 +0000182 // We only need to check empty bases.
183 if (!RD->isEmpty())
184 return true;
185
186 EmptyClassOffsetsMapTy::const_iterator I = EmptyClassOffsets.find(Offset);
187 if (I == EmptyClassOffsets.end())
188 return true;
189
190 const ClassVectorTy& Classes = I->second;
191 if (std::find(Classes.begin(), Classes.end(), RD) == Classes.end())
192 return true;
193
194 // There is already an empty class of the same type at this offset.
195 return false;
196}
197
198void EmptySubobjectMap::AddSubobjectAtOffset(const CXXRecordDecl *RD,
199 uint64_t Offset) {
200 // We only care about empty bases.
201 if (!RD->isEmpty())
202 return;
203
204 ClassVectorTy& Classes = EmptyClassOffsets[Offset];
205 assert(std::find(Classes.begin(), Classes.end(), RD) == Classes.end() &&
206 "Duplicate empty class detected!");
Anders Carlsson44687202010-06-08 19:09:24 +0000207
Anders Carlssondb319762010-05-27 18:20:57 +0000208 Classes.push_back(RD);
Anders Carlssoncc5de092010-06-08 15:56:03 +0000209
210 // Update the empty class offset.
211 MaxEmptyClassOffset = std::max(MaxEmptyClassOffset, Offset);
Anders Carlssondb319762010-05-27 18:20:57 +0000212}
213
214bool
Anders Carlssona7f3cdb2010-05-28 21:24:37 +0000215EmptySubobjectMap::CanPlaceBaseSubobjectAtOffset(const BaseSubobjectInfo *Info,
Anders Carlsson439edd12010-05-27 05:41:06 +0000216 uint64_t Offset) {
Anders Carlsson45c1d282010-06-08 16:20:35 +0000217 // We don't have to keep looking past the maximum offset that's known to
218 // contain an empty class.
219 if (!AnyEmptySubobjectsBeyondOffset(Offset))
220 return true;
221
Anders Carlssondb319762010-05-27 18:20:57 +0000222 if (!CanPlaceSubobjectAtOffset(Info->Class, Offset))
223 return false;
224
Anders Carlsson439edd12010-05-27 05:41:06 +0000225 // Traverse all non-virtual bases.
Anders Carlssona7774a62010-05-29 21:10:24 +0000226 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class);
Anders Carlsson439edd12010-05-27 05:41:06 +0000227 for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) {
Anders Carlssona7f3cdb2010-05-28 21:24:37 +0000228 BaseSubobjectInfo* Base = Info->Bases[I];
Anders Carlsson439edd12010-05-27 05:41:06 +0000229 if (Base->IsVirtual)
230 continue;
231
Anders Carlsson439edd12010-05-27 05:41:06 +0000232 uint64_t BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class);
233
234 if (!CanPlaceBaseSubobjectAtOffset(Base, BaseOffset))
235 return false;
236 }
237
Anders Carlssone3c24c72010-05-29 17:35:14 +0000238 if (Info->PrimaryVirtualBaseInfo) {
239 BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo;
Anders Carlsson439edd12010-05-27 05:41:06 +0000240
241 if (Info == PrimaryVirtualBaseInfo->Derived) {
242 if (!CanPlaceBaseSubobjectAtOffset(PrimaryVirtualBaseInfo, Offset))
243 return false;
244 }
245 }
246
Anders Carlssondb319762010-05-27 18:20:57 +0000247 // Traverse all member variables.
248 unsigned FieldNo = 0;
249 for (CXXRecordDecl::field_iterator I = Info->Class->field_begin(),
250 E = Info->Class->field_end(); I != E; ++I, ++FieldNo) {
251 const FieldDecl *FD = *I;
Anders Carlssona7774a62010-05-29 21:10:24 +0000252
Anders Carlssondb319762010-05-27 18:20:57 +0000253 uint64_t FieldOffset = Offset + Layout.getFieldOffset(FieldNo);
Anders Carlssondb319762010-05-27 18:20:57 +0000254 if (!CanPlaceFieldSubobjectAtOffset(FD, FieldOffset))
255 return false;
256 }
257
Anders Carlsson439edd12010-05-27 05:41:06 +0000258 return true;
259}
260
Anders Carlssona7f3cdb2010-05-28 21:24:37 +0000261void EmptySubobjectMap::UpdateEmptyBaseSubobjects(const BaseSubobjectInfo *Info,
Anders Carlssoncc59cc52010-06-13 18:00:18 +0000262 uint64_t Offset,
263 bool PlacingEmptyBase) {
264 if (!PlacingEmptyBase && Offset >= SizeOfLargestEmptySubobject) {
265 // We know that the only empty subobjects that can conflict with empty
266 // subobject of non-empty bases, are empty bases that can be placed at
267 // offset zero. Because of this, we only need to keep track of empty base
268 // subobjects with offsets less than the size of the largest empty
269 // subobject for our class.
270 return;
271 }
272
Anders Carlssondb319762010-05-27 18:20:57 +0000273 AddSubobjectAtOffset(Info->Class, Offset);
Anders Carlssona7774a62010-05-29 21:10:24 +0000274
Anders Carlsson439edd12010-05-27 05:41:06 +0000275 // Traverse all non-virtual bases.
Anders Carlssona7774a62010-05-29 21:10:24 +0000276 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class);
Anders Carlsson439edd12010-05-27 05:41:06 +0000277 for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) {
Anders Carlssona7f3cdb2010-05-28 21:24:37 +0000278 BaseSubobjectInfo* Base = Info->Bases[I];
Anders Carlsson439edd12010-05-27 05:41:06 +0000279 if (Base->IsVirtual)
280 continue;
Anders Carlssona7774a62010-05-29 21:10:24 +0000281
Anders Carlsson439edd12010-05-27 05:41:06 +0000282 uint64_t BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class);
Anders Carlssoncc59cc52010-06-13 18:00:18 +0000283 UpdateEmptyBaseSubobjects(Base, BaseOffset, PlacingEmptyBase);
Anders Carlsson439edd12010-05-27 05:41:06 +0000284 }
285
Anders Carlssone3c24c72010-05-29 17:35:14 +0000286 if (Info->PrimaryVirtualBaseInfo) {
287 BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo;
Anders Carlsson439edd12010-05-27 05:41:06 +0000288
289 if (Info == PrimaryVirtualBaseInfo->Derived)
Anders Carlssoncc59cc52010-06-13 18:00:18 +0000290 UpdateEmptyBaseSubobjects(PrimaryVirtualBaseInfo, Offset,
291 PlacingEmptyBase);
Anders Carlsson439edd12010-05-27 05:41:06 +0000292 }
Anders Carlssondb319762010-05-27 18:20:57 +0000293
Anders Carlssondb319762010-05-27 18:20:57 +0000294 // Traverse all member variables.
295 unsigned FieldNo = 0;
296 for (CXXRecordDecl::field_iterator I = Info->Class->field_begin(),
297 E = Info->Class->field_end(); I != E; ++I, ++FieldNo) {
298 const FieldDecl *FD = *I;
Anders Carlssona7774a62010-05-29 21:10:24 +0000299
Anders Carlssondb319762010-05-27 18:20:57 +0000300 uint64_t FieldOffset = Offset + Layout.getFieldOffset(FieldNo);
Anders Carlssondb319762010-05-27 18:20:57 +0000301 UpdateEmptyFieldSubobjects(FD, FieldOffset);
302 }
Anders Carlsson439edd12010-05-27 05:41:06 +0000303}
304
Anders Carlssona60b86a2010-05-29 20:49:49 +0000305bool EmptySubobjectMap::CanPlaceBaseAtOffset(const BaseSubobjectInfo *Info,
Anders Carlsson439edd12010-05-27 05:41:06 +0000306 uint64_t Offset) {
Anders Carlssonc121b4e2010-05-27 00:07:01 +0000307 // If we know this class doesn't have any empty subobjects we don't need to
308 // bother checking.
309 if (!SizeOfLargestEmptySubobject)
310 return true;
311
Anders Carlsson439edd12010-05-27 05:41:06 +0000312 if (!CanPlaceBaseSubobjectAtOffset(Info, Offset))
313 return false;
Anders Carlssondb319762010-05-27 18:20:57 +0000314
315 // We are able to place the base at this offset. Make sure to update the
316 // empty base subobject map.
Anders Carlssoncc59cc52010-06-13 18:00:18 +0000317 UpdateEmptyBaseSubobjects(Info, Offset, Info->Class->isEmpty());
Anders Carlssonc121b4e2010-05-27 00:07:01 +0000318 return true;
319}
320
Anders Carlssondb319762010-05-27 18:20:57 +0000321bool
322EmptySubobjectMap::CanPlaceFieldSubobjectAtOffset(const CXXRecordDecl *RD,
323 const CXXRecordDecl *Class,
Anders Carlssoncc5de092010-06-08 15:56:03 +0000324 uint64_t Offset) const {
Anders Carlsson45c1d282010-06-08 16:20:35 +0000325 // We don't have to keep looking past the maximum offset that's known to
326 // contain an empty class.
327 if (!AnyEmptySubobjectsBeyondOffset(Offset))
328 return true;
329
Anders Carlssondb319762010-05-27 18:20:57 +0000330 if (!CanPlaceSubobjectAtOffset(RD, Offset))
331 return false;
332
333 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
334
335 // Traverse all non-virtual bases.
336 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
337 E = RD->bases_end(); I != E; ++I) {
338 if (I->isVirtual())
339 continue;
340
341 const CXXRecordDecl *BaseDecl =
342 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
343
344 uint64_t BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl);
345 if (!CanPlaceFieldSubobjectAtOffset(BaseDecl, Class, BaseOffset))
346 return false;
347 }
348
Anders Carlsson44687202010-06-08 19:09:24 +0000349 if (RD == Class) {
350 // This is the most derived class, traverse virtual bases as well.
351 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
352 E = RD->vbases_end(); I != E; ++I) {
353 const CXXRecordDecl *VBaseDecl =
354 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
355
356 uint64_t VBaseOffset = Offset + Layout.getVBaseClassOffset(VBaseDecl);
357 if (!CanPlaceFieldSubobjectAtOffset(VBaseDecl, Class, VBaseOffset))
358 return false;
359 }
360 }
361
Anders Carlssondb319762010-05-27 18:20:57 +0000362 // Traverse all member variables.
363 unsigned FieldNo = 0;
364 for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
365 I != E; ++I, ++FieldNo) {
366 const FieldDecl *FD = *I;
367
368 uint64_t FieldOffset = Offset + Layout.getFieldOffset(FieldNo);
369
370 if (!CanPlaceFieldSubobjectAtOffset(FD, FieldOffset))
371 return false;
372 }
373
374 return true;
375}
376
377bool EmptySubobjectMap::CanPlaceFieldSubobjectAtOffset(const FieldDecl *FD,
Anders Carlssoncc5de092010-06-08 15:56:03 +0000378 uint64_t Offset) const {
Anders Carlsson45c1d282010-06-08 16:20:35 +0000379 // We don't have to keep looking past the maximum offset that's known to
380 // contain an empty class.
381 if (!AnyEmptySubobjectsBeyondOffset(Offset))
382 return true;
383
Anders Carlssondb319762010-05-27 18:20:57 +0000384 QualType T = FD->getType();
385 if (const RecordType *RT = T->getAs<RecordType>()) {
386 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
387 return CanPlaceFieldSubobjectAtOffset(RD, RD, Offset);
388 }
389
390 // If we have an array type we need to look at every element.
391 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) {
392 QualType ElemTy = Context.getBaseElementType(AT);
393 const RecordType *RT = ElemTy->getAs<RecordType>();
394 if (!RT)
395 return true;
396
397 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
398 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
399
400 uint64_t NumElements = Context.getConstantArrayElementCount(AT);
401 uint64_t ElementOffset = Offset;
402 for (uint64_t I = 0; I != NumElements; ++I) {
Anders Carlsson45c1d282010-06-08 16:20:35 +0000403 // We don't have to keep looking past the maximum offset that's known to
404 // contain an empty class.
405 if (!AnyEmptySubobjectsBeyondOffset(ElementOffset))
406 return true;
407
Anders Carlssondb319762010-05-27 18:20:57 +0000408 if (!CanPlaceFieldSubobjectAtOffset(RD, RD, ElementOffset))
409 return false;
410
411 ElementOffset += Layout.getSize();
412 }
413 }
414
415 return true;
416}
417
418bool
419EmptySubobjectMap::CanPlaceFieldAtOffset(const FieldDecl *FD, uint64_t Offset) {
420 if (!CanPlaceFieldSubobjectAtOffset(FD, Offset))
421 return false;
422
423 // We are able to place the member variable at this offset.
424 // Make sure to update the empty base subobject map.
425 UpdateEmptyFieldSubobjects(FD, Offset);
426 return true;
427}
428
429void EmptySubobjectMap::UpdateEmptyFieldSubobjects(const CXXRecordDecl *RD,
430 const CXXRecordDecl *Class,
431 uint64_t Offset) {
Anders Carlssonae111dc2010-06-13 17:49:16 +0000432 // We know that the only empty subobjects that can conflict with empty
Anders Carlssoncc59cc52010-06-13 18:00:18 +0000433 // field subobjects are subobjects of empty bases that can be placed at offset
Anders Carlssonae111dc2010-06-13 17:49:16 +0000434 // zero. Because of this, we only need to keep track of empty field
435 // subobjects with offsets less than the size of the largest empty
436 // subobject for our class.
437 if (Offset >= SizeOfLargestEmptySubobject)
438 return;
439
Anders Carlssondb319762010-05-27 18:20:57 +0000440 AddSubobjectAtOffset(RD, Offset);
441
442 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
443
444 // Traverse all non-virtual bases.
445 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
446 E = RD->bases_end(); I != E; ++I) {
447 if (I->isVirtual())
448 continue;
449
450 const CXXRecordDecl *BaseDecl =
451 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
452
453 uint64_t BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl);
454 UpdateEmptyFieldSubobjects(BaseDecl, Class, BaseOffset);
455 }
456
Anders Carlsson44687202010-06-08 19:09:24 +0000457 if (RD == Class) {
458 // This is the most derived class, traverse virtual bases as well.
459 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
460 E = RD->vbases_end(); I != E; ++I) {
461 const CXXRecordDecl *VBaseDecl =
462 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
463
464 uint64_t VBaseOffset = Offset + Layout.getVBaseClassOffset(VBaseDecl);
465 UpdateEmptyFieldSubobjects(VBaseDecl, Class, VBaseOffset);
466 }
467 }
468
Anders Carlssondb319762010-05-27 18:20:57 +0000469 // Traverse all member variables.
470 unsigned FieldNo = 0;
471 for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
472 I != E; ++I, ++FieldNo) {
473 const FieldDecl *FD = *I;
474
475 uint64_t FieldOffset = Offset + Layout.getFieldOffset(FieldNo);
476
477 UpdateEmptyFieldSubobjects(FD, FieldOffset);
478 }
479}
480
481void EmptySubobjectMap::UpdateEmptyFieldSubobjects(const FieldDecl *FD,
482 uint64_t Offset) {
483 QualType T = FD->getType();
484 if (const RecordType *RT = T->getAs<RecordType>()) {
485 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
486 UpdateEmptyFieldSubobjects(RD, RD, Offset);
487 return;
488 }
489
490 // If we have an array type we need to update every element.
491 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) {
492 QualType ElemTy = Context.getBaseElementType(AT);
493 const RecordType *RT = ElemTy->getAs<RecordType>();
494 if (!RT)
495 return;
496
497 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
498 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
499
500 uint64_t NumElements = Context.getConstantArrayElementCount(AT);
501 uint64_t ElementOffset = Offset;
502
503 for (uint64_t I = 0; I != NumElements; ++I) {
Anders Carlssonae111dc2010-06-13 17:49:16 +0000504 // We know that the only empty subobjects that can conflict with empty
Anders Carlssoncc59cc52010-06-13 18:00:18 +0000505 // field subobjects are subobjects of empty bases that can be placed at
Anders Carlssonae111dc2010-06-13 17:49:16 +0000506 // offset zero. Because of this, we only need to keep track of empty field
507 // subobjects with offsets less than the size of the largest empty
508 // subobject for our class.
509 if (ElementOffset >= SizeOfLargestEmptySubobject)
510 return;
511
Anders Carlssondb319762010-05-27 18:20:57 +0000512 UpdateEmptyFieldSubobjects(RD, RD, ElementOffset);
513 ElementOffset += Layout.getSize();
514 }
515 }
516}
517
Anders Carlssonc2226202010-05-26 05:58:59 +0000518class RecordLayoutBuilder {
Charles Davisc2c576a2010-08-19 00:55:19 +0000519protected:
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000520 // FIXME: Remove this and make the appropriate fields public.
521 friend class clang::ASTContext;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000522
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000523 ASTContext &Context;
524
Anders Carlssonf58de112010-05-26 15:32:58 +0000525 EmptySubobjectMap *EmptySubobjects;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000526
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000527 /// Size - The current size of the record layout.
528 uint64_t Size;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000529
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000530 /// Alignment - The current alignment of the record layout.
531 unsigned Alignment;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000532
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000533 /// \brief The alignment if attribute packed is not used.
534 unsigned UnpackedAlignment;
535
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000536 llvm::SmallVector<uint64_t, 16> FieldOffsets;
537
538 /// Packed - Whether the record is packed or not.
Daniel Dunbar6da10982010-05-27 05:45:51 +0000539 unsigned Packed : 1;
540
541 unsigned IsUnion : 1;
542
543 unsigned IsMac68kAlign : 1;
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000544
545 /// UnfilledBitsInLastByte - If the last field laid out was a bitfield,
546 /// this contains the number of bits in the last byte that can be used for
547 /// an adjacent bitfield if necessary.
548 unsigned char UnfilledBitsInLastByte;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000549
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000550 /// MaxFieldAlignment - The maximum allowed field alignment. This is set by
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000551 /// #pragma pack.
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000552 unsigned MaxFieldAlignment;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000553
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000554 /// DataSize - The data size of the record being laid out.
555 uint64_t DataSize;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000556
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000557 uint64_t NonVirtualSize;
558 unsigned NonVirtualAlignment;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000559
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000560 /// PrimaryBase - the primary base class (if one exists) of the class
561 /// we're laying out.
562 const CXXRecordDecl *PrimaryBase;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000563
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000564 /// PrimaryBaseIsVirtual - Whether the primary base of the class we're laying
565 /// out is virtual.
566 bool PrimaryBaseIsVirtual;
567
568 typedef llvm::DenseMap<const CXXRecordDecl *, uint64_t> BaseOffsetsMapTy;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000569
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000570 /// Bases - base classes and their offsets in the record.
571 BaseOffsetsMapTy Bases;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000572
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000573 // VBases - virtual base classes and their offsets in the record.
574 BaseOffsetsMapTy VBases;
575
576 /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are
577 /// primary base classes for some other direct or indirect base class.
578 llvm::SmallSet<const CXXRecordDecl*, 32> IndirectPrimaryBases;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000579
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000580 /// FirstNearlyEmptyVBase - The first nearly empty virtual base class in
581 /// inheritance graph order. Used for determining the primary base class.
582 const CXXRecordDecl *FirstNearlyEmptyVBase;
583
584 /// VisitedVirtualBases - A set of all the visited virtual bases, used to
585 /// avoid visiting virtual bases more than once.
586 llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000587
Anders Carlssonc121b4e2010-05-27 00:07:01 +0000588 RecordLayoutBuilder(ASTContext &Context, EmptySubobjectMap *EmptySubobjects)
589 : Context(Context), EmptySubobjects(EmptySubobjects), Size(0), Alignment(8),
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000590 UnpackedAlignment(Alignment), Packed(false), IsUnion(false),
591 IsMac68kAlign(false), UnfilledBitsInLastByte(0), MaxFieldAlignment(0),
592 DataSize(0), NonVirtualSize(0), NonVirtualAlignment(8), PrimaryBase(0),
Daniel Dunbar6da10982010-05-27 05:45:51 +0000593 PrimaryBaseIsVirtual(false), FirstNearlyEmptyVBase(0) { }
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000594
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000595 void Layout(const RecordDecl *D);
Anders Carlssonc28a6c92010-05-26 15:10:00 +0000596 void Layout(const CXXRecordDecl *D);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000597 void Layout(const ObjCInterfaceDecl *D);
598
599 void LayoutFields(const RecordDecl *D);
600 void LayoutField(const FieldDecl *D);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000601 void LayoutWideBitField(uint64_t FieldSize, uint64_t TypeSize,
602 bool FieldPacked, const FieldDecl *D);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000603 void LayoutBitField(const FieldDecl *D);
604
Anders Carlssone3c24c72010-05-29 17:35:14 +0000605 /// BaseSubobjectInfoAllocator - Allocator for BaseSubobjectInfo objects.
606 llvm::SpecificBumpPtrAllocator<BaseSubobjectInfo> BaseSubobjectInfoAllocator;
607
608 typedef llvm::DenseMap<const CXXRecordDecl *, BaseSubobjectInfo *>
609 BaseSubobjectInfoMapTy;
610
611 /// VirtualBaseInfo - Map from all the (direct or indirect) virtual bases
612 /// of the class we're laying out to their base subobject info.
613 BaseSubobjectInfoMapTy VirtualBaseInfo;
614
615 /// NonVirtualBaseInfo - Map from all the direct non-virtual bases of the
616 /// class we're laying out to their base subobject info.
617 BaseSubobjectInfoMapTy NonVirtualBaseInfo;
618
619 /// ComputeBaseSubobjectInfo - Compute the base subobject information for the
620 /// bases of the given class.
621 void ComputeBaseSubobjectInfo(const CXXRecordDecl *RD);
622
623 /// ComputeBaseSubobjectInfo - Compute the base subobject information for a
624 /// single class and all of its base classes.
625 BaseSubobjectInfo *ComputeBaseSubobjectInfo(const CXXRecordDecl *RD,
626 bool IsVirtual,
627 BaseSubobjectInfo *Derived);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000628
629 /// DeterminePrimaryBase - Determine the primary base of the given class.
630 void DeterminePrimaryBase(const CXXRecordDecl *RD);
631
632 void SelectPrimaryVBase(const CXXRecordDecl *RD);
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000633
Charles Davisc2c576a2010-08-19 00:55:19 +0000634 virtual uint64_t GetVirtualPointersSize(const CXXRecordDecl *RD) const;
635
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000636 /// IdentifyPrimaryBases - Identify all virtual base classes, direct or
637 /// indirect, that are primary base classes for some other direct or indirect
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000638 /// base class.
639 void IdentifyPrimaryBases(const CXXRecordDecl *RD);
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000640
Charles Davisc2c576a2010-08-19 00:55:19 +0000641 virtual bool IsNearlyEmpty(const CXXRecordDecl *RD) const;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000642
643 /// LayoutNonVirtualBases - Determines the primary base class (if any) and
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000644 /// lays it out. Will then proceed to lay out all non-virtual base clasess.
645 void LayoutNonVirtualBases(const CXXRecordDecl *RD);
646
647 /// LayoutNonVirtualBase - Lays out a single non-virtual base.
Anders Carlssonbb0e6782010-05-29 17:42:25 +0000648 void LayoutNonVirtualBase(const BaseSubobjectInfo *Base);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000649
Anders Carlsson6b0d9142010-05-29 19:44:50 +0000650 void AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info,
Charles Davisc2c576a2010-08-19 00:55:19 +0000651 uint64_t Offset);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000652
653 /// LayoutVirtualBases - Lays out all the virtual bases.
654 void LayoutVirtualBases(const CXXRecordDecl *RD,
655 const CXXRecordDecl *MostDerivedClass);
656
657 /// LayoutVirtualBase - Lays out a single virtual base.
Anders Carlssond6ff5d72010-05-29 17:48:36 +0000658 void LayoutVirtualBase(const BaseSubobjectInfo *Base);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000659
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000660 /// LayoutBase - Will lay out a base and return the offset where it was
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000661 /// placed, in bits.
Anders Carlssond7f3fcf2010-05-29 20:47:33 +0000662 uint64_t LayoutBase(const BaseSubobjectInfo *Base);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000663
Anders Carlssonc28a6c92010-05-26 15:10:00 +0000664 /// InitializeLayout - Initialize record layout for the given record decl.
Daniel Dunbar6da10982010-05-27 05:45:51 +0000665 void InitializeLayout(const Decl *D);
Anders Carlssonc28a6c92010-05-26 15:10:00 +0000666
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000667 /// FinishLayout - Finalize record layout. Adjust record size based on the
668 /// alignment.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000669 void FinishLayout(const NamedDecl *D);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000670
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000671 void UpdateAlignment(unsigned NewAlignment, unsigned UnpackedNewAlignment);
672 void UpdateAlignment(unsigned NewAlignment) {
673 UpdateAlignment(NewAlignment, NewAlignment);
674 }
675
676 void CheckFieldPadding(uint64_t Offset, uint64_t UnpaddedOffset,
677 uint64_t UnpackedOffset, unsigned UnpackedAlign,
678 bool isPacked, const FieldDecl *D);
679
680 DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000681
Argyrios Kyrtzidis499e6e42010-08-15 10:17:39 +0000682 RecordLayoutBuilder(const RecordLayoutBuilder&); // DO NOT IMPLEMENT
683 void operator=(const RecordLayoutBuilder&); // DO NOT IMPLEMENT
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000684public:
685 static const CXXMethodDecl *ComputeKeyFunction(const CXXRecordDecl *RD);
Argyrios Kyrtzidis3c6cd172010-08-25 00:32:14 +0000686
687 virtual ~RecordLayoutBuilder() { }
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000688};
Benjamin Kramerc7656cd2010-05-26 09:58:31 +0000689} // end anonymous namespace
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000690
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000691/// IsNearlyEmpty - Indicates when a class has a vtable pointer, but
692/// no other data.
Anders Carlssonc2226202010-05-26 05:58:59 +0000693bool RecordLayoutBuilder::IsNearlyEmpty(const CXXRecordDecl *RD) const {
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000694 // FIXME: Audit the corners
695 if (!RD->isDynamicClass())
696 return false;
Anders Carlsson5efc56e2010-04-16 15:07:51 +0000697 const ASTRecordLayout &BaseInfo = Context.getASTRecordLayout(RD);
698 if (BaseInfo.getNonVirtualSize() == Context.Target.getPointerWidth(0))
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000699 return true;
700 return false;
701}
702
Anders Carlssonc2226202010-05-26 05:58:59 +0000703void RecordLayoutBuilder::IdentifyPrimaryBases(const CXXRecordDecl *RD) {
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000704 const ASTRecordLayout::PrimaryBaseInfo &BaseInfo =
Anders Carlsson5efc56e2010-04-16 15:07:51 +0000705 Context.getASTRecordLayout(RD).getPrimaryBaseInfo();
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000706
Anders Carlsson81430692009-09-22 03:02:06 +0000707 // If the record has a primary base class that is virtual, add it to the set
708 // of primary bases.
Anders Carlssona30c0d32009-11-27 22:14:40 +0000709 if (BaseInfo.isVirtual())
710 IndirectPrimaryBases.insert(BaseInfo.getBase());
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000711
Anders Carlsson81430692009-09-22 03:02:06 +0000712 // Now traverse all bases and find primary bases for them.
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000713 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000714 e = RD->bases_end(); i != e; ++i) {
Sebastian Redl1054fae2009-10-25 17:03:50 +0000715 assert(!i->getType()->isDependentType() &&
716 "Cannot layout class with dependent bases.");
Mike Stump11289f42009-09-09 15:08:12 +0000717 const CXXRecordDecl *Base =
Mike Stump78696a72009-08-11 04:03:59 +0000718 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000719
Mike Stump78696a72009-08-11 04:03:59 +0000720 // Only bases with virtual bases participate in computing the
721 // indirect primary virtual base classes.
Mike Stumpc2f591b2009-08-13 22:53:07 +0000722 if (Base->getNumVBases())
Anders Carlsson81430692009-09-22 03:02:06 +0000723 IdentifyPrimaryBases(Base);
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000724 }
725}
726
Anders Carlsson81430692009-09-22 03:02:06 +0000727void
Anders Carlssonc2226202010-05-26 05:58:59 +0000728RecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD) {
Anders Carlssonf2fa75b2010-03-11 03:39:12 +0000729 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000730 E = RD->bases_end(); I != E; ++I) {
Anders Carlssonf2fa75b2010-03-11 03:39:12 +0000731 assert(!I->getType()->isDependentType() &&
Sebastian Redl1054fae2009-10-25 17:03:50 +0000732 "Cannot layout class with dependent bases.");
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000733
Mike Stump11289f42009-09-09 15:08:12 +0000734 const CXXRecordDecl *Base =
Anders Carlssonf2fa75b2010-03-11 03:39:12 +0000735 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000736
Anders Carlssonf2fa75b2010-03-11 03:39:12 +0000737 // Check if this is a nearly empty virtual base.
738 if (I->isVirtual() && IsNearlyEmpty(Base)) {
739 // If it's not an indirect primary base, then we've found our primary
740 // base.
Anders Carlsson81430692009-09-22 03:02:06 +0000741 if (!IndirectPrimaryBases.count(Base)) {
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000742 PrimaryBase = Base;
743 PrimaryBaseIsVirtual = true;
Mike Stump6f3793b2009-08-12 21:50:08 +0000744 return;
745 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000746
Anders Carlssonf2fa75b2010-03-11 03:39:12 +0000747 // Is this the first nearly empty virtual base?
748 if (!FirstNearlyEmptyVBase)
749 FirstNearlyEmptyVBase = Base;
Mike Stump6f3793b2009-08-12 21:50:08 +0000750 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000751
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000752 SelectPrimaryVBase(Base);
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000753 if (PrimaryBase)
Zhongxing Xuec345b72010-02-15 04:28:35 +0000754 return;
Mike Stump6f3793b2009-08-12 21:50:08 +0000755 }
756}
757
Charles Davisc2c576a2010-08-19 00:55:19 +0000758uint64_t
759RecordLayoutBuilder::GetVirtualPointersSize(const CXXRecordDecl *RD) const {
760 return Context.Target.getPointerWidth(0);
761}
762
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000763/// DeterminePrimaryBase - Determine the primary base of the given class.
Anders Carlssonc2226202010-05-26 05:58:59 +0000764void RecordLayoutBuilder::DeterminePrimaryBase(const CXXRecordDecl *RD) {
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000765 // If the class isn't dynamic, it won't have a primary base.
766 if (!RD->isDynamicClass())
767 return;
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000768
Anders Carlsson81430692009-09-22 03:02:06 +0000769 // Compute all the primary virtual bases for all of our direct and
Mike Stump590a7c72009-08-13 23:26:06 +0000770 // indirect bases, and record all their primary virtual base classes.
Mike Stump590a7c72009-08-13 23:26:06 +0000771 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000772 e = RD->bases_end(); i != e; ++i) {
Sebastian Redl1054fae2009-10-25 17:03:50 +0000773 assert(!i->getType()->isDependentType() &&
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000774 "Cannot lay out class with dependent bases.");
Mike Stump11289f42009-09-09 15:08:12 +0000775 const CXXRecordDecl *Base =
Mike Stump590a7c72009-08-13 23:26:06 +0000776 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson81430692009-09-22 03:02:06 +0000777 IdentifyPrimaryBases(Base);
Mike Stump590a7c72009-08-13 23:26:06 +0000778 }
779
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000780 // If the record has a dynamic base class, attempt to choose a primary base
781 // class. It is the first (in direct base class order) non-virtual dynamic
Anders Carlsson81430692009-09-22 03:02:06 +0000782 // base class, if one exists.
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000783 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000784 e = RD->bases_end(); i != e; ++i) {
Anders Carlsson03ff3792009-11-27 22:05:05 +0000785 // Ignore virtual bases.
786 if (i->isVirtual())
787 continue;
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000788
Anders Carlsson03ff3792009-11-27 22:05:05 +0000789 const CXXRecordDecl *Base =
790 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
791
792 if (Base->isDynamicClass()) {
793 // We found it.
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000794 PrimaryBase = Base;
795 PrimaryBaseIsVirtual = false;
Anders Carlsson03ff3792009-11-27 22:05:05 +0000796 return;
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000797 }
798 }
799
800 // Otherwise, it is the first nearly empty virtual base that is not an
Mike Stump78696a72009-08-11 04:03:59 +0000801 // indirect primary virtual base class, if one exists.
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000802 if (RD->getNumVBases() != 0) {
803 SelectPrimaryVBase(RD);
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000804 if (PrimaryBase)
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000805 return;
806 }
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000807
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000808 // Otherwise, it is the first nearly empty virtual base that is not an
809 // indirect primary virtual base class, if one exists.
810 if (FirstNearlyEmptyVBase) {
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000811 PrimaryBase = FirstNearlyEmptyVBase;
812 PrimaryBaseIsVirtual = true;
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000813 return;
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000814 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000815
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000816 // Otherwise there is no primary base class.
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000817 assert(!PrimaryBase && "Should not get here with a primary base!");
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000818
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000819 // Allocate the virtual table pointer at offset zero.
820 assert(DataSize == 0 && "Vtable pointer must be at offset zero!");
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000821
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000822 // Update the size.
Charles Davisc2c576a2010-08-19 00:55:19 +0000823 Size += GetVirtualPointersSize(RD);
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000824 DataSize = Size;
825
826 // Update the alignment.
Anders Carlsson5efc56e2010-04-16 15:07:51 +0000827 UpdateAlignment(Context.Target.getPointerAlign(0));
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000828}
829
Anders Carlssone3c24c72010-05-29 17:35:14 +0000830BaseSubobjectInfo *
831RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD,
832 bool IsVirtual,
833 BaseSubobjectInfo *Derived) {
834 BaseSubobjectInfo *Info;
835
836 if (IsVirtual) {
837 // Check if we already have info about this virtual base.
838 BaseSubobjectInfo *&InfoSlot = VirtualBaseInfo[RD];
839 if (InfoSlot) {
840 assert(InfoSlot->Class == RD && "Wrong class for virtual base info!");
841 return InfoSlot;
842 }
843
844 // We don't, create it.
845 InfoSlot = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo;
846 Info = InfoSlot;
847 } else {
848 Info = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo;
849 }
850
851 Info->Class = RD;
852 Info->IsVirtual = IsVirtual;
853 Info->Derived = 0;
854 Info->PrimaryVirtualBaseInfo = 0;
855
856 const CXXRecordDecl *PrimaryVirtualBase = 0;
857 BaseSubobjectInfo *PrimaryVirtualBaseInfo = 0;
858
859 // Check if this base has a primary virtual base.
860 if (RD->getNumVBases()) {
861 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
862 if (Layout.getPrimaryBaseWasVirtual()) {
863 // This base does have a primary virtual base.
864 PrimaryVirtualBase = Layout.getPrimaryBase();
865 assert(PrimaryVirtualBase && "Didn't have a primary virtual base!");
866
867 // Now check if we have base subobject info about this primary base.
868 PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase);
869
870 if (PrimaryVirtualBaseInfo) {
871 if (PrimaryVirtualBaseInfo->Derived) {
872 // We did have info about this primary base, and it turns out that it
873 // has already been claimed as a primary virtual base for another
874 // base.
875 PrimaryVirtualBase = 0;
876 } else {
877 // We can claim this base as our primary base.
878 Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo;
879 PrimaryVirtualBaseInfo->Derived = Info;
880 }
881 }
882 }
883 }
884
885 // Now go through all direct bases.
886 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
887 E = RD->bases_end(); I != E; ++I) {
888 bool IsVirtual = I->isVirtual();
889
890 const CXXRecordDecl *BaseDecl =
891 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
892
893 Info->Bases.push_back(ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, Info));
894 }
895
896 if (PrimaryVirtualBase && !PrimaryVirtualBaseInfo) {
897 // Traversing the bases must have created the base info for our primary
898 // virtual base.
899 PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase);
900 assert(PrimaryVirtualBaseInfo &&
901 "Did not create a primary virtual base!");
902
903 // Claim the primary virtual base as our primary virtual base.
904 Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo;
905 PrimaryVirtualBaseInfo->Derived = Info;
906 }
907
908 return Info;
909}
910
911void RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD) {
912 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
913 E = RD->bases_end(); I != E; ++I) {
914 bool IsVirtual = I->isVirtual();
915
916 const CXXRecordDecl *BaseDecl =
917 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
918
919 // Compute the base subobject info for this base.
920 BaseSubobjectInfo *Info = ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, 0);
921
922 if (IsVirtual) {
923 // ComputeBaseInfo has already added this base for us.
924 assert(VirtualBaseInfo.count(BaseDecl) &&
925 "Did not add virtual base!");
926 } else {
927 // Add the base info to the map of non-virtual bases.
928 assert(!NonVirtualBaseInfo.count(BaseDecl) &&
929 "Non-virtual base already exists!");
930 NonVirtualBaseInfo.insert(std::make_pair(BaseDecl, Info));
931 }
932 }
933}
934
Anders Carlsson09ffa322010-03-10 22:21:28 +0000935void
Anders Carlssonc2226202010-05-26 05:58:59 +0000936RecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) {
Anders Carlssone3c24c72010-05-29 17:35:14 +0000937 // Then, determine the primary base class.
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000938 DeterminePrimaryBase(RD);
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000939
Anders Carlssone3c24c72010-05-29 17:35:14 +0000940 // Compute base subobject info.
941 ComputeBaseSubobjectInfo(RD);
942
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000943 // If we have a primary base class, lay it out.
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000944 if (PrimaryBase) {
945 if (PrimaryBaseIsVirtual) {
Anders Carlssone3c24c72010-05-29 17:35:14 +0000946 // If the primary virtual base was a primary virtual base of some other
947 // base class we'll have to steal it.
948 BaseSubobjectInfo *PrimaryBaseInfo = VirtualBaseInfo.lookup(PrimaryBase);
949 PrimaryBaseInfo->Derived = 0;
950
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000951 // We have a virtual primary base, insert it as an indirect primary base.
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000952 IndirectPrimaryBases.insert(PrimaryBase);
Anders Carlssonfe900962010-03-11 05:42:17 +0000953
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000954 assert(!VisitedVirtualBases.count(PrimaryBase) &&
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000955 "vbase already visited!");
956 VisitedVirtualBases.insert(PrimaryBase);
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000957
Anders Carlssond6ff5d72010-05-29 17:48:36 +0000958 LayoutVirtualBase(PrimaryBaseInfo);
Anders Carlssonbb0e6782010-05-29 17:42:25 +0000959 } else {
960 BaseSubobjectInfo *PrimaryBaseInfo =
961 NonVirtualBaseInfo.lookup(PrimaryBase);
962 assert(PrimaryBaseInfo &&
963 "Did not find base info for non-virtual primary base!");
964
965 LayoutNonVirtualBase(PrimaryBaseInfo);
966 }
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000967 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000968
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000969 // Now lay out the non-virtual bases.
970 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000971 E = RD->bases_end(); I != E; ++I) {
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000972
973 // Ignore virtual bases.
974 if (I->isVirtual())
975 continue;
976
Anders Carlssonbb0e6782010-05-29 17:42:25 +0000977 const CXXRecordDecl *BaseDecl =
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000978 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
979
980 // Skip the primary base.
Anders Carlssonbb0e6782010-05-29 17:42:25 +0000981 if (BaseDecl == PrimaryBase && !PrimaryBaseIsVirtual)
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000982 continue;
983
984 // Lay out the base.
Anders Carlssonbb0e6782010-05-29 17:42:25 +0000985 BaseSubobjectInfo *BaseInfo = NonVirtualBaseInfo.lookup(BaseDecl);
986 assert(BaseInfo && "Did not find base info for non-virtual base!");
987
988 LayoutNonVirtualBase(BaseInfo);
Anders Carlsson09ffa322010-03-10 22:21:28 +0000989 }
990}
991
Anders Carlssonbb0e6782010-05-29 17:42:25 +0000992void RecordLayoutBuilder::LayoutNonVirtualBase(const BaseSubobjectInfo *Base) {
Anders Carlsson0d0b5882010-03-10 22:26:24 +0000993 // Layout the base.
Anders Carlssond7f3fcf2010-05-29 20:47:33 +0000994 uint64_t Offset = LayoutBase(Base);
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000995
Anders Carlsson0d0b5882010-03-10 22:26:24 +0000996 // Add its base class offset.
Anders Carlssonbb0e6782010-05-29 17:42:25 +0000997 assert(!Bases.count(Base->Class) && "base offset already exists!");
998 Bases.insert(std::make_pair(Base->Class, Offset));
Anders Carlsson6b0d9142010-05-29 19:44:50 +0000999
1000 AddPrimaryVirtualBaseOffsets(Base, Offset);
Anders Carlsson09ffa322010-03-10 22:21:28 +00001001}
Mike Stump2b84dd32009-11-05 04:02:15 +00001002
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001003void
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001004RecordLayoutBuilder::AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info,
1005 uint64_t Offset) {
1006 // This base isn't interesting, it has no virtual bases.
1007 if (!Info->Class->getNumVBases())
1008 return;
1009
1010 // First, check if we have a virtual primary base to add offsets for.
1011 if (Info->PrimaryVirtualBaseInfo) {
1012 assert(Info->PrimaryVirtualBaseInfo->IsVirtual &&
1013 "Primary virtual base is not virtual!");
1014 if (Info->PrimaryVirtualBaseInfo->Derived == Info) {
1015 // Add the offset.
1016 assert(!VBases.count(Info->PrimaryVirtualBaseInfo->Class) &&
1017 "primary vbase offset already exists!");
1018 VBases.insert(std::make_pair(Info->PrimaryVirtualBaseInfo->Class,
1019 Offset));
Anders Carlssonea7b1822010-04-15 16:12:58 +00001020
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001021 // Traverse the primary virtual base.
1022 AddPrimaryVirtualBaseOffsets(Info->PrimaryVirtualBaseInfo, Offset);
1023 }
Anders Carlssonea7b1822010-04-15 16:12:58 +00001024 }
1025
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001026 // Now go through all direct non-virtual bases.
1027 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class);
1028 for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) {
1029 const BaseSubobjectInfo *Base = Info->Bases[I];
1030 if (Base->IsVirtual)
Anders Carlssonea7b1822010-04-15 16:12:58 +00001031 continue;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001032
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001033 uint64_t BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class);
1034 AddPrimaryVirtualBaseOffsets(Base, BaseOffset);
Anders Carlssonea7b1822010-04-15 16:12:58 +00001035 }
1036}
1037
1038void
Anders Carlssonc2226202010-05-26 05:58:59 +00001039RecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
Anders Carlssonea7b1822010-04-15 16:12:58 +00001040 const CXXRecordDecl *MostDerivedClass) {
Anders Carlssonde710c92010-03-11 04:33:54 +00001041 const CXXRecordDecl *PrimaryBase;
Anders Carlsson291279e2010-04-10 18:42:27 +00001042 bool PrimaryBaseIsVirtual;
Anders Carlssonfe900962010-03-11 05:42:17 +00001043
Anders Carlsson291279e2010-04-10 18:42:27 +00001044 if (MostDerivedClass == RD) {
Anders Carlssond20e7cd2010-05-26 05:20:58 +00001045 PrimaryBase = this->PrimaryBase;
1046 PrimaryBaseIsVirtual = this->PrimaryBaseIsVirtual;
Anders Carlsson291279e2010-04-10 18:42:27 +00001047 } else {
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001048 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlssonde710c92010-03-11 04:33:54 +00001049 PrimaryBase = Layout.getPrimaryBase();
Anders Carlsson291279e2010-04-10 18:42:27 +00001050 PrimaryBaseIsVirtual = Layout.getPrimaryBaseWasVirtual();
1051 }
1052
Anders Carlssonf7b7a1e2010-03-11 04:24:02 +00001053 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1054 E = RD->bases_end(); I != E; ++I) {
1055 assert(!I->getType()->isDependentType() &&
Sebastian Redl1054fae2009-10-25 17:03:50 +00001056 "Cannot layout class with dependent bases.");
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001057
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001058 const CXXRecordDecl *BaseDecl =
Anders Carlssonf7b7a1e2010-03-11 04:24:02 +00001059 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1060
1061 if (I->isVirtual()) {
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001062 if (PrimaryBase != BaseDecl || !PrimaryBaseIsVirtual) {
1063 bool IndirectPrimaryBase = IndirectPrimaryBases.count(BaseDecl);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001064
Anders Carlsson291279e2010-04-10 18:42:27 +00001065 // Only lay out the virtual base if it's not an indirect primary base.
1066 if (!IndirectPrimaryBase) {
1067 // Only visit virtual bases once.
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001068 if (!VisitedVirtualBases.insert(BaseDecl))
Anders Carlsson291279e2010-04-10 18:42:27 +00001069 continue;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001070
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001071 const BaseSubobjectInfo *BaseInfo = VirtualBaseInfo.lookup(BaseDecl);
1072 assert(BaseInfo && "Did not find virtual base info!");
1073 LayoutVirtualBase(BaseInfo);
Anders Carlsson6a848892010-03-11 04:10:39 +00001074 }
Mike Stump2b84dd32009-11-05 04:02:15 +00001075 }
Mike Stumpc2f591b2009-08-13 22:53:07 +00001076 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001077
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001078 if (!BaseDecl->getNumVBases()) {
Anders Carlssonf7b7a1e2010-03-11 04:24:02 +00001079 // This base isn't interesting since it doesn't have any virtual bases.
1080 continue;
Mike Stump996576f32009-08-16 19:04:13 +00001081 }
Anders Carlssonf7b7a1e2010-03-11 04:24:02 +00001082
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001083 LayoutVirtualBases(BaseDecl, MostDerivedClass);
Mike Stump6b2556f2009-08-06 13:41:24 +00001084 }
1085}
1086
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001087void RecordLayoutBuilder::LayoutVirtualBase(const BaseSubobjectInfo *Base) {
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001088 assert(!Base->Derived && "Trying to lay out a primary virtual base!");
1089
Anders Carlsson0d0b5882010-03-10 22:26:24 +00001090 // Layout the base.
Anders Carlssond7f3fcf2010-05-29 20:47:33 +00001091 uint64_t Offset = LayoutBase(Base);
Anders Carlsson0d0b5882010-03-10 22:26:24 +00001092
1093 // Add its base class offset.
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001094 assert(!VBases.count(Base->Class) && "vbase offset already exists!");
1095 VBases.insert(std::make_pair(Base->Class, Offset));
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001096
1097 AddPrimaryVirtualBaseOffsets(Base, Offset);
Anders Carlsson09ffa322010-03-10 22:21:28 +00001098}
1099
Anders Carlssond7f3fcf2010-05-29 20:47:33 +00001100uint64_t RecordLayoutBuilder::LayoutBase(const BaseSubobjectInfo *Base) {
1101 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base->Class);
Anders Carlsson09ffa322010-03-10 22:21:28 +00001102
1103 // If we have an empty base class, try to place it at offset 0.
Anders Carlssond7f3fcf2010-05-29 20:47:33 +00001104 if (Base->Class->isEmpty() &&
Anders Carlssonb1fcdd02010-05-30 06:52:33 +00001105 EmptySubobjects->CanPlaceBaseAtOffset(Base, 0)) {
Anders Carlsson2357bfd2010-05-08 22:35:05 +00001106 Size = std::max(Size, Layout.getSize());
Anders Carlsson09ffa322010-03-10 22:21:28 +00001107
1108 return 0;
1109 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001110
Anders Carlsson2357bfd2010-05-08 22:35:05 +00001111 unsigned BaseAlign = Layout.getNonVirtualAlign();
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001112
Anders Carlsson09ffa322010-03-10 22:21:28 +00001113 // Round up the current record size to the base's alignment boundary.
1114 uint64_t Offset = llvm::RoundUpToAlignment(DataSize, BaseAlign);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001115
Anders Carlsson09ffa322010-03-10 22:21:28 +00001116 // Try to place the base.
Anders Carlssonb1fcdd02010-05-30 06:52:33 +00001117 while (!EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset))
Anders Carlsson09ffa322010-03-10 22:21:28 +00001118 Offset += BaseAlign;
Anders Carlsson09ffa322010-03-10 22:21:28 +00001119
Anders Carlssond7f3fcf2010-05-29 20:47:33 +00001120 if (!Base->Class->isEmpty()) {
Anders Carlsson09ffa322010-03-10 22:21:28 +00001121 // Update the data size.
Anders Carlsson2357bfd2010-05-08 22:35:05 +00001122 DataSize = Offset + Layout.getNonVirtualSize();
Anders Carlsson09ffa322010-03-10 22:21:28 +00001123
1124 Size = std::max(Size, DataSize);
1125 } else
Anders Carlsson2357bfd2010-05-08 22:35:05 +00001126 Size = std::max(Size, Offset + Layout.getSize());
Anders Carlsson09ffa322010-03-10 22:21:28 +00001127
1128 // Remember max struct/class alignment.
1129 UpdateAlignment(BaseAlign);
1130
Anders Carlsson09ffa322010-03-10 22:21:28 +00001131 return Offset;
1132}
1133
Daniel Dunbar6da10982010-05-27 05:45:51 +00001134void RecordLayoutBuilder::InitializeLayout(const Decl *D) {
1135 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
1136 IsUnion = RD->isUnion();
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001137
Anders Carlsson28a5fa22009-08-08 19:38:24 +00001138 Packed = D->hasAttr<PackedAttr>();
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001139
Daniel Dunbar6da10982010-05-27 05:45:51 +00001140 // mac68k alignment supersedes maximum field alignment and attribute aligned,
1141 // and forces all structures to have 2-byte alignment. The IBM docs on it
1142 // allude to additional (more complicated) semantics, especially with regard
1143 // to bit-fields, but gcc appears not to follow that.
1144 if (D->hasAttr<AlignMac68kAttr>()) {
1145 IsMac68kAlign = true;
1146 MaxFieldAlignment = 2 * 8;
1147 Alignment = 2 * 8;
1148 } else {
1149 if (const MaxFieldAlignmentAttr *MFAA = D->getAttr<MaxFieldAlignmentAttr>())
1150 MaxFieldAlignment = MFAA->getAlignment();
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001151
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001152 if (unsigned MaxAlign = D->getMaxAlignment())
1153 UpdateAlignment(MaxAlign);
Daniel Dunbar6da10982010-05-27 05:45:51 +00001154 }
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001155}
Anders Carlsson6d9f6f32009-07-19 00:18:47 +00001156
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001157void RecordLayoutBuilder::Layout(const RecordDecl *D) {
1158 InitializeLayout(D);
Anders Carlsson118ce162009-07-18 21:48:39 +00001159 LayoutFields(D);
Mike Stump11289f42009-09-09 15:08:12 +00001160
Anders Carlsson79474332009-07-18 20:20:21 +00001161 // Finally, round the size of the total struct up to the alignment of the
1162 // struct itself.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001163 FinishLayout(D);
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001164}
1165
1166void RecordLayoutBuilder::Layout(const CXXRecordDecl *RD) {
1167 InitializeLayout(RD);
1168
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001169 // Lay out the vtable and the non-virtual bases.
1170 LayoutNonVirtualBases(RD);
1171
1172 LayoutFields(RD);
1173
1174 NonVirtualSize = Size;
1175 NonVirtualAlignment = Alignment;
1176
1177 // Lay out the virtual bases and add the primary virtual base offsets.
1178 LayoutVirtualBases(RD, RD);
1179
1180 VisitedVirtualBases.clear();
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001181
1182 // Finally, round the size of the total struct up to the alignment of the
1183 // struct itself.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001184 FinishLayout(RD);
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001185
Anders Carlsson5b441d72010-04-10 21:24:48 +00001186#ifndef NDEBUG
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001187 // Check that we have base offsets for all bases.
1188 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1189 E = RD->bases_end(); I != E; ++I) {
1190 if (I->isVirtual())
1191 continue;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001192
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001193 const CXXRecordDecl *BaseDecl =
1194 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1195
1196 assert(Bases.count(BaseDecl) && "Did not find base offset!");
1197 }
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001198
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001199 // And all virtual bases.
1200 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
1201 E = RD->vbases_end(); I != E; ++I) {
1202 const CXXRecordDecl *BaseDecl =
1203 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001204
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001205 assert(VBases.count(BaseDecl) && "Did not find base offset!");
Anders Carlsson5b441d72010-04-10 21:24:48 +00001206 }
1207#endif
Anders Carlsson79474332009-07-18 20:20:21 +00001208}
1209
Anders Carlssonc2226202010-05-26 05:58:59 +00001210void RecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D) {
Anders Carlsson4f516282009-07-18 20:50:59 +00001211 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001212 const ASTRecordLayout &SL = Context.getASTObjCInterfaceLayout(SD);
Anders Carlsson4f516282009-07-18 20:50:59 +00001213
1214 UpdateAlignment(SL.getAlignment());
Mike Stump11289f42009-09-09 15:08:12 +00001215
Anders Carlsson4f516282009-07-18 20:50:59 +00001216 // We start laying out ivars not at the end of the superclass
1217 // structure, but at the next byte following the last field.
Anders Carlsson27b50132009-07-18 21:26:44 +00001218 Size = llvm::RoundUpToAlignment(SL.getDataSize(), 8);
Anders Carlsson47680d82009-09-26 01:34:51 +00001219 DataSize = Size;
Anders Carlsson4f516282009-07-18 20:50:59 +00001220 }
Mike Stump11289f42009-09-09 15:08:12 +00001221
Daniel Dunbar6da10982010-05-27 05:45:51 +00001222 InitializeLayout(D);
Daniel Dunbar40130442010-05-27 01:12:46 +00001223
Anders Carlsson4f516282009-07-18 20:50:59 +00001224 // Layout each ivar sequentially.
1225 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001226 Context.ShallowCollectObjCIvars(D, Ivars);
Anders Carlsson4f516282009-07-18 20:50:59 +00001227 for (unsigned i = 0, e = Ivars.size(); i != e; ++i)
1228 LayoutField(Ivars[i]);
Mike Stump11289f42009-09-09 15:08:12 +00001229
Anders Carlsson4f516282009-07-18 20:50:59 +00001230 // Finally, round the size of the total struct up to the alignment of the
1231 // struct itself.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001232 FinishLayout(D);
Anders Carlsson4f516282009-07-18 20:50:59 +00001233}
1234
Anders Carlssonc2226202010-05-26 05:58:59 +00001235void RecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
Anders Carlsson118ce162009-07-18 21:48:39 +00001236 // Layout each field, for now, just sequentially, respecting alignment. In
1237 // the future, this will need to be tweakable by targets.
Mike Stump11289f42009-09-09 15:08:12 +00001238 for (RecordDecl::field_iterator Field = D->field_begin(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001239 FieldEnd = D->field_end(); Field != FieldEnd; ++Field)
Anders Carlsson118ce162009-07-18 21:48:39 +00001240 LayoutField(*Field);
1241}
1242
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001243void RecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize,
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001244 uint64_t TypeSize,
1245 bool FieldPacked,
1246 const FieldDecl *D) {
Anders Carlsson57235162010-04-16 15:57:11 +00001247 assert(Context.getLangOptions().CPlusPlus &&
1248 "Can only have wide bit-fields in C++!");
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001249
Anders Carlsson57235162010-04-16 15:57:11 +00001250 // Itanium C++ ABI 2.4:
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001251 // If sizeof(T)*8 < n, let T' be the largest integral POD type with
Anders Carlsson57235162010-04-16 15:57:11 +00001252 // sizeof(T')*8 <= n.
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001253
Anders Carlsson57235162010-04-16 15:57:11 +00001254 QualType IntegralPODTypes[] = {
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001255 Context.UnsignedCharTy, Context.UnsignedShortTy, Context.UnsignedIntTy,
Anders Carlsson57235162010-04-16 15:57:11 +00001256 Context.UnsignedLongTy, Context.UnsignedLongLongTy
1257 };
1258
Anders Carlsson57235162010-04-16 15:57:11 +00001259 QualType Type;
1260 for (unsigned I = 0, E = llvm::array_lengthof(IntegralPODTypes);
1261 I != E; ++I) {
1262 uint64_t Size = Context.getTypeSize(IntegralPODTypes[I]);
Anders Carlsson57235162010-04-16 15:57:11 +00001263
1264 if (Size > FieldSize)
1265 break;
1266
1267 Type = IntegralPODTypes[I];
1268 }
1269 assert(!Type.isNull() && "Did not find a type!");
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001270
Anders Carlsson57235162010-04-16 15:57:11 +00001271 unsigned TypeAlign = Context.getTypeAlign(Type);
1272
1273 // We're not going to use any of the unfilled bits in the last byte.
1274 UnfilledBitsInLastByte = 0;
1275
Anders Carlssonaad5fa82010-04-17 20:21:41 +00001276 uint64_t FieldOffset;
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001277 uint64_t UnpaddedFieldOffset = DataSize - UnfilledBitsInLastByte;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001278
Anders Carlsson57235162010-04-16 15:57:11 +00001279 if (IsUnion) {
1280 DataSize = std::max(DataSize, FieldSize);
Anders Carlssonaad5fa82010-04-17 20:21:41 +00001281 FieldOffset = 0;
Anders Carlsson57235162010-04-16 15:57:11 +00001282 } else {
Anders Carlssonaad5fa82010-04-17 20:21:41 +00001283 // The bitfield is allocated starting at the next offset aligned appropriately
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001284 // for T', with length n bits.
Anders Carlssonaad5fa82010-04-17 20:21:41 +00001285 FieldOffset = llvm::RoundUpToAlignment(DataSize, TypeAlign);
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001286
Anders Carlsson57235162010-04-16 15:57:11 +00001287 uint64_t NewSizeInBits = FieldOffset + FieldSize;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001288
Anders Carlsson57235162010-04-16 15:57:11 +00001289 DataSize = llvm::RoundUpToAlignment(NewSizeInBits, 8);
1290 UnfilledBitsInLastByte = DataSize - NewSizeInBits;
1291 }
1292
1293 // Place this field at the current location.
1294 FieldOffsets.push_back(FieldOffset);
1295
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001296 CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, FieldOffset,
1297 TypeAlign, FieldPacked, D);
1298
Anders Carlsson57235162010-04-16 15:57:11 +00001299 // Update the size.
1300 Size = std::max(Size, DataSize);
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001301
Anders Carlsson57235162010-04-16 15:57:11 +00001302 // Remember max struct/class alignment.
1303 UpdateAlignment(TypeAlign);
1304}
1305
Anders Carlssonc2226202010-05-26 05:58:59 +00001306void RecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
Anders Carlsson07209442009-11-22 17:37:31 +00001307 bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001308 uint64_t UnpaddedFieldOffset = DataSize - UnfilledBitsInLastByte;
1309 uint64_t FieldOffset = IsUnion ? 0 : UnpaddedFieldOffset;
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001310 uint64_t FieldSize = D->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001311
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001312 std::pair<uint64_t, unsigned> FieldInfo = Context.getTypeInfo(D->getType());
Anders Carlsson07209442009-11-22 17:37:31 +00001313 uint64_t TypeSize = FieldInfo.first;
1314 unsigned FieldAlign = FieldInfo.second;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001315
Anders Carlsson57235162010-04-16 15:57:11 +00001316 if (FieldSize > TypeSize) {
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001317 LayoutWideBitField(FieldSize, TypeSize, FieldPacked, D);
Anders Carlsson57235162010-04-16 15:57:11 +00001318 return;
1319 }
1320
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001321 // The align if the field is not packed. This is to check if the attribute
1322 // was unnecessary (-Wpacked).
1323 unsigned UnpackedFieldAlign = FieldAlign;
1324 uint64_t UnpackedFieldOffset = FieldOffset;
1325 if (!Context.Target.useBitFieldTypeAlignment())
1326 UnpackedFieldAlign = 1;
1327
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001328 if (FieldPacked || !Context.Target.useBitFieldTypeAlignment())
Anders Carlsson07209442009-11-22 17:37:31 +00001329 FieldAlign = 1;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001330 FieldAlign = std::max(FieldAlign, D->getMaxAlignment());
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001331 UnpackedFieldAlign = std::max(UnpackedFieldAlign, D->getMaxAlignment());
Anders Carlsson07209442009-11-22 17:37:31 +00001332
1333 // The maximum field alignment overrides the aligned attribute.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001334 if (MaxFieldAlignment) {
Anders Carlsson07209442009-11-22 17:37:31 +00001335 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001336 UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignment);
1337 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001338
Daniel Dunbar3d9289c2010-04-15 06:18:39 +00001339 // Check if we need to add padding to give the field the correct alignment.
Anders Carlsson07209442009-11-22 17:37:31 +00001340 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
Daniel Dunbarf35e7652010-06-29 18:34:35 +00001341 FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001342
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001343 if (FieldSize == 0 ||
1344 (UnpackedFieldOffset & (UnpackedFieldAlign-1)) + FieldSize > TypeSize)
1345 UnpackedFieldOffset = llvm::RoundUpToAlignment(UnpackedFieldOffset,
1346 UnpackedFieldAlign);
1347
Daniel Dunbar3d9289c2010-04-15 06:18:39 +00001348 // Padding members don't affect overall alignment.
Anders Carlsson07209442009-11-22 17:37:31 +00001349 if (!D->getIdentifier())
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001350 FieldAlign = UnpackedFieldAlign = 1;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001351
Anders Carlsson07209442009-11-22 17:37:31 +00001352 // Place this field at the current location.
1353 FieldOffsets.push_back(FieldOffset);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001354
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001355 CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, UnpackedFieldOffset,
1356 UnpackedFieldAlign, FieldPacked, D);
1357
Anders Carlssonba958402009-11-22 19:13:51 +00001358 // Update DataSize to include the last byte containing (part of) the bitfield.
1359 if (IsUnion) {
1360 // FIXME: I think FieldSize should be TypeSize here.
1361 DataSize = std::max(DataSize, FieldSize);
1362 } else {
1363 uint64_t NewSizeInBits = FieldOffset + FieldSize;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001364
Anders Carlssonba958402009-11-22 19:13:51 +00001365 DataSize = llvm::RoundUpToAlignment(NewSizeInBits, 8);
1366 UnfilledBitsInLastByte = DataSize - NewSizeInBits;
1367 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001368
Anders Carlssonba958402009-11-22 19:13:51 +00001369 // Update the size.
1370 Size = std::max(Size, DataSize);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001371
Anders Carlsson07209442009-11-22 17:37:31 +00001372 // Remember max struct/class alignment.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001373 UpdateAlignment(FieldAlign, UnpackedFieldAlign);
Anders Carlsson07209442009-11-22 17:37:31 +00001374}
1375
Anders Carlssonc2226202010-05-26 05:58:59 +00001376void RecordLayoutBuilder::LayoutField(const FieldDecl *D) {
Anders Carlsson07209442009-11-22 17:37:31 +00001377 if (D->isBitField()) {
1378 LayoutBitField(D);
1379 return;
1380 }
1381
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001382 uint64_t UnpaddedFieldOffset = DataSize - UnfilledBitsInLastByte;
1383
Anders Carlssonba958402009-11-22 19:13:51 +00001384 // Reset the unfilled bits.
1385 UnfilledBitsInLastByte = 0;
1386
Anders Carlsson07209442009-11-22 17:37:31 +00001387 bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
Anders Carlsson47680d82009-09-26 01:34:51 +00001388 uint64_t FieldOffset = IsUnion ? 0 : DataSize;
Anders Carlsson79474332009-07-18 20:20:21 +00001389 uint64_t FieldSize;
1390 unsigned FieldAlign;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001391
Anders Carlsson07209442009-11-22 17:37:31 +00001392 if (D->getType()->isIncompleteArrayType()) {
1393 // This is a flexible array member; we can't directly
1394 // query getTypeInfo about these, so we figure it out here.
1395 // Flexible array members don't have any size, but they
1396 // have to be aligned appropriately for their element type.
1397 FieldSize = 0;
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001398 const ArrayType* ATy = Context.getAsArrayType(D->getType());
1399 FieldAlign = Context.getTypeAlign(ATy->getElementType());
Anders Carlsson07209442009-11-22 17:37:31 +00001400 } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) {
1401 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001402 FieldSize = Context.Target.getPointerWidth(AS);
1403 FieldAlign = Context.Target.getPointerAlign(AS);
Anders Carlsson79474332009-07-18 20:20:21 +00001404 } else {
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001405 std::pair<uint64_t, unsigned> FieldInfo = Context.getTypeInfo(D->getType());
Anders Carlsson07209442009-11-22 17:37:31 +00001406 FieldSize = FieldInfo.first;
1407 FieldAlign = FieldInfo.second;
Anders Carlsson79474332009-07-18 20:20:21 +00001408 }
Mike Stump11289f42009-09-09 15:08:12 +00001409
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001410 // The align if the field is not packed. This is to check if the attribute
1411 // was unnecessary (-Wpacked).
1412 unsigned UnpackedFieldAlign = FieldAlign;
1413 uint64_t UnpackedFieldOffset = FieldOffset;
1414
Anders Carlsson07209442009-11-22 17:37:31 +00001415 if (FieldPacked)
1416 FieldAlign = 8;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001417 FieldAlign = std::max(FieldAlign, D->getMaxAlignment());
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001418 UnpackedFieldAlign = std::max(UnpackedFieldAlign, D->getMaxAlignment());
Anders Carlsson07209442009-11-22 17:37:31 +00001419
1420 // The maximum field alignment overrides the aligned attribute.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001421 if (MaxFieldAlignment) {
Anders Carlsson07209442009-11-22 17:37:31 +00001422 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001423 UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignment);
1424 }
Anders Carlsson07209442009-11-22 17:37:31 +00001425
1426 // Round up the current record size to the field's alignment boundary.
1427 FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001428 UnpackedFieldOffset = llvm::RoundUpToAlignment(UnpackedFieldOffset,
1429 UnpackedFieldAlign);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001430
Anders Carlssonb1fcdd02010-05-30 06:52:33 +00001431 if (!IsUnion && EmptySubobjects) {
1432 // Check if we can place the field at this offset.
1433 while (!EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset)) {
Anders Carlsson07209442009-11-22 17:37:31 +00001434 // We couldn't place the field at the offset. Try again at a new offset.
1435 FieldOffset += FieldAlign;
1436 }
Anders Carlsson07209442009-11-22 17:37:31 +00001437 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001438
Anders Carlsson79474332009-07-18 20:20:21 +00001439 // Place this field at the current location.
1440 FieldOffsets.push_back(FieldOffset);
Mike Stump11289f42009-09-09 15:08:12 +00001441
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001442 CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, UnpackedFieldOffset,
1443 UnpackedFieldAlign, FieldPacked, D);
1444
Anders Carlsson79474332009-07-18 20:20:21 +00001445 // Reserve space for this field.
1446 if (IsUnion)
1447 Size = std::max(Size, FieldSize);
1448 else
1449 Size = FieldOffset + FieldSize;
Mike Stump11289f42009-09-09 15:08:12 +00001450
Anders Carlsson47680d82009-09-26 01:34:51 +00001451 // Update the data size.
1452 DataSize = Size;
Mike Stump11289f42009-09-09 15:08:12 +00001453
Anders Carlsson79474332009-07-18 20:20:21 +00001454 // Remember max struct/class alignment.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001455 UpdateAlignment(FieldAlign, UnpackedFieldAlign);
Anders Carlsson79474332009-07-18 20:20:21 +00001456}
1457
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001458void RecordLayoutBuilder::FinishLayout(const NamedDecl *D) {
Anders Carlsson79474332009-07-18 20:20:21 +00001459 // In C++, records cannot be of size 0.
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001460 if (Context.getLangOptions().CPlusPlus && Size == 0)
Anders Carlsson79474332009-07-18 20:20:21 +00001461 Size = 8;
1462 // Finally, round the size of the record up to the alignment of the
1463 // record itself.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001464 uint64_t UnpaddedSize = Size - UnfilledBitsInLastByte;
1465 uint64_t UnpackedSize = llvm::RoundUpToAlignment(Size, UnpackedAlignment);
Anders Carlsson07209442009-11-22 17:37:31 +00001466 Size = llvm::RoundUpToAlignment(Size, Alignment);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001467
1468 unsigned CharBitNum = Context.Target.getCharWidth();
1469 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) {
1470 // Warn if padding was introduced to the struct/class/union.
1471 if (Size > UnpaddedSize) {
1472 unsigned PadSize = Size - UnpaddedSize;
1473 bool InBits = true;
1474 if (PadSize % CharBitNum == 0) {
1475 PadSize = PadSize / CharBitNum;
1476 InBits = false;
1477 }
1478 Diag(RD->getLocation(), diag::warn_padded_struct_size)
1479 << Context.getTypeDeclType(RD)
1480 << PadSize
1481 << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not
1482 }
1483
1484 // Warn if we packed it unnecessarily. If the alignment is 1 byte don't
1485 // bother since there won't be alignment issues.
1486 if (Packed && UnpackedAlignment > CharBitNum && Size == UnpackedSize)
1487 Diag(D->getLocation(), diag::warn_unnecessary_packed)
1488 << Context.getTypeDeclType(RD);
1489 }
Anders Carlsson79474332009-07-18 20:20:21 +00001490}
1491
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001492void RecordLayoutBuilder::UpdateAlignment(unsigned NewAlignment,
1493 unsigned UnpackedNewAlignment) {
Daniel Dunbar6da10982010-05-27 05:45:51 +00001494 // The alignment is not modified when using 'mac68k' alignment.
1495 if (IsMac68kAlign)
1496 return;
1497
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001498 if (NewAlignment > Alignment) {
1499 assert(llvm::isPowerOf2_32(NewAlignment && "Alignment not a power of 2"));
1500 Alignment = NewAlignment;
1501 }
1502
1503 if (UnpackedNewAlignment > UnpackedAlignment) {
1504 assert(llvm::isPowerOf2_32(UnpackedNewAlignment &&
1505 "Alignment not a power of 2"));
1506 UnpackedAlignment = UnpackedNewAlignment;
1507 }
1508}
1509
1510void RecordLayoutBuilder::CheckFieldPadding(uint64_t Offset,
1511 uint64_t UnpaddedOffset,
1512 uint64_t UnpackedOffset,
1513 unsigned UnpackedAlign,
1514 bool isPacked,
1515 const FieldDecl *D) {
1516 // We let objc ivars without warning, objc interfaces generally are not used
1517 // for padding tricks.
1518 if (isa<ObjCIvarDecl>(D))
Anders Carlsson79474332009-07-18 20:20:21 +00001519 return;
Mike Stump11289f42009-09-09 15:08:12 +00001520
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001521 unsigned CharBitNum = Context.Target.getCharWidth();
Mike Stump11289f42009-09-09 15:08:12 +00001522
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001523 // Warn if padding was introduced to the struct/class.
1524 if (!IsUnion && Offset > UnpaddedOffset) {
1525 unsigned PadSize = Offset - UnpaddedOffset;
1526 bool InBits = true;
1527 if (PadSize % CharBitNum == 0) {
1528 PadSize = PadSize / CharBitNum;
1529 InBits = false;
1530 }
1531 if (D->getIdentifier())
1532 Diag(D->getLocation(), diag::warn_padded_struct_field)
1533 << (D->getParent()->isStruct() ? 0 : 1) // struct|class
1534 << Context.getTypeDeclType(D->getParent())
1535 << PadSize
1536 << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1) // plural or not
1537 << D->getIdentifier();
1538 else
1539 Diag(D->getLocation(), diag::warn_padded_struct_anon_field)
1540 << (D->getParent()->isStruct() ? 0 : 1) // struct|class
1541 << Context.getTypeDeclType(D->getParent())
1542 << PadSize
1543 << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not
1544 }
1545
1546 // Warn if we packed it unnecessarily. If the alignment is 1 byte don't
1547 // bother since there won't be alignment issues.
1548 if (isPacked && UnpackedAlign > CharBitNum && Offset == UnpackedOffset)
1549 Diag(D->getLocation(), diag::warn_unnecessary_packed)
1550 << D->getIdentifier();
Anders Carlsson79474332009-07-18 20:20:21 +00001551}
Mike Stump11289f42009-09-09 15:08:12 +00001552
Anders Carlsson5ebf8b42009-12-07 04:35:11 +00001553const CXXMethodDecl *
Anders Carlssonc2226202010-05-26 05:58:59 +00001554RecordLayoutBuilder::ComputeKeyFunction(const CXXRecordDecl *RD) {
Daniel Dunbarccabe482010-04-19 20:44:53 +00001555 // If a class isn't polymorphic it doesn't have a key function.
Anders Carlsson5ebf8b42009-12-07 04:35:11 +00001556 if (!RD->isPolymorphic())
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001557 return 0;
Eli Friedmanf2c79b62009-12-08 03:56:49 +00001558
1559 // A class inside an anonymous namespace doesn't have a key function. (Or
1560 // at least, there's no point to assigning a key function to such a class;
1561 // this doesn't affect the ABI.)
1562 if (RD->isInAnonymousNamespace())
1563 return 0;
1564
Argyrios Kyrtzidis8c64bbe2010-10-13 02:39:41 +00001565 // Template instantiations don't have key functions,see Itanium C++ ABI 5.2.6.
1566 // Same behavior as GCC.
1567 TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
1568 if (TSK == TSK_ImplicitInstantiation ||
1569 TSK == TSK_ExplicitInstantiationDefinition)
1570 return 0;
1571
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001572 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
1573 E = RD->method_end(); I != E; ++I) {
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001574 const CXXMethodDecl *MD = *I;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001575
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001576 if (!MD->isVirtual())
1577 continue;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001578
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001579 if (MD->isPure())
1580 continue;
Eli Friedmanf2c79b62009-12-08 03:56:49 +00001581
Anders Carlssonf98849e2009-12-02 17:15:43 +00001582 // Ignore implicit member functions, they are always marked as inline, but
1583 // they don't have a body until they're defined.
1584 if (MD->isImplicit())
1585 continue;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001586
Douglas Gregora318efd2010-01-05 19:06:31 +00001587 if (MD->isInlineSpecified())
1588 continue;
Eli Friedman71a26d82009-12-06 20:50:05 +00001589
1590 if (MD->hasInlineBody())
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001591 continue;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001592
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001593 // We found it.
1594 return MD;
1595 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001596
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001597 return 0;
1598}
1599
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001600DiagnosticBuilder
1601RecordLayoutBuilder::Diag(SourceLocation Loc, unsigned DiagID) {
1602 return Context.getDiagnostics().Report(
1603 FullSourceLoc(Loc, Context.getSourceManager()), DiagID);
1604}
1605
Charles Davisc2c576a2010-08-19 00:55:19 +00001606// This class implements layout specific to the Microsoft ABI.
1607class MSRecordLayoutBuilder: public RecordLayoutBuilder {
John McCall1ed01822010-08-19 01:21:57 +00001608public:
Charles Davisc2c576a2010-08-19 00:55:19 +00001609 MSRecordLayoutBuilder(ASTContext& Ctx, EmptySubobjectMap *EmptySubobjects):
1610 RecordLayoutBuilder(Ctx, EmptySubobjects) {}
1611
1612 virtual bool IsNearlyEmpty(const CXXRecordDecl *RD) const;
1613 virtual uint64_t GetVirtualPointersSize(const CXXRecordDecl *RD) const;
1614};
1615
1616bool MSRecordLayoutBuilder::IsNearlyEmpty(const CXXRecordDecl *RD) const {
1617 // FIXME: Audit the corners
1618 if (!RD->isDynamicClass())
1619 return false;
1620 const ASTRecordLayout &BaseInfo = Context.getASTRecordLayout(RD);
1621 // In the Microsoft ABI, classes can have one or two vtable pointers.
1622 if (BaseInfo.getNonVirtualSize() == Context.Target.getPointerWidth(0) ||
1623 BaseInfo.getNonVirtualSize() == Context.Target.getPointerWidth(0) * 2)
1624 return true;
1625 return false;
1626}
1627
1628uint64_t
1629MSRecordLayoutBuilder::GetVirtualPointersSize(const CXXRecordDecl *RD) const {
1630 // We should reserve space for two pointers if the class has both
1631 // virtual functions and virtual bases.
1632 if (RD->isPolymorphic() && RD->getNumVBases() > 0)
1633 return 2 * Context.Target.getPointerWidth(0);
1634 return Context.Target.getPointerWidth(0);
1635}
1636
Anders Carlssondf291d82010-05-26 04:56:53 +00001637/// getASTRecordLayout - Get or compute information about the layout of the
1638/// specified record (struct/union/class), which indicates its size and field
1639/// position information.
1640const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
1641 D = D->getDefinition();
1642 assert(D && "Cannot get layout of forward declarations!");
1643
1644 // Look up this layout, if already laid out, return what we have.
1645 // Note that we can't save a reference to the entry because this function
1646 // is recursive.
1647 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
1648 if (Entry) return *Entry;
1649
Anders Carlssond2954862010-05-26 05:10:47 +00001650 const ASTRecordLayout *NewEntry;
1651
1652 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
Anders Carlssonc121b4e2010-05-27 00:07:01 +00001653 EmptySubobjectMap EmptySubobjects(*this, RD);
Anders Carlsson439edd12010-05-27 05:41:06 +00001654
Charles Davisc2c576a2010-08-19 00:55:19 +00001655 // When compiling for Microsoft, use the special MS builder.
Argyrios Kyrtzidis3c6cd172010-08-25 00:32:14 +00001656 llvm::OwningPtr<RecordLayoutBuilder> Builder;
Charles Davis6bcb07a2010-08-19 02:18:14 +00001657 switch (Target.getCXXABI()) {
1658 default:
Argyrios Kyrtzidis3c6cd172010-08-25 00:32:14 +00001659 Builder.reset(new RecordLayoutBuilder(*this, &EmptySubobjects));
Charles Davis6bcb07a2010-08-19 02:18:14 +00001660 break;
1661 case CXXABI_Microsoft:
Argyrios Kyrtzidis3c6cd172010-08-25 00:32:14 +00001662 Builder.reset(new MSRecordLayoutBuilder(*this, &EmptySubobjects));
Charles Davis6bcb07a2010-08-19 02:18:14 +00001663 }
Charles Davisc2c576a2010-08-19 00:55:19 +00001664 Builder->Layout(RD);
Anders Carlssond2954862010-05-26 05:10:47 +00001665
1666 // FIXME: This is not always correct. See the part about bitfields at
1667 // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info.
1668 // FIXME: IsPODForThePurposeOfLayout should be stored in the record layout.
1669 bool IsPODForThePurposeOfLayout = cast<CXXRecordDecl>(D)->isPOD();
1670
1671 // FIXME: This should be done in FinalizeLayout.
1672 uint64_t DataSize =
Charles Davisc2c576a2010-08-19 00:55:19 +00001673 IsPODForThePurposeOfLayout ? Builder->Size : Builder->DataSize;
Anders Carlssond2954862010-05-26 05:10:47 +00001674 uint64_t NonVirtualSize =
Charles Davisc2c576a2010-08-19 00:55:19 +00001675 IsPODForThePurposeOfLayout ? DataSize : Builder->NonVirtualSize;
Anders Carlssond2954862010-05-26 05:10:47 +00001676
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001677 NewEntry =
Charles Davisc2c576a2010-08-19 00:55:19 +00001678 new (*this) ASTRecordLayout(*this, Builder->Size, Builder->Alignment,
1679 DataSize, Builder->FieldOffsets.data(),
1680 Builder->FieldOffsets.size(),
Anders Carlssond2954862010-05-26 05:10:47 +00001681 NonVirtualSize,
Charles Davisc2c576a2010-08-19 00:55:19 +00001682 Builder->NonVirtualAlignment,
Anders Carlssonc121b4e2010-05-27 00:07:01 +00001683 EmptySubobjects.SizeOfLargestEmptySubobject,
Charles Davisc2c576a2010-08-19 00:55:19 +00001684 Builder->PrimaryBase,
1685 Builder->PrimaryBaseIsVirtual,
1686 Builder->Bases, Builder->VBases);
Anders Carlssond2954862010-05-26 05:10:47 +00001687 } else {
Anders Carlssonc121b4e2010-05-27 00:07:01 +00001688 RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0);
Anders Carlssond2954862010-05-26 05:10:47 +00001689 Builder.Layout(D);
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001690
Anders Carlssond2954862010-05-26 05:10:47 +00001691 NewEntry =
1692 new (*this) ASTRecordLayout(*this, Builder.Size, Builder.Alignment,
1693 Builder.Size,
1694 Builder.FieldOffsets.data(),
1695 Builder.FieldOffsets.size());
1696 }
1697
Anders Carlssondf291d82010-05-26 04:56:53 +00001698 ASTRecordLayouts[D] = NewEntry;
1699
1700 if (getLangOptions().DumpRecordLayouts) {
1701 llvm::errs() << "\n*** Dumping AST Record Layout\n";
1702 DumpRecordLayout(D, llvm::errs());
1703 }
1704
1705 return *NewEntry;
1706}
1707
1708const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) {
1709 RD = cast<CXXRecordDecl>(RD->getDefinition());
1710 assert(RD && "Cannot get key function for forward declarations!");
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001711
Anders Carlssondf291d82010-05-26 04:56:53 +00001712 const CXXMethodDecl *&Entry = KeyFunctions[RD];
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001713 if (!Entry)
Anders Carlssonc2226202010-05-26 05:58:59 +00001714 Entry = RecordLayoutBuilder::ComputeKeyFunction(RD);
Anders Carlssondf291d82010-05-26 04:56:53 +00001715 else
Anders Carlssonc2226202010-05-26 05:58:59 +00001716 assert(Entry == RecordLayoutBuilder::ComputeKeyFunction(RD) &&
Anders Carlssondf291d82010-05-26 04:56:53 +00001717 "Key function changed!");
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001718
Anders Carlssondf291d82010-05-26 04:56:53 +00001719 return Entry;
1720}
1721
1722/// getInterfaceLayoutImpl - Get or compute information about the
1723/// layout of the given interface.
1724///
1725/// \param Impl - If given, also include the layout of the interface's
1726/// implementation. This may differ by including synthesized ivars.
1727const ASTRecordLayout &
1728ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
1729 const ObjCImplementationDecl *Impl) {
1730 assert(!D->isForwardDecl() && "Invalid interface decl!");
1731
1732 // Look up this layout, if already laid out, return what we have.
1733 ObjCContainerDecl *Key =
1734 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
1735 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
1736 return *Entry;
1737
1738 // Add in synthesized ivar count if laying out an implementation.
1739 if (Impl) {
1740 unsigned SynthCount = CountNonClassIvars(D);
1741 // If there aren't any sythesized ivars then reuse the interface
1742 // entry. Note we can't cache this because we simply free all
1743 // entries later; however we shouldn't look up implementations
1744 // frequently.
1745 if (SynthCount == 0)
1746 return getObjCLayout(D, 0);
1747 }
1748
Anders Carlssonc121b4e2010-05-27 00:07:01 +00001749 RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0);
Anders Carlsson6ed3a9a2010-05-26 05:04:25 +00001750 Builder.Layout(D);
1751
Anders Carlssondf291d82010-05-26 04:56:53 +00001752 const ASTRecordLayout *NewEntry =
Anders Carlsson6ed3a9a2010-05-26 05:04:25 +00001753 new (*this) ASTRecordLayout(*this, Builder.Size, Builder.Alignment,
1754 Builder.DataSize,
1755 Builder.FieldOffsets.data(),
1756 Builder.FieldOffsets.size());
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001757
Anders Carlssondf291d82010-05-26 04:56:53 +00001758 ObjCLayouts[Key] = NewEntry;
1759
1760 return *NewEntry;
1761}
1762
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001763static void PrintOffset(llvm::raw_ostream &OS,
1764 uint64_t Offset, unsigned IndentLevel) {
1765 OS << llvm::format("%4d | ", Offset);
1766 OS.indent(IndentLevel * 2);
1767}
1768
1769static void DumpCXXRecordLayout(llvm::raw_ostream &OS,
1770 const CXXRecordDecl *RD, ASTContext &C,
1771 uint64_t Offset,
1772 unsigned IndentLevel,
1773 const char* Description,
1774 bool IncludeVirtualBases) {
1775 const ASTRecordLayout &Info = C.getASTRecordLayout(RD);
1776
1777 PrintOffset(OS, Offset, IndentLevel);
Dan Gohman145f3f12010-04-19 16:39:44 +00001778 OS << C.getTypeDeclType(const_cast<CXXRecordDecl *>(RD)).getAsString();
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001779 if (Description)
1780 OS << ' ' << Description;
1781 if (RD->isEmpty())
1782 OS << " (empty)";
1783 OS << '\n';
1784
1785 IndentLevel++;
1786
1787 const CXXRecordDecl *PrimaryBase = Info.getPrimaryBase();
1788
1789 // Vtable pointer.
1790 if (RD->isDynamicClass() && !PrimaryBase) {
1791 PrintOffset(OS, Offset, IndentLevel);
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001792 OS << '(' << RD << " vtable pointer)\n";
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001793 }
1794 // Dump (non-virtual) bases
1795 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1796 E = RD->bases_end(); I != E; ++I) {
1797 assert(!I->getType()->isDependentType() &&
1798 "Cannot layout class with dependent bases.");
1799 if (I->isVirtual())
1800 continue;
1801
1802 const CXXRecordDecl *Base =
1803 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1804
1805 uint64_t BaseOffset = Offset + Info.getBaseClassOffset(Base) / 8;
1806
1807 DumpCXXRecordLayout(OS, Base, C, BaseOffset, IndentLevel,
1808 Base == PrimaryBase ? "(primary base)" : "(base)",
1809 /*IncludeVirtualBases=*/false);
1810 }
1811
1812 // Dump fields.
1813 uint64_t FieldNo = 0;
1814 for (CXXRecordDecl::field_iterator I = RD->field_begin(),
1815 E = RD->field_end(); I != E; ++I, ++FieldNo) {
1816 const FieldDecl *Field = *I;
1817 uint64_t FieldOffset = Offset + Info.getFieldOffset(FieldNo) / 8;
1818
1819 if (const RecordType *RT = Field->getType()->getAs<RecordType>()) {
1820 if (const CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
1821 DumpCXXRecordLayout(OS, D, C, FieldOffset, IndentLevel,
Daniel Dunbar56df9772010-08-17 22:39:59 +00001822 Field->getName().data(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001823 /*IncludeVirtualBases=*/true);
1824 continue;
1825 }
1826 }
1827
1828 PrintOffset(OS, FieldOffset, IndentLevel);
Benjamin Kramerb11416d2010-04-17 09:33:03 +00001829 OS << Field->getType().getAsString() << ' ' << Field << '\n';
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001830 }
1831
1832 if (!IncludeVirtualBases)
1833 return;
1834
1835 // Dump virtual bases.
1836 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
1837 E = RD->vbases_end(); I != E; ++I) {
1838 assert(I->isVirtual() && "Found non-virtual class!");
1839 const CXXRecordDecl *VBase =
1840 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1841
1842 uint64_t VBaseOffset = Offset + Info.getVBaseClassOffset(VBase) / 8;
1843 DumpCXXRecordLayout(OS, VBase, C, VBaseOffset, IndentLevel,
1844 VBase == PrimaryBase ?
1845 "(primary virtual base)" : "(virtual base)",
1846 /*IncludeVirtualBases=*/false);
1847 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001848
1849 OS << " sizeof=" << Info.getSize() / 8;
1850 OS << ", dsize=" << Info.getDataSize() / 8;
1851 OS << ", align=" << Info.getAlignment() / 8 << '\n';
1852 OS << " nvsize=" << Info.getNonVirtualSize() / 8;
1853 OS << ", nvalign=" << Info.getNonVirtualAlign() / 8 << '\n';
1854 OS << '\n';
1855}
Daniel Dunbarccabe482010-04-19 20:44:53 +00001856
1857void ASTContext::DumpRecordLayout(const RecordDecl *RD,
1858 llvm::raw_ostream &OS) {
1859 const ASTRecordLayout &Info = getASTRecordLayout(RD);
1860
1861 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
1862 return DumpCXXRecordLayout(OS, CXXRD, *this, 0, 0, 0,
1863 /*IncludeVirtualBases=*/true);
1864
1865 OS << "Type: " << getTypeDeclType(RD).getAsString() << "\n";
1866 OS << "Record: ";
1867 RD->dump();
1868 OS << "\nLayout: ";
1869 OS << "<ASTRecordLayout\n";
1870 OS << " Size:" << Info.getSize() << "\n";
1871 OS << " DataSize:" << Info.getDataSize() << "\n";
1872 OS << " Alignment:" << Info.getAlignment() << "\n";
1873 OS << " FieldOffsets: [";
1874 for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i) {
1875 if (i) OS << ", ";
1876 OS << Info.getFieldOffset(i);
1877 }
1878 OS << "]>\n";
1879}