Merge pull request #700 from Sergio0694/dev/immutable-builder-enumerate
Add and leverage ImmutableArrayBuilder<T>.AsEnumerable()
このコミットが含まれているのは:
2ファイル変更
+97
-14
検索条件に一致するファイルはありません
@@ -269,9 +269,10 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter
|
||||
// the property access to the corresponding HLSL syntax. For instance, m[M11, M12] will become m._m00_m01.
|
||||
if (HlslKnownProperties.IsKnownMatrixIndexer(propertyName))
|
||||
{
|
||||
bool isValid = true;
|
||||
using ImmutableArrayBuilder<string> hlslPropertyParts = new();
|
||||
|
||||
// Validate the arguments
|
||||
// Validate the arguments and gather all the indexer parts in a single step (without using LINQ).
|
||||
// This prepares all the individual parts to combine into the HLSL property name when rewriting.
|
||||
foreach (ArgumentSyntax argument in node.ArgumentList.Arguments)
|
||||
{
|
||||
if (SemanticModel.For(node).GetOperation(argument.Expression) is not IFieldReferenceOperation fieldReference ||
|
||||
@@ -279,20 +280,24 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter
|
||||
{
|
||||
Diagnostics.Add(NonConstantMatrixSwizzledIndex, argument);
|
||||
|
||||
isValid = false;
|
||||
hlslPropertyParts.Clear();
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// We have a valid field reference, we can process it
|
||||
string fieldName = fieldReference.Field.Name;
|
||||
char row = (char)(fieldName[1] - 1);
|
||||
char column = (char)(fieldName[2] - 1);
|
||||
|
||||
hlslPropertyParts.Add($"_m{row}{column}");
|
||||
}
|
||||
|
||||
if (isValid)
|
||||
// If we have any property parts, it means the property access is valid.
|
||||
// So we can create the combined HLSL property and rewrite the node.
|
||||
if (hlslPropertyParts.Count > 0)
|
||||
{
|
||||
// Rewrite the indexer as a property access
|
||||
string hlslPropertyName = string.Join("",
|
||||
from argument in node.ArgumentList.Arguments
|
||||
let fieldReference = (IFieldReferenceOperation)SemanticModel.For(node).GetOperation(argument.Expression)!
|
||||
let fieldName = fieldReference.Field.Name
|
||||
let row = (char)(fieldName[1] - 1)
|
||||
let column = (char)(fieldName[2] - 1)
|
||||
select $"_m{row}{column}");
|
||||
string hlslPropertyName = string.Join("", hlslPropertyParts.AsEnumerable());
|
||||
|
||||
return MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
@@ -47,7 +49,7 @@ internal struct ImmutableArrayBuilder<T> : IDisposable
|
||||
public readonly int Count
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => this.writer!.WrittenSpan.Length;
|
||||
get => this.writer!.Count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -95,6 +97,18 @@ internal struct ImmutableArrayBuilder<T> : IDisposable
|
||||
this.writer!.Insert(index, item);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an <see cref="IEnumerable{T}"/> instance for the current builder.
|
||||
/// </summary>
|
||||
/// <returns>An <see cref="IEnumerable{T}"/> instance for the current builder.</returns>
|
||||
/// <remarks>
|
||||
/// The builder should not be mutated while an enumerator is in use.
|
||||
/// </remarks>
|
||||
public readonly IEnumerable<T> AsEnumerable()
|
||||
{
|
||||
return this.writer!;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ImmutableArray{T}.Builder.ToImmutable"/>
|
||||
public readonly ImmutableArray<T> ToImmutable()
|
||||
{
|
||||
@@ -133,7 +147,7 @@ internal struct ImmutableArrayBuilder<T> : IDisposable
|
||||
/// <summary>
|
||||
/// A class handling the actual buffer writing.
|
||||
/// </summary>
|
||||
private sealed class Writer
|
||||
private sealed class Writer : IList<T>, IReadOnlyList<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// The underlying <typeparamref name="T"/> array.
|
||||
@@ -162,6 +176,9 @@ internal struct ImmutableArrayBuilder<T> : IDisposable
|
||||
this.index = 0;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int Count => this.index;
|
||||
|
||||
/// <inheritdoc cref="ImmutableArrayBuilder{T}.WrittenSpan"/>
|
||||
public ReadOnlySpan<T> WrittenSpan
|
||||
{
|
||||
@@ -169,6 +186,19 @@ internal struct ImmutableArrayBuilder<T> : IDisposable
|
||||
get => new(this.array, 0, this.index);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool ICollection<T>.IsReadOnly => true;
|
||||
|
||||
/// <inheritdoc/>
|
||||
T IReadOnlyList<T>.this[int index] => WrittenSpan[index];
|
||||
|
||||
/// <inheritdoc/>
|
||||
T IList<T>.this[int index]
|
||||
{
|
||||
get => WrittenSpan[index];
|
||||
set => throw new NotSupportedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ImmutableArrayBuilder{T}.Advance"/>
|
||||
public Span<T> Advance(int requestedSize)
|
||||
{
|
||||
@@ -268,6 +298,54 @@ internal struct ImmutableArrayBuilder<T> : IDisposable
|
||||
|
||||
this.array = newArray;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
int IList<T>.IndexOf(T item)
|
||||
{
|
||||
return Array.IndexOf(this.array, item, 0, this.index);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void IList<T>.RemoveAt(int index)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool ICollection<T>.Contains(T item)
|
||||
{
|
||||
return Array.IndexOf(this.array, item, 0, this.index) >= 0;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ICollection<T>.CopyTo(T[] array, int arrayIndex)
|
||||
{
|
||||
Array.Copy(this.array, 0, array, arrayIndex, this.index);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool ICollection<T>.Remove(T item)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
IEnumerator<T> IEnumerable<T>.GetEnumerator()
|
||||
{
|
||||
T?[] array = this.array!;
|
||||
int length = this.index;
|
||||
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
yield return array[i]!;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return ((IEnumerable<T>)this).GetEnumerator();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする