-
-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TODO: impl true incremental generator
- Loading branch information
Showing
9 changed files
with
173 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System.Collections; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace MemoryPack.Generator; | ||
|
||
public readonly struct EquatableArray<T> : IEquatable<EquatableArray<T>>, IEnumerable<T> | ||
where T : IEquatable<T> | ||
{ | ||
readonly T[]? array; | ||
|
||
public EquatableArray() // for collection literal [] | ||
{ | ||
array = []; | ||
} | ||
|
||
public EquatableArray(T[] array) | ||
{ | ||
this.array = array; | ||
} | ||
|
||
public static implicit operator EquatableArray<T>(T[] array) | ||
{ | ||
return new EquatableArray<T>(array); | ||
} | ||
|
||
public ref readonly T this[int index] | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get => ref array![index]; | ||
} | ||
|
||
public int Length => array!.Length; | ||
|
||
public ReadOnlySpan<T> AsSpan() | ||
{ | ||
return array.AsSpan(); | ||
} | ||
|
||
public ReadOnlySpan<T>.Enumerator GetEnumerator() | ||
{ | ||
return AsSpan().GetEnumerator(); | ||
} | ||
|
||
IEnumerator<T> IEnumerable<T>.GetEnumerator() | ||
{ | ||
return array.AsEnumerable().GetEnumerator(); | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return array.AsEnumerable().GetEnumerator(); | ||
} | ||
|
||
public bool Equals(EquatableArray<T> other) | ||
{ | ||
return AsSpan().SequenceEqual(other.AsSpan()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Microsoft.CodeAnalysis; | ||
using System.Collections.Immutable; | ||
|
||
namespace MemoryPack.Generator; | ||
|
||
public class EquatableTypeSymbol(ITypeSymbol typeSymbol) : IEquatable<EquatableTypeSymbol> | ||
{ | ||
// Used for build argument parser, maybe ok to equals name. | ||
public ITypeSymbol TypeSymbol => typeSymbol; | ||
|
||
// GetMembers is called for Enum and fields is not condition for command equality. | ||
public ImmutableArray<ISymbol> GetMembers() => typeSymbol.GetMembers(); | ||
|
||
public TypeKind TypeKind { get; } = typeSymbol.TypeKind; | ||
public SpecialType SpecialType { get; } = typeSymbol.SpecialType; | ||
|
||
public string ToFullyQualifiedFormatDisplayString() => typeSymbol.ToFullyQualifiedFormatDisplayString(); | ||
public string ToDisplayString(NullableFlowState state, SymbolDisplayFormat format) => typeSymbol.ToDisplayString(state, format); | ||
|
||
public bool Equals(EquatableTypeSymbol other) | ||
{ | ||
if (this.TypeKind != other.TypeKind) return false; | ||
if (this.SpecialType != other.SpecialType) return false; | ||
if (this.TypeSymbol.Name != other.TypeSymbol.Name) return false; | ||
|
||
return this.TypeSymbol.EqualsNamespaceAndName(other.TypeSymbol); | ||
} | ||
} | ||
|
||
static class EquatableTypeSymbolExtensions | ||
{ | ||
public static EquatableTypeSymbol ToEquatable(this ITypeSymbol typeSymbol) => new(typeSymbol); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
38 changes: 38 additions & 0 deletions
38
tests/MemoryPack.Tests/SourceGeneratorTests/IncrementalGeneratorTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace MemoryPack.Tests.SourceGeneratorTests; | ||
|
||
public class IncrementalGeneratorTest | ||
{ | ||
[Fact] | ||
public void Run() | ||
{ | ||
// lang=C#-test | ||
var step1 = """ | ||
[MemoryPackable] | ||
public partial class MyClass | ||
{ | ||
public int MyProperty { get; set; } | ||
} | ||
"""; | ||
|
||
// lang=C#-test | ||
var step2 = """ | ||
[MemoryPackable] | ||
public partial class MyClass | ||
{ | ||
public int MyProperty { get; set; } | ||
// unrelated line | ||
} | ||
"""; | ||
|
||
var hoge = CSharpGeneratorRunner.GetIncrementalGeneratorTrackedStepsReasons("MemoryPack.MemoryPackable.", step1, step2); | ||
|
||
} | ||
} | ||
|
||
|