Skip to content

Commit

Permalink
#8 incremental generator for LinqGen
Browse files Browse the repository at this point in the history
  • Loading branch information
cathei committed Oct 15, 2023
1 parent c1dd806 commit 3794290
Show file tree
Hide file tree
Showing 9 changed files with 368 additions and 104 deletions.
10 changes: 8 additions & 2 deletions LinqGen.Generator/CodeGenUtils.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// LinqGen.Generator, Maxwell Keonwoo Kang <[email protected]>, 2022

using System;
using System.Collections.Immutable;
using System.Linq;

namespace Cathei.LinqGen.Generator;
Expand Down Expand Up @@ -96,7 +97,7 @@ symbol.TypeArguments[1] is not INamedTypeSymbol resultSignatureSymbol ||
}

public static bool TryParseStubMethod(IMethodSymbol methodSymbol,
out ITypeSymbol inputElementSymbol, out INamedTypeSymbol[] signatureSymbols)
out ITypeSymbol inputElementSymbol, out IEnumerable<INamedTypeSymbol> signatureSymbols)
{
if (methodSymbol.ReceiverType is not INamedTypeSymbol receiverTypeSymbol ||
!TryParseStubInterface(receiverTypeSymbol, out inputElementSymbol, out var receiverSignatureSymbol))
Expand Down Expand Up @@ -124,7 +125,7 @@ public static bool TryParseStubMethod(IMethodSymbol methodSymbol,
signatureSymbolsList.Add(paramSignatureSymbol);
}

signatureSymbols = signatureSymbolsList.ToArray();
signatureSymbols = signatureSymbolsList;
return true;
}

Expand Down Expand Up @@ -782,4 +783,9 @@ public static BlockSyntax AddStatements(this BlockSyntax block, SyntaxList<State
{
return block.WithStatements(block.Statements.AddRange(statements));
}

public static int HashCombine(int a, int b)
{
return unchecked((a * 397) ^ b);
}
}
21 changes: 12 additions & 9 deletions LinqGen.Generator/EvaluationKey.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// LinqGen.Generator, Maxwell Keonwoo Kang <[email protected]>, 2022

using System;

namespace Cathei.LinqGen.Generator;

public readonly struct EvaluationKey : IEqualityComparer<EvaluationKey>
public readonly struct EvaluationKey : IEquatable<EvaluationKey>
{
private static readonly SymbolEqualityComparer SymbolComparer = SymbolEqualityComparer.Default;

Expand All @@ -18,17 +20,18 @@ public EvaluationKey(
InputElementSymbol = inputElementSymbol;
}

public bool Equals(EvaluationKey x, EvaluationKey y)
public bool Equals(EvaluationKey other)
{
return SymbolComparer.Equals(x.SignatureSymbol, y.SignatureSymbol) &&
SymbolComparer.Equals(x.MethodSymbol, y.MethodSymbol) &&
SymbolComparer.Equals(x.InputElementSymbol, y.InputElementSymbol);
return SymbolComparer.Equals(SignatureSymbol, other.SignatureSymbol) &&
SymbolComparer.Equals(MethodSymbol, other.MethodSymbol) &&
SymbolComparer.Equals(InputElementSymbol, other.InputElementSymbol);
}

public int GetHashCode(EvaluationKey obj)
public override int GetHashCode()
{
return SymbolComparer.GetHashCode(obj.SignatureSymbol) ^
SymbolComparer.GetHashCode(obj.MethodSymbol) ^
SymbolComparer.GetHashCode(obj.InputElementSymbol);
int hashCode = SymbolComparer.GetHashCode(SignatureSymbol);
hashCode = HashCombine(hashCode, SymbolComparer.GetHashCode(MethodSymbol));
hashCode = HashCombine(hashCode, SymbolComparer.GetHashCode(InputElementSymbol));
return hashCode;
}
}
5 changes: 3 additions & 2 deletions LinqGen.Generator/Instructions/Instruction.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// LinqGen.Generator, Maxwell Keonwoo Kang <[email protected]>, 2022

using System;
using System.Collections.Immutable;
using System.Linq;

