Skip to content

Commit dd78cd6

Browse files
committed
ASTBridging: Bridge swift::PlatformKind directly
1 parent de46b95 commit dd78cd6

15 files changed

+165
-162
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "swift/AST/DiagnosticList.h"
2727
#include "swift/AST/GenericTypeParamKind.h"
2828
#include "swift/AST/LayoutConstraintKind.h"
29+
#include "swift/AST/PlatformKind.h"
2930
#include "swift/Basic/BasicBridging.h"
3031

3132
#ifdef USED_IN_CPP_SOURCE
@@ -67,7 +68,6 @@ class MacroIntroducedDeclName;
6768
enum class MacroIntroducedDeclNameKind;
6869
enum class ParamSpecifier : uint8_t;
6970
class ParsedAutoDiffParameter;
70-
enum class PlatformKind : uint8_t;
7171
class ProtocolConformanceRef;
7272
class RegexLiteralPatternFeature;
7373
class RegexLiteralPatternFeatureKind;
@@ -88,7 +88,6 @@ struct BridgedSubstitutionMap;
8888
struct BridgedGenericSignature;
8989
struct BridgedConformance;
9090
class BridgedParameterList;
91-
enum BridgedPlatformKind : size_t;
9291

9392
// Forward declare the underlying AST node type for each wrapper.
9493
namespace swift {
@@ -683,20 +682,14 @@ BridgedClosureExpr_asDeclContext(BridgedClosureExpr cClosure);
683682
// MARK: Availability
684683
//===----------------------------------------------------------------------===//
685684

686-
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedPlatformKind : size_t {
687-
BridgedPlatformKind_None,
688-
#define AVAILABILITY_PLATFORM(X, PrettyName) BridgedPlatformKind_##X,
689-
#include "swift/AST/PlatformKinds.def"
690-
};
691-
692-
SWIFT_NAME("BridgedPlatformKind.init(from:)")
693-
BridgedPlatformKind BridgedPlatformKind_fromString(BridgedStringRef cStr);
685+
BRIDGED_OPTIONAL(swift::PlatformKind, PlatformKind)
694686

695-
SWIFT_NAME("BridgedPlatformKind.init(from:)")
696-
BridgedPlatformKind
697-
BridgedPlatformKind_fromIdentifier(BridgedIdentifier cIdent);
687+
SWIFT_NAME("BridgedOptionalPlatformKind.init(from:)")
688+
BridgedOptionalPlatformKind PlatformKind_fromString(BridgedStringRef cStr);
698689

699-
swift::PlatformKind unbridge(BridgedPlatformKind cPlatform);
690+
SWIFT_NAME("BridgedOptionalPlatformKind.init(from:)")
691+
BridgedOptionalPlatformKind
692+
PlatformKind_fromIdentifier(BridgedIdentifier cIdent);
700693

701694
SWIFT_NAME("BridgedAvailabilityMacroMap.has(self:name:)")
702695
bool BridgedAvailabilityMacroMap_hasName(BridgedAvailabilityMacroMap map,
@@ -931,7 +924,7 @@ SWIFT_NAME(
931924
"BridgedBackDeployedAttr.createParsed(_:atLoc:range:platform:version:)")
932925
BridgedBackDeployedAttr BridgedBackDeployedAttr_createParsed(
933926
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
934-
BridgedSourceRange cRange, BridgedPlatformKind cPlatform,
927+
BridgedSourceRange cRange, swift::PlatformKind platform,
935928
BridgedVersionTuple cVersion);
936929

937930
SWIFT_NAME("BridgedCDeclAttr.createParsed(_:atLoc:range:name:)")
@@ -1161,7 +1154,7 @@ SWIFT_NAME("BridgedOriginallyDefinedInAttr.createParsed(_:atLoc:range:"
11611154
BridgedOriginallyDefinedInAttr BridgedOriginallyDefinedInAttr_createParsed(
11621155
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
11631156
BridgedSourceRange cRange, BridgedStringRef cModuleName,
1164-
BridgedPlatformKind cPlatform, BridgedVersionTuple cVersion);
1157+
swift::PlatformKind platform, BridgedVersionTuple cVersion);
11651158

11661159
SWIFT_NAME("BridgedStorageRestrictionsAttr.createParsed(_:atLoc:range:"
11671160
"initializes:accesses:)")

include/swift/AST/Attr.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include "swift/AST/LifetimeDependence.h"
2929
#include "swift/AST/MacroDeclaration.h"
3030
#include "swift/AST/Ownership.h"
31-
#include "swift/AST/PlatformKind.h"
31+
#include "swift/AST/PlatformKindUtils.h"
3232
#include "swift/AST/StorageImpl.h"
3333
#include "swift/Basic/Debug.h"
3434
#include "swift/Basic/EnumTraits.h"
@@ -41,10 +41,10 @@
4141
#include "swift/Basic/SourceLoc.h"
4242
#include "swift/Basic/UUID.h"
4343
#include "swift/Basic/Version.h"
44-
#include "llvm/ADT/bit.h"
4544
#include "llvm/ADT/DenseMapInfo.h"
4645
#include "llvm/ADT/SmallVector.h"
4746
#include "llvm/ADT/StringRef.h"
47+
#include "llvm/ADT/bit.h"
4848
#include "llvm/ADT/iterator_range.h"
4949
#include "llvm/Support/ErrorHandling.h"
5050
#include "llvm/Support/TrailingObjects.h"

include/swift/AST/AvailabilityConstraint.h

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) 2024 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2024 - 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
@@ -20,7 +20,7 @@
2020
#include "swift/AST/Attr.h"
2121
#include "swift/AST/AvailabilityDomain.h"
2222
#include "swift/AST/AvailabilityRange.h"
23-
#include "swift/AST/PlatformKind.h"
23+
#include "swift/AST/PlatformKindUtils.h"
2424
#include "swift/Basic/LLVM.h"
2525
#include "swift/Basic/OptionSet.h"
2626

include/swift/AST/AvailabilityContext.h

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) 2024 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2024 - 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
@@ -20,7 +20,7 @@
2020

