Skip to content

Commit

Permalink
Address couple trim warnings (#1272)
Browse files Browse the repository at this point in the history
* Remove unused delegate

* Improve trim error message for authored scenario.

* Fix build

* Move message
  • Loading branch information
manodasanW authored Nov 15, 2022
1 parent 7712e7c commit 3dcca78
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 131 deletions.
6 changes: 0 additions & 6 deletions src/WinRT.Runtime/Projections/IDisposable.net5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ unsafe interface IDisposable : global::System.IDisposable
[Guid("30D5A829-7FA4-4026-83BB-D75BAE4EA99E")]
public struct Vftbl
{
private delegate int CloseDelegate(IntPtr thisPtr);
internal IInspectable.Vftbl IInspectableVftbl;
private void* _Close_0;
public delegate* unmanaged[Stdcall]<IntPtr, int> Close_0 { get => (delegate* unmanaged[Stdcall]<IntPtr, int>)_Close_0; set => _Close_0 = value; }
Expand All @@ -35,9 +34,7 @@ static unsafe Vftbl()
AbiToProjectionVftable = new Vftbl
{
IInspectableVftbl = global::WinRT.IInspectable.Vftbl.AbiToProjectionVftable,

_Close_0 = (delegate* unmanaged<IntPtr, int>)&Do_Abi_Close_0

};
var nativeVftbl = (IntPtr*)ComWrappersSupport.AllocateVtableMemory(typeof(Vftbl), Marshal.SizeOf<global::WinRT.IInspectable.Vftbl>() + sizeof(IntPtr) * 1);
Marshal.StructureToPtr(AbiToProjectionVftable, (IntPtr)nativeVftbl, false);
Expand All @@ -46,11 +43,8 @@ static unsafe Vftbl()


[UnmanagedCallersOnly]

private static unsafe int Do_Abi_Close_0(IntPtr thisPtr)
{


try
{
global::WinRT.ComWrappersSupport.FindObject<global::System.IDisposable>(thisPtr).Dispose();
Expand Down
255 changes: 130 additions & 125 deletions src/WinRT.Runtime/TypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Collections.Concurrent;
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

namespace WinRT
{
#if EMBED
internal
#else
public
#endif
static class TypeExtensions
{
using System.Reflection;

namespace WinRT
{
#if EMBED
internal
#else
public
#endif
static class TypeExtensions
{
private readonly static ConcurrentDictionary<Type, Type> HelperTypeCache = new ConcurrentDictionary<Type, Type>();

#if NET
#if NET
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods |
DynamicallyAccessedMemberTypes.NonPublicMethods |
DynamicallyAccessedMemberTypes.PublicNestedTypes |
DynamicallyAccessedMemberTypes.PublicFields)]
#endif
public static Type FindHelperType(this Type type)
{
DynamicallyAccessedMemberTypes.NonPublicMethods |
DynamicallyAccessedMemberTypes.PublicNestedTypes |
DynamicallyAccessedMemberTypes.PublicFields)]
#endif
public static Type FindHelperType(this Type type)
{
return HelperTypeCache.GetOrAdd(type,
#if NET
#if NET
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods |
DynamicallyAccessedMemberTypes.NonPublicMethods |
DynamicallyAccessedMemberTypes.PublicNestedTypes |
DynamicallyAccessedMemberTypes.PublicFields)]
#endif
(type) =>
{
if (typeof(Exception).IsAssignableFrom(type))
{
type = typeof(Exception);
}
Type customMapping = Projections.FindCustomHelperTypeMapping(type);
if (customMapping is not null)
{
return customMapping;
}
var helperTypeAtribute = type.GetCustomAttribute<WindowsRuntimeHelperTypeAttribute>();
DynamicallyAccessedMemberTypes.NonPublicMethods |
DynamicallyAccessedMemberTypes.PublicNestedTypes |
DynamicallyAccessedMemberTypes.PublicFields)]
#endif
(type) =>
{
if (typeof(Exception).IsAssignableFrom(type))
{
type = typeof(Exception);
}
Type customMapping = Projections.FindCustomHelperTypeMapping(type);
if (customMapping is not null)
{
return customMapping;
}
var helperTypeAtribute = type.GetCustomAttribute<WindowsRuntimeHelperTypeAttribute>();
if (helperTypeAtribute is not null)
{
return GetHelperTypeFromAttribute(helperTypeAtribute, type);
}
return FindHelperTypeFallback(type);
}
return FindHelperTypeFallback(type);
});

#if NET
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "No members of the generic type are dynamically accessed other than for the attributes on it.")]
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.PublicFields)]
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.PublicFields)]
#endif
static Type GetHelperTypeFromAttribute(WindowsRuntimeHelperTypeAttribute helperTypeAtribute, Type type)
{
Expand Down Expand Up @@ -86,107 +86,112 @@ static Type FindHelperTypeFallback(Type type)

var helper = $"ABI.{fullTypeName}";
return type.Assembly.GetType(helper) ?? Type.GetType(helper);
}
}
}

