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