Skip to content

Commit d98d6fd

Browse files
Merge pull request swiftlang#79672 from AnthonyLatsis/danaus-plexippus-sp
[NFC] Basic: Garden the feature infrastructure a bit
2 parents f6e9b68 + 0210872 commit d98d6fd

File tree

4 files changed

+88
-72
lines changed

4 files changed

+88
-72
lines changed

include/swift/Basic/Features.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2021 - 2023 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2021 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -78,7 +78,7 @@
7878
// A feature that's both suppressible and upcoming.
7979
// Delegates to whichever the includer defines.
8080
#ifndef SUPPRESSIBLE_UPCOMING_FEATURE
81-
# if defined(SUPPRESSIBLE_UPCOMING_FEATURE) && \
81+
# if defined(SUPPRESSIBLE_LANGUAGE_FEATURE) && \
8282
defined(UPCOMING_FEATURE)
8383
# error ambiguous defines when including Features.def
8484
# elif defined(SUPPRESSIBLE_LANGUAGE_FEATURE)

lib/Basic/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ add_swift_host_library(swiftBasic STATIC
5252
Edit.cpp
5353
EditorPlaceholder.cpp
5454
ExponentialGrowthAppendingBinaryByteStream.cpp
55+
Feature.cpp
5556
FileSystem.cpp
5657
FileTypes.cpp
5758
Fingerprint.cpp

lib/Basic/Feature.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//===--- Feature.cpp --------------------------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 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/Basic/Feature.h"
14+
#include "llvm/ADT/StringSwitch.h"
15+
#include "llvm/Support/ErrorHandling.h"
16+
17+
using namespace swift;
18+
19+
bool swift::isFeatureAvailableInProduction(Feature feature) {
20+
switch (feature) {
21+
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
22+
case Feature::FeatureName: \
23+
return true;
24+
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
25+
case Feature::FeatureName: \
26+
return AvailableInProd;
27+
#include "swift/Basic/Features.def"
28+
}
29+
llvm_unreachable("covered switch");
30+
}
31+
32+
llvm::StringRef swift::getFeatureName(Feature feature) {
33+
switch (feature) {
34+
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
35+
case Feature::FeatureName: \
36+
return #FeatureName;
37+
#include "swift/Basic/Features.def"
38+
}
39+
llvm_unreachable("covered switch");
40+
}
41+
42+
std::optional<Feature> swift::getUpcomingFeature(llvm::StringRef name) {
43+
return llvm::StringSwitch<std::optional<Feature>>(name)
44+
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description)
45+
#define UPCOMING_FEATURE(FeatureName, SENumber, Version) \
46+
.Case(#FeatureName, Feature::FeatureName)
47+
#include "swift/Basic/Features.def"
48+
.Default(std::nullopt);
49+
}
50+
51+
std::optional<Feature> swift::getExperimentalFeature(llvm::StringRef name) {
52+
return llvm::StringSwitch<std::optional<Feature>>(name)
53+
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description)
54+
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
55+
.Case(#FeatureName, Feature::FeatureName)
56+
#include "swift/Basic/Features.def"
57+
.Default(std::nullopt);
58+
}
59+
60+
std::optional<unsigned> swift::getFeatureLanguageVersion(Feature feature) {
61+
switch (feature) {
62+
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description)
63+
#define UPCOMING_FEATURE(FeatureName, SENumber, Version) \
64+
case Feature::FeatureName: \
65+
return Version;
66+
#include "swift/Basic/Features.def"
67+
default:
68+
return std::nullopt;
69+
}
70+
}
71+
72+
bool swift::includeInModuleInterface(Feature feature) {
73+
switch (feature) {
74+
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
75+
case Feature::FeatureName: \
76+
return true;
77+
#define EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE(FeatureName, \
78+
AvailableInProd) \
79+
case Feature::FeatureName: \
80+
return false;
81+
#include "swift/Basic/Features.def"
82+
}
83+
llvm_unreachable("covered switch");
84+
}

lib/Basic/LangOptions.cpp

Lines changed: 1 addition & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -620,75 +620,6 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
620620
return { false, false };
621621
}
622622

623-
llvm::StringRef swift::getFeatureName(Feature feature) {
624-
switch (feature) {
625-
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
626-
case Feature::FeatureName: \
627-
return #FeatureName;
628-
#include "swift/Basic/Features.def"
629-
}
630-
llvm_unreachable("covered switch");
631-
}
632-
633-
bool swift::isFeatureAvailableInProduction(Feature feature) {
634-
switch (feature) {
635-
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
636-
case Feature::FeatureName: \
637-
return true;
638-
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
639-
case Feature::FeatureName: return AvailableInProd;
640-
#define OPTIONAL_LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
641-
LANGUAGE_FEATURE(FeatureName, SENumber, Description)
642-
#include "swift/Basic/Features.def"
643-
}
644-
llvm_unreachable("covered switch");
645-
}
646-
647-
std::optional<Feature> swift::getUpcomingFeature(llvm::StringRef name) {
648-
return llvm::StringSwitch<std::optional<Feature>>(name)
649-
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description)
650-
#define UPCOMING_FEATURE(FeatureName, SENumber, Version) \
651-
.Case(#FeatureName, Feature::FeatureName)
652-
#include "swift/Basic/Features.def"
653-
.Default(std::nullopt);
654-
}
655-
656-
std::optional<Feature> swift::getExperimentalFeature(llvm::StringRef name) {
657-
return llvm::StringSwitch<std::optional<Feature>>(name)
658-
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description)
659-
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
660-
.Case(#FeatureName, Feature::FeatureName)
661-
#define OPTIONAL_LANGUAGE_FEATURE(FeatureName, SENumber, Description)
662-
#include "swift/Basic/Features.def"
663-
.Default(std::nullopt);
664-
}
665-
666-
std::optional<unsigned> swift::getFeatureLanguageVersion(Feature feature) {
667-
switch (feature) {
668-
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description)
669-
#define UPCOMING_FEATURE(FeatureName, SENumber, Version) \
670-
case Feature::FeatureName: return Version;
671-
#define OPTIONAL_LANGUAGE_FEATURE(FeatureName, SENumber, Description)
672-
#include "swift/Basic/Features.def"
673-
default:
674-
return std::nullopt;
675-
}
676-
}
677-
678-
bool swift::includeInModuleInterface(Feature feature) {
679-
switch (feature) {
680-
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
681-
case Feature::FeatureName: \
682-
return true;
683-
#define EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE(FeatureName, AvailableInProd) \
684-
case Feature::FeatureName: return false;
685-
#define OPTIONAL_LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
686-
LANGUAGE_FEATURE(FeatureName, SENumber, Description)
687-
#include "swift/Basic/Features.def"
688-
}
689-
llvm_unreachable("covered switch");
690-
}
691-
692623
llvm::StringRef swift::getPlaygroundOptionName(PlaygroundOption option) {
693624
switch (option) {
694625
#define PLAYGROUND_OPTION(OptionName, Description, DefaultOn, HighPerfOn) \

0 commit comments

Comments
 (0)