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