File: StructLayout.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (642 lines) | stat: -rw-r--r-- 24,572 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
//===--- StructLayout.cpp - Layout of structures --------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
//  This file implements algorithms for laying out structures.
//
//===----------------------------------------------------------------------===//

#include "llvm/Support/ErrorHandling.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/DiagnosticsIRGen.h"
#include "swift/ABI/MetadataValues.h"

#include "BitPatternBuilder.h"
#include "Field.h"
#include "FixedTypeInfo.h"
#include "IRGenFunction.h"
#include "IRGenModule.h"
#include "StructLayout.h"
#include "TypeInfo.h"

using namespace swift;
using namespace irgen;

/// Does this layout kind require a heap header?
static bool requiresHeapHeader(LayoutKind kind) {
  switch (kind) {
  case LayoutKind::NonHeapObject: return false;
  case LayoutKind::HeapObject: return true;
  }
  llvm_unreachable("bad layout kind!");
}

/// Perform structure layout on the given types.
StructLayout::StructLayout(IRGenModule &IGM, std::optional<CanType> type,
                           LayoutKind layoutKind, LayoutStrategy strategy,
                           ArrayRef<const TypeInfo *> types,
                           llvm::StructType *typeToFill) {
  NominalTypeDecl *decl = nullptr;

  if (type) {
    decl = type->getAnyNominal();
  }

  Elements.reserve(types.size());

  // Fill in the Elements array.
  for (auto type : types)
    Elements.push_back(ElementLayout::getIncomplete(*type));

  assert(typeToFill == nullptr || typeToFill->isOpaque());

  StructLayoutBuilder builder(IGM);

  // Add the heap header if necessary.
  if (requiresHeapHeader(layoutKind)) {
    builder.addHeapHeader();
  }

  auto deinit = (decl && decl->getValueTypeDestructor())
    ? IsNotTriviallyDestroyable : IsTriviallyDestroyable;
  auto copyable = (decl && !decl->canBeCopyable())
    ? IsNotCopyable : IsCopyable;

  // Handle a raw layout specification on a struct.
  RawLayoutAttr *rawLayout = nullptr;
  if (decl) {
    rawLayout = decl->getAttrs().getAttribute<RawLayoutAttr>();
  }
  if (rawLayout && type) {
    auto sd = cast<StructDecl>(decl);
    IsKnownTriviallyDestroyable = deinit;
    // Raw layout types are never bitwise-borrowable.
    IsKnownBitwiseTakable = IsBitwiseTakableOnly;
    SpareBits.clear();
    assert(!copyable);
    IsKnownCopyable = copyable;
    assert(builder.getHeaderSize() == Size(0));
    headerSize = Size(0);
    IsLoadable = false;

    auto &Diags = IGM.Context.Diags;
    // Fixed size and alignment specified.
    if (auto sizeAndAlign = rawLayout->getSizeAndAlignment()) {
      auto size = Size(sizeAndAlign->first);
      auto requestedAlignment = Alignment(sizeAndAlign->second);
      MinimumAlign = IGM.getCappedAlignment(requestedAlignment);
      if (requestedAlignment > MinimumAlign) {
        Diags.diagnose(rawLayout->getLocation(),
                       diag::alignment_more_than_maximum,
                       MinimumAlign.getValue());
      }
      
      MinimumSize = size;
      SpareBits.extendWithClearBits(MinimumSize.getValueInBits());
      IsFixedLayout = true;
      IsKnownAlwaysFixedSize = IsFixedSize;
    } else if (auto likeType = rawLayout->getResolvedScalarLikeType(sd)) {
      // If our likeType is dependent, then all calls to try and lay it out will
      // be non-fixed, but in a concrete case we want a fixed layout, so try to
      // substitute it out.
      auto subs = (*type)->getContextSubstitutionMap(IGM.getSwiftModule(), decl);
      auto loweredLikeType = IGM.getLoweredType(likeType->subst(subs));
      const TypeInfo &likeTypeInfo = IGM.getTypeInfo(loweredLikeType);
                                      
      // Take layout attributes from the like type.
      if (const FixedTypeInfo *likeFixedType = dyn_cast<FixedTypeInfo>(&likeTypeInfo)) {
        MinimumSize = likeFixedType->getFixedSize();
        SpareBits.extendWithClearBits(MinimumSize.getValueInBits());
        MinimumAlign = likeFixedType->getFixedAlignment();
        IsFixedLayout = true;
        IsKnownAlwaysFixedSize = IsFixedSize;

        // @_rawLayout(like: T) has an optional `movesAsLike` which enforces that
        // a value of this raw layout type should have the same move semantics
        // as the like its like.
        if (rawLayout->shouldMoveAsLikeType()) {
          IsKnownTriviallyDestroyable = likeFixedType->isTriviallyDestroyable(ResilienceExpansion::Maximal);
          // Raw layout types are still never bitwise-borrowable.
          IsKnownBitwiseTakable = likeFixedType->getBitwiseTakable(ResilienceExpansion::Maximal)
            & IsBitwiseTakableOnly;
        }
      } else {
        MinimumSize = Size(0);
        MinimumAlign = Alignment(1);
        IsFixedLayout = false;
        IsKnownAlwaysFixedSize = IsNotFixedSize;

        // We don't know our like type, so assume we're not known to be bitwise
        // takable.
        if (rawLayout->shouldMoveAsLikeType()) {
          IsKnownTriviallyDestroyable = IsNotTriviallyDestroyable;
          IsKnownBitwiseTakable = IsNotBitwiseTakable;
        }
      }
    } else if (auto likeArray = rawLayout->getResolvedArrayLikeTypeAndCount(sd)) {
      auto elementType = likeArray->first;
      unsigned count = likeArray->second;
      
      auto subs = (*type)->getContextSubstitutionMap(IGM.getSwiftModule(), decl);
      auto loweredElementType = IGM.getLoweredType(elementType.subst(subs));
      const TypeInfo &likeTypeInfo = IGM.getTypeInfo(loweredElementType);
      
      // Take layout attributes from the like type.
      if (const FixedTypeInfo *likeFixedType = dyn_cast<FixedTypeInfo>(&likeTypeInfo)) {
        MinimumSize = likeFixedType->getFixedStride() * count;
        SpareBits.extendWithClearBits(MinimumSize.getValueInBits());
        MinimumAlign = likeFixedType->getFixedAlignment();
        IsFixedLayout = true;
        IsKnownAlwaysFixedSize = IsFixedSize;
      } else {
        MinimumSize = Size(0);
        MinimumAlign = Alignment(1);
        IsFixedLayout = false;
        IsKnownAlwaysFixedSize = IsNotFixedSize;
      }
    } else {
      llvm_unreachable("unhandled raw layout variant?");
    }
    
    // Set the LLVM struct type for a fixed layout according to the stride and
    // alignment we determined.
    if (IsKnownAlwaysFixedSize) {
      auto eltTy = llvm::IntegerType::get(IGM.getLLVMContext(), 8);
      auto bodyTy = llvm::ArrayType::get(eltTy, MinimumSize.getValue());
      if (typeToFill) {
        typeToFill->setBody(bodyTy, /*packed*/ true);
        Ty = typeToFill;
      } else {
        Ty = llvm::StructType::get(IGM.getLLVMContext(), bodyTy, /*packed*/ true);
      }
    } else {
      Ty = (typeToFill ? typeToFill : IGM.OpaqueTy);
    }
  } else {
    bool nonEmpty = builder.addFields(Elements, strategy);

    // Special-case: there's nothing to store.
    // In this case, produce an opaque type;  this tends to cause lovely
    // assertions.
    if (!nonEmpty) {
      assert(!builder.empty() == requiresHeapHeader(layoutKind));
      MinimumAlign = Alignment(1);
      MinimumSize = Size(0);
      headerSize = builder.getHeaderSize();
      SpareBits.clear();
      IsFixedLayout = true;
      IsLoadable = true;
      IsKnownTriviallyDestroyable = deinit & builder.isTriviallyDestroyable();
      IsKnownBitwiseTakable = builder.isBitwiseTakable();
      IsKnownAlwaysFixedSize = builder.isAlwaysFixedSize();
      IsKnownCopyable = copyable & builder.isCopyable();
      Ty = (typeToFill ? typeToFill : IGM.OpaqueTy);
    } else {
      MinimumAlign = builder.getAlignment();
      MinimumSize = builder.getSize();
      headerSize = builder.getHeaderSize();
      SpareBits = builder.getSpareBits();
      IsFixedLayout = builder.isFixedLayout();
      IsLoadable = builder.isLoadable();
      IsKnownTriviallyDestroyable = deinit & builder.isTriviallyDestroyable();
      IsKnownBitwiseTakable = builder.isBitwiseTakable();
      IsKnownAlwaysFixedSize = builder.isAlwaysFixedSize();
      IsKnownCopyable = copyable & builder.isCopyable();
      if (typeToFill) {
        builder.setAsBodyOfStruct(typeToFill);
        Ty = typeToFill;
      } else {
        Ty = builder.getAsAnonStruct();
      }
    }
  }

  assert(typeToFill == nullptr || Ty == typeToFill);

  // If the struct is not @frozen, it will have a dynamic
  // layout outside of its resilience domain.
  if (decl) {
    if (IGM.isResilient(decl, ResilienceExpansion::Minimal))
      IsKnownAlwaysFixedSize = IsNotFixedSize;

    applyLayoutAttributes(IGM, decl, IsFixedLayout, MinimumAlign);
  }
}