2121
#include "swift/AST/AvailabilityDomain.h"
2222
#include "swift/AST/AvailabilityRange.h"
23-
#include "swift/AST/PlatformKind.h"
23+
#include "swift/AST/PlatformKindUtils.h"
2424
#include "swift/Basic/Debug.h"
2525
#include "swift/Basic/LLVM.h"
2626
#include <optional>

include/swift/AST/AvailabilityDomain.h

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) 2024 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2024 - 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
@@ -21,7 +21,7 @@
2121
#include "swift/AST/ASTAllocated.h"
2222
#include "swift/AST/AvailabilityRange.h"
2323
#include "swift/AST/Identifier.h"
24-
#include "swift/AST/PlatformKind.h"
24+
#include "swift/AST/PlatformKindUtils.h"
2525
#include "swift/Basic/Assertions.h"
2626
#include "swift/Basic/LLVM.h"
2727
#include "swift/Basic/SourceLoc.h"

include/swift/AST/AvailabilitySpec.h

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) 2014 - 2017 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
@@ -20,7 +20,7 @@
2020
#include "swift/AST/ASTAllocated.h"
2121
#include "swift/AST/AvailabilityDomain.h"
2222
#include "swift/AST/Identifier.h"
23-
#include "swift/AST/PlatformKind.h"
23+
#include "swift/AST/PlatformKindUtils.h"
2424
#include "swift/Basic/STLExtras.h"
2525
#include "swift/Basic/SourceLoc.h"
2626
#include "llvm/ADT/DenseMap.h"

include/swift/AST/PlatformKind.h

Lines changed: 11 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,36 @@
1-
//===--- PlatformKind.h - Swift Language Platform Kinds ---------*- C++ -*-===//
1+
//===--- AST/PlatformKind.h - Swift Language Platform Kinds -----*- C++ -*-===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 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
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12-
//
13-
// This file defines the platform kinds for API availability.
14-
//
12+
///
13+
/// This file defines the platform kinds for API availability.
14+
///
1515
//===----------------------------------------------------------------------===//
1616

1717
#ifndef SWIFT_AST_PLATFORM_KIND_H
1818
#define SWIFT_AST_PLATFORM_KIND_H
1919

20-
#include "swift/Basic/LLVM.h"
21-
#include "swift/Config.h"
22-
#include "llvm/ADT/StringRef.h"
23-
#include "llvm/Support/VersionTuple.h"
24-
#include "llvm/TargetParser/Triple.h"
25-
#include <optional>
20+
/// This header is included in a bridging header. Be *very* careful with what
21+
/// you include here! See include caveats in `ASTBridging.h`.
22+
#include "swift/Basic/SwiftBridging.h"
23+
#include <stdint.h>
2624