#if NET
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods |
DynamicallyAccessedMemberTypes.NonPublicMethods |
DynamicallyAccessedMemberTypes.PublicNestedTypes |
DynamicallyAccessedMemberTypes.PublicFields)]
#endif
public static Type GetHelperType(this Type type)
{
var helperType = type.FindHelperType();
if (helperType is object)
return helperType;
throw new InvalidOperationException($"Target type is not a projected type: {type.FullName}.");
#if NET
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods |
DynamicallyAccessedMemberTypes.NonPublicMethods |
DynamicallyAccessedMemberTypes.PublicNestedTypes |
DynamicallyAccessedMemberTypes.PublicFields)]
#endif
public static Type GetHelperType(this Type type)
{
var helperType = type.FindHelperType();
if (helperType is object)
return helperType;
throw new InvalidOperationException($"Target type is not a projected type: {type.FullName}.");
}

#if NET
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]
#endif
public static Type GetGuidType(this Type type)
{
return type.IsDelegate() ? type.GetHelperType() : type;
#if NET
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]
#endif
public static Type GetGuidType(this Type type)
{
return type.IsDelegate() ? type.GetHelperType() : type;
}

#if NET
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicConstructors | DynamicallyAccessedMemberTypes.PublicFields)]
#endif
#endif
public static Type FindVftblType(
#if NET
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes)]
#endif
this Type helperType)
{
Type vftblType = helperType.GetNestedType("Vftbl");
if (vftblType is null)
{
return null;
}
if (helperType.IsGenericType && vftblType is object)
{
vftblType = vftblType.MakeGenericType(helperType.GetGenericArguments());
}
return vftblType;
}

internal static IntPtr GetAbiToProjectionVftblPtr(
#endif
this Type helperType)
{
Type vftblType = helperType.GetNestedType("Vftbl");
if (vftblType is null)
{
return null;
}
if (helperType.IsGenericType && vftblType is object)
{
vftblType = vftblType.MakeGenericType(helperType.GetGenericArguments());
}
return vftblType;
}

internal static IntPtr GetAbiToProjectionVftblPtr(
#if NET
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]
#endif
this Type helperType)
{
return (IntPtr)(helperType.FindVftblType() ?? helperType).GetField("AbiToProjectionVftablePtr", BindingFlags.Public | BindingFlags.Static).GetValue(null);
}

public static Type GetAbiType(this Type type)
{
return type.GetHelperType().GetMethod("GetAbi", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static).ReturnType;
}

public static Type GetMarshalerType(this Type type)
#endif
this Type helperType)
{
return (IntPtr)(helperType.FindVftblType() ?? helperType).GetField("AbiToProjectionVftablePtr", BindingFlags.Public | BindingFlags.Static).GetValue(null);
}

public static Type GetAbiType(this Type type)
{
return type.GetHelperType().GetMethod("CreateMarshaler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static).ReturnType;
return type.GetHelperType().GetMethod("GetAbi", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static).ReturnType;
}

internal static Type GetMarshaler2Type(this Type type)
public static Type GetMarshalerType(this Type type)
{
return type.GetHelperType().GetMethod("CreateMarshaler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static).ReturnType;
}

internal static Type GetMarshaler2Type(this Type type)
{
var helperType = type.GetHelperType();
var createMarshaler = helperType.GetMethod("CreateMarshaler2", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static) ??
helperType.GetMethod("CreateMarshaler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
return createMarshaler.ReturnType;
}

internal static Type GetMarshalerArrayType(this Type type)
{
return type.GetHelperType().GetMethod("CreateMarshalerArray", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)?.ReturnType;
}

public static bool IsDelegate(this Type type)
{
return typeof(Delegate).IsAssignableFrom(type);
}

internal static bool IsTypeOfType(this Type type)
{
return typeof(Type).IsAssignableFrom(type);
}

public static Type GetRuntimeClassCCWType(this Type type)
{
return type.IsClass && !type.IsArray ? type.GetAuthoringMetadataType() : null;

internal static Type GetMarshalerArrayType(this Type type)
{
return type.GetHelperType().GetMethod("CreateMarshalerArray", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)?.ReturnType;
}

private readonly static ConcurrentDictionary<Type, Type> AuthoringMetadataTypeCache = new ConcurrentDictionary<Type, Type>();
internal static Type GetAuthoringMetadataType(this Type type)
{
return AuthoringMetadataTypeCache.GetOrAdd(type, (type) =>
{
var ccwTypeName = $"ABI.Impl.{type.FullName}";
return type.Assembly.GetType(ccwTypeName, false);
});
}
}
}
public static bool IsDelegate(this Type type)
{
return typeof(Delegate).IsAssignableFrom(type);
}

internal static bool IsTypeOfType(this Type type)
{
return typeof(Type).IsAssignableFrom(type);
}

public static Type GetRuntimeClassCCWType(this Type type)
{
return type.IsClass && !type.IsArray ? type.GetAuthoringMetadataType() : null;
}

private readonly static ConcurrentDictionary<Type, Type> AuthoringMetadataTypeCache = new ConcurrentDictionary<Type, Type>();

internal static Type GetAuthoringMetadataType(this Type type)
{
return AuthoringMetadataTypeCache.GetOrAdd(type,
#if NET
[RequiresUnreferencedCodeAttribute("If authoring a WinRT component in C# using C#/WinRT authoring support, it might require ABI helper types that might get trimmed. Avoid marking such components trimmable or ensure types don't get trimmed from it.")]
#endif
(type) =>
{
var ccwTypeName = $"ABI.Impl.{type.FullName}";
return type.Assembly.GetType(ccwTypeName, false);
});
}
}
}

0 comments on commit 3dcca78

Please sign in to comment.