Skip to content

Commit

Permalink
Move SwiftProjection.Module to the top level
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlabelle committed Sep 15, 2024
1 parent d6757d3 commit f0a1aaf
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 137 deletions.
91 changes: 91 additions & 0 deletions Generator/Sources/ProjectionModel/Module.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import Collections
import DotNetMetadata
import DotNetXMLDocs

public final class Module {
public unowned let projection: SwiftProjection
public let name: String
public let abiModuleName: String
public let flattenNamespaces: Bool
private var weakReferences: Set<Unowned<Module>> = []

// OrderedSets are not sorted. Sort it lazily for stable iteration.
private var lazySortedTypeDefinitions = OrderedSet<TypeDefinition>()
private var typeDefinitionsSorted = true

public private(set) var genericInstantiationsByDefinition = [TypeDefinition: [[TypeNode]]]()

internal init(projection: SwiftProjection, name: String, flattenNamespaces: Bool = false) {
self.projection = projection
self.name = name
self.abiModuleName = name + CAbi.moduleSuffix
self.flattenNamespaces = flattenNamespaces
}

public var typeDefinitions: OrderedSet<TypeDefinition> {
if !typeDefinitionsSorted {
lazySortedTypeDefinitions.sort { $0.fullName < $1.fullName }
typeDefinitionsSorted = true
}
return lazySortedTypeDefinitions
}

public var references: [Module] { weakReferences.map { $0.object } }

public var isEmpty: Bool { lazySortedTypeDefinitions.isEmpty }

public func addAssembly(_ assembly: Assembly, documentation: AssemblyDocumentation? = nil) {
projection.addAssembly(assembly, module: self, documentation: documentation)
}

public func hasTypeDefinition(_ type: TypeDefinition) -> Bool {
lazySortedTypeDefinitions.contains(type)
}

public func addTypeDefinition(_ type: TypeDefinition) {
precondition(projection.getModule(type.assembly) === self)
lazySortedTypeDefinitions.append(type)
typeDefinitionsSorted = false
}

public func addGenericInstantiation(_ type: BoundType) {
precondition(!type.genericArgs.isEmpty && !type.isParameterized)
guard genericInstantiationsByDefinition[type.definition]?.contains(type.genericArgs) != true else { return }
genericInstantiationsByDefinition[type.definition, default: []].append(type.genericArgs)
}

public func addReference(_ other: Module) {
weakReferences.insert(Unowned(other))
}

public func getNamespaceModuleName(namespace: String) -> String {
precondition(!flattenNamespaces)
return "\(name)_\(SwiftProjection.toCompactNamespace(namespace))"
}

internal func getName(_ typeDefinition: TypeDefinition, namespaced: Bool = true) throws -> String {
// Map: Namespace.TypeName
// To: Namespace_TypeName
// Map: Namespace.Subnamespace.TypeName/NestedTypeName
// To: NamespaceSubnamespace_TypeName_NestedTypeName
var result: String = ""
if let enclosingType = try typeDefinition.enclosingType {
result += try getName(enclosingType, namespaced: namespaced) + "_"
}
else if namespaced && !flattenNamespaces {
result += typeDefinition.namespace.flatMap { SwiftProjection.toCompactNamespace($0) + "_" } ?? ""
}

result += typeDefinition.nameWithoutGenericArity

return result
}

private static func getNamespaceOrEmpty(_ type: TypeDefinition) -> String {
var namespacedType = type
while namespacedType.namespace == nil, let enclosingType = try? namespacedType.enclosingType {
namespacedType = enclosingType
}
return namespacedType.namespace ?? ""
}
}
29 changes: 0 additions & 29 deletions Generator/Sources/ProjectionModel/ReferenceNullability.swift

This file was deleted.

93 changes: 0 additions & 93 deletions Generator/Sources/ProjectionModel/SwiftProjection+Module.swift

This file was deleted.

