Skip to content

Commit

Permalink
Renamed SwiftProjection to Projection and moved its Module nested cla…
Browse files Browse the repository at this point in the history
…ss to the top level (#289)
  • Loading branch information
tristanlabelle authored Sep 15, 2024
1 parent d6757d3 commit ecddb1d
Show file tree
Hide file tree
Showing 28 changed files with 213 additions and 241 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: Projection
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: Projection, 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)_\(Projection.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 { Projection.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 ?? ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import CodeWriters
import DotNetMetadata
import WindowsMetadata

extension SwiftProjection {
extension Projection {
public func toABIType(_ type: BoundType) throws -> SwiftType {
if let classDefinition = type.definition as? ClassDefinition {
// The ABI representation of a (non-static) class is that of its default interface.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import DotNetMetadata
import DotNetXMLDocs
import WindowsMetadata

extension SwiftProjection {
extension Projection {
public static func toVisibility(_ visibility: DotNetMetadata.Visibility, inheritableClass: Bool = false) -> SwiftVisibility {
switch visibility {
case .compilerControlled: return .fileprivate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import CodeWriters
import DotNetMetadata
import DotNetXMLDocs

extension SwiftProjection {
extension Projection {
public func getDocumentation(_ typeDefinition: TypeDefinition) -> MemberDocumentation? {
guard let documentation = assembliesToModules[typeDefinition.assembly]?.documentation else { return nil }
return documentation.members[.type(toDocumentationTypeReference(typeDefinition))]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import CodeWriters
import DotNetMetadata

extension SwiftProjection {
extension Projection {
internal func toParamName(_ param: ParamBase) -> String {
switch param {
case is ReturnParam: "_result"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import DotNetMetadata
import WindowsMetadata
import CodeWriters

extension SwiftProjection {
extension Projection {
public func toType(_ type: TypeNode) throws -> SwiftType {
switch type {
case let .bound(type):
Expand Down Expand Up @@ -116,7 +116,7 @@ extension SwiftProjection {
else {
return .chain([
.init(projectionTypeName),
.init(try SwiftProjection.toProjectionInstantiationTypeName(genericArgs: type.genericArgs))
.init(try Projection.toProjectionInstantiationTypeName(genericArgs: type.genericArgs))
])
}
}()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import Collections
import DotNetMetadata
import DotNetXMLDocs

public class SwiftProjection {
public class Projection {
internal struct AssemblyEntry {
var module: Module
var documentation: AssemblyDocumentation?
}

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)
}
}
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.

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
Loading

0 comments on commit ecddb1d

Please sign in to comment.