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

Disable and trim 'Marshaler<T>' on Native AOT #1907

Open
wants to merge 16 commits into
base: staging/2.3
Choose a base branch
from
Open
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
39 changes: 37 additions & 2 deletions src/Authoring/WinRT.SourceGenerator/AotOptimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,8 @@ genericParameter is INamedTypeSymbol genericParameterIface &&
genericParameters.Add(new GenericParameter(
ToFullyQualifiedString(genericParameter),
GeneratorHelper.GetAbiType(genericParameter, mapper),
isNullable ? TypeKind.Interface : genericParameter.TypeKind));
isNullable ? TypeKind.Interface : genericParameter.TypeKind,
ComputeTypeFlags(genericParameter, compilation)));
}

genericInterfacesToAddToVtable.Add(new GenericInterface(
Expand Down Expand Up @@ -774,6 +775,25 @@ void CheckForInterfaceToUseForRuntimeClassName(INamedTypeSymbol iface)
}
}

private static TypeFlags ComputeTypeFlags(ITypeSymbol symbol, Compilation compilation)
{
TypeFlags typeFlags = TypeFlags.None;

// Check for exception types
if (symbol.TypeKind is TypeKind.Class)
{
var exceptionType = compilation.GetTypeByMetadataName("System.Exception")!;

if (SymbolEqualityComparer.Default.Equals(symbol, exceptionType) ||
symbol.InheritsFromType(exceptionType))
{
typeFlags |= TypeFlags.Exception;
}
}

return typeFlags;
}

private static bool TryGetCompatibleWindowsRuntimeTypesForVariantType(INamedTypeSymbol type, TypeMapper mapper, Stack<INamedTypeSymbol> typeStack, Func<ISymbol, TypeMapper, bool> isWinRTType, INamedTypeSymbol objectType, out IList<INamedTypeSymbol> compatibleTypes)
{
compatibleTypes = null;
Expand Down Expand Up @@ -1912,7 +1932,8 @@ namespace {{bindableCustomProperties.Namespace}}
internal readonly record struct GenericParameter(
string ProjectedType,
string AbiType,
TypeKind TypeKind);
TypeKind TypeKind,
TypeFlags TypeFlags);

