2008-12-08 Atsushi Enomoto <atsushi@ximian.com>
[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 namespace Mono.Util.CorCompare.Cecil {
7
8         static class TypeHelper {
9
10                 public static AssemblyResolver Resolver = new AssemblyResolver ();
11
12                 internal static bool IsPublic (TypeReference typeref)
13                 {
14                         if (typeref == null)
15                                 throw new ArgumentException ("typeref must not be null");
16
17                         TypeDefinition td = Resolve (typeref);
18                         return td.IsPublic;
19                 }
20
21                 internal static bool IsDelegate (TypeReference typeref)
22                 {
23                         return IsDerivedFrom (typeref, "System.MulticastDelegate");
24                 }
25
26                 static TypeDefinition Resolve (TypeReference reference)
27                 {
28                         return Resolver.Resolve (reference);
29                 }
30
31                 internal static bool IsDerivedFrom (TypeReference type, string derivedFrom)
32                 {
33                         foreach (var def in WalkHierarchy (type))
34                                 if (def.FullName == derivedFrom)
35                                         return true;
36
37                         return false;
38                 }
39
40                 internal static IEnumerable<TypeDefinition> WalkHierarchy (TypeReference type)
41                 {
42                         for (var def = Resolve (type); def != null; def = GetBaseType (def))
43                                 yield return def;
44                 }
45
46                 internal static IEnumerable<TypeReference> GetInterfaces (TypeReference type)
47                 {
48                         var ifaces = new Dictionary<string, TypeReference> ();
49
50                         foreach (var def in WalkHierarchy (type))
51                                 foreach (TypeReference iface in def.Interfaces)
52                                         ifaces [iface.FullName] = iface;
53
54                         return ifaces.Values;
55                 }
56
57                 internal static TypeDefinition GetBaseType (TypeDefinition child)
58                 {
59                         if (child.BaseType == null)
60                                 return null;
61
62                         return Resolve (child.BaseType);
63                 }
64
65                 internal static bool IsPublic (CustomAttribute att)
66                 {
67                         return IsPublic (att.Constructor.DeclaringType);
68                 }
69
70                 internal static string GetFullName (CustomAttribute att)
71                 {
72                         return att.Constructor.DeclaringType.FullName;
73                 }
74
75                 internal static TypeDefinition GetTypeDefinition (CustomAttribute att)
76                 {
77                         return Resolve (att.Constructor.DeclaringType);
78                 }
79         }
80 }