Skip to content

Commit

Permalink
write iasync info
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbrix committed Aug 16, 2023
1 parent b56571d commit d42fee1
Show file tree
Hide file tree
Showing 16 changed files with 478 additions and 59 deletions.
153 changes: 153 additions & 0 deletions swiftwinrt/Resources/CWinRT/include/CAsyncInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// This header is written to make handling of IAsyncInfo simpler in the swiftwinrt
// code generation by giving the interface a mangled name

/* verify that the <rpcndr.h> version is high enough to compile this file*/
#ifndef __REQUIRED_RPCNDR_H_VERSION__
#define __REQUIRED_RPCNDR_H_VERSION__ 501
#endif

/* verify that the <rpcsal.h> version is high enough to compile this file*/
#ifndef __REQUIRED_RPCSAL_H_VERSION__
#define __REQUIRED_RPCSAL_H_VERSION__ 100
#endif

#include "rpc.h"
#include "rpcndr.h"

#ifndef __RPCNDR_H_VERSION__
#error this stub requires an updated version of <rpcndr.h>
#endif /* __RPCNDR_H_VERSION__ */

#ifndef COM_NO_WINDOWS_H
#include "windows.h"
#include "ole2.h"
#endif /*COM_NO_WINDOWS_H*/

#ifndef __asyncinfo_h__
#define __asyncinfo_h__

#if defined(_MSC_VER) && (_MSC_VER >= 1020)
#pragma once
#endif

#ifndef DECLSPEC_XFGVIRT
#if defined(_CONTROL_FLOW_GUARD_XFG)
#define DECLSPEC_XFGVIRT(base, func) __declspec(xfg_virtual(base, func))
#else
#define DECLSPEC_XFGVIRT(base, func)
#endif
#endif

/* Forward Declarations */

#ifndef ____x_ABI_CWindows_CFoundation_CIAsyncInfo_FWD_DEFINED__
#define ____x_ABI_CWindows_CFoundation_CIAsyncInfo_FWD_DEFINED__
typedef interface __x_ABI_CWindows_CFoundation_CIAsyncInfo __x_ABI_CWindows_CFoundation_CIAsyncInfo;

#endif /* ____x_ABI_CWindows_CFoundation_CIAsyncInfo_FWD_DEFINED__ */


/* header files for imported files */
#include "inspectable.h"

/* interface __MIDL_itf_asyncinfo_0000_0000 */
/* [local] */

typedef /* [v1_enum] */
enum __x_ABI_CWindows_CFoundation_CAsyncStatus
{
__x_ABI_CWindows_CFoundation_CAsyncStatus_Started = 0,
__x_ABI_CWindows_CFoundation_CAsyncStatus_Completed = ( __x_ABI_CWindows_CFoundation_CAsyncStatus_Started + 1 ) ,
__x_ABI_CWindows_CFoundation_CAsyncStatus_Canceled = ( __x_ABI_CWindows_CFoundation_CAsyncStatus_Completed + 1 ) ,
__x_ABI_CWindows_CFoundation_CAsyncStatus_Error = ( __x_ABI_CWindows_CFoundation_CAsyncStatus_Canceled + 1 )
} __x_ABI_CWindows_CFoundation_CAsyncStatus;


#ifndef ____x_ABI_CWindows_CFoundation_CIAsyncInfo_INTERFACE_DEFINED__
#define ____x_ABI_CWindows_CFoundation_CIAsyncInfo_INTERFACE_DEFINED__

/* interface __x_ABI_CWindows_CFoundation_CIAsyncInfo */
/* [unique][uuid][object] */


EXTERN_C const IID IID___x_ABI_CWindows_CFoundation_CIAsyncInfo;

