2005-06-04 Martin Baulig <martin@ximian.com>
authorMartin Baulig <martin@novell.com>
Tue, 28 Jun 2005 14:21:28 +0000 (14:21 -0000)
committerMartin Baulig <martin@novell.com>
Tue, 28 Jun 2005 14:21:28 +0000 (14:21 -0000)
**** Merged r45395 from GCS ****

The big Foreach rewrite - Part II.

* typemanager.cs (TypeManager.object_getcurrent_void): Replaced
with `PropertyInfo ienumerator_getcurrent'.

* codegen.cs (VariableStorage): Removed.

* statement.cs
(Foreach): Derive from Statement, not ExceptionStatement.
(Foreach.CollectionForeach): New nested class.  Moved all the code
dealing with collection foreach here.
(Foreach.ForeachHelperMethods): Removed.
(Foreach.TemporaryVariable): Implement IMemoryLocation.

svn path=/trunk/mcs/; revision=46648

mcs/mcs/ChangeLog
mcs/mcs/codegen.cs
mcs/mcs/statement.cs
mcs/mcs/typemanager.cs

index 563944a9671f049d975d309b854a4a936e80a2a5..bf67a55502aed9d26298726d888c16141d826878 100644 (file)
@@ -1,3 +1,21 @@
+2005-06-04  Martin Baulig  <martin@ximian.com>
+
+       **** Merged r45395 from GCS ****
+
+       The big Foreach rewrite - Part II.
+
+       * typemanager.cs (TypeManager.object_getcurrent_void): Replaced
+       with `PropertyInfo ienumerator_getcurrent'.
+
+       * codegen.cs (VariableStorage): Removed.
+
+       * statement.cs
+       (Foreach): Derive from Statement, not ExceptionStatement.
+       (Foreach.CollectionForeach): New nested class.  Moved all the code
+       dealing with collection foreach here.
+       (Foreach.ForeachHelperMethods): Removed.
+       (Foreach.TemporaryVariable): Implement IMemoryLocation.
+
 2005-05-23  Martin Baulig  <martin@ximian.com>
 
        * statement.cs (Try.DoResolve): Don't create a `finally' if we
index 4aed84d4095c5049a65e6cb0358fd51886ddf962..3365beaeaa5e77fa751f8daf943acf0f2e1842ac 100644 (file)
@@ -183,85 +183,6 @@ namespace Mono.CSharp {
                }
        }
 
-       //
-       // Provides "local" store across code that can yield: locals
-       // or fields, notice that this should not be used by anonymous
-       // methods to create local storage, those only require
-       // variable mapping.
-       //
-       public class VariableStorage {
-               FieldBuilder fb;
-               LocalBuilder local;
-               
-               static int count;
-               
-               public VariableStorage (EmitContext ec, Type t)
-               {
-                       count++;
-                       if (ec.InIterator)
-                               fb = ec.CurrentIterator.MapVariable ("s_", count.ToString (), t);
-                       else
-                               local = ec.ig.DeclareLocal (t);
-               }
-
-               public void EmitThis (ILGenerator ig)
-               {
-                       if (fb != null)
-                               ig.Emit (OpCodes.Ldarg_0);
-               }
-
-               public void EmitStore (ILGenerator ig)
-               {
-                       if (fb == null)
-                               ig.Emit (OpCodes.Stloc, local);
-                       else
-                               ig.Emit (OpCodes.Stfld, fb);
-               }
-
-               public void EmitLoad (ILGenerator ig)
-               {
-                       if (fb == null)
-                               ig.Emit (OpCodes.Ldloc, local);
-                       else 
-                               ig.Emit (OpCodes.Ldfld, fb);
-               }
-
-               public void EmitLoadAddress (ILGenerator ig)
-               {
-                       if (fb == null)
-                               ig.Emit (OpCodes.Ldloca, local);
-                       else 
-                               ig.Emit (OpCodes.Ldflda, fb);
-               }
-               
-               public void EmitCall (ILGenerator ig, MethodInfo mi)
-               {
-                       // FIXME : we should handle a call like tostring
-                       // here, where boxing is needed. However, we will
-                       // never encounter that with the current usage.
-                       
-                       bool value_type_call;
-                       EmitThis (ig);
-                       if (fb == null) {
-                               value_type_call = local.LocalType.IsValueType;
-                               
-                               if (value_type_call)
-                                       ig.Emit (OpCodes.Ldloca, local);
-                               else
-                                       ig.Emit (OpCodes.Ldloc, local);
-                       } else {
-                               value_type_call = fb.FieldType.IsValueType;
-                               
-                               if (value_type_call)
-                                       ig.Emit (OpCodes.Ldflda, fb);
-                               else
-                                       ig.Emit (OpCodes.Ldfld, fb);
-                       }
-                       
-                       ig.Emit (value_type_call ? OpCodes.Call : OpCodes.Callvirt, mi);
-               }
-       }
-
        /// <summary>
        ///   An Emit Context is created for each body of code (from methods,
        ///   properties bodies, indexer bodies or constructor bodies)
