-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Implement a few utility methods.
1 parent
63b73e7
commit 71a85d7
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |