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

Collection fixes #7

Merged
merged 6 commits into from
Feb 12, 2023
Merged
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
17 changes: 13 additions & 4 deletions LinqGen.Generator/CodeGenUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public static class CodeGenUtils
private const string UnityNativeArrayTypeName = "NativeArray`1";
private const string UnityNativeSliceTypeName = "NativeSlice`1";

private static readonly string[] CollectionInterfaces = { "ICollection`1", "IReadOnlyCollection`1", "ICollection" };

public static bool IsStubMethod(IMethodSymbol symbol)
{
// is it member of extension class or member of stub enumerable?
Expand Down Expand Up @@ -633,10 +635,17 @@ public static bool TryGetGenericEnumerableInterface(ITypeSymbol symbol, out INam
return interfaceSymbol != null!;
}

public static bool TryGetGenericCollectionInterface(ITypeSymbol symbol, out INamedTypeSymbol interfaceSymbol)
public static bool TryGetGenericCollectionInterface(ITypeSymbol symbol, out INamedTypeSymbol? interfaceSymbol)
{
interfaceSymbol = GetInterface(symbol, SystemCollectionsGenericNamespace, "ICollection`1")!;
return interfaceSymbol != null!;
foreach (var collectionInterface in CollectionInterfaces)
{
interfaceSymbol = GetInterface(symbol, SystemCollectionsGenericNamespace, collectionInterface);
if (interfaceSymbol != null)
return true;
}

interfaceSymbol = null;
return false;
}

public static bool TryGetComparableSelfInterface(ITypeSymbol symbol, out INamedTypeSymbol interfaceSymbol)
Expand All @@ -660,9 +669,9 @@ public static bool TryGetEquatableSelfInterface(ITypeSymbol symbol, out INamedTy
public static IMethodSymbol? GetEnumeratorSymbol(ITypeSymbol enumerableSymbol)
{
// find GetEnumerator with same rule as C# duck typing
// TODO fallback to interface implementation
return enumerableSymbol.GetMembers()
.OfType<IMethodSymbol>()
.Concat(enumerableSymbol.AllInterfaces.SelectMany(a=>a.GetMembers()).OfType<IMethodSymbol>())
.FirstOrDefault(x =>
x.DeclaredAccessibility == Accessibility.Public &&
x.Name == "GetEnumerator" && x.Parameters.Length == 0 && x.TypeParameters.Length == 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ public sealed class EnumerableGeneration : Generation
public EnumerableGeneration(in LinqGenExpression expression, int id,
ITypeSymbol sourceSymbol) : base(expression, id)
{
// TODO ICollection, ICollection<T>, IReadOnlyCollection<T> ...
IsCollection = TryGetGenericCollectionInterface(sourceSymbol, out _);

var enumeratorSymbol = GetEnumeratorSymbol(sourceSymbol)!.ReturnType;
Expand Down
Loading