Fixed overload resolution in matrix constructors

このコミットが含まれているのは:
Sergio Pedri committed 2021-03-02 18:08:32 +01:00
1 親 ec3d8059c3
コミット 808dcbdf79
2ファイル変更
+71 -8

検索条件に一致するファイルはありません

+40 -7
ファイルの表示
@@ -15,6 +15,23 @@ namespace ComputeSharp.SourceGenerators.Mappings
/// </summary>
internal static class HlslKnownTypes
{
/// <summary>
/// The collection of known matrix types.
/// </summary>
private static readonly IReadOnlyCollection<Type> KnownMatrixTypes = BuildKnownMatrixTypes();
/// <summary>
/// Builds the mapping of known matrix types.
/// </summary>
[Pure]
private static IReadOnlyCollection<Type> BuildKnownMatrixTypes()
{
return (
from type in Assembly.GetExecutingAssembly().ExportedTypes
where Regex.IsMatch(type.FullName, @"^ComputeSharp\.(Bool|Double|Float|Int|UInt)")
select type).ToArray();
}
/// <summary>
/// The mapping of supported known types to HLSL types.
/// </summary>
@@ -39,11 +56,7 @@ namespace ComputeSharp.SourceGenerators.Mappings
};
// Add all the mapped vector and matrix types
foreach (
var type in
from type in Assembly.GetExecutingAssembly().ExportedTypes
where Regex.IsMatch(type.FullName, @"^ComputeSharp\.(Bool|Double|Float|Int|UInt)")
select type)
foreach (var type in KnownMatrixTypes)
{
knownTypes.Add(type.FullName, type.Name.ToLower());
}
@@ -120,15 +133,35 @@ namespace ComputeSharp.SourceGenerators.Mappings
}
/// <summary>
/// Checks whether or not a given type name matches a typed resource type.
/// Checks whether or not a given type name matches a scalar or vector type.
/// </summary>
/// <param name="typeName">The input type name to check.</param>
/// <returns>Whether or not <paramref name="typeName"/> represents a typed resource type.</returns>
/// <returns>Whether or not <paramref name="typeName"/> represents a scalar or vector.</returns>
public static bool IsScalarOrVectorType(string typeName)
{
return KnownTypes.ContainsKey(typeName);
}
/// <summary>
/// Checks whether or not a given type name matches a vector type.
/// </summary>
/// <param name="typeName">The input type name to check.</param>
/// <returns>Whether or not <paramref name="typeName"/> represents a vector type.</returns>
public static bool IsVectorType(string typeName)
{
return HlslMappedVectorTypes.Any(type => type.FullName == typeName);
}
/// <summary>
/// Checks whether or not a given type name matches a matrix type.
/// </summary>
/// <param name="typeName">The input type name to check.</param>
/// <returns>Whether or not <paramref name="typeName"/> represents a matrix type.</returns>
public static bool IsMatrixType(string typeName)
{
return KnownMatrixTypes.Any(type => type.FullName == typeName);
}
/// <summary>
/// Gets the mapped HLSL-compatible type name for the input type symbol.
/// </summary>
+31 -1
ファイルの表示
@@ -240,7 +240,22 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
return CastExpression(updatedNode.Type, LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0)));
}
return InvocationExpression(updatedNode.Type, updatedNode.ArgumentList);
// Add explicit casts for matrix constructors to help the overload resolution
if (this.semanticModel.GetTypeInfo(node).Type is ITypeSymbol matrixType &&
HlslKnownTypes.IsMatrixType(matrixType.GetFullMetadataName()))
{
for (int i = 0; i < node.ArgumentList!.Arguments.Count; i++)
{
IArgumentOperation argumentOperation = (IArgumentOperation)this.semanticModel.GetOperation(node.ArgumentList.Arguments[i])!;
INamedTypeSymbol elementType = (INamedTypeSymbol)argumentOperation.Parameter.Type;
updatedNode = updatedNode.ReplaceNode(
updatedNode.ArgumentList!.Arguments[i].Expression,
CastExpression(IdentifierName(HlslKnownTypes.GetMappedName(elementType)), updatedNode.ArgumentList.Arguments[i].Expression));
}
}
return InvocationExpression(updatedNode.Type, updatedNode.ArgumentList!);
}
/// <inheritdoc/>
@@ -271,6 +286,21 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
return CastExpression(explicitType, LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0)));
}
// Add explicit casts like with the explicit object creation expressions above
if (this.semanticModel.GetTypeInfo(node).Type is ITypeSymbol matrixType &&
HlslKnownTypes.IsMatrixType(matrixType.GetFullMetadataName()))
{
for (int i = 0; i < node.ArgumentList.Arguments.Count; i++)
{
IArgumentOperation argumentOperation = (IArgumentOperation)this.semanticModel.GetOperation(node.ArgumentList.Arguments[i])!;
INamedTypeSymbol elementType = (INamedTypeSymbol)argumentOperation.Parameter.Type;
updatedNode = updatedNode.ReplaceNode(
updatedNode.ArgumentList.Arguments[i].Expression,
CastExpression(IdentifierName(HlslKnownTypes.GetMappedName(elementType)), updatedNode.ArgumentList.Arguments[i].Expression));
}
}
return InvocationExpression(explicitType, updatedNode.ArgumentList);
}