|
| 1 | +import Foundation |
| 2 | + |
| 3 | +// MARK: - LibraryWrapperGenerator |
| 4 | + |
| 5 | +/// Generator for SourceKitten's library wrappers. |
| 6 | +enum LibraryWrapperGenerator: CaseIterable { |
| 7 | + case clang |
| 8 | + case sourcekit |
| 9 | + |
| 10 | + /// The relative file path for this library wrapper's generated source file. |
| 11 | + var filePath: String { |
| 12 | + return "Source/SourceKittenFramework/library_wrapper_\(moduleName).swift" |
| 13 | + } |
| 14 | + |
| 15 | + /// Generate the Swift source code for this library wrapper. |
| 16 | + /// |
| 17 | + /// - parameter compilerArguments: The compiler arguments to SourceKittenFramework. |
| 18 | + /// |
| 19 | + /// - returns: The generated Swift source code for this library wrapper. |
| 20 | + func generate(compilerArguments: [String]) throws -> String { |
| 21 | + let freeFunctions = try extractFreeFunctions(compilerArguments: compilerArguments) |
| 22 | + .joined(separator: "\n") |
| 23 | + return [ |
| 24 | + fileHeader, |
| 25 | + "// swiftlint:disable unused_declaration - We don't care if some of these are unused.", |
| 26 | + freeFunctions, |
| 27 | + self == .clang ? "#endif" : nil |
| 28 | + ] |
| 29 | + .compactMap { $0 } |
| 30 | + .joined(separator: "\n\n") + "\n" |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// MARK: - Private |
| 35 | + |
| 36 | +private extension LibraryWrapperGenerator { |
| 37 | + /// The wrapped module name. |
| 38 | + var moduleName: String { |
| 39 | + switch self { |
| 40 | + case .clang: |
| 41 | + return "Clang_C" |
| 42 | + case .sourcekit: |
| 43 | + return "SourceKit" |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /// The top section of the generated library wrapper. |
| 48 | + var fileHeader: String { |
| 49 | + switch self { |
| 50 | + case .clang: |
| 51 | + return """ |
| 52 | + #if !os(Linux) |
| 53 | +
|
| 54 | + #if os(Windows) |
| 55 | + import WinSDK |
| 56 | + #else |
| 57 | + import Darwin |
| 58 | + #endif |
| 59 | +
|
| 60 | + #if SWIFT_PACKAGE |
| 61 | + import Clang_C |
| 62 | + #endif |
| 63 | +
|
| 64 | + #if os(Windows) |
| 65 | + private let library = toolchainLoader.load(path: "libclang.dll") |
| 66 | + #else |
| 67 | + private let library = toolchainLoader.load(path: "libclang.dylib") |
| 68 | + #endif |
| 69 | + """ |
| 70 | + case .sourcekit: |
| 71 | + return """ |
| 72 | + #if SWIFT_PACKAGE |
| 73 | + import SourceKit |
| 74 | + #endif |
| 75 | +
|
| 76 | + #if os(Linux) |
| 77 | + private let library = toolchainLoader.load(path: "libsourcekitdInProc.so") |
| 78 | + #elseif os(Windows) |
| 79 | + private let library = toolchainLoader.load(path: "sourcekitdInProc.dll") |
| 80 | + #else |
| 81 | + private let library = toolchainLoader.load(path: "sourcekitdInProc.framework/Versions/A/sourcekitdInProc") |
| 82 | + #endif |
| 83 | + """ |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + func extractFreeFunctions(compilerArguments: [String]) throws -> [String] { |
| 88 | + let sourceKitResponse = try interfaceForModule(moduleName, compilerArguments: compilerArguments) |
| 89 | + let substructure = SwiftDocKey.getSubstructure(Structure(sourceKitResponse: sourceKitResponse).dictionary)! |
| 90 | + let source = sourceKitResponse["key.sourcetext"] as! String |
| 91 | + return source.extractFreeFunctions(inSubstructure: substructure) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +private func interfaceForModule(_ module: String, compilerArguments: [String]) throws -> [String: SourceKitRepresentable] { |
| 96 | + return try Request.customRequest(request: [ |
| 97 | + "key.request": UID("source.request.editor.open.interface"), |
| 98 | + "key.name": UUID().uuidString, |
| 99 | + "key.compilerargs": compilerArguments, |
| 100 | + "key.modulename": module |
| 101 | + ]).send() |
| 102 | +} |
| 103 | + |
| 104 | +private extension String { |
| 105 | + func nameFromFullFunctionName() -> String { |
| 106 | + return String(self[..<range(of: "(")!.lowerBound]) |
| 107 | + } |
| 108 | + |
| 109 | + func extractFreeFunctions(inSubstructure substructure: [[String: SourceKitRepresentable]]) -> [String] { |
| 110 | + substructure |
| 111 | + .filter { SwiftDeclarationKind(rawValue: SwiftDocKey.getKind($0)!) == .functionFree } |
| 112 | + .compactMap { function -> String? in |
| 113 | + let name = (function["key.name"] as! String).nameFromFullFunctionName() |
| 114 | + let unsupportedFunctions = [ |
| 115 | + "clang_executeOnThread", |
| 116 | + "sourcekitd_variant_dictionary_apply", |
| 117 | + "sourcekitd_variant_array_apply" |
| 118 | + ] |
| 119 | + guard !unsupportedFunctions.contains(name) else { |
| 120 | + return nil |
| 121 | + } |
| 122 | + |
| 123 | + let parameters = SwiftDocKey.getSubstructure(function)?.map { parameterStructure in |
| 124 | + return parameterStructure["key.typename"] as! String |
| 125 | + } ?? [] |
| 126 | + var returnTypes = [String]() |
| 127 | + if let offset = SwiftDocKey.getOffset(function), let length = SwiftDocKey.getLength(function) { |
| 128 | + let stringView = StringView(self) |
| 129 | + if let functionDeclaration = stringView.substringWithByteRange(ByteRange(location: offset, length: length)), |
| 130 | + let startOfReturnArrow = functionDeclaration.range(of: "->", options: .backwards)?.lowerBound { |
| 131 | + let adjustedDistance = distance(from: startIndex, to: startOfReturnArrow) |
| 132 | + let adjustedReturnTypeStartIndex = functionDeclaration.index(functionDeclaration.startIndex, |
| 133 | + offsetBy: adjustedDistance + 3) |
| 134 | + returnTypes.append(String(functionDeclaration[adjustedReturnTypeStartIndex...])) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + let joinedParameters = parameters.map({ $0.replacingOccurrences(of: "!", with: "?") }).joined(separator: ", ") |
| 139 | + let joinedReturnTypes = returnTypes.map({ $0.replacingOccurrences(of: "!", with: "?") }).joined(separator: ", ") |
| 140 | + let lhs = "internal let \(name): @convention(c) (\(joinedParameters)) -> (\(joinedReturnTypes))" |
| 141 | + let rhs = "library.load(symbol: \"\(name)\")" |
| 142 | + return "\(lhs) = \(rhs)".replacingOccurrences(of: "SourceKittenFramework.", with: "") |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments