using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Mappings;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace ComputeSharp.SourceGeneration.SyntaxRewriters;
///
partial class HlslSourceRewriter
{
///
/// Tracks the associated type for a value and returns the HLSL compatible .
///
/// The input to check.
/// The instance with info on the input tree.
/// A instance that represents a type compatible with HLSL.
protected TypeSyntax TrackType(SyntaxNode node, SemanticModel semanticModel)
{
ITypeSymbol typeSymbol = semanticModel.GetTypeInfo(node, CancellationToken).Type!;
return TrackType(typeSymbol);
}
///
/// Tracks an instance and returns the HLSL compatible .
///
/// The input instance to process.
/// A instance that represents a type compatible with HLSL.
protected TypeSyntax TrackType(ITypeSymbol typeSymbol)
{
string typeName = typeSymbol.GetFullyQualifiedName();
DiscoveredTypes.Add((INamedTypeSymbol)typeSymbol);
if (HlslKnownTypes.TryGetMappedName(typeName, out string? mappedName))
{
return ParseTypeName(mappedName);
}
return ParseTypeName(typeName.ToHlslIdentifierName());
}
///
/// 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.
protected TRoot ReplaceAndTrackType(TRoot node, SyntaxNode sourceType, SemanticModel semanticModel)
where TRoot : TypeSyntax
{
return ReplaceAndTrackType(node, 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.
protected TRoot ReplaceAndTrackType(TRoot node, TypeSyntax targetType, SyntaxNode sourceType, SemanticModel semanticModel)
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, CancellationToken).Type!,
PointerTypeSyntax pointerType => semanticModel.GetTypeInfo(pointerType.ElementType, CancellationToken).Type!,
ArrayTypeSyntax arrayType => semanticModel.GetTypeInfo(arrayType.ElementType, CancellationToken).Type!,
_ => semanticModel.GetTypeInfo(sourceType, CancellationToken).Type!
};
// Do nothing if the type is just void
if (typeSymbol.SpecialType == SpecialType.System_Void)
{
return node;
}
string typeName = typeSymbol.GetFullyQualifiedName();
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.ToHlslIdentifierName()));
}
}