using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using ComputeSharp.SourceGenerators.Mappings;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace ComputeSharp.SourceGenerators.Extensions
{
///
/// A with some extension methods for C# syntax nodes.
///
internal static class SyntaxNodeExtensions
{
///
/// Checks a value and replaces the value type to be HLSL compatible, if needed.
///
/// The type of the input instance.
/// The input to check and modify if needed.
/// The source to use to get type into from .
/// The instance with info on the input tree.
/// The collection of currently discovered types.
/// A instance that represents a type compatible with HLSL.
[Pure]
public static TRoot ReplaceAndTrackType(this TRoot node, SyntaxNode sourceType, SemanticModel semanticModel, ICollection discoveredTypes)
where TRoot : TypeSyntax
{
return node.ReplaceAndTrackType(node, sourceType, semanticModel, discoveredTypes);
}
///
/// Checks a value and replaces the value type to be HLSL compatible, if needed.
///
/// The type of the input instance.
/// The input to check and modify if needed.
/// The target node to replace.
/// The source to use to get type into from .
/// The instance with info on the input tree.
/// The collection of currently discovered types.
/// A instance that represents a type compatible with HLSL.
[Pure]
public static TRoot ReplaceAndTrackType(this TRoot node, TypeSyntax targetType, SyntaxNode sourceType, SemanticModel semanticModel, ICollection discoveredTypes)
where TRoot : SyntaxNode
{
// Skip immediately for function pointers
if (sourceType is FunctionPointerTypeSyntax)
{
return node.ReplaceNode(targetType, ParseTypeName("void*"));
}
// Handle the various possible type kinds
ITypeSymbol typeSymbol = sourceType switch
{
RefTypeSyntax refType => semanticModel.GetTypeInfo(refType.Type).Type!,
PointerTypeSyntax pointerType => semanticModel.GetTypeInfo(pointerType.ElementType).Type!,
_ => semanticModel.GetTypeInfo(sourceType).Type!
};
// Do nothing if the type is just void
if (typeSymbol.SpecialType == SpecialType.System_Void)
{
return node;
}
string typeName = typeSymbol.ToDisplayString(ISymbolExtensions.FullyQualifiedWithoutGlobalFormat);
discoveredTypes.Add((INamedTypeSymbol)typeSymbol);
if (HlslKnownTypes.TryGetMappedName(typeName, out string? mappedName))
{
TypeSyntax newType = ParseTypeName(mappedName!);
return node.ReplaceNode(targetType, newType);
}
return node.ReplaceNode(targetType, ParseTypeName(typeName.Replace(".", "__")));
}
///
/// Tracks the associated type for a value and returns the HLSL compatible .
///
/// The input to check.
/// The instance with info on the input tree.
/// The collection of currently discovered types.
/// A instance that represents a type compatible with HLSL.
[Pure]
public static TypeSyntax TrackType(this SyntaxNode node, SemanticModel semanticModel, ICollection discoveredTypes)
{
ITypeSymbol typeSymbol = semanticModel.GetTypeInfo(node).Type!;
string typeName = typeSymbol.ToDisplayString(ISymbolExtensions.FullyQualifiedWithoutGlobalFormat);
discoveredTypes.Add((INamedTypeSymbol)typeSymbol);
if (HlslKnownTypes.TryGetMappedName(typeName, out string? mappedName))
{
return ParseTypeName(mappedName!);
}
return ParseTypeName(typeName.Replace(".", "__"));
}
///
/// Returns a with a block body.
///
/// The input node.
/// A node like the one in input, but always with a block body.
[Pure]
public static MethodDeclarationSyntax WithBlockBody(this MethodDeclarationSyntax node)
{
if (node.ExpressionBody is ArrowExpressionClauseSyntax arrow)
{
StatementSyntax statement = node.ReturnType switch
{
PredefinedTypeSyntax pts when pts.Keyword.IsKind(SyntaxKind.VoidKeyword) => ExpressionStatement(arrow.Expression),
_ => ReturnStatement(arrow.Expression)
};
return node
.WithBody(Block(statement))
.WithExpressionBody(null)
.WithSemicolonToken(MissingToken(SyntaxKind.SemicolonToken));
}
return node;
}
///
/// Returns a with a block body.
///
/// The input node.
/// A node like the one in input, but always with a block body.
///
/// This method is the same as , but it is necessary to
/// duplicate the code because the two types don't have a common base type or interface that can be leveraged.
///
[Pure]
public static LocalFunctionStatementSyntax WithBlockBody(this LocalFunctionStatementSyntax node)
{
if (node.ExpressionBody is ArrowExpressionClauseSyntax arrow)
{
StatementSyntax statement = node.ReturnType switch
{
PredefinedTypeSyntax pts when pts.Keyword.IsKind(SyntaxKind.VoidKeyword) => ExpressionStatement(arrow.Expression),
_ => ReturnStatement(arrow.Expression)
};
return node
.WithBody(Block(statement))
.WithExpressionBody(null)
.WithSemicolonToken(MissingToken(SyntaxKind.SemicolonToken));
}
return node;
}
///
/// Returns a instance with no accessibility modifiers.
///
/// The input node.
/// A node just like but with no accessibility modifiers.
[Pure]
public static MethodDeclarationSyntax WithoutAccessibilityModifiers(this MethodDeclarationSyntax node)
{
return node.WithModifiers(TokenList(node.Modifiers.Where(static modifier => modifier.Kind() switch
{
SyntaxKind.PublicKeyword => false,
SyntaxKind.PrivateKeyword => false,
SyntaxKind.ProtectedKeyword => false,
SyntaxKind.InternalKeyword => false,
_ => true
}).ToArray()));
}
}
}