Refactored code to use file scoped using declarations

このコミットが含まれているのは:
Sergio Pedri committed 2021-10-30 15:11:43 +02:00
1 親 52ec5a79b0
コミット d5e3114f7c
275ファイル変更
+91970 -92271

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

+120 -121
ファイルの表示
@@ -7,145 +7,144 @@ using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace ComputeSharp.SourceGenerators.Extensions
namespace ComputeSharp.SourceGenerators.Extensions;
/// <summary>
/// Extension methods for <see cref="ISymbol"/> types.
/// </summary>
internal static class ISymbolExtensions
{
/// <summary>
/// Extension methods for <see cref="ISymbol"/> types.
/// A custom <see cref="SymbolDisplayFormat"/> instance with fully qualified style, without global::.
/// </summary>
internal static class ISymbolExtensions
public static readonly SymbolDisplayFormat FullyQualifiedWithoutGlobalFormat = new(
globalNamespaceStyle: SymbolDisplayGlobalNamespaceStyle.Omitted,
typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces,
genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters);
/// <summary>
/// A custom <see cref="SymbolDisplayFormat"/> instance with fully qualified style, without global:: and parameters.
/// </summary>
private static readonly SymbolDisplayFormat FullyQualifiedWithoutGlobalAndParametersFormat =
FullyQualifiedWithoutGlobalFormat
.WithMemberOptions(SymbolDisplayMemberOptions.IncludeContainingType)
.WithParameterOptions(SymbolDisplayParameterOptions.None);
/// <summary>
/// Gets the full metadata name for a given <see cref="ITypeSymbol"/> instance.
/// </summary>
/// <param name="symbol">The input <see cref="ITypeSymbol"/> instance.</param>
/// <returns>The full metadata name for <paramref name="symbol"/>.</returns>
[Pure]
public static string GetFullMetadataName(this ITypeSymbol symbol)
{
/// <summary>
/// A custom <see cref="SymbolDisplayFormat"/> instance with fully qualified style, without global::.
/// </summary>
public static readonly SymbolDisplayFormat FullyQualifiedWithoutGlobalFormat = new(
globalNamespaceStyle: SymbolDisplayGlobalNamespaceStyle.Omitted,
typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces,
genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters);
/// <summary>
/// A custom <see cref="SymbolDisplayFormat"/> instance with fully qualified style, without global:: and parameters.
/// </summary>
private static readonly SymbolDisplayFormat FullyQualifiedWithoutGlobalAndParametersFormat =
FullyQualifiedWithoutGlobalFormat
.WithMemberOptions(SymbolDisplayMemberOptions.IncludeContainingType)
.WithParameterOptions(SymbolDisplayParameterOptions.None);
/// <summary>
/// Gets the full metadata name for a given <see cref="ITypeSymbol"/> instance.
/// </summary>
/// <param name="symbol">The input <see cref="ITypeSymbol"/> instance.</param>
/// <returns>The full metadata name for <paramref name="symbol"/>.</returns>
[Pure]
public static string GetFullMetadataName(this ITypeSymbol symbol)
static StringBuilder BuildFrom(ISymbol? symbol, StringBuilder builder)
{
static StringBuilder BuildFrom(ISymbol? symbol, StringBuilder builder)
return symbol switch
{
return symbol switch
{
INamespaceSymbol ns when ns.IsGlobalNamespace => builder,
INamespaceSymbol ns when ns.ContainingNamespace is { IsGlobalNamespace: false }
=> BuildFrom(ns.ContainingNamespace, builder.Insert(0, $".{ns.MetadataName}")),
ITypeSymbol ts when ts.ContainingType is ISymbol pt => BuildFrom(pt, builder.Insert(0, $"+{ts.MetadataName}")),
ITypeSymbol ts when ts.ContainingNamespace is ISymbol pn => BuildFrom(pn, builder.Insert(0, $".{ts.MetadataName}")),
ISymbol => BuildFrom(symbol.ContainingSymbol, builder.Insert(0, symbol.MetadataName)),
_ => builder
};
}
return BuildFrom(symbol, new StringBuilder(256)).ToString();
INamespaceSymbol ns when ns.IsGlobalNamespace => builder,
INamespaceSymbol ns when ns.ContainingNamespace is { IsGlobalNamespace: false }
=> BuildFrom(ns.ContainingNamespace, builder.Insert(0, $".{ns.MetadataName}")),
ITypeSymbol ts when ts.ContainingType is ISymbol pt => BuildFrom(pt, builder.Insert(0, $"+{ts.MetadataName}")),
ITypeSymbol ts when ts.ContainingNamespace is ISymbol pn => BuildFrom(pn, builder.Insert(0, $".{ts.MetadataName}")),
ISymbol => BuildFrom(symbol.ContainingSymbol, builder.Insert(0, symbol.MetadataName)),
_ => builder
};
}
/// <summary>
/// Gets the full metadata name for a given <see cref="IMethodSymbol"/> instance.
/// </summary>
/// <param name="symbol">The input <see cref="IMethodSymbol"/> instance.</param>
/// <param name="includeParameters">Whether or not to also include parameter types.</param>
/// <returns>The full metadata name for <paramref name="symbol"/>.</returns>
[Pure]
public static string GetFullMetadataName(this IMethodSymbol symbol, bool includeParameters = false)
return BuildFrom(symbol, new StringBuilder(256)).ToString();
}
/// <summary>
/// Gets the full metadata name for a given <see cref="IMethodSymbol"/> instance.
/// </summary>
/// <param name="symbol">The input <see cref="IMethodSymbol"/> instance.</param>
/// <param name="includeParameters">Whether or not to also include parameter types.</param>
/// <returns>The full metadata name for <paramref name="symbol"/>.</returns>
[Pure]
public static string GetFullMetadataName(this IMethodSymbol symbol, bool includeParameters = false)
{
if (includeParameters)
{
if (includeParameters)
{
var parameters = string.Join(", ", symbol.Parameters.Select(static p => ((INamedTypeSymbol)p.Type).GetFullMetadataName()));
var parameters = string.Join(", ", symbol.Parameters.Select(static p => ((INamedTypeSymbol)p.Type).GetFullMetadataName()));
return $"{symbol.ToDisplayString(FullyQualifiedWithoutGlobalAndParametersFormat)}({parameters})";
}
return symbol.ToDisplayString(FullyQualifiedWithoutGlobalAndParametersFormat);
return $"{symbol.ToDisplayString(FullyQualifiedWithoutGlobalAndParametersFormat)}({parameters})";
}
/// <summary>
/// Gets the full metadata name for a given <see cref="IPropertySymbol"/> instance.
/// </summary>
/// <param name="symbol">The input <see cref="IPropertySymbol"/> instance.</param>
/// <returns>The full metadata name for <paramref name="symbol"/>.</returns>
[Pure]
public static string GetFullMetadataName(this IPropertySymbol symbol)
return symbol.ToDisplayString(FullyQualifiedWithoutGlobalAndParametersFormat);
}
/// <summary>
/// Gets the full metadata name for a given <see cref="IPropertySymbol"/> instance.
/// </summary>
/// <param name="symbol">The input <see cref="IPropertySymbol"/> instance.</param>
/// <returns>The full metadata name for <paramref name="symbol"/>.</returns>
[Pure]
public static string GetFullMetadataName(this IPropertySymbol symbol)
{
string declaringTypeName = symbol.ContainingType.GetFullMetadataName();
if (symbol.IsIndexer)
{
string declaringTypeName = symbol.ContainingType.GetFullMetadataName();
var parameters = string.Join(", ", symbol.Parameters.Select(static p => ((INamedTypeSymbol)p.Type).GetFullMetadataName()));
if (symbol.IsIndexer)
{
var parameters = string.Join(", ", symbol.Parameters.Select(static p => ((INamedTypeSymbol)p.Type).GetFullMetadataName()));
return $"{declaringTypeName}.this[{parameters}]";
}
return $"{declaringTypeName}.{symbol.Name}";
return $"{declaringTypeName}.this[{parameters}]";
}
/// <summary>
/// Gets the full metadata name for a given <see cref="IFieldSymbol"/> instance.
/// </summary>
/// <param name="symbol">The input <see cref="IFieldSymbol"/> instance.</param>
/// <returns>The full metadata name for <paramref name="symbol"/>.</returns>
[Pure]
public static string GetFullMetadataName(this IFieldSymbol symbol)
return $"{declaringTypeName}.{symbol.Name}";
}
/// <summary>
/// Gets the full metadata name for a given <see cref="IFieldSymbol"/> instance.
/// </summary>
/// <param name="symbol">The input <see cref="IFieldSymbol"/> instance.</param>
/// <returns>The full metadata name for <paramref name="symbol"/>.</returns>
[Pure]
public static string GetFullMetadataName(this IFieldSymbol symbol)
{
return $"{symbol.ContainingType.GetFullMetadataName()}.{symbol.Name}";
}
/// <summary>
/// Gets a valid filename for a target symbol and generator type.
/// </summary>
/// <param name="symbol">The symbol being processed.</param>
/// <returns>A filename in the form "&lt;SYMBOL_FULLNAME&gt;"</returns>
[Pure]
public static string GetGeneratedFileName(this INamedTypeSymbol symbol)
{
return symbol.GetFullMetadataName().Replace('`', '-').Replace('+', '.');
}
/// <summary>
/// Gets a valid filename for a target symbol and generator type.
/// </summary>
/// <param name="symbol">The symbol being processed.</param>
/// <returns>A filename in the form "&lt;SYMBOL_FULLNAME&gt;"</returns>
[Pure]
public static string GetGeneratedFileName(this IMethodSymbol symbol)
{
return symbol.GetFullMetadataName(true).Replace('`', '-').Replace('+', '.');
}
/// <summary>
/// Tracks an <see cref="ITypeSymbol"/> instance and returns an HLSL compatible <see cref="TypeSyntax"/> object.
/// </summary>
/// <param name="typeSymbol">The input <see cref="ITypeSymbol"/> instance to process.</param>
/// <param name="discoveredTypes">The collection of currently discovered types.</param>
/// <returns>A <see cref="SyntaxNode"/> instance that represents a type compatible with HLSL.</returns>
[Pure]
public static TypeSyntax TrackType(this ITypeSymbol typeSymbol, ICollection<INamedTypeSymbol> discoveredTypes)
{
string typeName = typeSymbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat);
discoveredTypes.Add((INamedTypeSymbol)typeSymbol);
if (HlslKnownTypes.TryGetMappedName(typeName, out string? mappedName))
{
return $"{symbol.ContainingType.GetFullMetadataName()}.{symbol.Name}";
return ParseTypeName(mappedName!);
}
/// <summary>
/// Gets a valid filename for a target symbol and generator type.
/// </summary>
/// <param name="symbol">The symbol being processed.</param>
/// <returns>A filename in the form "&lt;SYMBOL_FULLNAME&gt;"</returns>
[Pure]
public static string GetGeneratedFileName(this INamedTypeSymbol symbol)
{
return symbol.GetFullMetadataName().Replace('`', '-').Replace('+', '.');
}
/// <summary>
/// Gets a valid filename for a target symbol and generator type.
/// </summary>
/// <param name="symbol">The symbol being processed.</param>
/// <returns>A filename in the form "&lt;SYMBOL_FULLNAME&gt;"</returns>
[Pure]
public static string GetGeneratedFileName(this IMethodSymbol symbol)
{
return symbol.GetFullMetadataName(true).Replace('`', '-').Replace('+', '.');
}
/// <summary>
/// Tracks an <see cref="ITypeSymbol"/> instance and returns an HLSL compatible <see cref="TypeSyntax"/> object.
/// </summary>
/// <param name="typeSymbol">The input <see cref="ITypeSymbol"/> instance to process.</param>
/// <param name="discoveredTypes">The collection of currently discovered types.</param>
/// <returns>A <see cref="SyntaxNode"/> instance that represents a type compatible with HLSL.</returns>
[Pure]
public static TypeSyntax TrackType(this ITypeSymbol typeSymbol, ICollection<INamedTypeSymbol> discoveredTypes)
{
string typeName = typeSymbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat);
discoveredTypes.Add((INamedTypeSymbol)typeSymbol);
if (HlslKnownTypes.TryGetMappedName(typeName, out string? mappedName))
{
return ParseTypeName(mappedName!);
}
return ParseTypeName(typeName.ToHlslIdentifierName());
}
return ParseTypeName(typeName.ToHlslIdentifierName());
}
}
+12 -13
ファイルの表示
@@ -1,21 +1,20 @@
using System.Diagnostics.Contracts;
namespace ComputeSharp.SourceGenerators.Extensions
namespace ComputeSharp.SourceGenerators.Extensions;
/// <summary>
/// Extension methods for <see cref="string"/> types.
/// </summary>
internal static class StringExtensions
{
/// <summary>
/// Extension methods for <see cref="string"/> types.
/// Converts a given <see cref="string"/> instance to a valid HLSL identifier name.
/// </summary>
internal static class StringExtensions
/// <param name="text">The input <see cref="string"/> instance to convert.</param>
/// <returns>An valid HLSL identifier name from <paramref name="text"/>.</returns>
[Pure]
public static string ToHlslIdentifierName(this string text)
{
/// <summary>
/// Converts a given <see cref="string"/> instance to a valid HLSL identifier name.
/// </summary>
/// <param name="text">The input <see cref="string"/> instance to convert.</param>
/// <returns>An valid HLSL identifier name from <paramref name="text"/>.</returns>
[Pure]
public static string ToHlslIdentifierName(this string text)
{
return text.Replace('.', '_').Replace('+', '_');
}
return text.Replace('.', '_').Replace('+', '_');
}
}
+170 -171
ファイルの表示
@@ -7,202 +7,201 @@ using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace ComputeSharp.SourceGenerators.Extensions
namespace ComputeSharp.SourceGenerators.Extensions;
/// <summary>
/// A <see langword="class"/> with some extension methods for C# syntax nodes.
/// </summary>
internal static class SyntaxNodeExtensions
{
/// <summary>
/// A <see langword="class"/> with some extension methods for C# syntax nodes.
/// Checks a <see cref="SyntaxNode"/> value and replaces the value type to be HLSL compatible, if needed.
/// </summary>
internal static class SyntaxNodeExtensions
/// <typeparam name="TRoot">The type of the input <see cref="TypeSyntax"/> instance.</typeparam>
/// <param name="node">The input <see cref="SyntaxNode"/> to check and modify if needed.</param>
/// <param name="sourceType">The source <see cref="SyntaxNode"/> to use to get type into from <paramref name="semanticModel"/>.</param>
/// <param name="semanticModel">The <see cref="SemanticModel"/> instance with info on the input tree.</param>
/// <param name="discoveredTypes">The collection of currently discovered types.</param>
/// <returns>A <see cref="SyntaxNode"/> instance that represents a type compatible with HLSL.</returns>
[Pure]
public static TRoot ReplaceAndTrackType<TRoot>(this TRoot node, SyntaxNode sourceType, SemanticModel semanticModel, ICollection<INamedTypeSymbol> discoveredTypes)
where TRoot : TypeSyntax
{
/// <summary>
/// Checks a <see cref="SyntaxNode"/> value and replaces the value type to be HLSL compatible, if needed.
/// </summary>
/// <typeparam name="TRoot">The type of the input <see cref="TypeSyntax"/> instance.</typeparam>
/// <param name="node">The input <see cref="SyntaxNode"/> to check and modify if needed.</param>
/// <param name="sourceType">The source <see cref="SyntaxNode"/> to use to get type into from <paramref name="semanticModel"/>.</param>
/// <param name="semanticModel">The <see cref="SemanticModel"/> instance with info on the input tree.</param>
/// <param name="discoveredTypes">The collection of currently discovered types.</param>
/// <returns>A <see cref="SyntaxNode"/> instance that represents a type compatible with HLSL.</returns>
[Pure]
public static TRoot ReplaceAndTrackType<TRoot>(this TRoot node, SyntaxNode sourceType, SemanticModel semanticModel, ICollection<INamedTypeSymbol> discoveredTypes)
where TRoot : TypeSyntax
return node.ReplaceAndTrackType(node, sourceType, semanticModel, discoveredTypes);
}
/// <summary>
/// Checks a <see cref="SyntaxNode"/> value and replaces the value type to be HLSL compatible, if needed.
/// </summary>
/// <typeparam name="TRoot">The type of the input <see cref="SyntaxNode"/> instance.</typeparam>
/// <param name="node">The input <see cref="SyntaxNode"/> to check and modify if needed.</param>
/// <param name="targetType">The target <see cref="TypeSyntax"/> node to replace.</param>
/// <param name="sourceType">The source <see cref="SyntaxNode"/> to use to get type into from <paramref name="semanticModel"/>.</param>
/// <param name="semanticModel">The <see cref="SemanticModel"/> instance with info on the input tree.</param>
/// <param name="discoveredTypes">The collection of currently discovered types.</param>
/// <returns>A <see cref="SyntaxNode"/> instance that represents a type compatible with HLSL.</returns>
[Pure]
public static TRoot ReplaceAndTrackType<TRoot>(this TRoot node, TypeSyntax targetType, SyntaxNode sourceType, SemanticModel semanticModel, ICollection<INamedTypeSymbol> discoveredTypes)
where TRoot : SyntaxNode
{
// Skip immediately for function pointers
if (sourceType is FunctionPointerTypeSyntax)
{
return node.ReplaceAndTrackType(node, sourceType, semanticModel, discoveredTypes);
return node.ReplaceNode(targetType, ParseTypeName("void*"));
}
/// <summary>
/// Checks a <see cref="SyntaxNode"/> value and replaces the value type to be HLSL compatible, if needed.
/// </summary>
/// <typeparam name="TRoot">The type of the input <see cref="SyntaxNode"/> instance.</typeparam>
/// <param name="node">The input <see cref="SyntaxNode"/> to check and modify if needed.</param>
/// <param name="targetType">The target <see cref="TypeSyntax"/> node to replace.</param>
/// <param name="sourceType">The source <see cref="SyntaxNode"/> to use to get type into from <paramref name="semanticModel"/>.</param>
/// <param name="semanticModel">The <see cref="SemanticModel"/> instance with info on the input tree.</param>
/// <param name="discoveredTypes">The collection of currently discovered types.</param>
/// <returns>A <see cref="SyntaxNode"/> instance that represents a type compatible with HLSL.</returns>
[Pure]
public static TRoot ReplaceAndTrackType<TRoot>(this TRoot node, TypeSyntax targetType, SyntaxNode sourceType, SemanticModel semanticModel, ICollection<INamedTypeSymbol> discoveredTypes)
where TRoot : SyntaxNode
// Handle the various possible type kinds
ITypeSymbol typeSymbol = sourceType switch
{
// Skip immediately for function pointers
if (sourceType is FunctionPointerTypeSyntax)
{
return node.ReplaceNode(targetType, ParseTypeName("void*"));
}
RefTypeSyntax refType => semanticModel.GetTypeInfo(refType.Type).Type!,
PointerTypeSyntax pointerType => semanticModel.GetTypeInfo(pointerType.ElementType).Type!,
_ => semanticModel.GetTypeInfo(sourceType).Type!
};
// Handle the various possible type kinds
ITypeSymbol typeSymbol = sourceType switch
// 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.ToHlslIdentifierName()));
}
/// <summary>
/// Tracks the associated type for a <see cref="SyntaxNode"/> value and returns the HLSL compatible <see cref="TypeSyntax"/>.
/// </summary>
/// <param name="node">The input <see cref="SyntaxNode"/> to check.</param>
/// <param name="semanticModel">The <see cref="SemanticModel"/> instance with info on the input tree.</param>
/// <param name="discoveredTypes">The collection of currently discovered types.</param>
/// <returns>A <see cref="SyntaxNode"/> instance that represents a type compatible with HLSL.</returns>
[Pure]
public static TypeSyntax TrackType(this SyntaxNode node, SemanticModel semanticModel, ICollection<INamedTypeSymbol> 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.ToHlslIdentifierName());
}
/// <summary>
/// Returns a <see cref="MethodDeclarationSyntax"/> with a block body.
/// </summary>
/// <param name="node">The input <see cref="MethodDeclarationSyntax"/> node.</param>
/// <returns>A node like the one in input, but always with a block body.</returns>
[Pure]
public static MethodDeclarationSyntax WithBlockBody(this MethodDeclarationSyntax node)
{
if (node.ExpressionBody is ArrowExpressionClauseSyntax arrow)
{
StatementSyntax statement = node.ReturnType switch
{
RefTypeSyntax refType => semanticModel.GetTypeInfo(refType.Type).Type!,
PointerTypeSyntax pointerType => semanticModel.GetTypeInfo(pointerType.ElementType).Type!,
_ => semanticModel.GetTypeInfo(sourceType).Type!
PredefinedTypeSyntax pts when pts.Keyword.IsKind(SyntaxKind.VoidKeyword) => ExpressionStatement(arrow.Expression),
_ => ReturnStatement(arrow.Expression)
};
// 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.ToHlslIdentifierName()));
return node
.WithBody(Block(statement))
.WithExpressionBody(null)
.WithSemicolonToken(MissingToken(SyntaxKind.SemicolonToken));
}
/// <summary>
/// Tracks the associated type for a <see cref="SyntaxNode"/> value and returns the HLSL compatible <see cref="TypeSyntax"/>.
/// </summary>
/// <param name="node">The input <see cref="SyntaxNode"/> to check.</param>
/// <param name="semanticModel">The <see cref="SemanticModel"/> instance with info on the input tree.</param>
/// <param name="discoveredTypes">The collection of currently discovered types.</param>
/// <returns>A <see cref="SyntaxNode"/> instance that represents a type compatible with HLSL.</returns>
[Pure]
public static TypeSyntax TrackType(this SyntaxNode node, SemanticModel semanticModel, ICollection<INamedTypeSymbol> discoveredTypes)
return node;
}
/// <summary>
/// Returns a <see cref="MethodDeclarationSyntax"/> as a method definition.
/// </summary>
/// <param name="node">The input <see cref="MethodDeclarationSyntax"/> node.</param>
/// <returns>A node like the one in input, but just as a definition.</returns>
[Pure]
public static MethodDeclarationSyntax AsDefinition(this MethodDeclarationSyntax node)
{
if (node.ExpressionBody is ArrowExpressionClauseSyntax)
{
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.ToHlslIdentifierName());
return node.WithExpressionBody(null).WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
}
/// <summary>
/// Returns a <see cref="MethodDeclarationSyntax"/> with a block body.
/// </summary>
/// <param name="node">The input <see cref="MethodDeclarationSyntax"/> node.</param>
/// <returns>A node like the one in input, but always with a block body.</returns>
[Pure]
public static MethodDeclarationSyntax WithBlockBody(this MethodDeclarationSyntax node)
return node.WithBody(null).WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
}
/// <summary>
/// Returns a <see cref="LocalFunctionStatementSyntax"/> with a block body.
/// </summary>
/// <param name="node">The input <see cref="LocalFunctionStatementSyntax"/> node.</param>
/// <returns>A node like the one in input, but always with a block body.</returns>
/// <remarks>
/// This method is the same as <see cref="WithBlockBody(MethodDeclarationSyntax)"/>, 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.
/// </remarks>
[Pure]
public static LocalFunctionStatementSyntax WithBlockBody(this LocalFunctionStatementSyntax node)
{
if (node.ExpressionBody is ArrowExpressionClauseSyntax arrow)
{
if (node.ExpressionBody is ArrowExpressionClauseSyntax arrow)
StatementSyntax statement = node.ReturnType switch
{
StatementSyntax statement = node.ReturnType switch
{
PredefinedTypeSyntax pts when pts.Keyword.IsKind(SyntaxKind.VoidKeyword) => ExpressionStatement(arrow.Expression),
_ => ReturnStatement(arrow.Expression)
};
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;
return node
.WithBody(Block(statement))
.WithExpressionBody(null)
.WithSemicolonToken(MissingToken(SyntaxKind.SemicolonToken));
}
/// <summary>
/// Returns a <see cref="MethodDeclarationSyntax"/> as a method definition.
/// </summary>
/// <param name="node">The input <see cref="MethodDeclarationSyntax"/> node.</param>
/// <returns>A node like the one in input, but just as a definition.</returns>
[Pure]
public static MethodDeclarationSyntax AsDefinition(this MethodDeclarationSyntax node)
return node;
}
/// <summary>
/// Returns a <see cref="LocalFunctionStatementSyntax"/> as a method definition.
/// </summary>
/// <param name="node">The input <see cref="LocalFunctionStatementSyntax"/> node.</param>
/// <returns>A node like the one in input, but just as a definition.</returns>
[Pure]
public static LocalFunctionStatementSyntax AsDefinition(this LocalFunctionStatementSyntax node)
{
if (node.ExpressionBody is ArrowExpressionClauseSyntax)
{
if (node.ExpressionBody is ArrowExpressionClauseSyntax)
{
return node.WithExpressionBody(null).WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
}
return node.WithBody(null).WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
return node.WithExpressionBody(null).WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
}
/// <summary>
/// Returns a <see cref="LocalFunctionStatementSyntax"/> with a block body.
/// </summary>
/// <param name="node">The input <see cref="LocalFunctionStatementSyntax"/> node.</param>
/// <returns>A node like the one in input, but always with a block body.</returns>
/// <remarks>
/// This method is the same as <see cref="WithBlockBody(MethodDeclarationSyntax)"/>, 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.
/// </remarks>
[Pure]
public static LocalFunctionStatementSyntax WithBlockBody(this LocalFunctionStatementSyntax node)
return node.WithBody(null).WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
}
/// <summary>
/// Returns a <see cref="MethodDeclarationSyntax"/> instance with no accessibility modifiers.
/// </summary>
/// <param name="node">The input <see cref="MethodDeclarationSyntax"/> node.</param>
/// <returns>A node just like <paramref name="node"/> but with no accessibility modifiers.</returns>
[Pure]
public static MethodDeclarationSyntax WithoutAccessibilityModifiers(this MethodDeclarationSyntax node)
{
return node.WithModifiers(TokenList(node.Modifiers.Where(static modifier => modifier.Kind() switch
{
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;
}
/// <summary>
/// Returns a <see cref="LocalFunctionStatementSyntax"/> as a method definition.
/// </summary>
/// <param name="node">The input <see cref="LocalFunctionStatementSyntax"/> node.</param>
/// <returns>A node like the one in input, but just as a definition.</returns>
[Pure]
public static LocalFunctionStatementSyntax AsDefinition(this LocalFunctionStatementSyntax node)
{
if (node.ExpressionBody is ArrowExpressionClauseSyntax)
{
return node.WithExpressionBody(null).WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
}
return node.WithBody(null).WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
}
/// <summary>
/// Returns a <see cref="MethodDeclarationSyntax"/> instance with no accessibility modifiers.
/// </summary>
/// <param name="node">The input <see cref="MethodDeclarationSyntax"/> node.</param>
/// <returns>A node just like <paramref name="node"/> but with no accessibility modifiers.</returns>
[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()));
}
SyntaxKind.PublicKeyword => false,
SyntaxKind.PrivateKeyword => false,
SyntaxKind.ProtectedKeyword => false,
SyntaxKind.InternalKeyword => false,
_ => true
}).ToArray()));
}
}