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::.
///
public static readonly SymbolDisplayFormat FullyQualifiedWithoutGlobalFormat =
SymbolDisplayFormat.FullyQualifiedFormat
.WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted);
///
/// A custom instance with fully qualified style, without global:: and parameters.
///
private static readonly SymbolDisplayFormat FullyQualifiedWithoutGlobalAndParametersFormat =
FullyQualifiedWithoutGlobalFormat
.WithMemberOptions(SymbolDisplayMemberOptions.IncludeContainingType)
.WithParameterOptions(SymbolDisplayParameterOptions.None);
///
/// Gets the fully qualified name for a given symbol.
///
/// The input instance.
/// The fully qualified name for .
public static string GetFullyQualifiedName(this ISymbol symbol)
{
return symbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat);
}
///
/// Gets the full metadata name for a given instance.
///
/// The input instance.
/// The full metadata name for .
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();
}
///
/// 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 .
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 the full metadata name for a given instance.
///
/// The input instance.
/// The full metadata name for .
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}";
}
///
/// Gets the full metadata name for a given instance.
///
/// The input instance.
/// The full metadata name for .
public static string GetFullMetadataName(this IFieldSymbol symbol)
{
return $"{symbol.ContainingType.GetFullMetadataName()}.{symbol.Name}";
}
///
/// Gets a valid filename for a target symbol and generator type.
///
/// The symbol being processed.
/// A filename in the form "<SYMBOL_FULLNAME>"
public static string GetGeneratedFileName(this INamedTypeSymbol symbol)
{
return symbol.GetFullMetadataName().Replace('`', '-').Replace('+', '.');
}
}