blob: eb39d084244ac615855e64ad2e7e25932be0b829 [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
Chandler Carruth55fc8732012-12-04 09:13:33 +000010#include "clang/AST/RecordLayout.h"
Benjamin Kramerd4f51982012-07-04 18:45:14 +000011#include "clang/AST/ASTContext.h"
Anders Carlssonbda4c102009-07-18 20:20:21 +000012#include "clang/AST/Attr.h"
Anders Carlsson245656e2010-11-24 22:55:48 +000013#include "clang/AST/CXXInheritance.h"
Anders Carlssonbda4c102009-07-18 20:20:21 +000014#include "clang/AST/Decl.h"
Anders Carlsson74cbe222009-07-19 00:18:47 +000015#include "clang/AST/DeclCXX.h"
Anders Carlsson93fab9d2009-07-18 20:50:59 +000016#include "clang/AST/DeclObjC.h"
Anders Carlssonbda4c102009-07-18 20:20:21 +000017#include "clang/AST/Expr.h"
Anders Carlssonbda4c102009-07-18 20:20:21 +000018#include "clang/Basic/TargetInfo.h"
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +000019#include "clang/Sema/SemaDiagnostic.h"
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +000020#include "llvm/ADT/SmallSet.h"
Ted Kremenek4d96d9f2011-03-19 01:00:36 +000021#include "llvm/Support/CrashRecoveryContext.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "llvm/Support/Format.h"
23#include "llvm/Support/MathExtras.h"
Anders Carlssonbda4c102009-07-18 20:20:21 +000024
25using namespace clang;
26
Benjamin Kramer7e220282010-05-26 09:58:31 +000027namespace {
Anders Carlsson6a91c032010-05-26 15:32:58 +000028
Anders Carlssonea2f41c2010-05-28 21:24:37 +000029/// BaseSubobjectInfo - Represents a single base subobject in a complete class.
30/// For a class hierarchy like
31///
32/// class A { };
33/// class B : A { };
34/// class C : A, B { };
35///
36/// The BaseSubobjectInfo graph for C will have three BaseSubobjectInfo
37/// instances, one for B and two for A.
38///
39/// If a base is virtual, it will only have one BaseSubobjectInfo allocated.
40struct BaseSubobjectInfo {
41 /// Class - The class for this base info.
Anders Carlsson4a257992010-05-28 21:13:31 +000042 const CXXRecordDecl *Class;
Anders Carlssonea2f41c2010-05-28 21:24:37 +000043
44 /// IsVirtual - Whether the BaseInfo represents a virtual base or not.
Anders Carlsson4a257992010-05-28 21:13:31 +000045 bool IsVirtual;
46
Anders Carlssonea2f41c2010-05-28 21:24:37 +000047 /// Bases - Information about the base subobjects.
Chris Lattner5f9e2722011-07-23 10:55:15 +000048 SmallVector<BaseSubobjectInfo*, 4> Bases;
Anders Carlssonea2f41c2010-05-28 21:24:37 +000049
Anders Carlsson6e264542010-05-29 17:35:14 +000050 /// PrimaryVirtualBaseInfo - Holds the base info for the primary virtual base
51 /// of this base info (if one exists).
52 BaseSubobjectInfo *PrimaryVirtualBaseInfo;
Anders Carlssonea2f41c2010-05-28 21:24:37 +000053
54 // FIXME: Document.
55 const BaseSubobjectInfo *Derived;
Anders Carlsson4a257992010-05-28 21:13:31 +000056};
57
Anders Carlsson6a91c032010-05-26 15:32:58 +000058/// EmptySubobjectMap - Keeps track of which empty subobjects exist at different
59/// offsets while laying out a C++ class.
60class EmptySubobjectMap {
Jay Foad4ba2a172011-01-12 09:06:06 +000061 const ASTContext &Context;
Anders Carlsson8c6acc62010-10-31 21:54:55 +000062 uint64_t CharWidth;
63
Anders Carlsson6a91c032010-05-26 15:32:58 +000064 /// Class - The class whose empty entries we're keeping track of.
65 const CXXRecordDecl *Class;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +000066
Anders Carlsson58b16b62010-05-27 05:41:06 +000067 /// EmptyClassOffsets - A map from offsets to empty record decls.
Chris Lattner5f9e2722011-07-23 10:55:15 +000068 typedef SmallVector<const CXXRecordDecl *, 1> ClassVectorTy;
Anders Carlssond8da7632010-10-31 21:22:43 +000069 typedef llvm::DenseMap<CharUnits, ClassVectorTy> EmptyClassOffsetsMapTy;
Anders Carlsson58b16b62010-05-27 05:41:06 +000070 EmptyClassOffsetsMapTy EmptyClassOffsets;
71
Anders Carlssonc8cb4622010-06-08 15:56:03 +000072 /// MaxEmptyClassOffset - The highest offset known to contain an empty
73 /// base subobject.
Anders Carlssonfe5ef732010-10-31 21:39:24 +000074 CharUnits MaxEmptyClassOffset;
Anders Carlssonc8cb4622010-06-08 15:56:03 +000075
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +000076 /// ComputeEmptySubobjectSizes - Compute the size of the largest base or
Anders Carlsson0c54fc92010-05-26 15:54:25 +000077 /// member subobject that is empty.
78 void ComputeEmptySubobjectSizes();
Anders Carlsson58b16b62010-05-27 05:41:06 +000079
Anders Carlssonfe5ef732010-10-31 21:39:24 +000080 void AddSubobjectAtOffset(const CXXRecordDecl *RD, CharUnits Offset);
Anders Carlsson812a3452010-05-27 18:20:57 +000081
Anders Carlssonea2f41c2010-05-28 21:24:37 +000082 void UpdateEmptyBaseSubobjects(const BaseSubobjectInfo *Info,
Anders Carlssona3d43802010-10-31 22:13:23 +000083 CharUnits Offset, bool PlacingEmptyBase);
Anders Carlsson58b16b62010-05-27 05:41:06 +000084
Anders Carlsson812a3452010-05-27 18:20:57 +000085 void UpdateEmptyFieldSubobjects(const CXXRecordDecl *RD,
86 const CXXRecordDecl *Class,
Anders Carlssona3d43802010-10-31 22:13:23 +000087 CharUnits Offset);
88 void UpdateEmptyFieldSubobjects(const FieldDecl *FD, CharUnits Offset);
Anders Carlsson812a3452010-05-27 18:20:57 +000089
Anders Carlssonc8cb4622010-06-08 15:56:03 +000090 /// AnyEmptySubobjectsBeyondOffset - Returns whether there are any empty
91 /// subobjects beyond the given offset.
Anders Carlssonfe5ef732010-10-31 21:39:24 +000092 bool AnyEmptySubobjectsBeyondOffset(CharUnits Offset) const {
Anders Carlssonc8cb4622010-06-08 15:56:03 +000093 return Offset <= MaxEmptyClassOffset;
94 }
95
Anders Carlsson8c6acc62010-10-31 21:54:55 +000096 CharUnits
97 getFieldOffset(const ASTRecordLayout &Layout, unsigned FieldNo) const {
98 uint64_t FieldOffset = Layout.getFieldOffset(FieldNo);
99 assert(FieldOffset % CharWidth == 0 &&
100 "Field offset not at char boundary!");
101
Ken Dyckff3a5172011-01-24 01:28:50 +0000102 return Context.toCharUnitsFromBits(FieldOffset);
Anders Carlssond8da7632010-10-31 21:22:43 +0000103 }
Anders Carlssond8da7632010-10-31 21:22:43 +0000104
Charles Davisc9f8aec2010-08-19 00:55:19 +0000105protected:
Anders Carlssonfe5ef732010-10-31 21:39:24 +0000106 bool CanPlaceSubobjectAtOffset(const CXXRecordDecl *RD,
107 CharUnits Offset) const;
Charles Davisc9f8aec2010-08-19 00:55:19 +0000108
109 bool CanPlaceBaseSubobjectAtOffset(const BaseSubobjectInfo *Info,
Anders Carlssona3d43802010-10-31 22:13:23 +0000110 CharUnits Offset);
Charles Davisc9f8aec2010-08-19 00:55:19 +0000111
112 bool CanPlaceFieldSubobjectAtOffset(const CXXRecordDecl *RD,
113 const CXXRecordDecl *Class,
Anders Carlssona3d43802010-10-31 22:13:23 +0000114 CharUnits Offset) const;
Charles Davisc9f8aec2010-08-19 00:55:19 +0000115 bool CanPlaceFieldSubobjectAtOffset(const FieldDecl *FD,
Anders Carlsson8c6acc62010-10-31 21:54:55 +0000116 CharUnits Offset) const;
Charles Davisc9f8aec2010-08-19 00:55:19 +0000117
Anders Carlsson6a91c032010-05-26 15:32:58 +0000118public:
Anders Carlsson0c54fc92010-05-26 15:54:25 +0000119 /// This holds the size of the largest empty subobject (either a base
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000120 /// or a member). Will be zero if the record being built doesn't contain
Anders Carlsson0c54fc92010-05-26 15:54:25 +0000121 /// any empty classes.
Anders Carlssona3d43802010-10-31 22:13:23 +0000122 CharUnits SizeOfLargestEmptySubobject;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000123
Jay Foad4ba2a172011-01-12 09:06:06 +0000124 EmptySubobjectMap(const ASTContext &Context, const CXXRecordDecl *Class)
Anders Carlssona3d43802010-10-31 22:13:23 +0000125 : Context(Context), CharWidth(Context.getCharWidth()), Class(Class) {
Anders Carlsson261febd2010-05-27 00:07:01 +0000126 ComputeEmptySubobjectSizes();
127 }
128
129 /// CanPlaceBaseAtOffset - Return whether the given base class can be placed
130 /// at the given offset.
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000131 /// Returns false if placing the record will result in two components
Anders Carlsson261febd2010-05-27 00:07:01 +0000132 /// (direct or indirect) of the same type having the same offset.
Anders Carlssonc8cb4622010-06-08 15:56:03 +0000133 bool CanPlaceBaseAtOffset(const BaseSubobjectInfo *Info,
Anders Carlssona3d43802010-10-31 22:13:23 +0000134 CharUnits Offset);
Anders Carlsson812a3452010-05-27 18:20:57 +0000135
136 /// CanPlaceFieldAtOffset - Return whether a field can be placed at the given
137 /// offset.
Anders Carlssona3d43802010-10-31 22:13:23 +0000138 bool CanPlaceFieldAtOffset(const FieldDecl *FD, CharUnits Offset);
Anders Carlsson6a91c032010-05-26 15:32:58 +0000139};
Anders Carlsson0c54fc92010-05-26 15:54:25 +0000140
141void EmptySubobjectMap::ComputeEmptySubobjectSizes() {
142 // Check the bases.
143 for (CXXRecordDecl::base_class_const_iterator I = Class->bases_begin(),
144 E = Class->bases_end(); I != E; ++I) {
145 const CXXRecordDecl *BaseDecl =
146 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
147
Anders Carlssona3d43802010-10-31 22:13:23 +0000148 CharUnits EmptySize;
Anders Carlsson0c54fc92010-05-26 15:54:25 +0000149 const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl);
150 if (BaseDecl->isEmpty()) {
151 // If the class decl is empty, get its size.
Ken Dyck5f022d82011-02-09 01:59:34 +0000152 EmptySize = Layout.getSize();
Anders Carlsson0c54fc92010-05-26 15:54:25 +0000153 } else {
154 // Otherwise, we get the largest empty subobject for the decl.
155 EmptySize = Layout.getSizeOfLargestEmptySubobject();
156 }
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000157
Anders Carlssona3d43802010-10-31 22:13:23 +0000158 if (EmptySize > SizeOfLargestEmptySubobject)
159 SizeOfLargestEmptySubobject = EmptySize;
Anders Carlsson0c54fc92010-05-26 15:54:25 +0000160 }
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000161
Anders Carlsson0c54fc92010-05-26 15:54:25 +0000162 // Check the fields.
163 for (CXXRecordDecl::field_iterator I = Class->field_begin(),
164 E = Class->field_end(); I != E; ++I) {
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000165
166 const RecordType *RT =
David Blaikie581deb32012-06-06 20:45:41 +0000167 Context.getBaseElementType(I->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
Reid Klecknerd6a08d12013-05-14 20:30:42 +0000214 // If we have empty structures inside a union, we can assign both
Rafael Espindola272324b2010-12-29 23:02:58 +0000215 // 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) {
David Blaikie581deb32012-06-06 20:45:41 +0000264 if (I->isBitField())
Anders Carlsson8c6acc62010-10-31 21:54:55 +0000265 continue;
266
Anders Carlssona3d43802010-10-31 22:13:23 +0000267 CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo);
David Blaikie581deb32012-06-06 20:45:41 +0000268 if (!CanPlaceFieldSubobjectAtOffset(*I, FieldOffset))
Anders Carlsson812a3452010-05-27 18:20:57 +0000269 return false;
270 }
271
Anders Carlsson58b16b62010-05-27 05:41:06 +0000272 return true;
273}
274
Anders Carlssonea2f41c2010-05-28 21:24:37 +0000275void EmptySubobjectMap::UpdateEmptyBaseSubobjects(const BaseSubobjectInfo *Info,
Anders Carlssona3d43802010-10-31 22:13:23 +0000276 CharUnits Offset,
Anders Carlssone3362bc2010-06-13 18:00:18 +0000277 bool PlacingEmptyBase) {
278 if (!PlacingEmptyBase && Offset >= SizeOfLargestEmptySubobject) {
279 // We know that the only empty subobjects that can conflict with empty
280 // subobject of non-empty bases, are empty bases that can be placed at
281 // offset zero. Because of this, we only need to keep track of empty base
282 // subobjects with offsets less than the size of the largest empty
283 // subobject for our class.
284 return;
285 }
286
Anders Carlssona3d43802010-10-31 22:13:23 +0000287 AddSubobjectAtOffset(Info->Class, Offset);
Anders Carlsson4137d512010-05-29 21:10:24 +0000288
Anders Carlsson58b16b62010-05-27 05:41:06 +0000289 // Traverse all non-virtual bases.
Anders Carlsson4137d512010-05-29 21:10:24 +0000290 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class);
Anders Carlsson58b16b62010-05-27 05:41:06 +0000291 for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) {
Anders Carlssonea2f41c2010-05-28 21:24:37 +0000292 BaseSubobjectInfo* Base = Info->Bases[I];
Anders Carlsson58b16b62010-05-27 05:41:06 +0000293 if (Base->IsVirtual)
294 continue;
Anders Carlsson4137d512010-05-29 21:10:24 +0000295
Anders Carlsson6a356742010-11-01 00:21:58 +0000296 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class);
Anders Carlssone3362bc2010-06-13 18:00:18 +0000297 UpdateEmptyBaseSubobjects(Base, BaseOffset, PlacingEmptyBase);
Anders Carlsson58b16b62010-05-27 05:41:06 +0000298 }
299
Anders Carlsson6e264542010-05-29 17:35:14 +0000300 if (Info->PrimaryVirtualBaseInfo) {
301 BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo;
Anders Carlsson58b16b62010-05-27 05:41:06 +0000302
303 if (Info == PrimaryVirtualBaseInfo->Derived)
Anders Carlssone3362bc2010-06-13 18:00:18 +0000304 UpdateEmptyBaseSubobjects(PrimaryVirtualBaseInfo, Offset,
305 PlacingEmptyBase);
Anders Carlsson58b16b62010-05-27 05:41:06 +0000306 }
Anders Carlsson812a3452010-05-27 18:20:57 +0000307
Anders Carlsson812a3452010-05-27 18:20:57 +0000308 // Traverse all member variables.
309 unsigned FieldNo = 0;
310 for (CXXRecordDecl::field_iterator I = Info->Class->field_begin(),
311 E = Info->Class->field_end(); I != E; ++I, ++FieldNo) {
David Blaikie581deb32012-06-06 20:45:41 +0000312 if (I->isBitField())
Anders Carlsson8c6acc62010-10-31 21:54:55 +0000313 continue;
Anders Carlsson4137d512010-05-29 21:10:24 +0000314
Anders Carlssona3d43802010-10-31 22:13:23 +0000315 CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo);
David Blaikie581deb32012-06-06 20:45:41 +0000316 UpdateEmptyFieldSubobjects(*I, FieldOffset);
Anders Carlsson812a3452010-05-27 18:20:57 +0000317 }
Anders Carlsson58b16b62010-05-27 05:41:06 +0000318}
319
Anders Carlsson5b1319c2010-05-29 20:49:49 +0000320bool EmptySubobjectMap::CanPlaceBaseAtOffset(const BaseSubobjectInfo *Info,
Anders Carlssona3d43802010-10-31 22:13:23 +0000321 CharUnits Offset) {
Anders Carlsson261febd2010-05-27 00:07:01 +0000322 // If we know this class doesn't have any empty subobjects we don't need to
323 // bother checking.
Anders Carlssona3d43802010-10-31 22:13:23 +0000324 if (SizeOfLargestEmptySubobject.isZero())
Anders Carlsson261febd2010-05-27 00:07:01 +0000325 return true;
326
Anders Carlsson58b16b62010-05-27 05:41:06 +0000327 if (!CanPlaceBaseSubobjectAtOffset(Info, Offset))
328 return false;
Anders Carlsson812a3452010-05-27 18:20:57 +0000329
330 // We are able to place the base at this offset. Make sure to update the
331 // empty base subobject map.
Anders Carlssone3362bc2010-06-13 18:00:18 +0000332 UpdateEmptyBaseSubobjects(Info, Offset, Info->Class->isEmpty());
Anders Carlsson261febd2010-05-27 00:07:01 +0000333 return true;
334}
335
Anders Carlsson812a3452010-05-27 18:20:57 +0000336bool
337EmptySubobjectMap::CanPlaceFieldSubobjectAtOffset(const CXXRecordDecl *RD,
338 const CXXRecordDecl *Class,
Anders Carlssona3d43802010-10-31 22:13:23 +0000339 CharUnits Offset) const {
Anders Carlsson2177ab72010-06-08 16:20:35 +0000340 // We don't have to keep looking past the maximum offset that's known to
341 // contain an empty class.
Anders Carlssona3d43802010-10-31 22:13:23 +0000342 if (!AnyEmptySubobjectsBeyondOffset(Offset))
Anders Carlsson2177ab72010-06-08 16:20:35 +0000343 return true;
344
Anders Carlssona3d43802010-10-31 22:13:23 +0000345 if (!CanPlaceSubobjectAtOffset(RD, Offset))
Anders Carlsson812a3452010-05-27 18:20:57 +0000346 return false;
347
348 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
349
350 // Traverse all non-virtual bases.
351 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
352 E = RD->bases_end(); I != E; ++I) {
353 if (I->isVirtual())
354 continue;
355
356 const CXXRecordDecl *BaseDecl =
357 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
358
Anders Carlsson6a356742010-11-01 00:21:58 +0000359 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl);
Anders Carlsson812a3452010-05-27 18:20:57 +0000360 if (!CanPlaceFieldSubobjectAtOffset(BaseDecl, Class, BaseOffset))
361 return false;
362 }
363
Anders Carlsson45f5b542010-06-08 19:09:24 +0000364 if (RD == Class) {
365 // This is the most derived class, traverse virtual bases as well.
366 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
367 E = RD->vbases_end(); I != E; ++I) {
368 const CXXRecordDecl *VBaseDecl =
369 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
370
Anders Carlsson3069a0d2010-10-31 23:45:59 +0000371 CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBaseDecl);
Anders Carlsson45f5b542010-06-08 19:09:24 +0000372 if (!CanPlaceFieldSubobjectAtOffset(VBaseDecl, Class, VBaseOffset))
373 return false;
374 }
375 }
376
Anders Carlsson812a3452010-05-27 18:20:57 +0000377 // Traverse all member variables.
378 unsigned FieldNo = 0;
379 for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
380 I != E; ++I, ++FieldNo) {
David Blaikie581deb32012-06-06 20:45:41 +0000381 if (I->isBitField())
Anders Carlsson8c6acc62010-10-31 21:54:55 +0000382 continue;
383
Anders Carlssona3d43802010-10-31 22:13:23 +0000384 CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo);
Anders Carlsson812a3452010-05-27 18:20:57 +0000385
David Blaikie581deb32012-06-06 20:45:41 +0000386 if (!CanPlaceFieldSubobjectAtOffset(*I, FieldOffset))
Anders Carlsson812a3452010-05-27 18:20:57 +0000387 return false;
388 }
389
390 return true;
391}
392
Anders Carlsson8c6acc62010-10-31 21:54:55 +0000393bool
394EmptySubobjectMap::CanPlaceFieldSubobjectAtOffset(const FieldDecl *FD,
395 CharUnits Offset) const {
Anders Carlsson2177ab72010-06-08 16:20:35 +0000396 // We don't have to keep looking past the maximum offset that's known to
397 // contain an empty class.
Anders Carlsson8c6acc62010-10-31 21:54:55 +0000398 if (!AnyEmptySubobjectsBeyondOffset(Offset))
Anders Carlsson2177ab72010-06-08 16:20:35 +0000399 return true;
400
Anders Carlsson812a3452010-05-27 18:20:57 +0000401 QualType T = FD->getType();
402 if (const RecordType *RT = T->getAs<RecordType>()) {
403 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Anders Carlssona3d43802010-10-31 22:13:23 +0000404 return CanPlaceFieldSubobjectAtOffset(RD, RD, Offset);
Anders Carlsson812a3452010-05-27 18:20:57 +0000405 }
406
407 // If we have an array type we need to look at every element.
408 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) {
409 QualType ElemTy = Context.getBaseElementType(AT);
410 const RecordType *RT = ElemTy->getAs<RecordType>();
411 if (!RT)
412 return true;
413
414 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
415 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
416
417 uint64_t NumElements = Context.getConstantArrayElementCount(AT);
Anders Carlsson8c6acc62010-10-31 21:54:55 +0000418 CharUnits ElementOffset = Offset;
Anders Carlsson812a3452010-05-27 18:20:57 +0000419 for (uint64_t I = 0; I != NumElements; ++I) {
Anders Carlsson2177ab72010-06-08 16:20:35 +0000420 // We don't have to keep looking past the maximum offset that's known to
421 // contain an empty class.
Anders Carlsson8c6acc62010-10-31 21:54:55 +0000422 if (!AnyEmptySubobjectsBeyondOffset(ElementOffset))
Anders Carlsson2177ab72010-06-08 16:20:35 +0000423 return true;
424
Anders Carlssona3d43802010-10-31 22:13:23 +0000425 if (!CanPlaceFieldSubobjectAtOffset(RD, RD, ElementOffset))
Anders Carlsson812a3452010-05-27 18:20:57 +0000426 return false;
427
Ken Dyck5f022d82011-02-09 01:59:34 +0000428 ElementOffset += Layout.getSize();
Anders Carlsson812a3452010-05-27 18:20:57 +0000429 }
430 }
431
432 return true;
433}
434
435bool
Anders Carlssona3d43802010-10-31 22:13:23 +0000436EmptySubobjectMap::CanPlaceFieldAtOffset(const FieldDecl *FD,
437 CharUnits Offset) {
438 if (!CanPlaceFieldSubobjectAtOffset(FD, Offset))
Anders Carlsson812a3452010-05-27 18:20:57 +0000439 return false;
440
441 // We are able to place the member variable at this offset.
442 // Make sure to update the empty base subobject map.
443 UpdateEmptyFieldSubobjects(FD, Offset);
444 return true;
445}
446
447void EmptySubobjectMap::UpdateEmptyFieldSubobjects(const CXXRecordDecl *RD,
448 const CXXRecordDecl *Class,
Anders Carlssona3d43802010-10-31 22:13:23 +0000449 CharUnits Offset) {
Anders Carlsson5ccfdd82010-06-13 17:49:16 +0000450 // We know that the only empty subobjects that can conflict with empty
Anders Carlssone3362bc2010-06-13 18:00:18 +0000451 // field subobjects are subobjects of empty bases that can be placed at offset
Anders Carlsson5ccfdd82010-06-13 17:49:16 +0000452 // zero. Because of this, we only need to keep track of empty field
453 // subobjects with offsets less than the size of the largest empty
454 // subobject for our class.
455 if (Offset >= SizeOfLargestEmptySubobject)
456 return;
457
Anders Carlssona3d43802010-10-31 22:13:23 +0000458 AddSubobjectAtOffset(RD, Offset);
Anders Carlsson812a3452010-05-27 18:20:57 +0000459
460 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
461
462 // Traverse all non-virtual bases.
463 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
464 E = RD->bases_end(); I != E; ++I) {
465 if (I->isVirtual())
466 continue;
467
468 const CXXRecordDecl *BaseDecl =
469 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
470
Anders Carlsson6a356742010-11-01 00:21:58 +0000471 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl);
Anders Carlsson812a3452010-05-27 18:20:57 +0000472 UpdateEmptyFieldSubobjects(BaseDecl, Class, BaseOffset);
473 }
474
Anders Carlsson45f5b542010-06-08 19:09:24 +0000475 if (RD == Class) {
476 // This is the most derived class, traverse virtual bases as well.
477 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
478 E = RD->vbases_end(); I != E; ++I) {
479 const CXXRecordDecl *VBaseDecl =
480 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
481
Anders Carlsson3069a0d2010-10-31 23:45:59 +0000482 CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBaseDecl);
Anders Carlsson45f5b542010-06-08 19:09:24 +0000483 UpdateEmptyFieldSubobjects(VBaseDecl, Class, VBaseOffset);
484 }
485 }
486
Anders Carlsson812a3452010-05-27 18:20:57 +0000487 // Traverse all member variables.
488 unsigned FieldNo = 0;
489 for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
490 I != E; ++I, ++FieldNo) {
David Blaikie581deb32012-06-06 20:45:41 +0000491 if (I->isBitField())
Anders Carlssonfa84fba2010-11-01 15:14:51 +0000492 continue;
493
Anders Carlssona3d43802010-10-31 22:13:23 +0000494 CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo);
Anders Carlsson812a3452010-05-27 18:20:57 +0000495
David Blaikie581deb32012-06-06 20:45:41 +0000496 UpdateEmptyFieldSubobjects(*I, FieldOffset);
Anders Carlsson812a3452010-05-27 18:20:57 +0000497 }
498}
499
500void EmptySubobjectMap::UpdateEmptyFieldSubobjects(const FieldDecl *FD,
Anders Carlssona3d43802010-10-31 22:13:23 +0000501 CharUnits Offset) {
Anders Carlsson812a3452010-05-27 18:20:57 +0000502 QualType T = FD->getType();
503 if (const RecordType *RT = T->getAs<RecordType>()) {
504 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
505 UpdateEmptyFieldSubobjects(RD, RD, Offset);
506 return;
507 }
508
509 // If we have an array type we need to update every element.
510 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) {
511 QualType ElemTy = Context.getBaseElementType(AT);
512 const RecordType *RT = ElemTy->getAs<RecordType>();
513 if (!RT)
514 return;
515
516 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
517 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
518
519 uint64_t NumElements = Context.getConstantArrayElementCount(AT);
Anders Carlssona3d43802010-10-31 22:13:23 +0000520 CharUnits ElementOffset = Offset;
Anders Carlsson812a3452010-05-27 18:20:57 +0000521
522 for (uint64_t I = 0; I != NumElements; ++I) {
Anders Carlsson5ccfdd82010-06-13 17:49:16 +0000523 // We know that the only empty subobjects that can conflict with empty
Anders Carlssone3362bc2010-06-13 18:00:18 +0000524 // field subobjects are subobjects of empty bases that can be placed at
Anders Carlsson5ccfdd82010-06-13 17:49:16 +0000525 // offset zero. Because of this, we only need to keep track of empty field
526 // subobjects with offsets less than the size of the largest empty
527 // subobject for our class.
528 if (ElementOffset >= SizeOfLargestEmptySubobject)
529 return;
530
Anders Carlsson812a3452010-05-27 18:20:57 +0000531 UpdateEmptyFieldSubobjects(RD, RD, ElementOffset);
Ken Dyck5f022d82011-02-09 01:59:34 +0000532 ElementOffset += Layout.getSize();
Anders Carlsson812a3452010-05-27 18:20:57 +0000533 }
534 }
535}
536
John McCall441c6232012-05-01 08:55:32 +0000537typedef llvm::SmallPtrSet<const CXXRecordDecl*, 4> ClassSetTy;
538
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000539class RecordLayoutBuilder {
Charles Davisc9f8aec2010-08-19 00:55:19 +0000540protected:
Anders Carlsson9392fa62010-05-26 05:41:04 +0000541 // FIXME: Remove this and make the appropriate fields public.
542 friend class clang::ASTContext;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000543
Jay Foad4ba2a172011-01-12 09:06:06 +0000544 const ASTContext &Context;
Anders Carlsson9392fa62010-05-26 05:41:04 +0000545
Anders Carlsson6a91c032010-05-26 15:32:58 +0000546 EmptySubobjectMap *EmptySubobjects;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000547
Anders Carlsson9392fa62010-05-26 05:41:04 +0000548 /// Size - The current size of the record layout.
549 uint64_t Size;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000550
Anders Carlsson9392fa62010-05-26 05:41:04 +0000551 /// Alignment - The current alignment of the record layout.
Ken Dyckea7f6c22011-02-16 02:05:21 +0000552 CharUnits Alignment;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000553
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +0000554 /// \brief The alignment if attribute packed is not used.
Ken Dyck6feb4bb2011-02-16 02:11:31 +0000555 CharUnits UnpackedAlignment;
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +0000556
Chris Lattner5f9e2722011-07-23 10:55:15 +0000557 SmallVector<uint64_t, 16> FieldOffsets;
Anders Carlsson9392fa62010-05-26 05:41:04 +0000558
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000559 /// \brief Whether the external AST source has provided a layout for this
560 /// record.
561 unsigned ExternalLayout : 1;
Douglas Gregor394f7b62012-01-28 00:53:29 +0000562
563 /// \brief Whether we need to infer alignment, even when we have an
564 /// externally-provided layout.
565 unsigned InferAlignment : 1;
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000566
Anders Carlsson9392fa62010-05-26 05:41:04 +0000567 /// Packed - Whether the record is packed or not.
Daniel Dunbarc6082fe2010-05-27 05:45:51 +0000568 unsigned Packed : 1;
569
570 unsigned IsUnion : 1;
571
572 unsigned IsMac68kAlign : 1;
Fariborz Jahanian62055b02011-04-26 23:52:16 +0000573
574 unsigned IsMsStruct : 1;
Anders Carlsson9392fa62010-05-26 05:41:04 +0000575
576 /// UnfilledBitsInLastByte - If the last field laid out was a bitfield,
577 /// this contains the number of bits in the last byte that can be used for
578 /// an adjacent bitfield if necessary.
579 unsigned char UnfilledBitsInLastByte;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000580
Anders Carlsson9392fa62010-05-26 05:41:04 +0000581 /// MaxFieldAlignment - The maximum allowed field alignment. This is set by
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000582 /// #pragma pack.
Ken Dyck834945c2011-02-17 01:49:42 +0000583 CharUnits MaxFieldAlignment;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000584
Anders Carlsson9392fa62010-05-26 05:41:04 +0000585 /// DataSize - The data size of the record being laid out.
586 uint64_t DataSize;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000587
Ken Dycka1fdb0b2011-02-16 01:52:01 +0000588 CharUnits NonVirtualSize;
Ken Dyckdf205382011-02-16 01:43:15 +0000589 CharUnits NonVirtualAlignment;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000590
Fariborz Jahanian855a8e72011-05-03 20:21:04 +0000591 FieldDecl *ZeroLengthBitfield;
Fariborz Jahanian340fa242011-05-02 17:20:56 +0000592
Anders Carlsson9392fa62010-05-26 05:41:04 +0000593 /// PrimaryBase - the primary base class (if one exists) of the class
594 /// we're laying out.
595 const CXXRecordDecl *PrimaryBase;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000596
Anders Carlsson9392fa62010-05-26 05:41:04 +0000597 /// PrimaryBaseIsVirtual - Whether the primary base of the class we're laying
598 /// out is virtual.
599 bool PrimaryBaseIsVirtual;
600
John McCall441c6232012-05-01 08:55:32 +0000601 /// HasOwnVFPtr - Whether the class provides its own vtable/vftbl
602 /// pointer, as opposed to inheriting one from a primary base class.
603 bool HasOwnVFPtr;
Eli Friedman227e4832011-10-21 22:49:56 +0000604
Eli Friedman2fe36362011-09-27 19:12:27 +0000605 /// VBPtrOffset - Virtual base table offset. Only for MS layout.
606 CharUnits VBPtrOffset;
607
Anders Carlsson376bda92010-10-31 21:01:46 +0000608 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetsMapTy;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000609
Anders Carlsson9392fa62010-05-26 05:41:04 +0000610 /// Bases - base classes and their offsets in the record.
611 BaseOffsetsMapTy Bases;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000612
Anders Carlsson9392fa62010-05-26 05:41:04 +0000613 // VBases - virtual base classes and their offsets in the record.
John McCall441c6232012-05-01 08:55:32 +0000614 ASTRecordLayout::VBaseOffsetsMapTy VBases;
Anders Carlsson9392fa62010-05-26 05:41:04 +0000615
616 /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are
617 /// primary base classes for some other direct or indirect base class.
Anders Carlsson245656e2010-11-24 22:55:48 +0000618 CXXIndirectPrimaryBaseSet IndirectPrimaryBases;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000619
Anders Carlsson9392fa62010-05-26 05:41:04 +0000620 /// FirstNearlyEmptyVBase - The first nearly empty virtual base class in
621 /// inheritance graph order. Used for determining the primary base class.
622 const CXXRecordDecl *FirstNearlyEmptyVBase;
623
624 /// VisitedVirtualBases - A set of all the visited virtual bases, used to
625 /// avoid visiting virtual bases more than once.
626 llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000627
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000628 /// \brief Externally-provided size.
629 uint64_t ExternalSize;
630
631 /// \brief Externally-provided alignment.
632 uint64_t ExternalAlign;
633
634 /// \brief Externally-provided field offsets.
635 llvm::DenseMap<const FieldDecl *, uint64_t> ExternalFieldOffsets;
636
637 /// \brief Externally-provided direct, non-virtual base offsets.
638 llvm::DenseMap<const CXXRecordDecl *, CharUnits> ExternalBaseOffsets;
639
640 /// \brief Externally-provided virtual base offsets.
641 llvm::DenseMap<const CXXRecordDecl *, CharUnits> ExternalVirtualBaseOffsets;
642
John McCall9da23522011-11-08 04:01:03 +0000643 RecordLayoutBuilder(const ASTContext &Context,
644 EmptySubobjectMap *EmptySubobjects)
Ken Dyckea7f6c22011-02-16 02:05:21 +0000645 : Context(Context), EmptySubobjects(EmptySubobjects), Size(0),
John McCall9da23522011-11-08 04:01:03 +0000646 Alignment(CharUnits::One()), UnpackedAlignment(CharUnits::One()),
Douglas Gregor394f7b62012-01-28 00:53:29 +0000647 ExternalLayout(false), InferAlignment(false),
648 Packed(false), IsUnion(false), IsMac68kAlign(false), IsMsStruct(false),
Ken Dyck834945c2011-02-17 01:49:42 +0000649 UnfilledBitsInLastByte(0), MaxFieldAlignment(CharUnits::Zero()),
650 DataSize(0), NonVirtualSize(CharUnits::Zero()),
Fariborz Jahanian340fa242011-05-02 17:20:56 +0000651 NonVirtualAlignment(CharUnits::One()),
Fariborz Jahanian855a8e72011-05-03 20:21:04 +0000652 ZeroLengthBitfield(0), PrimaryBase(0),
Eli Friedman227e4832011-10-21 22:49:56 +0000653 PrimaryBaseIsVirtual(false),
John McCall441c6232012-05-01 08:55:32 +0000654 HasOwnVFPtr(false),
Eli Friedman227e4832011-10-21 22:49:56 +0000655 VBPtrOffset(CharUnits::fromQuantity(-1)),
Eli Friedman2fe36362011-09-27 19:12:27 +0000656 FirstNearlyEmptyVBase(0) { }
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000657
John McCall9da23522011-11-08 04:01:03 +0000658 /// Reset this RecordLayoutBuilder to a fresh state, using the given
659 /// alignment as the initial alignment. This is used for the
660 /// correct layout of vb-table pointers in MSVC.
661 void resetWithTargetAlignment(CharUnits TargetAlignment) {
662 const ASTContext &Context = this->Context;
663 EmptySubobjectMap *EmptySubobjects = this->EmptySubobjects;
664 this->~RecordLayoutBuilder();
665 new (this) RecordLayoutBuilder(Context, EmptySubobjects);
666 Alignment = UnpackedAlignment = TargetAlignment;
667 }
668
Anders Carlsson9392fa62010-05-26 05:41:04 +0000669 void Layout(const RecordDecl *D);
Anders Carlssonc6cab682010-05-26 15:10:00 +0000670 void Layout(const CXXRecordDecl *D);
Anders Carlsson9392fa62010-05-26 05:41:04 +0000671 void Layout(const ObjCInterfaceDecl *D);
672
673 void LayoutFields(const RecordDecl *D);
674 void LayoutField(const FieldDecl *D);
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +0000675 void LayoutWideBitField(uint64_t FieldSize, uint64_t TypeSize,
676 bool FieldPacked, const FieldDecl *D);
Anders Carlsson9392fa62010-05-26 05:41:04 +0000677 void LayoutBitField(const FieldDecl *D);
John McCall9da23522011-11-08 04:01:03 +0000678
John McCallb8b2c9d2013-01-25 22:30:49 +0000679 TargetCXXABI getCXXABI() const {
680 return Context.getTargetInfo().getCXXABI();
681 }
682
John McCall9da23522011-11-08 04:01:03 +0000683 bool isMicrosoftCXXABI() const {
John McCallb8b2c9d2013-01-25 22:30:49 +0000684 return getCXXABI().isMicrosoft();
John McCall9da23522011-11-08 04:01:03 +0000685 }
686
Eli Friedman2fe36362011-09-27 19:12:27 +0000687 void MSLayoutVirtualBases(const CXXRecordDecl *RD);
Anders Carlsson9392fa62010-05-26 05:41:04 +0000688
Anders Carlsson6e264542010-05-29 17:35:14 +0000689 /// BaseSubobjectInfoAllocator - Allocator for BaseSubobjectInfo objects.
690 llvm::SpecificBumpPtrAllocator<BaseSubobjectInfo> BaseSubobjectInfoAllocator;
691
692 typedef llvm::DenseMap<const CXXRecordDecl *, BaseSubobjectInfo *>
693 BaseSubobjectInfoMapTy;
694
695 /// VirtualBaseInfo - Map from all the (direct or indirect) virtual bases
696 /// of the class we're laying out to their base subobject info.
697 BaseSubobjectInfoMapTy VirtualBaseInfo;
698
699 /// NonVirtualBaseInfo - Map from all the direct non-virtual bases of the
700 /// class we're laying out to their base subobject info.
701 BaseSubobjectInfoMapTy NonVirtualBaseInfo;
702
703 /// ComputeBaseSubobjectInfo - Compute the base subobject information for the
704 /// bases of the given class.
705 void ComputeBaseSubobjectInfo(const CXXRecordDecl *RD);
706
707 /// ComputeBaseSubobjectInfo - Compute the base subobject information for a
708 /// single class and all of its base classes.
709 BaseSubobjectInfo *ComputeBaseSubobjectInfo(const CXXRecordDecl *RD,
710 bool IsVirtual,
711 BaseSubobjectInfo *Derived);
Anders Carlsson9392fa62010-05-26 05:41:04 +0000712
713 /// DeterminePrimaryBase - Determine the primary base of the given class.
714 void DeterminePrimaryBase(const CXXRecordDecl *RD);
715
716 void SelectPrimaryVBase(const CXXRecordDecl *RD);
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000717
Eli Friedman227e4832011-10-21 22:49:56 +0000718 void EnsureVTablePointerAlignment(CharUnits UnpackedBaseAlign);
Charles Davisc9f8aec2010-08-19 00:55:19 +0000719
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000720 /// LayoutNonVirtualBases - Determines the primary base class (if any) and
Anders Carlsson9392fa62010-05-26 05:41:04 +0000721 /// lays it out. Will then proceed to lay out all non-virtual base clasess.
722 void LayoutNonVirtualBases(const CXXRecordDecl *RD);
723
724 /// LayoutNonVirtualBase - Lays out a single non-virtual base.
Anders Carlsson07cebc52010-05-29 17:42:25 +0000725 void LayoutNonVirtualBase(const BaseSubobjectInfo *Base);
Anders Carlsson9392fa62010-05-26 05:41:04 +0000726
Anders Carlssona2311512010-10-31 22:20:42 +0000727 void AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info,
728 CharUnits Offset);
Anders Carlsson9392fa62010-05-26 05:41:04 +0000729
John McCall9da23522011-11-08 04:01:03 +0000730 bool needsVFTable(const CXXRecordDecl *RD) const;
John McCall441c6232012-05-01 08:55:32 +0000731 bool hasNewVirtualFunction(const CXXRecordDecl *RD,
732 bool IgnoreDestructor = false) const;
John McCall9da23522011-11-08 04:01:03 +0000733 bool isPossiblePrimaryBase(const CXXRecordDecl *Base) const;
Eli Friedman97c0aef2011-10-18 00:55:28 +0000734
John McCall441c6232012-05-01 08:55:32 +0000735 void computeVtordisps(const CXXRecordDecl *RD,
736 ClassSetTy &VtordispVBases);
737
Anders Carlsson9392fa62010-05-26 05:41:04 +0000738 /// LayoutVirtualBases - Lays out all the virtual bases.
739 void LayoutVirtualBases(const CXXRecordDecl *RD,
740 const CXXRecordDecl *MostDerivedClass);
741
742 /// LayoutVirtualBase - Lays out a single virtual base.
John McCall441c6232012-05-01 08:55:32 +0000743 void LayoutVirtualBase(const BaseSubobjectInfo *Base,
744 bool IsVtordispNeed = false);
Anders Carlsson9392fa62010-05-26 05:41:04 +0000745
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +0000746 /// LayoutBase - Will lay out a base and return the offset where it was
Anders Carlssona2311512010-10-31 22:20:42 +0000747 /// placed, in chars.
748 CharUnits LayoutBase(const BaseSubobjectInfo *Base);
Anders Carlsson9392fa62010-05-26 05:41:04 +0000749
Anders Carlssonc6cab682010-05-26 15:10:00 +0000750 /// InitializeLayout - Initialize record layout for the given record decl.
Daniel Dunbarc6082fe2010-05-27 05:45:51 +0000751 void InitializeLayout(const Decl *D);
Anders Carlssonc6cab682010-05-26 15:10:00 +0000752
Anders Carlsson9392fa62010-05-26 05:41:04 +0000753 /// FinishLayout - Finalize record layout. Adjust record size based on the
754 /// alignment.
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +0000755 void FinishLayout(const NamedDecl *D);
Anders Carlsson9392fa62010-05-26 05:41:04 +0000756
Ken Dyck3263e092011-02-19 18:58:07 +0000757 void UpdateAlignment(CharUnits NewAlignment, CharUnits UnpackedNewAlignment);
758 void UpdateAlignment(CharUnits NewAlignment) {
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +0000759 UpdateAlignment(NewAlignment, NewAlignment);
760 }
761
Douglas Gregor394f7b62012-01-28 00:53:29 +0000762 /// \brief Retrieve the externally-supplied field offset for the given
763 /// field.
764 ///
765 /// \param Field The field whose offset is being queried.
766 /// \param ComputedOffset The offset that we've computed for this field.
767 uint64_t updateExternalFieldOffset(const FieldDecl *Field,
768 uint64_t ComputedOffset);
769
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +0000770 void CheckFieldPadding(uint64_t Offset, uint64_t UnpaddedOffset,
771 uint64_t UnpackedOffset, unsigned UnpackedAlign,
772 bool isPacked, const FieldDecl *D);
773
774 DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID);
Anders Carlsson9392fa62010-05-26 05:41:04 +0000775
Ken Dycka0c21c42011-02-24 01:13:28 +0000776 CharUnits getSize() const {
Ken Dyck99113442011-02-24 01:33:05 +0000777 assert(Size % Context.getCharWidth() == 0);
Ken Dycka0c21c42011-02-24 01:13:28 +0000778 return Context.toCharUnitsFromBits(Size);
779 }
780 uint64_t getSizeInBits() const { return Size; }
781
782 void setSize(CharUnits NewSize) { Size = Context.toBits(NewSize); }
783 void setSize(uint64_t NewSize) { Size = NewSize; }
784
Eli Friedman2fe36362011-09-27 19:12:27 +0000785 CharUnits getAligment() const { return Alignment; }
786
Ken Dycka0c21c42011-02-24 01:13:28 +0000787 CharUnits getDataSize() const {
Ken Dyck99113442011-02-24 01:33:05 +0000788 assert(DataSize % Context.getCharWidth() == 0);
Ken Dycka0c21c42011-02-24 01:13:28 +0000789 return Context.toCharUnitsFromBits(DataSize);
790 }
791 uint64_t getDataSizeInBits() const { return DataSize; }
792
793 void setDataSize(CharUnits NewSize) { DataSize = Context.toBits(NewSize); }
794 void setDataSize(uint64_t NewSize) { DataSize = NewSize; }
795
Dmitri Gribenkof56faa02012-09-15 20:20:27 +0000796 RecordLayoutBuilder(const RecordLayoutBuilder &) LLVM_DELETED_FUNCTION;
797 void operator=(const RecordLayoutBuilder &) LLVM_DELETED_FUNCTION;
Anders Carlsson9392fa62010-05-26 05:41:04 +0000798};
Benjamin Kramer7e220282010-05-26 09:58:31 +0000799} // end anonymous namespace
Anders Carlsson9392fa62010-05-26 05:41:04 +0000800
Anders Carlsson3f066522009-09-22 03:02:06 +0000801void
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000802RecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD) {
Anders Carlsson584e1df2010-03-11 03:39:12 +0000803 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000804 E = RD->bases_end(); I != E; ++I) {
Anders Carlsson584e1df2010-03-11 03:39:12 +0000805 assert(!I->getType()->isDependentType() &&
Sebastian Redl9994a342009-10-25 17:03:50 +0000806 "Cannot layout class with dependent bases.");
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000807
Mike Stump1eb44332009-09-09 15:08:12 +0000808 const CXXRecordDecl *Base =
Anders Carlsson584e1df2010-03-11 03:39:12 +0000809 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
Anders Carlsson200c5c22010-03-11 00:15:35 +0000810
Anders Carlsson584e1df2010-03-11 03:39:12 +0000811 // Check if this is a nearly empty virtual base.
Anders Carlssondae0cb52010-11-25 01:51:53 +0000812 if (I->isVirtual() && Context.isNearlyEmpty(Base)) {
Anders Carlsson584e1df2010-03-11 03:39:12 +0000813 // If it's not an indirect primary base, then we've found our primary
814 // base.
Anders Carlsson3f066522009-09-22 03:02:06 +0000815 if (!IndirectPrimaryBases.count(Base)) {
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000816 PrimaryBase = Base;
817 PrimaryBaseIsVirtual = true;
Mike Stumpd76264e2009-08-12 21:50:08 +0000818 return;
819 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000820
Anders Carlsson584e1df2010-03-11 03:39:12 +0000821 // Is this the first nearly empty virtual base?
822 if (!FirstNearlyEmptyVBase)
823 FirstNearlyEmptyVBase = Base;
Mike Stumpd76264e2009-08-12 21:50:08 +0000824 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000825
Anders Carlsson200c5c22010-03-11 00:15:35 +0000826 SelectPrimaryVBase(Base);
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000827 if (PrimaryBase)
Zhongxing Xu94ba3802010-02-15 04:28:35 +0000828 return;
Mike Stumpd76264e2009-08-12 21:50:08 +0000829 }
830}
831
Anders Carlsson200c5c22010-03-11 00:15:35 +0000832/// DeterminePrimaryBase - Determine the primary base of the given class.
Anders Carlsson7d0918a2010-05-26 05:58:59 +0000833void RecordLayoutBuilder::DeterminePrimaryBase(const CXXRecordDecl *RD) {
Anders Carlsson200c5c22010-03-11 00:15:35 +0000834 // If the class isn't dynamic, it won't have a primary base.
835 if (!RD->isDynamicClass())
836 return;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000837
Anders Carlsson3f066522009-09-22 03:02:06 +0000838 // Compute all the primary virtual bases for all of our direct and
Mike Stump0880e752009-08-13 23:26:06 +0000839 // indirect bases, and record all their primary virtual base classes.
Anders Carlsson245656e2010-11-24 22:55:48 +0000840 RD->getIndirectPrimaryBases(IndirectPrimaryBases);
Mike Stump0880e752009-08-13 23:26:06 +0000841
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000842 // If the record has a dynamic base class, attempt to choose a primary base
843 // class. It is the first (in direct base class order) non-virtual dynamic
Anders Carlsson3f066522009-09-22 03:02:06 +0000844 // base class, if one exists.
Mike Stump6f376332009-08-05 22:37:18 +0000845 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000846 e = RD->bases_end(); i != e; ++i) {
Anders Carlssonce2009a2009-11-27 22:05:05 +0000847 // Ignore virtual bases.
848 if (i->isVirtual())
849 continue;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000850
Anders Carlssonce2009a2009-11-27 22:05:05 +0000851 const CXXRecordDecl *Base =
852 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
853
John McCall9da23522011-11-08 04:01:03 +0000854 if (isPossiblePrimaryBase(Base)) {
Anders Carlssonce2009a2009-11-27 22:05:05 +0000855 // We found it.
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000856 PrimaryBase = Base;
857 PrimaryBaseIsVirtual = false;
Anders Carlssonce2009a2009-11-27 22:05:05 +0000858 return;
Mike Stump6f376332009-08-05 22:37:18 +0000859 }
860 }
861
Eli Friedman97c0aef2011-10-18 00:55:28 +0000862 // The Microsoft ABI doesn't have primary virtual bases.
John McCall9da23522011-11-08 04:01:03 +0000863 if (isMicrosoftCXXABI()) {
Eli Friedman97c0aef2011-10-18 00:55:28 +0000864 assert(!PrimaryBase && "Should not get here with a primary base!");
865 return;
866 }
867
868 // Under the Itanium ABI, if there is no non-virtual primary base class,
869 // try to compute the primary virtual base. The primary virtual base is
870 // the first nearly empty virtual base that is not an indirect primary
871 // virtual base class, if one exists.
Anders Carlsson200c5c22010-03-11 00:15:35 +0000872 if (RD->getNumVBases() != 0) {
873 SelectPrimaryVBase(RD);
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000874 if (PrimaryBase)
Anders Carlsson200c5c22010-03-11 00:15:35 +0000875 return;
876 }
Mike Stump6f376332009-08-05 22:37:18 +0000877
Eli Friedman97c0aef2011-10-18 00:55:28 +0000878 // Otherwise, it is the first indirect primary base class, if one exists.
Anders Carlsson200c5c22010-03-11 00:15:35 +0000879 if (FirstNearlyEmptyVBase) {
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000880 PrimaryBase = FirstNearlyEmptyVBase;
881 PrimaryBaseIsVirtual = true;
Mike Stump6f376332009-08-05 22:37:18 +0000882 return;
Anders Carlsson200c5c22010-03-11 00:15:35 +0000883 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +0000884
Anders Carlsson28fdd0a2010-05-26 05:20:58 +0000885 assert(!PrimaryBase && "Should not get here with a primary base!");
Mike Stump6f376332009-08-05 22:37:18 +0000886}
887
Anders Carlsson6e264542010-05-29 17:35:14 +0000888BaseSubobjectInfo *
889RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD,
890 bool IsVirtual,
891 BaseSubobjectInfo *Derived) {
892 BaseSubobjectInfo *Info;
893
894 if (IsVirtual) {
895 // Check if we already have info about this virtual base.
896 BaseSubobjectInfo *&InfoSlot = VirtualBaseInfo[RD];
897 if (InfoSlot) {
898 assert(InfoSlot->Class == RD && "Wrong class for virtual base info!");
899 return InfoSlot;
900 }
901
902 // We don't, create it.
903 InfoSlot = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo;
904 Info = InfoSlot;
905 } else {
906 Info = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo;
907 }
908
909 Info->Class = RD;
910 Info->IsVirtual = IsVirtual;
911 Info->Derived = 0;
912 Info->PrimaryVirtualBaseInfo = 0;
913
914 const CXXRecordDecl *PrimaryVirtualBase = 0;
915 BaseSubobjectInfo *PrimaryVirtualBaseInfo = 0;
916
917 // Check if this base has a primary virtual base.
918 if (RD->getNumVBases()) {
919 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlssonc9e814b2010-11-24 23:12:57 +0000920 if (Layout.isPrimaryBaseVirtual()) {
Anders Carlsson6e264542010-05-29 17:35:14 +0000921 // This base does have a primary virtual base.
922 PrimaryVirtualBase = Layout.getPrimaryBase();
923 assert(PrimaryVirtualBase && "Didn't have a primary virtual base!");
924
925 // Now check if we have base subobject info about this primary base.
926 PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase);
927
928 if (PrimaryVirtualBaseInfo) {
929 if (PrimaryVirtualBaseInfo->Derived) {
930 // We did have info about this primary base, and it turns out that it
931 // has already been claimed as a primary virtual base for another
932 // base.
933 PrimaryVirtualBase = 0;
934 } else {
935 // We can claim this base as our primary base.
936 Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo;
937 PrimaryVirtualBaseInfo->Derived = Info;
938 }
939 }
940 }
941 }
942
943 // Now go through all direct bases.
944 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
945 E = RD->bases_end(); I != E; ++I) {
946 bool IsVirtual = I->isVirtual();
947
948 const CXXRecordDecl *BaseDecl =
949 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
950
951 Info->Bases.push_back(ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, Info));
952 }
953
954 if (PrimaryVirtualBase && !PrimaryVirtualBaseInfo) {
955 // Traversing the bases must have created the base info for our primary
956 // virtual base.
957 PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase);
958 assert(PrimaryVirtualBaseInfo &&
959 "Did not create a primary virtual base!");
960
961 // Claim the primary virtual base as our primary virtual base.
962 Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo;
963 PrimaryVirtualBaseInfo->Derived = Info;
964 }
965
966 return Info;
967}
968
969void RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD) {
970 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
971 E = RD->bases_end(); I != E; ++I) {
972 bool IsVirtual = I->isVirtual();
973
974 const CXXRecordDecl *BaseDecl =
975 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
976
977 // Compute the base subobject info for this base.
978 BaseSubobjectInfo *Info = ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, 0);
979
980 if (IsVirtual) {
981 // ComputeBaseInfo has already added this base for us.
982 assert(VirtualBaseInfo.count(BaseDecl) &&
983 "Did not add virtual base!");
984 } else {
985 // Add the base info to the map of non-virtual bases.
986 assert(!NonVirtualBaseInfo.count(BaseDecl) &&
987 "Non-virtual base already exists!");
988 NonVirtualBaseInfo.insert(std::make_pair(BaseDecl, Info));
989 }
990 }
991}
992
Anders Carlssone239b9d2010-03-10 22:21:28 +0000993void
Eli Friedman227e4832011-10-21 22:49:56 +0000994RecordLayoutBuilder::EnsureVTablePointerAlignment(CharUnits UnpackedBaseAlign) {
Eli Friedman97c0aef2011-10-18 00:55:28 +0000995 CharUnits BaseAlign = (Packed) ? CharUnits::One() : UnpackedBaseAlign;
996
997 // The maximum field alignment overrides base align.
998 if (!MaxFieldAlignment.isZero()) {
999 BaseAlign = std::min(BaseAlign, MaxFieldAlignment);
1000 UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment);
1001 }
1002
1003 // Round up the current record size to pointer alignment.
Eli Friedman227e4832011-10-21 22:49:56 +00001004 setSize(getSize().RoundUpToAlignment(BaseAlign));
1005 setDataSize(getSize());
Eli Friedman97c0aef2011-10-18 00:55:28 +00001006
1007 // Update the alignment.
1008 UpdateAlignment(BaseAlign, UnpackedBaseAlign);
1009}
1010
1011void
Anders Carlsson7d0918a2010-05-26 05:58:59 +00001012RecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) {
Anders Carlsson6e264542010-05-29 17:35:14 +00001013 // Then, determine the primary base class.
Anders Carlsson200c5c22010-03-11 00:15:35 +00001014 DeterminePrimaryBase(RD);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001015
Anders Carlsson6e264542010-05-29 17:35:14 +00001016 // Compute base subobject info.
1017 ComputeBaseSubobjectInfo(RD);
1018
Anders Carlsson200c5c22010-03-11 00:15:35 +00001019 // If we have a primary base class, lay it out.
Anders Carlsson28fdd0a2010-05-26 05:20:58 +00001020 if (PrimaryBase) {
1021 if (PrimaryBaseIsVirtual) {
Anders Carlsson6e264542010-05-29 17:35:14 +00001022 // If the primary virtual base was a primary virtual base of some other
1023 // base class we'll have to steal it.
1024 BaseSubobjectInfo *PrimaryBaseInfo = VirtualBaseInfo.lookup(PrimaryBase);
1025 PrimaryBaseInfo->Derived = 0;
1026
Anders Carlsson200c5c22010-03-11 00:15:35 +00001027 // We have a virtual primary base, insert it as an indirect primary base.
Anders Carlsson28fdd0a2010-05-26 05:20:58 +00001028 IndirectPrimaryBases.insert(PrimaryBase);
Anders Carlsson37147ea2010-03-11 05:42:17 +00001029
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001030 assert(!VisitedVirtualBases.count(PrimaryBase) &&
Anders Carlsson28fdd0a2010-05-26 05:20:58 +00001031 "vbase already visited!");
1032 VisitedVirtualBases.insert(PrimaryBase);
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001033
Anders Carlsson276b4912010-05-29 17:48:36 +00001034 LayoutVirtualBase(PrimaryBaseInfo);
Anders Carlsson07cebc52010-05-29 17:42:25 +00001035 } else {
1036 BaseSubobjectInfo *PrimaryBaseInfo =
1037 NonVirtualBaseInfo.lookup(PrimaryBase);
1038 assert(PrimaryBaseInfo &&
1039 "Did not find base info for non-virtual primary base!");
1040
1041 LayoutNonVirtualBase(PrimaryBaseInfo);
1042 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001043
John McCall9da23522011-11-08 04:01:03 +00001044 // If this class needs a vtable/vf-table and didn't get one from a
1045 // primary base, add it in now.
1046 } else if (needsVFTable(RD)) {
Eli Friedman97c0aef2011-10-18 00:55:28 +00001047 assert(DataSize == 0 && "Vtable pointer must be at offset zero!");
Eli Friedman97c0aef2011-10-18 00:55:28 +00001048 CharUnits PtrWidth =
1049 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
Eli Friedman227e4832011-10-21 22:49:56 +00001050 CharUnits PtrAlign =
1051 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(0));
1052 EnsureVTablePointerAlignment(PtrAlign);
John McCall441c6232012-05-01 08:55:32 +00001053 HasOwnVFPtr = true;
Eli Friedman97c0aef2011-10-18 00:55:28 +00001054 setSize(getSize() + PtrWidth);
1055 setDataSize(getSize());
1056 }
1057
John McCall9da23522011-11-08 04:01:03 +00001058 bool HasDirectVirtualBases = false;
1059 bool HasNonVirtualBaseWithVBTable = false;
1060
Anders Carlsson200c5c22010-03-11 00:15:35 +00001061 // Now lay out the non-virtual bases.
1062 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001063 E = RD->bases_end(); I != E; ++I) {
Anders Carlsson200c5c22010-03-11 00:15:35 +00001064
John McCall9da23522011-11-08 04:01:03 +00001065 // Ignore virtual bases, but remember that we saw one.
1066 if (I->isVirtual()) {
1067 HasDirectVirtualBases = true;
Anders Carlsson200c5c22010-03-11 00:15:35 +00001068 continue;
John McCall9da23522011-11-08 04:01:03 +00001069 }
Anders Carlsson200c5c22010-03-11 00:15:35 +00001070
Anders Carlsson07cebc52010-05-29 17:42:25 +00001071 const CXXRecordDecl *BaseDecl =
John McCall9da23522011-11-08 04:01:03 +00001072 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
Anders Carlsson200c5c22010-03-11 00:15:35 +00001073
John McCall9da23522011-11-08 04:01:03 +00001074 // Remember if this base has virtual bases itself.
1075 if (BaseDecl->getNumVBases())
1076 HasNonVirtualBaseWithVBTable = true;
1077
1078 // Skip the primary base, because we've already laid it out. The
1079 // !PrimaryBaseIsVirtual check is required because we might have a
1080 // non-virtual base of the same type as a primary virtual base.
Anders Carlsson07cebc52010-05-29 17:42:25 +00001081 if (BaseDecl == PrimaryBase && !PrimaryBaseIsVirtual)
Anders Carlsson200c5c22010-03-11 00:15:35 +00001082 continue;
1083
1084 // Lay out the base.
Anders Carlsson07cebc52010-05-29 17:42:25 +00001085 BaseSubobjectInfo *BaseInfo = NonVirtualBaseInfo.lookup(BaseDecl);
1086 assert(BaseInfo && "Did not find base info for non-virtual base!");
1087
1088 LayoutNonVirtualBase(BaseInfo);
Anders Carlssone239b9d2010-03-10 22:21:28 +00001089 }
Eli Friedman97c0aef2011-10-18 00:55:28 +00001090
John McCall9da23522011-11-08 04:01:03 +00001091 // In the MS ABI, add the vb-table pointer if we need one, which is
1092 // whenever we have a virtual base and we can't re-use a vb-table
1093 // pointer from a non-virtual base.
1094 if (isMicrosoftCXXABI() &&
1095 HasDirectVirtualBases && !HasNonVirtualBaseWithVBTable) {
Eli Friedman97c0aef2011-10-18 00:55:28 +00001096 CharUnits PtrWidth =
1097 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
Eli Friedman227e4832011-10-21 22:49:56 +00001098 CharUnits PtrAlign =
1099 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(0));
John McCall9da23522011-11-08 04:01:03 +00001100
1101 // MSVC potentially over-aligns the vb-table pointer by giving it
1102 // the max alignment of all the non-virtual objects in the class.
1103 // This is completely unnecessary, but we're not here to pass
1104 // judgment.
1105 //
1106 // Note that we've only laid out the non-virtual bases, so on the
1107 // first pass Alignment won't be set correctly here, but if the
1108 // vb-table doesn't end up aligned correctly we'll come through
1109 // and redo the layout from scratch with the right alignment.
1110 //
1111 // TODO: Instead of doing this, just lay out the fields as if the
1112 // vb-table were at offset zero, then retroactively bump the field
1113 // offsets up.
Eli Friedman227e4832011-10-21 22:49:56 +00001114 PtrAlign = std::max(PtrAlign, Alignment);
John McCall9da23522011-11-08 04:01:03 +00001115
1116 EnsureVTablePointerAlignment(PtrAlign);
1117 VBPtrOffset = getSize();
1118 setSize(getSize() + PtrWidth);
1119 setDataSize(getSize());
Eli Friedman97c0aef2011-10-18 00:55:28 +00001120 }
Anders Carlssone239b9d2010-03-10 22:21:28 +00001121}
1122
Anders Carlsson07cebc52010-05-29 17:42:25 +00001123void RecordLayoutBuilder::LayoutNonVirtualBase(const BaseSubobjectInfo *Base) {
Anders Carlssone3bdbee2010-03-10 22:26:24 +00001124 // Layout the base.
Anders Carlssona2311512010-10-31 22:20:42 +00001125 CharUnits Offset = LayoutBase(Base);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001126
Anders Carlssone3bdbee2010-03-10 22:26:24 +00001127 // Add its base class offset.
Anders Carlsson07cebc52010-05-29 17:42:25 +00001128 assert(!Bases.count(Base->Class) && "base offset already exists!");
Anders Carlssona2311512010-10-31 22:20:42 +00001129 Bases.insert(std::make_pair(Base->Class, Offset));
Anders Carlsson3cd09cc2010-05-29 19:44:50 +00001130
1131 AddPrimaryVirtualBaseOffsets(Base, Offset);
Anders Carlssone239b9d2010-03-10 22:21:28 +00001132}
Mike Stump968db332009-11-05 04:02:15 +00001133
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001134void
Anders Carlsson3cd09cc2010-05-29 19:44:50 +00001135RecordLayoutBuilder::AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info,
Anders Carlssona2311512010-10-31 22:20:42 +00001136 CharUnits Offset) {
Anders Carlsson3cd09cc2010-05-29 19:44:50 +00001137 // This base isn't interesting, it has no virtual bases.
1138 if (!Info->Class->getNumVBases())
1139 return;
1140
1141 // First, check if we have a virtual primary base to add offsets for.
1142 if (Info->PrimaryVirtualBaseInfo) {
1143 assert(Info->PrimaryVirtualBaseInfo->IsVirtual &&
1144 "Primary virtual base is not virtual!");
1145 if (Info->PrimaryVirtualBaseInfo->Derived == Info) {
1146 // Add the offset.
1147 assert(!VBases.count(Info->PrimaryVirtualBaseInfo->Class) &&
1148 "primary vbase offset already exists!");
1149 VBases.insert(std::make_pair(Info->PrimaryVirtualBaseInfo->Class,
John McCall441c6232012-05-01 08:55:32 +00001150 ASTRecordLayout::VBaseInfo(Offset, false)));
Anders Carlsson97913572010-04-15 16:12:58 +00001151
Anders Carlsson3cd09cc2010-05-29 19:44:50 +00001152 // Traverse the primary virtual base.
1153 AddPrimaryVirtualBaseOffsets(Info->PrimaryVirtualBaseInfo, Offset);
1154 }
Anders Carlsson97913572010-04-15 16:12:58 +00001155 }
1156
Anders Carlsson3cd09cc2010-05-29 19:44:50 +00001157 // Now go through all direct non-virtual bases.
1158 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class);
1159 for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) {
1160 const BaseSubobjectInfo *Base = Info->Bases[I];
1161 if (Base->IsVirtual)
Anders Carlsson97913572010-04-15 16:12:58 +00001162 continue;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001163
Anders Carlsson6a356742010-11-01 00:21:58 +00001164 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class);
Anders Carlsson3cd09cc2010-05-29 19:44:50 +00001165 AddPrimaryVirtualBaseOffsets(Base, BaseOffset);
Anders Carlsson97913572010-04-15 16:12:58 +00001166 }
1167}
1168
John McCall9da23522011-11-08 04:01:03 +00001169/// needsVFTable - Return true if this class needs a vtable or vf-table
1170/// when laid out as a base class. These are treated the same because
1171/// they're both always laid out at offset zero.
1172///
1173/// This function assumes that the class has no primary base.
1174bool RecordLayoutBuilder::needsVFTable(const CXXRecordDecl *RD) const {
1175 assert(!PrimaryBase);
1176
1177 // In the Itanium ABI, every dynamic class needs a vtable: even if
1178 // this class has no virtual functions as a base class (i.e. it's
1179 // non-polymorphic or only has virtual functions from virtual
1180 // bases),x it still needs a vtable to locate its virtual bases.
1181 if (!isMicrosoftCXXABI())
1182 return RD->isDynamicClass();
1183
1184 // In the MS ABI, we need a vfptr if the class has virtual functions
1185 // other than those declared by its virtual bases. The AST doesn't
1186 // tell us that directly, and checking manually for virtual
1187 // functions that aren't overrides is expensive, but there are
1188 // some important shortcuts:
1189
1190 // - Non-polymorphic classes have no virtual functions at all.
1191 if (!RD->isPolymorphic()) return false;
1192
1193 // - Polymorphic classes with no virtual bases must either declare
1194 // virtual functions directly or inherit them, but in the latter
1195 // case we would have a primary base.
1196 if (RD->getNumVBases() == 0) return true;
1197
1198 return hasNewVirtualFunction(RD);
1199}
1200
John McCall441c6232012-05-01 08:55:32 +00001201/// Does the given class inherit non-virtually from any of the classes
1202/// in the given set?
1203static bool hasNonVirtualBaseInSet(const CXXRecordDecl *RD,
1204 const ClassSetTy &set) {
1205 for (CXXRecordDecl::base_class_const_iterator
1206 I = RD->bases_begin(), E = RD->bases_end(); I != E; ++I) {
1207 // Ignore virtual links.
1208 if (I->isVirtual()) continue;
1209
1210 // Check whether the set contains the base.
1211 const CXXRecordDecl *base = I->getType()->getAsCXXRecordDecl();
1212 if (set.count(base))
1213 return true;
1214
1215 // Otherwise, recurse and propagate.
1216 if (hasNonVirtualBaseInSet(base, set))
1217 return true;
1218 }
1219
1220 return false;
1221}
1222
1223/// Does the given method (B::foo()) already override a method (A::foo())
1224/// such that A requires a vtordisp in B? If so, we don't need to add a
1225/// new vtordisp for B in a yet-more-derived class C providing C::foo().
1226static bool overridesMethodRequiringVtorDisp(const ASTContext &Context,
1227 const CXXMethodDecl *M) {
1228 CXXMethodDecl::method_iterator
1229 I = M->begin_overridden_methods(), E = M->end_overridden_methods();
1230 if (I == E) return false;
1231
1232 const ASTRecordLayout::VBaseOffsetsMapTy &offsets =
1233 Context.getASTRecordLayout(M->getParent()).getVBaseOffsetsMap();
1234 do {
1235 const CXXMethodDecl *overridden = *I;
1236
1237 // If the overridden method's class isn't recognized as a virtual
1238 // base in the derived class, ignore it.
1239 ASTRecordLayout::VBaseOffsetsMapTy::const_iterator
1240 it = offsets.find(overridden->getParent());
1241 if (it == offsets.end()) continue;
1242
1243 // Otherwise, check if the overridden method's class needs a vtordisp.
1244 if (it->second.hasVtorDisp()) return true;
1245
1246 } while (++I != E);
1247 return false;
1248}
1249
1250/// In the Microsoft ABI, decide which of the virtual bases require a
1251/// vtordisp field.
1252void RecordLayoutBuilder::computeVtordisps(const CXXRecordDecl *RD,
1253 ClassSetTy &vtordispVBases) {
1254 // Bail out if we have no virtual bases.
1255 assert(RD->getNumVBases());
1256
1257 // Build up the set of virtual bases that we haven't decided yet.
1258 ClassSetTy undecidedVBases;
1259 for (CXXRecordDecl::base_class_const_iterator
1260 I = RD->vbases_begin(), E = RD->vbases_end(); I != E; ++I) {
1261 const CXXRecordDecl *vbase = I->getType()->getAsCXXRecordDecl();
1262 undecidedVBases.insert(vbase);
1263 }
1264 assert(!undecidedVBases.empty());
1265
1266 // A virtual base requires a vtordisp field in a derived class if it
1267 // requires a vtordisp field in a base class. Walk all the direct
1268 // bases and collect this information.
1269 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1270 E = RD->bases_end(); I != E; ++I) {
1271 const CXXRecordDecl *base = I->getType()->getAsCXXRecordDecl();
1272 const ASTRecordLayout &baseLayout = Context.getASTRecordLayout(base);
1273
1274 // Iterate over the set of virtual bases provided by this class.
1275 for (ASTRecordLayout::VBaseOffsetsMapTy::const_iterator
1276 VI = baseLayout.getVBaseOffsetsMap().begin(),
1277 VE = baseLayout.getVBaseOffsetsMap().end(); VI != VE; ++VI) {
1278 // If it doesn't need a vtordisp in this base, ignore it.
1279 if (!VI->second.hasVtorDisp()) continue;
1280
1281 // If we've already seen it and decided it needs a vtordisp, ignore it.
1282 if (!undecidedVBases.erase(VI->first))
1283 continue;
1284
1285 // Add it.
1286 vtordispVBases.insert(VI->first);
1287
1288 // Quit as soon as we've decided everything.
1289 if (undecidedVBases.empty())
1290 return;
1291 }
1292 }
1293
1294 // Okay, we have virtual bases that we haven't yet decided about. A
1295 // virtual base requires a vtordisp if any the non-destructor
1296 // virtual methods declared in this class directly override a method
1297 // provided by that virtual base. (If so, we need to emit a thunk
1298 // for that method, to be used in the construction vftable, which
1299 // applies an additional 'vtordisp' this-adjustment.)
1300
1301 // Collect the set of bases directly overridden by any method in this class.
1302 // It's possible that some of these classes won't be virtual bases, or won't be
1303 // provided by virtual bases, or won't be virtual bases in the overridden
1304 // instance but are virtual bases elsewhere. Only the last matters for what
1305 // we're doing, and we can ignore those: if we don't directly override
1306 // a method provided by a virtual copy of a base class, but we do directly
1307 // override a method provided by a non-virtual copy of that base class,
1308 // then we must indirectly override the method provided by the virtual base,
1309 // and so we should already have collected it in the loop above.
1310 ClassSetTy overriddenBases;
1311 for (CXXRecordDecl::method_iterator
1312 M = RD->method_begin(), E = RD->method_end(); M != E; ++M) {
1313 // Ignore non-virtual methods and destructors.
1314 if (isa<CXXDestructorDecl>(*M) || !M->isVirtual())
1315 continue;
1316
1317 for (CXXMethodDecl::method_iterator I = M->begin_overridden_methods(),
1318 E = M->end_overridden_methods(); I != E; ++I) {
1319 const CXXMethodDecl *overriddenMethod = (*I);
1320
1321 // Ignore methods that override methods from vbases that require
1322 // require vtordisps.
1323 if (overridesMethodRequiringVtorDisp(Context, overriddenMethod))
1324 continue;
1325
1326 // As an optimization, check immediately whether we're overriding
1327 // something from the undecided set.
1328 const CXXRecordDecl *overriddenBase = overriddenMethod->getParent();
1329 if (undecidedVBases.erase(overriddenBase)) {
1330 vtordispVBases.insert(overriddenBase);
1331 if (undecidedVBases.empty()) return;
1332
1333 // We can't 'continue;' here because one of our undecided
1334 // vbases might non-virtually inherit from this base.
1335 // Consider:
1336 // struct A { virtual void foo(); };
1337 // struct B : A {};
1338 // struct C : virtual A, virtual B { virtual void foo(); };
1339 // We need a vtordisp for B here.
1340 }
1341
1342 // Otherwise, just collect it.
1343 overriddenBases.insert(overriddenBase);
1344 }
1345 }
1346
1347 // Walk the undecided v-bases and check whether they (non-virtually)
1348 // provide any of the overridden bases. We don't need to consider
1349 // virtual links because the vtordisp inheres to the layout
1350 // subobject containing the base.
1351 for (ClassSetTy::const_iterator
1352 I = undecidedVBases.begin(), E = undecidedVBases.end(); I != E; ++I) {
1353 if (hasNonVirtualBaseInSet(*I, overriddenBases))
1354 vtordispVBases.insert(*I);
1355 }
1356}
1357
John McCall9da23522011-11-08 04:01:03 +00001358/// hasNewVirtualFunction - Does the given polymorphic class declare a
1359/// virtual function that does not override a method from any of its
1360/// base classes?
Eli Friedman2fe36362011-09-27 19:12:27 +00001361bool
John McCall441c6232012-05-01 08:55:32 +00001362RecordLayoutBuilder::hasNewVirtualFunction(const CXXRecordDecl *RD,
1363 bool IgnoreDestructor) const {
John McCall9da23522011-11-08 04:01:03 +00001364 if (!RD->getNumBases())
1365 return true;
1366
Eli Friedman2fe36362011-09-27 19:12:27 +00001367 for (CXXRecordDecl::method_iterator method = RD->method_begin();
1368 method != RD->method_end();
1369 ++method) {
John McCall441c6232012-05-01 08:55:32 +00001370 if (method->isVirtual() && !method->size_overridden_methods() &&
1371 !(IgnoreDestructor && method->getKind() == Decl::CXXDestructor)) {
Eli Friedman2fe36362011-09-27 19:12:27 +00001372 return true;
1373 }
1374 }
1375 return false;
1376}
1377
John McCall9da23522011-11-08 04:01:03 +00001378/// isPossiblePrimaryBase - Is the given base class an acceptable
1379/// primary base class?
Eli Friedman97c0aef2011-10-18 00:55:28 +00001380bool
John McCall441c6232012-05-01 08:55:32 +00001381RecordLayoutBuilder::isPossiblePrimaryBase(const CXXRecordDecl *base) const {
John McCall9da23522011-11-08 04:01:03 +00001382 // In the Itanium ABI, a class can be a primary base class if it has
1383 // a vtable for any reason.
1384 if (!isMicrosoftCXXABI())
John McCall441c6232012-05-01 08:55:32 +00001385 return base->isDynamicClass();
John McCall9da23522011-11-08 04:01:03 +00001386
1387 // In the MS ABI, a class can only be a primary base class if it
1388 // provides a vf-table at a static offset. That means it has to be
1389 // non-virtual base. The existence of a separate vb-table means
1390 // that it's possible to get virtual functions only from a virtual
1391 // base, which we have to guard against.
1392
1393 // First off, it has to have virtual functions.
John McCall441c6232012-05-01 08:55:32 +00001394 if (!base->isPolymorphic()) return false;
John McCall9da23522011-11-08 04:01:03 +00001395
John McCall441c6232012-05-01 08:55:32 +00001396 // If it has no virtual bases, then the vfptr must be at a static offset.
1397 if (!base->getNumVBases()) return true;
1398
1399 // Otherwise, the necessary information is cached in the layout.
1400 const ASTRecordLayout &layout = Context.getASTRecordLayout(base);
John McCall9da23522011-11-08 04:01:03 +00001401
John McCall441c6232012-05-01 08:55:32 +00001402 // If the base has its own vfptr, it can be a primary base.
1403 if (layout.hasOwnVFPtr()) return true;
1404
1405 // If the base has a primary base class, then it can be a primary base.
1406 if (layout.getPrimaryBase()) return true;
1407
1408 // Otherwise it can't.
1409 return false;
Eli Friedman2fe36362011-09-27 19:12:27 +00001410}
1411
Anders Carlsson97913572010-04-15 16:12:58 +00001412void
Anders Carlsson7d0918a2010-05-26 05:58:59 +00001413RecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
Anders Carlsson97913572010-04-15 16:12:58 +00001414 const CXXRecordDecl *MostDerivedClass) {
Anders Carlsson88f42962010-03-11 04:33:54 +00001415 const CXXRecordDecl *PrimaryBase;
Anders Carlssonbdda6c12010-04-10 18:42:27 +00001416 bool PrimaryBaseIsVirtual;
Anders Carlsson37147ea2010-03-11 05:42:17 +00001417
Anders Carlssonbdda6c12010-04-10 18:42:27 +00001418 if (MostDerivedClass == RD) {
Anders Carlsson28fdd0a2010-05-26 05:20:58 +00001419 PrimaryBase = this->PrimaryBase;
1420 PrimaryBaseIsVirtual = this->PrimaryBaseIsVirtual;
Anders Carlssonbdda6c12010-04-10 18:42:27 +00001421 } else {
Anders Carlsson0f0e9b02010-04-16 15:07:51 +00001422 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlsson88f42962010-03-11 04:33:54 +00001423 PrimaryBase = Layout.getPrimaryBase();
Anders Carlssonc9e814b2010-11-24 23:12:57 +00001424 PrimaryBaseIsVirtual = Layout.isPrimaryBaseVirtual();
Anders Carlssonbdda6c12010-04-10 18:42:27 +00001425 }
1426
Anders Carlsson622e2472010-03-11 04:24:02 +00001427 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1428 E = RD->bases_end(); I != E; ++I) {
1429 assert(!I->getType()->isDependentType() &&
Sebastian Redl9994a342009-10-25 17:03:50 +00001430 "Cannot layout class with dependent bases.");
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001431
Anders Carlsson276b4912010-05-29 17:48:36 +00001432 const CXXRecordDecl *BaseDecl =
John McCall9da23522011-11-08 04:01:03 +00001433 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
Anders Carlsson622e2472010-03-11 04:24:02 +00001434
1435 if (I->isVirtual()) {
Anders Carlsson276b4912010-05-29 17:48:36 +00001436 if (PrimaryBase != BaseDecl || !PrimaryBaseIsVirtual) {
1437 bool IndirectPrimaryBase = IndirectPrimaryBases.count(BaseDecl);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001438
Anders Carlssonbdda6c12010-04-10 18:42:27 +00001439 // Only lay out the virtual base if it's not an indirect primary base.
1440 if (!IndirectPrimaryBase) {
1441 // Only visit virtual bases once.
Anders Carlsson276b4912010-05-29 17:48:36 +00001442 if (!VisitedVirtualBases.insert(BaseDecl))
Anders Carlssonbdda6c12010-04-10 18:42:27 +00001443 continue;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001444
Anders Carlsson276b4912010-05-29 17:48:36 +00001445 const BaseSubobjectInfo *BaseInfo = VirtualBaseInfo.lookup(BaseDecl);
1446 assert(BaseInfo && "Did not find virtual base info!");
1447 LayoutVirtualBase(BaseInfo);
Anders Carlsson147b5dd2010-03-11 04:10:39 +00001448 }
Mike Stump968db332009-11-05 04:02:15 +00001449 }
Mike Stump4ef98092009-08-13 22:53:07 +00001450 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001451
Anders Carlsson276b4912010-05-29 17:48:36 +00001452 if (!BaseDecl->getNumVBases()) {
Anders Carlsson622e2472010-03-11 04:24:02 +00001453 // This base isn't interesting since it doesn't have any virtual bases.
1454 continue;
Mike Stumpfe3010d2009-08-16 19:04:13 +00001455 }
Anders Carlsson622e2472010-03-11 04:24:02 +00001456
Anders Carlsson276b4912010-05-29 17:48:36 +00001457 LayoutVirtualBases(BaseDecl, MostDerivedClass);
Mike Stumpeb19fa92009-08-06 13:41:24 +00001458 }
1459}
1460
John McCall9da23522011-11-08 04:01:03 +00001461void RecordLayoutBuilder::MSLayoutVirtualBases(const CXXRecordDecl *RD) {
John McCall9da23522011-11-08 04:01:03 +00001462 if (!RD->getNumVBases())
1463 return;
1464
John McCall441c6232012-05-01 08:55:32 +00001465 ClassSetTy VtordispVBases;
1466 computeVtordisps(RD, VtordispVBases);
1467
John McCall9da23522011-11-08 04:01:03 +00001468 // This is substantially simplified because there are no virtual
1469 // primary bases.
1470 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
1471 E = RD->vbases_end(); I != E; ++I) {
1472 const CXXRecordDecl *BaseDecl = I->getType()->getAsCXXRecordDecl();
1473 const BaseSubobjectInfo *BaseInfo = VirtualBaseInfo.lookup(BaseDecl);
1474 assert(BaseInfo && "Did not find virtual base info!");
John McCall441c6232012-05-01 08:55:32 +00001475
1476 // If this base requires a vtordisp, add enough space for an int field.
1477 // This is apparently always 32-bits, even on x64.
1478 bool vtordispNeeded = false;
1479 if (VtordispVBases.count(BaseDecl)) {
1480 CharUnits IntSize =
1481 CharUnits::fromQuantity(Context.getTargetInfo().getIntWidth() / 8);
1482
1483 setSize(getSize() + IntSize);
1484 setDataSize(getSize());
1485 vtordispNeeded = true;
1486 }
1487
1488 LayoutVirtualBase(BaseInfo, vtordispNeeded);
John McCall9da23522011-11-08 04:01:03 +00001489 }
1490}
1491
John McCall441c6232012-05-01 08:55:32 +00001492void RecordLayoutBuilder::LayoutVirtualBase(const BaseSubobjectInfo *Base,
1493 bool IsVtordispNeed) {
Anders Carlsson3cd09cc2010-05-29 19:44:50 +00001494 assert(!Base->Derived && "Trying to lay out a primary virtual base!");
1495
Anders Carlssone3bdbee2010-03-10 22:26:24 +00001496 // Layout the base.
Anders Carlssona2311512010-10-31 22:20:42 +00001497 CharUnits Offset = LayoutBase(Base);
Anders Carlssone3bdbee2010-03-10 22:26:24 +00001498
1499 // Add its base class offset.
Anders Carlsson276b4912010-05-29 17:48:36 +00001500 assert(!VBases.count(Base->Class) && "vbase offset already exists!");
John McCall441c6232012-05-01 08:55:32 +00001501 VBases.insert(std::make_pair(Base->Class,
1502 ASTRecordLayout::VBaseInfo(Offset, IsVtordispNeed)));
1503
1504 if (!isMicrosoftCXXABI())
1505 AddPrimaryVirtualBaseOffsets(Base, Offset);
Anders Carlssone239b9d2010-03-10 22:21:28 +00001506}
1507
Anders Carlssona2311512010-10-31 22:20:42 +00001508CharUnits RecordLayoutBuilder::LayoutBase(const BaseSubobjectInfo *Base) {
Anders Carlssonb1d880b2010-05-29 20:47:33 +00001509 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base->Class);
Anders Carlssone239b9d2010-03-10 22:21:28 +00001510
Douglas Gregor453dbcb2012-01-26 07:55:45 +00001511
1512 CharUnits Offset;
1513
1514 // Query the external layout to see if it provides an offset.
1515 bool HasExternalLayout = false;
1516 if (ExternalLayout) {
1517 llvm::DenseMap<const CXXRecordDecl *, CharUnits>::iterator Known;
1518 if (Base->IsVirtual) {
1519 Known = ExternalVirtualBaseOffsets.find(Base->Class);
1520 if (Known != ExternalVirtualBaseOffsets.end()) {
1521 Offset = Known->second;
1522 HasExternalLayout = true;
1523 }
1524 } else {
1525 Known = ExternalBaseOffsets.find(Base->Class);
1526 if (Known != ExternalBaseOffsets.end()) {
1527 Offset = Known->second;
1528 HasExternalLayout = true;
1529 }
1530 }
1531 }
1532
Anders Carlssone239b9d2010-03-10 22:21:28 +00001533 // If we have an empty base class, try to place it at offset 0.
Anders Carlssonb1d880b2010-05-29 20:47:33 +00001534 if (Base->Class->isEmpty() &&
Douglas Gregor453dbcb2012-01-26 07:55:45 +00001535 (!HasExternalLayout || Offset == CharUnits::Zero()) &&
Anders Carlssona3d43802010-10-31 22:13:23 +00001536 EmptySubobjects->CanPlaceBaseAtOffset(Base, CharUnits::Zero())) {
Ken Dyckf079b732011-02-28 02:01:38 +00001537 setSize(std::max(getSize(), Layout.getSize()));
Anders Carlssone239b9d2010-03-10 22:21:28 +00001538
Anders Carlssona2311512010-10-31 22:20:42 +00001539 return CharUnits::Zero();
Anders Carlssone239b9d2010-03-10 22:21:28 +00001540 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001541
Ken Dyck3263e092011-02-19 18:58:07 +00001542 CharUnits UnpackedBaseAlign = Layout.getNonVirtualAlign();
1543 CharUnits BaseAlign = (Packed) ? CharUnits::One() : UnpackedBaseAlign;
Argyrios Kyrtzidis43ddd9f2010-12-09 00:35:20 +00001544
1545 // The maximum field alignment overrides base align.
Ken Dyck834945c2011-02-17 01:49:42 +00001546 if (!MaxFieldAlignment.isZero()) {
Ken Dyck3263e092011-02-19 18:58:07 +00001547 BaseAlign = std::min(BaseAlign, MaxFieldAlignment);
1548 UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment);
Argyrios Kyrtzidis43ddd9f2010-12-09 00:35:20 +00001549 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001550
Douglas Gregor453dbcb2012-01-26 07:55:45 +00001551 if (!HasExternalLayout) {
1552 // Round up the current record size to the base's alignment boundary.
1553 Offset = getDataSize().RoundUpToAlignment(BaseAlign);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001554
Douglas Gregor453dbcb2012-01-26 07:55:45 +00001555 // Try to place the base.
1556 while (!EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset))
1557 Offset += BaseAlign;
1558 } else {
1559 bool Allowed = EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset);
1560 (void)Allowed;
1561 assert(Allowed && "Base subobject externally placed at overlapping offset");
Douglas Gregorf5967602012-10-26 22:31:14 +00001562
1563 if (InferAlignment && Offset < getDataSize().RoundUpToAlignment(BaseAlign)){
1564 // The externally-supplied base offset is before the base offset we
1565 // computed. Assume that the structure is packed.
1566 Alignment = CharUnits::One();
1567 InferAlignment = false;
1568 }
Douglas Gregor453dbcb2012-01-26 07:55:45 +00001569 }
1570
Anders Carlssonb1d880b2010-05-29 20:47:33 +00001571 if (!Base->Class->isEmpty()) {
Anders Carlssone239b9d2010-03-10 22:21:28 +00001572 // Update the data size.
Ken Dyckf079b732011-02-28 02:01:38 +00001573 setDataSize(Offset + Layout.getNonVirtualSize());
Anders Carlssone239b9d2010-03-10 22:21:28 +00001574
Ken Dyckf079b732011-02-28 02:01:38 +00001575 setSize(std::max(getSize(), getDataSize()));
Anders Carlssone239b9d2010-03-10 22:21:28 +00001576 } else
Ken Dyckf079b732011-02-28 02:01:38 +00001577 setSize(std::max(getSize(), Offset + Layout.getSize()));
Anders Carlssone239b9d2010-03-10 22:21:28 +00001578
1579 // Remember max struct/class alignment.
Argyrios Kyrtzidis43ddd9f2010-12-09 00:35:20 +00001580 UpdateAlignment(BaseAlign, UnpackedBaseAlign);
Anders Carlssone239b9d2010-03-10 22:21:28 +00001581
Ken Dyckf079b732011-02-28 02:01:38 +00001582 return Offset;
Anders Carlssone239b9d2010-03-10 22:21:28 +00001583}
1584
Daniel Dunbarc6082fe2010-05-27 05:45:51 +00001585void RecordLayoutBuilder::InitializeLayout(const Decl *D) {
Eli Friedman5f608ae2012-10-12 23:29:20 +00001586 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) {
Daniel Dunbarc6082fe2010-05-27 05:45:51 +00001587 IsUnion = RD->isUnion();
Eli Friedman5f608ae2012-10-12 23:29:20 +00001588 IsMsStruct = RD->isMsStruct(Context);
1589 }
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001590
Eli Friedman5f608ae2012-10-12 23:29:20 +00001591 Packed = D->hasAttr<PackedAttr>();
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001592
Daniel Dunbar88934e82011-10-05 21:04:55 +00001593 // Honor the default struct packing maximum alignment flag.
David Blaikie4e4d0842012-03-11 07:00:24 +00001594 if (unsigned DefaultMaxFieldAlignment = Context.getLangOpts().PackStruct) {
Daniel Dunbar88934e82011-10-05 21:04:55 +00001595 MaxFieldAlignment = CharUnits::fromQuantity(DefaultMaxFieldAlignment);
1596 }
1597
Daniel Dunbarc6082fe2010-05-27 05:45:51 +00001598 // mac68k alignment supersedes maximum field alignment and attribute aligned,
1599 // and forces all structures to have 2-byte alignment. The IBM docs on it
1600 // allude to additional (more complicated) semantics, especially with regard
1601 // to bit-fields, but gcc appears not to follow that.
1602 if (D->hasAttr<AlignMac68kAttr>()) {
1603 IsMac68kAlign = true;
Ken Dyck834945c2011-02-17 01:49:42 +00001604 MaxFieldAlignment = CharUnits::fromQuantity(2);
Ken Dyckea7f6c22011-02-16 02:05:21 +00001605 Alignment = CharUnits::fromQuantity(2);
Daniel Dunbarc6082fe2010-05-27 05:45:51 +00001606 } else {
1607 if (const MaxFieldAlignmentAttr *MFAA = D->getAttr<MaxFieldAlignmentAttr>())
Ken Dyck834945c2011-02-17 01:49:42 +00001608 MaxFieldAlignment = Context.toCharUnitsFromBits(MFAA->getAlignment());
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001609
Sean Huntcf807c42010-08-18 23:23:40 +00001610 if (unsigned MaxAlign = D->getMaxAlignment())
Ken Dyck3263e092011-02-19 18:58:07 +00001611 UpdateAlignment(Context.toCharUnitsFromBits(MaxAlign));
Daniel Dunbarc6082fe2010-05-27 05:45:51 +00001612 }
Douglas Gregor453dbcb2012-01-26 07:55:45 +00001613
1614 // If there is an external AST source, ask it for the various offsets.
1615 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
1616 if (ExternalASTSource *External = Context.getExternalSource()) {
1617 ExternalLayout = External->layoutRecordType(RD,
1618 ExternalSize,
1619 ExternalAlign,
1620 ExternalFieldOffsets,
1621 ExternalBaseOffsets,
1622 ExternalVirtualBaseOffsets);
1623
1624 // Update based on external alignment.
1625 if (ExternalLayout) {
Douglas Gregor394f7b62012-01-28 00:53:29 +00001626 if (ExternalAlign > 0) {
1627 Alignment = Context.toCharUnitsFromBits(ExternalAlign);
Douglas Gregor394f7b62012-01-28 00:53:29 +00001628 } else {
1629 // The external source didn't have alignment information; infer it.
1630 InferAlignment = true;
1631 }
Douglas Gregor453dbcb2012-01-26 07:55:45 +00001632 }
1633 }
Anders Carlssonc6cab682010-05-26 15:10:00 +00001634}
Anders Carlsson74cbe222009-07-19 00:18:47 +00001635
Anders Carlssonc6cab682010-05-26 15:10:00 +00001636void RecordLayoutBuilder::Layout(const RecordDecl *D) {
1637 InitializeLayout(D);
Anders Carlssona2df41c2009-07-18 21:48:39 +00001638 LayoutFields(D);
Mike Stump1eb44332009-09-09 15:08:12 +00001639
Anders Carlssonbda4c102009-07-18 20:20:21 +00001640 // Finally, round the size of the total struct up to the alignment of the
1641 // struct itself.
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001642 FinishLayout(D);
Anders Carlssonc6cab682010-05-26 15:10:00 +00001643}
1644
1645void RecordLayoutBuilder::Layout(const CXXRecordDecl *RD) {
1646 InitializeLayout(RD);
1647
Anders Carlssonc6cab682010-05-26 15:10:00 +00001648 // Lay out the vtable and the non-virtual bases.
1649 LayoutNonVirtualBases(RD);
1650
1651 LayoutFields(RD);
1652
Ken Dyck90ce2db2011-03-10 01:53:59 +00001653 NonVirtualSize = Context.toCharUnitsFromBits(
1654 llvm::RoundUpToAlignment(getSizeInBits(),
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001655 Context.getTargetInfo().getCharAlign()));
Ken Dyckea7f6c22011-02-16 02:05:21 +00001656 NonVirtualAlignment = Alignment;
Anders Carlssonc6cab682010-05-26 15:10:00 +00001657
John McCall441c6232012-05-01 08:55:32 +00001658 if (isMicrosoftCXXABI()) {
1659 if (NonVirtualSize != NonVirtualSize.RoundUpToAlignment(Alignment)) {
John McCall9da23522011-11-08 04:01:03 +00001660 CharUnits AlignMember =
1661 NonVirtualSize.RoundUpToAlignment(Alignment) - NonVirtualSize;
Anders Carlssonc6cab682010-05-26 15:10:00 +00001662
John McCall9da23522011-11-08 04:01:03 +00001663 setSize(getSize() + AlignMember);
1664 setDataSize(getSize());
Anders Carlssonc6cab682010-05-26 15:10:00 +00001665
John McCall9da23522011-11-08 04:01:03 +00001666 NonVirtualSize = Context.toCharUnitsFromBits(
1667 llvm::RoundUpToAlignment(getSizeInBits(),
1668 Context.getTargetInfo().getCharAlign()));
John McCall441c6232012-05-01 08:55:32 +00001669 }
John McCall9da23522011-11-08 04:01:03 +00001670
1671 MSLayoutVirtualBases(RD);
John McCall9da23522011-11-08 04:01:03 +00001672 } else {
1673 // Lay out the virtual bases and add the primary virtual base offsets.
1674 LayoutVirtualBases(RD, RD);
1675 }
1676
1677 // Finally, round the size of the total struct up to the alignment
Eli Friedman901dd662011-12-01 00:37:01 +00001678 // of the struct itself.
1679 FinishLayout(RD);
Anders Carlssonc6cab682010-05-26 15:10:00 +00001680
Anders Carlssona1e87162010-04-10 21:24:48 +00001681#ifndef NDEBUG
Anders Carlssonc6cab682010-05-26 15:10:00 +00001682 // Check that we have base offsets for all bases.
1683 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1684 E = RD->bases_end(); I != E; ++I) {
1685 if (I->isVirtual())
1686 continue;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001687
Anders Carlssonc6cab682010-05-26 15:10:00 +00001688 const CXXRecordDecl *BaseDecl =
1689 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1690
1691 assert(Bases.count(BaseDecl) && "Did not find base offset!");
1692 }
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001693
Anders Carlssonc6cab682010-05-26 15:10:00 +00001694 // And all virtual bases.
1695 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
1696 E = RD->vbases_end(); I != E; ++I) {
1697 const CXXRecordDecl *BaseDecl =
1698 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001699
Anders Carlssonc6cab682010-05-26 15:10:00 +00001700 assert(VBases.count(BaseDecl) && "Did not find base offset!");
Anders Carlssona1e87162010-04-10 21:24:48 +00001701 }
1702#endif
Anders Carlssonbda4c102009-07-18 20:20:21 +00001703}
1704
Anders Carlsson7d0918a2010-05-26 05:58:59 +00001705void RecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D) {
Anders Carlsson93fab9d2009-07-18 20:50:59 +00001706 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
Anders Carlsson0f0e9b02010-04-16 15:07:51 +00001707 const ASTRecordLayout &SL = Context.getASTObjCInterfaceLayout(SD);
Anders Carlsson93fab9d2009-07-18 20:50:59 +00001708
Ken Dyck3263e092011-02-19 18:58:07 +00001709 UpdateAlignment(SL.getAlignment());
Mike Stump1eb44332009-09-09 15:08:12 +00001710
Anders Carlsson93fab9d2009-07-18 20:50:59 +00001711 // We start laying out ivars not at the end of the superclass
1712 // structure, but at the next byte following the last field.
Ken Dycka0c21c42011-02-24 01:13:28 +00001713 setSize(SL.getDataSize());
Ken Dyckf079b732011-02-28 02:01:38 +00001714 setDataSize(getSize());
Anders Carlsson93fab9d2009-07-18 20:50:59 +00001715 }
Mike Stump1eb44332009-09-09 15:08:12 +00001716
Daniel Dunbarc6082fe2010-05-27 05:45:51 +00001717 InitializeLayout(D);
Anders Carlsson93fab9d2009-07-18 20:50:59 +00001718 // Layout each ivar sequentially.
Jordy Rosedb8264e2011-07-22 02:08:32 +00001719 for (const ObjCIvarDecl *IVD = D->all_declared_ivar_begin(); IVD;
1720 IVD = IVD->getNextIvar())
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00001721 LayoutField(IVD);
Mike Stump1eb44332009-09-09 15:08:12 +00001722
Anders Carlsson93fab9d2009-07-18 20:50:59 +00001723 // Finally, round the size of the total struct up to the alignment of the
1724 // struct itself.
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001725 FinishLayout(D);
Anders Carlsson93fab9d2009-07-18 20:50:59 +00001726}
1727
Anders Carlsson7d0918a2010-05-26 05:58:59 +00001728void RecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
Anders Carlssona2df41c2009-07-18 21:48:39 +00001729 // Layout each field, for now, just sequentially, respecting alignment. In
1730 // the future, this will need to be tweakable by targets.
Fariborz Jahanian62055b02011-04-26 23:52:16 +00001731 const FieldDecl *LastFD = 0;
Fariborz Jahanian855a8e72011-05-03 20:21:04 +00001732 ZeroLengthBitfield = 0;
Fariborz Jahanian6ec50ad2011-05-11 16:58:31 +00001733 unsigned RemainingInAlignment = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001734 for (RecordDecl::field_iterator Field = D->field_begin(),
Fariborz Jahanian62055b02011-04-26 23:52:16 +00001735 FieldEnd = D->field_end(); Field != FieldEnd; ++Field) {
1736 if (IsMsStruct) {
David Blaikie581deb32012-06-06 20:45:41 +00001737 FieldDecl *FD = *Field;
Fariborz Jahanian855a8e72011-05-03 20:21:04 +00001738 if (Context.ZeroBitfieldFollowsBitfield(FD, LastFD))
1739 ZeroLengthBitfield = FD;
Fariborz Jahanian62055b02011-04-26 23:52:16 +00001740 // Zero-length bitfields following non-bitfield members are
1741 // ignored:
Fariborz Jahanian855a8e72011-05-03 20:21:04 +00001742 else if (Context.ZeroBitfieldFollowsNonBitfield(FD, LastFD))
Fariborz Jahanian62055b02011-04-26 23:52:16 +00001743 continue;
Fariborz Jahanian31e7f222011-05-06 22:42:22 +00001744 // FIXME. streamline these conditions into a simple one.
Fariborz Jahanian52bbe7a2011-05-06 21:56:12 +00001745 else if (Context.BitfieldFollowsBitfield(FD, LastFD) ||
Chad Rosierdd7fddb2011-08-04 23:34:15 +00001746 Context.BitfieldFollowsNonBitfield(FD, LastFD) ||
1747 Context.NonBitfieldFollowsBitfield(FD, LastFD)) {
Fariborz Jahanian31e7f222011-05-06 22:42:22 +00001748 // 1) Adjacent bit fields are packed into the same 1-, 2-, or
Fariborz Jahanian9b3acaa2011-05-04 18:51:37 +00001749 // 4-byte allocation unit if the integral types are the same
1750 // size and if the next bit field fits into the current
1751 // allocation unit without crossing the boundary imposed by the
1752 // common alignment requirements of the bit fields.
Fariborz Jahanian31e7f222011-05-06 22:42:22 +00001753 // 2) Establish a new alignment for a bitfield following
Fariborz Jahanian52bbe7a2011-05-06 21:56:12 +00001754 // a non-bitfield if size of their types differ.
Fariborz Jahanian31e7f222011-05-06 22:42:22 +00001755 // 3) Establish a new alignment for a non-bitfield following
1756 // a bitfield if size of their types differ.
Fariborz Jahanian9b3acaa2011-05-04 18:51:37 +00001757 std::pair<uint64_t, unsigned> FieldInfo =
1758 Context.getTypeInfo(FD->getType());
1759 uint64_t TypeSize = FieldInfo.first;
1760 unsigned FieldAlign = FieldInfo.second;
Fariborz Jahanian30364d02011-05-09 22:03:17 +00001761 // This check is needed for 'long long' in -m32 mode.
Fariborz Jahanian364a59e2011-12-12 21:16:36 +00001762 if (TypeSize > FieldAlign &&
1763 (Context.hasSameType(FD->getType(),
1764 Context.UnsignedLongLongTy)
1765 ||Context.hasSameType(FD->getType(),
1766 Context.LongLongTy)))
Fariborz Jahanian30364d02011-05-09 22:03:17 +00001767 FieldAlign = TypeSize;
Fariborz Jahanian9b3acaa2011-05-04 18:51:37 +00001768 FieldInfo = Context.getTypeInfo(LastFD->getType());
1769 uint64_t TypeSizeLastFD = FieldInfo.first;
1770 unsigned FieldAlignLastFD = FieldInfo.second;
Fariborz Jahanian30364d02011-05-09 22:03:17 +00001771 // This check is needed for 'long long' in -m32 mode.
Fariborz Jahanian364a59e2011-12-12 21:16:36 +00001772 if (TypeSizeLastFD > FieldAlignLastFD &&
1773 (Context.hasSameType(LastFD->getType(),
1774 Context.UnsignedLongLongTy)
1775 || Context.hasSameType(LastFD->getType(),
1776 Context.LongLongTy)))
Fariborz Jahanian30364d02011-05-09 22:03:17 +00001777 FieldAlignLastFD = TypeSizeLastFD;
Fariborz Jahanian6ec50ad2011-05-11 16:58:31 +00001778
Fariborz Jahanian9b3acaa2011-05-04 18:51:37 +00001779 if (TypeSizeLastFD != TypeSize) {
Fariborz Jahanian6ec50ad2011-05-11 16:58:31 +00001780 if (RemainingInAlignment &&
1781 LastFD && LastFD->isBitField() &&
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001782 LastFD->getBitWidthValue(Context)) {
Fariborz Jahanian6ec50ad2011-05-11 16:58:31 +00001783 // If previous field was a bitfield with some remaining unfilled
1784 // bits, pad the field so current field starts on its type boundary.
1785 uint64_t FieldOffset =
1786 getDataSizeInBits() - UnfilledBitsInLastByte;
1787 uint64_t NewSizeInBits = RemainingInAlignment + FieldOffset;
1788 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001789 Context.getTargetInfo().getCharAlign()));
Fariborz Jahanian6ec50ad2011-05-11 16:58:31 +00001790 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
1791 RemainingInAlignment = 0;
1792 }
1793
Fariborz Jahanian9b3acaa2011-05-04 18:51:37 +00001794 uint64_t UnpaddedFieldOffset =
1795 getDataSizeInBits() - UnfilledBitsInLastByte;
1796 FieldAlign = std::max(FieldAlign, FieldAlignLastFD);
Fariborz Jahanian6ec50ad2011-05-11 16:58:31 +00001797
Fariborz Jahanianed63e032011-05-10 19:00:50 +00001798 // The maximum field alignment overrides the aligned attribute.
1799 if (!MaxFieldAlignment.isZero()) {
1800 unsigned MaxFieldAlignmentInBits =
1801 Context.toBits(MaxFieldAlignment);
1802 FieldAlign = std::min(FieldAlign, MaxFieldAlignmentInBits);
1803 }
Fariborz Jahanian6ec50ad2011-05-11 16:58:31 +00001804
Fariborz Jahanian9b3acaa2011-05-04 18:51:37 +00001805 uint64_t NewSizeInBits =
1806 llvm::RoundUpToAlignment(UnpaddedFieldOffset, FieldAlign);
1807 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001808 Context.getTargetInfo().getCharAlign()));
Fariborz Jahanian9b3acaa2011-05-04 18:51:37 +00001809 UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits;
1810 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
1811 }
Fariborz Jahanian6ec50ad2011-05-11 16:58:31 +00001812 if (FD->isBitField()) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001813 uint64_t FieldSize = FD->getBitWidthValue(Context);
Fariborz Jahanian6ec50ad2011-05-11 16:58:31 +00001814 assert (FieldSize > 0 && "LayoutFields - ms_struct layout");
1815 if (RemainingInAlignment < FieldSize)
1816 RemainingInAlignment = TypeSize - FieldSize;
1817 else
1818 RemainingInAlignment -= FieldSize;
1819 }
1820 }
1821 else if (FD->isBitField()) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001822 uint64_t FieldSize = FD->getBitWidthValue(Context);
Fariborz Jahanian6ec50ad2011-05-11 16:58:31 +00001823 std::pair<uint64_t, unsigned> FieldInfo =
1824 Context.getTypeInfo(FD->getType());
1825 uint64_t TypeSize = FieldInfo.first;
1826 RemainingInAlignment = TypeSize - FieldSize;
Fariborz Jahanian9b3acaa2011-05-04 18:51:37 +00001827 }
Fariborz Jahanian62055b02011-04-26 23:52:16 +00001828 LastFD = FD;
1829 }
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001830 else if (!Context.getTargetInfo().useBitFieldTypeAlignment() &&
1831 Context.getTargetInfo().useZeroLengthBitfieldAlignment()) {
David Blaikie581deb32012-06-06 20:45:41 +00001832 if (Field->isBitField() && Field->getBitWidthValue(Context) == 0)
1833 ZeroLengthBitfield = *Field;
Chad Rosier61a62212011-08-04 01:21:14 +00001834 }
David Blaikie581deb32012-06-06 20:45:41 +00001835 LayoutField(*Field);
Fariborz Jahanian62055b02011-04-26 23:52:16 +00001836 }
Fariborz Jahanian6ec50ad2011-05-11 16:58:31 +00001837 if (IsMsStruct && RemainingInAlignment &&
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001838 LastFD && LastFD->isBitField() && LastFD->getBitWidthValue(Context)) {
Fariborz Jahanian6ec50ad2011-05-11 16:58:31 +00001839 // If we ended a bitfield before the full length of the type then
1840 // pad the struct out to the full length of the last type.
1841 uint64_t FieldOffset =
1842 getDataSizeInBits() - UnfilledBitsInLastByte;
1843 uint64_t NewSizeInBits = RemainingInAlignment + FieldOffset;
1844 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001845 Context.getTargetInfo().getCharAlign()));
Fariborz Jahanian6ec50ad2011-05-11 16:58:31 +00001846 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
1847 }
Anders Carlssona2df41c2009-07-18 21:48:39 +00001848}
1849
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001850void RecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize,
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001851 uint64_t TypeSize,
1852 bool FieldPacked,
1853 const FieldDecl *D) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001854 assert(Context.getLangOpts().CPlusPlus &&
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001855 "Can only have wide bit-fields in C++!");
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001856
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001857 // Itanium C++ ABI 2.4:
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001858 // If sizeof(T)*8 < n, let T' be the largest integral POD type with
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001859 // sizeof(T')*8 <= n.
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001860
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001861 QualType IntegralPODTypes[] = {
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001862 Context.UnsignedCharTy, Context.UnsignedShortTy, Context.UnsignedIntTy,
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001863 Context.UnsignedLongTy, Context.UnsignedLongLongTy
1864 };
1865
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001866 QualType Type;
1867 for (unsigned I = 0, E = llvm::array_lengthof(IntegralPODTypes);
1868 I != E; ++I) {
1869 uint64_t Size = Context.getTypeSize(IntegralPODTypes[I]);
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001870
1871 if (Size > FieldSize)
1872 break;
1873
1874 Type = IntegralPODTypes[I];
1875 }
1876 assert(!Type.isNull() && "Did not find a type!");
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001877
Ken Dyck3b3e1a12011-03-01 01:36:00 +00001878 CharUnits TypeAlign = Context.getTypeAlignInChars(Type);
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001879
1880 // We're not going to use any of the unfilled bits in the last byte.
1881 UnfilledBitsInLastByte = 0;
1882
Anders Carlssonde9f1532010-04-17 20:21:41 +00001883 uint64_t FieldOffset;
Ken Dycka0c21c42011-02-24 01:13:28 +00001884 uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001885
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001886 if (IsUnion) {
Ken Dycka0c21c42011-02-24 01:13:28 +00001887 setDataSize(std::max(getDataSizeInBits(), FieldSize));
Anders Carlssonde9f1532010-04-17 20:21:41 +00001888 FieldOffset = 0;
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001889 } else {
Chad Rosierb8fca902011-08-05 22:38:04 +00001890 // The bitfield is allocated starting at the next offset aligned
1891 // appropriately for T', with length n bits.
Ken Dyck3b3e1a12011-03-01 01:36:00 +00001892 FieldOffset = llvm::RoundUpToAlignment(getDataSizeInBits(),
1893 Context.toBits(TypeAlign));
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001894
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001895 uint64_t NewSizeInBits = FieldOffset + FieldSize;
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001896
Ken Dyckd5e3ed02011-03-10 02:00:35 +00001897 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001898 Context.getTargetInfo().getCharAlign()));
Ken Dycka0c21c42011-02-24 01:13:28 +00001899 UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits;
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001900 }
1901
1902 // Place this field at the current location.
1903 FieldOffsets.push_back(FieldOffset);
1904
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001905 CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, FieldOffset,
Ken Dyck3b3e1a12011-03-01 01:36:00 +00001906 Context.toBits(TypeAlign), FieldPacked, D);
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001907
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001908 // Update the size.
Ken Dycka0c21c42011-02-24 01:13:28 +00001909 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00001910
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001911 // Remember max struct/class alignment.
Ken Dyck3b3e1a12011-03-01 01:36:00 +00001912 UpdateAlignment(TypeAlign);
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001913}
1914
Anders Carlsson7d0918a2010-05-26 05:58:59 +00001915void RecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001916 bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
Ken Dycka0c21c42011-02-24 01:13:28 +00001917 uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte;
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001918 uint64_t FieldOffset = IsUnion ? 0 : UnpaddedFieldOffset;
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001919 uint64_t FieldSize = D->getBitWidthValue(Context);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001920
Anders Carlsson0f0e9b02010-04-16 15:07:51 +00001921 std::pair<uint64_t, unsigned> FieldInfo = Context.getTypeInfo(D->getType());
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001922 uint64_t TypeSize = FieldInfo.first;
1923 unsigned FieldAlign = FieldInfo.second;
Fariborz Jahanian855a8e72011-05-03 20:21:04 +00001924
Fariborz Jahanian30364d02011-05-09 22:03:17 +00001925 // This check is needed for 'long long' in -m32 mode.
Fariborz Jahanian364a59e2011-12-12 21:16:36 +00001926 if (IsMsStruct && (TypeSize > FieldAlign) &&
1927 (Context.hasSameType(D->getType(),
1928 Context.UnsignedLongLongTy)
1929 || Context.hasSameType(D->getType(), Context.LongLongTy)))
Fariborz Jahanian30364d02011-05-09 22:03:17 +00001930 FieldAlign = TypeSize;
Chad Rosier61a62212011-08-04 01:21:14 +00001931
Fariborz Jahanian855a8e72011-05-03 20:21:04 +00001932 if (ZeroLengthBitfield) {
Chad Rosierb8fca902011-08-05 22:38:04 +00001933 std::pair<uint64_t, unsigned> FieldInfo;
1934 unsigned ZeroLengthBitfieldAlignment;
1935 if (IsMsStruct) {
1936 // If a zero-length bitfield is inserted after a bitfield,
1937 // and the alignment of the zero-length bitfield is
1938 // greater than the member that follows it, `bar', `bar'
1939 // will be aligned as the type of the zero-length bitfield.
1940 if (ZeroLengthBitfield != D) {
1941 FieldInfo = Context.getTypeInfo(ZeroLengthBitfield->getType());
1942 ZeroLengthBitfieldAlignment = FieldInfo.second;
1943 // Ignore alignment of subsequent zero-length bitfields.
1944 if ((ZeroLengthBitfieldAlignment > FieldAlign) || (FieldSize == 0))
1945 FieldAlign = ZeroLengthBitfieldAlignment;
1946 if (FieldSize)
1947 ZeroLengthBitfield = 0;
1948 }
1949 } else {
1950 // The alignment of a zero-length bitfield affects the alignment
1951 // of the next member. The alignment is the max of the zero
1952 // length bitfield's alignment and a target specific fixed value.
1953 unsigned ZeroLengthBitfieldBoundary =
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001954 Context.getTargetInfo().getZeroLengthBitfieldBoundary();
Chad Rosierb8fca902011-08-05 22:38:04 +00001955 if (ZeroLengthBitfieldBoundary > FieldAlign)
1956 FieldAlign = ZeroLengthBitfieldBoundary;
Fariborz Jahanian855a8e72011-05-03 20:21:04 +00001957 }
1958 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001959
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001960 if (FieldSize > TypeSize) {
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001961 LayoutWideBitField(FieldSize, TypeSize, FieldPacked, D);
Anders Carlsson4cf6f5f2010-04-16 15:57:11 +00001962 return;
1963 }
1964
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001965 // The align if the field is not packed. This is to check if the attribute
1966 // was unnecessary (-Wpacked).
1967 unsigned UnpackedFieldAlign = FieldAlign;
1968 uint64_t UnpackedFieldOffset = FieldOffset;
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001969 if (!Context.getTargetInfo().useBitFieldTypeAlignment() && !ZeroLengthBitfield)
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001970 UnpackedFieldAlign = 1;
1971
Chad Rosierb8fca902011-08-05 22:38:04 +00001972 if (FieldPacked ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001973 (!Context.getTargetInfo().useBitFieldTypeAlignment() && !ZeroLengthBitfield))
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001974 FieldAlign = 1;
Sean Huntcf807c42010-08-18 23:23:40 +00001975 FieldAlign = std::max(FieldAlign, D->getMaxAlignment());
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001976 UnpackedFieldAlign = std::max(UnpackedFieldAlign, D->getMaxAlignment());
Anders Carlsson42dbcc42009-11-22 17:37:31 +00001977
1978 // The maximum field alignment overrides the aligned attribute.
Eli Friedmanbff22ac2011-12-02 02:38:48 +00001979 if (!MaxFieldAlignment.isZero() && FieldSize != 0) {
Ken Dyck834945c2011-02-17 01:49:42 +00001980 unsigned MaxFieldAlignmentInBits = Context.toBits(MaxFieldAlignment);
1981 FieldAlign = std::min(FieldAlign, MaxFieldAlignmentInBits);
1982 UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignmentInBits);
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001983 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001984
Douglas Gregor394f7b62012-01-28 00:53:29 +00001985 // Check if we need to add padding to give the field the correct alignment.
1986 if (FieldSize == 0 ||
1987 (MaxFieldAlignment.isZero() &&
1988 (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize))
1989 FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00001990
Douglas Gregor394f7b62012-01-28 00:53:29 +00001991 if (FieldSize == 0 ||
1992 (MaxFieldAlignment.isZero() &&
1993 (UnpackedFieldOffset & (UnpackedFieldAlign-1)) + FieldSize > TypeSize))
1994 UnpackedFieldOffset = llvm::RoundUpToAlignment(UnpackedFieldOffset,
1995 UnpackedFieldAlign);
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00001996
Chad Rosierb8fca902011-08-05 22:38:04 +00001997 // Padding members don't affect overall alignment, unless zero length bitfield
1998 // alignment is enabled.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001999 if (!D->getIdentifier() && !Context.getTargetInfo().useZeroLengthBitfieldAlignment())
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00002000 FieldAlign = UnpackedFieldAlign = 1;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002001
Chad Rosierb8fca902011-08-05 22:38:04 +00002002 if (!IsMsStruct)
2003 ZeroLengthBitfield = 0;
2004
Douglas Gregor394f7b62012-01-28 00:53:29 +00002005 if (ExternalLayout)
2006 FieldOffset = updateExternalFieldOffset(D, FieldOffset);
2007
Anders Carlsson42dbcc42009-11-22 17:37:31 +00002008 // Place this field at the current location.
2009 FieldOffsets.push_back(FieldOffset);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002010
Douglas Gregor453dbcb2012-01-26 07:55:45 +00002011 if (!ExternalLayout)
2012 CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, UnpackedFieldOffset,
2013 UnpackedFieldAlign, FieldPacked, D);
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00002014
Anders Carlssone4fc0d92009-11-22 19:13:51 +00002015 // Update DataSize to include the last byte containing (part of) the bitfield.
2016 if (IsUnion) {
2017 // FIXME: I think FieldSize should be TypeSize here.
Ken Dycka0c21c42011-02-24 01:13:28 +00002018 setDataSize(std::max(getDataSizeInBits(), FieldSize));
Anders Carlssone4fc0d92009-11-22 19:13:51 +00002019 } else {
2020 uint64_t NewSizeInBits = FieldOffset + FieldSize;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002021
Ken Dyckd5e3ed02011-03-10 02:00:35 +00002022 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002023 Context.getTargetInfo().getCharAlign()));
Ken Dycka0c21c42011-02-24 01:13:28 +00002024 UnfilledBitsInLastByte = getDataSizeInBits() - NewSizeInBits;
Anders Carlssone4fc0d92009-11-22 19:13:51 +00002025 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002026
Anders Carlssone4fc0d92009-11-22 19:13:51 +00002027 // Update the size.
Ken Dycka0c21c42011-02-24 01:13:28 +00002028 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002029
Anders Carlsson42dbcc42009-11-22 17:37:31 +00002030 // Remember max struct/class alignment.
Ken Dyck3263e092011-02-19 18:58:07 +00002031 UpdateAlignment(Context.toCharUnitsFromBits(FieldAlign),
2032 Context.toCharUnitsFromBits(UnpackedFieldAlign));
Anders Carlsson42dbcc42009-11-22 17:37:31 +00002033}
2034
Douglas Gregor453dbcb2012-01-26 07:55:45 +00002035void RecordLayoutBuilder::LayoutField(const FieldDecl *D) {
Anders Carlsson42dbcc42009-11-22 17:37:31 +00002036 if (D->isBitField()) {
2037 LayoutBitField(D);
2038 return;
2039 }
2040
Ken Dycka0c21c42011-02-24 01:13:28 +00002041 uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastByte;
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00002042
Anders Carlssone4fc0d92009-11-22 19:13:51 +00002043 // Reset the unfilled bits.
2044 UnfilledBitsInLastByte = 0;
2045
Anders Carlsson42dbcc42009-11-22 17:37:31 +00002046 bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
Ken Dyck9ed9a252011-02-20 02:06:09 +00002047 CharUnits FieldOffset =
Ken Dycka0c21c42011-02-24 01:13:28 +00002048 IsUnion ? CharUnits::Zero() : getDataSize();
Ken Dyck9ed9a252011-02-20 02:06:09 +00002049 CharUnits FieldSize;
2050 CharUnits FieldAlign;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002051
Anders Carlsson42dbcc42009-11-22 17:37:31 +00002052 if (D->getType()->isIncompleteArrayType()) {
2053 // This is a flexible array member; we can't directly
2054 // query getTypeInfo about these, so we figure it out here.
2055 // Flexible array members don't have any size, but they
2056 // have to be aligned appropriately for their element type.
Ken Dyck9ed9a252011-02-20 02:06:09 +00002057 FieldSize = CharUnits::Zero();
Anders Carlsson0f0e9b02010-04-16 15:07:51 +00002058 const ArrayType* ATy = Context.getAsArrayType(D->getType());
Ken Dyck9ed9a252011-02-20 02:06:09 +00002059 FieldAlign = Context.getTypeAlignInChars(ATy->getElementType());
Anders Carlsson42dbcc42009-11-22 17:37:31 +00002060 } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) {
2061 unsigned AS = RT->getPointeeType().getAddressSpace();
Ken Dyck9ed9a252011-02-20 02:06:09 +00002062 FieldSize =
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002063 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(AS));
Ken Dyck9ed9a252011-02-20 02:06:09 +00002064 FieldAlign =
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002065 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(AS));
Anders Carlssonbda4c102009-07-18 20:20:21 +00002066 } else {
Ken Dyck9ed9a252011-02-20 02:06:09 +00002067 std::pair<CharUnits, CharUnits> FieldInfo =
2068 Context.getTypeInfoInChars(D->getType());
Anders Carlsson42dbcc42009-11-22 17:37:31 +00002069 FieldSize = FieldInfo.first;
2070 FieldAlign = FieldInfo.second;
Chad Rosier61a62212011-08-04 01:21:14 +00002071
Fariborz Jahanian855a8e72011-05-03 20:21:04 +00002072 if (ZeroLengthBitfield) {
Chad Rosier61a62212011-08-04 01:21:14 +00002073 CharUnits ZeroLengthBitfieldBoundary =
2074 Context.toCharUnitsFromBits(
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002075 Context.getTargetInfo().getZeroLengthBitfieldBoundary());
Chad Rosier61a62212011-08-04 01:21:14 +00002076 if (ZeroLengthBitfieldBoundary == CharUnits::Zero()) {
2077 // If a zero-length bitfield is inserted after a bitfield,
2078 // and the alignment of the zero-length bitfield is
2079 // greater than the member that follows it, `bar', `bar'
2080 // will be aligned as the type of the zero-length bitfield.
2081 std::pair<CharUnits, CharUnits> FieldInfo =
2082 Context.getTypeInfoInChars(ZeroLengthBitfield->getType());
2083 CharUnits ZeroLengthBitfieldAlignment = FieldInfo.second;
2084 if (ZeroLengthBitfieldAlignment > FieldAlign)
2085 FieldAlign = ZeroLengthBitfieldAlignment;
Chad Rosier0e7bf402011-08-04 19:25:14 +00002086 } else if (ZeroLengthBitfieldBoundary > FieldAlign) {
Chad Rosier61a62212011-08-04 01:21:14 +00002087 // Align 'bar' based on a fixed alignment specified by the target.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002088 assert(Context.getTargetInfo().useZeroLengthBitfieldAlignment() &&
Chad Rosier6e43f3f2011-08-04 17:52:43 +00002089 "ZeroLengthBitfieldBoundary should only be used in conjunction"
2090 " with useZeroLengthBitfieldAlignment.");
Chad Rosier61a62212011-08-04 01:21:14 +00002091 FieldAlign = ZeroLengthBitfieldBoundary;
2092 }
Fariborz Jahanian855a8e72011-05-03 20:21:04 +00002093 ZeroLengthBitfield = 0;
2094 }
Douglas Gregor6f755502011-02-01 15:15:22 +00002095
Eli Friedman5f608ae2012-10-12 23:29:20 +00002096 if (IsMsStruct) {
Douglas Gregor6f755502011-02-01 15:15:22 +00002097 // If MS bitfield layout is required, figure out what type is being
2098 // laid out and align the field to the width of that type.
2099
2100 // Resolve all typedefs down to their base type and round up the field
2101 // alignment if necessary.
2102 QualType T = Context.getBaseElementType(D->getType());
2103 if (const BuiltinType *BTy = T->getAs<BuiltinType>()) {
Ken Dyck9ed9a252011-02-20 02:06:09 +00002104 CharUnits TypeSize = Context.getTypeSizeInChars(BTy);
Douglas Gregor6f755502011-02-01 15:15:22 +00002105 if (TypeSize > FieldAlign)
2106 FieldAlign = TypeSize;
2107 }
2108 }
Anders Carlssonbda4c102009-07-18 20:20:21 +00002109 }
Mike Stump1eb44332009-09-09 15:08:12 +00002110
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00002111 // The align if the field is not packed. This is to check if the attribute
2112 // was unnecessary (-Wpacked).
Ken Dyck9ed9a252011-02-20 02:06:09 +00002113 CharUnits UnpackedFieldAlign = FieldAlign;
2114 CharUnits UnpackedFieldOffset = FieldOffset;
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00002115
Anders Carlsson42dbcc42009-11-22 17:37:31 +00002116 if (FieldPacked)
Ken Dyck9ed9a252011-02-20 02:06:09 +00002117 FieldAlign = CharUnits::One();
2118 CharUnits MaxAlignmentInChars =
2119 Context.toCharUnitsFromBits(D->getMaxAlignment());
2120 FieldAlign = std::max(FieldAlign, MaxAlignmentInChars);
2121 UnpackedFieldAlign = std::max(UnpackedFieldAlign, MaxAlignmentInChars);
Anders Carlsson42dbcc42009-11-22 17:37:31 +00002122
2123 // The maximum field alignment overrides the aligned attribute.
Ken Dyck834945c2011-02-17 01:49:42 +00002124 if (!MaxFieldAlignment.isZero()) {
Ken Dyck9ed9a252011-02-20 02:06:09 +00002125 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
2126 UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignment);
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00002127 }
Anders Carlsson42dbcc42009-11-22 17:37:31 +00002128
Douglas Gregor394f7b62012-01-28 00:53:29 +00002129 // Round up the current record size to the field's alignment boundary.
2130 FieldOffset = FieldOffset.RoundUpToAlignment(FieldAlign);
2131 UnpackedFieldOffset =
2132 UnpackedFieldOffset.RoundUpToAlignment(UnpackedFieldAlign);
2133
2134 if (ExternalLayout) {
2135 FieldOffset = Context.toCharUnitsFromBits(
2136 updateExternalFieldOffset(D, Context.toBits(FieldOffset)));
2137
2138 if (!IsUnion && EmptySubobjects) {
2139 // Record the fact that we're placing a field at this offset.
2140 bool Allowed = EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset);
2141 (void)Allowed;
2142 assert(Allowed && "Externally-placed field cannot be placed here");
2143 }
2144 } else {
Douglas Gregor453dbcb2012-01-26 07:55:45 +00002145 if (!IsUnion && EmptySubobjects) {
2146 // Check if we can place the field at this offset.
2147 while (!EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset)) {
2148 // We couldn't place the field at the offset. Try again at a new offset.
2149 FieldOffset += FieldAlign;
2150 }
Anders Carlsson42dbcc42009-11-22 17:37:31 +00002151 }
Anders Carlsson42dbcc42009-11-22 17:37:31 +00002152 }
Douglas Gregor453dbcb2012-01-26 07:55:45 +00002153
Anders Carlssonbda4c102009-07-18 20:20:21 +00002154 // Place this field at the current location.
Ken Dyck9ed9a252011-02-20 02:06:09 +00002155 FieldOffsets.push_back(Context.toBits(FieldOffset));
Mike Stump1eb44332009-09-09 15:08:12 +00002156
Douglas Gregor453dbcb2012-01-26 07:55:45 +00002157 if (!ExternalLayout)
2158 CheckFieldPadding(Context.toBits(FieldOffset), UnpaddedFieldOffset,
2159 Context.toBits(UnpackedFieldOffset),
2160 Context.toBits(UnpackedFieldAlign), FieldPacked, D);
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00002161
Anders Carlssonbda4c102009-07-18 20:20:21 +00002162 // Reserve space for this field.
Eli Friedmancd7a21b2012-01-12 23:27:03 +00002163 uint64_t FieldSizeInBits = Context.toBits(FieldSize);
Anders Carlssonbda4c102009-07-18 20:20:21 +00002164 if (IsUnion)
Eli Friedman83be12c2012-01-12 23:48:56 +00002165 setDataSize(std::max(getDataSizeInBits(), FieldSizeInBits));
Anders Carlssonbda4c102009-07-18 20:20:21 +00002166 else
Eli Friedman83be12c2012-01-12 23:48:56 +00002167 setDataSize(FieldOffset + FieldSize);
Mike Stump1eb44332009-09-09 15:08:12 +00002168
Eli Friedman83be12c2012-01-12 23:48:56 +00002169 // Update the size.
2170 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
Mike Stump1eb44332009-09-09 15:08:12 +00002171
Anders Carlssonbda4c102009-07-18 20:20:21 +00002172 // Remember max struct/class alignment.
Ken Dyck9ed9a252011-02-20 02:06:09 +00002173 UpdateAlignment(FieldAlign, UnpackedFieldAlign);
Anders Carlssonbda4c102009-07-18 20:20:21 +00002174}
2175
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00002176void RecordLayoutBuilder::FinishLayout(const NamedDecl *D) {
Anders Carlssonbda4c102009-07-18 20:20:21 +00002177 // In C++, records cannot be of size 0.
David Blaikie4e4d0842012-03-11 07:00:24 +00002178 if (Context.getLangOpts().CPlusPlus && getSizeInBits() == 0) {
Fariborz Jahanianadf082e2011-02-02 19:36:18 +00002179 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
2180 // Compatibility with gcc requires a class (pod or non-pod)
2181 // which is not empty but of size 0; such as having fields of
2182 // array of zero-length, remains of Size 0
2183 if (RD->isEmpty())
Ken Dyckf079b732011-02-28 02:01:38 +00002184 setSize(CharUnits::One());
Fariborz Jahanianadf082e2011-02-02 19:36:18 +00002185 }
2186 else
Ken Dyckf079b732011-02-28 02:01:38 +00002187 setSize(CharUnits::One());
Fariborz Jahanianadf082e2011-02-02 19:36:18 +00002188 }
Eli Friedman901dd662011-12-01 00:37:01 +00002189
Douglas Gregorf5967602012-10-26 22:31:14 +00002190 // Finally, round the size of the record up to the alignment of the
2191 // record itself.
2192 uint64_t UnpaddedSize = getSizeInBits() - UnfilledBitsInLastByte;
2193 uint64_t UnpackedSizeInBits =
2194 llvm::RoundUpToAlignment(getSizeInBits(),
2195 Context.toBits(UnpackedAlignment));
2196 CharUnits UnpackedSize = Context.toCharUnitsFromBits(UnpackedSizeInBits);
2197 uint64_t RoundedSize
2198 = llvm::RoundUpToAlignment(getSizeInBits(), Context.toBits(Alignment));
2199
2200 if (ExternalLayout) {
2201 // If we're inferring alignment, and the external size is smaller than
2202 // our size after we've rounded up to alignment, conservatively set the
2203 // alignment to 1.
2204 if (InferAlignment && ExternalSize < RoundedSize) {
2205 Alignment = CharUnits::One();
2206 InferAlignment = false;
2207 }
2208 setSize(ExternalSize);
2209 return;
2210 }
2211
2212
Eli Friedman901dd662011-12-01 00:37:01 +00002213 // MSVC doesn't round up to the alignment of the record with virtual bases.
2214 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
2215 if (isMicrosoftCXXABI() && RD->getNumVBases())
2216 return;
2217 }
2218
Douglas Gregorf5967602012-10-26 22:31:14 +00002219 // Set the size to the final size.
2220 setSize(RoundedSize);
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00002221
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002222 unsigned CharBitNum = Context.getTargetInfo().getCharWidth();
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00002223 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) {
2224 // Warn if padding was introduced to the struct/class/union.
Ken Dycka0c21c42011-02-24 01:13:28 +00002225 if (getSizeInBits() > UnpaddedSize) {
2226 unsigned PadSize = getSizeInBits() - UnpaddedSize;
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00002227 bool InBits = true;
2228 if (PadSize % CharBitNum == 0) {
2229 PadSize = PadSize / CharBitNum;
2230 InBits = false;
2231 }
2232 Diag(RD->getLocation(), diag::warn_padded_struct_size)
2233 << Context.getTypeDeclType(RD)
2234 << PadSize
2235 << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not
2236 }
2237
2238 // Warn if we packed it unnecessarily. If the alignment is 1 byte don't
2239 // bother since there won't be alignment issues.
Ken Dycka0c21c42011-02-24 01:13:28 +00002240 if (Packed && UnpackedAlignment > CharUnits::One() &&
Ken Dyckf079b732011-02-28 02:01:38 +00002241 getSize() == UnpackedSize)
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00002242 Diag(D->getLocation(), diag::warn_unnecessary_packed)
2243 << Context.getTypeDeclType(RD);
2244 }
Anders Carlssonbda4c102009-07-18 20:20:21 +00002245}
2246
Ken Dyck3263e092011-02-19 18:58:07 +00002247void RecordLayoutBuilder::UpdateAlignment(CharUnits NewAlignment,
2248 CharUnits UnpackedNewAlignment) {
Douglas Gregor453dbcb2012-01-26 07:55:45 +00002249 // The alignment is not modified when using 'mac68k' alignment or when
Douglas Gregor394f7b62012-01-28 00:53:29 +00002250 // we have an externally-supplied layout that also provides overall alignment.
2251 if (IsMac68kAlign || (ExternalLayout && !InferAlignment))
Daniel Dunbarc6082fe2010-05-27 05:45:51 +00002252 return;
2253
Ken Dyck3263e092011-02-19 18:58:07 +00002254 if (NewAlignment > Alignment) {
2255 assert(llvm::isPowerOf2_32(NewAlignment.getQuantity() &&
2256 "Alignment not a power of 2"));
2257 Alignment = NewAlignment;
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00002258 }
2259
Ken Dyck3263e092011-02-19 18:58:07 +00002260 if (UnpackedNewAlignment > UnpackedAlignment) {
2261 assert(llvm::isPowerOf2_32(UnpackedNewAlignment.getQuantity() &&
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00002262 "Alignment not a power of 2"));
Ken Dyck3263e092011-02-19 18:58:07 +00002263 UnpackedAlignment = UnpackedNewAlignment;
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00002264 }
2265}
2266
Douglas Gregor394f7b62012-01-28 00:53:29 +00002267uint64_t
2268RecordLayoutBuilder::updateExternalFieldOffset(const FieldDecl *Field,
2269 uint64_t ComputedOffset) {
2270 assert(ExternalFieldOffsets.find(Field) != ExternalFieldOffsets.end() &&
2271 "Field does not have an external offset");
2272
2273 uint64_t ExternalFieldOffset = ExternalFieldOffsets[Field];
2274
2275 if (InferAlignment && ExternalFieldOffset < ComputedOffset) {
2276 // The externally-supplied field offset is before the field offset we
2277 // computed. Assume that the structure is packed.
Douglas Gregorf5967602012-10-26 22:31:14 +00002278 Alignment = CharUnits::One();
Douglas Gregor394f7b62012-01-28 00:53:29 +00002279 InferAlignment = false;
2280 }
2281
2282 // Use the externally-supplied field offset.
Benjamin Kramer7aaa1672012-08-31 22:14:25 +00002283 return ExternalFieldOffset;
2284}
2285
2286/// \brief Get diagnostic %select index for tag kind for
2287/// field padding diagnostic message.
2288/// WARNING: Indexes apply to particular diagnostics only!
2289///
2290/// \returns diagnostic %select index.
2291static unsigned getPaddingDiagFromTagKind(TagTypeKind Tag) {
2292 switch (Tag) {
2293 case TTK_Struct: return 0;
2294 case TTK_Interface: return 1;
2295 case TTK_Class: return 2;
2296 default: llvm_unreachable("Invalid tag kind for field padding diagnostic!");
2297 }
2298}
2299
2300void RecordLayoutBuilder::CheckFieldPadding(uint64_t Offset,
2301 uint64_t UnpaddedOffset,
2302 uint64_t UnpackedOffset,
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00002303 unsigned UnpackedAlign,
2304 bool isPacked,
2305 const FieldDecl *D) {
2306 // We let objc ivars without warning, objc interfaces generally are not used
2307 // for padding tricks.
2308 if (isa<ObjCIvarDecl>(D))
Anders Carlssonbda4c102009-07-18 20:20:21 +00002309 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002310
Ted Kremenekae5860e2011-09-06 19:40:45 +00002311 // Don't warn about structs created without a SourceLocation. This can
2312 // be done by clients of the AST, such as codegen.
2313 if (D->getLocation().isInvalid())
2314 return;
2315
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002316 unsigned CharBitNum = Context.getTargetInfo().getCharWidth();
Mike Stump1eb44332009-09-09 15:08:12 +00002317
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00002318 // Warn if padding was introduced to the struct/class.
2319 if (!IsUnion && Offset > UnpaddedOffset) {
2320 unsigned PadSize = Offset - UnpaddedOffset;
2321 bool InBits = true;
2322 if (PadSize % CharBitNum == 0) {
2323 PadSize = PadSize / CharBitNum;
2324 InBits = false;
Benjamin Kramer7aaa1672012-08-31 22:14:25 +00002325 }
2326 if (D->getIdentifier())
2327 Diag(D->getLocation(), diag::warn_padded_struct_field)
2328 << getPaddingDiagFromTagKind(D->getParent()->getTagKind())
2329 << Context.getTypeDeclType(D->getParent())
2330 << PadSize
2331 << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1) // plural or not
2332 << D->getIdentifier();
2333 else
2334 Diag(D->getLocation(), diag::warn_padded_struct_anon_field)
2335 << getPaddingDiagFromTagKind(D->getParent()->getTagKind())
2336 << Context.getTypeDeclType(D->getParent())
2337 << PadSize
2338 << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00002339 }
2340
2341 // Warn if we packed it unnecessarily. If the alignment is 1 byte don't
2342 // bother since there won't be alignment issues.
2343 if (isPacked && UnpackedAlign > CharBitNum && Offset == UnpackedOffset)
2344 Diag(D->getLocation(), diag::warn_unnecessary_packed)
2345 << D->getIdentifier();
Anders Carlssonbda4c102009-07-18 20:20:21 +00002346}
Mike Stump1eb44332009-09-09 15:08:12 +00002347
John McCalld5617ee2013-01-25 22:31:03 +00002348static const CXXMethodDecl *computeKeyFunction(ASTContext &Context,
2349 const CXXRecordDecl *RD) {
Daniel Dunbar8d8ab742010-04-19 20:44:53 +00002350 // If a class isn't polymorphic it doesn't have a key function.
Anders Carlssonf53df232009-12-07 04:35:11 +00002351 if (!RD->isPolymorphic())
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00002352 return 0;
Eli Friedman61eab882009-12-08 03:56:49 +00002353
Eli Friedmancb5d2d02011-06-10 21:53:06 +00002354 // A class that is not externally visible doesn't have a key function. (Or
Eli Friedman61eab882009-12-08 03:56:49 +00002355 // at least, there's no point to assigning a key function to such a class;
2356 // this doesn't affect the ABI.)
Rafael Espindola181e3ec2013-05-13 00:12:11 +00002357 if (!RD->isExternallyVisible())
Eli Friedman61eab882009-12-08 03:56:49 +00002358 return 0;
2359
Argyrios Kyrtzidis3bd5b6c2010-10-13 02:39:41 +00002360 // Template instantiations don't have key functions,see Itanium C++ ABI 5.2.6.
2361 // Same behavior as GCC.
2362 TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
2363 if (TSK == TSK_ImplicitInstantiation ||
2364 TSK == TSK_ExplicitInstantiationDefinition)
2365 return 0;
2366
John McCalld5617ee2013-01-25 22:31:03 +00002367 bool allowInlineFunctions =
2368 Context.getTargetInfo().getCXXABI().canKeyFunctionBeInline();
2369
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002370 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
2371 E = RD->method_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002372 const CXXMethodDecl *MD = *I;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002373
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00002374 if (!MD->isVirtual())
2375 continue;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002376
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00002377 if (MD->isPure())
2378 continue;
Eli Friedman61eab882009-12-08 03:56:49 +00002379
Anders Carlsson5ec02ae2009-12-02 17:15:43 +00002380 // Ignore implicit member functions, they are always marked as inline, but
2381 // they don't have a body until they're defined.
2382 if (MD->isImplicit())
2383 continue;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002384
Douglas Gregorbd6d6192010-01-05 19:06:31 +00002385 if (MD->isInlineSpecified())
2386 continue;
Eli Friedmand7d7f672009-12-06 20:50:05 +00002387
2388 if (MD->hasInlineBody())
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00002389 continue;
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002390
Benjamin Kramerc23aca42012-08-03 15:43:22 +00002391 // Ignore inline deleted or defaulted functions.
Benjamin Kramerf3fce802012-08-03 08:39:58 +00002392 if (!MD->isUserProvided())
2393 continue;
2394
John McCalld5617ee2013-01-25 22:31:03 +00002395 // In certain ABIs, ignore functions with out-of-line inline definitions.
2396 if (!allowInlineFunctions) {
2397 const FunctionDecl *Def;
2398 if (MD->hasBody(Def) && Def->isInlineSpecified())
2399 continue;
2400 }
2401
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00002402 // We found it.
2403 return MD;
2404 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002405
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00002406 return 0;
2407}
2408
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00002409DiagnosticBuilder
2410RecordLayoutBuilder::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00002411 return Context.getDiagnostics().Report(Loc, DiagID);
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +00002412}
2413
John McCalla438b2d2013-01-29 01:14:22 +00002414/// Does the target C++ ABI require us to skip over the tail-padding
2415/// of the given class (considering it as a base class) when allocating
2416/// objects?
2417static bool mustSkipTailPadding(TargetCXXABI ABI, const CXXRecordDecl *RD) {
2418 switch (ABI.getTailPaddingUseRules()) {
2419 case TargetCXXABI::AlwaysUseTailPadding:
2420 return false;
2421
2422 case TargetCXXABI::UseTailPaddingUnlessPOD03:
2423 // FIXME: To the extent that this is meant to cover the Itanium ABI
2424 // rules, we should implement the restrictions about over-sized
2425 // bitfields:
2426 //
2427 // http://mentorembedded.github.com/cxx-abi/abi.html#POD :
2428 // In general, a type is considered a POD for the purposes of
2429 // layout if it is a POD type (in the sense of ISO C++
2430 // [basic.types]). However, a POD-struct or POD-union (in the
2431 // sense of ISO C++ [class]) with a bitfield member whose
2432 // declared width is wider than the declared type of the
2433 // bitfield is not a POD for the purpose of layout. Similarly,
2434 // an array type is not a POD for the purpose of layout if the
2435 // element type of the array is not a POD for the purpose of
2436 // layout.
2437 //
2438 // Where references to the ISO C++ are made in this paragraph,
2439 // the Technical Corrigendum 1 version of the standard is
2440 // intended.
2441 return RD->isPOD();
2442
2443 case TargetCXXABI::UseTailPaddingUnlessPOD11:
2444 // This is equivalent to RD->getTypeForDecl().isCXX11PODType(),
2445 // but with a lot of abstraction penalty stripped off. This does
2446 // assume that these properties are set correctly even in C++98
2447 // mode; fortunately, that is true because we want to assign
2448 // consistently semantics to the type-traits intrinsics (or at
2449 // least as many of them as possible).
2450 return RD->isTrivial() && RD->isStandardLayout();
2451 }
2452
2453 llvm_unreachable("bad tail-padding use kind");
2454}
2455
Anders Carlsson1e641ce2010-05-26 04:56:53 +00002456/// getASTRecordLayout - Get or compute information about the layout of the
2457/// specified record (struct/union/class), which indicates its size and field
2458/// position information.
Jay Foad4ba2a172011-01-12 09:06:06 +00002459const ASTRecordLayout &
2460ASTContext::getASTRecordLayout(const RecordDecl *D) const {
John McCall65959352011-10-07 02:39:22 +00002461 // These asserts test different things. A record has a definition
2462 // as soon as we begin to parse the definition. That definition is
2463 // not a complete definition (which is what isDefinition() tests)
2464 // until we *finish* parsing the definition.
Sean Callananfd5a5f52012-02-08 00:04:52 +00002465
2466 if (D->hasExternalLexicalStorage() && !D->getDefinition())
2467 getExternalSource()->CompleteType(const_cast<RecordDecl*>(D));
2468
Anders Carlsson1e641ce2010-05-26 04:56:53 +00002469 D = D->getDefinition();
2470 assert(D && "Cannot get layout of forward declarations!");
John McCall5e1cdac2011-10-07 06:10:15 +00002471 assert(D->isCompleteDefinition() && "Cannot layout type before complete!");
Anders Carlsson1e641ce2010-05-26 04:56:53 +00002472
2473 // Look up this layout, if already laid out, return what we have.
2474 // Note that we can't save a reference to the entry because this function
2475 // is recursive.
2476 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
2477 if (Entry) return *Entry;
2478
Anders Carlsson2f64e372010-05-26 05:10:47 +00002479 const ASTRecordLayout *NewEntry;
2480
2481 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
Anders Carlsson261febd2010-05-27 00:07:01 +00002482 EmptySubobjectMap EmptySubobjects(*this, RD);
John McCall9da23522011-11-08 04:01:03 +00002483 RecordLayoutBuilder Builder(*this, &EmptySubobjects);
2484 Builder.Layout(RD);
Anders Carlsson58b16b62010-05-27 05:41:06 +00002485
John McCall9da23522011-11-08 04:01:03 +00002486 // MSVC gives the vb-table pointer an alignment equal to that of
2487 // the non-virtual part of the structure. That's an inherently
2488 // multi-pass operation. If our first pass doesn't give us
2489 // adequate alignment, try again with the specified minimum
2490 // alignment. This is *much* more maintainable than computing the
2491 // alignment in advance in a separately-coded pass; it's also
2492 // significantly more efficient in the common case where the
2493 // vb-table doesn't need extra padding.
2494 if (Builder.VBPtrOffset != CharUnits::fromQuantity(-1) &&
2495 (Builder.VBPtrOffset % Builder.NonVirtualAlignment) != 0) {
2496 Builder.resetWithTargetAlignment(Builder.NonVirtualAlignment);
2497 Builder.Layout(RD);
Eli Friedman2fe36362011-09-27 19:12:27 +00002498 }
2499
John McCalla438b2d2013-01-29 01:14:22 +00002500 // In certain situations, we are allowed to lay out objects in the
2501 // tail-padding of base classes. This is ABI-dependent.
2502 // FIXME: this should be stored in the record layout.
2503 bool skipTailPadding =
2504 mustSkipTailPadding(getTargetInfo().getCXXABI(), cast<CXXRecordDecl>(D));
Anders Carlsson2f64e372010-05-26 05:10:47 +00002505
2506 // FIXME: This should be done in FinalizeLayout.
Ken Dyckf079b732011-02-28 02:01:38 +00002507 CharUnits DataSize =
John McCalla438b2d2013-01-29 01:14:22 +00002508 skipTailPadding ? Builder.getSize() : Builder.getDataSize();
Ken Dyckf079b732011-02-28 02:01:38 +00002509 CharUnits NonVirtualSize =
John McCalla438b2d2013-01-29 01:14:22 +00002510 skipTailPadding ? DataSize : Builder.NonVirtualSize;
Anders Carlsson2f64e372010-05-26 05:10:47 +00002511
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00002512 NewEntry =
John McCall9da23522011-11-08 04:01:03 +00002513 new (*this) ASTRecordLayout(*this, Builder.getSize(),
2514 Builder.Alignment,
John McCall441c6232012-05-01 08:55:32 +00002515 Builder.HasOwnVFPtr,
John McCall9da23522011-11-08 04:01:03 +00002516 Builder.VBPtrOffset,
Ken Dyckf079b732011-02-28 02:01:38 +00002517 DataSize,
John McCall9da23522011-11-08 04:01:03 +00002518 Builder.FieldOffsets.data(),
2519 Builder.FieldOffsets.size(),
Ken Dycka1fdb0b2011-02-16 01:52:01 +00002520 NonVirtualSize,
John McCall9da23522011-11-08 04:01:03 +00002521 Builder.NonVirtualAlignment,
Anders Carlsson261febd2010-05-27 00:07:01 +00002522 EmptySubobjects.SizeOfLargestEmptySubobject,
John McCall9da23522011-11-08 04:01:03 +00002523 Builder.PrimaryBase,
2524 Builder.PrimaryBaseIsVirtual,
2525 Builder.Bases, Builder.VBases);
Anders Carlsson2f64e372010-05-26 05:10:47 +00002526 } else {
John McCall9da23522011-11-08 04:01:03 +00002527 RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0);
Anders Carlsson2f64e372010-05-26 05:10:47 +00002528 Builder.Layout(D);
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00002529
Anders Carlsson2f64e372010-05-26 05:10:47 +00002530 NewEntry =
Ken Dyckf079b732011-02-28 02:01:38 +00002531 new (*this) ASTRecordLayout(*this, Builder.getSize(),
Ken Dyckea7f6c22011-02-16 02:05:21 +00002532 Builder.Alignment,
Ken Dyckf079b732011-02-28 02:01:38 +00002533 Builder.getSize(),
Anders Carlsson2f64e372010-05-26 05:10:47 +00002534 Builder.FieldOffsets.data(),
2535 Builder.FieldOffsets.size());
2536 }
2537
Anders Carlsson1e641ce2010-05-26 04:56:53 +00002538 ASTRecordLayouts[D] = NewEntry;
2539
David Blaikie4e4d0842012-03-11 07:00:24 +00002540 if (getLangOpts().DumpRecordLayouts) {
Anders Carlsson1e641ce2010-05-26 04:56:53 +00002541 llvm::errs() << "\n*** Dumping AST Record Layout\n";
David Blaikie4e4d0842012-03-11 07:00:24 +00002542 DumpRecordLayout(D, llvm::errs(), getLangOpts().DumpRecordLayoutsSimple);
Anders Carlsson1e641ce2010-05-26 04:56:53 +00002543 }
2544
2545 return *NewEntry;
2546}
2547
John McCalld5617ee2013-01-25 22:31:03 +00002548const CXXMethodDecl *ASTContext::getCurrentKeyFunction(const CXXRecordDecl *RD) {
2549 assert(RD->getDefinition() && "Cannot get key function for forward decl!");
Anders Carlsson1e641ce2010-05-26 04:56:53 +00002550 RD = cast<CXXRecordDecl>(RD->getDefinition());
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00002551
John McCalld5617ee2013-01-25 22:31:03 +00002552 const CXXMethodDecl *&entry = KeyFunctions[RD];
2553 if (!entry) {
2554 entry = computeKeyFunction(*this, RD);
2555 }
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00002556
John McCalld5617ee2013-01-25 22:31:03 +00002557 return entry;
2558}
2559
2560void ASTContext::setNonKeyFunction(const CXXMethodDecl *method) {
2561 assert(method == method->getFirstDeclaration() &&
2562 "not working with method declaration from class definition");
2563
2564 // Look up the cache entry. Since we're working with the first
2565 // declaration, its parent must be the class definition, which is
2566 // the correct key for the KeyFunctions hash.
2567 llvm::DenseMap<const CXXRecordDecl*, const CXXMethodDecl*>::iterator
2568 i = KeyFunctions.find(method->getParent());
2569
2570 // If it's not cached, there's nothing to do.
2571 if (i == KeyFunctions.end()) return;
2572
2573 // If it is cached, check whether it's the target method, and if so,
2574 // remove it from the cache.
2575 if (i->second == method) {
2576 // FIXME: remember that we did this for module / chained PCH state?
2577 KeyFunctions.erase(i);
2578 }
Anders Carlsson1e641ce2010-05-26 04:56:53 +00002579}
2580
Richard Smith2d6a5672012-01-14 04:30:29 +00002581static uint64_t getFieldOffset(const ASTContext &C, const FieldDecl *FD) {
2582 const ASTRecordLayout &Layout = C.getASTRecordLayout(FD->getParent());
2583 return Layout.getFieldOffset(FD->getFieldIndex());
2584}
2585
2586uint64_t ASTContext::getFieldOffset(const ValueDecl *VD) const {
2587 uint64_t OffsetInBits;
2588 if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) {
2589 OffsetInBits = ::getFieldOffset(*this, FD);
2590 } else {
2591 const IndirectFieldDecl *IFD = cast<IndirectFieldDecl>(VD);
2592
2593 OffsetInBits = 0;
2594 for (IndirectFieldDecl::chain_iterator CI = IFD->chain_begin(),
2595 CE = IFD->chain_end();
2596 CI != CE; ++CI)
2597 OffsetInBits += ::getFieldOffset(*this, cast<FieldDecl>(*CI));
2598 }
2599
2600 return OffsetInBits;
2601}
2602
Eric Christopher68395a72011-10-05 06:00:51 +00002603/// getObjCLayout - Get or compute information about the layout of the
2604/// given interface.
Anders Carlsson1e641ce2010-05-26 04:56:53 +00002605///
2606/// \param Impl - If given, also include the layout of the interface's
2607/// implementation. This may differ by including synthesized ivars.
2608const ASTRecordLayout &
2609ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
Jay Foad4ba2a172011-01-12 09:06:06 +00002610 const ObjCImplementationDecl *Impl) const {
Douglas Gregore7aa27a2011-12-20 15:50:13 +00002611 // Retrieve the definition
Sean Callanancad313b2012-03-15 16:33:08 +00002612 if (D->hasExternalLexicalStorage() && !D->getDefinition())
2613 getExternalSource()->CompleteType(const_cast<ObjCInterfaceDecl*>(D));
Douglas Gregore7aa27a2011-12-20 15:50:13 +00002614 D = D->getDefinition();
2615 assert(D && D->isThisDeclarationADefinition() && "Invalid interface decl!");
Anders Carlsson1e641ce2010-05-26 04:56:53 +00002616
2617 // Look up this layout, if already laid out, return what we have.
Roman Divacky31ba6132012-09-06 15:59:27 +00002618 const ObjCContainerDecl *Key =
2619 Impl ? (const ObjCContainerDecl*) Impl : (const ObjCContainerDecl*) D;
Anders Carlsson1e641ce2010-05-26 04:56:53 +00002620 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
2621 return *Entry;
2622
2623 // Add in synthesized ivar count if laying out an implementation.
2624 if (Impl) {
2625 unsigned SynthCount = CountNonClassIvars(D);
2626 // If there aren't any sythesized ivars then reuse the interface
2627 // entry. Note we can't cache this because we simply free all
2628 // entries later; however we shouldn't look up implementations
2629 // frequently.
2630 if (SynthCount == 0)
2631 return getObjCLayout(D, 0);
2632 }
2633
John McCall9da23522011-11-08 04:01:03 +00002634 RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0);
Anders Carlsson36cdc612010-05-26 05:04:25 +00002635 Builder.Layout(D);
2636
Anders Carlsson1e641ce2010-05-26 04:56:53 +00002637 const ASTRecordLayout *NewEntry =
Ken Dyckf079b732011-02-28 02:01:38 +00002638 new (*this) ASTRecordLayout(*this, Builder.getSize(),
Ken Dyckea7f6c22011-02-16 02:05:21 +00002639 Builder.Alignment,
Ken Dyckf079b732011-02-28 02:01:38 +00002640 Builder.getDataSize(),
Anders Carlsson36cdc612010-05-26 05:04:25 +00002641 Builder.FieldOffsets.data(),
2642 Builder.FieldOffsets.size());
Daniel Dunbar0aa7edb2010-05-27 02:25:46 +00002643
Anders Carlsson1e641ce2010-05-26 04:56:53 +00002644 ObjCLayouts[Key] = NewEntry;
2645
2646 return *NewEntry;
2647}
2648
Chris Lattner5f9e2722011-07-23 10:55:15 +00002649static void PrintOffset(raw_ostream &OS,
Anders Carlsson3069a0d2010-10-31 23:45:59 +00002650 CharUnits Offset, unsigned IndentLevel) {
Benjamin Kramere4ebbf72011-11-05 09:02:52 +00002651 OS << llvm::format("%4" PRId64 " | ", (int64_t)Offset.getQuantity());
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002652 OS.indent(IndentLevel * 2);
2653}
2654
Eli Bendersky0ee69cc2012-12-08 00:07:24 +00002655static void PrintIndentNoOffset(raw_ostream &OS, unsigned IndentLevel) {
2656 OS << " | ";
2657 OS.indent(IndentLevel * 2);
2658}
2659
Chris Lattner5f9e2722011-07-23 10:55:15 +00002660static void DumpCXXRecordLayout(raw_ostream &OS,
Jay Foad4ba2a172011-01-12 09:06:06 +00002661 const CXXRecordDecl *RD, const ASTContext &C,
Anders Carlsson3069a0d2010-10-31 23:45:59 +00002662 CharUnits Offset,
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002663 unsigned IndentLevel,
2664 const char* Description,
2665 bool IncludeVirtualBases) {
Anders Carlsson3069a0d2010-10-31 23:45:59 +00002666 const ASTRecordLayout &Layout = C.getASTRecordLayout(RD);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002667
2668 PrintOffset(OS, Offset, IndentLevel);
Dan Gohmancb421fa2010-04-19 16:39:44 +00002669 OS << C.getTypeDeclType(const_cast<CXXRecordDecl *>(RD)).getAsString();
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002670 if (Description)
2671 OS << ' ' << Description;
2672 if (RD->isEmpty())
2673 OS << " (empty)";
2674 OS << '\n';
2675
2676 IndentLevel++;
2677
Anders Carlsson3069a0d2010-10-31 23:45:59 +00002678 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
John McCall441c6232012-05-01 08:55:32 +00002679 bool HasVfptr = Layout.hasOwnVFPtr();
Eli Friedman2fe36362011-09-27 19:12:27 +00002680 bool HasVbptr = Layout.getVBPtrOffset() != CharUnits::fromQuantity(-1);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002681
2682 // Vtable pointer.
Eli Friedman227e4832011-10-21 22:49:56 +00002683 if (RD->isDynamicClass() && !PrimaryBase &&
John McCallb8b2c9d2013-01-25 22:30:49 +00002684 !C.getTargetInfo().getCXXABI().isMicrosoft()) {
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002685 PrintOffset(OS, Offset, IndentLevel);
Benjamin Kramerb8989f22011-10-14 18:45:37 +00002686 OS << '(' << *RD << " vtable pointer)\n";
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002687 }
Eli Friedman2fe36362011-09-27 19:12:27 +00002688
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002689 // Dump (non-virtual) bases
2690 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
2691 E = RD->bases_end(); I != E; ++I) {
2692 assert(!I->getType()->isDependentType() &&
2693 "Cannot layout class with dependent bases.");
2694 if (I->isVirtual())
2695 continue;
2696
2697 const CXXRecordDecl *Base =
2698 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
2699
Anders Carlsson3069a0d2010-10-31 23:45:59 +00002700 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base);
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002701
2702 DumpCXXRecordLayout(OS, Base, C, BaseOffset, IndentLevel,
2703 Base == PrimaryBase ? "(primary base)" : "(base)",
2704 /*IncludeVirtualBases=*/false);
2705 }
Eli Friedman227e4832011-10-21 22:49:56 +00002706
2707 // vfptr and vbptr (for Microsoft C++ ABI)
2708 if (HasVfptr) {
John McCall441c6232012-05-01 08:55:32 +00002709 PrintOffset(OS, Offset, IndentLevel);
Eli Friedman227e4832011-10-21 22:49:56 +00002710 OS << '(' << *RD << " vftable pointer)\n";
2711 }
Eli Friedman2fe36362011-09-27 19:12:27 +00002712 if (HasVbptr) {
2713 PrintOffset(OS, Offset + Layout.getVBPtrOffset(), IndentLevel);
Benjamin Kramerb8989f22011-10-14 18:45:37 +00002714 OS << '(' << *RD << " vbtable pointer)\n";
Eli Friedman2fe36362011-09-27 19:12:27 +00002715 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002716
2717 // Dump fields.
2718 uint64_t FieldNo = 0;
2719 for (CXXRecordDecl::field_iterator I = RD->field_begin(),
2720 E = RD->field_end(); I != E; ++I, ++FieldNo) {
David Blaikie581deb32012-06-06 20:45:41 +00002721 const FieldDecl &Field = **I;
Anders Carlsson3069a0d2010-10-31 23:45:59 +00002722 CharUnits FieldOffset = Offset +
Ken Dyckfb1e3bc2011-01-18 01:56:16 +00002723 C.toCharUnitsFromBits(Layout.getFieldOffset(FieldNo));
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002724
David Blaikie262bc182012-04-30 02:36:29 +00002725 if (const RecordType *RT = Field.getType()->getAs<RecordType>()) {
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002726 if (const CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
2727 DumpCXXRecordLayout(OS, D, C, FieldOffset, IndentLevel,
David Blaikie262bc182012-04-30 02:36:29 +00002728 Field.getName().data(),
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002729 /*IncludeVirtualBases=*/true);
2730 continue;
2731 }
2732 }
2733
2734 PrintOffset(OS, FieldOffset, IndentLevel);
David Blaikie262bc182012-04-30 02:36:29 +00002735 OS << Field.getType().getAsString() << ' ' << Field << '\n';
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002736 }
2737
2738 if (!IncludeVirtualBases)
2739 return;
2740
2741 // Dump virtual bases.
John McCall441c6232012-05-01 08:55:32 +00002742 const ASTRecordLayout::VBaseOffsetsMapTy &vtordisps =
2743 Layout.getVBaseOffsetsMap();
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002744 for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
2745 E = RD->vbases_end(); I != E; ++I) {
2746 assert(I->isVirtual() && "Found non-virtual class!");
2747 const CXXRecordDecl *VBase =
2748 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
2749
Anders Carlsson3069a0d2010-10-31 23:45:59 +00002750 CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBase);
John McCall441c6232012-05-01 08:55:32 +00002751
2752 if (vtordisps.find(VBase)->second.hasVtorDisp()) {
2753 PrintOffset(OS, VBaseOffset - CharUnits::fromQuantity(4), IndentLevel);
2754 OS << "(vtordisp for vbase " << *VBase << ")\n";
2755 }
2756
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002757 DumpCXXRecordLayout(OS, VBase, C, VBaseOffset, IndentLevel,
2758 VBase == PrimaryBase ?
2759 "(primary virtual base)" : "(virtual base)",
2760 /*IncludeVirtualBases=*/false);
2761 }
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002762
Eli Bendersky0ee69cc2012-12-08 00:07:24 +00002763 PrintIndentNoOffset(OS, IndentLevel - 1);
2764 OS << "[sizeof=" << Layout.getSize().getQuantity();
Ken Dyckec299032011-02-11 02:20:09 +00002765 OS << ", dsize=" << Layout.getDataSize().getQuantity();
Ken Dyckdac54c12011-02-15 02:32:40 +00002766 OS << ", align=" << Layout.getAlignment().getQuantity() << '\n';
Eli Bendersky0ee69cc2012-12-08 00:07:24 +00002767
2768 PrintIndentNoOffset(OS, IndentLevel - 1);
2769 OS << " nvsize=" << Layout.getNonVirtualSize().getQuantity();
2770 OS << ", nvalign=" << Layout.getNonVirtualAlign().getQuantity() << "]\n";
Daniel Dunbarbf9e48c2010-04-08 02:59:49 +00002771 OS << '\n';
2772}
Daniel Dunbar8d8ab742010-04-19 20:44:53 +00002773
2774void ASTContext::DumpRecordLayout(const RecordDecl *RD,
Douglas Gregor453dbcb2012-01-26 07:55:45 +00002775 raw_ostream &OS,
2776 bool Simple) const {
Daniel Dunbar8d8ab742010-04-19 20:44:53 +00002777 const ASTRecordLayout &Info = getASTRecordLayout(RD);
2778
2779 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
Douglas Gregor453dbcb2012-01-26 07:55:45 +00002780 if (!Simple)
2781 return DumpCXXRecordLayout(OS, CXXRD, *this, CharUnits(), 0, 0,
2782 /*IncludeVirtualBases=*/true);
Daniel Dunbar8d8ab742010-04-19 20:44:53 +00002783
2784 OS << "Type: " << getTypeDeclType(RD).getAsString() << "\n";
Douglas Gregor453dbcb2012-01-26 07:55:45 +00002785 if (!Simple) {
2786 OS << "Record: ";
2787 RD->dump();
2788 }
Daniel Dunbar8d8ab742010-04-19 20:44:53 +00002789 OS << "\nLayout: ";
2790 OS << "<ASTRecordLayout\n";
Ken Dyckdd76a9a2011-02-11 01:54:29 +00002791 OS << " Size:" << toBits(Info.getSize()) << "\n";
Ken Dyckec299032011-02-11 02:20:09 +00002792 OS << " DataSize:" << toBits(Info.getDataSize()) << "\n";
Ken Dyckdac54c12011-02-15 02:32:40 +00002793 OS << " Alignment:" << toBits(Info.getAlignment()) << "\n";
Daniel Dunbar8d8ab742010-04-19 20:44:53 +00002794 OS << " FieldOffsets: [";
2795 for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i) {
2796 if (i) OS << ", ";
2797 OS << Info.getFieldOffset(i);
2798 }
2799 OS << "]>\n";
2800}