blob: efb1957564b8ec482c36df0f5fd6dcbe9fa5eb0d [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 Friedman43114f92011-10-21 22:49:56 +0000595 /// VFPtrOffset - Virtual function table offset. Only for MS layout.
596 CharUnits VFPtrOffset;
597
Eli Friedman84d2d3a2011-09-27 19:12:27 +0000598 /// VBPtrOffset - Virtual base table offset. Only for MS layout.
599 CharUnits VBPtrOffset;
600
Anders Carlsson22f57202010-10-31 21:01:46 +0000601 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetsMapTy;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000602
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000603 /// Bases - base classes and their offsets in the record.
604 BaseOffsetsMapTy Bases;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000605
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000606 // VBases - virtual base classes and their offsets in the record.
607 BaseOffsetsMapTy VBases;
608
609 /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are
610 /// primary base classes for some other direct or indirect base class.
Anders Carlsson5adde292010-11-24 22:55:48 +0000611 CXXIndirectPrimaryBaseSet IndirectPrimaryBases;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000612
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000613 /// FirstNearlyEmptyVBase - The first nearly empty virtual base class in
614 /// inheritance graph order. Used for determining the primary base class.
615 const CXXRecordDecl *FirstNearlyEmptyVBase;
616
617 /// VisitedVirtualBases - A set of all the visited virtual bases, used to
618 /// avoid visiting virtual bases more than once.
619 llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000620
Jay Foad39c79802011-01-12 09:06:06 +0000621 RecordLayoutBuilder(const ASTContext &Context, EmptySubobjectMap
Eli Friedman84d2d3a2011-09-27 19:12:27 +0000622 *EmptySubobjects, CharUnits Alignment)
Ken Dyck4731d5b2011-02-16 02:05:21 +0000623 : Context(Context), EmptySubobjects(EmptySubobjects), Size(0),
Eli Friedman84d2d3a2011-09-27 19:12:27 +0000624 Alignment(Alignment), UnpackedAlignment(Alignment),
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000625 Packed(false), IsUnion(false),
626 IsMac68kAlign(false), IsMsStruct(false),
Ken Dyck02ced6f2011-02-17 01:49:42 +0000627 UnfilledBitsInLastByte(0), MaxFieldAlignment(CharUnits::Zero()),
628 DataSize(0), NonVirtualSize(CharUnits::Zero()),
Fariborz Jahanianeb397412011-05-02 17:20:56 +0000629 NonVirtualAlignment(CharUnits::One()),
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +0000630 ZeroLengthBitfield(0), PrimaryBase(0),
Eli Friedman43114f92011-10-21 22:49:56 +0000631 PrimaryBaseIsVirtual(false),
632 VFPtrOffset(CharUnits::fromQuantity(-1)),
633 VBPtrOffset(CharUnits::fromQuantity(-1)),
Eli Friedman84d2d3a2011-09-27 19:12:27 +0000634 FirstNearlyEmptyVBase(0) { }
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000635
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000636 void Layout(const RecordDecl *D);
Anders Carlssonc28a6c92010-05-26 15:10:00 +0000637 void Layout(const CXXRecordDecl *D);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000638 void Layout(const ObjCInterfaceDecl *D);
639
640 void LayoutFields(const RecordDecl *D);
641 void LayoutField(const FieldDecl *D);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000642 void LayoutWideBitField(uint64_t FieldSize, uint64_t TypeSize,
643 bool FieldPacked, const FieldDecl *D);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000644 void LayoutBitField(const FieldDecl *D);
Eli Friedman84d2d3a2011-09-27 19:12:27 +0000645 void MSLayoutVirtualBases(const CXXRecordDecl *RD);
646 void MSLayout(const CXXRecordDecl *RD);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000647
Anders Carlssone3c24c72010-05-29 17:35:14 +0000648 /// BaseSubobjectInfoAllocator - Allocator for BaseSubobjectInfo objects.
649 llvm::SpecificBumpPtrAllocator<BaseSubobjectInfo> BaseSubobjectInfoAllocator;
650
651 typedef llvm::DenseMap<const CXXRecordDecl *, BaseSubobjectInfo *>
652 BaseSubobjectInfoMapTy;
653
654 /// VirtualBaseInfo - Map from all the (direct or indirect) virtual bases
655 /// of the class we're laying out to their base subobject info.
656 BaseSubobjectInfoMapTy VirtualBaseInfo;
657
658 /// NonVirtualBaseInfo - Map from all the direct non-virtual bases of the
659 /// class we're laying out to their base subobject info.
660 BaseSubobjectInfoMapTy NonVirtualBaseInfo;
661
662 /// ComputeBaseSubobjectInfo - Compute the base subobject information for the
663 /// bases of the given class.
664 void ComputeBaseSubobjectInfo(const CXXRecordDecl *RD);
665
666 /// ComputeBaseSubobjectInfo - Compute the base subobject information for a
667 /// single class and all of its base classes.
668 BaseSubobjectInfo *ComputeBaseSubobjectInfo(const CXXRecordDecl *RD,
669 bool IsVirtual,
670 BaseSubobjectInfo *Derived);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000671
672 /// DeterminePrimaryBase - Determine the primary base of the given class.
673 void DeterminePrimaryBase(const CXXRecordDecl *RD);
674
675 void SelectPrimaryVBase(const CXXRecordDecl *RD);
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000676
Eli Friedman43114f92011-10-21 22:49:56 +0000677 void EnsureVTablePointerAlignment(CharUnits UnpackedBaseAlign);
Charles Davisc2c576a2010-08-19 00:55:19 +0000678
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000679 /// LayoutNonVirtualBases - Determines the primary base class (if any) and
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000680 /// lays it out. Will then proceed to lay out all non-virtual base clasess.
681 void LayoutNonVirtualBases(const CXXRecordDecl *RD);
682
683 /// LayoutNonVirtualBase - Lays out a single non-virtual base.
Anders Carlssonbb0e6782010-05-29 17:42:25 +0000684 void LayoutNonVirtualBase(const BaseSubobjectInfo *Base);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000685
Anders Carlssona2f8e412010-10-31 22:20:42 +0000686 void AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info,
687 CharUnits Offset);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000688
Eli Friedman5e9534b2011-10-18 00:55:28 +0000689 bool HasNewVirtualFunction(const CXXRecordDecl *RD) const;
690 bool BaseHasVFPtr(const CXXRecordDecl *RD) const;
691
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000692 /// LayoutVirtualBases - Lays out all the virtual bases.
693 void LayoutVirtualBases(const CXXRecordDecl *RD,
694 const CXXRecordDecl *MostDerivedClass);
695
696 /// LayoutVirtualBase - Lays out a single virtual base.
Anders Carlssond6ff5d72010-05-29 17:48:36 +0000697 void LayoutVirtualBase(const BaseSubobjectInfo *Base);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000698
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000699 /// LayoutBase - Will lay out a base and return the offset where it was
Anders Carlssona2f8e412010-10-31 22:20:42 +0000700 /// placed, in chars.
701 CharUnits LayoutBase(const BaseSubobjectInfo *Base);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000702
Anders Carlssonc28a6c92010-05-26 15:10:00 +0000703 /// InitializeLayout - Initialize record layout for the given record decl.
Daniel Dunbar6da10982010-05-27 05:45:51 +0000704 void InitializeLayout(const Decl *D);
Anders Carlssonc28a6c92010-05-26 15:10:00 +0000705
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000706 /// FinishLayout - Finalize record layout. Adjust record size based on the
707 /// alignment.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000708 void FinishLayout(const NamedDecl *D);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000709
Ken Dyck85ef0432011-02-19 18:58:07 +0000710 void UpdateAlignment(CharUnits NewAlignment, CharUnits UnpackedNewAlignment);
711 void UpdateAlignment(CharUnits NewAlignment) {
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000712 UpdateAlignment(NewAlignment, NewAlignment);
713 }
714
715 void CheckFieldPadding(uint64_t Offset, uint64_t UnpaddedOffset,
716 uint64_t UnpackedOffset, unsigned UnpackedAlign,
717 bool isPacked, const FieldDecl *D);
718
719 DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000720
Ken Dyckecfc7552011-02-24 01:13:28 +0000721 CharUnits getSize() const {
Ken Dyck3c215f22011-02-24 01:33:05 +0000722 assert(Size % Context.getCharWidth() == 0);
Ken Dyckecfc7552011-02-24 01:13:28 +0000723 return Context.toCharUnitsFromBits(Size);
724 }
725 uint64_t getSizeInBits() const { return Size; }
726
727 void setSize(CharUnits NewSize) { Size = Context.toBits(NewSize); }
728 void setSize(uint64_t NewSize) { Size = NewSize; }
729
Eli Friedman84d2d3a2011-09-27 19:12:27 +0000730 CharUnits getAligment() const { return Alignment; }
731
Ken Dyckecfc7552011-02-24 01:13:28 +0000732 CharUnits getDataSize() const {
Ken Dyck3c215f22011-02-24 01:33:05 +0000733 assert(DataSize % Context.getCharWidth() == 0);
Ken Dyckecfc7552011-02-24 01:13:28 +0000734 return Context.toCharUnitsFromBits(DataSize);
735 }
736 uint64_t getDataSizeInBits() const { return DataSize; }
737
738 void setDataSize(CharUnits NewSize) { DataSize = Context.toBits(NewSize); }
739 void setDataSize(uint64_t NewSize) { DataSize = NewSize; }
740
Argyrios Kyrtzidis499e6e42010-08-15 10:17:39 +0000741 RecordLayoutBuilder(const RecordLayoutBuilder&); // DO NOT IMPLEMENT
742 void operator=(const RecordLayoutBuilder&); // DO NOT IMPLEMENT
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000743public:
744 static const CXXMethodDecl *ComputeKeyFunction(const CXXRecordDecl *RD);
Argyrios Kyrtzidis3c6cd172010-08-25 00:32:14 +0000745
746 virtual ~RecordLayoutBuilder() { }
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
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000781/// DeterminePrimaryBase - Determine the primary base of the given class.
Anders Carlssonc2226202010-05-26 05:58:59 +0000782void RecordLayoutBuilder::DeterminePrimaryBase(const CXXRecordDecl *RD) {
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000783 // If the class isn't dynamic, it won't have a primary base.
784 if (!RD->isDynamicClass())
785 return;
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000786
Anders Carlsson81430692009-09-22 03:02:06 +0000787 // Compute all the primary virtual bases for all of our direct and
Mike Stump590a7c72009-08-13 23:26:06 +0000788 // indirect bases, and record all their primary virtual base classes.
Anders Carlsson5adde292010-11-24 22:55:48 +0000789 RD->getIndirectPrimaryBases(IndirectPrimaryBases);
Mike Stump590a7c72009-08-13 23:26:06 +0000790
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000791 // If the record has a dynamic base class, attempt to choose a primary base
792 // class. It is the first (in direct base class order) non-virtual dynamic
Anders Carlsson81430692009-09-22 03:02:06 +0000793 // base class, if one exists.
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000794 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000795 e = RD->bases_end(); i != e; ++i) {
Anders Carlsson03ff3792009-11-27 22:05:05 +0000796 // Ignore virtual bases.
797 if (i->isVirtual())
798 continue;
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000799
Anders Carlsson03ff3792009-11-27 22:05:05 +0000800 const CXXRecordDecl *Base =
801 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
802
803 if (Base->isDynamicClass()) {
804 // We found it.
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000805 PrimaryBase = Base;
806 PrimaryBaseIsVirtual = false;
Anders Carlsson03ff3792009-11-27 22:05:05 +0000807 return;
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000808 }
809 }
810
Eli Friedman5e9534b2011-10-18 00:55:28 +0000811 // The Microsoft ABI doesn't have primary virtual bases.
812 if (Context.getTargetInfo().getCXXABI() == CXXABI_Microsoft) {
813 assert(!PrimaryBase && "Should not get here with a primary base!");
814 return;
815 }
816
817 // Under the Itanium ABI, if there is no non-virtual primary base class,
818 // try to compute the primary virtual base. The primary virtual base is
819 // the first nearly empty virtual base that is not an indirect primary
820 // virtual base class, if one exists.
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000821 if (RD->getNumVBases() != 0) {
822 SelectPrimaryVBase(RD);
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000823 if (PrimaryBase)
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000824 return;
825 }
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000826
Eli Friedman5e9534b2011-10-18 00:55:28 +0000827 // Otherwise, it is the first indirect primary base class, if one exists.
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000828 if (FirstNearlyEmptyVBase) {
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000829 PrimaryBase = FirstNearlyEmptyVBase;
830 PrimaryBaseIsVirtual = true;
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000831 return;
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000832 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000833
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000834 assert(!PrimaryBase && "Should not get here with a primary base!");
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000835}
836
Anders Carlssone3c24c72010-05-29 17:35:14 +0000837BaseSubobjectInfo *
838RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD,
839 bool IsVirtual,
840 BaseSubobjectInfo *Derived) {
841 BaseSubobjectInfo *Info;
842
843 if (IsVirtual) {
844 // Check if we already have info about this virtual base.
845 BaseSubobjectInfo *&InfoSlot = VirtualBaseInfo[RD];
846 if (InfoSlot) {
847 assert(InfoSlot->Class == RD && "Wrong class for virtual base info!");
848 return InfoSlot;
849 }
850
851 // We don't, create it.
852 InfoSlot = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo;
853 Info = InfoSlot;
854 } else {
855 Info = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo;
856 }
857
858 Info->Class = RD;
859 Info->IsVirtual = IsVirtual;
860 Info->Derived = 0;
861 Info->PrimaryVirtualBaseInfo = 0;
862
863 const CXXRecordDecl *PrimaryVirtualBase = 0;
864 BaseSubobjectInfo *PrimaryVirtualBaseInfo = 0;
865
866 // Check if this base has a primary virtual base.
867 if (RD->getNumVBases()) {
868 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlsson7f95cd12010-11-24 23:12:57 +0000869 if (Layout.isPrimaryBaseVirtual()) {
Anders Carlssone3c24c72010-05-29 17:35:14 +0000870 // This base does have a primary virtual base.
871 PrimaryVirtualBase = Layout.getPrimaryBase();
872 assert(PrimaryVirtualBase && "Didn't have a primary virtual base!");
873
874 // Now check if we have base subobject info about this primary base.
875 PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase);
876
877 if (PrimaryVirtualBaseInfo) {
878 if (PrimaryVirtualBaseInfo->Derived) {
879 // We did have info about this primary base, and it turns out that it
880 // has already been claimed as a primary virtual base for another
881 // base.
882 PrimaryVirtualBase = 0;
883 } else {
884 // We can claim this base as our primary base.
885 Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo;
886 PrimaryVirtualBaseInfo->Derived = Info;
887 }
888 }
889 }
890 }
891
892 // Now go through all direct bases.
893 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
894 E = RD->bases_end(); I != E; ++I) {
895 bool IsVirtual = I->isVirtual();
896
897 const CXXRecordDecl *BaseDecl =
898 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
899
900 Info->Bases.push_back(ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, Info));
901 }
902
903 if (PrimaryVirtualBase && !PrimaryVirtualBaseInfo) {
904 // Traversing the bases must have created the base info for our primary
905 // virtual base.
906 PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase);
907 assert(PrimaryVirtualBaseInfo &&
908 "Did not create a primary virtual base!");
909
910 // Claim the primary virtual base as our primary virtual base.
911 Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo;
912 PrimaryVirtualBaseInfo->Derived = Info;
913 }
914
915 return Info;
916}
917
918void RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD) {
919 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
920 E = RD->bases_end(); I != E; ++I) {
921 bool IsVirtual = I->isVirtual();
922
923 const CXXRecordDecl *BaseDecl =
924 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
925
926 // Compute the base subobject info for this base.
927 BaseSubobjectInfo *Info = ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, 0);
928
929 if (IsVirtual) {
930 // ComputeBaseInfo has already added this base for us.
931 assert(VirtualBaseInfo.count(BaseDecl) &&
932 "Did not add virtual base!");
933 } else {
934 // Add the base info to the map of non-virtual bases.
935 assert(!NonVirtualBaseInfo.count(BaseDecl) &&
936 "Non-virtual base already exists!");
937 NonVirtualBaseInfo.insert(std::make_pair(BaseDecl, Info));
938 }
939 }
940}
941
Anders Carlsson09ffa322010-03-10 22:21:28 +0000942void
Eli Friedman43114f92011-10-21 22:49:56 +0000943RecordLayoutBuilder::EnsureVTablePointerAlignment(CharUnits UnpackedBaseAlign) {
Eli Friedman5e9534b2011-10-18 00:55:28 +0000944 CharUnits BaseAlign = (Packed) ? CharUnits::One() : UnpackedBaseAlign;
945
946 // The maximum field alignment overrides base align.
947 if (!MaxFieldAlignment.isZero()) {
948 BaseAlign = std::min(BaseAlign, MaxFieldAlignment);
949 UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment);
950 }
951
952 // Round up the current record size to pointer alignment.
Eli Friedman43114f92011-10-21 22:49:56 +0000953 setSize(getSize().RoundUpToAlignment(BaseAlign));
954 setDataSize(getSize());
Eli Friedman5e9534b2011-10-18 00:55:28 +0000955
956 // Update the alignment.
957 UpdateAlignment(BaseAlign, UnpackedBaseAlign);
958}
959
960void
Anders Carlssonc2226202010-05-26 05:58:59 +0000961RecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) {
Anders Carlssone3c24c72010-05-29 17:35:14 +0000962 // Then, determine the primary base class.
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000963 DeterminePrimaryBase(RD);
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000964
Anders Carlssone3c24c72010-05-29 17:35:14 +0000965 // Compute base subobject info.
966 ComputeBaseSubobjectInfo(RD);
967
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000968 // If we have a primary base class, lay it out.
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000969 if (PrimaryBase) {
970 if (PrimaryBaseIsVirtual) {
Anders Carlssone3c24c72010-05-29 17:35:14 +0000971 // If the primary virtual base was a primary virtual base of some other
972 // base class we'll have to steal it.
973 BaseSubobjectInfo *PrimaryBaseInfo = VirtualBaseInfo.lookup(PrimaryBase);
974 PrimaryBaseInfo->Derived = 0;
975
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000976 // We have a virtual primary base, insert it as an indirect primary base.
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000977 IndirectPrimaryBases.insert(PrimaryBase);
Anders Carlssonfe900962010-03-11 05:42:17 +0000978
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000979 assert(!VisitedVirtualBases.count(PrimaryBase) &&
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000980 "vbase already visited!");
981 VisitedVirtualBases.insert(PrimaryBase);
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000982
Anders Carlssond6ff5d72010-05-29 17:48:36 +0000983 LayoutVirtualBase(PrimaryBaseInfo);
Anders Carlssonbb0e6782010-05-29 17:42:25 +0000984 } else {
985 BaseSubobjectInfo *PrimaryBaseInfo =
986 NonVirtualBaseInfo.lookup(PrimaryBase);
987 assert(PrimaryBaseInfo &&
988 "Did not find base info for non-virtual primary base!");
989
990 LayoutNonVirtualBase(PrimaryBaseInfo);
991 }
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000992 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000993
Eli Friedman5e9534b2011-10-18 00:55:28 +0000994 if (Context.getTargetInfo().getCXXABI() != CXXABI_Microsoft &&
995 !PrimaryBase && RD->isDynamicClass()) {
996 // Under the Itanium ABI, a dynamic class without a primary base has a
997 // vtable pointer. It is placed at offset 0.
998 assert(DataSize == 0 && "Vtable pointer must be at offset zero!");
Eli Friedman5e9534b2011-10-18 00:55:28 +0000999 CharUnits PtrWidth =
1000 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
Eli Friedman43114f92011-10-21 22:49:56 +00001001 CharUnits PtrAlign =
1002 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(0));
1003 EnsureVTablePointerAlignment(PtrAlign);
Eli Friedman5e9534b2011-10-18 00:55:28 +00001004 setSize(getSize() + PtrWidth);
1005 setDataSize(getSize());
1006 }
1007
Anders Carlsson8630b5b2010-03-11 00:15:35 +00001008 // Now lay out the non-virtual bases.
1009 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001010 E = RD->bases_end(); I != E; ++I) {
Anders Carlsson8630b5b2010-03-11 00:15:35 +00001011
1012 // Ignore virtual bases.
1013 if (I->isVirtual())
1014 continue;
1015
Anders Carlssonbb0e6782010-05-29 17:42:25 +00001016 const CXXRecordDecl *BaseDecl =
Anders Carlsson8630b5b2010-03-11 00:15:35 +00001017 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1018
1019 // Skip the primary base.
Anders Carlssonbb0e6782010-05-29 17:42:25 +00001020 if (BaseDecl == PrimaryBase && !PrimaryBaseIsVirtual)
Anders Carlsson8630b5b2010-03-11 00:15:35 +00001021 continue;
1022
1023 // Lay out the base.
Anders Carlssonbb0e6782010-05-29 17:42:25 +00001024 BaseSubobjectInfo *BaseInfo = NonVirtualBaseInfo.lookup(BaseDecl);
1025 assert(BaseInfo && "Did not find base info for non-virtual base!");
1026
1027 LayoutNonVirtualBase(BaseInfo);
Anders Carlsson09ffa322010-03-10 22:21:28 +00001028 }
Eli Friedman5e9534b2011-10-18 00:55:28 +00001029
1030 if (Context.getTargetInfo().getCXXABI() == CXXABI_Microsoft) {
1031 // Under the MS ABI, there are separate virtual function table and
1032 // virtual base table pointers. A vfptr is necessary a if a class defines
1033 // a virtual function which is not overriding a function from a base;
1034 // a vbptr is necessary if a class has virtual bases. Either can come
1035 // from a primary base, if it exists. Otherwise, they are placed
1036 // after any base classes.
1037 CharUnits PtrWidth =
1038 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
Eli Friedman43114f92011-10-21 22:49:56 +00001039 CharUnits PtrAlign =
1040 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(0));
1041 PtrAlign = std::max(PtrAlign, Alignment);
Eli Friedman5e9534b2011-10-18 00:55:28 +00001042 if (HasNewVirtualFunction(RD) &&
1043 (!PrimaryBase || !BaseHasVFPtr(PrimaryBase))) {
Eli Friedman43114f92011-10-21 22:49:56 +00001044 EnsureVTablePointerAlignment(PtrAlign);
1045 VFPtrOffset = getSize();
Eli Friedman5e9534b2011-10-18 00:55:28 +00001046 setSize(getSize() + PtrWidth);
1047 setDataSize(getSize());
1048 }
1049 if (RD->getNumVBases() &&
1050 (!PrimaryBase || !PrimaryBase->getNumVBases())) {
Eli Friedman43114f92011-10-21 22:49:56 +00001051 EnsureVTablePointerAlignment(PtrAlign);
Eli Friedman5e9534b2011-10-18 00:55:28 +00001052 VBPtrOffset = getSize();
1053 setSize(getSize() + PtrWidth);
1054 setDataSize(getSize());
1055 }
1056 }
Anders Carlsson09ffa322010-03-10 22:21:28 +00001057}
1058
Anders Carlssonbb0e6782010-05-29 17:42:25 +00001059void RecordLayoutBuilder::LayoutNonVirtualBase(const BaseSubobjectInfo *Base) {
Anders Carlsson0d0b5882010-03-10 22:26:24 +00001060 // Layout the base.
Anders Carlssona2f8e412010-10-31 22:20:42 +00001061 CharUnits Offset = LayoutBase(Base);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001062
Anders Carlsson0d0b5882010-03-10 22:26:24 +00001063 // Add its base class offset.
Anders Carlssonbb0e6782010-05-29 17:42:25 +00001064 assert(!Bases.count(Base->Class) && "base offset already exists!");
Anders Carlssona2f8e412010-10-31 22:20:42 +00001065 Bases.insert(std::make_pair(Base->Class, Offset));
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001066
1067 AddPrimaryVirtualBaseOffsets(Base, Offset);
Anders Carlsson09ffa322010-03-10 22:21:28 +00001068}
Mike Stump2b84dd32009-11-05 04:02:15 +00001069
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001070void
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001071RecordLayoutBuilder::AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info,
Anders Carlssona2f8e412010-10-31 22:20:42 +00001072 CharUnits Offset) {
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001073 // This base isn't interesting, it has no virtual bases.
1074 if (!Info->Class->getNumVBases())
1075 return;
1076
1077 // First, check if we have a virtual primary base to add offsets for.
1078 if (Info->PrimaryVirtualBaseInfo) {
1079 assert(Info->PrimaryVirtualBaseInfo->IsVirtual &&
1080 "Primary virtual base is not virtual!");
1081 if (Info->PrimaryVirtualBaseInfo->Derived == Info) {
1082 // Add the offset.
1083 assert(!VBases.count(Info->PrimaryVirtualBaseInfo->Class) &&
1084 "primary vbase offset already exists!");
1085 VBases.insert(std::make_pair(Info->PrimaryVirtualBaseInfo->Class,
Anders Carlssona2f8e412010-10-31 22:20:42 +00001086 Offset));
Anders Carlssonea7b1822010-04-15 16:12:58 +00001087
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001088 // Traverse the primary virtual base.
1089 AddPrimaryVirtualBaseOffsets(Info->PrimaryVirtualBaseInfo, Offset);
1090 }
Anders Carlssonea7b1822010-04-15 16:12:58 +00001091 }
1092
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001093 // Now go through all direct non-virtual bases.
1094 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class);
1095 for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) {
1096 const BaseSubobjectInfo *Base = Info->Bases[I];
1097 if (Base->IsVirtual)
Anders Carlssonea7b1822010-04-15 16:12:58 +00001098 continue;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001099
Anders Carlsson0a14ee92010-11-01 00:21:58 +00001100 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class);
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001101 AddPrimaryVirtualBaseOffsets(Base, BaseOffset);
Anders Carlssonea7b1822010-04-15 16:12:58 +00001102 }
1103}
1104
Eli Friedman84d2d3a2011-09-27 19:12:27 +00001105bool
1106RecordLayoutBuilder::HasNewVirtualFunction(const CXXRecordDecl *RD) const {
1107 for (CXXRecordDecl::method_iterator method = RD->method_begin();
1108 method != RD->method_end();
1109 ++method) {
1110 if (method->isVirtual() &&
1111 !method->size_overridden_methods()) {
1112 return true;
1113 }
1114 }
1115 return false;
1116}
1117
Eli Friedman5e9534b2011-10-18 00:55:28 +00001118bool
1119RecordLayoutBuilder::BaseHasVFPtr(const CXXRecordDecl *Base) const {
1120 // FIXME: This function is inefficient.
1121 if (HasNewVirtualFunction(Base))
1122 return true;
1123 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base);
1124 if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase())
1125 return BaseHasVFPtr(PrimaryBase);
1126 return false;
Eli Friedman84d2d3a2011-09-27 19:12:27 +00001127}
1128
Anders Carlssonea7b1822010-04-15 16:12:58 +00001129void
Anders Carlssonc2226202010-05-26 05:58:59 +00001130RecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
Anders Carlssonea7b1822010-04-15 16:12:58 +00001131 const CXXRecordDecl *MostDerivedClass) {
Anders Carlssonde710c92010-03-11 04:33:54 +00001132 const CXXRecordDecl *PrimaryBase;
Anders Carlsson291279e2010-04-10 18:42:27 +00001133 bool PrimaryBaseIsVirtual;
Anders Carlssonfe900962010-03-11 05:42:17 +00001134
Anders Carlsson291279e2010-04-10 18:42:27 +00001135 if (MostDerivedClass == RD) {
Anders Carlssond20e7cd2010-05-26 05:20:58 +00001136 PrimaryBase = this->PrimaryBase;
1137 PrimaryBaseIsVirtual = this->PrimaryBaseIsVirtual;
Anders Carlsson291279e2010-04-10 18:42:27 +00001138 } else {
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001139 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlssonde710c92010-03-11 04:33:54 +00001140 PrimaryBase = Layout.getPrimaryBase();
Anders Carlsson7f95cd12010-11-24 23:12:57 +00001141 PrimaryBaseIsVirtual = Layout.isPrimaryBaseVirtual();
Anders Carlsson291279e2010-04-10 18:42:27 +00001142 }
1143
Anders Carlssonf7b7a1e2010-03-11 04:24:02 +00001144 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1145 E = RD->bases_end(); I != E; ++I) {
1146 assert(!I->getType()->isDependentType() &&
Sebastian Redl1054fae2009-10-25 17:03:50 +00001147 "Cannot layout class with dependent bases.");
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001148
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001149 const CXXRecordDecl *BaseDecl =
Anders Carlssonf7b7a1e2010-03-11 04:24:02 +00001150 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1151
1152 if (I->isVirtual()) {
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001153 if (PrimaryBase != BaseDecl || !PrimaryBaseIsVirtual) {
1154 bool IndirectPrimaryBase = IndirectPrimaryBases.count(BaseDecl);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001155
Anders Carlsson291279e2010-04-10 18:42:27 +00001156 // Only lay out the virtual base if it's not an indirect primary base.
1157 if (!IndirectPrimaryBase) {
1158 // Only visit virtual bases once.
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001159 if (!VisitedVirtualBases.insert(BaseDecl))
Anders Carlsson291279e2010-04-10 18:42:27 +00001160 continue;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001161
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001162 const BaseSubobjectInfo *BaseInfo = VirtualBaseInfo.lookup(BaseDecl);
1163 assert(BaseInfo && "Did not find virtual base info!");
1164 LayoutVirtualBase(BaseInfo);
Anders Carlsson6a848892010-03-11 04:10:39 +00001165 }
Mike Stump2b84dd32009-11-05 04:02:15 +00001166 }
Mike Stumpc2f591b2009-08-13 22:53:07 +00001167 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001168
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001169 if (!BaseDecl->getNumVBases()) {
Anders Carlssonf7b7a1e2010-03-11 04:24:02 +00001170 // This base isn't interesting since it doesn't have any virtual bases.
1171 continue;
Mike Stump996576f32009-08-16 19:04:13 +00001172 }
Anders Carlssonf7b7a1e2010-03-11 04:24:02 +00001173
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001174 LayoutVirtualBases(BaseDecl, MostDerivedClass);
Mike Stump6b2556f2009-08-06 13:41:24 +00001175 }
1176}
1177
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001178void RecordLayoutBuilder::LayoutVirtualBase(const BaseSubobjectInfo *Base) {
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001179 assert(!Base->Derived && "Trying to lay out a primary virtual base!");
1180
Anders Carlsson0d0b5882010-03-10 22:26:24 +00001181 // Layout the base.
Anders Carlssona2f8e412010-10-31 22:20:42 +00001182 CharUnits Offset = LayoutBase(Base);
Anders Carlsson0d0b5882010-03-10 22:26:24 +00001183
1184 // Add its base class offset.
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001185 assert(!VBases.count(Base->Class) && "vbase offset already exists!");
Anders Carlssona2f8e412010-10-31 22:20:42 +00001186 VBases.insert(std::make_pair(Base->Class, Offset));
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001187
1188 AddPrimaryVirtualBaseOffsets(Base, Offset);
Anders Carlsson09ffa322010-03-10 22:21:28 +00001189}
1190
Anders Carlssona2f8e412010-10-31 22:20:42 +00001191CharUnits RecordLayoutBuilder::LayoutBase(const BaseSubobjectInfo *Base) {
Anders Carlssond7f3fcf2010-05-29 20:47:33 +00001192 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base->Class);
Anders Carlsson09ffa322010-03-10 22:21:28 +00001193
1194 // If we have an empty base class, try to place it at offset 0.
Anders Carlssond7f3fcf2010-05-29 20:47:33 +00001195 if (Base->Class->isEmpty() &&
Anders Carlsson28466ab2010-10-31 22:13:23 +00001196 EmptySubobjects->CanPlaceBaseAtOffset(Base, CharUnits::Zero())) {
Ken Dyck1b4420e2011-02-28 02:01:38 +00001197 setSize(std::max(getSize(), Layout.getSize()));
Anders Carlsson09ffa322010-03-10 22:21:28 +00001198
Anders Carlssona2f8e412010-10-31 22:20:42 +00001199 return CharUnits::Zero();
Anders Carlsson09ffa322010-03-10 22:21:28 +00001200 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001201
Ken Dyck85ef0432011-02-19 18:58:07 +00001202 CharUnits UnpackedBaseAlign = Layout.getNonVirtualAlign();
1203 CharUnits BaseAlign = (Packed) ? CharUnits::One() : UnpackedBaseAlign;
Argyrios Kyrtzidis8b542742010-12-09 00:35:20 +00001204
1205 // The maximum field alignment overrides base align.
Ken Dyck02ced6f2011-02-17 01:49:42 +00001206 if (!MaxFieldAlignment.isZero()) {
Ken Dyck85ef0432011-02-19 18:58:07 +00001207 BaseAlign = std::min(BaseAlign, MaxFieldAlignment);
1208 UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment);
Argyrios Kyrtzidis8b542742010-12-09 00:35:20 +00001209 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001210
Anders Carlsson09ffa322010-03-10 22:21:28 +00001211 // Round up the current record size to the base's alignment boundary.
Ken Dyck1b4420e2011-02-28 02:01:38 +00001212 CharUnits Offset = getDataSize().RoundUpToAlignment(BaseAlign);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001213
Anders Carlsson09ffa322010-03-10 22:21:28 +00001214 // Try to place the base.
Ken Dyck1b4420e2011-02-28 02:01:38 +00001215 while (!EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset))
1216 Offset += BaseAlign;
Anders Carlsson09ffa322010-03-10 22:21:28 +00001217
Anders Carlssond7f3fcf2010-05-29 20:47:33 +00001218 if (!Base->Class->isEmpty()) {
Anders Carlsson09ffa322010-03-10 22:21:28 +00001219 // Update the data size.
Ken Dyck1b4420e2011-02-28 02:01:38 +00001220 setDataSize(Offset + Layout.getNonVirtualSize());
Anders Carlsson09ffa322010-03-10 22:21:28 +00001221
Ken Dyck1b4420e2011-02-28 02:01:38 +00001222 setSize(std::max(getSize(), getDataSize()));
Anders Carlsson09ffa322010-03-10 22:21:28 +00001223 } else
Ken Dyck1b4420e2011-02-28 02:01:38 +00001224 setSize(std::max(getSize(), Offset + Layout.getSize()));
Anders Carlsson09ffa322010-03-10 22:21:28 +00001225
1226 // Remember max struct/class alignment.
Argyrios Kyrtzidis8b542742010-12-09 00:35:20 +00001227 UpdateAlignment(BaseAlign, UnpackedBaseAlign);
Anders Carlsson09ffa322010-03-10 22:21:28 +00001228
Ken Dyck1b4420e2011-02-28 02:01:38 +00001229 return Offset;
Anders Carlsson09ffa322010-03-10 22:21:28 +00001230}
1231
Daniel Dunbar6da10982010-05-27 05:45:51 +00001232void RecordLayoutBuilder::InitializeLayout(const Decl *D) {
1233 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
1234 IsUnion = RD->isUnion();
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001235
Anders Carlsson28a5fa22009-08-08 19:38:24 +00001236 Packed = D->hasAttr<PackedAttr>();
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001237
1238 IsMsStruct = D->hasAttr<MsStructAttr>();
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001239
Daniel Dunbar096ed292011-10-05 21:04:55 +00001240 // Honor the default struct packing maximum alignment flag.
1241 if (unsigned DefaultMaxFieldAlignment = Context.getLangOptions().PackStruct) {
1242 MaxFieldAlignment = CharUnits::fromQuantity(DefaultMaxFieldAlignment);
1243 }
1244
Daniel Dunbar6da10982010-05-27 05:45:51 +00001245 // mac68k alignment supersedes maximum field alignment and attribute aligned,
1246 // and forces all structures to have 2-byte alignment. The IBM docs on it
1247 // allude to additional (more complicated) semantics, especially with regard
1248 // to bit-fields, but gcc appears not to follow that.
1249 if (D->hasAttr<AlignMac68kAttr>()) {
1250 IsMac68kAlign = true;
Ken Dyck02ced6f2011-02-17 01:49:42 +00001251 MaxFieldAlignment = CharUnits::fromQuantity(2);
Ken Dyck4731d5b2011-02-16 02:05:21 +00001252 Alignment = CharUnits::fromQuantity(2);
Daniel Dunbar6da10982010-05-27 05:45:51 +00001253 } else {
1254 if (const MaxFieldAlignmentAttr *MFAA = D->getAttr<MaxFieldAlignmentAttr>())
Ken Dyck02ced6f2011-02-17 01:49:42 +00001255 MaxFieldAlignment = Context.toCharUnitsFromBits(MFAA->getAlignment());
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001256
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001257 if (unsigned MaxAlign = D->getMaxAlignment())
Ken Dyck85ef0432011-02-19 18:58:07 +00001258 UpdateAlignment(Context.toCharUnitsFromBits(MaxAlign));
Daniel Dunbar6da10982010-05-27 05:45:51 +00001259 }
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001260}
Anders Carlsson6d9f6f32009-07-19 00:18:47 +00001261
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001262void RecordLayoutBuilder::Layout(const RecordDecl *D) {
1263 InitializeLayout(D);
Anders Carlsson118ce162009-07-18 21:48:39 +00001264 LayoutFields(D);
Mike Stump11289f42009-09-09 15:08:12 +00001265
Anders Carlsson79474332009-07-18 20:20:21 +00001266 // Finally, round the size of the total struct up to the alignment of the
1267 // struct itself.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001268 FinishLayout(D);
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001269}
1270
1271void RecordLayoutBuilder::Layout(const CXXRecordDecl *RD) {
Eli Friedman84d2d3a2011-09-27 19:12:27 +00001272 if (Context.getTargetInfo().getCXXABI() == CXXABI_Microsoft) {
Eli Friedman66005df2011-10-18 01:18:41 +00001273 MSLayout(RD);
1274 return;
Eli Friedman84d2d3a2011-09-27 19:12:27 +00001275 }
1276
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001277 InitializeLayout(RD);
1278
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001279 // Lay out the vtable and the non-virtual bases.
1280 LayoutNonVirtualBases(RD);
1281
1282 LayoutFields(RD);
1283
Ken Dycke7380752011-03-10 01:53:59 +00001284 NonVirtualSize = Context.toCharUnitsFromBits(
1285 llvm::RoundUpToAlignment(getSizeInBits(),
Douglas Gregore8bbc122011-09-02 00:18:52 +00001286 Context.getTargetInfo().getCharAlign()));
Ken Dyck4731d5b2011-02-16 02:05:21 +00001287 NonVirtualAlignment = Alignment;
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001288
1289 // Lay out the virtual bases and add the primary virtual base offsets.
1290 LayoutVirtualBases(RD, RD);
1291
1292 VisitedVirtualBases.clear();
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001293
1294 // Finally, round the size of the total struct up to the alignment of the
1295 // struct itself.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001296 FinishLayout(RD);
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001297
Anders Carlsson5b441d72010-04-10 21:24:48 +00001298#ifndef NDEBUG
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001299 // Check that we have base offsets for all bases.
1300 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1301 E = RD->bases_end(); I != E; ++I) {
1302 if (I->isVirtual())
1303 continue;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001304
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001305 const CXXRecordDecl *BaseDecl =
1306 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1307
1308 assert(Bases.count(BaseDecl) && "Did not find base offset!");
1309 }
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001310
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001311 // And all virtual bases.
1312 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
1313 E = RD->vbases_end(); I != E; ++I) {
1314 const CXXRecordDecl *BaseDecl =
1315 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001316
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001317 assert(VBases.count(BaseDecl) && "Did not find base offset!");
Anders Carlsson5b441d72010-04-10 21:24:48 +00001318 }
1319#endif
Anders Carlsson79474332009-07-18 20:20:21 +00001320}
1321
Anders Carlssonc2226202010-05-26 05:58:59 +00001322void RecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D) {
Anders Carlsson4f516282009-07-18 20:50:59 +00001323 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001324 const ASTRecordLayout &SL = Context.getASTObjCInterfaceLayout(SD);
Anders Carlsson4f516282009-07-18 20:50:59 +00001325
Ken Dyck85ef0432011-02-19 18:58:07 +00001326 UpdateAlignment(SL.getAlignment());
Mike Stump11289f42009-09-09 15:08:12 +00001327
Anders Carlsson4f516282009-07-18 20:50:59 +00001328 // We start laying out ivars not at the end of the superclass
1329 // structure, but at the next byte following the last field.
Ken Dyckecfc7552011-02-24 01:13:28 +00001330 setSize(SL.getDataSize());
Ken Dyck1b4420e2011-02-28 02:01:38 +00001331 setDataSize(getSize());
Anders Carlsson4f516282009-07-18 20:50:59 +00001332 }
Mike Stump11289f42009-09-09 15:08:12 +00001333
Daniel Dunbar6da10982010-05-27 05:45:51 +00001334 InitializeLayout(D);
Anders Carlsson4f516282009-07-18 20:50:59 +00001335 // Layout each ivar sequentially.
Jordy Rosea91768e2011-07-22 02:08:32 +00001336 for (const ObjCIvarDecl *IVD = D->all_declared_ivar_begin(); IVD;
1337 IVD = IVD->getNextIvar())
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00001338 LayoutField(IVD);
Mike Stump11289f42009-09-09 15:08:12 +00001339
Anders Carlsson4f516282009-07-18 20:50:59 +00001340 // Finally, round the size of the total struct up to the alignment of the
1341 // struct itself.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001342 FinishLayout(D);
Anders Carlsson4f516282009-07-18 20:50:59 +00001343}
1344
Anders Carlssonc2226202010-05-26 05:58:59 +00001345void RecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
Anders Carlsson118ce162009-07-18 21:48:39 +00001346 // Layout each field, for now, just sequentially, respecting alignment. In
1347 // the future, this will need to be tweakable by targets.
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001348 const FieldDecl *LastFD = 0;
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001349 ZeroLengthBitfield = 0;
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001350 unsigned RemainingInAlignment = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001351 for (RecordDecl::field_iterator Field = D->field_begin(),
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001352 FieldEnd = D->field_end(); Field != FieldEnd; ++Field) {
1353 if (IsMsStruct) {
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001354 FieldDecl *FD = (*Field);
1355 if (Context.ZeroBitfieldFollowsBitfield(FD, LastFD))
1356 ZeroLengthBitfield = FD;
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001357 // Zero-length bitfields following non-bitfield members are
1358 // ignored:
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001359 else if (Context.ZeroBitfieldFollowsNonBitfield(FD, LastFD))
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001360 continue;
Fariborz Jahanian307eace2011-05-06 22:42:22 +00001361 // FIXME. streamline these conditions into a simple one.
Fariborz Jahanianb7a28792011-05-06 21:56:12 +00001362 else if (Context.BitfieldFollowsBitfield(FD, LastFD) ||
Chad Rosierf01a7dd2011-08-04 23:34:15 +00001363 Context.BitfieldFollowsNonBitfield(FD, LastFD) ||
1364 Context.NonBitfieldFollowsBitfield(FD, LastFD)) {
Fariborz Jahanian307eace2011-05-06 22:42:22 +00001365 // 1) Adjacent bit fields are packed into the same 1-, 2-, or
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001366 // 4-byte allocation unit if the integral types are the same
1367 // size and if the next bit field fits into the current
1368 // allocation unit without crossing the boundary imposed by the
1369 // common alignment requirements of the bit fields.
Fariborz Jahanian307eace2011-05-06 22:42:22 +00001370 // 2) Establish a new alignment for a bitfield following
Fariborz Jahanianb7a28792011-05-06 21:56:12 +00001371 // a non-bitfield if size of their types differ.
Fariborz Jahanian307eace2011-05-06 22:42:22 +00001372 // 3) Establish a new alignment for a non-bitfield following
1373 // a bitfield if size of their types differ.
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001374 std::pair<uint64_t, unsigned> FieldInfo =
1375 Context.getTypeInfo(FD->getType());
1376 uint64_t TypeSize = FieldInfo.first;
1377 unsigned FieldAlign = FieldInfo.second;
Fariborz Jahanian7adbed62011-05-09 22:03:17 +00001378 // This check is needed for 'long long' in -m32 mode.
1379 if (TypeSize > FieldAlign)
1380 FieldAlign = TypeSize;
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001381 FieldInfo = Context.getTypeInfo(LastFD->getType());
1382 uint64_t TypeSizeLastFD = FieldInfo.first;
1383 unsigned FieldAlignLastFD = FieldInfo.second;
Fariborz Jahanian7adbed62011-05-09 22:03:17 +00001384 // This check is needed for 'long long' in -m32 mode.
1385 if (TypeSizeLastFD > FieldAlignLastFD)
1386 FieldAlignLastFD = TypeSizeLastFD;
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001387
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001388 if (TypeSizeLastFD != TypeSize) {
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001389 if (RemainingInAlignment &&
1390 LastFD && LastFD->isBitField() &&
Richard Smithcaf33902011-10-10 18:28:20 +00001391 LastFD->getBitWidthValue(Context)) {
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001392 // If previous field was a bitfield with some remaining unfilled
1393 // bits, pad the field so current field starts on its type boundary.
1394 uint64_t FieldOffset =
1395 getDataSizeInBits() - UnfilledBitsInLastByte;
1396 uint64_t NewSizeInBits = RemainingInAlignment + FieldOffset;
1397 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
Douglas Gregore8bbc122011-09-02 00:18:52 +00001398 Context.getTargetInfo().getCharAlign()));
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001399 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
1400 RemainingInAlignment = 0;
1401 }
1402
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001403 uint64_t UnpaddedFieldOffset =
1404 getDataSizeInBits() - UnfilledBitsInLastByte;
1405 FieldAlign = std::max(FieldAlign, FieldAlignLastFD);
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001406
Fariborz Jahanian07ceb0a2011-05-10 19:00:50 +00001407 // The maximum field alignment overrides the aligned attribute.
1408 if (!MaxFieldAlignment.isZero()) {
1409 unsigned MaxFieldAlignmentInBits =
1410 Context.toBits(MaxFieldAlignment);
1411 FieldAlign = std::min(FieldAlign, MaxFieldAlignmentInBits);
1412 }
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001413
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001414 uint64_t NewSizeInBits =
1415 llvm::RoundUpToAlignment(UnpaddedFieldOffset, FieldAlign);
1416 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
Douglas Gregore8bbc122011-09-02 00:18:52 +00001417 Context.getTargetInfo().getCharAlign()));
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001418 UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits;
1419 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
1420 }
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001421 if (FD->isBitField()) {
Richard Smithcaf33902011-10-10 18:28:20 +00001422 uint64_t FieldSize = FD->getBitWidthValue(Context);
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001423 assert (FieldSize > 0 && "LayoutFields - ms_struct layout");
1424 if (RemainingInAlignment < FieldSize)
1425 RemainingInAlignment = TypeSize - FieldSize;
1426 else
1427 RemainingInAlignment -= FieldSize;
1428 }
1429 }
1430 else if (FD->isBitField()) {
Richard Smithcaf33902011-10-10 18:28:20 +00001431 uint64_t FieldSize = FD->getBitWidthValue(Context);
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001432 std::pair<uint64_t, unsigned> FieldInfo =
1433 Context.getTypeInfo(FD->getType());
1434 uint64_t TypeSize = FieldInfo.first;
1435 RemainingInAlignment = TypeSize - FieldSize;
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001436 }
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001437 LastFD = FD;
1438 }
Douglas Gregore8bbc122011-09-02 00:18:52 +00001439 else if (!Context.getTargetInfo().useBitFieldTypeAlignment() &&
1440 Context.getTargetInfo().useZeroLengthBitfieldAlignment()) {
Chad Rosier18903ee2011-08-04 01:21:14 +00001441 FieldDecl *FD = (*Field);
Richard Smithcaf33902011-10-10 18:28:20 +00001442 if (FD->isBitField() && FD->getBitWidthValue(Context) == 0)
Chad Rosier18903ee2011-08-04 01:21:14 +00001443 ZeroLengthBitfield = FD;
Chad Rosier18903ee2011-08-04 01:21:14 +00001444 }
Anders Carlsson118ce162009-07-18 21:48:39 +00001445 LayoutField(*Field);
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001446 }
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001447 if (IsMsStruct && RemainingInAlignment &&
Richard Smithcaf33902011-10-10 18:28:20 +00001448 LastFD && LastFD->isBitField() && LastFD->getBitWidthValue(Context)) {
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001449 // If we ended a bitfield before the full length of the type then
1450 // pad the struct out to the full length of the last type.
1451 uint64_t FieldOffset =
1452 getDataSizeInBits() - UnfilledBitsInLastByte;
1453 uint64_t NewSizeInBits = RemainingInAlignment + FieldOffset;
1454 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
Douglas Gregore8bbc122011-09-02 00:18:52 +00001455 Context.getTargetInfo().getCharAlign()));
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001456 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
1457 }
Anders Carlsson118ce162009-07-18 21:48:39 +00001458}
1459
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001460void RecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize,
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001461 uint64_t TypeSize,
1462 bool FieldPacked,
1463 const FieldDecl *D) {
Anders Carlsson57235162010-04-16 15:57:11 +00001464 assert(Context.getLangOptions().CPlusPlus &&
1465 "Can only have wide bit-fields in C++!");
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001466
Anders Carlsson57235162010-04-16 15:57:11 +00001467 // Itanium C++ ABI 2.4:
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001468 // If sizeof(T)*8 < n, let T' be the largest integral POD type with
Anders Carlsson57235162010-04-16 15:57:11 +00001469 // sizeof(T')*8 <= n.
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001470
Anders Carlsson57235162010-04-16 15:57:11 +00001471 QualType IntegralPODTypes[] = {
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001472 Context.UnsignedCharTy, Context.UnsignedShortTy, Context.UnsignedIntTy,
Anders Carlsson57235162010-04-16 15:57:11 +00001473 Context.UnsignedLongTy, Context.UnsignedLongLongTy
1474 };
1475
Anders Carlsson57235162010-04-16 15:57:11 +00001476 QualType Type;
1477 for (unsigned I = 0, E = llvm::array_lengthof(IntegralPODTypes);
1478 I != E; ++I) {
1479 uint64_t Size = Context.getTypeSize(IntegralPODTypes[I]);
Anders Carlsson57235162010-04-16 15:57:11 +00001480
1481 if (Size > FieldSize)
1482 break;
1483
1484 Type = IntegralPODTypes[I];
1485 }
1486 assert(!Type.isNull() && "Did not find a type!");
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001487
Ken Dyckdbe37f32011-03-01 01:36:00 +00001488 CharUnits TypeAlign = Context.getTypeAlignInChars(Type);
Anders Carlsson57235162010-04-16 15:57:11 +00001489
1490 // We're not going to use any of the unfilled bits in the last byte.
1491 UnfilledBitsInLastByte = 0;
1492
Anders Carlssonaad5fa82010-04-17 20:21:41 +00001493 uint64_t FieldOffset;
Ken Dyckecfc7552011-02-24 01:13:28 +00001494 uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001495
Anders Carlsson57235162010-04-16 15:57:11 +00001496 if (IsUnion) {
Ken Dyckecfc7552011-02-24 01:13:28 +00001497 setDataSize(std::max(getDataSizeInBits(), FieldSize));
Anders Carlssonaad5fa82010-04-17 20:21:41 +00001498 FieldOffset = 0;
Anders Carlsson57235162010-04-16 15:57:11 +00001499 } else {
Chad Rosiere1a6a0e2011-08-05 22:38:04 +00001500 // The bitfield is allocated starting at the next offset aligned
1501 // appropriately for T', with length n bits.
Ken Dyckdbe37f32011-03-01 01:36:00 +00001502 FieldOffset = llvm::RoundUpToAlignment(getDataSizeInBits(),
1503 Context.toBits(TypeAlign));
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001504
Anders Carlsson57235162010-04-16 15:57:11 +00001505 uint64_t NewSizeInBits = FieldOffset + FieldSize;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001506
Ken Dycka1a2e8d2011-03-10 02:00:35 +00001507 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
Douglas Gregore8bbc122011-09-02 00:18:52 +00001508 Context.getTargetInfo().getCharAlign()));
Ken Dyckecfc7552011-02-24 01:13:28 +00001509 UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits;
Anders Carlsson57235162010-04-16 15:57:11 +00001510 }
1511
1512 // Place this field at the current location.
1513 FieldOffsets.push_back(FieldOffset);
1514
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001515 CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, FieldOffset,
Ken Dyckdbe37f32011-03-01 01:36:00 +00001516 Context.toBits(TypeAlign), FieldPacked, D);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001517
Anders Carlsson57235162010-04-16 15:57:11 +00001518 // Update the size.
Ken Dyckecfc7552011-02-24 01:13:28 +00001519 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001520
Anders Carlsson57235162010-04-16 15:57:11 +00001521 // Remember max struct/class alignment.
Ken Dyckdbe37f32011-03-01 01:36:00 +00001522 UpdateAlignment(TypeAlign);
Anders Carlsson57235162010-04-16 15:57:11 +00001523}
1524
Anders Carlssonc2226202010-05-26 05:58:59 +00001525void RecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
Anders Carlsson07209442009-11-22 17:37:31 +00001526 bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
Ken Dyckecfc7552011-02-24 01:13:28 +00001527 uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte;
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001528 uint64_t FieldOffset = IsUnion ? 0 : UnpaddedFieldOffset;
Richard Smithcaf33902011-10-10 18:28:20 +00001529 uint64_t FieldSize = D->getBitWidthValue(Context);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001530
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001531 std::pair<uint64_t, unsigned> FieldInfo = Context.getTypeInfo(D->getType());
Anders Carlsson07209442009-11-22 17:37:31 +00001532 uint64_t TypeSize = FieldInfo.first;
1533 unsigned FieldAlign = FieldInfo.second;
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001534
Fariborz Jahanian7adbed62011-05-09 22:03:17 +00001535 // This check is needed for 'long long' in -m32 mode.
1536 if (IsMsStruct && (TypeSize > FieldAlign))
1537 FieldAlign = TypeSize;
Chad Rosier18903ee2011-08-04 01:21:14 +00001538
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001539 if (ZeroLengthBitfield) {
Chad Rosiere1a6a0e2011-08-05 22:38:04 +00001540 std::pair<uint64_t, unsigned> FieldInfo;
1541 unsigned ZeroLengthBitfieldAlignment;
1542 if (IsMsStruct) {
1543 // If a zero-length bitfield is inserted after a bitfield,
1544 // and the alignment of the zero-length bitfield is
1545 // greater than the member that follows it, `bar', `bar'
1546 // will be aligned as the type of the zero-length bitfield.
1547 if (ZeroLengthBitfield != D) {
1548 FieldInfo = Context.getTypeInfo(ZeroLengthBitfield->getType());
1549 ZeroLengthBitfieldAlignment = FieldInfo.second;
1550 // Ignore alignment of subsequent zero-length bitfields.
1551 if ((ZeroLengthBitfieldAlignment > FieldAlign) || (FieldSize == 0))
1552 FieldAlign = ZeroLengthBitfieldAlignment;
1553 if (FieldSize)
1554 ZeroLengthBitfield = 0;
1555 }
1556 } else {
1557 // The alignment of a zero-length bitfield affects the alignment
1558 // of the next member. The alignment is the max of the zero
1559 // length bitfield's alignment and a target specific fixed value.
1560 unsigned ZeroLengthBitfieldBoundary =
Douglas Gregore8bbc122011-09-02 00:18:52 +00001561 Context.getTargetInfo().getZeroLengthBitfieldBoundary();
Chad Rosiere1a6a0e2011-08-05 22:38:04 +00001562 if (ZeroLengthBitfieldBoundary > FieldAlign)
1563 FieldAlign = ZeroLengthBitfieldBoundary;
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001564 }
1565 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001566
Anders Carlsson57235162010-04-16 15:57:11 +00001567 if (FieldSize > TypeSize) {
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001568 LayoutWideBitField(FieldSize, TypeSize, FieldPacked, D);
Anders Carlsson57235162010-04-16 15:57:11 +00001569 return;
1570 }
1571
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001572 // The align if the field is not packed. This is to check if the attribute
1573 // was unnecessary (-Wpacked).
1574 unsigned UnpackedFieldAlign = FieldAlign;
1575 uint64_t UnpackedFieldOffset = FieldOffset;
Douglas Gregore8bbc122011-09-02 00:18:52 +00001576 if (!Context.getTargetInfo().useBitFieldTypeAlignment() && !ZeroLengthBitfield)
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001577 UnpackedFieldAlign = 1;
1578
Chad Rosiere1a6a0e2011-08-05 22:38:04 +00001579 if (FieldPacked ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00001580 (!Context.getTargetInfo().useBitFieldTypeAlignment() && !ZeroLengthBitfield))
Anders Carlsson07209442009-11-22 17:37:31 +00001581 FieldAlign = 1;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001582 FieldAlign = std::max(FieldAlign, D->getMaxAlignment());
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001583 UnpackedFieldAlign = std::max(UnpackedFieldAlign, D->getMaxAlignment());
Anders Carlsson07209442009-11-22 17:37:31 +00001584
1585 // The maximum field alignment overrides the aligned attribute.
Ken Dyck02ced6f2011-02-17 01:49:42 +00001586 if (!MaxFieldAlignment.isZero()) {
1587 unsigned MaxFieldAlignmentInBits = Context.toBits(MaxFieldAlignment);
1588 FieldAlign = std::min(FieldAlign, MaxFieldAlignmentInBits);
1589 UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignmentInBits);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001590 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001591
Daniel Dunbar3d9289c2010-04-15 06:18:39 +00001592 // Check if we need to add padding to give the field the correct alignment.
Anders Carlsson07209442009-11-22 17:37:31 +00001593 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
Daniel Dunbarf35e7652010-06-29 18:34:35 +00001594 FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001595
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001596 if (FieldSize == 0 ||
1597 (UnpackedFieldOffset & (UnpackedFieldAlign-1)) + FieldSize > TypeSize)
1598 UnpackedFieldOffset = llvm::RoundUpToAlignment(UnpackedFieldOffset,
1599 UnpackedFieldAlign);
1600
Chad Rosiere1a6a0e2011-08-05 22:38:04 +00001601 // Padding members don't affect overall alignment, unless zero length bitfield
1602 // alignment is enabled.
Douglas Gregore8bbc122011-09-02 00:18:52 +00001603 if (!D->getIdentifier() && !Context.getTargetInfo().useZeroLengthBitfieldAlignment())
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001604 FieldAlign = UnpackedFieldAlign = 1;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001605
Chad Rosiere1a6a0e2011-08-05 22:38:04 +00001606 if (!IsMsStruct)
1607 ZeroLengthBitfield = 0;
1608
Anders Carlsson07209442009-11-22 17:37:31 +00001609 // Place this field at the current location.
1610 FieldOffsets.push_back(FieldOffset);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001611
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001612 CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, UnpackedFieldOffset,
1613 UnpackedFieldAlign, FieldPacked, D);
1614
Anders Carlssonba958402009-11-22 19:13:51 +00001615 // Update DataSize to include the last byte containing (part of) the bitfield.
1616 if (IsUnion) {
1617 // FIXME: I think FieldSize should be TypeSize here.
Ken Dyckecfc7552011-02-24 01:13:28 +00001618 setDataSize(std::max(getDataSizeInBits(), FieldSize));
Anders Carlssonba958402009-11-22 19:13:51 +00001619 } else {
1620 uint64_t NewSizeInBits = FieldOffset + FieldSize;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001621
Ken Dycka1a2e8d2011-03-10 02:00:35 +00001622 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
Douglas Gregore8bbc122011-09-02 00:18:52 +00001623 Context.getTargetInfo().getCharAlign()));
Ken Dyckecfc7552011-02-24 01:13:28 +00001624 UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits;
Anders Carlssonba958402009-11-22 19:13:51 +00001625 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001626
Anders Carlssonba958402009-11-22 19:13:51 +00001627 // Update the size.
Ken Dyckecfc7552011-02-24 01:13:28 +00001628 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001629
Anders Carlsson07209442009-11-22 17:37:31 +00001630 // Remember max struct/class alignment.
Ken Dyck85ef0432011-02-19 18:58:07 +00001631 UpdateAlignment(Context.toCharUnitsFromBits(FieldAlign),
1632 Context.toCharUnitsFromBits(UnpackedFieldAlign));
Anders Carlsson07209442009-11-22 17:37:31 +00001633}
1634
Anders Carlssonc2226202010-05-26 05:58:59 +00001635void RecordLayoutBuilder::LayoutField(const FieldDecl *D) {
Anders Carlsson07209442009-11-22 17:37:31 +00001636 if (D->isBitField()) {
1637 LayoutBitField(D);
1638 return;
1639 }
1640
Ken Dyckecfc7552011-02-24 01:13:28 +00001641 uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte;
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001642
Anders Carlssonba958402009-11-22 19:13:51 +00001643 // Reset the unfilled bits.
1644 UnfilledBitsInLastByte = 0;
1645
Anders Carlsson07209442009-11-22 17:37:31 +00001646 bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
Ken Dyck6d90e892011-02-20 02:06:09 +00001647 CharUnits FieldOffset =
Ken Dyckecfc7552011-02-24 01:13:28 +00001648 IsUnion ? CharUnits::Zero() : getDataSize();
Ken Dyck6d90e892011-02-20 02:06:09 +00001649 CharUnits FieldSize;
1650 CharUnits FieldAlign;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001651
Anders Carlsson07209442009-11-22 17:37:31 +00001652 if (D->getType()->isIncompleteArrayType()) {
1653 // This is a flexible array member; we can't directly
1654 // query getTypeInfo about these, so we figure it out here.
1655 // Flexible array members don't have any size, but they
1656 // have to be aligned appropriately for their element type.
Ken Dyck6d90e892011-02-20 02:06:09 +00001657 FieldSize = CharUnits::Zero();
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001658 const ArrayType* ATy = Context.getAsArrayType(D->getType());
Ken Dyck6d90e892011-02-20 02:06:09 +00001659 FieldAlign = Context.getTypeAlignInChars(ATy->getElementType());
Anders Carlsson07209442009-11-22 17:37:31 +00001660 } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) {
1661 unsigned AS = RT->getPointeeType().getAddressSpace();
Ken Dyck6d90e892011-02-20 02:06:09 +00001662 FieldSize =
Douglas Gregore8bbc122011-09-02 00:18:52 +00001663 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(AS));
Ken Dyck6d90e892011-02-20 02:06:09 +00001664 FieldAlign =
Douglas Gregore8bbc122011-09-02 00:18:52 +00001665 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(AS));
Anders Carlsson79474332009-07-18 20:20:21 +00001666 } else {
Ken Dyck6d90e892011-02-20 02:06:09 +00001667 std::pair<CharUnits, CharUnits> FieldInfo =
1668 Context.getTypeInfoInChars(D->getType());
Anders Carlsson07209442009-11-22 17:37:31 +00001669 FieldSize = FieldInfo.first;
1670 FieldAlign = FieldInfo.second;
Chad Rosier18903ee2011-08-04 01:21:14 +00001671
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001672 if (ZeroLengthBitfield) {
Chad Rosier18903ee2011-08-04 01:21:14 +00001673 CharUnits ZeroLengthBitfieldBoundary =
1674 Context.toCharUnitsFromBits(
Douglas Gregore8bbc122011-09-02 00:18:52 +00001675 Context.getTargetInfo().getZeroLengthBitfieldBoundary());
Chad Rosier18903ee2011-08-04 01:21:14 +00001676 if (ZeroLengthBitfieldBoundary == CharUnits::Zero()) {
1677 // If a zero-length bitfield is inserted after a bitfield,
1678 // and the alignment of the zero-length bitfield is
1679 // greater than the member that follows it, `bar', `bar'
1680 // will be aligned as the type of the zero-length bitfield.
1681 std::pair<CharUnits, CharUnits> FieldInfo =
1682 Context.getTypeInfoInChars(ZeroLengthBitfield->getType());
1683 CharUnits ZeroLengthBitfieldAlignment = FieldInfo.second;
1684 if (ZeroLengthBitfieldAlignment > FieldAlign)
1685 FieldAlign = ZeroLengthBitfieldAlignment;
Chad Rosier64b18ee2011-08-04 19:25:14 +00001686 } else if (ZeroLengthBitfieldBoundary > FieldAlign) {
Chad Rosier18903ee2011-08-04 01:21:14 +00001687 // Align 'bar' based on a fixed alignment specified by the target.
Douglas Gregore8bbc122011-09-02 00:18:52 +00001688 assert(Context.getTargetInfo().useZeroLengthBitfieldAlignment() &&
Chad Rosiera336c6f2011-08-04 17:52:43 +00001689 "ZeroLengthBitfieldBoundary should only be used in conjunction"
1690 " with useZeroLengthBitfieldAlignment.");
Chad Rosier18903ee2011-08-04 01:21:14 +00001691 FieldAlign = ZeroLengthBitfieldBoundary;
1692 }
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001693 ZeroLengthBitfield = 0;
1694 }
Douglas Gregordbe39272011-02-01 15:15:22 +00001695
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001696 if (Context.getLangOptions().MSBitfields || IsMsStruct) {
Douglas Gregordbe39272011-02-01 15:15:22 +00001697 // If MS bitfield layout is required, figure out what type is being
1698 // laid out and align the field to the width of that type.
1699
1700 // Resolve all typedefs down to their base type and round up the field
1701 // alignment if necessary.
1702 QualType T = Context.getBaseElementType(D->getType());
1703 if (const BuiltinType *BTy = T->getAs<BuiltinType>()) {
Ken Dyck6d90e892011-02-20 02:06:09 +00001704 CharUnits TypeSize = Context.getTypeSizeInChars(BTy);
Douglas Gregordbe39272011-02-01 15:15:22 +00001705 if (TypeSize > FieldAlign)
1706 FieldAlign = TypeSize;
1707 }
1708 }
Anders Carlsson79474332009-07-18 20:20:21 +00001709 }
Mike Stump11289f42009-09-09 15:08:12 +00001710
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001711 // The align if the field is not packed. This is to check if the attribute
1712 // was unnecessary (-Wpacked).
Ken Dyck6d90e892011-02-20 02:06:09 +00001713 CharUnits UnpackedFieldAlign = FieldAlign;
1714 CharUnits UnpackedFieldOffset = FieldOffset;
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001715
Anders Carlsson07209442009-11-22 17:37:31 +00001716 if (FieldPacked)
Ken Dyck6d90e892011-02-20 02:06:09 +00001717 FieldAlign = CharUnits::One();
1718 CharUnits MaxAlignmentInChars =
1719 Context.toCharUnitsFromBits(D->getMaxAlignment());
1720 FieldAlign = std::max(FieldAlign, MaxAlignmentInChars);
1721 UnpackedFieldAlign = std::max(UnpackedFieldAlign, MaxAlignmentInChars);
Anders Carlsson07209442009-11-22 17:37:31 +00001722
1723 // The maximum field alignment overrides the aligned attribute.
Ken Dyck02ced6f2011-02-17 01:49:42 +00001724 if (!MaxFieldAlignment.isZero()) {
Ken Dyck6d90e892011-02-20 02:06:09 +00001725 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
1726 UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignment);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001727 }
Anders Carlsson07209442009-11-22 17:37:31 +00001728
1729 // Round up the current record size to the field's alignment boundary.
Ken Dyck6d90e892011-02-20 02:06:09 +00001730 FieldOffset = FieldOffset.RoundUpToAlignment(FieldAlign);
1731 UnpackedFieldOffset =
1732 UnpackedFieldOffset.RoundUpToAlignment(UnpackedFieldAlign);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001733
Anders Carlssonb1fcdd02010-05-30 06:52:33 +00001734 if (!IsUnion && EmptySubobjects) {
1735 // Check if we can place the field at this offset.
Ken Dyck6d90e892011-02-20 02:06:09 +00001736 while (!EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset)) {
Anders Carlsson07209442009-11-22 17:37:31 +00001737 // We couldn't place the field at the offset. Try again at a new offset.
1738 FieldOffset += FieldAlign;
1739 }
Anders Carlsson07209442009-11-22 17:37:31 +00001740 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001741
Anders Carlsson79474332009-07-18 20:20:21 +00001742 // Place this field at the current location.
Ken Dyck6d90e892011-02-20 02:06:09 +00001743 FieldOffsets.push_back(Context.toBits(FieldOffset));
Mike Stump11289f42009-09-09 15:08:12 +00001744
Ken Dyck6d90e892011-02-20 02:06:09 +00001745 CheckFieldPadding(Context.toBits(FieldOffset), UnpaddedFieldOffset,
1746 Context.toBits(UnpackedFieldOffset),
1747 Context.toBits(UnpackedFieldAlign), FieldPacked, D);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001748
Anders Carlsson79474332009-07-18 20:20:21 +00001749 // Reserve space for this field.
Daniel Dunbar30cdc702011-02-24 16:40:53 +00001750 uint64_t FieldSizeInBits = Context.toBits(FieldSize);
Anders Carlsson79474332009-07-18 20:20:21 +00001751 if (IsUnion)
Daniel Dunbar30cdc702011-02-24 16:40:53 +00001752 setSize(std::max(getSizeInBits(), FieldSizeInBits));
Anders Carlsson79474332009-07-18 20:20:21 +00001753 else
Ken Dyck1b4420e2011-02-28 02:01:38 +00001754 setSize(FieldOffset + FieldSize);
Mike Stump11289f42009-09-09 15:08:12 +00001755
Anders Carlsson47680d82009-09-26 01:34:51 +00001756 // Update the data size.
Daniel Dunbar30cdc702011-02-24 16:40:53 +00001757 setDataSize(getSizeInBits());
Mike Stump11289f42009-09-09 15:08:12 +00001758
Anders Carlsson79474332009-07-18 20:20:21 +00001759 // Remember max struct/class alignment.
Ken Dyck6d90e892011-02-20 02:06:09 +00001760 UpdateAlignment(FieldAlign, UnpackedFieldAlign);
Anders Carlsson79474332009-07-18 20:20:21 +00001761}
1762
Eli Friedman84d2d3a2011-09-27 19:12:27 +00001763void RecordLayoutBuilder::MSLayoutVirtualBases(const CXXRecordDecl *RD) {
1764
1765 if (!RD->getNumVBases())
1766 return;
1767
1768 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
1769 E = RD->vbases_end(); I != E; ++I) {
1770
1771 const CXXRecordDecl* BaseDecl = I->getType()->getAsCXXRecordDecl();
1772 const BaseSubobjectInfo* BaseInfo = VirtualBaseInfo.lookup(BaseDecl);
1773
1774 assert(BaseInfo && "Did not find virtual base info!");
1775
1776 LayoutVirtualBase(BaseInfo);
1777 }
1778}
1779
1780void RecordLayoutBuilder::MSLayout(const CXXRecordDecl *RD) {
1781
Eli Friedman84d2d3a2011-09-27 19:12:27 +00001782 InitializeLayout(RD);
1783
Eli Friedman5e9534b2011-10-18 00:55:28 +00001784 LayoutNonVirtualBases(RD);
Eli Friedman84d2d3a2011-09-27 19:12:27 +00001785
1786 LayoutFields(RD);
1787
1788 NonVirtualSize = Context.toCharUnitsFromBits(
1789 llvm::RoundUpToAlignment(getSizeInBits(),
1790 Context.getTargetInfo().getCharAlign()));
1791 NonVirtualAlignment = Alignment;
1792
1793 if (NonVirtualSize != NonVirtualSize.RoundUpToAlignment(Alignment)) {
1794 CharUnits AlignMember =
1795 NonVirtualSize.RoundUpToAlignment(Alignment) - NonVirtualSize;
1796
1797 setSize(getSize() + AlignMember);
1798 setDataSize(getSize());
1799
1800 NonVirtualSize = Context.toCharUnitsFromBits(
1801 llvm::RoundUpToAlignment(getSizeInBits(),
1802 Context.getTargetInfo().getCharAlign()));
1803 }
1804
1805 MSLayoutVirtualBases(RD);
1806
1807 VisitedVirtualBases.clear();
1808
1809 // Finally, round the size of the total struct up to the alignment of the
1810 // struct itself.
1811 if (!RD->getNumVBases())
1812 FinishLayout(RD);
1813
1814#ifndef NDEBUG
1815 // Check that we have base offsets for all bases.
1816 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1817 E = RD->bases_end(); I != E; ++I) {
1818 if (I->isVirtual())
1819 continue;
1820
1821 const CXXRecordDecl *BaseDecl =
1822 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1823
1824 assert(Bases.count(BaseDecl) && "Did not find base offset!");
1825 }
1826
1827 // And all virtual bases.
1828 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
1829 E = RD->vbases_end(); I != E; ++I) {
1830 const CXXRecordDecl *BaseDecl =
1831 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1832
1833 assert(VBases.count(BaseDecl) && "Did not find base offset!");
1834 }
1835#endif
1836}
1837
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001838void RecordLayoutBuilder::FinishLayout(const NamedDecl *D) {
Anders Carlsson79474332009-07-18 20:20:21 +00001839 // In C++, records cannot be of size 0.
Ken Dyckecfc7552011-02-24 01:13:28 +00001840 if (Context.getLangOptions().CPlusPlus && getSizeInBits() == 0) {
Fariborz Jahanian09b23312011-02-02 19:36:18 +00001841 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
1842 // Compatibility with gcc requires a class (pod or non-pod)
1843 // which is not empty but of size 0; such as having fields of
1844 // array of zero-length, remains of Size 0
1845 if (RD->isEmpty())
Ken Dyck1b4420e2011-02-28 02:01:38 +00001846 setSize(CharUnits::One());
Fariborz Jahanian09b23312011-02-02 19:36:18 +00001847 }
1848 else
Ken Dyck1b4420e2011-02-28 02:01:38 +00001849 setSize(CharUnits::One());
Fariborz Jahanian09b23312011-02-02 19:36:18 +00001850 }
Anders Carlsson79474332009-07-18 20:20:21 +00001851 // Finally, round the size of the record up to the alignment of the
1852 // record itself.
Ken Dyckecfc7552011-02-24 01:13:28 +00001853 uint64_t UnpaddedSize = getSizeInBits() - UnfilledBitsInLastByte;
Ken Dyck1b4420e2011-02-28 02:01:38 +00001854 uint64_t UnpackedSizeInBits =
Ken Dyckecfc7552011-02-24 01:13:28 +00001855 llvm::RoundUpToAlignment(getSizeInBits(),
1856 Context.toBits(UnpackedAlignment));
Ken Dyck1b4420e2011-02-28 02:01:38 +00001857 CharUnits UnpackedSize = Context.toCharUnitsFromBits(UnpackedSizeInBits);
Ken Dyckecfc7552011-02-24 01:13:28 +00001858 setSize(llvm::RoundUpToAlignment(getSizeInBits(), Context.toBits(Alignment)));
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001859
Douglas Gregore8bbc122011-09-02 00:18:52 +00001860 unsigned CharBitNum = Context.getTargetInfo().getCharWidth();
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001861 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) {
1862 // Warn if padding was introduced to the struct/class/union.
Ken Dyckecfc7552011-02-24 01:13:28 +00001863 if (getSizeInBits() > UnpaddedSize) {
1864 unsigned PadSize = getSizeInBits() - UnpaddedSize;
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001865 bool InBits = true;
1866 if (PadSize % CharBitNum == 0) {
1867 PadSize = PadSize / CharBitNum;
1868 InBits = false;
1869 }
1870 Diag(RD->getLocation(), diag::warn_padded_struct_size)
1871 << Context.getTypeDeclType(RD)
1872 << PadSize
1873 << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not
1874 }
1875
1876 // Warn if we packed it unnecessarily. If the alignment is 1 byte don't
1877 // bother since there won't be alignment issues.
Ken Dyckecfc7552011-02-24 01:13:28 +00001878 if (Packed && UnpackedAlignment > CharUnits::One() &&
Ken Dyck1b4420e2011-02-28 02:01:38 +00001879 getSize() == UnpackedSize)
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001880 Diag(D->getLocation(), diag::warn_unnecessary_packed)
1881 << Context.getTypeDeclType(RD);
1882 }
Anders Carlsson79474332009-07-18 20:20:21 +00001883}
1884
Ken Dyck85ef0432011-02-19 18:58:07 +00001885void RecordLayoutBuilder::UpdateAlignment(CharUnits NewAlignment,
1886 CharUnits UnpackedNewAlignment) {
Daniel Dunbar6da10982010-05-27 05:45:51 +00001887 // The alignment is not modified when using 'mac68k' alignment.
1888 if (IsMac68kAlign)
1889 return;
1890
Ken Dyck85ef0432011-02-19 18:58:07 +00001891 if (NewAlignment > Alignment) {
1892 assert(llvm::isPowerOf2_32(NewAlignment.getQuantity() &&
1893 "Alignment not a power of 2"));
1894 Alignment = NewAlignment;
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001895 }
1896
Ken Dyck85ef0432011-02-19 18:58:07 +00001897 if (UnpackedNewAlignment > UnpackedAlignment) {
1898 assert(llvm::isPowerOf2_32(UnpackedNewAlignment.getQuantity() &&
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001899 "Alignment not a power of 2"));
Ken Dyck85ef0432011-02-19 18:58:07 +00001900 UnpackedAlignment = UnpackedNewAlignment;
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001901 }
1902}
1903
1904void RecordLayoutBuilder::CheckFieldPadding(uint64_t Offset,
1905 uint64_t UnpaddedOffset,
1906 uint64_t UnpackedOffset,
1907 unsigned UnpackedAlign,
1908 bool isPacked,
1909 const FieldDecl *D) {
1910 // We let objc ivars without warning, objc interfaces generally are not used
1911 // for padding tricks.
1912 if (isa<ObjCIvarDecl>(D))
Anders Carlsson79474332009-07-18 20:20:21 +00001913 return;
Mike Stump11289f42009-09-09 15:08:12 +00001914
Ted Kremenekfed48af2011-09-06 19:40:45 +00001915 // Don't warn about structs created without a SourceLocation. This can
1916 // be done by clients of the AST, such as codegen.
1917 if (D->getLocation().isInvalid())
1918 return;
1919
Douglas Gregore8bbc122011-09-02 00:18:52 +00001920 unsigned CharBitNum = Context.getTargetInfo().getCharWidth();
Mike Stump11289f42009-09-09 15:08:12 +00001921
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001922 // Warn if padding was introduced to the struct/class.
1923 if (!IsUnion && Offset > UnpaddedOffset) {
1924 unsigned PadSize = Offset - UnpaddedOffset;
1925 bool InBits = true;
1926 if (PadSize % CharBitNum == 0) {
1927 PadSize = PadSize / CharBitNum;
1928 InBits = false;
1929 }
1930 if (D->getIdentifier())
1931 Diag(D->getLocation(), diag::warn_padded_struct_field)
1932 << (D->getParent()->isStruct() ? 0 : 1) // struct|class
1933 << Context.getTypeDeclType(D->getParent())
1934 << PadSize
1935 << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1) // plural or not
1936 << D->getIdentifier();
1937 else
1938 Diag(D->getLocation(), diag::warn_padded_struct_anon_field)
1939 << (D->getParent()->isStruct() ? 0 : 1) // struct|class
1940 << Context.getTypeDeclType(D->getParent())
1941 << PadSize
1942 << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not
1943 }
1944
1945 // Warn if we packed it unnecessarily. If the alignment is 1 byte don't
1946 // bother since there won't be alignment issues.
1947 if (isPacked && UnpackedAlign > CharBitNum && Offset == UnpackedOffset)
1948 Diag(D->getLocation(), diag::warn_unnecessary_packed)
1949 << D->getIdentifier();
Anders Carlsson79474332009-07-18 20:20:21 +00001950}
Mike Stump11289f42009-09-09 15:08:12 +00001951
Anders Carlsson5ebf8b42009-12-07 04:35:11 +00001952const CXXMethodDecl *
Anders Carlssonc2226202010-05-26 05:58:59 +00001953RecordLayoutBuilder::ComputeKeyFunction(const CXXRecordDecl *RD) {
Daniel Dunbarccabe482010-04-19 20:44:53 +00001954 // If a class isn't polymorphic it doesn't have a key function.
Anders Carlsson5ebf8b42009-12-07 04:35:11 +00001955 if (!RD->isPolymorphic())
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001956 return 0;
Eli Friedmanf2c79b62009-12-08 03:56:49 +00001957
Eli Friedman300f55d2011-06-10 21:53:06 +00001958 // A class that is not externally visible doesn't have a key function. (Or
Eli Friedmanf2c79b62009-12-08 03:56:49 +00001959 // at least, there's no point to assigning a key function to such a class;
1960 // this doesn't affect the ABI.)
Eli Friedman300f55d2011-06-10 21:53:06 +00001961 if (RD->getLinkage() != ExternalLinkage)
Eli Friedmanf2c79b62009-12-08 03:56:49 +00001962 return 0;
1963
Argyrios Kyrtzidis8c64bbe2010-10-13 02:39:41 +00001964 // Template instantiations don't have key functions,see Itanium C++ ABI 5.2.6.
1965 // Same behavior as GCC.
1966 TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
1967 if (TSK == TSK_ImplicitInstantiation ||
1968 TSK == TSK_ExplicitInstantiationDefinition)
1969 return 0;
1970
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001971 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
1972 E = RD->method_end(); I != E; ++I) {
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001973 const CXXMethodDecl *MD = *I;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001974
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001975 if (!MD->isVirtual())
1976 continue;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001977
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001978 if (MD->isPure())
1979 continue;
Eli Friedmanf2c79b62009-12-08 03:56:49 +00001980
Anders Carlssonf98849e2009-12-02 17:15:43 +00001981 // Ignore implicit member functions, they are always marked as inline, but
1982 // they don't have a body until they're defined.
1983 if (MD->isImplicit())
1984 continue;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001985
Douglas Gregora318efd2010-01-05 19:06:31 +00001986 if (MD->isInlineSpecified())
1987 continue;
Eli Friedman71a26d82009-12-06 20:50:05 +00001988
1989 if (MD->hasInlineBody())
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001990 continue;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001991
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001992 // We found it.
1993 return MD;
1994 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001995
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001996 return 0;
1997}
1998
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001999DiagnosticBuilder
2000RecordLayoutBuilder::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00002001 return Context.getDiagnostics().Report(Loc, DiagID);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002002}
2003
Anders Carlssondf291d82010-05-26 04:56:53 +00002004/// getASTRecordLayout - Get or compute information about the layout of the
2005/// specified record (struct/union/class), which indicates its size and field
2006/// position information.
Jay Foad39c79802011-01-12 09:06:06 +00002007const ASTRecordLayout &
2008ASTContext::getASTRecordLayout(const RecordDecl *D) const {
John McCall0710e552011-10-07 02:39:22 +00002009 // These asserts test different things. A record has a definition
2010 // as soon as we begin to parse the definition. That definition is
2011 // not a complete definition (which is what isDefinition() tests)
2012 // until we *finish* parsing the definition.
Anders Carlssondf291d82010-05-26 04:56:53 +00002013 D = D->getDefinition();
2014 assert(D && "Cannot get layout of forward declarations!");
John McCallf937c022011-10-07 06:10:15 +00002015 assert(D->isCompleteDefinition() && "Cannot layout type before complete!");
Anders Carlssondf291d82010-05-26 04:56:53 +00002016
2017 // Look up this layout, if already laid out, return what we have.
2018 // Note that we can't save a reference to the entry because this function
2019 // is recursive.
2020 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
2021 if (Entry) return *Entry;
2022
Anders Carlssond2954862010-05-26 05:10:47 +00002023 const ASTRecordLayout *NewEntry;
2024
2025 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
Anders Carlssonc121b4e2010-05-27 00:07:01 +00002026 EmptySubobjectMap EmptySubobjects(*this, RD);
Anders Carlsson439edd12010-05-27 05:41:06 +00002027
Argyrios Kyrtzidis3c6cd172010-08-25 00:32:14 +00002028 llvm::OwningPtr<RecordLayoutBuilder> Builder;
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002029 CharUnits TargetAlign = CharUnits::One();
2030
2031 Builder.reset(new RecordLayoutBuilder(*this,
2032 &EmptySubobjects,
2033 TargetAlign));
2034
Ted Kremenekf75d0892011-03-19 01:00:36 +00002035 // Recover resources if we crash before exiting this method.
Ted Kremenek022dbc12011-03-22 01:15:19 +00002036 llvm::CrashRecoveryContextCleanupRegistrar<RecordLayoutBuilder>
2037 RecordBuilderCleanup(Builder.get());
Ted Kremenekf75d0892011-03-19 01:00:36 +00002038
Charles Davisc2c576a2010-08-19 00:55:19 +00002039 Builder->Layout(RD);
Anders Carlssond2954862010-05-26 05:10:47 +00002040
Eli Friedman43114f92011-10-21 22:49:56 +00002041 TargetAlign = Builder->NonVirtualAlignment;
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002042
2043 if (getTargetInfo().getCXXABI() == CXXABI_Microsoft &&
2044 TargetAlign.getQuantity() > 4) {
2045 // MSVC rounds the vtable pointer to the struct alignment in what must
2046 // be a multi-pass operation. For now, let the builder figure out the
2047 // alignment and recalculate the layout once its known.
2048 Builder.reset(new RecordLayoutBuilder(*this,
2049 &EmptySubobjects,
2050 TargetAlign));
2051
2052 Builder->Layout(RD);
2053
2054 // Recover resources if we crash before exiting this method.
2055 llvm::CrashRecoveryContextCleanupRegistrar<RecordLayoutBuilder>
2056 RecordBuilderCleanup(Builder.get());
2057 }
2058
Anders Carlssond2954862010-05-26 05:10:47 +00002059 // FIXME: This is not always correct. See the part about bitfields at
2060 // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info.
2061 // FIXME: IsPODForThePurposeOfLayout should be stored in the record layout.
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002062 // This does not affect the calculations of MSVC layouts
2063 bool IsPODForThePurposeOfLayout =
Eli Friedman43114f92011-10-21 22:49:56 +00002064 (getTargetInfo().getCXXABI() != CXXABI_Microsoft) &&
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002065 cast<CXXRecordDecl>(D)->isPOD();
Anders Carlssond2954862010-05-26 05:10:47 +00002066
2067 // FIXME: This should be done in FinalizeLayout.
Ken Dyck1b4420e2011-02-28 02:01:38 +00002068 CharUnits DataSize =
2069 IsPODForThePurposeOfLayout ? Builder->getSize() : Builder->getDataSize();
2070 CharUnits NonVirtualSize =
2071 IsPODForThePurposeOfLayout ? DataSize : Builder->NonVirtualSize;
Anders Carlssond2954862010-05-26 05:10:47 +00002072
Daniel Dunbar592a85c2010-05-27 02:25:46 +00002073 NewEntry =
Ken Dyck1b4420e2011-02-28 02:01:38 +00002074 new (*this) ASTRecordLayout(*this, Builder->getSize(),
Ken Dyck4731d5b2011-02-16 02:05:21 +00002075 Builder->Alignment,
Eli Friedman43114f92011-10-21 22:49:56 +00002076 Builder->VFPtrOffset,
2077 Builder->VBPtrOffset,
Ken Dyck1b4420e2011-02-28 02:01:38 +00002078 DataSize,
Ken Dyckd5090c12011-02-11 02:20:09 +00002079 Builder->FieldOffsets.data(),
Charles Davisc2c576a2010-08-19 00:55:19 +00002080 Builder->FieldOffsets.size(),
Ken Dyckaf1c83f2011-02-16 01:52:01 +00002081 NonVirtualSize,
Ken Dycka2d3dda2011-02-16 01:43:15 +00002082 Builder->NonVirtualAlignment,
Anders Carlssonc121b4e2010-05-27 00:07:01 +00002083 EmptySubobjects.SizeOfLargestEmptySubobject,
Charles Davisc2c576a2010-08-19 00:55:19 +00002084 Builder->PrimaryBase,
2085 Builder->PrimaryBaseIsVirtual,
2086 Builder->Bases, Builder->VBases);
Anders Carlssond2954862010-05-26 05:10:47 +00002087 } else {
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002088 RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0, CharUnits::One());
Anders Carlssond2954862010-05-26 05:10:47 +00002089 Builder.Layout(D);
Daniel Dunbar592a85c2010-05-27 02:25:46 +00002090
Anders Carlssond2954862010-05-26 05:10:47 +00002091 NewEntry =
Ken Dyck1b4420e2011-02-28 02:01:38 +00002092 new (*this) ASTRecordLayout(*this, Builder.getSize(),
Ken Dyck4731d5b2011-02-16 02:05:21 +00002093 Builder.Alignment,
Ken Dyck1b4420e2011-02-28 02:01:38 +00002094 Builder.getSize(),
Anders Carlssond2954862010-05-26 05:10:47 +00002095 Builder.FieldOffsets.data(),
2096 Builder.FieldOffsets.size());
2097 }
2098
Anders Carlssondf291d82010-05-26 04:56:53 +00002099 ASTRecordLayouts[D] = NewEntry;
2100
2101 if (getLangOptions().DumpRecordLayouts) {
2102 llvm::errs() << "\n*** Dumping AST Record Layout\n";
2103 DumpRecordLayout(D, llvm::errs());
2104 }
2105
2106 return *NewEntry;
2107}
2108
2109const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) {
2110 RD = cast<CXXRecordDecl>(RD->getDefinition());
2111 assert(RD && "Cannot get key function for forward declarations!");
Daniel Dunbar592a85c2010-05-27 02:25:46 +00002112
Anders Carlssondf291d82010-05-26 04:56:53 +00002113 const CXXMethodDecl *&Entry = KeyFunctions[RD];
Daniel Dunbar592a85c2010-05-27 02:25:46 +00002114 if (!Entry)
Anders Carlssonc2226202010-05-26 05:58:59 +00002115 Entry = RecordLayoutBuilder::ComputeKeyFunction(RD);
Daniel Dunbar592a85c2010-05-27 02:25:46 +00002116
Anders Carlssondf291d82010-05-26 04:56:53 +00002117 return Entry;
2118}
2119
Eric Christopher8a39a012011-10-05 06:00:51 +00002120/// getObjCLayout - Get or compute information about the layout of the
2121/// given interface.
Anders Carlssondf291d82010-05-26 04:56:53 +00002122///
2123/// \param Impl - If given, also include the layout of the interface's
2124/// implementation. This may differ by including synthesized ivars.
2125const ASTRecordLayout &
2126ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
Jay Foad39c79802011-01-12 09:06:06 +00002127 const ObjCImplementationDecl *Impl) const {
Anders Carlssondf291d82010-05-26 04:56:53 +00002128 assert(!D->isForwardDecl() && "Invalid interface decl!");
2129
2130 // Look up this layout, if already laid out, return what we have.
2131 ObjCContainerDecl *Key =
2132 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
2133 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
2134 return *Entry;
2135
2136 // Add in synthesized ivar count if laying out an implementation.
2137 if (Impl) {
2138 unsigned SynthCount = CountNonClassIvars(D);
2139 // If there aren't any sythesized ivars then reuse the interface
2140 // entry. Note we can't cache this because we simply free all
2141 // entries later; however we shouldn't look up implementations
2142 // frequently.
2143 if (SynthCount == 0)
2144 return getObjCLayout(D, 0);
2145 }
2146
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002147 RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0, CharUnits::One());
Anders Carlsson6ed3a9a2010-05-26 05:04:25 +00002148 Builder.Layout(D);
2149
Anders Carlssondf291d82010-05-26 04:56:53 +00002150 const ASTRecordLayout *NewEntry =
Ken Dyck1b4420e2011-02-28 02:01:38 +00002151 new (*this) ASTRecordLayout(*this, Builder.getSize(),
Ken Dyck4731d5b2011-02-16 02:05:21 +00002152 Builder.Alignment,
Ken Dyck1b4420e2011-02-28 02:01:38 +00002153 Builder.getDataSize(),
Anders Carlsson6ed3a9a2010-05-26 05:04:25 +00002154 Builder.FieldOffsets.data(),
2155 Builder.FieldOffsets.size());
Daniel Dunbar592a85c2010-05-27 02:25:46 +00002156
Anders Carlssondf291d82010-05-26 04:56:53 +00002157 ObjCLayouts[Key] = NewEntry;
2158
2159 return *NewEntry;
2160}
2161
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002162static void PrintOffset(raw_ostream &OS,
Anders Carlsson3f018712010-10-31 23:45:59 +00002163 CharUnits Offset, unsigned IndentLevel) {
Benjamin Kramer96ad7172011-11-05 09:02:52 +00002164 OS << llvm::format("%4" PRId64 " | ", (int64_t)Offset.getQuantity());
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002165 OS.indent(IndentLevel * 2);
2166}
2167
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002168static void DumpCXXRecordLayout(raw_ostream &OS,
Jay Foad39c79802011-01-12 09:06:06 +00002169 const CXXRecordDecl *RD, const ASTContext &C,
Anders Carlsson3f018712010-10-31 23:45:59 +00002170 CharUnits Offset,
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002171 unsigned IndentLevel,
2172 const char* Description,
2173 bool IncludeVirtualBases) {
Anders Carlsson3f018712010-10-31 23:45:59 +00002174 const ASTRecordLayout &Layout = C.getASTRecordLayout(RD);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002175
2176 PrintOffset(OS, Offset, IndentLevel);
Dan Gohman145f3f12010-04-19 16:39:44 +00002177 OS << C.getTypeDeclType(const_cast<CXXRecordDecl *>(RD)).getAsString();
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002178 if (Description)
2179 OS << ' ' << Description;
2180 if (RD->isEmpty())
2181 OS << " (empty)";
2182 OS << '\n';
2183
2184 IndentLevel++;
2185
Anders Carlsson3f018712010-10-31 23:45:59 +00002186 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Eli Friedman43114f92011-10-21 22:49:56 +00002187 bool HasVfptr = Layout.getVFPtrOffset() != CharUnits::fromQuantity(-1);
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002188 bool HasVbptr = Layout.getVBPtrOffset() != CharUnits::fromQuantity(-1);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002189
2190 // Vtable pointer.
Eli Friedman43114f92011-10-21 22:49:56 +00002191 if (RD->isDynamicClass() && !PrimaryBase &&
2192 C.getTargetInfo().getCXXABI() != CXXABI_Microsoft) {
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002193 PrintOffset(OS, Offset, IndentLevel);
Benjamin Kramerb89514a2011-10-14 18:45:37 +00002194 OS << '(' << *RD << " vtable pointer)\n";
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002195 }
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002196
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002197 // Dump (non-virtual) bases
2198 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
2199 E = RD->bases_end(); I != E; ++I) {
2200 assert(!I->getType()->isDependentType() &&
2201 "Cannot layout class with dependent bases.");
2202 if (I->isVirtual())
2203 continue;
2204
2205 const CXXRecordDecl *Base =
2206 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
2207
Anders Carlsson3f018712010-10-31 23:45:59 +00002208 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002209
2210 DumpCXXRecordLayout(OS, Base, C, BaseOffset, IndentLevel,
2211 Base == PrimaryBase ? "(primary base)" : "(base)",
2212 /*IncludeVirtualBases=*/false);
2213 }
Eli Friedman43114f92011-10-21 22:49:56 +00002214
2215 // vfptr and vbptr (for Microsoft C++ ABI)
2216 if (HasVfptr) {
2217 PrintOffset(OS, Offset + Layout.getVFPtrOffset(), IndentLevel);
2218 OS << '(' << *RD << " vftable pointer)\n";
2219 }
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002220 if (HasVbptr) {
2221 PrintOffset(OS, Offset + Layout.getVBPtrOffset(), IndentLevel);
Benjamin Kramerb89514a2011-10-14 18:45:37 +00002222 OS << '(' << *RD << " vbtable pointer)\n";
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002223 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002224
2225 // Dump fields.
2226 uint64_t FieldNo = 0;
2227 for (CXXRecordDecl::field_iterator I = RD->field_begin(),
2228 E = RD->field_end(); I != E; ++I, ++FieldNo) {
2229 const FieldDecl *Field = *I;
Anders Carlsson3f018712010-10-31 23:45:59 +00002230 CharUnits FieldOffset = Offset +
Ken Dyck86a7fcc2011-01-18 01:56:16 +00002231 C.toCharUnitsFromBits(Layout.getFieldOffset(FieldNo));
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002232
2233 if (const RecordType *RT = Field->getType()->getAs<RecordType>()) {
2234 if (const CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
2235 DumpCXXRecordLayout(OS, D, C, FieldOffset, IndentLevel,
Daniel Dunbar56df9772010-08-17 22:39:59 +00002236 Field->getName().data(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002237 /*IncludeVirtualBases=*/true);
2238 continue;
2239 }
2240 }
2241
2242 PrintOffset(OS, FieldOffset, IndentLevel);
Benjamin Kramerb89514a2011-10-14 18:45:37 +00002243 OS << Field->getType().getAsString() << ' ' << *Field << '\n';
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002244 }
2245
2246 if (!IncludeVirtualBases)
2247 return;
2248
2249 // Dump virtual bases.
2250 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
2251 E = RD->vbases_end(); I != E; ++I) {
2252 assert(I->isVirtual() && "Found non-virtual class!");
2253 const CXXRecordDecl *VBase =
2254 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
2255
Anders Carlsson3f018712010-10-31 23:45:59 +00002256 CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBase);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002257 DumpCXXRecordLayout(OS, VBase, C, VBaseOffset, IndentLevel,
2258 VBase == PrimaryBase ?
2259 "(primary virtual base)" : "(virtual base)",
2260 /*IncludeVirtualBases=*/false);
2261 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002262
Ken Dyckc8ae5502011-02-09 01:59:34 +00002263 OS << " sizeof=" << Layout.getSize().getQuantity();
Ken Dyckd5090c12011-02-11 02:20:09 +00002264 OS << ", dsize=" << Layout.getDataSize().getQuantity();
Ken Dyck7ad11e72011-02-15 02:32:40 +00002265 OS << ", align=" << Layout.getAlignment().getQuantity() << '\n';
Ken Dyck316d6f62011-02-01 01:52:10 +00002266 OS << " nvsize=" << Layout.getNonVirtualSize().getQuantity();
Ken Dyckbec02852011-02-08 02:02:47 +00002267 OS << ", nvalign=" << Layout.getNonVirtualAlign().getQuantity() << '\n';
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002268 OS << '\n';
2269}
Daniel Dunbarccabe482010-04-19 20:44:53 +00002270
2271void ASTContext::DumpRecordLayout(const RecordDecl *RD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002272 raw_ostream &OS) const {
Daniel Dunbarccabe482010-04-19 20:44:53 +00002273 const ASTRecordLayout &Info = getASTRecordLayout(RD);
2274
2275 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Anders Carlsson3f018712010-10-31 23:45:59 +00002276 return DumpCXXRecordLayout(OS, CXXRD, *this, CharUnits(), 0, 0,
Daniel Dunbarccabe482010-04-19 20:44:53 +00002277 /*IncludeVirtualBases=*/true);
2278
2279 OS << "Type: " << getTypeDeclType(RD).getAsString() << "\n";
2280 OS << "Record: ";
2281 RD->dump();
2282 OS << "\nLayout: ";
2283 OS << "<ASTRecordLayout\n";
Ken Dyckb0fcc592011-02-11 01:54:29 +00002284 OS << " Size:" << toBits(Info.getSize()) << "\n";
Ken Dyckd5090c12011-02-11 02:20:09 +00002285 OS << " DataSize:" << toBits(Info.getDataSize()) << "\n";
Ken Dyck7ad11e72011-02-15 02:32:40 +00002286 OS << " Alignment:" << toBits(Info.getAlignment()) << "\n";
Daniel Dunbarccabe482010-04-19 20:44:53 +00002287 OS << " FieldOffsets: [";
2288 for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i) {
2289 if (i) OS << ", ";
2290 OS << Info.getFieldOffset(i);
2291 }
2292 OS << "]>\n";
2293}