From ecddb1d47642bd7ecabe8537fc60ad3a2d49f3c1 Mon Sep 17 00:00:00 2001 From: Tristan Labelle Date: Sun, 15 Sep 2024 09:32:30 -0400 Subject: [PATCH] Renamed SwiftProjection to Projection and moved its Module nested class to the top level (#289) --- .../Sources/ProjectionModel/Module.swift | 91 ++++++++++++++++++ ...jection+abi.swift => Projection+abi.swift} | 2 +- ...sion.swift => Projection+conversion.swift} | 2 +- ...n.swift => Projection+documentation.swift} | 2 +- ...n+params.swift => Projection+params.swift} | 2 +- ...ion+types.swift => Projection+types.swift} | 4 +- ...SwiftProjection.swift => Projection.swift} | 7 +- .../ReferenceNullability.swift | 29 ------ .../SwiftProjection+Module.swift | 93 ------------------- .../SwiftWinRT/Writing/ABIModule.swift | 10 +- .../Writing/ABIProjectionType.swift | 22 ++--- .../SwiftWinRT/Writing/COMImportClass.swift | 4 +- .../Writing/COMInteropExtension.swift | 6 +- .../SwiftWinRT/Writing/ClassDefinition.swift | 26 +++--- .../SwiftWinRT/Writing/EnumDefinition.swift | 18 ++-- .../SwiftWinRT/Writing/ExeManifest.swift | 2 +- .../Writing/ExtensionProperties.swift | 10 +- .../Writing/InterfaceDefinition.swift | 22 ++--- .../Writing/InterfaceImplementation.swift | 30 +++--- .../SwiftWinRT/Writing/NamespaceAlias.swift | 8 +- .../Writing/SecondaryInterfaces.swift | 4 +- .../SwiftWinRT/Writing/SourcePreamble.swift | 2 +- .../SwiftWinRT/Writing/StructDefinition.swift | 16 ++-- .../SwiftWinRT/Writing/SwiftPackageFile.swift | 4 +- .../SwiftWinRT/Writing/TypeDefinition.swift | 6 +- .../SwiftWinRT/Writing/VirtualTable.swift | 6 +- .../Sources/SwiftWinRT/createProjection.swift | 6 +- .../SwiftWinRT/writeProjectionFiles.swift | 20 ++-- 28 files changed, 213 insertions(+), 241 deletions(-) create mode 100644 Generator/Sources/ProjectionModel/Module.swift rename Generator/Sources/ProjectionModel/{SwiftProjection+abi.swift => Projection+abi.swift} (97%) rename Generator/Sources/ProjectionModel/{SwiftProjection+conversion.swift => Projection+conversion.swift} (99%) rename Generator/Sources/ProjectionModel/{SwiftProjection+documentation.swift => Projection+documentation.swift} (99%) rename Generator/Sources/ProjectionModel/{SwiftProjection+params.swift => Projection+params.swift} (99%) rename Generator/Sources/ProjectionModel/{SwiftProjection+types.swift => Projection+types.swift} (98%) rename Generator/Sources/ProjectionModel/{SwiftProjection.swift => Projection.swift} (76%) delete mode 100644 Generator/Sources/ProjectionModel/ReferenceNullability.swift delete mode 100644 Generator/Sources/ProjectionModel/SwiftProjection+Module.swift diff --git a/Generator/Sources/ProjectionModel/Module.swift b/Generator/Sources/ProjectionModel/Module.swift new file mode 100644 index 00000000..dcaa199a --- /dev/null +++ b/Generator/Sources/ProjectionModel/Module.swift @@ -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> = [] + + // OrderedSets are not sorted. Sort it lazily for stable iteration. + private var lazySortedTypeDefinitions = OrderedSet() + 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 { + 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 ?? "" + } +} \ No newline at end of file diff --git a/Generator/Sources/ProjectionModel/SwiftProjection+abi.swift b/Generator/Sources/ProjectionModel/Projection+abi.swift similarity index 97% rename from Generator/Sources/ProjectionModel/SwiftProjection+abi.swift rename to Generator/Sources/ProjectionModel/Projection+abi.swift index 2dd3eabd..3102d9c2 100644 --- a/Generator/Sources/ProjectionModel/SwiftProjection+abi.swift +++ b/Generator/Sources/ProjectionModel/Projection+abi.swift @@ -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. diff --git a/Generator/Sources/ProjectionModel/SwiftProjection+conversion.swift b/Generator/Sources/ProjectionModel/Projection+conversion.swift similarity index 99% rename from Generator/Sources/ProjectionModel/SwiftProjection+conversion.swift rename to Generator/Sources/ProjectionModel/Projection+conversion.swift index 32fa037b..aa12cdf4 100644 --- a/Generator/Sources/ProjectionModel/SwiftProjection+conversion.swift +++ b/Generator/Sources/ProjectionModel/Projection+conversion.swift @@ -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 diff --git a/Generator/Sources/ProjectionModel/SwiftProjection+documentation.swift b/Generator/Sources/ProjectionModel/Projection+documentation.swift similarity index 99% rename from Generator/Sources/ProjectionModel/SwiftProjection+documentation.swift rename to Generator/Sources/ProjectionModel/Projection+documentation.swift index 255659fa..27db9295 100644 --- a/Generator/Sources/ProjectionModel/SwiftProjection+documentation.swift +++ b/Generator/Sources/ProjectionModel/Projection+documentation.swift @@ -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))] diff --git a/Generator/Sources/ProjectionModel/SwiftProjection+params.swift b/Generator/Sources/ProjectionModel/Projection+params.swift similarity index 99% rename from Generator/Sources/ProjectionModel/SwiftProjection+params.swift rename to Generator/Sources/ProjectionModel/Projection+params.swift index 7fff144b..abfda785 100644 --- a/Generator/Sources/ProjectionModel/SwiftProjection+params.swift +++ b/Generator/Sources/ProjectionModel/Projection+params.swift @@ -1,7 +1,7 @@ import CodeWriters import DotNetMetadata -extension SwiftProjection { +extension Projection { internal func toParamName(_ param: ParamBase) -> String { switch param { case is ReturnParam: "_result" diff --git a/Generator/Sources/ProjectionModel/SwiftProjection+types.swift b/Generator/Sources/ProjectionModel/Projection+types.swift similarity index 98% rename from Generator/Sources/ProjectionModel/SwiftProjection+types.swift rename to Generator/Sources/ProjectionModel/Projection+types.swift index fb56fb83..4954a18b 100644 --- a/Generator/Sources/ProjectionModel/SwiftProjection+types.swift +++ b/Generator/Sources/ProjectionModel/Projection+types.swift @@ -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): @@ -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)) ]) } }() diff --git a/Generator/Sources/ProjectionModel/SwiftProjection.swift b/Generator/Sources/ProjectionModel/Projection.swift similarity index 76% rename from Generator/Sources/ProjectionModel/SwiftProjection.swift rename to Generator/Sources/ProjectionModel/Projection.swift index aa7ca42e..dfed3fa2 100644 --- a/Generator/Sources/ProjectionModel/SwiftProjection.swift +++ b/Generator/Sources/ProjectionModel/Projection.swift @@ -2,7 +2,7 @@ import Collections import DotNetMetadata import DotNetXMLDocs -public class SwiftProjection { +public class Projection { internal struct AssemblyEntry { var module: Module var documentation: AssemblyDocumentation? @@ -10,7 +10,6 @@ public class SwiftProjection { public private(set) var modulesByName = OrderedDictionary() internal var assembliesToModules = [Assembly: AssemblyEntry]() - public var referenceReturnNullability: ReferenceNullability { .explicit } public init() {} @@ -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) + } } \ No newline at end of file diff --git a/Generator/Sources/ProjectionModel/ReferenceNullability.swift b/Generator/Sources/ProjectionModel/ReferenceNullability.swift deleted file mode 100644 index bfe3891a..00000000 --- a/Generator/Sources/ProjectionModel/ReferenceNullability.swift +++ /dev/null @@ -1,29 +0,0 @@ -import CodeWriters - -/// Specifies how to express the nullability of WinRT reference types in Swift -public enum ReferenceNullability: Hashable { - /// Specifies to use explicitly unwrapped optionals - case explicit - /// Specifies to use implicitly unwrapped optionals - case implicit - /// Specifies to not represent null values at all. - /// Code might have to fail fast or throw errors when null values are encountered. - case none -} - -extension ReferenceNullability { - public func disallowImplicit() -> ReferenceNullability { - switch self { - case .implicit:.explicit - default: self - } - } - - public func applyTo(type: SwiftType) -> SwiftType { - switch self { - case .explicit: return .optional(wrapped: type, implicitUnwrap: false) - case .implicit: return .optional(wrapped: type, implicitUnwrap: true) - case .none: return type - } - } -} \ No newline at end of file diff --git a/Generator/Sources/ProjectionModel/SwiftProjection+Module.swift b/Generator/Sources/ProjectionModel/SwiftProjection+Module.swift deleted file mode 100644 index 4c605eb1..00000000 --- a/Generator/Sources/ProjectionModel/SwiftProjection+Module.swift +++ /dev/null @@ -1,93 +0,0 @@ -import Collections -import DotNetMetadata -import DotNetXMLDocs - -extension SwiftProjection { - public class Module { - public unowned let projection: SwiftProjection - public let name: String - public let abiModuleName: String - public let flattenNamespaces: Bool - - // OrderedSets are not sorted. Sort it lazily for stable iteration. - private var lazySortedTypeDefinitions = OrderedSet() - private var typeDefinitionsSorted = true - - public private(set) var genericInstantiationsByDefinition = [TypeDefinition: [[TypeNode]]]() - private var weakReferences: Set> = [] - - 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 { - 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.assembliesToModules[assembly] = AssemblyEntry(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 ?? "" - } - } -} \ No newline at end of file diff --git a/Generator/Sources/SwiftWinRT/Writing/ABIModule.swift b/Generator/Sources/SwiftWinRT/Writing/ABIModule.swift index 398882cf..4dfc4f96 100644 --- a/Generator/Sources/SwiftWinRT/Writing/ABIModule.swift +++ b/Generator/Sources/SwiftWinRT/Writing/ABIModule.swift @@ -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") @@ -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 @@ -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 } @@ -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 { @@ -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() // Add nongeneric type definitions diff --git a/Generator/Sources/SwiftWinRT/Writing/ABIProjectionType.swift b/Generator/Sources/SwiftWinRT/Writing/ABIProjectionType.swift index bb6d131e..8603bb1b 100644 --- a/Generator/Sources/SwiftWinRT/Writing/ABIProjectionType.swift +++ b/Generator/Sources/SwiftWinRT/Writing/ABIProjectionType.swift @@ -6,7 +6,7 @@ import CodeWriters import struct Foundation.UUID /// Writes a type or extension providing the ABIProjection conformance for a given projected WinRT type. -internal func writeABIProjectionConformance(_ typeDefinition: TypeDefinition, genericArgs: [TypeNode]?, projection: SwiftProjection, to writer: SwiftSourceFileWriter) throws { +internal func writeABIProjectionConformance(_ typeDefinition: TypeDefinition, genericArgs: [TypeNode]?, projection: Projection, to writer: SwiftSourceFileWriter) throws { if SupportModules.WinRT.getBuiltInTypeKind(typeDefinition) == .definitionAndProjection { // The support module already defines a projection, just import and reexport it. if typeDefinition.isReferenceType { @@ -70,7 +70,7 @@ internal func writeABIProjectionConformance(_ typeDefinition: TypeDefinition, ge type: .identifier(projection.toProjectionTypeName(typeDefinition))) { writer in try writeInterfaceOrDelegateProjectionType( typeDefinition.bindType(genericArgs: genericArgs), - projectionName: try SwiftProjection.toProjectionInstantiationTypeName(genericArgs: genericArgs), + projectionName: try Projection.toProjectionInstantiationTypeName(genericArgs: genericArgs), projection: projection, to: writer) } @@ -79,7 +79,7 @@ internal func writeABIProjectionConformance(_ typeDefinition: TypeDefinition, ge // Generic type definition. Create a namespace for projections of specializations. // public enum IVectorProjection {} try writer.writeEnum( - visibility: SwiftProjection.toVisibility(typeDefinition.visibility), + visibility: Projection.toVisibility(typeDefinition.visibility), name: projection.toProjectionTypeName(typeDefinition)) { _ in } } } @@ -87,7 +87,7 @@ internal func writeABIProjectionConformance(_ typeDefinition: TypeDefinition, ge /// Writes an extension to a struct to provide the ABIProjection conformance. fileprivate func writeStructProjectionExtension( _ structDefinition: StructDefinition, - projection: SwiftProjection, + projection: Projection, to writer: SwiftSourceFileWriter) throws { let isInert = try projection.isProjectionInert(structDefinition) @@ -139,7 +139,7 @@ fileprivate func writeStructProjectionExtension( for (index, field) in fields.enumerated() { if index > 0 { output.write(",", endLine: true) } try writeStructABIToSwiftInitializerParam( - abiValueName: "value", abiFieldName: field.name, swiftFieldName: SwiftProjection.toMemberName(field), + abiValueName: "value", abiFieldName: field.name, swiftFieldName: Projection.toMemberName(field), typeProjection: projection.getTypeProjection(field.type), to: output) } } @@ -162,7 +162,7 @@ fileprivate func writeStructProjectionExtension( for (index, field) in fields.enumerated() { if index > 0 { output.write(",", endLine: true) } try writeStructSwiftToABIInitializerParam( - swiftValueName: "value", swiftFieldName: SwiftProjection.toMemberName(field), abiFieldName: field.name, + swiftValueName: "value", swiftFieldName: Projection.toMemberName(field), abiFieldName: field.name, typeProjection: projection.getTypeProjection(field.type), to: output) } } @@ -252,7 +252,7 @@ fileprivate func writeIReferenceIDProperty(propertyName: String, parameterizedID fileprivate func writeClassProjectionType( _ classDefinition: ClassDefinition, defaultInterface: BoundInterface, - projection: SwiftProjection, + projection: Projection, to writer: SwiftSourceFileWriter) throws { assert(!classDefinition.isStatic) @@ -262,7 +262,7 @@ fileprivate func writeClassProjectionType( let projectionTypeName = try projection.toProjectionTypeName(classDefinition) try writer.writeClass( - visibility: SwiftProjection.toVisibility(classDefinition.visibility), + visibility: Projection.toVisibility(classDefinition.visibility), name: projectionTypeName, protocolConformances: [ projectionProtocol ]) { writer throws in let typeName = try projection.toTypeName(classDefinition) @@ -295,7 +295,7 @@ fileprivate func writeClassProjectionType( fileprivate func writeInterfaceOrDelegateProjectionType( _ type: BoundType, projectionName: String, - projection: SwiftProjection, + projection: Projection, to writer: some SwiftDeclarationWriter) throws { precondition(type.definition is InterfaceDefinition || type.definition is DelegateDefinition) let projectionProtocol = type.definition is InterfaceDefinition @@ -304,7 +304,7 @@ fileprivate func writeInterfaceOrDelegateProjectionType( // Projections of generic instantiations are not owned by any specific module. // Making them internal avoids clashes between redundant definitions across modules. try writer.writeEnum( - visibility: type.genericArgs.isEmpty ? SwiftProjection.toVisibility(type.definition.visibility) : .internal, + visibility: type.genericArgs.isEmpty ? Projection.toVisibility(type.definition.visibility) : .internal, name: projectionName, protocolConformances: [ projectionProtocol ]) { writer throws in @@ -361,7 +361,7 @@ internal func writeReferenceTypeProjectionConformance( apiType: BoundType, abiType: BoundType, wrapImpl: (_ writer: inout SwiftStatementWriter, _ paramName: String) throws -> Void, toCOMImpl: ((_ writer: inout SwiftStatementWriter, _ paramName: String) throws -> Void)? = nil, - projection: SwiftProjection, + projection: Projection, to writer: SwiftTypeDefinitionWriter) throws { writer.writeTypeAlias(visibility: .public, name: "SwiftObject", target: try projection.toType(apiType.asNode).unwrapOptional()) diff --git a/Generator/Sources/SwiftWinRT/Writing/COMImportClass.swift b/Generator/Sources/SwiftWinRT/Writing/COMImportClass.swift index 9ec78bd1..a0f3cc67 100644 --- a/Generator/Sources/SwiftWinRT/Writing/COMImportClass.swift +++ b/Generator/Sources/SwiftWinRT/Writing/COMImportClass.swift @@ -10,7 +10,7 @@ internal func writeCOMImportClass( visibility: SwiftVisibility, name: String, projectionName: String, - projection: SwiftProjection, + projection: Projection, to writer: SwiftTypeDefinitionWriter) throws { let importBaseTypeName: String let protocolConformances: [SwiftType] @@ -66,7 +66,7 @@ internal func writeCOMImportClass( /// Gathers all generic arguments from the given interfaces and writes them as type aliases /// For example, if an interface is IMap, write K = String and V = Int32 -internal func writeGenericTypeAliases(interfaces: [BoundInterface], projection: SwiftProjection, to writer: SwiftTypeDefinitionWriter) throws { +internal func writeGenericTypeAliases(interfaces: [BoundInterface], projection: Projection, to writer: SwiftTypeDefinitionWriter) throws { var typeAliases = OrderedDictionary() for interface in interfaces { diff --git a/Generator/Sources/SwiftWinRT/Writing/COMInteropExtension.swift b/Generator/Sources/SwiftWinRT/Writing/COMInteropExtension.swift index 0bc24d74..1d4a4da1 100644 --- a/Generator/Sources/SwiftWinRT/Writing/COMInteropExtension.swift +++ b/Generator/Sources/SwiftWinRT/Writing/COMInteropExtension.swift @@ -5,7 +5,7 @@ import ProjectionModel import CodeWriters import struct Foundation.UUID -internal func writeCOMInteropExtension(abiType: BoundType, projection: SwiftProjection, to writer: SwiftSourceFileWriter) throws { +internal func writeCOMInteropExtension(abiType: BoundType, projection: Projection, to writer: SwiftSourceFileWriter) throws { let abiSwiftType = try projection.toABIType(abiType) let visibility: SwiftVisibility = abiType.genericArgs.isEmpty ? .public : .internal @@ -69,7 +69,7 @@ internal func toIIDExpression(_ uuid: UUID) throws -> String { fileprivate func writeCOMInteropMethod( _ method: Method, typeGenericArgs: [TypeNode], visibility: SwiftVisibility, methodKind: ABIMethodKind, - projection: SwiftProjection, to writer: SwiftTypeDefinitionWriter) throws { + projection: Projection, to writer: SwiftTypeDefinitionWriter) throws { let abiMethodName = try method.findAttribute(OverloadAttribute.self)?.methodName ?? method.name let (paramProjections, returnProjection) = try projection.getParamProjections( method: method, genericTypeArgs: typeGenericArgs, abiKind: methodKind) @@ -77,7 +77,7 @@ fileprivate func writeCOMInteropMethod( // Generic instantiations can exist in multiple modules, so use internal visibility to avoid collisions try writer.writeFunc( visibility: visibility, - name: SwiftProjection.toInteropMethodName(method), + name: Projection.toInteropMethodName(method), params: paramProjections.map { $0.toSwiftParam() }, throws: true, returnType: returnProjection.map { $0.typeProjection.swiftType }) { writer in try writeSwiftToABICall( diff --git a/Generator/Sources/SwiftWinRT/Writing/ClassDefinition.swift b/Generator/Sources/SwiftWinRT/Writing/ClassDefinition.swift index 7e7c212b..fd9ebca0 100644 --- a/Generator/Sources/SwiftWinRT/Writing/ClassDefinition.swift +++ b/Generator/Sources/SwiftWinRT/Writing/ClassDefinition.swift @@ -5,7 +5,7 @@ import ProjectionModel import WindowsMetadata import struct Foundation.UUID -internal func writeClassDefinition(_ classDefinition: ClassDefinition, projection: SwiftProjection, to writer: SwiftSourceFileWriter) throws { +internal func writeClassDefinition(_ classDefinition: ClassDefinition, projection: Projection, to writer: SwiftSourceFileWriter) throws { let classKind = try ClassKind(classDefinition) let interfaces = try ClassInterfaces(of: classDefinition, kind: classKind) let typeName = try projection.toTypeName(classDefinition) @@ -39,7 +39,7 @@ internal func writeClassDefinition(_ classDefinition: ClassDefinition, projectio try writer.writeClass( documentation: projection.getDocumentationComment(classDefinition), - visibility: SwiftProjection.toVisibility(classDefinition.visibility, inheritableClass: !classDefinition.isSealed), + visibility: Projection.toVisibility(classDefinition.visibility, inheritableClass: !classDefinition.isSealed), final: classDefinition.isSealed, name: typeName, base: base, protocolConformances: protocolConformances) { writer in try writeClassMembers( classDefinition, interfaces: interfaces, kind: classKind, @@ -52,7 +52,7 @@ internal func writeClassDefinition(_ classDefinition: ClassDefinition, projectio try writer.writeEnum( documentation: projection.getDocumentationComment(classDefinition), - visibility: SwiftProjection.toVisibility(classDefinition.visibility), + visibility: Projection.toVisibility(classDefinition.visibility), name: typeName) { writer in try writeClassMembers( classDefinition, interfaces: interfaces, kind: .static, @@ -135,7 +135,7 @@ fileprivate struct ClassInterfaces { fileprivate func writeClassMembers( _ classDefinition: ClassDefinition, interfaces: ClassInterfaces, kind: ClassKind, - projection: SwiftProjection, to writer: SwiftTypeDefinitionWriter) throws { + projection: Projection, to writer: SwiftTypeDefinitionWriter) throws { try writeGenericTypeAliases(interfaces: classDefinition.baseInterfaces.map { try $0.interface }, projection: projection, to: writer) try writeClassInterfaceImplementations( @@ -158,7 +158,7 @@ fileprivate func writeClassMembers( fileprivate func writeClassInterfaceImplementations( _ classDefinition: ClassDefinition, interfaces: ClassInterfaces, kind: ClassKind, - projection: SwiftProjection, to writer: SwiftTypeDefinitionWriter) throws { + projection: Projection, to writer: SwiftTypeDefinitionWriter) throws { if interfaces.hasDefaultFactory { writeMarkComment(forInterface: "IActivationFactory", to: writer) try writeDefaultActivatableInitializer(classDefinition, projection: projection, to: writer) @@ -208,7 +208,7 @@ fileprivate func writeClassInterfaceImplementations( fileprivate func writeClassInterfaceProperties( _ classDefinition: ClassDefinition, interfaces: ClassInterfaces, kind: ClassKind, - projection: SwiftProjection, to writer: SwiftTypeDefinitionWriter) throws { + projection: Projection, to writer: SwiftTypeDefinitionWriter) throws { // Instance properties, initializers and deinit if kind.isComposable, let defaultInterface = interfaces.default { try SecondaryInterfaces.writeDeclaration( @@ -241,7 +241,7 @@ fileprivate func writeClassInterfaceProperties( fileprivate func writeClassOverrideSupport( _ classDefinition: ClassDefinition, interfaces: [BoundInterface], - projection: SwiftProjection, to writer: SwiftTypeDefinitionWriter) throws { + projection: Projection, to writer: SwiftTypeDefinitionWriter) throws { let outerPropertySuffix = "outer" for interface in interfaces { @@ -292,7 +292,7 @@ fileprivate func writeMarkComment(forInterface interfaceName: String, to writer: fileprivate func writeComposableInitializers( _ classDefinition: ClassDefinition, factoryInterface: InterfaceDefinition, base: ClassDefinition?, - projection: SwiftProjection, to writer: SwiftTypeDefinitionWriter) throws { + projection: Projection, to writer: SwiftTypeDefinitionWriter) throws { let propertyName = SecondaryInterfaces.getPropertyName(factoryInterface.bind()) for method in factoryInterface.methods { @@ -311,7 +311,7 @@ fileprivate func writeComposableInitializers( let innerObjectParamName = params[params.count - 1].name output.writeFullLine("(\(outerObjectParamName), \(innerObjectParamName): inout IInspectablePointer?) in") try writeInteropMethodCall( - name: SwiftProjection.toInteropMethodName(method), params: params, returnParam: returnParam, + name: Projection.toInteropMethodName(method), params: params, returnParam: returnParam, thisPointer: .init(name: "Self.\(propertyName)", lazy: true), projection: projection, to: writer.output) } @@ -321,7 +321,7 @@ fileprivate func writeComposableInitializers( fileprivate func writeDefaultActivatableInitializer( _ classDefinition: ClassDefinition, - projection: SwiftProjection, to writer: SwiftTypeDefinitionWriter) throws { + projection: Projection, to writer: SwiftTypeDefinitionWriter) throws { let documentationComment: SwiftDocumentationComment? if let constructor = classDefinition.findConstructor(arity: 0, inherited: false) { documentationComment = try projection.getDocumentationComment(constructor) @@ -340,7 +340,7 @@ fileprivate func writeDefaultActivatableInitializer( fileprivate func writeActivatableInitializers( _ classDefinition: ClassDefinition, activationFactory: InterfaceDefinition, - projection: SwiftProjection, to writer: SwiftTypeDefinitionWriter) throws { + projection: Projection, to writer: SwiftTypeDefinitionWriter) throws { let propertyName = SecondaryInterfaces.getPropertyName(activationFactory.bind()) for method in activationFactory.methods { let (params, returnParam) = try projection.getParamProjections(method: method, genericTypeArgs: [], abiKind: .activationFactory) @@ -356,7 +356,7 @@ fileprivate func writeActivatableInitializers( let output = writer.output output.write("self.init(_wrapping: ") try writeInteropMethodCall( - name: SwiftProjection.toInteropMethodName(method), params: params, returnParam: returnParam, + name: Projection.toInteropMethodName(method), params: params, returnParam: returnParam, thisPointer: .init(name: "Self.\(propertyName)", lazy: true), projection: projection, to: writer.output) output.write(")") @@ -366,7 +366,7 @@ fileprivate func writeActivatableInitializers( } fileprivate func writeSupportComposableInitializers( - defaultInterface: BoundInterface, projection: SwiftProjection, to writer: SwiftTypeDefinitionWriter) throws { + defaultInterface: BoundInterface, projection: Projection, to writer: SwiftTypeDefinitionWriter) throws { // public init(_wrapping inner: COMReference) { // super.init(_wrapping: inner.cast()) // } diff --git a/Generator/Sources/SwiftWinRT/Writing/EnumDefinition.swift b/Generator/Sources/SwiftWinRT/Writing/EnumDefinition.swift index faa7c3fa..647fc091 100644 --- a/Generator/Sources/SwiftWinRT/Writing/EnumDefinition.swift +++ b/Generator/Sources/SwiftWinRT/Writing/EnumDefinition.swift @@ -2,7 +2,7 @@ import CodeWriters import DotNetMetadata import ProjectionModel -internal func writeEnumDefinition(_ enumDefinition: EnumDefinition, projection: SwiftProjection, to writer: SwiftSourceFileWriter) throws { +internal func writeEnumDefinition(_ enumDefinition: EnumDefinition, projection: Projection, to writer: SwiftSourceFileWriter) throws { if SupportModules.WinRT.getBuiltInTypeKind(enumDefinition) != nil { // Defined in WindowsRuntime, merely reexport it here. let typeName = try projection.toTypeName(enumDefinition) @@ -18,14 +18,14 @@ internal func writeEnumDefinition(_ enumDefinition: EnumDefinition, projection: } } -fileprivate func writeOpenEnumDefinition(_ enumDefinition: EnumDefinition, projection: SwiftProjection, to writer: SwiftSourceFileWriter) throws { +fileprivate func writeOpenEnumDefinition(_ enumDefinition: EnumDefinition, projection: Projection, to writer: SwiftSourceFileWriter) throws { // Enums are syntactic sugar for integers in .NET, // so we cannot guarantee that the enumerants are exhaustive, // therefore we cannot project them to Swift enums // since they would be unable to represent unknown values. try writer.writeStruct( documentation: projection.getDocumentationComment(enumDefinition), - visibility: SwiftProjection.toVisibility(enumDefinition.visibility), + visibility: Projection.toVisibility(enumDefinition.visibility), name: try projection.toTypeName(enumDefinition), protocolConformances: [ .identifier(name: enumDefinition.isFlags ? "OptionSet" : "RawRepresentable"), @@ -41,22 +41,22 @@ fileprivate func writeOpenEnumDefinition(_ enumDefinition: EnumDefinition, proje } for field in enumDefinition.fields.filter({ $0.visibility == .public && $0.isStatic }) { - let value = SwiftProjection.toConstant(try field.literalValue!) + let value = Projection.toConstant(try field.literalValue!) // Avoid "warning: static property '' produces an empty option set" let initializer = value == "0" ? "Self()" : "Self(rawValue: \(value))" try writer.writeStoredProperty( documentation: projection.getDocumentationComment(field), visibility: .public, static: true, declarator: .let, - name: SwiftProjection.toMemberName(field), + name: Projection.toMemberName(field), initialValue: initializer) } } } -fileprivate func writeClosedEnumDefinition(_ enumDefinition: EnumDefinition, projection: SwiftProjection, to writer: SwiftSourceFileWriter) throws { +fileprivate func writeClosedEnumDefinition(_ enumDefinition: EnumDefinition, projection: Projection, to writer: SwiftSourceFileWriter) throws { try writer.writeEnum( documentation: projection.getDocumentationComment(enumDefinition), - visibility: SwiftProjection.toVisibility(enumDefinition.visibility), + visibility: Projection.toVisibility(enumDefinition.visibility), name: try projection.toTypeName(enumDefinition), rawValueType: try projection.toType(enumDefinition.underlyingType.bindNode()), protocolConformances: [ @@ -66,8 +66,8 @@ fileprivate func writeClosedEnumDefinition(_ enumDefinition: EnumDefinition, pro for field in enumDefinition.fields.filter({ $0.visibility == .public && $0.isStatic }) { try writer.writeEnumCase( documentation: projection.getDocumentationComment(field), - name: SwiftProjection.toMemberName(field), - rawValue: SwiftProjection.toConstant(try field.literalValue!)) + name: Projection.toMemberName(field), + rawValue: Projection.toConstant(try field.literalValue!)) } } } \ No newline at end of file diff --git a/Generator/Sources/SwiftWinRT/Writing/ExeManifest.swift b/Generator/Sources/SwiftWinRT/Writing/ExeManifest.swift index 6962f5ef..29f9c6a8 100644 --- a/Generator/Sources/SwiftWinRT/Writing/ExeManifest.swift +++ b/Generator/Sources/SwiftWinRT/Writing/ExeManifest.swift @@ -4,7 +4,7 @@ import ProjectionModel import FoundationXML import struct Foundation.URL -internal func writeExeManifestFile(projectionConfig: ProjectionConfig, projection: SwiftProjection, toPath path: String) throws { +internal func writeExeManifestFile(projectionConfig: ProjectionConfig, projection: Projection, toPath path: String) throws { var activatableClassesPerFileName: OrderedDictionary = [:] for module in projection.modulesByName.values { guard let moduleConfig = projectionConfig.modules[module.name], diff --git a/Generator/Sources/SwiftWinRT/Writing/ExtensionProperties.swift b/Generator/Sources/SwiftWinRT/Writing/ExtensionProperties.swift index f3225623..7399cbe4 100644 --- a/Generator/Sources/SwiftWinRT/Writing/ExtensionProperties.swift +++ b/Generator/Sources/SwiftWinRT/Writing/ExtensionProperties.swift @@ -4,7 +4,7 @@ import ProjectionModel internal func writeExtensionProperties( typeDefinition: TypeDefinition, interfaces: [InterfaceDefinition], static: Bool, - projection: SwiftProjection, to writer: SwiftSourceFileWriter) throws { + projection: Projection, to writer: SwiftSourceFileWriter) throws { // Only write the extension if we have at least one property (which needs a getter) let hasGetters = try interfaces.contains { try $0.properties.contains { try $0.getter != nil } } guard hasGetters else { return } @@ -29,7 +29,7 @@ internal func writeExtensionProperties( internal func writeNonthrowingPropertyImplementation( property: Property, static: Bool, - projection: SwiftProjection, to writer: SwiftTypeDefinitionWriter) throws { + projection: Projection, to writer: SwiftTypeDefinitionWriter) throws { guard let getter = try property.getter else { return } let selfKeyword = `static` ? "Self" : "self" @@ -37,7 +37,7 @@ internal func writeNonthrowingPropertyImplementation( let writeSetter: ((inout SwiftStatementWriter) throws -> Void)? if let setter = try property.setter { writeSetter = { writer in - writer.writeStatement("try! \(selfKeyword).\(SwiftProjection.toMemberName(setter))(newValue)") + writer.writeStatement("try! \(selfKeyword).\(Projection.toMemberName(setter))(newValue)") } } else { writeSetter = nil @@ -54,13 +54,13 @@ internal func writeNonthrowingPropertyImplementation( documentation: projection.getDocumentationComment(property), visibility: .public, static: `static`, - name: SwiftProjection.toMemberName(property), + name: Projection.toMemberName(property), type: propertyType, get: { writer in let output = writer.output output.write("try! ") if catchNullResult { output.write("NullResult.catch(") } - output.write("\(selfKeyword).\(SwiftProjection.toMemberName(getter))()") + output.write("\(selfKeyword).\(Projection.toMemberName(getter))()") if catchNullResult { output.write(")") } }, set: writeSetter) diff --git a/Generator/Sources/SwiftWinRT/Writing/InterfaceDefinition.swift b/Generator/Sources/SwiftWinRT/Writing/InterfaceDefinition.swift index 3ee79ef9..68a8480b 100644 --- a/Generator/Sources/SwiftWinRT/Writing/InterfaceDefinition.swift +++ b/Generator/Sources/SwiftWinRT/Writing/InterfaceDefinition.swift @@ -14,7 +14,7 @@ import WindowsMetadata // This provides a more natural (C#-like) syntax when using those types: // // var foo: IFoo? = getFoo() -internal func writeInterfaceDefinition(_ interface: InterfaceDefinition, projection: SwiftProjection, to writer: SwiftSourceFileWriter) throws { +internal func writeInterfaceDefinition(_ interface: InterfaceDefinition, projection: Projection, to writer: SwiftSourceFileWriter) throws { if SupportModules.WinRT.getBuiltInTypeKind(interface) != nil { // Defined in WindowsRuntime, merely reexport it here. let protocolName = try projection.toProtocolName(interface) @@ -31,7 +31,7 @@ internal func writeInterfaceDefinition(_ interface: InterfaceDefinition, project } } -fileprivate func writeProtocol(_ interfaceDefinition: InterfaceDefinition, projection: SwiftProjection, to writer: SwiftSourceFileWriter) throws { +fileprivate func writeProtocol(_ interfaceDefinition: InterfaceDefinition, projection: Projection, to writer: SwiftSourceFileWriter) throws { var baseProtocols = [SwiftType]() var whereGenericConstraints = OrderedDictionary() for baseInterface in interfaceDefinition.baseInterfaces { @@ -54,7 +54,7 @@ fileprivate func writeProtocol(_ interfaceDefinition: InterfaceDefinition, proje let protocolName = try projection.toProtocolName(interfaceDefinition) try writer.writeProtocol( documentation: documentation.map { projection.toDocumentationComment($0) }, - visibility: SwiftProjection.toVisibility(interfaceDefinition.visibility), + visibility: Projection.toVisibility(interfaceDefinition.visibility), name: protocolName, typeParams: interfaceDefinition.genericParams.map { $0.name }, bases: baseProtocols, @@ -72,8 +72,8 @@ fileprivate func writeProtocol(_ interfaceDefinition: InterfaceDefinition, proje guard method.nameKind == .regular else { continue } try writer.writeFunc( documentation: projection.getDocumentationComment(method), - attributes: SwiftProjection.getSwiftAttributes(method), - name: SwiftProjection.toMemberName(method), + attributes: Projection.getSwiftAttributes(method), + name: Projection.toMemberName(method), typeParams: method.genericParams.map { $0.name }, params: method.params.map { try projection.toParameter($0) }, throws: true, @@ -84,7 +84,7 @@ fileprivate func writeProtocol(_ interfaceDefinition: InterfaceDefinition, proje if let addAccessor = try event.addAccessor { try writer.writeFunc( documentation: projection.getDocumentationComment(event), - name: SwiftProjection.toMemberName(event), + name: Projection.toMemberName(event), params: addAccessor.params.map { try projection.toParameter(label: "adding", $0) }, throws: true, returnType: SupportModules.WinRT.eventRegistration) @@ -92,7 +92,7 @@ fileprivate func writeProtocol(_ interfaceDefinition: InterfaceDefinition, proje if let removeAccessor = try event.removeAccessor { try writer.writeFunc( - name: SwiftProjection.toMemberName(event), + name: Projection.toMemberName(event), params: removeAccessor.params.map { try projection.toParameter(label: "removing", $0) }, throws: true) } @@ -105,7 +105,7 @@ fileprivate func writeProtocol(_ interfaceDefinition: InterfaceDefinition, proje if let getter = try property.getter { try writer.writeFunc( documentation: projection.getDocumentationComment(property), - name: SwiftProjection.toMemberName(getter), + name: Projection.toMemberName(getter), throws: true, returnType: projection.toReturnType(property.type)) } @@ -113,7 +113,7 @@ fileprivate func writeProtocol(_ interfaceDefinition: InterfaceDefinition, proje if let setter = try property.setter { try writer.writeFunc( isPropertySetter: true, - name: SwiftProjection.toMemberName(setter), + name: Projection.toMemberName(setter), params: setter.params.map { try projection.toParameter($0) }, throws: true) } @@ -126,10 +126,10 @@ fileprivate func writeProtocol(_ interfaceDefinition: InterfaceDefinition, proje projection: projection, to: writer) } -fileprivate func writeProtocolTypeAlias(_ interfaceDefinition: InterfaceDefinition, projection: SwiftProjection, to writer: SwiftSourceFileWriter) throws { +fileprivate func writeProtocolTypeAlias(_ interfaceDefinition: InterfaceDefinition, projection: Projection, to writer: SwiftSourceFileWriter) throws { writer.writeTypeAlias( documentation: projection.getDocumentationComment(interfaceDefinition), - visibility: SwiftProjection.toVisibility(interfaceDefinition.visibility), + visibility: Projection.toVisibility(interfaceDefinition.visibility), name: try projection.toTypeName(interfaceDefinition), typeParams: interfaceDefinition.genericParams.map { $0.name }, target: .identifier( diff --git a/Generator/Sources/SwiftWinRT/Writing/InterfaceImplementation.swift b/Generator/Sources/SwiftWinRT/Writing/InterfaceImplementation.swift index 87eff5db..e3d01187 100644 --- a/Generator/Sources/SwiftWinRT/Writing/InterfaceImplementation.swift +++ b/Generator/Sources/SwiftWinRT/Writing/InterfaceImplementation.swift @@ -16,7 +16,7 @@ internal struct ThisPointer { internal func writeInterfaceImplementation( abiType: BoundType, classDefinition: ClassDefinition? = nil, documentation: Bool = true, overridable: Bool = false, static: Bool = false, thisPointer: ThisPointer, - projection: SwiftProjection, to writer: SwiftTypeDefinitionWriter) throws { + projection: Projection, to writer: SwiftTypeDefinitionWriter) throws { for method in abiType.definition.methods { guard method.isPublic && !(method is Constructor) else { continue } // Generate Delegate.Invoke as a regular method @@ -45,7 +45,7 @@ internal func writeInterfaceImplementation( fileprivate func writeInterfacePropertyImplementation( _ property: Property, typeGenericArgs: [TypeNode], classDefinition: ClassDefinition?, documentation: Bool, overridable: Bool, static: Bool, thisPointer: ThisPointer, - projection: SwiftProjection, to writer: SwiftTypeDefinitionWriter) throws { + projection: Projection, to writer: SwiftTypeDefinitionWriter) throws { if try property.definingType.hasAttribute(ExclusiveToAttribute.self) { // The property is exclusive to this class so it doesn't come // from an interface that would an extension property. @@ -61,11 +61,11 @@ fileprivate func writeInterfacePropertyImplementation( documentation: documentation ? projection.getDocumentationComment(abiMember: property, classDefinition: classDefinition) : nil, visibility: overridable ? .open : .public, static: `static`, - name: SwiftProjection.toMemberName(getter), + name: Projection.toMemberName(getter), throws: true, returnType: returnParamProjection.swiftType) { writer throws in try writeInteropMethodCall( - name: SwiftProjection.toInteropMethodName(getter), params: [], returnParam: returnParamProjection, + name: Projection.toInteropMethodName(getter), params: [], returnParam: returnParamProjection, thisPointer: thisPointer, projection: projection, to: writer.output) } } @@ -78,11 +78,11 @@ fileprivate func writeInterfacePropertyImplementation( documentation: documentation ? projection.getDocumentationComment(abiMember: property, classDefinition: classDefinition) : nil, visibility: .public, static: `static`, - name: SwiftProjection.toMemberName(setter), + name: Projection.toMemberName(setter), params: [ newValueParamProjection.toSwiftParam() ], throws: true) { writer throws in try writeInteropMethodCall( - name: SwiftProjection.toInteropMethodName(setter), + name: Projection.toInteropMethodName(setter), params: [ newValueParamProjection ], returnParam: nil, thisPointer: thisPointer, projection: projection, to: writer.output) } @@ -92,8 +92,8 @@ fileprivate func writeInterfacePropertyImplementation( fileprivate func writeInterfaceEventImplementation( _ event: Event, typeGenericArgs: [TypeNode], classDefinition: ClassDefinition?, documentation: Bool, overridable: Bool, static: Bool, thisPointer: ThisPointer, - projection: SwiftProjection, to writer: SwiftTypeDefinitionWriter) throws { - let name = SwiftProjection.toMemberName(event) + projection: Projection, to writer: SwiftTypeDefinitionWriter) throws { + let name = Projection.toMemberName(event) // public [static] func myEvent(adding handler: @escaping MyEventHandler) throws -> EventRegistration { ... } if let addAccessor = try event.addAccessor, let handlerParameter = try addAccessor.params.first { @@ -108,7 +108,7 @@ fileprivate func writeInterfaceEventImplementation( let output = writer.output output.write("let _token = ") try writeInteropMethodCall( - name: SwiftProjection.toInteropMethodName(addAccessor), + name: Projection.toInteropMethodName(addAccessor), params: [ handlerParamProjection ], returnParam: nil, thisPointer: thisPointer, projection: projection, to: output) output.endLine() @@ -127,7 +127,7 @@ fileprivate func writeInterfaceEventImplementation( params: [ tokenParamProjection.toSwiftParam(label: "removing") ], throws: true) { writer throws in try writeInteropMethodCall( - name: SwiftProjection.toInteropMethodName(removeAccessor), + name: Projection.toInteropMethodName(removeAccessor), params: [ tokenParamProjection ], returnParam: nil, thisPointer: thisPointer, projection: projection, to: writer.output) } @@ -137,19 +137,19 @@ fileprivate func writeInterfaceEventImplementation( fileprivate func writeInterfaceMethodImplementation( _ method: Method, typeGenericArgs: [TypeNode], classDefinition: ClassDefinition?, documentation: Bool, overridable: Bool, static: Bool, thisPointer: ThisPointer, - projection: SwiftProjection, to writer: SwiftTypeDefinitionWriter) throws { + projection: Projection, to writer: SwiftTypeDefinitionWriter) throws { let (params, returnParam) = try projection.getParamProjections(method: method, genericTypeArgs: typeGenericArgs) try writer.writeFunc( documentation: documentation ? projection.getDocumentationComment(abiMember: method, classDefinition: classDefinition) : nil, - attributes: SwiftProjection.getSwiftAttributes(method), + attributes: Projection.getSwiftAttributes(method), visibility: overridable ? .open : .public, static: `static`, - name: SwiftProjection.toMemberName(method), + name: Projection.toMemberName(method), params: params.map { $0.toSwiftParam() }, throws: true, returnType: returnParam?.swiftType) { writer throws in try writeInteropMethodCall( - name: SwiftProjection.toInteropMethodName(method), + name: Projection.toInteropMethodName(method), params: params, returnParam: returnParam, thisPointer: thisPointer, projection: projection, to: writer.output) } @@ -157,7 +157,7 @@ fileprivate func writeInterfaceMethodImplementation( internal func writeInteropMethodCall( name: String, params: [ParamProjection], returnParam: ParamProjection?, thisPointer: ThisPointer, - projection: SwiftProjection, to output: IndentedTextOutputStream) throws { + projection: Projection, to output: IndentedTextOutputStream) throws { let nullReturnAsError = { if let returnParam, case .return(nullAsError: true) = returnParam.passBy { return true } diff --git a/Generator/Sources/SwiftWinRT/Writing/NamespaceAlias.swift b/Generator/Sources/SwiftWinRT/Writing/NamespaceAlias.swift index d1567a49..b39a3e72 100644 --- a/Generator/Sources/SwiftWinRT/Writing/NamespaceAlias.swift +++ b/Generator/Sources/SwiftWinRT/Writing/NamespaceAlias.swift @@ -2,9 +2,9 @@ import CodeWriters import DotNetMetadata import ProjectionModel -internal func writeNamespaceAlias(_ typeDefinition: TypeDefinition, projection: SwiftProjection, to writer: SwiftSourceFileWriter) throws { +internal func writeNamespaceAlias(_ typeDefinition: TypeDefinition, projection: Projection, to writer: SwiftSourceFileWriter) throws { try writer.writeTypeAlias( - visibility: SwiftProjection.toVisibility(typeDefinition.visibility), + visibility: Projection.toVisibility(typeDefinition.visibility), name: projection.toTypeName(typeDefinition, namespaced: false), typeParams: typeDefinition.genericParams.map { $0.name }, target: SwiftType.identifier( @@ -13,7 +13,7 @@ internal func writeNamespaceAlias(_ typeDefinition: TypeDefinition, projection: if let interface = typeDefinition as? InterfaceDefinition { try writer.writeProtocol( - visibility: SwiftProjection.toVisibility(interface.visibility), + visibility: Projection.toVisibility(interface.visibility), name: projection.toProtocolName(interface, namespaced: false), typeParams: interface.genericParams.map { $0.name }, bases: [projection.toBaseProtocol(interface)]) { _ in } @@ -21,7 +21,7 @@ internal func writeNamespaceAlias(_ typeDefinition: TypeDefinition, projection: if typeDefinition is InterfaceDefinition || typeDefinition is DelegateDefinition { try writer.writeTypeAlias( - visibility: SwiftProjection.toVisibility(typeDefinition.visibility), + visibility: Projection.toVisibility(typeDefinition.visibility), name: projection.toProjectionTypeName(typeDefinition, namespaced: false), target: SwiftType.identifier( name: projection.toProjectionTypeName(typeDefinition))) diff --git a/Generator/Sources/SwiftWinRT/Writing/SecondaryInterfaces.swift b/Generator/Sources/SwiftWinRT/Writing/SecondaryInterfaces.swift index fecf92cb..105be856 100644 --- a/Generator/Sources/SwiftWinRT/Writing/SecondaryInterfaces.swift +++ b/Generator/Sources/SwiftWinRT/Writing/SecondaryInterfaces.swift @@ -7,7 +7,7 @@ import WindowsMetadata internal enum SecondaryInterfaces { internal static func writeDeclaration( _ interface: BoundInterface, static: Bool, composable: Bool = false, - projection: SwiftProjection, to writer: SwiftTypeDefinitionWriter) throws { + projection: Projection, to writer: SwiftTypeDefinitionWriter) throws { let interfaceName = try projection.toTypeName(interface.definition, namespaced: false) let abiStructType = try projection.toABIType(interface.asBoundType) @@ -41,7 +41,7 @@ internal enum SecondaryInterfaces { internal static let activationFactoryPropertyName = "_iactivationFactory" internal static func writeActivationFactoryDeclaration( - classDefinition: ClassDefinition, projection: SwiftProjection, to writer: SwiftTypeDefinitionWriter) throws { + classDefinition: ClassDefinition, projection: Projection, to writer: SwiftTypeDefinitionWriter) throws { let storedPropertyName = "_lazyIActivationFactory" let abiStructType = SwiftType.identifier("SWRT_IActivationFactory") diff --git a/Generator/Sources/SwiftWinRT/Writing/SourcePreamble.swift b/Generator/Sources/SwiftWinRT/Writing/SourcePreamble.swift index 35ee69c6..8efe22ff 100644 --- a/Generator/Sources/SwiftWinRT/Writing/SourcePreamble.swift +++ b/Generator/Sources/SwiftWinRT/Writing/SourcePreamble.swift @@ -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 { diff --git a/Generator/Sources/SwiftWinRT/Writing/StructDefinition.swift b/Generator/Sources/SwiftWinRT/Writing/StructDefinition.swift index 3c05c752..1311b1a6 100644 --- a/Generator/Sources/SwiftWinRT/Writing/StructDefinition.swift +++ b/Generator/Sources/SwiftWinRT/Writing/StructDefinition.swift @@ -3,7 +3,7 @@ import DotNetMetadata import ProjectionModel import WindowsMetadata -internal func writeStructDefinition(_ structDefinition: StructDefinition, projection: SwiftProjection, to writer: SwiftSourceFileWriter) throws { +internal func writeStructDefinition(_ structDefinition: StructDefinition, projection: Projection, to writer: SwiftSourceFileWriter) throws { if SupportModules.WinRT.getBuiltInTypeKind(structDefinition) != nil { // Defined in WindowsRuntime, merely reexport it here. let typeName = try projection.toTypeName(structDefinition) @@ -16,7 +16,7 @@ internal func writeStructDefinition(_ structDefinition: StructDefinition, projec ] try writer.writeStruct( documentation: projection.getDocumentationComment(structDefinition), - visibility: SwiftProjection.toVisibility(structDefinition.visibility), + visibility: Projection.toVisibility(structDefinition.visibility), name: try projection.toTypeName(structDefinition), typeParams: structDefinition.genericParams.map { $0.name }, protocolConformances: protocolConformances) { writer throws in @@ -27,18 +27,18 @@ internal func writeStructDefinition(_ structDefinition: StructDefinition, projec } } -fileprivate func writeStructFields(_ structDefinition: StructDefinition, projection: SwiftProjection, to writer: SwiftTypeDefinitionWriter) throws { +fileprivate func writeStructFields(_ structDefinition: StructDefinition, projection: Projection, to writer: SwiftTypeDefinitionWriter) throws { for field in structDefinition.fields { assert(field.isInstance && !field.isInitOnly && field.isPublic) try writer.writeStoredProperty( documentation: projection.getDocumentationComment(field), - visibility: .public, declarator: .var, name: SwiftProjection.toMemberName(field), + visibility: .public, declarator: .var, name: Projection.toMemberName(field), type: projection.toType(field.type)) } } -fileprivate func writeDefaultInitializer(_ structDefinition: StructDefinition, projection: SwiftProjection, to writer: SwiftTypeDefinitionWriter) throws { +fileprivate func writeDefaultInitializer(_ structDefinition: StructDefinition, projection: Projection, to writer: SwiftTypeDefinitionWriter) throws { func getInitializer(type: TypeNode) -> String { switch type { case .array(_): return "[]" @@ -62,17 +62,17 @@ fileprivate func writeDefaultInitializer(_ structDefinition: StructDefinition, p try writer.writeInit(visibility: .public, params: []) { writer in for field in structDefinition.fields { - let name = SwiftProjection.toMemberName(field) + let name = Projection.toMemberName(field) let initializer = getInitializer(type: try field.type) writer.writeStatement("self.\(name) = \(initializer)") } } } -fileprivate func writeFieldwiseInitializer(_ structDefinition: StructDefinition, projection: SwiftProjection, to writer: SwiftTypeDefinitionWriter) throws { +fileprivate func writeFieldwiseInitializer(_ structDefinition: StructDefinition, projection: Projection, to writer: SwiftTypeDefinitionWriter) throws { let params = try structDefinition.fields .filter { $0.visibility == .public && $0.isInstance } - .map { SwiftParam(name: SwiftProjection.toMemberName($0), type: try projection.toType($0.type)) } + .map { SwiftParam(name: Projection.toMemberName($0), type: try projection.toType($0.type)) } guard !params.isEmpty else { return } writer.writeInit(visibility: .public, params: params) { diff --git a/Generator/Sources/SwiftWinRT/Writing/SwiftPackageFile.swift b/Generator/Sources/SwiftWinRT/Writing/SwiftPackageFile.swift index 1e00f550..ef2b1246 100644 --- a/Generator/Sources/SwiftWinRT/Writing/SwiftPackageFile.swift +++ b/Generator/Sources/SwiftWinRT/Writing/SwiftPackageFile.swift @@ -5,7 +5,7 @@ import ProjectionModel import struct Foundation.URL func writeSwiftPackageFile( - _ projection: SwiftProjection, + _ projection: Projection, supportPackageLocation: String, excludeCMakeLists: Bool, dynamicLibraries: Bool, @@ -53,7 +53,7 @@ func writeSwiftPackageFile( for namespace in namespaces { var namespaceModuleTarget: SwiftPackage.Target = .target( name: module.getNamespaceModuleName(namespace: namespace)) - let compactNamespace = SwiftProjection.toCompactNamespace(namespace) + let compactNamespace = Projection.toCompactNamespace(namespace) namespaceModuleTarget.path = "\(module.name)/Namespaces/\(compactNamespace)" namespaceModuleTarget.dependencies.append(.target(name: module.name)) package.targets.append(namespaceModuleTarget) diff --git a/Generator/Sources/SwiftWinRT/Writing/TypeDefinition.swift b/Generator/Sources/SwiftWinRT/Writing/TypeDefinition.swift index 6382641e..f1b771d2 100644 --- a/Generator/Sources/SwiftWinRT/Writing/TypeDefinition.swift +++ b/Generator/Sources/SwiftWinRT/Writing/TypeDefinition.swift @@ -2,7 +2,7 @@ import CodeWriters import DotNetMetadata import ProjectionModel -internal func writeTypeDefinition(_ typeDefinition: TypeDefinition, projection: SwiftProjection, to writer: SwiftSourceFileWriter) throws { +internal func writeTypeDefinition(_ typeDefinition: TypeDefinition, projection: Projection, to writer: SwiftSourceFileWriter) throws { switch typeDefinition { case let structDefinition as StructDefinition: try writeStructDefinition(structDefinition, projection: projection, to: writer) @@ -19,10 +19,10 @@ internal func writeTypeDefinition(_ typeDefinition: TypeDefinition, projection: } } -fileprivate func writeDelegateDefinition(_ delegateDefinition: DelegateDefinition, projection: SwiftProjection, to writer: SwiftSourceFileWriter) throws { +fileprivate func writeDelegateDefinition(_ delegateDefinition: DelegateDefinition, projection: Projection, to writer: SwiftSourceFileWriter) throws { try writer.writeTypeAlias( documentation: projection.getDocumentationComment(delegateDefinition), - visibility: SwiftProjection.toVisibility(delegateDefinition.visibility), + visibility: Projection.toVisibility(delegateDefinition.visibility), name: try projection.toTypeName(delegateDefinition), typeParams: delegateDefinition.genericParams.map { $0.name }, target: .function( diff --git a/Generator/Sources/SwiftWinRT/Writing/VirtualTable.swift b/Generator/Sources/SwiftWinRT/Writing/VirtualTable.swift index 0a0f7164..6bbe7039 100644 --- a/Generator/Sources/SwiftWinRT/Writing/VirtualTable.swift +++ b/Generator/Sources/SwiftWinRT/Writing/VirtualTable.swift @@ -5,7 +5,7 @@ import WindowsMetadata internal func writeVirtualTableProperty( visibility: SwiftVisibility = .private, name: String, abiType: BoundType, swiftType: BoundType, - projection: SwiftProjection, to writer: SwiftTypeDefinitionWriter) throws { + projection: Projection, to writer: SwiftTypeDefinitionWriter) throws { try writer.writeStoredProperty( visibility: visibility, static: true, declarator: .var, name: name, initializer: { output in try writeVirtualTable(abiType: abiType, swiftType: swiftType, projection: projection, to: output) }) @@ -13,7 +13,7 @@ internal func writeVirtualTableProperty( fileprivate func writeVirtualTable( abiType: BoundType, swiftType: BoundType, - projection: SwiftProjection, to output: IndentedTextOutputStream) throws { + projection: Projection, to output: IndentedTextOutputStream) throws { let vtableStructType: SwiftType = try projection.toABIVirtualTableType(abiType) try output.writeIndentedBlock(header: "\(vtableStructType)(", footer: ")") { // IUnknown methods @@ -50,7 +50,7 @@ fileprivate func writeVirtualTable( try output.writeIndentedBlock(header: " { this in") { try writeVirtualTableFunc( params: params, returnParam: returnParam, - swiftMemberName: SwiftProjection.toMemberName(method), + swiftMemberName: Projection.toMemberName(method), methodKind: WinRTMethodKind(from: method), to: output) } diff --git a/Generator/Sources/SwiftWinRT/createProjection.swift b/Generator/Sources/SwiftWinRT/createProjection.swift index e3293fce..841321cc 100644 --- a/Generator/Sources/SwiftWinRT/createProjection.swift +++ b/Generator/Sources/SwiftWinRT/createProjection.swift @@ -5,13 +5,13 @@ import OrderedCollections import ProjectionModel import WindowsMetadata -internal func createProjection(commandLineArguments: CommandLineArguments, projectionConfig: ProjectionConfig, winMDLoadContext: WinMDLoadContext) throws -> SwiftProjection { +internal func createProjection(commandLineArguments: CommandLineArguments, projectionConfig: ProjectionConfig, winMDLoadContext: WinMDLoadContext) throws -> Projection { var winMDFilePaths = OrderedSet(commandLineArguments.references) if let windowsSdkVersion = commandLineArguments.windowsSdkVersion { winMDFilePaths.formUnion(try getWindowsSdkWinMDPaths(sdkVersion: windowsSdkVersion)) } - let projection = SwiftProjection() + let projection = Projection() // Preload assemblies and create modules for filePath in winMDFilePaths.sorted(by: { $0.lastPathComponent < $1.lastPathComponent }) { @@ -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) } diff --git a/Generator/Sources/SwiftWinRT/writeProjectionFiles.swift b/Generator/Sources/SwiftWinRT/writeProjectionFiles.swift index 5282e157..79ff9f9c 100644 --- a/Generator/Sources/SwiftWinRT/writeProjectionFiles.swift +++ b/Generator/Sources/SwiftWinRT/writeProjectionFiles.swift @@ -7,7 +7,7 @@ import ProjectionModel import WindowsMetadata internal func writeProjectionFiles( - _ projection: SwiftProjection, + _ projection: Projection, directoryPath: String, generateCMakeLists: Bool, dynamicLibraries: Bool) throws { @@ -32,7 +32,7 @@ internal func writeProjectionFiles( } fileprivate func writeModuleFiles( - _ module: SwiftProjection.Module, + _ module: Module, directoryPath: String, generateCMakeLists: Bool, dynamicLibrary: Bool) throws { @@ -59,14 +59,14 @@ 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) { // All WinRT types should have namespaces guard let namespace = typeDefinition.namespace else { continue } - let compactNamespace = SwiftProjection.toCompactNamespace(namespace) + let compactNamespace = Projection.toCompactNamespace(namespace) let namespaceDirectoryPath = "\(directoryPath)\\\(compactNamespace)" let typeName = try module.projection.toTypeName(typeDefinition) @@ -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] = [] @@ -125,7 +125,7 @@ fileprivate func writeNamespaceModuleFiles(_ module: SwiftProjection.Module, dir guard !typeDefinitions.isEmpty else { continue } guard let namespace else { continue } - let compactNamespace = SwiftProjection.toCompactNamespace(namespace) + let compactNamespace = Projection.toCompactNamespace(namespace) compactNamespaces.append(compactNamespace) let namespaceAliasesPath = "\(directoryPath)\\\(compactNamespace)\\Aliases.swift" try writeNamespaceAliasesFile(typeDefinitions: typeDefinitions, module: module, toPath: namespaceAliasesPath) @@ -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) @@ -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 is implemented generically in the support module. if typeDefinition.namespace == "Windows.Foundation", typeDefinition.name == "IReference`1" { return false } @@ -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)