[11700] | 1 | // Copyright (c) 2013 AlphaSierraPapa for the SharpDevelop Team |
---|
| 2 | // |
---|
| 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this |
---|
| 4 | // software and associated documentation files (the "Software"), to deal in the Software |
---|
| 5 | // without restriction, including without limitation the rights to use, copy, modify, merge, |
---|
| 6 | // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
---|
| 7 | // to whom the Software is furnished to do so, subject to the following conditions: |
---|
| 8 | // |
---|
| 9 | // The above copyright notice and this permission notice shall be included in all copies or |
---|
| 10 | // substantial portions of the Software. |
---|
| 11 | // |
---|
| 12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
---|
| 13 | // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
---|
| 14 | // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
---|
| 15 | // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
---|
| 16 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
| 17 | // DEALINGS IN THE SOFTWARE. |
---|
| 18 | |
---|
| 19 | using System; |
---|
| 20 | using System.Collections.Generic; |
---|
| 21 | using ICSharpCode.NRefactory.TypeSystem; |
---|
| 22 | |
---|
| 23 | namespace ICSharpCode.NRefactory.Analysis |
---|
| 24 | { |
---|
| 25 | /// <summary> |
---|
| 26 | /// A graph where type definitions are nodes; and edges are given by inheritance. |
---|
| 27 | /// </summary> |
---|
| 28 | public class TypeGraph |
---|
| 29 | { |
---|
| 30 | Dictionary<AssemblyQualifiedTypeName, TypeGraphNode> dict; |
---|
| 31 | |
---|
| 32 | /// <summary> |
---|
| 33 | /// Builds a graph of all type definitions in the specified set of assemblies. |
---|
| 34 | /// </summary> |
---|
| 35 | /// <param name="assemblies">The input assemblies. The assemblies may belong to multiple compilations.</param> |
---|
| 36 | /// <remarks>The resulting graph may be cyclic if there are cyclic type definitions.</remarks> |
---|
| 37 | public TypeGraph(IEnumerable<IAssembly> assemblies) |
---|
| 38 | { |
---|
| 39 | if (assemblies == null) |
---|
| 40 | throw new ArgumentNullException("assemblies"); |
---|
| 41 | dict = new Dictionary<AssemblyQualifiedTypeName, TypeGraphNode>(); |
---|
| 42 | foreach (IAssembly assembly in assemblies) { |
---|
| 43 | foreach (ITypeDefinition typeDef in assembly.GetAllTypeDefinitions()) { |
---|
| 44 | // Overwrite previous entry - duplicates can occur if there are multiple versions of the |
---|
| 45 | // same project loaded in the solution (e.g. separate .csprojs for separate target frameworks) |
---|
| 46 | dict[new AssemblyQualifiedTypeName(typeDef)] = new TypeGraphNode(typeDef); |
---|
| 47 | } |
---|
| 48 | } |
---|
| 49 | foreach (IAssembly assembly in assemblies) { |
---|
| 50 | foreach (ITypeDefinition typeDef in assembly.GetAllTypeDefinitions()) { |
---|
| 51 | TypeGraphNode typeNode = dict[new AssemblyQualifiedTypeName(typeDef)]; |
---|
| 52 | foreach (IType baseType in typeDef.DirectBaseTypes) { |
---|
| 53 | ITypeDefinition baseTypeDef = baseType.GetDefinition(); |
---|
| 54 | if (baseTypeDef != null) { |
---|
| 55 | TypeGraphNode baseTypeNode; |
---|
| 56 | if (dict.TryGetValue(new AssemblyQualifiedTypeName(baseTypeDef), out baseTypeNode)) { |
---|
| 57 | typeNode.BaseTypes.Add(baseTypeNode); |
---|
| 58 | baseTypeNode.DerivedTypes.Add(typeNode); |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | } |
---|
| 62 | } |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | public TypeGraphNode GetNode(ITypeDefinition typeDefinition) |
---|
| 67 | { |
---|
| 68 | if (typeDefinition == null) |
---|
| 69 | return null; |
---|
| 70 | return GetNode(new AssemblyQualifiedTypeName(typeDefinition)); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | public TypeGraphNode GetNode(AssemblyQualifiedTypeName typeName) |
---|
| 74 | { |
---|
| 75 | TypeGraphNode node; |
---|
| 76 | if (dict.TryGetValue(typeName, out node)) |
---|
| 77 | return node; |
---|
| 78 | else |
---|
| 79 | return null; |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | } |
---|