internal readonly record struct GenericInterface(
string Interface,
Expand Down Expand Up @@ -1962,6 +1983,20 @@ internal readonly record struct CsWinRTAotOptimizerProperties(
bool IsCsWinRTCcwLookupTableGeneratorEnabled,
bool IsCsWinRTAotOptimizerInAutoMode);

/// <summary>
/// Additional flags for discovered types.
/// </summary>
[Flags]
internal enum TypeFlags
{
None = 0x0,

/// <summary>
/// The type derives from <see cref="System.Exception"/>.
/// </summary>
Exception = 0x1 << 0
}

/// <summary>
/// A model describing a type info in a type hierarchy.
/// </summary>
Expand Down
23 changes: 23 additions & 0 deletions src/Authoring/WinRT.SourceGenerator/Extensions/SymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,27 @@ public static bool IsAccessibleFromCompilationAssembly(this ISymbol symbol, Comp
{
return compilation.IsSymbolAccessibleWithin(symbol, compilation.Assembly);
}

/// <summary>
/// Checks whether or not a given <see cref="ITypeSymbol"/> inherits from a specified type.
/// </summary>
/// <param name="typeSymbol">The target <see cref="ITypeSymbol"/> instance to check.</param>
/// <param name="baseTypeSymbol">The <see cref="ITypeSymbol"/> instane to check for inheritance from.</param>
/// <returns>Whether or not <paramref name="typeSymbol"/> inherits from <paramref name="baseTypeSymbol"/>.</returns>
public static bool InheritsFromType(this ITypeSymbol typeSymbol, ITypeSymbol baseTypeSymbol)
{
INamedTypeSymbol? currentBaseTypeSymbol = typeSymbol.BaseType;

while (currentBaseTypeSymbol is not null)
{
if (SymbolEqualityComparer.Default.Equals(currentBaseTypeSymbol, baseTypeSymbol))
{
return true;
}

currentBaseTypeSymbol = currentBaseTypeSymbol.BaseType;
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,19 @@ public static string GetInstantiation(string genericInterface, EquatableArray<Ge
}
else if (genericInterface == "System.Collections.Generic.IList`1")
{
return GetIListInstantiation(genericParameters[0].ProjectedType, genericParameters[0].AbiType, genericParameters[0].TypeKind);
return GetIListInstantiation(
genericParameters[0].ProjectedType,
genericParameters[0].AbiType,
genericParameters[0].TypeKind,
genericParameters[0].TypeFlags);
}
else if (genericInterface == "System.Collections.Generic.IReadOnlyList`1")
{
return GetIReadOnlyListInstantiation(genericParameters[0].ProjectedType, genericParameters[0].AbiType, genericParameters[0].TypeKind);
return GetIReadOnlyListInstantiation(
genericParameters[0].ProjectedType,
genericParameters[0].AbiType,
genericParameters[0].TypeKind,
genericParameters[0].TypeFlags);
}
else if (genericInterface == "System.Collections.Generic.IDictionary`2")
{
Expand All @@ -46,7 +54,11 @@ public static string GetInstantiation(string genericInterface, EquatableArray<Ge
}
else if (genericInterface == "System.Collections.Generic.IEnumerator`1")
{
return GetIEnumeratorInstantiation(genericParameters[0].ProjectedType, genericParameters[0].AbiType, genericParameters[0].TypeKind);
return GetIEnumeratorInstantiation(
genericParameters[0].ProjectedType,
genericParameters[0].AbiType,
genericParameters[0].TypeKind,
genericParameters[0].TypeFlags);
}
else if (genericInterface == "System.Collections.Generic.KeyValuePair`2")
{
Expand Down Expand Up @@ -222,7 +234,7 @@ private static unsafe int Do_Abi_First_0(IntPtr thisPtr, IntPtr* __return_value_
return iEnumerableInstantiation;
}

private static string GetIEnumeratorInstantiation(string genericType, string abiType, TypeKind typeKind)
private static string GetIEnumeratorInstantiation(string genericType, string abiType, TypeKind typeKind, TypeFlags typeFlags)
{
string iEnumeratorInstantiation = $$"""
internal static class IEnumerator_{{GeneratorHelper.EscapeTypeNameForIdentifier(genericType)}}
Expand Down Expand Up @@ -271,7 +283,7 @@ private static unsafe int Do_Abi_GetMany_3(IntPtr thisPtr, int __itemsSize, IntP
try
{
____return_value__ = global::ABI.System.Collections.Generic.IEnumeratorMethods<{{genericType}}>.Abi_GetMany_3(thisPtr, ref __items);
{{GeneratorHelper.GetCopyManagedArrayMarshaler(genericType, abiType, typeKind)}}.CopyManagedArray(__items, items);
{{GeneratorHelper.GetCopyManagedArrayMarshaler(genericType, abiType, typeKind, typeFlags)}}.CopyManagedArray(__items, items);
*__return_value__ = ____return_value__;
}
catch (global::System.Exception __exception__)
Expand Down Expand Up @@ -326,7 +338,7 @@ private static unsafe int Do_Abi_get_HasCurrent_1(IntPtr thisPtr, byte* __return
return iEnumeratorInstantiation;
}

private static string GetIListInstantiation(string genericType, string abiType, TypeKind typeKind)
private static string GetIListInstantiation(string genericType, string abiType, TypeKind typeKind, TypeFlags typeFlags)
{
string iListInstantiation = $$"""
internal static class IList_{{GeneratorHelper.EscapeTypeNameForIdentifier(genericType)}}
Expand Down Expand Up @@ -513,7 +525,7 @@ private static unsafe int Do_Abi_GetMany_10(IntPtr thisPtr, uint startIndex, int
try
{
____return_value__ = global::ABI.System.Collections.Generic.IListMethods<{{genericType}}>.Abi_GetMany_10(thisPtr, startIndex, ref __items);
{{GeneratorHelper.GetCopyManagedArrayMarshaler(genericType, abiType, typeKind)}}.CopyManagedArray(__items, items);
{{GeneratorHelper.GetCopyManagedArrayMarshaler(genericType, abiType, typeKind, typeFlags)}}.CopyManagedArray(__items, items);
*__return_value__ = ____return_value__;
}
catch (global::System.Exception __exception__)
Expand Down Expand Up @@ -563,7 +575,7 @@ private static unsafe int Do_Abi_get_Size_1(IntPtr thisPtr, uint* __return_value
return iListInstantiation;
}

private static string GetIReadOnlyListInstantiation(string genericType, string abiType, TypeKind typeKind)
private static string GetIReadOnlyListInstantiation(string genericType, string abiType, TypeKind typeKind, TypeFlags typeFlags)
{
string iReadOnlylistInstantiation = $$"""
internal static class IReadOnlyList_{{GeneratorHelper.EscapeTypeNameForIdentifier(genericType)}}
Expand Down Expand Up @@ -633,7 +645,7 @@ private static unsafe int Do_Abi_GetMany_3(IntPtr thisPtr, uint startIndex, int
try
{
____return_value__ = global::ABI.System.Collections.Generic.IReadOnlyListMethods<{{genericType}}>.Abi_GetMany_3(thisPtr, startIndex, ref __items);
{{GeneratorHelper.GetCopyManagedArrayMarshaler(genericType, abiType, typeKind)}}.CopyManagedArray(__items, items);
{{GeneratorHelper.GetCopyManagedArrayMarshaler(genericType, abiType, typeKind, typeFlags)}}.CopyManagedArray(__items, items);
*__return_value__ = ____return_value__;
}
catch (global::System.Exception __exception__)
Expand Down
15 changes: 12 additions & 3 deletions src/Authoring/WinRT.SourceGenerator/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,12 +1007,21 @@ public static string GetFromManagedMarshaler(string type, string abiType, TypeKi
}
}

public static string GetCopyManagedArrayMarshaler(string type, string abiType, TypeKind kind)
public static string GetCopyManagedArrayMarshaler(string type, string abiType, TypeKind kind, TypeFlags typeFlags)
{
if (kind == TypeKind.Class || kind == TypeKind.Delegate)
{
// TODO: Classes and delegates are missing CopyManagedArray.
return $$"""Marshaler<{{type}}>""";
if (type is "System.String") return "global::WinRT.MarshalString";
if (type is "System.Type") return "global::ABI.System.Type";
if (type is "System.Object") return "global::WinRT.MarshalInspectable<object>";

if (typeFlags.HasFlag(TypeFlags.Exception))
{
return "global::WinRT.MarshalNonBlittable<Exception>";
}

// Handles all other classes and delegate types
return $"global::WinRT.MarshalGenericHelper<{type}>";
}
else
{
Expand Down
5 changes: 5 additions & 0 deletions src/WinRT.Runtime/AttributeMessages.net5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ internal static class AttributeMessages
/// </summary>
public const string MarshallingOrGenericInstantiationsRequiresDynamicCode = "The necessary marshalling code or generic instantiations might not be available.";

/// <summary>
/// Message for APIs not supported when dynamic code is not available (ie. in AOT environments).
/// </summary>
public const string NotSupportedIfDynamicCodeIsNotAvailable = "The annotated API is not supported when dynamic code is not available (ie. in AOT environments).";

/// <summary>
/// Message for suppressing trim warnings for <see cref="System.Type.MakeGenericType"/> calls with ABI types as type arguments for <see langword="unmanaged"/> constrained type parameters.
/// </summary>
Expand Down
14 changes: 3 additions & 11 deletions src/WinRT.Runtime/CastExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,15 @@ public static AgileReference<T> AsAgile<T>(this T value) where T : class
return new AgileReference<T>(null);
}

var marshal = Marshaler<T>.CreateMarshaler2(value);
var objrefValue = MarshalInspectable<T>.CreateMarshaler2(value);
try
{
if (marshal is IObjectReference objref)
{
return new AgileReference<T>(objref);
}
else if (marshal is ObjectReferenceValue objrefValue)
{
return new AgileReference<T>(objrefValue);
}
return new AgileReference<T>(objrefValue);
}
finally
{
Marshaler<T>.DisposeMarshaler(marshal);
objrefValue.Dispose();
}
throw new InvalidOperationException($"Object type is not a projected type: {nameof(value)}.");
}

private static bool TryGetRefForObject(object value, bool allowComposed, out IObjectReference reference)
Expand Down
Loading