Skip to content

Commit b3d535c

Browse files
authored
use a connected type rather than one single type (#201)
* use a connected type rather than one single type * remove makefromabi file
1 parent 35e2ea1 commit b3d535c

12 files changed

+693
-550
lines changed

Diff for: swiftwinrt/Resources/Support/MakeFromAbi.swift

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import Foundation
44
// when we cast to `any MakeFromAbi`, plus that requires a lot more exported
55
// simples than we want
66
public protocol MakeFromAbi {
7-
static func from(typeName: String, abi: SUPPORT_MODULE.IInspectable) -> Any?
7+
associatedtype SwiftType
8+
static func from(abi: SUPPORT_MODULE.IInspectable) -> SwiftType
89
}
910

1011
func make(typeName: SwiftTypeName, from abi: SUPPORT_MODULE.IInspectable) -> Any? {
11-
guard let makerType = NSClassFromString("\(typeName.module).__MakeFromAbi") as? any MakeFromAbi.Type else {
12+
guard let makerType = NSClassFromString("\(typeName.module).\(typeName.typeName)Maker") as? any MakeFromAbi.Type else {
1213
return nil
1314
}
14-
return makerType.from(typeName: typeName.typeName, abi: abi)
15+
return makerType.from(abi: abi)
1516
}
1617

1718
func makeFrom(abi: SUPPORT_MODULE.IInspectable) -> Any? {

Diff for: swiftwinrt/code_writers.h

+20-14
Original file line numberDiff line numberDiff line change
@@ -1203,31 +1203,37 @@ bind_bridge_fullname(type));
12031203
return true;
12041204
}
12051205

1206-
static void write_make_from_abi_case(writer& w, metadata_type const& type)
1207-
{
1208-
if (skip_write_from_abi(w, type)) return;
1209-
w.write("case \"%\": return make%From(abi: abi)\n", type.swift_type_name(), type.swift_type_name());
1210-
}
1211-
12121206
static void write_make_from_abi(writer& w, metadata_type const& type)
12131207
{
12141208
if (skip_write_from_abi(w, type)) return;
12151209

1216-
w.write("fileprivate func make%From(abi: %.IInspectable) -> Any {\n", type.swift_type_name(), w.support);
1217-
1210+
std::string fromAbi;
1211+
std::string swiftType;
12181212
if (is_interface(type))
12191213
{
1220-
auto indent = w.push_indent();
1221-
w.write("let swiftAbi: %.% = try! abi.QueryInterface()\n", abi_namespace(type),
1214+
fromAbi = w.write_temp("let swiftAbi: %.% = try! abi.QueryInterface()\n", abi_namespace(type),
12221215
type.swift_type_name());
1223-
w.write("return %.from(abi: RawPointer(swiftAbi))!\n", bind_bridge_fullname(type));
1216+
fromAbi += w.write_temp(" return %.from(abi: RawPointer(swiftAbi))!", bind_bridge_fullname(type));
1217+
swiftType = w.write_temp("%", bind<write_swift_interface_existential_identifier>(type));
12241218
}
12251219
else if (is_class(&type))
12261220
{
1227-
auto indent = w.push_indent();
1228-
w.write("return %(fromAbi: abi)\n", type.swift_type_name());
1221+
fromAbi = w.write_temp("return %(fromAbi: abi)", type.swift_type_name());
1222+
swiftType = w.write_temp("%", bind<write_swift_type_identifier>(type));
12291223
}
1230-
w.write("}\n\n");
1224+
else
1225+
{
1226+
throw std::exception("Invalid type for MakeFromAbi");
1227+
}
1228+
1229+
w.write(R"(^@_spi(WinRTInternal)
1230+
public class %Maker: MakeFromAbi {
1231+
public typealias SwiftType = %
1232+
public static func from(abi: %.IInspectable) -> SwiftType {
1233+
%
1234+
}
1235+
}
1236+
)", type.swift_type_name(), swiftType, w.support, fromAbi);
12311237
}
12321238

12331239
static void write_interface_bridge(writer& w, metadata_type const& type)

Diff for: swiftwinrt/file_writers.h

+4-32
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ namespace swiftwinrt
237237

238238
w.write("%", w.filter.bind_each<write_struct_bridgeable>(members.structs));
239239

240+
// MakeFromAbi has to be in impl file (or main file) otherwise they get stripped away
241+
w.write("%", w.filter.bind_each<write_make_from_abi>(members.interfaces));
242+
w.write("%", w.filter.bind_each<write_make_from_abi>(members.classes));
243+
240244
w.swap();
241245
write_preamble(w, /* swift_code: */ true);
242246

@@ -274,36 +278,4 @@ namespace swiftwinrt
274278
write_preamble(w, /* swift_code: */ true);
275279
w.save_file("Generics");
276280
}
277-
278-
static void write_module_make_from_abi(std::string_view const& module, type_cache const& members, include_only_used_filter const& filter)
279-
{
280-
writer w;
281-
w.filter = filter;
282-
w.support = settings.support;
283-
w.c_mod = settings.get_c_module_name();
284-
w.type_namespace = module;
285-
w.swift_module = module;
286-
w.cache = members.cache;
287-
w.write("%", w.filter.bind_each<write_make_from_abi>(members.interfaces));
288-
w.write("%", w.filter.bind_each<write_make_from_abi>(members.classes));
289-
290-
w.write("@_spi(__MakeFromAbi_DoNotImport)\n");
291-
w.write("public class __MakeFromAbi: MakeFromAbi {\n");
292-
w.write(" public static func from(typeName: String, abi: %.IInspectable) -> Any? {\n", w.support);
293-
w.write(" switch typeName {\n");
294-
{
295-
auto indent_guard = w.push_indent(indent{ 3 });
296-
w.write("%", w.filter.bind_each<write_make_from_abi_case>(members.interfaces));
297-
w.write("%", w.filter.bind_each<write_make_from_abi_case>(members.classes));
298-
}
299-
300-
w.write(" default: return nil\n");
301-
w.write(" }\n");
302-
w.write(" }\n");
303-
w.write("}\n");
304-
305-
w.swap();
306-
write_preamble(w, /* swift_code: */ true);
307-
w.save_file("MakeFromAbi");
308-
}
309281
}

