Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: Generate enums as structs, replace extension with .init #180

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 25 additions & 29 deletions swiftwinrt/code_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,28 @@
#include "metadata_cache.h"
namespace swiftwinrt
{
static void write_enum_def(writer& w, enum_type const& type)
{
// Metadata attributes don't have backing code
if (type.swift_logical_namespace() == "Windows.Foundation.Metadata" || !can_write(w, type))
{
return;
}

write_documentation_comment(w, type);
w.write("public typealias % = %\n", type, bind_type_mangled(type));
}

static void write_enum_extension(writer& w, enum_type const& type)
{
if (type.swift_logical_namespace() == "Windows.Foundation.Metadata" || !can_write(w, type))
{
return;
}
w.write("extension % {\n", get_full_swift_type_name(w, type));
w.write("public struct % : RawRepresentable, Hashable, Codable, Sendable {\n", type.type().TypeName());
w.write(" public var rawValue: Swift.Int32\n\n");
w.write(" public init(rawValue: Swift.Int32 = 0) {\n");
w.write(" self.rawValue = rawValue\n");
w.write(" }\n\n");
{
auto format = R"( public static var % : % {
%_%
}
)";
auto format = " public static let % = Self(rawValue: %)\n\n";
for (const auto& field : type.type().FieldList())
{
if (field.Constant())
{
// this enum is not written by our ABI tool, so it doesn't use a mangled name
if (get_full_type_name(type) == "Windows.Foundation.Collections.CollectionChange")
{
w.write(format, get_swift_name(field), get_full_swift_type_name(w, type), type, field.Name());
}
else
{
// we use mangled names for enums because otherwise the WinAppSDK enums collide with the Windows ones
w.write(format, get_swift_name(field), get_full_swift_type_name(w, type), bind_type_mangled(type), field.Name());
}
w.write(format, get_swift_name(field), field.Constant().ValueInt32());
}
}
}
w.write("}\n");

w.write("extension %: ^@retroactive Hashable, ^@retroactive Codable, ^@retroactive ^@unchecked Sendable {}\n\n", get_full_swift_type_name(w, type));
}

