// 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 System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics.CodeAnalysis; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; namespace ComputeSharp.SourceGeneration.Extensions; /// /// Extension methods for the type. /// internal static class CompilationExtensions { /// /// Tries to build a set of instances form the input mapping of names. /// /// The to consider for analysis. /// The input fully qualified type names. /// The resulting mapping of resolved instances. /// Whether all requested instances could be resolved. public static bool TryBuildNamedTypeSymbolSet( this Compilation compilation, IEnumerable fullyQualifiedTypeNames, [NotNullWhen(true)] out ImmutableHashSet? typeSymbols) { ImmutableHashSet.Builder builder = ImmutableHashSet.CreateBuilder(SymbolEqualityComparer.Default); foreach (string fullyQualifiedTypeName in fullyQualifiedTypeNames) { if (compilation.GetTypeByMetadataName(fullyQualifiedTypeName) is not INamedTypeSymbol typeSymbol) { typeSymbols = null; return false; } _ = builder.Add(typeSymbol); } typeSymbols = builder.ToImmutable(); return true; } /// /// Checks whether a given compilation (assumed to be for C#) is using the preview language version. /// /// The to consider for analysis. /// Whether is using the preview language version. public static bool IsLanguageVersionPreview(this Compilation compilation) { return ((CSharpCompilation)compilation).LanguageVersion == LanguageVersion.Preview; } /// /// Checks whether the AllowUnsafeBlocks option is set for a given compilation. /// /// The object to check. /// Whether the AllowUnsafeBlocks option is set for . public static bool IsAllowUnsafeBlocksEnabled(this Compilation compilation) { return compilation.Options is CSharpCompilationOptions { AllowUnsafe: true }; } }