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