void irgen::applyLayoutAttributes(IRGenModule &IGM,
                                  NominalTypeDecl *decl,
                                  bool IsFixedLayout,
                                  Alignment &MinimumAlign) {
  auto &Diags = IGM.Context.Diags;

  if (auto alignment = decl->getAttrs().getAttribute<AlignmentAttr>()) {
    assert(!decl->getAttrs().hasAttribute<RawLayoutAttr>()
           && "_alignment and _rawLayout not supported together");
    auto value = alignment->getValue();
    assert(value != 0 && ((value - 1) & value) == 0
           && "alignment not a power of two!");
    
    if (!IsFixedLayout)
      Diags.diagnose(alignment->getLocation(),
                     diag::alignment_dynamic_type_layout_unsupported);
    else if (value < MinimumAlign.getValue())
      Diags.diagnose(alignment->getLocation(),
                   diag::alignment_less_than_natural, MinimumAlign.getValue());
    else {
      auto requestedAlignment = Alignment(value);
      MinimumAlign = IGM.getCappedAlignment(requestedAlignment);
      if (requestedAlignment > MinimumAlign)
        Diags.diagnose(alignment->getLocation(),
                       diag::alignment_more_than_maximum,
                       MinimumAlign.getValue());
    }
  }
}

llvm::Constant *StructLayout::emitSize(IRGenModule &IGM) const {
  assert(isFixedLayout());
  return IGM.getSize(getSize());
}

