1 //=== RecordLayoutBuilder.cpp - Helper class for building record layouts ---==//
2 //
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
10 #include "clang/AST/RecordLayout.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/AST/Attr.h"
13 #include "clang/AST/CXXInheritance.h"
14 #include "clang/AST/Decl.h"
15 #include "clang/AST/DeclCXX.h"
16 #include "clang/AST/DeclObjC.h"
17 #include "clang/AST/Expr.h"
18 #include "clang/Basic/TargetInfo.h"
19 #include "clang/Sema/SemaDiagnostic.h"
20 #include "llvm/ADT/SmallSet.h"
21 #include "llvm/Support/CrashRecoveryContext.h"
22 #include "llvm/Support/Format.h"
23 #include "llvm/Support/MathExtras.h"
24
25 using namespace clang;
26
27 namespace {
28
29 /// 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.
40 struct BaseSubobjectInfo {
41 /// Class - The class for this base info.
42 const CXXRecordDecl *Class;
43
44 /// IsVirtual - Whether the BaseInfo represents a virtual base or not.
45 bool IsVirtual;
46
47 /// Bases - Information about the base subobjects.
48 SmallVector<BaseSubobjectInfo*, 4> Bases;
49
50 /// PrimaryVirtualBaseInfo - Holds the base info for the primary virtual base
51 /// of this base info (if one exists).
52 BaseSubobjectInfo *PrimaryVirtualBaseInfo;
53
54 // FIXME: Document.
55 const BaseSubobjectInfo *Derived;
56 };
57
58 /// EmptySubobjectMap - Keeps track of which empty subobjects exist at different
59 /// offsets while laying out a C++ class.
60 class EmptySubobjectMap {
61 const ASTContext &Context;
62 uint64_t CharWidth;
63
64 /// Class - The class whose empty entries we're keeping track of.
65 const CXXRecordDecl *Class;
66
67 /// EmptyClassOffsets - A map from offsets to empty record decls.
68 typedef llvm::TinyPtrVector<const CXXRecordDecl *> ClassVectorTy;
69 typedef llvm::DenseMap<CharUnits, ClassVectorTy> EmptyClassOffsetsMapTy;
70 EmptyClassOffsetsMapTy EmptyClassOffsets;
71
72 /// MaxEmptyClassOffset - The highest offset known to contain an empty
73 /// base subobject.
74 CharUnits MaxEmptyClassOffset;
75
76 /// ComputeEmptySubobjectSizes - Compute the size of the largest base or
77 /// member subobject that is empty.
78 void ComputeEmptySubobjectSizes();
79
80 void AddSubobjectAtOffset(const CXXRecordDecl *RD, CharUnits Offset);
81
82 void UpdateEmptyBaseSubobjects(const BaseSubobjectInfo *Info,
83 CharUnits Offset, bool PlacingEmptyBase);
84
85 void UpdateEmptyFieldSubobjects(const CXXRecordDecl *RD,
86 const CXXRecordDecl *Class,
87 CharUnits Offset);
88 void UpdateEmptyFieldSubobjects(const FieldDecl *FD, CharUnits Offset);
89
90 /// AnyEmptySubobjectsBeyondOffset - Returns whether there are any empty
91 /// subobjects beyond the given offset.
AnyEmptySubobjectsBeyondOffset(CharUnits Offset) const92 bool AnyEmptySubobjectsBeyondOffset(CharUnits Offset) const {
93 return Offset <= MaxEmptyClassOffset;
94 }
95
96 CharUnits
getFieldOffset(const ASTRecordLayout & Layout,unsigned FieldNo) const97 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
102 return Context.toCharUnitsFromBits(FieldOffset);
103 }
104
105 protected:
106 bool CanPlaceSubobjectAtOffset(const CXXRecordDecl *RD,
107 CharUnits Offset) const;
108
109 bool CanPlaceBaseSubobjectAtOffset(const BaseSubobjectInfo *Info,
110 CharUnits Offset);
111
112 bool CanPlaceFieldSubobjectAtOffset(const CXXRecordDecl *RD,
113 const CXXRecordDecl *Class,
114 CharUnits Offset) const;
115 bool CanPlaceFieldSubobjectAtOffset(const FieldDecl *FD,
116 CharUnits Offset) const;
117
118 public:
119 /// This holds the size of the largest empty subobject (either a base
120 /// or a member). Will be zero if the record being built doesn't contain
121 /// any empty classes.
122 CharUnits SizeOfLargestEmptySubobject;
123
EmptySubobjectMap(const ASTContext & Context,const CXXRecordDecl * Class)124 EmptySubobjectMap(const ASTContext &Context, const CXXRecordDecl *Class)
125 : Context(Context), CharWidth(Context.getCharWidth()), Class(Class) {
126 ComputeEmptySubobjectSizes();
127 }
128
129 /// CanPlaceBaseAtOffset - Return whether the given base class can be placed
130 /// at the given offset.
131 /// Returns false if placing the record will result in two components
132 /// (direct or indirect) of the same type having the same offset.
133 bool CanPlaceBaseAtOffset(const BaseSubobjectInfo *Info,
134 CharUnits Offset);
135
136 /// CanPlaceFieldAtOffset - Return whether a field can be placed at the given
137 /// offset.
138 bool CanPlaceFieldAtOffset(const FieldDecl *FD, CharUnits Offset);
139 };
140
ComputeEmptySubobjectSizes()141 void EmptySubobjectMap::ComputeEmptySubobjectSizes() {
142 // Check the bases.
143 for (const auto &I : Class->bases()) {
144 const CXXRecordDecl *BaseDecl = I.getType()->getAsCXXRecordDecl();
145
146 CharUnits EmptySize;
147 const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl);
148 if (BaseDecl->isEmpty()) {
149 // If the class decl is empty, get its size.
150 EmptySize = Layout.getSize();
151 } else {
152 // Otherwise, we get the largest empty subobject for the decl.
153 EmptySize = Layout.getSizeOfLargestEmptySubobject();
154 }
155
156 if (EmptySize > SizeOfLargestEmptySubobject)
157 SizeOfLargestEmptySubobject = EmptySize;
158 }
159
160 // Check the fields.
161 for (const auto *I : Class->fields()) {
162 const RecordType *RT =
163 Context.getBaseElementType(I->getType())->getAs<RecordType>();
164
165 // We only care about record types.
166 if (!RT)
167 continue;
168
169 CharUnits EmptySize;
170 const CXXRecordDecl *MemberDecl = RT->getAsCXXRecordDecl();
171 const ASTRecordLayout &Layout = Context.getASTRecordLayout(MemberDecl);
172 if (MemberDecl->isEmpty()) {
173 // If the class decl is empty, get its size.
174 EmptySize = Layout.getSize();
175 } else {
176 // Otherwise, we get the largest empty subobject for the decl.
177 EmptySize = Layout.getSizeOfLargestEmptySubobject();
178 }
179
180 if (EmptySize > SizeOfLargestEmptySubobject)
181 SizeOfLargestEmptySubobject = EmptySize;
182 }
183 }
184
185 bool
CanPlaceSubobjectAtOffset(const CXXRecordDecl * RD,CharUnits Offset) const186 EmptySubobjectMap::CanPlaceSubobjectAtOffset(const CXXRecordDecl *RD,
187 CharUnits Offset) const {
188 // We only need to check empty bases.
189 if (!RD->isEmpty())
190 return true;
191
192 EmptyClassOffsetsMapTy::const_iterator I = EmptyClassOffsets.find(Offset);
193 if (I == EmptyClassOffsets.end())
194 return true;
195
196 const ClassVectorTy& Classes = I->second;
197 if (std::find(Classes.begin(), Classes.end(), RD) == Classes.end())
198 return true;
199
200 // There is already an empty class of the same type at this offset.
201 return false;
202 }
203
AddSubobjectAtOffset(const CXXRecordDecl * RD,CharUnits Offset)204 void EmptySubobjectMap::AddSubobjectAtOffset(const CXXRecordDecl *RD,
205 CharUnits Offset) {
206 // We only care about empty bases.
207 if (!RD->isEmpty())
208 return;
209
210 // If we have empty structures inside a union, we can assign both
211 // the same offset. Just avoid pushing them twice in the list.
212 ClassVectorTy& Classes = EmptyClassOffsets[Offset];
213 if (std::find(Classes.begin(), Classes.end(), RD) != Classes.end())
214 return;
215
216 Classes.push_back(RD);
217
218 // Update the empty class offset.
219 if (Offset > MaxEmptyClassOffset)
220 MaxEmptyClassOffset = Offset;
221 }
222
223 bool
CanPlaceBaseSubobjectAtOffset(const BaseSubobjectInfo * Info,CharUnits Offset)224 EmptySubobjectMap::CanPlaceBaseSubobjectAtOffset(const BaseSubobjectInfo *Info,
225 CharUnits Offset) {
226 // We don't have to keep looking past the maximum offset that's known to
227 // contain an empty class.
228 if (!AnyEmptySubobjectsBeyondOffset(Offset))
229 return true;
230
231 if (!CanPlaceSubobjectAtOffset(Info->Class, Offset))
232 return false;
233
234 // Traverse all non-virtual bases.
235 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class);
236 for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) {
237 BaseSubobjectInfo* Base = Info->Bases[I];
238 if (Base->IsVirtual)
239 continue;
240
241 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class);
242
243 if (!CanPlaceBaseSubobjectAtOffset(Base, BaseOffset))
244 return false;
245 }
246
247 if (Info->PrimaryVirtualBaseInfo) {
248 BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo;
249
250 if (Info == PrimaryVirtualBaseInfo->Derived) {
251 if (!CanPlaceBaseSubobjectAtOffset(PrimaryVirtualBaseInfo, Offset))
252 return false;
253 }
254 }
255
256 // Traverse all member variables.
257 unsigned FieldNo = 0;
258 for (CXXRecordDecl::field_iterator I = Info->Class->field_begin(),
259 E = Info->Class->field_end(); I != E; ++I, ++FieldNo) {
260 if (I->isBitField())
261 continue;
262
263 CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo);
264 if (!CanPlaceFieldSubobjectAtOffset(*I, FieldOffset))
265 return false;
266 }
267
268 return true;
269 }
270
UpdateEmptyBaseSubobjects(const BaseSubobjectInfo * Info,CharUnits Offset,bool PlacingEmptyBase)271 void EmptySubobjectMap::UpdateEmptyBaseSubobjects(const BaseSubobjectInfo *Info,
272 CharUnits Offset,
273 bool PlacingEmptyBase) {
274 if (!PlacingEmptyBase && Offset >= SizeOfLargestEmptySubobject) {
275 // We know that the only empty subobjects that can conflict with empty
276 // subobject of non-empty bases, are empty bases that can be placed at
277 // offset zero. Because of this, we only need to keep track of empty base
278 // subobjects with offsets less than the size of the largest empty
279 // subobject for our class.
280 return;
281 }
282
283 AddSubobjectAtOffset(Info->Class, Offset);
284
285 // Traverse all non-virtual bases.
286 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class);
287 for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) {
288 BaseSubobjectInfo* Base = Info->Bases[I];
289 if (Base->IsVirtual)
290 continue;
291
292 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class);
293 UpdateEmptyBaseSubobjects(Base, BaseOffset, PlacingEmptyBase);
294 }
295
296 if (Info->PrimaryVirtualBaseInfo) {
297 BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo;
298
299 if (Info == PrimaryVirtualBaseInfo->Derived)
300 UpdateEmptyBaseSubobjects(PrimaryVirtualBaseInfo, Offset,
301 PlacingEmptyBase);
302 }
303
304 // Traverse all member variables.
305 unsigned FieldNo = 0;
306 for (CXXRecordDecl::field_iterator I = Info->Class->field_begin(),
307 E = Info->Class->field_end(); I != E; ++I, ++FieldNo) {
308 if (I->isBitField())
309 continue;
310
311 CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo);
312 UpdateEmptyFieldSubobjects(*I, FieldOffset);
313 }
314 }
315
CanPlaceBaseAtOffset(const BaseSubobjectInfo * Info,CharUnits Offset)316 bool EmptySubobjectMap::CanPlaceBaseAtOffset(const BaseSubobjectInfo *Info,
317 CharUnits Offset) {
318 // If we know this class doesn't have any empty subobjects we don't need to
319 // bother checking.
320 if (SizeOfLargestEmptySubobject.isZero())
321 return true;
322
323 if (!CanPlaceBaseSubobjectAtOffset(Info, Offset))
324 return false;
325
326 // We are able to place the base at this offset. Make sure to update the
327 // empty base subobject map.
328 UpdateEmptyBaseSubobjects(Info, Offset, Info->Class->isEmpty());
329 return true;
330 }
331
332 bool
CanPlaceFieldSubobjectAtOffset(const CXXRecordDecl * RD,const CXXRecordDecl * Class,CharUnits Offset) const333 EmptySubobjectMap::CanPlaceFieldSubobjectAtOffset(const CXXRecordDecl *RD,
334 const CXXRecordDecl *Class,
335 CharUnits Offset) const {
336 // We don't have to keep looking past the maximum offset that's known to
337 // contain an empty class.
338 if (!AnyEmptySubobjectsBeyondOffset(Offset))
339 return true;
340
341 if (!CanPlaceSubobjectAtOffset(RD, Offset))
342 return false;
343
344 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
345
346 // Traverse all non-virtual bases.
347 for (const auto &I : RD->bases()) {
348 if (I.isVirtual())
349 continue;
350
351 const CXXRecordDecl *BaseDecl = I.getType()->getAsCXXRecordDecl();
352
353 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl);
354 if (!CanPlaceFieldSubobjectAtOffset(BaseDecl, Class, BaseOffset))
355 return false;
356 }
357
358 if (RD == Class) {
359 // This is the most derived class, traverse virtual bases as well.
360 for (const auto &I : RD->vbases()) {
361 const CXXRecordDecl *VBaseDecl = I.getType()->getAsCXXRecordDecl();
362
363 CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBaseDecl);
364 if (!CanPlaceFieldSubobjectAtOffset(VBaseDecl, Class, VBaseOffset))
365 return false;
366 }
367 }
368
369 // Traverse all member variables.
370 unsigned FieldNo = 0;
371 for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
372 I != E; ++I, ++FieldNo) {
373 if (I->isBitField())
374 continue;
375
376 CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo);
377
378 if (!CanPlaceFieldSubobjectAtOffset(*I, FieldOffset))
379 return false;
380 }
381
382 return true;
383 }
384
385 bool
CanPlaceFieldSubobjectAtOffset(const FieldDecl * FD,CharUnits Offset) const386 EmptySubobjectMap::CanPlaceFieldSubobjectAtOffset(const FieldDecl *FD,
387 CharUnits Offset) const {
388 // We don't have to keep looking past the maximum offset that's known to
389 // contain an empty class.
390 if (!AnyEmptySubobjectsBeyondOffset(Offset))
391 return true;
392
393 QualType T = FD->getType();
394 if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl())
395 return CanPlaceFieldSubobjectAtOffset(RD, RD, Offset);
396
397 // If we have an array type we need to look at every element.
398 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) {
399 QualType ElemTy = Context.getBaseElementType(AT);
400 const RecordType *RT = ElemTy->getAs<RecordType>();
401 if (!RT)
402 return true;
403
404 const CXXRecordDecl *RD = RT->getAsCXXRecordDecl();
405 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
406
407 uint64_t NumElements = Context.getConstantArrayElementCount(AT);
408 CharUnits ElementOffset = Offset;
409 for (uint64_t I = 0; I != NumElements; ++I) {
410 // We don't have to keep looking past the maximum offset that's known to
411 // contain an empty class.
412 if (!AnyEmptySubobjectsBeyondOffset(ElementOffset))
413 return true;
414
415 if (!CanPlaceFieldSubobjectAtOffset(RD, RD, ElementOffset))
416 return false;
417
418 ElementOffset += Layout.getSize();
419 }
420 }
421
422 return true;
423 }
424
425 bool
CanPlaceFieldAtOffset(const FieldDecl * FD,CharUnits Offset)426 EmptySubobjectMap::CanPlaceFieldAtOffset(const FieldDecl *FD,
427 CharUnits Offset) {
428 if (!CanPlaceFieldSubobjectAtOffset(FD, Offset))
429 return false;
430
431 // We are able to place the member variable at this offset.
432 // Make sure to update the empty base subobject map.
433 UpdateEmptyFieldSubobjects(FD, Offset);
434 return true;
435 }
436
UpdateEmptyFieldSubobjects(const CXXRecordDecl * RD,const CXXRecordDecl * Class,CharUnits Offset)437 void EmptySubobjectMap::UpdateEmptyFieldSubobjects(const CXXRecordDecl *RD,
438 const CXXRecordDecl *Class,
439 CharUnits Offset) {
440 // We know that the only empty subobjects that can conflict with empty
441 // field subobjects are subobjects of empty bases that can be placed at offset
442 // zero. Because of this, we only need to keep track of empty field
443 // subobjects with offsets less than the size of the largest empty
444 // subobject for our class.
445 if (Offset >= SizeOfLargestEmptySubobject)
446 return;
447
448 AddSubobjectAtOffset(RD, Offset);
449
450 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
451
452 // Traverse all non-virtual bases.
453 for (const auto &I : RD->bases()) {
454 if (I.isVirtual())
455 continue;
456
457 const CXXRecordDecl *BaseDecl = I.getType()->getAsCXXRecordDecl();
458
459 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(BaseDecl);
460 UpdateEmptyFieldSubobjects(BaseDecl, Class, BaseOffset);
461 }
462
463 if (RD == Class) {
464 // This is the most derived class, traverse virtual bases as well.
465 for (const auto &I : RD->vbases()) {
466 const CXXRecordDecl *VBaseDecl = I.getType()->getAsCXXRecordDecl();
467
468 CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBaseDecl);
469 UpdateEmptyFieldSubobjects(VBaseDecl, Class, VBaseOffset);
470 }
471 }
472
473 // Traverse all member variables.
474 unsigned FieldNo = 0;
475 for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
476 I != E; ++I, ++FieldNo) {
477 if (I->isBitField())
478 continue;
479
480 CharUnits FieldOffset = Offset + getFieldOffset(Layout, FieldNo);
481
482 UpdateEmptyFieldSubobjects(*I, FieldOffset);
483 }
484 }
485
UpdateEmptyFieldSubobjects(const FieldDecl * FD,CharUnits Offset)486 void EmptySubobjectMap::UpdateEmptyFieldSubobjects(const FieldDecl *FD,
487 CharUnits Offset) {
488 QualType T = FD->getType();
489 if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) {
490 UpdateEmptyFieldSubobjects(RD, RD, Offset);
491 return;
492 }
493
494 // If we have an array type we need to update every element.
495 if (const ConstantArrayType *AT = Context.getAsConstantArrayType(T)) {
496 QualType ElemTy = Context.getBaseElementType(AT);
497 const RecordType *RT = ElemTy->getAs<RecordType>();
498 if (!RT)
499 return;
500
501 const CXXRecordDecl *RD = RT->getAsCXXRecordDecl();
502 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
503
504 uint64_t NumElements = Context.getConstantArrayElementCount(AT);
505 CharUnits ElementOffset = Offset;
506
507 for (uint64_t I = 0; I != NumElements; ++I) {
508 // We know that the only empty subobjects that can conflict with empty
509 // field subobjects are subobjects of empty bases that can be placed at
510 // offset zero. Because of this, we only need to keep track of empty field
511 // subobjects with offsets less than the size of the largest empty
512 // subobject for our class.
513 if (ElementOffset >= SizeOfLargestEmptySubobject)
514 return;
515
516 UpdateEmptyFieldSubobjects(RD, RD, ElementOffset);
517 ElementOffset += Layout.getSize();
518 }
519 }
520 }
521
522 typedef llvm::SmallPtrSet<const CXXRecordDecl*, 4> ClassSetTy;
523
524 class RecordLayoutBuilder {
525 protected:
526 // FIXME: Remove this and make the appropriate fields public.
527 friend class clang::ASTContext;
528
529 const ASTContext &Context;
530
531 EmptySubobjectMap *EmptySubobjects;
532
533 /// Size - The current size of the record layout.
534 uint64_t Size;
535
536 /// Alignment - The current alignment of the record layout.
537 CharUnits Alignment;
538
539 /// \brief The alignment if attribute packed is not used.
540 CharUnits UnpackedAlignment;
541
542 SmallVector<uint64_t, 16> FieldOffsets;
543
544 /// \brief Whether the external AST source has provided a layout for this
545 /// record.
546 unsigned ExternalLayout : 1;
547
548 /// \brief Whether we need to infer alignment, even when we have an
549 /// externally-provided layout.
550 unsigned InferAlignment : 1;
551
552 /// Packed - Whether the record is packed or not.
553 unsigned Packed : 1;
554
555 unsigned IsUnion : 1;
556
557 unsigned IsMac68kAlign : 1;
558
559 unsigned IsMsStruct : 1;
560
561 /// UnfilledBitsInLastUnit - If the last field laid out was a bitfield,
562 /// this contains the number of bits in the last unit that can be used for
563 /// an adjacent bitfield if necessary. The unit in question is usually
564 /// a byte, but larger units are used if IsMsStruct.
565 unsigned char UnfilledBitsInLastUnit;
566 /// LastBitfieldTypeSize - If IsMsStruct, represents the size of the type
567 /// of the previous field if it was a bitfield.
568 unsigned char LastBitfieldTypeSize;
569
570 /// MaxFieldAlignment - The maximum allowed field alignment. This is set by
571 /// #pragma pack.
572 CharUnits MaxFieldAlignment;
573
574 /// DataSize - The data size of the record being laid out.
575 uint64_t DataSize;
576
577 CharUnits NonVirtualSize;
578 CharUnits NonVirtualAlignment;
579
580 /// PrimaryBase - the primary base class (if one exists) of the class
581 /// we're laying out.
582 const CXXRecordDecl *PrimaryBase;
583
584 /// PrimaryBaseIsVirtual - Whether the primary base of the class we're laying
585 /// out is virtual.
586 bool PrimaryBaseIsVirtual;
587
588 /// HasOwnVFPtr - Whether the class provides its own vtable/vftbl
589 /// pointer, as opposed to inheriting one from a primary base class.
590 bool HasOwnVFPtr;
591
592 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetsMapTy;
593
594 /// Bases - base classes and their offsets in the record.
595 BaseOffsetsMapTy Bases;
596
597 // VBases - virtual base classes and their offsets in the record.
598 ASTRecordLayout::VBaseOffsetsMapTy VBases;
599
600 /// IndirectPrimaryBases - Virtual base classes, direct or indirect, that are
601 /// primary base classes for some other direct or indirect base class.
602 CXXIndirectPrimaryBaseSet IndirectPrimaryBases;
603
604 /// FirstNearlyEmptyVBase - The first nearly empty virtual base class in
605 /// inheritance graph order. Used for determining the primary base class.
606 const CXXRecordDecl *FirstNearlyEmptyVBase;
607
608 /// VisitedVirtualBases - A set of all the visited virtual bases, used to
609 /// avoid visiting virtual bases more than once.
610 llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases;
611
612 /// \brief Externally-provided size.
613 uint64_t ExternalSize;
614
615 /// \brief Externally-provided alignment.
616 uint64_t ExternalAlign;
617
618 /// \brief Externally-provided field offsets.
619 llvm::DenseMap<const FieldDecl *, uint64_t> ExternalFieldOffsets;
620
621 /// \brief Externally-provided direct, non-virtual base offsets.
622 llvm::DenseMap<const CXXRecordDecl *, CharUnits> ExternalBaseOffsets;
623
624 /// \brief Externally-provided virtual base offsets.
625 llvm::DenseMap<const CXXRecordDecl *, CharUnits> ExternalVirtualBaseOffsets;
626
RecordLayoutBuilder(const ASTContext & Context,EmptySubobjectMap * EmptySubobjects)627 RecordLayoutBuilder(const ASTContext &Context,
628 EmptySubobjectMap *EmptySubobjects)
629 : Context(Context), EmptySubobjects(EmptySubobjects), Size(0),
630 Alignment(CharUnits::One()), UnpackedAlignment(CharUnits::One()),
631 ExternalLayout(false), InferAlignment(false),
632 Packed(false), IsUnion(false), IsMac68kAlign(false), IsMsStruct(false),
633 UnfilledBitsInLastUnit(0), LastBitfieldTypeSize(0),
634 MaxFieldAlignment(CharUnits::Zero()),
635 DataSize(0), NonVirtualSize(CharUnits::Zero()),
636 NonVirtualAlignment(CharUnits::One()),
637 PrimaryBase(nullptr), PrimaryBaseIsVirtual(false),
638 HasOwnVFPtr(false),
639 FirstNearlyEmptyVBase(nullptr) {}
640
641 /// Reset this RecordLayoutBuilder to a fresh state, using the given
642 /// alignment as the initial alignment. This is used for the
643 /// correct layout of vb-table pointers in MSVC.
resetWithTargetAlignment(CharUnits TargetAlignment)644 void resetWithTargetAlignment(CharUnits TargetAlignment) {
645 const ASTContext &Context = this->Context;
646 EmptySubobjectMap *EmptySubobjects = this->EmptySubobjects;
647 this->~RecordLayoutBuilder();
648 new (this) RecordLayoutBuilder(Context, EmptySubobjects);
649 Alignment = UnpackedAlignment = TargetAlignment;
650 }
651
652 void Layout(const RecordDecl *D);
653 void Layout(const CXXRecordDecl *D);
654 void Layout(const ObjCInterfaceDecl *D);
655
656 void LayoutFields(const RecordDecl *D);
657 void LayoutField(const FieldDecl *D);
658 void LayoutWideBitField(uint64_t FieldSize, uint64_t TypeSize,
659 bool FieldPacked, const FieldDecl *D);
660 void LayoutBitField(const FieldDecl *D);
661
getCXXABI() const662 TargetCXXABI getCXXABI() const {
663 return Context.getTargetInfo().getCXXABI();
664 }
665
666 /// BaseSubobjectInfoAllocator - Allocator for BaseSubobjectInfo objects.
667 llvm::SpecificBumpPtrAllocator<BaseSubobjectInfo> BaseSubobjectInfoAllocator;
668
669 typedef llvm::DenseMap<const CXXRecordDecl *, BaseSubobjectInfo *>
670 BaseSubobjectInfoMapTy;
671
672 /// VirtualBaseInfo - Map from all the (direct or indirect) virtual bases
673 /// of the class we're laying out to their base subobject info.
674 BaseSubobjectInfoMapTy VirtualBaseInfo;
675
676 /// NonVirtualBaseInfo - Map from all the direct non-virtual bases of the
677 /// class we're laying out to their base subobject info.
678 BaseSubobjectInfoMapTy NonVirtualBaseInfo;
679
680 /// ComputeBaseSubobjectInfo - Compute the base subobject information for the
681 /// bases of the given class.
682 void ComputeBaseSubobjectInfo(const CXXRecordDecl *RD);
683
684 /// ComputeBaseSubobjectInfo - Compute the base subobject information for a
685 /// single class and all of its base classes.
686 BaseSubobjectInfo *ComputeBaseSubobjectInfo(const CXXRecordDecl *RD,
687 bool IsVirtual,
688 BaseSubobjectInfo *Derived);
689
690 /// DeterminePrimaryBase - Determine the primary base of the given class.
691 void DeterminePrimaryBase(const CXXRecordDecl *RD);
692
693 void SelectPrimaryVBase(const CXXRecordDecl *RD);
694
695 void EnsureVTablePointerAlignment(CharUnits UnpackedBaseAlign);
696
697 /// LayoutNonVirtualBases - Determines the primary base class (if any) and
698 /// lays it out. Will then proceed to lay out all non-virtual base clasess.
699 void LayoutNonVirtualBases(const CXXRecordDecl *RD);
700
701 /// LayoutNonVirtualBase - Lays out a single non-virtual base.
702 void LayoutNonVirtualBase(const BaseSubobjectInfo *Base);
703
704 void AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info,
705 CharUnits Offset);
706
707 /// LayoutVirtualBases - Lays out all the virtual bases.
708 void LayoutVirtualBases(const CXXRecordDecl *RD,
709 const CXXRecordDecl *MostDerivedClass);
710
711 /// LayoutVirtualBase - Lays out a single virtual base.
712 void LayoutVirtualBase(const BaseSubobjectInfo *Base);
713
714 /// LayoutBase - Will lay out a base and return the offset where it was
715 /// placed, in chars.
716 CharUnits LayoutBase(const BaseSubobjectInfo *Base);
717
718 /// InitializeLayout - Initialize record layout for the given record decl.
719 void InitializeLayout(const Decl *D);
720
721 /// FinishLayout - Finalize record layout. Adjust record size based on the
722 /// alignment.
723 void FinishLayout(const NamedDecl *D);
724
725 void UpdateAlignment(CharUnits NewAlignment, CharUnits UnpackedNewAlignment);
UpdateAlignment(CharUnits NewAlignment)726 void UpdateAlignment(CharUnits NewAlignment) {
727 UpdateAlignment(NewAlignment, NewAlignment);
728 }
729
730 /// \brief Retrieve the externally-supplied field offset for the given
731 /// field.
732 ///
733 /// \param Field The field whose offset is being queried.
734 /// \param ComputedOffset The offset that we've computed for this field.
735 uint64_t updateExternalFieldOffset(const FieldDecl *Field,
736 uint64_t ComputedOffset);
737
738 void CheckFieldPadding(uint64_t Offset, uint64_t UnpaddedOffset,
739 uint64_t UnpackedOffset, unsigned UnpackedAlign,
740 bool isPacked, const FieldDecl *D);
741
742 DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID);
743
getSize() const744 CharUnits getSize() const {
745 assert(Size % Context.getCharWidth() == 0);
746 return Context.toCharUnitsFromBits(Size);
747 }
getSizeInBits() const748 uint64_t getSizeInBits() const { return Size; }
749
setSize(CharUnits NewSize)750 void setSize(CharUnits NewSize) { Size = Context.toBits(NewSize); }
setSize(uint64_t NewSize)751 void setSize(uint64_t NewSize) { Size = NewSize; }
752
getAligment() const753 CharUnits getAligment() const { return Alignment; }
754
getDataSize() const755 CharUnits getDataSize() const {
756 assert(DataSize % Context.getCharWidth() == 0);
757 return Context.toCharUnitsFromBits(DataSize);
758 }
getDataSizeInBits() const759 uint64_t getDataSizeInBits() const { return DataSize; }
760
setDataSize(CharUnits NewSize)761 void setDataSize(CharUnits NewSize) { DataSize = Context.toBits(NewSize); }
setDataSize(uint64_t NewSize)762 void setDataSize(uint64_t NewSize) { DataSize = NewSize; }
763
764 RecordLayoutBuilder(const RecordLayoutBuilder &) LLVM_DELETED_FUNCTION;
765 void operator=(const RecordLayoutBuilder &) LLVM_DELETED_FUNCTION;
766 };
767 } // end anonymous namespace
768
769 void
SelectPrimaryVBase(const CXXRecordDecl * RD)770 RecordLayoutBuilder::SelectPrimaryVBase(const CXXRecordDecl *RD) {
771 for (const auto &I : RD->bases()) {
772 assert(!I.getType()->isDependentType() &&
773 "Cannot layout class with dependent bases.");
774
775 const CXXRecordDecl *Base = I.getType()->getAsCXXRecordDecl();
776
777 // Check if this is a nearly empty virtual base.
778 if (I.isVirtual() && Context.isNearlyEmpty(Base)) {
779 // If it's not an indirect primary base, then we've found our primary
780 // base.
781 if (!IndirectPrimaryBases.count(Base)) {
782 PrimaryBase = Base;
783 PrimaryBaseIsVirtual = true;
784 return;
785 }
786
787 // Is this the first nearly empty virtual base?
788 if (!FirstNearlyEmptyVBase)
789 FirstNearlyEmptyVBase = Base;
790 }
791
792 SelectPrimaryVBase(Base);
793 if (PrimaryBase)
794 return;
795 }
796 }
797
798 /// DeterminePrimaryBase - Determine the primary base of the given class.
DeterminePrimaryBase(const CXXRecordDecl * RD)799 void RecordLayoutBuilder::DeterminePrimaryBase(const CXXRecordDecl *RD) {
800 // If the class isn't dynamic, it won't have a primary base.
801 if (!RD->isDynamicClass())
802 return;
803
804 // Compute all the primary virtual bases for all of our direct and
805 // indirect bases, and record all their primary virtual base classes.
806 RD->getIndirectPrimaryBases(IndirectPrimaryBases);
807
808 // If the record has a dynamic base class, attempt to choose a primary base
809 // class. It is the first (in direct base class order) non-virtual dynamic
810 // base class, if one exists.
811 for (const auto &I : RD->bases()) {
812 // Ignore virtual bases.
813 if (I.isVirtual())
814 continue;
815
816 const CXXRecordDecl *Base = I.getType()->getAsCXXRecordDecl();
817
818 if (Base->isDynamicClass()) {
819 // We found it.
820 PrimaryBase = Base;
821 PrimaryBaseIsVirtual = false;
822 return;
823 }
824 }
825
826 // Under the Itanium ABI, if there is no non-virtual primary base class,
827 // try to compute the primary virtual base. The primary virtual base is
828 // the first nearly empty virtual base that is not an indirect primary
829 // virtual base class, if one exists.
830 if (RD->getNumVBases() != 0) {
831 SelectPrimaryVBase(RD);
832 if (PrimaryBase)
833 return;
834 }
835
836 // Otherwise, it is the first indirect primary base class, if one exists.
837 if (FirstNearlyEmptyVBase) {
838 PrimaryBase = FirstNearlyEmptyVBase;
839 PrimaryBaseIsVirtual = true;
840 return;
841 }
842
843 assert(!PrimaryBase && "Should not get here with a primary base!");
844 }
845
846 BaseSubobjectInfo *
ComputeBaseSubobjectInfo(const CXXRecordDecl * RD,bool IsVirtual,BaseSubobjectInfo * Derived)847 RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD,
848 bool IsVirtual,
849 BaseSubobjectInfo *Derived) {
850 BaseSubobjectInfo *Info;
851
852 if (IsVirtual) {
853 // Check if we already have info about this virtual base.
854 BaseSubobjectInfo *&InfoSlot = VirtualBaseInfo[RD];
855 if (InfoSlot) {
856 assert(InfoSlot->Class == RD && "Wrong class for virtual base info!");
857 return InfoSlot;
858 }
859
860 // We don't, create it.
861 InfoSlot = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo;
862 Info = InfoSlot;
863 } else {
864 Info = new (BaseSubobjectInfoAllocator.Allocate()) BaseSubobjectInfo;
865 }
866
867 Info->Class = RD;
868 Info->IsVirtual = IsVirtual;
869 Info->Derived = nullptr;
870 Info->PrimaryVirtualBaseInfo = nullptr;
871
872 const CXXRecordDecl *PrimaryVirtualBase = nullptr;
873 BaseSubobjectInfo *PrimaryVirtualBaseInfo = nullptr;
874
875 // Check if this base has a primary virtual base.
876 if (RD->getNumVBases()) {
877 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
878 if (Layout.isPrimaryBaseVirtual()) {
879 // This base does have a primary virtual base.
880 PrimaryVirtualBase = Layout.getPrimaryBase();
881 assert(PrimaryVirtualBase && "Didn't have a primary virtual base!");
882
883 // Now check if we have base subobject info about this primary base.
884 PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase);
885
886 if (PrimaryVirtualBaseInfo) {
887 if (PrimaryVirtualBaseInfo->Derived) {
888 // We did have info about this primary base, and it turns out that it
889 // has already been claimed as a primary virtual base for another
890 // base.
891 PrimaryVirtualBase = nullptr;
892 } else {
893 // We can claim this base as our primary base.
894 Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo;
895 PrimaryVirtualBaseInfo->Derived = Info;
896 }
897 }
898 }
899 }
900
901 // Now go through all direct bases.
902 for (const auto &I : RD->bases()) {
903 bool IsVirtual = I.isVirtual();
904
905 const CXXRecordDecl *BaseDecl = I.getType()->getAsCXXRecordDecl();
906
907 Info->Bases.push_back(ComputeBaseSubobjectInfo(BaseDecl, IsVirtual, Info));
908 }
909
910 if (PrimaryVirtualBase && !PrimaryVirtualBaseInfo) {
911 // Traversing the bases must have created the base info for our primary
912 // virtual base.
913 PrimaryVirtualBaseInfo = VirtualBaseInfo.lookup(PrimaryVirtualBase);
914 assert(PrimaryVirtualBaseInfo &&
915 "Did not create a primary virtual base!");
916
917 // Claim the primary virtual base as our primary virtual base.
918 Info->PrimaryVirtualBaseInfo = PrimaryVirtualBaseInfo;
919 PrimaryVirtualBaseInfo->Derived = Info;
920 }
921
922 return Info;
923 }
924
ComputeBaseSubobjectInfo(const CXXRecordDecl * RD)925 void RecordLayoutBuilder::ComputeBaseSubobjectInfo(const CXXRecordDecl *RD) {
926 for (const auto &I : RD->bases()) {
927 bool IsVirtual = I.isVirtual();
928
929 const CXXRecordDecl *BaseDecl = I.getType()->getAsCXXRecordDecl();
930
931 // Compute the base subobject info for this base.
932 BaseSubobjectInfo *Info = ComputeBaseSubobjectInfo(BaseDecl, IsVirtual,
933 nullptr);
934
935 if (IsVirtual) {
936 // ComputeBaseInfo has already added this base for us.
937 assert(VirtualBaseInfo.count(BaseDecl) &&
938 "Did not add virtual base!");
939 } else {
940 // Add the base info to the map of non-virtual bases.
941 assert(!NonVirtualBaseInfo.count(BaseDecl) &&
942 "Non-virtual base already exists!");
943 NonVirtualBaseInfo.insert(std::make_pair(BaseDecl, Info));
944 }
945 }
946 }
947
948 void
EnsureVTablePointerAlignment(CharUnits UnpackedBaseAlign)949 RecordLayoutBuilder::EnsureVTablePointerAlignment(CharUnits UnpackedBaseAlign) {
950 CharUnits BaseAlign = (Packed) ? CharUnits::One() : UnpackedBaseAlign;
951
952 // The maximum field alignment overrides base align.
953 if (!MaxFieldAlignment.isZero()) {
954 BaseAlign = std::min(BaseAlign, MaxFieldAlignment);
955 UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment);
956 }
957
958 // Round up the current record size to pointer alignment.
959 setSize(getSize().RoundUpToAlignment(BaseAlign));
960 setDataSize(getSize());
961
962 // Update the alignment.
963 UpdateAlignment(BaseAlign, UnpackedBaseAlign);
964 }
965
966 void
LayoutNonVirtualBases(const CXXRecordDecl * RD)967 RecordLayoutBuilder::LayoutNonVirtualBases(const CXXRecordDecl *RD) {
968 // Then, determine the primary base class.
969 DeterminePrimaryBase(RD);
970
971 // Compute base subobject info.
972 ComputeBaseSubobjectInfo(RD);
973
974 // If we have a primary base class, lay it out.
975 if (PrimaryBase) {
976 if (PrimaryBaseIsVirtual) {
977 // If the primary virtual base was a primary virtual base of some other
978 // base class we'll have to steal it.
979 BaseSubobjectInfo *PrimaryBaseInfo = VirtualBaseInfo.lookup(PrimaryBase);
980 PrimaryBaseInfo->Derived = nullptr;
981
982 // We have a virtual primary base, insert it as an indirect primary base.
983 IndirectPrimaryBases.insert(PrimaryBase);
984
985 assert(!VisitedVirtualBases.count(PrimaryBase) &&
986 "vbase already visited!");
987 VisitedVirtualBases.insert(PrimaryBase);
988
989 LayoutVirtualBase(PrimaryBaseInfo);
990 } else {
991 BaseSubobjectInfo *PrimaryBaseInfo =
992 NonVirtualBaseInfo.lookup(PrimaryBase);
993 assert(PrimaryBaseInfo &&
994 "Did not find base info for non-virtual primary base!");
995
996 LayoutNonVirtualBase(PrimaryBaseInfo);
997 }
998
999 // If this class needs a vtable/vf-table and didn't get one from a
1000 // primary base, add it in now.
1001 } else if (RD->isDynamicClass()) {
1002 assert(DataSize == 0 && "Vtable pointer must be at offset zero!");
1003 CharUnits PtrWidth =
1004 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
1005 CharUnits PtrAlign =
1006 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(0));
1007 EnsureVTablePointerAlignment(PtrAlign);
1008 HasOwnVFPtr = true;
1009 setSize(getSize() + PtrWidth);
1010 setDataSize(getSize());
1011 }
1012
1013 // Now lay out the non-virtual bases.
1014 for (const auto &I : RD->bases()) {
1015
1016 // Ignore virtual bases.
1017 if (I.isVirtual())
1018 continue;
1019
1020 const CXXRecordDecl *BaseDecl = I.getType()->getAsCXXRecordDecl();
1021
1022 // Skip the primary base, because we've already laid it out. The
1023 // !PrimaryBaseIsVirtual check is required because we might have a
1024 // non-virtual base of the same type as a primary virtual base.
1025 if (BaseDecl == PrimaryBase && !PrimaryBaseIsVirtual)
1026 continue;
1027
1028 // Lay out the base.
1029 BaseSubobjectInfo *BaseInfo = NonVirtualBaseInfo.lookup(BaseDecl);
1030 assert(BaseInfo && "Did not find base info for non-virtual base!");
1031
1032 LayoutNonVirtualBase(BaseInfo);
1033 }
1034 }
1035
LayoutNonVirtualBase(const BaseSubobjectInfo * Base)1036 void RecordLayoutBuilder::LayoutNonVirtualBase(const BaseSubobjectInfo *Base) {
1037 // Layout the base.
1038 CharUnits Offset = LayoutBase(Base);
1039
1040 // Add its base class offset.
1041 assert(!Bases.count(Base->Class) && "base offset already exists!");
1042 Bases.insert(std::make_pair(Base->Class, Offset));
1043
1044 AddPrimaryVirtualBaseOffsets(Base, Offset);
1045 }
1046
1047 void
AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo * Info,CharUnits Offset)1048 RecordLayoutBuilder::AddPrimaryVirtualBaseOffsets(const BaseSubobjectInfo *Info,
1049 CharUnits Offset) {
1050 // This base isn't interesting, it has no virtual bases.
1051 if (!Info->Class->getNumVBases())
1052 return;
1053
1054 // First, check if we have a virtual primary base to add offsets for.
1055 if (Info->PrimaryVirtualBaseInfo) {
1056 assert(Info->PrimaryVirtualBaseInfo->IsVirtual &&
1057 "Primary virtual base is not virtual!");
1058 if (Info->PrimaryVirtualBaseInfo->Derived == Info) {
1059 // Add the offset.
1060 assert(!VBases.count(Info->PrimaryVirtualBaseInfo->Class) &&
1061 "primary vbase offset already exists!");
1062 VBases.insert(std::make_pair(Info->PrimaryVirtualBaseInfo->Class,
1063 ASTRecordLayout::VBaseInfo(Offset, false)));
1064
1065 // Traverse the primary virtual base.
1066 AddPrimaryVirtualBaseOffsets(Info->PrimaryVirtualBaseInfo, Offset);
1067 }
1068 }
1069
1070 // Now go through all direct non-virtual bases.
1071 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Info->Class);
1072 for (unsigned I = 0, E = Info->Bases.size(); I != E; ++I) {
1073 const BaseSubobjectInfo *Base = Info->Bases[I];
1074 if (Base->IsVirtual)
1075 continue;
1076
1077 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base->Class);
1078 AddPrimaryVirtualBaseOffsets(Base, BaseOffset);
1079 }
1080 }
1081
1082 void
LayoutVirtualBases(const CXXRecordDecl * RD,const CXXRecordDecl * MostDerivedClass)1083 RecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
1084 const CXXRecordDecl *MostDerivedClass) {
1085 const CXXRecordDecl *PrimaryBase;
1086 bool PrimaryBaseIsVirtual;
1087
1088 if (MostDerivedClass == RD) {
1089 PrimaryBase = this->PrimaryBase;
1090 PrimaryBaseIsVirtual = this->PrimaryBaseIsVirtual;
1091 } else {
1092 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1093 PrimaryBase = Layout.getPrimaryBase();
1094 PrimaryBaseIsVirtual = Layout.isPrimaryBaseVirtual();
1095 }
1096
1097 for (const auto &I : RD->bases()) {
1098 assert(!I.getType()->isDependentType() &&
1099 "Cannot layout class with dependent bases.");
1100
1101 const CXXRecordDecl *BaseDecl = I.getType()->getAsCXXRecordDecl();
1102
1103 if (I.isVirtual()) {
1104 if (PrimaryBase != BaseDecl || !PrimaryBaseIsVirtual) {
1105 bool IndirectPrimaryBase = IndirectPrimaryBases.count(BaseDecl);
1106
1107 // Only lay out the virtual base if it's not an indirect primary base.
1108 if (!IndirectPrimaryBase) {
1109 // Only visit virtual bases once.
1110 if (!VisitedVirtualBases.insert(BaseDecl))
1111 continue;
1112
1113 const BaseSubobjectInfo *BaseInfo = VirtualBaseInfo.lookup(BaseDecl);
1114 assert(BaseInfo && "Did not find virtual base info!");
1115 LayoutVirtualBase(BaseInfo);
1116 }
1117 }
1118 }
1119
1120 if (!BaseDecl->getNumVBases()) {
1121 // This base isn't interesting since it doesn't have any virtual bases.
1122 continue;
1123 }
1124
1125 LayoutVirtualBases(BaseDecl, MostDerivedClass);
1126 }
1127 }
1128
LayoutVirtualBase(const BaseSubobjectInfo * Base)1129 void RecordLayoutBuilder::LayoutVirtualBase(const BaseSubobjectInfo *Base) {
1130 assert(!Base->Derived && "Trying to lay out a primary virtual base!");
1131
1132 // Layout the base.
1133 CharUnits Offset = LayoutBase(Base);
1134
1135 // Add its base class offset.
1136 assert(!VBases.count(Base->Class) && "vbase offset already exists!");
1137 VBases.insert(std::make_pair(Base->Class,
1138 ASTRecordLayout::VBaseInfo(Offset, false)));
1139
1140 AddPrimaryVirtualBaseOffsets(Base, Offset);
1141 }
1142
LayoutBase(const BaseSubobjectInfo * Base)1143 CharUnits RecordLayoutBuilder::LayoutBase(const BaseSubobjectInfo *Base) {
1144 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base->Class);
1145
1146
1147 CharUnits Offset;
1148
1149 // Query the external layout to see if it provides an offset.
1150 bool HasExternalLayout = false;
1151 if (ExternalLayout) {
1152 llvm::DenseMap<const CXXRecordDecl *, CharUnits>::iterator Known;
1153 if (Base->IsVirtual) {
1154 Known = ExternalVirtualBaseOffsets.find(Base->Class);
1155 if (Known != ExternalVirtualBaseOffsets.end()) {
1156 Offset = Known->second;
1157 HasExternalLayout = true;
1158 }
1159 } else {
1160 Known = ExternalBaseOffsets.find(Base->Class);
1161 if (Known != ExternalBaseOffsets.end()) {
1162 Offset = Known->second;
1163 HasExternalLayout = true;
1164 }
1165 }
1166 }
1167
1168 CharUnits UnpackedBaseAlign = Layout.getNonVirtualAlignment();
1169 CharUnits BaseAlign = (Packed) ? CharUnits::One() : UnpackedBaseAlign;
1170
1171 // If we have an empty base class, try to place it at offset 0.
1172 if (Base->Class->isEmpty() &&
1173 (!HasExternalLayout || Offset == CharUnits::Zero()) &&
1174 EmptySubobjects->CanPlaceBaseAtOffset(Base, CharUnits::Zero())) {
1175 setSize(std::max(getSize(), Layout.getSize()));
1176 UpdateAlignment(BaseAlign, UnpackedBaseAlign);
1177
1178 return CharUnits::Zero();
1179 }
1180
1181 // The maximum field alignment overrides base align.
1182 if (!MaxFieldAlignment.isZero()) {
1183 BaseAlign = std::min(BaseAlign, MaxFieldAlignment);
1184 UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment);
1185 }
1186
1187 if (!HasExternalLayout) {
1188 // Round up the current record size to the base's alignment boundary.
1189 Offset = getDataSize().RoundUpToAlignment(BaseAlign);
1190
1191 // Try to place the base.
1192 while (!EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset))
1193 Offset += BaseAlign;
1194 } else {
1195 bool Allowed = EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset);
1196 (void)Allowed;
1197 assert(Allowed && "Base subobject externally placed at overlapping offset");
1198
1199 if (InferAlignment && Offset < getDataSize().RoundUpToAlignment(BaseAlign)){
1200 // The externally-supplied base offset is before the base offset we
1201 // computed. Assume that the structure is packed.
1202 Alignment = CharUnits::One();
1203 InferAlignment = false;
1204 }
1205 }
1206
1207 if (!Base->Class->isEmpty()) {
1208 // Update the data size.
1209 setDataSize(Offset + Layout.getNonVirtualSize());
1210
1211 setSize(std::max(getSize(), getDataSize()));
1212 } else
1213 setSize(std::max(getSize(), Offset + Layout.getSize()));
1214
1215 // Remember max struct/class alignment.
1216 UpdateAlignment(BaseAlign, UnpackedBaseAlign);
1217
1218 return Offset;
1219 }
1220
InitializeLayout(const Decl * D)1221 void RecordLayoutBuilder::InitializeLayout(const Decl *D) {
1222 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) {
1223 IsUnion = RD->isUnion();
1224 IsMsStruct = RD->isMsStruct(Context);
1225 }
1226
1227 Packed = D->hasAttr<PackedAttr>();
1228
1229 // Honor the default struct packing maximum alignment flag.
1230 if (unsigned DefaultMaxFieldAlignment = Context.getLangOpts().PackStruct) {
1231 MaxFieldAlignment = CharUnits::fromQuantity(DefaultMaxFieldAlignment);
1232 }
1233
1234 // mac68k alignment supersedes maximum field alignment and attribute aligned,
1235 // and forces all structures to have 2-byte alignment. The IBM docs on it
1236 // allude to additional (more complicated) semantics, especially with regard
1237 // to bit-fields, but gcc appears not to follow that.
1238 if (D->hasAttr<AlignMac68kAttr>()) {
1239 IsMac68kAlign = true;
1240 MaxFieldAlignment = CharUnits::fromQuantity(2);
1241 Alignment = CharUnits::fromQuantity(2);
1242 } else {
1243 if (const MaxFieldAlignmentAttr *MFAA = D->getAttr<MaxFieldAlignmentAttr>())
1244 MaxFieldAlignment = Context.toCharUnitsFromBits(MFAA->getAlignment());
1245
1246 if (unsigned MaxAlign = D->getMaxAlignment())
1247 UpdateAlignment(Context.toCharUnitsFromBits(MaxAlign));
1248 }
1249
1250 // If there is an external AST source, ask it for the various offsets.
1251 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
1252 if (ExternalASTSource *External = Context.getExternalSource()) {
1253 ExternalLayout = External->layoutRecordType(RD,
1254 ExternalSize,
1255 ExternalAlign,
1256 ExternalFieldOffsets,
1257 ExternalBaseOffsets,
1258 ExternalVirtualBaseOffsets);
1259
1260 // Update based on external alignment.
1261 if (ExternalLayout) {
1262 if (ExternalAlign > 0) {
1263 Alignment = Context.toCharUnitsFromBits(ExternalAlign);
1264 } else {
1265 // The external source didn't have alignment information; infer it.
1266 InferAlignment = true;
1267 }
1268 }
1269 }
1270 }
1271
Layout(const RecordDecl * D)1272 void RecordLayoutBuilder::Layout(const RecordDecl *D) {
1273 InitializeLayout(D);
1274 LayoutFields(D);
1275
1276 // Finally, round the size of the total struct up to the alignment of the
1277 // struct itself.
1278 FinishLayout(D);
1279 }
1280
Layout(const CXXRecordDecl * RD)1281 void RecordLayoutBuilder::Layout(const CXXRecordDecl *RD) {
1282 InitializeLayout(RD);
1283
1284 // Lay out the vtable and the non-virtual bases.
1285 LayoutNonVirtualBases(RD);
1286
1287 LayoutFields(RD);
1288
1289 NonVirtualSize = Context.toCharUnitsFromBits(
1290 llvm::RoundUpToAlignment(getSizeInBits(),
1291 Context.getTargetInfo().getCharAlign()));
1292 NonVirtualAlignment = Alignment;
1293
1294 // Lay out the virtual bases and add the primary virtual base offsets.
1295 LayoutVirtualBases(RD, RD);
1296
1297 // Finally, round the size of the total struct up to the alignment
1298 // of the struct itself.
1299 FinishLayout(RD);
1300
1301 #ifndef NDEBUG
1302 // Check that we have base offsets for all bases.
1303 for (const auto &I : RD->bases()) {
1304 if (I.isVirtual())
1305 continue;
1306
1307 const CXXRecordDecl *BaseDecl = I.getType()->getAsCXXRecordDecl();
1308
1309 assert(Bases.count(BaseDecl) && "Did not find base offset!");
1310 }
1311
1312 // And all virtual bases.
1313 for (const auto &I : RD->vbases()) {
1314 const CXXRecordDecl *BaseDecl = I.getType()->getAsCXXRecordDecl();
1315
1316 assert(VBases.count(BaseDecl) && "Did not find base offset!");
1317 }
1318 #endif
1319 }
1320
Layout(const ObjCInterfaceDecl * D)1321 void RecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D) {
1322 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
1323 const ASTRecordLayout &SL = Context.getASTObjCInterfaceLayout(SD);
1324
1325 UpdateAlignment(SL.getAlignment());
1326
1327 // We start laying out ivars not at the end of the superclass
1328 // structure, but at the next byte following the last field.
1329 setSize(SL.getDataSize());
1330 setDataSize(getSize());
1331 }
1332
1333 InitializeLayout(D);
1334 // Layout each ivar sequentially.
1335 for (const ObjCIvarDecl *IVD = D->all_declared_ivar_begin(); IVD;
1336 IVD = IVD->getNextIvar())
1337 LayoutField(IVD);
1338
1339 // Finally, round the size of the total struct up to the alignment of the
1340 // struct itself.
1341 FinishLayout(D);
1342 }
1343
LayoutFields(const RecordDecl * D)1344 void RecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
1345 // Layout each field, for now, just sequentially, respecting alignment. In
1346 // the future, this will need to be tweakable by targets.
1347 for (const auto *Field : D->fields())
1348 LayoutField(Field);
1349 }
1350
LayoutWideBitField(uint64_t FieldSize,uint64_t TypeSize,bool FieldPacked,const FieldDecl * D)1351 void RecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize,
1352 uint64_t TypeSize,
1353 bool FieldPacked,
1354 const FieldDecl *D) {
1355 assert(Context.getLangOpts().CPlusPlus &&
1356 "Can only have wide bit-fields in C++!");
1357
1358 // Itanium C++ ABI 2.4:
1359 // If sizeof(T)*8 < n, let T' be the largest integral POD type with
1360 // sizeof(T')*8 <= n.
1361
1362 QualType IntegralPODTypes[] = {
1363 Context.UnsignedCharTy, Context.UnsignedShortTy, Context.UnsignedIntTy,
1364 Context.UnsignedLongTy, Context.UnsignedLongLongTy
1365 };
1366
1367 QualType Type;
1368 for (unsigned I = 0, E = llvm::array_lengthof(IntegralPODTypes);
1369 I != E; ++I) {
1370 uint64_t Size = Context.getTypeSize(IntegralPODTypes[I]);
1371
1372 if (Size > FieldSize)
1373 break;
1374
1375 Type = IntegralPODTypes[I];
1376 }
1377 assert(!Type.isNull() && "Did not find a type!");
1378
1379 CharUnits TypeAlign = Context.getTypeAlignInChars(Type);
1380
1381 // We're not going to use any of the unfilled bits in the last byte.
1382 UnfilledBitsInLastUnit = 0;
1383 LastBitfieldTypeSize = 0;
1384
1385 uint64_t FieldOffset;
1386 uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastUnit;
1387
1388 if (IsUnion) {
1389 setDataSize(std::max(getDataSizeInBits(), FieldSize));
1390 FieldOffset = 0;
1391 } else {
1392 // The bitfield is allocated starting at the next offset aligned
1393 // appropriately for T', with length n bits.
1394 FieldOffset = llvm::RoundUpToAlignment(getDataSizeInBits(),
1395 Context.toBits(TypeAlign));
1396
1397 uint64_t NewSizeInBits = FieldOffset + FieldSize;
1398
1399 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits,
1400 Context.getTargetInfo().getCharAlign()));
1401 UnfilledBitsInLastUnit = getDataSizeInBits() - NewSizeInBits;
1402 }
1403
1404 // Place this field at the current location.
1405 FieldOffsets.push_back(FieldOffset);
1406
1407 CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, FieldOffset,
1408 Context.toBits(TypeAlign), FieldPacked, D);
1409
1410 // Update the size.
1411 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
1412
1413 // Remember max struct/class alignment.
1414 UpdateAlignment(TypeAlign);
1415 }
1416
LayoutBitField(const FieldDecl * D)1417 void RecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
1418 bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
1419 uint64_t FieldSize = D->getBitWidthValue(Context);
1420 std::pair<uint64_t, unsigned> FieldInfo = Context.getTypeInfo(D->getType());
1421 uint64_t TypeSize = FieldInfo.first;
1422 unsigned FieldAlign = FieldInfo.second;
1423
1424 // UnfilledBitsInLastUnit is the difference between the end of the
1425 // last allocated bitfield (i.e. the first bit offset available for
1426 // bitfields) and the end of the current data size in bits (i.e. the
1427 // first bit offset available for non-bitfields). The current data
1428 // size in bits is always a multiple of the char size; additionally,
1429 // for ms_struct records it's also a multiple of the
1430 // LastBitfieldTypeSize (if set).
1431
1432 // The struct-layout algorithm is dictated by the platform ABI,
1433 // which in principle could use almost any rules it likes. In
1434 // practice, UNIXy targets tend to inherit the algorithm described
1435 // in the System V generic ABI. The basic bitfield layout rule in
1436 // System V is to place bitfields at the next available bit offset
1437 // where the entire bitfield would fit in an aligned storage unit of
1438 // the declared type; it's okay if an earlier or later non-bitfield
1439 // is allocated in the same storage unit. However, some targets
1440 // (those that !useBitFieldTypeAlignment(), e.g. ARM APCS) don't
1441 // require this storage unit to be aligned, and therefore always put
1442 // the bitfield at the next available bit offset.
1443
1444 // ms_struct basically requests a complete replacement of the
1445 // platform ABI's struct-layout algorithm, with the high-level goal
1446 // of duplicating MSVC's layout. For non-bitfields, this follows
1447 // the the standard algorithm. The basic bitfield layout rule is to
1448 // allocate an entire unit of the bitfield's declared type
1449 // (e.g. 'unsigned long'), then parcel it up among successive
1450 // bitfields whose declared types have the same size, making a new
1451 // unit as soon as the last can no longer store the whole value.
1452 // Since it completely replaces the platform ABI's algorithm,
1453 // settings like !useBitFieldTypeAlignment() do not apply.
1454
1455 // A zero-width bitfield forces the use of a new storage unit for
1456 // later bitfields. In general, this occurs by rounding up the
1457 // current size of the struct as if the algorithm were about to
1458 // place a non-bitfield of the field's formal type. Usually this
1459 // does not change the alignment of the struct itself, but it does
1460 // on some targets (those that useZeroLengthBitfieldAlignment(),
1461 // e.g. ARM). In ms_struct layout, zero-width bitfields are
1462 // ignored unless they follow a non-zero-width bitfield.
1463
1464 // A field alignment restriction (e.g. from #pragma pack) or
1465 // specification (e.g. from __attribute__((aligned))) changes the
1466 // formal alignment of the field. For System V, this alters the
1467 // required alignment of the notional storage unit that must contain
1468 // the bitfield. For ms_struct, this only affects the placement of
1469 // new storage units. In both cases, the effect of #pragma pack is
1470 // ignored on zero-width bitfields.
1471
1472 // On System V, a packed field (e.g. from #pragma pack or
1473 // __attribute__((packed))) always uses the next available bit
1474 // offset.
1475
1476 // In an ms_struct struct, the alignment of a fundamental type is
1477 // always equal to its size. This is necessary in order to mimic
1478 // the i386 alignment rules on targets which might not fully align
1479 // all types (e.g. Darwin PPC32, where alignof(long long) == 4).
1480
1481 // First, some simple bookkeeping to perform for ms_struct structs.
1482 if (IsMsStruct) {
1483 // The field alignment for integer types is always the size.
1484 FieldAlign = TypeSize;
1485
1486 // If the previous field was not a bitfield, or was a bitfield
1487 // with a different storage unit size, we're done with that
1488 // storage unit.
1489 if (LastBitfieldTypeSize != TypeSize) {
1490 // Also, ignore zero-length bitfields after non-bitfields.
1491 if (!LastBitfieldTypeSize && !FieldSize)
1492 FieldAlign = 1;
1493
1494 UnfilledBitsInLastUnit = 0;
1495 LastBitfieldTypeSize = 0;
1496 }
1497 }
1498
1499 // If the field is wider than its declared type, it follows
1500 // different rules in all cases.
1501 if (FieldSize > TypeSize) {
1502 LayoutWideBitField(FieldSize, TypeSize, FieldPacked, D);
1503 return;
1504 }
1505
1506 // Compute the next available bit offset.
1507 uint64_t FieldOffset =
1508 IsUnion ? 0 : (getDataSizeInBits() - UnfilledBitsInLastUnit);
1509
1510 // Handle targets that don't honor bitfield type alignment.
1511 if (!IsMsStruct && !Context.getTargetInfo().useBitFieldTypeAlignment()) {
1512 // Some such targets do honor it on zero-width bitfields.
1513 if (FieldSize == 0 &&
1514 Context.getTargetInfo().useZeroLengthBitfieldAlignment()) {
1515 // The alignment to round up to is the max of the field's natural
1516 // alignment and a target-specific fixed value (sometimes zero).
1517 unsigned ZeroLengthBitfieldBoundary =
1518 Context.getTargetInfo().getZeroLengthBitfieldBoundary();
1519 FieldAlign = std::max(FieldAlign, ZeroLengthBitfieldBoundary);
1520
1521 // If that doesn't apply, just ignore the field alignment.
1522 } else {
1523 FieldAlign = 1;
1524 }
1525 }
1526
1527 // Remember the alignment we would have used if the field were not packed.
1528 unsigned UnpackedFieldAlign = FieldAlign;
1529
1530 // Ignore the field alignment if the field is packed unless it has zero-size.
1531 if (!IsMsStruct && FieldPacked && FieldSize != 0)
1532 FieldAlign = 1;
1533
1534 // But, if there's an 'aligned' attribute on the field, honor that.
1535 if (unsigned ExplicitFieldAlign = D->getMaxAlignment()) {
1536 FieldAlign = std::max(FieldAlign, ExplicitFieldAlign);
1537 UnpackedFieldAlign = std::max(UnpackedFieldAlign, ExplicitFieldAlign);
1538 }
1539
1540 // But, if there's a #pragma pack in play, that takes precedent over
1541 // even the 'aligned' attribute, for non-zero-width bitfields.
1542 if (!MaxFieldAlignment.isZero() && FieldSize) {
1543 unsigned MaxFieldAlignmentInBits = Context.toBits(MaxFieldAlignment);
1544 FieldAlign = std::min(FieldAlign, MaxFieldAlignmentInBits);
1545 UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignmentInBits);
1546 }
1547
1548 // For purposes of diagnostics, we're going to simultaneously
1549 // compute the field offsets that we would have used if we weren't
1550 // adding any alignment padding or if the field weren't packed.
1551 uint64_t UnpaddedFieldOffset = FieldOffset;
1552 uint64_t UnpackedFieldOffset = FieldOffset;
1553
1554 // Check if we need to add padding to fit the bitfield within an
1555 // allocation unit with the right size and alignment. The rules are
1556 // somewhat different here for ms_struct structs.
1557 if (IsMsStruct) {
1558 // If it's not a zero-width bitfield, and we can fit the bitfield
1559 // into the active storage unit (and we haven't already decided to
1560 // start a new storage unit), just do so, regardless of any other
1561 // other consideration. Otherwise, round up to the right alignment.
1562 if (FieldSize == 0 || FieldSize > UnfilledBitsInLastUnit) {
1563 FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign);
1564 UnpackedFieldOffset = llvm::RoundUpToAlignment(UnpackedFieldOffset,
1565 UnpackedFieldAlign);
1566 UnfilledBitsInLastUnit = 0;
1567 }
1568
1569 } else {
1570 // #pragma pack, with any value, suppresses the insertion of padding.
1571 bool AllowPadding = MaxFieldAlignment.isZero();
1572
1573 // Compute the real offset.
1574 if (FieldSize == 0 ||
1575 (AllowPadding &&
1576 (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)) {
1577 FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign);
1578 }
1579
1580 // Repeat the computation for diagnostic purposes.
1581 if (FieldSize == 0 ||
1582 (AllowPadding &&
1583 (UnpackedFieldOffset & (UnpackedFieldAlign-1)) + FieldSize > TypeSize))
1584 UnpackedFieldOffset = llvm::RoundUpToAlignment(UnpackedFieldOffset,
1585 UnpackedFieldAlign);
1586 }
1587
1588 // If we're using external layout, give the external layout a chance
1589 // to override this information.
1590 if (ExternalLayout)
1591 FieldOffset = updateExternalFieldOffset(D, FieldOffset);
1592
1593 // Okay, place the bitfield at the calculated offset.
1594 FieldOffsets.push_back(FieldOffset);
1595
1596 // Bookkeeping:
1597
1598 // Anonymous members don't affect the overall record alignment,
1599 // except on targets where they do.
1600 if (!IsMsStruct &&
1601 !Context.getTargetInfo().useZeroLengthBitfieldAlignment() &&
1602 !D->getIdentifier())
1603 FieldAlign = UnpackedFieldAlign = 1;
1604
1605 // Diagnose differences in layout due to padding or packing.
1606 if (!ExternalLayout)
1607 CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, UnpackedFieldOffset,
1608 UnpackedFieldAlign, FieldPacked, D);
1609
1610 // Update DataSize to include the last byte containing (part of) the bitfield.
1611
1612 // For unions, this is just a max operation, as usual.
1613 if (IsUnion) {
1614 // FIXME: I think FieldSize should be TypeSize here.
1615 setDataSize(std::max(getDataSizeInBits(), FieldSize));
1616
1617 // For non-zero-width bitfields in ms_struct structs, allocate a new
1618 // storage unit if necessary.
1619 } else if (IsMsStruct && FieldSize) {
1620 // We should have cleared UnfilledBitsInLastUnit in every case
1621 // where we changed storage units.
1622 if (!UnfilledBitsInLastUnit) {
1623 setDataSize(FieldOffset + TypeSize);
1624 UnfilledBitsInLastUnit = TypeSize;
1625 }
1626 UnfilledBitsInLastUnit -= FieldSize;
1627 LastBitfieldTypeSize = TypeSize;
1628
1629 // Otherwise, bump the data size up to include the bitfield,
1630 // including padding up to char alignment, and then remember how
1631 // bits we didn't use.
1632 } else {
1633 uint64_t NewSizeInBits = FieldOffset + FieldSize;
1634 uint64_t CharAlignment = Context.getTargetInfo().getCharAlign();
1635 setDataSize(llvm::RoundUpToAlignment(NewSizeInBits, CharAlignment));
1636 UnfilledBitsInLastUnit = getDataSizeInBits() - NewSizeInBits;
1637
1638 // The only time we can get here for an ms_struct is if this is a
1639 // zero-width bitfield, which doesn't count as anything for the
1640 // purposes of unfilled bits.
1641 LastBitfieldTypeSize = 0;
1642 }
1643
1644 // Update the size.
1645 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
1646
1647 // Remember max struct/class alignment.
1648 UpdateAlignment(Context.toCharUnitsFromBits(FieldAlign),
1649 Context.toCharUnitsFromBits(UnpackedFieldAlign));
1650 }
1651
LayoutField(const FieldDecl * D)1652 void RecordLayoutBuilder::LayoutField(const FieldDecl *D) {
1653 if (D->isBitField()) {
1654 LayoutBitField(D);
1655 return;
1656 }
1657
1658 uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastUnit;
1659
1660 // Reset the unfilled bits.
1661 UnfilledBitsInLastUnit = 0;
1662 LastBitfieldTypeSize = 0;
1663
1664 bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
1665 CharUnits FieldOffset =
1666 IsUnion ? CharUnits::Zero() : getDataSize();
1667 CharUnits FieldSize;
1668 CharUnits FieldAlign;
1669
1670 if (D->getType()->isIncompleteArrayType()) {
1671 // This is a flexible array member; we can't directly
1672 // query getTypeInfo about these, so we figure it out here.
1673 // Flexible array members don't have any size, but they
1674 // have to be aligned appropriately for their element type.
1675 FieldSize = CharUnits::Zero();
1676 const ArrayType* ATy = Context.getAsArrayType(D->getType());
1677 FieldAlign = Context.getTypeAlignInChars(ATy->getElementType());
1678 } else if (const ReferenceType *RT = D->getType()->getAs<ReferenceType>()) {
1679 unsigned AS = RT->getPointeeType().getAddressSpace();
1680 FieldSize =
1681 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(AS));
1682 FieldAlign =
1683 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerAlign(AS));
1684 } else {
1685 std::pair<CharUnits, CharUnits> FieldInfo =
1686 Context.getTypeInfoInChars(D->getType());
1687 FieldSize = FieldInfo.first;
1688 FieldAlign = FieldInfo.second;
1689
1690 if (IsMsStruct) {
1691 // If MS bitfield layout is required, figure out what type is being
1692 // laid out and align the field to the width of that type.
1693
1694 // Resolve all typedefs down to their base type and round up the field
1695 // alignment if necessary.
1696 QualType T = Context.getBaseElementType(D->getType());
1697 if (const BuiltinType *BTy = T->getAs<BuiltinType>()) {
1698 CharUnits TypeSize = Context.getTypeSizeInChars(BTy);
1699 if (TypeSize > FieldAlign)
1700 FieldAlign = TypeSize;
1701 }
1702 }
1703 }
1704
1705 // The align if the field is not packed. This is to check if the attribute
1706 // was unnecessary (-Wpacked).
1707 CharUnits UnpackedFieldAlign = FieldAlign;
1708 CharUnits UnpackedFieldOffset = FieldOffset;
1709
1710 if (FieldPacked)
1711 FieldAlign = CharUnits::One();
1712 CharUnits MaxAlignmentInChars =
1713 Context.toCharUnitsFromBits(D->getMaxAlignment());
1714 FieldAlign = std::max(FieldAlign, MaxAlignmentInChars);
1715 UnpackedFieldAlign = std::max(UnpackedFieldAlign, MaxAlignmentInChars);
1716
1717 // The maximum field alignment overrides the aligned attribute.
1718 if (!MaxFieldAlignment.isZero()) {
1719 FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
1720 UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignment);
1721 }
1722
1723 // Round up the current record size to the field's alignment boundary.
1724 FieldOffset = FieldOffset.RoundUpToAlignment(FieldAlign);
1725 UnpackedFieldOffset =
1726 UnpackedFieldOffset.RoundUpToAlignment(UnpackedFieldAlign);
1727
1728 if (ExternalLayout) {
1729 FieldOffset = Context.toCharUnitsFromBits(
1730 updateExternalFieldOffset(D, Context.toBits(FieldOffset)));
1731
1732 if (!IsUnion && EmptySubobjects) {
1733 // Record the fact that we're placing a field at this offset.
1734 bool Allowed = EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset);
1735 (void)Allowed;
1736 assert(Allowed && "Externally-placed field cannot be placed here");
1737 }
1738 } else {
1739 if (!IsUnion && EmptySubobjects) {
1740 // Check if we can place the field at this offset.
1741 while (!EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset)) {
1742 // We couldn't place the field at the offset. Try again at a new offset.
1743 FieldOffset += FieldAlign;
1744 }
1745 }
1746 }
1747
1748 // Place this field at the current location.
1749 FieldOffsets.push_back(Context.toBits(FieldOffset));
1750
1751 if (!ExternalLayout)
1752 CheckFieldPadding(Context.toBits(FieldOffset), UnpaddedFieldOffset,
1753 Context.toBits(UnpackedFieldOffset),
1754 Context.toBits(UnpackedFieldAlign), FieldPacked, D);
1755
1756 // Reserve space for this field.
1757 uint64_t FieldSizeInBits = Context.toBits(FieldSize);
1758 if (IsUnion)
1759 setDataSize(std::max(getDataSizeInBits(), FieldSizeInBits));
1760 else
1761 setDataSize(FieldOffset + FieldSize);
1762
1763 // Update the size.
1764 setSize(std::max(getSizeInBits(), getDataSizeInBits()));
1765
1766 // Remember max struct/class alignment.
1767 UpdateAlignment(FieldAlign, UnpackedFieldAlign);
1768 }
1769
FinishLayout(const NamedDecl * D)1770 void RecordLayoutBuilder::FinishLayout(const NamedDecl *D) {
1771 // In C++, records cannot be of size 0.
1772 if (Context.getLangOpts().CPlusPlus && getSizeInBits() == 0) {
1773 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
1774 // Compatibility with gcc requires a class (pod or non-pod)
1775 // which is not empty but of size 0; such as having fields of
1776 // array of zero-length, remains of Size 0
1777 if (RD->isEmpty())
1778 setSize(CharUnits::One());
1779 }
1780 else
1781 setSize(CharUnits::One());
1782 }
1783
1784 // Finally, round the size of the record up to the alignment of the
1785 // record itself.
1786 uint64_t UnpaddedSize = getSizeInBits() - UnfilledBitsInLastUnit;
1787 uint64_t UnpackedSizeInBits =
1788 llvm::RoundUpToAlignment(getSizeInBits(),
1789 Context.toBits(UnpackedAlignment));
1790 CharUnits UnpackedSize = Context.toCharUnitsFromBits(UnpackedSizeInBits);
1791 uint64_t RoundedSize
1792 = llvm::RoundUpToAlignment(getSizeInBits(), Context.toBits(Alignment));
1793
1794 if (ExternalLayout) {
1795 // If we're inferring alignment, and the external size is smaller than
1796 // our size after we've rounded up to alignment, conservatively set the
1797 // alignment to 1.
1798 if (InferAlignment && ExternalSize < RoundedSize) {
1799 Alignment = CharUnits::One();
1800 InferAlignment = false;
1801 }
1802 setSize(ExternalSize);
1803 return;
1804 }
1805
1806 // Set the size to the final size.
1807 setSize(RoundedSize);
1808
1809 unsigned CharBitNum = Context.getTargetInfo().getCharWidth();
1810 if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) {
1811 // Warn if padding was introduced to the struct/class/union.
1812 if (getSizeInBits() > UnpaddedSize) {
1813 unsigned PadSize = getSizeInBits() - UnpaddedSize;
1814 bool InBits = true;
1815 if (PadSize % CharBitNum == 0) {
1816 PadSize = PadSize / CharBitNum;
1817 InBits = false;
1818 }
1819 Diag(RD->getLocation(), diag::warn_padded_struct_size)
1820 << Context.getTypeDeclType(RD)
1821 << PadSize
1822 << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not
1823 }
1824
1825 // Warn if we packed it unnecessarily. If the alignment is 1 byte don't
1826 // bother since there won't be alignment issues.
1827 if (Packed && UnpackedAlignment > CharUnits::One() &&
1828 getSize() == UnpackedSize)
1829 Diag(D->getLocation(), diag::warn_unnecessary_packed)
1830 << Context.getTypeDeclType(RD);
1831 }
1832 }
1833
UpdateAlignment(CharUnits NewAlignment,CharUnits UnpackedNewAlignment)1834 void RecordLayoutBuilder::UpdateAlignment(CharUnits NewAlignment,
1835 CharUnits UnpackedNewAlignment) {
1836 // The alignment is not modified when using 'mac68k' alignment or when
1837 // we have an externally-supplied layout that also provides overall alignment.
1838 if (IsMac68kAlign || (ExternalLayout && !InferAlignment))
1839 return;
1840
1841 if (NewAlignment > Alignment) {
1842 assert(llvm::isPowerOf2_32(NewAlignment.getQuantity() &&
1843 "Alignment not a power of 2"));
1844 Alignment = NewAlignment;
1845 }
1846
1847 if (UnpackedNewAlignment > UnpackedAlignment) {
1848 assert(llvm::isPowerOf2_32(UnpackedNewAlignment.getQuantity() &&
1849 "Alignment not a power of 2"));
1850 UnpackedAlignment = UnpackedNewAlignment;
1851 }
1852 }
1853
1854 uint64_t
updateExternalFieldOffset(const FieldDecl * Field,uint64_t ComputedOffset)1855 RecordLayoutBuilder::updateExternalFieldOffset(const FieldDecl *Field,
1856 uint64_t ComputedOffset) {
1857 assert(ExternalFieldOffsets.find(Field) != ExternalFieldOffsets.end() &&
1858 "Field does not have an external offset");
1859
1860 uint64_t ExternalFieldOffset = ExternalFieldOffsets[Field];
1861
1862 if (InferAlignment && ExternalFieldOffset < ComputedOffset) {
1863 // The externally-supplied field offset is before the field offset we
1864 // computed. Assume that the structure is packed.
1865 Alignment = CharUnits::One();
1866 InferAlignment = false;
1867 }
1868
1869 // Use the externally-supplied field offset.
1870 return ExternalFieldOffset;
1871 }
1872
1873 /// \brief Get diagnostic %select index for tag kind for
1874 /// field padding diagnostic message.
1875 /// WARNING: Indexes apply to particular diagnostics only!
1876 ///
1877 /// \returns diagnostic %select index.
getPaddingDiagFromTagKind(TagTypeKind Tag)1878 static unsigned getPaddingDiagFromTagKind(TagTypeKind Tag) {
1879 switch (Tag) {
1880 case TTK_Struct: return 0;
1881 case TTK_Interface: return 1;
1882 case TTK_Class: return 2;
1883 default: llvm_unreachable("Invalid tag kind for field padding diagnostic!");
1884 }
1885 }
1886
CheckFieldPadding(uint64_t Offset,uint64_t UnpaddedOffset,uint64_t UnpackedOffset,unsigned UnpackedAlign,bool isPacked,const FieldDecl * D)1887 void RecordLayoutBuilder::CheckFieldPadding(uint64_t Offset,
1888 uint64_t UnpaddedOffset,
1889 uint64_t UnpackedOffset,
1890 unsigned UnpackedAlign,
1891 bool isPacked,
1892 const FieldDecl *D) {
1893 // We let objc ivars without warning, objc interfaces generally are not used
1894 // for padding tricks.
1895 if (isa<ObjCIvarDecl>(D))
1896 return;
1897
1898 // Don't warn about structs created without a SourceLocation. This can
1899 // be done by clients of the AST, such as codegen.
1900 if (D->getLocation().isInvalid())
1901 return;
1902
1903 unsigned CharBitNum = Context.getTargetInfo().getCharWidth();
1904
1905 // Warn if padding was introduced to the struct/class.
1906 if (!IsUnion && Offset > UnpaddedOffset) {
1907 unsigned PadSize = Offset - UnpaddedOffset;
1908 bool InBits = true;
1909 if (PadSize % CharBitNum == 0) {
1910 PadSize = PadSize / CharBitNum;
1911 InBits = false;
1912 }
1913 if (D->getIdentifier())
1914 Diag(D->getLocation(), diag::warn_padded_struct_field)
1915 << getPaddingDiagFromTagKind(D->getParent()->getTagKind())
1916 << Context.getTypeDeclType(D->getParent())
1917 << PadSize
1918 << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1) // plural or not
1919 << D->getIdentifier();
1920 else
1921 Diag(D->getLocation(), diag::warn_padded_struct_anon_field)
1922 << getPaddingDiagFromTagKind(D->getParent()->getTagKind())
1923 << Context.getTypeDeclType(D->getParent())
1924 << PadSize
1925 << (InBits ? 1 : 0) /*(byte|bit)*/ << (PadSize > 1); // plural or not
1926 }
1927
1928 // Warn if we packed it unnecessarily. If the alignment is 1 byte don't
1929 // bother since there won't be alignment issues.
1930 if (isPacked && UnpackedAlign > CharBitNum && Offset == UnpackedOffset)
1931 Diag(D->getLocation(), diag::warn_unnecessary_packed)
1932 << D->getIdentifier();
1933 }
1934
computeKeyFunction(ASTContext & Context,const CXXRecordDecl * RD)1935 static const CXXMethodDecl *computeKeyFunction(ASTContext &Context,
1936 const CXXRecordDecl *RD) {
1937 // If a class isn't polymorphic it doesn't have a key function.
1938 if (!RD->isPolymorphic())
1939 return nullptr;
1940
1941 // A class that is not externally visible doesn't have a key function. (Or
1942 // at least, there's no point to assigning a key function to such a class;
1943 // this doesn't affect the ABI.)
1944 if (!RD->isExternallyVisible())
1945 return nullptr;
1946
1947 // Template instantiations don't have key functions per Itanium C++ ABI 5.2.6.
1948 // Same behavior as GCC.
1949 TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
1950 if (TSK == TSK_ImplicitInstantiation ||
1951 TSK == TSK_ExplicitInstantiationDeclaration ||
1952 TSK == TSK_ExplicitInstantiationDefinition)
1953 return nullptr;
1954
1955 bool allowInlineFunctions =
1956 Context.getTargetInfo().getCXXABI().canKeyFunctionBeInline();
1957
1958 for (const auto *MD : RD->methods()) {
1959 if (!MD->isVirtual())
1960 continue;
1961
1962 if (MD->isPure())
1963 continue;
1964
1965 // Ignore implicit member functions, they are always marked as inline, but
1966 // they don't have a body until they're defined.
1967 if (MD->isImplicit())
1968 continue;
1969
1970 if (MD->isInlineSpecified())
1971 continue;
1972
1973 if (MD->hasInlineBody())
1974 continue;
1975
1976 // Ignore inline deleted or defaulted functions.
1977 if (!MD->isUserProvided())
1978 continue;
1979
1980 // In certain ABIs, ignore functions with out-of-line inline definitions.
1981 if (!allowInlineFunctions) {
1982 const FunctionDecl *Def;
1983 if (MD->hasBody(Def) && Def->isInlineSpecified())
1984 continue;
1985 }
1986
1987 // We found it.
1988 return MD;
1989 }
1990
1991 return nullptr;
1992 }
1993
1994 DiagnosticBuilder
Diag(SourceLocation Loc,unsigned DiagID)1995 RecordLayoutBuilder::Diag(SourceLocation Loc, unsigned DiagID) {
1996 return Context.getDiagnostics().Report(Loc, DiagID);
1997 }
1998
1999 /// Does the target C++ ABI require us to skip over the tail-padding
2000 /// of the given class (considering it as a base class) when allocating
2001 /// objects?
mustSkipTailPadding(TargetCXXABI ABI,const CXXRecordDecl * RD)2002 static bool mustSkipTailPadding(TargetCXXABI ABI, const CXXRecordDecl *RD) {
2003 switch (ABI.getTailPaddingUseRules()) {
2004 case TargetCXXABI::AlwaysUseTailPadding:
2005 return false;
2006
2007 case TargetCXXABI::UseTailPaddingUnlessPOD03:
2008 // FIXME: To the extent that this is meant to cover the Itanium ABI
2009 // rules, we should implement the restrictions about over-sized
2010 // bitfields:
2011 //
2012 // http://mentorembedded.github.com/cxx-abi/abi.html#POD :
2013 // In general, a type is considered a POD for the purposes of
2014 // layout if it is a POD type (in the sense of ISO C++
2015 // [basic.types]). However, a POD-struct or POD-union (in the
2016 // sense of ISO C++ [class]) with a bitfield member whose
2017 // declared width is wider than the declared type of the
2018 // bitfield is not a POD for the purpose of layout. Similarly,
2019 // an array type is not a POD for the purpose of layout if the
2020 // element type of the array is not a POD for the purpose of
2021 // layout.
2022 //
2023 // Where references to the ISO C++ are made in this paragraph,
2024 // the Technical Corrigendum 1 version of the standard is
2025 // intended.
2026 return RD->isPOD();
2027
2028 case TargetCXXABI::UseTailPaddingUnlessPOD11:
2029 // This is equivalent to RD->getTypeForDecl().isCXX11PODType(),
2030 // but with a lot of abstraction penalty stripped off. This does
2031 // assume that these properties are set correctly even in C++98
2032 // mode; fortunately, that is true because we want to assign
2033 // consistently semantics to the type-traits intrinsics (or at
2034 // least as many of them as possible).
2035 return RD->isTrivial() && RD->isStandardLayout();
2036 }
2037
2038 llvm_unreachable("bad tail-padding use kind");
2039 }
2040
isMsLayout(const RecordDecl * D)2041 static bool isMsLayout(const RecordDecl* D) {
2042 return D->getASTContext().getTargetInfo().getCXXABI().isMicrosoft();
2043 }
2044
2045 // This section contains an implementation of struct layout that is, up to the
2046 // included tests, compatible with cl.exe (2013). The layout produced is
2047 // significantly different than those produced by the Itanium ABI. Here we note
2048 // the most important differences.
2049 //
2050 // * The alignment of bitfields in unions is ignored when computing the
2051 // alignment of the union.
2052 // * The existence of zero-width bitfield that occurs after anything other than
2053 // a non-zero length bitfield is ignored.
2054 // * There is no explicit primary base for the purposes of layout. All bases
2055 // with vfptrs are laid out first, followed by all bases without vfptrs.
2056 // * The Itanium equivalent vtable pointers are split into a vfptr (virtual
2057 // function pointer) and a vbptr (virtual base pointer). They can each be
2058 // shared with a, non-virtual bases. These bases need not be the same. vfptrs
2059 // always occur at offset 0. vbptrs can occur at an arbitrary offset and are
2060 // placed after the lexiographically last non-virtual base. This placement
2061 // is always before fields but can be in the middle of the non-virtual bases
2062 // due to the two-pass layout scheme for non-virtual-bases.
2063 // * Virtual bases sometimes require a 'vtordisp' field that is laid out before
2064 // the virtual base and is used in conjunction with virtual overrides during
2065 // construction and destruction. This is always a 4 byte value and is used as
2066 // an alternative to constructor vtables.
2067 // * vtordisps are allocated in a block of memory with size and alignment equal
2068 // to the alignment of the completed structure (before applying __declspec(
2069 // align())). The vtordisp always occur at the end of the allocation block,
2070 // immediately prior to the virtual base.
2071 // * vfptrs are injected after all bases and fields have been laid out. In
2072 // order to guarantee proper alignment of all fields, the vfptr injection
2073 // pushes all bases and fields back by the alignment imposed by those bases
2074 // and fields. This can potentially add a significant amount of padding.
2075 // vfptrs are always injected at offset 0.
2076 // * vbptrs are injected after all bases and fields have been laid out. In
2077 // order to guarantee proper alignment of all fields, the vfptr injection
2078 // pushes all bases and fields back by the alignment imposed by those bases
2079 // and fields. This can potentially add a significant amount of padding.
2080 // vbptrs are injected immediately after the last non-virtual base as
2081 // lexiographically ordered in the code. If this site isn't pointer aligned
2082 // the vbptr is placed at the next properly aligned location. Enough padding
2083 // is added to guarantee a fit.
2084 // * The last zero sized non-virtual base can be placed at the end of the
2085 // struct (potentially aliasing another object), or may alias with the first
2086 // field, even if they are of the same type.
2087 // * The last zero size virtual base may be placed at the end of the struct
2088 // potentially aliasing another object.
2089 // * The ABI attempts to avoid aliasing of zero sized bases by adding padding
2090 // between bases or vbases with specific properties. The criteria for
2091 // additional padding between two bases is that the first base is zero sized
2092 // or ends with a zero sized subobject and the second base is zero sized or
2093 // trails with a zero sized base or field (sharing of vfptrs can reorder the
2094 // layout of the so the leading base is not always the first one declared).
2095 // This rule does take into account fields that are not records, so padding
2096 // will occur even if the last field is, e.g. an int. The padding added for
2097 // bases is 1 byte. The padding added between vbases depends on the alignment
2098 // of the object but is at least 4 bytes (in both 32 and 64 bit modes).
2099 // * There is no concept of non-virtual alignment, non-virtual alignment and
2100 // alignment are always identical.
2101 // * There is a distinction between alignment and required alignment.
2102 // __declspec(align) changes the required alignment of a struct. This
2103 // alignment is _always_ obeyed, even in the presence of #pragma pack. A
2104 // record inherites required alignment from all of its fields an bases.
2105 // * __declspec(align) on bitfields has the effect of changing the bitfield's
2106 // alignment instead of its required alignment. This is the only known way
2107 // to make the alignment of a struct bigger than 8. Interestingly enough
2108 // this alignment is also immune to the effects of #pragma pack and can be
2109 // used to create structures with large alignment under #pragma pack.
2110 // However, because it does not impact required alignment, such a structure,
2111 // when used as a field or base, will not be aligned if #pragma pack is
2112 // still active at the time of use.
2113 //
2114 // Known incompatibilities:
2115 // * all: #pragma pack between fields in a record
2116 // * 2010 and back: If the last field in a record is a bitfield, every object
2117 // laid out after the record will have extra padding inserted before it. The
2118 // extra padding will have size equal to the size of the storage class of the
2119 // bitfield. 0 sized bitfields don't exhibit this behavior and the extra
2120 // padding can be avoided by adding a 0 sized bitfield after the non-zero-
2121 // sized bitfield.
2122 // * 2012 and back: In 64-bit mode, if the alignment of a record is 16 or
2123 // greater due to __declspec(align()) then a second layout phase occurs after
2124 // The locations of the vf and vb pointers are known. This layout phase
2125 // suffers from the "last field is a bitfield" bug in 2010 and results in
2126 // _every_ field getting padding put in front of it, potentially including the
2127 // vfptr, leaving the vfprt at a non-zero location which results in a fault if
2128 // anything tries to read the vftbl. The second layout phase also treats
2129 // bitfields as separate entities and gives them each storage rather than
2130 // packing them. Additionally, because this phase appears to perform a
2131 // (an unstable) sort on the members before laying them out and because merged
2132 // bitfields have the same address, the bitfields end up in whatever order
2133 // the sort left them in, a behavior we could never hope to replicate.
2134
2135 namespace {
2136 struct MicrosoftRecordLayoutBuilder {
2137 struct ElementInfo {
2138 CharUnits Size;
2139 CharUnits Alignment;
2140 };
2141 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetsMapTy;
MicrosoftRecordLayoutBuilder__anond999b0ee0211::MicrosoftRecordLayoutBuilder2142 MicrosoftRecordLayoutBuilder(const ASTContext &Context) : Context(Context) {}
2143 private:
2144 MicrosoftRecordLayoutBuilder(const MicrosoftRecordLayoutBuilder &)
2145 LLVM_DELETED_FUNCTION;
2146 void operator=(const MicrosoftRecordLayoutBuilder &) LLVM_DELETED_FUNCTION;
2147 public:
2148 void layout(const RecordDecl *RD);
2149 void cxxLayout(const CXXRecordDecl *RD);
2150 /// \brief Initializes size and alignment and honors some flags.
2151 void initializeLayout(const RecordDecl *RD);
2152 /// \brief Initialized C++ layout, compute alignment and virtual alignment and
2153 /// existence of vfptrs and vbptrs. Alignment is needed before the vfptr is
2154 /// laid out.
2155 void initializeCXXLayout(const CXXRecordDecl *RD);
2156 void layoutNonVirtualBases(const CXXRecordDecl *RD);
2157 void layoutNonVirtualBase(const CXXRecordDecl *BaseDecl,
2158 const ASTRecordLayout &BaseLayout,
2159 const ASTRecordLayout *&PreviousBaseLayout);
2160 void injectVFPtr(const CXXRecordDecl *RD);
2161 void injectVBPtr(const CXXRecordDecl *RD);
2162 /// \brief Lays out the fields of the record. Also rounds size up to
2163 /// alignment.
2164 void layoutFields(const RecordDecl *RD);
2165 void layoutField(const FieldDecl *FD);
2166 void layoutBitField(const FieldDecl *FD);
2167 /// \brief Lays out a single zero-width bit-field in the record and handles
2168 /// special cases associated with zero-width bit-fields.
2169 void layoutZeroWidthBitField(const FieldDecl *FD);
2170 void layoutVirtualBases(const CXXRecordDecl *RD);
2171 void finalizeLayout(const RecordDecl *RD);
2172 /// \brief Gets the size and alignment of a base taking pragma pack and
2173 /// __declspec(align) into account.
2174 ElementInfo getAdjustedElementInfo(const ASTRecordLayout &Layout);
2175 /// \brief Gets the size and alignment of a field taking pragma pack and
2176 /// __declspec(align) into account. It also updates RequiredAlignment as a
2177 /// side effect because it is most convenient to do so here.
2178 ElementInfo getAdjustedElementInfo(const FieldDecl *FD);
2179 /// \brief Places a field at an offset in CharUnits.
placeFieldAtOffset__anond999b0ee0211::MicrosoftRecordLayoutBuilder2180 void placeFieldAtOffset(CharUnits FieldOffset) {
2181 FieldOffsets.push_back(Context.toBits(FieldOffset));
2182 }
2183 /// \brief Places a bitfield at a bit offset.
placeFieldAtBitOffset__anond999b0ee0211::MicrosoftRecordLayoutBuilder2184 void placeFieldAtBitOffset(uint64_t FieldOffset) {
2185 FieldOffsets.push_back(FieldOffset);
2186 }
2187 /// \brief Compute the set of virtual bases for which vtordisps are required.
2188 llvm::SmallPtrSet<const CXXRecordDecl *, 2>
2189 computeVtorDispSet(const CXXRecordDecl *RD);
2190 const ASTContext &Context;
2191 /// \brief The size of the record being laid out.
2192 CharUnits Size;
2193 /// \brief The non-virtual size of the record layout.
2194 CharUnits NonVirtualSize;
2195 /// \brief The data size of the record layout.
2196 CharUnits DataSize;
2197 /// \brief The current alignment of the record layout.
2198 CharUnits Alignment;
2199 /// \brief The maximum allowed field alignment. This is set by #pragma pack.
2200 CharUnits MaxFieldAlignment;
2201 /// \brief The alignment that this record must obey. This is imposed by
2202 /// __declspec(align()) on the record itself or one of its fields or bases.
2203 CharUnits RequiredAlignment;
2204 /// \brief The size of the allocation of the currently active bitfield.
2205 /// This value isn't meaningful unless LastFieldIsNonZeroWidthBitfield
2206 /// is true.
2207 CharUnits CurrentBitfieldSize;
2208 /// \brief Offset to the virtual base table pointer (if one exists).
2209 CharUnits VBPtrOffset;
2210 /// \brief The size and alignment info of a pointer.
2211 ElementInfo PointerInfo;
2212 /// \brief The primary base class (if one exists).
2213 const CXXRecordDecl *PrimaryBase;
2214 /// \brief The class we share our vb-pointer with.
2215 const CXXRecordDecl *SharedVBPtrBase;
2216 /// \brief The collection of field offsets.
2217 SmallVector<uint64_t, 16> FieldOffsets;
2218 /// \brief Base classes and their offsets in the record.
2219 BaseOffsetsMapTy Bases;
2220 /// \brief virtual base classes and their offsets in the record.
2221 ASTRecordLayout::VBaseOffsetsMapTy VBases;
2222 /// \brief The number of remaining bits in our last bitfield allocation.
2223 /// This value isn't meaningful unless LastFieldIsNonZeroWidthBitfield is
2224 /// true.
2225 unsigned RemainingBitsInField;
2226 bool IsUnion : 1;
2227 /// \brief True if the last field laid out was a bitfield and was not 0
2228 /// width.
2229 bool LastFieldIsNonZeroWidthBitfield : 1;
2230 /// \brief True if the class has its own vftable pointer.
2231 bool HasOwnVFPtr : 1;
2232 /// \brief True if the class has a vbtable pointer.
2233 bool HasVBPtr : 1;
2234 /// \brief True if the last sub-object within the type is zero sized or the
2235 /// object itself is zero sized. This *does not* count members that are not
2236 /// records. Only used for MS-ABI.
2237 bool EndsWithZeroSizedObject : 1;
2238 /// \brief True if this class is zero sized or first base is zero sized or
2239 /// has this property. Only used for MS-ABI.
2240 bool LeadsWithZeroSizedBase : 1;
2241 };
2242 } // namespace
2243
2244 MicrosoftRecordLayoutBuilder::ElementInfo
getAdjustedElementInfo(const ASTRecordLayout & Layout)2245 MicrosoftRecordLayoutBuilder::getAdjustedElementInfo(
2246 const ASTRecordLayout &Layout) {
2247 ElementInfo Info;
2248 Info.Alignment = Layout.getAlignment();
2249 // Respect pragma pack.
2250 if (!MaxFieldAlignment.isZero())
2251 Info.Alignment = std::min(Info.Alignment, MaxFieldAlignment);
2252 // Track zero-sized subobjects here where it's already available.
2253 EndsWithZeroSizedObject = Layout.hasZeroSizedSubObject();
2254 // Respect required alignment, this is necessary because we may have adjusted
2255 // the alignment in the case of pragam pack. Note that the required alignment
2256 // doesn't actually apply to the struct alignment at this point.
2257 Alignment = std::max(Alignment, Info.Alignment);
2258 RequiredAlignment = std::max(RequiredAlignment, Layout.getRequiredAlignment());
2259 Info.Alignment = std::max(Info.Alignment, Layout.getRequiredAlignment());
2260 Info.Size = Layout.getNonVirtualSize();
2261 return Info;
2262 }
2263
2264 MicrosoftRecordLayoutBuilder::ElementInfo
getAdjustedElementInfo(const FieldDecl * FD)2265 MicrosoftRecordLayoutBuilder::getAdjustedElementInfo(
2266 const FieldDecl *FD) {
2267 ElementInfo Info;
2268 std::tie(Info.Size, Info.Alignment) =
2269 Context.getTypeInfoInChars(FD->getType());
2270 // Respect align attributes.
2271 CharUnits FieldRequiredAlignment =
2272 Context.toCharUnitsFromBits(FD->getMaxAlignment());
2273 // Respect attributes applied to subobjects of the field.
2274 if (FD->isBitField())
2275 // For some reason __declspec align impacts alignment rather than required
2276 // alignment when it is applied to bitfields.
2277 Info.Alignment = std::max(Info.Alignment, FieldRequiredAlignment);
2278 else {
2279 if (auto RT =
2280 FD->getType()->getBaseElementTypeUnsafe()->getAs<RecordType>()) {
2281 auto const &Layout = Context.getASTRecordLayout(RT->getDecl());
2282 EndsWithZeroSizedObject = Layout.hasZeroSizedSubObject();
2283 FieldRequiredAlignment = std::max(FieldRequiredAlignment,
2284 Layout.getRequiredAlignment());
2285 }
2286 // Capture required alignment as a side-effect.
2287 RequiredAlignment = std::max(RequiredAlignment, FieldRequiredAlignment);
2288 }
2289 // Respect pragma pack, attribute pack and declspec align
2290 if (!MaxFieldAlignment.isZero())
2291 Info.Alignment = std::min(Info.Alignment, MaxFieldAlignment);
2292 if (FD->hasAttr<PackedAttr>())
2293 Info.Alignment = CharUnits::One();
2294 Info.Alignment = std::max(Info.Alignment, FieldRequiredAlignment);
2295 return Info;
2296 }
2297
layout(const RecordDecl * RD)2298 void MicrosoftRecordLayoutBuilder::layout(const RecordDecl *RD) {
2299 initializeLayout(RD);
2300 layoutFields(RD);
2301 DataSize = Size = Size.RoundUpToAlignment(Alignment);
2302 RequiredAlignment = std::max(
2303 RequiredAlignment, Context.toCharUnitsFromBits(RD->getMaxAlignment()));
2304 finalizeLayout(RD);
2305 }
2306
cxxLayout(const CXXRecordDecl * RD)2307 void MicrosoftRecordLayoutBuilder::cxxLayout(const CXXRecordDecl *RD) {
2308 initializeLayout(RD);
2309 initializeCXXLayout(RD);
2310 layoutNonVirtualBases(RD);
2311 layoutFields(RD);
2312 injectVBPtr(RD);
2313 injectVFPtr(RD);
2314 if (HasOwnVFPtr || (HasVBPtr && !SharedVBPtrBase))
2315 Alignment = std::max(Alignment, PointerInfo.Alignment);
2316 auto RoundingAlignment = Alignment;
2317 if (!MaxFieldAlignment.isZero())
2318 RoundingAlignment = std::min(RoundingAlignment, MaxFieldAlignment);
2319 NonVirtualSize = Size = Size.RoundUpToAlignment(RoundingAlignment);
2320 RequiredAlignment = std::max(
2321 RequiredAlignment, Context.toCharUnitsFromBits(RD->getMaxAlignment()));
2322 layoutVirtualBases(RD);
2323 finalizeLayout(RD);
2324 }
2325
initializeLayout(const RecordDecl * RD)2326 void MicrosoftRecordLayoutBuilder::initializeLayout(const RecordDecl *RD) {
2327 IsUnion = RD->isUnion();
2328 Size = CharUnits::Zero();
2329 Alignment = CharUnits::One();
2330 // In 64-bit mode we always perform an alignment step after laying out vbases.
2331 // In 32-bit mode we do not. The check to see if we need to perform alignment
2332 // checks the RequiredAlignment field and performs alignment if it isn't 0.
2333 RequiredAlignment = Context.getTargetInfo().getPointerWidth(0) == 64 ?
2334 CharUnits::One() : CharUnits::Zero();
2335 // Compute the maximum field alignment.
2336 MaxFieldAlignment = CharUnits::Zero();
2337 // Honor the default struct packing maximum alignment flag.
2338 if (unsigned DefaultMaxFieldAlignment = Context.getLangOpts().PackStruct)
2339 MaxFieldAlignment = CharUnits::fromQuantity(DefaultMaxFieldAlignment);
2340 // Honor the packing attribute. The MS-ABI ignores pragma pack if its larger
2341 // than the pointer size.
2342 if (const MaxFieldAlignmentAttr *MFAA = RD->getAttr<MaxFieldAlignmentAttr>()){
2343 unsigned PackedAlignment = MFAA->getAlignment();
2344 if (PackedAlignment <= Context.getTargetInfo().getPointerWidth(0))
2345 MaxFieldAlignment = Context.toCharUnitsFromBits(PackedAlignment);
2346 }
2347 // Packed attribute forces max field alignment to be 1.
2348 if (RD->hasAttr<PackedAttr>())
2349 MaxFieldAlignment = CharUnits::One();
2350 }
2351
2352 void
initializeCXXLayout(const CXXRecordDecl * RD)2353 MicrosoftRecordLayoutBuilder::initializeCXXLayout(const CXXRecordDecl *RD) {
2354 EndsWithZeroSizedObject = false;
2355 LeadsWithZeroSizedBase = false;
2356 HasOwnVFPtr = false;
2357 HasVBPtr = false;
2358 PrimaryBase = nullptr;
2359 SharedVBPtrBase = nullptr;
2360 // Calculate pointer size and alignment. These are used for vfptr and vbprt
2361 // injection.
2362 PointerInfo.Size =
2363 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
2364 PointerInfo.Alignment = PointerInfo.Size;
2365 // Respect pragma pack.
2366 if (!MaxFieldAlignment.isZero())
2367 PointerInfo.Alignment = std::min(PointerInfo.Alignment, MaxFieldAlignment);
2368 }
2369
2370 void
layoutNonVirtualBases(const CXXRecordDecl * RD)2371 MicrosoftRecordLayoutBuilder::layoutNonVirtualBases(const CXXRecordDecl *RD) {
2372 // The MS-ABI lays out all bases that contain leading vfptrs before it lays
2373 // out any bases that do not contain vfptrs. We implement this as two passes
2374 // over the bases. This approach guarantees that the primary base is laid out
2375 // first. We use these passes to calculate some additional aggregated
2376 // information about the bases, such as reqruied alignment and the presence of
2377 // zero sized members.
2378 const ASTRecordLayout *PreviousBaseLayout = nullptr;
2379 // Iterate through the bases and lay out the non-virtual ones.
2380 for (const auto &I : RD->bases()) {
2381 const CXXRecordDecl *BaseDecl = I.getType()->getAsCXXRecordDecl();
2382 const ASTRecordLayout &BaseLayout = Context.getASTRecordLayout(BaseDecl);
2383 // Mark and skip virtual bases.
2384 if (I.isVirtual()) {
2385 HasVBPtr = true;
2386 continue;
2387 }
2388 // Check fo a base to share a VBPtr with.
2389 if (!SharedVBPtrBase && BaseLayout.hasVBPtr()) {
2390 SharedVBPtrBase = BaseDecl;
2391 HasVBPtr = true;
2392 }
2393 // Only lay out bases with extendable VFPtrs on the first pass.
2394 if (!BaseLayout.hasExtendableVFPtr())
2395 continue;
2396 // If we don't have a primary base, this one qualifies.
2397 if (!PrimaryBase) {
2398 PrimaryBase = BaseDecl;
2399 LeadsWithZeroSizedBase = BaseLayout.leadsWithZeroSizedBase();
2400 }
2401 // Lay out the base.
2402 layoutNonVirtualBase(BaseDecl, BaseLayout, PreviousBaseLayout);
2403 }
2404 // Figure out if we need a fresh VFPtr for this class.
2405 if (!PrimaryBase && RD->isDynamicClass())
2406 for (CXXRecordDecl::method_iterator i = RD->method_begin(),
2407 e = RD->method_end();
2408 !HasOwnVFPtr && i != e; ++i)
2409 HasOwnVFPtr = i->isVirtual() && i->size_overridden_methods() == 0;
2410 // If we don't have a primary base then we have a leading object that could
2411 // itself lead with a zero-sized object, something we track.
2412 bool CheckLeadingLayout = !PrimaryBase;
2413 // Iterate through the bases and lay out the non-virtual ones.
2414 for (const auto &I : RD->bases()) {
2415 if (I.isVirtual())
2416 continue;
2417 const CXXRecordDecl *BaseDecl = I.getType()->getAsCXXRecordDecl();
2418 const ASTRecordLayout &BaseLayout = Context.getASTRecordLayout(BaseDecl);
2419 // Only lay out bases without extendable VFPtrs on the second pass.
2420 if (BaseLayout.hasExtendableVFPtr()) {
2421 VBPtrOffset = Bases[BaseDecl] + BaseLayout.getNonVirtualSize();
2422 continue;
2423 }
2424 // If this is the first layout, check to see if it leads with a zero sized
2425 // object. If it does, so do we.
2426 if (CheckLeadingLayout) {
2427 CheckLeadingLayout = false;
2428 LeadsWithZeroSizedBase = BaseLayout.leadsWithZeroSizedBase();
2429 }
2430 // Lay out the base.
2431 layoutNonVirtualBase(BaseDecl, BaseLayout, PreviousBaseLayout);
2432 VBPtrOffset = Bases[BaseDecl] + BaseLayout.getNonVirtualSize();
2433 }
2434 // Set our VBPtroffset if we know it at this point.
2435 if (!HasVBPtr)
2436 VBPtrOffset = CharUnits::fromQuantity(-1);
2437 else if (SharedVBPtrBase) {
2438 const ASTRecordLayout &Layout = Context.getASTRecordLayout(SharedVBPtrBase);
2439 VBPtrOffset = Bases[SharedVBPtrBase] + Layout.getVBPtrOffset();
2440 }
2441 }
2442
layoutNonVirtualBase(const CXXRecordDecl * BaseDecl,const ASTRecordLayout & BaseLayout,const ASTRecordLayout * & PreviousBaseLayout)2443 void MicrosoftRecordLayoutBuilder::layoutNonVirtualBase(
2444 const CXXRecordDecl *BaseDecl,
2445 const ASTRecordLayout &BaseLayout,
2446 const ASTRecordLayout *&PreviousBaseLayout) {
2447 // Insert padding between two bases if the left first one is zero sized or
2448 // contains a zero sized subobject and the right is zero sized or one leads
2449 // with a zero sized base.
2450 if (PreviousBaseLayout && PreviousBaseLayout->hasZeroSizedSubObject() &&
2451 BaseLayout.leadsWithZeroSizedBase())
2452 Size++;
2453 ElementInfo Info = getAdjustedElementInfo(BaseLayout);
2454 CharUnits BaseOffset = Size.RoundUpToAlignment(Info.Alignment);
2455 Bases.insert(std::make_pair(BaseDecl, BaseOffset));
2456 Size = BaseOffset + BaseLayout.getNonVirtualSize();
2457 PreviousBaseLayout = &BaseLayout;
2458 }
2459
layoutFields(const RecordDecl * RD)2460 void MicrosoftRecordLayoutBuilder::layoutFields(const RecordDecl *RD) {
2461 LastFieldIsNonZeroWidthBitfield = false;
2462 for (const auto *Field : RD->fields())
2463 layoutField(Field);
2464 }
2465
layoutField(const FieldDecl * FD)2466 void MicrosoftRecordLayoutBuilder::layoutField(const FieldDecl *FD) {
2467 if (FD->isBitField()) {
2468 layoutBitField(FD);
2469 return;
2470 }
2471 LastFieldIsNonZeroWidthBitfield = false;
2472 ElementInfo Info = getAdjustedElementInfo(FD);
2473 Alignment = std::max(Alignment, Info.Alignment);
2474 if (IsUnion) {
2475 placeFieldAtOffset(CharUnits::Zero());
2476 Size = std::max(Size, Info.Size);
2477 } else {
2478 CharUnits FieldOffset = Size.RoundUpToAlignment(Info.Alignment);
2479 placeFieldAtOffset(FieldOffset);
2480 Size = FieldOffset + Info.Size;
2481 }
2482 }
2483
layoutBitField(const FieldDecl * FD)2484 void MicrosoftRecordLayoutBuilder::layoutBitField(const FieldDecl *FD) {
2485 unsigned Width = FD->getBitWidthValue(Context);
2486 if (Width == 0) {
2487 layoutZeroWidthBitField(FD);
2488 return;
2489 }
2490 ElementInfo Info = getAdjustedElementInfo(FD);
2491 // Clamp the bitfield to a containable size for the sake of being able
2492 // to lay them out. Sema will throw an error.
2493 if (Width > Context.toBits(Info.Size))
2494 Width = Context.toBits(Info.Size);
2495 // Check to see if this bitfield fits into an existing allocation. Note:
2496 // MSVC refuses to pack bitfields of formal types with different sizes
2497 // into the same allocation.
2498 if (!IsUnion && LastFieldIsNonZeroWidthBitfield &&
2499 CurrentBitfieldSize == Info.Size && Width <= RemainingBitsInField) {
2500 placeFieldAtBitOffset(Context.toBits(Size) - RemainingBitsInField);
2501 RemainingBitsInField -= Width;
2502 return;
2503 }
2504 LastFieldIsNonZeroWidthBitfield = true;
2505 CurrentBitfieldSize = Info.Size;
2506 if (IsUnion) {
2507 placeFieldAtOffset(CharUnits::Zero());
2508 Size = std::max(Size, Info.Size);
2509 // TODO: Add a Sema warning that MS ignores bitfield alignment in unions.
2510 } else {
2511 // Allocate a new block of memory and place the bitfield in it.
2512 CharUnits FieldOffset = Size.RoundUpToAlignment(Info.Alignment);
2513 placeFieldAtOffset(FieldOffset);
2514 Size = FieldOffset + Info.Size;
2515 Alignment = std::max(Alignment, Info.Alignment);
2516 RemainingBitsInField = Context.toBits(Info.Size) - Width;
2517 }
2518 }
2519
2520 void
layoutZeroWidthBitField(const FieldDecl * FD)2521 MicrosoftRecordLayoutBuilder::layoutZeroWidthBitField(const FieldDecl *FD) {
2522 // Zero-width bitfields are ignored unless they follow a non-zero-width
2523 // bitfield.
2524 if (!LastFieldIsNonZeroWidthBitfield) {
2525 placeFieldAtOffset(IsUnion ? CharUnits::Zero() : Size);
2526 // TODO: Add a Sema warning that MS ignores alignment for zero
2527 // sized bitfields that occur after zero-size bitfields or non-bitfields.
2528 return;
2529 }
2530 LastFieldIsNonZeroWidthBitfield = false;
2531 ElementInfo Info = getAdjustedElementInfo(FD);
2532 if (IsUnion) {
2533 placeFieldAtOffset(CharUnits::Zero());
2534 Size = std::max(Size, Info.Size);
2535 // TODO: Add a Sema warning that MS ignores bitfield alignment in unions.
2536 } else {
2537 // Round up the current record size to the field's alignment boundary.
2538 CharUnits FieldOffset = Size.RoundUpToAlignment(Info.Alignment);
2539 placeFieldAtOffset(FieldOffset);
2540 Size = FieldOffset;
2541 Alignment = std::max(Alignment, Info.Alignment);
2542 }
2543 }
2544
injectVBPtr(const CXXRecordDecl * RD)2545 void MicrosoftRecordLayoutBuilder::injectVBPtr(const CXXRecordDecl *RD) {
2546 if (!HasVBPtr || SharedVBPtrBase)
2547 return;
2548 // Inject the VBPointer at the injection site.
2549 CharUnits InjectionSite = VBPtrOffset;
2550 // But before we do, make sure it's properly aligned.
2551 VBPtrOffset = VBPtrOffset.RoundUpToAlignment(PointerInfo.Alignment);
2552 // Determine where the first field should be laid out after the vbptr.
2553 CharUnits FieldStart = VBPtrOffset + PointerInfo.Size;
2554 // Make sure that the amount we push the fields back by is a multiple of the
2555 // alignment.
2556 CharUnits Offset = (FieldStart - InjectionSite).RoundUpToAlignment(
2557 std::max(RequiredAlignment, Alignment));
2558 // Increase the size of the object and push back all fields by the offset
2559 // amount.
2560 Size += Offset;
2561 for (SmallVector<uint64_t, 16>::iterator i = FieldOffsets.begin(),
2562 e = FieldOffsets.end();
2563 i != e; ++i)
2564 *i += Context.toBits(Offset);
2565 for (BaseOffsetsMapTy::iterator i = Bases.begin(), e = Bases.end();
2566 i != e; ++i)
2567 if (i->second >= InjectionSite)
2568 i->second += Offset;
2569 }
2570
injectVFPtr(const CXXRecordDecl * RD)2571 void MicrosoftRecordLayoutBuilder::injectVFPtr(const CXXRecordDecl *RD) {
2572 if (!HasOwnVFPtr)
2573 return;
2574 // Make sure that the amount we push the struct back by is a multiple of the
2575 // alignment.
2576 CharUnits Offset = PointerInfo.Size.RoundUpToAlignment(
2577 std::max(RequiredAlignment, Alignment));
2578 // Increase the size of the object and push back all fields, the vbptr and all
2579 // bases by the offset amount.
2580 Size += Offset;
2581 for (SmallVectorImpl<uint64_t>::iterator i = FieldOffsets.begin(),
2582 e = FieldOffsets.end();
2583 i != e; ++i)
2584 *i += Context.toBits(Offset);
2585 if (HasVBPtr)
2586 VBPtrOffset += Offset;
2587 for (BaseOffsetsMapTy::iterator i = Bases.begin(), e = Bases.end();
2588 i != e; ++i)
2589 i->second += Offset;
2590 }
2591
layoutVirtualBases(const CXXRecordDecl * RD)2592 void MicrosoftRecordLayoutBuilder::layoutVirtualBases(const CXXRecordDecl *RD) {
2593 if (!HasVBPtr)
2594 return;
2595 // Vtordisps are always 4 bytes (even in 64-bit mode)
2596 CharUnits VtorDispSize = CharUnits::fromQuantity(4);
2597 CharUnits VtorDispAlignment = VtorDispSize;
2598 // vtordisps respect pragma pack.
2599 if (!MaxFieldAlignment.isZero())
2600 VtorDispAlignment = std::min(VtorDispAlignment, MaxFieldAlignment);
2601 // The alignment of the vtordisp is at least the required alignment of the
2602 // entire record. This requirement may be present to support vtordisp
2603 // injection.
2604 for (const auto &I : RD->vbases()) {
2605 const CXXRecordDecl *BaseDecl = I.getType()->getAsCXXRecordDecl();
2606 const ASTRecordLayout &BaseLayout = Context.getASTRecordLayout(BaseDecl);
2607 RequiredAlignment =
2608 std::max(RequiredAlignment, BaseLayout.getRequiredAlignment());
2609 }
2610 VtorDispAlignment = std::max(VtorDispAlignment, RequiredAlignment);
2611 // Compute the vtordisp set.
2612 llvm::SmallPtrSet<const CXXRecordDecl *, 2> HasVtordispSet =
2613 computeVtorDispSet(RD);
2614 // Iterate through the virtual bases and lay them out.
2615 const ASTRecordLayout *PreviousBaseLayout = nullptr;
2616 for (const auto &I : RD->vbases()) {
2617 const CXXRecordDecl *BaseDecl = I.getType()->getAsCXXRecordDecl();
2618 const ASTRecordLayout &BaseLayout = Context.getASTRecordLayout(BaseDecl);
2619 bool HasVtordisp = HasVtordispSet.count(BaseDecl);
2620 // Insert padding between two bases if the left first one is zero sized or
2621 // contains a zero sized subobject and the right is zero sized or one leads
2622 // with a zero sized base. The padding between virtual bases is 4
2623 // bytes (in both 32 and 64 bits modes) and always involves rounding up to
2624 // the required alignment, we don't know why.
2625 if ((PreviousBaseLayout && PreviousBaseLayout->hasZeroSizedSubObject() &&
2626 BaseLayout.leadsWithZeroSizedBase()) || HasVtordisp)
2627 Size = Size.RoundUpToAlignment(VtorDispAlignment) + VtorDispSize;
2628 // Insert the virtual base.
2629 ElementInfo Info = getAdjustedElementInfo(BaseLayout);
2630 CharUnits BaseOffset = Size.RoundUpToAlignment(Info.Alignment);
2631 VBases.insert(std::make_pair(BaseDecl,
2632 ASTRecordLayout::VBaseInfo(BaseOffset, HasVtordisp)));
2633 Size = BaseOffset + BaseLayout.getNonVirtualSize();
2634 PreviousBaseLayout = &BaseLayout;
2635 }
2636 }
2637
finalizeLayout(const RecordDecl * RD)2638 void MicrosoftRecordLayoutBuilder::finalizeLayout(const RecordDecl *RD) {
2639 // Respect required alignment. Note that in 32-bit mode Required alignment
2640 // may be 0 nad cause size not to be updated.
2641 DataSize = Size;
2642 if (!RequiredAlignment.isZero()) {
2643 Alignment = std::max(Alignment, RequiredAlignment);
2644 auto RoundingAlignment = Alignment;
2645 if (!MaxFieldAlignment.isZero())
2646 RoundingAlignment = std::min(RoundingAlignment, MaxFieldAlignment);
2647 RoundingAlignment = std::max(RoundingAlignment, RequiredAlignment);
2648 Size = Size.RoundUpToAlignment(RoundingAlignment);
2649 }
2650 // Zero-sized structures have size equal to their alignment.
2651 if (Size.isZero()) {
2652 EndsWithZeroSizedObject = true;
2653 LeadsWithZeroSizedBase = true;
2654 Size = Alignment;
2655 }
2656 }
2657
2658 // Recursively walks the non-virtual bases of a class and determines if any of
2659 // them are in the bases with overridden methods set.
RequiresVtordisp(const llvm::SmallPtrSet<const CXXRecordDecl *,2> & BasesWithOverriddenMethods,const CXXRecordDecl * RD)2660 static bool RequiresVtordisp(
2661 const llvm::SmallPtrSet<const CXXRecordDecl *, 2> &
2662 BasesWithOverriddenMethods,
2663 const CXXRecordDecl *RD) {
2664 if (BasesWithOverriddenMethods.count(RD))
2665 return true;
2666 // If any of a virtual bases non-virtual bases (recursively) requires a
2667 // vtordisp than so does this virtual base.
2668 for (const auto &I : RD->bases())
2669 if (!I.isVirtual() &&
2670 RequiresVtordisp(BasesWithOverriddenMethods,
2671 I.getType()->getAsCXXRecordDecl()))
2672 return true;
2673 return false;
2674 }
2675
2676 llvm::SmallPtrSet<const CXXRecordDecl *, 2>
computeVtorDispSet(const CXXRecordDecl * RD)2677 MicrosoftRecordLayoutBuilder::computeVtorDispSet(const CXXRecordDecl *RD) {
2678 llvm::SmallPtrSet<const CXXRecordDecl *, 2> HasVtordispSet;
2679
2680 // /vd2 or #pragma vtordisp(2): Always use vtordisps for virtual bases with
2681 // vftables.
2682 if (RD->getMSVtorDispMode() == MSVtorDispAttr::ForVFTable) {
2683 for (const auto &I : RD->vbases()) {
2684 const CXXRecordDecl *BaseDecl = I.getType()->getAsCXXRecordDecl();
2685 const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl);
2686 if (Layout.hasExtendableVFPtr())
2687 HasVtordispSet.insert(BaseDecl);
2688 }
2689 return HasVtordispSet;
2690 }
2691
2692 // If any of our bases need a vtordisp for this type, so do we. Check our
2693 // direct bases for vtordisp requirements.
2694 for (const auto &I : RD->bases()) {
2695 const CXXRecordDecl *BaseDecl = I.getType()->getAsCXXRecordDecl();
2696 const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl);
2697 for (const auto &bi : Layout.getVBaseOffsetsMap())
2698 if (bi.second.hasVtorDisp())
2699 HasVtordispSet.insert(bi.first);
2700 }
2701 // We don't introduce any additional vtordisps if either:
2702 // * A user declared constructor or destructor aren't declared.
2703 // * #pragma vtordisp(0) or the /vd0 flag are in use.
2704 if ((!RD->hasUserDeclaredConstructor() && !RD->hasUserDeclaredDestructor()) ||
2705 RD->getMSVtorDispMode() == MSVtorDispAttr::Never)
2706 return HasVtordispSet;
2707 // /vd1 or #pragma vtordisp(1): Try to guess based on whether we think it's
2708 // possible for a partially constructed object with virtual base overrides to
2709 // escape a non-trivial constructor.
2710 assert(RD->getMSVtorDispMode() == MSVtorDispAttr::ForVBaseOverride);
2711 // Compute a set of base classes which define methods we override. A virtual
2712 // base in this set will require a vtordisp. A virtual base that transitively
2713 // contains one of these bases as a non-virtual base will also require a
2714 // vtordisp.
2715 llvm::SmallPtrSet<const CXXMethodDecl *, 8> Work;
2716 llvm::SmallPtrSet<const CXXRecordDecl *, 2> BasesWithOverriddenMethods;
2717 // Seed the working set with our non-destructor virtual methods.
2718 for (const auto *I : RD->methods())
2719 if (I->isVirtual() && !isa<CXXDestructorDecl>(I))
2720 Work.insert(I);
2721 while (!Work.empty()) {
2722 const CXXMethodDecl *MD = *Work.begin();
2723 CXXMethodDecl::method_iterator i = MD->begin_overridden_methods(),
2724 e = MD->end_overridden_methods();
2725 // If a virtual method has no-overrides it lives in its parent's vtable.
2726 if (i == e)
2727 BasesWithOverriddenMethods.insert(MD->getParent());
2728 else
2729 Work.insert(i, e);
2730 // We've finished processing this element, remove it from the working set.
2731 Work.erase(MD);
2732 }
2733 // For each of our virtual bases, check if it is in the set of overridden
2734 // bases or if it transitively contains a non-virtual base that is.
2735 for (const auto &I : RD->vbases()) {
2736 const CXXRecordDecl *BaseDecl = I.getType()->getAsCXXRecordDecl();
2737 if (!HasVtordispSet.count(BaseDecl) &&
2738 RequiresVtordisp(BasesWithOverriddenMethods, BaseDecl))
2739 HasVtordispSet.insert(BaseDecl);
2740 }
2741 return HasVtordispSet;
2742 }
2743
2744 /// \brief Get or compute information about the layout of the specified record
2745 /// (struct/union/class), which indicates its size and field position
2746 /// information.
2747 const ASTRecordLayout *
BuildMicrosoftASTRecordLayout(const RecordDecl * D) const2748 ASTContext::BuildMicrosoftASTRecordLayout(const RecordDecl *D) const {
2749 MicrosoftRecordLayoutBuilder Builder(*this);
2750 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
2751 Builder.cxxLayout(RD);
2752 return new (*this) ASTRecordLayout(
2753 *this, Builder.Size, Builder.Alignment, Builder.RequiredAlignment,
2754 Builder.HasOwnVFPtr,
2755 Builder.HasOwnVFPtr || Builder.PrimaryBase,
2756 Builder.VBPtrOffset, Builder.NonVirtualSize, Builder.FieldOffsets.data(),
2757 Builder.FieldOffsets.size(), Builder.NonVirtualSize,
2758 Builder.Alignment, CharUnits::Zero(), Builder.PrimaryBase,
2759 false, Builder.SharedVBPtrBase,
2760 Builder.EndsWithZeroSizedObject, Builder.LeadsWithZeroSizedBase,
2761 Builder.Bases, Builder.VBases);
2762 } else {
2763 Builder.layout(D);
2764 return new (*this) ASTRecordLayout(
2765 *this, Builder.Size, Builder.Alignment, Builder.RequiredAlignment,
2766 Builder.Size, Builder.FieldOffsets.data(), Builder.FieldOffsets.size());
2767 }
2768 }
2769
2770 /// getASTRecordLayout - Get or compute information about the layout of the
2771 /// specified record (struct/union/class), which indicates its size and field
2772 /// position information.
2773 const ASTRecordLayout &
getASTRecordLayout(const RecordDecl * D) const2774 ASTContext::getASTRecordLayout(const RecordDecl *D) const {
2775 // These asserts test different things. A record has a definition
2776 // as soon as we begin to parse the definition. That definition is
2777 // not a complete definition (which is what isDefinition() tests)
2778 // until we *finish* parsing the definition.
2779
2780 if (D->hasExternalLexicalStorage() && !D->getDefinition())
2781 getExternalSource()->CompleteType(const_cast<RecordDecl*>(D));
2782
2783 D = D->getDefinition();
2784 assert(D && "Cannot get layout of forward declarations!");
2785 assert(!D->isInvalidDecl() && "Cannot get layout of invalid decl!");
2786 assert(D->isCompleteDefinition() && "Cannot layout type before complete!");
2787
2788 // Look up this layout, if already laid out, return what we have.
2789 // Note that we can't save a reference to the entry because this function
2790 // is recursive.
2791 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
2792 if (Entry) return *Entry;
2793
2794 const ASTRecordLayout *NewEntry = nullptr;
2795
2796 if (isMsLayout(D) && !D->getASTContext().getExternalSource()) {
2797 NewEntry = BuildMicrosoftASTRecordLayout(D);
2798 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
2799 EmptySubobjectMap EmptySubobjects(*this, RD);
2800 RecordLayoutBuilder Builder(*this, &EmptySubobjects);
2801 Builder.Layout(RD);
2802
2803 // In certain situations, we are allowed to lay out objects in the
2804 // tail-padding of base classes. This is ABI-dependent.
2805 // FIXME: this should be stored in the record layout.
2806 bool skipTailPadding =
2807 mustSkipTailPadding(getTargetInfo().getCXXABI(), cast<CXXRecordDecl>(D));
2808
2809 // FIXME: This should be done in FinalizeLayout.
2810 CharUnits DataSize =
2811 skipTailPadding ? Builder.getSize() : Builder.getDataSize();
2812 CharUnits NonVirtualSize =
2813 skipTailPadding ? DataSize : Builder.NonVirtualSize;
2814 NewEntry =
2815 new (*this) ASTRecordLayout(*this, Builder.getSize(),
2816 Builder.Alignment,
2817 /*RequiredAlignment : used by MS-ABI)*/
2818 Builder.Alignment,
2819 Builder.HasOwnVFPtr,
2820 RD->isDynamicClass(),
2821 CharUnits::fromQuantity(-1),
2822 DataSize,
2823 Builder.FieldOffsets.data(),
2824 Builder.FieldOffsets.size(),
2825 NonVirtualSize,
2826 Builder.NonVirtualAlignment,
2827 EmptySubobjects.SizeOfLargestEmptySubobject,
2828 Builder.PrimaryBase,
2829 Builder.PrimaryBaseIsVirtual,
2830 nullptr, false, false,
2831 Builder.Bases, Builder.VBases);
2832 } else {
2833 RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/nullptr);
2834 Builder.Layout(D);
2835
2836 NewEntry =
2837 new (*this) ASTRecordLayout(*this, Builder.getSize(),
2838 Builder.Alignment,
2839 /*RequiredAlignment : used by MS-ABI)*/
2840 Builder.Alignment,
2841 Builder.getSize(),
2842 Builder.FieldOffsets.data(),
2843 Builder.FieldOffsets.size());
2844 }
2845
2846 ASTRecordLayouts[D] = NewEntry;
2847
2848 if (getLangOpts().DumpRecordLayouts) {
2849 llvm::outs() << "\n*** Dumping AST Record Layout\n";
2850 DumpRecordLayout(D, llvm::outs(), getLangOpts().DumpRecordLayoutsSimple);
2851 }
2852
2853 return *NewEntry;
2854 }
2855
getCurrentKeyFunction(const CXXRecordDecl * RD)2856 const CXXMethodDecl *ASTContext::getCurrentKeyFunction(const CXXRecordDecl *RD) {
2857 if (!getTargetInfo().getCXXABI().hasKeyFunctions())
2858 return nullptr;
2859
2860 assert(RD->getDefinition() && "Cannot get key function for forward decl!");
2861 RD = cast<CXXRecordDecl>(RD->getDefinition());
2862
2863 // Beware:
2864 // 1) computing the key function might trigger deserialization, which might
2865 // invalidate iterators into KeyFunctions
2866 // 2) 'get' on the LazyDeclPtr might also trigger deserialization and
2867 // invalidate the LazyDeclPtr within the map itself
2868 LazyDeclPtr Entry = KeyFunctions[RD];
2869 const Decl *Result =
2870 Entry ? Entry.get(getExternalSource()) : computeKeyFunction(*this, RD);
2871
2872 // Store it back if it changed.
2873 if (Entry.isOffset() || Entry.isValid() != bool(Result))
2874 KeyFunctions[RD] = const_cast<Decl*>(Result);
2875
2876 return cast_or_null<CXXMethodDecl>(Result);
2877 }
2878
setNonKeyFunction(const CXXMethodDecl * Method)2879 void ASTContext::setNonKeyFunction(const CXXMethodDecl *Method) {
2880 assert(Method == Method->getFirstDecl() &&
2881 "not working with method declaration from class definition");
2882
2883 // Look up the cache entry. Since we're working with the first
2884 // declaration, its parent must be the class definition, which is
2885 // the correct key for the KeyFunctions hash.
2886 llvm::DenseMap<const CXXRecordDecl*, LazyDeclPtr>::iterator
2887 I = KeyFunctions.find(Method->getParent());
2888
2889 // If it's not cached, there's nothing to do.
2890 if (I == KeyFunctions.end()) return;
2891
2892 // If it is cached, check whether it's the target method, and if so,
2893 // remove it from the cache. Note, the call to 'get' might invalidate
2894 // the iterator and the LazyDeclPtr object within the map.
2895 LazyDeclPtr Ptr = I->second;
2896 if (Ptr.get(getExternalSource()) == Method) {
2897 // FIXME: remember that we did this for module / chained PCH state?
2898 KeyFunctions.erase(Method->getParent());
2899 }
2900 }
2901
getFieldOffset(const ASTContext & C,const FieldDecl * FD)2902 static uint64_t getFieldOffset(const ASTContext &C, const FieldDecl *FD) {
2903 const ASTRecordLayout &Layout = C.getASTRecordLayout(FD->getParent());
2904 return Layout.getFieldOffset(FD->getFieldIndex());
2905 }
2906
getFieldOffset(const ValueDecl * VD) const2907 uint64_t ASTContext::getFieldOffset(const ValueDecl *VD) const {
2908 uint64_t OffsetInBits;
2909 if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) {
2910 OffsetInBits = ::getFieldOffset(*this, FD);
2911 } else {
2912 const IndirectFieldDecl *IFD = cast<IndirectFieldDecl>(VD);
2913
2914 OffsetInBits = 0;
2915 for (const auto *CI : IFD->chain())
2916 OffsetInBits += ::getFieldOffset(*this, cast<FieldDecl>(CI));
2917 }
2918
2919 return OffsetInBits;
2920 }
2921
2922 /// getObjCLayout - Get or compute information about the layout of the
2923 /// given interface.
2924 ///
2925 /// \param Impl - If given, also include the layout of the interface's
2926 /// implementation. This may differ by including synthesized ivars.
2927 const ASTRecordLayout &
getObjCLayout(const ObjCInterfaceDecl * D,const ObjCImplementationDecl * Impl) const2928 ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
2929 const ObjCImplementationDecl *Impl) const {
2930 // Retrieve the definition
2931 if (D->hasExternalLexicalStorage() && !D->getDefinition())
2932 getExternalSource()->CompleteType(const_cast<ObjCInterfaceDecl*>(D));
2933 D = D->getDefinition();
2934 assert(D && D->isThisDeclarationADefinition() && "Invalid interface decl!");
2935
2936 // Look up this layout, if already laid out, return what we have.
2937 const ObjCContainerDecl *Key =
2938 Impl ? (const ObjCContainerDecl*) Impl : (const ObjCContainerDecl*) D;
2939 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
2940 return *Entry;
2941
2942 // Add in synthesized ivar count if laying out an implementation.
2943 if (Impl) {
2944 unsigned SynthCount = CountNonClassIvars(D);
2945 // If there aren't any sythesized ivars then reuse the interface
2946 // entry. Note we can't cache this because we simply free all
2947 // entries later; however we shouldn't look up implementations
2948 // frequently.
2949 if (SynthCount == 0)
2950 return getObjCLayout(D, nullptr);
2951 }
2952
2953 RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/nullptr);
2954 Builder.Layout(D);
2955
2956 const ASTRecordLayout *NewEntry =
2957 new (*this) ASTRecordLayout(*this, Builder.getSize(),
2958 Builder.Alignment,
2959 /*RequiredAlignment : used by MS-ABI)*/
2960 Builder.Alignment,
2961 Builder.getDataSize(),
2962 Builder.FieldOffsets.data(),
2963 Builder.FieldOffsets.size());
2964
2965 ObjCLayouts[Key] = NewEntry;
2966
2967 return *NewEntry;
2968 }
2969
PrintOffset(raw_ostream & OS,CharUnits Offset,unsigned IndentLevel)2970 static void PrintOffset(raw_ostream &OS,
2971 CharUnits Offset, unsigned IndentLevel) {
2972 OS << llvm::format("%4" PRId64 " | ", (int64_t)Offset.getQuantity());
2973 OS.indent(IndentLevel * 2);
2974 }
2975
PrintIndentNoOffset(raw_ostream & OS,unsigned IndentLevel)2976 static void PrintIndentNoOffset(raw_ostream &OS, unsigned IndentLevel) {
2977 OS << " | ";
2978 OS.indent(IndentLevel * 2);
2979 }
2980
DumpCXXRecordLayout(raw_ostream & OS,const CXXRecordDecl * RD,const ASTContext & C,CharUnits Offset,unsigned IndentLevel,const char * Description,bool IncludeVirtualBases)2981 static void DumpCXXRecordLayout(raw_ostream &OS,
2982 const CXXRecordDecl *RD, const ASTContext &C,
2983 CharUnits Offset,
2984 unsigned IndentLevel,
2985 const char* Description,
2986 bool IncludeVirtualBases) {
2987 const ASTRecordLayout &Layout = C.getASTRecordLayout(RD);
2988
2989 PrintOffset(OS, Offset, IndentLevel);
2990 OS << C.getTypeDeclType(const_cast<CXXRecordDecl *>(RD)).getAsString();
2991 if (Description)
2992 OS << ' ' << Description;
2993 if (RD->isEmpty())
2994 OS << " (empty)";
2995 OS << '\n';
2996
2997 IndentLevel++;
2998
2999 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
3000 bool HasOwnVFPtr = Layout.hasOwnVFPtr();
3001 bool HasOwnVBPtr = Layout.hasOwnVBPtr();
3002
3003 // Vtable pointer.
3004 if (RD->isDynamicClass() && !PrimaryBase && !isMsLayout(RD)) {
3005 PrintOffset(OS, Offset, IndentLevel);
3006 OS << '(' << *RD << " vtable pointer)\n";
3007 } else if (HasOwnVFPtr) {
3008 PrintOffset(OS, Offset, IndentLevel);
3009 // vfptr (for Microsoft C++ ABI)
3010 OS << '(' << *RD << " vftable pointer)\n";
3011 }
3012
3013 // Collect nvbases.
3014 SmallVector<const CXXRecordDecl *, 4> Bases;
3015 for (const auto &I : RD->bases()) {
3016 assert(!I.getType()->isDependentType() &&
3017 "Cannot layout class with dependent bases.");
3018 if (!I.isVirtual())
3019 Bases.push_back(I.getType()->getAsCXXRecordDecl());
3020 }
3021
3022 // Sort nvbases by offset.
3023 std::stable_sort(Bases.begin(), Bases.end(),
3024 [&](const CXXRecordDecl *L, const CXXRecordDecl *R) {
3025 return Layout.getBaseClassOffset(L) < Layout.getBaseClassOffset(R);
3026 });
3027
3028 // Dump (non-virtual) bases
3029 for (SmallVectorImpl<const CXXRecordDecl *>::iterator I = Bases.begin(),
3030 E = Bases.end();
3031 I != E; ++I) {
3032 const CXXRecordDecl *Base = *I;
3033 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base);
3034 DumpCXXRecordLayout(OS, Base, C, BaseOffset, IndentLevel,
3035 Base == PrimaryBase ? "(primary base)" : "(base)",
3036 /*IncludeVirtualBases=*/false);
3037 }
3038
3039 // vbptr (for Microsoft C++ ABI)
3040 if (HasOwnVBPtr) {
3041 PrintOffset(OS, Offset + Layout.getVBPtrOffset(), IndentLevel);
3042 OS << '(' << *RD << " vbtable pointer)\n";
3043 }
3044
3045 // Dump fields.
3046 uint64_t FieldNo = 0;
3047 for (CXXRecordDecl::field_iterator I = RD->field_begin(),
3048 E = RD->field_end(); I != E; ++I, ++FieldNo) {
3049 const FieldDecl &Field = **I;
3050 CharUnits FieldOffset = Offset +
3051 C.toCharUnitsFromBits(Layout.getFieldOffset(FieldNo));
3052
3053 if (const CXXRecordDecl *D = Field.getType()->getAsCXXRecordDecl()) {
3054 DumpCXXRecordLayout(OS, D, C, FieldOffset, IndentLevel,
3055 Field.getName().data(),
3056 /*IncludeVirtualBases=*/true);
3057 continue;
3058 }
3059
3060 PrintOffset(OS, FieldOffset, IndentLevel);
3061 OS << Field.getType().getAsString() << ' ' << Field << '\n';
3062 }
3063
3064 if (!IncludeVirtualBases)
3065 return;
3066
3067 // Dump virtual bases.
3068 const ASTRecordLayout::VBaseOffsetsMapTy &vtordisps =
3069 Layout.getVBaseOffsetsMap();
3070 for (const auto &I : RD->vbases()) {
3071 assert(I.isVirtual() && "Found non-virtual class!");
3072 const CXXRecordDecl *VBase = I.getType()->getAsCXXRecordDecl();
3073
3074 CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBase);
3075
3076 if (vtordisps.find(VBase)->second.hasVtorDisp()) {
3077 PrintOffset(OS, VBaseOffset - CharUnits::fromQuantity(4), IndentLevel);
3078 OS << "(vtordisp for vbase " << *VBase << ")\n";
3079 }
3080
3081 DumpCXXRecordLayout(OS, VBase, C, VBaseOffset, IndentLevel,
3082 VBase == PrimaryBase ?
3083 "(primary virtual base)" : "(virtual base)",
3084 /*IncludeVirtualBases=*/false);
3085 }
3086
3087 PrintIndentNoOffset(OS, IndentLevel - 1);
3088 OS << "[sizeof=" << Layout.getSize().getQuantity();
3089 if (!isMsLayout(RD))
3090 OS << ", dsize=" << Layout.getDataSize().getQuantity();
3091 OS << ", align=" << Layout.getAlignment().getQuantity() << '\n';
3092
3093 PrintIndentNoOffset(OS, IndentLevel - 1);
3094 OS << " nvsize=" << Layout.getNonVirtualSize().getQuantity();
3095 OS << ", nvalign=" << Layout.getNonVirtualAlignment().getQuantity() << "]\n";
3096 }
3097
DumpRecordLayout(const RecordDecl * RD,raw_ostream & OS,bool Simple) const3098 void ASTContext::DumpRecordLayout(const RecordDecl *RD,
3099 raw_ostream &OS,
3100 bool Simple) const {
3101 const ASTRecordLayout &Info = getASTRecordLayout(RD);
3102
3103 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
3104 if (!Simple)
3105 return DumpCXXRecordLayout(OS, CXXRD, *this, CharUnits(), 0, nullptr,
3106 /*IncludeVirtualBases=*/true);
3107
3108 OS << "Type: " << getTypeDeclType(RD).getAsString() << "\n";
3109 if (!Simple) {
3110 OS << "Record: ";
3111 RD->dump();
3112 }
3113 OS << "\nLayout: ";
3114 OS << "<ASTRecordLayout\n";
3115 OS << " Size:" << toBits(Info.getSize()) << "\n";
3116 if (!isMsLayout(RD))
3117 OS << " DataSize:" << toBits(Info.getDataSize()) << "\n";
3118 OS << " Alignment:" << toBits(Info.getAlignment()) << "\n";
3119 OS << " FieldOffsets: [";
3120 for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i) {
3121 if (i) OS << ", ";
3122 OS << Info.getFieldOffset(i);
3123 }
3124 OS << "]>\n";
3125 }
3126