Skip to content

Commit

Permalink
Embedded sources: cswinrt (#1009)
Browse files Browse the repository at this point in the history
  • Loading branch information
j0shuams authored Oct 29, 2021
1 parent 72b1236 commit a0b8c37
Show file tree
Hide file tree
Showing 30 changed files with 397 additions and 129 deletions.
3 changes: 3 additions & 0 deletions src/WinRT.Runtime/TypeNameSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ enum TypeNameGenerationFlags
NoCustomTypeName = 0x2
}

#if EMBED
internal
#endif
static class TypeNameSupport
{
private static List<Assembly> projectionAssemblies = new List<Assembly>();
Expand Down
60 changes: 42 additions & 18 deletions src/cswinrt/code_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ namespace cswinrt
return w.write_temp("%_%", method.Name(), get_vmethod_index(type, method));
}

std::string internal_if_embedded()
{
return (settings.embedded) ? "internal" : "public";
}

bool is_type_blittable(type_semantics const& semantics, bool for_array = false)
{
return call(semantics,
Expand Down Expand Up @@ -2813,11 +2818,12 @@ db_path.stem().string());

void write_static_class(writer& w, TypeDef const& type)
{
w.write(R"(%%public static class %
w.write(R"(%%% static class %
{
%})",
bind<write_winrt_attribute>(type),
bind<write_type_custom_attributes>(type, true),
internal_if_embedded(),
bind<write_type_name>(type, typedef_name_type::Projected, false),
bind<write_attributed_types>(type)
);
Expand Down Expand Up @@ -5412,12 +5418,13 @@ IInspectableVftbl = global::WinRT.IInspectable.Vftbl.AbiToProjectionVftable,
{
auto type_name = write_type_name_temp(w, type);

w.write(R"(%%public sealed class %: Attribute
w.write(R"(%%% sealed class %: Attribute
{
%}
)",
bind<write_winrt_attribute>(type),
bind<write_type_custom_attributes>(type, true),
internal_if_embedded(),
type_name,
[&](writer& w)
{
Expand Down Expand Up @@ -5453,7 +5460,7 @@ IInspectableVftbl = global::WinRT.IInspectable.Vftbl.AbiToProjectionVftable,
bind<write_winrt_attribute>(type),
bind<write_guid_attribute>(type),
bind<write_type_custom_attributes>(type, false),
is_exclusive_to(type) || is_projection_internal(type) ? "internal" : "public",
is_exclusive_to(type) || (is_projection_internal(type) || settings.embedded) ? "internal" : "public",
type_name,
bind<write_type_inheritance>(type, object_type{}, false, false),
bind<write_interface_member_signatures>(type)
Expand All @@ -5473,7 +5480,7 @@ IInspectableVftbl = global::WinRT.IInspectable.Vftbl.AbiToProjectionVftable,

w.write(R"([global::WinRT.ObjectReferenceWrapper(nameof(_obj))]
%
public unsafe class % : %
% unsafe class % : %
{
%
internal static ObjectReference<Vftbl> FromAbi(IntPtr thisPtr)%
Expand All @@ -5492,6 +5499,7 @@ _obj = obj;%
)",
// Interface abi implementation
bind<write_guid_attribute>(type),
internal_if_embedded(),
type_name,
bind<write_type_name>(type, typedef_name_type::CCW, false),
// Vftbl
Expand Down Expand Up @@ -5560,10 +5568,11 @@ public static Guid PIID = Vftbl.PIID;
if (!nongeneric_delegates.empty())
{
w.write(R"([global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
public static class %
% static class %
{
%}
)",
internal_if_embedded(),
nongenerics_class,
bind_each(nongeneric_delegates));
}
Expand All @@ -5578,7 +5587,10 @@ public static class %
{
%
}
)", is_exclusive_to(iface) ? "internal" : "public", bind<write_type_name>(iface, typedef_name_type::StaticAbiClass, false), bind<write_static_abi_class_members>(iface));
)",
is_exclusive_to(iface) ? "internal" : internal_if_embedded(),
bind<write_type_name>(iface, typedef_name_type::StaticAbiClass, false),
bind<write_static_abi_class_members>(iface));
}

bool write_abi_interface(writer& w, TypeDef const& type)
Expand Down Expand Up @@ -5659,10 +5671,11 @@ AbiToProjectionVftablePtr = ComWrappersSupport.AllocateVtableMemory(typeof(@), s
if (!nongeneric_delegates.empty())
{
w.write(R"([global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
public static class %
% static class %
{
%}
)",
internal_if_embedded(),
nongenerics_class,
bind_each(nongeneric_delegates));
}
Expand Down Expand Up @@ -5821,7 +5834,7 @@ private readonly % _comp;
}

