using System.Collections.Generic;
using Microsoft.CodeAnalysis;
namespace ComputeSharp.SourceGeneration.Helpers;
///
/// A type providing instances for nodes.
///
internal sealed class SemanticModelProvider
{
///
/// The source instance.
///
private readonly Compilation compilation;
///
/// The map of loaded instances.
///
private readonly Dictionary semanticModelsMap;
///
/// Creates a new instance with the specified parameters.
///
/// The source instance.
public SemanticModelProvider(Compilation compilation)
{
this.compilation = compilation;
this.semanticModelsMap = new Dictionary();
}
///
/// Gets a instance with info on a given .
///
/// The input to get info for.
/// A instance containing info on .
public SemanticModel For(SyntaxNode syntaxNode)
{
if (!this.semanticModelsMap.TryGetValue(syntaxNode.SyntaxTree, out SemanticModel semanticModel))
{
semanticModel = this.compilation.GetSemanticModel(syntaxNode.SyntaxTree);
this.semanticModelsMap.Add(syntaxNode.SyntaxTree, semanticModel);
}
return semanticModel;
}
}