diff --git a/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems b/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems index 948079f9..eb8eff64 100644 --- a/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems +++ b/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems @@ -10,6 +10,7 @@ + diff --git a/src/ComputeSharp.SourceGeneration/Extensions/CompilationExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/CompilationExtensions.cs new file mode 100644 index 00000000..53ac9c6a --- /dev/null +++ b/src/ComputeSharp.SourceGeneration/Extensions/CompilationExtensions.cs @@ -0,0 +1,78 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.CodeAnalysis; + +namespace ComputeSharp.SourceGeneration.Extensions; + +/// +/// Extension methods for the type. +/// +internal static class CompilationExtensions +{ + /// + /// + /// Checks whether or not a type with a specified metadata name is accessible from a given instance. + /// + /// + /// This method enumerates candidate type symbols to find a match in the following order: + /// + /// + /// If only one type with the given name is found within the compilation and its referenced assemblies, check its accessibility. + /// + /// + /// If the current defines the symbol, check its accessibility. + /// + /// + /// Otherwise, check whether the type exists and is accessible from any of the referenced assemblies. + /// + /// + /// + /// + /// The to consider for analysis. + /// The fully-qualified metadata type name to find. + /// Whether a type with the specified metadata name can be accessed from the given compilation. + public static bool HasAccessibleTypeWithMetadataName(this Compilation compilation, string fullyQualifiedMetadataName) + { + // Try to get the unique type with this name + INamedTypeSymbol? type = compilation.GetTypeByMetadataName(fullyQualifiedMetadataName); + + // If there is only a single matching symbol, check its accessibility + if (type is not null) + { + return type.CanBeAccessedFrom(compilation.Assembly); + } + + // Otherwise, try to get the unique type with this name originally defined in 'compilation' + type ??= compilation.Assembly.GetTypeByMetadataName(fullyQualifiedMetadataName); + + if (type is not null) + { + return type.CanBeAccessedFrom(compilation.Assembly); + } + + // Otherwise, check whether the type is defined and accessible from any of the referenced assemblies + foreach (IModuleSymbol module in compilation.Assembly.Modules) + { + foreach (IAssemblySymbol referencedAssembly in module.ReferencedAssemblySymbols) + { + if (referencedAssembly.GetTypeByMetadataName(fullyQualifiedMetadataName) is not INamedTypeSymbol currentType) + { + continue; + } + + switch (currentType.GetEffectiveAccessibility()) + { + case Accessibility.Public: + case Accessibility.Internal when referencedAssembly.GivesAccessTo(compilation.Assembly): + return true; + default: + continue; + } + } + } + + return false; + } +} diff --git a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs index 906998f8..914adbd8 100644 --- a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs +++ b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs @@ -180,4 +180,57 @@ internal static class ISymbolExtensions return false; } + + /// + /// Calculates the effective accessibility for a given symbol. + /// + /// The instance to check. + /// The effective accessibility for . + public static Accessibility GetEffectiveAccessibility(this ISymbol symbol) + { + // Start by assuming it's visible + Accessibility visibility = Accessibility.Public; + + // Handle special cases + switch (symbol.Kind) + { + case SymbolKind.Alias: return Accessibility.Private; + case SymbolKind.Parameter: return GetEffectiveAccessibility(symbol.ContainingSymbol); + case SymbolKind.TypeParameter: return Accessibility.Private; + } + + // Traverse the symbol hierarchy to determine the effective accessibility + while (symbol is not null && symbol.Kind != SymbolKind.Namespace) + { + switch (symbol.DeclaredAccessibility) + { + case Accessibility.NotApplicable: + case Accessibility.Private: + return Accessibility.Private; + case Accessibility.Internal: + case Accessibility.ProtectedAndInternal: + visibility = Accessibility.Internal; + break; + } + + symbol = symbol.ContainingSymbol; + } + + return visibility; + } + + /// + /// Checks whether or not a given symbol can be accessed from a specified assembly. + /// + /// The input instance to check. + /// The assembly to check the accessibility of for. + /// Whether can access . + public static bool CanBeAccessedFrom(this ISymbol symbol, IAssemblySymbol assembly) + { + Accessibility accessibility = symbol.GetEffectiveAccessibility(); + + return + accessibility == Accessibility.Public || + accessibility == Accessibility.Internal && symbol.ContainingAssembly.GivesAccessTo(assembly); + } }