typedef struct __x_ABI_CWindows_CFoundation_CIAsyncInfoVtbl
{
BEGIN_INTERFACE

DECLSPEC_XFGVIRT(IUnknown, QueryInterface)
HRESULT ( STDMETHODCALLTYPE *QueryInterface )(
__RPC__in __x_ABI_CWindows_CFoundation_CIAsyncInfo * This,
/* [in] */ __RPC__in REFIID riid,
/* [annotation][iid_is][out] */
_COM_Outptr_ void **ppvObject);

DECLSPEC_XFGVIRT(IUnknown, AddRef)
ULONG ( STDMETHODCALLTYPE *AddRef )(
__RPC__in __x_ABI_CWindows_CFoundation_CIAsyncInfo * This);

DECLSPEC_XFGVIRT(IUnknown, Release)
ULONG ( STDMETHODCALLTYPE *Release )(
__RPC__in __x_ABI_CWindows_CFoundation_CIAsyncInfo * This);

DECLSPEC_XFGVIRT(IInspectable, GetIids)
HRESULT ( STDMETHODCALLTYPE *GetIids )(
__RPC__in __x_ABI_CWindows_CFoundation_CIAsyncInfo * This,
/* [out] */ __RPC__out ULONG *iidCount,
/* [size_is][size_is][out] */ __RPC__deref_out_ecount_full_opt(*iidCount) IID **iids);

DECLSPEC_XFGVIRT(IInspectable, GetRuntimeClassName)
HRESULT ( STDMETHODCALLTYPE *GetRuntimeClassName )(
__RPC__in __x_ABI_CWindows_CFoundation_CIAsyncInfo * This,
/* [out] */ __RPC__deref_out_opt HSTRING *className);

DECLSPEC_XFGVIRT(IInspectable, GetTrustLevel)
HRESULT ( STDMETHODCALLTYPE *GetTrustLevel )(
__RPC__in __x_ABI_CWindows_CFoundation_CIAsyncInfo * This,
/* [out] */ __RPC__out TrustLevel *trustLevel);

DECLSPEC_XFGVIRT(__x_ABI_CWindows_CFoundation_CIAsyncInfo, get_Id)
/* [propget] */ HRESULT ( STDMETHODCALLTYPE *get_Id )(
__RPC__in __x_ABI_CWindows_CFoundation_CIAsyncInfo * This,
/* [retval][out] */ __RPC__out unsigned __int32 *id);

DECLSPEC_XFGVIRT(__x_ABI_CWindows_CFoundation_CIAsyncInfo, get_Status)
/* [propget] */ HRESULT ( STDMETHODCALLTYPE *get_Status )(
__RPC__in __x_ABI_CWindows_CFoundation_CIAsyncInfo * This,
/* [retval][out] */ __RPC__out __x_ABI_CWindows_CFoundation_CAsyncStatus *status);

DECLSPEC_XFGVIRT(__x_ABI_CWindows_CFoundation_CIAsyncInfo, get_ErrorCode)
/* [propget] */ HRESULT ( STDMETHODCALLTYPE *get_ErrorCode )(
__RPC__in __x_ABI_CWindows_CFoundation_CIAsyncInfo * This,
/* [retval][out] */ __RPC__out HRESULT *errorCode);

DECLSPEC_XFGVIRT(__x_ABI_CWindows_CFoundation_CIAsyncInfo, Cancel)
HRESULT ( STDMETHODCALLTYPE *Cancel )(
__RPC__in __x_ABI_CWindows_CFoundation_CIAsyncInfo * This);

DECLSPEC_XFGVIRT(__x_ABI_CWindows_CFoundation_CIAsyncInfo, Close)
HRESULT ( STDMETHODCALLTYPE *Close )(
__RPC__in __x_ABI_CWindows_CFoundation_CIAsyncInfo * This);

END_INTERFACE
} __x_ABI_CWindows_CFoundation_CIAsyncInfoVtbl;

interface __x_ABI_CWindows_CFoundation_CIAsyncInfo
{
CONST_VTBL struct __x_ABI_CWindows_CFoundation_CIAsyncInfoVtbl *lpVtbl;
};


#endif /* ____x_ABI_CWindows_CFoundation_CIAsyncInfo_INTERFACE_DEFINED__ */


/* interface __MIDL_itf_asyncinfo_0000_0001 */
/* [local] */

extern RPC_IF_HANDLE __MIDL_itf_asyncinfo_0000_0001_v0_0_c_ifspec;
extern RPC_IF_HANDLE __MIDL_itf_asyncinfo_0000_0001_v0_0_s_ifspec;


#endif

9 changes: 2 additions & 7 deletions swiftwinrt/abi_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,17 @@ namespace swiftwinrt
// Don't write "built-in types" since these are defined in other header files
// These aren't completely removed from metadata because we need to generate them
// on the swift side
static std::set<std::string_view> removed_types = {
"Windows.Foundation.Collections.CollectionChange",
"Windows.Foundation.Collections.IVectorChangedEventArgs"
};

static bool should_write(metadata_type const& type)
{
return !removed_types.contains(type.swift_full_name());
return !use_sdk_c_header_definition(type);
}

