2 // outline -- support for rendering in monop
3 // Some code stolen from updater.cs in monodoc.
6 // Ben Maurer (bmaurer@users.sourceforge.net)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Reflection;
34 using System.Collections;
35 using System.CodeDom.Compiler;
39 public class Outline {
45 public Outline (Type t, TextWriter output, Options options)
48 this.o = new IndentedTextWriter (output, " ");
49 this.options = options;
52 public void OutlineType ()
57 o.Write (GetTypeVisibility (t));
59 if (t.IsClass && !t.IsSubclassOf (typeof (System.MulticastDelegate))) {
61 o.Write (t.IsAbstract ? " static" : " sealed");
62 else if (t.IsAbstract)
63 o.Write (" abstract");
67 o.Write (GetTypeKind (t));
70 Type [] interfaces = (Type []) Comparer.Sort (t.GetInterfaces ());
71 Type parent = t.BaseType;
73 if (t.IsSubclassOf (typeof (System.MulticastDelegate))) {
76 method = t.GetMethod ("Invoke");
78 o.Write (FormatType (method.ReturnType));
80 o.Write (GetTypeName (t));
82 OutlineParams (method.GetParameters ());
88 o.Write (GetTypeName (t));
89 if (((parent != null && parent != typeof (object) && parent != typeof (ValueType)) || interfaces.Length != 0) && ! t.IsEnum) {
93 if (parent != null && parent != typeof (object) && parent != typeof (ValueType)) {
94 o.Write (FormatType (parent));
98 foreach (Type intf in interfaces) {
99 if (!first) o.Write (", ");
102 o.Write (FormatType (intf));
107 Type underlyingType = Enum.GetUnderlyingType (t);
108 if (underlyingType != typeof (int))
109 o.Write (" : {0}", FormatType (underlyingType));
116 bool is_first = true;
117 foreach (FieldInfo fi in t.GetFields (BindingFlags.Public | BindingFlags.Static)) {
125 o.Indent--; o.WriteLine ("}");
131 foreach (ConstructorInfo ci in t.GetConstructors (DefaultFlags)) {
133 if (! ShowMember (ci))
140 OutlineConstructor (ci);
148 foreach (MethodInfo m in Comparer.Sort (t.GetMethods (DefaultFlags))) {
150 if (! ShowMember (m))
153 if ((m.Attributes & MethodAttributes.SpecialName) != 0)
167 foreach (MethodInfo m in t.GetMethods (DefaultFlags)) {
169 if (! ShowMember (m))
172 if ((m.Attributes & MethodAttributes.SpecialName) == 0)
174 if (!(m.Name.StartsWith ("op_")))
188 foreach (PropertyInfo pi in Comparer.Sort (t.GetProperties (DefaultFlags))) {
190 if (! ((pi.CanRead && ShowMember (pi.GetGetMethod (true))) ||
191 (pi.CanWrite && ShowMember (pi.GetSetMethod (true)))))
198 OutlineProperty (pi);
205 foreach (FieldInfo fi in t.GetFields (DefaultFlags)) {
207 if (! ShowMember (fi))
221 foreach (EventInfo ei in Comparer.Sort (t.GetEvents (DefaultFlags))) {
223 if (! ShowMember (ei.GetAddMethod ()))
237 foreach (Type ntype in Comparer.Sort (t.GetNestedTypes (DefaultFlags))) {
239 if (! ShowMember (ntype))
246 new Outline (ntype, o, options).OutlineType ();
249 o.Indent--; o.WriteLine ("}");
252 BindingFlags DefaultFlags {
254 BindingFlags f = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
256 if (options.DeclaredOnly)
257 f |= BindingFlags.DeclaredOnly;
263 // FIXME: add other interesting attributes?
264 void OutlineAttributes ()
266 if (t.IsSerializable)
267 o.WriteLine ("[Serializable]");
269 if (t.IsDefined (typeof (System.FlagsAttribute), true))
270 o.WriteLine ("[Flags]");
272 if (t.IsDefined (typeof (System.ObsoleteAttribute), true))
273 o.WriteLine ("[Obsolete]");
276 void OutlineEvent (EventInfo ei)
278 MethodBase accessor = ei.GetAddMethod ();
280 o.Write (GetMethodVisibility (accessor));
282 o.Write (FormatType (ei.EventHandlerType));
288 void OutlineConstructor (ConstructorInfo ci)
290 o.Write (GetMethodVisibility (ci));
291 o.Write (RemoveGenericArity (t.Name));
293 OutlineParams (ci.GetParameters ());
298 void OutlineProperty (PropertyInfo pi)
300 ParameterInfo [] idxp = pi.GetIndexParameters ();
301 MethodBase g = pi.GetGetMethod (true);
302 MethodBase s = pi.GetSetMethod (true);
303 MethodBase accessor = g != null ? g : s;
305 if (pi.CanRead && pi.CanWrite) {
308 // Get the more accessible accessor
309 if ((g.Attributes & MethodAttributes.MemberAccessMask) !=
310 (s.Attributes & MethodAttributes.MemberAccessMask)) {
312 if (g.IsPublic) accessor = g;
313 else if (s.IsPublic) accessor = s;
314 else if (g.IsFamilyOrAssembly) accessor = g;
315 else if (s.IsFamilyOrAssembly) accessor = s;
316 else if (g.IsAssembly || g.IsFamily) accessor = g;
317 else if (s.IsAssembly || s.IsFamily) accessor = s;
321 o.Write (GetMethodVisibility (accessor));
322 o.Write (GetMethodModifiers (accessor));
323 o.Write (FormatType (pi.PropertyType));
326 if (idxp.Length == 0)
330 OutlineParams (idxp);
337 if (g != null && ShowMember (g)) {
338 if ((g.Attributes & MethodAttributes.MemberAccessMask) !=
339 (accessor.Attributes & MethodAttributes.MemberAccessMask))
340 o.Write (GetMethodVisibility (g));
341 o.WriteLine ("get;");
344 if (s != null && ShowMember (s)) {
345 if ((s.Attributes & MethodAttributes.MemberAccessMask) !=
346 (accessor.Attributes & MethodAttributes.MemberAccessMask))
347 o.Write (GetMethodVisibility (s));
348 o.WriteLine ("set;");
355 void OutlineMethod (MethodInfo mi)
357 o.Write (GetMethodVisibility (mi));
358 o.Write (GetMethodModifiers (mi));
359 o.Write (FormatType (mi.ReturnType));
363 OutlineParams (mi.GetParameters ());
367 void OutlineOperator (MethodInfo mi)
369 o.Write (GetMethodVisibility (mi));
370 o.Write (GetMethodModifiers (mi));
371 if (mi.Name == "op_Explicit" || mi.Name == "op_Implicit") {
372 o.Write (mi.Name.Substring (3).ToLower ());
373 o.Write (" operator ");
374 o.Write (FormatType (mi.ReturnType));
376 o.Write (FormatType (mi.ReturnType));
377 o.Write (" operator ");
378 o.Write (OperatorFromName (mi.Name));
381 OutlineParams (mi.GetParameters ());
385 void OutlineParams (ParameterInfo [] pi)
388 foreach (ParameterInfo p in pi) {
389 if (p.ParameterType.IsByRef) {
390 o.Write (p.IsOut ? "out " : "ref ");
391 o.Write (FormatType (p.ParameterType.GetElementType ()));
392 } else if (p.IsDefined (typeof (ParamArrayAttribute), false)) {
394 o.Write (FormatType (p.ParameterType));
396 o.Write (FormatType (p.ParameterType));
401 if (i + 1 < pi.Length)
407 void OutlineField (FieldInfo fi)
409 if (fi.IsPublic) o.Write ("public ");
410 if (fi.IsFamily) o.Write ("protected ");
411 if (fi.IsPrivate) o.Write ("private ");
412 if (fi.IsAssembly) o.Write ("internal ");
413 if (fi.IsLiteral) o.Write ("const ");
414 if (fi.IsInitOnly) o.Write ("readonly ");
416 o.Write (FormatType (fi.FieldType));
422 o.Write (fi.GetValue (this));
427 static string GetMethodVisibility (MethodBase m)
429 // itnerfaces have no modifiers here
430 if (m.DeclaringType.IsInterface)
433 if (m.IsPublic) return "public ";
434 if (m.IsFamily) return "protected ";
435 if (m.IsPrivate) return "private ";
436 if (m.IsAssembly) return "internal ";
441 static string GetMethodModifiers (MethodBase method)
446 // all interface methods are "virtual" but we don't say that in c#
447 if (method.IsVirtual && !method.DeclaringType.IsInterface)
448 return ((method.Attributes & MethodAttributes.NewSlot) != 0) ?
455 static string GetTypeKind (Type t)
460 if (t.IsSubclassOf (typeof (System.MulticastDelegate)))
472 static string GetTypeVisibility (Type t)
474 switch (t.Attributes & TypeAttributes.VisibilityMask){
475 case TypeAttributes.Public:
476 case TypeAttributes.NestedPublic:
479 case TypeAttributes.NestedFamily:
480 case TypeAttributes.NestedFamANDAssem:
481 case TypeAttributes.NestedFamORAssem:
489 string FormatType (Type t)
491 string type = GetFullName (t);
493 if (!type.StartsWith ("System.")) {
494 if (t.Namespace == this.t.Namespace)
499 if (t.HasElementType) {
500 Type et = t.GetElementType ();
502 return FormatType (et) + " []";
504 return FormatType (et) + " *";
506 return "ref " + FormatType (et);
510 case "System.Byte": return "byte";
511 case "System.SByte": return "sbyte";
512 case "System.Int16": return "short";
513 case "System.Int32": return "int";
514 case "System.Int64": return "long";
516 case "System.UInt16": return "ushort";
517 case "System.UInt32": return "uint";
518 case "System.UInt64": return "ulong";
520 case "System.Single": return "float";
521 case "System.Double": return "double";
522 case "System.Decimal": return "decimal";
523 case "System.Boolean": return "bool";
524 case "System.Char": return "char";
525 case "System.String": return "string";
527 case "System.Object": return "object";
528 case "System.Void": return "void";
531 if (type.LastIndexOf(".") == 6)
532 return type.Substring(7);
537 public static string RemoveGenericArity (string name)
540 StringBuilder sb = new StringBuilder ();
541 while (start < name.Length) {
542 int pos = name.IndexOf ('`', start);
544 sb.Append (name.Substring (start));
547 sb.Append (name.Substring (start, pos-start));
551 while ((pos < name.Length) && Char.IsNumber (name [pos]))
557 return sb.ToString ();
560 string GetTypeName (Type t)
562 StringBuilder sb = new StringBuilder ();
564 return sb.ToString ();
567 void GetTypeName (StringBuilder sb, Type t)
569 sb.Append (RemoveGenericArity (t.Name));
571 Type[] args = t.GetGenericArguments ();
572 if (args.Length > 0) {
574 for (int i = 0; i < args.Length; i++) {
577 sb.Append (FormatType (args [i]));
584 string GetFullName (Type t)
586 StringBuilder sb = new StringBuilder ();
587 GetFullName_recursed (sb, t, false);
588 return sb.ToString ();
591 void GetFullName_recursed (StringBuilder sb, Type t, bool recursed)
594 if (t.IsGenericParameter) {
600 if (t.DeclaringType != null) {
601 GetFullName_recursed (sb, t.DeclaringType, true);
606 string ns = t.Namespace;
607 if ((ns != null) && (ns != "")) {
616 string OperatorFromName (string name)
619 case "op_UnaryPlus": return "+";
620 case "op_UnaryNegation": return "-";
621 case "op_LogicalNot": return "!";
622 case "op_OnesComplement": return "~";
623 case "op_Increment": return "++";
624 case "op_Decrement": return "--";
625 case "op_True": return "true";
626 case "op_False": return "false";
627 case "op_Addition": return "+";
628 case "op_Subtraction": return "-";
629 case "op_Multiply": return "*";
630 case "op_Division": return "/";
631 case "op_Modulus": return "%";
632 case "op_BitwiseAnd": return "&";
633 case "op_BitwiseOr": return "|";
634 case "op_ExclusiveOr": return "^";
635 case "op_LeftShift": return "<<";
636 case "op_RightShift": return ">>";
637 case "op_Equality": return "==";
638 case "op_Inequality": return "!=";
639 case "op_GreaterThan": return ">";
640 case "op_LessThan": return "<";
641 case "op_GreaterThanOrEqual": return ">=";
642 case "op_LessThanOrEqual": return "<=";
643 default: return name;
647 bool ShowMember (MemberInfo mi)
649 if (mi.MemberType == MemberTypes.Constructor && ((MethodBase) mi).IsStatic)
652 if (options.ShowPrivate)
655 switch (mi.MemberType) {
656 case MemberTypes.Constructor:
657 case MemberTypes.Method:
658 MethodBase mb = mi as MethodBase;
660 if (mb.IsFamily || mb.IsPublic || mb.IsFamilyOrAssembly)
666 case MemberTypes.Field:
667 FieldInfo fi = mi as FieldInfo;
669 if (fi.IsFamily || fi.IsPublic || fi.IsFamilyOrAssembly)
675 case MemberTypes.NestedType:
676 case MemberTypes.TypeInfo:
679 switch (t.Attributes & TypeAttributes.VisibilityMask){
680 case TypeAttributes.Public:
681 case TypeAttributes.NestedPublic:
682 case TypeAttributes.NestedFamily:
683 case TypeAttributes.NestedFamORAssem:
695 public class Comparer : IComparer {
696 delegate int ComparerFunc (object a, object b);
700 Comparer (ComparerFunc f)
705 public int Compare (object a, object b)
710 static int CompareType (object a, object b)
712 Type type1 = (Type) a;
713 Type type2 = (Type) b;
715 if (type1.IsSubclassOf (typeof (System.MulticastDelegate)) != type2.IsSubclassOf (typeof (System.MulticastDelegate)))
716 return (type1.IsSubclassOf (typeof (System.MulticastDelegate)))? -1:1;
717 return string.Compare (type1.Name, type2.Name);
721 static Comparer TypeComparer = new Comparer (new ComparerFunc (CompareType));
723 static Type [] Sort (Type [] types)
725 Array.Sort (types, TypeComparer);
729 static int CompareMemberInfo (object a, object b)
731 return string.Compare (((MemberInfo) a).Name, ((MemberInfo) b).Name);
734 static Comparer MemberInfoComparer = new Comparer (new ComparerFunc (CompareMemberInfo));
736 public static MemberInfo [] Sort (MemberInfo [] inf)
738 Array.Sort (inf, MemberInfoComparer);
742 static int CompareMethodBase (object a, object b)
744 MethodBase aa = (MethodBase) a, bb = (MethodBase) b;
746 if (aa.IsStatic == bb.IsStatic)
747 return CompareMemberInfo (a, b);
755 static Comparer MethodBaseComparer = new Comparer (new ComparerFunc (CompareMethodBase));
757 public static MethodBase [] Sort (MethodBase [] inf)
759 Array.Sort (inf, MethodBaseComparer);
763 static int ComparePropertyInfo (object a, object b)
765 PropertyInfo aa = (PropertyInfo) a, bb = (PropertyInfo) b;
767 bool astatic = (aa.CanRead ? aa.GetGetMethod (true) : aa.GetSetMethod (true)).IsStatic;
768 bool bstatic = (bb.CanRead ? bb.GetGetMethod (true) : bb.GetSetMethod (true)).IsStatic;
770 if (astatic == bstatic)
771 return CompareMemberInfo (a, b);
779 static Comparer PropertyInfoComparer = new Comparer (new ComparerFunc (ComparePropertyInfo));
781 public static PropertyInfo [] Sort (PropertyInfo [] inf)
783 Array.Sort (inf, PropertyInfoComparer);
787 static int CompareEventInfo (object a, object b)
789 EventInfo aa = (EventInfo) a, bb = (EventInfo) b;
791 bool astatic = aa.GetAddMethod (true).IsStatic;
792 bool bstatic = bb.GetAddMethod (true).IsStatic;
794 if (astatic == bstatic)
795 return CompareMemberInfo (a, b);
803 static Comparer EventInfoComparer = new Comparer (new ComparerFunc (CompareEventInfo));
805 public static EventInfo [] Sort (EventInfo [] inf)
807 Array.Sort (inf, EventInfoComparer);