Diff for: swiftwinrt/main.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,6 @@ Where <spec> is one or more of:
363363
// amount of code that is generated.
364364
auto types = mdCache.compile_namespaces(namespaces, mf);
365365
write_module_generics(module, types, mf);
366-
write_module_make_from_abi(module, types, mf);
367366
});
368367

369368
if (module == settings.support)

Diff for: tests/test_component/Sources/test_component/Windows.Foundation+Impl.swift

+84
Original file line numberDiff line numberDiff line change
@@ -547,3 +547,87 @@ extension TimeSpan: WinRTBridgeable {
547547
}
548548
}
549549

550+
@_spi(WinRTInternal)
551+
public class IAsyncActionMaker: MakeFromAbi {
552+
public typealias SwiftType = AnyIAsyncAction
553+
public static func from(abi: test_component.IInspectable) -> SwiftType {
554+
let swiftAbi: __ABI_Windows_Foundation.IAsyncAction = try! abi.QueryInterface()
555+
return __IMPL_Windows_Foundation.IAsyncActionBridge.from(abi: RawPointer(swiftAbi))!
556+
}
557+
}
558+
@_spi(WinRTInternal)
559+
public class IAsyncInfoMaker: MakeFromAbi {
560+
public typealias SwiftType = AnyIAsyncInfo
561+
public static func from(abi: test_component.IInspectable) -> SwiftType {
562+
let swiftAbi: __ABI_Windows_Foundation.IAsyncInfo = try! abi.QueryInterface()
563+
return __IMPL_Windows_Foundation.IAsyncInfoBridge.from(abi: RawPointer(swiftAbi))!
564+
}
565+
}
566+
@_spi(WinRTInternal)
567+
public class IClosableMaker: MakeFromAbi {
568+
public typealias SwiftType = AnyIClosable
569+
public static func from(abi: test_component.IInspectable) -> SwiftType {
570+
let swiftAbi: __ABI_Windows_Foundation.IClosable = try! abi.QueryInterface()
571+
return __IMPL_Windows_Foundation.IClosableBridge.from(abi: RawPointer(swiftAbi))!
572+
}
573+
}
574+
@_spi(WinRTInternal)
575+
public class IMemoryBufferMaker: MakeFromAbi {
576+
public typealias SwiftType = AnyIMemoryBuffer
577+
public static func from(abi: test_component.IInspectable) -> SwiftType {
578+
let swiftAbi: __ABI_Windows_Foundation.IMemoryBuffer = try! abi.QueryInterface()
579+
return __IMPL_Windows_Foundation.IMemoryBufferBridge.from(abi: RawPointer(swiftAbi))!
580+
}
581+
}
582+
@_spi(WinRTInternal)
583+
public class IMemoryBufferReferenceMaker: MakeFromAbi {
584+
public typealias SwiftType = AnyIMemoryBufferReference
585+
public static func from(abi: test_component.IInspectable) -> SwiftType {
586+
let swiftAbi: __ABI_Windows_Foundation.IMemoryBufferReference = try! abi.QueryInterface()
587+
return __IMPL_Windows_Foundation.IMemoryBufferReferenceBridge.from(abi: RawPointer(swiftAbi))!
588+
}
589+
}
590+
@_spi(WinRTInternal)
591+
public class IStringableMaker: MakeFromAbi {
592+
public typealias SwiftType = AnyIStringable
593+
public static func from(abi: test_component.IInspectable) -> SwiftType {
594+
let swiftAbi: __ABI_Windows_Foundation.IStringable = try! abi.QueryInterface()
595+
return __IMPL_Windows_Foundation.IStringableBridge.from(abi: RawPointer(swiftAbi))!
596+
}
597+
}
598+
@_spi(WinRTInternal)
599+
public class IWwwFormUrlDecoderEntryMaker: MakeFromAbi {
600+
public typealias SwiftType = AnyIWwwFormUrlDecoderEntry
601+
public static func from(abi: test_component.IInspectable) -> SwiftType {
602+
let swiftAbi: __ABI_Windows_Foundation.IWwwFormUrlDecoderEntry = try! abi.QueryInterface()
603+
return __IMPL_Windows_Foundation.IWwwFormUrlDecoderEntryBridge.from(abi: RawPointer(swiftAbi))!
604+
}
605+
}
606+
@_spi(WinRTInternal)
607+
public class DeferralMaker: MakeFromAbi {
608+
public typealias SwiftType = Deferral
609+
public static func from(abi: test_component.IInspectable) -> SwiftType {
610+
return Deferral(fromAbi: abi)
611+
}
612+
}
613+
@_spi(WinRTInternal)
614+
public class MemoryBufferMaker: MakeFromAbi {
615+
public typealias SwiftType = MemoryBuffer
616+
public static func from(abi: test_component.IInspectable) -> SwiftType {
617+
return MemoryBuffer(fromAbi: abi)
618+
}
619+
}
620+
@_spi(WinRTInternal)
621+
public class UriMaker: MakeFromAbi {
622+
public typealias SwiftType = Uri
623+
public static func from(abi: test_component.IInspectable) -> SwiftType {
624+
return Uri(fromAbi: abi)
625+
}
626+
}
627+
@_spi(WinRTInternal)
628+
public class WwwFormUrlDecoderMaker: MakeFromAbi {
629+
public typealias SwiftType = WwwFormUrlDecoder
630+
public static func from(abi: test_component.IInspectable) -> SwiftType {
631+
return WwwFormUrlDecoder(fromAbi: abi)
632+
}
633+
}

