|
| 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 | +import CodePrinting |
| 16 | +import SwiftExtract |
| 17 | + |
| 18 | +extension SwiftSymbolTable { |
| 19 | + package func printImportedModules(_ printer: inout CodePrinter) { |
| 20 | + let mainSymbolSourceModules = Set( |
| 21 | + self.importedModules.values.filter { $0.alternativeModules?.isMainSourceOfSymbols ?? false }.map(\.moduleName) |
| 22 | + ) |
| 23 | + |
| 24 | + for module in self.importedModules.keys.sorted() { |
| 25 | + guard module != "Swift" else { |
| 26 | + continue |
| 27 | + } |
| 28 | + |
| 29 | + // Synthetic stub modules (e.g. <javaClassStubs>) exist purely for |
| 30 | + // symbol-table resolution; they are not real Swift modules and must |
| 31 | + // not be emitted as `import` statements. |
| 32 | + guard !self.syntheticImportedModuleNames.contains(module) else { |
| 33 | + continue |
| 34 | + } |
| 35 | + |
| 36 | + guard let alternativeModules = self.importedModules[module]?.alternativeModules else { |
| 37 | + printer.print("import \(module)") |
| 38 | + continue |
| 39 | + } |
| 40 | + |
| 41 | + // Only the main source of symbols emits the conditional import block. |
| 42 | + // Secondary modules (e.g. FoundationEssentials when Foundation is the main source) |
| 43 | + // are skipped when their main source is already present, because the main source's |
| 44 | + // block already covers the import. If no main source is present, fall back to a |
| 45 | + // plain import so the module is still imported. |
| 46 | + guard alternativeModules.isMainSourceOfSymbols else { |
| 47 | + if mainSymbolSourceModules.isDisjoint(with: alternativeModules.moduleNames) { |
| 48 | + printer.print("import \(module)") |
| 49 | + } |
| 50 | + continue |
| 51 | + } |
| 52 | + |
| 53 | + var importGroups: [String: [String]] = [:] |
| 54 | + for name in alternativeModules.moduleNames { |
| 55 | + guard let otherModule = self.importedModules[name] else { continue } |
| 56 | + |
| 57 | + let groupKey = otherModule.requiredAvailablityOfModuleWithName ?? otherModule.moduleName |
| 58 | + importGroups[groupKey, default: []].append(otherModule.moduleName) |
| 59 | + } |
| 60 | + |
| 61 | + for (index, group) in importGroups.keys.sorted().enumerated() { |
| 62 | + if index > 0 && importGroups.keys.count > 1 { |
| 63 | + printer.print("#elseif canImport(\(group))") |
| 64 | + } else { |
| 65 | + printer.print("#if canImport(\(group))") |
| 66 | + } |
| 67 | + |
| 68 | + for groupModule in importGroups[group] ?? [] { |
| 69 | + printer.print("import \(groupModule)") |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + if importGroups.keys.isEmpty { |
| 74 | + printer.print("import \(module)") |
| 75 | + } else { |
| 76 | + printer.print("#else") |
| 77 | + printer.print("import \(module)") |
| 78 | + printer.print("#endif") |
| 79 | + } |
| 80 | + } |
| 81 | + printer.println() |
| 82 | + } |
| 83 | +} |
0 commit comments