[monop] Port to use IKVM.Reflection, add -xi and -xa options to easily lookup Xamarin...
[mono.git] / mcs / tools / monop / outline.cs
index db055e726d5ac07873bf423a4c606c377ca9e684..67e72d29f81bf9cd653c24f8b59c6b80ffbbbceb 100644 (file)
 //
 
 using System;
-using System.Reflection;
 using System.Collections;
 using System.CodeDom.Compiler;
 using System.IO;
 using System.Text;
+#if STATIC
+using IKVM.Reflection;
+using Type=IKVM.Reflection.Type;
+#else
+using System.Reflection;
+#endif
 
 namespace Mono.CSharp {
 public class Outline {
-
        bool declared_only;
        bool show_private;
        bool filter_obsolete;
        
        IndentedTextWriter o;
        Type t;
+       Type type_multicast_delegate, type_object, type_value_type, type_int, type_flags_attribute, type_obsolete_attribute, type_param_array_attribute;
        
+#if STATIC
+       Universe universe;
+       Assembly mscorlib;
+
+       public Outline (Universe universe, Assembly mscorlib, Type t, TextWriter output, bool declared_only, bool show_private, bool filter_obsolete)
+       {
+               if (universe == null)
+                       throw new ArgumentNullException ("universe");
+               if (mscorlib == null)
+                       throw new ArgumentNullException ("mscorlib");
+               this.universe = universe;
+               this.mscorlib = mscorlib;
+               this.t = t;
+               this.o = new IndentedTextWriter (output, "\t");
+               this.declared_only = declared_only;
+               this.show_private = show_private;
+               this.filter_obsolete = filter_obsolete;
+
+               type_multicast_delegate = mscorlib.GetType("System.MulticastDelegate");
+               type_object = mscorlib.GetType ("System.Object");
+               type_value_type = mscorlib.GetType ("System.ValueType");
+               type_int = mscorlib.GetType ("System.Int32");
+               type_flags_attribute = mscorlib.GetType ("System.FlagsAttribute");
+               type_obsolete_attribute = mscorlib.GetType ("System.ObsoleteAttribute");
+               type_param_array_attribute = mscorlib.GetType ("System.ParamArrayAttribute");
+       }
+#else
        public Outline (Type t, TextWriter output, bool declared_only, bool show_private, bool filter_obsolete)
        {
                this.t = t;
@@ -53,7 +85,16 @@ public class Outline {
                this.declared_only = declared_only;
                this.show_private = show_private;
                this.filter_obsolete = filter_obsolete;
+
+               type_multicast_delegate = typeof (System.MulticastDelegate);
+               type_object = typeof (object);
+               type_value_type = typeof (ValueType);
+               type_int = typeof (int);
+               type_flags_attribute = typeof (FlagsAttribute);
+               type_obsolete_attribute = typeof (ObsoleteAttribuet);
+               type_param_array_attribute = typeof (ParamArrayAttribute);
        }
+#endif
 
        public void OutlineType ()
         {
@@ -62,7 +103,7 @@ public class Outline {
                OutlineAttributes ();
                o.Write (GetTypeVisibility (t));
                
-               if (t.IsClass && !t.IsSubclassOf (typeof (System.MulticastDelegate))) {
+               if (t.IsClass && !t.IsSubclassOf (type_multicast_delegate)) {
                        if (t.IsSealed)
                                o.Write (t.IsAbstract ? " static" : " sealed");
                        else if (t.IsAbstract)
@@ -76,7 +117,7 @@ public class Outline {
                Type [] interfaces = (Type []) Comparer.Sort (TypeGetInterfaces (t, declared_only));
                Type parent = t.BaseType;
 
-               if (t.IsSubclassOf (typeof (System.MulticastDelegate))) {
+               if (t.IsSubclassOf (type_multicast_delegate)) {
                        MethodInfo method;
 
                        method = t.GetMethod ("Invoke");
@@ -95,11 +136,11 @@ public class Outline {
                }
                
                o.Write (GetTypeName (t));
-               if (((parent != null && parent != typeof (object) && parent != typeof (ValueType)) || interfaces.Length != 0) && ! t.IsEnum) {
+               if (((parent != null && parent != type_object && parent != type_value_type) || interfaces.Length != 0) && ! t.IsEnum) {
                        first = true;
                        o.Write (" : ");
                        
-                       if (parent != null && parent != typeof (object) && parent != typeof (ValueType)) {
+                       if (parent != null && parent != type_object && parent != type_value_type) {
                                o.Write (FormatType (parent));
                                first = false;
                        }
@@ -113,8 +154,8 @@ public class Outline {
                }
 
                if (t.IsEnum) {
-                       Type underlyingType = System.Enum.GetUnderlyingType (t);
-                       if (underlyingType != typeof (int))
+                       Type underlyingType = t.GetEnumUnderlyingType ();
+                       if (underlyingType != type_int)
                                o.Write (" : {0}", FormatType (underlyingType));
                }
                WriteGenericConstraints (t.GetGenericArguments ());
@@ -256,8 +297,12 @@ public class Outline {
                        if (first)
                                o.WriteLine ();
                        first = false;
-                       
+
+#if STATIC
+                       new Outline (universe, mscorlib, ntype, o, declared_only, show_private, filter_obsolete).OutlineType ();
+#else
                        new Outline (ntype, o, declared_only, show_private, filter_obsolete).OutlineType ();
+#endif
                }
                
                o.Indent--; o.WriteLine ("}");
@@ -280,20 +325,21 @@ public class Outline {
                if (t.IsSerializable)
                        o.WriteLine ("[Serializable]");
 
-               if (t.IsDefined (typeof (System.FlagsAttribute), true))
+               if (t.IsDefined (type_flags_attribute, true))
                        o.WriteLine ("[Flags]");
 
-               if (t.IsDefined (typeof (System.ObsoleteAttribute), true))
+               if (t.IsDefined (type_obsolete_attribute, true))
                        o.WriteLine ("[Obsolete]");
        }
 
        void OutlineMemberAttribute (MemberInfo mi)
        {
-               if (!mi.IsDefined (typeof (System.ObsoleteAttribute), false))
-                       return;
-               var oa = mi.GetCustomAttributes (typeof (System.ObsoleteAttribute), false) [0] as ObsoleteAttribute;
-               var msg = oa.Message;
-               o.WriteLine ("[Obsolete{0}]", msg == null || msg == "" ? "" : string.Format ("(\"{0}\")", msg));
+               var attrs = mi.GetCustomAttributesData ();
+               if (attrs.Count > 0)
+                       o.WriteLine ("");
+               
+               foreach (var attr in attrs)
+                       o.WriteLine (attr);
        }
        
        void OutlineEvent (EventInfo ei)
@@ -425,7 +471,7 @@ public class Outline {
                        if (p.ParameterType.IsByRef) {
                                o.Write (p.IsOut ? "out " : "ref ");
                                o.Write (FormatType (p.ParameterType.GetElementType ()));
-                       } else if (p.IsDefined (typeof (ParamArrayAttribute), false)) {
+                       } else if (p.IsDefined (type_param_array_attribute, false)) {
                                o.Write ("params ");
                                o.Write (FormatType (p.ParameterType));
                        } else {
@@ -454,7 +500,7 @@ public class Outline {
                o.Write (" ");
                o.Write (fi.Name);
                if (fi.IsLiteral) { 
-                       object v = fi.GetValue (this);
+                       object v = fi.GetRawConstantValue ();
 
                        // TODO: Escape values here
                        o.Write (" = ");
@@ -463,7 +509,7 @@ public class Outline {
                        else if (v is string)
                                o.Write ("\"{0}\"", v);
                        else
-                               o.Write (fi.GetValue (this));
+                               o.Write (fi.GetRawConstantValue ());
                }
                o.Write (";");
        }
@@ -521,12 +567,12 @@ public class Outline {
                return null;
        }
 
-       static string GetTypeKind (Type t)
+       string GetTypeKind (Type t)
        {
                if (t.IsEnum)
                        return "enum";
                if (t.IsClass) {
-                       if (t.IsSubclassOf (typeof (System.MulticastDelegate)))
+                       if (t.IsSubclassOf (type_multicast_delegate))
                                return "delegate";
                        else
                                return "class";
@@ -583,6 +629,8 @@ public class Outline {
                        return t.ToString ();
                
                if (!type.StartsWith ("System.")) {
+                       if (type.IndexOf (".") == -1)
+                               return type;
                        if (t.Namespace == this.t.Namespace)
                                return t.Name;
                        return type;
@@ -716,13 +764,13 @@ public class Outline {
                                GenericParameterAttributes.DefaultConstructorConstraint
                        };
                        
-                       if (t.BaseType != typeof (object) || ifaces.Length != 0 || attrs != 0) {
+                       if (t.BaseType != type_object || ifaces.Length != 0 || attrs != 0) {
                                o.Write (" where ");
                                o.Write (FormatType (t));
                                o.Write (" : ");
                        }
 
-                       if (t.BaseType != typeof (object)) {
+                       if (t.BaseType != type_object) {
                                o.Write (FormatType (t.BaseType));
                                first = false;
                        }
@@ -817,7 +865,7 @@ public class Outline {
                if (show_private)
                        return true;
 
-               if (filter_obsolete && mi.IsDefined (typeof (ObsoleteAttribute), false))
+               if (filter_obsolete && mi.IsDefined (type_obsolete_attribute, false))
                        return false;
                
                switch (mi.MemberType) {
@@ -905,8 +953,6 @@ public class Comparer : IComparer  {
                Type type1 = (Type) a;
                Type type2 = (Type) b;
 
-               if (type1.IsSubclassOf (typeof (System.MulticastDelegate)) != type2.IsSubclassOf (typeof (System.MulticastDelegate)))
-                               return (type1.IsSubclassOf (typeof (System.MulticastDelegate)))? -1:1;
                return string.Compare (type1.Name, type2.Name);
                        
        }