llvm::Constant *StructLayout::emitAlignMask(IRGenModule &IGM) const {
  assert(isFixedLayout());
  return IGM.getSize(getAlignment().asSize() - Size(1));
}

/// Bitcast an arbitrary pointer to be a pointer to this type.
Address StructLayout::emitCastTo(IRGenFunction &IGF,
                                 llvm::Value *ptr,
                                 const llvm::Twine &name) const {
  llvm::Value *addr =
    IGF.Builder.CreateBitCast(ptr, getType()->getPointerTo(), name);
  return Address(addr, getType(), getAlignment());
}

Address ElementLayout::project(IRGenFunction &IGF, Address baseAddr,
                               NonFixedOffsets offsets,
                               const llvm::Twine &suffix) const {
  switch (getKind()) {
  case Kind::Empty:
  case Kind::EmptyTailAllocatedCType:
    return getType().getUndefAddress();

  case Kind::Fixed:
    return IGF.Builder.CreateStructGEP(baseAddr,
                                       getStructIndex(),
                                       getByteOffset(),
                                 baseAddr.getAddress()->getName() + suffix);

  case Kind::NonFixed: {
    assert(offsets.has_value());
    llvm::Value *offset =
      offsets.value()->getOffsetForIndex(IGF, getNonFixedElementIndex());
    return IGF.emitByteOffsetGEP(baseAddr.getAddress(), offset, getType(),
                                 baseAddr.getAddress()->getName() + suffix);
  }

  case Kind::InitialNonFixedSize:
    return IGF.Builder.CreateElementBitCast(
        baseAddr, getType().getStorageType(),
        baseAddr.getAddress()->getName() + suffix);
  }
  llvm_unreachable("bad element layout kind");
}

void StructLayoutBuilder::addHeapHeader() {
  assert(StructFields.empty() && "adding heap header at a non-zero offset");
  CurSize = IGM.RefCountedStructSize;
  CurAlignment = IGM.getPointerAlignment();
  StructFields.push_back(IGM.RefCountedStructTy);
  headerSize = CurSize;
}

void StructLayoutBuilder::addNSObjectHeader() {
  assert(StructFields.empty() && "adding heap header at a non-zero offset");
  CurSize = IGM.getPointerSize();
  CurAlignment = IGM.getPointerAlignment();
  StructFields.push_back(IGM.ObjCClassPtrTy);
  headerSize = CurSize;
}

