Skip to content

[NFC] Basic: Garden the feature infrastructure a bit #79672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/swift/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021 - 2023 Apple Inc. and the Swift project authors
// Copyright (c) 2021 - 2025 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
Expand Down Expand Up @@ -78,7 +78,7 @@
// A feature that's both suppressible and upcoming.
// Delegates to whichever the includer defines.
#ifndef SUPPRESSIBLE_UPCOMING_FEATURE
# if defined(SUPPRESSIBLE_UPCOMING_FEATURE) && \
# if defined(SUPPRESSIBLE_LANGUAGE_FEATURE) && \
defined(UPCOMING_FEATURE)
# error ambiguous defines when including Features.def
# elif defined(SUPPRESSIBLE_LANGUAGE_FEATURE)
Expand Down
1 change: 1 addition & 0 deletions lib/Basic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ add_swift_host_library(swiftBasic STATIC
Edit.cpp
EditorPlaceholder.cpp
ExponentialGrowthAppendingBinaryByteStream.cpp
Feature.cpp
FileSystem.cpp
FileTypes.cpp
Fingerprint.cpp
Expand Down
84 changes: 84 additions & 0 deletions lib/Basic/Feature.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//===--- Feature.cpp --------------------------------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 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/Basic/Feature.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/ErrorHandling.h"

using namespace swift;

bool swift::isFeatureAvailableInProduction(Feature feature) {
switch (feature) {
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
case Feature::FeatureName: \
return true;
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
case Feature::FeatureName: \
return AvailableInProd;
#include "swift/Basic/Features.def"
}
llvm_unreachable("covered switch");
}

llvm::StringRef swift::getFeatureName(Feature feature) {
switch (feature) {
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
case Feature::FeatureName: \
return #FeatureName;
#include "swift/Basic/Features.def"
}
llvm_unreachable("covered switch");
}

std::optional<Feature> swift::getUpcomingFeature(llvm::StringRef name) {
return llvm::StringSwitch<std::optional<Feature>>(name)
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description)
#define UPCOMING_FEATURE(FeatureName, SENumber, Version) \
.Case(#FeatureName, Feature::FeatureName)
#include "swift/Basic/Features.def"
.Default(std::nullopt);
}

std::optional<Feature> swift::getExperimentalFeature(llvm::StringRef name) {
return llvm::StringSwitch<std::optional<Feature>>(name)
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description)
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
.Case(#FeatureName, Feature::FeatureName)
#include "swift/Basic/Features.def"
.Default(std::nullopt);
}

std::optional<unsigned> swift::getFeatureLanguageVersion(Feature feature) {
switch (feature) {
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description)
#define UPCOMING_FEATURE(FeatureName, SENumber, Version) \
case Feature::FeatureName: \
return Version;
#include "swift/Basic/Features.def"
default:
return std::nullopt;
}
}

bool swift::includeInModuleInterface(Feature feature) {
switch (feature) {
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
case Feature::FeatureName: \
return true;
#define EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE(FeatureName, \
AvailableInProd) \
case Feature::FeatureName: \
return false;
#include "swift/Basic/Features.def"
}
llvm_unreachable("covered switch");
}
71 changes: 1 addition & 70 deletions lib/Basic/LangOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2025 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
Expand Down Expand Up @@ -620,75 +620,6 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
return { false, false };
}

llvm::StringRef swift::getFeatureName(Feature feature) {
switch (feature) {
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
case Feature::FeatureName: \
return #FeatureName;
#include "swift/Basic/Features.def"
}
llvm_unreachable("covered switch");
}

bool swift::isFeatureAvailableInProduction(Feature feature) {
switch (feature) {
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
case Feature::FeatureName: \
return true;
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
case Feature::FeatureName: return AvailableInProd;
#define OPTIONAL_LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
LANGUAGE_FEATURE(FeatureName, SENumber, Description)
#include "swift/Basic/Features.def"
}
llvm_unreachable("covered switch");
}

std::optional<Feature> swift::getUpcomingFeature(llvm::StringRef name) {
return llvm::StringSwitch<std::optional<Feature>>(name)
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description)
#define UPCOMING_FEATURE(FeatureName, SENumber, Version) \
.Case(#FeatureName, Feature::FeatureName)
#include "swift/Basic/Features.def"
.Default(std::nullopt);
}

std::optional<Feature> swift::getExperimentalFeature(llvm::StringRef name) {
return llvm::StringSwitch<std::optional<Feature>>(name)
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description)
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
.Case(#FeatureName, Feature::FeatureName)
#define OPTIONAL_LANGUAGE_FEATURE(FeatureName, SENumber, Description)
#include "swift/Basic/Features.def"
.Default(std::nullopt);
}

std::optional<unsigned> swift::getFeatureLanguageVersion(Feature feature) {
switch (feature) {
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description)
#define UPCOMING_FEATURE(FeatureName, SENumber, Version) \
case Feature::FeatureName: return Version;
#define OPTIONAL_LANGUAGE_FEATURE(FeatureName, SENumber, Description)
#include "swift/Basic/Features.def"
default:
return std::nullopt;
}
}

bool swift::includeInModuleInterface(Feature feature) {
switch (feature) {
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
case Feature::FeatureName: \
return true;
#define EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE(FeatureName, AvailableInProd) \
case Feature::FeatureName: return false;
#define OPTIONAL_LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
LANGUAGE_FEATURE(FeatureName, SENumber, Description)
#include "swift/Basic/Features.def"
}
llvm_unreachable("covered switch");
}

llvm::StringRef swift::getPlaygroundOptionName(PlaygroundOption option) {
switch (option) {
#define PLAYGROUND_OPTION(OptionName, Description, DefaultOn, HighPerfOn) \
Expand Down