Skip to content

Commit

Permalink
Fix redundant reexports of IReference bindings (#481)
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlabelle authored Jan 15, 2025
1 parent f552d76 commit b781a5f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
13 changes: 0 additions & 13 deletions Generator/Sources/SwiftWinRT/Writing/ABIBinding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,6 @@ import struct Foundation.UUID

/// Writes a type or extension providing the ABIBinding conformance for a given projected WinRT type.
internal func writeABIBindingConformance(_ typeDefinition: TypeDefinition, genericArgs: [TypeNode]?, projection: Projection, to writer: SwiftSourceFileWriter) throws {
if SupportModules.WinRT.getBuiltInTypeKind(typeDefinition) == .definitionAndBinding {
// The support module already defines a projection, just reexport it.
if typeDefinition.isReferenceType {
writer.writeImport(exported: true, kind: .enum,
module: SupportModules.WinRT.moduleName,
symbolName: try projection.toBindingTypeName(typeDefinition))
}
else {
// The struct conforms to ABIBinding itself, and we already imported it.
}
return
}

if let structDefinition = typeDefinition as? StructDefinition {
assert(genericArgs == nil)
try writeStructBindingExtension(structDefinition, projection: projection, to: writer)
Expand Down
25 changes: 25 additions & 0 deletions Generator/Sources/SwiftWinRT/writeProjectionFiles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,36 @@ fileprivate func writeTypeDefinitionFile(
}

fileprivate func writeABIBindingConformanceFile(_ typeDefinition: TypeDefinition, module: Module, toPath path: String) throws {
let bindingDefinedInSupportModule = SupportModules.WinRT.getBuiltInTypeKind(typeDefinition) == .definitionAndBinding
if bindingDefinedInSupportModule {
if !module.hasTypeDefinition(typeDefinition) {
// This is a generic instantiation of a type that is defined in the support module.
// Since the support module defines the binding, it must be defined in a way that
// covers all generic instantiations, e.g. WindowsFoundation_IReferenceBinding.
return
}
if typeDefinition.isValueType {
// We're generating the module for WindowsFoundation and this type is a value type,
// like WindowsFoundation_Point. We're reexporting the type from the support module
// elsewhere, and it implements ABIBinding itself, so we don't need to generate it again.
return
}
}

let writer = SwiftSourceFileWriter(output: FileTextOutputStream(path: path, directoryCreation: .ancestors))
writeGeneratedCodePreamble(to: writer)
writeModulePreamble(module, to: writer)

if module.hasTypeDefinition(typeDefinition) {
if bindingDefinedInSupportModule {
// We're generating the module for WindowsFoundation and this type's binding is defined in the support module,
// like WindowsFoundation_IStringableBinding. Just reexport the binding here.
writer.writeImport(exported: true, kind: .enum,
module: SupportModules.WinRT.moduleName,
symbolName: try module.projection.toBindingTypeName(typeDefinition))
return
}

try writeABIBindingConformance(typeDefinition, genericArgs: nil, projection: module.projection, to: writer)
}

Expand Down

0 comments on commit b781a5f

Please sign in to comment.