void StructLayoutBuilder::addDefaultActorHeader(ElementLayout &elt) {
  assert(StructFields.size() == 1 &&
         StructFields[0] == IGM.RefCountedStructTy &&
         "adding default actor header at wrong offset");

  // These must match the DefaultActor class in Actor.h.
  auto size = NumWords_DefaultActor * IGM.getPointerSize();
  auto align = Alignment(Alignment_DefaultActor);
  auto ty = llvm::ArrayType::get(IGM.Int8PtrTy, NumWords_DefaultActor);

  // Note that we align the *entire structure* to the new alignment,
  // not the storage we're adding.  Otherwise we would potentially
  // get internal padding.
  assert(CurSize.isMultipleOf(IGM.getPointerSize()));
  assert(align >= CurAlignment);
  assert(CurSize == getDefaultActorStorageFieldOffset(IGM));
  elt.completeFixed(IsNotTriviallyDestroyable, CurSize, /*struct index*/ 1);
  CurSize += size;
  CurAlignment = align;
  StructFields.push_back(ty);
  headerSize = CurSize;
}

void StructLayoutBuilder::addNonDefaultDistributedActorHeader(ElementLayout &elt) {
  assert(StructFields.size() == 1 &&
         StructFields[0] == IGM.RefCountedStructTy &&
         "adding default actor header at wrong offset");

  // These must match the NonDefaultDistributedActor class in Actor.h.
  auto size = NumWords_NonDefaultDistributedActor * IGM.getPointerSize();
  auto align = Alignment(Alignment_NonDefaultDistributedActor);
  auto ty = llvm::ArrayType::get(IGM.Int8PtrTy, NumWords_NonDefaultDistributedActor);

  // Note that we align the *entire structure* to the new alignment,
  // not the storage we're adding.  Otherwise we would potentially
  // get internal padding.
  assert(CurSize.isMultipleOf(IGM.getPointerSize()));
  assert(align >= CurAlignment);
  assert(CurSize == getNonDefaultDistributedActorStorageFieldOffset(IGM));
  elt.completeFixed(IsNotTriviallyDestroyable, CurSize, /*struct index*/ 1);
  CurSize += size;
  CurAlignment = align;
  StructFields.push_back(ty);
  headerSize = CurSize;
}

Size irgen::getDefaultActorStorageFieldOffset(IRGenModule &IGM) {
  return IGM.RefCountedStructSize;
}

Size irgen::getNonDefaultDistributedActorStorageFieldOffset(IRGenModule &IGM) {
  return IGM.RefCountedStructSize;
}

bool StructLayoutBuilder::addFields(llvm::MutableArrayRef<ElementLayout> elts,
                                    LayoutStrategy strategy) {
  // Track whether we've added any storage to our layout.
  bool addedStorage = false;

  // Loop through the elements.  The only valid field in each element
  // is Type; StructIndex and ByteOffset need to be laid out.
  for (auto &elt : elts) {
    addedStorage |= addField(elt, strategy);
  }

  return addedStorage;
}

bool StructLayoutBuilder::addField(ElementLayout &elt,
                                  LayoutStrategy strategy) {
  auto &eltTI = elt.getType();
  IsKnownTriviallyDestroyable &= eltTI.isTriviallyDestroyable(ResilienceExpansion::Maximal);
  IsKnownBitwiseTakable &= eltTI.getBitwiseTakable(ResilienceExpansion::Maximal);
  IsKnownAlwaysFixedSize &= eltTI.isFixedSize(ResilienceExpansion::Minimal);
  IsLoadable &= eltTI.isLoadable();
  IsKnownCopyable &= eltTI.isCopyable(ResilienceExpansion::Maximal);

  if (eltTI.isKnownEmpty(ResilienceExpansion::Maximal)) {
    addEmptyElement(elt);
    // If the element type is empty, it adds nothing.
    ++NextNonFixedOffsetIndex;
    return false;
  }
  // TODO: consider using different layout rules.
  // If the rules are changed so that fields aren't necessarily laid
  // out sequentially, the computation of InstanceStart in the
  // RO-data will need to be fixed.

  // If this element is resiliently- or dependently-sized, record
  // that and configure the ElementLayout appropriately.
  if (isa<FixedTypeInfo>(eltTI)) {
    addFixedSizeElement(elt);
  } else {
    addNonFixedSizeElement(elt);
  }
  ++NextNonFixedOffsetIndex;
  return true;
}

