-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathLifetimeDependence.cpp
732 lines (669 loc) · 25.9 KB
/
LifetimeDependence.cpp
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
//===--- LifetimeDependence.cpp -----------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 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
//
//===----------------------------------------------------------------------===//
#include "swift/AST/LifetimeDependence.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ConformanceLookup.h"
#include "swift/AST/Decl.h"
#include "swift/AST/DiagnosticsSema.h"
#include "swift/AST/Module.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/Type.h"
#include "swift/AST/TypeRepr.h"
#include "swift/Basic/Assertions.h"
#include "swift/Basic/Defer.h"
#include "swift/Basic/Range.h"
namespace swift {
LifetimeEntry *
LifetimeEntry::create(const ASTContext &ctx, SourceLoc startLoc,
SourceLoc endLoc, ArrayRef<LifetimeDescriptor> sources,
std::optional<LifetimeDescriptor> targetDescriptor) {
void *mem = ctx.Allocate(sizeof(LifetimeEntry), alignof(LifetimeEntry));
return new (mem) LifetimeEntry(startLoc, endLoc, sources, targetDescriptor);
}
std::optional<LifetimeDependenceInfo>
getLifetimeDependenceFor(ArrayRef<LifetimeDependenceInfo> lifetimeDependencies,
unsigned index) {
for (auto dep : lifetimeDependencies) {
if (dep.getTargetIndex() == index) {
return dep;
}
}
return std::nullopt;
}
std::string LifetimeDependenceInfo::getString() const {
std::string lifetimeDependenceString;
auto getOnIndices = [](IndexSubset *bitvector) {
std::string result;
bool isFirstSetBit = true;
for (unsigned i = 0; i < bitvector->getCapacity(); i++) {
if (bitvector->contains(i)) {
if (!isFirstSetBit) {
result += ", ";
}
result += std::to_string(i);
isFirstSetBit = false;
}
}
return result;
};
if (inheritLifetimeParamIndices && !inheritLifetimeParamIndices->isEmpty()) {
lifetimeDependenceString =
"_inherit(" + getOnIndices(inheritLifetimeParamIndices) + ") ";
}
if (scopeLifetimeParamIndices && !scopeLifetimeParamIndices->isEmpty()) {
lifetimeDependenceString +=
"_scope(" + getOnIndices(scopeLifetimeParamIndices) + ") ";
}
return lifetimeDependenceString;
}
void LifetimeDependenceInfo::Profile(llvm::FoldingSetNodeID &ID) const {
if (inheritLifetimeParamIndices) {
ID.AddInteger((uint8_t)LifetimeDependenceKind::Inherit);
inheritLifetimeParamIndices->Profile(ID);
}
if (scopeLifetimeParamIndices) {
ID.AddInteger((uint8_t)LifetimeDependenceKind::Scope);
scopeLifetimeParamIndices->Profile(ID);
}
}
static LifetimeDependenceKind
getLifetimeDependenceKindFromType(Type sourceType) {
if (sourceType->isEscapable()) {
return LifetimeDependenceKind::Scope;
}
return LifetimeDependenceKind::Inherit;
}
// Warning: this is incorrect for Setter 'newValue' parameters. It should only
// be called for a Setter's 'self'.
static ValueOwnership getLoweredOwnership(AbstractFunctionDecl *afd) {
if (isa<ConstructorDecl>(afd)) {
return ValueOwnership::Owned;
}
if (auto *ad = dyn_cast<AccessorDecl>(afd)) {
if (ad->getAccessorKind() == AccessorKind::Set ||
isYieldingDefaultMutatingAccessor(ad->getAccessorKind())) {
return ValueOwnership::InOut;
}
}
return ValueOwnership::Shared;
}
static bool isBitwiseCopyable(Type type, ASTContext &ctx) {
auto *bitwiseCopyableProtocol =
ctx.getProtocol(KnownProtocolKind::BitwiseCopyable);
if (!bitwiseCopyableProtocol) {
return false;
}
return (bool)checkConformance(type, bitwiseCopyableProtocol);
}
static bool
isLifetimeDependenceCompatibleWithOwnership(LifetimeDependenceKind kind,
Type type, ValueOwnership ownership,
AbstractFunctionDecl *afd) {
auto &ctx = afd->getASTContext();
if (kind == LifetimeDependenceKind::Inherit) {
return true;
}
// Lifetime dependence always propagates through temporary BitwiseCopyable
// values, even if the dependence is scoped.
if (isBitwiseCopyable(type, ctx)) {
return true;
}
assert(kind == LifetimeDependenceKind::Scope);
auto loweredOwnership = ownership != ValueOwnership::Default
? ownership
: getLoweredOwnership(afd);
if (loweredOwnership == ValueOwnership::InOut ||
loweredOwnership == ValueOwnership::Shared) {
return true;
}
assert(loweredOwnership == ValueOwnership::Owned);
return false;
}
LifetimeDependenceInfo
LifetimeDependenceInfo::getForIndex(AbstractFunctionDecl *afd,
unsigned targetIndex, unsigned sourceIndex,
LifetimeDependenceKind kind) {
auto *dc = afd->getDeclContext();
auto &ctx = dc->getASTContext();
unsigned capacity = afd->hasImplicitSelfDecl()
? (afd->getParameters()->size() + 1)
: afd->getParameters()->size();
auto indexSubset = IndexSubset::get(ctx, capacity, {sourceIndex});
if (kind == LifetimeDependenceKind::Scope) {
return LifetimeDependenceInfo{/*inheritLifetimeParamIndices*/ nullptr,
/*scopeLifetimeParamIndices*/ indexSubset,
targetIndex,
/*isImmortal*/ false};
}
return LifetimeDependenceInfo{/*inheritLifetimeParamIndices*/ indexSubset,
/*scopeLifetimeParamIndices*/ nullptr,
targetIndex,
/*isImmortal*/ false};
}
void LifetimeDependenceInfo::getConcatenatedData(
SmallVectorImpl<bool> &concatenatedData) const {
auto pushData = [&](IndexSubset *paramIndices) {
if (paramIndices == nullptr) {
return;
}
assert(!paramIndices->isEmpty());
for (unsigned i = 0; i < paramIndices->getCapacity(); i++) {
if (paramIndices->contains(i)) {
concatenatedData.push_back(true);
continue;
}
concatenatedData.push_back(false);
}
};
if (hasInheritLifetimeParamIndices()) {
pushData(inheritLifetimeParamIndices);
}
if (hasScopeLifetimeParamIndices()) {
pushData(scopeLifetimeParamIndices);
}
}
static Type getResultOrYield(AbstractFunctionDecl *afd) {
if (auto *accessor = dyn_cast<AccessorDecl>(afd)) {
if (accessor->isCoroutine()) {
auto yieldTyInContext = accessor->mapTypeIntoContext(
accessor->getStorage()->getValueInterfaceType());
return yieldTyInContext;
}
}
Type resultType;
if (auto fn = dyn_cast<FuncDecl>(afd)) {
resultType = fn->getResultInterfaceType();
} else {
auto ctor = cast<ConstructorDecl>(afd);
resultType = ctor->getResultInterfaceType();
}
return afd->mapTypeIntoContext(resultType);
}
static bool hasEscapableResultOrYield(AbstractFunctionDecl *afd) {
return getResultOrYield(afd)->isEscapable();
}
static std::optional<LifetimeDependenceKind>
getLifetimeDependenceKind(LifetimeDescriptor descriptor,
AbstractFunctionDecl *afd, ParamDecl *decl) {
auto &ctx = afd->getASTContext();
auto &diags = ctx.Diags;
auto loc = descriptor.getLoc();
auto ownership = decl->getValueOwnership();
auto type = decl->getTypeInContext();
// For @lifetime attribute, we check if we had a "borrow" modifier, if not
// we infer inherit dependence.
auto parsedLifetimeKind = descriptor.getParsedLifetimeDependenceKind();
if (parsedLifetimeKind == ParsedLifetimeDependenceKind::Scope) {
bool isCompatible = isLifetimeDependenceCompatibleWithOwnership(
LifetimeDependenceKind::Scope, type, ownership, afd);
if (!isCompatible) {
diags.diagnose(
loc, diag::lifetime_dependence_cannot_use_parsed_borrow_consuming);
return std::nullopt;
}
return LifetimeDependenceKind::Scope;
}
if (type->isEscapable()) {
diags.diagnose(loc,
diag::lifetime_dependence_invalid_inherit_escapable_type);
return std::nullopt;
}
return LifetimeDependenceKind::Inherit;
}
// Finds the ParamDecl* and its index from a LifetimeDescriptor
static std::optional<std::pair<ParamDecl *, unsigned>>
getParamDeclFromDescriptor(AbstractFunctionDecl *afd,
LifetimeDescriptor descriptor) {
auto *dc = afd->getDeclContext();
auto &ctx = dc->getASTContext();
auto &diags = ctx.Diags;
switch (descriptor.getDescriptorKind()) {
case LifetimeDescriptor::DescriptorKind::Named: {
unsigned paramIndex = 0;
ParamDecl *candidateParam = nullptr;
for (auto *param : *afd->getParameters()) {
if (param->getParameterName().str() == descriptor.getName()) {
candidateParam = param;
break;
}
paramIndex++;
}
if (!candidateParam) {
diags.diagnose(descriptor.getLoc(),
diag::lifetime_dependence_invalid_param_name,
descriptor.getName());
return std::nullopt;
}
return std::make_pair(candidateParam, paramIndex);
}
case LifetimeDescriptor::DescriptorKind::Ordered: {
auto paramIndex = descriptor.getIndex();
if (paramIndex >= afd->getParameters()->size()) {
diags.diagnose(descriptor.getLoc(),
diag::lifetime_dependence_invalid_param_index, paramIndex);
return std::nullopt;
}
auto candidateParam = afd->getParameters()->get(paramIndex);
return std::make_pair(candidateParam, paramIndex);
}
case LifetimeDescriptor::DescriptorKind::Self: {
if (!afd->hasImplicitSelfDecl()) {
diags.diagnose(descriptor.getLoc(),
diag::lifetime_dependence_invalid_self_in_static);
return std::nullopt;
}
if (isa<ConstructorDecl>(afd)) {
diags.diagnose(descriptor.getLoc(),
diag::lifetime_dependence_invalid_self_in_init);
return std::nullopt;
}
auto *selfDecl = afd->getImplicitSelfDecl();
return std::make_pair(selfDecl, afd->getParameters()->size());
}
}
}
static std::optional<LifetimeDependenceInfo>
populateLifetimeDependence(AbstractFunctionDecl *afd, LifetimeEntry *entry) {
auto *dc = afd->getDeclContext();
auto &ctx = dc->getASTContext();
auto &diags = ctx.Diags;
auto capacity = afd->hasImplicitSelfDecl()
? (afd->getParameters()->size() + 1)
: afd->getParameters()->size();
SmallBitVector inheritIndices(capacity);
SmallBitVector scopeIndices(capacity);
auto updateLifetimeIndices = [&](LifetimeDescriptor descriptor,
unsigned paramIndexToSet,
LifetimeDependenceKind lifetimeKind) {
if (inheritIndices.test(paramIndexToSet) ||
scopeIndices.test(paramIndexToSet)) {
diags.diagnose(descriptor.getLoc(),
diag::lifetime_dependence_duplicate_param_id);
return true;
}
if (lifetimeKind == LifetimeDependenceKind::Inherit) {
inheritIndices.set(paramIndexToSet);
} else {
assert(lifetimeKind == LifetimeDependenceKind::Scope);
scopeIndices.set(paramIndexToSet);
}
return false;
};
auto targetDescriptor = entry->getTargetDescriptor();
unsigned targetIndex;
if (targetDescriptor.has_value()) {
auto targetDeclAndIndex =
getParamDeclFromDescriptor(afd, *targetDescriptor);
if (!targetDeclAndIndex.has_value()) {
return std::nullopt;
}
targetIndex = targetDeclAndIndex->second;
} else {
targetIndex = afd->hasImplicitSelfDecl() ? afd->getParameters()->size() + 1
: afd->getParameters()->size();
}
for (auto source : entry->getSources()) {
if (source.isImmortal()) {
auto immortalParam =
std::find_if(afd->getParameters()->begin(),
afd->getParameters()->end(), [](ParamDecl *param) {
return strcmp(param->getName().get(), "immortal") == 0;
});
if (immortalParam != afd->getParameters()->end()) {
diags.diagnose(*immortalParam,
diag::lifetime_dependence_immortal_conflict_name);
return std::nullopt;
}
return LifetimeDependenceInfo(nullptr, nullptr, targetIndex,
/*isImmortal*/ true);
}
auto paramDeclAndIndex = getParamDeclFromDescriptor(afd, source);
if (!paramDeclAndIndex.has_value()) {
return std::nullopt;
}
auto lifetimeKind =
getLifetimeDependenceKind(source, afd, paramDeclAndIndex->first);
if (!lifetimeKind.has_value()) {
return std::nullopt;
}
bool hasError =
updateLifetimeIndices(source, paramDeclAndIndex->second, *lifetimeKind);
if (hasError) {
return std::nullopt;
}
}
return LifetimeDependenceInfo(
inheritIndices.any() ? IndexSubset::get(ctx, inheritIndices) : nullptr,
scopeIndices.any() ? IndexSubset::get(ctx, scopeIndices) : nullptr,
targetIndex, /*isImmortal*/ false);
}
std::optional<ArrayRef<LifetimeDependenceInfo>>
LifetimeDependenceInfo::fromLifetimeAttribute(AbstractFunctionDecl *afd) {
auto *dc = afd->getDeclContext();
auto &ctx = dc->getASTContext();
auto &diags = ctx.Diags;
SmallVector<LifetimeDependenceInfo, 1> lifetimeDependencies;
llvm::SmallSet<unsigned, 1> lifetimeDependentTargets;
auto lifetimeAttrs = afd->getAttrs().getAttributes<LifetimeAttr>();
for (auto attr : lifetimeAttrs) {
auto lifetimeDependenceInfo =
populateLifetimeDependence(afd, attr->getLifetimeEntry());
if (!lifetimeDependenceInfo.has_value()) {
return std::nullopt;
}
auto targetIndex = lifetimeDependenceInfo->getTargetIndex();
if (lifetimeDependentTargets.contains(targetIndex)) {
// TODO: Diagnose at the source location of the @lifetime attribute with
// duplicate target.
diags.diagnose(afd->getLoc(), diag::lifetime_dependence_duplicate_target);
}
lifetimeDependentTargets.insert(targetIndex);
lifetimeDependencies.push_back(*lifetimeDependenceInfo);
}
return afd->getASTContext().AllocateCopy(lifetimeDependencies);
}
// This utility is similar to its overloaded version that builds the
// LifetimeDependenceInfo from the swift decl. Reason for duplicated code is
// the apis on type and ownership is different in SIL compared to Sema.
std::optional<LifetimeDependenceInfo> LifetimeDependenceInfo::fromDependsOn(
LifetimeDependentTypeRepr *lifetimeDependentRepr, unsigned targetIndex,
ArrayRef<SILParameterInfo> params, DeclContext *dc) {
auto &ctx = dc->getASTContext();
auto &diags = ctx.Diags;
auto capacity = params.size(); // SIL parameters include self
SmallBitVector inheritLifetimeParamIndices(capacity);
SmallBitVector scopeLifetimeParamIndices(capacity);
auto updateLifetimeDependenceInfo = [&](LifetimeDescriptor descriptor,
unsigned paramIndexToSet,
ParameterConvention paramConvention) {
auto loc = descriptor.getLoc();
auto kind = descriptor.getParsedLifetimeDependenceKind();
if (kind == ParsedLifetimeDependenceKind::Scope &&
(!isGuaranteedParameterInCallee(paramConvention) &&
!isMutatingParameter(paramConvention))) {
diags.diagnose(loc, diag::lifetime_dependence_cannot_use_kind, "_scope",
getStringForParameterConvention(paramConvention));
return true;
}
if (inheritLifetimeParamIndices.test(paramIndexToSet) ||
scopeLifetimeParamIndices.test(paramIndexToSet)) {
diags.diagnose(loc, diag::lifetime_dependence_duplicate_param_id);
return true;
}
if (kind == ParsedLifetimeDependenceKind::Inherit) {
inheritLifetimeParamIndices.set(paramIndexToSet);
} else {
assert(kind == ParsedLifetimeDependenceKind::Scope);
scopeLifetimeParamIndices.set(paramIndexToSet);
}
return false;
};
for (auto source : lifetimeDependentRepr->getLifetimeEntry()->getSources()) {
switch (source.getDescriptorKind()) {
case LifetimeDescriptor::DescriptorKind::Ordered: {
auto index = source.getIndex();
if (index > capacity) {
diags.diagnose(source.getLoc(),
diag::lifetime_dependence_invalid_param_index, index);
return std::nullopt;
}
auto param = params[index];
auto paramConvention = param.getConvention();
if (updateLifetimeDependenceInfo(source, index, paramConvention)) {
return std::nullopt;
}
break;
}
case LifetimeDescriptor::DescriptorKind::Named: {
assert(source.isImmortal());
return LifetimeDependenceInfo(/*inheritLifetimeParamIndices*/ nullptr,
/*scopeLifetimeParamIndices*/ nullptr,
targetIndex,
/*isImmortal*/ true);
}
default:
llvm_unreachable("SIL can only have ordered or immortal lifetime "
"dependence specifier kind");
}
}
return LifetimeDependenceInfo(
inheritLifetimeParamIndices.any()
? IndexSubset::get(ctx, inheritLifetimeParamIndices)
: nullptr,
scopeLifetimeParamIndices.any()
? IndexSubset::get(ctx, scopeLifetimeParamIndices)
: nullptr,
targetIndex,
/*isImmortal*/ false);
}
std::optional<LifetimeDependenceInfo>
LifetimeDependenceInfo::infer(AbstractFunctionDecl *afd) {
auto *dc = afd->getDeclContext();
auto &ctx = dc->getASTContext();
// Disable inference if requested.
if (!ctx.LangOpts.EnableExperimentalLifetimeDependenceInference) {
return std::nullopt;
}
if (getResultOrYield(afd)->hasError()) {
return std::nullopt;
}
// Setters infer 'self' dependence on 'newValue'.
if (auto accessor = dyn_cast<AccessorDecl>(afd)) {
if (accessor->getAccessorKind() == AccessorKind::Set) {
return inferSetter(accessor);
}
} else if (auto *fd = dyn_cast<FuncDecl>(afd)) {
// Infer self dependence for a mutating function with no result.
//
// FIXME: temporary hack until we have dependsOn(self: param) syntax.
// Do not apply this to accessors (_modify). _modify is handled below like
// a mutating method.
if (fd->isMutating() && fd->getResultInterfaceType()->isVoid() &&
!dc->getSelfTypeInContext()->isEscapable()) {
return inferMutatingSelf(afd);
}
}
if (hasEscapableResultOrYield(afd)) {
return std::nullopt;
}
if (afd->getAttrs().hasAttribute<UnsafeNonEscapableResultAttr>()) {
return std::nullopt;
}
auto &diags = ctx.Diags;
auto returnTypeRepr = afd->getResultTypeRepr();
auto returnLoc = returnTypeRepr ? returnTypeRepr->getLoc() : afd->getLoc();
unsigned resultIndex = afd->hasImplicitSelfDecl()
? afd->getParameters()->size() + 1
: afd->getParameters()->size();
auto *cd = dyn_cast<ConstructorDecl>(afd);
if (cd && cd->isImplicit()) {
if (cd->getParameters()->size() == 0) {
return std::nullopt;
}
}
if (!cd && afd->hasImplicitSelfDecl()) {
Type selfTypeInContext = dc->getSelfTypeInContext();
if (selfTypeInContext->isEscapable()) {
if (isBitwiseCopyable(selfTypeInContext, ctx)) {
diags.diagnose(
returnLoc,
diag::lifetime_dependence_method_escapable_bitwisecopyable_self);
return std::nullopt;
}
}
auto kind = getLifetimeDependenceKindFromType(selfTypeInContext);
auto selfOwnership = afd->getImplicitSelfDecl()->getValueOwnership();
if (!isLifetimeDependenceCompatibleWithOwnership(kind, selfTypeInContext,
selfOwnership, afd)) {
diags.diagnose(returnLoc,
diag::lifetime_dependence_invalid_self_ownership);
return std::nullopt;
}
// Infer method dependence: result depends on self.
//
// This includes _modify. A _modify's yielded value depends on self. The
// caller of the _modify ensures that the 'self' depends on any value stored
// to the yielded address.
return LifetimeDependenceInfo::getForIndex(
afd, resultIndex, /*selfIndex */ afd->getParameters()->size(), kind);
}
std::optional<unsigned> candidateParamIndex;
std::optional<LifetimeDependenceKind> candidateLifetimeKind;
unsigned paramIndex = 0;
bool hasParamError = false;
for (auto *param : *afd->getParameters()) {
SWIFT_DEFER { paramIndex++; };
Type paramTypeInContext =
afd->mapTypeIntoContext(param->getInterfaceType());
if (paramTypeInContext->hasError()) {
hasParamError = true;
continue;
}
auto paramOwnership = param->getValueOwnership();
if (paramTypeInContext->isEscapable()) {
if (isBitwiseCopyable(paramTypeInContext, ctx)) {
continue;
}
if (paramOwnership == ValueOwnership::Default) {
continue;
}
}
candidateLifetimeKind =
getLifetimeDependenceKindFromType(paramTypeInContext);
if (!isLifetimeDependenceCompatibleWithOwnership(
*candidateLifetimeKind, paramTypeInContext, paramOwnership, afd)) {
continue;
}
if (candidateParamIndex) {
if (cd && afd->isImplicit()) {
diags.diagnose(
returnLoc,
diag::lifetime_dependence_cannot_infer_ambiguous_candidate,
"on implicit initializer");
return std::nullopt;
}
diags.diagnose(returnLoc,
diag::lifetime_dependence_cannot_infer_ambiguous_candidate,
"");
return std::nullopt;
}
candidateParamIndex = paramIndex;
}
if (!candidateParamIndex && !hasParamError) {
if (cd && afd->isImplicit()) {
diags.diagnose(returnLoc,
diag::lifetime_dependence_cannot_infer_no_candidates,
" on implicit initializer");
return std::nullopt;
}
diags.diagnose(returnLoc,
diag::lifetime_dependence_cannot_infer_no_candidates, "");
return std::nullopt;
}
return LifetimeDependenceInfo::getForIndex(
afd, resultIndex, *candidateParamIndex, *candidateLifetimeKind);
}
/// Infer LifetimeDependence on a setter where 'self' is nonescapable.
std::optional<LifetimeDependenceInfo> LifetimeDependenceInfo::inferSetter(
AbstractFunctionDecl *afd) {
auto *param = afd->getParameters()->get(0);
Type paramTypeInContext =
afd->mapTypeIntoContext(param->getInterfaceType());
if (paramTypeInContext->hasError()) {
return std::nullopt;
}
if (paramTypeInContext->isEscapable()) {
return std::nullopt;
}
auto kind = getLifetimeDependenceKindFromType(paramTypeInContext);
return LifetimeDependenceInfo::getForIndex(
afd, /*selfIndex */ afd->getParameters()->size(), 0, kind);
}
/// Infer LifetimeDependenceInfo on a mutating method where 'self' is
/// nonescapable and the result is 'void'. For now, we'll assume that 'self'
/// depends on a single nonescapable argument.
std::optional<LifetimeDependenceInfo> LifetimeDependenceInfo::inferMutatingSelf(
AbstractFunctionDecl *afd) {
std::optional<LifetimeDependenceInfo> dep;
for (unsigned paramIndex : range(afd->getParameters()->size())) {
auto *param = afd->getParameters()->get(paramIndex);
Type paramTypeInContext =
afd->mapTypeIntoContext(param->getInterfaceType());
if (paramTypeInContext->hasError()) {
continue;
}
if (paramTypeInContext->isEscapable()) {
continue;
}
if (dep) {
// Don't infer dependence on multiple nonescapable parameters. We may want
// to do this in the future if dependsOn(self: arg1, arg2) syntax is too
// cumbersome.
return std::nullopt;
}
int selfIndex = afd->getParameters()->size();
dep = LifetimeDependenceInfo::getForIndex(
afd, selfIndex, paramIndex, LifetimeDependenceKind::Inherit);
}
return dep;
}
std::optional<llvm::ArrayRef<LifetimeDependenceInfo>>
LifetimeDependenceInfo::get(AbstractFunctionDecl *afd) {
if (!afd->getASTContext().LangOpts.hasFeature(Feature::NonescapableTypes)) {
return std::nullopt;
}
assert(isa<FuncDecl>(afd) || isa<ConstructorDecl>(afd));
if (afd->getAttrs().hasAttribute<LifetimeAttr>()) {
return LifetimeDependenceInfo::fromLifetimeAttribute(afd);
}
SmallVector<LifetimeDependenceInfo, 1> lifetimeDependencies;
auto resultDependence = LifetimeDependenceInfo::infer(afd);
if (!resultDependence.has_value()) {
return std::nullopt;
}
lifetimeDependencies.push_back(*resultDependence);
return afd->getASTContext().AllocateCopy(lifetimeDependencies);
}
std::optional<llvm::ArrayRef<LifetimeDependenceInfo>>
LifetimeDependenceInfo::get(FunctionTypeRepr *funcRepr,
ArrayRef<SILParameterInfo> params,
ArrayRef<SILResultInfo> results, DeclContext *dc) {
if (!dc->getASTContext().LangOpts.hasFeature(Feature::NonescapableTypes)) {
return std::nullopt;
}
SmallVector<LifetimeDependenceInfo, 1> lifetimeDependencies;
auto getLifetimeDependenceFromDependsOnTypeModifier =
[&](TypeRepr *typeRepr,
unsigned targetIndex) -> std::optional<LifetimeDependenceInfo> {
auto *lifetimeTypeRepr =
dyn_cast_or_null<LifetimeDependentTypeRepr>(typeRepr);
if (!lifetimeTypeRepr) {
return std::nullopt;
}
return LifetimeDependenceInfo::fromDependsOn(lifetimeTypeRepr, targetIndex,
params, dc);
};
auto argsTypeRepr = funcRepr->getArgsTypeRepr()->getElements();
for (unsigned targetIndex : indices(argsTypeRepr)) {
if (auto result = getLifetimeDependenceFromDependsOnTypeModifier(
argsTypeRepr[targetIndex].Type, targetIndex)) {
lifetimeDependencies.push_back(*result);
}
}
auto result = getLifetimeDependenceFromDependsOnTypeModifier(
funcRepr->getResultTypeRepr(), params.size());
if (result) {
lifetimeDependencies.push_back(*result);
}
return dc->getASTContext().AllocateCopy(lifetimeDependencies);
}
} // namespace swift