2009-08-12 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / iterators.cs
index 5c4e4060f41f5c68c9f85a64babd3c8ebf718e13..ddd316fa14cef277eb578f740bc398dbde7fef4f 100644 (file)
@@ -33,7 +33,7 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
-               public static bool CheckContext (EmitContext ec, Location loc, bool isYieldBreak)
+               public static bool CheckContext (EmitContext ec, Location loc)
                {
                        for (Block block = ec.CurrentBlock; block != null; block = block.Parent) {
                                if (!block.Unsafe)
@@ -71,7 +71,7 @@ namespace Mono.CSharp {
                        Report.Debug (64, "RESOLVE YIELD #1", this, ec, expr, expr.GetType (),
                                      ec.CurrentAnonymousMethod, ec.CurrentIterator);
 
-                       if (!CheckContext (ec, loc, false))
+                       if (!CheckContext (ec, loc))
                                return false;
 
                        Iterator iterator = ec.CurrentIterator;
@@ -82,7 +82,8 @@ namespace Mono.CSharp {
                                        return false;
                        }
 
-                       unwind_protect = ec.CurrentBranching.AddResumePoint (this, loc, out resume_pc);
+                       if (!ec.CurrentBranching.CurrentUsageVector.IsUnreachable)
+                               unwind_protect = ec.CurrentBranching.AddResumePoint (this, loc, out resume_pc);
 
                        return true;
                }
@@ -111,9 +112,14 @@ namespace Mono.CSharp {
                        Report.Error (1625, loc, "Cannot yield in the body of a finally clause");
                }
 
+               protected override void CloneTo (CloneContext clonectx, Statement target)
+               {
+                       throw new NotSupportedException ();
+               }
+
                protected override bool DoResolve (EmitContext ec)
                {
-                       return Yield.CheckContext (ec, loc, true);
+                       return Yield.CheckContext (ec, loc);
                }
 
                protected override void DoEmit (EmitContext ec)
@@ -133,15 +139,22 @@ namespace Mono.CSharp {
        class IteratorStatement : Statement
        {
                Iterator iterator;
-               ExplicitBlock original_block;
+               Block original_block;
 
-               public IteratorStatement (Iterator iterator, ExplicitBlock original_block)
+               public IteratorStatement (Iterator iterator, Block original_block)
                {
                        this.iterator = iterator;
                        this.original_block = original_block;
                        this.loc = iterator.Location;
                }
 
+               protected override void CloneTo (CloneContext clonectx, Statement target)
+               {
+                       IteratorStatement t = (IteratorStatement) target;
+                       t.original_block = (ExplicitBlock) original_block.Clone (clonectx);
+                       t.iterator = (Iterator) iterator.Clone (clonectx);
+               }
+
                public override bool Resolve (EmitContext ec)
                {
                        ec.StartFlowBranching (iterator);
@@ -164,188 +177,17 @@ namespace Mono.CSharp {
 
        public class IteratorStorey : AnonymousMethodStorey
        {
-               public readonly Iterator Iterator;
-
-               TypeExpr iterator_type_expr;
-               Field pc_field;
-               Field current_field;
-
-               TypeExpr enumerator_type;
-               TypeExpr enumerable_type;
-               TypeArguments generic_args;
-               TypeExpr generic_enumerator_type;
-#if GMCS_SOURCE                
-               TypeExpr generic_enumerable_type;
-#endif
-
-               int local_name_idx;
-
-               public IteratorStorey (Iterator iterator)
-                       : base (iterator.Container.Toplevel, iterator.Host,
-                         iterator.OriginalMethod as MemberBase, iterator.GenericMethod, "Iterator")
-               {
-                       this.Iterator = iterator;
-                       HasHoistedVariables = true;
-               }
-
-               public Field PC {
-                       get { return pc_field; }
-               }
-
-               public Field CurrentField {
-                       get { return current_field; }
-               }
-
-               public ArrayList HoistedParameters {
-                       get { return hoisted_params; }
-               }
-
-               public override TypeExpr [] GetClassBases (out TypeExpr base_class)
+               class IteratorMethod : Method
                {
-                       iterator_type_expr = new TypeExpression (MutateType (Iterator.OriginalIteratorType), Location);
-
-#if GMCS_SOURCE
-                       generic_args = new TypeArguments (Location);
-                       generic_args.Add (iterator_type_expr);
-#endif
-
-                       ArrayList list = new ArrayList ();
-                       if (Iterator.IsEnumerable) {
-                               enumerable_type = new TypeExpression (
-                                       TypeManager.ienumerable_type, Location);
-                               list.Add (enumerable_type);
-
-#if GMCS_SOURCE
-                               generic_enumerable_type = new ConstructedType (
-                                       TypeManager.generic_ienumerable_type,
-                                       generic_args, Location);
-                               list.Add (generic_enumerable_type);
-#endif
-                       }
-
-                       enumerator_type = new TypeExpression (
-                               TypeManager.ienumerator_type, Location);
-                       list.Add (enumerator_type);
-
-                       list.Add (new TypeExpression (TypeManager.idisposable_type, Location));
-
-#if GMCS_SOURCE
-                       generic_enumerator_type = new ConstructedType (
-                               TypeManager.generic_ienumerator_type,
-                               generic_args, Location);
-                       list.Add (generic_enumerator_type);
-#endif
-
-                       type_bases = list;
-
-                       return base.GetClassBases (out base_class);
-               }
-
-               protected override string GetVariableMangledName (LocalInfo local_info)
-               {
-                       return "<" + local_info.Name + ">__" + local_name_idx++.ToString ();
-               }
-
-               public void DefineIteratorMembers ()
-               {
-                       pc_field = AddCompilerGeneratedField ("$PC", TypeManager.system_int32_expr);
-                       current_field = AddCompilerGeneratedField ("$current", iterator_type_expr);
+                       readonly IteratorStorey host;
 
-#if GMCS_SOURCE
-                       Define_Current (true);
-#endif
-                       Define_Current (false);
-                       new DisposeMethod (this);
-                       Define_Reset ();
-
-                       if (Iterator.IsEnumerable) {
-                               new GetEnumeratorMethod (this, false);
-#if GMCS_SOURCE
-                               new GetEnumeratorMethod (this, true);
-#endif
-                       }
-
-                       DoResolveMembers ();
-               }
-
-               void Define_Current (bool is_generic)
-               {
-                       MemberName left;
-                       TypeExpr type;
-
-                       if (is_generic) {
-                               left = new MemberName (
-                                       "System.Collections.Generic.IEnumerator",
-                                       generic_args, Location);
-                               type = iterator_type_expr;
-                       } else {
-                               left = new MemberName ("System.Collections.IEnumerator", Location);
-                               type = TypeManager.system_object_expr;
-                       }
-
-                       MemberName name = new MemberName (left, "Current", null, Location);
-
-                       ToplevelBlock get_block = new ToplevelBlock (Location);
-                       get_block.AddStatement (new CurrentBlock (this, is_generic));
-
-                       Accessor getter = new Accessor (get_block, 0, null, Location);
-
-                       Property current = new Property (
-                               this, type, Modifiers.DEBUGGER_HIDDEN, false, name, null, getter, null, false);
-                       AddProperty (current);
-               }
-
-               void Define_Reset ()
-               {
-                       Method reset = new Method (
-                               this, null, TypeManager.system_void_expr,
-                               Modifiers.PUBLIC | Modifiers.DEBUGGER_HIDDEN,
-                               false, new MemberName ("Reset", Location),
-                               Parameters.EmptyReadOnlyParameters, null);
-                       AddMethod (reset);
-
-                       reset.Block = new ToplevelBlock (Location);
-                       reset.Block.AddStatement (Create_ThrowNotSupported ());
-               }
-
-               Statement Create_ThrowNotSupported ()
-               {
-                       TypeExpr ex_type = new TypeLookupExpression ("System.NotSupportedException");
-
-                       return new Throw (new New (ex_type, null, Location), Location);
-               }
-
-               protected class GetEnumeratorMethod : Method
-               {
-                       public IteratorStorey Host;
-
-                       static MemberName GetMemberName (IteratorStorey host, bool is_generic)
+                       public IteratorMethod (IteratorStorey host, FullNamedExpression returnType, int mod, MemberName name)
+                               : base (host, null, returnType, mod | Modifiers.DEBUGGER_HIDDEN | Modifiers.COMPILER_GENERATED,
+                                 name, ParametersCompiled.EmptyReadOnlyParameters, null)
                        {
-                               MemberName left;
-                               if (is_generic) {
-                                       left = new MemberName (
-                                               "System.Collections.Generic.IEnumerable",
-                                               host.generic_args, host.Location);
-                               } else {
-                                       left = new MemberName (
-                                               "System.Collections.IEnumerable", host.Location);
-                               }
-
-                               return new MemberName (left, "GetEnumerator", host.Location);
-                       }
-
-                       public GetEnumeratorMethod (IteratorStorey host, bool is_generic)
-                               : base (host, null, is_generic ?
-                                       host.generic_enumerator_type : host.enumerator_type,
-                                       Modifiers.DEBUGGER_HIDDEN, false, GetMemberName (host, is_generic),
-                                       Parameters.EmptyReadOnlyParameters, null)
-                       {
-                               this.Host = host;
-
-                               host.AddMethod (this);
+                               this.host = host;
 
-                               Block = new ToplevelBlock (host.Iterator.Container.Toplevel, null, Location);
-                               Block.AddStatement (new GetEnumeratorStatement (host, type_name));
+                               Block = new ToplevelBlock (host.Iterator.Container.Toplevel, ParametersCompiled.EmptyReadOnlyParameters, Location);
                        }
 
                        public override EmitContext CreateEmitContext (DeclSpace tc, ILGenerator ig)
@@ -353,55 +195,75 @@ namespace Mono.CSharp {
                                EmitContext ec = new EmitContext (
                                        this, tc, this.ds, Location, ig, MemberType, ModFlags, false);
 
-                               ec.CurrentAnonymousMethod = Host.Iterator;
+                               ec.CurrentAnonymousMethod = host.Iterator;
                                return ec;
                        }
+               }
 
-                       protected class GetEnumeratorStatement : Statement
+               class GetEnumeratorMethod : IteratorMethod
+               {
+                       sealed class GetEnumeratorStatement : Statement
                        {
                                IteratorStorey host;
-                               Expression type;
+                               IteratorMethod host_method;
 
-                               Expression cast;
+                               Expression new_storey;
 
-                               public GetEnumeratorStatement (IteratorStorey host, Expression type)
+                               public GetEnumeratorStatement (IteratorStorey host, IteratorMethod host_method)
                                {
                                        this.host = host;
-                                       this.type = type;
-                                       loc = host.Location;
+                                       this.host_method = host_method;
+                                       loc = host_method.Location;
                                }
 
-                               public override bool Resolve (EmitContext ec)
+                               protected override void CloneTo (CloneContext clonectx, Statement target)
                                {
-                                       type = type.ResolveAsTypeTerminal (ec, false);
-                                       if ((type == null) || (type.Type == null))
-                                               return false;
+                                       throw new NotSupportedException ();
+                               }
 
+                               public override bool Resolve (EmitContext ec)
+                               {
                                        TypeExpression storey_type_expr = new TypeExpression (host.TypeBuilder, loc);
-                                       Expression new_storey;
+                                       ArrayList init = null;
+                                       if (host.hoisted_this != null) {
+                                               init = new ArrayList (host.hoisted_params == null ? 1 : host.HoistedParameters.Count + 1);
+                                               HoistedThis ht = host.hoisted_this;
+                                               FieldExpr from = new FieldExpr (ht.Field.FieldBuilder, loc);
+                                               from.InstanceExpression = CompilerGeneratedThis.Instance;
+                                               init.Add (new ElementInitializer (ht.Field.Name, from, loc));
+                                       }
+
                                        if (host.hoisted_params != null) {
-                                               ArrayList init = new ArrayList (host.HoistedParameters.Count);
-                                               foreach (HoistedParameter hp in host.HoistedParameters) {
-                                                       FieldExpr from = new FieldExpr (hp.Field.FieldBuilder, loc);
+                                               if (init == null)
+                                                       init = new ArrayList (host.HoistedParameters.Count);
+
+                                               for (int i = 0; i < host.hoisted_params.Count; ++i) {
+                                                       HoistedParameter hp = (HoistedParameter) host.hoisted_params [i];
+                                                       HoistedParameter hp_cp = (HoistedParameter) host.hoisted_params_copy [i];
+
+                                                       FieldExpr from = new FieldExpr (hp_cp.Field.FieldBuilder, loc);
                                                        from.InstanceExpression = CompilerGeneratedThis.Instance;
+
                                                        init.Add (new ElementInitializer (hp.Field.Name, from, loc));
                                                }
+                                       }
 
-                                               new_storey = new NewInitialize (storey_type_expr, new ArrayList (0),
+                                       if (init != null) {
+                                               new_storey = new NewInitialize (storey_type_expr, null,
                                                        new CollectionOrObjectInitializers (init, loc), loc);
                                        } else {
-                                               new_storey = new New (storey_type_expr, new ArrayList (0), loc);
+                                               new_storey = new New (storey_type_expr, null, loc);
                                        }
 
                                        new_storey = new_storey.Resolve (ec);
                                        if (new_storey != null)
-                                               cast = Convert.ImplicitConversionRequired (ec, new_storey, type.Type, loc);
+                                               new_storey = Convert.ImplicitConversionRequired (ec, new_storey, host_method.MemberType, loc);
 
                                        if (TypeManager.int_interlocked_compare_exchange == null) {
                                                Type t = TypeManager.CoreLookupType ("System.Threading", "Interlocked", Kind.Class, true);
                                                if (t != null) {
                                                        TypeManager.int_interlocked_compare_exchange = TypeManager.GetPredefinedMethod (
-                                                               t, "CompareExchange", loc, TypeManager.GetReferenceType (TypeManager.int32_type),
+                                                               t, "CompareExchange", loc, TypeManager.int32_type,
                                                                TypeManager.int32_type, TypeManager.int32_type);
                                                }
                                        }
@@ -417,19 +279,19 @@ namespace Mono.CSharp {
 
                                        ig.Emit (OpCodes.Ldarg_0);
                                        ig.Emit (OpCodes.Ldflda, host.PC.FieldBuilder);
-                                       ig.Emit (OpCodes.Ldc_I4, (int) Iterator.State.Start);
-                                       ig.Emit (OpCodes.Ldc_I4, (int) Iterator.State.Uninitialized);
+                                       IntConstant.EmitInt (ig, (int) Iterator.State.Start);
+                                       IntConstant.EmitInt (ig, (int) Iterator.State.Uninitialized);
                                        ig.Emit (OpCodes.Call, TypeManager.int_interlocked_compare_exchange);
 
-                                       ig.Emit (OpCodes.Ldc_I4, (int) Iterator.State.Uninitialized);
-                                       ig.Emit (OpCodes.Bne_Un, label_init);
+                                       IntConstant.EmitInt (ig, (int) Iterator.State.Uninitialized);
+                                       ig.Emit (OpCodes.Bne_Un_S, label_init);
 
                                        ig.Emit (OpCodes.Ldarg_0);
                                        ig.Emit (OpCodes.Ret);
 
                                        ig.MarkLabel (label_init);
 
-                                       cast.Emit (ec);
+                                       new_storey.Emit (ec);
                                        ig.Emit (OpCodes.Ret);
                                }
 
@@ -438,46 +300,17 @@ namespace Mono.CSharp {
                                        throw new NotSupportedException ();
                                }
                        }
-               }
-
-               protected class DisposeMethod : Method
-               {
-                       readonly IteratorStorey Host;
 
-                       public DisposeMethod (IteratorStorey host)
-                               : base (host, null, TypeManager.system_void_expr,
-                                       Modifiers.PUBLIC | Modifiers.DEBUGGER_HIDDEN | Modifiers.COMPILER_GENERATED,
-                                       false, new MemberName ("Dispose", host.Location),
-                                       Parameters.EmptyReadOnlyParameters, null)
+                       public GetEnumeratorMethod (IteratorStorey host, FullNamedExpression returnType, MemberName name)
+                               : base (host, returnType, 0, name)
                        {
-                               this.Host = host;
-
-                               host.AddMethod (this);
-
-                               Block = new ToplevelBlock (host.Iterator.Container, null, Location);
-                               Block.AddStatement (new DisposeMethodStatement (Host.Iterator));
-
-                               Report.Debug (64, "DISPOSE METHOD", host, Block);
-                       }
-
-                       public override EmitContext CreateEmitContext (DeclSpace tc, ILGenerator ig)
-                       {
-                               EmitContext ec = new EmitContext (
-                                       this, tc, this.ds, Location, ig, MemberType, ModFlags, false);
-
-                               ec.CurrentAnonymousMethod = Host.Iterator;
-                               return ec;
+                               Block.AddStatement (new GetEnumeratorStatement (host, this));
                        }
+               }
 
-                       //public override void Emit ()
-                       //{
-                       //    if (Parent.MemberName.IsGeneric)
-                       //        block.MutateHoistedGenericType (Host.Iterator.Storey);
-
-                       //    base.Emit ();
-                       //}
-
-                       protected class DisposeMethodStatement : Statement
+               class DisposeMethod : IteratorMethod
+               {
+                       sealed class DisposeMethodStatement : Statement
                        {
                                Iterator iterator;
 
@@ -487,6 +320,11 @@ namespace Mono.CSharp {
                                        this.loc = iterator.Location;
                                }
 
+                               protected override void CloneTo (CloneContext clonectx, Statement target)
+                               {
+                                       throw new NotSupportedException ();
+                               }
+
                                public override bool Resolve (EmitContext ec)
                                {
                                        return true;
@@ -502,42 +340,239 @@ namespace Mono.CSharp {
                                        throw new NotSupportedException ();
                                }
                        }
+
+                       public DisposeMethod (IteratorStorey host)
+                               : base (host, TypeManager.system_void_expr, Modifiers.PUBLIC, new MemberName ("Dispose", host.Location))
+                       {
+                               host.AddMethod (this);
+
+                               Block = new ToplevelBlock (host.Iterator.Container, ParametersCompiled.EmptyReadOnlyParameters, Location);
+                               Block.AddStatement (new DisposeMethodStatement (host.Iterator));
+                       }
                }
 
-               protected class CurrentBlock : Statement {
-                       IteratorStorey host;
-                       bool is_generic;
+               //
+               // Uses Method as method info
+               //
+               class DynamicMethodGroupExpr : MethodGroupExpr
+               {
+                       readonly Method method;
 
-                       public CurrentBlock (IteratorStorey host, bool is_generic)
+                       public DynamicMethodGroupExpr (Method method, Location loc)
+                               : base (null, loc)
                        {
-                               this.host = host;
-                               this.is_generic = is_generic;
-                               loc = host.Location;
+                               this.method = method;
                        }
 
-                       public override bool Resolve (EmitContext ec)
+                       public override Expression DoResolve (EmitContext ec)
                        {
-                               // We emit a 'ret', so prevent the enclosing TopLevelBlock from emitting one too
-                               ec.CurrentBranching.CurrentUsageVector.Goto ();
-                               return true;
+                               Methods = new MethodBase [] { method.MethodBuilder };
+                               type = method.Parent.TypeBuilder;
+                               InstanceExpression = new CompilerGeneratedThis (type, Location);
+                               return base.DoResolve (ec);
                        }
+               }
 
-                       protected override void DoEmit (EmitContext ec)
-                       {
-                               ILGenerator ig = ec.ig;
+               class DynamicFieldExpr : FieldExpr
+               {
+                       readonly Field field;
 
-                               ig.Emit (OpCodes.Ldarg_0);
-                               ig.Emit (OpCodes.Ldfld, host.CurrentField.FieldBuilder);
-                               if (!is_generic)
-                                       ig.Emit (OpCodes.Box, host.CurrentField.MemberType);
-                               ig.Emit (OpCodes.Ret);
+                       public DynamicFieldExpr (Field field, Location loc)
+                               : base (loc)
+                       {
+                               this.field = field;
                        }
 
-                       public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
+                       public override Expression DoResolve (EmitContext ec)
                        {
-                               throw new NotSupportedException ();
+                               FieldInfo = field.FieldBuilder;
+                               type = TypeManager.TypeToCoreType (FieldInfo.FieldType);
+                               InstanceExpression = new CompilerGeneratedThis (type, Location);
+                               return base.DoResolve (ec);
+                       }
+               }
+
+               public readonly Iterator Iterator;
+
+               TypeExpr iterator_type_expr;
+               Field pc_field;
+               Field current_field;
+
+               TypeExpr enumerator_type;
+               TypeExpr enumerable_type;
+               TypeArguments generic_args;
+               TypeExpr generic_enumerator_type;
+               TypeExpr generic_enumerable_type;
+
+               ArrayList hoisted_params_copy;
+               int local_name_idx;
+
+               public IteratorStorey (Iterator iterator)
+                       : base (iterator.Container.Toplevel, iterator.Host,
+                         iterator.OriginalMethod as MemberBase, iterator.GenericMethod, "Iterator")
+               {
+                       this.Iterator = iterator;
+               }
+
+               public Field PC {
+                       get { return pc_field; }
+               }
+
+               public Field CurrentField {
+                       get { return current_field; }
+               }
+
+               public ArrayList HoistedParameters {
+                       get { return hoisted_params; }
+               }
+
+               protected override TypeExpr [] ResolveBaseTypes (out TypeExpr base_class)
+               {
+                       iterator_type_expr = new TypeExpression (MutateType (Iterator.OriginalIteratorType), Location);
+                       generic_args = new TypeArguments (iterator_type_expr);
+
+                       ArrayList list = new ArrayList ();
+                       if (Iterator.IsEnumerable) {
+                               enumerable_type = new TypeExpression (
+                                       TypeManager.ienumerable_type, Location);
+                               list.Add (enumerable_type);
+
+                               if (TypeManager.generic_ienumerable_type != null) {
+                                       generic_enumerable_type = new GenericTypeExpr (
+                                               TypeManager.generic_ienumerable_type,
+                                               generic_args, Location);
+                                       list.Add (generic_enumerable_type);
+                               }
+                       }
+
+                       enumerator_type = new TypeExpression (
+                               TypeManager.ienumerator_type, Location);
+                       list.Add (enumerator_type);
+
+                       list.Add (new TypeExpression (TypeManager.idisposable_type, Location));
+
+                       if (TypeManager.generic_ienumerator_type != null) {
+                               generic_enumerator_type = new GenericTypeExpr (
+                                       TypeManager.generic_ienumerator_type,
+                                       generic_args, Location);
+                               list.Add (generic_enumerator_type);
+                       }
+
+                       type_bases = list;
+
+                       return base.ResolveBaseTypes (out base_class);
+               }
+
+               protected override string GetVariableMangledName (LocalInfo local_info)
+               {
+                       return "<" + local_info.Name + ">__" + local_name_idx++.ToString ();
+               }
+
+               public void DefineIteratorMembers ()
+               {
+                       pc_field = AddCompilerGeneratedField ("$PC", TypeManager.system_int32_expr);
+                       current_field = AddCompilerGeneratedField ("$current", iterator_type_expr);
+
+                       if (hoisted_params != null) {
+                               //
+                               // Iterators are independent, each GetEnumerator call has to
+                               // create same enumerator therefore we have to keep original values
+                               // around for re-initialization
+                               //
+                               // TODO: Do it for assigned/modified parameters only
+                               //
+                               hoisted_params_copy = new ArrayList (hoisted_params.Count);
+                               foreach (HoistedParameter hp in hoisted_params) {
+                                       hoisted_params_copy.Add (new HoistedParameter (hp, "<$>" + hp.Field.Name));
+                               }
+                       }
+
+                       if (generic_enumerator_type != null)
+                               Define_Current (true);
+
+                       Define_Current (false);
+                       new DisposeMethod (this);
+                       Define_Reset ();
+
+                       if (Iterator.IsEnumerable) {
+                               MemberName name = new MemberName (QualifiedAliasMember.GlobalAlias, "System", null, Location);
+                               name = new MemberName (name, "Collections", Location);
+                               name = new MemberName (name, "IEnumerable", Location);
+                               name = new MemberName (name, "GetEnumerator", Location);
+
+                               if (generic_enumerator_type != null) {
+                                       Method get_enumerator = new IteratorMethod (this, enumerator_type, 0, name);
+
+                                       name = new MemberName (name.Left.Left, "Generic", Location);
+                                       name = new MemberName (name, "IEnumerable", generic_args, Location);
+                                       name = new MemberName (name, "GetEnumerator", Location);
+                                       Method gget_enumerator = new GetEnumeratorMethod (this, generic_enumerator_type, name);
+
+                                       //
+                                       // Just call generic GetEnumerator implementation
+                                       //
+                                       get_enumerator.Block.AddStatement (
+                                               new Return (new Invocation (new DynamicMethodGroupExpr (gget_enumerator, Location), null), Location));
+
+                                       AddMethod (get_enumerator);
+                                       AddMethod (gget_enumerator);
+                               } else {
+                                       AddMethod (new GetEnumeratorMethod (this, enumerator_type, name));
+                               }
                        }
                }
+
+               protected override void EmitHoistedParameters (EmitContext ec, ArrayList hoisted)
+               {
+                       base.EmitHoistedParameters (ec, hoisted);
+                       base.EmitHoistedParameters (ec, hoisted_params_copy);
+               }
+
+               void Define_Current (bool is_generic)
+               {
+                       TypeExpr type;
+
+                       MemberName name = new MemberName (QualifiedAliasMember.GlobalAlias, "System", null, Location);
+                       name = new MemberName (name, "Collections", Location);
+
+                       if (is_generic) {
+                               name = new MemberName (name, "Generic", Location);
+                               name = new MemberName (name, "IEnumerator", generic_args, Location);
+                               type = iterator_type_expr;
+                       } else {
+                               name = new MemberName (name, "IEnumerator");
+                               type = TypeManager.system_object_expr;
+                       }
+
+                       name = new MemberName (name, "Current", Location);
+
+                       ToplevelBlock get_block = new ToplevelBlock (Location);
+                       get_block.AddStatement (new Return (new DynamicFieldExpr (CurrentField, Location), Location));
+                               
+                       Accessor getter = new Accessor (get_block, 0, null, null, Location);
+
+                       Property current = new Property (
+                               this, type, Modifiers.DEBUGGER_HIDDEN, name, null, getter, null, false);
+                       AddProperty (current);
+               }
+
+               void Define_Reset ()
+               {
+                       Method reset = new Method (
+                               this, null, TypeManager.system_void_expr,
+                               Modifiers.PUBLIC | Modifiers.DEBUGGER_HIDDEN,
+                               new MemberName ("Reset", Location),
+                               ParametersCompiled.EmptyReadOnlyParameters, null);
+                       AddMethod (reset);
+
+                       reset.Block = new ToplevelBlock (Location);
+
+                       Type ex_type = TypeManager.CoreLookupType ("System", "NotSupportedException", Kind.Class, true);
+                       if (ex_type == null)
+                               return;
+
+                       reset.Block.AddStatement (new Throw (new New (new TypeExpression (ex_type, Location), null, Location), Location));
+               }
        }
 
        //
@@ -545,7 +580,8 @@ namespace Mono.CSharp {
        //
        public class Iterator : AnonymousExpression {
                public readonly IMethodData OriginalMethod;
-
+               AnonymousMethodMethod method;
+               public readonly TypeContainer Host;
                public readonly bool IsEnumerable;
 
                //
@@ -581,11 +617,6 @@ namespace Mono.CSharp {
                        Start = 0
                }
 
-               public override void AddStoreyReference (AnonymousMethodStorey storey)
-               {
-                       // do nothing
-               }
-
                public void EmitYieldBreak (ILGenerator ig, bool unwind_protect)
                {
                        ig.Emit (unwind_protect ? OpCodes.Leave : OpCodes.Br, move_next_error);
@@ -729,7 +760,7 @@ namespace Mono.CSharp {
 
 
                ArrayList resume_points;
-               public int AddResumePoint (ResumableStatement stmt, Location loc)
+               public int AddResumePoint (ResumableStatement stmt)
                {
                        if (resume_points == null)
                                resume_points = new ArrayList ();
@@ -782,14 +813,15 @@ namespace Mono.CSharp {
                // Our constructor
                //
                private Iterator (IMethodData method, TypeContainer host, Type iterator_type, bool is_enumerable)
-                       : base (host,
-                               new ToplevelBlock (method.Block, Parameters.EmptyReadOnlyParameters, method.Block.StartLocation),
+                       : base (
+                               new ToplevelBlock (method.Block, ParametersCompiled.EmptyReadOnlyParameters, method.Block.StartLocation),
                                TypeManager.bool_type,
                                method.Location)
                {
                        this.OriginalMethod = method;
                        this.OriginalIteratorType = iterator_type;
                        this.IsEnumerable = is_enumerable;
+                       this.Host = host;
 
                        IteratorHost = Block.ChangeToIterator (this, method.Block);
                }
@@ -805,7 +837,7 @@ namespace Mono.CSharp {
                                this, Storey, null, TypeManager.system_boolean_expr,
                                Modifiers.PUBLIC, OriginalMethod.GetSignatureForError (),
                                new MemberName ("MoveNext", Location),
-                               Parameters.EmptyReadOnlyParameters);
+                               ParametersCompiled.EmptyReadOnlyParameters);
 
                        if (!Compatible (ec))
                                return null;
@@ -864,7 +896,7 @@ namespace Mono.CSharp {
                                return;
                        }
 
-                       Parameters parameters = method.ParameterInfo;
+                       ParametersCompiled parameters = method.ParameterInfo;
                        for (int i = 0; i < parameters.Count; i++) {
                                Parameter p = parameters [i];
                                Parameter.Modifier mod = p.ModFlags;
@@ -874,13 +906,13 @@ namespace Mono.CSharp {
                                        return;
                                }
 
-                               if ((mod & Parameter.Modifier.ARGLIST) != 0) {
+                               if (p is ArglistParameter) {
                                        Report.Error (1636, method.Location,
                                                "__arglist is not allowed in parameter list of iterators");
                                        return;
                                }
 
-                               if (p.ParameterType.IsPointer) {
+                               if (parameters.Types [i].IsPointer) {
                                        Report.Error (1637, p.Location,
                                                          "Iterators cannot have unsafe parameters or " +
                                                          "yield types");
@@ -922,13 +954,13 @@ namespace Mono.CSharp {
 
                        Type gt = TypeManager.DropGenericTypeArguments (ret);
                        if (gt == TypeManager.generic_ienumerable_type) {
-                               original_iterator_type = args [0];
+                               original_iterator_type = TypeManager.TypeToCoreType (args [0]);
                                is_enumerable = true;
                                return true;
                        }
                        
                        if (gt == TypeManager.generic_ienumerator_type) {
-                               original_iterator_type = args [0];
+                               original_iterator_type = TypeManager.TypeToCoreType (args [0]);
                                is_enumerable = false;
                                return true;
                        }