void StructLayoutBuilder::addFixedSizeElement(ElementLayout &elt) {
  auto &eltTI = cast<FixedTypeInfo>(elt.getType());

  // Note that, even in the presence of elements with non-fixed
  // size, we continue to compute the minimum size and alignment
  // requirements of the overall aggregate as if all the
  // non-fixed-size elements were empty.  This gives us minimum
  // bounds on the size and alignment of the aggregate.

  // The struct alignment is the max of the alignment of the fields.
  CurAlignment = std::max(CurAlignment, eltTI.getFixedAlignment());

  // If the current tuple size isn't a multiple of the field's
  // required alignment, we need to pad out.
  Alignment eltAlignment = eltTI.getFixedAlignment();
  if (Size offsetFromAlignment = CurSize % eltAlignment) {
    unsigned paddingRequired
      = eltAlignment.getValue() - offsetFromAlignment.getValue();
    assert(paddingRequired != 0);

    // Regardless, the storage size goes up.
    CurSize += Size(paddingRequired);

    // Add the padding to the fixed layout.
    if (isFixedLayout()) {
      auto paddingTy = llvm::ArrayType::get(IGM.Int8Ty, paddingRequired);
      StructFields.push_back(paddingTy);

      // The padding can be used as spare bits by enum layout.
      auto numBits = Size(paddingRequired).getValueInBits();
      auto mask = llvm::APInt::getAllOnes(numBits);
      CurSpareBits.push_back(SpareBitVector::fromAPInt(mask));
    }
  }

  // If the overall structure so far has a fixed layout, then add
  // this as a field to the layout.
  if (isFixedLayout()) {
    addElementAtFixedOffset(elt);
  // Otherwise, just remember the next non-fixed offset index.
  } else {
    addElementAtNonFixedOffset(elt);
  }
  CurSize += eltTI.getFixedSize();
}

void StructLayoutBuilder::addNonFixedSizeElement(ElementLayout &elt) {
  // If the element is the first non-empty element to be added to the
  // structure, we can assign it a fixed offset (namely zero) despite
  // it not having a fixed size/alignment.
  if (isFixedLayout() && CurSize.isZero()) {
    addNonFixedSizeElementAtOffsetZero(elt);
    IsFixedLayout = false;
    return;
  }

  // Otherwise, we cannot give it a fixed offset, even if all the
  // previous elements are non-fixed.  The problem is not that it has
  // an unknown *size*; it's that it has an unknown *alignment*, which
  // might force us to introduce padding.  Absent some sort of user
  // "max alignment" annotation (or having reached the platform
  // maximum alignment, if there is one), these are part and parcel.
  IsFixedLayout = false;
  addElementAtNonFixedOffset(elt);

  assert(!IsKnownAlwaysFixedSize);
}

/// Add an empty element to the aggregate.
void StructLayoutBuilder::addEmptyElement(ElementLayout &elt) {
  auto byteOffset = isFixedLayout() ? CurSize : Size(0);
  elt.completeEmpty(elt.getType().isTriviallyDestroyable(ResilienceExpansion::Maximal), byteOffset);
}

/// Add an element at the fixed offset of the current end of the
/// aggregate.
void StructLayoutBuilder::addElementAtFixedOffset(ElementLayout &elt) {
  assert(isFixedLayout());
  auto &eltTI = cast<FixedTypeInfo>(elt.getType());

  elt.completeFixed(elt.getType().isTriviallyDestroyable(ResilienceExpansion::Maximal),
                    CurSize, StructFields.size());
  StructFields.push_back(elt.getType().getStorageType());
  
  // Carry over the spare bits from the element.
  CurSpareBits.push_back(eltTI.getSpareBits());
}

/// Add an element at a non-fixed offset to the aggregate.
void StructLayoutBuilder::addElementAtNonFixedOffset(ElementLayout &elt) {
  assert(!isFixedLayout());
  elt.completeNonFixed(elt.getType().isTriviallyDestroyable(ResilienceExpansion::Maximal),
                       NextNonFixedOffsetIndex);
  CurSpareBits = SmallVector<SpareBitVector, 8>(); // clear spare bits
}

/// Add a non-fixed-size element to the aggregate at offset zero.
void StructLayoutBuilder::addNonFixedSizeElementAtOffsetZero(ElementLayout &elt) {
  assert(isFixedLayout());
  assert(!isa<FixedTypeInfo>(elt.getType()));
  assert(CurSize.isZero());
  elt.completeInitialNonFixedSize(elt.getType().isTriviallyDestroyable(ResilienceExpansion::Maximal));
  CurSpareBits = SmallVector<SpareBitVector, 8>(); // clear spare bits
}

