Added support for static fields and properties in local class

このコミットが含まれているのは:
Sergio Pedri committed 2019-09-06 13:41:55 +02:00
1 親 36d9a54d1b
コミット 4001c60566
3ファイル変更
+65 -2

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

+36 -1
ファイルの表示
@@ -197,7 +197,6 @@ namespace ComputeSharp.Shaders.Extensions
variable = (name, propertyInfo);
break;
case MethodInfo methodInfo:
method = (name, methodInfo);
return SyntaxFactory.InvocationExpression(SyntaxFactory.ParseExpression(name), node.ArgumentList).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia());
default: throw new InvalidOperationException($"Invalid symbol kind: {result.Value.MemberInfo.GetType()}");
@@ -210,6 +209,42 @@ namespace ComputeSharp.Shaders.Extensions
return node;
}
/// <summary>
/// Checks a <see cref="IdentifierNameSyntax"/> instance and replaces it to be HLSL compatible, if needed
/// </summary>
/// <param name="node">The input <see cref="IdentifierNameSyntax"/> to check and modify if needed</param>
/// <param name="declaringType">The <see cref="Type"/> in which the input syntax tree is declared in</param>
/// <param name="variable">The info on parsed static members, if any</param>
/// <returns>A <see cref="SyntaxNode"/> instance that is compatible with HLSL</returns>
[Pure]
public static IdentifierNameSyntax ReplaceIdentifierName(this IdentifierNameSyntax node, Type declaringType, out (string Name, ReadableMember MemberInfo)? variable)
{
// Set the variable to null
variable = null;
// Explore the nested classes from bottom to top
if (declaringType.TryFindAncestorMember(node.Identifier.ToString(), BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, out var result))
{
// If a static scalar or vector member is found, track it
string name = $"{result.Value.DeclaringType.Name}_{result.Value.MemberInfo.Name}";
switch (result.Value.MemberInfo)
{
case FieldInfo fieldInfo when HlslKnownTypes.IsKnownType(fieldInfo.FieldType):
variable = (name, fieldInfo);
break;
case PropertyInfo propertyInfo when HlslKnownTypes.IsKnownType(propertyInfo.PropertyType):
variable = (name, propertyInfo);
break;
default: return node;
}
// Return the mapped expression
return SyntaxFactory.IdentifierName(name).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia());
}
return node;
}
/// <summary>
/// Processes a loaded static member, either a field or a property
/// </summary>
+12
ファイルの表示
@@ -58,6 +58,18 @@ namespace ComputeSharp.Shaders.Mappings
/// </summary>
public static IReadOnlyCollection<Type> HlslMappedVectorTypes => _HlslMappedVectorTypes;
/// <summary>
/// Checks whether or not the input type is a known HLSL-compatible type
/// </summary>
/// <param name="type">The input <see cref="Type"/> instance to check</param>
/// <returns>A <see langword="bool"/> indicating whether the input <see cref="Type"/> is in fact a known HLSL type</returns>
[Pure]
public static bool IsKnownType(Type type) => IsKnownScalarType(type) ||
IsKnownVectorType(type) ||
IsConstantBufferType(type) ||
IsReadOnlyBufferType(type) ||
IsReadWriteBufferType(type);
/// <summary>
/// Checks whether or not the input type is a known scalar type
/// </summary>
+17 -1
ファイルの表示
@@ -27,7 +27,8 @@ namespace ComputeSharp.Shaders.Translation
/// <summary>
/// Creates a new <see cref="ShaderSyntaxRewriter"/> instance with the specified parameters
/// </summary>
/// <param name="semanticModel"></param>
/// <param name="semanticModel">The <see cref="Microsoft.CodeAnalysis.SemanticModel"/> instance to use to rewrite the decompiled code</param>
/// <param name="declaringType">The declaring type that hosts the root from which the current syntax tree is inspected</param>
public ShaderSyntaxRewriter(SemanticModel semanticModel, Type declaringType)
{
SemanticModel = semanticModel;
@@ -48,6 +49,21 @@ namespace ComputeSharp.Shaders.Translation
/// </summary>
public IReadOnlyDictionary<string, MethodInfo> StaticMethods => _StaticMethods;
/// <inheritdoc/>
public override SyntaxNode VisitIdentifierName(IdentifierNameSyntax node)
{
node = (IdentifierNameSyntax)base.VisitIdentifierName(node);
node = node.ReplaceIdentifierName(DeclaringType, out var variable);
// Register the captured member, if any
if (variable.HasValue && !_StaticMembers.ContainsKey(variable.Value.Name))
{
_StaticMembers.Add(variable.Value.Name, variable.Value.MemberInfo);
}
return node;
}
/// <inheritdoc/>
public override SyntaxNode VisitParameter(ParameterSyntax node)
{