2008-07-22 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / support.cs
index 994fb24c960289d2f615ae490e93f9c41c907139..d70fddb234ffb7e9a3c7b0a01a5ae5db21935c45 100644 (file)
@@ -5,7 +5,8 @@
 // Author:
 //   Miguel de Icaza (miguel@ximian.com)
 //
-// (C) 2001 Ximian, Inc (http://www.ximian.com)
+// Copyright 2001 Ximian, Inc (http://www.ximian.com)
+// Copyright 2003-2008 Novell, Inc
 //
 
 using System;
@@ -29,12 +30,16 @@ namespace Mono.CSharp {
 
                Parameter.Modifier ParameterModifier (int pos);
                string GetSignatureForError ();
+
+#if MS_COMPATIBLE
+               ParameterData InflateTypes (Type[] genArguments, Type[] argTypes);
+#endif
        }
 
        public class ReflectionParameters : ParameterData {
                ParameterInfo [] pi;
                Type [] types;
-               int params_idx = -1;
+               int params_idx = int.MaxValue;
                bool is_varargs;
                bool is_extension;
                ParameterData gpd;
@@ -54,7 +59,7 @@ namespace Mono.CSharp {
 
                        types = new Type [count];
                        for (int i = 0; i < count; i++)
-                               types [i] = pi [i].ParameterType;
+                               types [i] = TypeManager.TypeToCoreType (pi [i].ParameterType);
 
                        // TODO: This (if) should be done one level higher to correctly use
                        // out caching facilities.
@@ -85,7 +90,7 @@ namespace Mono.CSharp {
 
                                if (pi [i].IsDefined (TypeManager.param_array_type, false)) {
                                        params_idx = i;
-                                       return;
+                                       break;
                                }
                        }
 
@@ -133,12 +138,53 @@ namespace Mono.CSharp {
                        return sb.ToString ();
                }
 
+#if MS_COMPATIBLE
+               public ParameterData InflateTypes (Type[] genArguments, Type[] argTypes)
+               {
+                       ReflectionParameters p = (ReflectionParameters)MemberwiseClone ();
+
+                       for (int i = 0; i < types.Length; ++i) {
+                               if (types[i].IsGenericParameter) {
+                                       for (int ii = 0; ii < genArguments.Length; ++ii) {
+                                               if (types[i] != genArguments[ii])
+                                                       continue;
+
+                                               p.types[i] = argTypes[ii];
+                                               break;
+                                       }
+                                       continue;
+                               }
+                               
+                               if (types[i].IsGenericType) {
+                                       Type[] gen_arguments_open = types[i].GetGenericTypeDefinition ().GetGenericArguments ();
+                                       Type[] gen_arguments = types[i].GetGenericArguments ();
+                                       for (int ii = 0; ii < gen_arguments_open.Length; ++ii) {
+                                               if (gen_arguments [ii].IsGenericParameter) {
+                                                       for (int iii = 0; iii < genArguments.Length; ++iii) {
+                                                               if (gen_arguments [ii] != genArguments [iii])
+                                                                       continue;
+
+                                                               gen_arguments_open [ii] = argTypes [iii];
+                                                               break;
+                                                       }
+                                               } else {
+                                                       gen_arguments_open [ii] = gen_arguments [ii];
+                                               }
+                                       }
+
+                                       p.types[i] = types[i].GetGenericTypeDefinition ().MakeGenericType (gen_arguments_open);
+                               }
+                       }
+                       return p;
+               }
+#endif
+
                public Type ParameterType (int pos)
                {
                        if (is_varargs && pos >= pi.Length)
                                return TypeManager.runtime_argument_handle_type;
 
-                       return pi [pos].ParameterType;
+                       return types [pos];
                }
 
                public string ParameterName (int pos)
@@ -174,7 +220,7 @@ namespace Mono.CSharp {
                        if (params_idx == pos)
                                sb.Append ("params ");
 
-                       if (ExtensionMethodType != null)
+                       if (pos == 0 && ExtensionMethodType != null)
                                sb.Append ("this ");
 
                        sb.Append (TypeManager.CSharpName (partype).Replace ("&", ""));
@@ -184,15 +230,15 @@ namespace Mono.CSharp {
 
                public Parameter.Modifier ParameterModifier (int pos)
                {
-                       if (pos == params_idx)
+                       if (pos >= params_idx)
                                return Parameter.Modifier.PARAMS;
-                       else if (is_varargs && pos >= pi.Length)
+                       if (is_varargs && pos >= pi.Length)
                                return Parameter.Modifier.ARGLIST;
 
                        if (gpd != null)
                                return gpd.ParameterModifier (pos);
 
-                       Type t = pi [pos].ParameterType;
+                       Type t = types [pos];
                        if (t.IsByRef){
                                if ((pi [pos].Attributes & (ParameterAttributes.Out|ParameterAttributes.In)) == ParameterAttributes.Out)
                                        return Parameter.Modifier.OUT;
@@ -217,7 +263,7 @@ namespace Mono.CSharp {
                }
 
                public bool HasParams {
-                       get { return params_idx != -1; }
+                       get { return params_idx != int.MaxValue; }
                }
 
                public Type[] Types {
@@ -304,6 +350,20 @@ namespace Mono.CSharp {
                {
                        comparer = PtrComparer.Instance;
                }
+
+#if MS_COMPATIBLE
+               //
+               // Workaround System.InvalidOperationException for enums
+               //
+               protected override int GetHash (object key)
+               {
+                       TypeBuilder tb = key as TypeBuilder;
+                       if (tb != null && tb.BaseType == TypeManager.enum_type)
+                               key = tb.BaseType;
+
+                       return base.GetHash (key);
+               }
+#endif
        }
 
        /*
@@ -380,28 +440,28 @@ namespace Mono.CSharp {
        /// </summary>
        public class SeekableStreamReader
        {
-               public SeekableStreamReader (TextReader reader)
-               {
-                       this.reader = reader;
-                       this.buffer = new char [AverageReadLength * 3];
-
-                       // Let the StreamWriter autodetect the encoder
-                       reader.Peek ();
-               }
-
-               public SeekableStreamReader (Stream stream, Encoding encoding)
-                       : this (new StreamReader (stream, encoding, true))
-               { }
-
+               const int AverageReadLength = 1024;
                TextReader reader;
-
-               private const int AverageReadLength = 1024;
+               Stream stream;
+               Encoding encoding;
 
                char[] buffer;
                int buffer_start;       // in chars
                int char_count;         // count buffer[] valid characters
                int pos;                // index into buffer[]
 
+               public SeekableStreamReader (Stream stream, Encoding encoding)
+               {
+                       this.stream = stream;
+                       this.encoding = encoding;
+                       
+                       this.reader = new StreamReader (stream, encoding, true);
+                       this.buffer = new char [AverageReadLength * 3];
+
+                       // Let the StreamWriter autodetect the encoder
+                       reader.Peek ();
+               }
+
                /// <remarks>
                ///   This value corresponds to the current position in a stream of characters.
                ///   The StreamReader hides its manipulation of the underlying byte stream and all
@@ -413,8 +473,25 @@ namespace Mono.CSharp {
                        get { return buffer_start + pos; }
 
                        set {
-                               if (value < buffer_start || value > buffer_start + char_count)
-                                       throw new InternalErrorException ("can't seek that far back: " + (pos - value));
+                               if (value > buffer_start + char_count)
+                                       throw new InternalErrorException ("can't seek that far forward: " + (pos - value));
+                               
+                               if (value < buffer_start){
+                                       // Reinitialize.
+                                       stream.Position = 0;
+                                       reader = new StreamReader (stream, encoding, true);
+                                       buffer_start = 0;
+                                       char_count = 0;
+                                       pos = 0;
+                                       Peek ();
+
+                                       while (value > buffer_start + char_count){
+                                               pos = char_count+1;
+                                               Peek ();
+                                       }
+                                       pos = value - buffer_start;
+                               }
+
                                pos = value - buffer_start;
                        }
                }
@@ -536,4 +613,89 @@ namespace Mono.CSharp {
                        }
                }
        }
+
+       class PartialMethodDefinitionInfo : MethodInfo
+       {
+               MethodOrOperator mc;
+               MethodAttributes attrs;
+
+               public PartialMethodDefinitionInfo (MethodOrOperator mc)
+               {
+                       this.mc = mc;
+                       if ((mc.ModFlags & Modifiers.STATIC) != 0)
+                               attrs = MethodAttributes.Static;
+               }
+
+               public override MethodInfo GetBaseDefinition ()
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override ICustomAttributeProvider ReturnTypeCustomAttributes
+               {
+                       get { throw new NotImplementedException (); }
+               }
+
+               public override MethodAttributes Attributes
+               {
+                       get { return attrs; }
+               }
+
+               public override MethodImplAttributes GetMethodImplementationFlags ()
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override ParameterInfo [] GetParameters ()
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override object Invoke (object obj, BindingFlags invokeAttr, Binder binder, object [] parameters, CultureInfo culture)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override RuntimeMethodHandle MethodHandle
+               {
+                       get { throw new NotImplementedException (); }
+               }
+
+               public override Type DeclaringType
+               {
+                       get { return mc.Parent.TypeBuilder; }
+               }
+
+               public override object [] GetCustomAttributes (Type attributeType, bool inherit)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override object [] GetCustomAttributes (bool inherit)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override Type ReturnType {
+                       get {
+                               return mc.MemberType;
+                       }
+               }
+
+               public override bool IsDefined (Type attributeType, bool inherit)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override string Name
+               {
+                       get { return mc.Name; }
+               }
+
+               public override Type ReflectedType
+               {
+                       get { throw new NotImplementedException (); }
+               }
+       }
+
 }