782280535ddabeb45e580c696de2856c46866d13
[mono.git] / mcs / tools / corcompare / Util.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using Mono.Cecil;
5
6 using GuiCompare;
7
8 namespace CorCompare {
9
10         static class TypeHelper {
11
12                 public static AssemblyResolver Resolver = new AssemblyResolver ();
13
14                 internal static bool IsPublic (TypeReference typeref)
15                 {
16                         if (typeref == null)
17                                 throw new ArgumentNullException ("typeref");
18
19                         TypeDefinition td = typeref.Resolve ();
20                         if (td == null)
21                                 return false;
22
23                         return td.IsPublic;
24                 }
25
26                 internal static bool IsDelegate (TypeReference typeref)
27                 {
28                         return IsDerivedFrom (typeref, "System.MulticastDelegate");
29                 }
30
31                 internal static bool IsDerivedFrom (TypeReference type, string derivedFrom)
32                 {
33                         bool first = true;
34                         foreach (var def in WalkHierarchy (type)) {
35                                 if (first) {
36                                         first = false;
37                                         continue;
38                                 }
39                                 
40                                 if (def.FullName == derivedFrom)
41                                         return true;
42                         }
43                         
44                         return false;
45                 }
46
47                 internal static IEnumerable<TypeDefinition> WalkHierarchy (TypeReference type)
48                 {
49                         for (var def = type.Resolve (); def != null; def = GetBaseType (def))
50                                 yield return def;
51                 }
52
53                 internal static IEnumerable<TypeReference> GetInterfaces (TypeReference type)
54                 {
55                         var ifaces = new Dictionary<string, TypeReference> ();
56
57                         foreach (var def in WalkHierarchy (type))
58                                 foreach (TypeReference iface in def.Interfaces)
59                                         ifaces [iface.FullName] = iface;
60
61                         return ifaces.Values;
62                 }
63
64                 internal static TypeDefinition GetBaseType (TypeDefinition child)
65                 {
66                         if (child.BaseType == null)
67                                 return null;
68
69                         return child.BaseType.Resolve ();
70                 }
71
72                 internal static bool IsPublic (CustomAttribute att)
73                 {
74                         return IsPublic (att.AttributeType);
75                 }
76
77                 internal static string GetFullName (CustomAttribute att)
78                 {
79                         return att.AttributeType.FullName;
80                 }
81
82                 internal static TypeDefinition GetTypeDefinition (CustomAttribute att)
83                 {
84                         return att.AttributeType.Resolve ();
85                 }
86         }
87 }