blob: d6f3d5fea3b57dcbd4d61968b7d8c9c50a21dc8a [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
Douglas Gregore9fc3772012-01-26 07:55:45 +0000561 /// \brief Whether the external AST source has provided a layout for this
562 /// record.
563 unsigned ExternalLayout : 1;
564
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000565 /// Packed - Whether the record is packed or not.
Daniel Dunbar6da10982010-05-27 05:45:51 +0000566 unsigned Packed : 1;
567
568 unsigned IsUnion : 1;
569
570 unsigned IsMac68kAlign : 1;
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000571
572 unsigned IsMsStruct : 1;
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000573
574 /// UnfilledBitsInLastByte - If the last field laid out was a bitfield,
575 /// this contains the number of bits in the last byte that can be used for
576 /// an adjacent bitfield if necessary.
577 unsigned char UnfilledBitsInLastByte;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000578
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000579 /// MaxFieldAlignment - The maximum allowed field alignment. This is set by
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000580 /// #pragma pack.
Ken Dyck02ced6f2011-02-17 01:49:42 +0000581 CharUnits MaxFieldAlignment;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000582
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000583 /// DataSize - The data size of the record being laid out.
584 uint64_t DataSize;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000585
Ken Dyckaf1c83f2011-02-16 01:52:01 +0000586 CharUnits NonVirtualSize;
Ken Dycka2d3dda2011-02-16 01:43:15 +0000587 CharUnits NonVirtualAlignment;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000588
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +0000589 FieldDecl *ZeroLengthBitfield;
Fariborz Jahanianeb397412011-05-02 17:20:56 +0000590
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000591 /// PrimaryBase - the primary base class (if one exists) of the class
592 /// we're laying out.
593 const CXXRecordDecl *PrimaryBase;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000594
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000595 /// PrimaryBaseIsVirtual - Whether the primary base of the class we're laying
596 /// out is virtual.
597 bool PrimaryBaseIsVirtual;
598
Eli Friedman43114f92011-10-21 22:49:56 +0000599 /// VFPtrOffset - Virtual function table offset. Only for MS layout.
600 CharUnits VFPtrOffset;
601
Eli Friedman84d2d3a2011-09-27 19:12:27 +0000602 /// VBPtrOffset - Virtual base table offset. Only for MS layout.
603 CharUnits VBPtrOffset;
604
Anders Carlsson22f57202010-10-31 21:01:46 +0000605 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetsMapTy;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000606
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000607 /// Bases - base classes and their offsets in the record.
608 BaseOffsetsMapTy Bases;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000609
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000610 // VBases - virtual base classes and their offsets in the record.
611 BaseOffsetsMapTy VBases;
612
613 /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are
614 /// primary base classes for some other direct or indirect base class.
Anders Carlsson5adde292010-11-24 22:55:48 +0000615 CXXIndirectPrimaryBaseSet IndirectPrimaryBases;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000616
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000617 /// FirstNearlyEmptyVBase - The first nearly empty virtual base class in
618 /// inheritance graph order. Used for determining the primary base class.
619 const CXXRecordDecl *FirstNearlyEmptyVBase;
620
621 /// VisitedVirtualBases - A set of all the visited virtual bases, used to
622 /// avoid visiting virtual bases more than once.
623 llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000624
Douglas Gregore9fc3772012-01-26 07:55:45 +0000625 /// \brief Externally-provided size.
626 uint64_t ExternalSize;
627
628 /// \brief Externally-provided alignment.
629 uint64_t ExternalAlign;
630
631 /// \brief Externally-provided field offsets.
632 llvm::DenseMap<const FieldDecl *, uint64_t> ExternalFieldOffsets;
633
634 /// \brief Externally-provided direct, non-virtual base offsets.
635 llvm::DenseMap<const CXXRecordDecl *, CharUnits> ExternalBaseOffsets;
636
637 /// \brief Externally-provided virtual base offsets.
638 llvm::DenseMap<const CXXRecordDecl *, CharUnits> ExternalVirtualBaseOffsets;
639
John McCall0153cd32011-11-08 04:01:03 +0000640 RecordLayoutBuilder(const ASTContext &Context,
641 EmptySubobjectMap *EmptySubobjects)
Ken Dyck4731d5b2011-02-16 02:05:21 +0000642 : Context(Context), EmptySubobjects(EmptySubobjects), Size(0),
John McCall0153cd32011-11-08 04:01:03 +0000643 Alignment(CharUnits::One()), UnpackedAlignment(CharUnits::One()),
Douglas Gregore9fc3772012-01-26 07:55:45 +0000644 ExternalLayout(false), Packed(false), IsUnion(false),
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000645 IsMac68kAlign(false), IsMsStruct(false),
Ken Dyck02ced6f2011-02-17 01:49:42 +0000646 UnfilledBitsInLastByte(0), MaxFieldAlignment(CharUnits::Zero()),
647 DataSize(0), NonVirtualSize(CharUnits::Zero()),
Fariborz Jahanianeb397412011-05-02 17:20:56 +0000648 NonVirtualAlignment(CharUnits::One()),
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +0000649 ZeroLengthBitfield(0), PrimaryBase(0),
Eli Friedman43114f92011-10-21 22:49:56 +0000650 PrimaryBaseIsVirtual(false),
651 VFPtrOffset(CharUnits::fromQuantity(-1)),
652 VBPtrOffset(CharUnits::fromQuantity(-1)),
Eli Friedman84d2d3a2011-09-27 19:12:27 +0000653 FirstNearlyEmptyVBase(0) { }
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000654
John McCall0153cd32011-11-08 04:01:03 +0000655 /// Reset this RecordLayoutBuilder to a fresh state, using the given
656 /// alignment as the initial alignment. This is used for the
657 /// correct layout of vb-table pointers in MSVC.
658 void resetWithTargetAlignment(CharUnits TargetAlignment) {
659 const ASTContext &Context = this->Context;
660 EmptySubobjectMap *EmptySubobjects = this->EmptySubobjects;
661 this->~RecordLayoutBuilder();
662 new (this) RecordLayoutBuilder(Context, EmptySubobjects);
663 Alignment = UnpackedAlignment = TargetAlignment;
664 }
665
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000666 void Layout(const RecordDecl *D);
Anders Carlssonc28a6c92010-05-26 15:10:00 +0000667 void Layout(const CXXRecordDecl *D);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000668 void Layout(const ObjCInterfaceDecl *D);
669
670 void LayoutFields(const RecordDecl *D);
671 void LayoutField(const FieldDecl *D);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000672 void LayoutWideBitField(uint64_t FieldSize, uint64_t TypeSize,
673 bool FieldPacked, const FieldDecl *D);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000674 void LayoutBitField(const FieldDecl *D);
John McCall0153cd32011-11-08 04:01:03 +0000675
676 bool isMicrosoftCXXABI() const {
677 return Context.getTargetInfo().getCXXABI() == CXXABI_Microsoft;
678 }
679
Eli Friedman84d2d3a2011-09-27 19:12:27 +0000680 void MSLayoutVirtualBases(const CXXRecordDecl *RD);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000681
Anders Carlssone3c24c72010-05-29 17:35:14 +0000682 /// BaseSubobjectInfoAllocator - Allocator for BaseSubobjectInfo objects.
683 llvm::SpecificBumpPtrAllocator<BaseSubobjectInfo> BaseSubobjectInfoAllocator;
684
685 typedef llvm::DenseMap<const CXXRecordDecl *, BaseSubobjectInfo *>
686 BaseSubobjectInfoMapTy;
687
688 /// VirtualBaseInfo - Map from all the (direct or indirect) virtual bases
689 /// of the class we're laying out to their base subobject info.
690 BaseSubobjectInfoMapTy VirtualBaseInfo;
691
692 /// NonVirtualBaseInfo - Map from all the direct non-virtual bases of the
693 /// class we're laying out to their base subobject info.
694 BaseSubobjectInfoMapTy NonVirtualBaseInfo;
695
696 /// ComputeBaseSubobjectInfo - Compute the base subobject information for the
697 /// bases of the given class.
698 void ComputeBaseSubobjectInfo(const CXXRecordDecl *RD);
699
700 /// ComputeBaseSubobjectInfo - Compute the base subobject information for a
701 /// single class and all of its base classes.
702 BaseSubobjectInfo *ComputeBaseSubobjectInfo(const CXXRecordDecl *RD,
703 bool IsVirtual,
704 BaseSubobjectInfo *Derived);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000705
706 /// DeterminePrimaryBase - Determine the primary base of the given class.
707 void DeterminePrimaryBase(const CXXRecordDecl *RD);
708
709 void SelectPrimaryVBase(const CXXRecordDecl *RD);
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000710
Eli Friedman43114f92011-10-21 22:49:56 +0000711 void EnsureVTablePointerAlignment(CharUnits UnpackedBaseAlign);
Charles Davisc2c576a2010-08-19 00:55:19 +0000712
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000713 /// LayoutNonVirtualBases - Determines the primary base class (if any) and
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000714 /// lays it out. Will then proceed to lay out all non-virtual base clasess.
715 void LayoutNonVirtualBases(const CXXRecordDecl *RD);
716
717 /// LayoutNonVirtualBase - Lays out a single non-virtual base.
Anders Carlssonbb0e6782010-05-29 17:42:25 +0000718 void LayoutNonVirtualBase(const BaseSubobjectInfo *Base);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000719
Anders Carlssona2f8e412010-10-31 22:20:42 +0000720 void AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info,
721 CharUnits Offset);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000722
John McCall0153cd32011-11-08 04:01:03 +0000723 bool needsVFTable(const CXXRecordDecl *RD) const;
724 bool hasNewVirtualFunction(const CXXRecordDecl *RD) const;
725 bool isPossiblePrimaryBase(const CXXRecordDecl *Base) const;
Eli Friedman5e9534b2011-10-18 00:55:28 +0000726
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000727 /// LayoutVirtualBases - Lays out all the virtual bases.
728 void LayoutVirtualBases(const CXXRecordDecl *RD,
729 const CXXRecordDecl *MostDerivedClass);
730
731 /// LayoutVirtualBase - Lays out a single virtual base.
Anders Carlssond6ff5d72010-05-29 17:48:36 +0000732 void LayoutVirtualBase(const BaseSubobjectInfo *Base);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000733
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000734 /// LayoutBase - Will lay out a base and return the offset where it was
Anders Carlssona2f8e412010-10-31 22:20:42 +0000735 /// placed, in chars.
736 CharUnits LayoutBase(const BaseSubobjectInfo *Base);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000737
Anders Carlssonc28a6c92010-05-26 15:10:00 +0000738 /// InitializeLayout - Initialize record layout for the given record decl.
Daniel Dunbar6da10982010-05-27 05:45:51 +0000739 void InitializeLayout(const Decl *D);
Anders Carlssonc28a6c92010-05-26 15:10:00 +0000740
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000741 /// FinishLayout - Finalize record layout. Adjust record size based on the
742 /// alignment.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000743 void FinishLayout(const NamedDecl *D);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000744
Ken Dyck85ef0432011-02-19 18:58:07 +0000745 void UpdateAlignment(CharUnits NewAlignment, CharUnits UnpackedNewAlignment);
746 void UpdateAlignment(CharUnits NewAlignment) {
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000747 UpdateAlignment(NewAlignment, NewAlignment);
748 }
749
750 void CheckFieldPadding(uint64_t Offset, uint64_t UnpaddedOffset,
751 uint64_t UnpackedOffset, unsigned UnpackedAlign,
752 bool isPacked, const FieldDecl *D);
753
754 DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000755
Ken Dyckecfc7552011-02-24 01:13:28 +0000756 CharUnits getSize() const {
Ken Dyck3c215f22011-02-24 01:33:05 +0000757 assert(Size % Context.getCharWidth() == 0);
Ken Dyckecfc7552011-02-24 01:13:28 +0000758 return Context.toCharUnitsFromBits(Size);
759 }
760 uint64_t getSizeInBits() const { return Size; }
761
762 void setSize(CharUnits NewSize) { Size = Context.toBits(NewSize); }
763 void setSize(uint64_t NewSize) { Size = NewSize; }
764
Eli Friedman84d2d3a2011-09-27 19:12:27 +0000765 CharUnits getAligment() const { return Alignment; }
766
Ken Dyckecfc7552011-02-24 01:13:28 +0000767 CharUnits getDataSize() const {
Ken Dyck3c215f22011-02-24 01:33:05 +0000768 assert(DataSize % Context.getCharWidth() == 0);
Ken Dyckecfc7552011-02-24 01:13:28 +0000769 return Context.toCharUnitsFromBits(DataSize);
770 }
771 uint64_t getDataSizeInBits() const { return DataSize; }
772
773 void setDataSize(CharUnits NewSize) { DataSize = Context.toBits(NewSize); }
774 void setDataSize(uint64_t NewSize) { DataSize = NewSize; }
775
Argyrios Kyrtzidis499e6e42010-08-15 10:17:39 +0000776 RecordLayoutBuilder(const RecordLayoutBuilder&); // DO NOT IMPLEMENT
777 void operator=(const RecordLayoutBuilder&); // DO NOT IMPLEMENT
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000778public:
779 static const CXXMethodDecl *ComputeKeyFunction(const CXXRecordDecl *RD);
780};
Benjamin Kramerc7656cd2010-05-26 09:58:31 +0000781} // end anonymous namespace
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000782
Anders Carlsson81430692009-09-22 03:02:06 +0000783void
Anders Carlssonc2226202010-05-26 05:58:59 +0000784RecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD) {
Anders Carlssonf2fa75b2010-03-11 03:39:12 +0000785 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000786 E = RD->bases_end(); I != E; ++I) {
Anders Carlssonf2fa75b2010-03-11 03:39:12 +0000787 assert(!I->getType()->isDependentType() &&
Sebastian Redl1054fae2009-10-25 17:03:50 +0000788 "Cannot layout class with dependent bases.");
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000789
Mike Stump11289f42009-09-09 15:08:12 +0000790 const CXXRecordDecl *Base =
Anders Carlssonf2fa75b2010-03-11 03:39:12 +0000791 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000792
Anders Carlssonf2fa75b2010-03-11 03:39:12 +0000793 // Check if this is a nearly empty virtual base.
Anders Carlsson60a62632010-11-25 01:51:53 +0000794 if (I->isVirtual() && Context.isNearlyEmpty(Base)) {
Anders Carlssonf2fa75b2010-03-11 03:39:12 +0000795 // If it's not an indirect primary base, then we've found our primary
796 // base.
Anders Carlsson81430692009-09-22 03:02:06 +0000797 if (!IndirectPrimaryBases.count(Base)) {
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000798 PrimaryBase = Base;
799 PrimaryBaseIsVirtual = true;
Mike Stump6f3793b2009-08-12 21:50:08 +0000800 return;
801 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000802
Anders Carlssonf2fa75b2010-03-11 03:39:12 +0000803 // Is this the first nearly empty virtual base?
804 if (!FirstNearlyEmptyVBase)
805 FirstNearlyEmptyVBase = Base;
Mike Stump6f3793b2009-08-12 21:50:08 +0000806 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000807
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000808 SelectPrimaryVBase(Base);
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000809 if (PrimaryBase)
Zhongxing Xuec345b72010-02-15 04:28:35 +0000810 return;
Mike Stump6f3793b2009-08-12 21:50:08 +0000811 }
812}
813
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000814/// DeterminePrimaryBase - Determine the primary base of the given class.
Anders Carlssonc2226202010-05-26 05:58:59 +0000815void RecordLayoutBuilder::DeterminePrimaryBase(const CXXRecordDecl *RD) {
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000816 // If the class isn't dynamic, it won't have a primary base.
817 if (!RD->isDynamicClass())
818 return;
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000819
Anders Carlsson81430692009-09-22 03:02:06 +0000820 // Compute all the primary virtual bases for all of our direct and
Mike Stump590a7c72009-08-13 23:26:06 +0000821 // indirect bases, and record all their primary virtual base classes.
Anders Carlsson5adde292010-11-24 22:55:48 +0000822 RD->getIndirectPrimaryBases(IndirectPrimaryBases);
Mike Stump590a7c72009-08-13 23:26:06 +0000823
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000824 // If the record has a dynamic base class, attempt to choose a primary base
825 // class. It is the first (in direct base class order) non-virtual dynamic
Anders Carlsson81430692009-09-22 03:02:06 +0000826 // base class, if one exists.
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000827 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000828 e = RD->bases_end(); i != e; ++i) {
Anders Carlsson03ff3792009-11-27 22:05:05 +0000829 // Ignore virtual bases.
830 if (i->isVirtual())
831 continue;
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000832
Anders Carlsson03ff3792009-11-27 22:05:05 +0000833 const CXXRecordDecl *Base =
834 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
835
John McCall0153cd32011-11-08 04:01:03 +0000836 if (isPossiblePrimaryBase(Base)) {
Anders Carlsson03ff3792009-11-27 22:05:05 +0000837 // We found it.
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000838 PrimaryBase = Base;
839 PrimaryBaseIsVirtual = false;
Anders Carlsson03ff3792009-11-27 22:05:05 +0000840 return;
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000841 }
842 }
843
Eli Friedman5e9534b2011-10-18 00:55:28 +0000844 // The Microsoft ABI doesn't have primary virtual bases.
John McCall0153cd32011-11-08 04:01:03 +0000845 if (isMicrosoftCXXABI()) {
Eli Friedman5e9534b2011-10-18 00:55:28 +0000846 assert(!PrimaryBase && "Should not get here with a primary base!");
847 return;
848 }
849
850 // Under the Itanium ABI, if there is no non-virtual primary base class,
851 // try to compute the primary virtual base. The primary virtual base is
852 // the first nearly empty virtual base that is not an indirect primary
853 // virtual base class, if one exists.
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000854 if (RD->getNumVBases() != 0) {
855 SelectPrimaryVBase(RD);
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000856 if (PrimaryBase)
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000857 return;
858 }
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000859
Eli Friedman5e9534b2011-10-18 00:55:28 +0000860 // Otherwise, it is the first indirect primary base class, if one exists.
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000861 if (FirstNearlyEmptyVBase) {
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000862 PrimaryBase = FirstNearlyEmptyVBase;
863 PrimaryBaseIsVirtual = true;
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000864 return;
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000865 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000866
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000867 assert(!PrimaryBase && "Should not get here with a primary base!");
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000868}
869
Anders Carlssone3c24c72010-05-29 17:35:14 +0000870BaseSubobjectInfo *
871RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD,
872 bool IsVirtual,
873 BaseSubobjectInfo *Derived) {
874 BaseSubobjectInfo *Info;
875
876 if (IsVirtual) {
877 // Check if we already have info about this virtual base.
878 BaseSubobjectInfo *&InfoSlot = VirtualBaseInfo[RD];
879 if (InfoSlot) {
880 assert(InfoSlot->Class == RD && "Wrong class for virtual base info!");
881 return InfoSlot;
882 }
883
884 // We don't, create it.
885 InfoSlot = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo;
886 Info = InfoSlot;
887 } else {
888 Info = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo;
889 }
890
891 Info->Class = RD;
892 Info->IsVirtual = IsVirtual;
893 Info->Derived = 0;
894 Info->PrimaryVirtualBaseInfo = 0;
895
896 const CXXRecordDecl *PrimaryVirtualBase = 0;
897 BaseSubobjectInfo *PrimaryVirtualBaseInfo = 0;
898
899 // Check if this base has a primary virtual base.
900 if (RD->getNumVBases()) {
901 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlsson7f95cd12010-11-24 23:12:57 +0000902 if (Layout.isPrimaryBaseVirtual()) {
Anders Carlssone3c24c72010-05-29 17:35:14 +0000903 // This base does have a primary virtual base.
904 PrimaryVirtualBase = Layout.getPrimaryBase();
905 assert(PrimaryVirtualBase && "Didn't have a primary virtual base!");
906
907 // Now check if we have base subobject info about this primary base.
908 PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase);
909
910 if (PrimaryVirtualBaseInfo) {
911 if (PrimaryVirtualBaseInfo->Derived) {
912 // We did have info about this primary base, and it turns out that it
913 // has already been claimed as a primary virtual base for another
914 // base.
915 PrimaryVirtualBase = 0;
916 } else {
917 // We can claim this base as our primary base.
918 Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo;
919 PrimaryVirtualBaseInfo->Derived = Info;
920 }
921 }
922 }
923 }
924
925 // Now go through all direct bases.
926 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
927 E = RD->bases_end(); I != E; ++I) {
928 bool IsVirtual = I->isVirtual();
929
930 const CXXRecordDecl *BaseDecl =
931 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
932
933 Info->Bases.push_back(ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, Info));
934 }
935
936 if (PrimaryVirtualBase && !PrimaryVirtualBaseInfo) {
937 // Traversing the bases must have created the base info for our primary
938 // virtual base.
939 PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase);
940 assert(PrimaryVirtualBaseInfo &&
941 "Did not create a primary virtual base!");
942
943 // Claim the primary virtual base as our primary virtual base.
944 Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo;
945 PrimaryVirtualBaseInfo->Derived = Info;
946 }
947
948 return Info;
949}
950
951void RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD) {
952 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
953 E = RD->bases_end(); I != E; ++I) {
954 bool IsVirtual = I->isVirtual();
955
956 const CXXRecordDecl *BaseDecl =
957 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
958
959 // Compute the base subobject info for this base.
960 BaseSubobjectInfo *Info = ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, 0);
961
962 if (IsVirtual) {
963 // ComputeBaseInfo has already added this base for us.
964 assert(VirtualBaseInfo.count(BaseDecl) &&
965 "Did not add virtual base!");
966 } else {
967 // Add the base info to the map of non-virtual bases.
968 assert(!NonVirtualBaseInfo.count(BaseDecl) &&
969 "Non-virtual base already exists!");
970 NonVirtualBaseInfo.insert(std::make_pair(BaseDecl, Info));
971 }
972 }
973}
974
Anders Carlsson09ffa322010-03-10 22:21:28 +0000975void
Eli Friedman43114f92011-10-21 22:49:56 +0000976RecordLayoutBuilder::EnsureVTablePointerAlignment(CharUnits UnpackedBaseAlign) {
Eli Friedman5e9534b2011-10-18 00:55:28 +0000977 CharUnits BaseAlign = (Packed) ? CharUnits::One() : UnpackedBaseAlign;
978
979 // The maximum field alignment overrides base align.
980 if (!MaxFieldAlignment.isZero()) {
981 BaseAlign = std::min(BaseAlign, MaxFieldAlignment);
982 UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment);
983 }
984
985 // Round up the current record size to pointer alignment.
Eli Friedman43114f92011-10-21 22:49:56 +0000986 setSize(getSize().RoundUpToAlignment(BaseAlign));
987 setDataSize(getSize());
Eli Friedman5e9534b2011-10-18 00:55:28 +0000988
989 // Update the alignment.
990 UpdateAlignment(BaseAlign, UnpackedBaseAlign);
991}
992
993void
Anders Carlssonc2226202010-05-26 05:58:59 +0000994RecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) {
Anders Carlssone3c24c72010-05-29 17:35:14 +0000995 // Then, determine the primary base class.
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000996 DeterminePrimaryBase(RD);
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000997
Anders Carlssone3c24c72010-05-29 17:35:14 +0000998 // Compute base subobject info.
999 ComputeBaseSubobjectInfo(RD);
1000
Anders Carlsson8630b5b2010-03-11 00:15:35 +00001001 // If we have a primary base class, lay it out.
Anders Carlssond20e7cd2010-05-26 05:20:58 +00001002 if (PrimaryBase) {
1003 if (PrimaryBaseIsVirtual) {
Anders Carlssone3c24c72010-05-29 17:35:14 +00001004 // If the primary virtual base was a primary virtual base of some other
1005 // base class we'll have to steal it.
1006 BaseSubobjectInfo *PrimaryBaseInfo = VirtualBaseInfo.lookup(PrimaryBase);
1007 PrimaryBaseInfo->Derived = 0;
1008
Anders Carlsson8630b5b2010-03-11 00:15:35 +00001009 // We have a virtual primary base, insert it as an indirect primary base.
Anders Carlssond20e7cd2010-05-26 05:20:58 +00001010 IndirectPrimaryBases.insert(PrimaryBase);
Anders Carlssonfe900962010-03-11 05:42:17 +00001011
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001012 assert(!VisitedVirtualBases.count(PrimaryBase) &&
Anders Carlssond20e7cd2010-05-26 05:20:58 +00001013 "vbase already visited!");
1014 VisitedVirtualBases.insert(PrimaryBase);
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001015
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001016 LayoutVirtualBase(PrimaryBaseInfo);
Anders Carlssonbb0e6782010-05-29 17:42:25 +00001017 } else {
1018 BaseSubobjectInfo *PrimaryBaseInfo =
1019 NonVirtualBaseInfo.lookup(PrimaryBase);
1020 assert(PrimaryBaseInfo &&
1021 "Did not find base info for non-virtual primary base!");
1022
1023 LayoutNonVirtualBase(PrimaryBaseInfo);
1024 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001025
John McCall0153cd32011-11-08 04:01:03 +00001026 // If this class needs a vtable/vf-table and didn't get one from a
1027 // primary base, add it in now.
1028 } else if (needsVFTable(RD)) {
Eli Friedman5e9534b2011-10-18 00:55:28 +00001029 assert(DataSize == 0 && "Vtable pointer must be at offset zero!");
Eli Friedman5e9534b2011-10-18 00:55:28 +00001030 CharUnits PtrWidth =
1031 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
Eli Friedman43114f92011-10-21 22:49:56 +00001032 CharUnits PtrAlign =
1033 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(0));
1034 EnsureVTablePointerAlignment(PtrAlign);
John McCall0153cd32011-11-08 04:01:03 +00001035 if (isMicrosoftCXXABI())
1036 VFPtrOffset = getSize();
Eli Friedman5e9534b2011-10-18 00:55:28 +00001037 setSize(getSize() + PtrWidth);
1038 setDataSize(getSize());
1039 }
1040
John McCall0153cd32011-11-08 04:01:03 +00001041 bool HasDirectVirtualBases = false;
1042 bool HasNonVirtualBaseWithVBTable = false;
1043
Anders Carlsson8630b5b2010-03-11 00:15:35 +00001044 // Now lay out the non-virtual bases.
1045 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001046 E = RD->bases_end(); I != E; ++I) {
Anders Carlsson8630b5b2010-03-11 00:15:35 +00001047
John McCall0153cd32011-11-08 04:01:03 +00001048 // Ignore virtual bases, but remember that we saw one.
1049 if (I->isVirtual()) {
1050 HasDirectVirtualBases = true;
Anders Carlsson8630b5b2010-03-11 00:15:35 +00001051 continue;
John McCall0153cd32011-11-08 04:01:03 +00001052 }
Anders Carlsson8630b5b2010-03-11 00:15:35 +00001053
Anders Carlssonbb0e6782010-05-29 17:42:25 +00001054 const CXXRecordDecl *BaseDecl =
John McCall0153cd32011-11-08 04:01:03 +00001055 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
Anders Carlsson8630b5b2010-03-11 00:15:35 +00001056
John McCall0153cd32011-11-08 04:01:03 +00001057 // Remember if this base has virtual bases itself.
1058 if (BaseDecl->getNumVBases())
1059 HasNonVirtualBaseWithVBTable = true;
1060
1061 // Skip the primary base, because we've already laid it out. The
1062 // !PrimaryBaseIsVirtual check is required because we might have a
1063 // non-virtual base of the same type as a primary virtual base.
Anders Carlssonbb0e6782010-05-29 17:42:25 +00001064 if (BaseDecl == PrimaryBase && !PrimaryBaseIsVirtual)
Anders Carlsson8630b5b2010-03-11 00:15:35 +00001065 continue;
1066
1067 // Lay out the base.
Anders Carlssonbb0e6782010-05-29 17:42:25 +00001068 BaseSubobjectInfo *BaseInfo = NonVirtualBaseInfo.lookup(BaseDecl);
1069 assert(BaseInfo && "Did not find base info for non-virtual base!");
1070
1071 LayoutNonVirtualBase(BaseInfo);
Anders Carlsson09ffa322010-03-10 22:21:28 +00001072 }
Eli Friedman5e9534b2011-10-18 00:55:28 +00001073
John McCall0153cd32011-11-08 04:01:03 +00001074 // In the MS ABI, add the vb-table pointer if we need one, which is
1075 // whenever we have a virtual base and we can't re-use a vb-table
1076 // pointer from a non-virtual base.
1077 if (isMicrosoftCXXABI() &&
1078 HasDirectVirtualBases && !HasNonVirtualBaseWithVBTable) {
Eli Friedman5e9534b2011-10-18 00:55:28 +00001079 CharUnits PtrWidth =
1080 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
Eli Friedman43114f92011-10-21 22:49:56 +00001081 CharUnits PtrAlign =
1082 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(0));
John McCall0153cd32011-11-08 04:01:03 +00001083
1084 // MSVC potentially over-aligns the vb-table pointer by giving it
1085 // the max alignment of all the non-virtual objects in the class.
1086 // This is completely unnecessary, but we're not here to pass
1087 // judgment.
1088 //
1089 // Note that we've only laid out the non-virtual bases, so on the
1090 // first pass Alignment won't be set correctly here, but if the
1091 // vb-table doesn't end up aligned correctly we'll come through
1092 // and redo the layout from scratch with the right alignment.
1093 //
1094 // TODO: Instead of doing this, just lay out the fields as if the
1095 // vb-table were at offset zero, then retroactively bump the field
1096 // offsets up.
Eli Friedman43114f92011-10-21 22:49:56 +00001097 PtrAlign = std::max(PtrAlign, Alignment);
John McCall0153cd32011-11-08 04:01:03 +00001098
1099 EnsureVTablePointerAlignment(PtrAlign);
1100 VBPtrOffset = getSize();
1101 setSize(getSize() + PtrWidth);
1102 setDataSize(getSize());
Eli Friedman5e9534b2011-10-18 00:55:28 +00001103 }
Anders Carlsson09ffa322010-03-10 22:21:28 +00001104}
1105
Anders Carlssonbb0e6782010-05-29 17:42:25 +00001106void RecordLayoutBuilder::LayoutNonVirtualBase(const BaseSubobjectInfo *Base) {
Anders Carlsson0d0b5882010-03-10 22:26:24 +00001107 // Layout the base.
Anders Carlssona2f8e412010-10-31 22:20:42 +00001108 CharUnits Offset = LayoutBase(Base);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001109
Anders Carlsson0d0b5882010-03-10 22:26:24 +00001110 // Add its base class offset.
Anders Carlssonbb0e6782010-05-29 17:42:25 +00001111 assert(!Bases.count(Base->Class) && "base offset already exists!");
Anders Carlssona2f8e412010-10-31 22:20:42 +00001112 Bases.insert(std::make_pair(Base->Class, Offset));
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001113
1114 AddPrimaryVirtualBaseOffsets(Base, Offset);
Anders Carlsson09ffa322010-03-10 22:21:28 +00001115}
Mike Stump2b84dd32009-11-05 04:02:15 +00001116
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001117void
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001118RecordLayoutBuilder::AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info,
Anders Carlssona2f8e412010-10-31 22:20:42 +00001119 CharUnits Offset) {
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001120 // This base isn't interesting, it has no virtual bases.
1121 if (!Info->Class->getNumVBases())
1122 return;
1123
1124 // First, check if we have a virtual primary base to add offsets for.
1125 if (Info->PrimaryVirtualBaseInfo) {
1126 assert(Info->PrimaryVirtualBaseInfo->IsVirtual &&
1127 "Primary virtual base is not virtual!");
1128 if (Info->PrimaryVirtualBaseInfo->Derived == Info) {
1129 // Add the offset.
1130 assert(!VBases.count(Info->PrimaryVirtualBaseInfo->Class) &&
1131 "primary vbase offset already exists!");
1132 VBases.insert(std::make_pair(Info->PrimaryVirtualBaseInfo->Class,
Anders Carlssona2f8e412010-10-31 22:20:42 +00001133 Offset));
Anders Carlssonea7b1822010-04-15 16:12:58 +00001134
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001135 // Traverse the primary virtual base.
1136 AddPrimaryVirtualBaseOffsets(Info->PrimaryVirtualBaseInfo, Offset);
1137 }
Anders Carlssonea7b1822010-04-15 16:12:58 +00001138 }
1139
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001140 // Now go through all direct non-virtual bases.
1141 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class);
1142 for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) {
1143 const BaseSubobjectInfo *Base = Info->Bases[I];
1144 if (Base->IsVirtual)
Anders Carlssonea7b1822010-04-15 16:12:58 +00001145 continue;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001146
Anders Carlsson0a14ee92010-11-01 00:21:58 +00001147 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class);
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001148 AddPrimaryVirtualBaseOffsets(Base, BaseOffset);
Anders Carlssonea7b1822010-04-15 16:12:58 +00001149 }
1150}
1151
John McCall0153cd32011-11-08 04:01:03 +00001152/// needsVFTable - Return true if this class needs a vtable or vf-table
1153/// when laid out as a base class. These are treated the same because
1154/// they're both always laid out at offset zero.
1155///
1156/// This function assumes that the class has no primary base.
1157bool RecordLayoutBuilder::needsVFTable(const CXXRecordDecl *RD) const {
1158 assert(!PrimaryBase);
1159
1160 // In the Itanium ABI, every dynamic class needs a vtable: even if
1161 // this class has no virtual functions as a base class (i.e. it's
1162 // non-polymorphic or only has virtual functions from virtual
1163 // bases),x it still needs a vtable to locate its virtual bases.
1164 if (!isMicrosoftCXXABI())
1165 return RD->isDynamicClass();
1166
1167 // In the MS ABI, we need a vfptr if the class has virtual functions
1168 // other than those declared by its virtual bases. The AST doesn't
1169 // tell us that directly, and checking manually for virtual
1170 // functions that aren't overrides is expensive, but there are
1171 // some important shortcuts:
1172
1173 // - Non-polymorphic classes have no virtual functions at all.
1174 if (!RD->isPolymorphic()) return false;
1175
1176 // - Polymorphic classes with no virtual bases must either declare
1177 // virtual functions directly or inherit them, but in the latter
1178 // case we would have a primary base.
1179 if (RD->getNumVBases() == 0) return true;
1180
1181 return hasNewVirtualFunction(RD);
1182}
1183
1184/// hasNewVirtualFunction - Does the given polymorphic class declare a
1185/// virtual function that does not override a method from any of its
1186/// base classes?
Eli Friedman84d2d3a2011-09-27 19:12:27 +00001187bool
John McCall0153cd32011-11-08 04:01:03 +00001188RecordLayoutBuilder::hasNewVirtualFunction(const CXXRecordDecl *RD) const {
1189 assert(RD->isPolymorphic());
1190 if (!RD->getNumBases())
1191 return true;
1192
Eli Friedman84d2d3a2011-09-27 19:12:27 +00001193 for (CXXRecordDecl::method_iterator method = RD->method_begin();
1194 method != RD->method_end();
1195 ++method) {
John McCall0153cd32011-11-08 04:01:03 +00001196 if (method->isVirtual() && !method->size_overridden_methods()) {
Eli Friedman84d2d3a2011-09-27 19:12:27 +00001197 return true;
1198 }
1199 }
1200 return false;
1201}
1202
John McCall0153cd32011-11-08 04:01:03 +00001203/// isPossiblePrimaryBase - Is the given base class an acceptable
1204/// primary base class?
Eli Friedman5e9534b2011-10-18 00:55:28 +00001205bool
John McCall0153cd32011-11-08 04:01:03 +00001206RecordLayoutBuilder::isPossiblePrimaryBase(const CXXRecordDecl *Base) const {
1207 // In the Itanium ABI, a class can be a primary base class if it has
1208 // a vtable for any reason.
1209 if (!isMicrosoftCXXABI())
1210 return Base->isDynamicClass();
1211
1212 // In the MS ABI, a class can only be a primary base class if it
1213 // provides a vf-table at a static offset. That means it has to be
1214 // non-virtual base. The existence of a separate vb-table means
1215 // that it's possible to get virtual functions only from a virtual
1216 // base, which we have to guard against.
1217
1218 // First off, it has to have virtual functions.
1219 if (!Base->isPolymorphic()) return false;
1220
1221 // If it has no virtual bases, then everything is at a static offset.
1222 if (!Base->getNumVBases()) return true;
1223
1224 // Okay, just ask the base class's layout.
1225 return (Context.getASTRecordLayout(Base).getVFPtrOffset()
1226 != CharUnits::fromQuantity(-1));
Eli Friedman84d2d3a2011-09-27 19:12:27 +00001227}
1228
Anders Carlssonea7b1822010-04-15 16:12:58 +00001229void
Anders Carlssonc2226202010-05-26 05:58:59 +00001230RecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
Anders Carlssonea7b1822010-04-15 16:12:58 +00001231 const CXXRecordDecl *MostDerivedClass) {
Anders Carlssonde710c92010-03-11 04:33:54 +00001232 const CXXRecordDecl *PrimaryBase;
Anders Carlsson291279e2010-04-10 18:42:27 +00001233 bool PrimaryBaseIsVirtual;
Anders Carlssonfe900962010-03-11 05:42:17 +00001234
Anders Carlsson291279e2010-04-10 18:42:27 +00001235 if (MostDerivedClass == RD) {
Anders Carlssond20e7cd2010-05-26 05:20:58 +00001236 PrimaryBase = this->PrimaryBase;
1237 PrimaryBaseIsVirtual = this->PrimaryBaseIsVirtual;
Anders Carlsson291279e2010-04-10 18:42:27 +00001238 } else {
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001239 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlssonde710c92010-03-11 04:33:54 +00001240 PrimaryBase = Layout.getPrimaryBase();
Anders Carlsson7f95cd12010-11-24 23:12:57 +00001241 PrimaryBaseIsVirtual = Layout.isPrimaryBaseVirtual();
Anders Carlsson291279e2010-04-10 18:42:27 +00001242 }
1243
Anders Carlssonf7b7a1e2010-03-11 04:24:02 +00001244 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1245 E = RD->bases_end(); I != E; ++I) {
1246 assert(!I->getType()->isDependentType() &&
Sebastian Redl1054fae2009-10-25 17:03:50 +00001247 "Cannot layout class with dependent bases.");
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001248
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001249 const CXXRecordDecl *BaseDecl =
John McCall0153cd32011-11-08 04:01:03 +00001250 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
Anders Carlssonf7b7a1e2010-03-11 04:24:02 +00001251
1252 if (I->isVirtual()) {
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001253 if (PrimaryBase != BaseDecl || !PrimaryBaseIsVirtual) {
1254 bool IndirectPrimaryBase = IndirectPrimaryBases.count(BaseDecl);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001255
Anders Carlsson291279e2010-04-10 18:42:27 +00001256 // Only lay out the virtual base if it's not an indirect primary base.
1257 if (!IndirectPrimaryBase) {
1258 // Only visit virtual bases once.
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001259 if (!VisitedVirtualBases.insert(BaseDecl))
Anders Carlsson291279e2010-04-10 18:42:27 +00001260 continue;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001261
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001262 const BaseSubobjectInfo *BaseInfo = VirtualBaseInfo.lookup(BaseDecl);
1263 assert(BaseInfo && "Did not find virtual base info!");
1264 LayoutVirtualBase(BaseInfo);
Anders Carlsson6a848892010-03-11 04:10:39 +00001265 }
Mike Stump2b84dd32009-11-05 04:02:15 +00001266 }
Mike Stumpc2f591b2009-08-13 22:53:07 +00001267 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001268
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001269 if (!BaseDecl->getNumVBases()) {
Anders Carlssonf7b7a1e2010-03-11 04:24:02 +00001270 // This base isn't interesting since it doesn't have any virtual bases.
1271 continue;
Mike Stump996576f32009-08-16 19:04:13 +00001272 }
Anders Carlssonf7b7a1e2010-03-11 04:24:02 +00001273
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001274 LayoutVirtualBases(BaseDecl, MostDerivedClass);
Mike Stump6b2556f2009-08-06 13:41:24 +00001275 }
1276}
1277
John McCall0153cd32011-11-08 04:01:03 +00001278void RecordLayoutBuilder::MSLayoutVirtualBases(const CXXRecordDecl *RD) {
1279
1280 if (!RD->getNumVBases())
1281 return;
1282
1283 // This is substantially simplified because there are no virtual
1284 // primary bases.
1285 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
1286 E = RD->vbases_end(); I != E; ++I) {
1287 const CXXRecordDecl *BaseDecl = I->getType()->getAsCXXRecordDecl();
1288 const BaseSubobjectInfo *BaseInfo = VirtualBaseInfo.lookup(BaseDecl);
1289 assert(BaseInfo && "Did not find virtual base info!");
1290
1291 LayoutVirtualBase(BaseInfo);
1292 }
1293}
1294
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001295void RecordLayoutBuilder::LayoutVirtualBase(const BaseSubobjectInfo *Base) {
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001296 assert(!Base->Derived && "Trying to lay out a primary virtual base!");
1297
Anders Carlsson0d0b5882010-03-10 22:26:24 +00001298 // Layout the base.
Anders Carlssona2f8e412010-10-31 22:20:42 +00001299 CharUnits Offset = LayoutBase(Base);
Anders Carlsson0d0b5882010-03-10 22:26:24 +00001300
1301 // Add its base class offset.
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001302 assert(!VBases.count(Base->Class) && "vbase offset already exists!");
Anders Carlssona2f8e412010-10-31 22:20:42 +00001303 VBases.insert(std::make_pair(Base->Class, Offset));
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001304
1305 AddPrimaryVirtualBaseOffsets(Base, Offset);
Anders Carlsson09ffa322010-03-10 22:21:28 +00001306}
1307
Anders Carlssona2f8e412010-10-31 22:20:42 +00001308CharUnits RecordLayoutBuilder::LayoutBase(const BaseSubobjectInfo *Base) {
Anders Carlssond7f3fcf2010-05-29 20:47:33 +00001309 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base->Class);
Anders Carlsson09ffa322010-03-10 22:21:28 +00001310
Douglas Gregore9fc3772012-01-26 07:55:45 +00001311
1312 CharUnits Offset;
1313
1314 // Query the external layout to see if it provides an offset.
1315 bool HasExternalLayout = false;
1316 if (ExternalLayout) {
1317 llvm::DenseMap<const CXXRecordDecl *, CharUnits>::iterator Known;
1318 if (Base->IsVirtual) {
1319 Known = ExternalVirtualBaseOffsets.find(Base->Class);
1320 if (Known != ExternalVirtualBaseOffsets.end()) {
1321 Offset = Known->second;
1322 HasExternalLayout = true;
1323 }
1324 } else {
1325 Known = ExternalBaseOffsets.find(Base->Class);
1326 if (Known != ExternalBaseOffsets.end()) {
1327 Offset = Known->second;
1328 HasExternalLayout = true;
1329 }
1330 }
1331 }
1332
Anders Carlsson09ffa322010-03-10 22:21:28 +00001333 // If we have an empty base class, try to place it at offset 0.
Anders Carlssond7f3fcf2010-05-29 20:47:33 +00001334 if (Base->Class->isEmpty() &&
Douglas Gregore9fc3772012-01-26 07:55:45 +00001335 (!HasExternalLayout || Offset == CharUnits::Zero()) &&
Anders Carlsson28466ab2010-10-31 22:13:23 +00001336 EmptySubobjects->CanPlaceBaseAtOffset(Base, CharUnits::Zero())) {
Ken Dyck1b4420e2011-02-28 02:01:38 +00001337 setSize(std::max(getSize(), Layout.getSize()));
Anders Carlsson09ffa322010-03-10 22:21:28 +00001338
Anders Carlssona2f8e412010-10-31 22:20:42 +00001339 return CharUnits::Zero();
Anders Carlsson09ffa322010-03-10 22:21:28 +00001340 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001341
Ken Dyck85ef0432011-02-19 18:58:07 +00001342 CharUnits UnpackedBaseAlign = Layout.getNonVirtualAlign();
1343 CharUnits BaseAlign = (Packed) ? CharUnits::One() : UnpackedBaseAlign;
Argyrios Kyrtzidis8b542742010-12-09 00:35:20 +00001344
1345 // The maximum field alignment overrides base align.
Ken Dyck02ced6f2011-02-17 01:49:42 +00001346 if (!MaxFieldAlignment.isZero()) {
Ken Dyck85ef0432011-02-19 18:58:07 +00001347 BaseAlign = std::min(BaseAlign, MaxFieldAlignment);
1348 UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment);
Argyrios Kyrtzidis8b542742010-12-09 00:35:20 +00001349 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001350
Douglas Gregore9fc3772012-01-26 07:55:45 +00001351 if (!HasExternalLayout) {
1352 // Round up the current record size to the base's alignment boundary.
1353 Offset = getDataSize().RoundUpToAlignment(BaseAlign);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001354
Douglas Gregore9fc3772012-01-26 07:55:45 +00001355 // Try to place the base.
1356 while (!EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset))
1357 Offset += BaseAlign;
1358 } else {
1359 bool Allowed = EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset);
1360 (void)Allowed;
1361 assert(Allowed && "Base subobject externally placed at overlapping offset");
1362 }
1363
Anders Carlssond7f3fcf2010-05-29 20:47:33 +00001364 if (!Base->Class->isEmpty()) {
Anders Carlsson09ffa322010-03-10 22:21:28 +00001365 // Update the data size.
Ken Dyck1b4420e2011-02-28 02:01:38 +00001366 setDataSize(Offset + Layout.getNonVirtualSize());
Anders Carlsson09ffa322010-03-10 22:21:28 +00001367
Ken Dyck1b4420e2011-02-28 02:01:38 +00001368 setSize(std::max(getSize(), getDataSize()));
Anders Carlsson09ffa322010-03-10 22:21:28 +00001369 } else
Ken Dyck1b4420e2011-02-28 02:01:38 +00001370 setSize(std::max(getSize(), Offset + Layout.getSize()));
Anders Carlsson09ffa322010-03-10 22:21:28 +00001371
1372 // Remember max struct/class alignment.
Argyrios Kyrtzidis8b542742010-12-09 00:35:20 +00001373 UpdateAlignment(BaseAlign, UnpackedBaseAlign);
Anders Carlsson09ffa322010-03-10 22:21:28 +00001374
Ken Dyck1b4420e2011-02-28 02:01:38 +00001375 return Offset;
Anders Carlsson09ffa322010-03-10 22:21:28 +00001376}
1377
Daniel Dunbar6da10982010-05-27 05:45:51 +00001378void RecordLayoutBuilder::InitializeLayout(const Decl *D) {
1379 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
1380 IsUnion = RD->isUnion();
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001381
Anders Carlsson28a5fa22009-08-08 19:38:24 +00001382 Packed = D->hasAttr<PackedAttr>();
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001383
1384 IsMsStruct = D->hasAttr<MsStructAttr>();
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001385
Daniel Dunbar096ed292011-10-05 21:04:55 +00001386 // Honor the default struct packing maximum alignment flag.
1387 if (unsigned DefaultMaxFieldAlignment = Context.getLangOptions().PackStruct) {
1388 MaxFieldAlignment = CharUnits::fromQuantity(DefaultMaxFieldAlignment);
1389 }
1390
Daniel Dunbar6da10982010-05-27 05:45:51 +00001391 // mac68k alignment supersedes maximum field alignment and attribute aligned,
1392 // and forces all structures to have 2-byte alignment. The IBM docs on it
1393 // allude to additional (more complicated) semantics, especially with regard
1394 // to bit-fields, but gcc appears not to follow that.
1395 if (D->hasAttr<AlignMac68kAttr>()) {
1396 IsMac68kAlign = true;
Ken Dyck02ced6f2011-02-17 01:49:42 +00001397 MaxFieldAlignment = CharUnits::fromQuantity(2);
Ken Dyck4731d5b2011-02-16 02:05:21 +00001398 Alignment = CharUnits::fromQuantity(2);
Daniel Dunbar6da10982010-05-27 05:45:51 +00001399 } else {
1400 if (const MaxFieldAlignmentAttr *MFAA = D->getAttr<MaxFieldAlignmentAttr>())
Ken Dyck02ced6f2011-02-17 01:49:42 +00001401 MaxFieldAlignment = Context.toCharUnitsFromBits(MFAA->getAlignment());
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001402
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001403 if (unsigned MaxAlign = D->getMaxAlignment())
Ken Dyck85ef0432011-02-19 18:58:07 +00001404 UpdateAlignment(Context.toCharUnitsFromBits(MaxAlign));
Daniel Dunbar6da10982010-05-27 05:45:51 +00001405 }
Douglas Gregore9fc3772012-01-26 07:55:45 +00001406
1407 // If there is an external AST source, ask it for the various offsets.
1408 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
1409 if (ExternalASTSource *External = Context.getExternalSource()) {
1410 ExternalLayout = External->layoutRecordType(RD,
1411 ExternalSize,
1412 ExternalAlign,
1413 ExternalFieldOffsets,
1414 ExternalBaseOffsets,
1415 ExternalVirtualBaseOffsets);
1416
1417 // Update based on external alignment.
1418 if (ExternalLayout) {
1419 Alignment = Context.toCharUnitsFromBits(ExternalAlign);
1420 UnpackedAlignment = Alignment;
1421 }
1422 }
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001423}
Anders Carlsson6d9f6f32009-07-19 00:18:47 +00001424
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001425void RecordLayoutBuilder::Layout(const RecordDecl *D) {
1426 InitializeLayout(D);
Anders Carlsson118ce162009-07-18 21:48:39 +00001427 LayoutFields(D);
Mike Stump11289f42009-09-09 15:08:12 +00001428
Anders Carlsson79474332009-07-18 20:20:21 +00001429 // Finally, round the size of the total struct up to the alignment of the
1430 // struct itself.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001431 FinishLayout(D);
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001432}
1433
1434void RecordLayoutBuilder::Layout(const CXXRecordDecl *RD) {
1435 InitializeLayout(RD);
1436
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001437 // Lay out the vtable and the non-virtual bases.
1438 LayoutNonVirtualBases(RD);
1439
1440 LayoutFields(RD);
1441
Ken Dycke7380752011-03-10 01:53:59 +00001442 NonVirtualSize = Context.toCharUnitsFromBits(
1443 llvm::RoundUpToAlignment(getSizeInBits(),
Douglas Gregore8bbc122011-09-02 00:18:52 +00001444 Context.getTargetInfo().getCharAlign()));
Ken Dyck4731d5b2011-02-16 02:05:21 +00001445 NonVirtualAlignment = Alignment;
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001446
John McCall0153cd32011-11-08 04:01:03 +00001447 if (isMicrosoftCXXABI() &&
1448 NonVirtualSize != NonVirtualSize.RoundUpToAlignment(Alignment)) {
1449 CharUnits AlignMember =
1450 NonVirtualSize.RoundUpToAlignment(Alignment) - NonVirtualSize;
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001451
John McCall0153cd32011-11-08 04:01:03 +00001452 setSize(getSize() + AlignMember);
1453 setDataSize(getSize());
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001454
John McCall0153cd32011-11-08 04:01:03 +00001455 NonVirtualSize = Context.toCharUnitsFromBits(
1456 llvm::RoundUpToAlignment(getSizeInBits(),
1457 Context.getTargetInfo().getCharAlign()));
1458
1459 MSLayoutVirtualBases(RD);
1460
1461 } else {
1462 // Lay out the virtual bases and add the primary virtual base offsets.
1463 LayoutVirtualBases(RD, RD);
1464 }
1465
1466 // Finally, round the size of the total struct up to the alignment
Eli Friedman83a12582011-12-01 00:37:01 +00001467 // of the struct itself.
1468 FinishLayout(RD);
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001469
Anders Carlsson5b441d72010-04-10 21:24:48 +00001470#ifndef NDEBUG
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001471 // Check that we have base offsets for all bases.
1472 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1473 E = RD->bases_end(); I != E; ++I) {
1474 if (I->isVirtual())
1475 continue;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001476
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001477 const CXXRecordDecl *BaseDecl =
1478 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1479
1480 assert(Bases.count(BaseDecl) && "Did not find base offset!");
1481 }
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001482
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001483 // And all virtual bases.
1484 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
1485 E = RD->vbases_end(); I != E; ++I) {
1486 const CXXRecordDecl *BaseDecl =
1487 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001488
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001489 assert(VBases.count(BaseDecl) && "Did not find base offset!");
Anders Carlsson5b441d72010-04-10 21:24:48 +00001490 }
1491#endif
Anders Carlsson79474332009-07-18 20:20:21 +00001492}
1493
Anders Carlssonc2226202010-05-26 05:58:59 +00001494void RecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D) {
Anders Carlsson4f516282009-07-18 20:50:59 +00001495 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001496 const ASTRecordLayout &SL = Context.getASTObjCInterfaceLayout(SD);
Anders Carlsson4f516282009-07-18 20:50:59 +00001497
Ken Dyck85ef0432011-02-19 18:58:07 +00001498 UpdateAlignment(SL.getAlignment());
Mike Stump11289f42009-09-09 15:08:12 +00001499
Anders Carlsson4f516282009-07-18 20:50:59 +00001500 // We start laying out ivars not at the end of the superclass
1501 // structure, but at the next byte following the last field.
Ken Dyckecfc7552011-02-24 01:13:28 +00001502 setSize(SL.getDataSize());
Ken Dyck1b4420e2011-02-28 02:01:38 +00001503 setDataSize(getSize());
Anders Carlsson4f516282009-07-18 20:50:59 +00001504 }
Mike Stump11289f42009-09-09 15:08:12 +00001505
Daniel Dunbar6da10982010-05-27 05:45:51 +00001506 InitializeLayout(D);
Anders Carlsson4f516282009-07-18 20:50:59 +00001507 // Layout each ivar sequentially.
Jordy Rosea91768e2011-07-22 02:08:32 +00001508 for (const ObjCIvarDecl *IVD = D->all_declared_ivar_begin(); IVD;
1509 IVD = IVD->getNextIvar())
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00001510 LayoutField(IVD);
Mike Stump11289f42009-09-09 15:08:12 +00001511
Anders Carlsson4f516282009-07-18 20:50:59 +00001512 // Finally, round the size of the total struct up to the alignment of the
1513 // struct itself.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001514 FinishLayout(D);
Anders Carlsson4f516282009-07-18 20:50:59 +00001515}
1516
Anders Carlssonc2226202010-05-26 05:58:59 +00001517void RecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
Anders Carlsson118ce162009-07-18 21:48:39 +00001518 // Layout each field, for now, just sequentially, respecting alignment. In
1519 // the future, this will need to be tweakable by targets.
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001520 const FieldDecl *LastFD = 0;
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001521 ZeroLengthBitfield = 0;
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001522 unsigned RemainingInAlignment = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001523 for (RecordDecl::field_iterator Field = D->field_begin(),
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001524 FieldEnd = D->field_end(); Field != FieldEnd; ++Field) {
1525 if (IsMsStruct) {
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001526 FieldDecl *FD = (*Field);
1527 if (Context.ZeroBitfieldFollowsBitfield(FD, LastFD))
1528 ZeroLengthBitfield = FD;
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001529 // Zero-length bitfields following non-bitfield members are
1530 // ignored:
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001531 else if (Context.ZeroBitfieldFollowsNonBitfield(FD, LastFD))
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001532 continue;
Fariborz Jahanian307eace2011-05-06 22:42:22 +00001533 // FIXME. streamline these conditions into a simple one.
Fariborz Jahanianb7a28792011-05-06 21:56:12 +00001534 else if (Context.BitfieldFollowsBitfield(FD, LastFD) ||
Chad Rosierf01a7dd2011-08-04 23:34:15 +00001535 Context.BitfieldFollowsNonBitfield(FD, LastFD) ||
1536 Context.NonBitfieldFollowsBitfield(FD, LastFD)) {
Fariborz Jahanian307eace2011-05-06 22:42:22 +00001537 // 1) Adjacent bit fields are packed into the same 1-, 2-, or
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001538 // 4-byte allocation unit if the integral types are the same
1539 // size and if the next bit field fits into the current
1540 // allocation unit without crossing the boundary imposed by the
1541 // common alignment requirements of the bit fields.
Fariborz Jahanian307eace2011-05-06 22:42:22 +00001542 // 2) Establish a new alignment for a bitfield following
Fariborz Jahanianb7a28792011-05-06 21:56:12 +00001543 // a non-bitfield if size of their types differ.
Fariborz Jahanian307eace2011-05-06 22:42:22 +00001544 // 3) Establish a new alignment for a non-bitfield following
1545 // a bitfield if size of their types differ.
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001546 std::pair<uint64_t, unsigned> FieldInfo =
1547 Context.getTypeInfo(FD->getType());
1548 uint64_t TypeSize = FieldInfo.first;
1549 unsigned FieldAlign = FieldInfo.second;
Fariborz Jahanian7adbed62011-05-09 22:03:17 +00001550 // This check is needed for 'long long' in -m32 mode.
Fariborz Jahanian0586df42011-12-12 21:16:36 +00001551 if (TypeSize > FieldAlign &&
1552 (Context.hasSameType(FD->getType(),
1553 Context.UnsignedLongLongTy)
1554 ||Context.hasSameType(FD->getType(),
1555 Context.LongLongTy)))
Fariborz Jahanian7adbed62011-05-09 22:03:17 +00001556 FieldAlign = TypeSize;
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001557 FieldInfo = Context.getTypeInfo(LastFD->getType());
1558 uint64_t TypeSizeLastFD = FieldInfo.first;
1559 unsigned FieldAlignLastFD = FieldInfo.second;
Fariborz Jahanian7adbed62011-05-09 22:03:17 +00001560 // This check is needed for 'long long' in -m32 mode.
Fariborz Jahanian0586df42011-12-12 21:16:36 +00001561 if (TypeSizeLastFD > FieldAlignLastFD &&
1562 (Context.hasSameType(LastFD->getType(),
1563 Context.UnsignedLongLongTy)
1564 || Context.hasSameType(LastFD->getType(),
1565 Context.LongLongTy)))
Fariborz Jahanian7adbed62011-05-09 22:03:17 +00001566 FieldAlignLastFD = TypeSizeLastFD;
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001567
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001568 if (TypeSizeLastFD != TypeSize) {
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001569 if (RemainingInAlignment &&
1570 LastFD && LastFD->isBitField() &&
Richard Smithcaf33902011-10-10 18:28:20 +00001571 LastFD->getBitWidthValue(Context)) {
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001572 // If previous field was a bitfield with some remaining unfilled
1573 // bits, pad the field so current field starts on its type boundary.
1574 uint64_t FieldOffset =
1575 getDataSizeInBits() - UnfilledBitsInLastByte;
1576 uint64_t NewSizeInBits = RemainingInAlignment + FieldOffset;
1577 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
Douglas Gregore8bbc122011-09-02 00:18:52 +00001578 Context.getTargetInfo().getCharAlign()));
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001579 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
1580 RemainingInAlignment = 0;
1581 }
1582
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001583 uint64_t UnpaddedFieldOffset =
1584 getDataSizeInBits() - UnfilledBitsInLastByte;
1585 FieldAlign = std::max(FieldAlign, FieldAlignLastFD);
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001586
Fariborz Jahanian07ceb0a2011-05-10 19:00:50 +00001587 // The maximum field alignment overrides the aligned attribute.
1588 if (!MaxFieldAlignment.isZero()) {
1589 unsigned MaxFieldAlignmentInBits =
1590 Context.toBits(MaxFieldAlignment);
1591 FieldAlign = std::min(FieldAlign, MaxFieldAlignmentInBits);
1592 }
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001593
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001594 uint64_t NewSizeInBits =
1595 llvm::RoundUpToAlignment(UnpaddedFieldOffset, FieldAlign);
1596 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
Douglas Gregore8bbc122011-09-02 00:18:52 +00001597 Context.getTargetInfo().getCharAlign()));
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001598 UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits;
1599 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
1600 }
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001601 if (FD->isBitField()) {
Richard Smithcaf33902011-10-10 18:28:20 +00001602 uint64_t FieldSize = FD->getBitWidthValue(Context);
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001603 assert (FieldSize > 0 && "LayoutFields - ms_struct layout");
1604 if (RemainingInAlignment < FieldSize)
1605 RemainingInAlignment = TypeSize - FieldSize;
1606 else
1607 RemainingInAlignment -= FieldSize;
1608 }
1609 }
1610 else if (FD->isBitField()) {
Richard Smithcaf33902011-10-10 18:28:20 +00001611 uint64_t FieldSize = FD->getBitWidthValue(Context);
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001612 std::pair<uint64_t, unsigned> FieldInfo =
1613 Context.getTypeInfo(FD->getType());
1614 uint64_t TypeSize = FieldInfo.first;
1615 RemainingInAlignment = TypeSize - FieldSize;
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001616 }
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001617 LastFD = FD;
1618 }
Douglas Gregore8bbc122011-09-02 00:18:52 +00001619 else if (!Context.getTargetInfo().useBitFieldTypeAlignment() &&
1620 Context.getTargetInfo().useZeroLengthBitfieldAlignment()) {
Chad Rosier18903ee2011-08-04 01:21:14 +00001621 FieldDecl *FD = (*Field);
Richard Smithcaf33902011-10-10 18:28:20 +00001622 if (FD->isBitField() && FD->getBitWidthValue(Context) == 0)
Chad Rosier18903ee2011-08-04 01:21:14 +00001623 ZeroLengthBitfield = FD;
Chad Rosier18903ee2011-08-04 01:21:14 +00001624 }
Anders Carlsson118ce162009-07-18 21:48:39 +00001625 LayoutField(*Field);
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001626 }
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001627 if (IsMsStruct && RemainingInAlignment &&
Richard Smithcaf33902011-10-10 18:28:20 +00001628 LastFD && LastFD->isBitField() && LastFD->getBitWidthValue(Context)) {
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001629 // If we ended a bitfield before the full length of the type then
1630 // pad the struct out to the full length of the last type.
1631 uint64_t FieldOffset =
1632 getDataSizeInBits() - UnfilledBitsInLastByte;
1633 uint64_t NewSizeInBits = RemainingInAlignment + FieldOffset;
1634 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
Douglas Gregore8bbc122011-09-02 00:18:52 +00001635 Context.getTargetInfo().getCharAlign()));
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001636 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
1637 }
Anders Carlsson118ce162009-07-18 21:48:39 +00001638}
1639
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001640void RecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize,
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001641 uint64_t TypeSize,
1642 bool FieldPacked,
1643 const FieldDecl *D) {
Anders Carlsson57235162010-04-16 15:57:11 +00001644 assert(Context.getLangOptions().CPlusPlus &&
1645 "Can only have wide bit-fields in C++!");
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001646
Anders Carlsson57235162010-04-16 15:57:11 +00001647 // Itanium C++ ABI 2.4:
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001648 // If sizeof(T)*8 < n, let T' be the largest integral POD type with
Anders Carlsson57235162010-04-16 15:57:11 +00001649 // sizeof(T')*8 <= n.
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001650
Anders Carlsson57235162010-04-16 15:57:11 +00001651 QualType IntegralPODTypes[] = {
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001652 Context.UnsignedCharTy, Context.UnsignedShortTy, Context.UnsignedIntTy,
Anders Carlsson57235162010-04-16 15:57:11 +00001653 Context.UnsignedLongTy, Context.UnsignedLongLongTy
1654 };
1655
Anders Carlsson57235162010-04-16 15:57:11 +00001656 QualType Type;
1657 for (unsigned I = 0, E = llvm::array_lengthof(IntegralPODTypes);
1658 I != E; ++I) {
1659 uint64_t Size = Context.getTypeSize(IntegralPODTypes[I]);
Anders Carlsson57235162010-04-16 15:57:11 +00001660
1661 if (Size > FieldSize)
1662 break;
1663
1664 Type = IntegralPODTypes[I];
1665 }
1666 assert(!Type.isNull() && "Did not find a type!");
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001667
Ken Dyckdbe37f32011-03-01 01:36:00 +00001668 CharUnits TypeAlign = Context.getTypeAlignInChars(Type);
Anders Carlsson57235162010-04-16 15:57:11 +00001669
1670 // We're not going to use any of the unfilled bits in the last byte.
1671 UnfilledBitsInLastByte = 0;
1672
Anders Carlssonaad5fa82010-04-17 20:21:41 +00001673 uint64_t FieldOffset;
Ken Dyckecfc7552011-02-24 01:13:28 +00001674 uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001675
Anders Carlsson57235162010-04-16 15:57:11 +00001676 if (IsUnion) {
Ken Dyckecfc7552011-02-24 01:13:28 +00001677 setDataSize(std::max(getDataSizeInBits(), FieldSize));
Anders Carlssonaad5fa82010-04-17 20:21:41 +00001678 FieldOffset = 0;
Anders Carlsson57235162010-04-16 15:57:11 +00001679 } else {
Chad Rosiere1a6a0e2011-08-05 22:38:04 +00001680 // The bitfield is allocated starting at the next offset aligned
1681 // appropriately for T', with length n bits.
Ken Dyckdbe37f32011-03-01 01:36:00 +00001682 FieldOffset = llvm::RoundUpToAlignment(getDataSizeInBits(),
1683 Context.toBits(TypeAlign));
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001684
Anders Carlsson57235162010-04-16 15:57:11 +00001685 uint64_t NewSizeInBits = FieldOffset + FieldSize;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001686
Ken Dycka1a2e8d2011-03-10 02:00:35 +00001687 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
Douglas Gregore8bbc122011-09-02 00:18:52 +00001688 Context.getTargetInfo().getCharAlign()));
Ken Dyckecfc7552011-02-24 01:13:28 +00001689 UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits;
Anders Carlsson57235162010-04-16 15:57:11 +00001690 }
1691
1692 // Place this field at the current location.
1693 FieldOffsets.push_back(FieldOffset);
1694
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001695 CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, FieldOffset,
Ken Dyckdbe37f32011-03-01 01:36:00 +00001696 Context.toBits(TypeAlign), FieldPacked, D);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001697
Anders Carlsson57235162010-04-16 15:57:11 +00001698 // Update the size.
Ken Dyckecfc7552011-02-24 01:13:28 +00001699 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001700
Anders Carlsson57235162010-04-16 15:57:11 +00001701 // Remember max struct/class alignment.
Ken Dyckdbe37f32011-03-01 01:36:00 +00001702 UpdateAlignment(TypeAlign);
Anders Carlsson57235162010-04-16 15:57:11 +00001703}
1704
Anders Carlssonc2226202010-05-26 05:58:59 +00001705void RecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
Anders Carlsson07209442009-11-22 17:37:31 +00001706 bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
Ken Dyckecfc7552011-02-24 01:13:28 +00001707 uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte;
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001708 uint64_t FieldOffset = IsUnion ? 0 : UnpaddedFieldOffset;
Richard Smithcaf33902011-10-10 18:28:20 +00001709 uint64_t FieldSize = D->getBitWidthValue(Context);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001710
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001711 std::pair<uint64_t, unsigned> FieldInfo = Context.getTypeInfo(D->getType());
Anders Carlsson07209442009-11-22 17:37:31 +00001712 uint64_t TypeSize = FieldInfo.first;
1713 unsigned FieldAlign = FieldInfo.second;
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001714
Douglas Gregore9fc3772012-01-26 07:55:45 +00001715 if (ExternalLayout) {
1716 assert(ExternalFieldOffsets.find(D) != ExternalFieldOffsets.end() &&
1717 "Field does not have an external offset");
1718 FieldOffset = ExternalFieldOffsets[D];
1719 }
1720
Fariborz Jahanian7adbed62011-05-09 22:03:17 +00001721 // This check is needed for 'long long' in -m32 mode.
Fariborz Jahanian0586df42011-12-12 21:16:36 +00001722 if (IsMsStruct && (TypeSize > FieldAlign) &&
1723 (Context.hasSameType(D->getType(),
1724 Context.UnsignedLongLongTy)
1725 || Context.hasSameType(D->getType(), Context.LongLongTy)))
Fariborz Jahanian7adbed62011-05-09 22:03:17 +00001726 FieldAlign = TypeSize;
Chad Rosier18903ee2011-08-04 01:21:14 +00001727
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001728 if (ZeroLengthBitfield) {
Chad Rosiere1a6a0e2011-08-05 22:38:04 +00001729 std::pair<uint64_t, unsigned> FieldInfo;
1730 unsigned ZeroLengthBitfieldAlignment;
1731 if (IsMsStruct) {
1732 // If a zero-length bitfield is inserted after a bitfield,
1733 // and the alignment of the zero-length bitfield is
1734 // greater than the member that follows it, `bar', `bar'
1735 // will be aligned as the type of the zero-length bitfield.
1736 if (ZeroLengthBitfield != D) {
1737 FieldInfo = Context.getTypeInfo(ZeroLengthBitfield->getType());
1738 ZeroLengthBitfieldAlignment = FieldInfo.second;
1739 // Ignore alignment of subsequent zero-length bitfields.
1740 if ((ZeroLengthBitfieldAlignment > FieldAlign) || (FieldSize == 0))
1741 FieldAlign = ZeroLengthBitfieldAlignment;
1742 if (FieldSize)
1743 ZeroLengthBitfield = 0;
1744 }
1745 } else {
1746 // The alignment of a zero-length bitfield affects the alignment
1747 // of the next member. The alignment is the max of the zero
1748 // length bitfield's alignment and a target specific fixed value.
1749 unsigned ZeroLengthBitfieldBoundary =
Douglas Gregore8bbc122011-09-02 00:18:52 +00001750 Context.getTargetInfo().getZeroLengthBitfieldBoundary();
Chad Rosiere1a6a0e2011-08-05 22:38:04 +00001751 if (ZeroLengthBitfieldBoundary > FieldAlign)
1752 FieldAlign = ZeroLengthBitfieldBoundary;
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001753 }
1754 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001755
Anders Carlsson57235162010-04-16 15:57:11 +00001756 if (FieldSize > TypeSize) {
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001757 LayoutWideBitField(FieldSize, TypeSize, FieldPacked, D);
Anders Carlsson57235162010-04-16 15:57:11 +00001758 return;
1759 }
1760
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001761 // The align if the field is not packed. This is to check if the attribute
1762 // was unnecessary (-Wpacked).
1763 unsigned UnpackedFieldAlign = FieldAlign;
1764 uint64_t UnpackedFieldOffset = FieldOffset;
Douglas Gregore8bbc122011-09-02 00:18:52 +00001765 if (!Context.getTargetInfo().useBitFieldTypeAlignment() && !ZeroLengthBitfield)
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001766 UnpackedFieldAlign = 1;
1767
Chad Rosiere1a6a0e2011-08-05 22:38:04 +00001768 if (FieldPacked ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00001769 (!Context.getTargetInfo().useBitFieldTypeAlignment() && !ZeroLengthBitfield))
Anders Carlsson07209442009-11-22 17:37:31 +00001770 FieldAlign = 1;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001771 FieldAlign = std::max(FieldAlign, D->getMaxAlignment());
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001772 UnpackedFieldAlign = std::max(UnpackedFieldAlign, D->getMaxAlignment());
Anders Carlsson07209442009-11-22 17:37:31 +00001773
1774 // The maximum field alignment overrides the aligned attribute.
Eli Friedmana91d38a2011-12-02 02:38:48 +00001775 if (!MaxFieldAlignment.isZero() && FieldSize != 0) {
Ken Dyck02ced6f2011-02-17 01:49:42 +00001776 unsigned MaxFieldAlignmentInBits = Context.toBits(MaxFieldAlignment);
1777 FieldAlign = std::min(FieldAlign, MaxFieldAlignmentInBits);
1778 UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignmentInBits);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001779 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001780
Douglas Gregore9fc3772012-01-26 07:55:45 +00001781 if (!ExternalLayout) {
1782 // Check if we need to add padding to give the field the correct alignment.
1783 if (FieldSize == 0 ||
1784 (MaxFieldAlignment.isZero() &&
1785 (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize))
1786 FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001787
Douglas Gregore9fc3772012-01-26 07:55:45 +00001788 if (FieldSize == 0 ||
1789 (MaxFieldAlignment.isZero() &&
1790 (UnpackedFieldOffset & (UnpackedFieldAlign-1)) + FieldSize > TypeSize))
1791 UnpackedFieldOffset = llvm::RoundUpToAlignment(UnpackedFieldOffset,
1792 UnpackedFieldAlign);
1793 }
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001794
Chad Rosiere1a6a0e2011-08-05 22:38:04 +00001795 // Padding members don't affect overall alignment, unless zero length bitfield
1796 // alignment is enabled.
Douglas Gregore8bbc122011-09-02 00:18:52 +00001797 if (!D->getIdentifier() && !Context.getTargetInfo().useZeroLengthBitfieldAlignment())
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001798 FieldAlign = UnpackedFieldAlign = 1;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001799
Chad Rosiere1a6a0e2011-08-05 22:38:04 +00001800 if (!IsMsStruct)
1801 ZeroLengthBitfield = 0;
1802
Anders Carlsson07209442009-11-22 17:37:31 +00001803 // Place this field at the current location.
1804 FieldOffsets.push_back(FieldOffset);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001805
Douglas Gregore9fc3772012-01-26 07:55:45 +00001806 if (!ExternalLayout)
1807 CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, UnpackedFieldOffset,
1808 UnpackedFieldAlign, FieldPacked, D);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001809
Anders Carlssonba958402009-11-22 19:13:51 +00001810 // Update DataSize to include the last byte containing (part of) the bitfield.
1811 if (IsUnion) {
1812 // FIXME: I think FieldSize should be TypeSize here.
Ken Dyckecfc7552011-02-24 01:13:28 +00001813 setDataSize(std::max(getDataSizeInBits(), FieldSize));
Anders Carlssonba958402009-11-22 19:13:51 +00001814 } else {
1815 uint64_t NewSizeInBits = FieldOffset + FieldSize;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001816
Ken Dycka1a2e8d2011-03-10 02:00:35 +00001817 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
Douglas Gregore8bbc122011-09-02 00:18:52 +00001818 Context.getTargetInfo().getCharAlign()));
Ken Dyckecfc7552011-02-24 01:13:28 +00001819 UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits;
Anders Carlssonba958402009-11-22 19:13:51 +00001820 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001821
Anders Carlssonba958402009-11-22 19:13:51 +00001822 // Update the size.
Ken Dyckecfc7552011-02-24 01:13:28 +00001823 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001824
Anders Carlsson07209442009-11-22 17:37:31 +00001825 // Remember max struct/class alignment.
Ken Dyck85ef0432011-02-19 18:58:07 +00001826 UpdateAlignment(Context.toCharUnitsFromBits(FieldAlign),
1827 Context.toCharUnitsFromBits(UnpackedFieldAlign));
Anders Carlsson07209442009-11-22 17:37:31 +00001828}
1829
Douglas Gregore9fc3772012-01-26 07:55:45 +00001830void RecordLayoutBuilder::LayoutField(const FieldDecl *D) {
Anders Carlsson07209442009-11-22 17:37:31 +00001831 if (D->isBitField()) {
1832 LayoutBitField(D);
1833 return;
1834 }
1835
Ken Dyckecfc7552011-02-24 01:13:28 +00001836 uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte;
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001837
Anders Carlssonba958402009-11-22 19:13:51 +00001838 // Reset the unfilled bits.
1839 UnfilledBitsInLastByte = 0;
1840
Anders Carlsson07209442009-11-22 17:37:31 +00001841 bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
Ken Dyck6d90e892011-02-20 02:06:09 +00001842 CharUnits FieldOffset =
Ken Dyckecfc7552011-02-24 01:13:28 +00001843 IsUnion ? CharUnits::Zero() : getDataSize();
Ken Dyck6d90e892011-02-20 02:06:09 +00001844 CharUnits FieldSize;
1845 CharUnits FieldAlign;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001846
Douglas Gregore9fc3772012-01-26 07:55:45 +00001847 if (ExternalLayout) {
1848 assert(ExternalFieldOffsets.find(D) != ExternalFieldOffsets.end() &&
1849 "Field does not have an external offset");
1850 FieldOffset = Context.toCharUnitsFromBits(ExternalFieldOffsets[D]);
1851 }
1852
1853
Anders Carlsson07209442009-11-22 17:37:31 +00001854 if (D->getType()->isIncompleteArrayType()) {
1855 // This is a flexible array member; we can't directly
1856 // query getTypeInfo about these, so we figure it out here.
1857 // Flexible array members don't have any size, but they
1858 // have to be aligned appropriately for their element type.
Ken Dyck6d90e892011-02-20 02:06:09 +00001859 FieldSize = CharUnits::Zero();
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001860 const ArrayType* ATy = Context.getAsArrayType(D->getType());
Ken Dyck6d90e892011-02-20 02:06:09 +00001861 FieldAlign = Context.getTypeAlignInChars(ATy->getElementType());
Anders Carlsson07209442009-11-22 17:37:31 +00001862 } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) {
1863 unsigned AS = RT->getPointeeType().getAddressSpace();
Ken Dyck6d90e892011-02-20 02:06:09 +00001864 FieldSize =
Douglas Gregore8bbc122011-09-02 00:18:52 +00001865 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(AS));
Ken Dyck6d90e892011-02-20 02:06:09 +00001866 FieldAlign =
Douglas Gregore8bbc122011-09-02 00:18:52 +00001867 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(AS));
Anders Carlsson79474332009-07-18 20:20:21 +00001868 } else {
Ken Dyck6d90e892011-02-20 02:06:09 +00001869 std::pair<CharUnits, CharUnits> FieldInfo =
1870 Context.getTypeInfoInChars(D->getType());
Anders Carlsson07209442009-11-22 17:37:31 +00001871 FieldSize = FieldInfo.first;
1872 FieldAlign = FieldInfo.second;
Chad Rosier18903ee2011-08-04 01:21:14 +00001873
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001874 if (ZeroLengthBitfield) {
Chad Rosier18903ee2011-08-04 01:21:14 +00001875 CharUnits ZeroLengthBitfieldBoundary =
1876 Context.toCharUnitsFromBits(
Douglas Gregore8bbc122011-09-02 00:18:52 +00001877 Context.getTargetInfo().getZeroLengthBitfieldBoundary());
Chad Rosier18903ee2011-08-04 01:21:14 +00001878 if (ZeroLengthBitfieldBoundary == CharUnits::Zero()) {
1879 // If a zero-length bitfield is inserted after a bitfield,
1880 // and the alignment of the zero-length bitfield is
1881 // greater than the member that follows it, `bar', `bar'
1882 // will be aligned as the type of the zero-length bitfield.
1883 std::pair<CharUnits, CharUnits> FieldInfo =
1884 Context.getTypeInfoInChars(ZeroLengthBitfield->getType());
1885 CharUnits ZeroLengthBitfieldAlignment = FieldInfo.second;
1886 if (ZeroLengthBitfieldAlignment > FieldAlign)
1887 FieldAlign = ZeroLengthBitfieldAlignment;
Chad Rosier64b18ee2011-08-04 19:25:14 +00001888 } else if (ZeroLengthBitfieldBoundary > FieldAlign) {
Chad Rosier18903ee2011-08-04 01:21:14 +00001889 // Align 'bar' based on a fixed alignment specified by the target.
Douglas Gregore8bbc122011-09-02 00:18:52 +00001890 assert(Context.getTargetInfo().useZeroLengthBitfieldAlignment() &&
Chad Rosiera336c6f2011-08-04 17:52:43 +00001891 "ZeroLengthBitfieldBoundary should only be used in conjunction"
1892 " with useZeroLengthBitfieldAlignment.");
Chad Rosier18903ee2011-08-04 01:21:14 +00001893 FieldAlign = ZeroLengthBitfieldBoundary;
1894 }
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001895 ZeroLengthBitfield = 0;
1896 }
Douglas Gregordbe39272011-02-01 15:15:22 +00001897
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001898 if (Context.getLangOptions().MSBitfields || IsMsStruct) {
Douglas Gregordbe39272011-02-01 15:15:22 +00001899 // If MS bitfield layout is required, figure out what type is being
1900 // laid out and align the field to the width of that type.
1901
1902 // Resolve all typedefs down to their base type and round up the field
1903 // alignment if necessary.
1904 QualType T = Context.getBaseElementType(D->getType());
1905 if (const BuiltinType *BTy = T->getAs<BuiltinType>()) {
Ken Dyck6d90e892011-02-20 02:06:09 +00001906 CharUnits TypeSize = Context.getTypeSizeInChars(BTy);
Douglas Gregordbe39272011-02-01 15:15:22 +00001907 if (TypeSize > FieldAlign)
1908 FieldAlign = TypeSize;
1909 }
1910 }
Anders Carlsson79474332009-07-18 20:20:21 +00001911 }
Mike Stump11289f42009-09-09 15:08:12 +00001912
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001913 // The align if the field is not packed. This is to check if the attribute
1914 // was unnecessary (-Wpacked).
Ken Dyck6d90e892011-02-20 02:06:09 +00001915 CharUnits UnpackedFieldAlign = FieldAlign;
1916 CharUnits UnpackedFieldOffset = FieldOffset;
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001917
Anders Carlsson07209442009-11-22 17:37:31 +00001918 if (FieldPacked)
Ken Dyck6d90e892011-02-20 02:06:09 +00001919 FieldAlign = CharUnits::One();
1920 CharUnits MaxAlignmentInChars =
1921 Context.toCharUnitsFromBits(D->getMaxAlignment());
1922 FieldAlign = std::max(FieldAlign, MaxAlignmentInChars);
1923 UnpackedFieldAlign = std::max(UnpackedFieldAlign, MaxAlignmentInChars);
Anders Carlsson07209442009-11-22 17:37:31 +00001924
1925 // The maximum field alignment overrides the aligned attribute.
Ken Dyck02ced6f2011-02-17 01:49:42 +00001926 if (!MaxFieldAlignment.isZero()) {
Ken Dyck6d90e892011-02-20 02:06:09 +00001927 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
1928 UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignment);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001929 }
Anders Carlsson07209442009-11-22 17:37:31 +00001930
Douglas Gregore9fc3772012-01-26 07:55:45 +00001931 if (!ExternalLayout) {
1932 // Round up the current record size to the field's alignment boundary.
1933 FieldOffset = FieldOffset.RoundUpToAlignment(FieldAlign);
1934 UnpackedFieldOffset =
1935 UnpackedFieldOffset.RoundUpToAlignment(UnpackedFieldAlign);
1936
1937 if (!IsUnion && EmptySubobjects) {
1938 // Check if we can place the field at this offset.
1939 while (!EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset)) {
1940 // We couldn't place the field at the offset. Try again at a new offset.
1941 FieldOffset += FieldAlign;
1942 }
Anders Carlsson07209442009-11-22 17:37:31 +00001943 }
Douglas Gregore9fc3772012-01-26 07:55:45 +00001944 } else if (!IsUnion && EmptySubobjects) {
1945 // Record the fact that we're placing a field at this offset.
1946 bool Allowed = EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset);
1947 (void)Allowed;
1948 assert(Allowed && "Externally-placed field cannot be placed here");
Anders Carlsson07209442009-11-22 17:37:31 +00001949 }
Douglas Gregore9fc3772012-01-26 07:55:45 +00001950
Anders Carlsson79474332009-07-18 20:20:21 +00001951 // Place this field at the current location.
Ken Dyck6d90e892011-02-20 02:06:09 +00001952 FieldOffsets.push_back(Context.toBits(FieldOffset));
Mike Stump11289f42009-09-09 15:08:12 +00001953
Douglas Gregore9fc3772012-01-26 07:55:45 +00001954 if (!ExternalLayout)
1955 CheckFieldPadding(Context.toBits(FieldOffset), UnpaddedFieldOffset,
1956 Context.toBits(UnpackedFieldOffset),
1957 Context.toBits(UnpackedFieldAlign), FieldPacked, D);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001958
Anders Carlsson79474332009-07-18 20:20:21 +00001959 // Reserve space for this field.
Eli Friedman43f18342012-01-12 23:27:03 +00001960 uint64_t FieldSizeInBits = Context.toBits(FieldSize);
Anders Carlsson79474332009-07-18 20:20:21 +00001961 if (IsUnion)
Eli Friedman2e108372012-01-12 23:48:56 +00001962 setDataSize(std::max(getDataSizeInBits(), FieldSizeInBits));
Anders Carlsson79474332009-07-18 20:20:21 +00001963 else
Eli Friedman2e108372012-01-12 23:48:56 +00001964 setDataSize(FieldOffset + FieldSize);
Mike Stump11289f42009-09-09 15:08:12 +00001965
Eli Friedman2e108372012-01-12 23:48:56 +00001966 // Update the size.
1967 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
Mike Stump11289f42009-09-09 15:08:12 +00001968
Anders Carlsson79474332009-07-18 20:20:21 +00001969 // Remember max struct/class alignment.
Ken Dyck6d90e892011-02-20 02:06:09 +00001970 UpdateAlignment(FieldAlign, UnpackedFieldAlign);
Anders Carlsson79474332009-07-18 20:20:21 +00001971}
1972
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001973void RecordLayoutBuilder::FinishLayout(const NamedDecl *D) {
Anders Carlsson79474332009-07-18 20:20:21 +00001974 // In C++, records cannot be of size 0.
Ken Dyckecfc7552011-02-24 01:13:28 +00001975 if (Context.getLangOptions().CPlusPlus && getSizeInBits() == 0) {
Fariborz Jahanian09b23312011-02-02 19:36:18 +00001976 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
1977 // Compatibility with gcc requires a class (pod or non-pod)
1978 // which is not empty but of size 0; such as having fields of
1979 // array of zero-length, remains of Size 0
1980 if (RD->isEmpty())
Ken Dyck1b4420e2011-02-28 02:01:38 +00001981 setSize(CharUnits::One());
Fariborz Jahanian09b23312011-02-02 19:36:18 +00001982 }
1983 else
Ken Dyck1b4420e2011-02-28 02:01:38 +00001984 setSize(CharUnits::One());
Fariborz Jahanian09b23312011-02-02 19:36:18 +00001985 }
Eli Friedman83a12582011-12-01 00:37:01 +00001986
1987 // MSVC doesn't round up to the alignment of the record with virtual bases.
1988 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
1989 if (isMicrosoftCXXABI() && RD->getNumVBases())
1990 return;
1991 }
1992
Anders Carlsson79474332009-07-18 20:20:21 +00001993 // Finally, round the size of the record up to the alignment of the
1994 // record itself.
Ken Dyckecfc7552011-02-24 01:13:28 +00001995 uint64_t UnpaddedSize = getSizeInBits() - UnfilledBitsInLastByte;
Ken Dyck1b4420e2011-02-28 02:01:38 +00001996 uint64_t UnpackedSizeInBits =
Ken Dyckecfc7552011-02-24 01:13:28 +00001997 llvm::RoundUpToAlignment(getSizeInBits(),
1998 Context.toBits(UnpackedAlignment));
Ken Dyck1b4420e2011-02-28 02:01:38 +00001999 CharUnits UnpackedSize = Context.toCharUnitsFromBits(UnpackedSizeInBits);
Ken Dyckecfc7552011-02-24 01:13:28 +00002000 setSize(llvm::RoundUpToAlignment(getSizeInBits(), Context.toBits(Alignment)));
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002001
Douglas Gregore8bbc122011-09-02 00:18:52 +00002002 unsigned CharBitNum = Context.getTargetInfo().getCharWidth();
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002003 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) {
2004 // Warn if padding was introduced to the struct/class/union.
Ken Dyckecfc7552011-02-24 01:13:28 +00002005 if (getSizeInBits() > UnpaddedSize) {
2006 unsigned PadSize = getSizeInBits() - UnpaddedSize;
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002007 bool InBits = true;
2008 if (PadSize % CharBitNum == 0) {
2009 PadSize = PadSize / CharBitNum;
2010 InBits = false;
2011 }
2012 Diag(RD->getLocation(), diag::warn_padded_struct_size)
2013 << Context.getTypeDeclType(RD)
2014 << PadSize
2015 << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not
2016 }
2017
2018 // Warn if we packed it unnecessarily. If the alignment is 1 byte don't
2019 // bother since there won't be alignment issues.
Ken Dyckecfc7552011-02-24 01:13:28 +00002020 if (Packed && UnpackedAlignment > CharUnits::One() &&
Ken Dyck1b4420e2011-02-28 02:01:38 +00002021 getSize() == UnpackedSize)
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002022 Diag(D->getLocation(), diag::warn_unnecessary_packed)
2023 << Context.getTypeDeclType(RD);
2024 }
Anders Carlsson79474332009-07-18 20:20:21 +00002025}
2026
Ken Dyck85ef0432011-02-19 18:58:07 +00002027void RecordLayoutBuilder::UpdateAlignment(CharUnits NewAlignment,
2028 CharUnits UnpackedNewAlignment) {
Douglas Gregore9fc3772012-01-26 07:55:45 +00002029 // The alignment is not modified when using 'mac68k' alignment or when
2030 // we have an externally-supplied layout.
2031 if (IsMac68kAlign || ExternalLayout)
Daniel Dunbar6da10982010-05-27 05:45:51 +00002032 return;
2033
Ken Dyck85ef0432011-02-19 18:58:07 +00002034 if (NewAlignment > Alignment) {
2035 assert(llvm::isPowerOf2_32(NewAlignment.getQuantity() &&
2036 "Alignment not a power of 2"));
2037 Alignment = NewAlignment;
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002038 }
2039
Ken Dyck85ef0432011-02-19 18:58:07 +00002040 if (UnpackedNewAlignment > UnpackedAlignment) {
2041 assert(llvm::isPowerOf2_32(UnpackedNewAlignment.getQuantity() &&
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002042 "Alignment not a power of 2"));
Ken Dyck85ef0432011-02-19 18:58:07 +00002043 UnpackedAlignment = UnpackedNewAlignment;
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002044 }
2045}
2046
2047void RecordLayoutBuilder::CheckFieldPadding(uint64_t Offset,
2048 uint64_t UnpaddedOffset,
2049 uint64_t UnpackedOffset,
2050 unsigned UnpackedAlign,
2051 bool isPacked,
2052 const FieldDecl *D) {
2053 // We let objc ivars without warning, objc interfaces generally are not used
2054 // for padding tricks.
2055 if (isa<ObjCIvarDecl>(D))
Anders Carlsson79474332009-07-18 20:20:21 +00002056 return;
Mike Stump11289f42009-09-09 15:08:12 +00002057
Ted Kremenekfed48af2011-09-06 19:40:45 +00002058 // Don't warn about structs created without a SourceLocation. This can
2059 // be done by clients of the AST, such as codegen.
2060 if (D->getLocation().isInvalid())
2061 return;
2062
Douglas Gregore8bbc122011-09-02 00:18:52 +00002063 unsigned CharBitNum = Context.getTargetInfo().getCharWidth();
Mike Stump11289f42009-09-09 15:08:12 +00002064
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002065 // Warn if padding was introduced to the struct/class.
2066 if (!IsUnion && Offset > UnpaddedOffset) {
2067 unsigned PadSize = Offset - UnpaddedOffset;
2068 bool InBits = true;
2069 if (PadSize % CharBitNum == 0) {
2070 PadSize = PadSize / CharBitNum;
2071 InBits = false;
2072 }
2073 if (D->getIdentifier())
2074 Diag(D->getLocation(), diag::warn_padded_struct_field)
2075 << (D->getParent()->isStruct() ? 0 : 1) // struct|class
2076 << Context.getTypeDeclType(D->getParent())
2077 << PadSize
2078 << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1) // plural or not
2079 << D->getIdentifier();
2080 else
2081 Diag(D->getLocation(), diag::warn_padded_struct_anon_field)
2082 << (D->getParent()->isStruct() ? 0 : 1) // struct|class
2083 << Context.getTypeDeclType(D->getParent())
2084 << PadSize
2085 << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not
2086 }
2087
2088 // Warn if we packed it unnecessarily. If the alignment is 1 byte don't
2089 // bother since there won't be alignment issues.
2090 if (isPacked && UnpackedAlign > CharBitNum && Offset == UnpackedOffset)
2091 Diag(D->getLocation(), diag::warn_unnecessary_packed)
2092 << D->getIdentifier();
Anders Carlsson79474332009-07-18 20:20:21 +00002093}
Mike Stump11289f42009-09-09 15:08:12 +00002094
Anders Carlsson5ebf8b42009-12-07 04:35:11 +00002095const CXXMethodDecl *
Anders Carlssonc2226202010-05-26 05:58:59 +00002096RecordLayoutBuilder::ComputeKeyFunction(const CXXRecordDecl *RD) {
Daniel Dunbarccabe482010-04-19 20:44:53 +00002097 // If a class isn't polymorphic it doesn't have a key function.
Anders Carlsson5ebf8b42009-12-07 04:35:11 +00002098 if (!RD->isPolymorphic())
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00002099 return 0;
Eli Friedmanf2c79b62009-12-08 03:56:49 +00002100
Eli Friedman300f55d2011-06-10 21:53:06 +00002101 // A class that is not externally visible doesn't have a key function. (Or
Eli Friedmanf2c79b62009-12-08 03:56:49 +00002102 // at least, there's no point to assigning a key function to such a class;
2103 // this doesn't affect the ABI.)
Eli Friedman300f55d2011-06-10 21:53:06 +00002104 if (RD->getLinkage() != ExternalLinkage)
Eli Friedmanf2c79b62009-12-08 03:56:49 +00002105 return 0;
2106
Argyrios Kyrtzidis8c64bbe2010-10-13 02:39:41 +00002107 // Template instantiations don't have key functions,see Itanium C++ ABI 5.2.6.
2108 // Same behavior as GCC.
2109 TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
2110 if (TSK == TSK_ImplicitInstantiation ||
2111 TSK == TSK_ExplicitInstantiationDefinition)
2112 return 0;
2113
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002114 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
2115 E = RD->method_end(); I != E; ++I) {
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00002116 const CXXMethodDecl *MD = *I;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002117
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00002118 if (!MD->isVirtual())
2119 continue;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002120
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00002121 if (MD->isPure())
2122 continue;
Eli Friedmanf2c79b62009-12-08 03:56:49 +00002123
Anders Carlssonf98849e2009-12-02 17:15:43 +00002124 // Ignore implicit member functions, they are always marked as inline, but
2125 // they don't have a body until they're defined.
2126 if (MD->isImplicit())
2127 continue;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002128
Douglas Gregora318efd2010-01-05 19:06:31 +00002129 if (MD->isInlineSpecified())
2130 continue;
Eli Friedman71a26d82009-12-06 20:50:05 +00002131
2132 if (MD->hasInlineBody())
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00002133 continue;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002134
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00002135 // We found it.
2136 return MD;
2137 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002138
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00002139 return 0;
2140}
2141
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002142DiagnosticBuilder
2143RecordLayoutBuilder::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00002144 return Context.getDiagnostics().Report(Loc, DiagID);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002145}
2146
Anders Carlssondf291d82010-05-26 04:56:53 +00002147/// getASTRecordLayout - Get or compute information about the layout of the
2148/// specified record (struct/union/class), which indicates its size and field
2149/// position information.
Jay Foad39c79802011-01-12 09:06:06 +00002150const ASTRecordLayout &
2151ASTContext::getASTRecordLayout(const RecordDecl *D) const {
John McCall0710e552011-10-07 02:39:22 +00002152 // These asserts test different things. A record has a definition
2153 // as soon as we begin to parse the definition. That definition is
2154 // not a complete definition (which is what isDefinition() tests)
2155 // until we *finish* parsing the definition.
Anders Carlssondf291d82010-05-26 04:56:53 +00002156 D = D->getDefinition();
2157 assert(D && "Cannot get layout of forward declarations!");
John McCallf937c022011-10-07 06:10:15 +00002158 assert(D->isCompleteDefinition() && "Cannot layout type before complete!");
Anders Carlssondf291d82010-05-26 04:56:53 +00002159
2160 // Look up this layout, if already laid out, return what we have.
2161 // Note that we can't save a reference to the entry because this function
2162 // is recursive.
2163 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
2164 if (Entry) return *Entry;
2165
Anders Carlssond2954862010-05-26 05:10:47 +00002166 const ASTRecordLayout *NewEntry;
2167
2168 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
Anders Carlssonc121b4e2010-05-27 00:07:01 +00002169 EmptySubobjectMap EmptySubobjects(*this, RD);
John McCall0153cd32011-11-08 04:01:03 +00002170 RecordLayoutBuilder Builder(*this, &EmptySubobjects);
2171 Builder.Layout(RD);
Anders Carlsson439edd12010-05-27 05:41:06 +00002172
John McCall0153cd32011-11-08 04:01:03 +00002173 // MSVC gives the vb-table pointer an alignment equal to that of
2174 // the non-virtual part of the structure. That's an inherently
2175 // multi-pass operation. If our first pass doesn't give us
2176 // adequate alignment, try again with the specified minimum
2177 // alignment. This is *much* more maintainable than computing the
2178 // alignment in advance in a separately-coded pass; it's also
2179 // significantly more efficient in the common case where the
2180 // vb-table doesn't need extra padding.
2181 if (Builder.VBPtrOffset != CharUnits::fromQuantity(-1) &&
2182 (Builder.VBPtrOffset % Builder.NonVirtualAlignment) != 0) {
2183 Builder.resetWithTargetAlignment(Builder.NonVirtualAlignment);
2184 Builder.Layout(RD);
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002185 }
2186
Anders Carlssond2954862010-05-26 05:10:47 +00002187 // FIXME: This is not always correct. See the part about bitfields at
2188 // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info.
2189 // FIXME: IsPODForThePurposeOfLayout should be stored in the record layout.
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002190 // This does not affect the calculations of MSVC layouts
2191 bool IsPODForThePurposeOfLayout =
John McCall0153cd32011-11-08 04:01:03 +00002192 (!Builder.isMicrosoftCXXABI() && cast<CXXRecordDecl>(D)->isPOD());
Anders Carlssond2954862010-05-26 05:10:47 +00002193
2194 // FIXME: This should be done in FinalizeLayout.
Ken Dyck1b4420e2011-02-28 02:01:38 +00002195 CharUnits DataSize =
John McCall0153cd32011-11-08 04:01:03 +00002196 IsPODForThePurposeOfLayout ? Builder.getSize() : Builder.getDataSize();
Ken Dyck1b4420e2011-02-28 02:01:38 +00002197 CharUnits NonVirtualSize =
John McCall0153cd32011-11-08 04:01:03 +00002198 IsPODForThePurposeOfLayout ? DataSize : Builder.NonVirtualSize;
Anders Carlssond2954862010-05-26 05:10:47 +00002199
Daniel Dunbar592a85c2010-05-27 02:25:46 +00002200 NewEntry =
John McCall0153cd32011-11-08 04:01:03 +00002201 new (*this) ASTRecordLayout(*this, Builder.getSize(),
2202 Builder.Alignment,
2203 Builder.VFPtrOffset,
2204 Builder.VBPtrOffset,
Ken Dyck1b4420e2011-02-28 02:01:38 +00002205 DataSize,
John McCall0153cd32011-11-08 04:01:03 +00002206 Builder.FieldOffsets.data(),
2207 Builder.FieldOffsets.size(),
Ken Dyckaf1c83f2011-02-16 01:52:01 +00002208 NonVirtualSize,
John McCall0153cd32011-11-08 04:01:03 +00002209 Builder.NonVirtualAlignment,
Anders Carlssonc121b4e2010-05-27 00:07:01 +00002210 EmptySubobjects.SizeOfLargestEmptySubobject,
John McCall0153cd32011-11-08 04:01:03 +00002211 Builder.PrimaryBase,
2212 Builder.PrimaryBaseIsVirtual,
2213 Builder.Bases, Builder.VBases);
Anders Carlssond2954862010-05-26 05:10:47 +00002214 } else {
John McCall0153cd32011-11-08 04:01:03 +00002215 RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0);
Anders Carlssond2954862010-05-26 05:10:47 +00002216 Builder.Layout(D);
Daniel Dunbar592a85c2010-05-27 02:25:46 +00002217
Anders Carlssond2954862010-05-26 05:10:47 +00002218 NewEntry =
Ken Dyck1b4420e2011-02-28 02:01:38 +00002219 new (*this) ASTRecordLayout(*this, Builder.getSize(),
Ken Dyck4731d5b2011-02-16 02:05:21 +00002220 Builder.Alignment,
Ken Dyck1b4420e2011-02-28 02:01:38 +00002221 Builder.getSize(),
Anders Carlssond2954862010-05-26 05:10:47 +00002222 Builder.FieldOffsets.data(),
2223 Builder.FieldOffsets.size());
2224 }
2225
Anders Carlssondf291d82010-05-26 04:56:53 +00002226 ASTRecordLayouts[D] = NewEntry;
2227
2228 if (getLangOptions().DumpRecordLayouts) {
2229 llvm::errs() << "\n*** Dumping AST Record Layout\n";
Douglas Gregore9fc3772012-01-26 07:55:45 +00002230 DumpRecordLayout(D, llvm::errs(), getLangOptions().DumpRecordLayoutsSimple);
Anders Carlssondf291d82010-05-26 04:56:53 +00002231 }
2232
2233 return *NewEntry;
2234}
2235
2236const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) {
2237 RD = cast<CXXRecordDecl>(RD->getDefinition());
2238 assert(RD && "Cannot get key function for forward declarations!");
Daniel Dunbar592a85c2010-05-27 02:25:46 +00002239
Anders Carlssondf291d82010-05-26 04:56:53 +00002240 const CXXMethodDecl *&Entry = KeyFunctions[RD];
Daniel Dunbar592a85c2010-05-27 02:25:46 +00002241 if (!Entry)
Anders Carlssonc2226202010-05-26 05:58:59 +00002242 Entry = RecordLayoutBuilder::ComputeKeyFunction(RD);
Daniel Dunbar592a85c2010-05-27 02:25:46 +00002243
Anders Carlssondf291d82010-05-26 04:56:53 +00002244 return Entry;
2245}
2246
Richard Smithdafff942012-01-14 04:30:29 +00002247static uint64_t getFieldOffset(const ASTContext &C, const FieldDecl *FD) {
2248 const ASTRecordLayout &Layout = C.getASTRecordLayout(FD->getParent());
2249 return Layout.getFieldOffset(FD->getFieldIndex());
2250}
2251
2252uint64_t ASTContext::getFieldOffset(const ValueDecl *VD) const {
2253 uint64_t OffsetInBits;
2254 if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) {
2255 OffsetInBits = ::getFieldOffset(*this, FD);
2256 } else {
2257 const IndirectFieldDecl *IFD = cast<IndirectFieldDecl>(VD);
2258
2259 OffsetInBits = 0;
2260 for (IndirectFieldDecl::chain_iterator CI = IFD->chain_begin(),
2261 CE = IFD->chain_end();
2262 CI != CE; ++CI)
2263 OffsetInBits += ::getFieldOffset(*this, cast<FieldDecl>(*CI));
2264 }
2265
2266 return OffsetInBits;
2267}
2268
Eric Christopher8a39a012011-10-05 06:00:51 +00002269/// getObjCLayout - Get or compute information about the layout of the
2270/// given interface.
Anders Carlssondf291d82010-05-26 04:56:53 +00002271///
2272/// \param Impl - If given, also include the layout of the interface's
2273/// implementation. This may differ by including synthesized ivars.
2274const ASTRecordLayout &
2275ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
Jay Foad39c79802011-01-12 09:06:06 +00002276 const ObjCImplementationDecl *Impl) const {
Douglas Gregor64d92572011-12-20 15:50:13 +00002277 // Retrieve the definition
2278 D = D->getDefinition();
2279 assert(D && D->isThisDeclarationADefinition() && "Invalid interface decl!");
Anders Carlssondf291d82010-05-26 04:56:53 +00002280
2281 // Look up this layout, if already laid out, return what we have.
2282 ObjCContainerDecl *Key =
2283 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
2284 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
2285 return *Entry;
2286
2287 // Add in synthesized ivar count if laying out an implementation.
2288 if (Impl) {
2289 unsigned SynthCount = CountNonClassIvars(D);
2290 // If there aren't any sythesized ivars then reuse the interface
2291 // entry. Note we can't cache this because we simply free all
2292 // entries later; however we shouldn't look up implementations
2293 // frequently.
2294 if (SynthCount == 0)
2295 return getObjCLayout(D, 0);
2296 }
2297
John McCall0153cd32011-11-08 04:01:03 +00002298 RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0);
Anders Carlsson6ed3a9a2010-05-26 05:04:25 +00002299 Builder.Layout(D);
2300
Anders Carlssondf291d82010-05-26 04:56:53 +00002301 const ASTRecordLayout *NewEntry =
Ken Dyck1b4420e2011-02-28 02:01:38 +00002302 new (*this) ASTRecordLayout(*this, Builder.getSize(),
Ken Dyck4731d5b2011-02-16 02:05:21 +00002303 Builder.Alignment,
Ken Dyck1b4420e2011-02-28 02:01:38 +00002304 Builder.getDataSize(),
Anders Carlsson6ed3a9a2010-05-26 05:04:25 +00002305 Builder.FieldOffsets.data(),
2306 Builder.FieldOffsets.size());
Daniel Dunbar592a85c2010-05-27 02:25:46 +00002307
Anders Carlssondf291d82010-05-26 04:56:53 +00002308 ObjCLayouts[Key] = NewEntry;
2309
2310 return *NewEntry;
2311}
2312
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002313static void PrintOffset(raw_ostream &OS,
Anders Carlsson3f018712010-10-31 23:45:59 +00002314 CharUnits Offset, unsigned IndentLevel) {
Benjamin Kramer96ad7172011-11-05 09:02:52 +00002315 OS << llvm::format("%4" PRId64 " | ", (int64_t)Offset.getQuantity());
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002316 OS.indent(IndentLevel * 2);
2317}
2318
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002319static void DumpCXXRecordLayout(raw_ostream &OS,
Jay Foad39c79802011-01-12 09:06:06 +00002320 const CXXRecordDecl *RD, const ASTContext &C,
Anders Carlsson3f018712010-10-31 23:45:59 +00002321 CharUnits Offset,
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002322 unsigned IndentLevel,
2323 const char* Description,
2324 bool IncludeVirtualBases) {
Anders Carlsson3f018712010-10-31 23:45:59 +00002325 const ASTRecordLayout &Layout = C.getASTRecordLayout(RD);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002326
2327 PrintOffset(OS, Offset, IndentLevel);
Dan Gohman145f3f12010-04-19 16:39:44 +00002328 OS << C.getTypeDeclType(const_cast<CXXRecordDecl *>(RD)).getAsString();
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002329 if (Description)
2330 OS << ' ' << Description;
2331 if (RD->isEmpty())
2332 OS << " (empty)";
2333 OS << '\n';
2334
2335 IndentLevel++;
2336
Anders Carlsson3f018712010-10-31 23:45:59 +00002337 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Eli Friedman43114f92011-10-21 22:49:56 +00002338 bool HasVfptr = Layout.getVFPtrOffset() != CharUnits::fromQuantity(-1);
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002339 bool HasVbptr = Layout.getVBPtrOffset() != CharUnits::fromQuantity(-1);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002340
2341 // Vtable pointer.
Eli Friedman43114f92011-10-21 22:49:56 +00002342 if (RD->isDynamicClass() && !PrimaryBase &&
2343 C.getTargetInfo().getCXXABI() != CXXABI_Microsoft) {
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002344 PrintOffset(OS, Offset, IndentLevel);
Benjamin Kramerb89514a2011-10-14 18:45:37 +00002345 OS << '(' << *RD << " vtable pointer)\n";
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002346 }
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002347
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002348 // Dump (non-virtual) bases
2349 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
2350 E = RD->bases_end(); I != E; ++I) {
2351 assert(!I->getType()->isDependentType() &&
2352 "Cannot layout class with dependent bases.");
2353 if (I->isVirtual())
2354 continue;
2355
2356 const CXXRecordDecl *Base =
2357 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
2358
Anders Carlsson3f018712010-10-31 23:45:59 +00002359 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002360
2361 DumpCXXRecordLayout(OS, Base, C, BaseOffset, IndentLevel,
2362 Base == PrimaryBase ? "(primary base)" : "(base)",
2363 /*IncludeVirtualBases=*/false);
2364 }
Eli Friedman43114f92011-10-21 22:49:56 +00002365
2366 // vfptr and vbptr (for Microsoft C++ ABI)
2367 if (HasVfptr) {
2368 PrintOffset(OS, Offset + Layout.getVFPtrOffset(), IndentLevel);
2369 OS << '(' << *RD << " vftable pointer)\n";
2370 }
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002371 if (HasVbptr) {
2372 PrintOffset(OS, Offset + Layout.getVBPtrOffset(), IndentLevel);
Benjamin Kramerb89514a2011-10-14 18:45:37 +00002373 OS << '(' << *RD << " vbtable pointer)\n";
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002374 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002375
2376 // Dump fields.
2377 uint64_t FieldNo = 0;
2378 for (CXXRecordDecl::field_iterator I = RD->field_begin(),
2379 E = RD->field_end(); I != E; ++I, ++FieldNo) {
2380 const FieldDecl *Field = *I;
Anders Carlsson3f018712010-10-31 23:45:59 +00002381 CharUnits FieldOffset = Offset +
Ken Dyck86a7fcc2011-01-18 01:56:16 +00002382 C.toCharUnitsFromBits(Layout.getFieldOffset(FieldNo));
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002383
2384 if (const RecordType *RT = Field->getType()->getAs<RecordType>()) {
2385 if (const CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
2386 DumpCXXRecordLayout(OS, D, C, FieldOffset, IndentLevel,
Daniel Dunbar56df9772010-08-17 22:39:59 +00002387 Field->getName().data(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002388 /*IncludeVirtualBases=*/true);
2389 continue;
2390 }
2391 }
2392
2393 PrintOffset(OS, FieldOffset, IndentLevel);
Benjamin Kramerb89514a2011-10-14 18:45:37 +00002394 OS << Field->getType().getAsString() << ' ' << *Field << '\n';
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002395 }
2396
2397 if (!IncludeVirtualBases)
2398 return;
2399
2400 // Dump virtual bases.
2401 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
2402 E = RD->vbases_end(); I != E; ++I) {
2403 assert(I->isVirtual() && "Found non-virtual class!");
2404 const CXXRecordDecl *VBase =
2405 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
2406
Anders Carlsson3f018712010-10-31 23:45:59 +00002407 CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBase);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002408 DumpCXXRecordLayout(OS, VBase, C, VBaseOffset, IndentLevel,
2409 VBase == PrimaryBase ?
2410 "(primary virtual base)" : "(virtual base)",
2411 /*IncludeVirtualBases=*/false);
2412 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002413
Ken Dyckc8ae5502011-02-09 01:59:34 +00002414 OS << " sizeof=" << Layout.getSize().getQuantity();
Ken Dyckd5090c12011-02-11 02:20:09 +00002415 OS << ", dsize=" << Layout.getDataSize().getQuantity();
Ken Dyck7ad11e72011-02-15 02:32:40 +00002416 OS << ", align=" << Layout.getAlignment().getQuantity() << '\n';
Ken Dyck316d6f62011-02-01 01:52:10 +00002417 OS << " nvsize=" << Layout.getNonVirtualSize().getQuantity();
Ken Dyckbec02852011-02-08 02:02:47 +00002418 OS << ", nvalign=" << Layout.getNonVirtualAlign().getQuantity() << '\n';
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002419 OS << '\n';
2420}
Daniel Dunbarccabe482010-04-19 20:44:53 +00002421
2422void ASTContext::DumpRecordLayout(const RecordDecl *RD,
Douglas Gregore9fc3772012-01-26 07:55:45 +00002423 raw_ostream &OS,
2424 bool Simple) const {
Daniel Dunbarccabe482010-04-19 20:44:53 +00002425 const ASTRecordLayout &Info = getASTRecordLayout(RD);
2426
2427 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Douglas Gregore9fc3772012-01-26 07:55:45 +00002428 if (!Simple)
2429 return DumpCXXRecordLayout(OS, CXXRD, *this, CharUnits(), 0, 0,
2430 /*IncludeVirtualBases=*/true);
Daniel Dunbarccabe482010-04-19 20:44:53 +00002431
2432 OS << "Type: " << getTypeDeclType(RD).getAsString() << "\n";
Douglas Gregore9fc3772012-01-26 07:55:45 +00002433 if (!Simple) {
2434 OS << "Record: ";
2435 RD->dump();
2436 }
Daniel Dunbarccabe482010-04-19 20:44:53 +00002437 OS << "\nLayout: ";
2438 OS << "<ASTRecordLayout\n";
Ken Dyckb0fcc592011-02-11 01:54:29 +00002439 OS << " Size:" << toBits(Info.getSize()) << "\n";
Ken Dyckd5090c12011-02-11 02:20:09 +00002440 OS << " DataSize:" << toBits(Info.getDataSize()) << "\n";
Ken Dyck7ad11e72011-02-15 02:32:40 +00002441 OS << " Alignment:" << toBits(Info.getAlignment()) << "\n";
Daniel Dunbarccabe482010-04-19 20:44:53 +00002442 OS << " FieldOffsets: [";
2443 for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i) {
2444 if (i) OS << ", ";
2445 OS << Info.getFieldOffset(i);
2446 }
2447 OS << "]>\n";
2448}