2725
namespace swift {
2826

29-
class LangOptions;
30-
3127
/// Available platforms for the availability attribute.
32-
enum class PlatformKind: uint8_t {
28+
enum class ENUM_EXTENSIBILITY_ATTR(closed) PlatformKind : uint8_t {
3329
none,
3430
#define AVAILABILITY_PLATFORM(X, PrettyName) X,
3531
#include "swift/AST/PlatformKinds.def"
3632
};
3733

38-
/// Returns the short string representing the platform, suitable for
39-
/// use in availability specifications (e.g., "OSX").
40-
StringRef platformString(PlatformKind platform);
41-
42-
/// Returns the platform kind corresponding to the passed-in short platform name
43-
/// or None if such a platform kind does not exist.
44-
std::optional<PlatformKind> platformFromString(StringRef Name);
45-
46-
/// Safely converts the given unsigned value to a valid \c PlatformKind value or
47-
/// \c nullopt otherwise.
48-
std::optional<PlatformKind> platformFromUnsigned(unsigned value);
49-
50-
/// Returns a valid platform string that is closest to the candidate string
51-
/// based on edit distance. Returns \c None if the closest valid platform
52-
/// distance is not within a minimum threshold.
53-
std::optional<StringRef> closestCorrectedPlatformString(StringRef candidate);
54-
55-
/// Returns a human-readable version of the platform name as a string, suitable
56-
/// for emission in diagnostics (e.g., "macOS").
57-
StringRef prettyPlatformString(PlatformKind platform);
58-
59-
/// Returns the base platform for an application-extension platform. For example
60-
/// `iOS` would be returned for `iOSApplicationExtension`. Returns `None` for
61-
/// platforms which are not application extension platforms.
62-
std::optional<PlatformKind>
63-
basePlatformForExtensionPlatform(PlatformKind Platform);
64-
65-
/// Returns true if \p Platform represents and application extension platform,
66-
/// e.g. `iOSApplicationExtension`.
67-
inline bool isApplicationExtensionPlatform(PlatformKind Platform) {
68-
return basePlatformForExtensionPlatform(Platform).has_value();
69-
}
70-
71-
/// Returns whether the passed-in platform is active, given the language
72-
/// options. A platform is active if either it is the target platform or its
73-
/// AppExtension variant is the target platform. For example, OS X is
74-
/// considered active when the target operating system is OS X and app extension
75-
/// restrictions are enabled, but OSXApplicationExtension is not considered
76-
/// active when the target platform is OS X and app extension restrictions are
77-
/// disabled. PlatformKind::none is always considered active.
78-
/// If ForTargetVariant is true then for zippered builds the target-variant
79-
/// triple will be used rather than the target to determine whether the
80-
/// platform is active.
81-
bool isPlatformActive(PlatformKind Platform, const LangOptions &LangOpts,
82-
bool ForTargetVariant = false, bool ForRuntimeQuery = false);
83-
84-
/// Returns the target platform for the given language options.
85-
PlatformKind targetPlatform(const LangOptions &LangOpts);
86-
87-
/// Returns the target variant platform for the given language options.
88-
PlatformKind targetVariantPlatform(const LangOptions &LangOpts);
89-
90-
/// Returns true when availability attributes from the "parent" platform
91-
/// should also apply to the "child" platform for declarations without
92-
/// an explicit attribute for the child.
93-
bool inheritsAvailabilityFromPlatform(PlatformKind Child, PlatformKind Parent);
94-
95-
/// Returns the LLVM triple OS type for the given platform, if there is one.
96-
std::optional<llvm::Triple::OSType>
97-
tripleOSTypeForPlatform(PlatformKind platform);
98-
99-
llvm::VersionTuple canonicalizePlatformVersion(
100-
PlatformKind platform, const llvm::VersionTuple &version);
101-
102-
/// Returns true if \p Platform should be considered to be SPI and therefore not
103-
/// printed in public `.swiftinterface` files, for example.
104-
bool isPlatformSPI(PlatformKind Platform);
105-
10634
} // end namespace swift
10735

108-
#endif
36+
#endif // SWIFT_AST_PLATFORM_KIND_H

include/swift/AST/PlatformKindUtils.h

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
//===--- AST/PlatformKindUtils.h --------------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 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+
/// This file declares operations for working with `PlatformKind`.
14+
///
15+
//===----------------------------------------------------------------------===//
16+
17+
#ifndef SWIFT_AST_PLATFORM_KIND_UTILS_H
18+
#define SWIFT_AST_PLATFORM_KIND_UTILS_H
19+
20+
#include "swift/AST/PlatformKind.h"
21+
#include "swift/Basic/LLVM.h"
22+
#include "swift/Config.h"
23+
#include "llvm/Support/VersionTuple.h"
24+
#include "llvm/TargetParser/Triple.h"
25+
#include <optional>
26+
27+
namespace swift {
28+
29+
class LangOptions;
30+
31+
/// Returns the short string representing the platform, suitable for
32+
/// use in availability specifications (e.g., "OSX").
33+
StringRef platformString(PlatformKind platform);
34+
35+
/// Returns the platform kind corresponding to the passed-in short platform name
36+
/// or None if such a platform kind does not exist.
37+
std::optional<PlatformKind> platformFromString(StringRef Name);
38+
39+
/// Safely converts the given unsigned value to a valid \c PlatformKind value or
40+
/// \c nullopt otherwise.
41+
std::optional<PlatformKind> platformFromUnsigned(unsigned value);
42+
43+
/// Returns a valid platform string that is closest to the candidate string
44+
/// based on edit distance. Returns \c None if the closest valid platform
45+
/// distance is not within a minimum threshold.
46+
std::optional<StringRef> closestCorrectedPlatformString(StringRef candidate);
47+
48+
/// Returns a human-readable version of the platform name as a string, suitable
49+
/// for emission in diagnostics (e.g., "macOS").
50+
StringRef prettyPlatformString(PlatformKind platform);
51+
52+
/// Returns the base platform for an application-extension platform. For example
53+
/// `iOS` would be returned for `iOSApplicationExtension`. Returns `None` for
54+
/// platforms which are not application extension platforms.
55+
std::optional<PlatformKind>
56+
basePlatformForExtensionPlatform(PlatformKind Platform);
57+
58+
/// Returns true if \p Platform represents and application extension platform,
59+
/// e.g. `iOSApplicationExtension`.
60+
inline bool isApplicationExtensionPlatform(PlatformKind Platform) {
61+
return basePlatformForExtensionPlatform(Platform).has_value();
62+
}
63+
64+
/// Returns whether the passed-in platform is active, given the language
65+
/// options. A platform is active if either it is the target platform or its
66+
/// AppExtension variant is the target platform. For example, OS X is
67+
/// considered active when the target operating system is OS X and app extension
68+
/// restrictions are enabled, but OSXApplicationExtension is not considered
69+
/// active when the target platform is OS X and app extension restrictions are
70+
/// disabled. PlatformKind::none is always considered active.
71+
/// If ForTargetVariant is true then for zippered builds the target-variant
72+
/// triple will be used rather than the target to determine whether the
73+
/// platform is active.
74+
bool isPlatformActive(PlatformKind Platform, const LangOptions &LangOpts,
75+
bool ForTargetVariant = false, bool ForRuntimeQuery = false);
76+
77+
/// Returns the target platform for the given language options.
78+
PlatformKind targetPlatform(const LangOptions &LangOpts);
79+
80+
/// Returns the target variant platform for the given language options.
81+
PlatformKind targetVariantPlatform(const LangOptions &LangOpts);
82+
83+
/// Returns true when availability attributes from the "parent" platform
84+
/// should also apply to the "child" platform for declarations without
85+
/// an explicit attribute for the child.
86+
bool inheritsAvailabilityFromPlatform(PlatformKind Child, PlatformKind Parent);
87+
88+
/// Returns the LLVM triple OS type for the given platform, if there is one.
89+
std::optional<llvm::Triple::OSType>
90+
tripleOSTypeForPlatform(PlatformKind platform);
91+
92+
llvm::VersionTuple canonicalizePlatformVersion(
93+
PlatformKind platform, const llvm::VersionTuple &version);
94+
95+
/// Returns true if \p Platform should be considered to be SPI and therefore not
96+
/// printed in public `.swiftinterface` files, for example.
97+
bool isPlatformSPI(PlatformKind Platform);
98+
99+
} // end namespace swift
100+
101+
#endif // SWIFT_AST_PLATFORM_KIND_UTILS_H

0 commit comments

Comments
 (0)