diff --git a/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems b/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems index 05057cf3..1fa9c05e 100644 --- a/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems +++ b/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems @@ -13,6 +13,10 @@ + + + + diff --git a/src/ComputeSharp.SourceGeneration/Extensions/IFieldSymbolExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/IFieldSymbolExtensions.cs new file mode 100644 index 00000000..92c3c869 --- /dev/null +++ b/src/ComputeSharp.SourceGeneration/Extensions/IFieldSymbolExtensions.cs @@ -0,0 +1,28 @@ +using System; +using ComputeSharp.SourceGeneration.Helpers; +using Microsoft.CodeAnalysis; + +namespace ComputeSharp.SourceGeneration.Extensions; + +/// +/// Extension methods for types. +/// +internal static class IFieldSymbolExtensions +{ + /// + /// Gets the fully qualified metadata name for a given instance. + /// + /// The input instance. + /// The fully qualified metadata name for . + public static string GetFullyQualifiedMetadataName(this IFieldSymbol symbol) + { + using ImmutableArrayBuilder builder = ImmutableArrayBuilder.Rent(); + + symbol.ContainingType!.AppendFullyQualifiedMetadataName(in builder); + + builder.Add('.'); + builder.AddRange(symbol.Name.AsSpan()); + + return builder.ToString(); + } +} \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration/Extensions/IMethodSymbolExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/IMethodSymbolExtensions.cs new file mode 100644 index 00000000..fc01872c --- /dev/null +++ b/src/ComputeSharp.SourceGeneration/Extensions/IMethodSymbolExtensions.cs @@ -0,0 +1,54 @@ +using System; +using ComputeSharp.SourceGeneration.Helpers; +using Microsoft.CodeAnalysis; + +namespace ComputeSharp.SourceGeneration.Extensions; + +/// +/// Extension methods for types. +/// +internal static class IMethodSymbolExtensions +{ + /// + /// Gets the fully qualified metadata name for a given instance. + /// + /// The input instance. + /// Whether or not to also include parameter types. + /// The fully qualified metadata name for . + public static string GetFullyQualifiedMetadataName(this IMethodSymbol symbol, bool includeParameters = false) + { + using ImmutableArrayBuilder builder = ImmutableArrayBuilder.Rent(); + + symbol.ContainingType!.AppendFullyQualifiedMetadataName(in builder); + + // Always add the method name after the containing type + builder.Add('.'); + builder.AddRange(symbol.Name.AsSpan()); + + if (includeParameters) + { + builder.Add('('); + + bool isFirstParameter = true; + + foreach (IParameterSymbol parameter in symbol.Parameters) + { + // Append the leading ', ' if needed + if (isFirstParameter) + { + isFirstParameter = false; + } + else + { + builder.AddRange(", ".AsSpan()); + } + + parameter.Type.AppendFullyQualifiedMetadataName(in builder); + } + + builder.Add(')'); + } + + return builder.ToString(); + } +} \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration/Extensions/IPropertySymbolExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/IPropertySymbolExtensions.cs new file mode 100644 index 00000000..57fc7661 --- /dev/null +++ b/src/ComputeSharp.SourceGeneration/Extensions/IPropertySymbolExtensions.cs @@ -0,0 +1,58 @@ +using System; +using ComputeSharp.SourceGeneration.Helpers; +using Microsoft.CodeAnalysis; + +namespace ComputeSharp.SourceGeneration.Extensions; + +/// +/// Extension methods for types. +/// +internal static class IPropertySymbolExtensions +{ + /// + /// Gets the fully qualified metadata name for a given instance. + /// + /// The input instance. + /// The fully qualified metadata name for . + public static string GetFullyQualifiedMetadataName(this IPropertySymbol symbol) + { + using ImmutableArrayBuilder builder = ImmutableArrayBuilder.Rent(); + + symbol.ContainingType!.AppendFullyQualifiedMetadataName(in builder); + + if (symbol.IsIndexer) + { + // Add the ".this[...]" suffix (parameters will be added below) + builder.AddRange(".this[".AsSpan()); + + bool isFirstParameter = true; + + // If the property is an indexer, also includes the parameters + foreach (IParameterSymbol parameter in symbol.Parameters) + { + // Append the leading ', ' if needed + if (isFirstParameter) + { + isFirstParameter = false; + } + else + { + builder.AddRange(", ".AsSpan()); + } + + parameter.Type.AppendFullyQualifiedMetadataName(in builder); + } + + // Add the closing bracket for ".this[...]" + builder.Add(']'); + } + else + { + // For normal properties, just append the name after the containing type + builder.Add('.'); + builder.AddRange(symbol.Name.AsSpan()); + } + + return builder.ToString(); + } +} \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs index 3195986d..fc24ec06 100644 --- a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs +++ b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs @@ -1,6 +1,3 @@ -using System; -using System.Linq; -using ComputeSharp.SourceGeneration.Helpers; using Microsoft.CodeAnalysis; namespace ComputeSharp.SourceGeneration.Extensions; @@ -29,142 +26,6 @@ internal static class ISymbolExtensions return symbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat); } - /// - /// Checks whether or not a given type symbol has a specified fully qualified metadata name. - /// - /// The input instance to check. - /// The full name to check. - /// Whether has a full name equals to . - public static bool HasFullyQualifiedMetadataName(this ITypeSymbol symbol, string name) - { - using ImmutableArrayBuilder builder = ImmutableArrayBuilder.Rent(); - - AppendFullyQualifiedMetadataName(symbol, in builder); - - return builder.WrittenSpan.SequenceEqual(name.AsSpan()); - } - - /// - /// Gets the fully qualified metadata name for a given instance. - /// - /// The input instance. - /// The fully qualified metadata name for . - public static string GetFullyQualifiedMetadataName(this ITypeSymbol symbol) - { - using ImmutableArrayBuilder builder = ImmutableArrayBuilder.Rent(); - - AppendFullyQualifiedMetadataName(symbol, in builder); - - return builder.ToString(); - } - - /// - /// Gets the fully qualified metadata name for a given instance. - /// - /// The input instance. - /// Whether or not to also include parameter types. - /// The fully qualified metadata name for . - public static string GetFullyQualifiedMetadataName(this IMethodSymbol symbol, bool includeParameters = false) - { - using ImmutableArrayBuilder builder = ImmutableArrayBuilder.Rent(); - - AppendFullyQualifiedMetadataName(symbol.ContainingType!, in builder); - - // Always add the method name after the containing type - builder.Add('.'); - builder.AddRange(symbol.Name.AsSpan()); - - if (includeParameters) - { - builder.Add('('); - - bool isFirstParameter = true; - - foreach (IParameterSymbol parameter in symbol.Parameters) - { - // Append the leading ', ' if needed - if (isFirstParameter) - { - isFirstParameter = false; - } - else - { - builder.AddRange(", ".AsSpan()); - } - - AppendFullyQualifiedMetadataName(parameter.Type, in builder); - } - - builder.Add(')'); - } - - return builder.ToString(); - } - - /// - /// Gets the fully qualified metadata name for a given instance. - /// - /// The input instance. - /// The fully qualified metadata name for . - public static string GetFullyQualifiedMetadataName(this IPropertySymbol symbol) - { - using ImmutableArrayBuilder builder = ImmutableArrayBuilder.Rent(); - - AppendFullyQualifiedMetadataName(symbol.ContainingType!, in builder); - - if (symbol.IsIndexer) - { - // Add the ".this[...]" suffix (parameters will be added below) - builder.AddRange(".this[".AsSpan()); - - bool isFirstParameter = true; - - // If the property is an indexer, also includes the parameters - foreach (IParameterSymbol parameter in symbol.Parameters) - { - // Append the leading ', ' if needed - if (isFirstParameter) - { - isFirstParameter = false; - } - else - { - builder.AddRange(", ".AsSpan()); - } - - AppendFullyQualifiedMetadataName(parameter.Type, in builder); - } - - // Add the closing bracket for ".this[...]" - builder.Add(']'); - } - else - { - // For normal properties, just append the name after the containing type - builder.Add('.'); - builder.AddRange(symbol.Name.AsSpan()); - } - - return builder.ToString(); - } - - /// - /// Gets the fully qualified metadata name for a given instance. - /// - /// The input instance. - /// The fully qualified metadata name for . - public static string GetFullyQualifiedMetadataName(this IFieldSymbol symbol) - { - using ImmutableArrayBuilder builder = ImmutableArrayBuilder.Rent(); - - AppendFullyQualifiedMetadataName(symbol.ContainingType!, in builder); - - builder.Add('.'); - builder.AddRange(symbol.Name.AsSpan()); - - return builder.ToString(); - } - /// /// Tries to get an attribute with the specified fully qualified metadata name. /// @@ -242,53 +103,4 @@ internal static class ISymbolExtensions accessibility == Accessibility.Public || (accessibility == Accessibility.Internal && symbol.ContainingAssembly.GivesAccessTo(assembly)); } - - /// - /// Appends the fully qualified metadata name for a given symbol to a target builder. - /// - /// The input instance. - /// The target instance. - private static void AppendFullyQualifiedMetadataName(this ITypeSymbol symbol, in ImmutableArrayBuilder builder) - { - static void BuildFrom(ISymbol? symbol, in ImmutableArrayBuilder builder) - { - switch (symbol) - { - // Namespaces that are nested also append a leading '.' - case INamespaceSymbol { ContainingNamespace.IsGlobalNamespace: false }: - BuildFrom(symbol.ContainingNamespace, in builder); - builder.Add('.'); - builder.AddRange(symbol.MetadataName.AsSpan()); - break; - - // Other namespaces (ie. the one right before global) skip the leading '.' - case INamespaceSymbol { IsGlobalNamespace: false }: - builder.AddRange(symbol.MetadataName.AsSpan()); - break; - - // Types with no namespace just have their metadata name directly written - case ITypeSymbol { ContainingSymbol: INamespaceSymbol { IsGlobalNamespace: true } }: - builder.AddRange(symbol.MetadataName.AsSpan()); - break; - - // Types with a containing non-global namespace also append a leading '.' - case ITypeSymbol { ContainingSymbol: INamespaceSymbol namespaceSymbol }: - BuildFrom(namespaceSymbol, in builder); - builder.Add('.'); - builder.AddRange(symbol.MetadataName.AsSpan()); - break; - - // Nested types append a leading '+' - case ITypeSymbol { ContainingSymbol: ITypeSymbol typeSymbol }: - BuildFrom(typeSymbol, in builder); - builder.Add('+'); - builder.AddRange(symbol.MetadataName.AsSpan()); - break; - default: - break; - } - } - - BuildFrom(symbol, in builder); - } } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration/Extensions/ITypeSymbolExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/ITypeSymbolExtensions.cs new file mode 100644 index 00000000..a9fd5b26 --- /dev/null +++ b/src/ComputeSharp.SourceGeneration/Extensions/ITypeSymbolExtensions.cs @@ -0,0 +1,89 @@ +using System; +using ComputeSharp.SourceGeneration.Helpers; +using Microsoft.CodeAnalysis; + +namespace ComputeSharp.SourceGeneration.Extensions; + +/// +/// Extension methods for types. +/// +internal static class ITypeSymbolExtensions +{ + /// + /// Checks whether or not a given type symbol has a specified fully qualified metadata name. + /// + /// The input instance to check. + /// The full name to check. + /// Whether has a full name equals to . + public static bool HasFullyQualifiedMetadataName(this ITypeSymbol symbol, string name) + { + using ImmutableArrayBuilder builder = ImmutableArrayBuilder.Rent(); + + symbol.AppendFullyQualifiedMetadataName(in builder); + + return builder.WrittenSpan.SequenceEqual(name.AsSpan()); + } + + /// + /// Gets the fully qualified metadata name for a given instance. + /// + /// The input instance. + /// The fully qualified metadata name for . + public static string GetFullyQualifiedMetadataName(this ITypeSymbol symbol) + { + using ImmutableArrayBuilder builder = ImmutableArrayBuilder.Rent(); + + symbol.AppendFullyQualifiedMetadataName(in builder); + + return builder.ToString(); + } + + /// + /// Appends the fully qualified metadata name for a given symbol to a target builder. + /// + /// The input instance. + /// The target instance. + public static void AppendFullyQualifiedMetadataName(this ITypeSymbol symbol, in ImmutableArrayBuilder builder) + { + static void BuildFrom(ISymbol? symbol, in ImmutableArrayBuilder builder) + { + switch (symbol) + { + // Namespaces that are nested also append a leading '.' + case INamespaceSymbol { ContainingNamespace.IsGlobalNamespace: false }: + BuildFrom(symbol.ContainingNamespace, in builder); + builder.Add('.'); + builder.AddRange(symbol.MetadataName.AsSpan()); + break; + + // Other namespaces (ie. the one right before global) skip the leading '.' + case INamespaceSymbol { IsGlobalNamespace: false }: + builder.AddRange(symbol.MetadataName.AsSpan()); + break; + + // Types with no namespace just have their metadata name directly written + case ITypeSymbol { ContainingSymbol: INamespaceSymbol { IsGlobalNamespace: true } }: + builder.AddRange(symbol.MetadataName.AsSpan()); + break; + + // Types with a containing non-global namespace also append a leading '.' + case ITypeSymbol { ContainingSymbol: INamespaceSymbol namespaceSymbol }: + BuildFrom(namespaceSymbol, in builder); + builder.Add('.'); + builder.AddRange(symbol.MetadataName.AsSpan()); + break; + + // Nested types append a leading '+' + case ITypeSymbol { ContainingSymbol: ITypeSymbol typeSymbol }: + BuildFrom(typeSymbol, in builder); + builder.Add('+'); + builder.AddRange(symbol.MetadataName.AsSpan()); + break; + default: + break; + } + } + + BuildFrom(symbol, in builder); + } +} \ No newline at end of file