static void write_guid_value(writer& w, std::vector<FixedArgSig> const& args)
Expand Down Expand Up @@ -244,6 +222,10 @@ namespace swiftwinrt
// api call for easy passing to the ABI
w.write("%", local_name);
}
else if (category == param_category::enum_type)
{
w.write(".init(rawValue: %.rawValue)", param_name);
}
else if (is_type_blittable(category))
{
// fundamentals and enums can be simply copied
Expand Down Expand Up @@ -307,6 +289,10 @@ namespace swiftwinrt
w.write("&%.val", local_param_name);
}
}
else if (category == param_category::enum_type)
{
w.write("&%", local_param_name);
}
else if (is_blittable)
{
w.write("&%", param_name);
Expand Down Expand Up @@ -1667,6 +1653,16 @@ vtable);
local_param_name);
guard.push("WindowsDeleteString(%)\n", local_param_name);
}
else if (category == param_category::enum_type)
{
w.write("var %: % = .init(rawValue: %.rawValue)\n",
local_param_name,
bind_type_mangled(param.type),
param_name);
guard.push("% = .init(rawValue: %.rawValue)\n",
param_name,
local_param_name);
}
else if (category == param_category::struct_type &&
is_struct_blittable(signature_type) &&
!is_guid(category))
Expand Down
4 changes: 4 additions & 0 deletions swiftwinrt/code_writers/common_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ namespace swiftwinrt
w.write(".from(abi: %)", name);
}
}
else if (category == param_category::enum_type)
{
w.write(".init(rawValue: %.rawValue)", name);
}
else if (is_type_blittable(category))
{
// fundamental types can just be simply copied to since the types match
Expand Down
11 changes: 10 additions & 1 deletion swiftwinrt/code_writers/struct_writers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,22 @@ namespace swiftwinrt
{
s();

if (dynamic_cast<const struct_type*>(field.type))
auto category = get_category(field.type);

if (category == param_category::struct_type)
{
w.write("%: .from(swift: swift.%)",
get_abi_name(field),
get_swift_name(field)
);
}
else if (category == param_category::enum_type)
{
w.write("%: .init(rawValue: swift.%.rawValue)",
get_abi_name(field),
get_swift_name(field)
);
}
else
{
w.write("%: swift.%",
Expand Down
2 changes: 1 addition & 1 deletion swiftwinrt/code_writers/type_writers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ void swiftwinrt::write_default_init_assignment(writer& w, metadata_type const& s
}
else if (category == param_category::enum_type)
{
w.write(" = .init(0)");
w.write(" = .init(rawValue: 0)");
}
else if (is_boolean(&sig))
{
Expand Down
1 change: 0 additions & 1 deletion swiftwinrt/file_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ namespace swiftwinrt
w.swift_module = get_swift_module(ns);
w.cache = members.cache;

w.write("%", w.filter.bind_each<write_enum_def>(members.enums));
w.write("%", w.filter.bind_each<write_class>(members.classes));
w.write("%", w.filter.bind_each<write_delegate>(members.delegates));
w.write("%", w.filter.bind_each<write_struct>(members.structs));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,11 @@ public enum __ABI_Windows_Foundation {
}

open func get_StatusImpl() throws -> test_component.AsyncStatus {
var result: __x_ABI_CWindows_CFoundation_CAsyncStatus = .init(0)
var result: __x_ABI_CWindows_CFoundation_CAsyncStatus = .init(rawValue: 0)
_ = try perform(as: __x_ABI_CWindows_CFoundation_CIAsyncInfo.self) { pThis in
try CHECKED(pThis.pointee.lpVtbl.pointee.get_Status(pThis, &result))
}
return result
return .init(rawValue: result.rawValue)
}

open func get_ErrorCodeImpl() throws -> HRESULT {
Expand Down Expand Up @@ -273,7 +273,7 @@ public enum __ABI_Windows_Foundation {
get_Status: {
guard let __unwrapped__instance = IAsyncInfoWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }
let result = __unwrapped__instance.status
$1?.initialize(to: result)
$1?.initialize(to: .init(rawValue: result.rawValue))
return S_OK
},

Expand Down Expand Up @@ -534,11 +534,11 @@ public enum __ABI_Windows_Foundation {
override public class var IID: test_component.IID { IID___x_ABI_CWindows_CFoundation_CIPropertyValue }

open func get_TypeImpl() throws -> test_component.PropertyType {
var value: __x_ABI_CWindows_CFoundation_CPropertyType = .init(0)
var value: __x_ABI_CWindows_CFoundation_CPropertyType = .init(rawValue: 0)
_ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValue.self) { pThis in
try CHECKED(pThis.pointee.lpVtbl.pointee.get_Type(pThis, &value))
}
return value
return .init(rawValue: value.rawValue)
}

open func get_IsNumericScalarImpl() throws -> Bool {
Expand Down Expand Up @@ -726,7 +726,7 @@ public enum __ABI_Windows_Foundation {
get_Type: {
guard let __unwrapped__instance = IPropertyValueWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }
let value = __unwrapped__instance.type
$1?.initialize(to: value)
$1?.initialize(to: .init(rawValue: value.rawValue))
return S_OK
},

Expand Down Expand Up @@ -1334,7 +1334,7 @@ extension __ABI_Windows_Foundation {
let asyncInfoWrapper = __ABI_Windows_Foundation.IAsyncActionWrapper(asyncInfo)
let _asyncInfo = try! asyncInfoWrapper?.toABI { $0 }
_ = try perform(as: __x_ABI_CWindows_CFoundation_CIAsyncActionCompletedHandler.self) { pThis in
try CHECKED(pThis.pointee.lpVtbl.pointee.Invoke(pThis, _asyncInfo, asyncStatus))
try CHECKED(pThis.pointee.lpVtbl.pointee.Invoke(pThis, _asyncInfo, .init(rawValue: asyncStatus.rawValue)))
}
}

Expand All @@ -1350,7 +1350,7 @@ extension __ABI_Windows_Foundation {
do {
guard let __unwrapped__instance = AsyncActionCompletedHandlerWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }
let asyncInfo: test_component.AnyIAsyncAction? = __ABI_Windows_Foundation.IAsyncActionWrapper.unwrapFrom(abi: ComPtr($1))
let asyncStatus: test_component.AsyncStatus = $2
let asyncStatus: test_component.AsyncStatus = .init(rawValue: $2.rawValue)
try __unwrapped__instance(asyncInfo, asyncStatus)
return S_OK
} catch { return failWith(err: E_FAIL) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ public enum __ABI_Windows_Foundation_Collections {
override public class var IID: test_component.IID { IID___x_ABI_CWindows_CFoundation_CCollections_CIVectorChangedEventArgs }

open func get_CollectionChangeImpl() throws -> test_component.CollectionChange {
var value: __x_ABI_CWindows_CFoundation_CCollections_CCollectionChange = .init(0)
var value: __x_ABI_CWindows_CFoundation_CCollections_CCollectionChange = .init(rawValue: 0)
_ = try perform(as: __x_ABI_CWindows_CFoundation_CCollections_CIVectorChangedEventArgs.self) { pThis in
try CHECKED(pThis.pointee.lpVtbl.pointee.get_CollectionChange(pThis, &value))
}
return value
return .init(rawValue: value.rawValue)
}

open func get_IndexImpl() throws -> UInt32 {
Expand Down Expand Up @@ -150,7 +150,7 @@ public enum __ABI_Windows_Foundation_Collections {
get_CollectionChange: {
guard let __unwrapped__instance = IVectorChangedEventArgsWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }
let value = __unwrapped__instance.collectionChange
$1?.initialize(to: value)
$1?.initialize(to: .init(rawValue: value.rawValue))
return S_OK
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import Foundation
import Ctest_component

/// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.collectionchange)
public typealias CollectionChange = __x_ABI_CWindows_CFoundation_CCollections_CCollectionChange
/// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.propertyset)
public final class PropertySet : WinRTClass, IObservableMap, IMap, IIterable, IPropertySet {
public typealias K = String
Expand Down Expand Up @@ -489,19 +487,19 @@ public protocol IVector<T> : IIterable, Collection where Element == T, Index ==

public typealias AnyIVector<T> = any IVector<T>

extension test_component.CollectionChange {
public static var reset : test_component.CollectionChange {
CollectionChange_Reset
}
public static var itemInserted : test_component.CollectionChange {
CollectionChange_ItemInserted
}
public static var itemRemoved : test_component.CollectionChange {
CollectionChange_ItemRemoved
}
public static var itemChanged : test_component.CollectionChange {
CollectionChange_ItemChanged
public struct CollectionChange : RawRepresentable, Hashable, Codable, Sendable {
public var rawValue: Swift.Int32

public init(rawValue: Swift.Int32 = 0) {
self.rawValue = rawValue
}
}
extension test_component.CollectionChange: @retroactive Hashable, @retroactive Codable, @retroactive @unchecked Sendable {}

public static let reset = Self(rawValue: 0)

public static let itemInserted = Self(rawValue: 1)

public static let itemRemoved = Self(rawValue: 2)

public static let itemChanged = Self(rawValue: 3)

}
Loading
Loading