using System.Diagnostics.Contracts; using ComputeSharp.SourceGenerators.Mappings; using Microsoft.CodeAnalysis; 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 { /// /// A custom instance with fully qualified style, without global::. /// private static readonly SymbolDisplayFormat FullyQualifiedWithoutGlobalFormat = new( globalNamespaceStyle: SymbolDisplayGlobalNamespaceStyle.Omitted, typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces, genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters); /// /// 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. /// A instance that represents a type compatible with HLSL. [Pure] public static TRoot ReplaceType(this TRoot node, SyntaxNode sourceType, SemanticModel semanticModel) where TRoot : TypeSyntax { return node.ReplaceType(node, sourceType, semanticModel); } /// /// 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. /// A instance that represents a type compatible with HLSL. [Pure] public static TRoot ReplaceType(this TRoot node, TypeSyntax targetType, SyntaxNode sourceType, SemanticModel semanticModel) where TRoot : SyntaxNode { if (semanticModel.GetTypeInfo(sourceType).Type is ITypeSymbol typeSymbol && HlslKnownTypes.TryGetMappedName(typeSymbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat), out string? mappedName)) { TypeSyntax newType = ParseTypeName(mappedName!); return node.ReplaceNode(targetType, newType); } return node; } /// /// 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 to use for the input node. /// A instance that represents a type compatible with HLSL. [Pure] public static TypeSyntax ReplaceType(this LiteralExpressionSyntax node, SemanticModel semanticModel) { ITypeSymbol typeSymbol = semanticModel.GetTypeInfo(node).Type!; string typeName = typeSymbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat); if (HlslKnownTypes.TryGetMappedName(typeName, out string? mappedName)) { return ParseTypeName(mappedName!); } return ParseTypeName(typeName); } /// /// 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) { return node.WithBody((node.Body, node.ExpressionBody) switch { (BlockSyntax block, _) => block, (_, ArrowExpressionClauseSyntax arrow) => Block(ExpressionStatement(arrow.Expression)), _ => Block() }); } } }