static void write_includes(writer& w, type_cache const& types, std::string_view fileName)
{
// Forced dependencies
w.write(R"^-^(// Header files for imported files
#include "inspectable.h"
#include "AsyncInfo.h"
#include "CAsyncInfo.h"
#include "EventToken.h"
#include "windowscontracts.h"
)^-^");
Expand Down
12 changes: 3 additions & 9 deletions swiftwinrt/code_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ namespace swiftwinrt
{
static void write_enum_def(writer& w, enum_type const& type)
{
// Async status is defined in it's own header with a nice name of AsyncStatus.
// Metadata attributes don't have backing code
if (get_full_type_name(type) == "Windows.Foundation.AsyncStatus" ||
type.swift_logical_namespace() == "Windows.Foundation.Metadata" || !can_write(w, type))
if (type.swift_logical_namespace() == "Windows.Foundation.Metadata" || !can_write(w, type))
{
return;
}
Expand All @@ -19,8 +17,7 @@ namespace swiftwinrt

static void write_enum_extension(writer& w, enum_type const& type)
{
if (get_full_type_name(type) == "Windows.Foundation.AsyncStatus" ||
type.swift_logical_namespace() == "Windows.Foundation.Metadata" || !can_write(w, type))
if (type.swift_logical_namespace() == "Windows.Foundation.Metadata" || !can_write(w, type))
{
return;
}
Expand Down Expand Up @@ -1182,12 +1179,9 @@ public static func makeAbi() -> CABI {
auto interfaces = type.required_interfaces;
separator s{ w };
auto implements = w.write_temp("%", bind_each([&](writer& w, std::pair<std::string, interface_info> const& iface) {
// TODO: https://linear.app/the-browser-company/issue/WIN-103/swiftwinrt-write-iasyncinfo
if (!iface.first.ends_with("IAsyncInfo") && can_write(w, iface.second.type))
{
s();
write_swift_type_identifier(w, *iface.second.type);
}}, interfaces));
}, interfaces));


std::vector<std::string> eventSourceInvokeLines;
Expand Down
7 changes: 1 addition & 6 deletions swiftwinrt/code_writers/can_write.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace swiftwinrt
{
static bool can_write(writer& w, typedef_base const& type);
static bool can_write(generic_inst const& type);
static bool can_write(generic_inst const& type) { return true; }
static bool can_write(writer& w, const metadata_type* type)
{
if (auto typed = dynamic_cast<const typedef_base*>(type))
Expand All @@ -14,11 +14,6 @@ namespace swiftwinrt
{
return can_write(*generics);
}
if (auto mapped = dynamic_cast<const mapped_type*>(type))
{
// TODO: https://linear.app/the-browser-company/issue/WIN-103/swiftwinrt-write-iasyncinfo
if (mapped->type().TypeName() == "IAsyncInfo") return false;
}
return true;
}

Expand Down
8 changes: 8 additions & 0 deletions swiftwinrt/file_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ namespace swiftwinrt
auto package_template = find_resource(RESOURCE_TYPE_OTHER_FILE_STR, RESOURCE_NAME_CWINRT_PACKAGE_SWIFT_STR);
fill_template_placeholders_to_file(package_template, dir_path / "Package.swift");
}

auto support_files = get_named_resources_of_type(
GetModuleHandle(NULL), RESOURCE_TYPE_C_INCLUDE_FILE_STR, /* make_lowercase: */ true);
for (const auto& support_file : support_files)
{
auto path = dir_path / "include" / (support_file.first + ".h");
fill_template_placeholders_to_file(support_file.second, path);
}
}

static void write_namespace_abi(std::string_view const& ns, type_cache const& members, include_only_used_filter const& filter)
Expand Down
12 changes: 11 additions & 1 deletion swiftwinrt/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,6 @@ namespace swiftwinrt
}
if (mapped->swift_type_name() == "EventRegistrationToken") return param_category::struct_type;
if (mapped->swift_type_name() == "IAsyncInfo") return param_category::object_type;
if (mapped->swift_type_name() == "AsyncStatus") return param_category::enum_type;
if (mapped->swift_type_name() == "HResult") return param_category::fundamental_type;
assert(false); // unexpected mapped type
}
Expand Down Expand Up @@ -908,4 +907,15 @@ namespace swiftwinrt

return true;
}

inline bool use_sdk_c_header_definition(metadata_type const& type)
{
static std::set<std::string_view> removed_types = {
"Windows.Foundation.Collections.CollectionChange",
"Windows.Foundation.Collections.IVectorChangedEventArgs",
"Windows.Foundation.IAsyncInfo",
"Windows.Foundation.AsyncStatus"
};
return removed_types.contains(type.swift_full_name());
}
}
2 changes: 1 addition & 1 deletion swiftwinrt/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ Where <spec> is one or more of:

