|
| 1 | +//===--- EmbeddedSupport.swift --------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2022-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 | +import ASTBridging |
| 14 | +import BasicBridging |
| 15 | +import SwiftIfConfig |
| 16 | +import SwiftSyntax |
| 17 | + |
| 18 | +extension ExportedSourceFile { |
| 19 | + /// Return the configured regions for this source file. |
| 20 | + mutating func configuredRegionsIfEmbedded(astContext: BridgedASTContext) -> ConfiguredRegions { |
| 21 | + if let _configuredRegionsIfEmbedded { |
| 22 | + return _configuredRegionsIfEmbedded |
| 23 | + } |
| 24 | + |
| 25 | + // Ensure that we've already computed the configured regions, which can |
| 26 | + // emit diagnostics. |
| 27 | + _ = configuredRegions(astContext: astContext) |
| 28 | + |
| 29 | + let configuration = EmbeddedBuildConfiguration( |
| 30 | + ctx: astContext, |
| 31 | + sourceBuffer: buffer |
| 32 | + ) |
| 33 | + |
| 34 | + let regions = syntax.configuredRegions(in: configuration) |
| 35 | + _configuredRegionsIfEmbedded = regions |
| 36 | + return regions |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/// Determine whether the given source location is active when this source |
| 41 | +/// file is compiled in Embedded Swift. |
| 42 | +@_cdecl("swift_ASTGen_activeInEmbeddedSwift") |
| 43 | +public func activeInEmbeddedSwift( |
| 44 | + astContext: BridgedASTContext, |
| 45 | + sourceFilePtr: UnsafeMutableRawPointer, |
| 46 | + at location: SourceLoc |
| 47 | +) -> Int { |
| 48 | + guard let token = findToken( |
| 49 | + in: UnsafeRawPointer(sourceFilePtr), |
| 50 | + at: location.raw?.assumingMemoryBound(to: UInt8.self) |
| 51 | + ) else { |
| 52 | + return 0 |
| 53 | + } |
| 54 | + |
| 55 | + let sourceFile = sourceFilePtr.assumingMemoryBound(to: ExportedSourceFile.self) |
| 56 | + let regions = sourceFile.pointee.configuredRegionsIfEmbedded(astContext: astContext) |
| 57 | + return regions.isActive(token) == .active ? 1 : 0 |
| 58 | +} |
| 59 | + |
| 60 | +/// A build configuration that is a thin shim over the |
| 61 | +/// CompilerBuildConfiguration that enables the Embedded language feature, |
| 62 | +/// allowing the compiler to determine whether code we're in will be available |
| 63 | +/// in an embedded context or not. |
| 64 | +struct EmbeddedBuildConfiguration: BuildConfiguration { |
| 65 | + let configuration: CompilerBuildConfiguration |
| 66 | + |
| 67 | + init(ctx: BridgedASTContext, sourceBuffer: UnsafeBufferPointer<UInt8>) { |
| 68 | + self.configuration = .init(ctx: ctx, sourceBuffer: sourceBuffer) |
| 69 | + } |
| 70 | + |
| 71 | + func isCustomConditionSet(name: String) throws -> Bool { |
| 72 | + // $Embedded is set when building Embedded Swift |
| 73 | + if name == "$Embedded" { |
| 74 | + return true |
| 75 | + } |
| 76 | + |
| 77 | + return try configuration.isCustomConditionSet(name: name) |
| 78 | + } |
| 79 | + |
| 80 | + func hasFeature(name: String) throws -> Bool { |
| 81 | + // The "Embedded" feature is set when building Embedded Swift. |
| 82 | + if name == "Embedded" { |
| 83 | + return true |
| 84 | + } |
| 85 | + |
| 86 | + return try configuration.hasFeature(name: name) |
| 87 | + } |
| 88 | + |
| 89 | + func hasAttribute(name: String) throws -> Bool { |
| 90 | + return try configuration.hasAttribute(name: name) |
| 91 | + } |
| 92 | + |
| 93 | + func canImport( |
| 94 | + importPath: [(TokenSyntax, String)], |
| 95 | + version: CanImportVersion |
| 96 | + ) throws -> Bool { |
| 97 | + // FIXME: We would prefer to call the underlying canImport in some kind of |
| 98 | + // "replay" mode that is not allowed to produce diagnostics or trigger any |
| 99 | + // loading. |
| 100 | + return false |
| 101 | + } |
| 102 | + |
| 103 | + func isActiveTargetOS(name: String) throws -> Bool { |
| 104 | + return try configuration.isActiveTargetOS(name: name) |
| 105 | + } |
| 106 | + |
| 107 | + func isActiveTargetArchitecture(name: String) throws -> Bool { |
| 108 | + return try configuration.isActiveTargetArchitecture(name: name) |
| 109 | + } |
| 110 | + |
| 111 | + func isActiveTargetEnvironment(name: String) throws -> Bool { |
| 112 | + return try configuration.isActiveTargetEnvironment(name: name) |
| 113 | + } |
| 114 | + |
| 115 | + func isActiveTargetRuntime(name: String) throws -> Bool { |
| 116 | + return try configuration.isActiveTargetRuntime(name: name) |
| 117 | + } |
| 118 | + |
| 119 | + func isActiveTargetPointerAuthentication(name: String) throws -> Bool { |
| 120 | + return try configuration.isActiveTargetPointerAuthentication(name: name) |
| 121 | + } |
| 122 | + |
| 123 | + var targetPointerBitWidth: Int { |
| 124 | + return configuration.targetPointerBitWidth |
| 125 | + } |
| 126 | + |
| 127 | + var targetAtomicBitWidths: [Int] { |
| 128 | + return configuration.targetAtomicBitWidths |
| 129 | + } |
| 130 | + |
| 131 | + var endianness: Endianness { |
| 132 | + return configuration.endianness |
| 133 | + } |
| 134 | + |
| 135 | + var languageVersion: VersionTuple { |
| 136 | + return configuration.languageVersion |
| 137 | + } |
| 138 | + |
| 139 | + var compilerVersion: VersionTuple { |
| 140 | + return configuration.compilerVersion |
| 141 | + } |
| 142 | +} |
0 commit comments