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 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
|
//===--- 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/NameLookup.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/Pattern.h"
#include "clang/AST/DeclObjC.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; }
static bool usesFeatureRethrowsProtocol(Decl *decl,
SmallPtrSet<Decl *, 16> &checked) {
// Make sure we don't recurse.
if (!checked.insert(decl).second)
return false;
// Check an inheritance clause for a marker protocol.
auto checkInherited = [&](InheritedTypes inherited) -> bool {
for (unsigned i : inherited.getIndices()) {
if (auto inheritedType = inherited.getResolvedType(i)) {
if (inheritedType->isExistentialType()) {
auto layout = inheritedType->getExistentialLayout();
for (ProtocolDecl *proto : layout.getProtocols()) {
if (usesFeatureRethrowsProtocol(proto, checked))
return true;
}
}
}
}
return false;
};
if (auto nominal = dyn_cast<NominalTypeDecl>(decl)) {
if (checkInherited(nominal->getInherited()))
return true;
}
if (auto proto = dyn_cast<ProtocolDecl>(decl)) {
if (proto->getAttrs().hasAttribute<AtRethrowsAttr>())
return true;
}
if (auto ext = dyn_cast<ExtensionDecl>(decl)) {
if (auto nominal = ext->getSelfNominalTypeDecl())
if (usesFeatureRethrowsProtocol(nominal, checked))
return true;
if (checkInherited(ext->getInherited()))
return true;
}
if (auto genericSig =
decl->getInnermostDeclContext()->getGenericSignatureOfContext()) {
for (const auto &req : genericSig.getRequirements()) {
if (req.getKind() == RequirementKind::Conformance &&
usesFeatureRethrowsProtocol(req.getProtocolDecl(), checked))
return true;
}
}
if (auto value = dyn_cast<ValueDecl>(decl)) {
if (Type type = value->getInterfaceType()) {
bool hasRethrowsProtocol = type.findIf([&](Type type) {
if (auto nominal = type->getAnyNominal()) {
if (usesFeatureRethrowsProtocol(nominal, checked))
return true;
}
return false;
});
if (hasRethrowsProtocol)
return true;
}
}
return false;
}
static bool usesFeatureRethrowsProtocol(Decl *decl) {
SmallPtrSet<Decl *, 16> checked;
return usesFeatureRethrowsProtocol(decl, checked);
}
UNINTERESTING_FEATURE(BuiltinBuildTaskExecutorRef)
UNINTERESTING_FEATURE(BuiltinBuildComplexEqualityExecutor)
UNINTERESTING_FEATURE(BuiltinCreateAsyncTaskInGroupWithExecutor)
UNINTERESTING_FEATURE(BuiltinCreateAsyncDiscardingTaskInGroup)
UNINTERESTING_FEATURE(BuiltinCreateAsyncDiscardingTaskInGroupWithExecutor)
UNINTERESTING_FEATURE(BuiltinUnprotectedStackAlloc)
UNINTERESTING_FEATURE(BuiltinAllocVector)
UNINTERESTING_FEATURE(BuiltinCreateTask)
static bool usesFeatureNewCxxMethodSafetyHeuristics(Decl *decl) {
return decl->hasClangNode();
}
static bool usesFeatureSpecializeAttributeWithAvailability(Decl *decl) {
if (auto func = dyn_cast<AbstractFunctionDecl>(decl)) {
for (auto specialize : func->getAttrs().getAttributes<SpecializeAttr>()) {
if (!specialize->getAvailableAttrs().empty())
return true;
}
}
return false;
}
static bool usesFeaturePrimaryAssociatedTypes2(Decl *decl) {
if (auto *protoDecl = dyn_cast<ProtocolDecl>(decl)) {
if (protoDecl->getPrimaryAssociatedTypes().size() > 0)
return true;
}
return false;
}
static bool usesFeatureAssociatedTypeAvailability(Decl *decl) {
return isa<AssociatedTypeDecl>(decl) &&
decl->getAttrs().hasAttribute<AvailableAttr>();
}
static bool isImplicitRethrowsProtocol(const ProtocolDecl *proto) {
return proto->isSpecificProtocol(KnownProtocolKind::AsyncSequence) ||
proto->isSpecificProtocol(KnownProtocolKind::AsyncIteratorProtocol);
}
static bool usesFeatureAsyncSequenceFailure(Decl *decl) {
if (auto proto = dyn_cast<ProtocolDecl>(decl)) {
return isImplicitRethrowsProtocol(proto);
}
return false;
}
static bool usesFeatureMacros(Decl *decl) { return isa<MacroDecl>(decl); }
static bool usesFeatureFreestandingExpressionMacros(Decl *decl) {
auto macro = dyn_cast<MacroDecl>(decl);
if (!macro)
return false;
return macro->getMacroRoles().contains(MacroRole::Expression);
}
static bool usesFeatureAttachedMacros(Decl *decl) {
auto macro = dyn_cast<MacroDecl>(decl);
if (!macro)
return false;
return static_cast<bool>(macro->getMacroRoles() & getAttachedMacroRoles());
}
static bool usesFeatureExtensionMacros(Decl *decl) {
auto macro = dyn_cast<MacroDecl>(decl);
if (!macro)
return false;
return macro->getMacroRoles().contains(MacroRole::Extension);
}
static bool usesFeatureMoveOnly(Decl *decl) {
if (auto *extension = dyn_cast<ExtensionDecl>(decl)) {
if (auto *nominal = extension->getExtendedNominal())
return usesFeatureMoveOnly(nominal);
return false;
}
auto hasInverseInType = [&](Type type) {
return type.findIf([&](Type type) -> bool {
if (auto *NTD = type->getAnyNominal()) {
if (NTD->getAttrs().hasAttribute<MoveOnlyAttr>())
return true;
}
return false;
});
};
if (auto *TD = dyn_cast<TypeDecl>(decl)) {
if (auto *alias = dyn_cast<TypeAliasDecl>(TD))
return hasInverseInType(alias->getUnderlyingType());
if (auto *NTD = dyn_cast<NominalTypeDecl>(TD)) {
if (NTD->getAttrs().hasAttribute<MoveOnlyAttr>())
return true;
}
return false;
}
if (auto *VD = dyn_cast<ValueDecl>(decl)) {
return hasInverseInType(VD->getInterfaceType());
}
return false;
}
static bool usesFeatureMoveOnlyResilientTypes(Decl *decl) {
if (auto *nomDecl = dyn_cast<NominalTypeDecl>(decl))
return nomDecl->isResilient() && usesFeatureMoveOnly(decl);
return false;
}
static bool hasParameterPacks(Decl *decl) {
if (auto genericContext = decl->getAsGenericContext()) {
auto sig = genericContext->getGenericSignature();
if (llvm::any_of(sig.getGenericParams(),
[&](const GenericTypeParamType *GP) {
return GP->isParameterPack();
})) {
return true;
}
}
return false;
}
/// A declaration needs the $ParameterPacks feature if it declares a
/// generic parameter pack, or if its type references a generic nominal
/// or type alias which declares a generic parameter pack.
static bool usesFeatureParameterPacks(Decl *decl) {
if (hasParameterPacks(decl))
return true;
if (auto *valueDecl = dyn_cast<ValueDecl>(decl)) {
if (valueDecl->getInterfaceType().findIf([&](Type t) {
if (auto *alias = dyn_cast<TypeAliasType>(t.getPointer()))
return hasParameterPacks(alias->getDecl());
if (auto *nominal = t->getAnyNominal())
return hasParameterPacks(nominal);
return false;
})) {
return true;
}
}
return false;
}
static bool usesFeatureLexicalLifetimes(Decl *decl) {
return decl->getAttrs().hasAttribute<EagerMoveAttr>() ||
decl->getAttrs().hasAttribute<NoEagerMoveAttr>() ||
decl->getAttrs().hasAttribute<LexicalLifetimesAttr>();
}
static bool usesFeatureFreestandingMacros(Decl *decl) {
auto macro = dyn_cast<MacroDecl>(decl);
if (!macro)
return false;
return macro->getMacroRoles().contains(MacroRole::Declaration);
}
static bool usesFeatureRetroactiveAttribute(Decl *decl) {
auto ext = dyn_cast<ExtensionDecl>(decl);
if (!ext)
return false;
return llvm::any_of(
ext->getInherited().getEntries(),
[](const InheritedEntry &entry) { return entry.isRetroactive(); });
}
static bool usesFeatureExtensionMacroAttr(Decl *decl) {
return usesFeatureExtensionMacros(decl);
}
static bool usesFeatureTypedThrows(Decl *decl) {
if (auto func = dyn_cast<AbstractFunctionDecl>(decl)) {
return usesTypeMatching(decl, [](Type ty) {
if (auto funcType = ty->getAs<AnyFunctionType>())
return funcType->hasThrownError();
return false;
});
}
return false;
}
static bool usesFeatureOptionalIsolatedParameters(Decl *decl) {
auto *value = dyn_cast<ValueDecl>(decl);
if (!value)
return false;
auto *paramList = getParameterList(value);
if (!paramList)
return false;
for (auto param : *paramList) {
if (param->isIsolated()) {
auto paramType = param->getInterfaceType();
return !paramType->getOptionalObjectType().isNull();
}
}
return false;
}
static bool usesFeatureAssociatedTypeImplements(Decl *decl) {
return isa<TypeDecl>(decl) && decl->getAttrs().hasAttribute<ImplementsAttr>();
}
static bool usesFeatureExpressionMacroDefaultArguments(Decl *decl) {
if (auto func = dyn_cast<AbstractFunctionDecl>(decl)) {
for (auto param : *func->getParameters()) {
if (param->getDefaultArgumentKind() ==
DefaultArgumentKind::ExpressionMacro)
return true;
}
}
return false;
}
UNINTERESTING_FEATURE(BuiltinStoreRaw)
UNINTERESTING_FEATURE(BuiltinAddressOfRawLayout)
// ----------------------------------------------------------------------------
// MARK: - Upcoming Features
// ----------------------------------------------------------------------------
UNINTERESTING_FEATURE(ConciseMagicFile)
UNINTERESTING_FEATURE(ForwardTrailingClosures)
UNINTERESTING_FEATURE(StrictConcurrency)
UNINTERESTING_FEATURE(BareSlashRegexLiterals)
UNINTERESTING_FEATURE(DeprecateApplicationMain)
static bool usesFeatureImportObjcForwardDeclarations(Decl *decl) {
ClangNode clangNode = decl->getClangNode();
if (!clangNode)
return false;
const clang::Decl *clangDecl = clangNode.getAsDecl();
if (!clangDecl)
return false;
if (auto objCInterfaceDecl = dyn_cast<clang::ObjCInterfaceDecl>(clangDecl))
return !objCInterfaceDecl->hasDefinition();
if (auto objCProtocolDecl = dyn_cast<clang::ObjCProtocolDecl>(clangDecl))
return !objCProtocolDecl->hasDefinition();
return false;
}
UNINTERESTING_FEATURE(DisableOutwardActorInference)
UNINTERESTING_FEATURE(InternalImportsByDefault)
UNINTERESTING_FEATURE(IsolatedDefaultValues)
UNINTERESTING_FEATURE(GlobalConcurrency)
UNINTERESTING_FEATURE(FullTypedThrows)
UNINTERESTING_FEATURE(ExistentialAny)
UNINTERESTING_FEATURE(InferSendableFromCaptures)
UNINTERESTING_FEATURE(ImplicitOpenExistentials)
// ----------------------------------------------------------------------------
// MARK: - Experimental Features
// ----------------------------------------------------------------------------
UNINTERESTING_FEATURE(StaticAssert)
UNINTERESTING_FEATURE(NamedOpaqueTypes)
UNINTERESTING_FEATURE(FlowSensitiveConcurrencyCaptures)
static bool usesFeatureCodeItemMacros(Decl *decl) {
auto macro = dyn_cast<MacroDecl>(decl);
if (!macro)
return false;
return macro->getMacroRoles().contains(MacroRole::CodeItem);
}
UNINTERESTING_FEATURE(BodyMacros)
UNINTERESTING_FEATURE(PreambleMacros)
UNINTERESTING_FEATURE(TupleConformances)
static bool usesFeatureSymbolLinkageMarkers(Decl *decl) {
auto &attrs = decl->getAttrs();
return std::any_of(attrs.begin(), attrs.end(), [](auto *attr) {
if (isa<UsedAttr>(attr))
return true;
if (isa<SectionAttr>(attr))
return true;
return false;
});
}
UNINTERESTING_FEATURE(LazyImmediate)
static bool usesFeatureMoveOnlyClasses(Decl *decl) {
return isa<ClassDecl>(decl) && usesFeatureMoveOnly(decl);
}
static bool usesFeatureNoImplicitCopy(Decl *decl) {
return decl->isNoImplicitCopy();
}
UNINTERESTING_FEATURE(OldOwnershipOperatorSpellings)
static bool usesFeatureMoveOnlyEnumDeinits(Decl *decl) {
if (auto *ei = dyn_cast<EnumDecl>(decl)) {
return usesFeatureMoveOnly(ei) && ei->getValueTypeDestructor();
}
return false;
}
UNINTERESTING_FEATURE(MoveOnlyTuples)
// Partial consumption does not affect declarations directly.
UNINTERESTING_FEATURE(MoveOnlyPartialConsumption)
UNINTERESTING_FEATURE(MoveOnlyPartialReinitialization)
UNINTERESTING_FEATURE(OneWayClosureParameters)
static bool usesFeatureLayoutPrespecialization(Decl *decl) {
auto &attrs = decl->getAttrs();
return std::any_of(attrs.begin(), attrs.end(), [](auto *attr) {
if (auto *specialize = dyn_cast<SpecializeAttr>(attr)) {
return !specialize->getTypeErasedParams().empty();
}
return false;
});
}
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(ParserDiagnostics)
UNINTERESTING_FEATURE(ImplicitSome)
UNINTERESTING_FEATURE(ParserASTGen)
UNINTERESTING_FEATURE(BuiltinMacros)
UNINTERESTING_FEATURE(ImportSymbolicCXXDecls)
UNINTERESTING_FEATURE(GenerateBindingsForThrowingFunctionsInCXX)
static bool usesFeatureReferenceBindings(Decl *decl) {
auto *vd = dyn_cast<VarDecl>(decl);
return vd && vd->getIntroducer() == VarDecl::Introducer::InOut;
}
UNINTERESTING_FEATURE(BuiltinModule)
UNINTERESTING_FEATURE(RegionBasedIsolation)
UNINTERESTING_FEATURE(PlaygroundExtendedCallbacks)
UNINTERESTING_FEATURE(ThenStatements)
UNINTERESTING_FEATURE(DoExpressions)
UNINTERESTING_FEATURE(ImplicitLastExprResults)
static bool usesFeatureRawLayout(Decl *decl) {
return decl->getAttrs().hasAttribute<RawLayoutAttr>();
}
UNINTERESTING_FEATURE(Embedded)
UNINTERESTING_FEATURE(SuppressedAssociatedTypes)
static bool disallowFeatureSuppression(StringRef featureName, Decl *decl);
static bool allBoundTypesAreCopyable(Type type, DeclContext *context) {
assert(type->getAnyNominal());
auto bgt = type->getAs<BoundGenericType>();
if (!bgt)
return false; // nothing is bound.
for (auto argInterfaceTy : bgt->getGenericArgs())
if (context->mapTypeIntoContext(argInterfaceTy)->isNoncopyable())
return false;
return true;
}
static bool usesFeatureNoncopyableGenerics(Decl *decl) {
if (decl->getAttrs().hasAttribute<PreInverseGenericsAttr>())
return true;
if (auto *valueDecl = dyn_cast<ValueDecl>(decl)) {
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 (!inverses.empty())
return true;
}
if (isa<AbstractFunctionDecl>(valueDecl) ||
isa<AbstractStorageDecl>(valueDecl)) {
auto *context = decl->getInnermostDeclContext();
auto usesFeature = valueDecl->getInterfaceType().findIf(
[&](Type type) -> bool {
auto *nominalDecl = type->getAnyNominal();
if (!nominalDecl || !isa<StructDecl, EnumDecl, ClassDecl>(nominalDecl))
return false;
if (!usesFeatureNoncopyableGenerics(nominalDecl))
return false;
// If we only _refer_ to a TypeDecl that uses NoncopyableGenerics,
// and a suppressed version of that decl is in the interface, then we're
// only referring to the un-suppressed version if any of the bound types
// are noncopyable. (rdar://127389991)
if (!disallowFeatureSuppression("NoncopyableGenerics", nominalDecl)
&& allBoundTypesAreCopyable(type, context)) {
return false;
}
return true;
});
if (usesFeature)
return true;
}
}
if (auto *ext = dyn_cast<ExtensionDecl>(decl)) {
if (auto *nominal = ext->getExtendedNominal())
if (usesFeatureNoncopyableGenerics(nominal))
return true;
}
SmallVector<Requirement, 2> reqs;
SmallVector<InverseRequirement, 2> inverseReqs;
if (auto *proto = dyn_cast<ProtocolDecl>(decl)) {
// We have baked-in support for Sendable not needing to state inverses
// in its inheritance clause within interface files. So, it technically is
// not using the feature with respect to the concerns of an interface file.
if (proto->isSpecificProtocol(KnownProtocolKind::Sendable))
return false;
proto->getRequirementSignature().getRequirementsWithInverses(
proto, reqs, inverseReqs);
} else if (auto *genCtx = decl->getAsGenericContext()) {
if (auto genericSig = genCtx->getGenericSignature())
genericSig->getRequirementsWithInverses(reqs, inverseReqs);
}
return !inverseReqs.empty();
}
UNINTERESTING_FEATURE(NoncopyableGenerics2)
static bool usesFeatureStructLetDestructuring(Decl *decl) {
auto sd = dyn_cast<StructDecl>(decl);
if (!sd)
return false;
for (auto member : sd->getStoredProperties()) {
if (!member->isLet())
continue;
auto init = member->getParentPattern();
if (!init)
continue;
if (!init->getSingleVar())
return true;
}
return false;
}
static bool usesFeatureNonescapableTypes(Decl *decl) {
if (decl->getAttrs().hasAttribute<NonEscapableAttr>() ||
decl->getAttrs().hasAttribute<UnsafeNonEscapableResultAttr>()) {
return true;
}
auto *fd = dyn_cast<FuncDecl>(decl);
if (fd && fd->getAttrs().getAttribute(DeclAttrKind::ResultDependsOnSelf)) {
return true;
}
auto *pd = dyn_cast<ParamDecl>(decl);
if (pd && pd->hasResultDependsOn()) {
return true;
}
return false;
}
static bool usesFeatureStaticExclusiveOnly(Decl *decl) {
return decl->getAttrs().hasAttribute<StaticExclusiveOnlyAttr>();
}
static bool usesFeatureExtractConstantsFromMembers(Decl *decl) {
return decl->getAttrs().hasAttribute<ExtractConstantsFromMembersAttr>();
}
UNINTERESTING_FEATURE(BitwiseCopyable)
UNINTERESTING_FEATURE(FixedArrays)
UNINTERESTING_FEATURE(GroupActorErrors)
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;
}
UNINTERESTING_FEATURE(DynamicActorIsolation)
UNINTERESTING_FEATURE(NonfrozenEnumExhaustivity)
UNINTERESTING_FEATURE(BorrowingSwitch)
UNINTERESTING_FEATURE(ClosureIsolation)
UNINTERESTING_FEATURE(Extern)
UNINTERESTING_FEATURE(ConsumeSelfInDeinit)
static bool usesFeatureConformanceSuppression(Decl *decl) {
auto *nominal = dyn_cast<NominalTypeDecl>(decl);
if (!nominal)
return false;
auto inherited = InheritedTypes(nominal);
for (auto index : indices(inherited.getEntries())) {
// Ensure that InheritedTypeRequest has set the isSuppressed bit if
// appropriate.
auto resolvedTy = inherited.getResolvedType(index);
(void)resolvedTy;
auto entry = inherited.getEntry(index);
if (!entry.isSuppressed())
continue;
auto ty = entry.getType();
if (!ty)
continue;
auto kp = ty->getKnownProtocol();
if (!kp)
continue;
auto rpk = getRepressibleProtocolKind(*kp);
if (!rpk)
continue;
return true;
}
return false;
}
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(MemberImportVisibility)
UNINTERESTING_FEATURE(IsolatedAny2)
UNINTERESTING_FEATURE(ObjCImplementation)
UNINTERESTING_FEATURE(CImplementation)
UNINTERESTING_FEATURE(DebugDescriptionMacro)
static bool usesFeatureGlobalActorIsolatedTypesUsability(Decl *decl) {
return false;
}
UNINTERESTING_FEATURE(ReinitializeConsumeInMultiBlockDefer)
UNINTERESTING_FEATURE(SE427NoInferenceOnExtension)
// ----------------------------------------------------------------------------
// 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;
}
|