/// Produce the current fields as an anonymous structure.
llvm::StructType *StructLayoutBuilder::getAsAnonStruct() const {
  auto ty = llvm::StructType::get(IGM.getLLVMContext(), StructFields,
                                  /*isPacked*/ true);
  assert((!isFixedLayout()
          || IGM.DataLayout.getStructLayout(ty)->getSizeInBytes()
            == CurSize.getValue())
         && "LLVM size of fixed struct type does not match StructLayout size");
  return ty;
}

/// Set the current fields as the body of the given struct type.
void StructLayoutBuilder::setAsBodyOfStruct(llvm::StructType *type) const {
  assert(type->isOpaque());
  type->setBody(StructFields, /*isPacked*/ true);
  assert((!isFixedLayout()
          || IGM.DataLayout.getStructLayout(type)->getSizeInBytes()
            == CurSize.getValue())
         && "LLVM size of fixed struct type does not match StructLayout size");
}

/// Return the spare bit mask of the structure built so far.
SpareBitVector StructLayoutBuilder::getSpareBits() const {
  auto spareBits = BitPatternBuilder(IGM.Triple.isLittleEndian());
  for (const auto &v : CurSpareBits) {
    spareBits.append(v);
  }
  return spareBits.build();
}

unsigned irgen::getNumFields(const NominalTypeDecl *target) {
  auto numFields =
    target->getStoredPropertiesAndMissingMemberPlaceholders().size();
  if (auto cls = dyn_cast<ClassDecl>(target)) {
    if (cls->isRootDefaultActor()) {
      numFields++;
    } else if (cls->isNonDefaultExplicitDistributedActor()) {
      numFields++;
    }
  }
  return numFields;
}

void irgen::forEachField(IRGenModule &IGM, const NominalTypeDecl *typeDecl,
                         llvm::function_ref<void(Field field)> fn) {
  auto classDecl = dyn_cast<ClassDecl>(typeDecl);
  if (classDecl) {
    if (classDecl->isRootDefaultActor()) {
      fn(Field::DefaultActorStorage);
    } else if (classDecl->isNonDefaultExplicitDistributedActor()) {
      fn(Field::NonDefaultDistributedActorStorage);
    }
  }

  for (auto decl :
         typeDecl->getStoredPropertiesAndMissingMemberPlaceholders()) {
    if (auto var = dyn_cast<VarDecl>(decl)) {
      fn(var);
    } else {
      fn(cast<MissingMemberDecl>(decl));
    }
  }
}

SILType Field::getType(IRGenModule &IGM, SILType baseType) const {
  switch (getKind()) {
  case Field::Var:
    return baseType.getFieldType(getVarDecl(), IGM.getSILModule(),
                                 TypeExpansionContext::minimal());
  case Field::MissingMember:
    llvm_unreachable("cannot ask for type of missing member");
  case Field::DefaultActorStorage:
    return SILType::getPrimitiveObjectType(
                             IGM.Context.TheDefaultActorStorageType);
  case Field::NonDefaultDistributedActorStorage:
    return SILType::getPrimitiveObjectType(
                             IGM.Context.TheNonDefaultDistributedActorStorageType);
  }
  llvm_unreachable("bad field kind");
}

Type Field::getInterfaceType(IRGenModule &IGM) const {
  switch (getKind()) {
  case Field::Var:
    return getVarDecl()->getInterfaceType();
  case Field::MissingMember:
    llvm_unreachable("cannot ask for type of missing member");
  case Field::DefaultActorStorage:
    return IGM.Context.TheDefaultActorStorageType;
  case Field::NonDefaultDistributedActorStorage:
    return IGM.Context.TheNonDefaultDistributedActorStorageType;
  }
  llvm_unreachable("bad field kind");
}

StringRef Field::getName() const {
  switch (getKind()) {
  case Field::Var:
    return getVarDecl()->getName().str();
  case Field::MissingMember:
    llvm_unreachable("cannot ask for type of missing member");
  case Field::DefaultActorStorage:
    return DEFAULT_ACTOR_STORAGE_FIELD_NAME;
  case Field::NonDefaultDistributedActorStorage:
    return NON_DEFAULT_DISTRIBUTED_ACTOR_STORAGE_FIELD_NAME;
  }
  llvm_unreachable("bad field kind");
}