using System; using System.Collections.Generic; using System.Collections.Immutable; using ComputeSharp.SourceGeneration.Extensions; using Microsoft.CodeAnalysis; using static Microsoft.CodeAnalysis.SymbolDisplayTypeQualificationStyle; namespace ComputeSharp.SourceGeneration.Models; /// /// A model describing the hierarchy info for a specific type. /// /// The filename hint for the current type. /// The metadata name for the current type. /// Gets the namespace for the current type. /// Gets the sequence of type definitions containing the current type. internal sealed record HierarchyInfo(string FilenameHint, string MetadataName, string Namespace, ImmutableArray Names) { /// /// Creates a new instance from a given . /// /// The input instance to gather info for. /// A instance describing . public static HierarchyInfo From(INamedTypeSymbol typeSymbol) { ImmutableArray.Builder names = ImmutableArray.CreateBuilder(); for (INamedTypeSymbol? parent = typeSymbol; parent is not null; parent = parent.ContainingType) { names.Add(parent.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat)); } return new( typeSymbol.GetGeneratedFileName(), typeSymbol.MetadataName, typeSymbol.ContainingNamespace.ToDisplayString(new(typeQualificationStyle: NameAndContainingTypesAndNamespaces)), names.ToImmutable()); } /// /// An implementation for . /// public sealed class Comparer : IEqualityComparer { /// /// The singleton instance. /// public static Comparer Default { get; } = new(); /// public bool Equals(HierarchyInfo? x, HierarchyInfo? y) { if (x is null && y is null) { return true; } if (x is null || y is null) { return false; } if (ReferenceEquals(x, y)) { return true; } return x.FilenameHint == y.FilenameHint && x.MetadataName == y.MetadataName && x.Namespace == y.Namespace && x.Names.SequenceEqual(y.Names); } /// public int GetHashCode(HierarchyInfo obj) { HashCode hashCode = default; hashCode.Add(obj.FilenameHint); hashCode.Add(obj.MetadataName); hashCode.Add(obj.Namespace); hashCode.AddRange(obj.Names); return hashCode.ToHashCode(); } } }