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
|
//===--- FeatureSet.cpp - Language feature support --------------*- C++ -*-===//
//
// 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 "FeatureSet.h"
#include "swift/AST/Decl.h"
#include "swift/AST/ExistentialLayout.h"
#include "swift/AST/GenericParamList.h"
#include "swift/AST/NameLookup.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/Pattern.h"
#include "clang/AST/DeclObjC.h"
#include "swift/Basic/Assertions.h"
using namespace swift;
/// Does the interface of this declaration use a type for which the
/// given predicate returns true?
static bool usesTypeMatching(Decl *decl, llvm::function_ref<bool(Type)> fn) {
if (auto value = dyn_cast<ValueDecl>(decl)) {
if (Type type = value->getInterfaceType()) {
return type.findIf(fn);
}
}
return false;
}
// ----------------------------------------------------------------------------
// MARK: - Standard Features
// ----------------------------------------------------------------------------
/// Functions to determine which features a particular declaration uses. The
/// usesFeatureNNN functions correspond to the features in Features.def.
#define BASELINE_LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
static bool usesFeature##FeatureName(Decl *decl) { return false; }
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description)
#include "swift/Basic/Features.def"
#define UNINTERESTING_FEATURE(FeatureName) \
static bool usesFeature##FeatureName(Decl *decl) { return false; }
// ----------------------------------------------------------------------------
// MARK: - Upcoming Features
// ----------------------------------------------------------------------------
UNINTERESTING_FEATURE(ConciseMagicFile)
UNINTERESTING_FEATURE(ForwardTrailingClosures)
UNINTERESTING_FEATURE(StrictConcurrency)
UNINTERESTING_FEATURE(BareSlashRegexLiterals)
UNINTERESTING_FEATURE(DeprecateApplicationMain)
UNINTERESTING_FEATURE(ImportObjcForwardDeclarations)
UNINTERESTING_FEATURE(DisableOutwardActorInference)
UNINTERESTING_FEATURE(InternalImportsByDefault)
UNINTERESTING_FEATURE(IsolatedDefaultValues)
UNINTERESTING_FEATURE(GlobalConcurrency)
UNINTERESTING_FEATURE(FullTypedThrows)
UNINTERESTING_FEATURE(ExistentialAny)
UNINTERESTING_FEATURE(InferSendableFromCaptures)
UNINTERESTING_FEATURE(ImplicitOpenExistentials)
UNINTERESTING_FEATURE(MemberImportVisibility)
// ----------------------------------------------------------------------------
// MARK: - Experimental Features
// ----------------------------------------------------------------------------
UNINTERESTING_FEATURE(StaticAssert)
UNINTERESTING_FEATURE(NamedOpaqueTypes)
UNINTERESTING_FEATURE(FlowSensitiveConcurrencyCaptures)
UNINTERESTING_FEATURE(CodeItemMacros)
UNINTERESTING_FEATURE(PreambleMacros)
UNINTERESTING_FEATURE(TupleConformances)
UNINTERESTING_FEATURE(SymbolLinkageMarkers)
UNINTERESTING_FEATURE(LazyImmediate)
UNINTERESTING_FEATURE(MoveOnlyClasses)
UNINTERESTING_FEATURE(NoImplicitCopy)
UNINTERESTING_FEATURE(OldOwnershipOperatorSpellings)
UNINTERESTING_FEATURE(MoveOnlyEnumDeinits)
UNINTERESTING_FEATURE(MoveOnlyTuples)
UNINTERESTING_FEATURE(MoveOnlyPartialReinitialization)
UNINTERESTING_FEATURE(OneWayClosureParameters)
UNINTERESTING_FEATURE(LayoutPrespecialization)
UNINTERESTING_FEATURE(AccessLevelOnImport)
UNINTERESTING_FEATURE(AllowNonResilientAccessInPackage)
UNINTERESTING_FEATURE(ClientBypassResilientAccessInPackage)
UNINTERESTING_FEATURE(LayoutStringValueWitnesses)
UNINTERESTING_FEATURE(LayoutStringValueWitnessesInstantiation)
UNINTERESTING_FEATURE(DifferentiableProgramming)
UNINTERESTING_FEATURE(ForwardModeDifferentiation)
UNINTERESTING_FEATURE(AdditiveArithmeticDerivedConformances)
UNINTERESTING_FEATURE(SendableCompletionHandlers)
UNINTERESTING_FEATURE(OpaqueTypeErasure)
UNINTERESTING_FEATURE(PackageCMO)
UNINTERESTING_FEATURE(ParserRoundTrip)
UNINTERESTING_FEATURE(ParserValidation)
UNINTERESTING_FEATURE(ImplicitSome)
UNINTERESTING_FEATURE(ParserASTGen)
UNINTERESTING_FEATURE(BuiltinMacros)
UNINTERESTING_FEATURE(ImportSymbolicCXXDecls)
UNINTERESTING_FEATURE(GenerateBindingsForThrowingFunctionsInCXX)
UNINTERESTING_FEATURE(ReferenceBindings)
UNINTERESTING_FEATURE(BuiltinModule)
UNINTERESTING_FEATURE(RegionBasedIsolation)
UNINTERESTING_FEATURE(PlaygroundExtendedCallbacks)
UNINTERESTING_FEATURE(ThenStatements)
UNINTERESTING_FEATURE(DoExpressions)
UNINTERESTING_FEATURE(ImplicitLastExprResults)
UNINTERESTING_FEATURE(RawLayout)
UNINTERESTING_FEATURE(Embedded)
UNINTERESTING_FEATURE(Volatile)
UNINTERESTING_FEATURE(SuppressedAssociatedTypes)
UNINTERESTING_FEATURE(StructLetDestructuring)
UNINTERESTING_FEATURE(MacrosOnImports)
static bool usesFeatureNonescapableTypes(Decl *decl) {
auto containsNonEscapable =
[](SmallVectorImpl<InverseRequirement> &inverseReqs) {
auto foundIt =
llvm::find_if(inverseReqs, [](InverseRequirement inverseReq) {
if (inverseReq.getKind() == InvertibleProtocolKind::Escapable) {
return true;
}
return false;
});
return foundIt != inverseReqs.end();
};
if (auto *valueDecl = dyn_cast<ValueDecl>(decl)) {
if (isa<StructDecl, EnumDecl, ClassDecl>(decl)) {
auto *nominalDecl = cast<NominalTypeDecl>(valueDecl);
InvertibleProtocolSet inverses;
bool anyObject = false;
getDirectlyInheritedNominalTypeDecls(nominalDecl, inverses, anyObject);
if (inverses.containsEscapable()) {
return true;
}
}
if (auto proto = dyn_cast<ProtocolDecl>(decl)) {
auto reqSig = proto->getRequirementSignature();
SmallVector<Requirement, 2> reqs;
SmallVector<InverseRequirement, 2> inverses;
reqSig.getRequirementsWithInverses(proto, reqs, inverses);
if (containsNonEscapable(inverses))
return true;
}
if (isa<AbstractFunctionDecl>(valueDecl) ||
isa<AbstractStorageDecl>(valueDecl)) {
if (valueDecl->getInterfaceType().findIf([&](Type type) -> bool {
if (auto *nominalDecl = type->getAnyNominal()) {
if (isa<StructDecl, EnumDecl, ClassDecl>(nominalDecl))
return usesFeatureNonescapableTypes(nominalDecl);
}
return false;
})) {
return true;
}
}
}
if (auto *ext = dyn_cast<ExtensionDecl>(decl)) {
if (auto *nominal = ext->getExtendedNominal())
if (usesFeatureNonescapableTypes(nominal))
return true;
}
if (auto *genCtx = decl->getAsGenericContext()) {
if (auto genericSig = genCtx->getGenericSignature()) {
SmallVector<Requirement, 2> reqs;
SmallVector<InverseRequirement, 2> inverseReqs;
genericSig->getRequirementsWithInverses(reqs, inverseReqs);
if (containsNonEscapable(inverseReqs)) {
return true;
}
}
}
return false;
}
UNINTERESTING_FEATURE(StaticExclusiveOnly)
UNINTERESTING_FEATURE(ExtractConstantsFromMembers)
UNINTERESTING_FEATURE(FixedArrays)
UNINTERESTING_FEATURE(GroupActorErrors)
UNINTERESTING_FEATURE(SameElementRequirements)
UNINTERESTING_FEATURE(UnspecifiedMeansMainActorIsolated)
UNINTERESTING_FEATURE(GenerateForceToMainActorThunks)
UNINTERESTING_FEATURE(Span)
static bool usesFeatureSendingArgsAndResults(Decl *decl) {
auto isFunctionTypeWithSending = [](Type type) {
auto fnType = type->getAs<AnyFunctionType>();
if (!fnType)
return false;
if (fnType->hasExtInfo() && fnType->hasSendingResult())
return true;
return llvm::any_of(fnType->getParams(),
[](AnyFunctionType::Param param) {
return param.getParameterFlags().isSending();
});
};
auto declUsesFunctionTypesThatUseSending = [&](Decl *decl) {
return usesTypeMatching(decl, isFunctionTypeWithSending);
};
if (auto *pd = dyn_cast<ParamDecl>(decl)) {
if (pd->isSending()) {
return true;
}
if (declUsesFunctionTypesThatUseSending(pd))
return true;
}
if (auto *fDecl = dyn_cast<AbstractFunctionDecl>(decl)) {
// First check for param decl results.
if (llvm::any_of(fDecl->getParameters()->getArray(), [](ParamDecl *pd) {
return usesFeatureSendingArgsAndResults(pd);
}))
return true;
if (declUsesFunctionTypesThatUseSending(decl))
return true;
}
// Check if we have a pattern binding decl for a function that has sending
// parameters and results.
if (auto *pbd = dyn_cast<PatternBindingDecl>(decl)) {
for (auto index : range(pbd->getNumPatternEntries())) {
auto *pattern = pbd->getPattern(index);
if (pattern->hasType() && isFunctionTypeWithSending(pattern->getType()))
return true;
}
}
return false;
}
static bool usesFeatureLifetimeDependence(Decl *decl) {
if (decl->getAttrs().hasAttribute<LifetimeAttr>()) {
return true;
}
auto *afd = dyn_cast<AbstractFunctionDecl>(decl);
if (!afd) {
return false;
}
return afd->getInterfaceType()
->getAs<AnyFunctionType>()
->hasLifetimeDependencies();
}
UNINTERESTING_FEATURE(DynamicActorIsolation)
UNINTERESTING_FEATURE(NonfrozenEnumExhaustivity)
UNINTERESTING_FEATURE(ClosureIsolation)
UNINTERESTING_FEATURE(Extern)
UNINTERESTING_FEATURE(ConsumeSelfInDeinit)
static bool usesFeatureBitwiseCopyable2(Decl *decl) {
if (!decl->getModuleContext()->isStdlibModule()) {
return false;
}
if (auto *proto = dyn_cast<ProtocolDecl>(decl)) {
return proto->getNameStr() == "BitwiseCopyable";
}
if (auto *typealias = dyn_cast<TypeAliasDecl>(decl)) {
return typealias->getNameStr() == "_BitwiseCopyable";
}
return false;
}
static bool usesFeatureIsolatedAny(Decl *decl) {
return usesTypeMatching(decl, [](Type type) {
if (auto fnType = type->getAs<AnyFunctionType>()) {
return fnType->getIsolation().isErased();
}
return false;
});
}
UNINTERESTING_FEATURE(IsolatedAny2)
UNINTERESTING_FEATURE(GlobalActorIsolatedTypesUsability)
UNINTERESTING_FEATURE(ObjCImplementation)
UNINTERESTING_FEATURE(ObjCImplementationWithResilientStorage)
UNINTERESTING_FEATURE(CImplementation)
UNINTERESTING_FEATURE(Sensitive)
UNINTERESTING_FEATURE(DebugDescriptionMacro)
UNINTERESTING_FEATURE(ReinitializeConsumeInMultiBlockDefer)
UNINTERESTING_FEATURE(SE427NoInferenceOnExtension)
UNINTERESTING_FEATURE(TrailingComma)
static bool usesFeatureAllowUnsafeAttribute(Decl *decl) {
return decl->getAttrs().hasAttribute<UnsafeAttr>();
}
UNINTERESTING_FEATURE(WarnUnsafe)
UNINTERESTING_FEATURE(SafeInterop)
UNINTERESTING_FEATURE(AssumeResilientCxxTypes)
UNINTERESTING_FEATURE(CoroutineAccessorsUnwindOnCallerError)
UNINTERESTING_FEATURE(CoroutineAccessorsAllocateInCallee)
bool swift::usesFeatureIsolatedDeinit(const Decl *decl) {
if (auto cd = dyn_cast<ClassDecl>(decl)) {
return cd->getFormalAccess() == AccessLevel::Open &&
usesFeatureIsolatedDeinit(cd->getDestructor());
} else if (auto dd = dyn_cast<DestructorDecl>(decl)) {
if (dd->hasExplicitIsolationAttribute()) {
return true;
}
if (auto superDD = dd->getSuperDeinit()) {
return usesFeatureIsolatedDeinit(superDD);
}
return false;
} else {
return false;
}
}
static bool usesFeatureValueGenerics(Decl *decl) {
auto genericContext = decl->getAsGenericContext();
if (!genericContext || !genericContext->getGenericParams())
return false;
for (auto param : genericContext->getGenericParams()->getParams()) {
if (param->isValue())
return true;
continue;
}
return false;
}
static bool usesFeatureCoroutineAccessors(Decl *decl) {
auto accessorDeclUsesFeatureCoroutineAccessors = [](AccessorDecl *accessor) {
return requiresFeatureCoroutineAccessors(accessor->getAccessorKind());
};
switch (decl->getKind()) {
case DeclKind::Var: {
auto *var = cast<VarDecl>(decl);
return llvm::any_of(var->getAllAccessors(),
accessorDeclUsesFeatureCoroutineAccessors);
}
case DeclKind::Accessor: {
auto *accessor = cast<AccessorDecl>(decl);
return accessorDeclUsesFeatureCoroutineAccessors(accessor);
}
default:
return false;
}
}
// ----------------------------------------------------------------------------
// MARK: - FeatureSet
// ----------------------------------------------------------------------------
void FeatureSet::collectRequiredFeature(Feature feature,
InsertOrRemove operation) {
required.insertOrRemove(feature, operation == Insert);
}
void FeatureSet::collectSuppressibleFeature(Feature feature,
InsertOrRemove operation) {
suppressible.insertOrRemove(numFeatures() - size_t(feature),
operation == Insert);
}
static bool hasFeatureSuppressionAttribute(Decl *decl, StringRef featureName,
bool inverted) {
auto attr = decl->getAttrs().getAttribute<AllowFeatureSuppressionAttr>();
if (!attr)
return false;
if (attr->getInverted() != inverted)
return false;
for (auto suppressedFeature : attr->getSuppressedFeatures()) {
if (suppressedFeature.is(featureName))
return true;
}
return false;
}
static bool disallowFeatureSuppression(StringRef featureName, Decl *decl) {
return hasFeatureSuppressionAttribute(decl, featureName, true);
}
static bool allowFeatureSuppression(StringRef featureName, Decl *decl) {
return hasFeatureSuppressionAttribute(decl, featureName, false);
}
/// Go through all the features used by the given declaration and
/// either add or remove them to this set.
void FeatureSet::collectFeaturesUsed(Decl *decl, InsertOrRemove operation) {
// Go through each of the features, checking whether the
// declaration uses that feature.
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
if (usesFeature##FeatureName(decl)) \
collectRequiredFeature(Feature::FeatureName, operation);
#define SUPPRESSIBLE_LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
if (usesFeature##FeatureName(decl)) { \
if (disallowFeatureSuppression(#FeatureName, decl)) \
collectRequiredFeature(Feature::FeatureName, operation); \
else \
collectSuppressibleFeature(Feature::FeatureName, operation); \
}
#define CONDITIONALLY_SUPPRESSIBLE_LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
if (usesFeature##FeatureName(decl)) { \
if (allowFeatureSuppression(#FeatureName, decl)) \
collectSuppressibleFeature(Feature::FeatureName, operation); \
else \
collectRequiredFeature(Feature::FeatureName, operation); \
}
#include "swift/Basic/Features.def"
}
FeatureSet swift::getUniqueFeaturesUsed(Decl *decl) {
// Add all the features used by this declaration.
FeatureSet features;
features.collectFeaturesUsed(decl, FeatureSet::Insert);
// Remove all the features used by all enclosing declarations.
Decl *enclosingDecl = decl;
while (!features.empty()) {
// Find the next outermost enclosing declaration.
if (auto accessor = dyn_cast<AccessorDecl>(enclosingDecl))
enclosingDecl = accessor->getStorage();
else
enclosingDecl = enclosingDecl->getDeclContext()->getAsDecl();
if (!enclosingDecl)
break;
features.collectFeaturesUsed(enclosingDecl, FeatureSet::Remove);
}
return features;
}
|