blob: 97355dc04e045d2c613b85b0c0c8af890f61a5b4 [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"
Anders Carlsson5adde292010-11-24 22:55:48 +000011#include "clang/AST/CXXInheritance.h"
Anders Carlsson79474332009-07-18 20:20:21 +000012#include "clang/AST/Decl.h"
Anders Carlsson6d9f6f32009-07-19 00:18:47 +000013#include "clang/AST/DeclCXX.h"
Anders Carlsson4f516282009-07-18 20:50:59 +000014#include "clang/AST/DeclObjC.h"
Anders Carlsson79474332009-07-18 20:20:21 +000015#include "clang/AST/Expr.h"
Anders Carlsson35a36eb2010-05-26 05:41:04 +000016#include "clang/AST/RecordLayout.h"
Anders Carlsson79474332009-07-18 20:20:21 +000017#include "clang/Basic/TargetInfo.h"
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +000018#include "clang/Sema/SemaDiagnostic.h"
Daniel Dunbaraa423af2010-04-08 02:59:49 +000019#include "llvm/Support/Format.h"
20#include "llvm/ADT/SmallSet.h"
21#include "llvm/Support/MathExtras.h"
Ted Kremenekf75d0892011-03-19 01:00:36 +000022#include "llvm/Support/CrashRecoveryContext.h"
Anders Carlsson79474332009-07-18 20:20:21 +000023
24using namespace clang;
25
Benjamin Kramerc7656cd2010-05-26 09:58:31 +000026namespace {
Anders Carlssonf58de112010-05-26 15:32:58 +000027
Anders Carlssona7f3cdb2010-05-28 21:24:37 +000028/// BaseSubobjectInfo - Represents a single base subobject in a complete class.
29/// For a class hierarchy like
30///
31/// class A { };
32/// class B : A { };
33/// class C : A, B { };
34///
35/// The BaseSubobjectInfo graph for C will have three BaseSubobjectInfo
36/// instances, one for B and two for A.
37///
38/// If a base is virtual, it will only have one BaseSubobjectInfo allocated.
39struct BaseSubobjectInfo {
40 /// Class - The class for this base info.
Anders Carlsson056818f2010-05-28 21:13:31 +000041 const CXXRecordDecl *Class;
Anders Carlssona7f3cdb2010-05-28 21:24:37 +000042
43 /// IsVirtual - Whether the BaseInfo represents a virtual base or not.
Anders Carlsson056818f2010-05-28 21:13:31 +000044 bool IsVirtual;
45
Anders Carlssona7f3cdb2010-05-28 21:24:37 +000046 /// Bases - Information about the base subobjects.
Chris Lattner0e62c1c2011-07-23 10:55:15 +000047 SmallVector<BaseSubobjectInfo*, 4> Bases;
Anders Carlssona7f3cdb2010-05-28 21:24:37 +000048
Anders Carlssone3c24c72010-05-29 17:35:14 +000049 /// PrimaryVirtualBaseInfo - Holds the base info for the primary virtual base
50 /// of this base info (if one exists).
51 BaseSubobjectInfo *PrimaryVirtualBaseInfo;
Anders Carlssona7f3cdb2010-05-28 21:24:37 +000052
53 // FIXME: Document.
54 const BaseSubobjectInfo *Derived;
Anders Carlsson056818f2010-05-28 21:13:31 +000055};
56
Anders Carlssonf58de112010-05-26 15:32:58 +000057/// EmptySubobjectMap - Keeps track of which empty subobjects exist at different
58/// offsets while laying out a C++ class.
59class EmptySubobjectMap {
Jay Foad39c79802011-01-12 09:06:06 +000060 const ASTContext &Context;
Anders Carlsson233e2722010-10-31 21:54:55 +000061 uint64_t CharWidth;
62
Anders Carlssonf58de112010-05-26 15:32:58 +000063 /// Class - The class whose empty entries we're keeping track of.
64 const CXXRecordDecl *Class;
Daniel Dunbar592a85c2010-05-27 02:25:46 +000065
Anders Carlsson439edd12010-05-27 05:41:06 +000066 /// EmptyClassOffsets - A map from offsets to empty record decls.
Chris Lattner0e62c1c2011-07-23 10:55:15 +000067 typedef SmallVector<const CXXRecordDecl *, 1> ClassVectorTy;
Anders Carlssonf8f756d2010-10-31 21:22:43 +000068 typedef llvm::DenseMap<CharUnits, ClassVectorTy> EmptyClassOffsetsMapTy;
Anders Carlsson439edd12010-05-27 05:41:06 +000069 EmptyClassOffsetsMapTy EmptyClassOffsets;
70
Anders Carlssoncc5de092010-06-08 15:56:03 +000071 /// MaxEmptyClassOffset - The highest offset known to contain an empty
72 /// base subobject.
Anders Carlsson725190f2010-10-31 21:39:24 +000073 CharUnits MaxEmptyClassOffset;
Anders Carlssoncc5de092010-06-08 15:56:03 +000074
Daniel Dunbar592a85c2010-05-27 02:25:46 +000075 /// ComputeEmptySubobjectSizes - Compute the size of the largest base or
Anders Carlssonc5ca1f72010-05-26 15:54:25 +000076 /// member subobject that is empty.
77 void ComputeEmptySubobjectSizes();
Anders Carlsson439edd12010-05-27 05:41:06 +000078
Anders Carlsson725190f2010-10-31 21:39:24 +000079 void AddSubobjectAtOffset(const CXXRecordDecl *RD, CharUnits Offset);
Anders Carlssondb319762010-05-27 18:20:57 +000080
Anders Carlssona7f3cdb2010-05-28 21:24:37 +000081 void UpdateEmptyBaseSubobjects(const BaseSubobjectInfo *Info,
Anders Carlsson28466ab2010-10-31 22:13:23 +000082 CharUnits Offset, bool PlacingEmptyBase);
Anders Carlsson439edd12010-05-27 05:41:06 +000083
Anders Carlssondb319762010-05-27 18:20:57 +000084 void UpdateEmptyFieldSubobjects(const CXXRecordDecl *RD,
85 const CXXRecordDecl *Class,
Anders Carlsson28466ab2010-10-31 22:13:23 +000086 CharUnits Offset);
87 void UpdateEmptyFieldSubobjects(const FieldDecl *FD, CharUnits Offset);
Anders Carlssondb319762010-05-27 18:20:57 +000088
Anders Carlssoncc5de092010-06-08 15:56:03 +000089 /// AnyEmptySubobjectsBeyondOffset - Returns whether there are any empty
90 /// subobjects beyond the given offset.
Anders Carlsson725190f2010-10-31 21:39:24 +000091 bool AnyEmptySubobjectsBeyondOffset(CharUnits Offset) const {
Anders Carlssoncc5de092010-06-08 15:56:03 +000092 return Offset <= MaxEmptyClassOffset;
93 }
94
Anders Carlsson233e2722010-10-31 21:54:55 +000095 CharUnits
96 getFieldOffset(const ASTRecordLayout &Layout, unsigned FieldNo) const {
97 uint64_t FieldOffset = Layout.getFieldOffset(FieldNo);
98 assert(FieldOffset % CharWidth == 0 &&
99 "Field offset not at char boundary!");
100
Ken Dyck7c4026b2011-01-24 01:28:50 +0000101 return Context.toCharUnitsFromBits(FieldOffset);
Anders Carlssonf8f756d2010-10-31 21:22:43 +0000102 }
Anders Carlssonf8f756d2010-10-31 21:22:43 +0000103
Charles Davisc2c576a2010-08-19 00:55:19 +0000104protected:
Anders Carlsson725190f2010-10-31 21:39:24 +0000105 bool CanPlaceSubobjectAtOffset(const CXXRecordDecl *RD,
106 CharUnits Offset) const;
Charles Davisc2c576a2010-08-19 00:55:19 +0000107
108 bool CanPlaceBaseSubobjectAtOffset(const BaseSubobjectInfo *Info,
Anders Carlsson28466ab2010-10-31 22:13:23 +0000109 CharUnits Offset);
Charles Davisc2c576a2010-08-19 00:55:19 +0000110
111 bool CanPlaceFieldSubobjectAtOffset(const CXXRecordDecl *RD,
112 const CXXRecordDecl *Class,
Anders Carlsson28466ab2010-10-31 22:13:23 +0000113 CharUnits Offset) const;
Charles Davisc2c576a2010-08-19 00:55:19 +0000114 bool CanPlaceFieldSubobjectAtOffset(const FieldDecl *FD,
Anders Carlsson233e2722010-10-31 21:54:55 +0000115 CharUnits Offset) const;
Charles Davisc2c576a2010-08-19 00:55:19 +0000116
Anders Carlssonf58de112010-05-26 15:32:58 +0000117public:
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000118 /// This holds the size of the largest empty subobject (either a base
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000119 /// or a member). Will be zero if the record being built doesn't contain
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000120 /// any empty classes.
Anders Carlsson28466ab2010-10-31 22:13:23 +0000121 CharUnits SizeOfLargestEmptySubobject;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000122
Jay Foad39c79802011-01-12 09:06:06 +0000123 EmptySubobjectMap(const ASTContext &Context, const CXXRecordDecl *Class)
Anders Carlsson28466ab2010-10-31 22:13:23 +0000124 : Context(Context), CharWidth(Context.getCharWidth()), Class(Class) {
Anders Carlssonc121b4e2010-05-27 00:07:01 +0000125 ComputeEmptySubobjectSizes();
126 }
127
128 /// CanPlaceBaseAtOffset - Return whether the given base class can be placed
129 /// at the given offset.
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000130 /// Returns false if placing the record will result in two components
Anders Carlssonc121b4e2010-05-27 00:07:01 +0000131 /// (direct or indirect) of the same type having the same offset.
Anders Carlssoncc5de092010-06-08 15:56:03 +0000132 bool CanPlaceBaseAtOffset(const BaseSubobjectInfo *Info,
Anders Carlsson28466ab2010-10-31 22:13:23 +0000133 CharUnits Offset);
Anders Carlssondb319762010-05-27 18:20:57 +0000134
135 /// CanPlaceFieldAtOffset - Return whether a field can be placed at the given
136 /// offset.
Anders Carlsson28466ab2010-10-31 22:13:23 +0000137 bool CanPlaceFieldAtOffset(const FieldDecl *FD, CharUnits Offset);
Anders Carlssonf58de112010-05-26 15:32:58 +0000138};
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000139
140void EmptySubobjectMap::ComputeEmptySubobjectSizes() {
141 // Check the bases.
142 for (CXXRecordDecl::base_class_const_iterator I = Class->bases_begin(),
143 E = Class->bases_end(); I != E; ++I) {
144 const CXXRecordDecl *BaseDecl =
145 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
146
Anders Carlsson28466ab2010-10-31 22:13:23 +0000147 CharUnits EmptySize;
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000148 const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl);
149 if (BaseDecl->isEmpty()) {
150 // If the class decl is empty, get its size.
Ken Dyckc8ae5502011-02-09 01:59:34 +0000151 EmptySize = Layout.getSize();
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000152 } else {
153 // Otherwise, we get the largest empty subobject for the decl.
154 EmptySize = Layout.getSizeOfLargestEmptySubobject();
155 }
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000156
Anders Carlsson28466ab2010-10-31 22:13:23 +0000157 if (EmptySize > SizeOfLargestEmptySubobject)
158 SizeOfLargestEmptySubobject = EmptySize;
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000159 }
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000160
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000161 // Check the fields.
162 for (CXXRecordDecl::field_iterator I = Class->field_begin(),
163 E = Class->field_end(); I != E; ++I) {
164 const FieldDecl *FD = *I;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000165
166 const RecordType *RT =
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000167 Context.getBaseElementType(FD->getType())->getAs<RecordType>();
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000168
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000169 // We only care about record types.
170 if (!RT)
171 continue;
172
Anders Carlsson28466ab2010-10-31 22:13:23 +0000173 CharUnits EmptySize;
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000174 const CXXRecordDecl *MemberDecl = cast<CXXRecordDecl>(RT->getDecl());
175 const ASTRecordLayout &Layout = Context.getASTRecordLayout(MemberDecl);
176 if (MemberDecl->isEmpty()) {
177 // If the class decl is empty, get its size.
Ken Dyckc8ae5502011-02-09 01:59:34 +0000178 EmptySize = Layout.getSize();
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000179 } else {
180 // Otherwise, we get the largest empty subobject for the decl.
181 EmptySize = Layout.getSizeOfLargestEmptySubobject();
182 }
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000183
Anders Carlsson28466ab2010-10-31 22:13:23 +0000184 if (EmptySize > SizeOfLargestEmptySubobject)
185 SizeOfLargestEmptySubobject = EmptySize;
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000186 }
187}
188
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000189bool
Anders Carlssondb319762010-05-27 18:20:57 +0000190EmptySubobjectMap::CanPlaceSubobjectAtOffset(const CXXRecordDecl *RD,
Anders Carlsson725190f2010-10-31 21:39:24 +0000191 CharUnits Offset) const {
Anders Carlssondb319762010-05-27 18:20:57 +0000192 // We only need to check empty bases.
193 if (!RD->isEmpty())
194 return true;
195
Anders Carlsson725190f2010-10-31 21:39:24 +0000196 EmptyClassOffsetsMapTy::const_iterator I = EmptyClassOffsets.find(Offset);
Anders Carlssondb319762010-05-27 18:20:57 +0000197 if (I == EmptyClassOffsets.end())
198 return true;
199
200 const ClassVectorTy& Classes = I->second;
201 if (std::find(Classes.begin(), Classes.end(), RD) == Classes.end())
202 return true;
203
204 // There is already an empty class of the same type at this offset.
205 return false;
206}
207
208void EmptySubobjectMap::AddSubobjectAtOffset(const CXXRecordDecl *RD,
Anders Carlsson725190f2010-10-31 21:39:24 +0000209 CharUnits Offset) {
Anders Carlssondb319762010-05-27 18:20:57 +0000210 // We only care about empty bases.
211 if (!RD->isEmpty())
212 return;
213
Rafael Espindola7bcde192010-12-29 23:02:58 +0000214 // If we have empty structures inside an union, we can assign both
215 // the same offset. Just avoid pushing them twice in the list.
Anders Carlsson725190f2010-10-31 21:39:24 +0000216 ClassVectorTy& Classes = EmptyClassOffsets[Offset];
Rafael Espindola7bcde192010-12-29 23:02:58 +0000217 if (std::find(Classes.begin(), Classes.end(), RD) != Classes.end())
218 return;
219
Anders Carlssondb319762010-05-27 18:20:57 +0000220 Classes.push_back(RD);
Anders Carlssoncc5de092010-06-08 15:56:03 +0000221
222 // Update the empty class offset.
Anders Carlsson725190f2010-10-31 21:39:24 +0000223 if (Offset > MaxEmptyClassOffset)
224 MaxEmptyClassOffset = Offset;
Anders Carlssondb319762010-05-27 18:20:57 +0000225}
226
227bool
Anders Carlsson28466ab2010-10-31 22:13:23 +0000228EmptySubobjectMap::CanPlaceBaseSubobjectAtOffset(const BaseSubobjectInfo *Info,
229 CharUnits Offset) {
Anders Carlsson45c1d282010-06-08 16:20:35 +0000230 // We don't have to keep looking past the maximum offset that's known to
231 // contain an empty class.
Anders Carlsson28466ab2010-10-31 22:13:23 +0000232 if (!AnyEmptySubobjectsBeyondOffset(Offset))
Anders Carlsson45c1d282010-06-08 16:20:35 +0000233 return true;
234
Anders Carlsson28466ab2010-10-31 22:13:23 +0000235 if (!CanPlaceSubobjectAtOffset(Info->Class, Offset))
Anders Carlssondb319762010-05-27 18:20:57 +0000236 return false;
237
Anders Carlsson439edd12010-05-27 05:41:06 +0000238 // Traverse all non-virtual bases.
Anders Carlssona7774a62010-05-29 21:10:24 +0000239 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class);
Anders Carlsson439edd12010-05-27 05:41:06 +0000240 for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) {
Anders Carlssona7f3cdb2010-05-28 21:24:37 +0000241 BaseSubobjectInfo* Base = Info->Bases[I];
Anders Carlsson439edd12010-05-27 05:41:06 +0000242 if (Base->IsVirtual)
243 continue;
244
Anders Carlsson0a14ee92010-11-01 00:21:58 +0000245 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class);
Anders Carlsson439edd12010-05-27 05:41:06 +0000246
247 if (!CanPlaceBaseSubobjectAtOffset(Base, BaseOffset))
248 return false;
249 }
250
Anders Carlssone3c24c72010-05-29 17:35:14 +0000251 if (Info->PrimaryVirtualBaseInfo) {
252 BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo;
Anders Carlsson439edd12010-05-27 05:41:06 +0000253
254 if (Info == PrimaryVirtualBaseInfo->Derived) {
255 if (!CanPlaceBaseSubobjectAtOffset(PrimaryVirtualBaseInfo, Offset))
256 return false;
257 }
258 }
259
Anders Carlssondb319762010-05-27 18:20:57 +0000260 // Traverse all member variables.
261 unsigned FieldNo = 0;
262 for (CXXRecordDecl::field_iterator I = Info->Class->field_begin(),
263 E = Info->Class->field_end(); I != E; ++I, ++FieldNo) {
264 const FieldDecl *FD = *I;
Anders Carlsson233e2722010-10-31 21:54:55 +0000265 if (FD->isBitField())
266 continue;
267
Anders Carlsson28466ab2010-10-31 22:13:23 +0000268 CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo);
Anders Carlssondb319762010-05-27 18:20:57 +0000269 if (!CanPlaceFieldSubobjectAtOffset(FD, FieldOffset))
270 return false;
271 }
272
Anders Carlsson439edd12010-05-27 05:41:06 +0000273 return true;
274}
275
Anders Carlssona7f3cdb2010-05-28 21:24:37 +0000276void EmptySubobjectMap::UpdateEmptyBaseSubobjects(const BaseSubobjectInfo *Info,
Anders Carlsson28466ab2010-10-31 22:13:23 +0000277 CharUnits Offset,
Anders Carlssoncc59cc52010-06-13 18:00:18 +0000278 bool PlacingEmptyBase) {
279 if (!PlacingEmptyBase && Offset >= SizeOfLargestEmptySubobject) {
280 // We know that the only empty subobjects that can conflict with empty
281 // subobject of non-empty bases, are empty bases that can be placed at
282 // offset zero. Because of this, we only need to keep track of empty base
283 // subobjects with offsets less than the size of the largest empty
284 // subobject for our class.
285 return;
286 }
287
Anders Carlsson28466ab2010-10-31 22:13:23 +0000288 AddSubobjectAtOffset(Info->Class, Offset);
Anders Carlssona7774a62010-05-29 21:10:24 +0000289
Anders Carlsson439edd12010-05-27 05:41:06 +0000290 // Traverse all non-virtual bases.
Anders Carlssona7774a62010-05-29 21:10:24 +0000291 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class);
Anders Carlsson439edd12010-05-27 05:41:06 +0000292 for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) {
Anders Carlssona7f3cdb2010-05-28 21:24:37 +0000293 BaseSubobjectInfo* Base = Info->Bases[I];
Anders Carlsson439edd12010-05-27 05:41:06 +0000294 if (Base->IsVirtual)
295 continue;
Anders Carlssona7774a62010-05-29 21:10:24 +0000296
Anders Carlsson0a14ee92010-11-01 00:21:58 +0000297 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class);
Anders Carlssoncc59cc52010-06-13 18:00:18 +0000298 UpdateEmptyBaseSubobjects(Base, BaseOffset, PlacingEmptyBase);
Anders Carlsson439edd12010-05-27 05:41:06 +0000299 }
300
Anders Carlssone3c24c72010-05-29 17:35:14 +0000301 if (Info->PrimaryVirtualBaseInfo) {
302 BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo;
Anders Carlsson439edd12010-05-27 05:41:06 +0000303
304 if (Info == PrimaryVirtualBaseInfo->Derived)
Anders Carlssoncc59cc52010-06-13 18:00:18 +0000305 UpdateEmptyBaseSubobjects(PrimaryVirtualBaseInfo, Offset,
306 PlacingEmptyBase);
Anders Carlsson439edd12010-05-27 05:41:06 +0000307 }
Anders Carlssondb319762010-05-27 18:20:57 +0000308
Anders Carlssondb319762010-05-27 18:20:57 +0000309 // Traverse all member variables.
310 unsigned FieldNo = 0;
311 for (CXXRecordDecl::field_iterator I = Info->Class->field_begin(),
312 E = Info->Class->field_end(); I != E; ++I, ++FieldNo) {
313 const FieldDecl *FD = *I;
Anders Carlsson233e2722010-10-31 21:54:55 +0000314 if (FD->isBitField())
315 continue;
Anders Carlssona7774a62010-05-29 21:10:24 +0000316
Anders Carlsson28466ab2010-10-31 22:13:23 +0000317 CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo);
318 UpdateEmptyFieldSubobjects(FD, FieldOffset);
Anders Carlssondb319762010-05-27 18:20:57 +0000319 }
Anders Carlsson439edd12010-05-27 05:41:06 +0000320}
321
Anders Carlssona60b86a2010-05-29 20:49:49 +0000322bool EmptySubobjectMap::CanPlaceBaseAtOffset(const BaseSubobjectInfo *Info,
Anders Carlsson28466ab2010-10-31 22:13:23 +0000323 CharUnits Offset) {
Anders Carlssonc121b4e2010-05-27 00:07:01 +0000324 // If we know this class doesn't have any empty subobjects we don't need to
325 // bother checking.
Anders Carlsson28466ab2010-10-31 22:13:23 +0000326 if (SizeOfLargestEmptySubobject.isZero())
Anders Carlssonc121b4e2010-05-27 00:07:01 +0000327 return true;
328
Anders Carlsson439edd12010-05-27 05:41:06 +0000329 if (!CanPlaceBaseSubobjectAtOffset(Info, Offset))
330 return false;
Anders Carlssondb319762010-05-27 18:20:57 +0000331
332 // We are able to place the base at this offset. Make sure to update the
333 // empty base subobject map.
Anders Carlssoncc59cc52010-06-13 18:00:18 +0000334 UpdateEmptyBaseSubobjects(Info, Offset, Info->Class->isEmpty());
Anders Carlssonc121b4e2010-05-27 00:07:01 +0000335 return true;
336}
337
Anders Carlssondb319762010-05-27 18:20:57 +0000338bool
339EmptySubobjectMap::CanPlaceFieldSubobjectAtOffset(const CXXRecordDecl *RD,
340 const CXXRecordDecl *Class,
Anders Carlsson28466ab2010-10-31 22:13:23 +0000341 CharUnits Offset) const {
Anders Carlsson45c1d282010-06-08 16:20:35 +0000342 // We don't have to keep looking past the maximum offset that's known to
343 // contain an empty class.
Anders Carlsson28466ab2010-10-31 22:13:23 +0000344 if (!AnyEmptySubobjectsBeyondOffset(Offset))
Anders Carlsson45c1d282010-06-08 16:20:35 +0000345 return true;
346
Anders Carlsson28466ab2010-10-31 22:13:23 +0000347 if (!CanPlaceSubobjectAtOffset(RD, Offset))
Anders Carlssondb319762010-05-27 18:20:57 +0000348 return false;
349
350 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
351
352 // Traverse all non-virtual bases.
353 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
354 E = RD->bases_end(); I != E; ++I) {
355 if (I->isVirtual())
356 continue;
357
358 const CXXRecordDecl *BaseDecl =
359 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
360
Anders Carlsson0a14ee92010-11-01 00:21:58 +0000361 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl);
Anders Carlssondb319762010-05-27 18:20:57 +0000362 if (!CanPlaceFieldSubobjectAtOffset(BaseDecl, Class, BaseOffset))
363 return false;
364 }
365
Anders Carlsson44687202010-06-08 19:09:24 +0000366 if (RD == Class) {
367 // This is the most derived class, traverse virtual bases as well.
368 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
369 E = RD->vbases_end(); I != E; ++I) {
370 const CXXRecordDecl *VBaseDecl =
371 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
372
Anders Carlsson3f018712010-10-31 23:45:59 +0000373 CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBaseDecl);
Anders Carlsson44687202010-06-08 19:09:24 +0000374 if (!CanPlaceFieldSubobjectAtOffset(VBaseDecl, Class, VBaseOffset))
375 return false;
376 }
377 }
378
Anders Carlssondb319762010-05-27 18:20:57 +0000379 // Traverse all member variables.
380 unsigned FieldNo = 0;
381 for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
382 I != E; ++I, ++FieldNo) {
383 const FieldDecl *FD = *I;
Anders Carlsson233e2722010-10-31 21:54:55 +0000384 if (FD->isBitField())
385 continue;
386
Anders Carlsson28466ab2010-10-31 22:13:23 +0000387 CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo);
Anders Carlssondb319762010-05-27 18:20:57 +0000388
389 if (!CanPlaceFieldSubobjectAtOffset(FD, FieldOffset))
390 return false;
391 }
392
393 return true;
394}
395
Anders Carlsson233e2722010-10-31 21:54:55 +0000396bool
397EmptySubobjectMap::CanPlaceFieldSubobjectAtOffset(const FieldDecl *FD,
398 CharUnits Offset) const {
Anders Carlsson45c1d282010-06-08 16:20:35 +0000399 // We don't have to keep looking past the maximum offset that's known to
400 // contain an empty class.
Anders Carlsson233e2722010-10-31 21:54:55 +0000401 if (!AnyEmptySubobjectsBeyondOffset(Offset))
Anders Carlsson45c1d282010-06-08 16:20:35 +0000402 return true;
403
Anders Carlssondb319762010-05-27 18:20:57 +0000404 QualType T = FD->getType();
405 if (const RecordType *RT = T->getAs<RecordType>()) {
406 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Anders Carlsson28466ab2010-10-31 22:13:23 +0000407 return CanPlaceFieldSubobjectAtOffset(RD, RD, Offset);
Anders Carlssondb319762010-05-27 18:20:57 +0000408 }
409
410 // If we have an array type we need to look at every element.
411 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) {
412 QualType ElemTy = Context.getBaseElementType(AT);
413 const RecordType *RT = ElemTy->getAs<RecordType>();
414 if (!RT)
415 return true;
416
417 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
418 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
419
420 uint64_t NumElements = Context.getConstantArrayElementCount(AT);
Anders Carlsson233e2722010-10-31 21:54:55 +0000421 CharUnits ElementOffset = Offset;
Anders Carlssondb319762010-05-27 18:20:57 +0000422 for (uint64_t I = 0; I != NumElements; ++I) {
Anders Carlsson45c1d282010-06-08 16:20:35 +0000423 // We don't have to keep looking past the maximum offset that's known to
424 // contain an empty class.
Anders Carlsson233e2722010-10-31 21:54:55 +0000425 if (!AnyEmptySubobjectsBeyondOffset(ElementOffset))
Anders Carlsson45c1d282010-06-08 16:20:35 +0000426 return true;
427
Anders Carlsson28466ab2010-10-31 22:13:23 +0000428 if (!CanPlaceFieldSubobjectAtOffset(RD, RD, ElementOffset))
Anders Carlssondb319762010-05-27 18:20:57 +0000429 return false;
430
Ken Dyckc8ae5502011-02-09 01:59:34 +0000431 ElementOffset += Layout.getSize();
Anders Carlssondb319762010-05-27 18:20:57 +0000432 }
433 }
434
435 return true;
436}
437
438bool
Anders Carlsson28466ab2010-10-31 22:13:23 +0000439EmptySubobjectMap::CanPlaceFieldAtOffset(const FieldDecl *FD,
440 CharUnits Offset) {
441 if (!CanPlaceFieldSubobjectAtOffset(FD, Offset))
Anders Carlssondb319762010-05-27 18:20:57 +0000442 return false;
443
444 // We are able to place the member variable at this offset.
445 // Make sure to update the empty base subobject map.
446 UpdateEmptyFieldSubobjects(FD, Offset);
447 return true;
448}
449
450void EmptySubobjectMap::UpdateEmptyFieldSubobjects(const CXXRecordDecl *RD,
451 const CXXRecordDecl *Class,
Anders Carlsson28466ab2010-10-31 22:13:23 +0000452 CharUnits Offset) {
Anders Carlssonae111dc2010-06-13 17:49:16 +0000453 // We know that the only empty subobjects that can conflict with empty
Anders Carlssoncc59cc52010-06-13 18:00:18 +0000454 // field subobjects are subobjects of empty bases that can be placed at offset
Anders Carlssonae111dc2010-06-13 17:49:16 +0000455 // zero. Because of this, we only need to keep track of empty field
456 // subobjects with offsets less than the size of the largest empty
457 // subobject for our class.
458 if (Offset >= SizeOfLargestEmptySubobject)
459 return;
460
Anders Carlsson28466ab2010-10-31 22:13:23 +0000461 AddSubobjectAtOffset(RD, Offset);
Anders Carlssondb319762010-05-27 18:20:57 +0000462
463 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
464
465 // Traverse all non-virtual bases.
466 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
467 E = RD->bases_end(); I != E; ++I) {
468 if (I->isVirtual())
469 continue;
470
471 const CXXRecordDecl *BaseDecl =
472 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
473
Anders Carlsson0a14ee92010-11-01 00:21:58 +0000474 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl);
Anders Carlssondb319762010-05-27 18:20:57 +0000475 UpdateEmptyFieldSubobjects(BaseDecl, Class, BaseOffset);
476 }
477
Anders Carlsson44687202010-06-08 19:09:24 +0000478 if (RD == Class) {
479 // This is the most derived class, traverse virtual bases as well.
480 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
481 E = RD->vbases_end(); I != E; ++I) {
482 const CXXRecordDecl *VBaseDecl =
483 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
484
Anders Carlsson3f018712010-10-31 23:45:59 +0000485 CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBaseDecl);
Anders Carlsson44687202010-06-08 19:09:24 +0000486 UpdateEmptyFieldSubobjects(VBaseDecl, Class, VBaseOffset);
487 }
488 }
489
Anders Carlssondb319762010-05-27 18:20:57 +0000490 // Traverse all member variables.
491 unsigned FieldNo = 0;
492 for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
493 I != E; ++I, ++FieldNo) {
494 const FieldDecl *FD = *I;
Anders Carlsson09814d32010-11-01 15:14:51 +0000495 if (FD->isBitField())
496 continue;
497
Anders Carlsson28466ab2010-10-31 22:13:23 +0000498 CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo);
Anders Carlssondb319762010-05-27 18:20:57 +0000499
Anders Carlsson28466ab2010-10-31 22:13:23 +0000500 UpdateEmptyFieldSubobjects(FD, FieldOffset);
Anders Carlssondb319762010-05-27 18:20:57 +0000501 }
502}
503
504void EmptySubobjectMap::UpdateEmptyFieldSubobjects(const FieldDecl *FD,
Anders Carlsson28466ab2010-10-31 22:13:23 +0000505 CharUnits Offset) {
Anders Carlssondb319762010-05-27 18:20:57 +0000506 QualType T = FD->getType();
507 if (const RecordType *RT = T->getAs<RecordType>()) {
508 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
509 UpdateEmptyFieldSubobjects(RD, RD, Offset);
510 return;
511 }
512
513 // If we have an array type we need to update every element.
514 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) {
515 QualType ElemTy = Context.getBaseElementType(AT);
516 const RecordType *RT = ElemTy->getAs<RecordType>();
517 if (!RT)
518 return;
519
520 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
521 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
522
523 uint64_t NumElements = Context.getConstantArrayElementCount(AT);
Anders Carlsson28466ab2010-10-31 22:13:23 +0000524 CharUnits ElementOffset = Offset;
Anders Carlssondb319762010-05-27 18:20:57 +0000525
526 for (uint64_t I = 0; I != NumElements; ++I) {
Anders Carlssonae111dc2010-06-13 17:49:16 +0000527 // We know that the only empty subobjects that can conflict with empty
Anders Carlssoncc59cc52010-06-13 18:00:18 +0000528 // field subobjects are subobjects of empty bases that can be placed at
Anders Carlssonae111dc2010-06-13 17:49:16 +0000529 // offset zero. Because of this, we only need to keep track of empty field
530 // subobjects with offsets less than the size of the largest empty
531 // subobject for our class.
532 if (ElementOffset >= SizeOfLargestEmptySubobject)
533 return;
534
Anders Carlssondb319762010-05-27 18:20:57 +0000535 UpdateEmptyFieldSubobjects(RD, RD, ElementOffset);
Ken Dyckc8ae5502011-02-09 01:59:34 +0000536 ElementOffset += Layout.getSize();
Anders Carlssondb319762010-05-27 18:20:57 +0000537 }
538 }
539}
540
Anders Carlssonc2226202010-05-26 05:58:59 +0000541class RecordLayoutBuilder {
Charles Davisc2c576a2010-08-19 00:55:19 +0000542protected:
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000543 // FIXME: Remove this and make the appropriate fields public.
544 friend class clang::ASTContext;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000545
Jay Foad39c79802011-01-12 09:06:06 +0000546 const ASTContext &Context;
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000547
Anders Carlssonf58de112010-05-26 15:32:58 +0000548 EmptySubobjectMap *EmptySubobjects;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000549
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000550 /// Size - The current size of the record layout.
551 uint64_t Size;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000552
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000553 /// Alignment - The current alignment of the record layout.
Ken Dyck4731d5b2011-02-16 02:05:21 +0000554 CharUnits Alignment;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000555
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000556 /// \brief The alignment if attribute packed is not used.
Ken Dyck1300b3b2011-02-16 02:11:31 +0000557 CharUnits UnpackedAlignment;
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000558
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000559 SmallVector<uint64_t, 16> FieldOffsets;
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000560
561 /// Packed - Whether the record is packed or not.
Daniel Dunbar6da10982010-05-27 05:45:51 +0000562 unsigned Packed : 1;
563
564 unsigned IsUnion : 1;
565
566 unsigned IsMac68kAlign : 1;
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000567
568 unsigned IsMsStruct : 1;
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000569
570 /// UnfilledBitsInLastByte - If the last field laid out was a bitfield,
571 /// this contains the number of bits in the last byte that can be used for
572 /// an adjacent bitfield if necessary.
573 unsigned char UnfilledBitsInLastByte;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000574
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000575 /// MaxFieldAlignment - The maximum allowed field alignment. This is set by
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000576 /// #pragma pack.
Ken Dyck02ced6f2011-02-17 01:49:42 +0000577 CharUnits MaxFieldAlignment;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000578
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000579 /// DataSize - The data size of the record being laid out.
580 uint64_t DataSize;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000581
Ken Dyckaf1c83f2011-02-16 01:52:01 +0000582 CharUnits NonVirtualSize;
Ken Dycka2d3dda2011-02-16 01:43:15 +0000583 CharUnits NonVirtualAlignment;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000584
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +0000585 FieldDecl *ZeroLengthBitfield;
Fariborz Jahanianeb397412011-05-02 17:20:56 +0000586
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000587 /// PrimaryBase - the primary base class (if one exists) of the class
588 /// we're laying out.
589 const CXXRecordDecl *PrimaryBase;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000590
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000591 /// PrimaryBaseIsVirtual - Whether the primary base of the class we're laying
592 /// out is virtual.
593 bool PrimaryBaseIsVirtual;
594
Eli Friedman84d2d3a2011-09-27 19:12:27 +0000595 /// VBPtrOffset - Virtual base table offset. Only for MS layout.
596 CharUnits VBPtrOffset;
597
Anders Carlsson22f57202010-10-31 21:01:46 +0000598 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetsMapTy;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000599
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000600 /// Bases - base classes and their offsets in the record.
601 BaseOffsetsMapTy Bases;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000602
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000603 // VBases - virtual base classes and their offsets in the record.
604 BaseOffsetsMapTy VBases;
605
606 /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are
607 /// primary base classes for some other direct or indirect base class.
Anders Carlsson5adde292010-11-24 22:55:48 +0000608 CXXIndirectPrimaryBaseSet IndirectPrimaryBases;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000609
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000610 /// FirstNearlyEmptyVBase - The first nearly empty virtual base class in
611 /// inheritance graph order. Used for determining the primary base class.
612 const CXXRecordDecl *FirstNearlyEmptyVBase;
613
614 /// VisitedVirtualBases - A set of all the visited virtual bases, used to
615 /// avoid visiting virtual bases more than once.
616 llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000617
Jay Foad39c79802011-01-12 09:06:06 +0000618 RecordLayoutBuilder(const ASTContext &Context, EmptySubobjectMap
Eli Friedman84d2d3a2011-09-27 19:12:27 +0000619 *EmptySubobjects, CharUnits Alignment)
Ken Dyck4731d5b2011-02-16 02:05:21 +0000620 : Context(Context), EmptySubobjects(EmptySubobjects), Size(0),
Eli Friedman84d2d3a2011-09-27 19:12:27 +0000621 Alignment(Alignment), UnpackedAlignment(Alignment),
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000622 Packed(false), IsUnion(false),
623 IsMac68kAlign(false), IsMsStruct(false),
Ken Dyck02ced6f2011-02-17 01:49:42 +0000624 UnfilledBitsInLastByte(0), MaxFieldAlignment(CharUnits::Zero()),
625 DataSize(0), NonVirtualSize(CharUnits::Zero()),
Fariborz Jahanianeb397412011-05-02 17:20:56 +0000626 NonVirtualAlignment(CharUnits::One()),
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +0000627 ZeroLengthBitfield(0), PrimaryBase(0),
Eli Friedman84d2d3a2011-09-27 19:12:27 +0000628 PrimaryBaseIsVirtual(false), VBPtrOffset(CharUnits::fromQuantity(-1)),
629 FirstNearlyEmptyVBase(0) { }
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000630
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000631 void Layout(const RecordDecl *D);
Anders Carlssonc28a6c92010-05-26 15:10:00 +0000632 void Layout(const CXXRecordDecl *D);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000633 void Layout(const ObjCInterfaceDecl *D);
634
635 void LayoutFields(const RecordDecl *D);
636 void LayoutField(const FieldDecl *D);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000637 void LayoutWideBitField(uint64_t FieldSize, uint64_t TypeSize,
638 bool FieldPacked, const FieldDecl *D);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000639 void LayoutBitField(const FieldDecl *D);
Eli Friedman84d2d3a2011-09-27 19:12:27 +0000640 void MSLayoutVirtualBases(const CXXRecordDecl *RD);
641 void MSLayout(const CXXRecordDecl *RD);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000642
Anders Carlssone3c24c72010-05-29 17:35:14 +0000643 /// BaseSubobjectInfoAllocator - Allocator for BaseSubobjectInfo objects.
644 llvm::SpecificBumpPtrAllocator<BaseSubobjectInfo> BaseSubobjectInfoAllocator;
645
646 typedef llvm::DenseMap<const CXXRecordDecl *, BaseSubobjectInfo *>
647 BaseSubobjectInfoMapTy;
648
649 /// VirtualBaseInfo - Map from all the (direct or indirect) virtual bases
650 /// of the class we're laying out to their base subobject info.
651 BaseSubobjectInfoMapTy VirtualBaseInfo;
652
653 /// NonVirtualBaseInfo - Map from all the direct non-virtual bases of the
654 /// class we're laying out to their base subobject info.
655 BaseSubobjectInfoMapTy NonVirtualBaseInfo;
656
657 /// ComputeBaseSubobjectInfo - Compute the base subobject information for the
658 /// bases of the given class.
659 void ComputeBaseSubobjectInfo(const CXXRecordDecl *RD);
660
661 /// ComputeBaseSubobjectInfo - Compute the base subobject information for a
662 /// single class and all of its base classes.
663 BaseSubobjectInfo *ComputeBaseSubobjectInfo(const CXXRecordDecl *RD,
664 bool IsVirtual,
665 BaseSubobjectInfo *Derived);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000666
667 /// DeterminePrimaryBase - Determine the primary base of the given class.
668 void DeterminePrimaryBase(const CXXRecordDecl *RD);
669
670 void SelectPrimaryVBase(const CXXRecordDecl *RD);
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000671
Eli Friedman84d2d3a2011-09-27 19:12:27 +0000672 CharUnits GetVirtualPointersSize(const CXXRecordDecl *RD) const;
Charles Davisc2c576a2010-08-19 00:55:19 +0000673
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000674 /// LayoutNonVirtualBases - Determines the primary base class (if any) and
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000675 /// lays it out. Will then proceed to lay out all non-virtual base clasess.
676 void LayoutNonVirtualBases(const CXXRecordDecl *RD);
677
678 /// LayoutNonVirtualBase - Lays out a single non-virtual base.
Anders Carlssonbb0e6782010-05-29 17:42:25 +0000679 void LayoutNonVirtualBase(const BaseSubobjectInfo *Base);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000680
Anders Carlssona2f8e412010-10-31 22:20:42 +0000681 void AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info,
682 CharUnits Offset);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000683
684 /// LayoutVirtualBases - Lays out all the virtual bases.
685 void LayoutVirtualBases(const CXXRecordDecl *RD,
686 const CXXRecordDecl *MostDerivedClass);
687
688 /// LayoutVirtualBase - Lays out a single virtual base.
Anders Carlssond6ff5d72010-05-29 17:48:36 +0000689 void LayoutVirtualBase(const BaseSubobjectInfo *Base);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000690
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000691 /// LayoutBase - Will lay out a base and return the offset where it was
Anders Carlssona2f8e412010-10-31 22:20:42 +0000692 /// placed, in chars.
693 CharUnits LayoutBase(const BaseSubobjectInfo *Base);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000694
Anders Carlssonc28a6c92010-05-26 15:10:00 +0000695 /// InitializeLayout - Initialize record layout for the given record decl.
Daniel Dunbar6da10982010-05-27 05:45:51 +0000696 void InitializeLayout(const Decl *D);
Anders Carlssonc28a6c92010-05-26 15:10:00 +0000697
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000698 /// FinishLayout - Finalize record layout. Adjust record size based on the
699 /// alignment.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000700 void FinishLayout(const NamedDecl *D);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000701
Ken Dyck85ef0432011-02-19 18:58:07 +0000702 void UpdateAlignment(CharUnits NewAlignment, CharUnits UnpackedNewAlignment);
703 void UpdateAlignment(CharUnits NewAlignment) {
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000704 UpdateAlignment(NewAlignment, NewAlignment);
705 }
706
707 void CheckFieldPadding(uint64_t Offset, uint64_t UnpaddedOffset,
708 uint64_t UnpackedOffset, unsigned UnpackedAlign,
709 bool isPacked, const FieldDecl *D);
710
711 DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000712
Ken Dyckecfc7552011-02-24 01:13:28 +0000713 CharUnits getSize() const {
Ken Dyck3c215f22011-02-24 01:33:05 +0000714 assert(Size % Context.getCharWidth() == 0);
Ken Dyckecfc7552011-02-24 01:13:28 +0000715 return Context.toCharUnitsFromBits(Size);
716 }
717 uint64_t getSizeInBits() const { return Size; }
718
719 void setSize(CharUnits NewSize) { Size = Context.toBits(NewSize); }
720 void setSize(uint64_t NewSize) { Size = NewSize; }
721
Eli Friedman84d2d3a2011-09-27 19:12:27 +0000722 CharUnits getAligment() const { return Alignment; }
723
Ken Dyckecfc7552011-02-24 01:13:28 +0000724 CharUnits getDataSize() const {
Ken Dyck3c215f22011-02-24 01:33:05 +0000725 assert(DataSize % Context.getCharWidth() == 0);
Ken Dyckecfc7552011-02-24 01:13:28 +0000726 return Context.toCharUnitsFromBits(DataSize);
727 }
728 uint64_t getDataSizeInBits() const { return DataSize; }
729
730 void setDataSize(CharUnits NewSize) { DataSize = Context.toBits(NewSize); }
731 void setDataSize(uint64_t NewSize) { DataSize = NewSize; }
732
Eli Friedman84d2d3a2011-09-27 19:12:27 +0000733 bool HasVBPtr(const CXXRecordDecl *RD) const;
734 bool HasNewVirtualFunction(const CXXRecordDecl *RD) const;
735
736 /// Add vbptr or vfptr to layout.
737 void AddVPointer();
Ken Dyckecfc7552011-02-24 01:13:28 +0000738
Argyrios Kyrtzidis499e6e42010-08-15 10:17:39 +0000739 RecordLayoutBuilder(const RecordLayoutBuilder&); // DO NOT IMPLEMENT
740 void operator=(const RecordLayoutBuilder&); // DO NOT IMPLEMENT
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000741public:
742 static const CXXMethodDecl *ComputeKeyFunction(const CXXRecordDecl *RD);
Argyrios Kyrtzidis3c6cd172010-08-25 00:32:14 +0000743
744 virtual ~RecordLayoutBuilder() { }
Eli Friedman84d2d3a2011-09-27 19:12:27 +0000745
746 CharUnits GetVBPtrOffset() const { return VBPtrOffset; }
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000747};
Benjamin Kramerc7656cd2010-05-26 09:58:31 +0000748} // end anonymous namespace
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000749
Anders Carlsson81430692009-09-22 03:02:06 +0000750void
Anders Carlssonc2226202010-05-26 05:58:59 +0000751RecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD) {
Anders Carlssonf2fa75b2010-03-11 03:39:12 +0000752 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000753 E = RD->bases_end(); I != E; ++I) {
Anders Carlssonf2fa75b2010-03-11 03:39:12 +0000754 assert(!I->getType()->isDependentType() &&
Sebastian Redl1054fae2009-10-25 17:03:50 +0000755 "Cannot layout class with dependent bases.");
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000756
Mike Stump11289f42009-09-09 15:08:12 +0000757 const CXXRecordDecl *Base =
Anders Carlssonf2fa75b2010-03-11 03:39:12 +0000758 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000759
Anders Carlssonf2fa75b2010-03-11 03:39:12 +0000760 // Check if this is a nearly empty virtual base.
Anders Carlsson60a62632010-11-25 01:51:53 +0000761 if (I->isVirtual() && Context.isNearlyEmpty(Base)) {
Anders Carlssonf2fa75b2010-03-11 03:39:12 +0000762 // If it's not an indirect primary base, then we've found our primary
763 // base.
Anders Carlsson81430692009-09-22 03:02:06 +0000764 if (!IndirectPrimaryBases.count(Base)) {
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000765 PrimaryBase = Base;
766 PrimaryBaseIsVirtual = true;
Mike Stump6f3793b2009-08-12 21:50:08 +0000767 return;
768 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000769
Anders Carlssonf2fa75b2010-03-11 03:39:12 +0000770 // Is this the first nearly empty virtual base?
771 if (!FirstNearlyEmptyVBase)
772 FirstNearlyEmptyVBase = Base;
Mike Stump6f3793b2009-08-12 21:50:08 +0000773 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000774
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000775 SelectPrimaryVBase(Base);
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000776 if (PrimaryBase)
Zhongxing Xuec345b72010-02-15 04:28:35 +0000777 return;
Mike Stump6f3793b2009-08-12 21:50:08 +0000778 }
779}
780
Ken Dyckaab635e2011-03-01 01:22:45 +0000781CharUnits
Charles Davisc2c576a2010-08-19 00:55:19 +0000782RecordLayoutBuilder::GetVirtualPointersSize(const CXXRecordDecl *RD) const {
Douglas Gregore8bbc122011-09-02 00:18:52 +0000783 return Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
Charles Davisc2c576a2010-08-19 00:55:19 +0000784}
785
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000786/// DeterminePrimaryBase - Determine the primary base of the given class.
Anders Carlssonc2226202010-05-26 05:58:59 +0000787void RecordLayoutBuilder::DeterminePrimaryBase(const CXXRecordDecl *RD) {
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000788 // If the class isn't dynamic, it won't have a primary base.
789 if (!RD->isDynamicClass())
790 return;
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000791
Anders Carlsson81430692009-09-22 03:02:06 +0000792 // Compute all the primary virtual bases for all of our direct and
Mike Stump590a7c72009-08-13 23:26:06 +0000793 // indirect bases, and record all their primary virtual base classes.
Anders Carlsson5adde292010-11-24 22:55:48 +0000794 RD->getIndirectPrimaryBases(IndirectPrimaryBases);
Mike Stump590a7c72009-08-13 23:26:06 +0000795
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000796 // If the record has a dynamic base class, attempt to choose a primary base
797 // class. It is the first (in direct base class order) non-virtual dynamic
Anders Carlsson81430692009-09-22 03:02:06 +0000798 // base class, if one exists.
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000799 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000800 e = RD->bases_end(); i != e; ++i) {
Anders Carlsson03ff3792009-11-27 22:05:05 +0000801 // Ignore virtual bases.
802 if (i->isVirtual())
803 continue;
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000804
Anders Carlsson03ff3792009-11-27 22:05:05 +0000805 const CXXRecordDecl *Base =
806 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
807
808 if (Base->isDynamicClass()) {
809 // We found it.
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000810 PrimaryBase = Base;
811 PrimaryBaseIsVirtual = false;
Anders Carlsson03ff3792009-11-27 22:05:05 +0000812 return;
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000813 }
814 }
815
816 // Otherwise, it is the first nearly empty virtual base that is not an
Mike Stump78696a72009-08-11 04:03:59 +0000817 // indirect primary virtual base class, if one exists.
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000818 if (RD->getNumVBases() != 0) {
819 SelectPrimaryVBase(RD);
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000820 if (PrimaryBase)
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000821 return;
822 }
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000823
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000824 // Otherwise, it is the first nearly empty virtual base that is not an
825 // indirect primary virtual base class, if one exists.
826 if (FirstNearlyEmptyVBase) {
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000827 PrimaryBase = FirstNearlyEmptyVBase;
828 PrimaryBaseIsVirtual = true;
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000829 return;
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000830 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000831
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000832 // Otherwise there is no primary base class.
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000833 assert(!PrimaryBase && "Should not get here with a primary base!");
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000834
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000835 // Allocate the virtual table pointer at offset zero.
836 assert(DataSize == 0 && "Vtable pointer must be at offset zero!");
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000837
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000838 // Update the size.
Ken Dyckaab635e2011-03-01 01:22:45 +0000839 setSize(getSize() + GetVirtualPointersSize(RD));
Ken Dyck1b4420e2011-02-28 02:01:38 +0000840 setDataSize(getSize());
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000841
Ken Dyck85ef0432011-02-19 18:58:07 +0000842 CharUnits UnpackedBaseAlign =
Douglas Gregore8bbc122011-09-02 00:18:52 +0000843 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(0));
Ken Dyck85ef0432011-02-19 18:58:07 +0000844 CharUnits BaseAlign = (Packed) ? CharUnits::One() : UnpackedBaseAlign;
Argyrios Kyrtzidisd62c9be2010-12-09 02:47:58 +0000845
846 // The maximum field alignment overrides base align.
Ken Dyck02ced6f2011-02-17 01:49:42 +0000847 if (!MaxFieldAlignment.isZero()) {
Ken Dyck85ef0432011-02-19 18:58:07 +0000848 BaseAlign = std::min(BaseAlign, MaxFieldAlignment);
849 UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment);
Argyrios Kyrtzidisd62c9be2010-12-09 02:47:58 +0000850 }
851
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000852 // Update the alignment.
Argyrios Kyrtzidisd62c9be2010-12-09 02:47:58 +0000853 UpdateAlignment(BaseAlign, UnpackedBaseAlign);
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000854}
855
Anders Carlssone3c24c72010-05-29 17:35:14 +0000856BaseSubobjectInfo *
857RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD,
858 bool IsVirtual,
859 BaseSubobjectInfo *Derived) {
860 BaseSubobjectInfo *Info;
861
862 if (IsVirtual) {
863 // Check if we already have info about this virtual base.
864 BaseSubobjectInfo *&InfoSlot = VirtualBaseInfo[RD];
865 if (InfoSlot) {
866 assert(InfoSlot->Class == RD && "Wrong class for virtual base info!");
867 return InfoSlot;
868 }
869
870 // We don't, create it.
871 InfoSlot = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo;
872 Info = InfoSlot;
873 } else {
874 Info = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo;
875 }
876
877 Info->Class = RD;
878 Info->IsVirtual = IsVirtual;
879 Info->Derived = 0;
880 Info->PrimaryVirtualBaseInfo = 0;
881
882 const CXXRecordDecl *PrimaryVirtualBase = 0;
883 BaseSubobjectInfo *PrimaryVirtualBaseInfo = 0;
884
885 // Check if this base has a primary virtual base.
886 if (RD->getNumVBases()) {
887 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlsson7f95cd12010-11-24 23:12:57 +0000888 if (Layout.isPrimaryBaseVirtual()) {
Anders Carlssone3c24c72010-05-29 17:35:14 +0000889 // This base does have a primary virtual base.
890 PrimaryVirtualBase = Layout.getPrimaryBase();
891 assert(PrimaryVirtualBase && "Didn't have a primary virtual base!");
892
893 // Now check if we have base subobject info about this primary base.
894 PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase);
895
896 if (PrimaryVirtualBaseInfo) {
897 if (PrimaryVirtualBaseInfo->Derived) {
898 // We did have info about this primary base, and it turns out that it
899 // has already been claimed as a primary virtual base for another
900 // base.
901 PrimaryVirtualBase = 0;
902 } else {
903 // We can claim this base as our primary base.
904 Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo;
905 PrimaryVirtualBaseInfo->Derived = Info;
906 }
907 }
908 }
909 }
910
911 // Now go through all direct bases.
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 Info->Bases.push_back(ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, Info));
920 }
921
922 if (PrimaryVirtualBase && !PrimaryVirtualBaseInfo) {
923 // Traversing the bases must have created the base info for our primary
924 // virtual base.
925 PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase);
926 assert(PrimaryVirtualBaseInfo &&
927 "Did not create a primary virtual base!");
928
929 // Claim the primary virtual base as our primary virtual base.
930 Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo;
931 PrimaryVirtualBaseInfo->Derived = Info;
932 }
933
934 return Info;
935}
936
937void RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD) {
938 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
939 E = RD->bases_end(); I != E; ++I) {
940 bool IsVirtual = I->isVirtual();
941
942 const CXXRecordDecl *BaseDecl =
943 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
944
945 // Compute the base subobject info for this base.
946 BaseSubobjectInfo *Info = ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, 0);
947
948 if (IsVirtual) {
949 // ComputeBaseInfo has already added this base for us.
950 assert(VirtualBaseInfo.count(BaseDecl) &&
951 "Did not add virtual base!");
952 } else {
953 // Add the base info to the map of non-virtual bases.
954 assert(!NonVirtualBaseInfo.count(BaseDecl) &&
955 "Non-virtual base already exists!");
956 NonVirtualBaseInfo.insert(std::make_pair(BaseDecl, Info));
957 }
958 }
959}
960
Anders Carlsson09ffa322010-03-10 22:21:28 +0000961void
Anders Carlssonc2226202010-05-26 05:58:59 +0000962RecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) {
Anders Carlssone3c24c72010-05-29 17:35:14 +0000963 // Then, determine the primary base class.
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000964 DeterminePrimaryBase(RD);
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000965
Anders Carlssone3c24c72010-05-29 17:35:14 +0000966 // Compute base subobject info.
967 ComputeBaseSubobjectInfo(RD);
968
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000969 // If we have a primary base class, lay it out.
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000970 if (PrimaryBase) {
971 if (PrimaryBaseIsVirtual) {
Anders Carlssone3c24c72010-05-29 17:35:14 +0000972 // If the primary virtual base was a primary virtual base of some other
973 // base class we'll have to steal it.
974 BaseSubobjectInfo *PrimaryBaseInfo = VirtualBaseInfo.lookup(PrimaryBase);
975 PrimaryBaseInfo->Derived = 0;
976
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000977 // We have a virtual primary base, insert it as an indirect primary base.
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000978 IndirectPrimaryBases.insert(PrimaryBase);
Anders Carlssonfe900962010-03-11 05:42:17 +0000979
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000980 assert(!VisitedVirtualBases.count(PrimaryBase) &&
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000981 "vbase already visited!");
982 VisitedVirtualBases.insert(PrimaryBase);
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000983
Anders Carlssond6ff5d72010-05-29 17:48:36 +0000984 LayoutVirtualBase(PrimaryBaseInfo);
Anders Carlssonbb0e6782010-05-29 17:42:25 +0000985 } else {
986 BaseSubobjectInfo *PrimaryBaseInfo =
987 NonVirtualBaseInfo.lookup(PrimaryBase);
988 assert(PrimaryBaseInfo &&
989 "Did not find base info for non-virtual primary base!");
990
991 LayoutNonVirtualBase(PrimaryBaseInfo);
992 }
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000993 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000994
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000995 // Now lay out the non-virtual bases.
996 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000997 E = RD->bases_end(); I != E; ++I) {
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000998
999 // Ignore virtual bases.
1000 if (I->isVirtual())
1001 continue;
1002
Anders Carlssonbb0e6782010-05-29 17:42:25 +00001003 const CXXRecordDecl *BaseDecl =
Anders Carlsson8630b5b2010-03-11 00:15:35 +00001004 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1005
1006 // Skip the primary base.
Anders Carlssonbb0e6782010-05-29 17:42:25 +00001007 if (BaseDecl == PrimaryBase && !PrimaryBaseIsVirtual)
Anders Carlsson8630b5b2010-03-11 00:15:35 +00001008 continue;
1009
1010 // Lay out the base.
Anders Carlssonbb0e6782010-05-29 17:42:25 +00001011 BaseSubobjectInfo *BaseInfo = NonVirtualBaseInfo.lookup(BaseDecl);
1012 assert(BaseInfo && "Did not find base info for non-virtual base!");
1013
1014 LayoutNonVirtualBase(BaseInfo);
Anders Carlsson09ffa322010-03-10 22:21:28 +00001015 }
1016}
1017
Anders Carlssonbb0e6782010-05-29 17:42:25 +00001018void RecordLayoutBuilder::LayoutNonVirtualBase(const BaseSubobjectInfo *Base) {
Anders Carlsson0d0b5882010-03-10 22:26:24 +00001019 // Layout the base.
Anders Carlssona2f8e412010-10-31 22:20:42 +00001020 CharUnits Offset = LayoutBase(Base);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001021
Anders Carlsson0d0b5882010-03-10 22:26:24 +00001022 // Add its base class offset.
Anders Carlssonbb0e6782010-05-29 17:42:25 +00001023 assert(!Bases.count(Base->Class) && "base offset already exists!");
Anders Carlssona2f8e412010-10-31 22:20:42 +00001024 Bases.insert(std::make_pair(Base->Class, Offset));
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001025
1026 AddPrimaryVirtualBaseOffsets(Base, Offset);
Anders Carlsson09ffa322010-03-10 22:21:28 +00001027}
Mike Stump2b84dd32009-11-05 04:02:15 +00001028
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001029void
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001030RecordLayoutBuilder::AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info,
Anders Carlssona2f8e412010-10-31 22:20:42 +00001031 CharUnits Offset) {
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001032 // This base isn't interesting, it has no virtual bases.
1033 if (!Info->Class->getNumVBases())
1034 return;
1035
1036 // First, check if we have a virtual primary base to add offsets for.
1037 if (Info->PrimaryVirtualBaseInfo) {
1038 assert(Info->PrimaryVirtualBaseInfo->IsVirtual &&
1039 "Primary virtual base is not virtual!");
1040 if (Info->PrimaryVirtualBaseInfo->Derived == Info) {
1041 // Add the offset.
1042 assert(!VBases.count(Info->PrimaryVirtualBaseInfo->Class) &&
1043 "primary vbase offset already exists!");
1044 VBases.insert(std::make_pair(Info->PrimaryVirtualBaseInfo->Class,
Anders Carlssona2f8e412010-10-31 22:20:42 +00001045 Offset));
Anders Carlssonea7b1822010-04-15 16:12:58 +00001046
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001047 // Traverse the primary virtual base.
1048 AddPrimaryVirtualBaseOffsets(Info->PrimaryVirtualBaseInfo, Offset);
1049 }
Anders Carlssonea7b1822010-04-15 16:12:58 +00001050 }
1051
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001052 // Now go through all direct non-virtual bases.
1053 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class);
1054 for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) {
1055 const BaseSubobjectInfo *Base = Info->Bases[I];
1056 if (Base->IsVirtual)
Anders Carlssonea7b1822010-04-15 16:12:58 +00001057 continue;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001058
Anders Carlsson0a14ee92010-11-01 00:21:58 +00001059 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class);
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001060 AddPrimaryVirtualBaseOffsets(Base, BaseOffset);
Anders Carlssonea7b1822010-04-15 16:12:58 +00001061 }
1062}
1063
Eli Friedman84d2d3a2011-09-27 19:12:27 +00001064void RecordLayoutBuilder::AddVPointer() {
1065 CharUnits PtrWidth =
1066 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
1067 setSize(getSize() + PtrWidth);
1068 setDataSize(getSize());
1069
1070 if (Alignment > PtrWidth) {
1071 setSize(getSize() + (Alignment - PtrWidth));
1072 setDataSize(getSize());
1073 }
1074}
1075
1076bool
1077RecordLayoutBuilder::HasNewVirtualFunction(const CXXRecordDecl *RD) const {
1078 for (CXXRecordDecl::method_iterator method = RD->method_begin();
1079 method != RD->method_end();
1080 ++method) {
1081 if (method->isVirtual() &&
1082 !method->size_overridden_methods()) {
1083 return true;
1084 }
1085 }
1086 return false;
1087}
1088
1089bool
1090RecordLayoutBuilder::HasVBPtr(const CXXRecordDecl *RD) const {
1091 if (!RD->getNumBases())
1092 return false;
1093
1094 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1095 E = RD->bases_end(); I != E; ++I) {
1096 if (!I->isVirtual()) {
1097 return false;
1098 }
1099 }
1100 return true;
1101}
1102
Anders Carlssonea7b1822010-04-15 16:12:58 +00001103void
Anders Carlssonc2226202010-05-26 05:58:59 +00001104RecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
Anders Carlssonea7b1822010-04-15 16:12:58 +00001105 const CXXRecordDecl *MostDerivedClass) {
Anders Carlssonde710c92010-03-11 04:33:54 +00001106 const CXXRecordDecl *PrimaryBase;
Anders Carlsson291279e2010-04-10 18:42:27 +00001107 bool PrimaryBaseIsVirtual;
Anders Carlssonfe900962010-03-11 05:42:17 +00001108
Anders Carlsson291279e2010-04-10 18:42:27 +00001109 if (MostDerivedClass == RD) {
Anders Carlssond20e7cd2010-05-26 05:20:58 +00001110 PrimaryBase = this->PrimaryBase;
1111 PrimaryBaseIsVirtual = this->PrimaryBaseIsVirtual;
Anders Carlsson291279e2010-04-10 18:42:27 +00001112 } else {
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001113 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlssonde710c92010-03-11 04:33:54 +00001114 PrimaryBase = Layout.getPrimaryBase();
Anders Carlsson7f95cd12010-11-24 23:12:57 +00001115 PrimaryBaseIsVirtual = Layout.isPrimaryBaseVirtual();
Anders Carlsson291279e2010-04-10 18:42:27 +00001116 }
1117
Anders Carlssonf7b7a1e2010-03-11 04:24:02 +00001118 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1119 E = RD->bases_end(); I != E; ++I) {
1120 assert(!I->getType()->isDependentType() &&
Sebastian Redl1054fae2009-10-25 17:03:50 +00001121 "Cannot layout class with dependent bases.");
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001122
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001123 const CXXRecordDecl *BaseDecl =
Anders Carlssonf7b7a1e2010-03-11 04:24:02 +00001124 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1125
1126 if (I->isVirtual()) {
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001127 if (PrimaryBase != BaseDecl || !PrimaryBaseIsVirtual) {
1128 bool IndirectPrimaryBase = IndirectPrimaryBases.count(BaseDecl);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001129
Anders Carlsson291279e2010-04-10 18:42:27 +00001130 // Only lay out the virtual base if it's not an indirect primary base.
1131 if (!IndirectPrimaryBase) {
1132 // Only visit virtual bases once.
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001133 if (!VisitedVirtualBases.insert(BaseDecl))
Anders Carlsson291279e2010-04-10 18:42:27 +00001134 continue;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001135
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001136 const BaseSubobjectInfo *BaseInfo = VirtualBaseInfo.lookup(BaseDecl);
1137 assert(BaseInfo && "Did not find virtual base info!");
1138 LayoutVirtualBase(BaseInfo);
Anders Carlsson6a848892010-03-11 04:10:39 +00001139 }
Mike Stump2b84dd32009-11-05 04:02:15 +00001140 }
Mike Stumpc2f591b2009-08-13 22:53:07 +00001141 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001142
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001143 if (!BaseDecl->getNumVBases()) {
Anders Carlssonf7b7a1e2010-03-11 04:24:02 +00001144 // This base isn't interesting since it doesn't have any virtual bases.
1145 continue;
Mike Stump996576f32009-08-16 19:04:13 +00001146 }
Anders Carlssonf7b7a1e2010-03-11 04:24:02 +00001147
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001148 LayoutVirtualBases(BaseDecl, MostDerivedClass);
Mike Stump6b2556f2009-08-06 13:41:24 +00001149 }
1150}
1151
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001152void RecordLayoutBuilder::LayoutVirtualBase(const BaseSubobjectInfo *Base) {
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001153 assert(!Base->Derived && "Trying to lay out a primary virtual base!");
1154
Anders Carlsson0d0b5882010-03-10 22:26:24 +00001155 // Layout the base.
Anders Carlssona2f8e412010-10-31 22:20:42 +00001156 CharUnits Offset = LayoutBase(Base);
Anders Carlsson0d0b5882010-03-10 22:26:24 +00001157
1158 // Add its base class offset.
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001159 assert(!VBases.count(Base->Class) && "vbase offset already exists!");
Anders Carlssona2f8e412010-10-31 22:20:42 +00001160 VBases.insert(std::make_pair(Base->Class, Offset));
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001161
1162 AddPrimaryVirtualBaseOffsets(Base, Offset);
Anders Carlsson09ffa322010-03-10 22:21:28 +00001163}
1164
Anders Carlssona2f8e412010-10-31 22:20:42 +00001165CharUnits RecordLayoutBuilder::LayoutBase(const BaseSubobjectInfo *Base) {
Anders Carlssond7f3fcf2010-05-29 20:47:33 +00001166 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base->Class);
Anders Carlsson09ffa322010-03-10 22:21:28 +00001167
1168 // If we have an empty base class, try to place it at offset 0.
Anders Carlssond7f3fcf2010-05-29 20:47:33 +00001169 if (Base->Class->isEmpty() &&
Anders Carlsson28466ab2010-10-31 22:13:23 +00001170 EmptySubobjects->CanPlaceBaseAtOffset(Base, CharUnits::Zero())) {
Ken Dyck1b4420e2011-02-28 02:01:38 +00001171 setSize(std::max(getSize(), Layout.getSize()));
Anders Carlsson09ffa322010-03-10 22:21:28 +00001172
Anders Carlssona2f8e412010-10-31 22:20:42 +00001173 return CharUnits::Zero();
Anders Carlsson09ffa322010-03-10 22:21:28 +00001174 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001175
Ken Dyck85ef0432011-02-19 18:58:07 +00001176 CharUnits UnpackedBaseAlign = Layout.getNonVirtualAlign();
1177 CharUnits BaseAlign = (Packed) ? CharUnits::One() : UnpackedBaseAlign;
Argyrios Kyrtzidis8b542742010-12-09 00:35:20 +00001178
1179 // The maximum field alignment overrides base align.
Ken Dyck02ced6f2011-02-17 01:49:42 +00001180 if (!MaxFieldAlignment.isZero()) {
Ken Dyck85ef0432011-02-19 18:58:07 +00001181 BaseAlign = std::min(BaseAlign, MaxFieldAlignment);
1182 UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment);
Argyrios Kyrtzidis8b542742010-12-09 00:35:20 +00001183 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001184
Anders Carlsson09ffa322010-03-10 22:21:28 +00001185 // Round up the current record size to the base's alignment boundary.
Ken Dyck1b4420e2011-02-28 02:01:38 +00001186 CharUnits Offset = getDataSize().RoundUpToAlignment(BaseAlign);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001187
Anders Carlsson09ffa322010-03-10 22:21:28 +00001188 // Try to place the base.
Ken Dyck1b4420e2011-02-28 02:01:38 +00001189 while (!EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset))
1190 Offset += BaseAlign;
Anders Carlsson09ffa322010-03-10 22:21:28 +00001191
Anders Carlssond7f3fcf2010-05-29 20:47:33 +00001192 if (!Base->Class->isEmpty()) {
Anders Carlsson09ffa322010-03-10 22:21:28 +00001193 // Update the data size.
Ken Dyck1b4420e2011-02-28 02:01:38 +00001194 setDataSize(Offset + Layout.getNonVirtualSize());
Anders Carlsson09ffa322010-03-10 22:21:28 +00001195
Ken Dyck1b4420e2011-02-28 02:01:38 +00001196 setSize(std::max(getSize(), getDataSize()));
Anders Carlsson09ffa322010-03-10 22:21:28 +00001197 } else
Ken Dyck1b4420e2011-02-28 02:01:38 +00001198 setSize(std::max(getSize(), Offset + Layout.getSize()));
Anders Carlsson09ffa322010-03-10 22:21:28 +00001199
1200 // Remember max struct/class alignment.
Argyrios Kyrtzidis8b542742010-12-09 00:35:20 +00001201 UpdateAlignment(BaseAlign, UnpackedBaseAlign);
Anders Carlsson09ffa322010-03-10 22:21:28 +00001202
Ken Dyck1b4420e2011-02-28 02:01:38 +00001203 return Offset;
Anders Carlsson09ffa322010-03-10 22:21:28 +00001204}
1205
Daniel Dunbar6da10982010-05-27 05:45:51 +00001206void RecordLayoutBuilder::InitializeLayout(const Decl *D) {
1207 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
1208 IsUnion = RD->isUnion();
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001209
Anders Carlsson28a5fa22009-08-08 19:38:24 +00001210 Packed = D->hasAttr<PackedAttr>();
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001211
1212 IsMsStruct = D->hasAttr<MsStructAttr>();
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001213
Daniel Dunbar6da10982010-05-27 05:45:51 +00001214 // mac68k alignment supersedes maximum field alignment and attribute aligned,
1215 // and forces all structures to have 2-byte alignment. The IBM docs on it
1216 // allude to additional (more complicated) semantics, especially with regard
1217 // to bit-fields, but gcc appears not to follow that.
1218 if (D->hasAttr<AlignMac68kAttr>()) {
1219 IsMac68kAlign = true;
Ken Dyck02ced6f2011-02-17 01:49:42 +00001220 MaxFieldAlignment = CharUnits::fromQuantity(2);
Ken Dyck4731d5b2011-02-16 02:05:21 +00001221 Alignment = CharUnits::fromQuantity(2);
Daniel Dunbar6da10982010-05-27 05:45:51 +00001222 } else {
1223 if (const MaxFieldAlignmentAttr *MFAA = D->getAttr<MaxFieldAlignmentAttr>())
Ken Dyck02ced6f2011-02-17 01:49:42 +00001224 MaxFieldAlignment = Context.toCharUnitsFromBits(MFAA->getAlignment());
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001225
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001226 if (unsigned MaxAlign = D->getMaxAlignment())
Ken Dyck85ef0432011-02-19 18:58:07 +00001227 UpdateAlignment(Context.toCharUnitsFromBits(MaxAlign));
Daniel Dunbar6da10982010-05-27 05:45:51 +00001228 }
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001229}
Anders Carlsson6d9f6f32009-07-19 00:18:47 +00001230
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001231void RecordLayoutBuilder::Layout(const RecordDecl *D) {
1232 InitializeLayout(D);
Anders Carlsson118ce162009-07-18 21:48:39 +00001233 LayoutFields(D);
Mike Stump11289f42009-09-09 15:08:12 +00001234
Anders Carlsson79474332009-07-18 20:20:21 +00001235 // Finally, round the size of the total struct up to the alignment of the
1236 // struct itself.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001237 FinishLayout(D);
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001238}
1239
1240void RecordLayoutBuilder::Layout(const CXXRecordDecl *RD) {
Eli Friedman84d2d3a2011-09-27 19:12:27 +00001241 if (Context.getTargetInfo().getCXXABI() == CXXABI_Microsoft) {
1242 MSLayout(RD);
1243 return ;
1244 }
1245
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001246 InitializeLayout(RD);
1247
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001248 // Lay out the vtable and the non-virtual bases.
1249 LayoutNonVirtualBases(RD);
1250
1251 LayoutFields(RD);
1252
Ken Dycke7380752011-03-10 01:53:59 +00001253 NonVirtualSize = Context.toCharUnitsFromBits(
1254 llvm::RoundUpToAlignment(getSizeInBits(),
Douglas Gregore8bbc122011-09-02 00:18:52 +00001255 Context.getTargetInfo().getCharAlign()));
Ken Dyck4731d5b2011-02-16 02:05:21 +00001256 NonVirtualAlignment = Alignment;
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001257
1258 // Lay out the virtual bases and add the primary virtual base offsets.
1259 LayoutVirtualBases(RD, RD);
1260
1261 VisitedVirtualBases.clear();
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001262
1263 // Finally, round the size of the total struct up to the alignment of the
1264 // struct itself.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001265 FinishLayout(RD);
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001266
Anders Carlsson5b441d72010-04-10 21:24:48 +00001267#ifndef NDEBUG
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001268 // Check that we have base offsets for all bases.
1269 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1270 E = RD->bases_end(); I != E; ++I) {
1271 if (I->isVirtual())
1272 continue;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001273
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001274 const CXXRecordDecl *BaseDecl =
1275 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1276
1277 assert(Bases.count(BaseDecl) && "Did not find base offset!");
1278 }
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001279
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001280 // And all virtual bases.
1281 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
1282 E = RD->vbases_end(); I != E; ++I) {
1283 const CXXRecordDecl *BaseDecl =
1284 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001285
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001286 assert(VBases.count(BaseDecl) && "Did not find base offset!");
Anders Carlsson5b441d72010-04-10 21:24:48 +00001287 }
1288#endif
Anders Carlsson79474332009-07-18 20:20:21 +00001289}
1290
Anders Carlssonc2226202010-05-26 05:58:59 +00001291void RecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D) {
Anders Carlsson4f516282009-07-18 20:50:59 +00001292 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001293 const ASTRecordLayout &SL = Context.getASTObjCInterfaceLayout(SD);
Anders Carlsson4f516282009-07-18 20:50:59 +00001294
Ken Dyck85ef0432011-02-19 18:58:07 +00001295 UpdateAlignment(SL.getAlignment());
Mike Stump11289f42009-09-09 15:08:12 +00001296
Anders Carlsson4f516282009-07-18 20:50:59 +00001297 // We start laying out ivars not at the end of the superclass
1298 // structure, but at the next byte following the last field.
Ken Dyckecfc7552011-02-24 01:13:28 +00001299 setSize(SL.getDataSize());
Ken Dyck1b4420e2011-02-28 02:01:38 +00001300 setDataSize(getSize());
Anders Carlsson4f516282009-07-18 20:50:59 +00001301 }
Mike Stump11289f42009-09-09 15:08:12 +00001302
Daniel Dunbar6da10982010-05-27 05:45:51 +00001303 InitializeLayout(D);
Anders Carlsson4f516282009-07-18 20:50:59 +00001304 // Layout each ivar sequentially.
Jordy Rosea91768e2011-07-22 02:08:32 +00001305 for (const ObjCIvarDecl *IVD = D->all_declared_ivar_begin(); IVD;
1306 IVD = IVD->getNextIvar())
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00001307 LayoutField(IVD);
Mike Stump11289f42009-09-09 15:08:12 +00001308
Anders Carlsson4f516282009-07-18 20:50:59 +00001309 // Finally, round the size of the total struct up to the alignment of the
1310 // struct itself.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001311 FinishLayout(D);
Anders Carlsson4f516282009-07-18 20:50:59 +00001312}
1313
Anders Carlssonc2226202010-05-26 05:58:59 +00001314void RecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
Anders Carlsson118ce162009-07-18 21:48:39 +00001315 // Layout each field, for now, just sequentially, respecting alignment. In
1316 // the future, this will need to be tweakable by targets.
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001317 const FieldDecl *LastFD = 0;
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001318 ZeroLengthBitfield = 0;
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001319 unsigned RemainingInAlignment = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001320 for (RecordDecl::field_iterator Field = D->field_begin(),
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001321 FieldEnd = D->field_end(); Field != FieldEnd; ++Field) {
1322 if (IsMsStruct) {
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001323 FieldDecl *FD = (*Field);
1324 if (Context.ZeroBitfieldFollowsBitfield(FD, LastFD))
1325 ZeroLengthBitfield = FD;
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001326 // Zero-length bitfields following non-bitfield members are
1327 // ignored:
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001328 else if (Context.ZeroBitfieldFollowsNonBitfield(FD, LastFD))
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001329 continue;
Fariborz Jahanian307eace2011-05-06 22:42:22 +00001330 // FIXME. streamline these conditions into a simple one.
Fariborz Jahanianb7a28792011-05-06 21:56:12 +00001331 else if (Context.BitfieldFollowsBitfield(FD, LastFD) ||
Chad Rosierf01a7dd2011-08-04 23:34:15 +00001332 Context.BitfieldFollowsNonBitfield(FD, LastFD) ||
1333 Context.NonBitfieldFollowsBitfield(FD, LastFD)) {
Fariborz Jahanian307eace2011-05-06 22:42:22 +00001334 // 1) Adjacent bit fields are packed into the same 1-, 2-, or
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001335 // 4-byte allocation unit if the integral types are the same
1336 // size and if the next bit field fits into the current
1337 // allocation unit without crossing the boundary imposed by the
1338 // common alignment requirements of the bit fields.
Fariborz Jahanian307eace2011-05-06 22:42:22 +00001339 // 2) Establish a new alignment for a bitfield following
Fariborz Jahanianb7a28792011-05-06 21:56:12 +00001340 // a non-bitfield if size of their types differ.
Fariborz Jahanian307eace2011-05-06 22:42:22 +00001341 // 3) Establish a new alignment for a non-bitfield following
1342 // a bitfield if size of their types differ.
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001343 std::pair<uint64_t, unsigned> FieldInfo =
1344 Context.getTypeInfo(FD->getType());
1345 uint64_t TypeSize = FieldInfo.first;
1346 unsigned FieldAlign = FieldInfo.second;
Fariborz Jahanian7adbed62011-05-09 22:03:17 +00001347 // This check is needed for 'long long' in -m32 mode.
1348 if (TypeSize > FieldAlign)
1349 FieldAlign = TypeSize;
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001350 FieldInfo = Context.getTypeInfo(LastFD->getType());
1351 uint64_t TypeSizeLastFD = FieldInfo.first;
1352 unsigned FieldAlignLastFD = FieldInfo.second;
Fariborz Jahanian7adbed62011-05-09 22:03:17 +00001353 // This check is needed for 'long long' in -m32 mode.
1354 if (TypeSizeLastFD > FieldAlignLastFD)
1355 FieldAlignLastFD = TypeSizeLastFD;
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001356
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001357 if (TypeSizeLastFD != TypeSize) {
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001358 if (RemainingInAlignment &&
1359 LastFD && LastFD->isBitField() &&
1360 LastFD->getBitWidth()->EvaluateAsInt(Context).getZExtValue()) {
1361 // If previous field was a bitfield with some remaining unfilled
1362 // bits, pad the field so current field starts on its type boundary.
1363 uint64_t FieldOffset =
1364 getDataSizeInBits() - UnfilledBitsInLastByte;
1365 uint64_t NewSizeInBits = RemainingInAlignment + FieldOffset;
1366 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
Douglas Gregore8bbc122011-09-02 00:18:52 +00001367 Context.getTargetInfo().getCharAlign()));
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001368 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
1369 RemainingInAlignment = 0;
1370 }
1371
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001372 uint64_t UnpaddedFieldOffset =
1373 getDataSizeInBits() - UnfilledBitsInLastByte;
1374 FieldAlign = std::max(FieldAlign, FieldAlignLastFD);
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001375
Fariborz Jahanian07ceb0a2011-05-10 19:00:50 +00001376 // The maximum field alignment overrides the aligned attribute.
1377 if (!MaxFieldAlignment.isZero()) {
1378 unsigned MaxFieldAlignmentInBits =
1379 Context.toBits(MaxFieldAlignment);
1380 FieldAlign = std::min(FieldAlign, MaxFieldAlignmentInBits);
1381 }
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001382
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001383 uint64_t NewSizeInBits =
1384 llvm::RoundUpToAlignment(UnpaddedFieldOffset, FieldAlign);
1385 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
Douglas Gregore8bbc122011-09-02 00:18:52 +00001386 Context.getTargetInfo().getCharAlign()));
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001387 UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits;
1388 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
1389 }
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001390 if (FD->isBitField()) {
1391 uint64_t FieldSize =
1392 FD->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
1393 assert (FieldSize > 0 && "LayoutFields - ms_struct layout");
1394 if (RemainingInAlignment < FieldSize)
1395 RemainingInAlignment = TypeSize - FieldSize;
1396 else
1397 RemainingInAlignment -= FieldSize;
1398 }
1399 }
1400 else if (FD->isBitField()) {
1401 uint64_t FieldSize =
1402 FD->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
1403 std::pair<uint64_t, unsigned> FieldInfo =
1404 Context.getTypeInfo(FD->getType());
1405 uint64_t TypeSize = FieldInfo.first;
1406 RemainingInAlignment = TypeSize - FieldSize;
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001407 }
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001408 LastFD = FD;
1409 }
Douglas Gregore8bbc122011-09-02 00:18:52 +00001410 else if (!Context.getTargetInfo().useBitFieldTypeAlignment() &&
1411 Context.getTargetInfo().useZeroLengthBitfieldAlignment()) {
Chad Rosier18903ee2011-08-04 01:21:14 +00001412 FieldDecl *FD = (*Field);
Chad Rosiere1a6a0e2011-08-05 22:38:04 +00001413 if (FD->isBitField() &&
1414 FD->getBitWidth()->EvaluateAsInt(Context).getZExtValue() == 0)
Chad Rosier18903ee2011-08-04 01:21:14 +00001415 ZeroLengthBitfield = FD;
Chad Rosier18903ee2011-08-04 01:21:14 +00001416 }
Anders Carlsson118ce162009-07-18 21:48:39 +00001417 LayoutField(*Field);
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001418 }
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001419 if (IsMsStruct && RemainingInAlignment &&
1420 LastFD && LastFD->isBitField() &&
1421 LastFD->getBitWidth()->EvaluateAsInt(Context).getZExtValue()) {
1422 // If we ended a bitfield before the full length of the type then
1423 // pad the struct out to the full length of the last type.
1424 uint64_t FieldOffset =
1425 getDataSizeInBits() - UnfilledBitsInLastByte;
1426 uint64_t NewSizeInBits = RemainingInAlignment + FieldOffset;
1427 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
Douglas Gregore8bbc122011-09-02 00:18:52 +00001428 Context.getTargetInfo().getCharAlign()));
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001429 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
1430 }
Anders Carlsson118ce162009-07-18 21:48:39 +00001431}
1432
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001433void RecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize,
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001434 uint64_t TypeSize,
1435 bool FieldPacked,
1436 const FieldDecl *D) {
Anders Carlsson57235162010-04-16 15:57:11 +00001437 assert(Context.getLangOptions().CPlusPlus &&
1438 "Can only have wide bit-fields in C++!");
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001439
Anders Carlsson57235162010-04-16 15:57:11 +00001440 // Itanium C++ ABI 2.4:
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001441 // If sizeof(T)*8 < n, let T' be the largest integral POD type with
Anders Carlsson57235162010-04-16 15:57:11 +00001442 // sizeof(T')*8 <= n.
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001443
Anders Carlsson57235162010-04-16 15:57:11 +00001444 QualType IntegralPODTypes[] = {
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001445 Context.UnsignedCharTy, Context.UnsignedShortTy, Context.UnsignedIntTy,
Anders Carlsson57235162010-04-16 15:57:11 +00001446 Context.UnsignedLongTy, Context.UnsignedLongLongTy
1447 };
1448
Anders Carlsson57235162010-04-16 15:57:11 +00001449 QualType Type;
1450 for (unsigned I = 0, E = llvm::array_lengthof(IntegralPODTypes);
1451 I != E; ++I) {
1452 uint64_t Size = Context.getTypeSize(IntegralPODTypes[I]);
Anders Carlsson57235162010-04-16 15:57:11 +00001453
1454 if (Size > FieldSize)
1455 break;
1456
1457 Type = IntegralPODTypes[I];
1458 }
1459 assert(!Type.isNull() && "Did not find a type!");
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001460
Ken Dyckdbe37f32011-03-01 01:36:00 +00001461 CharUnits TypeAlign = Context.getTypeAlignInChars(Type);
Anders Carlsson57235162010-04-16 15:57:11 +00001462
1463 // We're not going to use any of the unfilled bits in the last byte.
1464 UnfilledBitsInLastByte = 0;
1465
Anders Carlssonaad5fa82010-04-17 20:21:41 +00001466 uint64_t FieldOffset;
Ken Dyckecfc7552011-02-24 01:13:28 +00001467 uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001468
Anders Carlsson57235162010-04-16 15:57:11 +00001469 if (IsUnion) {
Ken Dyckecfc7552011-02-24 01:13:28 +00001470 setDataSize(std::max(getDataSizeInBits(), FieldSize));
Anders Carlssonaad5fa82010-04-17 20:21:41 +00001471 FieldOffset = 0;
Anders Carlsson57235162010-04-16 15:57:11 +00001472 } else {
Chad Rosiere1a6a0e2011-08-05 22:38:04 +00001473 // The bitfield is allocated starting at the next offset aligned
1474 // appropriately for T', with length n bits.
Ken Dyckdbe37f32011-03-01 01:36:00 +00001475 FieldOffset = llvm::RoundUpToAlignment(getDataSizeInBits(),
1476 Context.toBits(TypeAlign));
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001477
Anders Carlsson57235162010-04-16 15:57:11 +00001478 uint64_t NewSizeInBits = FieldOffset + FieldSize;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001479
Ken Dycka1a2e8d2011-03-10 02:00:35 +00001480 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
Douglas Gregore8bbc122011-09-02 00:18:52 +00001481 Context.getTargetInfo().getCharAlign()));
Ken Dyckecfc7552011-02-24 01:13:28 +00001482 UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits;
Anders Carlsson57235162010-04-16 15:57:11 +00001483 }
1484
1485 // Place this field at the current location.
1486 FieldOffsets.push_back(FieldOffset);
1487
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001488 CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, FieldOffset,
Ken Dyckdbe37f32011-03-01 01:36:00 +00001489 Context.toBits(TypeAlign), FieldPacked, D);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001490
Anders Carlsson57235162010-04-16 15:57:11 +00001491 // Update the size.
Ken Dyckecfc7552011-02-24 01:13:28 +00001492 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001493
Anders Carlsson57235162010-04-16 15:57:11 +00001494 // Remember max struct/class alignment.
Ken Dyckdbe37f32011-03-01 01:36:00 +00001495 UpdateAlignment(TypeAlign);
Anders Carlsson57235162010-04-16 15:57:11 +00001496}
1497
Anders Carlssonc2226202010-05-26 05:58:59 +00001498void RecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
Anders Carlsson07209442009-11-22 17:37:31 +00001499 bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
Ken Dyckecfc7552011-02-24 01:13:28 +00001500 uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte;
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001501 uint64_t FieldOffset = IsUnion ? 0 : UnpaddedFieldOffset;
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001502 uint64_t FieldSize = D->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001503
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001504 std::pair<uint64_t, unsigned> FieldInfo = Context.getTypeInfo(D->getType());
Anders Carlsson07209442009-11-22 17:37:31 +00001505 uint64_t TypeSize = FieldInfo.first;
1506 unsigned FieldAlign = FieldInfo.second;
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001507
Fariborz Jahanian7adbed62011-05-09 22:03:17 +00001508 // This check is needed for 'long long' in -m32 mode.
1509 if (IsMsStruct && (TypeSize > FieldAlign))
1510 FieldAlign = TypeSize;
Chad Rosier18903ee2011-08-04 01:21:14 +00001511
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001512 if (ZeroLengthBitfield) {
Chad Rosiere1a6a0e2011-08-05 22:38:04 +00001513 std::pair<uint64_t, unsigned> FieldInfo;
1514 unsigned ZeroLengthBitfieldAlignment;
1515 if (IsMsStruct) {
1516 // If a zero-length bitfield is inserted after a bitfield,
1517 // and the alignment of the zero-length bitfield is
1518 // greater than the member that follows it, `bar', `bar'
1519 // will be aligned as the type of the zero-length bitfield.
1520 if (ZeroLengthBitfield != D) {
1521 FieldInfo = Context.getTypeInfo(ZeroLengthBitfield->getType());
1522 ZeroLengthBitfieldAlignment = FieldInfo.second;
1523 // Ignore alignment of subsequent zero-length bitfields.
1524 if ((ZeroLengthBitfieldAlignment > FieldAlign) || (FieldSize == 0))
1525 FieldAlign = ZeroLengthBitfieldAlignment;
1526 if (FieldSize)
1527 ZeroLengthBitfield = 0;
1528 }
1529 } else {
1530 // The alignment of a zero-length bitfield affects the alignment
1531 // of the next member. The alignment is the max of the zero
1532 // length bitfield's alignment and a target specific fixed value.
1533 unsigned ZeroLengthBitfieldBoundary =
Douglas Gregore8bbc122011-09-02 00:18:52 +00001534 Context.getTargetInfo().getZeroLengthBitfieldBoundary();
Chad Rosiere1a6a0e2011-08-05 22:38:04 +00001535 if (ZeroLengthBitfieldBoundary > FieldAlign)
1536 FieldAlign = ZeroLengthBitfieldBoundary;
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001537 }
1538 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001539
Anders Carlsson57235162010-04-16 15:57:11 +00001540 if (FieldSize > TypeSize) {
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001541 LayoutWideBitField(FieldSize, TypeSize, FieldPacked, D);
Anders Carlsson57235162010-04-16 15:57:11 +00001542 return;
1543 }
1544
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001545 // The align if the field is not packed. This is to check if the attribute
1546 // was unnecessary (-Wpacked).
1547 unsigned UnpackedFieldAlign = FieldAlign;
1548 uint64_t UnpackedFieldOffset = FieldOffset;
Douglas Gregore8bbc122011-09-02 00:18:52 +00001549 if (!Context.getTargetInfo().useBitFieldTypeAlignment() && !ZeroLengthBitfield)
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001550 UnpackedFieldAlign = 1;
1551
Chad Rosiere1a6a0e2011-08-05 22:38:04 +00001552 if (FieldPacked ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00001553 (!Context.getTargetInfo().useBitFieldTypeAlignment() && !ZeroLengthBitfield))
Anders Carlsson07209442009-11-22 17:37:31 +00001554 FieldAlign = 1;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001555 FieldAlign = std::max(FieldAlign, D->getMaxAlignment());
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001556 UnpackedFieldAlign = std::max(UnpackedFieldAlign, D->getMaxAlignment());
Anders Carlsson07209442009-11-22 17:37:31 +00001557
1558 // The maximum field alignment overrides the aligned attribute.
Ken Dyck02ced6f2011-02-17 01:49:42 +00001559 if (!MaxFieldAlignment.isZero()) {
1560 unsigned MaxFieldAlignmentInBits = Context.toBits(MaxFieldAlignment);
1561 FieldAlign = std::min(FieldAlign, MaxFieldAlignmentInBits);
1562 UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignmentInBits);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001563 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001564
Daniel Dunbar3d9289c2010-04-15 06:18:39 +00001565 // Check if we need to add padding to give the field the correct alignment.
Anders Carlsson07209442009-11-22 17:37:31 +00001566 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
Daniel Dunbarf35e7652010-06-29 18:34:35 +00001567 FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001568
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001569 if (FieldSize == 0 ||
1570 (UnpackedFieldOffset & (UnpackedFieldAlign-1)) + FieldSize > TypeSize)
1571 UnpackedFieldOffset = llvm::RoundUpToAlignment(UnpackedFieldOffset,
1572 UnpackedFieldAlign);
1573
Chad Rosiere1a6a0e2011-08-05 22:38:04 +00001574 // Padding members don't affect overall alignment, unless zero length bitfield
1575 // alignment is enabled.
Douglas Gregore8bbc122011-09-02 00:18:52 +00001576 if (!D->getIdentifier() && !Context.getTargetInfo().useZeroLengthBitfieldAlignment())
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001577 FieldAlign = UnpackedFieldAlign = 1;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001578
Chad Rosiere1a6a0e2011-08-05 22:38:04 +00001579 if (!IsMsStruct)
1580 ZeroLengthBitfield = 0;
1581
Anders Carlsson07209442009-11-22 17:37:31 +00001582 // Place this field at the current location.
1583 FieldOffsets.push_back(FieldOffset);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001584
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001585 CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, UnpackedFieldOffset,
1586 UnpackedFieldAlign, FieldPacked, D);
1587
Anders Carlssonba958402009-11-22 19:13:51 +00001588 // Update DataSize to include the last byte containing (part of) the bitfield.
1589 if (IsUnion) {
1590 // FIXME: I think FieldSize should be TypeSize here.
Ken Dyckecfc7552011-02-24 01:13:28 +00001591 setDataSize(std::max(getDataSizeInBits(), FieldSize));
Anders Carlssonba958402009-11-22 19:13:51 +00001592 } else {
1593 uint64_t NewSizeInBits = FieldOffset + FieldSize;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001594
Ken Dycka1a2e8d2011-03-10 02:00:35 +00001595 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
Douglas Gregore8bbc122011-09-02 00:18:52 +00001596 Context.getTargetInfo().getCharAlign()));
Ken Dyckecfc7552011-02-24 01:13:28 +00001597 UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits;
Anders Carlssonba958402009-11-22 19:13:51 +00001598 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001599
Anders Carlssonba958402009-11-22 19:13:51 +00001600 // Update the size.
Ken Dyckecfc7552011-02-24 01:13:28 +00001601 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001602
Anders Carlsson07209442009-11-22 17:37:31 +00001603 // Remember max struct/class alignment.
Ken Dyck85ef0432011-02-19 18:58:07 +00001604 UpdateAlignment(Context.toCharUnitsFromBits(FieldAlign),
1605 Context.toCharUnitsFromBits(UnpackedFieldAlign));
Anders Carlsson07209442009-11-22 17:37:31 +00001606}
1607
Anders Carlssonc2226202010-05-26 05:58:59 +00001608void RecordLayoutBuilder::LayoutField(const FieldDecl *D) {
Anders Carlsson07209442009-11-22 17:37:31 +00001609 if (D->isBitField()) {
1610 LayoutBitField(D);
1611 return;
1612 }
1613
Ken Dyckecfc7552011-02-24 01:13:28 +00001614 uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte;
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001615
Anders Carlssonba958402009-11-22 19:13:51 +00001616 // Reset the unfilled bits.
1617 UnfilledBitsInLastByte = 0;
1618
Anders Carlsson07209442009-11-22 17:37:31 +00001619 bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
Ken Dyck6d90e892011-02-20 02:06:09 +00001620 CharUnits FieldOffset =
Ken Dyckecfc7552011-02-24 01:13:28 +00001621 IsUnion ? CharUnits::Zero() : getDataSize();
Ken Dyck6d90e892011-02-20 02:06:09 +00001622 CharUnits FieldSize;
1623 CharUnits FieldAlign;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001624
Anders Carlsson07209442009-11-22 17:37:31 +00001625 if (D->getType()->isIncompleteArrayType()) {
1626 // This is a flexible array member; we can't directly
1627 // query getTypeInfo about these, so we figure it out here.
1628 // Flexible array members don't have any size, but they
1629 // have to be aligned appropriately for their element type.
Ken Dyck6d90e892011-02-20 02:06:09 +00001630 FieldSize = CharUnits::Zero();
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001631 const ArrayType* ATy = Context.getAsArrayType(D->getType());
Ken Dyck6d90e892011-02-20 02:06:09 +00001632 FieldAlign = Context.getTypeAlignInChars(ATy->getElementType());
Anders Carlsson07209442009-11-22 17:37:31 +00001633 } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) {
1634 unsigned AS = RT->getPointeeType().getAddressSpace();
Ken Dyck6d90e892011-02-20 02:06:09 +00001635 FieldSize =
Douglas Gregore8bbc122011-09-02 00:18:52 +00001636 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(AS));
Ken Dyck6d90e892011-02-20 02:06:09 +00001637 FieldAlign =
Douglas Gregore8bbc122011-09-02 00:18:52 +00001638 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(AS));
Anders Carlsson79474332009-07-18 20:20:21 +00001639 } else {
Ken Dyck6d90e892011-02-20 02:06:09 +00001640 std::pair<CharUnits, CharUnits> FieldInfo =
1641 Context.getTypeInfoInChars(D->getType());
Anders Carlsson07209442009-11-22 17:37:31 +00001642 FieldSize = FieldInfo.first;
1643 FieldAlign = FieldInfo.second;
Chad Rosier18903ee2011-08-04 01:21:14 +00001644
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001645 if (ZeroLengthBitfield) {
Chad Rosier18903ee2011-08-04 01:21:14 +00001646 CharUnits ZeroLengthBitfieldBoundary =
1647 Context.toCharUnitsFromBits(
Douglas Gregore8bbc122011-09-02 00:18:52 +00001648 Context.getTargetInfo().getZeroLengthBitfieldBoundary());
Chad Rosier18903ee2011-08-04 01:21:14 +00001649 if (ZeroLengthBitfieldBoundary == CharUnits::Zero()) {
1650 // If a zero-length bitfield is inserted after a bitfield,
1651 // and the alignment of the zero-length bitfield is
1652 // greater than the member that follows it, `bar', `bar'
1653 // will be aligned as the type of the zero-length bitfield.
1654 std::pair<CharUnits, CharUnits> FieldInfo =
1655 Context.getTypeInfoInChars(ZeroLengthBitfield->getType());
1656 CharUnits ZeroLengthBitfieldAlignment = FieldInfo.second;
1657 if (ZeroLengthBitfieldAlignment > FieldAlign)
1658 FieldAlign = ZeroLengthBitfieldAlignment;
Chad Rosier64b18ee2011-08-04 19:25:14 +00001659 } else if (ZeroLengthBitfieldBoundary > FieldAlign) {
Chad Rosier18903ee2011-08-04 01:21:14 +00001660 // Align 'bar' based on a fixed alignment specified by the target.
Douglas Gregore8bbc122011-09-02 00:18:52 +00001661 assert(Context.getTargetInfo().useZeroLengthBitfieldAlignment() &&
Chad Rosiera336c6f2011-08-04 17:52:43 +00001662 "ZeroLengthBitfieldBoundary should only be used in conjunction"
1663 " with useZeroLengthBitfieldAlignment.");
Chad Rosier18903ee2011-08-04 01:21:14 +00001664 FieldAlign = ZeroLengthBitfieldBoundary;
1665 }
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001666 ZeroLengthBitfield = 0;
1667 }
Douglas Gregordbe39272011-02-01 15:15:22 +00001668
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001669 if (Context.getLangOptions().MSBitfields || IsMsStruct) {
Douglas Gregordbe39272011-02-01 15:15:22 +00001670 // If MS bitfield layout is required, figure out what type is being
1671 // laid out and align the field to the width of that type.
1672
1673 // Resolve all typedefs down to their base type and round up the field
1674 // alignment if necessary.
1675 QualType T = Context.getBaseElementType(D->getType());
1676 if (const BuiltinType *BTy = T->getAs<BuiltinType>()) {
Ken Dyck6d90e892011-02-20 02:06:09 +00001677 CharUnits TypeSize = Context.getTypeSizeInChars(BTy);
Douglas Gregordbe39272011-02-01 15:15:22 +00001678 if (TypeSize > FieldAlign)
1679 FieldAlign = TypeSize;
1680 }
1681 }
Anders Carlsson79474332009-07-18 20:20:21 +00001682 }
Mike Stump11289f42009-09-09 15:08:12 +00001683
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001684 // The align if the field is not packed. This is to check if the attribute
1685 // was unnecessary (-Wpacked).
Ken Dyck6d90e892011-02-20 02:06:09 +00001686 CharUnits UnpackedFieldAlign = FieldAlign;
1687 CharUnits UnpackedFieldOffset = FieldOffset;
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001688
Anders Carlsson07209442009-11-22 17:37:31 +00001689 if (FieldPacked)
Ken Dyck6d90e892011-02-20 02:06:09 +00001690 FieldAlign = CharUnits::One();
1691 CharUnits MaxAlignmentInChars =
1692 Context.toCharUnitsFromBits(D->getMaxAlignment());
1693 FieldAlign = std::max(FieldAlign, MaxAlignmentInChars);
1694 UnpackedFieldAlign = std::max(UnpackedFieldAlign, MaxAlignmentInChars);
Anders Carlsson07209442009-11-22 17:37:31 +00001695
1696 // The maximum field alignment overrides the aligned attribute.
Ken Dyck02ced6f2011-02-17 01:49:42 +00001697 if (!MaxFieldAlignment.isZero()) {
Ken Dyck6d90e892011-02-20 02:06:09 +00001698 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
1699 UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignment);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001700 }
Anders Carlsson07209442009-11-22 17:37:31 +00001701
1702 // Round up the current record size to the field's alignment boundary.
Ken Dyck6d90e892011-02-20 02:06:09 +00001703 FieldOffset = FieldOffset.RoundUpToAlignment(FieldAlign);
1704 UnpackedFieldOffset =
1705 UnpackedFieldOffset.RoundUpToAlignment(UnpackedFieldAlign);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001706
Anders Carlssonb1fcdd02010-05-30 06:52:33 +00001707 if (!IsUnion && EmptySubobjects) {
1708 // Check if we can place the field at this offset.
Ken Dyck6d90e892011-02-20 02:06:09 +00001709 while (!EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset)) {
Anders Carlsson07209442009-11-22 17:37:31 +00001710 // We couldn't place the field at the offset. Try again at a new offset.
1711 FieldOffset += FieldAlign;
1712 }
Anders Carlsson07209442009-11-22 17:37:31 +00001713 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001714
Anders Carlsson79474332009-07-18 20:20:21 +00001715 // Place this field at the current location.
Ken Dyck6d90e892011-02-20 02:06:09 +00001716 FieldOffsets.push_back(Context.toBits(FieldOffset));
Mike Stump11289f42009-09-09 15:08:12 +00001717
Ken Dyck6d90e892011-02-20 02:06:09 +00001718 CheckFieldPadding(Context.toBits(FieldOffset), UnpaddedFieldOffset,
1719 Context.toBits(UnpackedFieldOffset),
1720 Context.toBits(UnpackedFieldAlign), FieldPacked, D);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001721
Anders Carlsson79474332009-07-18 20:20:21 +00001722 // Reserve space for this field.
Daniel Dunbar30cdc702011-02-24 16:40:53 +00001723 uint64_t FieldSizeInBits = Context.toBits(FieldSize);
Anders Carlsson79474332009-07-18 20:20:21 +00001724 if (IsUnion)
Daniel Dunbar30cdc702011-02-24 16:40:53 +00001725 setSize(std::max(getSizeInBits(), FieldSizeInBits));
Anders Carlsson79474332009-07-18 20:20:21 +00001726 else
Ken Dyck1b4420e2011-02-28 02:01:38 +00001727 setSize(FieldOffset + FieldSize);
Mike Stump11289f42009-09-09 15:08:12 +00001728
Anders Carlsson47680d82009-09-26 01:34:51 +00001729 // Update the data size.
Daniel Dunbar30cdc702011-02-24 16:40:53 +00001730 setDataSize(getSizeInBits());
Mike Stump11289f42009-09-09 15:08:12 +00001731
Anders Carlsson79474332009-07-18 20:20:21 +00001732 // Remember max struct/class alignment.
Ken Dyck6d90e892011-02-20 02:06:09 +00001733 UpdateAlignment(FieldAlign, UnpackedFieldAlign);
Anders Carlsson79474332009-07-18 20:20:21 +00001734}
1735
Eli Friedman84d2d3a2011-09-27 19:12:27 +00001736void RecordLayoutBuilder::MSLayoutVirtualBases(const CXXRecordDecl *RD) {
1737
1738 if (!RD->getNumVBases())
1739 return;
1740
1741 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
1742 E = RD->vbases_end(); I != E; ++I) {
1743
1744 const CXXRecordDecl* BaseDecl = I->getType()->getAsCXXRecordDecl();
1745 const BaseSubobjectInfo* BaseInfo = VirtualBaseInfo.lookup(BaseDecl);
1746
1747 assert(BaseInfo && "Did not find virtual base info!");
1748
1749 LayoutVirtualBase(BaseInfo);
1750 }
1751}
1752
1753void RecordLayoutBuilder::MSLayout(const CXXRecordDecl *RD) {
1754
1755 bool IsVBPtrAddedToLayout = false;
1756
1757 InitializeLayout(RD);
1758
1759 if (HasVBPtr(RD)) {
1760 // If all bases are virtual and the class declares a new virtual function,
1761 // MSVC builds a vfptr.
1762 if (HasNewVirtualFunction(RD)) {
1763 AddVPointer();
1764 }
1765
1766 VBPtrOffset = getSize();
1767 AddVPointer();
1768 IsVBPtrAddedToLayout = true;
1769
1770 ComputeBaseSubobjectInfo(RD);
1771 } else {
1772 LayoutNonVirtualBases(RD);
1773 }
1774
1775 if (RD->getNumVBases() &&
1776 !IsVBPtrAddedToLayout) {
1777 // Add vbptr.
1778 VBPtrOffset = getSize();
1779 AddVPointer();
1780 }
1781
1782 LayoutFields(RD);
1783
1784 NonVirtualSize = Context.toCharUnitsFromBits(
1785 llvm::RoundUpToAlignment(getSizeInBits(),
1786 Context.getTargetInfo().getCharAlign()));
1787 NonVirtualAlignment = Alignment;
1788
1789 if (NonVirtualSize != NonVirtualSize.RoundUpToAlignment(Alignment)) {
1790 CharUnits AlignMember =
1791 NonVirtualSize.RoundUpToAlignment(Alignment) - NonVirtualSize;
1792
1793 setSize(getSize() + AlignMember);
1794 setDataSize(getSize());
1795
1796 NonVirtualSize = Context.toCharUnitsFromBits(
1797 llvm::RoundUpToAlignment(getSizeInBits(),
1798 Context.getTargetInfo().getCharAlign()));
1799 }
1800
1801 MSLayoutVirtualBases(RD);
1802
1803 VisitedVirtualBases.clear();
1804
1805 // Finally, round the size of the total struct up to the alignment of the
1806 // struct itself.
1807 if (!RD->getNumVBases())
1808 FinishLayout(RD);
1809
1810#ifndef NDEBUG
1811 // Check that we have base offsets for all bases.
1812 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1813 E = RD->bases_end(); I != E; ++I) {
1814 if (I->isVirtual())
1815 continue;
1816
1817 const CXXRecordDecl *BaseDecl =
1818 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1819
1820 assert(Bases.count(BaseDecl) && "Did not find base offset!");
1821 }
1822
1823 // And all virtual bases.
1824 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
1825 E = RD->vbases_end(); I != E; ++I) {
1826 const CXXRecordDecl *BaseDecl =
1827 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1828
1829 assert(VBases.count(BaseDecl) && "Did not find base offset!");
1830 }
1831#endif
1832}
1833
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001834void RecordLayoutBuilder::FinishLayout(const NamedDecl *D) {
Anders Carlsson79474332009-07-18 20:20:21 +00001835 // In C++, records cannot be of size 0.
Ken Dyckecfc7552011-02-24 01:13:28 +00001836 if (Context.getLangOptions().CPlusPlus && getSizeInBits() == 0) {
Fariborz Jahanian09b23312011-02-02 19:36:18 +00001837 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
1838 // Compatibility with gcc requires a class (pod or non-pod)
1839 // which is not empty but of size 0; such as having fields of
1840 // array of zero-length, remains of Size 0
1841 if (RD->isEmpty())
Ken Dyck1b4420e2011-02-28 02:01:38 +00001842 setSize(CharUnits::One());
Fariborz Jahanian09b23312011-02-02 19:36:18 +00001843 }
1844 else
Ken Dyck1b4420e2011-02-28 02:01:38 +00001845 setSize(CharUnits::One());
Fariborz Jahanian09b23312011-02-02 19:36:18 +00001846 }
Anders Carlsson79474332009-07-18 20:20:21 +00001847 // Finally, round the size of the record up to the alignment of the
1848 // record itself.
Ken Dyckecfc7552011-02-24 01:13:28 +00001849 uint64_t UnpaddedSize = getSizeInBits() - UnfilledBitsInLastByte;
Ken Dyck1b4420e2011-02-28 02:01:38 +00001850 uint64_t UnpackedSizeInBits =
Ken Dyckecfc7552011-02-24 01:13:28 +00001851 llvm::RoundUpToAlignment(getSizeInBits(),
1852 Context.toBits(UnpackedAlignment));
Ken Dyck1b4420e2011-02-28 02:01:38 +00001853 CharUnits UnpackedSize = Context.toCharUnitsFromBits(UnpackedSizeInBits);
Ken Dyckecfc7552011-02-24 01:13:28 +00001854 setSize(llvm::RoundUpToAlignment(getSizeInBits(), Context.toBits(Alignment)));
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001855
Douglas Gregore8bbc122011-09-02 00:18:52 +00001856 unsigned CharBitNum = Context.getTargetInfo().getCharWidth();
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001857 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) {
1858 // Warn if padding was introduced to the struct/class/union.
Ken Dyckecfc7552011-02-24 01:13:28 +00001859 if (getSizeInBits() > UnpaddedSize) {
1860 unsigned PadSize = getSizeInBits() - UnpaddedSize;
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001861 bool InBits = true;
1862 if (PadSize % CharBitNum == 0) {
1863 PadSize = PadSize / CharBitNum;
1864 InBits = false;
1865 }
1866 Diag(RD->getLocation(), diag::warn_padded_struct_size)
1867 << Context.getTypeDeclType(RD)
1868 << PadSize
1869 << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not
1870 }
1871
1872 // Warn if we packed it unnecessarily. If the alignment is 1 byte don't
1873 // bother since there won't be alignment issues.
Ken Dyckecfc7552011-02-24 01:13:28 +00001874 if (Packed && UnpackedAlignment > CharUnits::One() &&
Ken Dyck1b4420e2011-02-28 02:01:38 +00001875 getSize() == UnpackedSize)
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001876 Diag(D->getLocation(), diag::warn_unnecessary_packed)
1877 << Context.getTypeDeclType(RD);
1878 }
Anders Carlsson79474332009-07-18 20:20:21 +00001879}
1880
Ken Dyck85ef0432011-02-19 18:58:07 +00001881void RecordLayoutBuilder::UpdateAlignment(CharUnits NewAlignment,
1882 CharUnits UnpackedNewAlignment) {
Daniel Dunbar6da10982010-05-27 05:45:51 +00001883 // The alignment is not modified when using 'mac68k' alignment.
1884 if (IsMac68kAlign)
1885 return;
1886
Ken Dyck85ef0432011-02-19 18:58:07 +00001887 if (NewAlignment > Alignment) {
1888 assert(llvm::isPowerOf2_32(NewAlignment.getQuantity() &&
1889 "Alignment not a power of 2"));
1890 Alignment = NewAlignment;
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001891 }
1892
Ken Dyck85ef0432011-02-19 18:58:07 +00001893 if (UnpackedNewAlignment > UnpackedAlignment) {
1894 assert(llvm::isPowerOf2_32(UnpackedNewAlignment.getQuantity() &&
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001895 "Alignment not a power of 2"));
Ken Dyck85ef0432011-02-19 18:58:07 +00001896 UnpackedAlignment = UnpackedNewAlignment;
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001897 }
1898}
1899
1900void RecordLayoutBuilder::CheckFieldPadding(uint64_t Offset,
1901 uint64_t UnpaddedOffset,
1902 uint64_t UnpackedOffset,
1903 unsigned UnpackedAlign,
1904 bool isPacked,
1905 const FieldDecl *D) {
1906 // We let objc ivars without warning, objc interfaces generally are not used
1907 // for padding tricks.
1908 if (isa<ObjCIvarDecl>(D))
Anders Carlsson79474332009-07-18 20:20:21 +00001909 return;
Mike Stump11289f42009-09-09 15:08:12 +00001910
Ted Kremenekfed48af2011-09-06 19:40:45 +00001911 // Don't warn about structs created without a SourceLocation. This can
1912 // be done by clients of the AST, such as codegen.
1913 if (D->getLocation().isInvalid())
1914 return;
1915
Douglas Gregore8bbc122011-09-02 00:18:52 +00001916 unsigned CharBitNum = Context.getTargetInfo().getCharWidth();
Mike Stump11289f42009-09-09 15:08:12 +00001917
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001918 // Warn if padding was introduced to the struct/class.
1919 if (!IsUnion && Offset > UnpaddedOffset) {
1920 unsigned PadSize = Offset - UnpaddedOffset;
1921 bool InBits = true;
1922 if (PadSize % CharBitNum == 0) {
1923 PadSize = PadSize / CharBitNum;
1924 InBits = false;
1925 }
1926 if (D->getIdentifier())
1927 Diag(D->getLocation(), diag::warn_padded_struct_field)
1928 << (D->getParent()->isStruct() ? 0 : 1) // struct|class
1929 << Context.getTypeDeclType(D->getParent())
1930 << PadSize
1931 << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1) // plural or not
1932 << D->getIdentifier();
1933 else
1934 Diag(D->getLocation(), diag::warn_padded_struct_anon_field)
1935 << (D->getParent()->isStruct() ? 0 : 1) // struct|class
1936 << Context.getTypeDeclType(D->getParent())
1937 << PadSize
1938 << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not
1939 }
1940
1941 // Warn if we packed it unnecessarily. If the alignment is 1 byte don't
1942 // bother since there won't be alignment issues.
1943 if (isPacked && UnpackedAlign > CharBitNum && Offset == UnpackedOffset)
1944 Diag(D->getLocation(), diag::warn_unnecessary_packed)
1945 << D->getIdentifier();
Anders Carlsson79474332009-07-18 20:20:21 +00001946}
Mike Stump11289f42009-09-09 15:08:12 +00001947
Anders Carlsson5ebf8b42009-12-07 04:35:11 +00001948const CXXMethodDecl *
Anders Carlssonc2226202010-05-26 05:58:59 +00001949RecordLayoutBuilder::ComputeKeyFunction(const CXXRecordDecl *RD) {
Daniel Dunbarccabe482010-04-19 20:44:53 +00001950 // If a class isn't polymorphic it doesn't have a key function.
Anders Carlsson5ebf8b42009-12-07 04:35:11 +00001951 if (!RD->isPolymorphic())
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001952 return 0;
Eli Friedmanf2c79b62009-12-08 03:56:49 +00001953
Eli Friedman300f55d2011-06-10 21:53:06 +00001954 // A class that is not externally visible doesn't have a key function. (Or
Eli Friedmanf2c79b62009-12-08 03:56:49 +00001955 // at least, there's no point to assigning a key function to such a class;
1956 // this doesn't affect the ABI.)
Eli Friedman300f55d2011-06-10 21:53:06 +00001957 if (RD->getLinkage() != ExternalLinkage)
Eli Friedmanf2c79b62009-12-08 03:56:49 +00001958 return 0;
1959
Argyrios Kyrtzidis8c64bbe2010-10-13 02:39:41 +00001960 // Template instantiations don't have key functions,see Itanium C++ ABI 5.2.6.
1961 // Same behavior as GCC.
1962 TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
1963 if (TSK == TSK_ImplicitInstantiation ||
1964 TSK == TSK_ExplicitInstantiationDefinition)
1965 return 0;
1966
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001967 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
1968 E = RD->method_end(); I != E; ++I) {
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001969 const CXXMethodDecl *MD = *I;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001970
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001971 if (!MD->isVirtual())
1972 continue;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001973
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001974 if (MD->isPure())
1975 continue;
Eli Friedmanf2c79b62009-12-08 03:56:49 +00001976
Anders Carlssonf98849e2009-12-02 17:15:43 +00001977 // Ignore implicit member functions, they are always marked as inline, but
1978 // they don't have a body until they're defined.
1979 if (MD->isImplicit())
1980 continue;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001981
Douglas Gregora318efd2010-01-05 19:06:31 +00001982 if (MD->isInlineSpecified())
1983 continue;
Eli Friedman71a26d82009-12-06 20:50:05 +00001984
1985 if (MD->hasInlineBody())
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001986 continue;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001987
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001988 // We found it.
1989 return MD;
1990 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001991
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001992 return 0;
1993}
1994
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001995DiagnosticBuilder
1996RecordLayoutBuilder::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00001997 return Context.getDiagnostics().Report(Loc, DiagID);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001998}
1999
Anders Carlssondf291d82010-05-26 04:56:53 +00002000/// getASTRecordLayout - Get or compute information about the layout of the
2001/// specified record (struct/union/class), which indicates its size and field
2002/// position information.
Jay Foad39c79802011-01-12 09:06:06 +00002003const ASTRecordLayout &
2004ASTContext::getASTRecordLayout(const RecordDecl *D) const {
Anders Carlssondf291d82010-05-26 04:56:53 +00002005 D = D->getDefinition();
2006 assert(D && "Cannot get layout of forward declarations!");
2007
2008 // Look up this layout, if already laid out, return what we have.
2009 // Note that we can't save a reference to the entry because this function
2010 // is recursive.
2011 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
2012 if (Entry) return *Entry;
2013
Anders Carlssond2954862010-05-26 05:10:47 +00002014 const ASTRecordLayout *NewEntry;
2015
2016 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
Anders Carlssonc121b4e2010-05-27 00:07:01 +00002017 EmptySubobjectMap EmptySubobjects(*this, RD);
Anders Carlsson439edd12010-05-27 05:41:06 +00002018
Argyrios Kyrtzidis3c6cd172010-08-25 00:32:14 +00002019 llvm::OwningPtr<RecordLayoutBuilder> Builder;
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002020 CharUnits TargetAlign = CharUnits::One();
2021
2022 Builder.reset(new RecordLayoutBuilder(*this,
2023 &EmptySubobjects,
2024 TargetAlign));
2025
Ted Kremenekf75d0892011-03-19 01:00:36 +00002026 // Recover resources if we crash before exiting this method.
Ted Kremenek022dbc12011-03-22 01:15:19 +00002027 llvm::CrashRecoveryContextCleanupRegistrar<RecordLayoutBuilder>
2028 RecordBuilderCleanup(Builder.get());
Ted Kremenekf75d0892011-03-19 01:00:36 +00002029
Charles Davisc2c576a2010-08-19 00:55:19 +00002030 Builder->Layout(RD);
Anders Carlssond2954862010-05-26 05:10:47 +00002031
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002032 TargetAlign = Builder->getAligment();
2033
2034 if (getTargetInfo().getCXXABI() == CXXABI_Microsoft &&
2035 TargetAlign.getQuantity() > 4) {
2036 // MSVC rounds the vtable pointer to the struct alignment in what must
2037 // be a multi-pass operation. For now, let the builder figure out the
2038 // alignment and recalculate the layout once its known.
2039 Builder.reset(new RecordLayoutBuilder(*this,
2040 &EmptySubobjects,
2041 TargetAlign));
2042
2043 Builder->Layout(RD);
2044
2045 // Recover resources if we crash before exiting this method.
2046 llvm::CrashRecoveryContextCleanupRegistrar<RecordLayoutBuilder>
2047 RecordBuilderCleanup(Builder.get());
2048 }
2049
Anders Carlssond2954862010-05-26 05:10:47 +00002050 // FIXME: This is not always correct. See the part about bitfields at
2051 // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info.
2052 // FIXME: IsPODForThePurposeOfLayout should be stored in the record layout.
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002053 // This does not affect the calculations of MSVC layouts
2054 bool IsPODForThePurposeOfLayout =
2055 (getTargetInfo().getCXXABI() == CXXABI_Microsoft) ||
2056 cast<CXXRecordDecl>(D)->isPOD();
Anders Carlssond2954862010-05-26 05:10:47 +00002057
2058 // FIXME: This should be done in FinalizeLayout.
Ken Dyck1b4420e2011-02-28 02:01:38 +00002059 CharUnits DataSize =
2060 IsPODForThePurposeOfLayout ? Builder->getSize() : Builder->getDataSize();
2061 CharUnits NonVirtualSize =
2062 IsPODForThePurposeOfLayout ? DataSize : Builder->NonVirtualSize;
Anders Carlssond2954862010-05-26 05:10:47 +00002063
Daniel Dunbar592a85c2010-05-27 02:25:46 +00002064 NewEntry =
Ken Dyck1b4420e2011-02-28 02:01:38 +00002065 new (*this) ASTRecordLayout(*this, Builder->getSize(),
Ken Dyck4731d5b2011-02-16 02:05:21 +00002066 Builder->Alignment,
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002067 Builder->GetVBPtrOffset(),
Ken Dyck1b4420e2011-02-28 02:01:38 +00002068 DataSize,
Ken Dyckd5090c12011-02-11 02:20:09 +00002069 Builder->FieldOffsets.data(),
Charles Davisc2c576a2010-08-19 00:55:19 +00002070 Builder->FieldOffsets.size(),
Ken Dyckaf1c83f2011-02-16 01:52:01 +00002071 NonVirtualSize,
Ken Dycka2d3dda2011-02-16 01:43:15 +00002072 Builder->NonVirtualAlignment,
Anders Carlssonc121b4e2010-05-27 00:07:01 +00002073 EmptySubobjects.SizeOfLargestEmptySubobject,
Charles Davisc2c576a2010-08-19 00:55:19 +00002074 Builder->PrimaryBase,
2075 Builder->PrimaryBaseIsVirtual,
2076 Builder->Bases, Builder->VBases);
Anders Carlssond2954862010-05-26 05:10:47 +00002077 } else {
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002078 RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0, CharUnits::One());
Anders Carlssond2954862010-05-26 05:10:47 +00002079 Builder.Layout(D);
Daniel Dunbar592a85c2010-05-27 02:25:46 +00002080
Anders Carlssond2954862010-05-26 05:10:47 +00002081 NewEntry =
Ken Dyck1b4420e2011-02-28 02:01:38 +00002082 new (*this) ASTRecordLayout(*this, Builder.getSize(),
Ken Dyck4731d5b2011-02-16 02:05:21 +00002083 Builder.Alignment,
Ken Dyck1b4420e2011-02-28 02:01:38 +00002084 Builder.getSize(),
Anders Carlssond2954862010-05-26 05:10:47 +00002085 Builder.FieldOffsets.data(),
2086 Builder.FieldOffsets.size());
2087 }
2088
Anders Carlssondf291d82010-05-26 04:56:53 +00002089 ASTRecordLayouts[D] = NewEntry;
2090
2091 if (getLangOptions().DumpRecordLayouts) {
2092 llvm::errs() << "\n*** Dumping AST Record Layout\n";
2093 DumpRecordLayout(D, llvm::errs());
2094 }
2095
2096 return *NewEntry;
2097}
2098
2099const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) {
2100 RD = cast<CXXRecordDecl>(RD->getDefinition());
2101 assert(RD && "Cannot get key function for forward declarations!");
Daniel Dunbar592a85c2010-05-27 02:25:46 +00002102
Anders Carlssondf291d82010-05-26 04:56:53 +00002103 const CXXMethodDecl *&Entry = KeyFunctions[RD];
Daniel Dunbar592a85c2010-05-27 02:25:46 +00002104 if (!Entry)
Anders Carlssonc2226202010-05-26 05:58:59 +00002105 Entry = RecordLayoutBuilder::ComputeKeyFunction(RD);
Daniel Dunbar592a85c2010-05-27 02:25:46 +00002106
Anders Carlssondf291d82010-05-26 04:56:53 +00002107 return Entry;
2108}
2109
Eric Christopher8a39a012011-10-05 06:00:51 +00002110/// getObjCLayout - Get or compute information about the layout of the
2111/// given interface.
Anders Carlssondf291d82010-05-26 04:56:53 +00002112///
2113/// \param Impl - If given, also include the layout of the interface's
2114/// implementation. This may differ by including synthesized ivars.
2115const ASTRecordLayout &
2116ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
Jay Foad39c79802011-01-12 09:06:06 +00002117 const ObjCImplementationDecl *Impl) const {
Anders Carlssondf291d82010-05-26 04:56:53 +00002118 assert(!D->isForwardDecl() && "Invalid interface decl!");
2119
2120 // Look up this layout, if already laid out, return what we have.
2121 ObjCContainerDecl *Key =
2122 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
2123 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
2124 return *Entry;
2125
2126 // Add in synthesized ivar count if laying out an implementation.
2127 if (Impl) {
2128 unsigned SynthCount = CountNonClassIvars(D);
2129 // If there aren't any sythesized ivars then reuse the interface
2130 // entry. Note we can't cache this because we simply free all
2131 // entries later; however we shouldn't look up implementations
2132 // frequently.
2133 if (SynthCount == 0)
2134 return getObjCLayout(D, 0);
2135 }
2136
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002137 RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0, CharUnits::One());
Anders Carlsson6ed3a9a2010-05-26 05:04:25 +00002138 Builder.Layout(D);
2139
Anders Carlssondf291d82010-05-26 04:56:53 +00002140 const ASTRecordLayout *NewEntry =
Ken Dyck1b4420e2011-02-28 02:01:38 +00002141 new (*this) ASTRecordLayout(*this, Builder.getSize(),
Ken Dyck4731d5b2011-02-16 02:05:21 +00002142 Builder.Alignment,
Ken Dyck1b4420e2011-02-28 02:01:38 +00002143 Builder.getDataSize(),
Anders Carlsson6ed3a9a2010-05-26 05:04:25 +00002144 Builder.FieldOffsets.data(),
2145 Builder.FieldOffsets.size());
Daniel Dunbar592a85c2010-05-27 02:25:46 +00002146
Anders Carlssondf291d82010-05-26 04:56:53 +00002147 ObjCLayouts[Key] = NewEntry;
2148
2149 return *NewEntry;
2150}
2151
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002152static void PrintOffset(raw_ostream &OS,
Anders Carlsson3f018712010-10-31 23:45:59 +00002153 CharUnits Offset, unsigned IndentLevel) {
2154 OS << llvm::format("%4d | ", Offset.getQuantity());
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002155 OS.indent(IndentLevel * 2);
2156}
2157
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002158static void DumpCXXRecordLayout(raw_ostream &OS,
Jay Foad39c79802011-01-12 09:06:06 +00002159 const CXXRecordDecl *RD, const ASTContext &C,
Anders Carlsson3f018712010-10-31 23:45:59 +00002160 CharUnits Offset,
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002161 unsigned IndentLevel,
2162 const char* Description,
2163 bool IncludeVirtualBases) {
Anders Carlsson3f018712010-10-31 23:45:59 +00002164 const ASTRecordLayout &Layout = C.getASTRecordLayout(RD);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002165
2166 PrintOffset(OS, Offset, IndentLevel);
Dan Gohman145f3f12010-04-19 16:39:44 +00002167 OS << C.getTypeDeclType(const_cast<CXXRecordDecl *>(RD)).getAsString();
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002168 if (Description)
2169 OS << ' ' << Description;
2170 if (RD->isEmpty())
2171 OS << " (empty)";
2172 OS << '\n';
2173
2174 IndentLevel++;
2175
Anders Carlsson3f018712010-10-31 23:45:59 +00002176 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002177 bool HasVbptr = Layout.getVBPtrOffset() != CharUnits::fromQuantity(-1);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002178
2179 // Vtable pointer.
2180 if (RD->isDynamicClass() && !PrimaryBase) {
2181 PrintOffset(OS, Offset, IndentLevel);
Benjamin Kramerb11416d2010-04-17 09:33:03 +00002182 OS << '(' << RD << " vtable pointer)\n";
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002183 }
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002184
2185 if (HasVbptr && !PrimaryBase) {
2186 PrintOffset(OS, Offset + Layout.getVBPtrOffset(), IndentLevel);
2187 OS << '(' << RD << " vbtable pointer)\n";
2188
2189 // one vbtable per class
2190 HasVbptr = false;
2191 }
2192
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002193 // Dump (non-virtual) bases
2194 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
2195 E = RD->bases_end(); I != E; ++I) {
2196 assert(!I->getType()->isDependentType() &&
2197 "Cannot layout class with dependent bases.");
2198 if (I->isVirtual())
2199 continue;
2200
2201 const CXXRecordDecl *Base =
2202 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
2203
Anders Carlsson3f018712010-10-31 23:45:59 +00002204 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002205
2206 DumpCXXRecordLayout(OS, Base, C, BaseOffset, IndentLevel,
2207 Base == PrimaryBase ? "(primary base)" : "(base)",
2208 /*IncludeVirtualBases=*/false);
2209 }
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002210 // vbptr
2211 if (HasVbptr) {
2212 PrintOffset(OS, Offset + Layout.getVBPtrOffset(), IndentLevel);
2213 OS << '(' << RD << " vbtable pointer)\n";
2214 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002215
2216 // Dump fields.
2217 uint64_t FieldNo = 0;
2218 for (CXXRecordDecl::field_iterator I = RD->field_begin(),
2219 E = RD->field_end(); I != E; ++I, ++FieldNo) {
2220 const FieldDecl *Field = *I;
Anders Carlsson3f018712010-10-31 23:45:59 +00002221 CharUnits FieldOffset = Offset +
Ken Dyck86a7fcc2011-01-18 01:56:16 +00002222 C.toCharUnitsFromBits(Layout.getFieldOffset(FieldNo));
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002223
2224 if (const RecordType *RT = Field->getType()->getAs<RecordType>()) {
2225 if (const CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
2226 DumpCXXRecordLayout(OS, D, C, FieldOffset, IndentLevel,
Daniel Dunbar56df9772010-08-17 22:39:59 +00002227 Field->getName().data(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002228 /*IncludeVirtualBases=*/true);
2229 continue;
2230 }
2231 }
2232
2233 PrintOffset(OS, FieldOffset, IndentLevel);
Benjamin Kramerb11416d2010-04-17 09:33:03 +00002234 OS << Field->getType().getAsString() << ' ' << Field << '\n';
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002235 }
2236
2237 if (!IncludeVirtualBases)
2238 return;
2239
2240 // Dump virtual bases.
2241 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
2242 E = RD->vbases_end(); I != E; ++I) {
2243 assert(I->isVirtual() && "Found non-virtual class!");
2244 const CXXRecordDecl *VBase =
2245 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
2246
Anders Carlsson3f018712010-10-31 23:45:59 +00002247 CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBase);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002248 DumpCXXRecordLayout(OS, VBase, C, VBaseOffset, IndentLevel,
2249 VBase == PrimaryBase ?
2250 "(primary virtual base)" : "(virtual base)",
2251 /*IncludeVirtualBases=*/false);
2252 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002253
Ken Dyckc8ae5502011-02-09 01:59:34 +00002254 OS << " sizeof=" << Layout.getSize().getQuantity();
Ken Dyckd5090c12011-02-11 02:20:09 +00002255 OS << ", dsize=" << Layout.getDataSize().getQuantity();
Ken Dyck7ad11e72011-02-15 02:32:40 +00002256 OS << ", align=" << Layout.getAlignment().getQuantity() << '\n';
Ken Dyck316d6f62011-02-01 01:52:10 +00002257 OS << " nvsize=" << Layout.getNonVirtualSize().getQuantity();
Ken Dyckbec02852011-02-08 02:02:47 +00002258 OS << ", nvalign=" << Layout.getNonVirtualAlign().getQuantity() << '\n';
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002259 OS << '\n';
2260}
Daniel Dunbarccabe482010-04-19 20:44:53 +00002261
2262void ASTContext::DumpRecordLayout(const RecordDecl *RD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002263 raw_ostream &OS) const {
Daniel Dunbarccabe482010-04-19 20:44:53 +00002264 const ASTRecordLayout &Info = getASTRecordLayout(RD);
2265
2266 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Anders Carlsson3f018712010-10-31 23:45:59 +00002267 return DumpCXXRecordLayout(OS, CXXRD, *this, CharUnits(), 0, 0,
Daniel Dunbarccabe482010-04-19 20:44:53 +00002268 /*IncludeVirtualBases=*/true);
2269
2270 OS << "Type: " << getTypeDeclType(RD).getAsString() << "\n";
2271 OS << "Record: ";
2272 RD->dump();
2273 OS << "\nLayout: ";
2274 OS << "<ASTRecordLayout\n";
Ken Dyckb0fcc592011-02-11 01:54:29 +00002275 OS << " Size:" << toBits(Info.getSize()) << "\n";
Ken Dyckd5090c12011-02-11 02:20:09 +00002276 OS << " DataSize:" << toBits(Info.getDataSize()) << "\n";
Ken Dyck7ad11e72011-02-15 02:32:40 +00002277 OS << " Alignment:" << toBits(Info.getAlignment()) << "\n";
Daniel Dunbarccabe482010-04-19 20:44:53 +00002278 OS << " FieldOffsets: [";
2279 for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i) {
2280 if (i) OS << ", ";
2281 OS << Info.getFieldOffset(i);
2282 }
2283 OS << "]>\n";
2284}