using System; using System.Collections.Immutable; using System.Diagnostics.Contracts; using ComputeSharp.SourceGenerators.Extensions; using Microsoft.CodeAnalysis; using static Microsoft.CodeAnalysis.SymbolDisplayTypeQualificationStyle; namespace ComputeSharp.SourceGenerators.Models; /// /// A model describing the hierarchy info for a specific type. /// /// The filename hint 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 Namespace, ImmutableArray Names) { /// /// Creates a new instance from a given . /// /// The input instance to gather info for. /// A instance describing . [Pure] 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.ContainingNamespace.ToDisplayString(new(typeQualificationStyle: NameAndContainingTypesAndNamespaces)), names.ToImmutable()); } /// [Pure] public bool Equals(HierarchyInfo? other) { return other is not null && (ReferenceEquals(this, other) || (Namespace == other.Namespace && (Names == other.Names || Names.AsSpan().SequenceEqual(other.Names.AsSpan())))); } /// [Pure] public override int GetHashCode() { return HashCode.Combine(Namespace, Names, Names[0]); } }