Skip to content

Commit 1e8622a

Browse files
committed
[Frontend][AST][IRGen] Improve availability support.
Use .def files to generate code for feature availability and runtime version to OS version mappings. Also add a min-runtime-version option that can be used to avoid problems when building on Linux and Windows where because the runtime isn't part of the OS, availability doesn't solve the problem of trying to build the compiler against an older runtime. Added some functions to IRGen to help with that problem; essentially we test *both* the deployment target availability *and* the new runtime version based availability. rdar://121522431
1 parent f3189fb commit 1e8622a

File tree

15 files changed

+441
-478
lines changed

15 files changed

+441
-478
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,11 @@ include(FetchContent)
101101
check_language(Swift)
102102
if(CMAKE_Swift_COMPILER)
103103
enable_language(Swift)
104+
set(DEFAULT_SWIFT_MIN_RUNTIME_VERSION "${CMAKE_Swift_COMPILER_VERSION}")
104105
else()
105106
message(STATUS "WARNING! Did not find a host compiler swift?! Can not build
106107
any compiler host sources written in Swift")
108+
set(DEFAULT_SWIFT_MIN_RUNTIME_VERSION)
107109
endif()
108110

109111
# A convenience pattern to match Darwin platforms. Example:
@@ -464,6 +466,10 @@ set(SWIFT_DARWIN_MODULE_ARCHS "" CACHE STRING
464466
targets on Darwin platforms. These targets are in addition to the full \
465467
library targets.")
466468

469+
set(SWIFT_MIN_RUNTIME_VERSION "${DEFAULT_SWIFT_MIN_RUNTIME_VERSION}" CACHE STRING
470+
"Specify the minimum version of the runtime that we target when building \
471+
the compiler itself. This is used on non-Darwin platforms to ensure \
472+
that it's possible to build the compiler using host tools.")
467473

468474
#
469475
# User-configurable Android specific options.

SwiftCompilerSources/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,13 @@ function(add_swift_compiler_modules_library name)
106106
"-Xcc" "-std=c++17"
107107
"-Xcc" "-DCOMPILED_WITH_SWIFT" "-Xcc" "-DSWIFT_TARGET"
108108
"-Xcc" "-UIBOutlet" "-Xcc" "-UIBAction" "-Xcc" "-UIBInspectable")
109+
109110
if (NOT BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS")
111+
if(SWIFT_MIN_RUNTIME_VERSION)
112+
list(APPEND swift_compile_options
113+
"-Xfrontend" "-min-runtime-version"
114+
"-Xfrontend" "${SWIFT_MIN_RUNTIME_VERSION}")
115+
endif()
110116
list(APPEND swift_compile_options "-Xfrontend" "-disable-implicit-string-processing-module-import")
111117
endif()
112118

include/swift/AST/ASTContext.h

Lines changed: 84 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define SWIFT_AST_ASTCONTEXT_H
1919

2020
#include "swift/AST/ASTAllocated.h"
21+
#include "swift/AST/Availability.h"
2122
#include "swift/AST/Evaluator.h"
2223
#include "swift/AST/GenericSignature.h"
2324
#include "swift/AST/Identifier.h"
@@ -876,159 +877,124 @@ class ASTContext final {
876877
addCleanup([&object]{ object.~T(); });
877878
}
878879

879-
/// Get the runtime availability of the class metadata update callback
880-
/// mechanism for the target platform.
881-
AvailabilityContext getObjCMetadataUpdateCallbackAvailability();
882-
883-
/// Get the runtime availability of the objc_getClass() hook for the target
884-
/// platform.
885-
AvailabilityContext getObjCGetClassHookAvailability();
886-
887-
/// Get the runtime availability of features introduced in the Swift 5.0
888-
/// compiler for the target platform.
889-
AvailabilityContext getSwift50Availability();
890-
891-
/// Get the runtime availability of the opaque types language feature for the
892-
/// target platform.
893-
AvailabilityContext getOpaqueTypeAvailability();
894-
895-
/// Get the runtime availability of the objc_loadClassref() entry point for
896-
/// the target platform.
897-
AvailabilityContext getObjCClassStubsAvailability();
898-
899-
/// Get the runtime availability of features introduced in the Swift 5.1
900-
/// compiler for the target platform.
901-
AvailabilityContext getSwift51Availability();
902-
903-
/// Get the runtime availability of
904-
/// swift_getTypeByMangledNameInContextInMetadataState.
905-
AvailabilityContext getTypesInAbstractMetadataStateAvailability();
906-
907-
/// Get the runtime availability of support for prespecialized generic
908-
/// metadata.
909-
AvailabilityContext getPrespecializedGenericMetadataAvailability();
910-
911-
/// Get the runtime availability of the swift_compareTypeContextDescriptors
912-
/// for the target platform.
913-
AvailabilityContext getCompareTypeContextDescriptorsAvailability();
914-
915-
/// Get the runtime availability of the
916-
/// swift_compareProtocolConformanceDescriptors entry point for the target
917-
/// platform.
918-
AvailabilityContext getCompareProtocolConformanceDescriptorsAvailability();
919-
920-
/// Get the runtime availability of support for inter-module prespecialized
921-
/// generic metadata.
922-
AvailabilityContext getIntermodulePrespecializedGenericMetadataAvailability();
923-
924-
/// Get the runtime availability of support for concurrency.
925-
AvailabilityContext getConcurrencyAvailability();
926-
927-
/// Get the runtime availability of task executors.
928-
AvailabilityContext getTaskExecutorAvailability();
929-
930-
/// Get the runtime availability of the `DiscardingTaskGroup`,
931-
/// and supporting runtime functions function
932-
AvailabilityContext getConcurrencyDiscardingTaskGroupAvailability();
933-
934-
/// Get the back-deployed availability for concurrency.
935-
AvailabilityContext getBackDeployedConcurrencyAvailability();
936-
937-
/// The the availability since when distributed actors are able to have custom
938-
/// executors.
939-
AvailabilityContext
940-
getConcurrencyDistributedActorWithCustomExecutorAvailability();
941-
942-
/// Get the runtime availability of support for differentiation.
943-
AvailabilityContext getDifferentiationAvailability();
944-
945-
/// Get the runtime availability of support for typed throws.
946-
AvailabilityContext getTypedThrowsAvailability();
947-
948-
/// Get the runtime availability of getters and setters of multi payload enum
949-
/// tag single payloads.
950-
AvailabilityContext getMultiPayloadEnumTagSinglePayload();
951-
952-
/// Get the runtime availability of the Objective-C enabled
953-
/// swift_isUniquelyReferenced functions.
954-
AvailabilityContext getObjCIsUniquelyReferencedAvailability();
880+
/// Get the availability of features introduced in the specified version
881+
/// of the Swift compiler for the target platform.
882+
AvailabilityContext getSwiftAvailability(unsigned major, unsigned minor) const;
883+
884+
// For each feature defined in FeatureAvailability, define two functions;
885+
// the latter, with the suffix RuntimeAvailabilty, is for use with
886+
// AvailabilityContext::forRuntimeTarget(), and only looks at the Swift
887+
// runtime version.
888+
#define FEATURE(N, V) \
889+
inline AvailabilityContext get##N##Availability() const { \
890+
return getSwiftAvailability V; \
891+
} \
892+
inline AvailabilityContext get##N##RuntimeAvailability() const { \
893+
return AvailabilityContext(VersionRange::allGTE(llvm::VersionTuple V)); \
894+
}
955895

956-
/// Get the runtime availability of metadata manipulation runtime functions
957-
/// for extended existential types.
958-
AvailabilityContext getParameterizedExistentialRuntimeAvailability();
896+
#include "swift/AST/FeatureAvailability.def"
959897

960-
/// Get the runtime availability of array buffers placed in constant data
961-
/// sections.
962-
AvailabilityContext getStaticReadOnlyArraysAvailability();
898+
/// Get the runtime availability of features that have been introduced in the
899+
/// Swift compiler for future versions of the target platform.
900+
AvailabilityContext getSwiftFutureAvailability() const;
963901

964-
/// Get the runtime availability of runtime functions for
965-
/// variadic generic types.
966-
AvailabilityContext getVariadicGenericTypeAvailability();
902+
/// Returns `true` if versioned availability annotations are supported for the
903+
/// target triple.
904+
bool supportsVersionedAvailability() const;
967905

968-
/// Get the runtime availability of the conformsToProtocol runtime entrypoint
969-
/// that takes a signed protocol descriptor pointer.
970-
AvailabilityContext getSignedConformsToProtocolAvailability();
906+
//===--------------------------------------------------------------------===//
907+
// Compatibility availability functions
908+
//===--------------------------------------------------------------------===//
971909

972-
/// Get the runtime availability of runtime entrypoints that take signed type
973-
/// descriptors.
974-
AvailabilityContext getSignedDescriptorAvailability();
910+
// Note: Don't add more of these version-specific availability functions;
911+
// just use getSwiftAvailability() instead.
975912

976-
/// Get the runtime availability of the swift_initRawStructMetadata entrypoint
977-
/// that fixes up the value witness table of @_rawLayout dependent types.
978-
AvailabilityContext getInitRawStructMetadataAvailability();
913+
/// Get the runtime availability of features introduced in the Swift 5.0
914+
/// compiler for the target platform.
915+
inline AvailabilityContext getSwift50Availability() const {
916+
return getSwiftAvailability(5, 0);
917+
}
979918

980-
/// Get the runtime availability of being able to use symbolic references to
981-
/// objective c protocols.
982-
AvailabilityContext getObjCSymbolicReferencesAvailability();
919+
/// Get the runtime availability of features introduced in the Swift 5.1
920+
/// compiler for the target platform.
921+
inline AvailabilityContext getSwift51Availability() const {
922+
return getSwiftAvailability(5, 1);
923+
}
983924

984925
/// Get the runtime availability of features introduced in the Swift 5.2
985926
/// compiler for the target platform.
986-
AvailabilityContext getSwift52Availability();
927+
inline AvailabilityContext getSwift52Availability() const {
928+
return getSwiftAvailability(5, 2);
929+
}
987930

988931
/// Get the runtime availability of features introduced in the Swift 5.3
989932
/// compiler for the target platform.
990-
AvailabilityContext getSwift53Availability();
933+
inline AvailabilityContext getSwift53Availability() const {
934+
return getSwiftAvailability(5, 3);
935+
}
991936

992937
/// Get the runtime availability of features introduced in the Swift 5.4
993938
/// compiler for the target platform.
994-
AvailabilityContext getSwift54Availability();
939+
inline AvailabilityContext getSwift54Availability() const {
940+
return getSwiftAvailability(5, 4);
941+
}
995942

996943
/// Get the runtime availability of features introduced in the Swift 5.5
997944
/// compiler for the target platform.
998-
AvailabilityContext getSwift55Availability();
945+
inline AvailabilityContext getSwift55Availability() const {
946+
return getSwiftAvailability(5, 5);
947+
}
999948

1000949
/// Get the runtime availability of features introduced in the Swift 5.6
1001950
/// compiler for the target platform.
1002-
AvailabilityContext getSwift56Availability();
951+
inline AvailabilityContext getSwift56Availability() const {
952+
return getSwiftAvailability(5, 6);
953+
}
1003954

1004955
/// Get the runtime availability of features introduced in the Swift 5.7
1005956
/// compiler for the target platform.
1006-
AvailabilityContext getSwift57Availability();
957+
inline AvailabilityContext getSwift57Availability() const {
958+
return getSwiftAvailability(5, 7);
959+
}
1007960

1008961
/// Get the runtime availability of features introduced in the Swift 5.8
1009962
/// compiler for the target platform.
1010-
AvailabilityContext getSwift58Availability();
963+
inline AvailabilityContext getSwift58Availability() const {
964+
return getSwiftAvailability(5, 8);
965+
}
1011966

1012967
/// Get the runtime availability of features introduced in the Swift 5.9
1013968
/// compiler for the target platform.
1014-
AvailabilityContext getSwift59Availability();
969+
inline AvailabilityContext getSwift59Availability() const {
970+
return getSwiftAvailability(5, 9);
971+
}
1015972

1016-
/// Get the runtime availability of features introduced in the Swift 5.9
973+
/// Get the runtime availability of features introduced in the Swift 5.10
1017974
/// compiler for the target platform.
1018-
AvailabilityContext getSwift511Availability();
1019-
1020-
// Note: Update this function if you add a new getSwiftXYAvailability above.
1021-
/// Get the runtime availability for a particular version of Swift (5.0+).
1022-
AvailabilityContext
1023-
getSwift5PlusAvailability(llvm::VersionTuple swiftVersion);
975+
inline AvailabilityContext getSwift510Availability() const {
976+
return getSwiftAvailability(5, 10);
977+
}
1024978

1025-
/// Get the runtime availability of features that have been introduced in the
1026-
/// Swift compiler for future versions of the target platform.
1027-
AvailabilityContext getSwiftFutureAvailability();
979+
/// Get the runtime availability of features introduced in the Swift 5.11
980+
/// compiler for the target platform.
981+
inline AvailabilityContext getSwift511Availability() const {
982+
return getSwiftAvailability(5, 11);
983+
}
1028984

1029-
/// Returns `true` if versioned availability annotations are supported for the
1030-
/// target triple.
1031-
bool supportsVersionedAvailability() const;
985+
/// Get the runtime availability for a particular version of Swift (5.0+).
986+
inline AvailabilityContext
987+
getSwift5PlusAvailability(llvm::VersionTuple swiftVersion) const {
988+
return getSwiftAvailability(swiftVersion.getMajor(),
989+
*swiftVersion.getMinor());
990+
}
991+
992+
/// Get the runtime availability of getters and setters of multi payload enum
993+
/// tag single payloads.
994+
inline AvailabilityContext getMultiPayloadEnumTagSinglePayload() const {
995+
// This didn't fit the pattern, so needed renaming
996+
return getMultiPayloadEnumTagSinglePayloadAvailability();
997+
}
1032998

1033999
//===--------------------------------------------------------------------===//
10341000
// Diagnostics Helper functions

include/swift/AST/Availability.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,15 @@ class AvailabilityContext {
238238

239239
/// Creates a context that imposes the constraints of the ASTContext's
240240
/// deployment target.
241-
static AvailabilityContext forDeploymentTarget(ASTContext &Ctx);
241+
static AvailabilityContext forDeploymentTarget(const ASTContext &Ctx);
242242

243243
/// Creates a context that imposes the constraints of the ASTContext's
244244
/// inlining target (i.e. minimum inlining version).
245-
static AvailabilityContext forInliningTarget(ASTContext &Ctx);
245+
static AvailabilityContext forInliningTarget(const ASTContext &Ctx);
246+
247+
/// Creates a context that imposes the constraints of the ASTContext's
248+
/// minimum runtime version.
249+
static AvailabilityContext forRuntimeTarget(const ASTContext &Ctx);
246250

247251
/// Creates a context that imposes no constraints.
248252
///
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//===--- FeatureAvailability.def - Availability Metaprogramming -*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 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+
// This file defines macros used for macro-metaprogramming with feature
14+
// availability.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
18+
/// FEATURE(N, V)
19+
/// N - The name of the feature (in UpperCamelCase).
20+
/// V - The Swift version number, as a tuple, or FUTURE.
21+
#ifndef FEATURE
22+
#define FEATURE(N, V)
23+
#endif
24+
25+
/// FUTURE
26+
///
27+
/// The version to use for future support.
28+
#ifndef FUTURE
29+
#define FUTURE (99, 99)
30+
#endif
31+
32+
FEATURE(ObjCMetadataUpdateCallback, (5, 0))
33+
FEATURE(ObjCGetClassHook, (5, 0))
34+
35+
FEATURE(OpaqueType, (5, 1))
36+
FEATURE(ObjCClassStubs, (5, 1))
37+
FEATURE(BackDeployedConcurrency, (5, 1))
38+
39+
FEATURE(TypesInAbstractMetadataState, (5, 2))
40+
41+
FEATURE(PrespecializedGenericMetadata, (5, 4))
42+
FEATURE(CompareTypeContextDescriptors, (5, 4))
43+
FEATURE(CompareProtocolConformanceDescriptors, (5, 4))
44+
FEATURE(IntermodulePrespecializedGenericMetadata, (5, 4))
45+
46+
FEATURE(Concurrency, (5, 5))
47+
48+
FEATURE(MultiPayloadEnumTagSinglePayload, (5, 6))
49+
FEATURE(ObjCIsUniquelyReferenced, (5, 6))
50+
51+
FEATURE(ParameterizedExistentialRuntime, (5, 7))
52+
53+
FEATURE(VariadicGenericType, (5, 9))
54+
FEATURE(SignedConformsToProtocol, (5, 9))
55+
FEATURE(ConcurrencyDiscardingTaskGroup, (5, 9))
56+
FEATURE(ConcurrencyDistributedActorWithCustomExecutor, (5, 9))
57+
FEATURE(SignedDescriptor, (5, 9))
58+
59+
FEATURE(ObjCSymbolicReferences, (5, 11))
60+
FEATURE(TypedThrows, (5, 11))
61+
FEATURE(StaticReadOnlyArrays, (5, 11))
62+
63+
FEATURE(TaskExecutor, FUTURE)
64+
FEATURE(Differentiation, FUTURE)
65+
FEATURE(InitRawStructMetadata, FUTURE)
66+
67+
#undef FEATURE
68+
#undef FUTURE

0 commit comments

Comments
 (0)