namespace Cathei.LinqGen.Generator;
Expand All @@ -13,10 +14,10 @@ namespace Cathei.LinqGen.Generator;
/// </summary>
public abstract class Instruction
{
public INamedTypeSymbol[]? UpstreamSignatureSymbols { get; }
public ImmutableArray<INamedTypeSymbol> UpstreamSignatureSymbols { get; }
public string Id { get; }

protected Instruction(in LinqGenExpression expression, int id)
protected Instruction(in LinqGenExpression expression, uint id)
{
UpstreamSignatureSymbols = expression.UpstreamSignatureSymbols;
Id = Base62.Encode(id);
Expand Down
4 changes: 2 additions & 2 deletions LinqGen.Generator/Instructions/InstructionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static class InstructionFactory
/// <summary>
/// The Instruction instance must be unique per signature (per generic arguments combination).
/// </summary>
public static Generation? CreateGeneration(StringBuilder logBuilder, in LinqGenExpression expression, int id)
public static Generation? CreateGeneration(in LinqGenExpression expression, int id)
{
switch (expression.SignatureSymbol!.Name)
{
Expand Down Expand Up @@ -274,7 +274,7 @@ public static class InstructionFactory
return null;
}

public static Evaluation? CreateEvaluation(StringBuilder logBuilder, in LinqGenExpression expression, int id)
public static Evaluation? CreateEvaluation(in LinqGenExpression expression, int id)
{
switch (expression.MethodSymbol.Name)
{
Expand Down
5 changes: 3 additions & 2 deletions LinqGen.Generator/LinqGen.Generator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
<PackageDescription>Alloc-free and fast replacement for Linq, with code generation.</PackageDescription>
<RepositoryUrl>https://github.com/cathei/LinqGen</RepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3">
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.0.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
14 changes: 8 additions & 6 deletions LinqGen.Generator/LinqGenExpression.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// LinqGen.Generator, Maxwell Keonwoo Kang <[email protected]>, 2022

using System;
using System.Collections.Immutable;
using System.Linq;

namespace Cathei.LinqGen.Generator;
Expand All @@ -10,10 +11,10 @@ public readonly struct LinqGenExpression
public IMethodSymbol MethodSymbol { get; }
public INamedTypeSymbol? SignatureSymbol { get; }
public ITypeSymbol? InputElementSymbol { get; }
public INamedTypeSymbol[]? UpstreamSignatureSymbols { get; }
public ImmutableArray<INamedTypeSymbol> UpstreamSignatureSymbols { get; }

private LinqGenExpression(IMethodSymbol methodSymbol, INamedTypeSymbol? signatureSymbol,
ITypeSymbol? inputElementSymbol, INamedTypeSymbol[]? upstreamSignatureSymbols)
ITypeSymbol? inputElementSymbol, ImmutableArray<INamedTypeSymbol> upstreamSignatureSymbols)
{
MethodSymbol = methodSymbol;
SignatureSymbol = signatureSymbol;
Expand Down Expand Up @@ -57,7 +58,7 @@ public static bool TryParse(SemanticModel semanticModel,
}

ITypeSymbol? inputElementSymbol = null;
INamedTypeSymbol[]? upstreamSignatureSymbols = null;
IEnumerable<INamedTypeSymbol>? upstreamSignatureSymbols = null;

// this means it takes LinqGen enumerable as input, and upstream type is required
if (methodSymbol.ReceiverType is INamedTypeSymbol receiverTypeSymbol &&
Expand All @@ -80,10 +81,11 @@ public static bool TryParse(SemanticModel semanticModel,
signatureSymbol = NormalizeSignature(signatureSymbol);

if (upstreamSignatureSymbols != null)
upstreamSignatureSymbols = upstreamSignatureSymbols.Select(NormalizeSignature).ToArray();
upstreamSignatureSymbols = upstreamSignatureSymbols.Select(NormalizeSignature);

result = new LinqGenExpression(
methodSymbol, signatureSymbol, inputElementSymbol, upstreamSignatureSymbols);
methodSymbol, signatureSymbol, inputElementSymbol,
upstreamSignatureSymbols?.ToImmutableArray() ?? ImmutableArray<INamedTypeSymbol>.Empty);

return true;
}
Expand Down Expand Up @@ -129,7 +131,7 @@ public static bool TryParse(SemanticModel semanticModel,
}

result = new LinqGenExpression(
methodSymbol, null, inputElementSymbol, new[] { NormalizeSignature(upstreamSignatureSymbol) });
methodSymbol, null, inputElementSymbol, ImmutableArray.Create(upstreamSignatureSymbol));

return true;
}
Expand Down
Loading

0 comments on commit 3794290

Please sign in to comment.