|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024-2026 Apple Inc. and the Swift.org project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of Swift.org project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +/// Minimum access level a declaration must have to be considered for extraction. |
| 16 | +/// |
| 17 | +/// Language-neutral counterpart to a configuration's access-level setting. The |
| 18 | +/// concrete `Configuration` types in language layers (swift-java, swift-python) |
| 19 | +/// map their own enums onto this. |
| 20 | +public enum AccessLevelMode: String, Sendable { |
| 21 | + case `public` |
| 22 | + case `package` |
| 23 | + case `internal` |
| 24 | +} |
| 25 | + |
| 26 | +/// The configuration surface required by the language-neutral `SwiftExtract` |
| 27 | +/// analysis layer. |
| 28 | +/// |
| 29 | +/// `SwiftExtract` deliberately does NOT depend on any language-specific |
| 30 | +/// configuration module. Instead, each language layer makes its own |
| 31 | +/// `Configuration` type conform to this protocol, mapping its settings onto the |
| 32 | +/// neutral surface below. This keeps the analysis layer reusable across targets |
| 33 | +/// (e.g. Java/JNI/FFM, Python) without pulling target-specific config types into |
| 34 | +/// `SwiftExtract`. |
| 35 | +/// |
| 36 | +/// The two enum-typed members use `swiftExtract`-prefixed names so a conforming |
| 37 | +/// type can keep its own, differently-typed `logLevel` / |
| 38 | +/// `effectiveMinimumInputAccessLevelMode` members without a name collision. |
| 39 | +public protocol SwiftExtractConfiguration { |
| 40 | + /// Name of the Swift module being analyzed. |
| 41 | + var swiftModule: String? { get } |
| 42 | + |
| 43 | + /// Optional path to a JSON `StaticBuildConfiguration` used to resolve `#if`. |
| 44 | + var staticBuildConfigurationFile: String? { get } |
| 45 | + |
| 46 | + /// Glob patterns selecting which Swift files/types to extract. |
| 47 | + var swiftFilterInclude: [String]? { get } |
| 48 | + |
| 49 | + /// Glob patterns excluding Swift files/types from extraction. |
| 50 | + var swiftFilterExclude: [String]? { get } |
| 51 | + |
| 52 | + /// Stub declarations for imported modules whose source is unavailable to the |
| 53 | + /// analyzer. Keyed by module name; values are Swift declaration strings parsed |
| 54 | + /// as if they belonged to that module. |
| 55 | + var importedModuleStubs: [String: [String]]? { get } |
| 56 | + |
| 57 | + /// Minimum access level required for a declaration to be extracted. |
| 58 | + var swiftExtractAccessLevel: AccessLevelMode { get } |
| 59 | + |
| 60 | + /// Whether operator declarations (e.g. `static func + (…)`) should be |
| 61 | + /// extracted as ordinary `.function`s. Most targets (e.g. Java) cannot express |
| 62 | + /// Swift operators and leave this `false`; targets that map operators to |
| 63 | + /// language constructs (e.g. Python dunder methods) set it `true` and |
| 64 | + /// recognize the operator functions in a post-analysis pass. |
| 65 | + var extractsOperators: Bool { get } |
| 66 | + |
| 67 | + /// Verbosity for the analyzer's logger; `nil` falls back to `.info`. |
| 68 | + var swiftExtractLogLevel: Logger.Level? { get } |
| 69 | + |
| 70 | + /// Whether the given module name has stub declarations configured. |
| 71 | + func hasImportedModuleStub(moduleOfNominal moduleName: String) -> Bool |
| 72 | +} |
| 73 | + |
| 74 | +extension SwiftExtractConfiguration { |
| 75 | + public var extractsOperators: Bool { false } |
| 76 | + |
| 77 | + public func hasImportedModuleStub(moduleOfNominal moduleName: String) -> Bool { |
| 78 | + importedModuleStubs?.keys.contains(moduleName) ?? false |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/// A minimal, self-contained `SwiftExtractConfiguration` for callers that only |
| 83 | +/// need analysis (tests, tools) and don't have a richer language-specific |
| 84 | +/// configuration to supply. |
| 85 | +public struct DefaultSwiftExtractConfiguration: SwiftExtractConfiguration { |
| 86 | + public var swiftModule: String? |
| 87 | + public var staticBuildConfigurationFile: String? |
| 88 | + public var swiftFilterInclude: [String]? |
| 89 | + public var swiftFilterExclude: [String]? |
| 90 | + public var importedModuleStubs: [String: [String]]? |
| 91 | + public var swiftExtractAccessLevel: AccessLevelMode |
| 92 | + public var swiftExtractLogLevel: Logger.Level? |
| 93 | + public var extractsOperators: Bool |
| 94 | + |
| 95 | + public init( |
| 96 | + swiftModule: String? = nil, |
| 97 | + accessLevel: AccessLevelMode = .public, |
| 98 | + logLevel: Logger.Level? = nil, |
| 99 | + extractsOperators: Bool = false, |
| 100 | + staticBuildConfigurationFile: String? = nil, |
| 101 | + swiftFilterInclude: [String]? = nil, |
| 102 | + swiftFilterExclude: [String]? = nil, |
| 103 | + importedModuleStubs: [String: [String]]? = nil |
| 104 | + ) { |
| 105 | + self.swiftModule = swiftModule |
| 106 | + self.swiftExtractAccessLevel = accessLevel |
| 107 | + self.swiftExtractLogLevel = logLevel |
| 108 | + self.extractsOperators = extractsOperators |
| 109 | + self.staticBuildConfigurationFile = staticBuildConfigurationFile |
| 110 | + self.swiftFilterInclude = swiftFilterInclude |
| 111 | + self.swiftFilterExclude = swiftFilterExclude |
| 112 | + self.importedModuleStubs = importedModuleStubs |
| 113 | + } |
| 114 | +} |
0 commit comments