for (auto& ns : namespaces)
{
module_group.add([&, &ns = ns]
module_group.add([&, &ns = ns]
{
auto types = mdCache.compile_namespaces({ ns }, mf);
write_namespace_abi (ns, types, mf);
Expand Down
27 changes: 1 addition & 26 deletions swiftwinrt/metadata_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,6 @@ void metadata_cache::process_namespace_types(
target.enums.reserve(members.enums.size());
for (auto const& e : members.enums)
{
// 'AsyncStatus' is an enum
if (isFoundationNamespace)
{
if (auto ptr = mapped_type::from_typedef(e))
{
[[maybe_unused]] auto [itr, added] = table.emplace(e.TypeName(), *ptr);
XLANG_ASSERT(added);
continue;
}
}

target.enums.emplace_back(e);
[[maybe_unused]] auto [itr, added] = table.emplace(e.TypeName(), target.enums.back());
XLANG_ASSERT(added);
Expand Down Expand Up @@ -105,16 +94,6 @@ void metadata_cache::process_namespace_types(
target.interfaces.reserve(members.interfaces.size());
for (auto const& i : members.interfaces)
{
// 'IAsyncInfo' is an interface
if (isFoundationNamespace)
{
if (auto ptr = mapped_type::from_typedef(i))
{
[[maybe_unused]] auto [itr, added] = table.emplace(i.TypeName(), *ptr);
XLANG_ASSERT(added);
continue;
}
}
target.interfaces.emplace_back(i);
[[maybe_unused]] auto [itr, added] = table.emplace(i.TypeName(), target.interfaces.back());
XLANG_ASSERT(added);
Expand Down Expand Up @@ -462,11 +441,7 @@ void metadata_cache::process_interface_dependencies(init_state& state, interface

for (auto const& interfaces : get_interfaces(state, type.type()))
{
// TODO: https://linear.app/the-browser-company/issue/WIN-103/swiftwinrt-write-iasyncinfo
if (!interfaces.first.ends_with("IAsyncInfo"))
{
type.required_interfaces.push_back(interfaces);
}
type.required_interfaces.push_back(interfaces);
}

for (auto const& method : type.type().MethodList())
Expand Down
10 changes: 9 additions & 1 deletion swiftwinrt/metadata_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace swiftwinrt

inline void add_interface_dependencies_to_queue(processing_queue& to_process, interface_type const& type)
{
for (auto& member : type.functions)
for (const auto& member : type.functions)
{
if (member.return_type)
{
Expand All @@ -45,7 +45,15 @@ namespace swiftwinrt
to_process.emplace(param.type);
}
}
for (const auto& prop : type.properties)
{
to_process.emplace(prop.type);
}

for (const auto& event: type.events)
{
to_process.emplace(event.type);
}
for (auto& [_, required] : type.required_interfaces)
{
to_process.emplace(required.type);
Expand Down
3 changes: 3 additions & 0 deletions swiftwinrt/resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#define RESOURCE_TYPE_SWIFT_SUPPORT_FILE SWIFT_SUPPORT_FILE
#define RESOURCE_TYPE_SWIFT_SUPPORT_FILE_STR "SWIFT_SUPPORT_FILE"

#define RESOURCE_TYPE_C_INCLUDE_FILE C_HEADER_FILE
#define RESOURCE_TYPE_C_INCLUDE_FILE_STR "C_HEADER_FILE"

#define RESOURCE_TYPE_OTHER_FILE OTHER_FILE
#define RESOURCE_TYPE_OTHER_FILE_STR "OTHER_FILE"

Expand Down
4 changes: 3 additions & 1 deletion swiftwinrt/resources.rc
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ EventSource RESOURCE_TYPE_SWIFT_SUPPORT_FILE "Resources\\Support\\Events\\EventS
WinRTDelegate RESOURCE_TYPE_SWIFT_SUPPORT_FILE "Resources\\Support\\Events\\WinRTDelegate.swift"

RESOURCE_NAME_CWINRT_SHIM_C RESOURCE_TYPE_OTHER_FILE "Resources\\CWinRT\\shim.c"
RESOURCE_NAME_CWINRT_PACKAGE_SWIFT RESOURCE_TYPE_OTHER_FILE "Resources\\CWinRT\\Package.swift"
RESOURCE_NAME_CWINRT_PACKAGE_SWIFT RESOURCE_TYPE_OTHER_FILE "Resources\\CWinRT\\Package.swift"

CAsyncInfo RESOURCE_TYPE_C_INCLUDE_FILE "Resources\\CWinRT\\include\\CAsyncInfo.h"
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#pragma once
// Header files for imported files
#include "inspectable.h"
#include "AsyncInfo.h"
#include "CAsyncInfo.h"
#include "EventToken.h"
#include "windowscontracts.h"
#include "Windows.Foundation.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#pragma once
// Header files for imported files
#include "inspectable.h"
#include "AsyncInfo.h"
#include "CAsyncInfo.h"
#include "EventToken.h"
#include "windowscontracts.h"
#include "Windows.Foundation.h"
Expand Down
Loading

0 comments on commit d42fee1

Please sign in to comment.