|
| 1 | +//===--- LifetimeDependence.cpp -----------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "swift/AST/LifetimeDependence.h" |
| 14 | +#include "TypeChecker.h" |
| 15 | +#include "swift/AST/ASTContext.h" |
| 16 | +#include "swift/AST/Decl.h" |
| 17 | +#include "swift/AST/ParameterList.h" |
| 18 | +#include "swift/AST/Type.h" |
| 19 | +#include "swift/AST/TypeRepr.h" |
| 20 | + |
| 21 | +namespace swift { |
| 22 | +std::string LifetimeDependenceInfo::getString() const { |
| 23 | + std::string lifetimeDependenceString; |
| 24 | + auto getOnIndices = [](IndexSubset *bitvector) { |
| 25 | + std::string result; |
| 26 | + bool isFirstSetBit = true; |
| 27 | + for (unsigned i = 0; i < bitvector->getCapacity(); i++) { |
| 28 | + if (bitvector->contains(i)) { |
| 29 | + if (!isFirstSetBit) { |
| 30 | + result += ", "; |
| 31 | + } |
| 32 | + result += std::to_string(i); |
| 33 | + isFirstSetBit = false; |
| 34 | + } |
| 35 | + } |
| 36 | + return result; |
| 37 | + }; |
| 38 | + if (!inheritLifetimeParamIndices->isEmpty()) { |
| 39 | + lifetimeDependenceString = |
| 40 | + "_inherit(" + getOnIndices(inheritLifetimeParamIndices) + ")"; |
| 41 | + } |
| 42 | + if (!borrowLifetimeParamIndices->isEmpty()) { |
| 43 | + lifetimeDependenceString += |
| 44 | + "_borrow(" + getOnIndices(borrowLifetimeParamIndices) + ")"; |
| 45 | + } |
| 46 | + if (!mutateLifetimeParamIndices->isEmpty()) { |
| 47 | + lifetimeDependenceString += |
| 48 | + "_mutate(" + getOnIndices(mutateLifetimeParamIndices) + ")"; |
| 49 | + } |
| 50 | + return lifetimeDependenceString; |
| 51 | +} |
| 52 | + |
| 53 | +void LifetimeDependenceInfo::Profile(llvm::FoldingSetNodeID &ID) const { |
| 54 | + if (inheritLifetimeParamIndices) { |
| 55 | + inheritLifetimeParamIndices->Profile(ID); |
| 56 | + } |
| 57 | + if (borrowLifetimeParamIndices) { |
| 58 | + borrowLifetimeParamIndices->Profile(ID); |
| 59 | + } |
| 60 | + if (mutateLifetimeParamIndices) { |
| 61 | + mutateLifetimeParamIndices->Profile(ID); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +llvm::Optional<LifetimeDependenceInfo> |
| 66 | +LifetimeDependenceInfo::fromTypeRepr(AbstractFunctionDecl *afd, Type resultType, |
| 67 | + bool allowIndex) { |
| 68 | + auto *dc = afd->getDeclContext(); |
| 69 | + auto &ctx = dc->getASTContext(); |
| 70 | + auto &diags = ctx.Diags; |
| 71 | + auto capacity = afd->getParameters()->size() + 1; |
| 72 | + auto lifetimeDependentRepr = |
| 73 | + cast<LifetimeDependentReturnTypeRepr>(afd->getResultTypeRepr()); |
| 74 | + |
| 75 | + SmallBitVector inheritLifetimeParamIndices(capacity); |
| 76 | + SmallBitVector borrowLifetimeParamIndices(capacity); |
| 77 | + SmallBitVector mutateLifetimeParamIndices(capacity); |
| 78 | + |
| 79 | + auto updateLifetimeDependenceInfo = [&](LifetimeDependenceSpecifier specifier, |
| 80 | + unsigned paramIndexToSet, |
| 81 | + ValueOwnership ownership) { |
| 82 | + auto loc = specifier.getLoc(); |
| 83 | + auto kind = specifier.getLifetimeDependenceKind(); |
| 84 | + |
| 85 | + if (resultType->isEscapable()) { |
| 86 | + diags.diagnose(loc, diag::lifetime_dependence_invalid_return_type); |
| 87 | + return true; |
| 88 | + } |
| 89 | + |
| 90 | + if (ownership == ValueOwnership::Default) { |
| 91 | + diags.diagnose(loc, diag::lifetime_dependence_missing_ownership_modifier); |
| 92 | + return true; |
| 93 | + } |
| 94 | + if (kind == LifetimeDependenceKind::Borrow && |
| 95 | + ownership != ValueOwnership::Shared) { |
| 96 | + diags.diagnose(loc, diag::lifetime_dependence_cannot_use_kind, "borrow", |
| 97 | + getOwnershipSpelling(ownership)); |
| 98 | + return true; |
| 99 | + } |
| 100 | + if (kind == LifetimeDependenceKind::Mutate && |
| 101 | + ownership != ValueOwnership::InOut) { |
| 102 | + diags.diagnose(loc, diag::lifetime_dependence_cannot_use_kind, "mutate", |
| 103 | + getOwnershipSpelling(ownership)); |
| 104 | + return true; |
| 105 | + } |
| 106 | + if (kind == LifetimeDependenceKind::Consume && |
| 107 | + ownership != ValueOwnership::Owned) { |
| 108 | + diags.diagnose(loc, diag::lifetime_dependence_cannot_use_kind, "consume", |
| 109 | + getOwnershipSpelling(ownership)); |
| 110 | + return true; |
| 111 | + } |
| 112 | + if (inheritLifetimeParamIndices.test(paramIndexToSet) || |
| 113 | + borrowLifetimeParamIndices.test(paramIndexToSet)) { |
| 114 | + diags.diagnose(loc, diag::lifetime_dependence_duplicate_param_id); |
| 115 | + return true; |
| 116 | + } |
| 117 | + if (kind == LifetimeDependenceKind::Copy || |
| 118 | + kind == LifetimeDependenceKind::Consume) { |
| 119 | + inheritLifetimeParamIndices.set(paramIndexToSet); |
| 120 | + } else if (kind == LifetimeDependenceKind::Borrow) { |
| 121 | + borrowLifetimeParamIndices.set(paramIndexToSet); |
| 122 | + } else { |
| 123 | + assert(kind == LifetimeDependenceKind::Mutate); |
| 124 | + mutateLifetimeParamIndices.set(paramIndexToSet); |
| 125 | + } |
| 126 | + return false; |
| 127 | + }; |
| 128 | + |
| 129 | + for (auto specifier : lifetimeDependentRepr->getLifetimeDependencies()) { |
| 130 | + switch (specifier.getSpecifierKind()) { |
| 131 | + case LifetimeDependenceSpecifier::SpecifierKind::Named: { |
| 132 | + bool foundParamName = false; |
| 133 | + unsigned paramIndexToSet = 1; |
| 134 | + for (auto *param : *afd->getParameters()) { |
| 135 | + if (param->getParameterName() == specifier.getName()) { |
| 136 | + foundParamName = true; |
| 137 | + if (updateLifetimeDependenceInfo(specifier, paramIndexToSet, |
| 138 | + param->getValueOwnership())) { |
| 139 | + return llvm::None; |
| 140 | + } |
| 141 | + break; |
| 142 | + } |
| 143 | + paramIndexToSet++; |
| 144 | + } |
| 145 | + if (!foundParamName) { |
| 146 | + diags.diagnose(specifier.getLoc(), |
| 147 | + diag::lifetime_dependence_invalid_param_name, |
| 148 | + specifier.getName()); |
| 149 | + return llvm::None; |
| 150 | + } |
| 151 | + break; |
| 152 | + } |
| 153 | + case LifetimeDependenceSpecifier::SpecifierKind::Ordered: { |
| 154 | + auto paramIndex = specifier.getIndex(); |
| 155 | + if (paramIndex > afd->getParameters()->size()) { |
| 156 | + diags.diagnose(specifier.getLoc(), |
| 157 | + diag::lifetime_dependence_invalid_param_index, |
| 158 | + paramIndex); |
| 159 | + return llvm::None; |
| 160 | + } |
| 161 | + if (updateLifetimeDependenceInfo( |
| 162 | + specifier, /*paramIndexToSet*/ specifier.getIndex() + 1, |
| 163 | + afd->getParameters()->get(paramIndex)->getValueOwnership())) { |
| 164 | + return llvm::None; |
| 165 | + } |
| 166 | + break; |
| 167 | + } |
| 168 | + case LifetimeDependenceSpecifier::SpecifierKind::Self: { |
| 169 | + if (!afd->hasImplicitSelfDecl()) { |
| 170 | + diags.diagnose(specifier.getLoc(), |
| 171 | + diag::lifetime_dependence_invalid_self); |
| 172 | + return llvm::None; |
| 173 | + } |
| 174 | + if (updateLifetimeDependenceInfo( |
| 175 | + specifier, /*selfIndex*/ 0, |
| 176 | + afd->getImplicitSelfDecl()->getValueOwnership())) { |
| 177 | + return llvm::None; |
| 178 | + } |
| 179 | + break; |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + return LifetimeDependenceInfo( |
| 185 | + IndexSubset::get(ctx, inheritLifetimeParamIndices), |
| 186 | + IndexSubset::get(ctx, borrowLifetimeParamIndices), |
| 187 | + IndexSubset::get(ctx, mutateLifetimeParamIndices)); |
| 188 | +} |
| 189 | + |
| 190 | +llvm::Optional<LifetimeDependenceInfo> |
| 191 | +LifetimeDependenceInfo::get(AbstractFunctionDecl *afd, Type resultType, |
| 192 | + bool allowIndex) { |
| 193 | + auto *returnTypeRepr = afd->getResultTypeRepr(); |
| 194 | + if (!returnTypeRepr) { |
| 195 | + return llvm::None; |
| 196 | + } |
| 197 | + if (!isa<LifetimeDependentReturnTypeRepr>(returnTypeRepr)) { |
| 198 | + return llvm::None; |
| 199 | + } |
| 200 | + return LifetimeDependenceInfo::fromTypeRepr(afd, resultType, allowIndex); |
| 201 | +} |
| 202 | +} // namespace swift |
0 commit comments