blob: ace57be2ced957fc4c0ff3a8ab3def8eaf0d633c [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) {
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000164
165 const RecordType *RT =
David Blaikie40ed2972012-06-06 20:45:41 +0000166 Context.getBaseElementType(I->getType())->getAs<RecordType>();
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000167
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000168 // We only care about record types.
169 if (!RT)
170 continue;
171
Anders Carlsson28466ab2010-10-31 22:13:23 +0000172 CharUnits EmptySize;
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000173 const CXXRecordDecl *MemberDecl = cast<CXXRecordDecl>(RT->getDecl());
174 const ASTRecordLayout &Layout = Context.getASTRecordLayout(MemberDecl);
175 if (MemberDecl->isEmpty()) {
176 // If the class decl is empty, get its size.
Ken Dyckc8ae5502011-02-09 01:59:34 +0000177 EmptySize = Layout.getSize();
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000178 } else {
179 // Otherwise, we get the largest empty subobject for the decl.
180 EmptySize = Layout.getSizeOfLargestEmptySubobject();
181 }
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000182
Anders Carlsson28466ab2010-10-31 22:13:23 +0000183 if (EmptySize > SizeOfLargestEmptySubobject)
184 SizeOfLargestEmptySubobject = EmptySize;
Anders Carlssonc5ca1f72010-05-26 15:54:25 +0000185 }
186}
187
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000188bool
Anders Carlssondb319762010-05-27 18:20:57 +0000189EmptySubobjectMap::CanPlaceSubobjectAtOffset(const CXXRecordDecl *RD,
Anders Carlsson725190f2010-10-31 21:39:24 +0000190 CharUnits Offset) const {
Anders Carlssondb319762010-05-27 18:20:57 +0000191 // We only need to check empty bases.
192 if (!RD->isEmpty())
193 return true;
194
Anders Carlsson725190f2010-10-31 21:39:24 +0000195 EmptyClassOffsetsMapTy::const_iterator I = EmptyClassOffsets.find(Offset);
Anders Carlssondb319762010-05-27 18:20:57 +0000196 if (I == EmptyClassOffsets.end())
197 return true;
198
199 const ClassVectorTy& Classes = I->second;
200 if (std::find(Classes.begin(), Classes.end(), RD) == Classes.end())
201 return true;
202
203 // There is already an empty class of the same type at this offset.
204 return false;
205}
206
207void EmptySubobjectMap::AddSubobjectAtOffset(const CXXRecordDecl *RD,
Anders Carlsson725190f2010-10-31 21:39:24 +0000208 CharUnits Offset) {
Anders Carlssondb319762010-05-27 18:20:57 +0000209 // We only care about empty bases.
210 if (!RD->isEmpty())
211 return;
212
Rafael Espindola7bcde192010-12-29 23:02:58 +0000213 // If we have empty structures inside an union, we can assign both
214 // the same offset. Just avoid pushing them twice in the list.
Anders Carlsson725190f2010-10-31 21:39:24 +0000215 ClassVectorTy& Classes = EmptyClassOffsets[Offset];
Rafael Espindola7bcde192010-12-29 23:02:58 +0000216 if (std::find(Classes.begin(), Classes.end(), RD) != Classes.end())
217 return;
218
Anders Carlssondb319762010-05-27 18:20:57 +0000219 Classes.push_back(RD);
Anders Carlssoncc5de092010-06-08 15:56:03 +0000220
221 // Update the empty class offset.
Anders Carlsson725190f2010-10-31 21:39:24 +0000222 if (Offset > MaxEmptyClassOffset)
223 MaxEmptyClassOffset = Offset;
Anders Carlssondb319762010-05-27 18:20:57 +0000224}
225
226bool
Anders Carlsson28466ab2010-10-31 22:13:23 +0000227EmptySubobjectMap::CanPlaceBaseSubobjectAtOffset(const BaseSubobjectInfo *Info,
228 CharUnits Offset) {
Anders Carlsson45c1d282010-06-08 16:20:35 +0000229 // We don't have to keep looking past the maximum offset that's known to
230 // contain an empty class.
Anders Carlsson28466ab2010-10-31 22:13:23 +0000231 if (!AnyEmptySubobjectsBeyondOffset(Offset))
Anders Carlsson45c1d282010-06-08 16:20:35 +0000232 return true;
233
Anders Carlsson28466ab2010-10-31 22:13:23 +0000234 if (!CanPlaceSubobjectAtOffset(Info->Class, Offset))
Anders Carlssondb319762010-05-27 18:20:57 +0000235 return false;
236
Anders Carlsson439edd12010-05-27 05:41:06 +0000237 // Traverse all non-virtual bases.
Anders Carlssona7774a62010-05-29 21:10:24 +0000238 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class);
Anders Carlsson439edd12010-05-27 05:41:06 +0000239 for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) {
Anders Carlssona7f3cdb2010-05-28 21:24:37 +0000240 BaseSubobjectInfo* Base = Info->Bases[I];
Anders Carlsson439edd12010-05-27 05:41:06 +0000241 if (Base->IsVirtual)
242 continue;
243
Anders Carlsson0a14ee92010-11-01 00:21:58 +0000244 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class);
Anders Carlsson439edd12010-05-27 05:41:06 +0000245
246 if (!CanPlaceBaseSubobjectAtOffset(Base, BaseOffset))
247 return false;
248 }
249
Anders Carlssone3c24c72010-05-29 17:35:14 +0000250 if (Info->PrimaryVirtualBaseInfo) {
251 BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo;
Anders Carlsson439edd12010-05-27 05:41:06 +0000252
253 if (Info == PrimaryVirtualBaseInfo->Derived) {
254 if (!CanPlaceBaseSubobjectAtOffset(PrimaryVirtualBaseInfo, Offset))
255 return false;
256 }
257 }
258
Anders Carlssondb319762010-05-27 18:20:57 +0000259 // Traverse all member variables.
260 unsigned FieldNo = 0;
261 for (CXXRecordDecl::field_iterator I = Info->Class->field_begin(),
262 E = Info->Class->field_end(); I != E; ++I, ++FieldNo) {
David Blaikie40ed2972012-06-06 20:45:41 +0000263 if (I->isBitField())
Anders Carlsson233e2722010-10-31 21:54:55 +0000264 continue;
265
Anders Carlsson28466ab2010-10-31 22:13:23 +0000266 CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo);
David Blaikie40ed2972012-06-06 20:45:41 +0000267 if (!CanPlaceFieldSubobjectAtOffset(*I, FieldOffset))
Anders Carlssondb319762010-05-27 18:20:57 +0000268 return false;
269 }
270
Anders Carlsson439edd12010-05-27 05:41:06 +0000271 return true;
272}
273
Anders Carlssona7f3cdb2010-05-28 21:24:37 +0000274void EmptySubobjectMap::UpdateEmptyBaseSubobjects(const BaseSubobjectInfo *Info,
Anders Carlsson28466ab2010-10-31 22:13:23 +0000275 CharUnits Offset,
Anders Carlssoncc59cc52010-06-13 18:00:18 +0000276 bool PlacingEmptyBase) {
277 if (!PlacingEmptyBase && Offset >= SizeOfLargestEmptySubobject) {
278 // We know that the only empty subobjects that can conflict with empty
279 // subobject of non-empty bases, are empty bases that can be placed at
280 // offset zero. Because of this, we only need to keep track of empty base
281 // subobjects with offsets less than the size of the largest empty
282 // subobject for our class.
283 return;
284 }
285
Anders Carlsson28466ab2010-10-31 22:13:23 +0000286 AddSubobjectAtOffset(Info->Class, Offset);
Anders Carlssona7774a62010-05-29 21:10:24 +0000287
Anders Carlsson439edd12010-05-27 05:41:06 +0000288 // Traverse all non-virtual bases.
Anders Carlssona7774a62010-05-29 21:10:24 +0000289 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class);
Anders Carlsson439edd12010-05-27 05:41:06 +0000290 for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) {
Anders Carlssona7f3cdb2010-05-28 21:24:37 +0000291 BaseSubobjectInfo* Base = Info->Bases[I];
Anders Carlsson439edd12010-05-27 05:41:06 +0000292 if (Base->IsVirtual)
293 continue;
Anders Carlssona7774a62010-05-29 21:10:24 +0000294
Anders Carlsson0a14ee92010-11-01 00:21:58 +0000295 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class);
Anders Carlssoncc59cc52010-06-13 18:00:18 +0000296 UpdateEmptyBaseSubobjects(Base, BaseOffset, PlacingEmptyBase);
Anders Carlsson439edd12010-05-27 05:41:06 +0000297 }
298
Anders Carlssone3c24c72010-05-29 17:35:14 +0000299 if (Info->PrimaryVirtualBaseInfo) {
300 BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo;
Anders Carlsson439edd12010-05-27 05:41:06 +0000301
302 if (Info == PrimaryVirtualBaseInfo->Derived)
Anders Carlssoncc59cc52010-06-13 18:00:18 +0000303 UpdateEmptyBaseSubobjects(PrimaryVirtualBaseInfo, Offset,
304 PlacingEmptyBase);
Anders Carlsson439edd12010-05-27 05:41:06 +0000305 }
Anders Carlssondb319762010-05-27 18:20:57 +0000306
Anders Carlssondb319762010-05-27 18:20:57 +0000307 // Traverse all member variables.
308 unsigned FieldNo = 0;
309 for (CXXRecordDecl::field_iterator I = Info->Class->field_begin(),
310 E = Info->Class->field_end(); I != E; ++I, ++FieldNo) {
David Blaikie40ed2972012-06-06 20:45:41 +0000311 if (I->isBitField())
Anders Carlsson233e2722010-10-31 21:54:55 +0000312 continue;
Anders Carlssona7774a62010-05-29 21:10:24 +0000313
Anders Carlsson28466ab2010-10-31 22:13:23 +0000314 CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo);
David Blaikie40ed2972012-06-06 20:45:41 +0000315 UpdateEmptyFieldSubobjects(*I, FieldOffset);
Anders Carlssondb319762010-05-27 18:20:57 +0000316 }
Anders Carlsson439edd12010-05-27 05:41:06 +0000317}
318
Anders Carlssona60b86a2010-05-29 20:49:49 +0000319bool EmptySubobjectMap::CanPlaceBaseAtOffset(const BaseSubobjectInfo *Info,
Anders Carlsson28466ab2010-10-31 22:13:23 +0000320 CharUnits Offset) {
Anders Carlssonc121b4e2010-05-27 00:07:01 +0000321 // If we know this class doesn't have any empty subobjects we don't need to
322 // bother checking.
Anders Carlsson28466ab2010-10-31 22:13:23 +0000323 if (SizeOfLargestEmptySubobject.isZero())
Anders Carlssonc121b4e2010-05-27 00:07:01 +0000324 return true;
325
Anders Carlsson439edd12010-05-27 05:41:06 +0000326 if (!CanPlaceBaseSubobjectAtOffset(Info, Offset))
327 return false;
Anders Carlssondb319762010-05-27 18:20:57 +0000328
329 // We are able to place the base at this offset. Make sure to update the
330 // empty base subobject map.
Anders Carlssoncc59cc52010-06-13 18:00:18 +0000331 UpdateEmptyBaseSubobjects(Info, Offset, Info->Class->isEmpty());
Anders Carlssonc121b4e2010-05-27 00:07:01 +0000332 return true;
333}
334
Anders Carlssondb319762010-05-27 18:20:57 +0000335bool
336EmptySubobjectMap::CanPlaceFieldSubobjectAtOffset(const CXXRecordDecl *RD,
337 const CXXRecordDecl *Class,
Anders Carlsson28466ab2010-10-31 22:13:23 +0000338 CharUnits Offset) const {
Anders Carlsson45c1d282010-06-08 16:20:35 +0000339 // We don't have to keep looking past the maximum offset that's known to
340 // contain an empty class.
Anders Carlsson28466ab2010-10-31 22:13:23 +0000341 if (!AnyEmptySubobjectsBeyondOffset(Offset))
Anders Carlsson45c1d282010-06-08 16:20:35 +0000342 return true;
343
Anders Carlsson28466ab2010-10-31 22:13:23 +0000344 if (!CanPlaceSubobjectAtOffset(RD, Offset))
Anders Carlssondb319762010-05-27 18:20:57 +0000345 return false;
346
347 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
348
349 // Traverse all non-virtual bases.
350 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
351 E = RD->bases_end(); I != E; ++I) {
352 if (I->isVirtual())
353 continue;
354
355 const CXXRecordDecl *BaseDecl =
356 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
357
Anders Carlsson0a14ee92010-11-01 00:21:58 +0000358 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl);
Anders Carlssondb319762010-05-27 18:20:57 +0000359 if (!CanPlaceFieldSubobjectAtOffset(BaseDecl, Class, BaseOffset))
360 return false;
361 }
362
Anders Carlsson44687202010-06-08 19:09:24 +0000363 if (RD == Class) {
364 // This is the most derived class, traverse virtual bases as well.
365 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
366 E = RD->vbases_end(); I != E; ++I) {
367 const CXXRecordDecl *VBaseDecl =
368 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
369
Anders Carlsson3f018712010-10-31 23:45:59 +0000370 CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBaseDecl);
Anders Carlsson44687202010-06-08 19:09:24 +0000371 if (!CanPlaceFieldSubobjectAtOffset(VBaseDecl, Class, VBaseOffset))
372 return false;
373 }
374 }
375
Anders Carlssondb319762010-05-27 18:20:57 +0000376 // Traverse all member variables.
377 unsigned FieldNo = 0;
378 for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
379 I != E; ++I, ++FieldNo) {
David Blaikie40ed2972012-06-06 20:45:41 +0000380 if (I->isBitField())
Anders Carlsson233e2722010-10-31 21:54:55 +0000381 continue;
382
Anders Carlsson28466ab2010-10-31 22:13:23 +0000383 CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo);
Anders Carlssondb319762010-05-27 18:20:57 +0000384
David Blaikie40ed2972012-06-06 20:45:41 +0000385 if (!CanPlaceFieldSubobjectAtOffset(*I, FieldOffset))
Anders Carlssondb319762010-05-27 18:20:57 +0000386 return false;
387 }
388
389 return true;
390}
391
Anders Carlsson233e2722010-10-31 21:54:55 +0000392bool
393EmptySubobjectMap::CanPlaceFieldSubobjectAtOffset(const FieldDecl *FD,
394 CharUnits Offset) const {
Anders Carlsson45c1d282010-06-08 16:20:35 +0000395 // We don't have to keep looking past the maximum offset that's known to
396 // contain an empty class.
Anders Carlsson233e2722010-10-31 21:54:55 +0000397 if (!AnyEmptySubobjectsBeyondOffset(Offset))
Anders Carlsson45c1d282010-06-08 16:20:35 +0000398 return true;
399
Anders Carlssondb319762010-05-27 18:20:57 +0000400 QualType T = FD->getType();
401 if (const RecordType *RT = T->getAs<RecordType>()) {
402 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Anders Carlsson28466ab2010-10-31 22:13:23 +0000403 return CanPlaceFieldSubobjectAtOffset(RD, RD, Offset);
Anders Carlssondb319762010-05-27 18:20:57 +0000404 }
405
406 // If we have an array type we need to look at every element.
407 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) {
408 QualType ElemTy = Context.getBaseElementType(AT);
409 const RecordType *RT = ElemTy->getAs<RecordType>();
410 if (!RT)
411 return true;
412
413 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
414 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
415
416 uint64_t NumElements = Context.getConstantArrayElementCount(AT);
Anders Carlsson233e2722010-10-31 21:54:55 +0000417 CharUnits ElementOffset = Offset;
Anders Carlssondb319762010-05-27 18:20:57 +0000418 for (uint64_t I = 0; I != NumElements; ++I) {
Anders Carlsson45c1d282010-06-08 16:20:35 +0000419 // We don't have to keep looking past the maximum offset that's known to
420 // contain an empty class.
Anders Carlsson233e2722010-10-31 21:54:55 +0000421 if (!AnyEmptySubobjectsBeyondOffset(ElementOffset))
Anders Carlsson45c1d282010-06-08 16:20:35 +0000422 return true;
423
Anders Carlsson28466ab2010-10-31 22:13:23 +0000424 if (!CanPlaceFieldSubobjectAtOffset(RD, RD, ElementOffset))
Anders Carlssondb319762010-05-27 18:20:57 +0000425 return false;
426
Ken Dyckc8ae5502011-02-09 01:59:34 +0000427 ElementOffset += Layout.getSize();
Anders Carlssondb319762010-05-27 18:20:57 +0000428 }
429 }
430
431 return true;
432}
433
434bool
Anders Carlsson28466ab2010-10-31 22:13:23 +0000435EmptySubobjectMap::CanPlaceFieldAtOffset(const FieldDecl *FD,
436 CharUnits Offset) {
437 if (!CanPlaceFieldSubobjectAtOffset(FD, Offset))
Anders Carlssondb319762010-05-27 18:20:57 +0000438 return false;
439
440 // We are able to place the member variable at this offset.
441 // Make sure to update the empty base subobject map.
442 UpdateEmptyFieldSubobjects(FD, Offset);
443 return true;
444}
445
446void EmptySubobjectMap::UpdateEmptyFieldSubobjects(const CXXRecordDecl *RD,
447 const CXXRecordDecl *Class,
Anders Carlsson28466ab2010-10-31 22:13:23 +0000448 CharUnits Offset) {
Anders Carlssonae111dc2010-06-13 17:49:16 +0000449 // We know that the only empty subobjects that can conflict with empty
Anders Carlssoncc59cc52010-06-13 18:00:18 +0000450 // field subobjects are subobjects of empty bases that can be placed at offset
Anders Carlssonae111dc2010-06-13 17:49:16 +0000451 // zero. Because of this, we only need to keep track of empty field
452 // subobjects with offsets less than the size of the largest empty
453 // subobject for our class.
454 if (Offset >= SizeOfLargestEmptySubobject)
455 return;
456
Anders Carlsson28466ab2010-10-31 22:13:23 +0000457 AddSubobjectAtOffset(RD, Offset);
Anders Carlssondb319762010-05-27 18:20:57 +0000458
459 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
460
461 // Traverse all non-virtual bases.
462 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
463 E = RD->bases_end(); I != E; ++I) {
464 if (I->isVirtual())
465 continue;
466
467 const CXXRecordDecl *BaseDecl =
468 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
469
Anders Carlsson0a14ee92010-11-01 00:21:58 +0000470 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl);
Anders Carlssondb319762010-05-27 18:20:57 +0000471 UpdateEmptyFieldSubobjects(BaseDecl, Class, BaseOffset);
472 }
473
Anders Carlsson44687202010-06-08 19:09:24 +0000474 if (RD == Class) {
475 // This is the most derived class, traverse virtual bases as well.
476 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
477 E = RD->vbases_end(); I != E; ++I) {
478 const CXXRecordDecl *VBaseDecl =
479 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
480
Anders Carlsson3f018712010-10-31 23:45:59 +0000481 CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBaseDecl);
Anders Carlsson44687202010-06-08 19:09:24 +0000482 UpdateEmptyFieldSubobjects(VBaseDecl, Class, VBaseOffset);
483 }
484 }
485
Anders Carlssondb319762010-05-27 18:20:57 +0000486 // Traverse all member variables.
487 unsigned FieldNo = 0;
488 for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
489 I != E; ++I, ++FieldNo) {
David Blaikie40ed2972012-06-06 20:45:41 +0000490 if (I->isBitField())
Anders Carlsson09814d32010-11-01 15:14:51 +0000491 continue;
492
Anders Carlsson28466ab2010-10-31 22:13:23 +0000493 CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo);
Anders Carlssondb319762010-05-27 18:20:57 +0000494
David Blaikie40ed2972012-06-06 20:45:41 +0000495 UpdateEmptyFieldSubobjects(*I, FieldOffset);
Anders Carlssondb319762010-05-27 18:20:57 +0000496 }
497}
498
499void EmptySubobjectMap::UpdateEmptyFieldSubobjects(const FieldDecl *FD,
Anders Carlsson28466ab2010-10-31 22:13:23 +0000500 CharUnits Offset) {
Anders Carlssondb319762010-05-27 18:20:57 +0000501 QualType T = FD->getType();
502 if (const RecordType *RT = T->getAs<RecordType>()) {
503 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
504 UpdateEmptyFieldSubobjects(RD, RD, Offset);
505 return;
506 }
507
508 // If we have an array type we need to update every element.
509 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) {
510 QualType ElemTy = Context.getBaseElementType(AT);
511 const RecordType *RT = ElemTy->getAs<RecordType>();
512 if (!RT)
513 return;
514
515 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
516 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
517
518 uint64_t NumElements = Context.getConstantArrayElementCount(AT);
Anders Carlsson28466ab2010-10-31 22:13:23 +0000519 CharUnits ElementOffset = Offset;
Anders Carlssondb319762010-05-27 18:20:57 +0000520
521 for (uint64_t I = 0; I != NumElements; ++I) {
Anders Carlssonae111dc2010-06-13 17:49:16 +0000522 // We know that the only empty subobjects that can conflict with empty
Anders Carlssoncc59cc52010-06-13 18:00:18 +0000523 // field subobjects are subobjects of empty bases that can be placed at
Anders Carlssonae111dc2010-06-13 17:49:16 +0000524 // offset zero. Because of this, we only need to keep track of empty field
525 // subobjects with offsets less than the size of the largest empty
526 // subobject for our class.
527 if (ElementOffset >= SizeOfLargestEmptySubobject)
528 return;
529
Anders Carlssondb319762010-05-27 18:20:57 +0000530 UpdateEmptyFieldSubobjects(RD, RD, ElementOffset);
Ken Dyckc8ae5502011-02-09 01:59:34 +0000531 ElementOffset += Layout.getSize();
Anders Carlssondb319762010-05-27 18:20:57 +0000532 }
533 }
534}
535
John McCalle42a3362012-05-01 08:55:32 +0000536typedef llvm::SmallPtrSet<const CXXRecordDecl*, 4> ClassSetTy;
537
Anders Carlssonc2226202010-05-26 05:58:59 +0000538class RecordLayoutBuilder {
Charles Davisc2c576a2010-08-19 00:55:19 +0000539protected:
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000540 // FIXME: Remove this and make the appropriate fields public.
541 friend class clang::ASTContext;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000542
Jay Foad39c79802011-01-12 09:06:06 +0000543 const ASTContext &Context;
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000544
Anders Carlssonf58de112010-05-26 15:32:58 +0000545 EmptySubobjectMap *EmptySubobjects;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000546
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000547 /// Size - The current size of the record layout.
548 uint64_t Size;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000549
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000550 /// Alignment - The current alignment of the record layout.
Ken Dyck4731d5b2011-02-16 02:05:21 +0000551 CharUnits Alignment;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000552
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000553 /// \brief The alignment if attribute packed is not used.
Ken Dyck1300b3b2011-02-16 02:11:31 +0000554 CharUnits UnpackedAlignment;
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000555
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000556 SmallVector<uint64_t, 16> FieldOffsets;
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000557
Douglas Gregore9fc3772012-01-26 07:55:45 +0000558 /// \brief Whether the external AST source has provided a layout for this
559 /// record.
560 unsigned ExternalLayout : 1;
Douglas Gregor44ba7892012-01-28 00:53:29 +0000561
562 /// \brief Whether we need to infer alignment, even when we have an
563 /// externally-provided layout.
564 unsigned InferAlignment : 1;
Douglas Gregore9fc3772012-01-26 07:55:45 +0000565
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000566 /// Packed - Whether the record is packed or not.
Daniel Dunbar6da10982010-05-27 05:45:51 +0000567 unsigned Packed : 1;
568
569 unsigned IsUnion : 1;
570
571 unsigned IsMac68kAlign : 1;
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +0000572
573 unsigned IsMsStruct : 1;
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000574
575 /// UnfilledBitsInLastByte - If the last field laid out was a bitfield,
576 /// this contains the number of bits in the last byte that can be used for
577 /// an adjacent bitfield if necessary.
578 unsigned char UnfilledBitsInLastByte;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000579
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000580 /// MaxFieldAlignment - The maximum allowed field alignment. This is set by
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000581 /// #pragma pack.
Ken Dyck02ced6f2011-02-17 01:49:42 +0000582 CharUnits MaxFieldAlignment;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000583
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000584 /// DataSize - The data size of the record being laid out.
585 uint64_t DataSize;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000586
Ken Dyckaf1c83f2011-02-16 01:52:01 +0000587 CharUnits NonVirtualSize;
Ken Dycka2d3dda2011-02-16 01:43:15 +0000588 CharUnits NonVirtualAlignment;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000589
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +0000590 FieldDecl *ZeroLengthBitfield;
Fariborz Jahanianeb397412011-05-02 17:20:56 +0000591
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000592 /// PrimaryBase - the primary base class (if one exists) of the class
593 /// we're laying out.
594 const CXXRecordDecl *PrimaryBase;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000595
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000596 /// PrimaryBaseIsVirtual - Whether the primary base of the class we're laying
597 /// out is virtual.
598 bool PrimaryBaseIsVirtual;
599
John McCalle42a3362012-05-01 08:55:32 +0000600 /// HasOwnVFPtr - Whether the class provides its own vtable/vftbl
601 /// pointer, as opposed to inheriting one from a primary base class.
602 bool HasOwnVFPtr;
Eli Friedman43114f92011-10-21 22:49:56 +0000603
Eli Friedman84d2d3a2011-09-27 19:12:27 +0000604 /// VBPtrOffset - Virtual base table offset. Only for MS layout.
605 CharUnits VBPtrOffset;
606
Anders Carlsson22f57202010-10-31 21:01:46 +0000607 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetsMapTy;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000608
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000609 /// Bases - base classes and their offsets in the record.
610 BaseOffsetsMapTy Bases;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000611
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000612 // VBases - virtual base classes and their offsets in the record.
John McCalle42a3362012-05-01 08:55:32 +0000613 ASTRecordLayout::VBaseOffsetsMapTy VBases;
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000614
615 /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are
616 /// primary base classes for some other direct or indirect base class.
Anders Carlsson5adde292010-11-24 22:55:48 +0000617 CXXIndirectPrimaryBaseSet IndirectPrimaryBases;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000618
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000619 /// FirstNearlyEmptyVBase - The first nearly empty virtual base class in
620 /// inheritance graph order. Used for determining the primary base class.
621 const CXXRecordDecl *FirstNearlyEmptyVBase;
622
623 /// VisitedVirtualBases - A set of all the visited virtual bases, used to
624 /// avoid visiting virtual bases more than once.
625 llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases;
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000626
Douglas Gregore9fc3772012-01-26 07:55:45 +0000627 /// \brief Externally-provided size.
628 uint64_t ExternalSize;
629
630 /// \brief Externally-provided alignment.
631 uint64_t ExternalAlign;
632
633 /// \brief Externally-provided field offsets.
634 llvm::DenseMap<const FieldDecl *, uint64_t> ExternalFieldOffsets;
635
636 /// \brief Externally-provided direct, non-virtual base offsets.
637 llvm::DenseMap<const CXXRecordDecl *, CharUnits> ExternalBaseOffsets;
638
639 /// \brief Externally-provided virtual base offsets.
640 llvm::DenseMap<const CXXRecordDecl *, CharUnits> ExternalVirtualBaseOffsets;
641
John McCall0153cd32011-11-08 04:01:03 +0000642 RecordLayoutBuilder(const ASTContext &Context,
643 EmptySubobjectMap *EmptySubobjects)
Ken Dyck4731d5b2011-02-16 02:05:21 +0000644 : Context(Context), EmptySubobjects(EmptySubobjects), Size(0),
John McCall0153cd32011-11-08 04:01:03 +0000645 Alignment(CharUnits::One()), UnpackedAlignment(CharUnits::One()),
Douglas Gregor44ba7892012-01-28 00:53:29 +0000646 ExternalLayout(false), InferAlignment(false),
647 Packed(false), IsUnion(false), IsMac68kAlign(false), IsMsStruct(false),
Ken Dyck02ced6f2011-02-17 01:49:42 +0000648 UnfilledBitsInLastByte(0), MaxFieldAlignment(CharUnits::Zero()),
649 DataSize(0), NonVirtualSize(CharUnits::Zero()),
Fariborz Jahanianeb397412011-05-02 17:20:56 +0000650 NonVirtualAlignment(CharUnits::One()),
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +0000651 ZeroLengthBitfield(0), PrimaryBase(0),
Eli Friedman43114f92011-10-21 22:49:56 +0000652 PrimaryBaseIsVirtual(false),
John McCalle42a3362012-05-01 08:55:32 +0000653 HasOwnVFPtr(false),
Eli Friedman43114f92011-10-21 22:49:56 +0000654 VBPtrOffset(CharUnits::fromQuantity(-1)),
Eli Friedman84d2d3a2011-09-27 19:12:27 +0000655 FirstNearlyEmptyVBase(0) { }
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000656
John McCall0153cd32011-11-08 04:01:03 +0000657 /// Reset this RecordLayoutBuilder to a fresh state, using the given
658 /// alignment as the initial alignment. This is used for the
659 /// correct layout of vb-table pointers in MSVC.
660 void resetWithTargetAlignment(CharUnits TargetAlignment) {
661 const ASTContext &Context = this->Context;
662 EmptySubobjectMap *EmptySubobjects = this->EmptySubobjects;
663 this->~RecordLayoutBuilder();
664 new (this) RecordLayoutBuilder(Context, EmptySubobjects);
665 Alignment = UnpackedAlignment = TargetAlignment;
666 }
667
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000668 void Layout(const RecordDecl *D);
Anders Carlssonc28a6c92010-05-26 15:10:00 +0000669 void Layout(const CXXRecordDecl *D);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000670 void Layout(const ObjCInterfaceDecl *D);
671
672 void LayoutFields(const RecordDecl *D);
673 void LayoutField(const FieldDecl *D);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000674 void LayoutWideBitField(uint64_t FieldSize, uint64_t TypeSize,
675 bool FieldPacked, const FieldDecl *D);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000676 void LayoutBitField(const FieldDecl *D);
John McCall0153cd32011-11-08 04:01:03 +0000677
678 bool isMicrosoftCXXABI() const {
679 return Context.getTargetInfo().getCXXABI() == CXXABI_Microsoft;
680 }
681
Eli Friedman84d2d3a2011-09-27 19:12:27 +0000682 void MSLayoutVirtualBases(const CXXRecordDecl *RD);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000683
Anders Carlssone3c24c72010-05-29 17:35:14 +0000684 /// BaseSubobjectInfoAllocator - Allocator for BaseSubobjectInfo objects.
685 llvm::SpecificBumpPtrAllocator<BaseSubobjectInfo> BaseSubobjectInfoAllocator;
686
687 typedef llvm::DenseMap<const CXXRecordDecl *, BaseSubobjectInfo *>
688 BaseSubobjectInfoMapTy;
689
690 /// VirtualBaseInfo - Map from all the (direct or indirect) virtual bases
691 /// of the class we're laying out to their base subobject info.
692 BaseSubobjectInfoMapTy VirtualBaseInfo;
693
694 /// NonVirtualBaseInfo - Map from all the direct non-virtual bases of the
695 /// class we're laying out to their base subobject info.
696 BaseSubobjectInfoMapTy NonVirtualBaseInfo;
697
698 /// ComputeBaseSubobjectInfo - Compute the base subobject information for the
699 /// bases of the given class.
700 void ComputeBaseSubobjectInfo(const CXXRecordDecl *RD);
701
702 /// ComputeBaseSubobjectInfo - Compute the base subobject information for a
703 /// single class and all of its base classes.
704 BaseSubobjectInfo *ComputeBaseSubobjectInfo(const CXXRecordDecl *RD,
705 bool IsVirtual,
706 BaseSubobjectInfo *Derived);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000707
708 /// DeterminePrimaryBase - Determine the primary base of the given class.
709 void DeterminePrimaryBase(const CXXRecordDecl *RD);
710
711 void SelectPrimaryVBase(const CXXRecordDecl *RD);
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000712
Eli Friedman43114f92011-10-21 22:49:56 +0000713 void EnsureVTablePointerAlignment(CharUnits UnpackedBaseAlign);
Charles Davisc2c576a2010-08-19 00:55:19 +0000714
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000715 /// LayoutNonVirtualBases - Determines the primary base class (if any) and
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000716 /// lays it out. Will then proceed to lay out all non-virtual base clasess.
717 void LayoutNonVirtualBases(const CXXRecordDecl *RD);
718
719 /// LayoutNonVirtualBase - Lays out a single non-virtual base.
Anders Carlssonbb0e6782010-05-29 17:42:25 +0000720 void LayoutNonVirtualBase(const BaseSubobjectInfo *Base);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000721
Anders Carlssona2f8e412010-10-31 22:20:42 +0000722 void AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info,
723 CharUnits Offset);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000724
John McCall0153cd32011-11-08 04:01:03 +0000725 bool needsVFTable(const CXXRecordDecl *RD) const;
John McCalle42a3362012-05-01 08:55:32 +0000726 bool hasNewVirtualFunction(const CXXRecordDecl *RD,
727 bool IgnoreDestructor = false) const;
John McCall0153cd32011-11-08 04:01:03 +0000728 bool isPossiblePrimaryBase(const CXXRecordDecl *Base) const;
Eli Friedman5e9534b2011-10-18 00:55:28 +0000729
John McCalle42a3362012-05-01 08:55:32 +0000730 void computeVtordisps(const CXXRecordDecl *RD,
731 ClassSetTy &VtordispVBases);
732
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000733 /// LayoutVirtualBases - Lays out all the virtual bases.
734 void LayoutVirtualBases(const CXXRecordDecl *RD,
735 const CXXRecordDecl *MostDerivedClass);
736
737 /// LayoutVirtualBase - Lays out a single virtual base.
John McCalle42a3362012-05-01 08:55:32 +0000738 void LayoutVirtualBase(const BaseSubobjectInfo *Base,
739 bool IsVtordispNeed = false);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000740
Daniel Dunbar592a85c2010-05-27 02:25:46 +0000741 /// LayoutBase - Will lay out a base and return the offset where it was
Anders Carlssona2f8e412010-10-31 22:20:42 +0000742 /// placed, in chars.
743 CharUnits LayoutBase(const BaseSubobjectInfo *Base);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000744
Anders Carlssonc28a6c92010-05-26 15:10:00 +0000745 /// InitializeLayout - Initialize record layout for the given record decl.
Daniel Dunbar6da10982010-05-27 05:45:51 +0000746 void InitializeLayout(const Decl *D);
Anders Carlssonc28a6c92010-05-26 15:10:00 +0000747
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000748 /// FinishLayout - Finalize record layout. Adjust record size based on the
749 /// alignment.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000750 void FinishLayout(const NamedDecl *D);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000751
Ken Dyck85ef0432011-02-19 18:58:07 +0000752 void UpdateAlignment(CharUnits NewAlignment, CharUnits UnpackedNewAlignment);
753 void UpdateAlignment(CharUnits NewAlignment) {
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000754 UpdateAlignment(NewAlignment, NewAlignment);
755 }
756
Douglas Gregor44ba7892012-01-28 00:53:29 +0000757 /// \brief Retrieve the externally-supplied field offset for the given
758 /// field.
759 ///
760 /// \param Field The field whose offset is being queried.
761 /// \param ComputedOffset The offset that we've computed for this field.
762 uint64_t updateExternalFieldOffset(const FieldDecl *Field,
763 uint64_t ComputedOffset);
764
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000765 void CheckFieldPadding(uint64_t Offset, uint64_t UnpaddedOffset,
766 uint64_t UnpackedOffset, unsigned UnpackedAlign,
767 bool isPacked, const FieldDecl *D);
768
769 DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID);
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000770
Ken Dyckecfc7552011-02-24 01:13:28 +0000771 CharUnits getSize() const {
Ken Dyck3c215f22011-02-24 01:33:05 +0000772 assert(Size % Context.getCharWidth() == 0);
Ken Dyckecfc7552011-02-24 01:13:28 +0000773 return Context.toCharUnitsFromBits(Size);
774 }
775 uint64_t getSizeInBits() const { return Size; }
776
777 void setSize(CharUnits NewSize) { Size = Context.toBits(NewSize); }
778 void setSize(uint64_t NewSize) { Size = NewSize; }
779
Eli Friedman84d2d3a2011-09-27 19:12:27 +0000780 CharUnits getAligment() const { return Alignment; }
781
Ken Dyckecfc7552011-02-24 01:13:28 +0000782 CharUnits getDataSize() const {
Ken Dyck3c215f22011-02-24 01:33:05 +0000783 assert(DataSize % Context.getCharWidth() == 0);
Ken Dyckecfc7552011-02-24 01:13:28 +0000784 return Context.toCharUnitsFromBits(DataSize);
785 }
786 uint64_t getDataSizeInBits() const { return DataSize; }
787
788 void setDataSize(CharUnits NewSize) { DataSize = Context.toBits(NewSize); }
789 void setDataSize(uint64_t NewSize) { DataSize = NewSize; }
790
Argyrios Kyrtzidis499e6e42010-08-15 10:17:39 +0000791 RecordLayoutBuilder(const RecordLayoutBuilder&); // DO NOT IMPLEMENT
792 void operator=(const RecordLayoutBuilder&); // DO NOT IMPLEMENT
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000793public:
794 static const CXXMethodDecl *ComputeKeyFunction(const CXXRecordDecl *RD);
795};
Benjamin Kramerc7656cd2010-05-26 09:58:31 +0000796} // end anonymous namespace
Anders Carlsson35a36eb2010-05-26 05:41:04 +0000797
Anders Carlsson81430692009-09-22 03:02:06 +0000798void
Anders Carlssonc2226202010-05-26 05:58:59 +0000799RecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD) {
Anders Carlssonf2fa75b2010-03-11 03:39:12 +0000800 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000801 E = RD->bases_end(); I != E; ++I) {
Anders Carlssonf2fa75b2010-03-11 03:39:12 +0000802 assert(!I->getType()->isDependentType() &&
Sebastian Redl1054fae2009-10-25 17:03:50 +0000803 "Cannot layout class with dependent bases.");
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000804
Mike Stump11289f42009-09-09 15:08:12 +0000805 const CXXRecordDecl *Base =
Anders Carlssonf2fa75b2010-03-11 03:39:12 +0000806 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000807
Anders Carlssonf2fa75b2010-03-11 03:39:12 +0000808 // Check if this is a nearly empty virtual base.
Anders Carlsson60a62632010-11-25 01:51:53 +0000809 if (I->isVirtual() && Context.isNearlyEmpty(Base)) {
Anders Carlssonf2fa75b2010-03-11 03:39:12 +0000810 // If it's not an indirect primary base, then we've found our primary
811 // base.
Anders Carlsson81430692009-09-22 03:02:06 +0000812 if (!IndirectPrimaryBases.count(Base)) {
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000813 PrimaryBase = Base;
814 PrimaryBaseIsVirtual = true;
Mike Stump6f3793b2009-08-12 21:50:08 +0000815 return;
816 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000817
Anders Carlssonf2fa75b2010-03-11 03:39:12 +0000818 // Is this the first nearly empty virtual base?
819 if (!FirstNearlyEmptyVBase)
820 FirstNearlyEmptyVBase = Base;
Mike Stump6f3793b2009-08-12 21:50:08 +0000821 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000822
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000823 SelectPrimaryVBase(Base);
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000824 if (PrimaryBase)
Zhongxing Xuec345b72010-02-15 04:28:35 +0000825 return;
Mike Stump6f3793b2009-08-12 21:50:08 +0000826 }
827}
828
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000829/// DeterminePrimaryBase - Determine the primary base of the given class.
Anders Carlssonc2226202010-05-26 05:58:59 +0000830void RecordLayoutBuilder::DeterminePrimaryBase(const CXXRecordDecl *RD) {
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000831 // If the class isn't dynamic, it won't have a primary base.
832 if (!RD->isDynamicClass())
833 return;
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000834
Anders Carlsson81430692009-09-22 03:02:06 +0000835 // Compute all the primary virtual bases for all of our direct and
Mike Stump590a7c72009-08-13 23:26:06 +0000836 // indirect bases, and record all their primary virtual base classes.
Anders Carlsson5adde292010-11-24 22:55:48 +0000837 RD->getIndirectPrimaryBases(IndirectPrimaryBases);
Mike Stump590a7c72009-08-13 23:26:06 +0000838
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000839 // If the record has a dynamic base class, attempt to choose a primary base
840 // class. It is the first (in direct base class order) non-virtual dynamic
Anders Carlsson81430692009-09-22 03:02:06 +0000841 // base class, if one exists.
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000842 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000843 e = RD->bases_end(); i != e; ++i) {
Anders Carlsson03ff3792009-11-27 22:05:05 +0000844 // Ignore virtual bases.
845 if (i->isVirtual())
846 continue;
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000847
Anders Carlsson03ff3792009-11-27 22:05:05 +0000848 const CXXRecordDecl *Base =
849 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
850
John McCall0153cd32011-11-08 04:01:03 +0000851 if (isPossiblePrimaryBase(Base)) {
Anders Carlsson03ff3792009-11-27 22:05:05 +0000852 // We found it.
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000853 PrimaryBase = Base;
854 PrimaryBaseIsVirtual = false;
Anders Carlsson03ff3792009-11-27 22:05:05 +0000855 return;
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000856 }
857 }
858
Eli Friedman5e9534b2011-10-18 00:55:28 +0000859 // The Microsoft ABI doesn't have primary virtual bases.
John McCall0153cd32011-11-08 04:01:03 +0000860 if (isMicrosoftCXXABI()) {
Eli Friedman5e9534b2011-10-18 00:55:28 +0000861 assert(!PrimaryBase && "Should not get here with a primary base!");
862 return;
863 }
864
865 // Under the Itanium ABI, if there is no non-virtual primary base class,
866 // try to compute the primary virtual base. The primary virtual base is
867 // the first nearly empty virtual base that is not an indirect primary
868 // virtual base class, if one exists.
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000869 if (RD->getNumVBases() != 0) {
870 SelectPrimaryVBase(RD);
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000871 if (PrimaryBase)
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000872 return;
873 }
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000874
Eli Friedman5e9534b2011-10-18 00:55:28 +0000875 // Otherwise, it is the first indirect primary base class, if one exists.
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000876 if (FirstNearlyEmptyVBase) {
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000877 PrimaryBase = FirstNearlyEmptyVBase;
878 PrimaryBaseIsVirtual = true;
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000879 return;
Anders Carlsson8630b5b2010-03-11 00:15:35 +0000880 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +0000881
Anders Carlssond20e7cd2010-05-26 05:20:58 +0000882 assert(!PrimaryBase && "Should not get here with a primary base!");
Mike Stumpd8fe7b22009-08-05 22:37:18 +0000883}
884
Anders Carlssone3c24c72010-05-29 17:35:14 +0000885BaseSubobjectInfo *
886RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD,
887 bool IsVirtual,
888 BaseSubobjectInfo *Derived) {
889 BaseSubobjectInfo *Info;
890
891 if (IsVirtual) {
892 // Check if we already have info about this virtual base.
893 BaseSubobjectInfo *&InfoSlot = VirtualBaseInfo[RD];
894 if (InfoSlot) {
895 assert(InfoSlot->Class == RD && "Wrong class for virtual base info!");
896 return InfoSlot;
897 }
898
899 // We don't, create it.
900 InfoSlot = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo;
901 Info = InfoSlot;
902 } else {
903 Info = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo;
904 }
905
906 Info->Class = RD;
907 Info->IsVirtual = IsVirtual;
908 Info->Derived = 0;
909 Info->PrimaryVirtualBaseInfo = 0;
910
911 const CXXRecordDecl *PrimaryVirtualBase = 0;
912 BaseSubobjectInfo *PrimaryVirtualBaseInfo = 0;
913
914 // Check if this base has a primary virtual base.
915 if (RD->getNumVBases()) {
916 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlsson7f95cd12010-11-24 23:12:57 +0000917 if (Layout.isPrimaryBaseVirtual()) {
Anders Carlssone3c24c72010-05-29 17:35:14 +0000918 // This base does have a primary virtual base.
919 PrimaryVirtualBase = Layout.getPrimaryBase();
920 assert(PrimaryVirtualBase && "Didn't have a primary virtual base!");
921
922 // Now check if we have base subobject info about this primary base.
923 PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase);
924
925 if (PrimaryVirtualBaseInfo) {
926 if (PrimaryVirtualBaseInfo->Derived) {
927 // We did have info about this primary base, and it turns out that it
928 // has already been claimed as a primary virtual base for another
929 // base.
930 PrimaryVirtualBase = 0;
931 } else {
932 // We can claim this base as our primary base.
933 Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo;
934 PrimaryVirtualBaseInfo->Derived = Info;
935 }
936 }
937 }
938 }
939
940 // Now go through all direct bases.
941 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
942 E = RD->bases_end(); I != E; ++I) {
943 bool IsVirtual = I->isVirtual();
944
945 const CXXRecordDecl *BaseDecl =
946 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
947
948 Info->Bases.push_back(ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, Info));
949 }
950
951 if (PrimaryVirtualBase && !PrimaryVirtualBaseInfo) {
952 // Traversing the bases must have created the base info for our primary
953 // virtual base.
954 PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase);
955 assert(PrimaryVirtualBaseInfo &&
956 "Did not create a primary virtual base!");
957
958 // Claim the primary virtual base as our primary virtual base.
959 Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo;
960 PrimaryVirtualBaseInfo->Derived = Info;
961 }
962
963 return Info;
964}
965
966void RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD) {
967 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
968 E = RD->bases_end(); I != E; ++I) {
969 bool IsVirtual = I->isVirtual();
970
971 const CXXRecordDecl *BaseDecl =
972 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
973
974 // Compute the base subobject info for this base.
975 BaseSubobjectInfo *Info = ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, 0);
976
977 if (IsVirtual) {
978 // ComputeBaseInfo has already added this base for us.
979 assert(VirtualBaseInfo.count(BaseDecl) &&
980 "Did not add virtual base!");
981 } else {
982 // Add the base info to the map of non-virtual bases.
983 assert(!NonVirtualBaseInfo.count(BaseDecl) &&
984 "Non-virtual base already exists!");
985 NonVirtualBaseInfo.insert(std::make_pair(BaseDecl, Info));
986 }
987 }
988}
989
Anders Carlsson09ffa322010-03-10 22:21:28 +0000990void
Eli Friedman43114f92011-10-21 22:49:56 +0000991RecordLayoutBuilder::EnsureVTablePointerAlignment(CharUnits UnpackedBaseAlign) {
Eli Friedman5e9534b2011-10-18 00:55:28 +0000992 CharUnits BaseAlign = (Packed) ? CharUnits::One() : UnpackedBaseAlign;
993
994 // The maximum field alignment overrides base align.
995 if (!MaxFieldAlignment.isZero()) {
996 BaseAlign = std::min(BaseAlign, MaxFieldAlignment);
997 UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment);
998 }
999
1000 // Round up the current record size to pointer alignment.
Eli Friedman43114f92011-10-21 22:49:56 +00001001 setSize(getSize().RoundUpToAlignment(BaseAlign));
1002 setDataSize(getSize());
Eli Friedman5e9534b2011-10-18 00:55:28 +00001003
1004 // Update the alignment.
1005 UpdateAlignment(BaseAlign, UnpackedBaseAlign);
1006}
1007
1008void
Anders Carlssonc2226202010-05-26 05:58:59 +00001009RecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) {
Anders Carlssone3c24c72010-05-29 17:35:14 +00001010 // Then, determine the primary base class.
Anders Carlsson8630b5b2010-03-11 00:15:35 +00001011 DeterminePrimaryBase(RD);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001012
Anders Carlssone3c24c72010-05-29 17:35:14 +00001013 // Compute base subobject info.
1014 ComputeBaseSubobjectInfo(RD);
1015
Anders Carlsson8630b5b2010-03-11 00:15:35 +00001016 // If we have a primary base class, lay it out.
Anders Carlssond20e7cd2010-05-26 05:20:58 +00001017 if (PrimaryBase) {
1018 if (PrimaryBaseIsVirtual) {
Anders Carlssone3c24c72010-05-29 17:35:14 +00001019 // If the primary virtual base was a primary virtual base of some other
1020 // base class we'll have to steal it.
1021 BaseSubobjectInfo *PrimaryBaseInfo = VirtualBaseInfo.lookup(PrimaryBase);
1022 PrimaryBaseInfo->Derived = 0;
1023
Anders Carlsson8630b5b2010-03-11 00:15:35 +00001024 // We have a virtual primary base, insert it as an indirect primary base.
Anders Carlssond20e7cd2010-05-26 05:20:58 +00001025 IndirectPrimaryBases.insert(PrimaryBase);
Anders Carlssonfe900962010-03-11 05:42:17 +00001026
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001027 assert(!VisitedVirtualBases.count(PrimaryBase) &&
Anders Carlssond20e7cd2010-05-26 05:20:58 +00001028 "vbase already visited!");
1029 VisitedVirtualBases.insert(PrimaryBase);
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001030
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001031 LayoutVirtualBase(PrimaryBaseInfo);
Anders Carlssonbb0e6782010-05-29 17:42:25 +00001032 } else {
1033 BaseSubobjectInfo *PrimaryBaseInfo =
1034 NonVirtualBaseInfo.lookup(PrimaryBase);
1035 assert(PrimaryBaseInfo &&
1036 "Did not find base info for non-virtual primary base!");
1037
1038 LayoutNonVirtualBase(PrimaryBaseInfo);
1039 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001040
John McCall0153cd32011-11-08 04:01:03 +00001041 // If this class needs a vtable/vf-table and didn't get one from a
1042 // primary base, add it in now.
1043 } else if (needsVFTable(RD)) {
Eli Friedman5e9534b2011-10-18 00:55:28 +00001044 assert(DataSize == 0 && "Vtable pointer must be at offset zero!");
Eli Friedman5e9534b2011-10-18 00:55:28 +00001045 CharUnits PtrWidth =
1046 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
Eli Friedman43114f92011-10-21 22:49:56 +00001047 CharUnits PtrAlign =
1048 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(0));
1049 EnsureVTablePointerAlignment(PtrAlign);
John McCalle42a3362012-05-01 08:55:32 +00001050 HasOwnVFPtr = true;
Eli Friedman5e9534b2011-10-18 00:55:28 +00001051 setSize(getSize() + PtrWidth);
1052 setDataSize(getSize());
1053 }
1054
John McCall0153cd32011-11-08 04:01:03 +00001055 bool HasDirectVirtualBases = false;
1056 bool HasNonVirtualBaseWithVBTable = false;
1057
Anders Carlsson8630b5b2010-03-11 00:15:35 +00001058 // Now lay out the non-virtual bases.
1059 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001060 E = RD->bases_end(); I != E; ++I) {
Anders Carlsson8630b5b2010-03-11 00:15:35 +00001061
John McCall0153cd32011-11-08 04:01:03 +00001062 // Ignore virtual bases, but remember that we saw one.
1063 if (I->isVirtual()) {
1064 HasDirectVirtualBases = true;
Anders Carlsson8630b5b2010-03-11 00:15:35 +00001065 continue;
John McCall0153cd32011-11-08 04:01:03 +00001066 }
Anders Carlsson8630b5b2010-03-11 00:15:35 +00001067
Anders Carlssonbb0e6782010-05-29 17:42:25 +00001068 const CXXRecordDecl *BaseDecl =
John McCall0153cd32011-11-08 04:01:03 +00001069 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
Anders Carlsson8630b5b2010-03-11 00:15:35 +00001070
John McCall0153cd32011-11-08 04:01:03 +00001071 // Remember if this base has virtual bases itself.
1072 if (BaseDecl->getNumVBases())
1073 HasNonVirtualBaseWithVBTable = true;
1074
1075 // Skip the primary base, because we've already laid it out. The
1076 // !PrimaryBaseIsVirtual check is required because we might have a
1077 // non-virtual base of the same type as a primary virtual base.
Anders Carlssonbb0e6782010-05-29 17:42:25 +00001078 if (BaseDecl == PrimaryBase && !PrimaryBaseIsVirtual)
Anders Carlsson8630b5b2010-03-11 00:15:35 +00001079 continue;
1080
1081 // Lay out the base.
Anders Carlssonbb0e6782010-05-29 17:42:25 +00001082 BaseSubobjectInfo *BaseInfo = NonVirtualBaseInfo.lookup(BaseDecl);
1083 assert(BaseInfo && "Did not find base info for non-virtual base!");
1084
1085 LayoutNonVirtualBase(BaseInfo);
Anders Carlsson09ffa322010-03-10 22:21:28 +00001086 }
Eli Friedman5e9534b2011-10-18 00:55:28 +00001087
John McCall0153cd32011-11-08 04:01:03 +00001088 // In the MS ABI, add the vb-table pointer if we need one, which is
1089 // whenever we have a virtual base and we can't re-use a vb-table
1090 // pointer from a non-virtual base.
1091 if (isMicrosoftCXXABI() &&
1092 HasDirectVirtualBases && !HasNonVirtualBaseWithVBTable) {
Eli Friedman5e9534b2011-10-18 00:55:28 +00001093 CharUnits PtrWidth =
1094 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
Eli Friedman43114f92011-10-21 22:49:56 +00001095 CharUnits PtrAlign =
1096 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(0));
John McCall0153cd32011-11-08 04:01:03 +00001097
1098 // MSVC potentially over-aligns the vb-table pointer by giving it
1099 // the max alignment of all the non-virtual objects in the class.
1100 // This is completely unnecessary, but we're not here to pass
1101 // judgment.
1102 //
1103 // Note that we've only laid out the non-virtual bases, so on the
1104 // first pass Alignment won't be set correctly here, but if the
1105 // vb-table doesn't end up aligned correctly we'll come through
1106 // and redo the layout from scratch with the right alignment.
1107 //
1108 // TODO: Instead of doing this, just lay out the fields as if the
1109 // vb-table were at offset zero, then retroactively bump the field
1110 // offsets up.
Eli Friedman43114f92011-10-21 22:49:56 +00001111 PtrAlign = std::max(PtrAlign, Alignment);
John McCall0153cd32011-11-08 04:01:03 +00001112
1113 EnsureVTablePointerAlignment(PtrAlign);
1114 VBPtrOffset = getSize();
1115 setSize(getSize() + PtrWidth);
1116 setDataSize(getSize());
Eli Friedman5e9534b2011-10-18 00:55:28 +00001117 }
Anders Carlsson09ffa322010-03-10 22:21:28 +00001118}
1119
Anders Carlssonbb0e6782010-05-29 17:42:25 +00001120void RecordLayoutBuilder::LayoutNonVirtualBase(const BaseSubobjectInfo *Base) {
Anders Carlsson0d0b5882010-03-10 22:26:24 +00001121 // Layout the base.
Anders Carlssona2f8e412010-10-31 22:20:42 +00001122 CharUnits Offset = LayoutBase(Base);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001123
Anders Carlsson0d0b5882010-03-10 22:26:24 +00001124 // Add its base class offset.
Anders Carlssonbb0e6782010-05-29 17:42:25 +00001125 assert(!Bases.count(Base->Class) && "base offset already exists!");
Anders Carlssona2f8e412010-10-31 22:20:42 +00001126 Bases.insert(std::make_pair(Base->Class, Offset));
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001127
1128 AddPrimaryVirtualBaseOffsets(Base, Offset);
Anders Carlsson09ffa322010-03-10 22:21:28 +00001129}
Mike Stump2b84dd32009-11-05 04:02:15 +00001130
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001131void
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001132RecordLayoutBuilder::AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info,
Anders Carlssona2f8e412010-10-31 22:20:42 +00001133 CharUnits Offset) {
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001134 // This base isn't interesting, it has no virtual bases.
1135 if (!Info->Class->getNumVBases())
1136 return;
1137
1138 // First, check if we have a virtual primary base to add offsets for.
1139 if (Info->PrimaryVirtualBaseInfo) {
1140 assert(Info->PrimaryVirtualBaseInfo->IsVirtual &&
1141 "Primary virtual base is not virtual!");
1142 if (Info->PrimaryVirtualBaseInfo->Derived == Info) {
1143 // Add the offset.
1144 assert(!VBases.count(Info->PrimaryVirtualBaseInfo->Class) &&
1145 "primary vbase offset already exists!");
1146 VBases.insert(std::make_pair(Info->PrimaryVirtualBaseInfo->Class,
John McCalle42a3362012-05-01 08:55:32 +00001147 ASTRecordLayout::VBaseInfo(Offset, false)));
Anders Carlssonea7b1822010-04-15 16:12:58 +00001148
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001149 // Traverse the primary virtual base.
1150 AddPrimaryVirtualBaseOffsets(Info->PrimaryVirtualBaseInfo, Offset);
1151 }
Anders Carlssonea7b1822010-04-15 16:12:58 +00001152 }
1153
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001154 // Now go through all direct non-virtual bases.
1155 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class);
1156 for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) {
1157 const BaseSubobjectInfo *Base = Info->Bases[I];
1158 if (Base->IsVirtual)
Anders Carlssonea7b1822010-04-15 16:12:58 +00001159 continue;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001160
Anders Carlsson0a14ee92010-11-01 00:21:58 +00001161 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class);
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001162 AddPrimaryVirtualBaseOffsets(Base, BaseOffset);
Anders Carlssonea7b1822010-04-15 16:12:58 +00001163 }
1164}
1165
John McCall0153cd32011-11-08 04:01:03 +00001166/// needsVFTable - Return true if this class needs a vtable or vf-table
1167/// when laid out as a base class. These are treated the same because
1168/// they're both always laid out at offset zero.
1169///
1170/// This function assumes that the class has no primary base.
1171bool RecordLayoutBuilder::needsVFTable(const CXXRecordDecl *RD) const {
1172 assert(!PrimaryBase);
1173
1174 // In the Itanium ABI, every dynamic class needs a vtable: even if
1175 // this class has no virtual functions as a base class (i.e. it's
1176 // non-polymorphic or only has virtual functions from virtual
1177 // bases),x it still needs a vtable to locate its virtual bases.
1178 if (!isMicrosoftCXXABI())
1179 return RD->isDynamicClass();
1180
1181 // In the MS ABI, we need a vfptr if the class has virtual functions
1182 // other than those declared by its virtual bases. The AST doesn't
1183 // tell us that directly, and checking manually for virtual
1184 // functions that aren't overrides is expensive, but there are
1185 // some important shortcuts:
1186
1187 // - Non-polymorphic classes have no virtual functions at all.
1188 if (!RD->isPolymorphic()) return false;
1189
1190 // - Polymorphic classes with no virtual bases must either declare
1191 // virtual functions directly or inherit them, but in the latter
1192 // case we would have a primary base.
1193 if (RD->getNumVBases() == 0) return true;
1194
1195 return hasNewVirtualFunction(RD);
1196}
1197
John McCalle42a3362012-05-01 08:55:32 +00001198/// Does the given class inherit non-virtually from any of the classes
1199/// in the given set?
1200static bool hasNonVirtualBaseInSet(const CXXRecordDecl *RD,
1201 const ClassSetTy &set) {
1202 for (CXXRecordDecl::base_class_const_iterator
1203 I = RD->bases_begin(), E = RD->bases_end(); I != E; ++I) {
1204 // Ignore virtual links.
1205 if (I->isVirtual()) continue;
1206
1207 // Check whether the set contains the base.
1208 const CXXRecordDecl *base = I->getType()->getAsCXXRecordDecl();
1209 if (set.count(base))
1210 return true;
1211
1212 // Otherwise, recurse and propagate.
1213 if (hasNonVirtualBaseInSet(base, set))
1214 return true;
1215 }
1216
1217 return false;
1218}
1219
1220/// Does the given method (B::foo()) already override a method (A::foo())
1221/// such that A requires a vtordisp in B? If so, we don't need to add a
1222/// new vtordisp for B in a yet-more-derived class C providing C::foo().
1223static bool overridesMethodRequiringVtorDisp(const ASTContext &Context,
1224 const CXXMethodDecl *M) {
1225 CXXMethodDecl::method_iterator
1226 I = M->begin_overridden_methods(), E = M->end_overridden_methods();
1227 if (I == E) return false;
1228
1229 const ASTRecordLayout::VBaseOffsetsMapTy &offsets =
1230 Context.getASTRecordLayout(M->getParent()).getVBaseOffsetsMap();
1231 do {
1232 const CXXMethodDecl *overridden = *I;
1233
1234 // If the overridden method's class isn't recognized as a virtual
1235 // base in the derived class, ignore it.
1236 ASTRecordLayout::VBaseOffsetsMapTy::const_iterator
1237 it = offsets.find(overridden->getParent());
1238 if (it == offsets.end()) continue;
1239
1240 // Otherwise, check if the overridden method's class needs a vtordisp.
1241 if (it->second.hasVtorDisp()) return true;
1242
1243 } while (++I != E);
1244 return false;
1245}
1246
1247/// In the Microsoft ABI, decide which of the virtual bases require a
1248/// vtordisp field.
1249void RecordLayoutBuilder::computeVtordisps(const CXXRecordDecl *RD,
1250 ClassSetTy &vtordispVBases) {
1251 // Bail out if we have no virtual bases.
1252 assert(RD->getNumVBases());
1253
1254 // Build up the set of virtual bases that we haven't decided yet.
1255 ClassSetTy undecidedVBases;
1256 for (CXXRecordDecl::base_class_const_iterator
1257 I = RD->vbases_begin(), E = RD->vbases_end(); I != E; ++I) {
1258 const CXXRecordDecl *vbase = I->getType()->getAsCXXRecordDecl();
1259 undecidedVBases.insert(vbase);
1260 }
1261 assert(!undecidedVBases.empty());
1262
1263 // A virtual base requires a vtordisp field in a derived class if it
1264 // requires a vtordisp field in a base class. Walk all the direct
1265 // bases and collect this information.
1266 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1267 E = RD->bases_end(); I != E; ++I) {
1268 const CXXRecordDecl *base = I->getType()->getAsCXXRecordDecl();
1269 const ASTRecordLayout &baseLayout = Context.getASTRecordLayout(base);
1270
1271 // Iterate over the set of virtual bases provided by this class.
1272 for (ASTRecordLayout::VBaseOffsetsMapTy::const_iterator
1273 VI = baseLayout.getVBaseOffsetsMap().begin(),
1274 VE = baseLayout.getVBaseOffsetsMap().end(); VI != VE; ++VI) {
1275 // If it doesn't need a vtordisp in this base, ignore it.
1276 if (!VI->second.hasVtorDisp()) continue;
1277
1278 // If we've already seen it and decided it needs a vtordisp, ignore it.
1279 if (!undecidedVBases.erase(VI->first))
1280 continue;
1281
1282 // Add it.
1283 vtordispVBases.insert(VI->first);
1284
1285 // Quit as soon as we've decided everything.
1286 if (undecidedVBases.empty())
1287 return;
1288 }
1289 }
1290
1291 // Okay, we have virtual bases that we haven't yet decided about. A
1292 // virtual base requires a vtordisp if any the non-destructor
1293 // virtual methods declared in this class directly override a method
1294 // provided by that virtual base. (If so, we need to emit a thunk
1295 // for that method, to be used in the construction vftable, which
1296 // applies an additional 'vtordisp' this-adjustment.)
1297
1298 // Collect the set of bases directly overridden by any method in this class.
1299 // It's possible that some of these classes won't be virtual bases, or won't be
1300 // provided by virtual bases, or won't be virtual bases in the overridden
1301 // instance but are virtual bases elsewhere. Only the last matters for what
1302 // we're doing, and we can ignore those: if we don't directly override
1303 // a method provided by a virtual copy of a base class, but we do directly
1304 // override a method provided by a non-virtual copy of that base class,
1305 // then we must indirectly override the method provided by the virtual base,
1306 // and so we should already have collected it in the loop above.
1307 ClassSetTy overriddenBases;
1308 for (CXXRecordDecl::method_iterator
1309 M = RD->method_begin(), E = RD->method_end(); M != E; ++M) {
1310 // Ignore non-virtual methods and destructors.
1311 if (isa<CXXDestructorDecl>(*M) || !M->isVirtual())
1312 continue;
1313
1314 for (CXXMethodDecl::method_iterator I = M->begin_overridden_methods(),
1315 E = M->end_overridden_methods(); I != E; ++I) {
1316 const CXXMethodDecl *overriddenMethod = (*I);
1317
1318 // Ignore methods that override methods from vbases that require
1319 // require vtordisps.
1320 if (overridesMethodRequiringVtorDisp(Context, overriddenMethod))
1321 continue;
1322
1323 // As an optimization, check immediately whether we're overriding
1324 // something from the undecided set.
1325 const CXXRecordDecl *overriddenBase = overriddenMethod->getParent();
1326 if (undecidedVBases.erase(overriddenBase)) {
1327 vtordispVBases.insert(overriddenBase);
1328 if (undecidedVBases.empty()) return;
1329
1330 // We can't 'continue;' here because one of our undecided
1331 // vbases might non-virtually inherit from this base.
1332 // Consider:
1333 // struct A { virtual void foo(); };
1334 // struct B : A {};
1335 // struct C : virtual A, virtual B { virtual void foo(); };
1336 // We need a vtordisp for B here.
1337 }
1338
1339 // Otherwise, just collect it.
1340 overriddenBases.insert(overriddenBase);
1341 }
1342 }
1343
1344 // Walk the undecided v-bases and check whether they (non-virtually)
1345 // provide any of the overridden bases. We don't need to consider
1346 // virtual links because the vtordisp inheres to the layout
1347 // subobject containing the base.
1348 for (ClassSetTy::const_iterator
1349 I = undecidedVBases.begin(), E = undecidedVBases.end(); I != E; ++I) {
1350 if (hasNonVirtualBaseInSet(*I, overriddenBases))
1351 vtordispVBases.insert(*I);
1352 }
1353}
1354
John McCall0153cd32011-11-08 04:01:03 +00001355/// hasNewVirtualFunction - Does the given polymorphic class declare a
1356/// virtual function that does not override a method from any of its
1357/// base classes?
Eli Friedman84d2d3a2011-09-27 19:12:27 +00001358bool
John McCalle42a3362012-05-01 08:55:32 +00001359RecordLayoutBuilder::hasNewVirtualFunction(const CXXRecordDecl *RD,
1360 bool IgnoreDestructor) const {
John McCall0153cd32011-11-08 04:01:03 +00001361 if (!RD->getNumBases())
1362 return true;
1363
Eli Friedman84d2d3a2011-09-27 19:12:27 +00001364 for (CXXRecordDecl::method_iterator method = RD->method_begin();
1365 method != RD->method_end();
1366 ++method) {
John McCalle42a3362012-05-01 08:55:32 +00001367 if (method->isVirtual() && !method->size_overridden_methods() &&
1368 !(IgnoreDestructor && method->getKind() == Decl::CXXDestructor)) {
Eli Friedman84d2d3a2011-09-27 19:12:27 +00001369 return true;
1370 }
1371 }
1372 return false;
1373}
1374
John McCall0153cd32011-11-08 04:01:03 +00001375/// isPossiblePrimaryBase - Is the given base class an acceptable
1376/// primary base class?
Eli Friedman5e9534b2011-10-18 00:55:28 +00001377bool
John McCalle42a3362012-05-01 08:55:32 +00001378RecordLayoutBuilder::isPossiblePrimaryBase(const CXXRecordDecl *base) const {
John McCall0153cd32011-11-08 04:01:03 +00001379 // In the Itanium ABI, a class can be a primary base class if it has
1380 // a vtable for any reason.
1381 if (!isMicrosoftCXXABI())
John McCalle42a3362012-05-01 08:55:32 +00001382 return base->isDynamicClass();
John McCall0153cd32011-11-08 04:01:03 +00001383
1384 // In the MS ABI, a class can only be a primary base class if it
1385 // provides a vf-table at a static offset. That means it has to be
1386 // non-virtual base. The existence of a separate vb-table means
1387 // that it's possible to get virtual functions only from a virtual
1388 // base, which we have to guard against.
1389
1390 // First off, it has to have virtual functions.
John McCalle42a3362012-05-01 08:55:32 +00001391 if (!base->isPolymorphic()) return false;
John McCall0153cd32011-11-08 04:01:03 +00001392
John McCalle42a3362012-05-01 08:55:32 +00001393 // If it has no virtual bases, then the vfptr must be at a static offset.
1394 if (!base->getNumVBases()) return true;
1395
1396 // Otherwise, the necessary information is cached in the layout.
1397 const ASTRecordLayout &layout = Context.getASTRecordLayout(base);
John McCall0153cd32011-11-08 04:01:03 +00001398
John McCalle42a3362012-05-01 08:55:32 +00001399 // If the base has its own vfptr, it can be a primary base.
1400 if (layout.hasOwnVFPtr()) return true;
1401
1402 // If the base has a primary base class, then it can be a primary base.
1403 if (layout.getPrimaryBase()) return true;
1404
1405 // Otherwise it can't.
1406 return false;
Eli Friedman84d2d3a2011-09-27 19:12:27 +00001407}
1408
Anders Carlssonea7b1822010-04-15 16:12:58 +00001409void
Anders Carlssonc2226202010-05-26 05:58:59 +00001410RecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
Anders Carlssonea7b1822010-04-15 16:12:58 +00001411 const CXXRecordDecl *MostDerivedClass) {
Anders Carlssonde710c92010-03-11 04:33:54 +00001412 const CXXRecordDecl *PrimaryBase;
Anders Carlsson291279e2010-04-10 18:42:27 +00001413 bool PrimaryBaseIsVirtual;
Anders Carlssonfe900962010-03-11 05:42:17 +00001414
Anders Carlsson291279e2010-04-10 18:42:27 +00001415 if (MostDerivedClass == RD) {
Anders Carlssond20e7cd2010-05-26 05:20:58 +00001416 PrimaryBase = this->PrimaryBase;
1417 PrimaryBaseIsVirtual = this->PrimaryBaseIsVirtual;
Anders Carlsson291279e2010-04-10 18:42:27 +00001418 } else {
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001419 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlssonde710c92010-03-11 04:33:54 +00001420 PrimaryBase = Layout.getPrimaryBase();
Anders Carlsson7f95cd12010-11-24 23:12:57 +00001421 PrimaryBaseIsVirtual = Layout.isPrimaryBaseVirtual();
Anders Carlsson291279e2010-04-10 18:42:27 +00001422 }
1423
Anders Carlssonf7b7a1e2010-03-11 04:24:02 +00001424 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1425 E = RD->bases_end(); I != E; ++I) {
1426 assert(!I->getType()->isDependentType() &&
Sebastian Redl1054fae2009-10-25 17:03:50 +00001427 "Cannot layout class with dependent bases.");
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001428
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001429 const CXXRecordDecl *BaseDecl =
John McCall0153cd32011-11-08 04:01:03 +00001430 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
Anders Carlssonf7b7a1e2010-03-11 04:24:02 +00001431
1432 if (I->isVirtual()) {
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001433 if (PrimaryBase != BaseDecl || !PrimaryBaseIsVirtual) {
1434 bool IndirectPrimaryBase = IndirectPrimaryBases.count(BaseDecl);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001435
Anders Carlsson291279e2010-04-10 18:42:27 +00001436 // Only lay out the virtual base if it's not an indirect primary base.
1437 if (!IndirectPrimaryBase) {
1438 // Only visit virtual bases once.
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001439 if (!VisitedVirtualBases.insert(BaseDecl))
Anders Carlsson291279e2010-04-10 18:42:27 +00001440 continue;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001441
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001442 const BaseSubobjectInfo *BaseInfo = VirtualBaseInfo.lookup(BaseDecl);
1443 assert(BaseInfo && "Did not find virtual base info!");
1444 LayoutVirtualBase(BaseInfo);
Anders Carlsson6a848892010-03-11 04:10:39 +00001445 }
Mike Stump2b84dd32009-11-05 04:02:15 +00001446 }
Mike Stumpc2f591b2009-08-13 22:53:07 +00001447 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001448
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001449 if (!BaseDecl->getNumVBases()) {
Anders Carlssonf7b7a1e2010-03-11 04:24:02 +00001450 // This base isn't interesting since it doesn't have any virtual bases.
1451 continue;
Mike Stump996576f32009-08-16 19:04:13 +00001452 }
Anders Carlssonf7b7a1e2010-03-11 04:24:02 +00001453
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001454 LayoutVirtualBases(BaseDecl, MostDerivedClass);
Mike Stump6b2556f2009-08-06 13:41:24 +00001455 }
1456}
1457
John McCall0153cd32011-11-08 04:01:03 +00001458void RecordLayoutBuilder::MSLayoutVirtualBases(const CXXRecordDecl *RD) {
John McCall0153cd32011-11-08 04:01:03 +00001459 if (!RD->getNumVBases())
1460 return;
1461
John McCalle42a3362012-05-01 08:55:32 +00001462 ClassSetTy VtordispVBases;
1463 computeVtordisps(RD, VtordispVBases);
1464
John McCall0153cd32011-11-08 04:01:03 +00001465 // This is substantially simplified because there are no virtual
1466 // primary bases.
1467 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
1468 E = RD->vbases_end(); I != E; ++I) {
1469 const CXXRecordDecl *BaseDecl = I->getType()->getAsCXXRecordDecl();
1470 const BaseSubobjectInfo *BaseInfo = VirtualBaseInfo.lookup(BaseDecl);
1471 assert(BaseInfo && "Did not find virtual base info!");
John McCalle42a3362012-05-01 08:55:32 +00001472
1473 // If this base requires a vtordisp, add enough space for an int field.
1474 // This is apparently always 32-bits, even on x64.
1475 bool vtordispNeeded = false;
1476 if (VtordispVBases.count(BaseDecl)) {
1477 CharUnits IntSize =
1478 CharUnits::fromQuantity(Context.getTargetInfo().getIntWidth() / 8);
1479
1480 setSize(getSize() + IntSize);
1481 setDataSize(getSize());
1482 vtordispNeeded = true;
1483 }
1484
1485 LayoutVirtualBase(BaseInfo, vtordispNeeded);
John McCall0153cd32011-11-08 04:01:03 +00001486 }
1487}
1488
John McCalle42a3362012-05-01 08:55:32 +00001489void RecordLayoutBuilder::LayoutVirtualBase(const BaseSubobjectInfo *Base,
1490 bool IsVtordispNeed) {
Anders Carlsson6b0d9142010-05-29 19:44:50 +00001491 assert(!Base->Derived && "Trying to lay out a primary virtual base!");
1492
Anders Carlsson0d0b5882010-03-10 22:26:24 +00001493 // Layout the base.
Anders Carlssona2f8e412010-10-31 22:20:42 +00001494 CharUnits Offset = LayoutBase(Base);
Anders Carlsson0d0b5882010-03-10 22:26:24 +00001495
1496 // Add its base class offset.
Anders Carlssond6ff5d72010-05-29 17:48:36 +00001497 assert(!VBases.count(Base->Class) && "vbase offset already exists!");
John McCalle42a3362012-05-01 08:55:32 +00001498 VBases.insert(std::make_pair(Base->Class,
1499 ASTRecordLayout::VBaseInfo(Offset, IsVtordispNeed)));
1500
1501 if (!isMicrosoftCXXABI())
1502 AddPrimaryVirtualBaseOffsets(Base, Offset);
Anders Carlsson09ffa322010-03-10 22:21:28 +00001503}
1504
Anders Carlssona2f8e412010-10-31 22:20:42 +00001505CharUnits RecordLayoutBuilder::LayoutBase(const BaseSubobjectInfo *Base) {
Anders Carlssond7f3fcf2010-05-29 20:47:33 +00001506 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base->Class);
Anders Carlsson09ffa322010-03-10 22:21:28 +00001507
Douglas Gregore9fc3772012-01-26 07:55:45 +00001508
1509 CharUnits Offset;
1510
1511 // Query the external layout to see if it provides an offset.
1512 bool HasExternalLayout = false;
1513 if (ExternalLayout) {
1514 llvm::DenseMap<const CXXRecordDecl *, CharUnits>::iterator Known;
1515 if (Base->IsVirtual) {
1516 Known = ExternalVirtualBaseOffsets.find(Base->Class);
1517 if (Known != ExternalVirtualBaseOffsets.end()) {
1518 Offset = Known->second;
1519 HasExternalLayout = true;
1520 }
1521 } else {
1522 Known = ExternalBaseOffsets.find(Base->Class);
1523 if (Known != ExternalBaseOffsets.end()) {
1524 Offset = Known->second;
1525 HasExternalLayout = true;
1526 }
1527 }
1528 }
1529
Anders Carlsson09ffa322010-03-10 22:21:28 +00001530 // If we have an empty base class, try to place it at offset 0.
Anders Carlssond7f3fcf2010-05-29 20:47:33 +00001531 if (Base->Class->isEmpty() &&
Douglas Gregore9fc3772012-01-26 07:55:45 +00001532 (!HasExternalLayout || Offset == CharUnits::Zero()) &&
Anders Carlsson28466ab2010-10-31 22:13:23 +00001533 EmptySubobjects->CanPlaceBaseAtOffset(Base, CharUnits::Zero())) {
Ken Dyck1b4420e2011-02-28 02:01:38 +00001534 setSize(std::max(getSize(), Layout.getSize()));
Anders Carlsson09ffa322010-03-10 22:21:28 +00001535
Anders Carlssona2f8e412010-10-31 22:20:42 +00001536 return CharUnits::Zero();
Anders Carlsson09ffa322010-03-10 22:21:28 +00001537 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001538
Ken Dyck85ef0432011-02-19 18:58:07 +00001539 CharUnits UnpackedBaseAlign = Layout.getNonVirtualAlign();
1540 CharUnits BaseAlign = (Packed) ? CharUnits::One() : UnpackedBaseAlign;
Argyrios Kyrtzidis8b542742010-12-09 00:35:20 +00001541
1542 // The maximum field alignment overrides base align.
Ken Dyck02ced6f2011-02-17 01:49:42 +00001543 if (!MaxFieldAlignment.isZero()) {
Ken Dyck85ef0432011-02-19 18:58:07 +00001544 BaseAlign = std::min(BaseAlign, MaxFieldAlignment);
1545 UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment);
Argyrios Kyrtzidis8b542742010-12-09 00:35:20 +00001546 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001547
Douglas Gregore9fc3772012-01-26 07:55:45 +00001548 if (!HasExternalLayout) {
1549 // Round up the current record size to the base's alignment boundary.
1550 Offset = getDataSize().RoundUpToAlignment(BaseAlign);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001551
Douglas Gregore9fc3772012-01-26 07:55:45 +00001552 // Try to place the base.
1553 while (!EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset))
1554 Offset += BaseAlign;
1555 } else {
1556 bool Allowed = EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset);
1557 (void)Allowed;
1558 assert(Allowed && "Base subobject externally placed at overlapping offset");
1559 }
1560
Anders Carlssond7f3fcf2010-05-29 20:47:33 +00001561 if (!Base->Class->isEmpty()) {
Anders Carlsson09ffa322010-03-10 22:21:28 +00001562 // Update the data size.
Ken Dyck1b4420e2011-02-28 02:01:38 +00001563 setDataSize(Offset + Layout.getNonVirtualSize());
Anders Carlsson09ffa322010-03-10 22:21:28 +00001564
Ken Dyck1b4420e2011-02-28 02:01:38 +00001565 setSize(std::max(getSize(), getDataSize()));
Anders Carlsson09ffa322010-03-10 22:21:28 +00001566 } else
Ken Dyck1b4420e2011-02-28 02:01:38 +00001567 setSize(std::max(getSize(), Offset + Layout.getSize()));
Anders Carlsson09ffa322010-03-10 22:21:28 +00001568
1569 // Remember max struct/class alignment.
Argyrios Kyrtzidis8b542742010-12-09 00:35:20 +00001570 UpdateAlignment(BaseAlign, UnpackedBaseAlign);
Anders Carlsson09ffa322010-03-10 22:21:28 +00001571
Ken Dyck1b4420e2011-02-28 02:01:38 +00001572 return Offset;
Anders Carlsson09ffa322010-03-10 22:21:28 +00001573}
1574
Daniel Dunbar6da10982010-05-27 05:45:51 +00001575void RecordLayoutBuilder::InitializeLayout(const Decl *D) {
1576 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
1577 IsUnion = RD->isUnion();
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001578
Anders Carlsson28a5fa22009-08-08 19:38:24 +00001579 Packed = D->hasAttr<PackedAttr>();
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001580
1581 IsMsStruct = D->hasAttr<MsStructAttr>();
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001582
Daniel Dunbar096ed292011-10-05 21:04:55 +00001583 // Honor the default struct packing maximum alignment flag.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001584 if (unsigned DefaultMaxFieldAlignment = Context.getLangOpts().PackStruct) {
Daniel Dunbar096ed292011-10-05 21:04:55 +00001585 MaxFieldAlignment = CharUnits::fromQuantity(DefaultMaxFieldAlignment);
1586 }
1587
Daniel Dunbar6da10982010-05-27 05:45:51 +00001588 // mac68k alignment supersedes maximum field alignment and attribute aligned,
1589 // and forces all structures to have 2-byte alignment. The IBM docs on it
1590 // allude to additional (more complicated) semantics, especially with regard
1591 // to bit-fields, but gcc appears not to follow that.
1592 if (D->hasAttr<AlignMac68kAttr>()) {
1593 IsMac68kAlign = true;
Ken Dyck02ced6f2011-02-17 01:49:42 +00001594 MaxFieldAlignment = CharUnits::fromQuantity(2);
Ken Dyck4731d5b2011-02-16 02:05:21 +00001595 Alignment = CharUnits::fromQuantity(2);
Daniel Dunbar6da10982010-05-27 05:45:51 +00001596 } else {
1597 if (const MaxFieldAlignmentAttr *MFAA = D->getAttr<MaxFieldAlignmentAttr>())
Ken Dyck02ced6f2011-02-17 01:49:42 +00001598 MaxFieldAlignment = Context.toCharUnitsFromBits(MFAA->getAlignment());
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001599
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001600 if (unsigned MaxAlign = D->getMaxAlignment())
Ken Dyck85ef0432011-02-19 18:58:07 +00001601 UpdateAlignment(Context.toCharUnitsFromBits(MaxAlign));
Daniel Dunbar6da10982010-05-27 05:45:51 +00001602 }
Douglas Gregore9fc3772012-01-26 07:55:45 +00001603
1604 // If there is an external AST source, ask it for the various offsets.
1605 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
1606 if (ExternalASTSource *External = Context.getExternalSource()) {
1607 ExternalLayout = External->layoutRecordType(RD,
1608 ExternalSize,
1609 ExternalAlign,
1610 ExternalFieldOffsets,
1611 ExternalBaseOffsets,
1612 ExternalVirtualBaseOffsets);
1613
1614 // Update based on external alignment.
1615 if (ExternalLayout) {
Douglas Gregor44ba7892012-01-28 00:53:29 +00001616 if (ExternalAlign > 0) {
1617 Alignment = Context.toCharUnitsFromBits(ExternalAlign);
1618 UnpackedAlignment = Alignment;
1619 } else {
1620 // The external source didn't have alignment information; infer it.
1621 InferAlignment = true;
1622 }
Douglas Gregore9fc3772012-01-26 07:55:45 +00001623 }
1624 }
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001625}
Anders Carlsson6d9f6f32009-07-19 00:18:47 +00001626
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001627void RecordLayoutBuilder::Layout(const RecordDecl *D) {
1628 InitializeLayout(D);
Anders Carlsson118ce162009-07-18 21:48:39 +00001629 LayoutFields(D);
Mike Stump11289f42009-09-09 15:08:12 +00001630
Anders Carlsson79474332009-07-18 20:20:21 +00001631 // Finally, round the size of the total struct up to the alignment of the
1632 // struct itself.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001633 FinishLayout(D);
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001634}
1635
1636void RecordLayoutBuilder::Layout(const CXXRecordDecl *RD) {
1637 InitializeLayout(RD);
1638
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001639 // Lay out the vtable and the non-virtual bases.
1640 LayoutNonVirtualBases(RD);
1641
1642 LayoutFields(RD);
1643
Ken Dycke7380752011-03-10 01:53:59 +00001644 NonVirtualSize = Context.toCharUnitsFromBits(
1645 llvm::RoundUpToAlignment(getSizeInBits(),
Douglas Gregore8bbc122011-09-02 00:18:52 +00001646 Context.getTargetInfo().getCharAlign()));
Ken Dyck4731d5b2011-02-16 02:05:21 +00001647 NonVirtualAlignment = Alignment;
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001648
John McCalle42a3362012-05-01 08:55:32 +00001649 if (isMicrosoftCXXABI()) {
1650 if (NonVirtualSize != NonVirtualSize.RoundUpToAlignment(Alignment)) {
John McCall0153cd32011-11-08 04:01:03 +00001651 CharUnits AlignMember =
1652 NonVirtualSize.RoundUpToAlignment(Alignment) - NonVirtualSize;
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001653
John McCall0153cd32011-11-08 04:01:03 +00001654 setSize(getSize() + AlignMember);
1655 setDataSize(getSize());
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001656
John McCall0153cd32011-11-08 04:01:03 +00001657 NonVirtualSize = Context.toCharUnitsFromBits(
1658 llvm::RoundUpToAlignment(getSizeInBits(),
1659 Context.getTargetInfo().getCharAlign()));
John McCalle42a3362012-05-01 08:55:32 +00001660 }
John McCall0153cd32011-11-08 04:01:03 +00001661
1662 MSLayoutVirtualBases(RD);
John McCall0153cd32011-11-08 04:01:03 +00001663 } else {
1664 // Lay out the virtual bases and add the primary virtual base offsets.
1665 LayoutVirtualBases(RD, RD);
1666 }
1667
1668 // Finally, round the size of the total struct up to the alignment
Eli Friedman83a12582011-12-01 00:37:01 +00001669 // of the struct itself.
1670 FinishLayout(RD);
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001671
Anders Carlsson5b441d72010-04-10 21:24:48 +00001672#ifndef NDEBUG
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001673 // Check that we have base offsets for all bases.
1674 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1675 E = RD->bases_end(); I != E; ++I) {
1676 if (I->isVirtual())
1677 continue;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001678
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001679 const CXXRecordDecl *BaseDecl =
1680 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1681
1682 assert(Bases.count(BaseDecl) && "Did not find base offset!");
1683 }
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001684
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001685 // And all virtual bases.
1686 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
1687 E = RD->vbases_end(); I != E; ++I) {
1688 const CXXRecordDecl *BaseDecl =
1689 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001690
Anders Carlssonc28a6c92010-05-26 15:10:00 +00001691 assert(VBases.count(BaseDecl) && "Did not find base offset!");
Anders Carlsson5b441d72010-04-10 21:24:48 +00001692 }
1693#endif
Anders Carlsson79474332009-07-18 20:20:21 +00001694}
1695
Anders Carlssonc2226202010-05-26 05:58:59 +00001696void RecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D) {
Anders Carlsson4f516282009-07-18 20:50:59 +00001697 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001698 const ASTRecordLayout &SL = Context.getASTObjCInterfaceLayout(SD);
Anders Carlsson4f516282009-07-18 20:50:59 +00001699
Ken Dyck85ef0432011-02-19 18:58:07 +00001700 UpdateAlignment(SL.getAlignment());
Mike Stump11289f42009-09-09 15:08:12 +00001701
Anders Carlsson4f516282009-07-18 20:50:59 +00001702 // We start laying out ivars not at the end of the superclass
1703 // structure, but at the next byte following the last field.
Ken Dyckecfc7552011-02-24 01:13:28 +00001704 setSize(SL.getDataSize());
Ken Dyck1b4420e2011-02-28 02:01:38 +00001705 setDataSize(getSize());
Anders Carlsson4f516282009-07-18 20:50:59 +00001706 }
Mike Stump11289f42009-09-09 15:08:12 +00001707
Daniel Dunbar6da10982010-05-27 05:45:51 +00001708 InitializeLayout(D);
Anders Carlsson4f516282009-07-18 20:50:59 +00001709 // Layout each ivar sequentially.
Jordy Rosea91768e2011-07-22 02:08:32 +00001710 for (const ObjCIvarDecl *IVD = D->all_declared_ivar_begin(); IVD;
1711 IVD = IVD->getNextIvar())
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00001712 LayoutField(IVD);
Mike Stump11289f42009-09-09 15:08:12 +00001713
Anders Carlsson4f516282009-07-18 20:50:59 +00001714 // Finally, round the size of the total struct up to the alignment of the
1715 // struct itself.
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001716 FinishLayout(D);
Anders Carlsson4f516282009-07-18 20:50:59 +00001717}
1718
Anders Carlssonc2226202010-05-26 05:58:59 +00001719void RecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
Anders Carlsson118ce162009-07-18 21:48:39 +00001720 // Layout each field, for now, just sequentially, respecting alignment. In
1721 // the future, this will need to be tweakable by targets.
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001722 const FieldDecl *LastFD = 0;
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001723 ZeroLengthBitfield = 0;
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001724 unsigned RemainingInAlignment = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001725 for (RecordDecl::field_iterator Field = D->field_begin(),
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001726 FieldEnd = D->field_end(); Field != FieldEnd; ++Field) {
1727 if (IsMsStruct) {
David Blaikie40ed2972012-06-06 20:45:41 +00001728 FieldDecl *FD = *Field;
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001729 if (Context.ZeroBitfieldFollowsBitfield(FD, LastFD))
1730 ZeroLengthBitfield = FD;
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001731 // Zero-length bitfields following non-bitfield members are
1732 // ignored:
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001733 else if (Context.ZeroBitfieldFollowsNonBitfield(FD, LastFD))
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001734 continue;
Fariborz Jahanian307eace2011-05-06 22:42:22 +00001735 // FIXME. streamline these conditions into a simple one.
Fariborz Jahanianb7a28792011-05-06 21:56:12 +00001736 else if (Context.BitfieldFollowsBitfield(FD, LastFD) ||
Chad Rosierf01a7dd2011-08-04 23:34:15 +00001737 Context.BitfieldFollowsNonBitfield(FD, LastFD) ||
1738 Context.NonBitfieldFollowsBitfield(FD, LastFD)) {
Fariborz Jahanian307eace2011-05-06 22:42:22 +00001739 // 1) Adjacent bit fields are packed into the same 1-, 2-, or
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001740 // 4-byte allocation unit if the integral types are the same
1741 // size and if the next bit field fits into the current
1742 // allocation unit without crossing the boundary imposed by the
1743 // common alignment requirements of the bit fields.
Fariborz Jahanian307eace2011-05-06 22:42:22 +00001744 // 2) Establish a new alignment for a bitfield following
Fariborz Jahanianb7a28792011-05-06 21:56:12 +00001745 // a non-bitfield if size of their types differ.
Fariborz Jahanian307eace2011-05-06 22:42:22 +00001746 // 3) Establish a new alignment for a non-bitfield following
1747 // a bitfield if size of their types differ.
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001748 std::pair<uint64_t, unsigned> FieldInfo =
1749 Context.getTypeInfo(FD->getType());
1750 uint64_t TypeSize = FieldInfo.first;
1751 unsigned FieldAlign = FieldInfo.second;
Fariborz Jahanian7adbed62011-05-09 22:03:17 +00001752 // This check is needed for 'long long' in -m32 mode.
Fariborz Jahanian0586df42011-12-12 21:16:36 +00001753 if (TypeSize > FieldAlign &&
1754 (Context.hasSameType(FD->getType(),
1755 Context.UnsignedLongLongTy)
1756 ||Context.hasSameType(FD->getType(),
1757 Context.LongLongTy)))
Fariborz Jahanian7adbed62011-05-09 22:03:17 +00001758 FieldAlign = TypeSize;
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001759 FieldInfo = Context.getTypeInfo(LastFD->getType());
1760 uint64_t TypeSizeLastFD = FieldInfo.first;
1761 unsigned FieldAlignLastFD = FieldInfo.second;
Fariborz Jahanian7adbed62011-05-09 22:03:17 +00001762 // This check is needed for 'long long' in -m32 mode.
Fariborz Jahanian0586df42011-12-12 21:16:36 +00001763 if (TypeSizeLastFD > FieldAlignLastFD &&
1764 (Context.hasSameType(LastFD->getType(),
1765 Context.UnsignedLongLongTy)
1766 || Context.hasSameType(LastFD->getType(),
1767 Context.LongLongTy)))
Fariborz Jahanian7adbed62011-05-09 22:03:17 +00001768 FieldAlignLastFD = TypeSizeLastFD;
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001769
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001770 if (TypeSizeLastFD != TypeSize) {
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001771 if (RemainingInAlignment &&
1772 LastFD && LastFD->isBitField() &&
Richard Smithcaf33902011-10-10 18:28:20 +00001773 LastFD->getBitWidthValue(Context)) {
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001774 // If previous field was a bitfield with some remaining unfilled
1775 // bits, pad the field so current field starts on its type boundary.
1776 uint64_t FieldOffset =
1777 getDataSizeInBits() - UnfilledBitsInLastByte;
1778 uint64_t NewSizeInBits = RemainingInAlignment + FieldOffset;
1779 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
Douglas Gregore8bbc122011-09-02 00:18:52 +00001780 Context.getTargetInfo().getCharAlign()));
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001781 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
1782 RemainingInAlignment = 0;
1783 }
1784
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001785 uint64_t UnpaddedFieldOffset =
1786 getDataSizeInBits() - UnfilledBitsInLastByte;
1787 FieldAlign = std::max(FieldAlign, FieldAlignLastFD);
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001788
Fariborz Jahanian07ceb0a2011-05-10 19:00:50 +00001789 // The maximum field alignment overrides the aligned attribute.
1790 if (!MaxFieldAlignment.isZero()) {
1791 unsigned MaxFieldAlignmentInBits =
1792 Context.toBits(MaxFieldAlignment);
1793 FieldAlign = std::min(FieldAlign, MaxFieldAlignmentInBits);
1794 }
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001795
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001796 uint64_t NewSizeInBits =
1797 llvm::RoundUpToAlignment(UnpaddedFieldOffset, FieldAlign);
1798 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
Douglas Gregore8bbc122011-09-02 00:18:52 +00001799 Context.getTargetInfo().getCharAlign()));
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001800 UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits;
1801 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
1802 }
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001803 if (FD->isBitField()) {
Richard Smithcaf33902011-10-10 18:28:20 +00001804 uint64_t FieldSize = FD->getBitWidthValue(Context);
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001805 assert (FieldSize > 0 && "LayoutFields - ms_struct layout");
1806 if (RemainingInAlignment < FieldSize)
1807 RemainingInAlignment = TypeSize - FieldSize;
1808 else
1809 RemainingInAlignment -= FieldSize;
1810 }
1811 }
1812 else if (FD->isBitField()) {
Richard Smithcaf33902011-10-10 18:28:20 +00001813 uint64_t FieldSize = FD->getBitWidthValue(Context);
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001814 std::pair<uint64_t, unsigned> FieldInfo =
1815 Context.getTypeInfo(FD->getType());
1816 uint64_t TypeSize = FieldInfo.first;
1817 RemainingInAlignment = TypeSize - FieldSize;
Fariborz Jahanian84335f72011-05-04 18:51:37 +00001818 }
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001819 LastFD = FD;
1820 }
Douglas Gregore8bbc122011-09-02 00:18:52 +00001821 else if (!Context.getTargetInfo().useBitFieldTypeAlignment() &&
1822 Context.getTargetInfo().useZeroLengthBitfieldAlignment()) {
David Blaikie40ed2972012-06-06 20:45:41 +00001823 if (Field->isBitField() && Field->getBitWidthValue(Context) == 0)
1824 ZeroLengthBitfield = *Field;
Chad Rosier18903ee2011-08-04 01:21:14 +00001825 }
David Blaikie40ed2972012-06-06 20:45:41 +00001826 LayoutField(*Field);
Fariborz Jahanianbcb23a12011-04-26 23:52:16 +00001827 }
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001828 if (IsMsStruct && RemainingInAlignment &&
Richard Smithcaf33902011-10-10 18:28:20 +00001829 LastFD && LastFD->isBitField() && LastFD->getBitWidthValue(Context)) {
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001830 // If we ended a bitfield before the full length of the type then
1831 // pad the struct out to the full length of the last type.
1832 uint64_t FieldOffset =
1833 getDataSizeInBits() - UnfilledBitsInLastByte;
1834 uint64_t NewSizeInBits = RemainingInAlignment + FieldOffset;
1835 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
Douglas Gregore8bbc122011-09-02 00:18:52 +00001836 Context.getTargetInfo().getCharAlign()));
Fariborz Jahanian783243b2011-05-11 16:58:31 +00001837 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
1838 }
Anders Carlsson118ce162009-07-18 21:48:39 +00001839}
1840
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001841void RecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize,
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001842 uint64_t TypeSize,
1843 bool FieldPacked,
1844 const FieldDecl *D) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001845 assert(Context.getLangOpts().CPlusPlus &&
Anders Carlsson57235162010-04-16 15:57:11 +00001846 "Can only have wide bit-fields in C++!");
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001847
Anders Carlsson57235162010-04-16 15:57:11 +00001848 // Itanium C++ ABI 2.4:
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001849 // If sizeof(T)*8 < n, let T' be the largest integral POD type with
Anders Carlsson57235162010-04-16 15:57:11 +00001850 // sizeof(T')*8 <= n.
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001851
Anders Carlsson57235162010-04-16 15:57:11 +00001852 QualType IntegralPODTypes[] = {
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001853 Context.UnsignedCharTy, Context.UnsignedShortTy, Context.UnsignedIntTy,
Anders Carlsson57235162010-04-16 15:57:11 +00001854 Context.UnsignedLongTy, Context.UnsignedLongLongTy
1855 };
1856
Anders Carlsson57235162010-04-16 15:57:11 +00001857 QualType Type;
1858 for (unsigned I = 0, E = llvm::array_lengthof(IntegralPODTypes);
1859 I != E; ++I) {
1860 uint64_t Size = Context.getTypeSize(IntegralPODTypes[I]);
Anders Carlsson57235162010-04-16 15:57:11 +00001861
1862 if (Size > FieldSize)
1863 break;
1864
1865 Type = IntegralPODTypes[I];
1866 }
1867 assert(!Type.isNull() && "Did not find a type!");
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001868
Ken Dyckdbe37f32011-03-01 01:36:00 +00001869 CharUnits TypeAlign = Context.getTypeAlignInChars(Type);
Anders Carlsson57235162010-04-16 15:57:11 +00001870
1871 // We're not going to use any of the unfilled bits in the last byte.
1872 UnfilledBitsInLastByte = 0;
1873
Anders Carlssonaad5fa82010-04-17 20:21:41 +00001874 uint64_t FieldOffset;
Ken Dyckecfc7552011-02-24 01:13:28 +00001875 uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001876
Anders Carlsson57235162010-04-16 15:57:11 +00001877 if (IsUnion) {
Ken Dyckecfc7552011-02-24 01:13:28 +00001878 setDataSize(std::max(getDataSizeInBits(), FieldSize));
Anders Carlssonaad5fa82010-04-17 20:21:41 +00001879 FieldOffset = 0;
Anders Carlsson57235162010-04-16 15:57:11 +00001880 } else {
Chad Rosiere1a6a0e2011-08-05 22:38:04 +00001881 // The bitfield is allocated starting at the next offset aligned
1882 // appropriately for T', with length n bits.
Ken Dyckdbe37f32011-03-01 01:36:00 +00001883 FieldOffset = llvm::RoundUpToAlignment(getDataSizeInBits(),
1884 Context.toBits(TypeAlign));
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001885
Anders Carlsson57235162010-04-16 15:57:11 +00001886 uint64_t NewSizeInBits = FieldOffset + FieldSize;
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001887
Ken Dycka1a2e8d2011-03-10 02:00:35 +00001888 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
Douglas Gregore8bbc122011-09-02 00:18:52 +00001889 Context.getTargetInfo().getCharAlign()));
Ken Dyckecfc7552011-02-24 01:13:28 +00001890 UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits;
Anders Carlsson57235162010-04-16 15:57:11 +00001891 }
1892
1893 // Place this field at the current location.
1894 FieldOffsets.push_back(FieldOffset);
1895
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001896 CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, FieldOffset,
Ken Dyckdbe37f32011-03-01 01:36:00 +00001897 Context.toBits(TypeAlign), FieldPacked, D);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001898
Anders Carlsson57235162010-04-16 15:57:11 +00001899 // Update the size.
Ken Dyckecfc7552011-02-24 01:13:28 +00001900 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
Daniel Dunbar592a85c2010-05-27 02:25:46 +00001901
Anders Carlsson57235162010-04-16 15:57:11 +00001902 // Remember max struct/class alignment.
Ken Dyckdbe37f32011-03-01 01:36:00 +00001903 UpdateAlignment(TypeAlign);
Anders Carlsson57235162010-04-16 15:57:11 +00001904}
1905
Anders Carlssonc2226202010-05-26 05:58:59 +00001906void RecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
Anders Carlsson07209442009-11-22 17:37:31 +00001907 bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
Ken Dyckecfc7552011-02-24 01:13:28 +00001908 uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte;
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001909 uint64_t FieldOffset = IsUnion ? 0 : UnpaddedFieldOffset;
Richard Smithcaf33902011-10-10 18:28:20 +00001910 uint64_t FieldSize = D->getBitWidthValue(Context);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001911
Anders Carlsson5efc56e2010-04-16 15:07:51 +00001912 std::pair<uint64_t, unsigned> FieldInfo = Context.getTypeInfo(D->getType());
Anders Carlsson07209442009-11-22 17:37:31 +00001913 uint64_t TypeSize = FieldInfo.first;
1914 unsigned FieldAlign = FieldInfo.second;
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001915
Fariborz Jahanian7adbed62011-05-09 22:03:17 +00001916 // This check is needed for 'long long' in -m32 mode.
Fariborz Jahanian0586df42011-12-12 21:16:36 +00001917 if (IsMsStruct && (TypeSize > FieldAlign) &&
1918 (Context.hasSameType(D->getType(),
1919 Context.UnsignedLongLongTy)
1920 || Context.hasSameType(D->getType(), Context.LongLongTy)))
Fariborz Jahanian7adbed62011-05-09 22:03:17 +00001921 FieldAlign = TypeSize;
Chad Rosier18903ee2011-08-04 01:21:14 +00001922
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001923 if (ZeroLengthBitfield) {
Chad Rosiere1a6a0e2011-08-05 22:38:04 +00001924 std::pair<uint64_t, unsigned> FieldInfo;
1925 unsigned ZeroLengthBitfieldAlignment;
1926 if (IsMsStruct) {
1927 // If a zero-length bitfield is inserted after a bitfield,
1928 // and the alignment of the zero-length bitfield is
1929 // greater than the member that follows it, `bar', `bar'
1930 // will be aligned as the type of the zero-length bitfield.
1931 if (ZeroLengthBitfield != D) {
1932 FieldInfo = Context.getTypeInfo(ZeroLengthBitfield->getType());
1933 ZeroLengthBitfieldAlignment = FieldInfo.second;
1934 // Ignore alignment of subsequent zero-length bitfields.
1935 if ((ZeroLengthBitfieldAlignment > FieldAlign) || (FieldSize == 0))
1936 FieldAlign = ZeroLengthBitfieldAlignment;
1937 if (FieldSize)
1938 ZeroLengthBitfield = 0;
1939 }
1940 } else {
1941 // The alignment of a zero-length bitfield affects the alignment
1942 // of the next member. The alignment is the max of the zero
1943 // length bitfield's alignment and a target specific fixed value.
1944 unsigned ZeroLengthBitfieldBoundary =
Douglas Gregore8bbc122011-09-02 00:18:52 +00001945 Context.getTargetInfo().getZeroLengthBitfieldBoundary();
Chad Rosiere1a6a0e2011-08-05 22:38:04 +00001946 if (ZeroLengthBitfieldBoundary > FieldAlign)
1947 FieldAlign = ZeroLengthBitfieldBoundary;
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00001948 }
1949 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001950
Anders Carlsson57235162010-04-16 15:57:11 +00001951 if (FieldSize > TypeSize) {
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001952 LayoutWideBitField(FieldSize, TypeSize, FieldPacked, D);
Anders Carlsson57235162010-04-16 15:57:11 +00001953 return;
1954 }
1955
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001956 // The align if the field is not packed. This is to check if the attribute
1957 // was unnecessary (-Wpacked).
1958 unsigned UnpackedFieldAlign = FieldAlign;
1959 uint64_t UnpackedFieldOffset = FieldOffset;
Douglas Gregore8bbc122011-09-02 00:18:52 +00001960 if (!Context.getTargetInfo().useBitFieldTypeAlignment() && !ZeroLengthBitfield)
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001961 UnpackedFieldAlign = 1;
1962
Chad Rosiere1a6a0e2011-08-05 22:38:04 +00001963 if (FieldPacked ||
Douglas Gregore8bbc122011-09-02 00:18:52 +00001964 (!Context.getTargetInfo().useBitFieldTypeAlignment() && !ZeroLengthBitfield))
Anders Carlsson07209442009-11-22 17:37:31 +00001965 FieldAlign = 1;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001966 FieldAlign = std::max(FieldAlign, D->getMaxAlignment());
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001967 UnpackedFieldAlign = std::max(UnpackedFieldAlign, D->getMaxAlignment());
Anders Carlsson07209442009-11-22 17:37:31 +00001968
1969 // The maximum field alignment overrides the aligned attribute.
Eli Friedmana91d38a2011-12-02 02:38:48 +00001970 if (!MaxFieldAlignment.isZero() && FieldSize != 0) {
Ken Dyck02ced6f2011-02-17 01:49:42 +00001971 unsigned MaxFieldAlignmentInBits = Context.toBits(MaxFieldAlignment);
1972 FieldAlign = std::min(FieldAlign, MaxFieldAlignmentInBits);
1973 UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignmentInBits);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001974 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001975
Douglas Gregor44ba7892012-01-28 00:53:29 +00001976 // Check if we need to add padding to give the field the correct alignment.
1977 if (FieldSize == 0 ||
1978 (MaxFieldAlignment.isZero() &&
1979 (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize))
1980 FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001981
Douglas Gregor44ba7892012-01-28 00:53:29 +00001982 if (FieldSize == 0 ||
1983 (MaxFieldAlignment.isZero() &&
1984 (UnpackedFieldOffset & (UnpackedFieldAlign-1)) + FieldSize > TypeSize))
1985 UnpackedFieldOffset = llvm::RoundUpToAlignment(UnpackedFieldOffset,
1986 UnpackedFieldAlign);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001987
Chad Rosiere1a6a0e2011-08-05 22:38:04 +00001988 // Padding members don't affect overall alignment, unless zero length bitfield
1989 // alignment is enabled.
Douglas Gregore8bbc122011-09-02 00:18:52 +00001990 if (!D->getIdentifier() && !Context.getTargetInfo().useZeroLengthBitfieldAlignment())
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00001991 FieldAlign = UnpackedFieldAlign = 1;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00001992
Chad Rosiere1a6a0e2011-08-05 22:38:04 +00001993 if (!IsMsStruct)
1994 ZeroLengthBitfield = 0;
1995
Douglas Gregor44ba7892012-01-28 00:53:29 +00001996 if (ExternalLayout)
1997 FieldOffset = updateExternalFieldOffset(D, FieldOffset);
1998
Anders Carlsson07209442009-11-22 17:37:31 +00001999 // Place this field at the current location.
2000 FieldOffsets.push_back(FieldOffset);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002001
Douglas Gregore9fc3772012-01-26 07:55:45 +00002002 if (!ExternalLayout)
2003 CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, UnpackedFieldOffset,
2004 UnpackedFieldAlign, FieldPacked, D);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002005
Anders Carlssonba958402009-11-22 19:13:51 +00002006 // Update DataSize to include the last byte containing (part of) the bitfield.
2007 if (IsUnion) {
2008 // FIXME: I think FieldSize should be TypeSize here.
Ken Dyckecfc7552011-02-24 01:13:28 +00002009 setDataSize(std::max(getDataSizeInBits(), FieldSize));
Anders Carlssonba958402009-11-22 19:13:51 +00002010 } else {
2011 uint64_t NewSizeInBits = FieldOffset + FieldSize;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002012
Ken Dycka1a2e8d2011-03-10 02:00:35 +00002013 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
Douglas Gregore8bbc122011-09-02 00:18:52 +00002014 Context.getTargetInfo().getCharAlign()));
Ken Dyckecfc7552011-02-24 01:13:28 +00002015 UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits;
Anders Carlssonba958402009-11-22 19:13:51 +00002016 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002017
Anders Carlssonba958402009-11-22 19:13:51 +00002018 // Update the size.
Ken Dyckecfc7552011-02-24 01:13:28 +00002019 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002020
Anders Carlsson07209442009-11-22 17:37:31 +00002021 // Remember max struct/class alignment.
Ken Dyck85ef0432011-02-19 18:58:07 +00002022 UpdateAlignment(Context.toCharUnitsFromBits(FieldAlign),
2023 Context.toCharUnitsFromBits(UnpackedFieldAlign));
Anders Carlsson07209442009-11-22 17:37:31 +00002024}
2025
Douglas Gregore9fc3772012-01-26 07:55:45 +00002026void RecordLayoutBuilder::LayoutField(const FieldDecl *D) {
Anders Carlsson07209442009-11-22 17:37:31 +00002027 if (D->isBitField()) {
2028 LayoutBitField(D);
2029 return;
2030 }
2031
Ken Dyckecfc7552011-02-24 01:13:28 +00002032 uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte;
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002033
Anders Carlssonba958402009-11-22 19:13:51 +00002034 // Reset the unfilled bits.
2035 UnfilledBitsInLastByte = 0;
2036
Anders Carlsson07209442009-11-22 17:37:31 +00002037 bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
Ken Dyck6d90e892011-02-20 02:06:09 +00002038 CharUnits FieldOffset =
Ken Dyckecfc7552011-02-24 01:13:28 +00002039 IsUnion ? CharUnits::Zero() : getDataSize();
Ken Dyck6d90e892011-02-20 02:06:09 +00002040 CharUnits FieldSize;
2041 CharUnits FieldAlign;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002042
Anders Carlsson07209442009-11-22 17:37:31 +00002043 if (D->getType()->isIncompleteArrayType()) {
2044 // This is a flexible array member; we can't directly
2045 // query getTypeInfo about these, so we figure it out here.
2046 // Flexible array members don't have any size, but they
2047 // have to be aligned appropriately for their element type.
Ken Dyck6d90e892011-02-20 02:06:09 +00002048 FieldSize = CharUnits::Zero();
Anders Carlsson5efc56e2010-04-16 15:07:51 +00002049 const ArrayType* ATy = Context.getAsArrayType(D->getType());
Ken Dyck6d90e892011-02-20 02:06:09 +00002050 FieldAlign = Context.getTypeAlignInChars(ATy->getElementType());
Anders Carlsson07209442009-11-22 17:37:31 +00002051 } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) {
2052 unsigned AS = RT->getPointeeType().getAddressSpace();
Ken Dyck6d90e892011-02-20 02:06:09 +00002053 FieldSize =
Douglas Gregore8bbc122011-09-02 00:18:52 +00002054 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(AS));
Ken Dyck6d90e892011-02-20 02:06:09 +00002055 FieldAlign =
Douglas Gregore8bbc122011-09-02 00:18:52 +00002056 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(AS));
Anders Carlsson79474332009-07-18 20:20:21 +00002057 } else {
Ken Dyck6d90e892011-02-20 02:06:09 +00002058 std::pair<CharUnits, CharUnits> FieldInfo =
2059 Context.getTypeInfoInChars(D->getType());
Anders Carlsson07209442009-11-22 17:37:31 +00002060 FieldSize = FieldInfo.first;
2061 FieldAlign = FieldInfo.second;
Chad Rosier18903ee2011-08-04 01:21:14 +00002062
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00002063 if (ZeroLengthBitfield) {
Chad Rosier18903ee2011-08-04 01:21:14 +00002064 CharUnits ZeroLengthBitfieldBoundary =
2065 Context.toCharUnitsFromBits(
Douglas Gregore8bbc122011-09-02 00:18:52 +00002066 Context.getTargetInfo().getZeroLengthBitfieldBoundary());
Chad Rosier18903ee2011-08-04 01:21:14 +00002067 if (ZeroLengthBitfieldBoundary == CharUnits::Zero()) {
2068 // If a zero-length bitfield is inserted after a bitfield,
2069 // and the alignment of the zero-length bitfield is
2070 // greater than the member that follows it, `bar', `bar'
2071 // will be aligned as the type of the zero-length bitfield.
2072 std::pair<CharUnits, CharUnits> FieldInfo =
2073 Context.getTypeInfoInChars(ZeroLengthBitfield->getType());
2074 CharUnits ZeroLengthBitfieldAlignment = FieldInfo.second;
2075 if (ZeroLengthBitfieldAlignment > FieldAlign)
2076 FieldAlign = ZeroLengthBitfieldAlignment;
Chad Rosier64b18ee2011-08-04 19:25:14 +00002077 } else if (ZeroLengthBitfieldBoundary > FieldAlign) {
Chad Rosier18903ee2011-08-04 01:21:14 +00002078 // Align 'bar' based on a fixed alignment specified by the target.
Douglas Gregore8bbc122011-09-02 00:18:52 +00002079 assert(Context.getTargetInfo().useZeroLengthBitfieldAlignment() &&
Chad Rosiera336c6f2011-08-04 17:52:43 +00002080 "ZeroLengthBitfieldBoundary should only be used in conjunction"
2081 " with useZeroLengthBitfieldAlignment.");
Chad Rosier18903ee2011-08-04 01:21:14 +00002082 FieldAlign = ZeroLengthBitfieldBoundary;
2083 }
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00002084 ZeroLengthBitfield = 0;
2085 }
Douglas Gregordbe39272011-02-01 15:15:22 +00002086
David Blaikiebbafb8a2012-03-11 07:00:24 +00002087 if (Context.getLangOpts().MSBitfields || IsMsStruct) {
Douglas Gregordbe39272011-02-01 15:15:22 +00002088 // If MS bitfield layout is required, figure out what type is being
2089 // laid out and align the field to the width of that type.
2090
2091 // Resolve all typedefs down to their base type and round up the field
2092 // alignment if necessary.
2093 QualType T = Context.getBaseElementType(D->getType());
2094 if (const BuiltinType *BTy = T->getAs<BuiltinType>()) {
Ken Dyck6d90e892011-02-20 02:06:09 +00002095 CharUnits TypeSize = Context.getTypeSizeInChars(BTy);
Douglas Gregordbe39272011-02-01 15:15:22 +00002096 if (TypeSize > FieldAlign)
2097 FieldAlign = TypeSize;
2098 }
2099 }
Anders Carlsson79474332009-07-18 20:20:21 +00002100 }
Mike Stump11289f42009-09-09 15:08:12 +00002101
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002102 // The align if the field is not packed. This is to check if the attribute
2103 // was unnecessary (-Wpacked).
Ken Dyck6d90e892011-02-20 02:06:09 +00002104 CharUnits UnpackedFieldAlign = FieldAlign;
2105 CharUnits UnpackedFieldOffset = FieldOffset;
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002106
Anders Carlsson07209442009-11-22 17:37:31 +00002107 if (FieldPacked)
Ken Dyck6d90e892011-02-20 02:06:09 +00002108 FieldAlign = CharUnits::One();
2109 CharUnits MaxAlignmentInChars =
2110 Context.toCharUnitsFromBits(D->getMaxAlignment());
2111 FieldAlign = std::max(FieldAlign, MaxAlignmentInChars);
2112 UnpackedFieldAlign = std::max(UnpackedFieldAlign, MaxAlignmentInChars);
Anders Carlsson07209442009-11-22 17:37:31 +00002113
2114 // The maximum field alignment overrides the aligned attribute.
Ken Dyck02ced6f2011-02-17 01:49:42 +00002115 if (!MaxFieldAlignment.isZero()) {
Ken Dyck6d90e892011-02-20 02:06:09 +00002116 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
2117 UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignment);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002118 }
Anders Carlsson07209442009-11-22 17:37:31 +00002119
Douglas Gregor44ba7892012-01-28 00:53:29 +00002120 // Round up the current record size to the field's alignment boundary.
2121 FieldOffset = FieldOffset.RoundUpToAlignment(FieldAlign);
2122 UnpackedFieldOffset =
2123 UnpackedFieldOffset.RoundUpToAlignment(UnpackedFieldAlign);
2124
2125 if (ExternalLayout) {
2126 FieldOffset = Context.toCharUnitsFromBits(
2127 updateExternalFieldOffset(D, Context.toBits(FieldOffset)));
2128
2129 if (!IsUnion && EmptySubobjects) {
2130 // Record the fact that we're placing a field at this offset.
2131 bool Allowed = EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset);
2132 (void)Allowed;
2133 assert(Allowed && "Externally-placed field cannot be placed here");
2134 }
2135 } else {
Douglas Gregore9fc3772012-01-26 07:55:45 +00002136 if (!IsUnion && EmptySubobjects) {
2137 // Check if we can place the field at this offset.
2138 while (!EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset)) {
2139 // We couldn't place the field at the offset. Try again at a new offset.
2140 FieldOffset += FieldAlign;
2141 }
Anders Carlsson07209442009-11-22 17:37:31 +00002142 }
Anders Carlsson07209442009-11-22 17:37:31 +00002143 }
Douglas Gregore9fc3772012-01-26 07:55:45 +00002144
Anders Carlsson79474332009-07-18 20:20:21 +00002145 // Place this field at the current location.
Ken Dyck6d90e892011-02-20 02:06:09 +00002146 FieldOffsets.push_back(Context.toBits(FieldOffset));
Mike Stump11289f42009-09-09 15:08:12 +00002147
Douglas Gregore9fc3772012-01-26 07:55:45 +00002148 if (!ExternalLayout)
2149 CheckFieldPadding(Context.toBits(FieldOffset), UnpaddedFieldOffset,
2150 Context.toBits(UnpackedFieldOffset),
2151 Context.toBits(UnpackedFieldAlign), FieldPacked, D);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002152
Anders Carlsson79474332009-07-18 20:20:21 +00002153 // Reserve space for this field.
Eli Friedman43f18342012-01-12 23:27:03 +00002154 uint64_t FieldSizeInBits = Context.toBits(FieldSize);
Anders Carlsson79474332009-07-18 20:20:21 +00002155 if (IsUnion)
Eli Friedman2e108372012-01-12 23:48:56 +00002156 setDataSize(std::max(getDataSizeInBits(), FieldSizeInBits));
Anders Carlsson79474332009-07-18 20:20:21 +00002157 else
Eli Friedman2e108372012-01-12 23:48:56 +00002158 setDataSize(FieldOffset + FieldSize);
Mike Stump11289f42009-09-09 15:08:12 +00002159
Eli Friedman2e108372012-01-12 23:48:56 +00002160 // Update the size.
2161 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
Mike Stump11289f42009-09-09 15:08:12 +00002162
Anders Carlsson79474332009-07-18 20:20:21 +00002163 // Remember max struct/class alignment.
Ken Dyck6d90e892011-02-20 02:06:09 +00002164 UpdateAlignment(FieldAlign, UnpackedFieldAlign);
Anders Carlsson79474332009-07-18 20:20:21 +00002165}
2166
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002167void RecordLayoutBuilder::FinishLayout(const NamedDecl *D) {
Douglas Gregor44ba7892012-01-28 00:53:29 +00002168 if (ExternalLayout) {
2169 setSize(ExternalSize);
2170 return;
2171 }
2172
Anders Carlsson79474332009-07-18 20:20:21 +00002173 // In C++, records cannot be of size 0.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002174 if (Context.getLangOpts().CPlusPlus && getSizeInBits() == 0) {
Fariborz Jahanian09b23312011-02-02 19:36:18 +00002175 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
2176 // Compatibility with gcc requires a class (pod or non-pod)
2177 // which is not empty but of size 0; such as having fields of
2178 // array of zero-length, remains of Size 0
2179 if (RD->isEmpty())
Ken Dyck1b4420e2011-02-28 02:01:38 +00002180 setSize(CharUnits::One());
Fariborz Jahanian09b23312011-02-02 19:36:18 +00002181 }
2182 else
Ken Dyck1b4420e2011-02-28 02:01:38 +00002183 setSize(CharUnits::One());
Fariborz Jahanian09b23312011-02-02 19:36:18 +00002184 }
Eli Friedman83a12582011-12-01 00:37:01 +00002185
2186 // MSVC doesn't round up to the alignment of the record with virtual bases.
2187 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
2188 if (isMicrosoftCXXABI() && RD->getNumVBases())
2189 return;
2190 }
2191
Anders Carlsson79474332009-07-18 20:20:21 +00002192 // Finally, round the size of the record up to the alignment of the
2193 // record itself.
Ken Dyckecfc7552011-02-24 01:13:28 +00002194 uint64_t UnpaddedSize = getSizeInBits() - UnfilledBitsInLastByte;
Ken Dyck1b4420e2011-02-28 02:01:38 +00002195 uint64_t UnpackedSizeInBits =
Ken Dyckecfc7552011-02-24 01:13:28 +00002196 llvm::RoundUpToAlignment(getSizeInBits(),
2197 Context.toBits(UnpackedAlignment));
Ken Dyck1b4420e2011-02-28 02:01:38 +00002198 CharUnits UnpackedSize = Context.toCharUnitsFromBits(UnpackedSizeInBits);
Ken Dyckecfc7552011-02-24 01:13:28 +00002199 setSize(llvm::RoundUpToAlignment(getSizeInBits(), Context.toBits(Alignment)));
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002200
Douglas Gregore8bbc122011-09-02 00:18:52 +00002201 unsigned CharBitNum = Context.getTargetInfo().getCharWidth();
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002202 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) {
2203 // Warn if padding was introduced to the struct/class/union.
Ken Dyckecfc7552011-02-24 01:13:28 +00002204 if (getSizeInBits() > UnpaddedSize) {
2205 unsigned PadSize = getSizeInBits() - UnpaddedSize;
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002206 bool InBits = true;
2207 if (PadSize % CharBitNum == 0) {
2208 PadSize = PadSize / CharBitNum;
2209 InBits = false;
2210 }
2211 Diag(RD->getLocation(), diag::warn_padded_struct_size)
2212 << Context.getTypeDeclType(RD)
2213 << PadSize
2214 << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not
2215 }
2216
2217 // Warn if we packed it unnecessarily. If the alignment is 1 byte don't
2218 // bother since there won't be alignment issues.
Ken Dyckecfc7552011-02-24 01:13:28 +00002219 if (Packed && UnpackedAlignment > CharUnits::One() &&
Ken Dyck1b4420e2011-02-28 02:01:38 +00002220 getSize() == UnpackedSize)
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002221 Diag(D->getLocation(), diag::warn_unnecessary_packed)
2222 << Context.getTypeDeclType(RD);
2223 }
Anders Carlsson79474332009-07-18 20:20:21 +00002224}
2225
Ken Dyck85ef0432011-02-19 18:58:07 +00002226void RecordLayoutBuilder::UpdateAlignment(CharUnits NewAlignment,
2227 CharUnits UnpackedNewAlignment) {
Douglas Gregore9fc3772012-01-26 07:55:45 +00002228 // The alignment is not modified when using 'mac68k' alignment or when
Douglas Gregor44ba7892012-01-28 00:53:29 +00002229 // we have an externally-supplied layout that also provides overall alignment.
2230 if (IsMac68kAlign || (ExternalLayout && !InferAlignment))
Daniel Dunbar6da10982010-05-27 05:45:51 +00002231 return;
2232
Ken Dyck85ef0432011-02-19 18:58:07 +00002233 if (NewAlignment > Alignment) {
2234 assert(llvm::isPowerOf2_32(NewAlignment.getQuantity() &&
2235 "Alignment not a power of 2"));
2236 Alignment = NewAlignment;
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002237 }
2238
Ken Dyck85ef0432011-02-19 18:58:07 +00002239 if (UnpackedNewAlignment > UnpackedAlignment) {
2240 assert(llvm::isPowerOf2_32(UnpackedNewAlignment.getQuantity() &&
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002241 "Alignment not a power of 2"));
Ken Dyck85ef0432011-02-19 18:58:07 +00002242 UnpackedAlignment = UnpackedNewAlignment;
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002243 }
2244}
2245
Douglas Gregor44ba7892012-01-28 00:53:29 +00002246uint64_t
2247RecordLayoutBuilder::updateExternalFieldOffset(const FieldDecl *Field,
2248 uint64_t ComputedOffset) {
2249 assert(ExternalFieldOffsets.find(Field) != ExternalFieldOffsets.end() &&
2250 "Field does not have an external offset");
2251
2252 uint64_t ExternalFieldOffset = ExternalFieldOffsets[Field];
2253
2254 if (InferAlignment && ExternalFieldOffset < ComputedOffset) {
2255 // The externally-supplied field offset is before the field offset we
2256 // computed. Assume that the structure is packed.
2257 Alignment = CharUnits::fromQuantity(1);
2258 InferAlignment = false;
2259 }
2260
2261 // Use the externally-supplied field offset.
2262 return ExternalFieldOffset;
2263}
2264
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002265void RecordLayoutBuilder::CheckFieldPadding(uint64_t Offset,
2266 uint64_t UnpaddedOffset,
2267 uint64_t UnpackedOffset,
2268 unsigned UnpackedAlign,
2269 bool isPacked,
2270 const FieldDecl *D) {
2271 // We let objc ivars without warning, objc interfaces generally are not used
2272 // for padding tricks.
2273 if (isa<ObjCIvarDecl>(D))
Anders Carlsson79474332009-07-18 20:20:21 +00002274 return;
Mike Stump11289f42009-09-09 15:08:12 +00002275
Ted Kremenekfed48af2011-09-06 19:40:45 +00002276 // Don't warn about structs created without a SourceLocation. This can
2277 // be done by clients of the AST, such as codegen.
2278 if (D->getLocation().isInvalid())
2279 return;
2280
Douglas Gregore8bbc122011-09-02 00:18:52 +00002281 unsigned CharBitNum = Context.getTargetInfo().getCharWidth();
Mike Stump11289f42009-09-09 15:08:12 +00002282
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002283 // Warn if padding was introduced to the struct/class.
2284 if (!IsUnion && Offset > UnpaddedOffset) {
2285 unsigned PadSize = Offset - UnpaddedOffset;
2286 bool InBits = true;
2287 if (PadSize % CharBitNum == 0) {
2288 PadSize = PadSize / CharBitNum;
2289 InBits = false;
2290 }
2291 if (D->getIdentifier())
2292 Diag(D->getLocation(), diag::warn_padded_struct_field)
2293 << (D->getParent()->isStruct() ? 0 : 1) // struct|class
2294 << Context.getTypeDeclType(D->getParent())
2295 << PadSize
2296 << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1) // plural or not
2297 << D->getIdentifier();
2298 else
2299 Diag(D->getLocation(), diag::warn_padded_struct_anon_field)
2300 << (D->getParent()->isStruct() ? 0 : 1) // struct|class
2301 << Context.getTypeDeclType(D->getParent())
2302 << PadSize
2303 << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not
2304 }
2305
2306 // Warn if we packed it unnecessarily. If the alignment is 1 byte don't
2307 // bother since there won't be alignment issues.
2308 if (isPacked && UnpackedAlign > CharBitNum && Offset == UnpackedOffset)
2309 Diag(D->getLocation(), diag::warn_unnecessary_packed)
2310 << D->getIdentifier();
Anders Carlsson79474332009-07-18 20:20:21 +00002311}
Mike Stump11289f42009-09-09 15:08:12 +00002312
Anders Carlsson5ebf8b42009-12-07 04:35:11 +00002313const CXXMethodDecl *
Anders Carlssonc2226202010-05-26 05:58:59 +00002314RecordLayoutBuilder::ComputeKeyFunction(const CXXRecordDecl *RD) {
Daniel Dunbarccabe482010-04-19 20:44:53 +00002315 // If a class isn't polymorphic it doesn't have a key function.
Anders Carlsson5ebf8b42009-12-07 04:35:11 +00002316 if (!RD->isPolymorphic())
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00002317 return 0;
Eli Friedmanf2c79b62009-12-08 03:56:49 +00002318
Eli Friedman300f55d2011-06-10 21:53:06 +00002319 // A class that is not externally visible doesn't have a key function. (Or
Eli Friedmanf2c79b62009-12-08 03:56:49 +00002320 // at least, there's no point to assigning a key function to such a class;
2321 // this doesn't affect the ABI.)
Eli Friedman300f55d2011-06-10 21:53:06 +00002322 if (RD->getLinkage() != ExternalLinkage)
Eli Friedmanf2c79b62009-12-08 03:56:49 +00002323 return 0;
2324
Argyrios Kyrtzidis8c64bbe2010-10-13 02:39:41 +00002325 // Template instantiations don't have key functions,see Itanium C++ ABI 5.2.6.
2326 // Same behavior as GCC.
2327 TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
2328 if (TSK == TSK_ImplicitInstantiation ||
2329 TSK == TSK_ExplicitInstantiationDefinition)
2330 return 0;
2331
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002332 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
2333 E = RD->method_end(); I != E; ++I) {
David Blaikie40ed2972012-06-06 20:45:41 +00002334 const CXXMethodDecl *MD = *I;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002335
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00002336 if (!MD->isVirtual())
2337 continue;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002338
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00002339 if (MD->isPure())
2340 continue;
Eli Friedmanf2c79b62009-12-08 03:56:49 +00002341
Anders Carlssonf98849e2009-12-02 17:15:43 +00002342 // Ignore implicit member functions, they are always marked as inline, but
2343 // they don't have a body until they're defined.
2344 if (MD->isImplicit())
2345 continue;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002346
Douglas Gregora318efd2010-01-05 19:06:31 +00002347 if (MD->isInlineSpecified())
2348 continue;
Eli Friedman71a26d82009-12-06 20:50:05 +00002349
2350 if (MD->hasInlineBody())
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00002351 continue;
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002352
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00002353 // We found it.
2354 return MD;
2355 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002356
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00002357 return 0;
2358}
2359
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002360DiagnosticBuilder
2361RecordLayoutBuilder::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00002362 return Context.getDiagnostics().Report(Loc, DiagID);
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +00002363}
2364
Anders Carlssondf291d82010-05-26 04:56:53 +00002365/// getASTRecordLayout - Get or compute information about the layout of the
2366/// specified record (struct/union/class), which indicates its size and field
2367/// position information.
Jay Foad39c79802011-01-12 09:06:06 +00002368const ASTRecordLayout &
2369ASTContext::getASTRecordLayout(const RecordDecl *D) const {
John McCall0710e552011-10-07 02:39:22 +00002370 // These asserts test different things. A record has a definition
2371 // as soon as we begin to parse the definition. That definition is
2372 // not a complete definition (which is what isDefinition() tests)
2373 // until we *finish* parsing the definition.
Sean Callanan56c19892012-02-08 00:04:52 +00002374
2375 if (D->hasExternalLexicalStorage() && !D->getDefinition())
2376 getExternalSource()->CompleteType(const_cast<RecordDecl*>(D));
2377
Anders Carlssondf291d82010-05-26 04:56:53 +00002378 D = D->getDefinition();
2379 assert(D && "Cannot get layout of forward declarations!");
John McCallf937c022011-10-07 06:10:15 +00002380 assert(D->isCompleteDefinition() && "Cannot layout type before complete!");
Anders Carlssondf291d82010-05-26 04:56:53 +00002381
2382 // Look up this layout, if already laid out, return what we have.
2383 // Note that we can't save a reference to the entry because this function
2384 // is recursive.
2385 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
2386 if (Entry) return *Entry;
2387
Anders Carlssond2954862010-05-26 05:10:47 +00002388 const ASTRecordLayout *NewEntry;
2389
2390 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
Anders Carlssonc121b4e2010-05-27 00:07:01 +00002391 EmptySubobjectMap EmptySubobjects(*this, RD);
John McCall0153cd32011-11-08 04:01:03 +00002392 RecordLayoutBuilder Builder(*this, &EmptySubobjects);
2393 Builder.Layout(RD);
Anders Carlsson439edd12010-05-27 05:41:06 +00002394
John McCall0153cd32011-11-08 04:01:03 +00002395 // MSVC gives the vb-table pointer an alignment equal to that of
2396 // the non-virtual part of the structure. That's an inherently
2397 // multi-pass operation. If our first pass doesn't give us
2398 // adequate alignment, try again with the specified minimum
2399 // alignment. This is *much* more maintainable than computing the
2400 // alignment in advance in a separately-coded pass; it's also
2401 // significantly more efficient in the common case where the
2402 // vb-table doesn't need extra padding.
2403 if (Builder.VBPtrOffset != CharUnits::fromQuantity(-1) &&
2404 (Builder.VBPtrOffset % Builder.NonVirtualAlignment) != 0) {
2405 Builder.resetWithTargetAlignment(Builder.NonVirtualAlignment);
2406 Builder.Layout(RD);
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002407 }
2408
Anders Carlssond2954862010-05-26 05:10:47 +00002409 // FIXME: This is not always correct. See the part about bitfields at
2410 // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info.
2411 // FIXME: IsPODForThePurposeOfLayout should be stored in the record layout.
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002412 // This does not affect the calculations of MSVC layouts
2413 bool IsPODForThePurposeOfLayout =
John McCall0153cd32011-11-08 04:01:03 +00002414 (!Builder.isMicrosoftCXXABI() && cast<CXXRecordDecl>(D)->isPOD());
Anders Carlssond2954862010-05-26 05:10:47 +00002415
2416 // FIXME: This should be done in FinalizeLayout.
Ken Dyck1b4420e2011-02-28 02:01:38 +00002417 CharUnits DataSize =
John McCall0153cd32011-11-08 04:01:03 +00002418 IsPODForThePurposeOfLayout ? Builder.getSize() : Builder.getDataSize();
Ken Dyck1b4420e2011-02-28 02:01:38 +00002419 CharUnits NonVirtualSize =
John McCall0153cd32011-11-08 04:01:03 +00002420 IsPODForThePurposeOfLayout ? DataSize : Builder.NonVirtualSize;
Anders Carlssond2954862010-05-26 05:10:47 +00002421
Daniel Dunbar592a85c2010-05-27 02:25:46 +00002422 NewEntry =
John McCall0153cd32011-11-08 04:01:03 +00002423 new (*this) ASTRecordLayout(*this, Builder.getSize(),
2424 Builder.Alignment,
John McCalle42a3362012-05-01 08:55:32 +00002425 Builder.HasOwnVFPtr,
John McCall0153cd32011-11-08 04:01:03 +00002426 Builder.VBPtrOffset,
Ken Dyck1b4420e2011-02-28 02:01:38 +00002427 DataSize,
John McCall0153cd32011-11-08 04:01:03 +00002428 Builder.FieldOffsets.data(),
2429 Builder.FieldOffsets.size(),
Ken Dyckaf1c83f2011-02-16 01:52:01 +00002430 NonVirtualSize,
John McCall0153cd32011-11-08 04:01:03 +00002431 Builder.NonVirtualAlignment,
Anders Carlssonc121b4e2010-05-27 00:07:01 +00002432 EmptySubobjects.SizeOfLargestEmptySubobject,
John McCall0153cd32011-11-08 04:01:03 +00002433 Builder.PrimaryBase,
2434 Builder.PrimaryBaseIsVirtual,
2435 Builder.Bases, Builder.VBases);
Anders Carlssond2954862010-05-26 05:10:47 +00002436 } else {
John McCall0153cd32011-11-08 04:01:03 +00002437 RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0);
Anders Carlssond2954862010-05-26 05:10:47 +00002438 Builder.Layout(D);
Daniel Dunbar592a85c2010-05-27 02:25:46 +00002439
Anders Carlssond2954862010-05-26 05:10:47 +00002440 NewEntry =
Ken Dyck1b4420e2011-02-28 02:01:38 +00002441 new (*this) ASTRecordLayout(*this, Builder.getSize(),
Ken Dyck4731d5b2011-02-16 02:05:21 +00002442 Builder.Alignment,
Ken Dyck1b4420e2011-02-28 02:01:38 +00002443 Builder.getSize(),
Anders Carlssond2954862010-05-26 05:10:47 +00002444 Builder.FieldOffsets.data(),
2445 Builder.FieldOffsets.size());
2446 }
2447
Anders Carlssondf291d82010-05-26 04:56:53 +00002448 ASTRecordLayouts[D] = NewEntry;
2449
David Blaikiebbafb8a2012-03-11 07:00:24 +00002450 if (getLangOpts().DumpRecordLayouts) {
Anders Carlssondf291d82010-05-26 04:56:53 +00002451 llvm::errs() << "\n*** Dumping AST Record Layout\n";
David Blaikiebbafb8a2012-03-11 07:00:24 +00002452 DumpRecordLayout(D, llvm::errs(), getLangOpts().DumpRecordLayoutsSimple);
Anders Carlssondf291d82010-05-26 04:56:53 +00002453 }
2454
2455 return *NewEntry;
2456}
2457
2458const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) {
2459 RD = cast<CXXRecordDecl>(RD->getDefinition());
2460 assert(RD && "Cannot get key function for forward declarations!");
Daniel Dunbar592a85c2010-05-27 02:25:46 +00002461
Anders Carlssondf291d82010-05-26 04:56:53 +00002462 const CXXMethodDecl *&Entry = KeyFunctions[RD];
Daniel Dunbar592a85c2010-05-27 02:25:46 +00002463 if (!Entry)
Anders Carlssonc2226202010-05-26 05:58:59 +00002464 Entry = RecordLayoutBuilder::ComputeKeyFunction(RD);
Daniel Dunbar592a85c2010-05-27 02:25:46 +00002465
Anders Carlssondf291d82010-05-26 04:56:53 +00002466 return Entry;
2467}
2468
Richard Smithdafff942012-01-14 04:30:29 +00002469static uint64_t getFieldOffset(const ASTContext &C, const FieldDecl *FD) {
2470 const ASTRecordLayout &Layout = C.getASTRecordLayout(FD->getParent());
2471 return Layout.getFieldOffset(FD->getFieldIndex());
2472}
2473
2474uint64_t ASTContext::getFieldOffset(const ValueDecl *VD) const {
2475 uint64_t OffsetInBits;
2476 if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) {
2477 OffsetInBits = ::getFieldOffset(*this, FD);
2478 } else {
2479 const IndirectFieldDecl *IFD = cast<IndirectFieldDecl>(VD);
2480
2481 OffsetInBits = 0;
2482 for (IndirectFieldDecl::chain_iterator CI = IFD->chain_begin(),
2483 CE = IFD->chain_end();
2484 CI != CE; ++CI)
2485 OffsetInBits += ::getFieldOffset(*this, cast<FieldDecl>(*CI));
2486 }
2487
2488 return OffsetInBits;
2489}
2490
Eric Christopher8a39a012011-10-05 06:00:51 +00002491/// getObjCLayout - Get or compute information about the layout of the
2492/// given interface.
Anders Carlssondf291d82010-05-26 04:56:53 +00002493///
2494/// \param Impl - If given, also include the layout of the interface's
2495/// implementation. This may differ by including synthesized ivars.
2496const ASTRecordLayout &
2497ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
Jay Foad39c79802011-01-12 09:06:06 +00002498 const ObjCImplementationDecl *Impl) const {
Douglas Gregor64d92572011-12-20 15:50:13 +00002499 // Retrieve the definition
Sean Callanand9a909c2012-03-15 16:33:08 +00002500 if (D->hasExternalLexicalStorage() && !D->getDefinition())
2501 getExternalSource()->CompleteType(const_cast<ObjCInterfaceDecl*>(D));
Douglas Gregor64d92572011-12-20 15:50:13 +00002502 D = D->getDefinition();
2503 assert(D && D->isThisDeclarationADefinition() && "Invalid interface decl!");
Anders Carlssondf291d82010-05-26 04:56:53 +00002504
2505 // Look up this layout, if already laid out, return what we have.
2506 ObjCContainerDecl *Key =
2507 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
2508 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
2509 return *Entry;
2510
2511 // Add in synthesized ivar count if laying out an implementation.
2512 if (Impl) {
2513 unsigned SynthCount = CountNonClassIvars(D);
2514 // If there aren't any sythesized ivars then reuse the interface
2515 // entry. Note we can't cache this because we simply free all
2516 // entries later; however we shouldn't look up implementations
2517 // frequently.
2518 if (SynthCount == 0)
2519 return getObjCLayout(D, 0);
2520 }
2521
John McCall0153cd32011-11-08 04:01:03 +00002522 RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0);
Anders Carlsson6ed3a9a2010-05-26 05:04:25 +00002523 Builder.Layout(D);
2524
Anders Carlssondf291d82010-05-26 04:56:53 +00002525 const ASTRecordLayout *NewEntry =
Ken Dyck1b4420e2011-02-28 02:01:38 +00002526 new (*this) ASTRecordLayout(*this, Builder.getSize(),
Ken Dyck4731d5b2011-02-16 02:05:21 +00002527 Builder.Alignment,
Ken Dyck1b4420e2011-02-28 02:01:38 +00002528 Builder.getDataSize(),
Anders Carlsson6ed3a9a2010-05-26 05:04:25 +00002529 Builder.FieldOffsets.data(),
2530 Builder.FieldOffsets.size());
Daniel Dunbar592a85c2010-05-27 02:25:46 +00002531
Anders Carlssondf291d82010-05-26 04:56:53 +00002532 ObjCLayouts[Key] = NewEntry;
2533
2534 return *NewEntry;
2535}
2536
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002537static void PrintOffset(raw_ostream &OS,
Anders Carlsson3f018712010-10-31 23:45:59 +00002538 CharUnits Offset, unsigned IndentLevel) {
Benjamin Kramer96ad7172011-11-05 09:02:52 +00002539 OS << llvm::format("%4" PRId64 " | ", (int64_t)Offset.getQuantity());
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002540 OS.indent(IndentLevel * 2);
2541}
2542
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002543static void DumpCXXRecordLayout(raw_ostream &OS,
Jay Foad39c79802011-01-12 09:06:06 +00002544 const CXXRecordDecl *RD, const ASTContext &C,
Anders Carlsson3f018712010-10-31 23:45:59 +00002545 CharUnits Offset,
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002546 unsigned IndentLevel,
2547 const char* Description,
2548 bool IncludeVirtualBases) {
Anders Carlsson3f018712010-10-31 23:45:59 +00002549 const ASTRecordLayout &Layout = C.getASTRecordLayout(RD);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002550
2551 PrintOffset(OS, Offset, IndentLevel);
Dan Gohman145f3f12010-04-19 16:39:44 +00002552 OS << C.getTypeDeclType(const_cast<CXXRecordDecl *>(RD)).getAsString();
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002553 if (Description)
2554 OS << ' ' << Description;
2555 if (RD->isEmpty())
2556 OS << " (empty)";
2557 OS << '\n';
2558
2559 IndentLevel++;
2560
Anders Carlsson3f018712010-10-31 23:45:59 +00002561 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
John McCalle42a3362012-05-01 08:55:32 +00002562 bool HasVfptr = Layout.hasOwnVFPtr();
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002563 bool HasVbptr = Layout.getVBPtrOffset() != CharUnits::fromQuantity(-1);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002564
2565 // Vtable pointer.
Eli Friedman43114f92011-10-21 22:49:56 +00002566 if (RD->isDynamicClass() && !PrimaryBase &&
2567 C.getTargetInfo().getCXXABI() != CXXABI_Microsoft) {
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002568 PrintOffset(OS, Offset, IndentLevel);
Benjamin Kramerb89514a2011-10-14 18:45:37 +00002569 OS << '(' << *RD << " vtable pointer)\n";
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002570 }
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002571
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002572 // Dump (non-virtual) bases
2573 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
2574 E = RD->bases_end(); I != E; ++I) {
2575 assert(!I->getType()->isDependentType() &&
2576 "Cannot layout class with dependent bases.");
2577 if (I->isVirtual())
2578 continue;
2579
2580 const CXXRecordDecl *Base =
2581 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
2582
Anders Carlsson3f018712010-10-31 23:45:59 +00002583 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base);
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002584
2585 DumpCXXRecordLayout(OS, Base, C, BaseOffset, IndentLevel,
2586 Base == PrimaryBase ? "(primary base)" : "(base)",
2587 /*IncludeVirtualBases=*/false);
2588 }
Eli Friedman43114f92011-10-21 22:49:56 +00002589
2590 // vfptr and vbptr (for Microsoft C++ ABI)
2591 if (HasVfptr) {
John McCalle42a3362012-05-01 08:55:32 +00002592 PrintOffset(OS, Offset, IndentLevel);
Eli Friedman43114f92011-10-21 22:49:56 +00002593 OS << '(' << *RD << " vftable pointer)\n";
2594 }
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002595 if (HasVbptr) {
2596 PrintOffset(OS, Offset + Layout.getVBPtrOffset(), IndentLevel);
Benjamin Kramerb89514a2011-10-14 18:45:37 +00002597 OS << '(' << *RD << " vbtable pointer)\n";
Eli Friedman84d2d3a2011-09-27 19:12:27 +00002598 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002599
2600 // Dump fields.
2601 uint64_t FieldNo = 0;
2602 for (CXXRecordDecl::field_iterator I = RD->field_begin(),
2603 E = RD->field_end(); I != E; ++I, ++FieldNo) {
David Blaikie40ed2972012-06-06 20:45:41 +00002604 const FieldDecl &Field = **I;
Anders Carlsson3f018712010-10-31 23:45:59 +00002605 CharUnits FieldOffset = Offset +
Ken Dyck86a7fcc2011-01-18 01:56:16 +00002606 C.toCharUnitsFromBits(Layout.getFieldOffset(FieldNo));
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002607
David Blaikie2d7c57e2012-04-30 02:36:29 +00002608 if (const RecordType *RT = Field.getType()->getAs<RecordType>()) {
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002609 if (const CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
2610 DumpCXXRecordLayout(OS, D, C, FieldOffset, IndentLevel,
David Blaikie2d7c57e2012-04-30 02:36:29 +00002611 Field.getName().data(),
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002612 /*IncludeVirtualBases=*/true);
2613 continue;
2614 }
2615 }
2616
2617 PrintOffset(OS, FieldOffset, IndentLevel);
David Blaikie2d7c57e2012-04-30 02:36:29 +00002618 OS << Field.getType().getAsString() << ' ' << Field << '\n';
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002619 }
2620
2621 if (!IncludeVirtualBases)
2622 return;
2623
2624 // Dump virtual bases.
John McCalle42a3362012-05-01 08:55:32 +00002625 const ASTRecordLayout::VBaseOffsetsMapTy &vtordisps =
2626 Layout.getVBaseOffsetsMap();
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002627 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
2628 E = RD->vbases_end(); I != E; ++I) {
2629 assert(I->isVirtual() && "Found non-virtual class!");
2630 const CXXRecordDecl *VBase =
2631 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
2632
Anders Carlsson3f018712010-10-31 23:45:59 +00002633 CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBase);
John McCalle42a3362012-05-01 08:55:32 +00002634
2635 if (vtordisps.find(VBase)->second.hasVtorDisp()) {
2636 PrintOffset(OS, VBaseOffset - CharUnits::fromQuantity(4), IndentLevel);
2637 OS << "(vtordisp for vbase " << *VBase << ")\n";
2638 }
2639
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002640 DumpCXXRecordLayout(OS, VBase, C, VBaseOffset, IndentLevel,
2641 VBase == PrimaryBase ?
2642 "(primary virtual base)" : "(virtual base)",
2643 /*IncludeVirtualBases=*/false);
2644 }
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002645
Ken Dyckc8ae5502011-02-09 01:59:34 +00002646 OS << " sizeof=" << Layout.getSize().getQuantity();
Ken Dyckd5090c12011-02-11 02:20:09 +00002647 OS << ", dsize=" << Layout.getDataSize().getQuantity();
Ken Dyck7ad11e72011-02-15 02:32:40 +00002648 OS << ", align=" << Layout.getAlignment().getQuantity() << '\n';
Ken Dyck316d6f62011-02-01 01:52:10 +00002649 OS << " nvsize=" << Layout.getNonVirtualSize().getQuantity();
Ken Dyckbec02852011-02-08 02:02:47 +00002650 OS << ", nvalign=" << Layout.getNonVirtualAlign().getQuantity() << '\n';
Daniel Dunbaraa423af2010-04-08 02:59:49 +00002651 OS << '\n';
2652}
Daniel Dunbarccabe482010-04-19 20:44:53 +00002653
2654void ASTContext::DumpRecordLayout(const RecordDecl *RD,
Douglas Gregore9fc3772012-01-26 07:55:45 +00002655 raw_ostream &OS,
2656 bool Simple) const {
Daniel Dunbarccabe482010-04-19 20:44:53 +00002657 const ASTRecordLayout &Info = getASTRecordLayout(RD);
2658
2659 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Douglas Gregore9fc3772012-01-26 07:55:45 +00002660 if (!Simple)
2661 return DumpCXXRecordLayout(OS, CXXRD, *this, CharUnits(), 0, 0,
2662 /*IncludeVirtualBases=*/true);
Daniel Dunbarccabe482010-04-19 20:44:53 +00002663
2664 OS << "Type: " << getTypeDeclType(RD).getAsString() << "\n";
Douglas Gregore9fc3772012-01-26 07:55:45 +00002665 if (!Simple) {
2666 OS << "Record: ";
2667 RD->dump();
2668 }
Daniel Dunbarccabe482010-04-19 20:44:53 +00002669 OS << "\nLayout: ";
2670 OS << "<ASTRecordLayout\n";
Ken Dyckb0fcc592011-02-11 01:54:29 +00002671 OS << " Size:" << toBits(Info.getSize()) << "\n";
Ken Dyckd5090c12011-02-11 02:20:09 +00002672 OS << " DataSize:" << toBits(Info.getDataSize()) << "\n";
Ken Dyck7ad11e72011-02-15 02:32:40 +00002673 OS << " Alignment:" << toBits(Info.getAlignment()) << "\n";
Daniel Dunbarccabe482010-04-19 20:44:53 +00002674 OS << " FieldOffsets: [";
2675 for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i) {
2676 if (i) OS << ", ";
2677 OS << Info.getFieldOffset(i);
2678 }
2679 OS << "]>\n";
2680}