|
1 | 1 | using Mono.Cecil; |
2 | 2 | using OTAPI.UnifiedServerProcess.Extensions; |
| 3 | +using System; |
| 4 | +using System.Collections; |
3 | 5 | using System.Collections.Generic; |
4 | 6 |
|
5 | 7 | namespace OTAPI.UnifiedServerProcess.Core.Analysis |
6 | 8 | { |
7 | | - public class TypeInheritanceGraph |
| 9 | + public sealed class TypeInheritanceGraph |
8 | 10 | { |
9 | 11 | public TypeInheritanceGraph(ModuleDefinition module) { |
10 | | - TypeInheritanceChains = []; |
11 | | - foreach (var type in GetTypesInInheritanceOrder(module)) { |
12 | | - GetInheritancesTypes(type); |
13 | | - } |
| 12 | + _typeInheritanceChains = new(StringComparer.Ordinal); |
| 13 | + _directDerivedTypes = new(StringComparer.Ordinal); |
| 14 | + _derivedTypeTrees = new(StringComparer.Ordinal); |
| 15 | + |
| 16 | + // Precompute upward inheritance chains (base types + interfaces) for fast lookups. |
| 17 | + foreach (var type in GetTypesInInheritanceOrder(module)) |
| 18 | + GetInheritanceTypes(type); |
| 19 | + |
| 20 | + // Build a "base -> direct derived types" index (classes only) for descendant tree queries. |
| 21 | + foreach (var type in module.GetTypes()) |
| 22 | + IndexDerivedType(type); |
| 23 | + |
| 24 | + // Stabilize output order. |
| 25 | + foreach (var list in _directDerivedTypes.Values) |
| 26 | + list.Sort((a, b) => StringComparer.Ordinal.Compare(a.FullName, b.FullName)); |
14 | 27 | } |
15 | | - public Dictionary<string, TypeDefinition> GetInheritancesTypes(TypeDefinition type) { |
16 | | - if (TypeInheritanceChains.TryGetValue(type.FullName, out var result)) { |
17 | | - return result; |
18 | | - } |
19 | | - var types = new Dictionary<string, TypeDefinition>(); |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Gets all upward reachable types for <paramref name="type"/> (itself + base types + implemented interfaces). |
| 31 | + /// Cached per type full name. |
| 32 | + /// </summary> |
| 33 | + public Dictionary<string, TypeDefinition> GetInheritanceTypes(TypeDefinition type) { |
| 34 | + |
| 35 | + if (_typeInheritanceChains.TryGetValue(type.FullName, out var cached)) |
| 36 | + return cached; |
| 37 | + |
| 38 | + var types = new Dictionary<string, TypeDefinition>(StringComparer.Ordinal); |
20 | 39 | var currentType = type; |
| 40 | + |
21 | 41 | while (currentType != null) { |
22 | 42 | types.TryAdd(currentType.FullName, currentType); |
23 | 43 |
|
24 | | - foreach (var interfaceType in currentType.Interfaces) { |
| 44 | + foreach (var iface in currentType.Interfaces) { |
| 45 | + var resolved = iface.InterfaceType.TryResolve(); |
| 46 | + if (resolved is null) continue; |
| 47 | + types.TryAdd(resolved.FullName, resolved); |
| 48 | + } |
25 | 49 |
|
26 | | - var resolvedInterfaceType = interfaceType.InterfaceType.TryResolve(); |
| 50 | + currentType = currentType.BaseType?.TryResolve(); |
| 51 | + } |
27 | 52 |
|
28 | | - if (resolvedInterfaceType is null) continue; |
| 53 | + _typeInheritanceChains.Add(type.FullName, types); |
| 54 | + return types; |
| 55 | + } |
29 | 56 |
|
30 | | - types.TryAdd(resolvedInterfaceType.FullName, resolvedInterfaceType); |
31 | | - } |
32 | | - currentType = currentType.BaseType?.TryResolve(); |
| 57 | + /// <summary> |
| 58 | + /// Builds a derived-type tree rooted at <paramref name="root"/> (root included). |
| 59 | + /// Only supports class roots; interface roots are rejected to guarantee a tree. |
| 60 | + /// </summary> |
| 61 | + public TypeTreeNode GetDerivedTypeTree(TypeDefinition root) { |
| 62 | + if (root is null) throw new ArgumentNullException(nameof(root)); |
| 63 | + if (root.IsInterface) |
| 64 | + throw new NotSupportedException("Interface roots are not supported because implementations form a DAG, not a tree."); |
| 65 | + |
| 66 | + return BuildDerivedTypeTree(root); |
| 67 | + } |
| 68 | + |
| 69 | + private void IndexDerivedType(TypeDefinition type) { |
| 70 | + // Exclude interfaces so System.Object won't incorrectly "own" all interfaces as children. |
| 71 | + if (type is null || type.IsInterface) return; |
| 72 | + |
| 73 | + var baseType = type.BaseType?.TryResolve(); |
| 74 | + if (baseType is null) return; |
| 75 | + |
| 76 | + if (!_directDerivedTypes.TryGetValue(baseType.FullName, out var list)) { |
| 77 | + list = new List<TypeDefinition>(); |
| 78 | + _directDerivedTypes.Add(baseType.FullName, list); |
| 79 | + } |
| 80 | + |
| 81 | + list.Add(type); |
| 82 | + } |
| 83 | + |
| 84 | + private TypeTreeNode BuildDerivedTypeTree(TypeDefinition root) { |
| 85 | + if (_derivedTypeTrees.TryGetValue(root.FullName, out var cached)) |
| 86 | + return cached; |
| 87 | + |
| 88 | + var children = new List<TypeTreeNode>(); |
| 89 | + |
| 90 | + if (_directDerivedTypes.TryGetValue(root.FullName, out var directChildren)) { |
| 91 | + foreach (var child in directChildren) |
| 92 | + children.Add(BuildDerivedTypeTree(child)); |
33 | 93 | } |
34 | | - result = types; |
35 | | - TypeInheritanceChains.Add(type.FullName, result); |
36 | | - return result; |
| 94 | + |
| 95 | + var node = new TypeTreeNode(root, children); |
| 96 | + _derivedTypeTrees.Add(root.FullName, node); |
| 97 | + return node; |
37 | 98 | } |
38 | 99 |
|
39 | | - readonly Dictionary<string, Dictionary<string, TypeDefinition>> TypeInheritanceChains; |
| 100 | + private readonly Dictionary<string, Dictionary<string, TypeDefinition>> _typeInheritanceChains; |
| 101 | + private readonly Dictionary<string, List<TypeDefinition>> _directDerivedTypes; |
| 102 | + private readonly Dictionary<string, TypeTreeNode> _derivedTypeTrees; |
| 103 | + |
40 | 104 | private static List<TypeDefinition> GetTypesInInheritanceOrder(ModuleDefinition module) { |
41 | | - var allTypes = new HashSet<TypeDefinition>(); |
| 105 | + var visited = new HashSet<TypeDefinition>(); |
42 | 106 | var sorted = new List<TypeDefinition>(); |
43 | 107 |
|
44 | | - foreach (var type in module.GetTypes()) { |
45 | | - VisitType(type, allTypes, sorted); |
46 | | - } |
| 108 | + foreach (var type in module.GetTypes()) |
| 109 | + VisitType(type, visited, sorted); |
47 | 110 |
|
48 | 111 | return sorted; |
49 | 112 | } |
50 | 113 |
|
51 | | - private static void VisitType( |
52 | | - TypeDefinition type, |
53 | | - HashSet<TypeDefinition> visited, |
54 | | - List<TypeDefinition> sorted) { |
| 114 | + private static void VisitType(TypeDefinition type, HashSet<TypeDefinition> visited, List<TypeDefinition> sorted) { |
55 | 115 | if (type is null || visited.Contains(type)) return; |
56 | 116 |
|
57 | 117 | var baseType = type.BaseType?.TryResolve(); |
58 | | - if (baseType != null && !visited.Contains(baseType)) { |
| 118 | + if (baseType != null && !visited.Contains(baseType)) |
59 | 119 | VisitType(baseType, visited, sorted); |
60 | | - } |
61 | 120 |
|
62 | 121 | foreach (var iface in type.Interfaces) { |
63 | | - var interfaceType = iface.InterfaceType.TryResolve(); |
64 | | - if (interfaceType is null) { |
65 | | - continue; |
66 | | - } |
67 | | - VisitType(interfaceType, visited, sorted); |
| 122 | + var ifaceType = iface.InterfaceType.TryResolve(); |
| 123 | + if (ifaceType is null) continue; |
| 124 | + VisitType(ifaceType, visited, sorted); |
68 | 125 | } |
69 | 126 |
|
70 | | - if (visited.Add(type)) { |
| 127 | + if (visited.Add(type)) |
71 | 128 | sorted.Add(type); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + public enum TreeTraversal |
| 133 | + { |
| 134 | + PreOrder, |
| 135 | + PostOrder, |
| 136 | + BreadthFirst |
| 137 | + } |
| 138 | + |
| 139 | + /// <summary> |
| 140 | + /// Immutable derived-type tree node. |
| 141 | + /// Traversal yields TypeDefinitions in the requested order. |
| 142 | + /// </summary> |
| 143 | + public sealed class TypeTreeNode : IEnumerable<TypeDefinition> |
| 144 | + { |
| 145 | + public TypeTreeNode(TypeDefinition type, IReadOnlyList<TypeTreeNode> children) { |
| 146 | + Type = type ?? throw new ArgumentNullException(nameof(type)); |
| 147 | + Children = children ?? Array.Empty<TypeTreeNode>(); |
| 148 | + } |
| 149 | + |
| 150 | + public TypeDefinition Type { get; } |
| 151 | + public IReadOnlyList<TypeTreeNode> Children { get; } |
| 152 | + |
| 153 | + /// <summary> |
| 154 | + /// Traverses the tree and yields TypeDefinitions in the chosen order. |
| 155 | + /// </summary> |
| 156 | + public IEnumerable<TypeDefinition> Traverse(TreeTraversal order = TreeTraversal.PreOrder) => order switch { |
| 157 | + TreeTraversal.PreOrder => PreOrder(this), |
| 158 | + TreeTraversal.PostOrder => PostOrder(this), |
| 159 | + TreeTraversal.BreadthFirst => BreadthFirst(this), |
| 160 | + _ => throw new ArgumentOutOfRangeException(nameof(order)) |
| 161 | + }; |
| 162 | + |
| 163 | + // Default enumeration is pre-order. |
| 164 | + public IEnumerator<TypeDefinition> GetEnumerator() => Traverse().GetEnumerator(); |
| 165 | + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| 166 | + |
| 167 | + private static IEnumerable<TypeDefinition> PreOrder(TypeTreeNode node) { |
| 168 | + yield return node.Type; |
| 169 | + foreach (var child in node.Children) |
| 170 | + foreach (var t in PreOrder(child)) |
| 171 | + yield return t; |
| 172 | + } |
| 173 | + |
| 174 | + private static IEnumerable<TypeDefinition> PostOrder(TypeTreeNode node) { |
| 175 | + foreach (var child in node.Children) |
| 176 | + foreach (var t in PostOrder(child)) |
| 177 | + yield return t; |
| 178 | + yield return node.Type; |
| 179 | + } |
| 180 | + |
| 181 | + private static IEnumerable<TypeDefinition> BreadthFirst(TypeTreeNode node) { |
| 182 | + var q = new Queue<TypeTreeNode>(); |
| 183 | + q.Enqueue(node); |
| 184 | + |
| 185 | + while (q.Count > 0) { |
| 186 | + var cur = q.Dequeue(); |
| 187 | + yield return cur.Type; |
| 188 | + |
| 189 | + foreach (var child in cur.Children) |
| 190 | + q.Enqueue(child); |
72 | 191 | } |
73 | 192 | } |
74 | 193 | } |
|
0 commit comments