Diff for: tests/test_component/Sources/test_component/Windows.Foundation.Collections+Impl.swift

+37
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,40 @@ public enum __IMPL_Windows_Foundation_Collections {
152152
}
153153

154154
}
155+
@_spi(WinRTInternal)
156+
public class IPropertySetMaker: MakeFromAbi {
157+
public typealias SwiftType = AnyIPropertySet
158+
public static func from(abi: test_component.IInspectable) -> SwiftType {
159+
let swiftAbi: __ABI_Windows_Foundation_Collections.IPropertySet = try! abi.QueryInterface()
160+
return __IMPL_Windows_Foundation_Collections.IPropertySetBridge.from(abi: RawPointer(swiftAbi))!
161+
}
162+
}
163+
@_spi(WinRTInternal)
164+
public class IVectorChangedEventArgsMaker: MakeFromAbi {
165+
public typealias SwiftType = AnyIVectorChangedEventArgs
166+
public static func from(abi: test_component.IInspectable) -> SwiftType {
167+
let swiftAbi: __ABI_Windows_Foundation_Collections.IVectorChangedEventArgs = try! abi.QueryInterface()
168+
return __IMPL_Windows_Foundation_Collections.IVectorChangedEventArgsBridge.from(abi: RawPointer(swiftAbi))!
169+
}
170+
}
171+
@_spi(WinRTInternal)
172+
public class PropertySetMaker: MakeFromAbi {
173+
public typealias SwiftType = PropertySet
174+
public static func from(abi: test_component.IInspectable) -> SwiftType {
175+
return PropertySet(fromAbi: abi)
176+
}
177+
}
178+
@_spi(WinRTInternal)
179+
public class StringMapMaker: MakeFromAbi {
180+
public typealias SwiftType = StringMap
181+
public static func from(abi: test_component.IInspectable) -> SwiftType {
182+
return StringMap(fromAbi: abi)
183+
}
184+
}
185+
@_spi(WinRTInternal)
186+
public class ValueSetMaker: MakeFromAbi {
187+
public typealias SwiftType = ValueSet
188+
public static func from(abi: test_component.IInspectable) -> SwiftType {
189+
return ValueSet(fromAbi: abi)
190+
}
191+
}

0 commit comments

Comments
 (0)