Skip to content

Commit

Permalink
Implement a few utility methods.
Browse files Browse the repository at this point in the history
GGG-KILLER committed Jan 28, 2024
1 parent 63b73e7 commit 71a85d7
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions Tsu.Trees.RedGreen/sourcegen/Utils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Tsu.Trees.RedGreen.SourceGenerator.Model;

namespace Tsu.Trees.RedGreen.SourceGenerator;

internal static class Utils
{
public static bool DerivesFrom(this INamedTypeSymbol symbol, INamedTypeSymbol parent)
{
if (symbol.BaseType is null)
return false;

for (var type = symbol.BaseType; type is not null; type = type.BaseType)
{
if (SymbolEqualityComparer.Default.Equals(type, parent))
return true;
}

return false;
}

public static List<NodeInfo> ListAllNodes(TreeInfo root, IEnumerable<NodeInfo> knownNodes)
{
var visited = new HashSet<INamedTypeSymbol>(SymbolEqualityComparer.Default);
var nodes = new List<NodeInfo>(knownNodes.Where(x => x.NodeType.DerivesFrom(root.GreenBase)));

var stack = new Stack<INamedTypeSymbol>();
foreach (var node in nodes)
{
if (node.BaseType is not null)
stack.Push(node.BaseType);
visited.Add(node.NodeType);
}

while (stack.Count > 0)
{
var type = stack.Pop();
if (visited.Contains(type))
continue;

if (type.BaseType is not null)
stack.Push(type.BaseType);
visited.Add(type);
nodes.Add(new NodeInfo(
type.BaseType,
type,
ImmutableArray<TypedConstant>.Empty
));
}

return nodes;
}

}

0 comments on commit 71a85d7

Please sign in to comment.