using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
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 : Comparer
{
///
protected override void AddToHashCode(ref HashCode hashCode, HierarchyInfo obj)
{
hashCode.Add(obj.FilenameHint);
hashCode.Add(obj.MetadataName);
hashCode.Add(obj.Namespace);
hashCode.AddRange(obj.Names);
}
///
protected override bool AreEqual(HierarchyInfo x, HierarchyInfo y)
{
return
x.FilenameHint == y.FilenameHint &&
x.MetadataName == y.MetadataName &&
x.Namespace == y.Namespace &&
x.Names.SequenceEqual(y.Names);
}
}
}