5 changes: 4 additions & 1 deletion Generator/Sources/ProjectionModel/SwiftProjection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public class SwiftProjection {

public private(set) var modulesByName = OrderedDictionary<String, Module>()
internal var assembliesToModules = [Assembly: AssemblyEntry]()
public var referenceReturnNullability: ReferenceNullability { .explicit }

public init() {}

Expand All @@ -25,4 +24,8 @@ public class SwiftProjection {
public func getModule(_ assembly: Assembly) -> Module? {
assembliesToModules[assembly]?.module
}

internal func addAssembly(_ assembly: Assembly, module: Module, documentation: AssemblyDocumentation? = nil) {
assembliesToModules[assembly] = AssemblyEntry(module: module, documentation: documentation)
}
}
10 changes: 5 additions & 5 deletions Generator/Sources/SwiftWinRT/Writing/ABIModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import DotNetMetadata
import ProjectionModel
import WindowsMetadata

internal func writeABIModule(_ module: SwiftProjection.Module, directoryPath: String, generateCMakeLists: Bool) throws {
internal func writeABIModule(_ module: Module, directoryPath: String, generateCMakeLists: Bool) throws {
let includeDirectoryPath = "\(directoryPath)\\include"

try writeABIFile(module: module, toPath: "\(includeDirectoryPath)\\\(module.name).h")
Expand All @@ -24,7 +24,7 @@ internal func writeABIModule(_ module: SwiftProjection.Module, directoryPath: St
}
}

fileprivate func writeABIFile(module: SwiftProjection.Module, toPath path: String) throws {
fileprivate func writeABIFile(module: Module, toPath path: String) throws {
let cHeaderWriter = CSourceFileWriter(output: FileTextOutputStream(path: path, directoryCreation: .ancestors))

// Write includes
Expand Down Expand Up @@ -57,7 +57,7 @@ fileprivate func writeABIFile(module: SwiftProjection.Module, toPath path: Strin
}
}

fileprivate func getSortedEnums(module: SwiftProjection.Module) throws -> [EnumDefinition] {
fileprivate func getSortedEnums(module: Module) throws -> [EnumDefinition] {
var enumDefinitions = [EnumDefinition]()
for typeDefinition in module.typeDefinitions {
guard let enumDefinition = typeDefinition as? EnumDefinition else { continue }
Expand All @@ -69,7 +69,7 @@ fileprivate func getSortedEnums(module: SwiftProjection.Module) throws -> [EnumD
}

// Gets the module's structs in an order so that nested structs appear before their containers.
fileprivate func getSortedStructs(module: SwiftProjection.Module) throws -> [StructDefinition] {
fileprivate func getSortedStructs(module: Module) throws -> [StructDefinition] {
// Create an initial deterministic ordering of structs
var sortedByFullName = [StructDefinition]()
for typeDefinition in module.typeDefinitions {
Expand Down Expand Up @@ -102,7 +102,7 @@ fileprivate func getSortedStructs(module: SwiftProjection.Module) throws -> [Str
return sorted
}

fileprivate func getSortedInterfaces(module: SwiftProjection.Module) throws -> [BoundType] {
fileprivate func getSortedInterfaces(module: Module) throws -> [BoundType] {
var interfacesByMangledName = OrderedDictionary<String, BoundType>()

// Add nongeneric type definitions
Expand Down
2 changes: 1 addition & 1 deletion Generator/Sources/SwiftWinRT/Writing/SourcePreamble.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ internal func writeGeneratedCodePreamble(to writer: SwiftSourceFileWriter) {
writer.writeCommentLine("swiftlint:disable all", groupWithNext: false)
}

internal func writeModulePreamble(_ module: SwiftProjection.Module, importABI: Bool = true, to writer: SwiftSourceFileWriter) {
internal func writeModulePreamble(_ module: Module, importABI: Bool = true, to writer: SwiftSourceFileWriter) {
writer.writeImport(module: SupportModules.WinRT.moduleName)

if importABI {
Expand Down
2 changes: 1 addition & 1 deletion Generator/Sources/SwiftWinRT/createProjection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal func createProjection(commandLineArguments: CommandLineArguments, proje
}
}

func addReference(sourceModule: SwiftProjection.Module, targetAssembly: Assembly) {
func addReference(sourceModule: Module, targetAssembly: Assembly) {
if let targetModule = projection.getModule(targetAssembly), targetModule !== sourceModule {
sourceModule.addReference(targetModule)
}
Expand Down
14 changes: 7 additions & 7 deletions Generator/Sources/SwiftWinRT/writeProjectionFiles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal func writeProjectionFiles(
}

fileprivate func writeModuleFiles(
_ module: SwiftProjection.Module,
_ module: Module,
directoryPath: String,
generateCMakeLists: Bool,
dynamicLibrary: Bool) throws {
Expand All @@ -59,7 +59,7 @@ fileprivate func writeModuleFiles(
}

fileprivate func writeSwiftModuleFiles(
_ module: SwiftProjection.Module, directoryPath: String,
_ module: Module, directoryPath: String,
generateCMakeLists: Bool, dynamicLibrary: Bool) throws {
var cmakeSources: [String] = []
for typeDefinition in module.typeDefinitions + Array(module.genericInstantiationsByDefinition.keys) {
Expand Down Expand Up @@ -116,7 +116,7 @@ fileprivate func writeSwiftModuleFiles(
}
}

fileprivate func writeNamespaceModuleFiles(_ module: SwiftProjection.Module, directoryPath: String, generateCMakeLists: Bool) throws {
fileprivate func writeNamespaceModuleFiles(_ module: Module, directoryPath: String, generateCMakeLists: Bool) throws {
let typeDefinitionsByNamespace = Dictionary(grouping: module.typeDefinitions, by: { $0.namespace })

var compactNamespaces: [String] = []
Expand Down Expand Up @@ -156,14 +156,14 @@ fileprivate func hasSwiftDefinition(_ typeDefinition: TypeDefinition) throws ->
&& typeDefinition.isPublic
}

fileprivate func writeTypeDefinitionFile(_ typeDefinition: TypeDefinition, module: SwiftProjection.Module, toPath path: String) throws {
fileprivate func writeTypeDefinitionFile(_ typeDefinition: TypeDefinition, module: Module, toPath path: String) throws {
let writer = SwiftSourceFileWriter(output: FileTextOutputStream(path: path, directoryCreation: .ancestors))
writeGeneratedCodePreamble(to: writer)
writeModulePreamble(module, to: writer)
try writeTypeDefinition(typeDefinition, projection: module.projection, to: writer)
}

fileprivate func writeABIProjectionConformanceFile(_ typeDefinition: TypeDefinition, module: SwiftProjection.Module, toPath path: String) throws {
fileprivate func writeABIProjectionConformanceFile(_ typeDefinition: TypeDefinition, module: Module, toPath path: String) throws {
let writer = SwiftSourceFileWriter(output: FileTextOutputStream(path: path, directoryCreation: .ancestors))
writeGeneratedCodePreamble(to: writer)
writeModulePreamble(module, to: writer)
Expand Down Expand Up @@ -210,7 +210,7 @@ fileprivate func getExtensionFileBytes(typeDefinition: TypeDefinition) throws ->
}
}

fileprivate func writeCOMInteropExtensionFile(typeDefinition: TypeDefinition, module: SwiftProjection.Module, toPath path: String) throws -> Bool {
fileprivate func writeCOMInteropExtensionFile(typeDefinition: TypeDefinition, module: Module, toPath path: String) throws -> Bool {
// IReference<T> is implemented generically in the support module.
if typeDefinition.namespace == "Windows.Foundation", typeDefinition.name == "IReference`1" { return false }

Expand All @@ -232,7 +232,7 @@ fileprivate func writeCOMInteropExtensionFile(typeDefinition: TypeDefinition, mo
return true
}

internal func writeNamespaceAliasesFile(typeDefinitions: [TypeDefinition], module: SwiftProjection.Module, toPath path: String) throws {
internal func writeNamespaceAliasesFile(typeDefinitions: [TypeDefinition], module: Module, toPath path: String) throws {
let writer = SwiftSourceFileWriter(output: FileTextOutputStream(path: path, directoryCreation: .ancestors))
writeGeneratedCodePreamble(to: writer)
writer.writeImport(module: module.name)
Expand Down

0 comments on commit f0a1aaf

Please sign in to comment.