blob: 0770e1f151439d44bd83ecdac59c05e31ec12706 [file] [log] [blame]
Anders Carlsson9392fa62010-05-26 05:41:04 +00001//=== RecordLayoutBuilder.cpp - Helper class for building record layouts ---==//
Anders Carlssonbda4c102009-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 Carlssonbda4c102009-07-18 20:20:21 +000010#include "clang/AST/Attr.h"
Anders Carlsson245656e2010-11-24 22:55:48 +000011#include "clang/AST/CXXInheritance.h"
Anders Carlssonbda4c102009-07-18 20:20:21 +000012#include "clang/AST/Decl.h"
Anders Carlsson74cbe222009-07-19 00:18:47 +000013#include "clang/AST/DeclCXX.h"
Anders Carlsson93fab9d2009-07-18 20:50:59 +000014#include "clang/AST/DeclObjC.h"
Anders Carlssonbda4c102009-07-18 20:20:21 +000015#include "clang/AST/Expr.h"
Anders Carlsson9392fa62010-05-26 05:41:04 +000016#include "clang/AST/RecordLayout.h"
Anders Carlssonbda4c102009-07-18 20:20:21 +000017#include "clang/Basic/TargetInfo.h"
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +000018#include "clang/Sema/SemaDiagnostic.h"
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +000019#include "llvm/Support/Format.h"
20#include "llvm/ADT/SmallSet.h"
21#include "llvm/Support/MathExtras.h"
Ted Kremenek4d96d9f2011-03-19 01:00:36 +000022#include "llvm/Support/CrashRecoveryContext.h"
Anders Carlssonbda4c102009-07-18 20:20:21 +000023
24using namespace clang;
25
Benjamin Kramer7e220282010-05-26 09:58:31 +000026namespace {
Anders Carlsson6a91c032010-05-26 15:32:58 +000027
Anders Carlssonea2f41c2010-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 Carlsson4a257992010-05-28 21:13:31 +000041 const CXXRecordDecl *Class;
Anders Carlssonea2f41c2010-05-28 21:24:37 +000042
43 /// IsVirtual - Whether the BaseInfo represents a virtual base or not.
Anders Carlsson4a257992010-05-28 21:13:31 +000044 bool IsVirtual;
45
Anders Carlssonea2f41c2010-05-28 21:24:37 +000046 /// Bases - Information about the base subobjects.
47 llvm::SmallVector<BaseSubobjectInfo*, 4> Bases;
48
Anders Carlsson6e264542010-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 Carlssonea2f41c2010-05-28 21:24:37 +000052
53 // FIXME: Document.
54 const BaseSubobjectInfo *Derived;
Anders Carlsson4a257992010-05-28 21:13:31 +000055};
56
Anders Carlsson6a91c032010-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 Foad4ba2a172011-01-12 09:06:06 +000060 const ASTContext &Context;
Anders Carlsson8c6acc62010-10-31 21:54:55 +000061 uint64_t CharWidth;
62
Anders Carlsson6a91c032010-05-26 15:32:58 +000063 /// Class - The class whose empty entries we're keeping track of.
64 const CXXRecordDecl *Class;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +000065
Anders Carlsson58b16b62010-05-27 05:41:06 +000066 /// EmptyClassOffsets - A map from offsets to empty record decls.
67 typedef llvm::SmallVector<const CXXRecordDecl *, 1> ClassVectorTy;
Anders Carlssond8da7632010-10-31 21:22:43 +000068 typedef llvm::DenseMap<CharUnits, ClassVectorTy> EmptyClassOffsetsMapTy;
Anders Carlsson58b16b62010-05-27 05:41:06 +000069 EmptyClassOffsetsMapTy EmptyClassOffsets;
70
Anders Carlssonc8cb4622010-06-08 15:56:03 +000071 /// MaxEmptyClassOffset - The highest offset known to contain an empty
72 /// base subobject.
Anders Carlssonfe5ef732010-10-31 21:39:24 +000073 CharUnits MaxEmptyClassOffset;
Anders Carlssonc8cb4622010-06-08 15:56:03 +000074
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +000075 /// ComputeEmptySubobjectSizes - Compute the size of the largest base or
Anders Carlsson0c54fc92010-05-26 15:54:25 +000076 /// member subobject that is empty.
77 void ComputeEmptySubobjectSizes();
Anders Carlsson58b16b62010-05-27 05:41:06 +000078
Anders Carlssonfe5ef732010-10-31 21:39:24 +000079 void AddSubobjectAtOffset(const CXXRecordDecl *RD, CharUnits Offset);
Anders Carlsson812a3452010-05-27 18:20:57 +000080
Anders Carlssonea2f41c2010-05-28 21:24:37 +000081 void UpdateEmptyBaseSubobjects(const BaseSubobjectInfo *Info,
Anders Carlssona3d43802010-10-31 22:13:23 +000082 CharUnits Offset, bool PlacingEmptyBase);
Anders Carlsson58b16b62010-05-27 05:41:06 +000083
Anders Carlsson812a3452010-05-27 18:20:57 +000084 void UpdateEmptyFieldSubobjects(const CXXRecordDecl *RD,
85 const CXXRecordDecl *Class,
Anders Carlssona3d43802010-10-31 22:13:23 +000086 CharUnits Offset);
87 void UpdateEmptyFieldSubobjects(const FieldDecl *FD, CharUnits Offset);
Anders Carlsson812a3452010-05-27 18:20:57 +000088
Anders Carlssonc8cb4622010-06-08 15:56:03 +000089 /// AnyEmptySubobjectsBeyondOffset - Returns whether there are any empty
90 /// subobjects beyond the given offset.
Anders Carlssonfe5ef732010-10-31 21:39:24 +000091 bool AnyEmptySubobjectsBeyondOffset(CharUnits Offset) const {
Anders Carlssonc8cb4622010-06-08 15:56:03 +000092 return Offset <= MaxEmptyClassOffset;
93 }
94
Anders Carlsson8c6acc62010-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 Dyckff3a5172011-01-24 01:28:50 +0000101 return Context.toCharUnitsFromBits(FieldOffset);
Anders Carlssond8da7632010-10-31 21:22:43 +0000102 }
Anders Carlssond8da7632010-10-31 21:22:43 +0000103
Charles Davisc9f8aec2010-08-19 00:55:19 +0000104protected:
Anders Carlssonfe5ef732010-10-31 21:39:24 +0000105 bool CanPlaceSubobjectAtOffset(const CXXRecordDecl *RD,
106 CharUnits Offset) const;
Charles Davisc9f8aec2010-08-19 00:55:19 +0000107
108 bool CanPlaceBaseSubobjectAtOffset(const BaseSubobjectInfo *Info,
Anders Carlssona3d43802010-10-31 22:13:23 +0000109 CharUnits Offset);
Charles Davisc9f8aec2010-08-19 00:55:19 +0000110
111 bool CanPlaceFieldSubobjectAtOffset(const CXXRecordDecl *RD,
112 const CXXRecordDecl *Class,
Anders Carlssona3d43802010-10-31 22:13:23 +0000113 CharUnits Offset) const;
Charles Davisc9f8aec2010-08-19 00:55:19 +0000114 bool CanPlaceFieldSubobjectAtOffset(const FieldDecl *FD,
Anders Carlsson8c6acc62010-10-31 21:54:55 +0000115 CharUnits Offset) const;
Charles Davisc9f8aec2010-08-19 00:55:19 +0000116
Anders Carlsson6a91c032010-05-26 15:32:58 +0000117public:
Anders Carlsson0c54fc92010-05-26 15:54:25 +0000118 /// This holds the size of the largest empty subobject (either a base
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000119 /// or a member). Will be zero if the record being built doesn't contain
Anders Carlsson0c54fc92010-05-26 15:54:25 +0000120 /// any empty classes.
Anders Carlssona3d43802010-10-31 22:13:23 +0000121 CharUnits SizeOfLargestEmptySubobject;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000122
Jay Foad4ba2a172011-01-12 09:06:06 +0000123 EmptySubobjectMap(const ASTContext &Context, const CXXRecordDecl *Class)
Anders Carlssona3d43802010-10-31 22:13:23 +0000124 : Context(Context), CharWidth(Context.getCharWidth()), Class(Class) {
Anders Carlsson261febd2010-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 Dunbar0aa7edb2010-05-27 02:25:46 +0000130 /// Returns false if placing the record will result in two components
Anders Carlsson261febd2010-05-27 00:07:01 +0000131 /// (direct or indirect) of the same type having the same offset.
Anders Carlssonc8cb4622010-06-08 15:56:03 +0000132 bool CanPlaceBaseAtOffset(const BaseSubobjectInfo *Info,
Anders Carlssona3d43802010-10-31 22:13:23 +0000133 CharUnits Offset);
Anders Carlsson812a3452010-05-27 18:20:57 +0000134
135 /// CanPlaceFieldAtOffset - Return whether a field can be placed at the given
136 /// offset.
Anders Carlssona3d43802010-10-31 22:13:23 +0000137 bool CanPlaceFieldAtOffset(const FieldDecl *FD, CharUnits Offset);
Anders Carlsson6a91c032010-05-26 15:32:58 +0000138};
Anders Carlsson0c54fc92010-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 Carlssona3d43802010-10-31 22:13:23 +0000147 CharUnits EmptySize;
Anders Carlsson0c54fc92010-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 Dyck5f022d82011-02-09 01:59:34 +0000151 EmptySize = Layout.getSize();
Anders Carlsson0c54fc92010-05-26 15:54:25 +0000152 } else {
153 // Otherwise, we get the largest empty subobject for the decl.
154 EmptySize = Layout.getSizeOfLargestEmptySubobject();
155 }
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000156
Anders Carlssona3d43802010-10-31 22:13:23 +0000157 if (EmptySize > SizeOfLargestEmptySubobject)
158 SizeOfLargestEmptySubobject = EmptySize;
Anders Carlsson0c54fc92010-05-26 15:54:25 +0000159 }
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000160
Anders Carlsson0c54fc92010-05-26 15:54:25 +0000161 // Check the fields.
162 for (CXXRecordDecl::field_iterator I = Class->field_begin(),
163 E = Class->field_end(); I != E; ++I) {
164 const FieldDecl *FD = *I;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000165
166 const RecordType *RT =
Anders Carlsson0c54fc92010-05-26 15:54:25 +0000167 Context.getBaseElementType(FD->getType())->getAs<RecordType>();
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000168
Anders Carlsson0c54fc92010-05-26 15:54:25 +0000169 // We only care about record types.
170 if (!RT)
171 continue;
172
Anders Carlssona3d43802010-10-31 22:13:23 +0000173 CharUnits EmptySize;
Anders Carlsson0c54fc92010-05-26 15:54:25 +0000174 const CXXRecordDecl *MemberDecl = cast<CXXRecordDecl>(RT->getDecl());
175 const ASTRecordLayout &Layout = Context.getASTRecordLayout(MemberDecl);
176 if (MemberDecl->isEmpty()) {
177 // If the class decl is empty, get its size.
Ken Dyck5f022d82011-02-09 01:59:34 +0000178 EmptySize = Layout.getSize();
Anders Carlsson0c54fc92010-05-26 15:54:25 +0000179 } else {
180 // Otherwise, we get the largest empty subobject for the decl.
181 EmptySize = Layout.getSizeOfLargestEmptySubobject();
182 }
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000183
Anders Carlssona3d43802010-10-31 22:13:23 +0000184 if (EmptySize > SizeOfLargestEmptySubobject)
185 SizeOfLargestEmptySubobject = EmptySize;
Anders Carlsson0c54fc92010-05-26 15:54:25 +0000186 }
187}
188
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000189bool
Anders Carlsson812a3452010-05-27 18:20:57 +0000190EmptySubobjectMap::CanPlaceSubobjectAtOffset(const CXXRecordDecl *RD,
Anders Carlssonfe5ef732010-10-31 21:39:24 +0000191 CharUnits Offset) const {
Anders Carlsson812a3452010-05-27 18:20:57 +0000192 // We only need to check empty bases.
193 if (!RD->isEmpty())
194 return true;
195
Anders Carlssonfe5ef732010-10-31 21:39:24 +0000196 EmptyClassOffsetsMapTy::const_iterator I = EmptyClassOffsets.find(Offset);
Anders Carlsson812a3452010-05-27 18:20:57 +0000197 if (I == EmptyClassOffsets.end())
198 return true;
199
200 const ClassVectorTy& Classes = I->second;
201 if (std::find(Classes.begin(), Classes.end(), RD) == Classes.end())
202 return true;
203
204 // There is already an empty class of the same type at this offset.
205 return false;
206}
207
208void EmptySubobjectMap::AddSubobjectAtOffset(const CXXRecordDecl *RD,
Anders Carlssonfe5ef732010-10-31 21:39:24 +0000209 CharUnits Offset) {
Anders Carlsson812a3452010-05-27 18:20:57 +0000210 // We only care about empty bases.
211 if (!RD->isEmpty())
212 return;
213
Rafael Espindola272324b2010-12-29 23:02:58 +0000214 // If we have empty structures inside an union, we can assign both
215 // the same offset. Just avoid pushing them twice in the list.
Anders Carlssonfe5ef732010-10-31 21:39:24 +0000216 ClassVectorTy& Classes = EmptyClassOffsets[Offset];
Rafael Espindola272324b2010-12-29 23:02:58 +0000217 if (std::find(Classes.begin(), Classes.end(), RD) != Classes.end())
218 return;
219
Anders Carlsson812a3452010-05-27 18:20:57 +0000220 Classes.push_back(RD);
Anders Carlssonc8cb4622010-06-08 15:56:03 +0000221
222 // Update the empty class offset.
Anders Carlssonfe5ef732010-10-31 21:39:24 +0000223 if (Offset > MaxEmptyClassOffset)
224 MaxEmptyClassOffset = Offset;
Anders Carlsson812a3452010-05-27 18:20:57 +0000225}
226
227bool
Anders Carlssona3d43802010-10-31 22:13:23 +0000228EmptySubobjectMap::CanPlaceBaseSubobjectAtOffset(const BaseSubobjectInfo *Info,
229 CharUnits Offset) {
Anders Carlsson2177ab72010-06-08 16:20:35 +0000230 // We don't have to keep looking past the maximum offset that's known to
231 // contain an empty class.
Anders Carlssona3d43802010-10-31 22:13:23 +0000232 if (!AnyEmptySubobjectsBeyondOffset(Offset))
Anders Carlsson2177ab72010-06-08 16:20:35 +0000233 return true;
234
Anders Carlssona3d43802010-10-31 22:13:23 +0000235 if (!CanPlaceSubobjectAtOffset(Info->Class, Offset))
Anders Carlsson812a3452010-05-27 18:20:57 +0000236 return false;
237
Anders Carlsson58b16b62010-05-27 05:41:06 +0000238 // Traverse all non-virtual bases.
Anders Carlsson4137d512010-05-29 21:10:24 +0000239 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class);
Anders Carlsson58b16b62010-05-27 05:41:06 +0000240 for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) {
Anders Carlssonea2f41c2010-05-28 21:24:37 +0000241 BaseSubobjectInfo* Base = Info->Bases[I];
Anders Carlsson58b16b62010-05-27 05:41:06 +0000242 if (Base->IsVirtual)
243 continue;
244
Anders Carlsson6a356742010-11-01 00:21:58 +0000245 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class);
Anders Carlsson58b16b62010-05-27 05:41:06 +0000246
247 if (!CanPlaceBaseSubobjectAtOffset(Base, BaseOffset))
248 return false;
249 }
250
Anders Carlsson6e264542010-05-29 17:35:14 +0000251 if (Info->PrimaryVirtualBaseInfo) {
252 BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo;
Anders Carlsson58b16b62010-05-27 05:41:06 +0000253
254 if (Info == PrimaryVirtualBaseInfo->Derived) {
255 if (!CanPlaceBaseSubobjectAtOffset(PrimaryVirtualBaseInfo, Offset))
256 return false;
257 }
258 }
259
Anders Carlsson812a3452010-05-27 18:20:57 +0000260 // Traverse all member variables.
261 unsigned FieldNo = 0;
262 for (CXXRecordDecl::field_iterator I = Info->Class->field_begin(),
263 E = Info->Class->field_end(); I != E; ++I, ++FieldNo) {
264 const FieldDecl *FD = *I;
Anders Carlsson8c6acc62010-10-31 21:54:55 +0000265 if (FD->isBitField())
266 continue;
267
Anders Carlssona3d43802010-10-31 22:13:23 +0000268 CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo);
Anders Carlsson812a3452010-05-27 18:20:57 +0000269 if (!CanPlaceFieldSubobjectAtOffset(FD, FieldOffset))
270 return false;
271 }
272
Anders Carlsson58b16b62010-05-27 05:41:06 +0000273 return true;
274}
275
Anders Carlssonea2f41c2010-05-28 21:24:37 +0000276void EmptySubobjectMap::UpdateEmptyBaseSubobjects(const BaseSubobjectInfo *Info,
Anders Carlssona3d43802010-10-31 22:13:23 +0000277 CharUnits Offset,
Anders Carlssone3362bc2010-06-13 18:00:18 +0000278 bool PlacingEmptyBase) {
279 if (!PlacingEmptyBase && Offset >= SizeOfLargestEmptySubobject) {
280 // We know that the only empty subobjects that can conflict with empty
281 // subobject of non-empty bases, are empty bases that can be placed at
282 // offset zero. Because of this, we only need to keep track of empty base
283 // subobjects with offsets less than the size of the largest empty
284 // subobject for our class.
285 return;
286 }
287
Anders Carlssona3d43802010-10-31 22:13:23 +0000288 AddSubobjectAtOffset(Info->Class, Offset);
Anders Carlsson4137d512010-05-29 21:10:24 +0000289
Anders Carlsson58b16b62010-05-27 05:41:06 +0000290 // Traverse all non-virtual bases.
Anders Carlsson4137d512010-05-29 21:10:24 +0000291 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class);
Anders Carlsson58b16b62010-05-27 05:41:06 +0000292 for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) {
Anders Carlssonea2f41c2010-05-28 21:24:37 +0000293 BaseSubobjectInfo* Base = Info->Bases[I];
Anders Carlsson58b16b62010-05-27 05:41:06 +0000294 if (Base->IsVirtual)
295 continue;
Anders Carlsson4137d512010-05-29 21:10:24 +0000296
Anders Carlsson6a356742010-11-01 00:21:58 +0000297 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class);
Anders Carlssone3362bc2010-06-13 18:00:18 +0000298 UpdateEmptyBaseSubobjects(Base, BaseOffset, PlacingEmptyBase);
Anders Carlsson58b16b62010-05-27 05:41:06 +0000299 }
300
Anders Carlsson6e264542010-05-29 17:35:14 +0000301 if (Info->PrimaryVirtualBaseInfo) {
302 BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo;
Anders Carlsson58b16b62010-05-27 05:41:06 +0000303
304 if (Info == PrimaryVirtualBaseInfo->Derived)
Anders Carlssone3362bc2010-06-13 18:00:18 +0000305 UpdateEmptyBaseSubobjects(PrimaryVirtualBaseInfo, Offset,
306 PlacingEmptyBase);
Anders Carlsson58b16b62010-05-27 05:41:06 +0000307 }
Anders Carlsson812a3452010-05-27 18:20:57 +0000308
Anders Carlsson812a3452010-05-27 18:20:57 +0000309 // Traverse all member variables.
310 unsigned FieldNo = 0;
311 for (CXXRecordDecl::field_iterator I = Info->Class->field_begin(),
312 E = Info->Class->field_end(); I != E; ++I, ++FieldNo) {
313 const FieldDecl *FD = *I;
Anders Carlsson8c6acc62010-10-31 21:54:55 +0000314 if (FD->isBitField())
315 continue;
Anders Carlsson4137d512010-05-29 21:10:24 +0000316
Anders Carlssona3d43802010-10-31 22:13:23 +0000317 CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo);
318 UpdateEmptyFieldSubobjects(FD, FieldOffset);
Anders Carlsson812a3452010-05-27 18:20:57 +0000319 }
Anders Carlsson58b16b62010-05-27 05:41:06 +0000320}
321
Anders Carlsson5b1319c2010-05-29 20:49:49 +0000322bool EmptySubobjectMap::CanPlaceBaseAtOffset(const BaseSubobjectInfo *Info,
Anders Carlssona3d43802010-10-31 22:13:23 +0000323 CharUnits Offset) {
Anders Carlsson261febd2010-05-27 00:07:01 +0000324 // If we know this class doesn't have any empty subobjects we don't need to
325 // bother checking.
Anders Carlssona3d43802010-10-31 22:13:23 +0000326 if (SizeOfLargestEmptySubobject.isZero())
Anders Carlsson261febd2010-05-27 00:07:01 +0000327 return true;
328
Anders Carlsson58b16b62010-05-27 05:41:06 +0000329 if (!CanPlaceBaseSubobjectAtOffset(Info, Offset))
330 return false;
Anders Carlsson812a3452010-05-27 18:20:57 +0000331
332 // We are able to place the base at this offset. Make sure to update the
333 // empty base subobject map.
Anders Carlssone3362bc2010-06-13 18:00:18 +0000334 UpdateEmptyBaseSubobjects(Info, Offset, Info->Class->isEmpty());
Anders Carlsson261febd2010-05-27 00:07:01 +0000335 return true;
336}
337
Anders Carlsson812a3452010-05-27 18:20:57 +0000338bool
339EmptySubobjectMap::CanPlaceFieldSubobjectAtOffset(const CXXRecordDecl *RD,
340 const CXXRecordDecl *Class,
Anders Carlssona3d43802010-10-31 22:13:23 +0000341 CharUnits Offset) const {
Anders Carlsson2177ab72010-06-08 16:20:35 +0000342 // We don't have to keep looking past the maximum offset that's known to
343 // contain an empty class.
Anders Carlssona3d43802010-10-31 22:13:23 +0000344 if (!AnyEmptySubobjectsBeyondOffset(Offset))
Anders Carlsson2177ab72010-06-08 16:20:35 +0000345 return true;
346
Anders Carlssona3d43802010-10-31 22:13:23 +0000347 if (!CanPlaceSubobjectAtOffset(RD, Offset))
Anders Carlsson812a3452010-05-27 18:20:57 +0000348 return false;
349
350 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
351
352 // Traverse all non-virtual bases.
353 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
354 E = RD->bases_end(); I != E; ++I) {
355 if (I->isVirtual())
356 continue;
357
358 const CXXRecordDecl *BaseDecl =
359 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
360
Anders Carlsson6a356742010-11-01 00:21:58 +0000361 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl);
Anders Carlsson812a3452010-05-27 18:20:57 +0000362 if (!CanPlaceFieldSubobjectAtOffset(BaseDecl, Class, BaseOffset))
363 return false;
364 }
365
Anders Carlsson45f5b542010-06-08 19:09:24 +0000366 if (RD == Class) {
367 // This is the most derived class, traverse virtual bases as well.
368 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
369 E = RD->vbases_end(); I != E; ++I) {
370 const CXXRecordDecl *VBaseDecl =
371 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
372
Anders Carlsson3069a0d2010-10-31 23:45:59 +0000373 CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBaseDecl);
Anders Carlsson45f5b542010-06-08 19:09:24 +0000374 if (!CanPlaceFieldSubobjectAtOffset(VBaseDecl, Class, VBaseOffset))
375 return false;
376 }
377 }
378
Anders Carlsson812a3452010-05-27 18:20:57 +0000379 // Traverse all member variables.
380 unsigned FieldNo = 0;
381 for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
382 I != E; ++I, ++FieldNo) {
383 const FieldDecl *FD = *I;
Anders Carlsson8c6acc62010-10-31 21:54:55 +0000384 if (FD->isBitField())
385 continue;
386
Anders Carlssona3d43802010-10-31 22:13:23 +0000387 CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo);
Anders Carlsson812a3452010-05-27 18:20:57 +0000388
389 if (!CanPlaceFieldSubobjectAtOffset(FD, FieldOffset))
390 return false;
391 }
392
393 return true;
394}
395
Anders Carlsson8c6acc62010-10-31 21:54:55 +0000396bool
397EmptySubobjectMap::CanPlaceFieldSubobjectAtOffset(const FieldDecl *FD,
398 CharUnits Offset) const {
Anders Carlsson2177ab72010-06-08 16:20:35 +0000399 // We don't have to keep looking past the maximum offset that's known to
400 // contain an empty class.
Anders Carlsson8c6acc62010-10-31 21:54:55 +0000401 if (!AnyEmptySubobjectsBeyondOffset(Offset))
Anders Carlsson2177ab72010-06-08 16:20:35 +0000402 return true;
403
Anders Carlsson812a3452010-05-27 18:20:57 +0000404 QualType T = FD->getType();
405 if (const RecordType *RT = T->getAs<RecordType>()) {
406 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Anders Carlssona3d43802010-10-31 22:13:23 +0000407 return CanPlaceFieldSubobjectAtOffset(RD, RD, Offset);
Anders Carlsson812a3452010-05-27 18:20:57 +0000408 }
409
410 // If we have an array type we need to look at every element.
411 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) {
412 QualType ElemTy = Context.getBaseElementType(AT);
413 const RecordType *RT = ElemTy->getAs<RecordType>();
414 if (!RT)
415 return true;
416
417 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
418 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
419
420 uint64_t NumElements = Context.getConstantArrayElementCount(AT);
Anders Carlsson8c6acc62010-10-31 21:54:55 +0000421 CharUnits ElementOffset = Offset;
Anders Carlsson812a3452010-05-27 18:20:57 +0000422 for (uint64_t I = 0; I != NumElements; ++I) {
Anders Carlsson2177ab72010-06-08 16:20:35 +0000423 // We don't have to keep looking past the maximum offset that's known to
424 // contain an empty class.
Anders Carlsson8c6acc62010-10-31 21:54:55 +0000425 if (!AnyEmptySubobjectsBeyondOffset(ElementOffset))
Anders Carlsson2177ab72010-06-08 16:20:35 +0000426 return true;
427
Anders Carlssona3d43802010-10-31 22:13:23 +0000428 if (!CanPlaceFieldSubobjectAtOffset(RD, RD, ElementOffset))
Anders Carlsson812a3452010-05-27 18:20:57 +0000429 return false;
430
Ken Dyck5f022d82011-02-09 01:59:34 +0000431 ElementOffset += Layout.getSize();
Anders Carlsson812a3452010-05-27 18:20:57 +0000432 }
433 }
434
435 return true;
436}
437
438bool
Anders Carlssona3d43802010-10-31 22:13:23 +0000439EmptySubobjectMap::CanPlaceFieldAtOffset(const FieldDecl *FD,
440 CharUnits Offset) {
441 if (!CanPlaceFieldSubobjectAtOffset(FD, Offset))
Anders Carlsson812a3452010-05-27 18:20:57 +0000442 return false;
443
444 // We are able to place the member variable at this offset.
445 // Make sure to update the empty base subobject map.
446 UpdateEmptyFieldSubobjects(FD, Offset);
447 return true;
448}
449
450void EmptySubobjectMap::UpdateEmptyFieldSubobjects(const CXXRecordDecl *RD,
451 const CXXRecordDecl *Class,
Anders Carlssona3d43802010-10-31 22:13:23 +0000452 CharUnits Offset) {
Anders Carlsson5ccfdd82010-06-13 17:49:16 +0000453 // We know that the only empty subobjects that can conflict with empty
Anders Carlssone3362bc2010-06-13 18:00:18 +0000454 // field subobjects are subobjects of empty bases that can be placed at offset
Anders Carlsson5ccfdd82010-06-13 17:49:16 +0000455 // zero. Because of this, we only need to keep track of empty field
456 // subobjects with offsets less than the size of the largest empty
457 // subobject for our class.
458 if (Offset >= SizeOfLargestEmptySubobject)
459 return;
460
Anders Carlssona3d43802010-10-31 22:13:23 +0000461 AddSubobjectAtOffset(RD, Offset);
Anders Carlsson812a3452010-05-27 18:20:57 +0000462
463 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
464
465 // Traverse all non-virtual bases.
466 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
467 E = RD->bases_end(); I != E; ++I) {
468 if (I->isVirtual())
469 continue;
470
471 const CXXRecordDecl *BaseDecl =
472 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
473
Anders Carlsson6a356742010-11-01 00:21:58 +0000474 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl);
Anders Carlsson812a3452010-05-27 18:20:57 +0000475 UpdateEmptyFieldSubobjects(BaseDecl, Class, BaseOffset);
476 }
477
Anders Carlsson45f5b542010-06-08 19:09:24 +0000478 if (RD == Class) {
479 // This is the most derived class, traverse virtual bases as well.
480 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
481 E = RD->vbases_end(); I != E; ++I) {
482 const CXXRecordDecl *VBaseDecl =
483 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
484
Anders Carlsson3069a0d2010-10-31 23:45:59 +0000485 CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBaseDecl);
Anders Carlsson45f5b542010-06-08 19:09:24 +0000486 UpdateEmptyFieldSubobjects(VBaseDecl, Class, VBaseOffset);
487 }
488 }
489
Anders Carlsson812a3452010-05-27 18:20:57 +0000490 // Traverse all member variables.
491 unsigned FieldNo = 0;
492 for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
493 I != E; ++I, ++FieldNo) {
494 const FieldDecl *FD = *I;
Anders Carlssonfa84fba2010-11-01 15:14:51 +0000495 if (FD->isBitField())
496 continue;
497
Anders Carlssona3d43802010-10-31 22:13:23 +0000498 CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo);
Anders Carlsson812a3452010-05-27 18:20:57 +0000499
Anders Carlssona3d43802010-10-31 22:13:23 +0000500 UpdateEmptyFieldSubobjects(FD, FieldOffset);
Anders Carlsson812a3452010-05-27 18:20:57 +0000501 }
502}
503
504void EmptySubobjectMap::UpdateEmptyFieldSubobjects(const FieldDecl *FD,
Anders Carlssona3d43802010-10-31 22:13:23 +0000505 CharUnits Offset) {
Anders Carlsson812a3452010-05-27 18:20:57 +0000506 QualType T = FD->getType();
507 if (const RecordType *RT = T->getAs<RecordType>()) {
508 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
509 UpdateEmptyFieldSubobjects(RD, RD, Offset);
510 return;
511 }
512
513 // If we have an array type we need to update every element.
514 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) {
515 QualType ElemTy = Context.getBaseElementType(AT);
516 const RecordType *RT = ElemTy->getAs<RecordType>();
517 if (!RT)
518 return;
519
520 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
521 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
522
523 uint64_t NumElements = Context.getConstantArrayElementCount(AT);
Anders Carlssona3d43802010-10-31 22:13:23 +0000524 CharUnits ElementOffset = Offset;
Anders Carlsson812a3452010-05-27 18:20:57 +0000525
526 for (uint64_t I = 0; I != NumElements; ++I) {
Anders Carlsson5ccfdd82010-06-13 17:49:16 +0000527 // We know that the only empty subobjects that can conflict with empty
Anders Carlssone3362bc2010-06-13 18:00:18 +0000528 // field subobjects are subobjects of empty bases that can be placed at
Anders Carlsson5ccfdd82010-06-13 17:49:16 +0000529 // offset zero. Because of this, we only need to keep track of empty field
530 // subobjects with offsets less than the size of the largest empty
531 // subobject for our class.
532 if (ElementOffset >= SizeOfLargestEmptySubobject)
533 return;
534
Anders Carlsson812a3452010-05-27 18:20:57 +0000535 UpdateEmptyFieldSubobjects(RD, RD, ElementOffset);
Ken Dyck5f022d82011-02-09 01:59:34 +0000536 ElementOffset += Layout.getSize();
Anders Carlsson812a3452010-05-27 18:20:57 +0000537 }
538 }
539}
540
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000541class RecordLayoutBuilder {
Charles Davisc9f8aec2010-08-19 00:55:19 +0000542protected:
Anders Carlsson9392fa62010-05-26 05:41:04 +0000543 // FIXME: Remove this and make the appropriate fields public.
544 friend class clang::ASTContext;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000545
Jay Foad4ba2a172011-01-12 09:06:06 +0000546 const ASTContext &Context;
Anders Carlsson9392fa62010-05-26 05:41:04 +0000547
Anders Carlsson6a91c032010-05-26 15:32:58 +0000548 EmptySubobjectMap *EmptySubobjects;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000549
Anders Carlsson9392fa62010-05-26 05:41:04 +0000550 /// Size - The current size of the record layout.
551 uint64_t Size;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000552
Anders Carlsson9392fa62010-05-26 05:41:04 +0000553 /// Alignment - The current alignment of the record layout.
Ken Dyckea7f6c22011-02-16 02:05:21 +0000554 CharUnits Alignment;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000555
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +0000556 /// \brief The alignment if attribute packed is not used.
Ken Dyck6feb4bb2011-02-16 02:11:31 +0000557 CharUnits UnpackedAlignment;
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +0000558
Anders Carlsson9392fa62010-05-26 05:41:04 +0000559 llvm::SmallVector<uint64_t, 16> FieldOffsets;
560
561 /// Packed - Whether the record is packed or not.
Daniel Dunbarc6082fe2010-05-27 05:45:51 +0000562 unsigned Packed : 1;
563
564 unsigned IsUnion : 1;
565
566 unsigned IsMac68kAlign : 1;
Fariborz Jahanian62055b02011-04-26 23:52:16 +0000567
568 unsigned IsMsStruct : 1;
Anders Carlsson9392fa62010-05-26 05:41:04 +0000569
570 /// UnfilledBitsInLastByte - If the last field laid out was a bitfield,
571 /// this contains the number of bits in the last byte that can be used for
572 /// an adjacent bitfield if necessary.
573 unsigned char UnfilledBitsInLastByte;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000574
Anders Carlsson9392fa62010-05-26 05:41:04 +0000575 /// MaxFieldAlignment - The maximum allowed field alignment. This is set by
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000576 /// #pragma pack.
Ken Dyck834945c2011-02-17 01:49:42 +0000577 CharUnits MaxFieldAlignment;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000578
Anders Carlsson9392fa62010-05-26 05:41:04 +0000579 /// DataSize - The data size of the record being laid out.
580 uint64_t DataSize;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000581
Ken Dycka1fdb0b2011-02-16 01:52:01 +0000582 CharUnits NonVirtualSize;
Ken Dyckdf205382011-02-16 01:43:15 +0000583 CharUnits NonVirtualAlignment;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000584
Fariborz Jahanian340fa242011-05-02 17:20:56 +0000585 CharUnits ZeroLengthBitfieldAlignment;
586
Anders Carlsson9392fa62010-05-26 05:41:04 +0000587 /// PrimaryBase - the primary base class (if one exists) of the class
588 /// we're laying out.
589 const CXXRecordDecl *PrimaryBase;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000590
Anders Carlsson9392fa62010-05-26 05:41:04 +0000591 /// PrimaryBaseIsVirtual - Whether the primary base of the class we're laying
592 /// out is virtual.
593 bool PrimaryBaseIsVirtual;
594
Anders Carlsson376bda92010-10-31 21:01:46 +0000595 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetsMapTy;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000596
Anders Carlsson9392fa62010-05-26 05:41:04 +0000597 /// Bases - base classes and their offsets in the record.
598 BaseOffsetsMapTy Bases;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000599
Anders Carlsson9392fa62010-05-26 05:41:04 +0000600 // VBases - virtual base classes and their offsets in the record.
601 BaseOffsetsMapTy VBases;
602
603 /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are
604 /// primary base classes for some other direct or indirect base class.
Anders Carlsson245656e2010-11-24 22:55:48 +0000605 CXXIndirectPrimaryBaseSet IndirectPrimaryBases;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000606
Anders Carlsson9392fa62010-05-26 05:41:04 +0000607 /// FirstNearlyEmptyVBase - The first nearly empty virtual base class in
608 /// inheritance graph order. Used for determining the primary base class.
609 const CXXRecordDecl *FirstNearlyEmptyVBase;
610
611 /// VisitedVirtualBases - A set of all the visited virtual bases, used to
612 /// avoid visiting virtual bases more than once.
613 llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000614
Jay Foad4ba2a172011-01-12 09:06:06 +0000615 RecordLayoutBuilder(const ASTContext &Context, EmptySubobjectMap
616 *EmptySubobjects)
Ken Dyckea7f6c22011-02-16 02:05:21 +0000617 : Context(Context), EmptySubobjects(EmptySubobjects), Size(0),
Ken Dyck6feb4bb2011-02-16 02:11:31 +0000618 Alignment(CharUnits::One()), UnpackedAlignment(Alignment),
Fariborz Jahanian62055b02011-04-26 23:52:16 +0000619 Packed(false), IsUnion(false),
620 IsMac68kAlign(false), IsMsStruct(false),
Ken Dyck834945c2011-02-17 01:49:42 +0000621 UnfilledBitsInLastByte(0), MaxFieldAlignment(CharUnits::Zero()),
622 DataSize(0), NonVirtualSize(CharUnits::Zero()),
Fariborz Jahanian340fa242011-05-02 17:20:56 +0000623 NonVirtualAlignment(CharUnits::One()),
624 ZeroLengthBitfieldAlignment(CharUnits::Zero()), PrimaryBase(0),
Ken Dyck834945c2011-02-17 01:49:42 +0000625 PrimaryBaseIsVirtual(false), FirstNearlyEmptyVBase(0) { }
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000626
Anders Carlsson9392fa62010-05-26 05:41:04 +0000627 void Layout(const RecordDecl *D);
Anders Carlssonc6cab682010-05-26 15:10:00 +0000628 void Layout(const CXXRecordDecl *D);
Anders Carlsson9392fa62010-05-26 05:41:04 +0000629 void Layout(const ObjCInterfaceDecl *D);
630
631 void LayoutFields(const RecordDecl *D);
632 void LayoutField(const FieldDecl *D);
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +0000633 void LayoutWideBitField(uint64_t FieldSize, uint64_t TypeSize,
634 bool FieldPacked, const FieldDecl *D);
Anders Carlsson9392fa62010-05-26 05:41:04 +0000635 void LayoutBitField(const FieldDecl *D);
636
Anders Carlsson6e264542010-05-29 17:35:14 +0000637 /// BaseSubobjectInfoAllocator - Allocator for BaseSubobjectInfo objects.
638 llvm::SpecificBumpPtrAllocator<BaseSubobjectInfo> BaseSubobjectInfoAllocator;
639
640 typedef llvm::DenseMap<const CXXRecordDecl *, BaseSubobjectInfo *>
641 BaseSubobjectInfoMapTy;
642
643 /// VirtualBaseInfo - Map from all the (direct or indirect) virtual bases
644 /// of the class we're laying out to their base subobject info.
645 BaseSubobjectInfoMapTy VirtualBaseInfo;
646
647 /// NonVirtualBaseInfo - Map from all the direct non-virtual bases of the
648 /// class we're laying out to their base subobject info.
649 BaseSubobjectInfoMapTy NonVirtualBaseInfo;
650
651 /// ComputeBaseSubobjectInfo - Compute the base subobject information for the
652 /// bases of the given class.
653 void ComputeBaseSubobjectInfo(const CXXRecordDecl *RD);
654
655 /// ComputeBaseSubobjectInfo - Compute the base subobject information for a
656 /// single class and all of its base classes.
657 BaseSubobjectInfo *ComputeBaseSubobjectInfo(const CXXRecordDecl *RD,
658 bool IsVirtual,
659 BaseSubobjectInfo *Derived);
Anders Carlsson9392fa62010-05-26 05:41:04 +0000660
661 /// DeterminePrimaryBase - Determine the primary base of the given class.
662 void DeterminePrimaryBase(const CXXRecordDecl *RD);
663
664 void SelectPrimaryVBase(const CXXRecordDecl *RD);
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000665
Ken Dyck5dc989c2011-03-01 01:22:45 +0000666 virtual CharUnits GetVirtualPointersSize(const CXXRecordDecl *RD) const;
Charles Davisc9f8aec2010-08-19 00:55:19 +0000667
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000668 /// LayoutNonVirtualBases - Determines the primary base class (if any) and
Anders Carlsson9392fa62010-05-26 05:41:04 +0000669 /// lays it out. Will then proceed to lay out all non-virtual base clasess.
670 void LayoutNonVirtualBases(const CXXRecordDecl *RD);
671
672 /// LayoutNonVirtualBase - Lays out a single non-virtual base.
Anders Carlsson07cebc52010-05-29 17:42:25 +0000673 void LayoutNonVirtualBase(const BaseSubobjectInfo *Base);
Anders Carlsson9392fa62010-05-26 05:41:04 +0000674
Anders Carlssona2311512010-10-31 22:20:42 +0000675 void AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info,
676 CharUnits Offset);
Anders Carlsson9392fa62010-05-26 05:41:04 +0000677
678 /// LayoutVirtualBases - Lays out all the virtual bases.
679 void LayoutVirtualBases(const CXXRecordDecl *RD,
680 const CXXRecordDecl *MostDerivedClass);
681
682 /// LayoutVirtualBase - Lays out a single virtual base.
Anders Carlsson276b4912010-05-29 17:48:36 +0000683 void LayoutVirtualBase(const BaseSubobjectInfo *Base);
Anders Carlsson9392fa62010-05-26 05:41:04 +0000684
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000685 /// LayoutBase - Will lay out a base and return the offset where it was
Anders Carlssona2311512010-10-31 22:20:42 +0000686 /// placed, in chars.
687 CharUnits LayoutBase(const BaseSubobjectInfo *Base);
Anders Carlsson9392fa62010-05-26 05:41:04 +0000688
Anders Carlssonc6cab682010-05-26 15:10:00 +0000689 /// InitializeLayout - Initialize record layout for the given record decl.
Daniel Dunbarc6082fe2010-05-27 05:45:51 +0000690 void InitializeLayout(const Decl *D);
Anders Carlssonc6cab682010-05-26 15:10:00 +0000691
Anders Carlsson9392fa62010-05-26 05:41:04 +0000692 /// FinishLayout - Finalize record layout. Adjust record size based on the
693 /// alignment.
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +0000694 void FinishLayout(const NamedDecl *D);
Anders Carlsson9392fa62010-05-26 05:41:04 +0000695
Ken Dyck3263e092011-02-19 18:58:07 +0000696 void UpdateAlignment(CharUnits NewAlignment, CharUnits UnpackedNewAlignment);
697 void UpdateAlignment(CharUnits NewAlignment) {
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +0000698 UpdateAlignment(NewAlignment, NewAlignment);
699 }
700
701 void CheckFieldPadding(uint64_t Offset, uint64_t UnpaddedOffset,
702 uint64_t UnpackedOffset, unsigned UnpackedAlign,
703 bool isPacked, const FieldDecl *D);
704
705 DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID);
Anders Carlsson9392fa62010-05-26 05:41:04 +0000706
Ken Dycka0c21c42011-02-24 01:13:28 +0000707 CharUnits getSize() const {
Ken Dyck99113442011-02-24 01:33:05 +0000708 assert(Size % Context.getCharWidth() == 0);
Ken Dycka0c21c42011-02-24 01:13:28 +0000709 return Context.toCharUnitsFromBits(Size);
710 }
711 uint64_t getSizeInBits() const { return Size; }
712
713 void setSize(CharUnits NewSize) { Size = Context.toBits(NewSize); }
714 void setSize(uint64_t NewSize) { Size = NewSize; }
715
716 CharUnits getDataSize() const {
Ken Dyck99113442011-02-24 01:33:05 +0000717 assert(DataSize % Context.getCharWidth() == 0);
Ken Dycka0c21c42011-02-24 01:13:28 +0000718 return Context.toCharUnitsFromBits(DataSize);
719 }
720 uint64_t getDataSizeInBits() const { return DataSize; }
721
722 void setDataSize(CharUnits NewSize) { DataSize = Context.toBits(NewSize); }
723 void setDataSize(uint64_t NewSize) { DataSize = NewSize; }
724
725
Argyrios Kyrtzidisc83d2d72010-08-15 10:17:39 +0000726 RecordLayoutBuilder(const RecordLayoutBuilder&); // DO NOT IMPLEMENT
727 void operator=(const RecordLayoutBuilder&); // DO NOT IMPLEMENT
Anders Carlsson9392fa62010-05-26 05:41:04 +0000728public:
729 static const CXXMethodDecl *ComputeKeyFunction(const CXXRecordDecl *RD);
Argyrios Kyrtzidiscfe17e52010-08-25 00:32:14 +0000730
731 virtual ~RecordLayoutBuilder() { }
Anders Carlsson9392fa62010-05-26 05:41:04 +0000732};
Benjamin Kramer7e220282010-05-26 09:58:31 +0000733} // end anonymous namespace
Anders Carlsson9392fa62010-05-26 05:41:04 +0000734
Anders Carlsson3f066522009-09-22 03:02:06 +0000735void
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000736RecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD) {
Anders Carlsson584e1df2010-03-11 03:39:12 +0000737 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000738 E = RD->bases_end(); I != E; ++I) {
Anders Carlsson584e1df2010-03-11 03:39:12 +0000739 assert(!I->getType()->isDependentType() &&
Sebastian Redl9994a342009-10-25 17:03:50 +0000740 "Cannot layout class with dependent bases.");
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000741
Mike Stump1eb44332009-09-09 15:08:12 +0000742 const CXXRecordDecl *Base =
Anders Carlsson584e1df2010-03-11 03:39:12 +0000743 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson200c5c22010-03-11 00:15:35 +0000744
Anders Carlsson584e1df2010-03-11 03:39:12 +0000745 // Check if this is a nearly empty virtual base.
Anders Carlssondae0cb52010-11-25 01:51:53 +0000746 if (I->isVirtual() && Context.isNearlyEmpty(Base)) {
Anders Carlsson584e1df2010-03-11 03:39:12 +0000747 // If it's not an indirect primary base, then we've found our primary
748 // base.
Anders Carlsson3f066522009-09-22 03:02:06 +0000749 if (!IndirectPrimaryBases.count(Base)) {
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000750 PrimaryBase = Base;
751 PrimaryBaseIsVirtual = true;
Mike Stumpd76264e2009-08-12 21:50:08 +0000752 return;
753 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000754
Anders Carlsson584e1df2010-03-11 03:39:12 +0000755 // Is this the first nearly empty virtual base?
756 if (!FirstNearlyEmptyVBase)
757 FirstNearlyEmptyVBase = Base;
Mike Stumpd76264e2009-08-12 21:50:08 +0000758 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000759
Anders Carlsson200c5c22010-03-11 00:15:35 +0000760 SelectPrimaryVBase(Base);
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000761 if (PrimaryBase)
Zhongxing Xu94ba3802010-02-15 04:28:35 +0000762 return;
Mike Stumpd76264e2009-08-12 21:50:08 +0000763 }
764}
765
Ken Dyck5dc989c2011-03-01 01:22:45 +0000766CharUnits
Charles Davisc9f8aec2010-08-19 00:55:19 +0000767RecordLayoutBuilder::GetVirtualPointersSize(const CXXRecordDecl *RD) const {
Ken Dyck5dc989c2011-03-01 01:22:45 +0000768 return Context.toCharUnitsFromBits(Context.Target.getPointerWidth(0));
Charles Davisc9f8aec2010-08-19 00:55:19 +0000769}
770
Anders Carlsson200c5c22010-03-11 00:15:35 +0000771/// DeterminePrimaryBase - Determine the primary base of the given class.
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000772void RecordLayoutBuilder::DeterminePrimaryBase(const CXXRecordDecl *RD) {
Anders Carlsson200c5c22010-03-11 00:15:35 +0000773 // If the class isn't dynamic, it won't have a primary base.
774 if (!RD->isDynamicClass())
775 return;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000776
Anders Carlsson3f066522009-09-22 03:02:06 +0000777 // Compute all the primary virtual bases for all of our direct and
Mike Stump0880e752009-08-13 23:26:06 +0000778 // indirect bases, and record all their primary virtual base classes.
Anders Carlsson245656e2010-11-24 22:55:48 +0000779 RD->getIndirectPrimaryBases(IndirectPrimaryBases);
Mike Stump0880e752009-08-13 23:26:06 +0000780
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000781 // If the record has a dynamic base class, attempt to choose a primary base
782 // class. It is the first (in direct base class order) non-virtual dynamic
Anders Carlsson3f066522009-09-22 03:02:06 +0000783 // base class, if one exists.
Mike Stump6f376332009-08-05 22:37:18 +0000784 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000785 e = RD->bases_end(); i != e; ++i) {
Anders Carlssonce2009a2009-11-27 22:05:05 +0000786 // Ignore virtual bases.
787 if (i->isVirtual())
788 continue;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000789
Anders Carlssonce2009a2009-11-27 22:05:05 +0000790 const CXXRecordDecl *Base =
791 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
792
793 if (Base->isDynamicClass()) {
794 // We found it.
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000795 PrimaryBase = Base;
796 PrimaryBaseIsVirtual = false;
Anders Carlssonce2009a2009-11-27 22:05:05 +0000797 return;
Mike Stump6f376332009-08-05 22:37:18 +0000798 }
799 }
800
801 // Otherwise, it is the first nearly empty virtual base that is not an
Mike Stump49520942009-08-11 04:03:59 +0000802 // indirect primary virtual base class, if one exists.
Anders Carlsson200c5c22010-03-11 00:15:35 +0000803 if (RD->getNumVBases() != 0) {
804 SelectPrimaryVBase(RD);
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000805 if (PrimaryBase)
Anders Carlsson200c5c22010-03-11 00:15:35 +0000806 return;
807 }
Mike Stump6f376332009-08-05 22:37:18 +0000808
Anders Carlsson200c5c22010-03-11 00:15:35 +0000809 // Otherwise, it is the first nearly empty virtual base that is not an
810 // indirect primary virtual base class, if one exists.
811 if (FirstNearlyEmptyVBase) {
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000812 PrimaryBase = FirstNearlyEmptyVBase;
813 PrimaryBaseIsVirtual = true;
Mike Stump6f376332009-08-05 22:37:18 +0000814 return;
Anders Carlsson200c5c22010-03-11 00:15:35 +0000815 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000816
Anders Carlsson200c5c22010-03-11 00:15:35 +0000817 // Otherwise there is no primary base class.
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000818 assert(!PrimaryBase && "Should not get here with a primary base!");
Mike Stump6f376332009-08-05 22:37:18 +0000819
Anders Carlsson200c5c22010-03-11 00:15:35 +0000820 // Allocate the virtual table pointer at offset zero.
821 assert(DataSize == 0 && "Vtable pointer must be at offset zero!");
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000822
Anders Carlsson200c5c22010-03-11 00:15:35 +0000823 // Update the size.
Ken Dyck5dc989c2011-03-01 01:22:45 +0000824 setSize(getSize() + GetVirtualPointersSize(RD));
Ken Dyckf079b732011-02-28 02:01:38 +0000825 setDataSize(getSize());
Anders Carlsson200c5c22010-03-11 00:15:35 +0000826
Ken Dyck3263e092011-02-19 18:58:07 +0000827 CharUnits UnpackedBaseAlign =
828 Context.toCharUnitsFromBits(Context.Target.getPointerAlign(0));
829 CharUnits BaseAlign = (Packed) ? CharUnits::One() : UnpackedBaseAlign;
Argyrios Kyrtzidis5a821192010-12-09 02:47:58 +0000830
831 // The maximum field alignment overrides base align.
Ken Dyck834945c2011-02-17 01:49:42 +0000832 if (!MaxFieldAlignment.isZero()) {
Ken Dyck3263e092011-02-19 18:58:07 +0000833 BaseAlign = std::min(BaseAlign, MaxFieldAlignment);
834 UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment);
Argyrios Kyrtzidis5a821192010-12-09 02:47:58 +0000835 }
836
Anders Carlsson200c5c22010-03-11 00:15:35 +0000837 // Update the alignment.
Argyrios Kyrtzidis5a821192010-12-09 02:47:58 +0000838 UpdateAlignment(BaseAlign, UnpackedBaseAlign);
Mike Stump6f376332009-08-05 22:37:18 +0000839}
840
Anders Carlsson6e264542010-05-29 17:35:14 +0000841BaseSubobjectInfo *
842RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD,
843 bool IsVirtual,
844 BaseSubobjectInfo *Derived) {
845 BaseSubobjectInfo *Info;
846
847 if (IsVirtual) {
848 // Check if we already have info about this virtual base.
849 BaseSubobjectInfo *&InfoSlot = VirtualBaseInfo[RD];
850 if (InfoSlot) {
851 assert(InfoSlot->Class == RD && "Wrong class for virtual base info!");
852 return InfoSlot;
853 }
854
855 // We don't, create it.
856 InfoSlot = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo;
857 Info = InfoSlot;
858 } else {
859 Info = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo;
860 }
861
862 Info->Class = RD;
863 Info->IsVirtual = IsVirtual;
864 Info->Derived = 0;
865 Info->PrimaryVirtualBaseInfo = 0;
866
867 const CXXRecordDecl *PrimaryVirtualBase = 0;
868 BaseSubobjectInfo *PrimaryVirtualBaseInfo = 0;
869
870 // Check if this base has a primary virtual base.
871 if (RD->getNumVBases()) {
872 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlssonc9e814b2010-11-24 23:12:57 +0000873 if (Layout.isPrimaryBaseVirtual()) {
Anders Carlsson6e264542010-05-29 17:35:14 +0000874 // This base does have a primary virtual base.
875 PrimaryVirtualBase = Layout.getPrimaryBase();
876 assert(PrimaryVirtualBase && "Didn't have a primary virtual base!");
877
878 // Now check if we have base subobject info about this primary base.
879 PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase);
880
881 if (PrimaryVirtualBaseInfo) {
882 if (PrimaryVirtualBaseInfo->Derived) {
883 // We did have info about this primary base, and it turns out that it
884 // has already been claimed as a primary virtual base for another
885 // base.
886 PrimaryVirtualBase = 0;
887 } else {
888 // We can claim this base as our primary base.
889 Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo;
890 PrimaryVirtualBaseInfo->Derived = Info;
891 }
892 }
893 }
894 }
895
896 // Now go through all direct bases.
897 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
898 E = RD->bases_end(); I != E; ++I) {
899 bool IsVirtual = I->isVirtual();
900
901 const CXXRecordDecl *BaseDecl =
902 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
903
904 Info->Bases.push_back(ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, Info));
905 }
906
907 if (PrimaryVirtualBase && !PrimaryVirtualBaseInfo) {
908 // Traversing the bases must have created the base info for our primary
909 // virtual base.
910 PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase);
911 assert(PrimaryVirtualBaseInfo &&
912 "Did not create a primary virtual base!");
913
914 // Claim the primary virtual base as our primary virtual base.
915 Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo;
916 PrimaryVirtualBaseInfo->Derived = Info;
917 }
918
919 return Info;
920}
921
922void RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD) {
923 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
924 E = RD->bases_end(); I != E; ++I) {
925 bool IsVirtual = I->isVirtual();
926
927 const CXXRecordDecl *BaseDecl =
928 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
929
930 // Compute the base subobject info for this base.
931 BaseSubobjectInfo *Info = ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, 0);
932
933 if (IsVirtual) {
934 // ComputeBaseInfo has already added this base for us.
935 assert(VirtualBaseInfo.count(BaseDecl) &&
936 "Did not add virtual base!");
937 } else {
938 // Add the base info to the map of non-virtual bases.
939 assert(!NonVirtualBaseInfo.count(BaseDecl) &&
940 "Non-virtual base already exists!");
941 NonVirtualBaseInfo.insert(std::make_pair(BaseDecl, Info));
942 }
943 }
944}
945
Anders Carlssone239b9d2010-03-10 22:21:28 +0000946void
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000947RecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) {
Anders Carlsson6e264542010-05-29 17:35:14 +0000948 // Then, determine the primary base class.
Anders Carlsson200c5c22010-03-11 00:15:35 +0000949 DeterminePrimaryBase(RD);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000950
Anders Carlsson6e264542010-05-29 17:35:14 +0000951 // Compute base subobject info.
952 ComputeBaseSubobjectInfo(RD);
953
Anders Carlsson200c5c22010-03-11 00:15:35 +0000954 // If we have a primary base class, lay it out.
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000955 if (PrimaryBase) {
956 if (PrimaryBaseIsVirtual) {
Anders Carlsson6e264542010-05-29 17:35:14 +0000957 // If the primary virtual base was a primary virtual base of some other
958 // base class we'll have to steal it.
959 BaseSubobjectInfo *PrimaryBaseInfo = VirtualBaseInfo.lookup(PrimaryBase);
960 PrimaryBaseInfo->Derived = 0;
961
Anders Carlsson200c5c22010-03-11 00:15:35 +0000962 // We have a virtual primary base, insert it as an indirect primary base.
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000963 IndirectPrimaryBases.insert(PrimaryBase);
Anders Carlsson37147ea2010-03-11 05:42:17 +0000964
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000965 assert(!VisitedVirtualBases.count(PrimaryBase) &&
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000966 "vbase already visited!");
967 VisitedVirtualBases.insert(PrimaryBase);
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000968
Anders Carlsson276b4912010-05-29 17:48:36 +0000969 LayoutVirtualBase(PrimaryBaseInfo);
Anders Carlsson07cebc52010-05-29 17:42:25 +0000970 } else {
971 BaseSubobjectInfo *PrimaryBaseInfo =
972 NonVirtualBaseInfo.lookup(PrimaryBase);
973 assert(PrimaryBaseInfo &&
974 "Did not find base info for non-virtual primary base!");
975
976 LayoutNonVirtualBase(PrimaryBaseInfo);
977 }
Anders Carlsson200c5c22010-03-11 00:15:35 +0000978 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000979
Anders Carlsson200c5c22010-03-11 00:15:35 +0000980 // Now lay out the non-virtual bases.
981 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000982 E = RD->bases_end(); I != E; ++I) {
Anders Carlsson200c5c22010-03-11 00:15:35 +0000983
984 // Ignore virtual bases.
985 if (I->isVirtual())
986 continue;
987
Anders Carlsson07cebc52010-05-29 17:42:25 +0000988 const CXXRecordDecl *BaseDecl =
Anders Carlsson200c5c22010-03-11 00:15:35 +0000989 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
990
991 // Skip the primary base.
Anders Carlsson07cebc52010-05-29 17:42:25 +0000992 if (BaseDecl == PrimaryBase && !PrimaryBaseIsVirtual)
Anders Carlsson200c5c22010-03-11 00:15:35 +0000993 continue;
994
995 // Lay out the base.
Anders Carlsson07cebc52010-05-29 17:42:25 +0000996 BaseSubobjectInfo *BaseInfo = NonVirtualBaseInfo.lookup(BaseDecl);
997 assert(BaseInfo && "Did not find base info for non-virtual base!");
998
999 LayoutNonVirtualBase(BaseInfo);
Anders Carlssone239b9d2010-03-10 22:21:28 +00001000 }
1001}
1002
Anders Carlsson07cebc52010-05-29 17:42:25 +00001003void RecordLayoutBuilder::LayoutNonVirtualBase(const BaseSubobjectInfo *Base) {
Anders Carlssone3bdbee2010-03-10 22:26:24 +00001004 // Layout the base.
Anders Carlssona2311512010-10-31 22:20:42 +00001005 CharUnits Offset = LayoutBase(Base);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001006
Anders Carlssone3bdbee2010-03-10 22:26:24 +00001007 // Add its base class offset.
Anders Carlsson07cebc52010-05-29 17:42:25 +00001008 assert(!Bases.count(Base->Class) && "base offset already exists!");
Anders Carlssona2311512010-10-31 22:20:42 +00001009 Bases.insert(std::make_pair(Base->Class, Offset));
Anders Carlsson3cd09cc2010-05-29 19:44:50 +00001010
1011 AddPrimaryVirtualBaseOffsets(Base, Offset);
Anders Carlssone239b9d2010-03-10 22:21:28 +00001012}
Mike Stump968db332009-11-05 04:02:15 +00001013
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001014void
Anders Carlsson3cd09cc2010-05-29 19:44:50 +00001015RecordLayoutBuilder::AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info,
Anders Carlssona2311512010-10-31 22:20:42 +00001016 CharUnits Offset) {
Anders Carlsson3cd09cc2010-05-29 19:44:50 +00001017 // This base isn't interesting, it has no virtual bases.
1018 if (!Info->Class->getNumVBases())
1019 return;
1020
1021 // First, check if we have a virtual primary base to add offsets for.
1022 if (Info->PrimaryVirtualBaseInfo) {
1023 assert(Info->PrimaryVirtualBaseInfo->IsVirtual &&
1024 "Primary virtual base is not virtual!");
1025 if (Info->PrimaryVirtualBaseInfo->Derived == Info) {
1026 // Add the offset.
1027 assert(!VBases.count(Info->PrimaryVirtualBaseInfo->Class) &&
1028 "primary vbase offset already exists!");
1029 VBases.insert(std::make_pair(Info->PrimaryVirtualBaseInfo->Class,
Anders Carlssona2311512010-10-31 22:20:42 +00001030 Offset));
Anders Carlsson97913572010-04-15 16:12:58 +00001031
Anders Carlsson3cd09cc2010-05-29 19:44:50 +00001032 // Traverse the primary virtual base.
1033 AddPrimaryVirtualBaseOffsets(Info->PrimaryVirtualBaseInfo, Offset);
1034 }
Anders Carlsson97913572010-04-15 16:12:58 +00001035 }
1036
Anders Carlsson3cd09cc2010-05-29 19:44:50 +00001037 // Now go through all direct non-virtual bases.
1038 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class);
1039 for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) {
1040 const BaseSubobjectInfo *Base = Info->Bases[I];
1041 if (Base->IsVirtual)
Anders Carlsson97913572010-04-15 16:12:58 +00001042 continue;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001043
Anders Carlsson6a356742010-11-01 00:21:58 +00001044 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class);
Anders Carlsson3cd09cc2010-05-29 19:44:50 +00001045 AddPrimaryVirtualBaseOffsets(Base, BaseOffset);
Anders Carlsson97913572010-04-15 16:12:58 +00001046 }
1047}
1048
1049void
Anders Carlsson7d0918a2010-05-26 05:58:59 +00001050RecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
Anders Carlsson97913572010-04-15 16:12:58 +00001051 const CXXRecordDecl *MostDerivedClass) {
Anders Carlsson88f42962010-03-11 04:33:54 +00001052 const CXXRecordDecl *PrimaryBase;
Anders Carlssonbdda6c12010-04-10 18:42:27 +00001053 bool PrimaryBaseIsVirtual;
Anders Carlsson37147ea2010-03-11 05:42:17 +00001054
Anders Carlssonbdda6c12010-04-10 18:42:27 +00001055 if (MostDerivedClass == RD) {
Anders Carlsson28fdd0a2010-05-26 05:20:58 +00001056 PrimaryBase = this->PrimaryBase;
1057 PrimaryBaseIsVirtual = this->PrimaryBaseIsVirtual;
Anders Carlssonbdda6c12010-04-10 18:42:27 +00001058 } else {
Anders Carlsson0f0e9b02010-04-16 15:07:51 +00001059 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlsson88f42962010-03-11 04:33:54 +00001060 PrimaryBase = Layout.getPrimaryBase();
Anders Carlssonc9e814b2010-11-24 23:12:57 +00001061 PrimaryBaseIsVirtual = Layout.isPrimaryBaseVirtual();
Anders Carlssonbdda6c12010-04-10 18:42:27 +00001062 }
1063
Anders Carlsson622e2472010-03-11 04:24:02 +00001064 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1065 E = RD->bases_end(); I != E; ++I) {
1066 assert(!I->getType()->isDependentType() &&
Sebastian Redl9994a342009-10-25 17:03:50 +00001067 "Cannot layout class with dependent bases.");
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001068
Anders Carlsson276b4912010-05-29 17:48:36 +00001069 const CXXRecordDecl *BaseDecl =
Anders Carlsson622e2472010-03-11 04:24:02 +00001070 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1071
1072 if (I->isVirtual()) {
Anders Carlsson276b4912010-05-29 17:48:36 +00001073 if (PrimaryBase != BaseDecl || !PrimaryBaseIsVirtual) {
1074 bool IndirectPrimaryBase = IndirectPrimaryBases.count(BaseDecl);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001075
Anders Carlssonbdda6c12010-04-10 18:42:27 +00001076 // Only lay out the virtual base if it's not an indirect primary base.
1077 if (!IndirectPrimaryBase) {
1078 // Only visit virtual bases once.
Anders Carlsson276b4912010-05-29 17:48:36 +00001079 if (!VisitedVirtualBases.insert(BaseDecl))
Anders Carlssonbdda6c12010-04-10 18:42:27 +00001080 continue;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001081
Anders Carlsson276b4912010-05-29 17:48:36 +00001082 const BaseSubobjectInfo *BaseInfo = VirtualBaseInfo.lookup(BaseDecl);
1083 assert(BaseInfo && "Did not find virtual base info!");
1084 LayoutVirtualBase(BaseInfo);
Anders Carlsson147b5dd2010-03-11 04:10:39 +00001085 }
Mike Stump968db332009-11-05 04:02:15 +00001086 }
Mike Stump4ef98092009-08-13 22:53:07 +00001087 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001088
Anders Carlsson276b4912010-05-29 17:48:36 +00001089 if (!BaseDecl->getNumVBases()) {
Anders Carlsson622e2472010-03-11 04:24:02 +00001090 // This base isn't interesting since it doesn't have any virtual bases.
1091 continue;
Mike Stumpfe3010d2009-08-16 19:04:13 +00001092 }
Anders Carlsson622e2472010-03-11 04:24:02 +00001093
Anders Carlsson276b4912010-05-29 17:48:36 +00001094 LayoutVirtualBases(BaseDecl, MostDerivedClass);
Mike Stumpeb19fa92009-08-06 13:41:24 +00001095 }
1096}
1097
Anders Carlsson276b4912010-05-29 17:48:36 +00001098void RecordLayoutBuilder::LayoutVirtualBase(const BaseSubobjectInfo *Base) {
Anders Carlsson3cd09cc2010-05-29 19:44:50 +00001099 assert(!Base->Derived && "Trying to lay out a primary virtual base!");
1100
Anders Carlssone3bdbee2010-03-10 22:26:24 +00001101 // Layout the base.
Anders Carlssona2311512010-10-31 22:20:42 +00001102 CharUnits Offset = LayoutBase(Base);
Anders Carlssone3bdbee2010-03-10 22:26:24 +00001103
1104 // Add its base class offset.
Anders Carlsson276b4912010-05-29 17:48:36 +00001105 assert(!VBases.count(Base->Class) && "vbase offset already exists!");
Anders Carlssona2311512010-10-31 22:20:42 +00001106 VBases.insert(std::make_pair(Base->Class, Offset));
Anders Carlsson3cd09cc2010-05-29 19:44:50 +00001107
1108 AddPrimaryVirtualBaseOffsets(Base, Offset);
Anders Carlssone239b9d2010-03-10 22:21:28 +00001109}
1110
Anders Carlssona2311512010-10-31 22:20:42 +00001111CharUnits RecordLayoutBuilder::LayoutBase(const BaseSubobjectInfo *Base) {
Anders Carlssonb1d880b2010-05-29 20:47:33 +00001112 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base->Class);
Anders Carlssone239b9d2010-03-10 22:21:28 +00001113
1114 // If we have an empty base class, try to place it at offset 0.
Anders Carlssonb1d880b2010-05-29 20:47:33 +00001115 if (Base->Class->isEmpty() &&
Anders Carlssona3d43802010-10-31 22:13:23 +00001116 EmptySubobjects->CanPlaceBaseAtOffset(Base, CharUnits::Zero())) {
Ken Dyckf079b732011-02-28 02:01:38 +00001117 setSize(std::max(getSize(), Layout.getSize()));
Anders Carlssone239b9d2010-03-10 22:21:28 +00001118
Anders Carlssona2311512010-10-31 22:20:42 +00001119 return CharUnits::Zero();
Anders Carlssone239b9d2010-03-10 22:21:28 +00001120 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001121
Ken Dyck3263e092011-02-19 18:58:07 +00001122 CharUnits UnpackedBaseAlign = Layout.getNonVirtualAlign();
1123 CharUnits BaseAlign = (Packed) ? CharUnits::One() : UnpackedBaseAlign;
Argyrios Kyrtzidis43ddd9f2010-12-09 00:35:20 +00001124
1125 // The maximum field alignment overrides base align.
Ken Dyck834945c2011-02-17 01:49:42 +00001126 if (!MaxFieldAlignment.isZero()) {
Ken Dyck3263e092011-02-19 18:58:07 +00001127 BaseAlign = std::min(BaseAlign, MaxFieldAlignment);
1128 UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment);
Argyrios Kyrtzidis43ddd9f2010-12-09 00:35:20 +00001129 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001130
Anders Carlssone239b9d2010-03-10 22:21:28 +00001131 // Round up the current record size to the base's alignment boundary.
Ken Dyckf079b732011-02-28 02:01:38 +00001132 CharUnits Offset = getDataSize().RoundUpToAlignment(BaseAlign);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001133
Anders Carlssone239b9d2010-03-10 22:21:28 +00001134 // Try to place the base.
Ken Dyckf079b732011-02-28 02:01:38 +00001135 while (!EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset))
1136 Offset += BaseAlign;
Anders Carlssone239b9d2010-03-10 22:21:28 +00001137
Anders Carlssonb1d880b2010-05-29 20:47:33 +00001138 if (!Base->Class->isEmpty()) {
Anders Carlssone239b9d2010-03-10 22:21:28 +00001139 // Update the data size.
Ken Dyckf079b732011-02-28 02:01:38 +00001140 setDataSize(Offset + Layout.getNonVirtualSize());
Anders Carlssone239b9d2010-03-10 22:21:28 +00001141
Ken Dyckf079b732011-02-28 02:01:38 +00001142 setSize(std::max(getSize(), getDataSize()));
Anders Carlssone239b9d2010-03-10 22:21:28 +00001143 } else
Ken Dyckf079b732011-02-28 02:01:38 +00001144 setSize(std::max(getSize(), Offset + Layout.getSize()));
Anders Carlssone239b9d2010-03-10 22:21:28 +00001145
1146 // Remember max struct/class alignment.
Argyrios Kyrtzidis43ddd9f2010-12-09 00:35:20 +00001147 UpdateAlignment(BaseAlign, UnpackedBaseAlign);
Anders Carlssone239b9d2010-03-10 22:21:28 +00001148
Ken Dyckf079b732011-02-28 02:01:38 +00001149 return Offset;
Anders Carlssone239b9d2010-03-10 22:21:28 +00001150}
1151
Daniel Dunbarc6082fe2010-05-27 05:45:51 +00001152void RecordLayoutBuilder::InitializeLayout(const Decl *D) {
1153 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
1154 IsUnion = RD->isUnion();
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001155
Anders Carlssona5dd7222009-08-08 19:38:24 +00001156 Packed = D->hasAttr<PackedAttr>();
Fariborz Jahanian62055b02011-04-26 23:52:16 +00001157
1158 IsMsStruct = D->hasAttr<MsStructAttr>();
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001159
Daniel Dunbarc6082fe2010-05-27 05:45:51 +00001160 // mac68k alignment supersedes maximum field alignment and attribute aligned,
1161 // and forces all structures to have 2-byte alignment. The IBM docs on it
1162 // allude to additional (more complicated) semantics, especially with regard
1163 // to bit-fields, but gcc appears not to follow that.
1164 if (D->hasAttr<AlignMac68kAttr>()) {
1165 IsMac68kAlign = true;
Ken Dyck834945c2011-02-17 01:49:42 +00001166 MaxFieldAlignment = CharUnits::fromQuantity(2);
Ken Dyckea7f6c22011-02-16 02:05:21 +00001167 Alignment = CharUnits::fromQuantity(2);
Daniel Dunbarc6082fe2010-05-27 05:45:51 +00001168 } else {
1169 if (const MaxFieldAlignmentAttr *MFAA = D->getAttr<MaxFieldAlignmentAttr>())
Ken Dyck834945c2011-02-17 01:49:42 +00001170 MaxFieldAlignment = Context.toCharUnitsFromBits(MFAA->getAlignment());
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001171
Sean Huntcf807c42010-08-18 23:23:40 +00001172 if (unsigned MaxAlign = D->getMaxAlignment())
Ken Dyck3263e092011-02-19 18:58:07 +00001173 UpdateAlignment(Context.toCharUnitsFromBits(MaxAlign));
Daniel Dunbarc6082fe2010-05-27 05:45:51 +00001174 }
Anders Carlssonc6cab682010-05-26 15:10:00 +00001175}
Anders Carlsson74cbe222009-07-19 00:18:47 +00001176
Anders Carlssonc6cab682010-05-26 15:10:00 +00001177void RecordLayoutBuilder::Layout(const RecordDecl *D) {
1178 InitializeLayout(D);
Anders Carlssona2df41c2009-07-18 21:48:39 +00001179 LayoutFields(D);
Mike Stump1eb44332009-09-09 15:08:12 +00001180
Anders Carlssonbda4c102009-07-18 20:20:21 +00001181 // Finally, round the size of the total struct up to the alignment of the
1182 // struct itself.
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001183 FinishLayout(D);
Anders Carlssonc6cab682010-05-26 15:10:00 +00001184}
1185
1186void RecordLayoutBuilder::Layout(const CXXRecordDecl *RD) {
1187 InitializeLayout(RD);
1188
Anders Carlssonc6cab682010-05-26 15:10:00 +00001189 // Lay out the vtable and the non-virtual bases.
1190 LayoutNonVirtualBases(RD);
1191
1192 LayoutFields(RD);
1193
Ken Dyck90ce2db2011-03-10 01:53:59 +00001194 NonVirtualSize = Context.toCharUnitsFromBits(
1195 llvm::RoundUpToAlignment(getSizeInBits(),
1196 Context.Target.getCharAlign()));
Ken Dyckea7f6c22011-02-16 02:05:21 +00001197 NonVirtualAlignment = Alignment;
Anders Carlssonc6cab682010-05-26 15:10:00 +00001198
1199 // Lay out the virtual bases and add the primary virtual base offsets.
1200 LayoutVirtualBases(RD, RD);
1201
1202 VisitedVirtualBases.clear();
Anders Carlssonc6cab682010-05-26 15:10:00 +00001203
1204 // Finally, round the size of the total struct up to the alignment of the
1205 // struct itself.
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001206 FinishLayout(RD);
Anders Carlssonc6cab682010-05-26 15:10:00 +00001207
Anders Carlssona1e87162010-04-10 21:24:48 +00001208#ifndef NDEBUG
Anders Carlssonc6cab682010-05-26 15:10:00 +00001209 // Check that we have base offsets for all bases.
1210 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1211 E = RD->bases_end(); I != E; ++I) {
1212 if (I->isVirtual())
1213 continue;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001214
Anders Carlssonc6cab682010-05-26 15:10:00 +00001215 const CXXRecordDecl *BaseDecl =
1216 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1217
1218 assert(Bases.count(BaseDecl) && "Did not find base offset!");
1219 }
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001220
Anders Carlssonc6cab682010-05-26 15:10:00 +00001221 // And all virtual bases.
1222 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
1223 E = RD->vbases_end(); I != E; ++I) {
1224 const CXXRecordDecl *BaseDecl =
1225 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001226
Anders Carlssonc6cab682010-05-26 15:10:00 +00001227 assert(VBases.count(BaseDecl) && "Did not find base offset!");
Anders Carlssona1e87162010-04-10 21:24:48 +00001228 }
1229#endif
Anders Carlssonbda4c102009-07-18 20:20:21 +00001230}
1231
Anders Carlsson7d0918a2010-05-26 05:58:59 +00001232void RecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D) {
Anders Carlsson93fab9d2009-07-18 20:50:59 +00001233 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
Anders Carlsson0f0e9b02010-04-16 15:07:51 +00001234 const ASTRecordLayout &SL = Context.getASTObjCInterfaceLayout(SD);
Anders Carlsson93fab9d2009-07-18 20:50:59 +00001235
Ken Dyck3263e092011-02-19 18:58:07 +00001236 UpdateAlignment(SL.getAlignment());
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Anders Carlsson93fab9d2009-07-18 20:50:59 +00001238 // We start laying out ivars not at the end of the superclass
1239 // structure, but at the next byte following the last field.
Ken Dycka0c21c42011-02-24 01:13:28 +00001240 setSize(SL.getDataSize());
Ken Dyckf079b732011-02-28 02:01:38 +00001241 setDataSize(getSize());
Anders Carlsson93fab9d2009-07-18 20:50:59 +00001242 }
Mike Stump1eb44332009-09-09 15:08:12 +00001243
Daniel Dunbarc6082fe2010-05-27 05:45:51 +00001244 InitializeLayout(D);
Daniel Dunbar8a2c92c2010-05-27 01:12:46 +00001245
Anders Carlsson93fab9d2009-07-18 20:50:59 +00001246 // Layout each ivar sequentially.
1247 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
Anders Carlsson0f0e9b02010-04-16 15:07:51 +00001248 Context.ShallowCollectObjCIvars(D, Ivars);
Anders Carlsson93fab9d2009-07-18 20:50:59 +00001249 for (unsigned i = 0, e = Ivars.size(); i != e; ++i)
1250 LayoutField(Ivars[i]);
Mike Stump1eb44332009-09-09 15:08:12 +00001251
Anders Carlsson93fab9d2009-07-18 20:50:59 +00001252 // Finally, round the size of the total struct up to the alignment of the
1253 // struct itself.
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001254 FinishLayout(D);
Anders Carlsson93fab9d2009-07-18 20:50:59 +00001255}
1256
Anders Carlsson7d0918a2010-05-26 05:58:59 +00001257void RecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
Anders Carlssona2df41c2009-07-18 21:48:39 +00001258 // Layout each field, for now, just sequentially, respecting alignment. In
1259 // the future, this will need to be tweakable by targets.
Fariborz Jahanian62055b02011-04-26 23:52:16 +00001260 const FieldDecl *LastFD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001261 for (RecordDecl::field_iterator Field = D->field_begin(),
Fariborz Jahanian62055b02011-04-26 23:52:16 +00001262 FieldEnd = D->field_end(); Field != FieldEnd; ++Field) {
1263 if (IsMsStruct) {
Fariborz Jahanian340fa242011-05-02 17:20:56 +00001264 const FieldDecl *FD = (*Field);
1265 if (Context.ZeroBitfieldFollowsBitfield(FD, LastFD)) {
1266 // FIXME. Multiple zero bitfields may follow a bitfield.
1267 // set ZeroLengthBitfieldAlignment to max. of its
1268 // currrent and alignment of 'FD'.
1269 std::pair<CharUnits, CharUnits> FieldInfo =
1270 Context.getTypeInfoInChars(FD->getType());
1271 ZeroLengthBitfieldAlignment = FieldInfo.second;
1272 continue;
1273 }
Fariborz Jahanian62055b02011-04-26 23:52:16 +00001274 // Zero-length bitfields following non-bitfield members are
1275 // ignored:
Fariborz Jahanian14d56ef2011-04-27 17:14:21 +00001276 if (Context.ZeroBitfieldFollowsNonBitfield(FD, LastFD))
Fariborz Jahanian62055b02011-04-26 23:52:16 +00001277 continue;
Fariborz Jahanian62055b02011-04-26 23:52:16 +00001278 LastFD = FD;
1279 }
Anders Carlssona2df41c2009-07-18 21:48:39 +00001280 LayoutField(*Field);
Fariborz Jahanian62055b02011-04-26 23:52:16 +00001281 }
Anders Carlssona2df41c2009-07-18 21:48:39 +00001282}
1283
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001284void RecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize,
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001285 uint64_t TypeSize,
1286 bool FieldPacked,
1287 const FieldDecl *D) {
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001288 assert(Context.getLangOptions().CPlusPlus &&
1289 "Can only have wide bit-fields in C++!");
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001290
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001291 // Itanium C++ ABI 2.4:
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001292 // If sizeof(T)*8 < n, let T' be the largest integral POD type with
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001293 // sizeof(T')*8 <= n.
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001294
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001295 QualType IntegralPODTypes[] = {
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001296 Context.UnsignedCharTy, Context.UnsignedShortTy, Context.UnsignedIntTy,
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001297 Context.UnsignedLongTy, Context.UnsignedLongLongTy
1298 };
1299
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001300 QualType Type;
1301 for (unsigned I = 0, E = llvm::array_lengthof(IntegralPODTypes);
1302 I != E; ++I) {
1303 uint64_t Size = Context.getTypeSize(IntegralPODTypes[I]);
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001304
1305 if (Size > FieldSize)
1306 break;
1307
1308 Type = IntegralPODTypes[I];
1309 }
1310 assert(!Type.isNull() && "Did not find a type!");
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001311
Ken Dyck3b3e1a12011-03-01 01:36:00 +00001312 CharUnits TypeAlign = Context.getTypeAlignInChars(Type);
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001313
1314 // We're not going to use any of the unfilled bits in the last byte.
1315 UnfilledBitsInLastByte = 0;
1316
Anders Carlssonde9f1532010-04-17 20:21:41 +00001317 uint64_t FieldOffset;
Ken Dycka0c21c42011-02-24 01:13:28 +00001318 uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001319
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001320 if (IsUnion) {
Ken Dycka0c21c42011-02-24 01:13:28 +00001321 setDataSize(std::max(getDataSizeInBits(), FieldSize));
Anders Carlssonde9f1532010-04-17 20:21:41 +00001322 FieldOffset = 0;
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001323 } else {
Anders Carlssonde9f1532010-04-17 20:21:41 +00001324 // The bitfield is allocated starting at the next offset aligned appropriately
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001325 // for T', with length n bits.
Ken Dyck3b3e1a12011-03-01 01:36:00 +00001326 FieldOffset = llvm::RoundUpToAlignment(getDataSizeInBits(),
1327 Context.toBits(TypeAlign));
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001328
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001329 uint64_t NewSizeInBits = FieldOffset + FieldSize;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001330
Ken Dyckd5e3ed02011-03-10 02:00:35 +00001331 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
1332 Context.Target.getCharAlign()));
Ken Dycka0c21c42011-02-24 01:13:28 +00001333 UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits;
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001334 }
1335
1336 // Place this field at the current location.
1337 FieldOffsets.push_back(FieldOffset);
1338
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001339 CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, FieldOffset,
Ken Dyck3b3e1a12011-03-01 01:36:00 +00001340 Context.toBits(TypeAlign), FieldPacked, D);
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001341
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001342 // Update the size.
Ken Dycka0c21c42011-02-24 01:13:28 +00001343 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001344
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001345 // Remember max struct/class alignment.
Ken Dyck3b3e1a12011-03-01 01:36:00 +00001346 UpdateAlignment(TypeAlign);
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001347}
1348
Anders Carlsson7d0918a2010-05-26 05:58:59 +00001349void RecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001350 bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
Ken Dycka0c21c42011-02-24 01:13:28 +00001351 uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte;
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001352 uint64_t FieldOffset = IsUnion ? 0 : UnpaddedFieldOffset;
Anders Carlsson0f0e9b02010-04-16 15:07:51 +00001353 uint64_t FieldSize = D->getBitWidth()->EvaluateAsInt(Context).getZExtValue();
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001354
Anders Carlsson0f0e9b02010-04-16 15:07:51 +00001355 std::pair<uint64_t, unsigned> FieldInfo = Context.getTypeInfo(D->getType());
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001356 uint64_t TypeSize = FieldInfo.first;
1357 unsigned FieldAlign = FieldInfo.second;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001358
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001359 if (FieldSize > TypeSize) {
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001360 LayoutWideBitField(FieldSize, TypeSize, FieldPacked, D);
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001361 return;
1362 }
1363
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001364 // The align if the field is not packed. This is to check if the attribute
1365 // was unnecessary (-Wpacked).
1366 unsigned UnpackedFieldAlign = FieldAlign;
1367 uint64_t UnpackedFieldOffset = FieldOffset;
1368 if (!Context.Target.useBitFieldTypeAlignment())
1369 UnpackedFieldAlign = 1;
1370
Anders Carlsson0f0e9b02010-04-16 15:07:51 +00001371 if (FieldPacked || !Context.Target.useBitFieldTypeAlignment())
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001372 FieldAlign = 1;
Sean Huntcf807c42010-08-18 23:23:40 +00001373 FieldAlign = std::max(FieldAlign, D->getMaxAlignment());
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001374 UnpackedFieldAlign = std::max(UnpackedFieldAlign, D->getMaxAlignment());
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001375
1376 // The maximum field alignment overrides the aligned attribute.
Ken Dyck834945c2011-02-17 01:49:42 +00001377 if (!MaxFieldAlignment.isZero()) {
1378 unsigned MaxFieldAlignmentInBits = Context.toBits(MaxFieldAlignment);
1379 FieldAlign = std::min(FieldAlign, MaxFieldAlignmentInBits);
1380 UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignmentInBits);
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001381 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001382
Daniel Dunbarb6a16932010-04-15 06:18:39 +00001383 // Check if we need to add padding to give the field the correct alignment.
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001384 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
Daniel Dunbar84b03162010-06-29 18:34:35 +00001385 FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001386
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001387 if (FieldSize == 0 ||
1388 (UnpackedFieldOffset & (UnpackedFieldAlign-1)) + FieldSize > TypeSize)
1389 UnpackedFieldOffset = llvm::RoundUpToAlignment(UnpackedFieldOffset,
1390 UnpackedFieldAlign);
1391
Daniel Dunbarb6a16932010-04-15 06:18:39 +00001392 // Padding members don't affect overall alignment.
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001393 if (!D->getIdentifier())
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001394 FieldAlign = UnpackedFieldAlign = 1;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001395
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001396 // Place this field at the current location.
1397 FieldOffsets.push_back(FieldOffset);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001398
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001399 CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, UnpackedFieldOffset,
1400 UnpackedFieldAlign, FieldPacked, D);
1401
Anders Carlssone4fc0d92009-11-22 19:13:51 +00001402 // Update DataSize to include the last byte containing (part of) the bitfield.
1403 if (IsUnion) {
1404 // FIXME: I think FieldSize should be TypeSize here.
Ken Dycka0c21c42011-02-24 01:13:28 +00001405 setDataSize(std::max(getDataSizeInBits(), FieldSize));
Anders Carlssone4fc0d92009-11-22 19:13:51 +00001406 } else {
1407 uint64_t NewSizeInBits = FieldOffset + FieldSize;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001408
Ken Dyckd5e3ed02011-03-10 02:00:35 +00001409 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
1410 Context.Target.getCharAlign()));
Ken Dycka0c21c42011-02-24 01:13:28 +00001411 UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits;
Anders Carlssone4fc0d92009-11-22 19:13:51 +00001412 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001413
Anders Carlssone4fc0d92009-11-22 19:13:51 +00001414 // Update the size.
Ken Dycka0c21c42011-02-24 01:13:28 +00001415 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001416
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001417 // Remember max struct/class alignment.
Ken Dyck3263e092011-02-19 18:58:07 +00001418 UpdateAlignment(Context.toCharUnitsFromBits(FieldAlign),
1419 Context.toCharUnitsFromBits(UnpackedFieldAlign));
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001420}
1421
Anders Carlsson7d0918a2010-05-26 05:58:59 +00001422void RecordLayoutBuilder::LayoutField(const FieldDecl *D) {
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001423 if (D->isBitField()) {
1424 LayoutBitField(D);
1425 return;
1426 }
1427
Ken Dycka0c21c42011-02-24 01:13:28 +00001428 uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte;
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001429
Anders Carlssone4fc0d92009-11-22 19:13:51 +00001430 // Reset the unfilled bits.
1431 UnfilledBitsInLastByte = 0;
1432
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001433 bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
Ken Dyck9ed9a252011-02-20 02:06:09 +00001434 CharUnits FieldOffset =
Ken Dycka0c21c42011-02-24 01:13:28 +00001435 IsUnion ? CharUnits::Zero() : getDataSize();
Ken Dyck9ed9a252011-02-20 02:06:09 +00001436 CharUnits FieldSize;
1437 CharUnits FieldAlign;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001438
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001439 if (D->getType()->isIncompleteArrayType()) {
1440 // This is a flexible array member; we can't directly
1441 // query getTypeInfo about these, so we figure it out here.
1442 // Flexible array members don't have any size, but they
1443 // have to be aligned appropriately for their element type.
Ken Dyck9ed9a252011-02-20 02:06:09 +00001444 FieldSize = CharUnits::Zero();
Anders Carlsson0f0e9b02010-04-16 15:07:51 +00001445 const ArrayType* ATy = Context.getAsArrayType(D->getType());
Ken Dyck9ed9a252011-02-20 02:06:09 +00001446 FieldAlign = Context.getTypeAlignInChars(ATy->getElementType());
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001447 } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) {
1448 unsigned AS = RT->getPointeeType().getAddressSpace();
Ken Dyck9ed9a252011-02-20 02:06:09 +00001449 FieldSize =
1450 Context.toCharUnitsFromBits(Context.Target.getPointerWidth(AS));
1451 FieldAlign =
1452 Context.toCharUnitsFromBits(Context.Target.getPointerAlign(AS));
Anders Carlssonbda4c102009-07-18 20:20:21 +00001453 } else {
Ken Dyck9ed9a252011-02-20 02:06:09 +00001454 std::pair<CharUnits, CharUnits> FieldInfo =
1455 Context.getTypeInfoInChars(D->getType());
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001456 FieldSize = FieldInfo.first;
1457 FieldAlign = FieldInfo.second;
Fariborz Jahanian340fa242011-05-02 17:20:56 +00001458 if (ZeroLengthBitfieldAlignment > FieldAlign)
1459 FieldAlign = ZeroLengthBitfieldAlignment;
1460 ZeroLengthBitfieldAlignment = CharUnits::Zero();
Douglas Gregor6f755502011-02-01 15:15:22 +00001461
1462 if (Context.getLangOptions().MSBitfields) {
1463 // If MS bitfield layout is required, figure out what type is being
1464 // laid out and align the field to the width of that type.
1465
1466 // Resolve all typedefs down to their base type and round up the field
1467 // alignment if necessary.
1468 QualType T = Context.getBaseElementType(D->getType());
1469 if (const BuiltinType *BTy = T->getAs<BuiltinType>()) {
Ken Dyck9ed9a252011-02-20 02:06:09 +00001470 CharUnits TypeSize = Context.getTypeSizeInChars(BTy);
Douglas Gregor6f755502011-02-01 15:15:22 +00001471 if (TypeSize > FieldAlign)
1472 FieldAlign = TypeSize;
1473 }
1474 }
Anders Carlssonbda4c102009-07-18 20:20:21 +00001475 }
Mike Stump1eb44332009-09-09 15:08:12 +00001476
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001477 // The align if the field is not packed. This is to check if the attribute
1478 // was unnecessary (-Wpacked).
Ken Dyck9ed9a252011-02-20 02:06:09 +00001479 CharUnits UnpackedFieldAlign = FieldAlign;
1480 CharUnits UnpackedFieldOffset = FieldOffset;
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001481
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001482 if (FieldPacked)
Ken Dyck9ed9a252011-02-20 02:06:09 +00001483 FieldAlign = CharUnits::One();
1484 CharUnits MaxAlignmentInChars =
1485 Context.toCharUnitsFromBits(D->getMaxAlignment());
1486 FieldAlign = std::max(FieldAlign, MaxAlignmentInChars);
1487 UnpackedFieldAlign = std::max(UnpackedFieldAlign, MaxAlignmentInChars);
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001488
1489 // The maximum field alignment overrides the aligned attribute.
Ken Dyck834945c2011-02-17 01:49:42 +00001490 if (!MaxFieldAlignment.isZero()) {
Ken Dyck9ed9a252011-02-20 02:06:09 +00001491 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
1492 UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignment);
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001493 }
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001494
1495 // Round up the current record size to the field's alignment boundary.
Ken Dyck9ed9a252011-02-20 02:06:09 +00001496 FieldOffset = FieldOffset.RoundUpToAlignment(FieldAlign);
1497 UnpackedFieldOffset =
1498 UnpackedFieldOffset.RoundUpToAlignment(UnpackedFieldAlign);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001499
Anders Carlsson83a45e72010-05-30 06:52:33 +00001500 if (!IsUnion && EmptySubobjects) {
1501 // Check if we can place the field at this offset.
Ken Dyck9ed9a252011-02-20 02:06:09 +00001502 while (!EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset)) {
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001503 // We couldn't place the field at the offset. Try again at a new offset.
1504 FieldOffset += FieldAlign;
1505 }
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001506 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001507
Anders Carlssonbda4c102009-07-18 20:20:21 +00001508 // Place this field at the current location.
Ken Dyck9ed9a252011-02-20 02:06:09 +00001509 FieldOffsets.push_back(Context.toBits(FieldOffset));
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Ken Dyck9ed9a252011-02-20 02:06:09 +00001511 CheckFieldPadding(Context.toBits(FieldOffset), UnpaddedFieldOffset,
1512 Context.toBits(UnpackedFieldOffset),
1513 Context.toBits(UnpackedFieldAlign), FieldPacked, D);
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001514
Anders Carlssonbda4c102009-07-18 20:20:21 +00001515 // Reserve space for this field.
Daniel Dunbar6b46cd92011-02-24 16:40:53 +00001516 uint64_t FieldSizeInBits = Context.toBits(FieldSize);
Anders Carlssonbda4c102009-07-18 20:20:21 +00001517 if (IsUnion)
Daniel Dunbar6b46cd92011-02-24 16:40:53 +00001518 setSize(std::max(getSizeInBits(), FieldSizeInBits));
Anders Carlssonbda4c102009-07-18 20:20:21 +00001519 else
Ken Dyckf079b732011-02-28 02:01:38 +00001520 setSize(FieldOffset + FieldSize);
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Anders Carlssona2239352009-09-26 01:34:51 +00001522 // Update the data size.
Daniel Dunbar6b46cd92011-02-24 16:40:53 +00001523 setDataSize(getSizeInBits());
Mike Stump1eb44332009-09-09 15:08:12 +00001524
Anders Carlssonbda4c102009-07-18 20:20:21 +00001525 // Remember max struct/class alignment.
Ken Dyck9ed9a252011-02-20 02:06:09 +00001526 UpdateAlignment(FieldAlign, UnpackedFieldAlign);
Anders Carlssonbda4c102009-07-18 20:20:21 +00001527}
1528
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001529void RecordLayoutBuilder::FinishLayout(const NamedDecl *D) {
Anders Carlssonbda4c102009-07-18 20:20:21 +00001530 // In C++, records cannot be of size 0.
Ken Dycka0c21c42011-02-24 01:13:28 +00001531 if (Context.getLangOptions().CPlusPlus && getSizeInBits() == 0) {
Fariborz Jahanianadf082e2011-02-02 19:36:18 +00001532 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
1533 // Compatibility with gcc requires a class (pod or non-pod)
1534 // which is not empty but of size 0; such as having fields of
1535 // array of zero-length, remains of Size 0
1536 if (RD->isEmpty())
Ken Dyckf079b732011-02-28 02:01:38 +00001537 setSize(CharUnits::One());
Fariborz Jahanianadf082e2011-02-02 19:36:18 +00001538 }
1539 else
Ken Dyckf079b732011-02-28 02:01:38 +00001540 setSize(CharUnits::One());
Fariborz Jahanianadf082e2011-02-02 19:36:18 +00001541 }
Anders Carlssonbda4c102009-07-18 20:20:21 +00001542 // Finally, round the size of the record up to the alignment of the
1543 // record itself.
Ken Dycka0c21c42011-02-24 01:13:28 +00001544 uint64_t UnpaddedSize = getSizeInBits() - UnfilledBitsInLastByte;
Ken Dyckf079b732011-02-28 02:01:38 +00001545 uint64_t UnpackedSizeInBits =
Ken Dycka0c21c42011-02-24 01:13:28 +00001546 llvm::RoundUpToAlignment(getSizeInBits(),
1547 Context.toBits(UnpackedAlignment));
Ken Dyckf079b732011-02-28 02:01:38 +00001548 CharUnits UnpackedSize = Context.toCharUnitsFromBits(UnpackedSizeInBits);
Ken Dycka0c21c42011-02-24 01:13:28 +00001549 setSize(llvm::RoundUpToAlignment(getSizeInBits(), Context.toBits(Alignment)));
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001550
1551 unsigned CharBitNum = Context.Target.getCharWidth();
1552 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) {
1553 // Warn if padding was introduced to the struct/class/union.
Ken Dycka0c21c42011-02-24 01:13:28 +00001554 if (getSizeInBits() > UnpaddedSize) {
1555 unsigned PadSize = getSizeInBits() - UnpaddedSize;
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001556 bool InBits = true;
1557 if (PadSize % CharBitNum == 0) {
1558 PadSize = PadSize / CharBitNum;
1559 InBits = false;
1560 }
1561 Diag(RD->getLocation(), diag::warn_padded_struct_size)
1562 << Context.getTypeDeclType(RD)
1563 << PadSize
1564 << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not
1565 }
1566
1567 // Warn if we packed it unnecessarily. If the alignment is 1 byte don't
1568 // bother since there won't be alignment issues.
Ken Dycka0c21c42011-02-24 01:13:28 +00001569 if (Packed && UnpackedAlignment > CharUnits::One() &&
Ken Dyckf079b732011-02-28 02:01:38 +00001570 getSize() == UnpackedSize)
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001571 Diag(D->getLocation(), diag::warn_unnecessary_packed)
1572 << Context.getTypeDeclType(RD);
1573 }
Anders Carlssonbda4c102009-07-18 20:20:21 +00001574}
1575
Ken Dyck3263e092011-02-19 18:58:07 +00001576void RecordLayoutBuilder::UpdateAlignment(CharUnits NewAlignment,
1577 CharUnits UnpackedNewAlignment) {
Daniel Dunbarc6082fe2010-05-27 05:45:51 +00001578 // The alignment is not modified when using 'mac68k' alignment.
1579 if (IsMac68kAlign)
1580 return;
1581
Ken Dyck3263e092011-02-19 18:58:07 +00001582 if (NewAlignment > Alignment) {
1583 assert(llvm::isPowerOf2_32(NewAlignment.getQuantity() &&
1584 "Alignment not a power of 2"));
1585 Alignment = NewAlignment;
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001586 }
1587
Ken Dyck3263e092011-02-19 18:58:07 +00001588 if (UnpackedNewAlignment > UnpackedAlignment) {
1589 assert(llvm::isPowerOf2_32(UnpackedNewAlignment.getQuantity() &&
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001590 "Alignment not a power of 2"));
Ken Dyck3263e092011-02-19 18:58:07 +00001591 UnpackedAlignment = UnpackedNewAlignment;
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001592 }
1593}
1594
1595void RecordLayoutBuilder::CheckFieldPadding(uint64_t Offset,
1596 uint64_t UnpaddedOffset,
1597 uint64_t UnpackedOffset,
1598 unsigned UnpackedAlign,
1599 bool isPacked,
1600 const FieldDecl *D) {
1601 // We let objc ivars without warning, objc interfaces generally are not used
1602 // for padding tricks.
1603 if (isa<ObjCIvarDecl>(D))
Anders Carlssonbda4c102009-07-18 20:20:21 +00001604 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001605
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001606 unsigned CharBitNum = Context.Target.getCharWidth();
Mike Stump1eb44332009-09-09 15:08:12 +00001607
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001608 // Warn if padding was introduced to the struct/class.
1609 if (!IsUnion && Offset > UnpaddedOffset) {
1610 unsigned PadSize = Offset - UnpaddedOffset;
1611 bool InBits = true;
1612 if (PadSize % CharBitNum == 0) {
1613 PadSize = PadSize / CharBitNum;
1614 InBits = false;
1615 }
1616 if (D->getIdentifier())
1617 Diag(D->getLocation(), diag::warn_padded_struct_field)
1618 << (D->getParent()->isStruct() ? 0 : 1) // struct|class
1619 << Context.getTypeDeclType(D->getParent())
1620 << PadSize
1621 << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1) // plural or not
1622 << D->getIdentifier();
1623 else
1624 Diag(D->getLocation(), diag::warn_padded_struct_anon_field)
1625 << (D->getParent()->isStruct() ? 0 : 1) // struct|class
1626 << Context.getTypeDeclType(D->getParent())
1627 << PadSize
1628 << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not
1629 }
1630
1631 // Warn if we packed it unnecessarily. If the alignment is 1 byte don't
1632 // bother since there won't be alignment issues.
1633 if (isPacked && UnpackedAlign > CharBitNum && Offset == UnpackedOffset)
1634 Diag(D->getLocation(), diag::warn_unnecessary_packed)
1635 << D->getIdentifier();
Anders Carlssonbda4c102009-07-18 20:20:21 +00001636}
Mike Stump1eb44332009-09-09 15:08:12 +00001637
Anders Carlssonf53df232009-12-07 04:35:11 +00001638const CXXMethodDecl *
Anders Carlsson7d0918a2010-05-26 05:58:59 +00001639RecordLayoutBuilder::ComputeKeyFunction(const CXXRecordDecl *RD) {
Daniel Dunbar8d8ab742010-04-19 20:44:53 +00001640 // If a class isn't polymorphic it doesn't have a key function.
Anders Carlssonf53df232009-12-07 04:35:11 +00001641 if (!RD->isPolymorphic())
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00001642 return 0;
Eli Friedman61eab882009-12-08 03:56:49 +00001643
1644 // A class inside an anonymous namespace doesn't have a key function. (Or
1645 // at least, there's no point to assigning a key function to such a class;
1646 // this doesn't affect the ABI.)
1647 if (RD->isInAnonymousNamespace())
1648 return 0;
1649
Argyrios Kyrtzidis3bd5b6c2010-10-13 02:39:41 +00001650 // Template instantiations don't have key functions,see Itanium C++ ABI 5.2.6.
1651 // Same behavior as GCC.
1652 TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
1653 if (TSK == TSK_ImplicitInstantiation ||
1654 TSK == TSK_ExplicitInstantiationDefinition)
1655 return 0;
1656
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001657 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
1658 E = RD->method_end(); I != E; ++I) {
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00001659 const CXXMethodDecl *MD = *I;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001660
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00001661 if (!MD->isVirtual())
1662 continue;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001663
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00001664 if (MD->isPure())
1665 continue;
Eli Friedman61eab882009-12-08 03:56:49 +00001666
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00001667 // Ignore implicit member functions, they are always marked as inline, but
1668 // they don't have a body until they're defined.
1669 if (MD->isImplicit())
1670 continue;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001671
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001672 if (MD->isInlineSpecified())
1673 continue;
Eli Friedmand7d7f672009-12-06 20:50:05 +00001674
1675 if (MD->hasInlineBody())
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00001676 continue;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001677
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00001678 // We found it.
1679 return MD;
1680 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001681
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00001682 return 0;
1683}
1684
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001685DiagnosticBuilder
1686RecordLayoutBuilder::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00001687 return Context.getDiagnostics().Report(Loc, DiagID);
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001688}
1689
Benjamin Kramercb9c0742010-10-22 16:33:16 +00001690namespace {
1691 // This class implements layout specific to the Microsoft ABI.
1692 class MSRecordLayoutBuilder : public RecordLayoutBuilder {
1693 public:
Jay Foad4ba2a172011-01-12 09:06:06 +00001694 MSRecordLayoutBuilder(const ASTContext& Ctx,
1695 EmptySubobjectMap *EmptySubobjects) :
Benjamin Kramercb9c0742010-10-22 16:33:16 +00001696 RecordLayoutBuilder(Ctx, EmptySubobjects) {}
Charles Davisc9f8aec2010-08-19 00:55:19 +00001697
Ken Dyck5dc989c2011-03-01 01:22:45 +00001698 virtual CharUnits GetVirtualPointersSize(const CXXRecordDecl *RD) const;
Benjamin Kramercb9c0742010-10-22 16:33:16 +00001699 };
1700}
Charles Davisc9f8aec2010-08-19 00:55:19 +00001701
Ken Dyck5dc989c2011-03-01 01:22:45 +00001702CharUnits
Charles Davisc9f8aec2010-08-19 00:55:19 +00001703MSRecordLayoutBuilder::GetVirtualPointersSize(const CXXRecordDecl *RD) const {
1704 // We should reserve space for two pointers if the class has both
1705 // virtual functions and virtual bases.
Ken Dyck5dc989c2011-03-01 01:22:45 +00001706 CharUnits PointerWidth =
1707 Context.toCharUnitsFromBits(Context.Target.getPointerWidth(0));
Charles Davisc9f8aec2010-08-19 00:55:19 +00001708 if (RD->isPolymorphic() && RD->getNumVBases() > 0)
Ken Dyck5dc989c2011-03-01 01:22:45 +00001709 return 2 * PointerWidth;
1710 return PointerWidth;
Charles Davisc9f8aec2010-08-19 00:55:19 +00001711}
1712
Anders Carlsson1e641ce2010-05-26 04:56:53 +00001713/// getASTRecordLayout - Get or compute information about the layout of the
1714/// specified record (struct/union/class), which indicates its size and field
1715/// position information.
Jay Foad4ba2a172011-01-12 09:06:06 +00001716const ASTRecordLayout &
1717ASTContext::getASTRecordLayout(const RecordDecl *D) const {
Anders Carlsson1e641ce2010-05-26 04:56:53 +00001718 D = D->getDefinition();
1719 assert(D && "Cannot get layout of forward declarations!");
1720
1721 // Look up this layout, if already laid out, return what we have.
1722 // Note that we can't save a reference to the entry because this function
1723 // is recursive.
1724 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
1725 if (Entry) return *Entry;
1726
Anders Carlsson2f64e372010-05-26 05:10:47 +00001727 const ASTRecordLayout *NewEntry;
1728
1729 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
Anders Carlsson261febd2010-05-27 00:07:01 +00001730 EmptySubobjectMap EmptySubobjects(*this, RD);
Anders Carlsson58b16b62010-05-27 05:41:06 +00001731
Charles Davisc9f8aec2010-08-19 00:55:19 +00001732 // When compiling for Microsoft, use the special MS builder.
Argyrios Kyrtzidiscfe17e52010-08-25 00:32:14 +00001733 llvm::OwningPtr<RecordLayoutBuilder> Builder;
Charles Davis20cf7172010-08-19 02:18:14 +00001734 switch (Target.getCXXABI()) {
1735 default:
Argyrios Kyrtzidiscfe17e52010-08-25 00:32:14 +00001736 Builder.reset(new RecordLayoutBuilder(*this, &EmptySubobjects));
Charles Davis20cf7172010-08-19 02:18:14 +00001737 break;
1738 case CXXABI_Microsoft:
Argyrios Kyrtzidiscfe17e52010-08-25 00:32:14 +00001739 Builder.reset(new MSRecordLayoutBuilder(*this, &EmptySubobjects));
Charles Davis20cf7172010-08-19 02:18:14 +00001740 }
Ted Kremenek4d96d9f2011-03-19 01:00:36 +00001741 // Recover resources if we crash before exiting this method.
Ted Kremenek43d8bcf2011-03-22 01:15:19 +00001742 llvm::CrashRecoveryContextCleanupRegistrar<RecordLayoutBuilder>
1743 RecordBuilderCleanup(Builder.get());
Ted Kremenek4d96d9f2011-03-19 01:00:36 +00001744
Charles Davisc9f8aec2010-08-19 00:55:19 +00001745 Builder->Layout(RD);
Anders Carlsson2f64e372010-05-26 05:10:47 +00001746
1747 // FIXME: This is not always correct. See the part about bitfields at
1748 // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info.
1749 // FIXME: IsPODForThePurposeOfLayout should be stored in the record layout.
1750 bool IsPODForThePurposeOfLayout = cast<CXXRecordDecl>(D)->isPOD();
1751
1752 // FIXME: This should be done in FinalizeLayout.
Ken Dyckf079b732011-02-28 02:01:38 +00001753 CharUnits DataSize =
1754 IsPODForThePurposeOfLayout ? Builder->getSize() : Builder->getDataSize();
1755 CharUnits NonVirtualSize =
1756 IsPODForThePurposeOfLayout ? DataSize : Builder->NonVirtualSize;
Anders Carlsson2f64e372010-05-26 05:10:47 +00001757
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001758 NewEntry =
Ken Dyckf079b732011-02-28 02:01:38 +00001759 new (*this) ASTRecordLayout(*this, Builder->getSize(),
Ken Dyckea7f6c22011-02-16 02:05:21 +00001760 Builder->Alignment,
Ken Dyckf079b732011-02-28 02:01:38 +00001761 DataSize,
Ken Dyckec299032011-02-11 02:20:09 +00001762 Builder->FieldOffsets.data(),
Charles Davisc9f8aec2010-08-19 00:55:19 +00001763 Builder->FieldOffsets.size(),
Ken Dycka1fdb0b2011-02-16 01:52:01 +00001764 NonVirtualSize,
Ken Dyckdf205382011-02-16 01:43:15 +00001765 Builder->NonVirtualAlignment,
Anders Carlsson261febd2010-05-27 00:07:01 +00001766 EmptySubobjects.SizeOfLargestEmptySubobject,
Charles Davisc9f8aec2010-08-19 00:55:19 +00001767 Builder->PrimaryBase,
1768 Builder->PrimaryBaseIsVirtual,
1769 Builder->Bases, Builder->VBases);
Anders Carlsson2f64e372010-05-26 05:10:47 +00001770 } else {
Anders Carlsson261febd2010-05-27 00:07:01 +00001771 RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0);
Anders Carlsson2f64e372010-05-26 05:10:47 +00001772 Builder.Layout(D);
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001773
Anders Carlsson2f64e372010-05-26 05:10:47 +00001774 NewEntry =
Ken Dyckf079b732011-02-28 02:01:38 +00001775 new (*this) ASTRecordLayout(*this, Builder.getSize(),
Ken Dyckea7f6c22011-02-16 02:05:21 +00001776 Builder.Alignment,
Ken Dyckf079b732011-02-28 02:01:38 +00001777 Builder.getSize(),
Anders Carlsson2f64e372010-05-26 05:10:47 +00001778 Builder.FieldOffsets.data(),
1779 Builder.FieldOffsets.size());
1780 }
1781
Anders Carlsson1e641ce2010-05-26 04:56:53 +00001782 ASTRecordLayouts[D] = NewEntry;
1783
1784 if (getLangOptions().DumpRecordLayouts) {
1785 llvm::errs() << "\n*** Dumping AST Record Layout\n";
1786 DumpRecordLayout(D, llvm::errs());
1787 }
1788
1789 return *NewEntry;
1790}
1791
1792const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) {
1793 RD = cast<CXXRecordDecl>(RD->getDefinition());
1794 assert(RD && "Cannot get key function for forward declarations!");
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001795
Anders Carlsson1e641ce2010-05-26 04:56:53 +00001796 const CXXMethodDecl *&Entry = KeyFunctions[RD];
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001797 if (!Entry)
Anders Carlsson7d0918a2010-05-26 05:58:59 +00001798 Entry = RecordLayoutBuilder::ComputeKeyFunction(RD);
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001799
Anders Carlsson1e641ce2010-05-26 04:56:53 +00001800 return Entry;
1801}
1802
1803/// getInterfaceLayoutImpl - Get or compute information about the
1804/// layout of the given interface.
1805///
1806/// \param Impl - If given, also include the layout of the interface's
1807/// implementation. This may differ by including synthesized ivars.
1808const ASTRecordLayout &
1809ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
Jay Foad4ba2a172011-01-12 09:06:06 +00001810 const ObjCImplementationDecl *Impl) const {
Anders Carlsson1e641ce2010-05-26 04:56:53 +00001811 assert(!D->isForwardDecl() && "Invalid interface decl!");
1812
1813 // Look up this layout, if already laid out, return what we have.
1814 ObjCContainerDecl *Key =
1815 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
1816 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
1817 return *Entry;
1818
1819 // Add in synthesized ivar count if laying out an implementation.
1820 if (Impl) {
1821 unsigned SynthCount = CountNonClassIvars(D);
1822 // If there aren't any sythesized ivars then reuse the interface
1823 // entry. Note we can't cache this because we simply free all
1824 // entries later; however we shouldn't look up implementations
1825 // frequently.
1826 if (SynthCount == 0)
1827 return getObjCLayout(D, 0);
1828 }
1829
Anders Carlsson261febd2010-05-27 00:07:01 +00001830 RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0);
Anders Carlsson36cdc612010-05-26 05:04:25 +00001831 Builder.Layout(D);
1832
Anders Carlsson1e641ce2010-05-26 04:56:53 +00001833 const ASTRecordLayout *NewEntry =
Ken Dyckf079b732011-02-28 02:01:38 +00001834 new (*this) ASTRecordLayout(*this, Builder.getSize(),
Ken Dyckea7f6c22011-02-16 02:05:21 +00001835 Builder.Alignment,
Ken Dyckf079b732011-02-28 02:01:38 +00001836 Builder.getDataSize(),
Anders Carlsson36cdc612010-05-26 05:04:25 +00001837 Builder.FieldOffsets.data(),
1838 Builder.FieldOffsets.size());
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001839
Anders Carlsson1e641ce2010-05-26 04:56:53 +00001840 ObjCLayouts[Key] = NewEntry;
1841
1842 return *NewEntry;
1843}
1844
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001845static void PrintOffset(llvm::raw_ostream &OS,
Anders Carlsson3069a0d2010-10-31 23:45:59 +00001846 CharUnits Offset, unsigned IndentLevel) {
1847 OS << llvm::format("%4d | ", Offset.getQuantity());
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001848 OS.indent(IndentLevel * 2);
1849}
1850
1851static void DumpCXXRecordLayout(llvm::raw_ostream &OS,
Jay Foad4ba2a172011-01-12 09:06:06 +00001852 const CXXRecordDecl *RD, const ASTContext &C,
Anders Carlsson3069a0d2010-10-31 23:45:59 +00001853 CharUnits Offset,
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001854 unsigned IndentLevel,
1855 const char* Description,
1856 bool IncludeVirtualBases) {
Anders Carlsson3069a0d2010-10-31 23:45:59 +00001857 const ASTRecordLayout &Layout = C.getASTRecordLayout(RD);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001858
1859 PrintOffset(OS, Offset, IndentLevel);
Dan Gohmancb421fa2010-04-19 16:39:44 +00001860 OS << C.getTypeDeclType(const_cast<CXXRecordDecl *>(RD)).getAsString();
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001861 if (Description)
1862 OS << ' ' << Description;
1863 if (RD->isEmpty())
1864 OS << " (empty)";
1865 OS << '\n';
1866
1867 IndentLevel++;
1868
Anders Carlsson3069a0d2010-10-31 23:45:59 +00001869 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001870
1871 // Vtable pointer.
1872 if (RD->isDynamicClass() && !PrimaryBase) {
1873 PrintOffset(OS, Offset, IndentLevel);
Benjamin Kramer900fc632010-04-17 09:33:03 +00001874 OS << '(' << RD << " vtable pointer)\n";
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001875 }
1876 // Dump (non-virtual) bases
1877 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1878 E = RD->bases_end(); I != E; ++I) {
1879 assert(!I->getType()->isDependentType() &&
1880 "Cannot layout class with dependent bases.");
1881 if (I->isVirtual())
1882 continue;
1883
1884 const CXXRecordDecl *Base =
1885 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1886
Anders Carlsson3069a0d2010-10-31 23:45:59 +00001887 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001888
1889 DumpCXXRecordLayout(OS, Base, C, BaseOffset, IndentLevel,
1890 Base == PrimaryBase ? "(primary base)" : "(base)",
1891 /*IncludeVirtualBases=*/false);
1892 }
1893
1894 // Dump fields.
1895 uint64_t FieldNo = 0;
1896 for (CXXRecordDecl::field_iterator I = RD->field_begin(),
1897 E = RD->field_end(); I != E; ++I, ++FieldNo) {
1898 const FieldDecl *Field = *I;
Anders Carlsson3069a0d2010-10-31 23:45:59 +00001899 CharUnits FieldOffset = Offset +
Ken Dyckfb1e3bc2011-01-18 01:56:16 +00001900 C.toCharUnitsFromBits(Layout.getFieldOffset(FieldNo));
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001901
1902 if (const RecordType *RT = Field->getType()->getAs<RecordType>()) {
1903 if (const CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
1904 DumpCXXRecordLayout(OS, D, C, FieldOffset, IndentLevel,
Daniel Dunbar4087f272010-08-17 22:39:59 +00001905 Field->getName().data(),
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001906 /*IncludeVirtualBases=*/true);
1907 continue;
1908 }
1909 }
1910
1911 PrintOffset(OS, FieldOffset, IndentLevel);
Benjamin Kramer900fc632010-04-17 09:33:03 +00001912 OS << Field->getType().getAsString() << ' ' << Field << '\n';
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001913 }
1914
1915 if (!IncludeVirtualBases)
1916 return;
1917
1918 // Dump virtual bases.
1919 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
1920 E = RD->vbases_end(); I != E; ++I) {
1921 assert(I->isVirtual() && "Found non-virtual class!");
1922 const CXXRecordDecl *VBase =
1923 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1924
Anders Carlsson3069a0d2010-10-31 23:45:59 +00001925 CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBase);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001926 DumpCXXRecordLayout(OS, VBase, C, VBaseOffset, IndentLevel,
1927 VBase == PrimaryBase ?
1928 "(primary virtual base)" : "(virtual base)",
1929 /*IncludeVirtualBases=*/false);
1930 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001931
Ken Dyck5f022d82011-02-09 01:59:34 +00001932 OS << " sizeof=" << Layout.getSize().getQuantity();
Ken Dyckec299032011-02-11 02:20:09 +00001933 OS << ", dsize=" << Layout.getDataSize().getQuantity();
Ken Dyckdac54c12011-02-15 02:32:40 +00001934 OS << ", align=" << Layout.getAlignment().getQuantity() << '\n';
Ken Dyck5c3633f2011-02-01 01:52:10 +00001935 OS << " nvsize=" << Layout.getNonVirtualSize().getQuantity();
Ken Dyck68cf1a52011-02-08 02:02:47 +00001936 OS << ", nvalign=" << Layout.getNonVirtualAlign().getQuantity() << '\n';
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001937 OS << '\n';
1938}
Daniel Dunbar8d8ab742010-04-19 20:44:53 +00001939
1940void ASTContext::DumpRecordLayout(const RecordDecl *RD,
Jay Foad4ba2a172011-01-12 09:06:06 +00001941 llvm::raw_ostream &OS) const {
Daniel Dunbar8d8ab742010-04-19 20:44:53 +00001942 const ASTRecordLayout &Info = getASTRecordLayout(RD);
1943
1944 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Anders Carlsson3069a0d2010-10-31 23:45:59 +00001945 return DumpCXXRecordLayout(OS, CXXRD, *this, CharUnits(), 0, 0,
Daniel Dunbar8d8ab742010-04-19 20:44:53 +00001946 /*IncludeVirtualBases=*/true);
1947
1948 OS << "Type: " << getTypeDeclType(RD).getAsString() << "\n";
1949 OS << "Record: ";
1950 RD->dump();
1951 OS << "\nLayout: ";
1952 OS << "<ASTRecordLayout\n";
Ken Dyckdd76a9a2011-02-11 01:54:29 +00001953 OS << " Size:" << toBits(Info.getSize()) << "\n";
Ken Dyckec299032011-02-11 02:20:09 +00001954 OS << " DataSize:" << toBits(Info.getDataSize()) << "\n";
Ken Dyckdac54c12011-02-15 02:32:40 +00001955 OS << " Alignment:" << toBits(Info.getAlignment()) << "\n";
Daniel Dunbar8d8ab742010-04-19 20:44:53 +00001956 OS << " FieldOffsets: [";
1957 for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i) {
1958 if (i) OS << ", ";
1959 OS << Info.getFieldOffset(i);
1960 }
1961 OS << "]>\n";
1962}