w.write(R"(%[global::WinRT.ProjectedRuntimeClass(nameof(_default))]
%public %class %%, IEquatable<%>
%% %class %%, IEquatable<%>
{
public %IntPtr ThisPtr => _default.ThisPtr;
Expand Down Expand Up @@ -5859,6 +5872,7 @@ private % AsInternal(InterfaceTag<%> _) => _default;
)",
bind<write_winrt_attribute>(type),
bind<write_type_custom_attributes>(type, false),
internal_if_embedded(),
bind<write_class_modifiers>(type),
type_name,
bind<write_type_inheritance>(type, base_semantics, true, false),
Expand Down Expand Up @@ -5970,7 +5984,7 @@ _defaultLazy = new Lazy<%>(() => GetDefaultReference<%.Vftbl>());
w.write(R"(%
[global::WinRT.ProjectedRuntimeClass(nameof(_default))]
[global::WinRT.ObjectReferenceWrapper(nameof(_inner))]
%public %class %%, IWinRTObject, IEquatable<%>
%% %class %%, IWinRTObject, IEquatable<%>
{
private IntPtr ThisPtr => _inner == null ? (((IWinRTObject)this).NativeObject).ThisPtr : _inner.ThisPtr;
Expand Down Expand Up @@ -6008,6 +6022,7 @@ private struct InterfaceTag<I>{};
)",
bind<write_winrt_attribute>(type),
bind<write_type_custom_attributes>(type, true),
internal_if_embedded(),
bind<write_class_modifiers>(type),
type_name,
bind<write_type_inheritance>(type, base_semantics, true, false),
Expand Down Expand Up @@ -6125,7 +6140,7 @@ global::System.Collections.Concurrent.ConcurrentDictionary<RuntimeTypeHandle, ob
auto default_interface_abi_name = get_default_interface_name(w, type, true);

w.write(R"([global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
public struct %
% struct %
{
%
public static IntPtr GetAbi(IObjectReference value) => value is null ? IntPtr.Zero : MarshalInterfaceHelper<object>.GetAbi(value);
Expand All @@ -6141,6 +6156,7 @@ public static void DisposeAbi(IntPtr abi) => MarshalInspectable<object>.DisposeA
public static unsafe void DisposeAbiArray(object box) => MarshalInspectable<object>.DisposeAbiArray(box);
}
)",
internal_if_embedded(),
abi_type_name,
bind([&](writer& w)
{
Expand Down Expand Up @@ -6201,10 +6217,11 @@ public static unsafe void DisposeAbiArray(object box) => MarshalInspectable<obje
}

method_signature signature{ get_delegate_invoke(type) };
w.write(R"(%%public delegate % %(%);
w.write(R"(%%% delegate % %(%);
)",
bind<write_winrt_attribute>(type),
bind<write_type_custom_attributes>(type, false),
internal_if_embedded(),
bind<write_projection_return_type>(signature),
bind<write_type_name>(type, typedef_name_type::Projected, false),
bind_list<write_projection_parameter>(", ", signature.params()));
Expand All @@ -6223,7 +6240,7 @@ public static unsafe void DisposeAbiArray(object box) => MarshalInspectable<obje

w.write(R"([global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
%
public static class @%
% static class @%
{%
%
private static readonly global::WinRT.Interop.IDelegateVftbl AbiToProjectionVftable;
Expand Down Expand Up @@ -6253,7 +6270,7 @@ return abiDelegate is null ? null : (%)ComWrappersSupport.TryRegisterObjectForIn
}
[global::WinRT.ObjectReferenceWrapper(nameof(_nativeDelegate))]
#if NETSTANDARD2_0
#if !NET
private class NativeDelegateWrapper
#else
private class NativeDelegateWrapper : IWinRTObject
Expand All @@ -6266,7 +6283,7 @@ public NativeDelegateWrapper(ObjectReference<global::WinRT.Interop.IDelegateVftb
_nativeDelegate = nativeDelegate;
}
#if !NETSTANDARD2_0
#if NET
IObjectReference IWinRTObject.NativeObject => _nativeDelegate;
bool IWinRTObject.HasUnwrappableNativeObject => true;
private volatile global::System.Collections.Concurrent.ConcurrentDictionary<RuntimeTypeHandle, IObjectReference> _queryInterfaceCache;
Expand Down Expand Up @@ -6306,6 +6323,7 @@ private static unsafe int Do_Abi_Invoke%
)",
bind<write_guid_attribute>(type),
internal_if_embedded(),
type.TypeName(),
type_params,
[&](writer& w) {
Expand Down Expand Up @@ -6515,11 +6533,12 @@ public static Guid PIID = GuidGenerator.CreateIID(typeof(%));)",

auto enum_underlying_type = is_flags_enum(type) ? "uint" : "int";

w.write(R"(%%public enum % : %
w.write(R"(%%% enum % : %
{
)",
bind<write_winrt_attribute>(type),
bind<write_type_custom_attributes>(type, true),
internal_if_embedded(),
bind<write_type_name>(type, typedef_name_type::Projected, false), enum_underlying_type);
{
for (auto&& field : type.FieldList())
Expand Down Expand Up @@ -6569,7 +6588,7 @@ public static Guid PIID = GuidGenerator.CreateIID(typeof(%));)",
fields.emplace_back(field_info);
}

w.write(R"(%%public struct %: IEquatable<%>
w.write(R"(%%% struct %: IEquatable<%>
{
%
public %(%)
Expand All @@ -6587,6 +6606,7 @@ public override int GetHashCode() => %;
// struct
bind<write_winrt_attribute>(type),
bind<write_type_custom_attributes>(type, true),
internal_if_embedded(),
name,
name,
bind_each([](writer& w, auto&& field)
Expand Down Expand Up @@ -6631,7 +6651,10 @@ public override int GetHashCode() => %;
return;
}

w.write("[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]\npublic struct %\n{\n", bind<write_type_name>(type, typedef_name_type::ABI, false));
w.write("[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]\n% struct %\n{\n",
internal_if_embedded(),
bind<write_type_name>(type, typedef_name_type::ABI, false));

for (auto&& field : type.FieldList())
{
w.write("public ");
Expand Down Expand Up @@ -6983,7 +7006,7 @@ bind<write_factory_class_members>(type)
using System;
namespace WinRT
{
public static class Module
% static class Module
{
public static unsafe IntPtr GetActivationFactory(String runtimeClassId)
{%
Expand All @@ -6992,6 +7015,7 @@ return IntPtr.Zero;
}
}
)",
internal_if_embedded(),
bind_each([](writer& w, TypeDef const& type)
{
w.write(R"(
Expand Down
3 changes: 3 additions & 0 deletions src/cswinrt/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace cswinrt
{ "target", 0, 1, "<net6.0|net5.0|netstandard2.0>", "Target TFM for projection. Omit for compatibility with newest TFM (net5.0)." },
{ "component", 0, 0, {}, "Generate component projection." },
{ "verbose", 0, 0, {}, "Show detailed progress information" },
{ "embedded", 0, 0, {}, "Generate the projection as internal."},
{ "help", 0, option::no_max, {}, "Show detailed help" },
{ "?", 0, option::no_max, {}, {} },
};
Expand Down Expand Up @@ -84,6 +85,7 @@ Where <spec> is one or more of:
throw usage_exception{};
}


settings.verbose = args.exists("verbose");
auto target = args.value("target");
if (!target.empty() && target != "netstandard2.0" && !starts_with(target, "net5.0") && !starts_with(target, "net6.0"))
Expand All @@ -92,6 +94,7 @@ Where <spec> is one or more of:
}
settings.netstandard_compat = target == "netstandard2.0";
settings.component = args.exists("component");
settings.embedded = args.exists("embedded");
settings.input = args.files("input", database::is_database);

for (auto && include : args.values("include"))
Expand Down
1 change: 1 addition & 0 deletions src/cswinrt/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace cswinrt
winmd::reader::filter filter;
bool netstandard_compat{};
bool component{};
bool embedded{};
};

extern settings_type settings;
Expand Down
Loading

0 comments on commit a0b8c37

Please sign in to comment.