using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
namespace ComputeSharp.SourceGenerators.Extensions
{
///
/// Extension methods for types.
///
internal static class ISymbolExtensions
{
///
/// A custom instance with fully qualified style, without global:: and parameters.
///
private static readonly SymbolDisplayFormat FullyQualifiedWithoutGlobalAndParametersFormat = new(
SymbolDisplayGlobalNamespaceStyle.Omitted,
SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces,
SymbolDisplayGenericsOptions.IncludeTypeParameters,
memberOptions: SymbolDisplayMemberOptions.IncludeContainingType,
parameterOptions: SymbolDisplayParameterOptions.None);
///
/// Gets the full metadata name for a given instance.
///
/// The input instance.
/// The full metadata name for .
[Pure]
public static string GetFullMetadataName(this INamedTypeSymbol 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 pt => BuildFrom(pt, builder.Insert(0, $"+{ts.MetadataName}")),
ITypeSymbol ts when ts.ContainingNamespace is ISymbol pn => BuildFrom(pn, builder.Insert(0, $".{ts.MetadataName}")),
ISymbol => BuildFrom(symbol.ContainingSymbol, builder.Insert(0, symbol.MetadataName)),
_ => builder
};
}
return BuildFrom(symbol, new StringBuilder(256)).ToString();
}
///
/// Gets the full metadata name for a given instance.
///
/// The input instance.
/// Whether or not to also include parameter types.
/// The full metadata name for .
[Pure]
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);
}
///
/// Gets a valid filename for a target symbol and generator type.
///
/// The generator type processing the input symbol.
/// The symbol being processed.
/// A filename in the form "[ComputeSharp]_[<TGENERATOR>]_[<SYMBOL_FULLNAME>]"
[Pure]
public static string GetGeneratedFileName(this INamedTypeSymbol symbol)
{
string
metadataName = symbol.GetFullMetadataName(),
fixedName = metadataName.Replace('`', '-').Replace('+', '.');
return $"[{nameof(ComputeSharp)}]_[{typeof(TGenerator).Name}]_[{fixedName}]";
}
///
/// Gets a valid filename for a target symbol and generator type.
///
/// The generator type processing the input symbol.
/// The symbol being processed.
/// A filename in the form "[ComputeSharp]_[<TGENERATOR>]_[<SYMBOL_FULLNAME>]"
[Pure]
public static string GetGeneratedFileName(this IMethodSymbol symbol)
{
string
metadataName = symbol.GetFullMetadataName(true),
fixedName = metadataName.Replace('`', '-').Replace('+', '.');
return $"[{nameof(ComputeSharp)}]_[{typeof(TGenerator).Name}]_[{fixedName}]";
}
}
}