index 04ec680356bd52c57a3804e69d9be88adde088f5..72d5f4fec183d0548364d6485026e39462ce841a 100644 (file)
@@ -2209,9 +2209,6 @@ namespace Mono.CSharp {
                //
                public bool IsParameterReference (string name)
                {
-                       Parameter par;
-                       int idx;
-
                        for (ToplevelBlock t = this; t != null; t = t.Container) {
                                if (t.IsLocalParameter (name))
                                        return true;
@@ -4133,17 +4130,13 @@ namespace Mono.CSharp {
        /// <summary>
        ///   Implementation of the foreach C# statement
        /// </summary>
-       public class Foreach : ExceptionStatement {
+       public class Foreach : Statement {
                Expression type;
                Expression variable;
                Expression expr;
                Statement statement;
-               ForeachHelperMethods hm;
-               Expression empty, conv;
-               Type array_type, element_type;
-               Type var_type;
-               VariableStorage enumerator;
                ArrayForeach array;
+               CollectionForeach collection;
                
                public Foreach (Expression type, LocalVariableReference var, Expression expr,
                                Statement stmt, Location l)
@@ -4170,8 +4163,8 @@ namespace Mono.CSharp {
                        if (texpr == null)
                                return false;
 
-                       var_type = texpr.ResolveType (ec);
-                       
+                       Type var_type = texpr.Type;
+
                        //
                        // We need an instance variable.  Not sure this is the best
                        // way of doing this.
@@ -4181,473 +4174,17 @@ namespace Mono.CSharp {
                        //
                        if (!(expr.eclass == ExprClass.Variable || expr.eclass == ExprClass.Value ||
                              expr.eclass == ExprClass.PropertyAccess || expr.eclass == ExprClass.IndexerAccess)){
-                               error1579 (expr.Type);
+                               CollectionForeach.error1579 (expr.Type, loc);
                                return false;
                        }
 
                        if (expr.Type.IsArray) {
-                               array_type = expr.Type;
-                               element_type = TypeManager.GetElementType (array_type);
-
-                               empty = new EmptyExpression (element_type);
-
-                               array = new ArrayForeach (type, variable, expr, statement, loc);
+                               array = new ArrayForeach (var_type, variable, expr, statement, loc);
                                return array.Resolve (ec);
                        } else {
-                               hm = ProbeCollectionType (ec, expr.Type);
-                               if (hm == null){
-                                       error1579 (expr.Type);
-                                       return false;
-                               }
-
-                               // When ProbeCollection reported error
-                               if (hm.move_next == null)
-                                       return false;
-       
-                               array_type = expr.Type;
-                               element_type = hm.element_type;
-
-                               empty = new EmptyExpression (hm.element_type);
-                       }
-
-                       bool ok = true;
-
-                       ec.StartFlowBranching (FlowBranching.BranchingType.Loop, loc);
-                       ec.CurrentBranching.CreateSibling ();
-
-                       //
-                       //
-                       // FIXME: maybe we can apply the same trick we do in the
-                       // array handling to avoid creating empty and conv in some cases.
-                       //
-                       // Although it is not as important in this case, as the type
-                       // will not likely be object (what the enumerator will return).
-                       //
-                       conv = Convert.ExplicitConversion (ec, empty, var_type, loc);
-                       if (conv == null)
-                               ok = false;
-
-                       variable = variable.ResolveLValue (ec, empty, loc);
-                       if (variable == null)
-                               ok = false;
-
-                       bool disposable = (hm != null) && hm.is_disposable;
-                       FlowBranchingException branching = null;
-                       if (disposable)
-                               branching = ec.StartFlowBranching (this);
-
-                       if (!statement.Resolve (ec))
-                               ok = false;
-
-                       if (disposable) {
-                               ResolveFinally (branching);
-                               ec.EndFlowBranching ();
-                       } else
-                               emit_finally = true;
-
-                       ec.EndFlowBranching ();
-
-                       return ok;
-               }
-               
-               //
-               // Retrieves a `public bool MoveNext ()' method from the Type `t'
-               //
-               static MethodInfo FetchMethodMoveNext (Type t)
-               {
-                       MemberList move_next_list;
-                       
-                       move_next_list = TypeContainer.FindMembers (
-                               t, MemberTypes.Method,
-                               BindingFlags.Public | BindingFlags.Instance,
-                               Type.FilterName, "MoveNext");
-                       if (move_next_list.Count == 0)
-                               return null;
-
-                       foreach (MemberInfo m in move_next_list){
-                               MethodInfo mi = (MethodInfo) m;
-                               Type [] args;
-                               
-                               args = TypeManager.GetArgumentTypes (mi);
-                               if (args != null && args.Length == 0){
-                                       if (mi.ReturnType == TypeManager.bool_type)
-                                               return mi;
-                               }
-                       }
-                       return null;
-               }
-               
-               //
-               // Retrieves a `public T get_Current ()' method from the Type `t'
-               //
-               static MethodInfo FetchMethodGetCurrent (Type t)
-               {
-                       MemberList get_current_list;
-
-                       get_current_list = TypeContainer.FindMembers (
-                               t, MemberTypes.Method,
-                               BindingFlags.Public | BindingFlags.Instance,
-                               Type.FilterName, "get_Current");
-                       if (get_current_list.Count == 0)
-                               return null;
-
-                       foreach (MemberInfo m in get_current_list){
-                               MethodInfo mi = (MethodInfo) m;
-                               Type [] args;
-
-                               args = TypeManager.GetArgumentTypes (mi);
-                               if (args != null && args.Length == 0)
-                                       return mi;
-                       }
-                       return null;
-               }
-
-               //
-               // Retrieves a `public void Dispose ()' method from the Type `t'
-               //
-               static MethodInfo FetchMethodDispose (Type t)
-               {
-                       MemberList dispose_list;
-                       
-                       dispose_list = TypeContainer.FindMembers (
-                               t, MemberTypes.Method,
-                               BindingFlags.Public | BindingFlags.Instance,
-                               Type.FilterName, "Dispose");
-                       if (dispose_list.Count == 0)
-                               return null;
-
-                       foreach (MemberInfo m in dispose_list){
-                               MethodInfo mi = (MethodInfo) m;
-                               Type [] args;
-                               
-                               args = TypeManager.GetArgumentTypes (mi);
-                               if (args != null && args.Length == 0){
-                                       if (mi.ReturnType == TypeManager.void_type)
-                                               return mi;
-                               }
-                       }
-                       return null;
-               }
-
-               // 
-               // This struct records the helper methods used by the Foreach construct
-               //
-               class ForeachHelperMethods {
-                       public EmitContext ec;
-                       public MethodInfo get_enumerator;
-                       public MethodInfo move_next;
-                       public MethodInfo get_current;
-                       public Type element_type;
-                       public Type enumerator_type;
-                       public bool is_disposable;
-                       public readonly Location Location;
-
-                       public ForeachHelperMethods (EmitContext ec, Location loc)
-                       {
-                               this.ec = ec;
-                               this.Location = loc;
-                               this.element_type = TypeManager.object_type;
-                               this.enumerator_type = TypeManager.ienumerator_type;
-                               this.is_disposable = true;
-                       }
-               }
-               
-               static bool GetEnumeratorFilter (MemberInfo m, object criteria)
-               {
-                       if (m == null)
-                               return false;
-                       
-                       if (!(m is MethodInfo))
-                               return false;
-                       
-                       if (m.Name != "GetEnumerator")
-                               return false;
-
-                       MethodInfo mi = (MethodInfo) m;
-                       Type [] args = TypeManager.GetArgumentTypes (mi);
-                       if (args != null){
-                               if (args.Length != 0)
-                                       return false;
-                       }
-                       ForeachHelperMethods hm = (ForeachHelperMethods) criteria;
-
-                       // Check whether GetEnumerator is public
-                       if ((mi.Attributes & MethodAttributes.Public) != MethodAttributes.Public)
-                               return false;
-
-                       if ((mi.ReturnType == TypeManager.ienumerator_type) && (mi.DeclaringType == TypeManager.string_type))
-                               //
-                               // Apply the same optimization as MS: skip the GetEnumerator
-                               // returning an IEnumerator, and use the one returning a 
-                               // CharEnumerator instead. This allows us to avoid the 
-                               // try-finally block and the boxing.
-                               //
-                               return false;
-
-                       //
-                       // Ok, we can access it, now make sure that we can do something
-                       // with this `GetEnumerator'
-                       //
-                       
-                       Type return_type = mi.ReturnType;
-                       if (mi.ReturnType == TypeManager.ienumerator_type ||
-                           TypeManager.ienumerator_type.IsAssignableFrom (return_type) ||
-                           (!RootContext.StdLib && TypeManager.ImplementsInterface (return_type, TypeManager.ienumerator_type))) {
-                               
-                               //
-                               // If it is not an interface, lets try to find the methods ourselves.
-                               // For example, if we have:
-                               // public class Foo : IEnumerator { public bool MoveNext () {} public int Current { get {}}}
-                               // We can avoid the iface call. This is a runtime perf boost.
-                               // even bigger if we have a ValueType, because we avoid the cost
-                               // of boxing.
-                               //
-                               // We have to make sure that both methods exist for us to take
-                               // this path. If one of the methods does not exist, we will just
-                               // use the interface. Sadly, this complex if statement is the only
-                               // way I could do this without a goto
-                               //
-                               
-                               if (return_type.IsInterface ||
-                                   (hm.move_next = FetchMethodMoveNext (return_type)) == null ||
-                                   (hm.get_current = FetchMethodGetCurrent (return_type)) == null) {
-                                       
-                                       hm.move_next = TypeManager.bool_movenext_void;
-                                       hm.get_current = TypeManager.object_getcurrent_void;
-                                       return true;    
-                               }
-
-                       } else {
-
-                               if (return_type.IsPointer || return_type.IsArray) {
-                                       Report.SymbolRelatedToPreviousError (mi);
-                                       Type t = return_type.GetElementType ();
-                                       Report.SymbolRelatedToPreviousError (t);
-                                       Report.Error (202, hm.Location, "foreach requires that the return type '{0}' of '{1}' must have a suitable public MoveNext method and public Current property", 
-                                               TypeManager.CSharpName (return_type), TypeManager.GetFullNameSignature (m));
-                                       hm.get_enumerator = mi;
-                                       return false;
-                               }
-
-                               //
-                               // Ok, so they dont return an IEnumerable, we will have to
-                               // find if they support the GetEnumerator pattern.
-                               //
-                               
-                               hm.move_next = FetchMethodMoveNext (return_type);
-                               if (hm.move_next == null)
-                                       return false;
-                               
-                               hm.get_current = FetchMethodGetCurrent (return_type);
-                               if (hm.get_current == null)
-                                       return false;
-                       }
-                       
-                       hm.element_type = hm.get_current.ReturnType;
-                       hm.enumerator_type = return_type;
-                       hm.is_disposable = !hm.enumerator_type.IsSealed ||
-                               TypeManager.ImplementsInterface (
-                                       hm.enumerator_type, TypeManager.idisposable_type);
-
-                       return true;
-               }
-               
-               /// <summary>
-               ///   This filter is used to find the GetEnumerator method
-               ///   on which IEnumerator operates
-               /// </summary>
-               static MemberFilter FilterEnumerator;
-               
-               static Foreach ()
-               {
-                       FilterEnumerator = new MemberFilter (GetEnumeratorFilter);
-               }
-
-                void error1579 (Type t)
-                {
-                        Report.Error (1579, loc,
-                                      "foreach statement cannot operate on variables of type `" +
-                                      t.FullName + "' because that class does not provide a " +
-                                      " GetEnumerator method or it is inaccessible");
-                }
-
-               static bool TryType (Type t, ForeachHelperMethods hm)
-               {
-                       MemberList mi;
-                       
-                       mi = TypeContainer.FindMembers (t, MemberTypes.Method,
-                                                       BindingFlags.Public | BindingFlags.NonPublic |
-                                                       BindingFlags.Instance | BindingFlags.DeclaredOnly,
-                                                       FilterEnumerator, hm);
-
-                       if (mi.Count == 0)
-                               return false;
-
-                       hm.get_enumerator = (MethodInfo) mi [0];
-                       return true;    
-               }
-               
-               //
-               // Looks for a usable GetEnumerator in the Type, and if found returns
-               // the three methods that participate: GetEnumerator, MoveNext and get_Current
-               //
-               ForeachHelperMethods ProbeCollectionType (EmitContext ec, Type t)
-               {
-                       ForeachHelperMethods hm = new ForeachHelperMethods (ec, loc);
-
-                       for (Type tt = t; tt != null && tt != TypeManager.object_type;){
-                               if (TryType (tt, hm))
-                                       return hm;
-                               tt = tt.BaseType;
-                       }
-
-                       //
-                       // Now try to find the method in the interfaces
-                       //
-                       while (t != null){
-                               Type [] ifaces = t.GetInterfaces ();
-
-                               foreach (Type i in ifaces){
-                                       if (TryType (i, hm))
-                                               return hm;
-                               }
-                               
-                               //
-                               // Since TypeBuilder.GetInterfaces only returns the interface
-                               // types for this type, we have to keep looping, but once
-                               // we hit a non-TypeBuilder (ie, a Type), then we know we are
-                               // done, because it returns all the types
-                               //
-                               if ((t is TypeBuilder))
-                                       t = t.BaseType;
-                               else
-                                       break;
-                       } 
-
-                       return null;
-               }
-
-               //
-               // FIXME: possible optimization.
-               // We might be able to avoid creating `empty' if the type is the sam
-               //
-               bool EmitCollectionForeach (EmitContext ec)
-               {
-                       ILGenerator ig = ec.ig;
-
-                       enumerator = new VariableStorage (ec, hm.enumerator_type);
-                       enumerator.EmitThis (ig);
-                       //
-                       // Instantiate the enumerator
-                       //
-                       if (expr.Type.IsValueType) {
-                               IMemoryLocation ml = expr as IMemoryLocation;
-                               // Load the address of the value type.
-                               if (ml == null) {
-                                       // This happens if, for example, you have a property
-                                       // returning a struct which is IEnumerable
-                                       LocalBuilder t = ec.GetTemporaryLocal (expr.Type);
-                                       expr.Emit (ec);
-                                       ig.Emit (OpCodes.Stloc, t);
-                                       ig.Emit (OpCodes.Ldloca, t);
-                                       ec.FreeTemporaryLocal (t, expr.Type);
-                               } else {
-                                       ml.AddressOf (ec, AddressOp.Load);
-                               }
-                               
-                               // Emit the call.
-                               if (hm.get_enumerator.DeclaringType.IsValueType) {
-                                       // the method is declared on the value type
-                                       ig.Emit (OpCodes.Call, hm.get_enumerator);
-                               } else {
-                                       // it is an interface method, so we must box
-                                       ig.Emit (OpCodes.Box, expr.Type);
-                                       ig.Emit (OpCodes.Callvirt, hm.get_enumerator);
-                               }
-                       } else {
-                               expr.Emit (ec);
-                               ig.Emit (OpCodes.Callvirt, hm.get_enumerator);
-                       }
-                       enumerator.EmitStore (ig);
-
-                       //
-                       // Protect the code in a try/finalize block, so that
-                       // if the beast implement IDisposable, we get rid of it
-                       //
-                       if (hm.is_disposable && emit_finally)
-                               ig.BeginExceptionBlock ();
-                       
-                       Label end_try = ig.DefineLabel ();
-                       
-                       ig.MarkLabel (ec.LoopBegin);
-                       
-                       enumerator.EmitCall (ig, hm.move_next);
-                       
-                       ig.Emit (OpCodes.Brfalse, end_try);
-
-                       if (ec.InIterator)
-                               ig.Emit (OpCodes.Ldarg_0);
-                       
-                       enumerator.EmitCall (ig, hm.get_current);
-
-                       if (ec.InIterator){
-                               conv.Emit (ec);
-                               ig.Emit (OpCodes.Stfld, ((LocalVariableReference) variable).local_info.FieldBuilder);
-                       } else 
-                               ((IAssignMethod)variable).EmitAssign (ec, conv, false, false);
-                               
-                       statement.Emit (ec);
-                       ig.Emit (OpCodes.Br, ec.LoopBegin);
-                       ig.MarkLabel (end_try);
-                       
-                       //
-                       // Now the finally block
-                       //
-                       if (hm.is_disposable) {
-                               DoEmitFinally (ec);
-                               if (emit_finally)
-                                       ig.EndExceptionBlock ();
-                       }
-
-                       ig.MarkLabel (ec.LoopEnd);
-                       return false;
-               }
-
-               public override void EmitFinally (EmitContext ec)
-               {
-                       ILGenerator ig = ec.ig;
-
-                       if (hm.enumerator_type.IsValueType) {
-                               enumerator.EmitThis (ig);
-
-                               MethodInfo mi = FetchMethodDispose (hm.enumerator_type);
-                               if (mi != null) {
-                                       enumerator.EmitLoadAddress (ig);
-                                       ig.Emit (OpCodes.Call, mi);
-                               } else {
-                                       enumerator.EmitLoad (ig);
-                                       ig.Emit (OpCodes.Box, hm.enumerator_type);
-                                       ig.Emit (OpCodes.Callvirt, TypeManager.void_dispose_void);
-                               }
-                       } else {
-                               Label call_dispose = ig.DefineLabel ();
-
-                               enumerator.EmitThis (ig);
-                               enumerator.EmitLoad (ig);
-                               ig.Emit (OpCodes.Isinst, TypeManager.idisposable_type);
-                               ig.Emit (OpCodes.Dup);
-                               ig.Emit (OpCodes.Brtrue_S, call_dispose);
-                               ig.Emit (OpCodes.Pop);
-
-                               Label end_finally = ig.DefineLabel ();
-                               ig.Emit (OpCodes.Br, end_finally);
-
-                               ig.MarkLabel (call_dispose);
-                               ig.Emit (OpCodes.Callvirt, TypeManager.void_dispose_void);
-                               ig.MarkLabel (end_finally);
-
-                               if (emit_finally)
-                                       ig.Emit (OpCodes.Endfinally);
+                               collection = new CollectionForeach (
+                                       var_type, variable, expr, statement, loc);
+                               return collection.Resolve (ec);
                        }
                }
 
@@ -4658,9 +4195,9 @@ namespace Mono.CSharp {
                        Label old_begin = ec.LoopBegin, old_end = ec.LoopEnd;
                        ec.LoopBegin = ig.DefineLabel ();
                        ec.LoopEnd = ig.DefineLabel ();
-                       
-                       if (hm != null)
-                               EmitCollectionForeach (ec);
+
+                       if (collection != null)
+                               collection.Emit (ec);
                        else
                                array.Emit (ec);
                        
@@ -4668,7 +4205,7 @@ namespace Mono.CSharp {
                        ec.LoopEnd = old_end;
                }
 
-               protected class TemporaryVariable : Expression
+               protected class TemporaryVariable : Expression, IMemoryLocation
                {
                        FieldBuilder fb;
                        LocalBuilder local;
@@ -4706,6 +4243,18 @@ namespace Mono.CSharp {
                                }
                        }
 
+                       public void EmitLoadAddress (EmitContext ec)
+                       {
+                               ILGenerator ig = ec.ig;
+
+                               if (fb != null) {
+                                       ig.Emit (OpCodes.Ldarg_0);
+                                       ig.Emit (OpCodes.Ldflda, fb);
+                               } else {
+                                       ig.Emit (OpCodes.Ldloca, local);
+                               }
+                       }
+
                        public void Store (EmitContext ec, Expression right_side)
                        {
                                if (fb != null)
@@ -4730,6 +4279,11 @@ namespace Mono.CSharp {
                                else
                                        ig.Emit (OpCodes.Stfld, fb);
                        }
+
+                       public void AddressOf (EmitContext ec, AddressOp mode)
+                       {
+                               EmitLoadAddress (ec);
+                       }
                }
 
                protected class ArrayCounter : TemporaryVariable
@@ -4757,7 +4311,7 @@ namespace Mono.CSharp {
 
                protected class ArrayForeach : Statement
                {
-                       Expression type, variable, expr, conv;
+                       Expression variable, expr, conv;
                        Statement statement;
                        Type array_type, element_type;
                        Type var_type;
@@ -4768,10 +4322,10 @@ namespace Mono.CSharp {
                        TemporaryVariable copy;
                        Expression access;
 
-                       public ArrayForeach (Expression type, Expression var,
+                       public ArrayForeach (Type var_type, Expression var,
                                             Expression expr, Statement stmt, Location l)
                        {
-                               this.type = type;
+                               this.var_type = var_type;
                                this.variable = var;
                                this.expr = expr;
                                statement = stmt;
@@ -4780,12 +4334,6 @@ namespace Mono.CSharp {
 
                        public override bool Resolve (EmitContext ec)
                        {                               
-                               TypeExpr texpr = type.ResolveAsTypeTerminal (ec, false);
-                               if (texpr == null)
-                                       return false;
-                       
-                               var_type = texpr.Type;
-
                                array_type = expr.Type;
                                element_type = TypeManager.GetElementType (array_type);
                                rank = array_type.GetArrayRank ();
@@ -4875,5 +4423,413 @@ namespace Mono.CSharp {
                                ig.MarkLabel (ec.LoopEnd);
                        }
                }
+
+               protected class CollectionForeach : ExceptionStatement
+               {
+                       Expression variable, expr;
+                       Statement statement;
+
+                       TemporaryVariable enumerator;
+                       Expression init;
+                       Statement loop;
+
+                       MethodGroupExpr get_enumerator;
+                       PropertyExpr get_current;
+                       MethodInfo move_next;
+                       Type var_type, enumerator_type;
+                       bool is_disposable;
+
+                       public CollectionForeach (Type var_type, Expression var,
+                                                 Expression expr, Statement stmt, Location l)
+                       {
+                               this.var_type = var_type;
+                               this.variable = var;
+                               this.expr = expr;
+                               statement = stmt;
+                               loc = l;
+                       }
+
+                       bool GetEnumeratorFilter (EmitContext ec, MethodInfo mi)
+                       {
+                               Type [] args = TypeManager.GetArgumentTypes (mi);
+                               if (args != null){
+                                       if (args.Length != 0)
+                                               return false;
+                               }
+
+                               if (TypeManager.IsOverride (mi))
+                                       return false;
+                       
+                               // Check whether GetEnumerator is public
+                               if ((mi.Attributes & MethodAttributes.Public) != MethodAttributes.Public)
+                                       return false;
+
+                               if ((mi.ReturnType == TypeManager.ienumerator_type) && (mi.DeclaringType == TypeManager.string_type))
+                                       //
+                                       // Apply the same optimization as MS: skip the GetEnumerator
+                                       // returning an IEnumerator, and use the one returning a 
+                                       // CharEnumerator instead. This allows us to avoid the 
+                                       // try-finally block and the boxing.
+                                       //
+                                       return false;
+
+                               //
+                               // Ok, we can access it, now make sure that we can do something
+                               // with this `GetEnumerator'
+                               //
+
+                               Type return_type = mi.ReturnType;
+                               if (mi.ReturnType == TypeManager.ienumerator_type ||
+                                   TypeManager.ienumerator_type.IsAssignableFrom (return_type) ||
+                                   (!RootContext.StdLib && TypeManager.ImplementsInterface (return_type, TypeManager.ienumerator_type))) {
+                                       //
+                                       // If it is not an interface, lets try to find the methods ourselves.
+                                       // For example, if we have:
+                                       // public class Foo : IEnumerator { public bool MoveNext () {} public int Current { get {}}}
+                                       // We can avoid the iface call. This is a runtime perf boost.
+                                       // even bigger if we have a ValueType, because we avoid the cost
+                                       // of boxing.
+                                       //
+                                       // We have to make sure that both methods exist for us to take
+                                       // this path. If one of the methods does not exist, we will just
+                                       // use the interface. Sadly, this complex if statement is the only
+                                       // way I could do this without a goto
+                                       //
+
+                                       if (return_type.IsInterface ||
+                                           !FetchMoveNext (ec, return_type) ||
+                                           !FetchGetCurrent (ec, return_type)) {
+                                               move_next = TypeManager.bool_movenext_void;
+                                               get_current = new PropertyExpr (
+                                                       ec, TypeManager.ienumerator_getcurrent, loc);
+                                               return true;
+                                       }
+                               } else {
+                                       //
+                                       // Ok, so they dont return an IEnumerable, we will have to
+                                       // find if they support the GetEnumerator pattern.
+                                       //
+
+                                       if (!FetchMoveNext (ec, return_type))
+                                               return false;
+
+                                       if (!FetchGetCurrent (ec, return_type))
+                                               return false;
+                               }
+
+                               enumerator_type = return_type;
+                               is_disposable = !enumerator_type.IsSealed ||
+                                       TypeManager.ImplementsInterface (
+                                               enumerator_type, TypeManager.idisposable_type);
+
+                               return true;
+                       }
+
+                       //
+                       // Retrieves a `public bool MoveNext ()' method from the Type `t'
+                       //
+                       bool FetchMoveNext (EmitContext ec, Type t)
+                       {
+                               MemberList move_next_list;
+
+                               move_next_list = TypeContainer.FindMembers (
+                                       t, MemberTypes.Method,
+                                       BindingFlags.Public | BindingFlags.Instance,
+                                       Type.FilterName, "MoveNext");
+                               if (move_next_list.Count == 0)
+                                       return false;
+
+                               foreach (MemberInfo m in move_next_list){
+                                       MethodInfo mi = (MethodInfo) m;
+                                       Type [] args;
+                               
+                                       args = TypeManager.GetArgumentTypes (mi);
+                                       if ((args != null) && (args.Length == 0) &&
+                                           TypeManager.TypeToCoreType (mi.ReturnType) == TypeManager.bool_type) {
+                                               move_next = mi;
+                                               return true;
+                                       }
+                               }
+
+                               return false;
+                       }
+               
+                       //
+                       // Retrieves a `public T get_Current ()' method from the Type `t'
+                       //
+                       bool FetchGetCurrent (EmitContext ec, Type t)
+                       {
+                               PropertyExpr pe = Expression.MemberLookup (
+                                       ec, t, "Current", MemberTypes.Property,
+                                       Expression.AllBindingFlags, loc) as PropertyExpr;
+                               if (pe == null)
+                                       return false;
+
+                               get_current = pe;
+                               return true;
+                       }
+
+                       // 
+                       // Retrieves a `public void Dispose ()' method from the Type `t'
+                       //
+                       static MethodInfo FetchMethodDispose (Type t)
+                       {
+                               MemberList dispose_list;
+
+                               dispose_list = TypeContainer.FindMembers (
+                                       t, MemberTypes.Method,
+                                       BindingFlags.Public | BindingFlags.Instance,
+                                       Type.FilterName, "Dispose");
+                               if (dispose_list.Count == 0)
+                                       return null;
+
+                               foreach (MemberInfo m in dispose_list){
+                                       MethodInfo mi = (MethodInfo) m;
+                                       Type [] args;
+
+                                       args = TypeManager.GetArgumentTypes (mi);
+                                       if (args != null && args.Length == 0){
+                                               if (mi.ReturnType == TypeManager.void_type)
+                                                       return mi;
+                                       }
+                               }
+                               return null;
+                       }
+
+                       static public void error1579 (Type t, Location loc)
+                       {
+                               Report.Error (1579, loc, "foreach statement cannot operate on " +
+                                             "variables of type `{0}' because that class does " +
+                                             "not provide a GetEnumerator method or it is " +
+                                             "inaccessible", t.FullName);
+                       }
+
+                       bool TryType (EmitContext ec, Type t)
+                       {
+                               MethodGroupExpr mg = Expression.MemberLookup (
+                                       ec, t, "GetEnumerator", MemberTypes.Method,
+                                       Expression.AllBindingFlags, loc) as MethodGroupExpr;
+                               if (mg == null)
+                                       return false;
+
+                               foreach (MethodBase mb in mg.Methods) {
+                                       if (!GetEnumeratorFilter (ec, (MethodInfo) mb))
+                                               continue;
+
+                                       MethodInfo[] mi = new MethodInfo[] { (MethodInfo) mb };
+                                       get_enumerator = new MethodGroupExpr (mi, loc);
+
+                                       if (t != expr.Type) {
+                                               expr = Convert.ExplicitConversion (
+                                                       ec, expr, t, loc);
+                                               if (expr == null)
+                                                       throw new InternalErrorException ();
+                                       }
+
+                                       get_enumerator.InstanceExpression = expr;
+                                       get_enumerator.IsBase = t != expr.Type;
+
+                                       return true;
+                               }
+
+                               return false;
+                       }               
+
+                       bool ProbeCollectionType (EmitContext ec, Type t)
+                       {
+                               for (Type tt = t; tt != null && tt != TypeManager.object_type;){
+                                       if (TryType (ec, tt))
+                                               return true;
+                                       tt = tt.BaseType;
+                               }
+
+                               //
+                               // Now try to find the method in the interfaces
+                               //
+                               while (t != null){
+                                       Type [] ifaces = t.GetInterfaces ();
+
+                                       foreach (Type i in ifaces){
+                                               if (TryType (ec, i))
+                                                       return true;
+                                       }
+                               
+                                       //
+                                       // Since TypeBuilder.GetInterfaces only returns the interface
+                                       // types for this type, we have to keep looping, but once
+                                       // we hit a non-TypeBuilder (ie, a Type), then we know we are
+                                       // done, because it returns all the types
+                                       //
+                                       if ((t is TypeBuilder))
+                                               t = t.BaseType;
+                                       else
+                                               break;
+                               }
+
+                               return false;
+                       }
+
+                       public override bool Resolve (EmitContext ec)
+                       {
+                               enumerator_type = TypeManager.ienumerator_type;
+                               is_disposable = true;
+
+                               if (!ProbeCollectionType (ec, expr.Type)) {
+                                       error1579 (expr.Type, loc);
+                                       return false;
+                               }
+
+                               enumerator = new TemporaryVariable (enumerator_type, loc);
+                               enumerator.Resolve (ec);
+
+                               init = new Invocation (get_enumerator, new ArrayList (), loc);
+                               init = init.Resolve (ec);
+                               if (init == null)
+                                       return false;
+
+                               Expression move_next_expr;
+                               {
+                                       MemberInfo[] mi = new MemberInfo[] { move_next };
+                                       MethodGroupExpr mg = new MethodGroupExpr (mi, loc);
+                                       mg.InstanceExpression = enumerator;
+
+                                       move_next_expr = new Invocation (mg, new ArrayList (), loc);
+                               }
+
+                               get_current.InstanceExpression = enumerator;
+
+                               Statement block = new CollectionForeachStatement (
+                                       var_type, variable, get_current, statement, loc);
+
+                               loop = new While (move_next_expr, block, loc);
+
+                               bool ok = true;
+
+                               ec.StartFlowBranching (FlowBranching.BranchingType.Loop, loc);
+                               ec.CurrentBranching.CreateSibling ();
+
+                               FlowBranchingException branching = null;
+                               if (is_disposable)
+                                       branching = ec.StartFlowBranching (this);
+
+                               if (!loop.Resolve (ec))
+                                       ok = false;
+
+                               if (is_disposable) {
+                                       ResolveFinally (branching);
+                                       ec.EndFlowBranching ();
+                               } else
+                                       emit_finally = true;
+
+                               ec.EndFlowBranching ();
+
+                               return ok;
+                       }
+
+                       protected override void DoEmit (EmitContext ec)
+                       {
+                               ILGenerator ig = ec.ig;
+
+                               enumerator.Store (ec, init);
+
+                               //
+                               // Protect the code in a try/finalize block, so that
+                               // if the beast implement IDisposable, we get rid of it
+                               //
+                               if (is_disposable && emit_finally)
+                                       ig.BeginExceptionBlock ();
+                       
+                               loop.Emit (ec);
+
+                               //
+                               // Now the finally block
+                               //
+                               if (is_disposable) {
+                                       DoEmitFinally (ec);
+                                       if (emit_finally)
+                                               ig.EndExceptionBlock ();
+                               }
+                       }
+
+
+                       public override void EmitFinally (EmitContext ec)
+                       {
+                               ILGenerator ig = ec.ig;
+
+                               if (enumerator_type.IsValueType) {
+                                       enumerator.Emit (ec);
+
+                                       MethodInfo mi = FetchMethodDispose (enumerator_type);
+                                       if (mi != null) {
+                                               enumerator.EmitLoadAddress (ec);
+                                               ig.Emit (OpCodes.Call, mi);
+                                       } else {
+                                               enumerator.Emit (ec);
+                                               ig.Emit (OpCodes.Box, enumerator_type);
+                                               ig.Emit (OpCodes.Callvirt, TypeManager.void_dispose_void);
+                                       }
+                               } else {
+                                       Label call_dispose = ig.DefineLabel ();
+
+                                       enumerator.Emit (ec);
+                                       ig.Emit (OpCodes.Isinst, TypeManager.idisposable_type);
+                                       ig.Emit (OpCodes.Dup);
+                                       ig.Emit (OpCodes.Brtrue_S, call_dispose);
+                                       ig.Emit (OpCodes.Pop);
+
+                                       Label end_finally = ig.DefineLabel ();
+                                       ig.Emit (OpCodes.Br, end_finally);
+
+                                       ig.MarkLabel (call_dispose);
+                                       ig.Emit (OpCodes.Callvirt, TypeManager.void_dispose_void);
+                                       ig.MarkLabel (end_finally);
+                               }
+                       }
+               }
+
+               protected class CollectionForeachStatement : Statement
+               {
+                       Type type;
+                       Expression variable, current, conv;
+                       Statement statement;
+                       Assign assign;
+
+                       public CollectionForeachStatement (Type type, Expression variable,
+                                                          Expression current, Statement statement,
+                                                          Location loc)
+                       {
+                               this.type = type;
+                               this.variable = variable;
+                               this.current = current;
+                               this.statement = statement;
+                               this.loc = loc;
+                       }
+
+                       public override bool Resolve (EmitContext ec)
+                       {
+                               current = current.Resolve (ec);
+                               if (current == null)
+                                       return false;
+
+                               conv = Convert.ExplicitConversion (ec, current, type, loc);
+                               if (conv == null)
+                                       return false;
+
+                               assign = new Assign (variable, conv, loc);
+                               if (assign.Resolve (ec) == null)
+                                       return false;
+
+                               if (!statement.Resolve (ec))
+                                       return false;
+
+                               return true;
+                       }
+
+                       protected override void DoEmit (EmitContext ec)
+                       {
+                               assign.EmitStatement (ec);
+                               statement.Emit (ec);
+                       }
+               }
        }
 }
index 021cbb89f5e9e553b4ca1bb23ce01649d36cc7fe..dc17016c9a86dc2668d92691efdc95ff6cdea432 100644 (file)
@@ -166,7 +166,6 @@ public class TypeManager {
        static public MethodInfo string_concat_object_dot_dot_dot;
        static public MethodInfo string_isinterneted_string;
        static public MethodInfo system_type_get_type_from_handle;
-       static public MethodInfo object_getcurrent_void;
        static public MethodInfo bool_movenext_void;
        static public MethodInfo ienumerable_getenumerator_void;
        static public MethodInfo void_reset_void;
@@ -185,6 +184,7 @@ public class TypeManager {
        static public MethodInfo int_array_get_lower_bound_int;
        static public MethodInfo int_array_get_upper_bound_int;
        static public MethodInfo void_array_copyto_array_int;
+       static public PropertyInfo ienumerator_getcurrent;
        
        //
        // The attribute constructors.
@@ -973,6 +973,27 @@ public class TypeManager {
                return GetMethod (t, name, args, true);
        }
 
+       /// <summary>
+       ///   Returns the PropertyInfo for a property named `name' defined
+       ///   in type `t'
+       /// </summary>
+       static PropertyInfo GetProperty (Type t, string name)
+       {
+               MemberList list = FindMembers (t, MemberTypes.Property, BindingFlags.Public |
+                                   BindingFlags.Instance, Type.FilterName, name);
+               if (list.Count == 0) {
+                       Report.Error (-19, "Can not find the core property `" + name + "'");
+                       return null;
+               }
+
+               PropertyInfo pi = list [0] as PropertyInfo;
+               if (pi == null) {
+                       Report.Error (-19, "Can not find the core function `" + name + "'");
+                       return null;
+               }
+
+               return pi;
+       }
 
        /// <summary>
        ///    Returns the ConstructorInfo for "args"
@@ -1244,8 +1265,8 @@ public class TypeManager {
                // Void arguments
                //
                Type [] void_arg = {  };
-               object_getcurrent_void = GetMethod (
-                       ienumerator_type, "get_Current", void_arg);
+               ienumerator_getcurrent = GetProperty (
+                       ienumerator_type, "Current");
                bool_movenext_void = GetMethod (
                        ienumerator_type, "MoveNext", void_arg);
                void_reset_void = GetMethod (