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
23using namespace clang;
24
25namespace {
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.
38struct 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.
59struct 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.
79 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
85 bool getExternalNVBaseOffset(const CXXRecordDecl *RD, CharUnits &BaseOffset) {
86 auto Known = BaseOffsets.find(Val: RD);
87 if (Known == BaseOffsets.end())
88 return false;
89 BaseOffset = Known->second;
90 return true;
91 }
92
93 bool getExternalVBaseOffset(const CXXRecordDecl *RD, CharUnits &BaseOffset) {
94 auto Known = VirtualBaseOffsets.find(Val: 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.
104class 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.
137 bool AnyEmptySubobjectsBeyondOffset(CharUnits Offset) const {
138 return Offset <= MaxEmptyClassOffset;
139 }
140
141 CharUnits getFieldOffset(const ASTRecordLayout &Layout,
142 const FieldDecl *Field) const {
143 uint64_t FieldOffset = Layout.getFieldOffset(FieldNo: Field->getFieldIndex());
144 assert(FieldOffset % CharWidth == 0 &&
145 "Field offset not at char boundary!");
146
147 return Context.toCharUnitsFromBits(BitSize: FieldOffset);
148 }
149
150protected:
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
163public:
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
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
186void 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
230bool
231EmptySubobjectMap::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(Val: Offset);
238 if (I == EmptyClassOffsets.end())
239 return true;
240
241 const ClassVectorTy &Classes = I->second;
242 if (!llvm::is_contained(Range: Classes, Element: RD))
243 return true;
244
245 // There is already an empty class of the same type at this offset.
246 return false;
247}
248
249void 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(Range&: Classes, Element: RD))
259 return;
260
261 Classes.push_back(NewVal: RD);
262
263 // Update the empty class offset.
264 if (Offset > MaxEmptyClassOffset)
265 MaxEmptyClassOffset = Offset;
266}
267
268bool
269EmptySubobjectMap::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(RD: 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: Base->Class);
286
287 if (!CanPlaceBaseSubobjectAtOffset(Info: Base, Offset: BaseOffset))
288 return false;
289 }
290
291 if (Info->PrimaryVirtualBaseInfo) {
292 BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo;
293
294 if (Info == PrimaryVirtualBaseInfo->Derived) {
295 if (!CanPlaceBaseSubobjectAtOffset(Info: 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
313void 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(RD: 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: Base->Class);
334 UpdateEmptyBaseSubobjects(Info: Base, Offset: BaseOffset, PlacingEmptyBase);
335 }
336
337 if (Info->PrimaryVirtualBaseInfo) {
338 BaseSubobjectInfo *PrimaryVirtualBaseInfo = Info->PrimaryVirtualBaseInfo;
339
340 if (Info == PrimaryVirtualBaseInfo->Derived)
341 UpdateEmptyBaseSubobjects(Info: 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
355bool 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, PlacingEmptyBase: Info->Class->isEmpty());
368 return true;
369}
370
371bool
372EmptySubobjectMap::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(Base: BaseDecl);
393 if (!CanPlaceFieldSubobjectAtOffset(RD: BaseDecl, Class, Offset: 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(VBase: VBaseDecl);
403 if (!CanPlaceFieldSubobjectAtOffset(RD: VBaseDecl, Class, Offset: 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
421bool
422EmptySubobjectMap::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, Class: 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(CA: 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(Offset: ElementOffset))
449 return true;
450
451 if (!CanPlaceFieldSubobjectAtOffset(RD, Class: RD, Offset: ElementOffset))
452 return false;
453
454 ElementOffset += Layout.getSize();
455 }
456 }
457
458 return true;
459}
460
461bool 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
472void 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(Base: BaseDecl);
500 UpdateEmptyFieldSubobjects(RD: BaseDecl, Class, Offset: 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(VBase: VBaseDecl);
510 UpdateEmptyFieldSubobjects(RD: VBaseDecl, Class, Offset: 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
525void 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, Class: 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(CA: 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, Class: RD, Offset: ElementOffset,
557 PlacingOverlappingField);
558 ElementOffset += Layout.getSize();
559 }
560 }
561}
562
563typedef llvm::SmallPtrSet<const CXXRecordDecl*, 4> ClassSetTy;
564
565class ItaniumRecordLayoutBuilder {
566protected:
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
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
715 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);
780 void UpdateAlignment(CharUnits NewAlignment, CharUnits UnpackedNewAlignment) {
781 UpdateAlignment(NewAlignment, UnpackedNewAlignment, PreferredAlignment: NewAlignment);
782 }
783 void UpdateAlignment(CharUnits NewAlignment) {
784 UpdateAlignment(NewAlignment, UnpackedNewAlignment: NewAlignment, PreferredAlignment: 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
801 CharUnits getSize() const {
802 assert(Size % Context.getCharWidth() == 0);
803 return Context.toCharUnitsFromBits(BitSize: Size);
804 }
805 uint64_t getSizeInBits() const { return Size; }
806
807 void setSize(CharUnits NewSize) { Size = Context.toBits(CharSize: NewSize); }
808 void setSize(uint64_t NewSize) { Size = NewSize; }
809
810 CharUnits getAlignment() const { return Alignment; }
811
812 CharUnits getDataSize() const {
813 assert(DataSize % Context.getCharWidth() == 0);
814 return Context.toCharUnitsFromBits(BitSize: DataSize);
815 }
816 uint64_t getDataSizeInBits() const { return DataSize; }
817
818 void setDataSize(CharUnits NewSize) { DataSize = Context.toBits(CharSize: 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
826void 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(RD: Base)) {
835 // If it's not an indirect primary base, then we've found our primary
836 // base.
837 if (!IndirectPrimaryBases.count(Ptr: 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(RD: Base);
849 if (PrimaryBase)
850 return;
851 }
852}
853
854/// DeterminePrimaryBase - Determine the primary base of the given class.
855void 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(Bases&: 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
902BaseSubobjectInfo *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(Val: 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(Elt: ComputeBaseSubobjectInfo(RD: BaseDecl, IsVirtual, Derived: 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(Val: 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
979void 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(RD: BaseDecl, IsVirtual,
988 Derived: 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(KV: std::make_pair(x&: BaseDecl, y&: Info));
999 }
1000 }
1001}
1002
1003void 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(a: BaseAlign, b: MaxFieldAlignment);
1010 UnpackedBaseAlign = std::min(a: UnpackedBaseAlign, b: MaxFieldAlignment);
1011 }
1012
1013 // Round up the current record size to pointer alignment.
1014 setSize(getSize().alignTo(Align: BaseAlign));
1015
1016 // Update the alignment.
1017 UpdateAlignment(NewAlignment: BaseAlign, UnpackedNewAlignment: UnpackedBaseAlign, PreferredAlignment: BaseAlign);
1018}
1019
1020void 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(Val: PrimaryBase);
1034 PrimaryBaseInfo->Derived = nullptr;
1035
1036 // We have a virtual primary base, insert it as an indirect primary base.
1037 IndirectPrimaryBases.insert(Ptr: PrimaryBase);
1038
1039 assert(!VisitedVirtualBases.count(PrimaryBase) &&
1040 "vbase already visited!");
1041 VisitedVirtualBases.insert(Ptr: PrimaryBase);
1042
1043 LayoutVirtualBase(Base: PrimaryBaseInfo);
1044 } else {
1045 BaseSubobjectInfo *PrimaryBaseInfo =
1046 NonVirtualBaseInfo.lookup(Val: PrimaryBase);
1047 assert(PrimaryBaseInfo &&
1048 "Did not find base info for non-virtual primary base!");
1049
1050 LayoutNonVirtualBase(Base: 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 BitSize: Context.getTargetInfo().getPointerWidth(AddrSpace: LangAS::Default));
1059 CharUnits PtrAlign = Context.toCharUnitsFromBits(
1060 BitSize: Context.getTargetInfo().getPointerAlign(AddrSpace: LangAS::Default));
1061 EnsureVTablePointerAlignment(UnpackedBaseAlign: 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(Val: BaseDecl);
1088 assert(BaseInfo && "Did not find base info for non-virtual base!");
1089
1090 LayoutNonVirtualBase(Base: BaseInfo);
1091 }
1092}
1093
1094void 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(KV: std::make_pair(x: Base->Class, y&: Offset));
1102
1103 AddPrimaryVirtualBaseOffsets(Info: Base, Offset);
1104}
1105
1106void 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(KV: std::make_pair(x&: Info->PrimaryVirtualBaseInfo->Class,
1121 y: ASTRecordLayout::VBaseInfo(Offset, false)));
1122
1123 // Traverse the primary virtual base.
1124 AddPrimaryVirtualBaseOffsets(Info: 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: Base->Class);
1135 AddPrimaryVirtualBaseOffsets(Info: Base, Offset: BaseOffset);
1136 }
1137}
1138
1139void 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(Ptr: 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(Ptr: BaseDecl).second)
1167 continue;
1168
1169 const BaseSubobjectInfo *BaseInfo = VirtualBaseInfo.lookup(Val: BaseDecl);
1170 assert(BaseInfo && "Did not find virtual base info!");
1171 LayoutVirtualBase(Base: 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(RD: BaseDecl, MostDerivedClass);
1182 }
1183}
1184
1185void 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(KV: std::make_pair(x: Base->Class,
1195 y: ASTRecordLayout::VBaseInfo(Offset, false)));
1196
1197 AddPrimaryVirtualBaseOffsets(Info: Base, Offset);
1198}
1199
1200CharUnits
1201ItaniumRecordLayoutBuilder::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(RD: Base->Class, BaseOffset&: Offset);
1212 else
1213 HasExternalLayout = External.getExternalNVBaseOffset(RD: Base->Class, BaseOffset&: 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(Info: Base, Offset: CharUnits::Zero())) {
1258 setSize(std::max(a: getSize(), b: Layout.getSize()));
1259 // On PS4/PS5, don't update the alignment, to preserve compatibility.
1260 if (!Context.getTargetInfo().getTriple().isPS())
1261 UpdateAlignment(NewAlignment: BaseAlign, UnpackedNewAlignment: UnpackedAlignTo, PreferredAlignment: 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(a: BaseAlign, b: MaxFieldAlignment);
1270 PreferredBaseAlign = std::min(a: PreferredBaseAlign, b: MaxFieldAlignment);
1271 UnpackedAlignTo = std::min(a: UnpackedAlignTo, b: 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(Align: AlignTo);
1279
1280 // Try to place the base.
1281 while (!EmptySubobjects->CanPlaceBaseAtOffset(Info: Base, Offset))
1282 Offset += AlignTo;
1283 } else {
1284 bool Allowed = EmptySubobjects->CanPlaceBaseAtOffset(Info: Base, Offset);
1285 (void)Allowed;
1286 assert(Allowed && "Base subobject externally placed at overlapping offset");
1287
1288 if (InferAlignment && Offset < getDataSize().alignTo(Align: 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(a: getSize(), b: getDataSize()));
1301 } else
1302 setSize(std::max(a: getSize(), b: Offset + Layout.getSize()));
1303
1304 // Remember max struct/class alignment.
1305 UnadjustedAlignment = std::max(a: UnadjustedAlignment, b: BaseAlign);
1306 UpdateAlignment(NewAlignment: BaseAlign, UnpackedNewAlignment: UnpackedAlignTo, PreferredAlignment: PreferredBaseAlign);
1307
1308 return Offset;
1309}
1310
1311void ItaniumRecordLayoutBuilder::InitializeLayout(const Decl *D) {
1312 if (const RecordDecl *RD = dyn_cast<RecordDecl>(Val: D)) {
1313 IsUnion = RD->isUnion();
1314 IsMsStruct = RD->isMsStruct(C: 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(Quantity: 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(Quantity: 2);
1334 Alignment = CharUnits::fromQuantity(Quantity: 2);
1335 PreferredAlignment = CharUnits::fromQuantity(Quantity: 2);
1336 } else {
1337 if (D->hasAttr<AlignNaturalAttr>())
1338 IsNaturalAlign = true;
1339
1340 if (const MaxFieldAlignmentAttr *MFAA = D->getAttr<MaxFieldAlignmentAttr>())
1341 MaxFieldAlignment = Context.toCharUnitsFromBits(BitSize: MFAA->getAlignment());
1342
1343 if (unsigned MaxAlign = D->getMaxAlignment())
1344 UpdateAlignment(NewAlignment: Context.toCharUnitsFromBits(BitSize: 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>(Val: D))
1352 if (ExternalASTSource *Source = Context.getExternalSource()) {
1353 UseExternalLayout = Source->layoutRecordType(
1354 Record: RD, Size&: External.Size, Alignment&: External.Align, FieldOffsets&: External.FieldOffsets,
1355 BaseOffsets&: External.BaseOffsets, VirtualBaseOffsets&: External.VirtualBaseOffsets);
1356
1357 // Update based on external alignment.
1358 if (UseExternalLayout) {
1359 if (External.Align > 0) {
1360 Alignment = Context.toCharUnitsFromBits(BitSize: External.Align);
1361 PreferredAlignment = Context.toCharUnitsFromBits(BitSize: External.Align);
1362 } else {
1363 // The external source didn't have alignment information; infer it.
1364 InferAlignment = true;
1365 }
1366 }
1367 }
1368}
1369
1370void 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
1379void 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 BitSize: llvm::alignTo(Value: getSizeInBits(), Align: 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, MostDerivedClass: 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
1419void ItaniumRecordLayoutBuilder::Layout(const ObjCInterfaceDecl *D) {
1420 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
1421 const ASTRecordLayout &SL = Context.getASTObjCInterfaceLayout(D: SD);
1422
1423 UpdateAlignment(NewAlignment: 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
1442void 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(D: *I, InsertExtraPadding: InsertExtraPadding &&
1449 (std::next(x: I) != End || !HasFlexibleArrayMember));
1450 }
1451}
1452
1453// Rounds the specified size to have it a multiple of the char size.
1454static uint64_t
1455roundUpSizeToCharAlignment(uint64_t Size,
1456 const ASTContext &Context) {
1457 uint64_t CharAlignment = Context.getTargetInfo().getCharAlign();
1458 return llvm::alignTo(Value: Size, Align: CharAlignment);
1459}
1460
1461void 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(T: 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(Size: FieldSize,
1502 Context);
1503 setDataSize(std::max(a: getDataSizeInBits(), b: 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(Value: getDataSizeInBits(), Align: Context.toBits(CharSize: TypeAlign));
1509
1510 uint64_t NewSizeInBits = FieldOffset + FieldSize;
1511
1512 setDataSize(
1513 llvm::alignTo(Value: NewSizeInBits, Align: Context.getTargetInfo().getCharAlign()));
1514 UnfilledBitsInLastUnit = getDataSizeInBits() - NewSizeInBits;
1515 }
1516
1517 // Place this field at the current location.
1518 FieldOffsets.push_back(Elt: FieldOffset);
1519
1520 CheckFieldPadding(Offset: FieldOffset, UnpaddedOffset: UnpaddedFieldOffset, UnpackedOffset: FieldOffset,
1521 UnpackedAlign: Context.toBits(CharSize: TypeAlign), isPacked: FieldPacked, D);
1522
1523 // Update the size.
1524 setSize(std::max(a: getSizeInBits(), b: getDataSizeInBits()));
1525
1526 // Remember max struct/class alignment.
1527 UnadjustedAlignment = std::max(a: UnadjustedAlignment, b: TypeAlign);
1528 UpdateAlignment(NewAlignment: TypeAlign);
1529}
1530
1531static bool isAIXLayout(const ASTContext &Context) {
1532 return Context.getTargetInfo().getTriple().getOS() == llvm::Triple::AIX;
1533}
1534
1535void 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(a: FieldAlign, b: 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(a: FieldAlign, b: ExplicitFieldAlign);
1689 UnpackedFieldAlign = std::max(a: UnpackedFieldAlign, b: 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(CharSize: MaxFieldAlignment);
1695 if (!MaxFieldAlignment.isZero() && FieldSize) {
1696 UnpackedFieldAlign = std::min(a: UnpackedFieldAlign, b: MaxFieldAlignmentInBits);
1697 if (FieldPacked)
1698 FieldAlign = UnpackedFieldAlign;
1699 else
1700 FieldAlign = std::min(a: FieldAlign, b: 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(Value: FieldOffset, Align: FieldAlign);
1725 UnpackedFieldOffset =
1726 llvm::alignTo(Value: UnpackedFieldOffset, Align: 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(Value: FieldOffset, Align: 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(Value: FieldOffset, Align: 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(Value: UnpackedFieldOffset, Align: UnpackedFieldAlign);
1755 else if (ExplicitFieldAlign &&
1756 (MaxFieldAlignmentInBits == 0 ||
1757 ExplicitFieldAlign <= MaxFieldAlignmentInBits) &&
1758 Context.getTargetInfo().useExplicitBitFieldAlignment())
1759 UnpackedFieldOffset =
1760 llvm::alignTo(Value: UnpackedFieldOffset, Align: 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(Field: D, ComputedOffset: FieldOffset);
1767
1768 // Okay, place the bitfield at the calculated offset.
1769 FieldOffsets.push_back(Elt: 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(a: UnpackedFieldAlign, b: MaxFieldAlignmentInBits);
1789 FieldAlign = std::min(a: FieldAlign, b: MaxFieldAlignmentInBits);
1790 }
1791 }
1792
1793 // Diagnose differences in layout due to padding or packing.
1794 if (!UseExternalLayout)
1795 CheckFieldPadding(Offset: FieldOffset, UnpaddedOffset: UnpaddedFieldOffset, UnpackedOffset: UnpackedFieldOffset,
1796 UnpackedAlign: UnpackedFieldAlign, isPacked: 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(Size: FieldSize, Context);
1813 }
1814 setDataSize(std::max(a: getDataSizeInBits(), b: 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(Value: NewSizeInBits, Align: 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(a: getSizeInBits(), b: getDataSizeInBits()));
1845
1846 // Remember max struct/class alignment.
1847 UnadjustedAlignment =
1848 std::max(a: UnadjustedAlignment, b: Context.toCharUnitsFromBits(BitSize: FieldAlign));
1849 UpdateAlignment(NewAlignment: Context.toCharUnitsFromBits(BitSize: FieldAlign),
1850 UnpackedNewAlignment: Context.toCharUnitsFromBits(BitSize: UnpackedFieldAlign));
1851}
1852
1853void 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(D: FieldClass);
1920 EffectiveFieldSize =
1921 std::max(a: Layout.getNonVirtualSize(), b: 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(Value: 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(Value: 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(Quantity: 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(D: 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(BitSize: D->getMaxAlignment());
2031 PackedFieldAlign = std::max(a: PackedFieldAlign, b: MaxAlignmentInChars);
2032 PreferredAlign = std::max(a: PreferredAlign, b: MaxAlignmentInChars);
2033 UnpackedFieldAlign = std::max(a: UnpackedFieldAlign, b: MaxAlignmentInChars);
2034
2035 // The maximum field alignment overrides the aligned attribute.
2036 if (!MaxFieldAlignment.isZero()) {
2037 PackedFieldAlign = std::min(a: PackedFieldAlign, b: MaxFieldAlignment);
2038 PreferredAlign = std::min(a: PreferredAlign, b: MaxFieldAlignment);
2039 UnpackedFieldAlign = std::min(a: UnpackedFieldAlign, b: 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(Align: AlignTo);
2056 UnpackedFieldOffset = UnpackedFieldOffset.alignTo(Align: UnpackedFieldAlign);
2057
2058 if (UseExternalLayout) {
2059 FieldOffset = Context.toCharUnitsFromBits(
2060 BitSize: updateExternalFieldOffset(Field: D, ComputedOffset: Context.toBits(CharSize: FieldOffset)));
2061
2062 if (!IsUnion && EmptySubobjects) {
2063 // Record the fact that we're placing a field at this offset.
2064 bool Allowed = EmptySubobjects->CanPlaceFieldAtOffset(FD: D, Offset: 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(FD: D, Offset: 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(Align: AlignTo);
2077 else
2078 FieldOffset += AlignTo;
2079 }
2080 }
2081 }
2082
2083 // Place this field at the current location.
2084 FieldOffsets.push_back(Elt: Context.toBits(CharSize: FieldOffset));
2085
2086 if (!UseExternalLayout)
2087 CheckFieldPadding(Offset: Context.toBits(CharSize: FieldOffset), UnpaddedOffset: UnpaddedFieldOffset,
2088 UnpackedOffset: Context.toBits(CharSize: UnpackedFieldOffset),
2089 UnpackedAlign: Context.toBits(CharSize: UnpackedFieldAlign), isPacked: FieldPacked, D);
2090
2091 if (InsertExtraPadding) {
2092 CharUnits ASanAlignment = CharUnits::fromQuantity(Quantity: 8);
2093 CharUnits ExtraSizeForAsan = ASanAlignment;
2094 if (FieldSize % ASanAlignment)
2095 ExtraSizeForAsan +=
2096 ASanAlignment - CharUnits::fromQuantity(Quantity: FieldSize % ASanAlignment);
2097 EffectiveFieldSize = FieldSize = FieldSize + ExtraSizeForAsan;
2098 }
2099
2100 // Reserve space for this field.
2101 if (!IsOverlappingEmptyField) {
2102 uint64_t EffectiveFieldSizeInBits = Context.toBits(CharSize: EffectiveFieldSize);
2103 if (IsUnion)
2104 setDataSize(std::max(a: getDataSizeInBits(), b: EffectiveFieldSizeInBits));
2105 else
2106 setDataSize(FieldOffset + EffectiveFieldSize);
2107
2108 PaddedFieldSize = std::max(a: PaddedFieldSize, b: FieldOffset + FieldSize);
2109 setSize(std::max(a: getSizeInBits(), b: getDataSizeInBits()));
2110 } else {
2111 setSize(std::max(a: getSizeInBits(),
2112 b: (uint64_t)Context.toBits(CharSize: FieldOffset + FieldSize)));
2113 }
2114
2115 // Remember max struct/class ABI-specified alignment.
2116 UnadjustedAlignment = std::max(a: UnadjustedAlignment, b: FieldAlign);
2117 UpdateAlignment(NewAlignment: FieldAlign, UnpackedNewAlignment: UnpackedFieldAlign, PreferredAlignment: 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
2139void 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>(Val: 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(a: getSizeInBits(), b: (uint64_t)Context.toBits(CharSize: 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(Value: getSizeInBits(), Align: Context.toBits(CharSize: UnpackedAlignment));
2162
2163 uint64_t RoundedSize = llvm::alignTo(
2164 Value: getSizeInBits(),
2165 Align: Context.toBits(CharSize: !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>(Val: 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>(Val: 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
2219void 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
2246uint64_t
2247ItaniumRecordLayoutBuilder::updateExternalFieldOffset(const FieldDecl *Field,
2248 uint64_t ComputedOffset) {
2249 uint64_t ExternalFieldOffset = External.getExternalFieldOffset(FD: 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.
2268static 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
2280static 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>(Val: 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
2324void 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
2333static 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
2414DiagnosticBuilder 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?
2422static 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
2461static bool isMsLayout(const ASTContext &Context) {
2462 // Check if it's CUDA device compilation; ensure layout consistency with host.
2463 if (Context.getLangOpts().CUDA && Context.getLangOpts().CUDAIsDevice &&
2464 Context.getAuxTargetInfo())
2465 return Context.getAuxTargetInfo()->getCXXABI().isMicrosoft();
2466
2467 return Context.getTargetInfo().getCXXABI().isMicrosoft();
2468}
2469
2470// This section contains an implementation of struct layout that is, up to the
2471// included tests, compatible with cl.exe (2013). The layout produced is
2472// significantly different than those produced by the Itanium ABI. Here we note
2473// the most important differences.
2474//
2475// * The alignment of bitfields in unions is ignored when computing the
2476// alignment of the union.
2477// * The existence of zero-width bitfield that occurs after anything other than
2478// a non-zero length bitfield is ignored.
2479// * There is no explicit primary base for the purposes of layout. All bases
2480// with vfptrs are laid out first, followed by all bases without vfptrs.
2481// * The Itanium equivalent vtable pointers are split into a vfptr (virtual
2482// function pointer) and a vbptr (virtual base pointer). They can each be
2483// shared with a, non-virtual bases. These bases need not be the same. vfptrs
2484// always occur at offset 0. vbptrs can occur at an arbitrary offset and are
2485// placed after the lexicographically last non-virtual base. This placement
2486// is always before fields but can be in the middle of the non-virtual bases
2487// due to the two-pass layout scheme for non-virtual-bases.
2488// * Virtual bases sometimes require a 'vtordisp' field that is laid out before
2489// the virtual base and is used in conjunction with virtual overrides during
2490// construction and destruction. This is always a 4 byte value and is used as
2491// an alternative to constructor vtables.
2492// * vtordisps are allocated in a block of memory with size and alignment equal
2493// to the alignment of the completed structure (before applying __declspec(
2494// align())). The vtordisp always occur at the end of the allocation block,
2495// immediately prior to the virtual base.
2496// * vfptrs are injected after all bases and fields have been laid out. In
2497// order to guarantee proper alignment of all fields, the vfptr injection
2498// pushes all bases and fields back by the alignment imposed by those bases
2499// and fields. This can potentially add a significant amount of padding.
2500// vfptrs are always injected at offset 0.
2501// * vbptrs are injected after all bases and fields have been laid out. In
2502// order to guarantee proper alignment of all fields, the vfptr injection
2503// pushes all bases and fields back by the alignment imposed by those bases
2504// and fields. This can potentially add a significant amount of padding.
2505// vbptrs are injected immediately after the last non-virtual base as
2506// lexicographically ordered in the code. If this site isn't pointer aligned
2507// the vbptr is placed at the next properly aligned location. Enough padding
2508// is added to guarantee a fit.
2509// * The last zero sized non-virtual base can be placed at the end of the
2510// struct (potentially aliasing another object), or may alias with the first
2511// field, even if they are of the same type.
2512// * The last zero size virtual base may be placed at the end of the struct
2513// potentially aliasing another object.
2514// * The ABI attempts to avoid aliasing of zero sized bases by adding padding
2515// between bases or vbases with specific properties. The criteria for
2516// additional padding between two bases is that the first base is zero sized
2517// or ends with a zero sized subobject and the second base is zero sized or
2518// trails with a zero sized base or field (sharing of vfptrs can reorder the
2519// layout of the so the leading base is not always the first one declared).
2520// This rule does take into account fields that are not records, so padding
2521// will occur even if the last field is, e.g. an int. The padding added for
2522// bases is 1 byte. The padding added between vbases depends on the alignment
2523// of the object but is at least 4 bytes (in both 32 and 64 bit modes).
2524// * There is no concept of non-virtual alignment, non-virtual alignment and
2525// alignment are always identical.
2526// * There is a distinction between alignment and required alignment.
2527// __declspec(align) changes the required alignment of a struct. This
2528// alignment is _always_ obeyed, even in the presence of #pragma pack. A
2529// record inherits required alignment from all of its fields and bases.
2530// * __declspec(align) on bitfields has the effect of changing the bitfield's
2531// alignment instead of its required alignment. This is the only known way
2532// to make the alignment of a struct bigger than 8. Interestingly enough
2533// this alignment is also immune to the effects of #pragma pack and can be
2534// used to create structures with large alignment under #pragma pack.
2535// However, because it does not impact required alignment, such a structure,
2536// when used as a field or base, will not be aligned if #pragma pack is
2537// still active at the time of use.
2538//
2539// Known incompatibilities:
2540// * all: #pragma pack between fields in a record
2541// * 2010 and back: If the last field in a record is a bitfield, every object
2542// laid out after the record will have extra padding inserted before it. The
2543// extra padding will have size equal to the size of the storage class of the
2544// bitfield. 0 sized bitfields don't exhibit this behavior and the extra
2545// padding can be avoided by adding a 0 sized bitfield after the non-zero-
2546// sized bitfield.
2547// * 2012 and back: In 64-bit mode, if the alignment of a record is 16 or
2548// greater due to __declspec(align()) then a second layout phase occurs after
2549// The locations of the vf and vb pointers are known. This layout phase
2550// suffers from the "last field is a bitfield" bug in 2010 and results in
2551// _every_ field getting padding put in front of it, potentially including the
2552// vfptr, leaving the vfprt at a non-zero location which results in a fault if
2553// anything tries to read the vftbl. The second layout phase also treats
2554// bitfields as separate entities and gives them each storage rather than
2555// packing them. Additionally, because this phase appears to perform a
2556// (an unstable) sort on the members before laying them out and because merged
2557// bitfields have the same address, the bitfields end up in whatever order
2558// the sort left them in, a behavior we could never hope to replicate.
2559
2560namespace {
2561struct MicrosoftRecordLayoutBuilder {
2562 struct ElementInfo {
2563 CharUnits Size;
2564 CharUnits Alignment;
2565 };
2566 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetsMapTy;
2567 MicrosoftRecordLayoutBuilder(const ASTContext &Context,
2568 EmptySubobjectMap *EmptySubobjects)
2569 : Context(Context), EmptySubobjects(EmptySubobjects),
2570 RemainingBitsInField(0) {}
2571
2572private:
2573 MicrosoftRecordLayoutBuilder(const MicrosoftRecordLayoutBuilder &) = delete;
2574 void operator=(const MicrosoftRecordLayoutBuilder &) = delete;
2575public:
2576 void layout(const RecordDecl *RD);
2577 void cxxLayout(const CXXRecordDecl *RD);
2578 /// Initializes size and alignment and honors some flags.
2579 void initializeLayout(const RecordDecl *RD);
2580 /// Initialized C++ layout, compute alignment and virtual alignment and
2581 /// existence of vfptrs and vbptrs. Alignment is needed before the vfptr is
2582 /// laid out.
2583 void initializeCXXLayout(const CXXRecordDecl *RD);
2584 void layoutNonVirtualBases(const CXXRecordDecl *RD);
2585 void layoutNonVirtualBase(const CXXRecordDecl *RD,
2586 const CXXRecordDecl *BaseDecl,
2587 const ASTRecordLayout &BaseLayout,
2588 const ASTRecordLayout *&PreviousBaseLayout);
2589 void injectVFPtr(const CXXRecordDecl *RD);
2590 void injectVBPtr(const CXXRecordDecl *RD);
2591 /// Lays out the fields of the record. Also rounds size up to
2592 /// alignment.
2593 void layoutFields(const RecordDecl *RD);
2594 void layoutField(const FieldDecl *FD);
2595 void layoutBitField(const FieldDecl *FD);
2596 /// Lays out a single zero-width bit-field in the record and handles
2597 /// special cases associated with zero-width bit-fields.
2598 void layoutZeroWidthBitField(const FieldDecl *FD);
2599 void layoutVirtualBases(const CXXRecordDecl *RD);
2600 void finalizeLayout(const RecordDecl *RD);
2601 /// Gets the size and alignment of a base taking pragma pack and
2602 /// __declspec(align) into account.
2603 ElementInfo getAdjustedElementInfo(const ASTRecordLayout &Layout);
2604 /// Gets the size and alignment of a field taking pragma pack and
2605 /// __declspec(align) into account. It also updates RequiredAlignment as a
2606 /// side effect because it is most convenient to do so here.
2607 ElementInfo getAdjustedElementInfo(const FieldDecl *FD);
2608 /// Places a field at an offset in CharUnits.
2609 void placeFieldAtOffset(CharUnits FieldOffset) {
2610 FieldOffsets.push_back(Elt: Context.toBits(CharSize: FieldOffset));
2611 }
2612 /// Places a bitfield at a bit offset.
2613 void placeFieldAtBitOffset(uint64_t FieldOffset) {
2614 FieldOffsets.push_back(Elt: FieldOffset);
2615 }
2616 /// Compute the set of virtual bases for which vtordisps are required.
2617 void computeVtorDispSet(
2618 llvm::SmallPtrSetImpl<const CXXRecordDecl *> &HasVtorDispSet,
2619 const CXXRecordDecl *RD) const;
2620 const ASTContext &Context;
2621 EmptySubobjectMap *EmptySubobjects;
2622
2623 /// The size of the record being laid out.
2624 CharUnits Size;
2625 /// The non-virtual size of the record layout.
2626 CharUnits NonVirtualSize;
2627 /// The data size of the record layout.
2628 CharUnits DataSize;
2629 /// The current alignment of the record layout.
2630 CharUnits Alignment;
2631 /// The maximum allowed field alignment. This is set by #pragma pack.
2632 CharUnits MaxFieldAlignment;
2633 /// The alignment that this record must obey. This is imposed by
2634 /// __declspec(align()) on the record itself or one of its fields or bases.
2635 CharUnits RequiredAlignment;
2636 /// The size of the allocation of the currently active bitfield.
2637 /// This value isn't meaningful unless LastFieldIsNonZeroWidthBitfield
2638 /// is true.
2639 CharUnits CurrentBitfieldSize;
2640 /// Offset to the virtual base table pointer (if one exists).
2641 CharUnits VBPtrOffset;
2642 /// Minimum record size possible.
2643 CharUnits MinEmptyStructSize;
2644 /// The size and alignment info of a pointer.
2645 ElementInfo PointerInfo;
2646 /// The primary base class (if one exists).
2647 const CXXRecordDecl *PrimaryBase;
2648 /// The class we share our vb-pointer with.
2649 const CXXRecordDecl *SharedVBPtrBase;
2650 /// The collection of field offsets.
2651 SmallVector<uint64_t, 16> FieldOffsets;
2652 /// Base classes and their offsets in the record.
2653 BaseOffsetsMapTy Bases;
2654 /// virtual base classes and their offsets in the record.
2655 ASTRecordLayout::VBaseOffsetsMapTy VBases;
2656 /// The number of remaining bits in our last bitfield allocation.
2657 unsigned RemainingBitsInField;
2658 bool IsUnion : 1;
2659 /// True if the last field laid out was a bitfield and was not 0
2660 /// width.
2661 bool LastFieldIsNonZeroWidthBitfield : 1;
2662 /// True if the class has its own vftable pointer.
2663 bool HasOwnVFPtr : 1;
2664 /// True if the class has a vbtable pointer.
2665 bool HasVBPtr : 1;
2666 /// True if the last sub-object within the type is zero sized or the
2667 /// object itself is zero sized. This *does not* count members that are not
2668 /// records. Only used for MS-ABI.
2669 bool EndsWithZeroSizedObject : 1;
2670 /// True if this class is zero sized or first base is zero sized or
2671 /// has this property. Only used for MS-ABI.
2672 bool LeadsWithZeroSizedBase : 1;
2673
2674 /// True if the external AST source provided a layout for this record.
2675 bool UseExternalLayout : 1;
2676
2677 /// The layout provided by the external AST source. Only active if
2678 /// UseExternalLayout is true.
2679 ExternalLayout External;
2680};
2681} // namespace
2682
2683MicrosoftRecordLayoutBuilder::ElementInfo
2684MicrosoftRecordLayoutBuilder::getAdjustedElementInfo(
2685 const ASTRecordLayout &Layout) {
2686 ElementInfo Info;
2687 Info.Alignment = Layout.getAlignment();
2688 // Respect pragma pack.
2689 if (!MaxFieldAlignment.isZero())
2690 Info.Alignment = std::min(a: Info.Alignment, b: MaxFieldAlignment);
2691 // Track zero-sized subobjects here where it's already available.
2692 EndsWithZeroSizedObject = Layout.endsWithZeroSizedObject();
2693 // Respect required alignment, this is necessary because we may have adjusted
2694 // the alignment in the case of pragma pack. Note that the required alignment
2695 // doesn't actually apply to the struct alignment at this point.
2696 Alignment = std::max(a: Alignment, b: Info.Alignment);
2697 RequiredAlignment = std::max(a: RequiredAlignment, b: Layout.getRequiredAlignment());
2698 Info.Alignment = std::max(a: Info.Alignment, b: Layout.getRequiredAlignment());
2699 Info.Size = Layout.getNonVirtualSize();
2700 return Info;
2701}
2702
2703MicrosoftRecordLayoutBuilder::ElementInfo
2704MicrosoftRecordLayoutBuilder::getAdjustedElementInfo(
2705 const FieldDecl *FD) {
2706 // Get the alignment of the field type's natural alignment, ignore any
2707 // alignment attributes.
2708 auto TInfo =
2709 Context.getTypeInfoInChars(FD->getType()->getUnqualifiedDesugaredType());
2710 ElementInfo Info{TInfo.Width, TInfo.Align};
2711 // Respect align attributes on the field.
2712 CharUnits FieldRequiredAlignment =
2713 Context.toCharUnitsFromBits(BitSize: FD->getMaxAlignment());
2714 // Respect align attributes on the type.
2715 if (Context.isAlignmentRequired(FD->getType()))
2716 FieldRequiredAlignment = std::max(
2717 Context.getTypeAlignInChars(FD->getType()), FieldRequiredAlignment);
2718 // Respect attributes applied to subobjects of the field.
2719 if (FD->isBitField())
2720 // For some reason __declspec align impacts alignment rather than required
2721 // alignment when it is applied to bitfields.
2722 Info.Alignment = std::max(a: Info.Alignment, b: FieldRequiredAlignment);
2723 else {
2724 if (auto RT =
2725 FD->getType()->getBaseElementTypeUnsafe()->getAs<RecordType>()) {
2726 auto const &Layout = Context.getASTRecordLayout(D: RT->getDecl());
2727 EndsWithZeroSizedObject = Layout.endsWithZeroSizedObject();
2728 FieldRequiredAlignment = std::max(FieldRequiredAlignment,
2729 Layout.getRequiredAlignment());
2730 }
2731 // Capture required alignment as a side-effect.
2732 RequiredAlignment = std::max(a: RequiredAlignment, b: FieldRequiredAlignment);
2733 }
2734 // Respect pragma pack, attribute pack and declspec align
2735 if (!MaxFieldAlignment.isZero())
2736 Info.Alignment = std::min(a: Info.Alignment, b: MaxFieldAlignment);
2737 if (FD->hasAttr<PackedAttr>())
2738 Info.Alignment = CharUnits::One();
2739 Info.Alignment = std::max(a: Info.Alignment, b: FieldRequiredAlignment);
2740 return Info;
2741}
2742
2743void MicrosoftRecordLayoutBuilder::layout(const RecordDecl *RD) {
2744 // For C record layout, zero-sized records always have size 4.
2745 MinEmptyStructSize = CharUnits::fromQuantity(Quantity: 4);
2746 initializeLayout(RD);
2747 layoutFields(RD);
2748 DataSize = Size = Size.alignTo(Align: Alignment);
2749 RequiredAlignment = std::max(
2750 RequiredAlignment, Context.toCharUnitsFromBits(BitSize: RD->getMaxAlignment()));
2751 finalizeLayout(RD);
2752}
2753
2754void MicrosoftRecordLayoutBuilder::cxxLayout(const CXXRecordDecl *RD) {
2755 // The C++ standard says that empty structs have size 1.
2756 MinEmptyStructSize = CharUnits::One();
2757 initializeLayout(RD);
2758 initializeCXXLayout(RD);
2759 layoutNonVirtualBases(RD);
2760 layoutFields(RD);
2761 injectVBPtr(RD);
2762 injectVFPtr(RD);
2763 if (HasOwnVFPtr || (HasVBPtr && !SharedVBPtrBase))
2764 Alignment = std::max(a: Alignment, b: PointerInfo.Alignment);
2765 auto RoundingAlignment = Alignment;
2766 if (!MaxFieldAlignment.isZero())
2767 RoundingAlignment = std::min(a: RoundingAlignment, b: MaxFieldAlignment);
2768 if (!UseExternalLayout)
2769 Size = Size.alignTo(Align: RoundingAlignment);
2770 NonVirtualSize = Size;
2771 RequiredAlignment = std::max(
2772 RequiredAlignment, Context.toCharUnitsFromBits(BitSize: RD->getMaxAlignment()));
2773 layoutVirtualBases(RD);
2774 finalizeLayout(RD);
2775}
2776
2777void MicrosoftRecordLayoutBuilder::initializeLayout(const RecordDecl *RD) {
2778 IsUnion = RD->isUnion();
2779 Size = CharUnits::Zero();
2780 Alignment = CharUnits::One();
2781 // In 64-bit mode we always perform an alignment step after laying out vbases.
2782 // In 32-bit mode we do not. The check to see if we need to perform alignment
2783 // checks the RequiredAlignment field and performs alignment if it isn't 0.
2784 RequiredAlignment = Context.getTargetInfo().getTriple().isArch64Bit()
2785 ? CharUnits::One()
2786 : CharUnits::Zero();
2787 // Compute the maximum field alignment.
2788 MaxFieldAlignment = CharUnits::Zero();
2789 // Honor the default struct packing maximum alignment flag.
2790 if (unsigned DefaultMaxFieldAlignment = Context.getLangOpts().PackStruct)
2791 MaxFieldAlignment = CharUnits::fromQuantity(Quantity: DefaultMaxFieldAlignment);
2792 // Honor the packing attribute. The MS-ABI ignores pragma pack if its larger
2793 // than the pointer size.
2794 if (const MaxFieldAlignmentAttr *MFAA = RD->getAttr<MaxFieldAlignmentAttr>()){
2795 unsigned PackedAlignment = MFAA->getAlignment();
2796 if (PackedAlignment <=
2797 Context.getTargetInfo().getPointerWidth(AddrSpace: LangAS::Default))
2798 MaxFieldAlignment = Context.toCharUnitsFromBits(BitSize: PackedAlignment);
2799 }
2800 // Packed attribute forces max field alignment to be 1.
2801 if (RD->hasAttr<PackedAttr>())
2802 MaxFieldAlignment = CharUnits::One();
2803
2804 // Try to respect the external layout if present.
2805 UseExternalLayout = false;
2806 if (ExternalASTSource *Source = Context.getExternalSource())
2807 UseExternalLayout = Source->layoutRecordType(
2808 Record: RD, Size&: External.Size, Alignment&: External.Align, FieldOffsets&: External.FieldOffsets,
2809 BaseOffsets&: External.BaseOffsets, VirtualBaseOffsets&: External.VirtualBaseOffsets);
2810}
2811
2812void
2813MicrosoftRecordLayoutBuilder::initializeCXXLayout(const CXXRecordDecl *RD) {
2814 EndsWithZeroSizedObject = false;
2815 LeadsWithZeroSizedBase = false;
2816 HasOwnVFPtr = false;
2817 HasVBPtr = false;
2818 PrimaryBase = nullptr;
2819 SharedVBPtrBase = nullptr;
2820 // Calculate pointer size and alignment. These are used for vfptr and vbprt
2821 // injection.
2822 PointerInfo.Size = Context.toCharUnitsFromBits(
2823 BitSize: Context.getTargetInfo().getPointerWidth(AddrSpace: LangAS::Default));
2824 PointerInfo.Alignment = Context.toCharUnitsFromBits(
2825 BitSize: Context.getTargetInfo().getPointerAlign(AddrSpace: LangAS::Default));
2826 // Respect pragma pack.
2827 if (!MaxFieldAlignment.isZero())
2828 PointerInfo.Alignment = std::min(a: PointerInfo.Alignment, b: MaxFieldAlignment);
2829}
2830
2831void
2832MicrosoftRecordLayoutBuilder::layoutNonVirtualBases(const CXXRecordDecl *RD) {
2833 // The MS-ABI lays out all bases that contain leading vfptrs before it lays
2834 // out any bases that do not contain vfptrs. We implement this as two passes
2835 // over the bases. This approach guarantees that the primary base is laid out
2836 // first. We use these passes to calculate some additional aggregated
2837 // information about the bases, such as required alignment and the presence of
2838 // zero sized members.
2839 const ASTRecordLayout *PreviousBaseLayout = nullptr;
2840 bool HasPolymorphicBaseClass = false;
2841 // Iterate through the bases and lay out the non-virtual ones.
2842 for (const CXXBaseSpecifier &Base : RD->bases()) {
2843 const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
2844 HasPolymorphicBaseClass |= BaseDecl->isPolymorphic();
2845 const ASTRecordLayout &BaseLayout = Context.getASTRecordLayout(BaseDecl);
2846 // Mark and skip virtual bases.
2847 if (Base.isVirtual()) {
2848 HasVBPtr = true;
2849 continue;
2850 }
2851 // Check for a base to share a VBPtr with.
2852 if (!SharedVBPtrBase && BaseLayout.hasVBPtr()) {
2853 SharedVBPtrBase = BaseDecl;
2854 HasVBPtr = true;
2855 }
2856 // Only lay out bases with extendable VFPtrs on the first pass.
2857 if (!BaseLayout.hasExtendableVFPtr())
2858 continue;
2859 // If we don't have a primary base, this one qualifies.
2860 if (!PrimaryBase) {
2861 PrimaryBase = BaseDecl;
2862 LeadsWithZeroSizedBase = BaseLayout.leadsWithZeroSizedBase();
2863 }
2864 // Lay out the base.
2865 layoutNonVirtualBase(RD, BaseDecl, BaseLayout, PreviousBaseLayout);
2866 }
2867 // Figure out if we need a fresh VFPtr for this class.
2868 if (RD->isPolymorphic()) {
2869 if (!HasPolymorphicBaseClass)
2870 // This class introduces polymorphism, so we need a vftable to store the
2871 // RTTI information.
2872 HasOwnVFPtr = true;
2873 else if (!PrimaryBase) {
2874 // We have a polymorphic base class but can't extend its vftable. Add a
2875 // new vfptr if we would use any vftable slots.
2876 for (CXXMethodDecl *M : RD->methods()) {
2877 if (MicrosoftVTableContext::hasVtableSlot(MD: M) &&
2878 M->size_overridden_methods() == 0) {
2879 HasOwnVFPtr = true;
2880 break;
2881 }
2882 }
2883 }
2884 }
2885 // If we don't have a primary base then we have a leading object that could
2886 // itself lead with a zero-sized object, something we track.
2887 bool CheckLeadingLayout = !PrimaryBase;
2888 // Iterate through the bases and lay out the non-virtual ones.
2889 for (const CXXBaseSpecifier &Base : RD->bases()) {
2890 if (Base.isVirtual())
2891 continue;
2892 const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
2893 const ASTRecordLayout &BaseLayout = Context.getASTRecordLayout(BaseDecl);
2894 // Only lay out bases without extendable VFPtrs on the second pass.
2895 if (BaseLayout.hasExtendableVFPtr()) {
2896 VBPtrOffset = Bases[BaseDecl] + BaseLayout.getNonVirtualSize();
2897 continue;
2898 }
2899 // If this is the first layout, check to see if it leads with a zero sized
2900 // object. If it does, so do we.
2901 if (CheckLeadingLayout) {
2902 CheckLeadingLayout = false;
2903 LeadsWithZeroSizedBase = BaseLayout.leadsWithZeroSizedBase();
2904 }
2905 // Lay out the base.
2906 layoutNonVirtualBase(RD, BaseDecl, BaseLayout, PreviousBaseLayout);
2907 VBPtrOffset = Bases[BaseDecl] + BaseLayout.getNonVirtualSize();
2908 }
2909 // Set our VBPtroffset if we know it at this point.
2910 if (!HasVBPtr)
2911 VBPtrOffset = CharUnits::fromQuantity(Quantity: -1);
2912 else if (SharedVBPtrBase) {
2913 const ASTRecordLayout &Layout = Context.getASTRecordLayout(SharedVBPtrBase);
2914 VBPtrOffset = Bases[SharedVBPtrBase] + Layout.getVBPtrOffset();
2915 }
2916}
2917
2918static bool recordUsesEBO(const RecordDecl *RD) {
2919 if (!isa<CXXRecordDecl>(Val: RD))
2920 return false;
2921 if (RD->hasAttr<EmptyBasesAttr>())
2922 return true;
2923 if (auto *LVA = RD->getAttr<LayoutVersionAttr>())
2924 // TODO: Double check with the next version of MSVC.
2925 if (LVA->getVersion() <= LangOptions::MSVC2015)
2926 return false;
2927 // TODO: Some later version of MSVC will change the default behavior of the
2928 // compiler to enable EBO by default. When this happens, we will need an
2929 // additional isCompatibleWithMSVC check.
2930 return false;
2931}
2932
2933void MicrosoftRecordLayoutBuilder::layoutNonVirtualBase(
2934 const CXXRecordDecl *RD, const CXXRecordDecl *BaseDecl,
2935 const ASTRecordLayout &BaseLayout,
2936 const ASTRecordLayout *&PreviousBaseLayout) {
2937 // Insert padding between two bases if the left first one is zero sized or
2938 // contains a zero sized subobject and the right is zero sized or one leads
2939 // with a zero sized base.
2940 bool MDCUsesEBO = recordUsesEBO(RD);
2941 if (PreviousBaseLayout && PreviousBaseLayout->endsWithZeroSizedObject() &&
2942 BaseLayout.leadsWithZeroSizedBase() && !MDCUsesEBO)
2943 Size++;
2944 ElementInfo Info = getAdjustedElementInfo(Layout: BaseLayout);
2945 CharUnits BaseOffset;
2946
2947 // Respect the external AST source base offset, if present.
2948 bool FoundBase = false;
2949 if (UseExternalLayout) {
2950 FoundBase = External.getExternalNVBaseOffset(RD: BaseDecl, BaseOffset);
2951 if (BaseOffset > Size) {
2952 Size = BaseOffset;
2953 }
2954 }
2955
2956 if (!FoundBase) {
2957 if (MDCUsesEBO && BaseDecl->isEmpty() &&
2958 (BaseLayout.getNonVirtualSize() == CharUnits::Zero())) {
2959 BaseOffset = CharUnits::Zero();
2960 } else {
2961 // Otherwise, lay the base out at the end of the MDC.
2962 BaseOffset = Size = Size.alignTo(Align: Info.Alignment);
2963 }
2964 }
2965 Bases.insert(KV: std::make_pair(x&: BaseDecl, y&: BaseOffset));
2966 Size += BaseLayout.getNonVirtualSize();
2967 DataSize = Size;
2968 PreviousBaseLayout = &BaseLayout;
2969}
2970
2971void MicrosoftRecordLayoutBuilder::layoutFields(const RecordDecl *RD) {
2972 LastFieldIsNonZeroWidthBitfield = false;
2973 for (const FieldDecl *Field : RD->fields())
2974 layoutField(FD: Field);
2975}
2976
2977void MicrosoftRecordLayoutBuilder::layoutField(const FieldDecl *FD) {
2978 if (FD->isBitField()) {
2979 layoutBitField(FD);
2980 return;
2981 }
2982 LastFieldIsNonZeroWidthBitfield = false;
2983 ElementInfo Info = getAdjustedElementInfo(FD);
2984 Alignment = std::max(a: Alignment, b: Info.Alignment);
2985
2986 const CXXRecordDecl *FieldClass = FD->getType()->getAsCXXRecordDecl();
2987 bool IsOverlappingEmptyField = FD->isPotentiallyOverlapping() &&
2988 FieldClass->isEmpty() &&
2989 FieldClass->fields().empty();
2990 CharUnits FieldOffset = CharUnits::Zero();
2991
2992 if (UseExternalLayout) {
2993 FieldOffset =
2994 Context.toCharUnitsFromBits(BitSize: External.getExternalFieldOffset(FD));
2995 } else if (IsUnion) {
2996 FieldOffset = CharUnits::Zero();
2997 } else if (EmptySubobjects) {
2998 if (!IsOverlappingEmptyField)
2999 FieldOffset = DataSize.alignTo(Align: Info.Alignment);
3000
3001 while (!EmptySubobjects->CanPlaceFieldAtOffset(FD, Offset: FieldOffset)) {
3002 const CXXRecordDecl *ParentClass = cast<CXXRecordDecl>(Val: FD->getParent());
3003 bool HasBases = ParentClass && (!ParentClass->bases().empty() ||
3004 !ParentClass->vbases().empty());
3005 if (FieldOffset == CharUnits::Zero() && DataSize != CharUnits::Zero() &&
3006 HasBases) {
3007 // MSVC appears to only do this when there are base classes;
3008 // otherwise it overlaps no_unique_address fields in non-zero offsets.
3009 FieldOffset = DataSize.alignTo(Align: Info.Alignment);
3010 } else {
3011 FieldOffset += Info.Alignment;
3012 }
3013 }
3014 } else {
3015 FieldOffset = Size.alignTo(Align: Info.Alignment);
3016 }
3017
3018 uint64_t UnpaddedFielddOffsetInBits =
3019 Context.toBits(CharSize: DataSize) - RemainingBitsInField;
3020
3021 ::CheckFieldPadding(Context, IsUnion, Offset: Context.toBits(CharSize: FieldOffset),
3022 UnpaddedOffset: UnpaddedFielddOffsetInBits, D: FD);
3023
3024 RemainingBitsInField = 0;
3025
3026 placeFieldAtOffset(FieldOffset);
3027
3028 if (!IsOverlappingEmptyField)
3029 DataSize = std::max(a: DataSize, b: FieldOffset + Info.Size);
3030
3031 Size = std::max(a: Size, b: FieldOffset + Info.Size);
3032}
3033
3034void MicrosoftRecordLayoutBuilder::layoutBitField(const FieldDecl *FD) {
3035 unsigned Width = FD->getBitWidthValue();
3036 if (Width == 0) {
3037 layoutZeroWidthBitField(FD);
3038 return;
3039 }
3040 ElementInfo Info = getAdjustedElementInfo(FD);
3041 // Clamp the bitfield to a containable size for the sake of being able
3042 // to lay them out. Sema will throw an error.
3043 if (Width > Context.toBits(CharSize: Info.Size))
3044 Width = Context.toBits(CharSize: Info.Size);
3045 // Check to see if this bitfield fits into an existing allocation. Note:
3046 // MSVC refuses to pack bitfields of formal types with different sizes
3047 // into the same allocation.
3048 if (!UseExternalLayout && !IsUnion && LastFieldIsNonZeroWidthBitfield &&
3049 CurrentBitfieldSize == Info.Size && Width <= RemainingBitsInField) {
3050 placeFieldAtBitOffset(FieldOffset: Context.toBits(CharSize: Size) - RemainingBitsInField);
3051 RemainingBitsInField -= Width;
3052 return;
3053 }
3054 LastFieldIsNonZeroWidthBitfield = true;
3055 CurrentBitfieldSize = Info.Size;
3056 if (UseExternalLayout) {
3057 auto FieldBitOffset = External.getExternalFieldOffset(FD);
3058 placeFieldAtBitOffset(FieldOffset: FieldBitOffset);
3059 auto NewSize = Context.toCharUnitsFromBits(
3060 BitSize: llvm::alignDown(Value: FieldBitOffset, Align: Context.toBits(CharSize: Info.Alignment)) +
3061 Context.toBits(CharSize: Info.Size));
3062 Size = std::max(a: Size, b: NewSize);
3063 Alignment = std::max(a: Alignment, b: Info.Alignment);
3064 } else if (IsUnion) {
3065 placeFieldAtOffset(FieldOffset: CharUnits::Zero());
3066 Size = std::max(a: Size, b: Info.Size);
3067 // TODO: Add a Sema warning that MS ignores bitfield alignment in unions.
3068 } else {
3069 // Allocate a new block of memory and place the bitfield in it.
3070 CharUnits FieldOffset = Size.alignTo(Align: Info.Alignment);
3071 uint64_t UnpaddedFieldOffsetInBits =
3072 Context.toBits(CharSize: DataSize) - RemainingBitsInField;
3073 placeFieldAtOffset(FieldOffset);
3074 Size = FieldOffset + Info.Size;
3075 Alignment = std::max(a: Alignment, b: Info.Alignment);
3076 RemainingBitsInField = Context.toBits(CharSize: Info.Size) - Width;
3077 ::CheckFieldPadding(Context, IsUnion, Offset: Context.toBits(CharSize: FieldOffset),
3078 UnpaddedOffset: UnpaddedFieldOffsetInBits, D: FD);
3079 }
3080 DataSize = Size;
3081}
3082
3083void
3084MicrosoftRecordLayoutBuilder::layoutZeroWidthBitField(const FieldDecl *FD) {
3085 // Zero-width bitfields are ignored unless they follow a non-zero-width
3086 // bitfield.
3087 if (!LastFieldIsNonZeroWidthBitfield) {
3088 placeFieldAtOffset(FieldOffset: IsUnion ? CharUnits::Zero() : Size);
3089 // TODO: Add a Sema warning that MS ignores alignment for zero
3090 // sized bitfields that occur after zero-size bitfields or non-bitfields.
3091 return;
3092 }
3093 LastFieldIsNonZeroWidthBitfield = false;
3094 ElementInfo Info = getAdjustedElementInfo(FD);
3095 if (IsUnion) {
3096 placeFieldAtOffset(FieldOffset: CharUnits::Zero());
3097 Size = std::max(a: Size, b: Info.Size);
3098 // TODO: Add a Sema warning that MS ignores bitfield alignment in unions.
3099 } else {
3100 // Round up the current record size to the field's alignment boundary.
3101 CharUnits FieldOffset = Size.alignTo(Align: Info.Alignment);
3102 uint64_t UnpaddedFieldOffsetInBits =
3103 Context.toBits(CharSize: DataSize) - RemainingBitsInField;
3104 placeFieldAtOffset(FieldOffset);
3105 RemainingBitsInField = 0;
3106 Size = FieldOffset;
3107 Alignment = std::max(a: Alignment, b: Info.Alignment);
3108 ::CheckFieldPadding(Context, IsUnion, Offset: Context.toBits(CharSize: FieldOffset),
3109 UnpaddedOffset: UnpaddedFieldOffsetInBits, D: FD);
3110 }
3111 DataSize = Size;
3112}
3113
3114void MicrosoftRecordLayoutBuilder::injectVBPtr(const CXXRecordDecl *RD) {
3115 if (!HasVBPtr || SharedVBPtrBase)
3116 return;
3117 // Inject the VBPointer at the injection site.
3118 CharUnits InjectionSite = VBPtrOffset;
3119 // But before we do, make sure it's properly aligned.
3120 VBPtrOffset = VBPtrOffset.alignTo(Align: PointerInfo.Alignment);
3121 // Determine where the first field should be laid out after the vbptr.
3122 CharUnits FieldStart = VBPtrOffset + PointerInfo.Size;
3123 // Shift everything after the vbptr down, unless we're using an external
3124 // layout.
3125 if (UseExternalLayout) {
3126 // It is possible that there were no fields or bases located after vbptr,
3127 // so the size was not adjusted before.
3128 if (Size < FieldStart)
3129 Size = FieldStart;
3130 return;
3131 }
3132 // Make sure that the amount we push the fields back by is a multiple of the
3133 // alignment.
3134 CharUnits Offset = (FieldStart - InjectionSite)
3135 .alignTo(Align: std::max(a: RequiredAlignment, b: Alignment));
3136 Size += Offset;
3137 for (uint64_t &FieldOffset : FieldOffsets)
3138 FieldOffset += Context.toBits(CharSize: Offset);
3139 for (BaseOffsetsMapTy::value_type &Base : Bases)
3140 if (Base.second >= InjectionSite)
3141 Base.second += Offset;
3142}
3143
3144void MicrosoftRecordLayoutBuilder::injectVFPtr(const CXXRecordDecl *RD) {
3145 if (!HasOwnVFPtr)
3146 return;
3147 // Make sure that the amount we push the struct back by is a multiple of the
3148 // alignment.
3149 CharUnits Offset =
3150 PointerInfo.Size.alignTo(Align: std::max(a: RequiredAlignment, b: Alignment));
3151 // Push back the vbptr, but increase the size of the object and push back
3152 // regular fields by the offset only if not using external record layout.
3153 if (HasVBPtr)
3154 VBPtrOffset += Offset;
3155
3156 if (UseExternalLayout) {
3157 // The class may have size 0 and a vfptr (e.g. it's an interface class). The
3158 // size was not correctly set before in this case.
3159 if (Size.isZero())
3160 Size += Offset;
3161 return;
3162 }
3163
3164 Size += Offset;
3165
3166 // If we're using an external layout, the fields offsets have already
3167 // accounted for this adjustment.
3168 for (uint64_t &FieldOffset : FieldOffsets)
3169 FieldOffset += Context.toBits(CharSize: Offset);
3170 for (BaseOffsetsMapTy::value_type &Base : Bases)
3171 Base.second += Offset;
3172}
3173
3174void MicrosoftRecordLayoutBuilder::layoutVirtualBases(const CXXRecordDecl *RD) {
3175 if (!HasVBPtr)
3176 return;
3177 // Vtordisps are always 4 bytes (even in 64-bit mode)
3178 CharUnits VtorDispSize = CharUnits::fromQuantity(Quantity: 4);
3179 CharUnits VtorDispAlignment = VtorDispSize;
3180 // vtordisps respect pragma pack.
3181 if (!MaxFieldAlignment.isZero())
3182 VtorDispAlignment = std::min(a: VtorDispAlignment, b: MaxFieldAlignment);
3183 // The alignment of the vtordisp is at least the required alignment of the
3184 // entire record. This requirement may be present to support vtordisp
3185 // injection.
3186 for (const CXXBaseSpecifier &VBase : RD->vbases()) {
3187 const CXXRecordDecl *BaseDecl = VBase.getType()->getAsCXXRecordDecl();
3188 const ASTRecordLayout &BaseLayout = Context.getASTRecordLayout(BaseDecl);
3189 RequiredAlignment =
3190 std::max(a: RequiredAlignment, b: BaseLayout.getRequiredAlignment());
3191 }
3192 VtorDispAlignment = std::max(a: VtorDispAlignment, b: RequiredAlignment);
3193 // Compute the vtordisp set.
3194 llvm::SmallPtrSet<const CXXRecordDecl *, 2> HasVtorDispSet;
3195 computeVtorDispSet(HasVtorDispSet, RD);
3196 // Iterate through the virtual bases and lay them out.
3197 const ASTRecordLayout *PreviousBaseLayout = nullptr;
3198 for (const CXXBaseSpecifier &VBase : RD->vbases()) {
3199 const CXXRecordDecl *BaseDecl = VBase.getType()->getAsCXXRecordDecl();
3200 const ASTRecordLayout &BaseLayout = Context.getASTRecordLayout(BaseDecl);
3201 bool HasVtordisp = HasVtorDispSet.contains(Ptr: BaseDecl);
3202 // Insert padding between two bases if the left first one is zero sized or
3203 // contains a zero sized subobject and the right is zero sized or one leads
3204 // with a zero sized base. The padding between virtual bases is 4
3205 // bytes (in both 32 and 64 bits modes) and always involves rounding up to
3206 // the required alignment, we don't know why.
3207 if ((PreviousBaseLayout && PreviousBaseLayout->endsWithZeroSizedObject() &&
3208 BaseLayout.leadsWithZeroSizedBase() && !recordUsesEBO(RD)) ||
3209 HasVtordisp) {
3210 Size = Size.alignTo(Align: VtorDispAlignment) + VtorDispSize;
3211 Alignment = std::max(a: VtorDispAlignment, b: Alignment);
3212 }
3213 // Insert the virtual base.
3214 ElementInfo Info = getAdjustedElementInfo(Layout: BaseLayout);
3215 CharUnits BaseOffset;
3216
3217 // Respect the external AST source base offset, if present.
3218 if (UseExternalLayout) {
3219 if (!External.getExternalVBaseOffset(RD: BaseDecl, BaseOffset))
3220 BaseOffset = Size;
3221 } else
3222 BaseOffset = Size.alignTo(Align: Info.Alignment);
3223
3224 assert(BaseOffset >= Size && "base offset already allocated");
3225
3226 VBases.insert(KV: std::make_pair(x&: BaseDecl,
3227 y: ASTRecordLayout::VBaseInfo(BaseOffset, HasVtordisp)));
3228 Size = BaseOffset + BaseLayout.getNonVirtualSize();
3229 PreviousBaseLayout = &BaseLayout;
3230 }
3231}
3232
3233void MicrosoftRecordLayoutBuilder::finalizeLayout(const RecordDecl *RD) {
3234 uint64_t UnpaddedSizeInBits = Context.toBits(CharSize: DataSize);
3235 UnpaddedSizeInBits -= RemainingBitsInField;
3236
3237 // MS ABI allocates 1 byte for empty class
3238 // (not padding)
3239 if (Size.isZero())
3240 UnpaddedSizeInBits += 8;
3241
3242 // Respect required alignment. Note that in 32-bit mode Required alignment
3243 // may be 0 and cause size not to be updated.
3244 DataSize = Size;
3245 if (!RequiredAlignment.isZero()) {
3246 Alignment = std::max(a: Alignment, b: RequiredAlignment);
3247 auto RoundingAlignment = Alignment;
3248 if (!MaxFieldAlignment.isZero())
3249 RoundingAlignment = std::min(a: RoundingAlignment, b: MaxFieldAlignment);
3250 RoundingAlignment = std::max(a: RoundingAlignment, b: RequiredAlignment);
3251 Size = Size.alignTo(Align: RoundingAlignment);
3252 }
3253 if (Size.isZero()) {
3254 if (!recordUsesEBO(RD) || !cast<CXXRecordDecl>(Val: RD)->isEmpty()) {
3255 EndsWithZeroSizedObject = true;
3256 LeadsWithZeroSizedBase = true;
3257 }
3258 // Zero-sized structures have size equal to their alignment if a
3259 // __declspec(align) came into play.
3260 if (RequiredAlignment >= MinEmptyStructSize)
3261 Size = Alignment;
3262 else
3263 Size = MinEmptyStructSize;
3264 }
3265
3266 if (UseExternalLayout) {
3267 Size = Context.toCharUnitsFromBits(BitSize: External.Size);
3268 if (External.Align)
3269 Alignment = Context.toCharUnitsFromBits(BitSize: External.Align);
3270 return;
3271 }
3272 unsigned CharBitNum = Context.getTargetInfo().getCharWidth();
3273 uint64_t SizeInBits = Context.toBits(CharSize: Size);
3274
3275 if (SizeInBits > UnpaddedSizeInBits) {
3276 unsigned int PadSize = SizeInBits - UnpaddedSizeInBits;
3277 bool InBits = true;
3278 if (PadSize % CharBitNum == 0) {
3279 PadSize = PadSize / CharBitNum;
3280 InBits = false;
3281 }
3282
3283 Context.getDiagnostics().Report(RD->getLocation(),
3284 diag::warn_padded_struct_size)
3285 << Context.getTypeDeclType(RD) << PadSize
3286 << (InBits ? 1 : 0); // (byte|bit)
3287 }
3288}
3289
3290// Recursively walks the non-virtual bases of a class and determines if any of
3291// them are in the bases with overridden methods set.
3292static bool
3293RequiresVtordisp(const llvm::SmallPtrSetImpl<const CXXRecordDecl *> &
3294 BasesWithOverriddenMethods,
3295 const CXXRecordDecl *RD) {
3296 if (BasesWithOverriddenMethods.count(Ptr: RD))
3297 return true;
3298 // If any of a virtual bases non-virtual bases (recursively) requires a
3299 // vtordisp than so does this virtual base.
3300 for (const CXXBaseSpecifier &Base : RD->bases())
3301 if (!Base.isVirtual() &&
3302 RequiresVtordisp(BasesWithOverriddenMethods,
3303 RD: Base.getType()->getAsCXXRecordDecl()))
3304 return true;
3305 return false;
3306}
3307
3308void MicrosoftRecordLayoutBuilder::computeVtorDispSet(
3309 llvm::SmallPtrSetImpl<const CXXRecordDecl *> &HasVtordispSet,
3310 const CXXRecordDecl *RD) const {
3311 // /vd2 or #pragma vtordisp(2): Always use vtordisps for virtual bases with
3312 // vftables.
3313 if (RD->getMSVtorDispMode() == MSVtorDispMode::ForVFTable) {
3314 for (const CXXBaseSpecifier &Base : RD->vbases()) {
3315 const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
3316 const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl);
3317 if (Layout.hasExtendableVFPtr())
3318 HasVtordispSet.insert(Ptr: BaseDecl);
3319 }
3320 return;
3321 }
3322
3323 // If any of our bases need a vtordisp for this type, so do we. Check our
3324 // direct bases for vtordisp requirements.
3325 for (const CXXBaseSpecifier &Base : RD->bases()) {
3326 const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
3327 const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl);
3328 for (const auto &bi : Layout.getVBaseOffsetsMap())
3329 if (bi.second.hasVtorDisp())
3330 HasVtordispSet.insert(bi.first);
3331 }
3332 // We don't introduce any additional vtordisps if either:
3333 // * A user declared constructor or destructor aren't declared.
3334 // * #pragma vtordisp(0) or the /vd0 flag are in use.
3335 if ((!RD->hasUserDeclaredConstructor() && !RD->hasUserDeclaredDestructor()) ||
3336 RD->getMSVtorDispMode() == MSVtorDispMode::Never)
3337 return;
3338 // /vd1 or #pragma vtordisp(1): Try to guess based on whether we think it's
3339 // possible for a partially constructed object with virtual base overrides to
3340 // escape a non-trivial constructor.
3341 assert(RD->getMSVtorDispMode() == MSVtorDispMode::ForVBaseOverride);
3342 // Compute a set of base classes which define methods we override. A virtual
3343 // base in this set will require a vtordisp. A virtual base that transitively
3344 // contains one of these bases as a non-virtual base will also require a
3345 // vtordisp.
3346 llvm::SmallPtrSet<const CXXMethodDecl *, 8> Work;
3347 llvm::SmallPtrSet<const CXXRecordDecl *, 2> BasesWithOverriddenMethods;
3348 // Seed the working set with our non-destructor, non-pure virtual methods.
3349 for (const CXXMethodDecl *MD : RD->methods())
3350 if (MicrosoftVTableContext::hasVtableSlot(MD) &&
3351 !isa<CXXDestructorDecl>(Val: MD) && !MD->isPureVirtual())
3352 Work.insert(Ptr: MD);
3353 while (!Work.empty()) {
3354 const CXXMethodDecl *MD = *Work.begin();
3355 auto MethodRange = MD->overridden_methods();
3356 // If a virtual method has no-overrides it lives in its parent's vtable.
3357 if (MethodRange.begin() == MethodRange.end())
3358 BasesWithOverriddenMethods.insert(Ptr: MD->getParent());
3359 else
3360 Work.insert_range(R&: MethodRange);
3361 // We've finished processing this element, remove it from the working set.
3362 Work.erase(Ptr: MD);
3363 }
3364 // For each of our virtual bases, check if it is in the set of overridden
3365 // bases or if it transitively contains a non-virtual base that is.
3366 for (const CXXBaseSpecifier &Base : RD->vbases()) {
3367 const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
3368 if (!HasVtordispSet.count(Ptr: BaseDecl) &&
3369 RequiresVtordisp(BasesWithOverriddenMethods, RD: BaseDecl))
3370 HasVtordispSet.insert(Ptr: BaseDecl);
3371 }
3372}
3373
3374/// getASTRecordLayout - Get or compute information about the layout of the
3375/// specified record (struct/union/class), which indicates its size and field
3376/// position information.
3377const ASTRecordLayout &
3378ASTContext::getASTRecordLayout(const RecordDecl *D) const {
3379 // These asserts test different things. A record has a definition
3380 // as soon as we begin to parse the definition. That definition is
3381 // not a complete definition (which is what isDefinition() tests)
3382 // until we *finish* parsing the definition.
3383
3384 if (D->hasExternalLexicalStorage() && !D->getDefinition())
3385 getExternalSource()->CompleteType(const_cast<RecordDecl*>(D));
3386 // Complete the redecl chain (if necessary).
3387 (void)D->getMostRecentDecl();
3388
3389 D = D->getDefinition();
3390 assert(D && "Cannot get layout of forward declarations!");
3391 assert(!D->isInvalidDecl() && "Cannot get layout of invalid decl!");
3392 assert(D->isCompleteDefinition() && "Cannot layout type before complete!");
3393
3394 // Look up this layout, if already laid out, return what we have.
3395 // Note that we can't save a reference to the entry because this function
3396 // is recursive.
3397 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
3398 if (Entry) return *Entry;
3399
3400 const ASTRecordLayout *NewEntry = nullptr;
3401
3402 if (isMsLayout(Context: *this)) {
3403 if (const auto *RD = dyn_cast<CXXRecordDecl>(Val: D)) {
3404 EmptySubobjectMap EmptySubobjects(*this, RD);
3405 MicrosoftRecordLayoutBuilder Builder(*this, &EmptySubobjects);
3406 Builder.cxxLayout(RD);
3407 NewEntry = new (*this) ASTRecordLayout(
3408 *this, Builder.Size, Builder.Alignment, Builder.Alignment,
3409 Builder.Alignment, Builder.RequiredAlignment, Builder.HasOwnVFPtr,
3410 Builder.HasOwnVFPtr || Builder.PrimaryBase, Builder.VBPtrOffset,
3411 Builder.DataSize, Builder.FieldOffsets, Builder.NonVirtualSize,
3412 Builder.Alignment, Builder.Alignment, CharUnits::Zero(),
3413 Builder.PrimaryBase, false, Builder.SharedVBPtrBase,
3414 Builder.EndsWithZeroSizedObject, Builder.LeadsWithZeroSizedBase,
3415 Builder.Bases, Builder.VBases);
3416 } else {
3417 MicrosoftRecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/nullptr);
3418 Builder.layout(RD: D);
3419 NewEntry = new (*this) ASTRecordLayout(
3420 *this, Builder.Size, Builder.Alignment, Builder.Alignment,
3421 Builder.Alignment, Builder.RequiredAlignment, Builder.Size,
3422 Builder.FieldOffsets);
3423 }
3424 } else {
3425 if (const auto *RD = dyn_cast<CXXRecordDecl>(Val: D)) {
3426 EmptySubobjectMap EmptySubobjects(*this, RD);
3427 ItaniumRecordLayoutBuilder Builder(*this, &EmptySubobjects);
3428 Builder.Layout(RD);
3429
3430 // In certain situations, we are allowed to lay out objects in the
3431 // tail-padding of base classes. This is ABI-dependent.
3432 // FIXME: this should be stored in the record layout.
3433 bool skipTailPadding =
3434 mustSkipTailPadding(ABI: getTargetInfo().getCXXABI(), RD);
3435
3436 // FIXME: This should be done in FinalizeLayout.
3437 CharUnits DataSize =
3438 skipTailPadding ? Builder.getSize() : Builder.getDataSize();
3439 CharUnits NonVirtualSize =
3440 skipTailPadding ? DataSize : Builder.NonVirtualSize;
3441 NewEntry = new (*this) ASTRecordLayout(
3442 *this, Builder.getSize(), Builder.Alignment,
3443 Builder.PreferredAlignment, Builder.UnadjustedAlignment,
3444 /*RequiredAlignment : used by MS-ABI)*/
3445 Builder.Alignment, Builder.HasOwnVFPtr, RD->isDynamicClass(),
3446 CharUnits::fromQuantity(Quantity: -1), DataSize, Builder.FieldOffsets,
3447 NonVirtualSize, Builder.NonVirtualAlignment,
3448 Builder.PreferredNVAlignment,
3449 EmptySubobjects.SizeOfLargestEmptySubobject, Builder.PrimaryBase,
3450 Builder.PrimaryBaseIsVirtual, nullptr, false, false, Builder.Bases,
3451 Builder.VBases);
3452 } else {
3453 ItaniumRecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/nullptr);
3454 Builder.Layout(D);
3455
3456 NewEntry = new (*this) ASTRecordLayout(
3457 *this, Builder.getSize(), Builder.Alignment,
3458 Builder.PreferredAlignment, Builder.UnadjustedAlignment,
3459 /*RequiredAlignment : used by MS-ABI)*/
3460 Builder.Alignment, Builder.getSize(), Builder.FieldOffsets);
3461 }
3462 }
3463
3464 ASTRecordLayouts[D] = NewEntry;
3465
3466 if (getLangOpts().DumpRecordLayouts) {
3467 llvm::outs() << "\n*** Dumping AST Record Layout\n";
3468 DumpRecordLayout(RD: D, OS&: llvm::outs(), Simple: getLangOpts().DumpRecordLayoutsSimple);
3469 }
3470
3471 return *NewEntry;
3472}
3473
3474const CXXMethodDecl *ASTContext::getCurrentKeyFunction(const CXXRecordDecl *RD) {
3475 if (!getTargetInfo().getCXXABI().hasKeyFunctions())
3476 return nullptr;
3477
3478 assert(RD->getDefinition() && "Cannot get key function for forward decl!");
3479 RD = RD->getDefinition();
3480
3481 // Beware:
3482 // 1) computing the key function might trigger deserialization, which might
3483 // invalidate iterators into KeyFunctions
3484 // 2) 'get' on the LazyDeclPtr might also trigger deserialization and
3485 // invalidate the LazyDeclPtr within the map itself
3486 LazyDeclPtr Entry = KeyFunctions[RD];
3487 const Decl *Result =
3488 Entry ? Entry.get(Source: getExternalSource()) : computeKeyFunction(Context&: *this, RD);
3489
3490 // Store it back if it changed.
3491 if (Entry.isOffset() || Entry.isValid() != bool(Result))
3492 KeyFunctions[RD] = const_cast<Decl*>(Result);
3493
3494 return cast_or_null<CXXMethodDecl>(Val: Result);
3495}
3496
3497void ASTContext::setNonKeyFunction(const CXXMethodDecl *Method) {
3498 assert(Method == Method->getFirstDecl() &&
3499 "not working with method declaration from class definition");
3500
3501 // Look up the cache entry. Since we're working with the first
3502 // declaration, its parent must be the class definition, which is
3503 // the correct key for the KeyFunctions hash.
3504 const auto &Map = KeyFunctions;
3505 auto I = Map.find(Val: Method->getParent());
3506
3507 // If it's not cached, there's nothing to do.
3508 if (I == Map.end()) return;
3509
3510 // If it is cached, check whether it's the target method, and if so,
3511 // remove it from the cache. Note, the call to 'get' might invalidate
3512 // the iterator and the LazyDeclPtr object within the map.
3513 LazyDeclPtr Ptr = I->second;
3514 if (Ptr.get(Source: getExternalSource()) == Method) {
3515 // FIXME: remember that we did this for module / chained PCH state?
3516 KeyFunctions.erase(Val: Method->getParent());
3517 }
3518}
3519
3520static uint64_t getFieldOffset(const ASTContext &C, const FieldDecl *FD) {
3521 const ASTRecordLayout &Layout = C.getASTRecordLayout(D: FD->getParent());
3522 return Layout.getFieldOffset(FieldNo: FD->getFieldIndex());
3523}
3524
3525uint64_t ASTContext::getFieldOffset(const ValueDecl *VD) const {
3526 uint64_t OffsetInBits;
3527 if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: VD)) {
3528 OffsetInBits = ::getFieldOffset(C: *this, FD);
3529 } else {
3530 const IndirectFieldDecl *IFD = cast<IndirectFieldDecl>(Val: VD);
3531
3532 OffsetInBits = 0;
3533 for (const NamedDecl *ND : IFD->chain())
3534 OffsetInBits += ::getFieldOffset(C: *this, FD: cast<FieldDecl>(Val: ND));
3535 }
3536
3537 return OffsetInBits;
3538}
3539
3540uint64_t ASTContext::lookupFieldBitOffset(const ObjCInterfaceDecl *OID,
3541 const ObjCIvarDecl *Ivar) const {
3542 Ivar = Ivar->getCanonicalDecl();
3543 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
3544 const ASTRecordLayout *RL = &getASTObjCInterfaceLayout(D: Container);
3545
3546 // Compute field index.
3547 //
3548 // FIXME: The index here is closely tied to how ASTContext::getObjCLayout is
3549 // implemented. This should be fixed to get the information from the layout
3550 // directly.
3551 unsigned Index = 0;
3552
3553 for (const ObjCIvarDecl *IVD = Container->all_declared_ivar_begin();
3554 IVD; IVD = IVD->getNextIvar()) {
3555 if (Ivar == IVD)
3556 break;
3557 ++Index;
3558 }
3559 assert(Index < RL->getFieldCount() && "Ivar is not inside record layout!");
3560
3561 return RL->getFieldOffset(FieldNo: Index);
3562}
3563
3564/// getObjCLayout - Get or compute information about the layout of the
3565/// given interface.
3566///
3567/// \param Impl - If given, also include the layout of the interface's
3568/// implementation. This may differ by including synthesized ivars.
3569const ASTRecordLayout &
3570ASTContext::getObjCLayout(const ObjCInterfaceDecl *D) const {
3571 // Retrieve the definition
3572 if (D->hasExternalLexicalStorage() && !D->getDefinition())
3573 getExternalSource()->CompleteType(Class: const_cast<ObjCInterfaceDecl*>(D));
3574 D = D->getDefinition();
3575 assert(D && !D->isInvalidDecl() && D->isThisDeclarationADefinition() &&
3576 "Invalid interface decl!");
3577
3578 // Look up this layout, if already laid out, return what we have.
3579 if (const ASTRecordLayout *Entry = ObjCLayouts[D])
3580 return *Entry;
3581
3582 ItaniumRecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/nullptr);
3583 Builder.Layout(D);
3584
3585 const ASTRecordLayout *NewEntry = new (*this) ASTRecordLayout(
3586 *this, Builder.getSize(), Builder.Alignment, Builder.PreferredAlignment,
3587 Builder.UnadjustedAlignment,
3588 /*RequiredAlignment : used by MS-ABI)*/
3589 Builder.Alignment, Builder.getDataSize(), Builder.FieldOffsets);
3590
3591 ObjCLayouts[D] = NewEntry;
3592
3593 return *NewEntry;
3594}
3595
3596static void PrintOffset(raw_ostream &OS,
3597 CharUnits Offset, unsigned IndentLevel) {
3598 OS << llvm::format(Fmt: "%10" PRId64 " | ", Vals: (int64_t)Offset.getQuantity());
3599 OS.indent(NumSpaces: IndentLevel * 2);
3600}
3601
3602static void PrintBitFieldOffset(raw_ostream &OS, CharUnits Offset,
3603 unsigned Begin, unsigned Width,
3604 unsigned IndentLevel) {
3605 llvm::SmallString<10> Buffer;
3606 {
3607 llvm::raw_svector_ostream BufferOS(Buffer);
3608 BufferOS << Offset.getQuantity() << ':';
3609 if (Width == 0) {
3610 BufferOS << '-';
3611 } else {
3612 BufferOS << Begin << '-' << (Begin + Width - 1);
3613 }
3614 }
3615
3616 OS << llvm::right_justify(Str: Buffer, Width: 10) << " | ";
3617 OS.indent(NumSpaces: IndentLevel * 2);
3618}
3619
3620static void PrintIndentNoOffset(raw_ostream &OS, unsigned IndentLevel) {
3621 OS << " | ";
3622 OS.indent(NumSpaces: IndentLevel * 2);
3623}
3624
3625static void DumpRecordLayout(raw_ostream &OS, const RecordDecl *RD,
3626 const ASTContext &C,
3627 CharUnits Offset,
3628 unsigned IndentLevel,
3629 const char* Description,
3630 bool PrintSizeInfo,
3631 bool IncludeVirtualBases) {
3632 const ASTRecordLayout &Layout = C.getASTRecordLayout(D: RD);
3633 auto CXXRD = dyn_cast<CXXRecordDecl>(Val: RD);
3634
3635 PrintOffset(OS, Offset, IndentLevel);
3636 OS << C.getTypeDeclType(const_cast<RecordDecl *>(RD));
3637 if (Description)
3638 OS << ' ' << Description;
3639 if (CXXRD && CXXRD->isEmpty())
3640 OS << " (empty)";
3641 OS << '\n';
3642
3643 IndentLevel++;
3644
3645 // Dump bases.
3646 if (CXXRD) {
3647 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
3648 bool HasOwnVFPtr = Layout.hasOwnVFPtr();
3649 bool HasOwnVBPtr = Layout.hasOwnVBPtr();
3650
3651 // Vtable pointer.
3652 if (CXXRD->isDynamicClass() && !PrimaryBase && !isMsLayout(Context: C)) {
3653 PrintOffset(OS, Offset, IndentLevel);
3654 OS << '(' << *RD << " vtable pointer)\n";
3655 } else if (HasOwnVFPtr) {
3656 PrintOffset(OS, Offset, IndentLevel);
3657 // vfptr (for Microsoft C++ ABI)
3658 OS << '(' << *RD << " vftable pointer)\n";
3659 }
3660
3661 // Collect nvbases.
3662 SmallVector<const CXXRecordDecl *, 4> Bases;
3663 for (const CXXBaseSpecifier &Base : CXXRD->bases()) {
3664 assert(!Base.getType()->isDependentType() &&
3665 "Cannot layout class with dependent bases.");
3666 if (!Base.isVirtual())
3667 Bases.push_back(Elt: Base.getType()->getAsCXXRecordDecl());
3668 }
3669
3670 // Sort nvbases by offset.
3671 llvm::stable_sort(
3672 Range&: Bases, C: [&](const CXXRecordDecl *L, const CXXRecordDecl *R) {
3673 return Layout.getBaseClassOffset(Base: L) < Layout.getBaseClassOffset(Base: R);
3674 });
3675
3676 // Dump (non-virtual) bases
3677 for (const CXXRecordDecl *Base : Bases) {
3678 CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base);
3679 DumpRecordLayout(OS, Base, C, BaseOffset, IndentLevel,
3680 Base == PrimaryBase ? "(primary base)" : "(base)",
3681 /*PrintSizeInfo=*/false,
3682 /*IncludeVirtualBases=*/false);
3683 }
3684
3685 // vbptr (for Microsoft C++ ABI)
3686 if (HasOwnVBPtr) {
3687 PrintOffset(OS, Offset: Offset + Layout.getVBPtrOffset(), IndentLevel);
3688 OS << '(' << *RD << " vbtable pointer)\n";
3689 }
3690 }
3691
3692 // Dump fields.
3693 for (const FieldDecl *Field : RD->fields()) {
3694 uint64_t LocalFieldOffsetInBits =
3695 Layout.getFieldOffset(FieldNo: Field->getFieldIndex());
3696 CharUnits FieldOffset =
3697 Offset + C.toCharUnitsFromBits(BitSize: LocalFieldOffsetInBits);
3698
3699 // Recursively dump fields of record type.
3700 if (auto RT = Field->getType()->getAs<RecordType>()) {
3701 DumpRecordLayout(OS, RT->getDecl(), C, FieldOffset, IndentLevel,
3702 Field->getName().data(),
3703 /*PrintSizeInfo=*/false,
3704 /*IncludeVirtualBases=*/true);
3705 continue;
3706 }
3707
3708 if (Field->isBitField()) {
3709 uint64_t LocalFieldByteOffsetInBits = C.toBits(CharSize: FieldOffset - Offset);
3710 unsigned Begin = LocalFieldOffsetInBits - LocalFieldByteOffsetInBits;
3711 unsigned Width = Field->getBitWidthValue();
3712 PrintBitFieldOffset(OS, Offset: FieldOffset, Begin, Width, IndentLevel);
3713 } else {
3714 PrintOffset(OS, Offset: FieldOffset, IndentLevel);
3715 }
3716 const QualType &FieldType = C.getLangOpts().DumpRecordLayoutsCanonical
3717 ? Field->getType().getCanonicalType()
3718 : Field->getType();
3719 OS << FieldType << ' ' << *Field << '\n';
3720 }
3721
3722 // Dump virtual bases.
3723 if (CXXRD && IncludeVirtualBases) {
3724 const ASTRecordLayout::VBaseOffsetsMapTy &VtorDisps =
3725 Layout.getVBaseOffsetsMap();
3726
3727 for (const CXXBaseSpecifier &Base : CXXRD->vbases()) {
3728 assert(Base.isVirtual() && "Found non-virtual class!");
3729 const CXXRecordDecl *VBase = Base.getType()->getAsCXXRecordDecl();
3730
3731 CharUnits VBaseOffset = Offset + Layout.getVBaseClassOffset(VBase);
3732
3733 if (VtorDisps.find(Val: VBase)->second.hasVtorDisp()) {
3734 PrintOffset(OS, Offset: VBaseOffset - CharUnits::fromQuantity(Quantity: 4), IndentLevel);
3735 OS << "(vtordisp for vbase " << *VBase << ")\n";
3736 }
3737
3738 DumpRecordLayout(OS, VBase, C, VBaseOffset, IndentLevel,
3739 VBase == Layout.getPrimaryBase() ?
3740 "(primary virtual base)" : "(virtual base)",
3741 /*PrintSizeInfo=*/false,
3742 /*IncludeVirtualBases=*/false);
3743 }
3744 }
3745
3746 if (!PrintSizeInfo) return;
3747
3748 PrintIndentNoOffset(OS, IndentLevel: IndentLevel - 1);
3749 OS << "[sizeof=" << Layout.getSize().getQuantity();
3750 if (CXXRD && !isMsLayout(Context: C))
3751 OS << ", dsize=" << Layout.getDataSize().getQuantity();
3752 OS << ", align=" << Layout.getAlignment().getQuantity();
3753 if (C.getTargetInfo().defaultsToAIXPowerAlignment())
3754 OS << ", preferredalign=" << Layout.getPreferredAlignment().getQuantity();
3755
3756 if (CXXRD) {
3757 OS << ",\n";
3758 PrintIndentNoOffset(OS, IndentLevel: IndentLevel - 1);
3759 OS << " nvsize=" << Layout.getNonVirtualSize().getQuantity();
3760 OS << ", nvalign=" << Layout.getNonVirtualAlignment().getQuantity();
3761 if (C.getTargetInfo().defaultsToAIXPowerAlignment())
3762 OS << ", preferrednvalign="
3763 << Layout.getPreferredNVAlignment().getQuantity();
3764 }
3765 OS << "]\n";
3766}
3767
3768void ASTContext::DumpRecordLayout(const RecordDecl *RD, raw_ostream &OS,
3769 bool Simple) const {
3770 if (!Simple) {
3771 ::DumpRecordLayout(OS, RD, C: *this, Offset: CharUnits(), IndentLevel: 0, Description: nullptr,
3772 /*PrintSizeInfo*/ true,
3773 /*IncludeVirtualBases=*/true);
3774 return;
3775 }
3776
3777 // The "simple" format is designed to be parsed by the
3778 // layout-override testing code. There shouldn't be any external
3779 // uses of this format --- when LLDB overrides a layout, it sets up
3780 // the data structures directly --- so feel free to adjust this as
3781 // you like as long as you also update the rudimentary parser for it
3782 // in libFrontend.
3783
3784 const ASTRecordLayout &Info = getASTRecordLayout(D: RD);
3785 OS << "Type: " << getTypeDeclType(RD) << "\n";
3786 OS << "\nLayout: ";
3787 OS << "<ASTRecordLayout\n";
3788 OS << " Size:" << toBits(CharSize: Info.getSize()) << "\n";
3789 if (!isMsLayout(Context: *this))
3790 OS << " DataSize:" << toBits(CharSize: Info.getDataSize()) << "\n";
3791 OS << " Alignment:" << toBits(CharSize: Info.getAlignment()) << "\n";
3792 if (Target->defaultsToAIXPowerAlignment())
3793 OS << " PreferredAlignment:" << toBits(CharSize: Info.getPreferredAlignment())
3794 << "\n";
3795 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
3796 OS << " BaseOffsets: [";
3797 const CXXRecordDecl *Base = nullptr;
3798 for (auto I : CXXRD->bases()) {
3799 if (I.isVirtual())
3800 continue;
3801 if (Base)
3802 OS << ", ";
3803 Base = I.getType()->getAsCXXRecordDecl();
3804 OS << Info.CXXInfo->BaseOffsets[Base].getQuantity();
3805 }
3806 OS << "]>\n";
3807 OS << " VBaseOffsets: [";
3808 const CXXRecordDecl *VBase = nullptr;
3809 for (auto I : CXXRD->vbases()) {
3810 if (VBase)
3811 OS << ", ";
3812 VBase = I.getType()->getAsCXXRecordDecl();
3813 OS << Info.CXXInfo->VBaseOffsets[VBase].VBaseOffset.getQuantity();
3814 }
3815 OS << "]>\n";
3816 }
3817 OS << " FieldOffsets: [";
3818 for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i) {
3819 if (i)
3820 OS << ", ";
3821 OS << Info.getFieldOffset(FieldNo: i);
3822 }
3823 OS << "]>\n";
3824}
3825

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of clang/lib/AST/RecordLayoutBuilder.cpp