Monop: the all requrests station
[mono.git] / mcs / tools / monop / outline.cs
1 //
2 // outline -- support for rendering in monop
3 // Some code stolen from updater.cs in monodoc.
4 //
5 // Authors:
6 //      Ben Maurer (bmaurer@users.sourceforge.net)
7 //
8 // (C) 2004 Ben Maurer
9 //
10
11 using System;
12 using System.Reflection;
13 using System.Collections;
14 using System.CodeDom.Compiler;
15 using System.IO;
16         
17 public class Outline {
18         
19         IndentedTextWriter o;
20         Type t;
21         
22         public Outline (Type t, TextWriter output)
23         {
24                 this.t = t;
25                 this.o = new IndentedTextWriter (output, "    ");
26         }
27         
28         public void OutlineType ()
29         {
30                 o.Write (GetTypeVisibility (t));
31                 o.Write (" ");
32                 o.Write (t.IsValueType ? "struct" : "class");
33                 o.Write (" ");
34                 o.Write (t.Name);
35                 
36                 Type [] interfaces = (Type []) Comparer.Sort (t.GetInterfaces ());
37                 Type parent = t.BaseType;
38
39                 if ((parent != null && parent != typeof (object) && parent != typeof (ValueType)) || interfaces.Length != 0) {
40                         bool first = true;
41                         o.Write (" : ");
42                         
43                         if (parent != null && parent != typeof (object) && parent != typeof (ValueType)) {
44                                 o.Write (FormatType (parent));
45                                 first = false;
46                         }
47                         
48                         foreach (Type intf in interfaces) {
49                                 if (!first) o.Write (", ");
50                                 first = false;
51                                 
52                                 o.Write (FormatType (intf));
53                         }
54                 }
55                 
56                 o.WriteLine (" {");
57                 o.Indent++;
58                 
59                 foreach (ConstructorInfo ci in t.GetConstructors ()) {
60                         OutlineConstructor (ci);
61                         
62                         o.WriteLine ();
63                 }
64                 
65                 o.WriteLine ();
66                 
67                 foreach (MethodInfo m in Comparer.Sort (t.GetMethods ())) {
68                         if ((m.Attributes & MethodAttributes.SpecialName) != 0)
69                                 continue;
70                         
71                         OutlineMethod (m);
72                         
73                         o.WriteLine ();
74                 }
75                 
76                 o.WriteLine ();
77                 
78                 foreach (PropertyInfo pi in Comparer.Sort (t.GetProperties ())) {
79                         OutlineProperty (pi);
80                         
81                         o.WriteLine ();
82                 }
83                 
84                 o.WriteLine ();
85                 
86                 foreach (EventInfo ei in Comparer.Sort (t.GetEvents ())) {
87                         OutlineEvent (ei);
88                         
89                         o.WriteLine ();
90                 }
91                 
92                 
93                 o.Indent--; o.WriteLine ("}");
94         }
95         
96         void OutlineEvent (EventInfo ei)
97         {
98                 MethodBase accessor = ei.GetAddMethod ();
99                 
100                 o.Write (GetMethodVisibility (accessor));
101                 o.Write ("event ");
102                 o.Write (FormatType (ei.EventHandlerType));
103                 o.Write (" ");
104                 o.Write (ei.Name);
105                 o.Write (";");
106         }
107         
108         void OutlineConstructor (ConstructorInfo ci)
109         {
110                 o.Write (GetMethodVisibility (ci));
111                 o.Write (t.Name);
112                 o.Write (" (");
113                 OutlineParams (ci.GetParameters ());
114                 o.Write (");");
115         }
116         
117         
118         void OutlineProperty (PropertyInfo pi)
119         {
120                 ParameterInfo [] idxp = pi.GetIndexParameters ();
121                 MethodBase accessor = pi.CanRead ? pi.GetGetMethod () : pi.GetSetMethod ();
122                 
123                 o.Write (GetMethodVisibility (accessor));
124                 o.Write (GetMethodModifiers  (accessor));
125                 o.Write (FormatType (pi.PropertyType));
126                 o.Write (" ");
127                 
128                 if (idxp.Length == 0)
129                         o.Write (pi.Name);
130                 else {
131                         o.Write ("this [");
132                         OutlineParams (idxp);
133                         o.Write ("]");
134                 }
135                 
136                 o.WriteLine (" {");
137                 o.Indent ++;
138                 
139                 if (pi.CanRead)  o.WriteLine ("get;");
140                 if (pi.CanWrite) o.WriteLine ("set;");
141                 
142                 o.Indent --;
143                 o.Write ("}");
144         }
145         
146         void OutlineMethod (MethodInfo mi)
147         {
148                 o.Write (GetMethodVisibility (mi));
149                 o.Write (GetMethodModifiers  (mi));
150                 o.Write (FormatType (mi.ReturnType));
151                 o.Write (" ");
152                 o.Write (mi.Name);
153                 o.Write (" (");
154                 OutlineParams (mi.GetParameters ());
155                 o.Write (");");
156         }
157         
158         void OutlineParams (ParameterInfo [] pi)
159         {
160                 int i = 0;
161                 foreach (ParameterInfo p in pi) {
162                         bool isPointer = false;
163                         if (p.ParameterType.IsByRef) {
164                                 o.Write (p.IsOut ? "out " : "ref ");
165                                 o.Write (FormatType (p.ParameterType.GetElementType ()));
166                         } else
167                                 o.Write (FormatType (p.ParameterType));
168                         
169                         o.Write (" ");
170                         o.Write (p.Name);
171                         if (i + 1 < pi.Length)
172                                 o.Write (", ");
173                         i++;
174                 }
175         }
176         
177         static string GetMethodVisibility (MethodBase m)
178         {
179                 if (m.IsPublic)   return "public ";
180                 if (m.IsFamily)   return "protected ";
181                 if (m.IsPrivate)  return "private ";
182                 if (m.IsAssembly) return "internal ";
183                         
184                 return null;
185         }
186         
187         static string GetMethodModifiers (MethodBase method)
188         {
189                 if (method.IsStatic)
190                         return "static ";
191         
192                 if (method.IsVirtual)
193                         return ((method.Attributes & MethodAttributes.NewSlot) != 0) ?
194                                 "virtual " :
195                                 "override ";
196                 
197                 return null;
198         }
199         
200         static string GetTypeVisibility (Type t)
201         {
202                 switch (t.Attributes & TypeAttributes.VisibilityMask){
203                 case TypeAttributes.Public:
204                 case TypeAttributes.NestedPublic:
205                         return "public";
206
207                 case TypeAttributes.NestedFamily:
208                 case TypeAttributes.NestedFamANDAssem:
209                 case TypeAttributes.NestedFamORAssem:
210                         return "protected";
211
212                 default:
213                         return "internal";
214                 }
215         }
216         
217         static string FormatType (Type t)
218         {
219                 string type = t.FullName;
220                 if (!type.StartsWith ("System."))
221                         return type;
222                 
223                 if (t.HasElementType) {
224                         Type et = t.GetElementType ();
225                         if (t.IsArray)
226                                 return FormatType (et) + " []";
227                         if (t.IsPointer)
228                                 return FormatType (et) + " *";
229                         if (t.IsByRef)
230                                 return "ref " + FormatType (et);
231                 }
232         
233                 switch (type) {
234                 case "System.Byte": return "byte";
235                 case "System.SByte": return "sbyte";
236                 case "System.Int16": return "short";
237                 case "System.Int32": return "int";
238                 case "System.Int64": return "long";
239                         
240                 case "System.UInt16": return "ushort";
241                 case "System.UInt32": return "uint";
242                 case "System.UInt64": return "ulong";
243                         
244                 case "System.Single":  return "float";
245                 case "System.Double":  return "double";
246                 case "System.Decimal": return "decimal";
247                 case "System.Boolean": return "bool";
248                 case "System.Char":    return "char";
249                 case "System.String":  return "string";
250                         
251                 case "System.Object":  return "object";
252                 case "System.Void":  return "void";
253                 }
254         
255                 if (type.LastIndexOf(".") == 6)
256                         return type.Substring(7);
257                 
258                 return type;
259         }
260 }
261
262 public class Comparer : IComparer  {
263         delegate int ComparerFunc (object a, object b);
264         
265         ComparerFunc cmp;
266         
267         Comparer (ComparerFunc f)
268         {
269                 this.cmp = f;
270         }
271         
272         public int Compare (object a, object b)
273         {
274                 return cmp (a, b);
275         }
276         
277         static int CompareMemberInfo (object a, object b)
278         {
279                 return string.Compare (((MemberInfo) a).Name, ((MemberInfo) b).Name);
280         }
281         
282         static Comparer MemberInfoComparer = new Comparer (new ComparerFunc (CompareMemberInfo));
283         
284         public static MemberInfo [] Sort (MemberInfo [] inf)
285         {
286                 Array.Sort (inf, MemberInfoComparer);
287                 return inf;
288         }
289         
290         static int CompareMethodBase (object a, object b)
291         {
292                 MethodBase aa = (MethodBase) a, bb = (MethodBase) b;
293                 
294                 if (aa.IsStatic == bb.IsStatic)
295                         return CompareMemberInfo (a, b);
296                 
297                 if (aa.IsStatic)
298                         return -1;
299                 
300                 return 1;
301         }
302         
303         static Comparer MethodBaseComparer = new Comparer (new ComparerFunc (CompareMethodBase));
304         
305         public static MethodBase [] Sort (MethodBase [] inf)
306         {
307                 Array.Sort (inf, MethodBaseComparer);
308                 return inf;
309         }
310         
311         static int ComparePropertyInfo (object a, object b)
312         {
313                 PropertyInfo aa = (PropertyInfo) a, bb = (PropertyInfo) b;
314                 
315                 bool astatic = (aa.CanRead ? aa.GetGetMethod () : aa.GetSetMethod ()).IsStatic;
316                 bool bstatic = (bb.CanRead ? bb.GetGetMethod () : bb.GetSetMethod ()).IsStatic;
317                 
318                 if (astatic == bstatic)
319                         return CompareMemberInfo (a, b);
320                 
321                 if (astatic)
322                         return -1;
323                 
324                 return 1;
325         }
326         
327         static Comparer PropertyInfoComparer = new Comparer (new ComparerFunc (ComparePropertyInfo));
328         
329         public static PropertyInfo [] Sort (PropertyInfo [] inf)
330         {
331                 Array.Sort (inf, PropertyInfoComparer);
332                 return inf;
333         }
334         
335         static int CompareEventInfo (object a, object b)
336         {
337                 EventInfo aa = (EventInfo) a, bb = (EventInfo) b;
338                 
339                 bool astatic = aa.GetAddMethod (true).IsStatic;
340                 bool bstatic = bb.GetAddMethod (true).IsStatic;
341                 
342                 if (astatic == bstatic)
343                         return CompareMemberInfo (a, b);
344                 
345                 if (astatic)
346                         return -1;
347                 
348                 return 1;
349         }
350         
351         static Comparer EventInfoComparer = new Comparer (new ComparerFunc (CompareEventInfo));
352         
353         public static EventInfo [] Sort (EventInfo [] inf)
354         {
355                 Array.Sort (inf, EventInfoComparer);
356                 return inf;
357         }
358 }