2008-12-04 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / tools / corcompare / MissingNameSpace.cs
1 // Mono.Util.CorCompare.MissingNameSpace
2 //
3 // Author(s):
4 //   Nick Drochak (ndrochak@gol.com)
5 //
6 // (C) 2001-2002 Nick Drochak
7
8 using System;
9 using System.Collections;
10 using System.Xml;
11 using Mono.Cecil;
12
13 namespace Mono.Util.CorCompare {
14
15         /// <summary>
16         ///     Represents a namespace that has missing and/or MonoTODO classes.
17         /// </summary>
18         /// <remarks>
19         ///     created by - Nick
20         ///     created on - 2/20/2002 10:43:57 PM
21         /// </remarks>
22         class MissingNameSpace : MissingBase
23         {
24                 // e.g. <namespace name="System" missing="267" todo="453" complete="21">
25                 protected TypeDefinitionCollection rgTypesMono, rgTypesMS;
26                 string strNamespace;
27                 ArrayList rgTypes = new ArrayList ();
28                 protected static Hashtable htGhostTypes;
29                 static string[] rgstrGhostTypes = {"System.Object", "System.ValueType", "System.Delegate", "System.Enum"};
30
31
32                 static MissingNameSpace ()
33                 {
34                         htGhostTypes = new Hashtable ();
35
36                         foreach (string strGhostType in rgstrGhostTypes)
37                         {
38                                 htGhostTypes.Add (strGhostType, null);
39                         }
40                 }
41
42                 public MissingNameSpace (string nameSpace, TypeDefinitionCollection _rgTypesMono, TypeDefinitionCollection _rgTypesMS)
43                 {
44                         strNamespace = nameSpace;
45                         rgTypesMono = _rgTypesMono;
46                         rgTypesMS = _rgTypesMS;
47                         m_nodeStatus = new NodeStatus (_rgTypesMono, _rgTypesMS);
48                 }
49
50                 public virtual string [] MissingTypeNames (bool f)
51                 {
52                         return null;
53                 }
54
55                 public virtual ArrayList ToDoTypeNames
56                 {
57                         get { return null; }
58                 }
59
60                 public override string Name
61                 {
62                         get { return strNamespace; }
63                 }
64                 public override string Type
65                 {
66                         get { return "namespace"; }
67                 }
68
69
70                 /// <summary>
71                 /// first we go through all the microsoft types adding any mono types that match, or missing types otherwise
72                 /// then we go through the unmatched mono types adding those
73                 /// uses a hashtable to speed up lookups
74                 /// </summary>
75                 /// <returns></returns>
76                 public override NodeStatus Analyze ()
77                 {
78                         Hashtable htMono = new Hashtable ();
79                         if (rgTypesMono != null)
80                         {
81                                 foreach (TypeDefinition t in rgTypesMono)
82                                 {
83                                         htMono.Add (t.FullName, t);
84                                 }
85                         }
86                         if (rgTypesMS != null)
87                         {
88                                 foreach (TypeDefinition t in rgTypesMS)
89                                 {
90                                         TypeDefinition tMono = (TypeDefinition) htMono [t.FullName];
91                                         MissingType mt = null;
92                                         bool tIsPublic = (t.Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public;
93                                         if (tMono == null)
94                                         {
95                                                 if (tIsPublic && !htGhostTypes.Contains (t.FullName))
96                                                         mt = new MissingType (null, t);
97                                         }
98                                         else
99                                         {
100                                                 if (tIsPublic)
101                                                 {
102                                                         htMono.Remove (t.FullName);
103                                                         mt = new MissingType (tMono, t);
104                                                 }
105                                         }
106                                         if (mt != null)
107                                         {
108                                                 NodeStatus nsType = mt.Analyze ();
109                                                 m_nodeStatus.AddChildren (nsType);
110                                                 rgTypes.Add (mt);
111                                         }
112                                 }
113                         }
114                         // do any mono types that aren't in microsoft's namespace
115                         foreach (TypeDefinition tMono in htMono.Values)
116                         {
117                                 if ((tMono.Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public)
118                                 {
119                                         MissingType tdt = new MissingType (tMono, null);
120                                         NodeStatus nsType = tdt.Analyze ();
121                                         m_nodeStatus.AddChildren (nsType);
122                                         rgTypes.Add (tdt);
123                                 }
124                         }
125                         return m_nodeStatus;
126                 }
127
128                 public override XmlElement CreateXML (XmlDocument doc)
129                 {
130                         XmlElement eltNameSpace = base.CreateXML (doc);
131
132                         // TODO: include complete namespaces?
133 //                      if (m_nodeStatus.statusCountsTotal.cMissing > 0 || m_nodeStatus.statusCountsTotal.cTodo > 0)
134                         {
135                                 XmlElement eltClasses = doc.CreateElement("classes");
136                                 eltNameSpace.AppendChild (eltClasses);
137
138                                 foreach (MissingType type in rgTypes)
139                                 {
140                                         XmlElement eltClass = type.CreateXML (doc);
141                                         if (eltClass != null)
142                                                 eltClasses.AppendChild (eltClass);
143                                 }
144                         }
145                         return eltNameSpace;
146                 }
147
148
149                 public static ArrayList GetNamespaces (TypeDefinitionCollection types)
150                 {
151                         ArrayList nsList = new ArrayList();
152                         foreach (TypeDefinition t in types)
153                         {
154                                 if (!nsList.Contains(t.Namespace))
155                                 {
156                                         nsList.Add(t.Namespace);
157                                 }
158                         }
159                         return nsList;
160                 }
161         }
162 }
163