Move common extensions to shared generation project
このコミットが含まれているのは:
1 親
ae717681c4
コミット
9a51331910
53ファイル変更
+90
-650
検索条件に一致するファイルはありません
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace ComputeSharp.SourceGeneration.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="HashCode"/>.
|
||||
/// </summary>
|
||||
internal static class HashCodeExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds all items from a given <see cref="ImmutableArray{T}"/> instance to an hashcode.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to hash.</typeparam>
|
||||
/// <param name="hashCode">The target <see cref="HashCode"/> instance.</param>
|
||||
/// <param name="items">The input items to hash.</param>
|
||||
public static void AddRange<T>(this ref HashCode hashCode, ImmutableArray<T> items)
|
||||
{
|
||||
foreach (T item in items)
|
||||
{
|
||||
hashCode.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds all items from a given <see cref="ImmutableArray{T}"/> instance to an hashcode.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to hash.</typeparam>
|
||||
/// <param name="hashCode">The target <see cref="HashCode"/> instance.</param>
|
||||
/// <param name="comparer">A comparer to get hashcodes for <typeparamref name="T"/> items.</param>
|
||||
/// <param name="items">The input items to hash.</param>
|
||||
public static void AddRange<T>(this ref HashCode hashCode, ImmutableArray<T> items, IEqualityComparer<T> comparer)
|
||||
{
|
||||
foreach (T item in items)
|
||||
{
|
||||
hashCode.Add(item, comparer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace ComputeSharp.SourceGeneration.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="IEqualityComparer{T}"/>.
|
||||
/// </summary>
|
||||
internal static class IEqualityComparerExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets an <see cref="IEqualityComparer{T}"/> for an <see cref="ImmutableArray{T}"/> sequence.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to compare.</typeparam>
|
||||
/// <param name="comparer">The compare for individual <typeparamref name="T"/> items.</param>
|
||||
public static IEqualityComparer<ImmutableArray<T>> ForImmutableArray<T>(this IEqualityComparer<T> comparer)
|
||||
{
|
||||
return new Comparer<T>(comparer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="IEqualityComparer{T}"/> implementation for an <see cref="ImmutableArray{T}"/> value.
|
||||
/// </summary>
|
||||
public sealed class Comparer<T> : IEqualityComparer<ImmutableArray<T>>
|
||||
{
|
||||
/// <summary>
|
||||
/// The <typeparamref name="T"/> comparer.
|
||||
/// </summary>
|
||||
private readonly IEqualityComparer<T> comparer;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Comparer{T}"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="comparer">The <typeparamref name="T"/> comparer.</param>
|
||||
public Comparer(IEqualityComparer<T> comparer)
|
||||
{
|
||||
this.comparer = comparer;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool Equals(ImmutableArray<T> x, ImmutableArray<T> y)
|
||||
{
|
||||
return
|
||||
x == y ||
|
||||
x.SequenceEqual(y, this.comparer);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int GetHashCode(ImmutableArray<T> obj)
|
||||
{
|
||||
HashCode hashCode = default;
|
||||
|
||||
hashCode.AddRange(obj, this.comparer);
|
||||
|
||||
return hashCode.ToHashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace ComputeSharp.SourceGeneration.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="ISymbol"/> types.
|
||||
/// </summary>
|
||||
internal static class ISymbolExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// A custom <see cref="SymbolDisplayFormat"/> instance with fully qualified style, without global::.
|
||||
/// </summary>
|
||||
public static readonly SymbolDisplayFormat FullyQualifiedWithoutGlobalFormat =
|
||||
SymbolDisplayFormat.FullyQualifiedFormat
|
||||
.WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted);
|
||||
|
||||
/// <summary>
|
||||
/// A custom <see cref="SymbolDisplayFormat"/> instance with fully qualified style, without global:: and parameters.
|
||||
/// </summary>
|
||||
private static readonly SymbolDisplayFormat FullyQualifiedWithoutGlobalAndParametersFormat =
|
||||
FullyQualifiedWithoutGlobalFormat
|
||||
.WithMemberOptions(SymbolDisplayMemberOptions.IncludeContainingType)
|
||||
.WithParameterOptions(SymbolDisplayParameterOptions.None);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the fully qualified name for a given symbol.
|
||||
/// </summary>
|
||||
/// <param name="symbol">The input <see cref="ISymbol"/> instance.</param>
|
||||
/// <returns>The fully qualified name for <paramref name="symbol"/>.</returns>
|
||||
public static string GetFullyQualifiedName(this ISymbol symbol)
|
||||
{
|
||||
return symbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a valid filename for a given <see cref="ITypeSymbol"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="symbol">The input <see cref="ITypeSymbol"/> instance.</param>
|
||||
/// <returns>The full metadata name for <paramref name="symbol"/> that is also a valid filename.</returns>
|
||||
public static string GetFullMetadataName(this ITypeSymbol symbol)
|
||||
{
|
||||
static StringBuilder BuildFrom(ISymbol? symbol, StringBuilder builder)
|
||||
{
|
||||
return symbol switch
|
||||
{
|
||||
INamespaceSymbol ns when ns.IsGlobalNamespace => builder,
|
||||
INamespaceSymbol ns when ns.ContainingNamespace is { IsGlobalNamespace: false }
|
||||
=> BuildFrom(ns.ContainingNamespace, builder.Insert(0, $".{ns.MetadataName}")),
|
||||
ITypeSymbol ts when ts.ContainingType is ISymbol containingType
|
||||
=> BuildFrom(containingType, builder.Insert(0, $"+{ts.MetadataName}")),
|
||||
ITypeSymbol ts when ts.ContainingNamespace is ISymbol containingNamespace and not INamespaceSymbol { IsGlobalNamespace: true }
|
||||
=> BuildFrom(containingNamespace, builder.Insert(0, $".{ts.MetadataName}")),
|
||||
ISymbol => BuildFrom(symbol.ContainingSymbol, builder.Insert(0, symbol.MetadataName)),
|
||||
_ => builder
|
||||
};
|
||||
}
|
||||
|
||||
return BuildFrom(symbol, new StringBuilder(256)).ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the full metadata name for a given <see cref="IMethodSymbol"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="symbol">The input <see cref="IMethodSymbol"/> instance.</param>
|
||||
/// <param name="includeParameters">Whether or not to also include parameter types.</param>
|
||||
/// <returns>The full metadata name for <paramref name="symbol"/>.</returns>
|
||||
public static string GetFullMetadataName(this IMethodSymbol symbol, bool includeParameters = false)
|
||||
{
|
||||
if (includeParameters)
|
||||
{
|
||||
var parameters = string.Join(", ", symbol.Parameters.Select(static p => ((INamedTypeSymbol)p.Type).GetFullMetadataName()));
|
||||
|
||||
return $"{symbol.ToDisplayString(FullyQualifiedWithoutGlobalAndParametersFormat)}({parameters})";
|
||||
}
|
||||
|
||||
return symbol.ToDisplayString(FullyQualifiedWithoutGlobalAndParametersFormat);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the full metadata name for a given <see cref="IPropertySymbol"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="symbol">The input <see cref="IPropertySymbol"/> instance.</param>
|
||||
/// <returns>The full metadata name for <paramref name="symbol"/>.</returns>
|
||||
public static string GetFullMetadataName(this IPropertySymbol symbol)
|
||||
{
|
||||
string declaringTypeName = symbol.ContainingType.GetFullMetadataName();
|
||||
|
||||
if (symbol.IsIndexer)
|
||||
{
|
||||
var parameters = string.Join(", ", symbol.Parameters.Select(static p => ((INamedTypeSymbol)p.Type).GetFullMetadataName()));
|
||||
|
||||
return $"{declaringTypeName}.this[{parameters}]";
|
||||
}
|
||||
|
||||
return $"{declaringTypeName}.{symbol.Name}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the full metadata name for a given <see cref="IFieldSymbol"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="symbol">The input <see cref="IFieldSymbol"/> instance.</param>
|
||||
/// <returns>The full metadata name for <paramref name="symbol"/>.</returns>
|
||||
public static string GetFullMetadataName(this IFieldSymbol symbol)
|
||||
{
|
||||
return $"{symbol.ContainingType.GetFullMetadataName()}.{symbol.Name}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a valid filename for a target symbol and generator type.
|
||||
/// </summary>
|
||||
/// <param name="symbol">The symbol being processed.</param>
|
||||
/// <returns>A filename in the form "<SYMBOL_FULLNAME>"</returns>
|
||||
public static string GetGeneratedFileName(this INamedTypeSymbol symbol)
|
||||
{
|
||||
return symbol.GetFullMetadataName().Replace('`', '-').Replace('+', '.');
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get an attribute with the specified full metadata name.
|
||||
/// </summary>
|
||||
/// <param name="symbol">The input <see cref="ISymbol"/> instance to check.</param>
|
||||
/// <param name="name">The attribute name to look for.</param>
|
||||
/// <param name="attributeData">The resulting attribute data, if found.</param>
|
||||
/// <returns>Whether or not <paramref name="symbol"/> has an attribute with the specified name.</returns>
|
||||
public static bool TryGetAttributeWithFullMetadataName(this ISymbol symbol, string name, out AttributeData? attributeData)
|
||||
{
|
||||
ImmutableArray<AttributeData> attributes = symbol.GetAttributes();
|
||||
|
||||
foreach (AttributeData attribute in attributes)
|
||||
{
|
||||
if (attribute.AttributeClass?.GetFullMetadataName() == name)
|
||||
{
|
||||
attributeData = attribute;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
attributeData = null;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace ComputeSharp.SourceGeneration.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="ImmutableArray{T}"/>.
|
||||
/// </summary>
|
||||
internal static class ImmutableArrayExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Compares two sequences for equality.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The items to compare.</typeparam>
|
||||
/// <param name="left">The left sequence of items.</param>
|
||||
/// <param name="right">The right sequence of items.</param>
|
||||
/// <returns>Whether or not <paramref name="left"/> equals <paramref name="right"/>.</returns>
|
||||
public static bool SequenceEqual<T>(this ImmutableArray<T> left, ImmutableArray<T> right)
|
||||
where T : IEquatable<T>
|
||||
{
|
||||
return left.AsSpan().SequenceEqual(right.AsSpan());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compares two sequences for equality.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The items to compare.</typeparam>
|
||||
/// <param name="left">The left sequence of items.</param>
|
||||
/// <param name="right">The right sequence of items.</param>
|
||||
/// <param name="comparer">The <see cref="IEqualityComparer{T}"/> instance used to compare <typeparamref name="T"/> items.</param>
|
||||
/// <returns>Whether or not <paramref name="left"/> equals <paramref name="right"/>.</returns>
|
||||
public static bool SequenceEqual<T>(this ImmutableArray<T> left, ImmutableArray<T> right, IEqualityComparer<T> comparer)
|
||||
{
|
||||
if (left.Length != right.Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < left.Length; i++)
|
||||
{
|
||||
if (!comparer.Equals(left[i], right[i]))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace ComputeSharp.SourceGeneration.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="IncrementalValuesProvider{TValues}"/>.
|
||||
/// </summary>
|
||||
internal static class IncrementalValuesProviderExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="IncrementalValuesProvider{TValues}"/> instance with a gven pair of comparers.
|
||||
/// </summary>
|
||||
/// <typeparam name="TLeft">The type of left items in each tuple.</typeparam>
|
||||
/// <typeparam name="TRight">The type of right items in each tuple.</typeparam>
|
||||
/// <param name="source">The input <see cref="IncrementalValuesProvider{TValues}"/> instance.</param>
|
||||
/// <param name="comparerLeft">An <see cref="IEqualityComparer{T}"/> instance for <typeparamref name="TLeft"/> items.</param>
|
||||
/// <param name="comparerRight">An <see cref="IEqualityComparer{T}"/> instance for <typeparamref name="TRight"/> items.</param>
|
||||
/// <returns>An <see cref="IncrementalValuesProvider{TValues}"/> with the specified comparers applied to each item.</returns>
|
||||
public static IncrementalValuesProvider<(TLeft Left, TRight Right)> WithComparers<TLeft, TRight>(
|
||||
this IncrementalValuesProvider<(TLeft Left, TRight Right)> source,
|
||||
IEqualityComparer<TLeft> comparerLeft,
|
||||
IEqualityComparer<TRight> comparerRight)
|
||||
{
|
||||
return source.WithComparer(new Comparer<TLeft, TRight>(comparerLeft, comparerRight));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="IncrementalValuesProvider{TValues}"/> instance with a gven pair of comparers.
|
||||
/// </summary>
|
||||
/// <typeparam name="T1">The type of first items in each tuple.</typeparam>
|
||||
/// <typeparam name="T2">The type of second items in each tuple.</typeparam>
|
||||
/// <typeparam name="T3">The type of third items in each tuple.</typeparam>
|
||||
/// <param name="source">The input <see cref="IncrementalValuesProvider{TValues}"/> instance.</param>
|
||||
/// <param name="comparer1">An <see cref="IEqualityComparer{T}"/> instance for <typeparamref name="T1"/> items.</param>
|
||||
/// <param name="comparer2">An <see cref="IEqualityComparer{T}"/> instance for <typeparamref name="T2"/> items.</param>
|
||||
/// <param name="comparer3">An <see cref="IEqualityComparer{T}"/> instance for <typeparamref name="T3"/> items.</param>
|
||||
/// <returns>An <see cref="IncrementalValuesProvider{TValues}"/> with the specified comparers applied to each item.</returns>
|
||||
public static IncrementalValuesProvider<(T1, T2, T3)> WithComparers<T1, T2, T3>(
|
||||
this IncrementalValuesProvider<(T1, T2, T3)> source,
|
||||
IEqualityComparer<T1> comparer1,
|
||||
IEqualityComparer<T2> comparer2,
|
||||
IEqualityComparer<T3> comparer3)
|
||||
{
|
||||
return source.WithComparer(new Comparer<T1, T2, T3>(comparer1, comparer2, comparer3));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="IEqualityComparer{T}"/> implementation for a value tuple.
|
||||
/// </summary>
|
||||
public sealed class Comparer<TLeft, TRight> : IEqualityComparer<(TLeft Left, TRight Right)>
|
||||
{
|
||||
/// <summary>
|
||||
/// The <typeparamref name="TLeft"/> comparer.
|
||||
/// </summary>
|
||||
private readonly IEqualityComparer<TLeft> comparerLeft;
|
||||
|
||||
/// <summary>
|
||||
/// The <typeparamref name="TRight"/> comparer.
|
||||
/// </summary>
|
||||
private readonly IEqualityComparer<TRight> comparerRight;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Comparer{TLeft, TRight}"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="comparerLeft">The <typeparamref name="TLeft"/> comparer.</param>
|
||||
/// <param name="comparerRight">The <typeparamref name="TRight"/> comparer.</param>
|
||||
public Comparer(IEqualityComparer<TLeft> comparerLeft, IEqualityComparer<TRight> comparerRight)
|
||||
{
|
||||
this.comparerLeft = comparerLeft;
|
||||
this.comparerRight = comparerRight;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool Equals((TLeft Left, TRight Right) x, (TLeft Left, TRight Right) y)
|
||||
{
|
||||
return
|
||||
this.comparerLeft.Equals(x.Left, y.Left) &&
|
||||
this.comparerRight.Equals(x.Right, y.Right);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int GetHashCode((TLeft Left, TRight Right) obj)
|
||||
{
|
||||
return HashCode.Combine(
|
||||
this.comparerLeft.GetHashCode(obj.Left),
|
||||
this.comparerRight.GetHashCode(obj.Right));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="IEqualityComparer{T}"/> implementation for a value tuple.
|
||||
/// </summary>
|
||||
public sealed class Comparer<T1, T2, T3> : IEqualityComparer<(T1, T2, T3)>
|
||||
{
|
||||
/// <summary>
|
||||
/// The <typeparamref name="T1"/> comparer.
|
||||
/// </summary>
|
||||
private readonly IEqualityComparer<T1> comparer1;
|
||||
|
||||
/// <summary>
|
||||
/// The <typeparamref name="T2"/> comparer.
|
||||
/// </summary>
|
||||
private readonly IEqualityComparer<T2> comparer2;
|
||||
|
||||
/// <summary>
|
||||
/// The <typeparamref name="T3"/> comparer.
|
||||
/// </summary>
|
||||
private readonly IEqualityComparer<T3> comparer3;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Comparer{T1, T2, T3}"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="comparer1">The <typeparamref name="T1"/> comparer.</param>
|
||||
/// <param name="comparer2">The <typeparamref name="T2"/> comparer.</param>
|
||||
/// <param name="comparer3">The <typeparamref name="T3"/> comparer.</param>
|
||||
public Comparer(IEqualityComparer<T1> comparer1, IEqualityComparer<T2> comparer2, IEqualityComparer<T3> comparer3)
|
||||
{
|
||||
this.comparer1 = comparer1;
|
||||
this.comparer2 = comparer2;
|
||||
this.comparer3 = comparer3;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool Equals((T1, T2, T3) x, (T1, T2, T3) y)
|
||||
{
|
||||
return
|
||||
this.comparer1.Equals(x.Item1, y.Item1) &&
|
||||
this.comparer2.Equals(x.Item2, y.Item2) &&
|
||||
this.comparer3.Equals(x.Item3, y.Item3);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public int GetHashCode((T1, T2, T3) obj)
|
||||
{
|
||||
return HashCode.Combine(
|
||||
this.comparer1.GetHashCode(obj.Item1),
|
||||
this.comparer2.GetHashCode(obj.Item2),
|
||||
this.comparer3.GetHashCode(obj.Item3));
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする