merge all of this from head
[mono-project.git] / mcs / tools / monop / outline.cs
blob75eafb3b313835883050937c9872ebf9b932e42c
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 //
11 using System;
12 using System.Reflection;
13 using System.Collections;
14 using System.CodeDom.Compiler;
15 using System.IO;
17 public class Outline {
19 IndentedTextWriter o;
20 Type t;
22 public Outline (Type t, TextWriter output)
24 this.t = t;
25 this.o = new IndentedTextWriter (output, " ");
28 public void OutlineType (BindingFlags flags)
30 OutlineAttributes ();
31 o.Write (GetTypeVisibility (t));
33 if (t.IsClass && !t.IsSubclassOf (typeof (System.MulticastDelegate))) {
34 if (t.IsSealed)
35 o.Write (t.IsAbstract ? " static" : " sealed");
36 else if (t.IsAbstract)
37 o.Write (" abstract");
40 o.Write (" ");
41 o.Write (GetTypeKind (t));
42 o.Write (" ");
44 Type [] interfaces = (Type []) Comparer.Sort (t.GetInterfaces ());
45 Type parent = t.BaseType;
47 if (t.IsSubclassOf (typeof (System.MulticastDelegate))) {
48 MethodInfo method;
50 method = t.GetMethod ("Invoke");
52 o.Write (FormatType (method.ReturnType));
53 o.Write (" ");
54 o.Write (t.Name);
55 o.Write (" (");
56 OutlineParams (method.GetParameters ());
57 o.WriteLine (");");
59 return;
62 o.Write (t.Name);
63 if (((parent != null && parent != typeof (object) && parent != typeof (ValueType)) || interfaces.Length != 0) && ! t.IsEnum) {
64 bool first = true;
65 o.Write (" : ");
67 if (parent != null && parent != typeof (object) && parent != typeof (ValueType)) {
68 o.Write (FormatType (parent));
69 first = false;
72 foreach (Type intf in interfaces) {
73 if (!first) o.Write (", ");
74 first = false;
76 o.Write (FormatType (intf));
80 o.WriteLine (" {");
81 o.Indent++;
83 if (t.IsEnum) {
84 bool is_first = true;
85 foreach (FieldInfo fi in t.GetFields (BindingFlags.Public | BindingFlags.Static)) {
87 if (! is_first)
88 o.WriteLine (",");
89 is_first = false;
90 o.Write (fi.Name);
92 o.WriteLine ();
93 o.Indent--; o.WriteLine ("}");
94 return;
97 foreach (ConstructorInfo ci in t.GetConstructors (flags)) {
98 OutlineConstructor (ci);
100 o.WriteLine ();
103 o.WriteLine ();
105 foreach (MethodInfo m in Comparer.Sort (t.GetMethods (flags))) {
106 if ((m.Attributes & MethodAttributes.SpecialName) != 0)
107 continue;
109 OutlineMethod (m);
111 o.WriteLine ();
114 o.WriteLine ();
116 foreach (PropertyInfo pi in Comparer.Sort (t.GetProperties (flags))) {
117 OutlineProperty (pi);
119 o.WriteLine ();
122 o.WriteLine ();
124 foreach (FieldInfo fi in t.GetFields (flags))
126 OutlineField (fi);
129 o.WriteLine ();
131 foreach (EventInfo ei in Comparer.Sort (t.GetEvents (flags))) {
132 OutlineEvent (ei);
134 o.WriteLine ();
137 o.WriteLine ();
139 foreach (Type ntype in Comparer.Sort (t.GetNestedTypes (flags)))
140 new Outline (ntype, o).OutlineType (flags);
142 o.Indent--; o.WriteLine ("}");
145 // FIXME: add other interesting attributes?
146 void OutlineAttributes ()
148 if (t.IsSerializable)
149 o.WriteLine ("[Serializable]");
151 if (t.IsDefined (typeof (System.FlagsAttribute), true))
152 o.WriteLine ("[Flags]");
154 if (t.IsDefined (typeof (System.ObsoleteAttribute), true))
155 o.WriteLine ("[Obsolete]");
158 void OutlineEvent (EventInfo ei)
160 MethodBase accessor = ei.GetAddMethod ();
162 o.Write (GetMethodVisibility (accessor));
163 o.Write ("event ");
164 o.Write (FormatType (ei.EventHandlerType));
165 o.Write (" ");
166 o.Write (ei.Name);
167 o.Write (";");
170 void OutlineConstructor (ConstructorInfo ci)
172 o.Write (GetMethodVisibility (ci));
173 o.Write (t.Name);
174 o.Write (" (");
175 OutlineParams (ci.GetParameters ());
176 o.Write (");");
180 void OutlineProperty (PropertyInfo pi)
182 ParameterInfo [] idxp = pi.GetIndexParameters ();
183 MethodBase accessor = pi.CanRead ? pi.GetGetMethod (true) : pi.GetSetMethod (true);
185 o.Write (GetMethodVisibility (accessor));
186 o.Write (GetMethodModifiers (accessor));
187 o.Write (FormatType (pi.PropertyType));
188 o.Write (" ");
190 if (idxp.Length == 0)
191 o.Write (pi.Name);
192 else {
193 o.Write ("this [");
194 OutlineParams (idxp);
195 o.Write ("]");
198 o.WriteLine (" {");
199 o.Indent ++;
201 if (pi.CanRead) o.WriteLine ("get;");
202 if (pi.CanWrite) o.WriteLine ("set;");
204 o.Indent --;
205 o.Write ("}");
208 void OutlineMethod (MethodInfo mi)
210 o.Write (GetMethodVisibility (mi));
211 o.Write (GetMethodModifiers (mi));
212 o.Write (FormatType (mi.ReturnType));
213 o.Write (" ");
214 o.Write (mi.Name);
215 o.Write (" (");
216 OutlineParams (mi.GetParameters ());
217 o.Write (");");
220 void OutlineParams (ParameterInfo [] pi)
222 int i = 0;
223 foreach (ParameterInfo p in pi) {
224 if (p.ParameterType.IsByRef) {
225 o.Write (p.IsOut ? "out " : "ref ");
226 o.Write (FormatType (p.ParameterType.GetElementType ()));
227 } else if (p.IsDefined (typeof (ParamArrayAttribute), false)) {
228 o.Write ("params ");
229 o.Write (FormatType (p.ParameterType));
230 } else {
231 o.Write (FormatType (p.ParameterType));
234 o.Write (" ");
235 o.Write (p.Name);
236 if (i + 1 < pi.Length)
237 o.Write (", ");
238 i++;
242 void OutlineField (FieldInfo fi)
244 if (fi.IsPublic) o.Write ("public ");
245 if (fi.IsFamily) o.Write ("protected ");
246 if (fi.IsPrivate) o.Write ("private ");
247 if (fi.IsAssembly) o.Write ("internal ");
248 if (fi.IsLiteral) o.Write ("const ");
249 o.Write (FormatType (fi.FieldType));
250 o.Write (" ");
251 o.Write (fi.Name);
252 if (fi.IsLiteral)
254 o.Write (" = ");
255 o.Write (fi.GetValue (this));
257 o.WriteLine (";");
260 static string GetMethodVisibility (MethodBase m)
262 if (m.IsPublic) return "public ";
263 if (m.IsFamily) return "protected ";
264 if (m.IsPrivate) return "private ";
265 if (m.IsAssembly) return "internal ";
267 return null;
270 static string GetMethodModifiers (MethodBase method)
272 if (method.IsStatic)
273 return "static ";
275 if (method.IsVirtual)
276 return ((method.Attributes & MethodAttributes.NewSlot) != 0) ?
277 "virtual " :
278 "override ";
280 return null;
283 static string GetTypeKind (Type t)
285 if (t.IsEnum)
286 return "enum";
287 if (t.IsClass) {
288 if (t.IsSubclassOf (typeof (System.MulticastDelegate)))
289 return "delegate";
290 else
291 return "class";
293 if (t.IsInterface)
294 return "interface";
295 if (t.IsValueType)
296 return "struct";
297 return "class";
300 static string GetTypeVisibility (Type t)
302 switch (t.Attributes & TypeAttributes.VisibilityMask){
303 case TypeAttributes.Public:
304 case TypeAttributes.NestedPublic:
305 return "public";
307 case TypeAttributes.NestedFamily:
308 case TypeAttributes.NestedFamANDAssem:
309 case TypeAttributes.NestedFamORAssem:
310 return "protected";
312 default:
313 return "internal";
317 static string FormatType (Type t)
319 string type = t.FullName;
320 if (!type.StartsWith ("System."))
321 return type;
323 if (t.HasElementType) {
324 Type et = t.GetElementType ();
325 if (t.IsArray)
326 return FormatType (et) + " []";
327 if (t.IsPointer)
328 return FormatType (et) + " *";
329 if (t.IsByRef)
330 return "ref " + FormatType (et);
333 switch (type) {
334 case "System.Byte": return "byte";
335 case "System.SByte": return "sbyte";
336 case "System.Int16": return "short";
337 case "System.Int32": return "int";
338 case "System.Int64": return "long";
340 case "System.UInt16": return "ushort";
341 case "System.UInt32": return "uint";
342 case "System.UInt64": return "ulong";
344 case "System.Single": return "float";
345 case "System.Double": return "double";
346 case "System.Decimal": return "decimal";
347 case "System.Boolean": return "bool";
348 case "System.Char": return "char";
349 case "System.String": return "string";
351 case "System.Object": return "object";
352 case "System.Void": return "void";
355 if (type.LastIndexOf(".") == 6)
356 return type.Substring(7);
358 return type;
362 public class Comparer : IComparer {
363 delegate int ComparerFunc (object a, object b);
365 ComparerFunc cmp;
367 Comparer (ComparerFunc f)
369 this.cmp = f;
372 public int Compare (object a, object b)
374 return cmp (a, b);
377 static int CompareType (object a, object b)
379 Type type1 = (Type) a;
380 Type type2 = (Type) b;
382 if (type1.IsSubclassOf (typeof (System.MulticastDelegate)) != type2.IsSubclassOf (typeof (System.MulticastDelegate)))
383 return (type1.IsSubclassOf (typeof (System.MulticastDelegate)))? -1:1;
384 return string.Compare (type1.Name, type2.Name);
388 static Comparer TypeComparer = new Comparer (new ComparerFunc (CompareType));
390 static Type [] Sort (Type [] types)
392 Array.Sort (types, TypeComparer);
393 return types;
396 static int CompareMemberInfo (object a, object b)
398 return string.Compare (((MemberInfo) a).Name, ((MemberInfo) b).Name);
401 static Comparer MemberInfoComparer = new Comparer (new ComparerFunc (CompareMemberInfo));
403 public static MemberInfo [] Sort (MemberInfo [] inf)
405 Array.Sort (inf, MemberInfoComparer);
406 return inf;
409 static int CompareMethodBase (object a, object b)
411 MethodBase aa = (MethodBase) a, bb = (MethodBase) b;
413 if (aa.IsStatic == bb.IsStatic)
414 return CompareMemberInfo (a, b);
416 if (aa.IsStatic)
417 return -1;
419 return 1;
422 static Comparer MethodBaseComparer = new Comparer (new ComparerFunc (CompareMethodBase));
424 public static MethodBase [] Sort (MethodBase [] inf)
426 Array.Sort (inf, MethodBaseComparer);
427 return inf;
430 static int ComparePropertyInfo (object a, object b)
432 PropertyInfo aa = (PropertyInfo) a, bb = (PropertyInfo) b;
434 bool astatic = (aa.CanRead ? aa.GetGetMethod (true) : aa.GetSetMethod (true)).IsStatic;
435 bool bstatic = (bb.CanRead ? bb.GetGetMethod (true) : bb.GetSetMethod (true)).IsStatic;
437 if (astatic == bstatic)
438 return CompareMemberInfo (a, b);
440 if (astatic)
441 return -1;
443 return 1;
446 static Comparer PropertyInfoComparer = new Comparer (new ComparerFunc (ComparePropertyInfo));
448 public static PropertyInfo [] Sort (PropertyInfo [] inf)
450 Array.Sort (inf, PropertyInfoComparer);
451 return inf;
454 static int CompareEventInfo (object a, object b)
456 EventInfo aa = (EventInfo) a, bb = (EventInfo) b;
458 bool astatic = aa.GetAddMethod (true).IsStatic;
459 bool bstatic = bb.GetAddMethod (true).IsStatic;
461 if (astatic == bstatic)
462 return CompareMemberInfo (a, b);
464 if (astatic)
465 return -1;
467 return 1;
470 static Comparer EventInfoComparer = new Comparer (new ComparerFunc (CompareEventInfo));
472 public static EventInfo [] Sort (EventInfo [] inf)
474 Array.Sort (inf, EventInfoComparer);
475 return inf;