using System.Diagnostics.CodeAnalysis; using System.Threading; using Microsoft.CodeAnalysis; namespace ComputeSharp.SourceGeneration.Extensions; /// /// Extension methods for types. /// internal static class ISymbolExtensions { /// /// A custom instance with fully qualified style, without global::. /// public static readonly SymbolDisplayFormat FullyQualifiedWithoutGlobalFormat = new( globalNamespaceStyle: SymbolDisplayGlobalNamespaceStyle.Omitted, typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces, genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters, miscellaneousOptions: SymbolDisplayMiscellaneousOptions.EscapeKeywordIdentifiers); /// /// Checks whether a given symbol is accessible from its containing assembly (including eg. through nested types). /// /// The input instance. /// The instance currently in use. /// Whether is accessible from its containing assembly. public static bool IsAccessibleFromContainingAssembly(this ISymbol symbol, Compilation compilation) { // If the symbol is associated across multiple assemblies, it must be accessible if (symbol.ContainingAssembly is not IAssemblySymbol assemblySymbol) { return true; } return compilation.IsSymbolAccessibleWithin(symbol, assemblySymbol); } /// /// Checks whether a given symbol is accessible from the assembly of a given compilation (including eg. through nested types). /// /// The input instance. /// The instance currently in use. /// Whether is accessible from the assembly for . public static bool IsAccessibleFromCompilationAssembly(this ISymbol symbol, Compilation compilation) { return compilation.IsSymbolAccessibleWithin(symbol, compilation.Assembly); } /// /// Gets the fully qualified name for a given symbol. /// /// The input instance. /// Whether to include the global:: prefix. /// The fully qualified name for . public static string GetFullyQualifiedName(this ISymbol symbol, bool includeGlobal = false) { return includeGlobal ? symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat) : symbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat); } /// /// Checks whether or not a given symbol has an attribute with the specified type. /// /// The input instance to check. /// The instance for the attribute type to look for. /// Whether or not has an attribute with the specified type. public static bool HasAttributeWithType(this ISymbol symbol, ITypeSymbol typeSymbol) { return TryGetAttributeWithType(symbol, typeSymbol, out _); } /// /// Tries to get an attribute with the specified type. /// /// The input instance to check. /// The instance for the attribute type to look for. /// The resulting attribute, if it was found. /// Whether or not has an attribute with the specified name. public static bool TryGetAttributeWithType(this ISymbol symbol, ITypeSymbol typeSymbol, [NotNullWhen(true)] out AttributeData? attributeData) { foreach (AttributeData attribute in symbol.GetAttributes()) { if (SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, typeSymbol)) { attributeData = attribute; return true; } } attributeData = null; return false; } /// /// Tries to get an attribute with the specified fully qualified metadata name. /// /// The input instance to check. /// The attribute name to look for. /// The resulting attribute data, if found. /// Whether or not has an attribute with the specified name. public static bool TryGetAttributeWithFullyQualifiedMetadataName(this ISymbol symbol, string name, [NotNullWhen(true)] out AttributeData? attributeData) { foreach (AttributeData attribute in symbol.GetAttributes()) { if (attribute.AttributeClass is INamedTypeSymbol attributeSymbol && attributeSymbol.HasFullyQualifiedMetadataName(name)) { attributeData = attribute; return true; } } attributeData = null; return false; } /// /// Tries to get a syntax node with a given type from an input symbol. /// /// The type of syntax node to look for. /// The input instance to get the syntax node for. /// The used to cancel the operation, if needed. /// The resulting syntax node, if found. /// Whether or not a syntax node of type was retrieved successfully. public static bool TryGetSyntaxNode(this ISymbol symbol, CancellationToken token, [NotNullWhen(true)] out T? syntaxNode) where T : SyntaxNode { // If there are no syntax references, there is nothing to do if (symbol.DeclaringSyntaxReferences is not [SyntaxReference syntaxReference, ..]) { syntaxNode = null; return false; } // Get the target node, and check that it's of the desired type T? candidateNode = syntaxReference.GetSyntax(token) as T; syntaxNode = candidateNode; return candidateNode is not null; } }