2010-01-07 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / iterators.cs
index d671e21898027a9d09261d693d073114a1d6f6b3..5362bba08bcfb75ea0a8366906d77dc9f60e1f97 100644 (file)
@@ -3,28 +3,28 @@
 //
 // Author:
 //   Miguel de Icaza (miguel@ximian.com)
+//   Marek Safar (marek.safar@gmail.com)
 //
-// (C) 2003 Ximian, Inc.
+// Dual licensed under the terms of the MIT X11 or GNU GPL
+// Copyright 2003 Ximian, Inc.
+// Copyright 2003-2008 Novell, Inc.
 //
+
 // TODO:
 //    Flow analysis for Yield.
-//    Emit calls to base object constructor.
-//
-// Generics note:
-//    Current should be defined to return T, and IEnumerator.Current returns object
 //
 
 using System;
-using System.Collections;
+using System.Collections.Generic;
 using System.Reflection;
 using System.Reflection.Emit;
 
 namespace Mono.CSharp {
 
-       public class Yield : ResumableStatement {
+       class Yield : ResumableStatement {
                Expression expr;
                bool unwind_protect;
-
+               Iterator iterator;
                int resume_pc;
 
                public Yield (Expression expr, Location l)
@@ -33,22 +33,14 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
-               public static bool CheckContext (EmitContext ec, Location loc, bool isYieldBreak)
+               public static bool CheckContext (ResolveContext ec, Location loc)
                {
-                       for (Block block = ec.CurrentBlock; block != null; block = block.Parent) {
-                               if (!block.Unsafe)
-                                       continue;
-
-                               Report.Error (1629, loc, "Unsafe code may not appear in iterators");
-                               return false;
-                       }
-
                        //
                        // We can't use `ec.InUnsafe' here because it's allowed to have an iterator
                        // inside an unsafe class.  See test-martin-29.cs for an example.
                        //
                        if (!ec.CurrentAnonymousMethod.IsIterator) {
-                               Report.Error (1621, loc,
+                               ec.Report.Error (1621, loc,
                                              "The yield statement cannot be used inside " +
                                              "anonymous method blocks");
                                return false;
@@ -56,10 +48,14 @@ namespace Mono.CSharp {
 
                        return true;
                }
+
+               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
+               {
+                       expr.MutateHoistedGenericType (storey);
+               }
                
-               public override bool Resolve (EmitContext ec)
+               public override bool Resolve (BlockContext ec)
                {
-                       Report.Debug (64, "RESOLVE YIELD", this, ec, expr, expr.GetType ());
                        expr = expr.Resolve (ec);
                        if (expr == null)
                                return false;
@@ -67,25 +63,26 @@ 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;
-                       if (expr.Type != iterator.IteratorType) {
+                       iterator = ec.CurrentIterator;
+                       if (expr.Type != iterator.OriginalIteratorType) {
                                expr = Convert.ImplicitConversionRequired (
-                                       ec, expr, iterator.IteratorType, loc);
+                                       ec, expr, iterator.OriginalIteratorType, loc);
                                if (expr == null)
                                        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;
                }
 
                protected override void DoEmit (EmitContext ec)
                {
-                       ec.CurrentIterator.MarkYield (ec, expr, resume_pc, unwind_protect, resume_point);
+                       iterator.MarkYield (ec, expr, resume_pc, unwind_protect, resume_point);
                }
 
                protected override void CloneTo (CloneContext clonectx, Statement t)
@@ -96,351 +93,176 @@ namespace Mono.CSharp {
                }
        }
 
-       public class YieldBreak : ExitStatement {
+       public class YieldBreak : ExitStatement
+       {
+               Iterator iterator;
+
                public YieldBreak (Location l)
                {
                        loc = l;
                }
 
-               public override void Error_FinallyClause ()
+               public override void Error_FinallyClause (Report Report)
                {
                        Report.Error (1625, loc, "Cannot yield in the body of a finally clause");
                }
 
-               protected override bool DoResolve (EmitContext ec)
-               {
-                       return Yield.CheckContext (ec, loc, true);
-               }
-
-               protected override void DoEmit (EmitContext ec)
-               {
-                       ec.CurrentIterator.EmitYieldBreak (ec.ig, unwind_protect);
-               }
-       }
-
-       public class IteratorHost : RootScopeInfo
-       {
-               public readonly Iterator Iterator;
-
-               TypeExpr iterator_type_expr;
-               Field pc_field;
-               Field current_field;
-               MethodInfo dispose_method;
-
-               TypeExpr enumerator_type;
-               TypeExpr enumerable_type;
-               TypeArguments generic_args;
-               TypeExpr generic_enumerator_type;
-#if GMCS_SOURCE                
-               TypeExpr generic_enumerable_type;
-#endif
-               
-               public IteratorHost (Iterator iterator)
-                       : base (iterator.Container.Toplevel, iterator.Host, iterator.GenericMethod,
-                               iterator.Location)
-               {
-                       this.Iterator = iterator;
-               }
-
-               public override bool IsIterator {
-                       get { return true; }
-               }
-
-               public MethodInfo Dispose {
-                       get { return dispose_method; }
-               }
-
-               public Field PC {
-                       get { return pc_field; }
-               }
-
-               public Field CurrentField {
-                       get { return current_field; }
-               }
-
-               public Type IteratorType {
-                       get { return iterator_type_expr.Type; }
-               }
-
-               public override TypeExpr [] GetClassBases (out TypeExpr base_class)
+               protected override void CloneTo (CloneContext clonectx, Statement target)
                {
-                       iterator_type_expr = InflateType (Iterator.OriginalIteratorType);
-
-#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
-
-                       Bases = list;
-
-                       return base.GetClassBases (out base_class);
+                       throw new NotSupportedException ();
                }
 
-               protected override bool DoResolveMembers ()
+               protected override bool DoResolve (BlockContext ec)
                {
-                       pc_field = CaptureVariable ("$PC", TypeManager.system_int32_expr);
-                       current_field = CaptureVariable ("$current", iterator_type_expr);
-
-#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
-                       }
-
-                       return base.DoResolveMembers ();
+                       iterator = ec.CurrentIterator;
+                       return Yield.CheckContext (ec, loc);
                }
 
-               public void CaptureScopes ()
+               protected override void DoEmit (EmitContext ec)
                {
-                       Report.Debug (128, "DEFINE ITERATOR HOST", this, scopes);
-
-                       foreach (ScopeInfo si in scopes)
-                               CaptureScope (si);
-
-                       foreach (ScopeInfo si in scopes) {
-                               if (!si.Define ())
-                                       throw new InternalErrorException ();
-                               if (si.DefineType () == null)
-                                       throw new InternalErrorException ();
-                               if (!si.ResolveType ())
-                                       throw new InternalErrorException ();
-                               if (!si.ResolveMembers ())
-                                       throw new InternalErrorException ();
-                               if (!si.DefineMembers ())
-                                       throw new InternalErrorException ();
-                       }
+                       iterator.EmitYieldBreak (ec.ig, unwind_protect);
                }
 
-               protected override bool DoDefineMembers ()
+               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
-                       if (!base.DoDefineMembers ())
-                               return false;
-
-                       FetchMethodDispose ();
-
-                       return true;
+                       // nothing to do
                }
+       }
 
-               protected override void EmitScopeConstructor (EmitContext ec)
-               {
-                       ec.ig.Emit (OpCodes.Ldarg_0);
-                       ec.ig.Emit (OpCodes.Ldarg_1);
-                       ec.ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
-                       base.EmitScopeConstructor (ec);
-               }
+       //
+       // Wraps method block into iterator wrapper block
+       //
+       class IteratorStatement : Statement
+       {
+               Iterator iterator;
+               Block original_block;
 
-               void FetchMethodDispose ()
+               public IteratorStatement (Iterator iterator, Block original_block)
                {
-                       MemberList dispose_list;
-
-                       dispose_list = FindMembers (
-                               CurrentType != null ? CurrentType : TypeBuilder,
-                               MemberTypes.Method, BindingFlags.Public | BindingFlags.Instance,
-                               Type.FilterName, "Dispose");
-
-                       if (dispose_list.Count != 1)
-                               throw new InternalErrorException ("Cannot find Dipose() method.");
-
-                       dispose_method = (MethodInfo) dispose_list [0];
+                       this.iterator = iterator;
+                       this.original_block = original_block;
+                       this.loc = iterator.Location;
                }
 
-               void Define_Current (bool is_generic)
+               protected override void CloneTo (CloneContext clonectx, Statement target)
                {
-                       MemberName left;
-                       Expression 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, 0, false, name, null, getter, null, false);
-                       AddProperty (current);
+                       IteratorStatement t = (IteratorStatement) target;
+                       t.original_block = (ExplicitBlock) original_block.Clone (clonectx);
+                       t.iterator = (Iterator) iterator.Clone (clonectx);
                }
 
-               void Define_Reset ()
+               public override bool Resolve (BlockContext ec)
                {
-                       Method reset = new Method (
-                               this, null, TypeManager.system_void_expr, Modifiers.PUBLIC,
-                               false, new MemberName ("Reset", Location),
-                               Parameters.EmptyReadOnlyParameters, null);
-                       AddMethod (reset);
-
-                       reset.Block = new ToplevelBlock (Location);
-                       reset.Block.AddStatement (Create_ThrowNotSupported ());
+                       ec.StartFlowBranching (iterator);
+                       bool ok = original_block.Resolve (ec);
+                       ec.EndFlowBranching ();
+                       return ok;
                }
 
-               Statement Create_ThrowNotSupported ()
+               protected override void DoEmit (EmitContext ec)
                {
-                       TypeExpr ex_type = new TypeLookupExpression ("System.NotSupportedException");
-
-                       return new Throw (new New (ex_type, null, Location), Location);
+                       iterator.EmitMoveNext (ec, original_block);
                }
 
-               protected override ScopeInitializer CreateScopeInitializer ()
+               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
                {
-                       return new IteratorHostInitializer (this);
+                       original_block.MutateHoistedGenericType (storey);
+                       iterator.MutateHoistedGenericType (storey);
                }
+       }
 
-               protected class IteratorHostInitializer : RootScopeInitializer
+       public class IteratorStorey : AnonymousMethodStorey
+       {
+               class IteratorMethod : Method
                {
-                       new public readonly IteratorHost Host;
-                       protected Iterator.State state;
+                       readonly IteratorStorey host;
 
-                       public IteratorHostInitializer (IteratorHost host)
-                               : base (host)
+                       public IteratorMethod (IteratorStorey host, FullNamedExpression returnType, Modifiers mod, MemberName name)
+                               : base (host, null, returnType, mod | Modifiers.DEBUGGER_HIDDEN | Modifiers.COMPILER_GENERATED,
+                                 name, ParametersCompiled.EmptyReadOnlyParameters, null)
                        {
-                               this.Host = host;
-                       }
+                               this.host = host;
 
-                       protected override bool DoResolveInternal (EmitContext ec)
-                       {
-                               if (this is EnumeratorScopeInitializer)
-                                       state = Iterator.State.Start;
-                               else if (Host.Iterator.IsEnumerable)
-                                       state = Iterator.State.Uninitialized;
-                               else
-                                       state = Iterator.State.Start;
-
-                               return base.DoResolveInternal (ec);
+                               Block = new ToplevelBlock (Compiler, host.Iterator.Container.Toplevel, ParametersCompiled.EmptyReadOnlyParameters, Location);
                        }
 
-                       protected override void EmitScopeConstructor (EmitContext ec)
+                       public override EmitContext CreateEmitContext (ILGenerator ig)
                        {
-                               ec.ig.Emit (OpCodes.Ldc_I4, (int) state);
-                               base.EmitScopeConstructor (ec);
+                               EmitContext ec = new EmitContext (this, ig, MemberType);
+
+                               ec.CurrentAnonymousMethod = host.Iterator;
+                               return ec;
                        }
                }
 
-               protected class GetEnumeratorMethod : Method
+               class GetEnumeratorMethod : IteratorMethod
                {
-                       public IteratorHost Host;
-
-                       static MemberName GetMemberName (IteratorHost host, bool is_generic)
-                       {
-                               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 (IteratorHost host, bool is_generic)
-                               : base (host, null, is_generic ?
-                                       host.generic_enumerator_type : host.enumerator_type,
-                                       0, false, GetMemberName (host, is_generic),
-                                       Parameters.EmptyReadOnlyParameters, null)
+                       sealed class GetEnumeratorStatement : Statement
                        {
-                               this.Host = host;
+                               IteratorStorey host;
+                               IteratorMethod host_method;
 
-                               host.AddMethod (this);
+                               Expression new_storey;
 
-                               Block = new ToplevelBlock (host.Iterator.Container.Toplevel, null, Location);
-                               Block.AddStatement (new GetEnumeratorStatement (host, Type));
-                       }
+                               public GetEnumeratorStatement (IteratorStorey host, IteratorMethod host_method)
+                               {
+                                       this.host = host;
+                                       this.host_method = host_method;
+                                       loc = host_method.Location;
+                               }
 
-                       public override EmitContext CreateEmitContext (DeclSpace tc, ILGenerator ig)
-                       {
-                               EmitContext ec = new EmitContext (
-                                       this, tc, this.ds, Location, ig, MemberType, ModFlags, false);
+                               protected override void CloneTo (CloneContext clonectx, Statement target)
+                               {
+                                       throw new NotSupportedException ();
+                               }
 
-                               ec.CurrentAnonymousMethod = Host.Iterator;
-                               return ec;
-                       }
+                               public override bool Resolve (BlockContext ec)
+                               {
+                                       TypeExpression storey_type_expr = new TypeExpression (host.TypeBuilder, loc);
+                                       List<Expression> init = null;
+                                       if (host.hoisted_this != null) {
+                                               init = new List<Expression> (host.hoisted_params == null ? 1 : host.HoistedParameters.Count + 1);
+                                               HoistedThis ht = host.hoisted_this;
+                                               FieldExpr from = new FieldExpr (ht.Field, loc);
+                                               from.InstanceExpression = CompilerGeneratedThis.Instance;
+                                               init.Add (new ElementInitializer (ht.Field.Name, from, loc));
+                                       }
 
-                       protected class GetEnumeratorStatement : Statement
-                       {
-                               IteratorHost host;
-                               Expression type;
+                                       if (host.hoisted_params != null) {
+                                               if (init == null)
+                                                       init = new List<Expression> (host.HoistedParameters.Count);
 
-                               ExpressionStatement initializer;
-                               Expression cast;
-                               MethodInfo ce;
+                                               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];
 
-                               public GetEnumeratorStatement (IteratorHost host, Expression type)
-                               {
-                                       this.host = host;
-                                       this.type = type;
-                                       loc = host.Location;
-                               }
+                                                       FieldExpr from = new FieldExpr (hp_cp.Field, loc);
+                                                       from.InstanceExpression = CompilerGeneratedThis.Instance;
 
-                               public override bool Resolve (EmitContext ec)
-                               {
-                                       type = type.ResolveAsTypeTerminal (ec, false);
-                                       if ((type == null) || (type.Type == null))
-                                               return false;
+                                                       init.Add (new ElementInitializer (hp.Field.Name, from, loc));
+                                               }
+                                       }
 
-                                       initializer = host.GetEnumeratorInitializer (ec);
-                                       if (initializer == null)
-                                               return false;
+                                       if (init != null) {
+                                               new_storey = new NewInitialize (storey_type_expr, null,
+                                                       new CollectionOrObjectInitializers (init, loc), loc);
+                                       } else {
+                                               new_storey = new New (storey_type_expr, null, loc);
+                                       }
 
-                                       cast = new ClassCast (initializer, type.Type);
+                                       new_storey = new_storey.Resolve (ec);
+                                       if (new_storey != null)
+                                               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);
+                                               Type t = TypeManager.CoreLookupType (ec.Compiler, "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);
                                                }
                                        }
 
-                                       ce = TypeManager.int_interlocked_compare_exchange;
-
                                        ec.CurrentBranching.CurrentUsageVector.Goto ();
                                        return true;
                                }
@@ -451,55 +273,39 @@ namespace Mono.CSharp {
                                        Label label_init = ig.DefineLabel ();
 
                                        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);
-                                       ig.Emit (OpCodes.Call, ce);
+                                       ig.Emit (OpCodes.Ldflda, host.PC.Spec.MetaInfo);
+                                       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);
 
-                                       initializer.EmitStatement (ec);
-                                       cast.Emit (ec);
+                                       new_storey.Emit (ec);
                                        ig.Emit (OpCodes.Ret);
                                }
-                       }
-               }
-
-               protected class DisposeMethod : Method
-               {
-                       public IteratorHost Host;
 
-                       public DisposeMethod (IteratorHost host)
-                               : base (host, null, TypeManager.system_void_expr, Modifiers.PUBLIC,
-                                       false, new MemberName ("Dispose", host.Location),
-                                       Parameters.EmptyReadOnlyParameters, null)
-                       {
-                               this.Host = host;
-
-                               host.AddMethod (this);
-
-                               Block = new ToplevelBlock (host.Iterator.Block, null, Location);
-                               Block.AddStatement (new DisposeMethodStatement (Host.Iterator));
-
-                               Report.Debug (64, "DISPOSE METHOD", host, Block);
+                               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
+                               {
+                                       throw new NotSupportedException ();
+                               }
                        }
 
-                       public override EmitContext CreateEmitContext (DeclSpace tc, ILGenerator ig)
+                       public GetEnumeratorMethod (IteratorStorey host, FullNamedExpression returnType, MemberName name)
+                               : base (host, returnType, 0, name)
                        {
-                               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));
                        }
+               }
 
-                       protected class DisposeMethodStatement : Statement
+               class DisposeMethod : IteratorMethod
+               {
+                       sealed class DisposeMethodStatement : Statement
                        {
                                Iterator iterator;
 
@@ -509,7 +315,12 @@ namespace Mono.CSharp {
                                        this.loc = iterator.Location;
                                }
 
-                               public override bool Resolve (EmitContext ec)
+                               protected override void CloneTo (CloneContext clonectx, Statement target)
+                               {
+                                       throw new NotSupportedException ();
+                               }
+
+                               public override bool Resolve (BlockContext ec)
                                {
                                        return true;
                                }
@@ -518,103 +329,257 @@ namespace Mono.CSharp {
                                {
                                        iterator.EmitDispose (ec);
                                }
+
+                               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
+                               {
+                                       throw new NotSupportedException ();
+                               }
                        }
-               }
 
-               protected ScopeInitializer GetEnumeratorInitializer (EmitContext ec)
-               {
-                       ScopeInitializer init = new EnumeratorScopeInitializer (this);
-                       if (init.Resolve (ec) == null)
-                               throw new InternalErrorException ();
-                       return init;
+                       public DisposeMethod (IteratorStorey host)
+                               : base (host, TypeManager.system_void_expr, Modifiers.PUBLIC, new MemberName ("Dispose", host.Location))
+                       {
+                               host.AddMethod (this);
+
+                               Block = new ToplevelBlock (Compiler, host.Iterator.Container, ParametersCompiled.EmptyReadOnlyParameters, Location);
+                               Block.AddStatement (new DisposeMethodStatement (host.Iterator));
+                       }
                }
 
-               protected class EnumeratorScopeInitializer : IteratorHostInitializer
+               //
+               // Uses Method as method info
+               //
+               class DynamicMethodGroupExpr : MethodGroupExpr
                {
-                       IteratorHost host;
+                       readonly Method method;
 
-                       public EnumeratorScopeInitializer (IteratorHost host)
-                               : base (host)
+                       public DynamicMethodGroupExpr (Method method, Location loc)
+                               : base (null, loc)
                        {
-                               this.host = host;
+                               this.method = method;
+                               eclass = ExprClass.Unresolved;
                        }
 
-                       protected override bool DoResolveInternal (EmitContext ec)
+                       protected override Expression DoResolve (ResolveContext ec)
                        {
-                               type = host.IsGeneric ? host.CurrentType : host.TypeBuilder;
-                               return base.DoResolveInternal (ec);
+                               Methods = new MethodBase [] { method.MethodBuilder };
+                               type = method.Parent.TypeBuilder;
+                               InstanceExpression = new CompilerGeneratedThis (type, Location);
+                               return base.DoResolve (ec);
                        }
+               }
 
-                       protected override void DoEmit (EmitContext ec)
-                       {
-                               DoEmitInstance (ec);
-                       }
+               class DynamicFieldExpr : FieldExpr
+               {
+                       readonly Field field;
 
-                       protected override bool IsGetEnumerator {
-                               get { return true; }
+                       public DynamicFieldExpr (Field field, Location loc)
+                               : base (loc)
+                       {
+                               this.field = field;
                        }
 
-                       protected override void EmitParameterReference (EmitContext ec,
-                                                                       CapturedParameter cp)
+                       protected override Expression DoResolve (ResolveContext ec)
                        {
-                               ec.ig.Emit (OpCodes.Ldarg_0);
-                               ec.ig.Emit (OpCodes.Ldfld, cp.Field.FieldBuilder);
+                               spec = field.Spec;
+                               type = TypeManager.TypeToCoreType (spec.FieldType);
+                               InstanceExpression = new CompilerGeneratedThis (type, Location);
+                               return base.DoResolve (ec);
                        }
                }
 
-               protected class CurrentBlock : Statement {
-                       IteratorHost host;
-                       bool is_generic;
+               public readonly Iterator Iterator;
 
-                       public CurrentBlock (IteratorHost host, bool is_generic)
-                       {
-                               this.host = host;
-                               this.is_generic = is_generic;
-                               loc = host.Location;
+               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;
+
+               List<HoistedParameter> 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 IList<HoistedParameter> 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);
+
+                       var list = new List<FullNamedExpression> ();
+                       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);
+                               }
                        }
 
-                       public override bool Resolve (EmitContext ec)
-                       {
-                               if (TypeManager.invalid_operation_exception_ctor == null) {
-                                       Type t = TypeManager.CoreLookupType ("System", "InvalidOperationException", Kind.Class, true);
-                                       if (t != null)
-                                               TypeManager.invalid_operation_exception_ctor = TypeManager.GetPredefinedConstructor (t, loc, Type.EmptyTypes);
+                       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 List<HoistedParameter> (hoisted_params.Count);
+                               foreach (HoistedParameter hp in hoisted_params) {
+                                       hoisted_params_copy.Add (new HoistedParameter (hp, "<$>" + hp.Field.Name));
                                }
+                       }
 
-                               ec.CurrentBranching.CurrentUsageVector.Goto ();
-                               return true;
+                       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 DoEmit (EmitContext ec)
-                       {
-                               ILGenerator ig = ec.ig;
-                               Label label_ok = ig.DefineLabel ();
+               protected override void EmitHoistedParameters (EmitContext ec, IList<HoistedParameter> hoisted)
+               {
+                       base.EmitHoistedParameters (ec, hoisted);
+                       base.EmitHoistedParameters (ec, hoisted_params_copy);
+               }
 
-                               ig.Emit (OpCodes.Ldarg_0);
-                               ig.Emit (OpCodes.Ldfld, host.PC.FieldBuilder);
-                               ig.Emit (OpCodes.Ldc_I4, (int) Iterator.State.Start);
-                               ig.Emit (OpCodes.Bgt, label_ok);
+               void Define_Current (bool is_generic)
+               {
+                       TypeExpr type;
 
-                               ig.Emit (OpCodes.Newobj, TypeManager.invalid_operation_exception_ctor);
-                               ig.Emit (OpCodes.Throw);
+                       MemberName name = new MemberName (QualifiedAliasMember.GlobalAlias, "System", null, Location);
+                       name = new MemberName (name, "Collections", Location);
 
-                               ig.MarkLabel (label_ok);
-                               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);
+                       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 (Compiler, 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);
                }
-       }
 
-       public class Iterator : AnonymousContainer {
-               protected readonly ToplevelBlock OriginalBlock;
-               protected readonly IMethodData OriginalMethod;
-               protected ToplevelBlock block;
+               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 (Compiler, Location);
+
+                       Type ex_type = TypeManager.CoreLookupType (Compiler, "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));
+               }
+       }
+
+       //
+       // Iterators are implemented as hidden anonymous block
+       //
+       public class Iterator : AnonymousExpression {
+               public readonly IMethodData OriginalMethod;
+               AnonymousMethodMethod method;
+               public readonly TypeContainer Host;
                public readonly bool IsEnumerable;
-               public readonly bool IsStatic;
+               List<ResumableStatement> resume_points;
 
                //
                // The state as we generate the iterator
@@ -630,8 +595,17 @@ namespace Mono.CSharp {
                        get { return current_pc; }
                }
 
+               public Block Container {
+                       get { return OriginalMethod.Block; }
+               }
+
+               public GenericMethod GenericMethod {
+                       get { return OriginalMethod.GenericMethod; }
+               }
+
                public readonly Type OriginalIteratorType;
-               public readonly IteratorHost IteratorHost;
+
+               readonly IteratorStorey IteratorHost;
 
                public enum State {
                        Running = -3, // Used only in CurrentPC, never stored into $PC
@@ -650,11 +624,11 @@ namespace Mono.CSharp {
                        ILGenerator ig = ec.ig;
 
                        ig.Emit (OpCodes.Ldarg_0);
-                       ig.Emit (OpCodes.Ldfld, IteratorHost.PC.FieldBuilder);
+                       ig.Emit (OpCodes.Ldfld, IteratorHost.PC.Spec.MetaInfo);
 
                        ig.Emit (OpCodes.Ldarg_0);
                        IntConstant.EmitInt (ig, (int) State.After);
-                       ig.Emit (OpCodes.Stfld, IteratorHost.PC.FieldBuilder);
+                       ig.Emit (OpCodes.Stfld, IteratorHost.PC.Spec.MetaInfo);
 
                        // We only care if the PC is zero (start executing) or non-zero (don't do anything)
                        ig.Emit (OpCodes.Brtrue, move_next_error);
@@ -682,20 +656,20 @@ namespace Mono.CSharp {
 
                        current_pc = ec.GetTemporaryLocal (TypeManager.uint32_type);
                        ig.Emit (OpCodes.Ldarg_0);
-                       ig.Emit (OpCodes.Ldfld, IteratorHost.PC.FieldBuilder);
+                       ig.Emit (OpCodes.Ldfld, IteratorHost.PC.Spec.MetaInfo);
                        ig.Emit (OpCodes.Stloc, current_pc);
 
                        // We're actually in state 'running', but this is as good a PC value as any if there's an abnormal exit
                        ig.Emit (OpCodes.Ldarg_0);
                        IntConstant.EmitInt (ig, (int) State.After);
-                       ig.Emit (OpCodes.Stfld, IteratorHost.PC.FieldBuilder);
+                       ig.Emit (OpCodes.Stfld, IteratorHost.PC.Spec.MetaInfo);
 
                        Label [] labels = new Label [1 + resume_points.Count];
                        labels [0] = ig.DefineLabel ();
 
                        bool need_skip_finally = false;
                        for (int i = 0; i < resume_points.Count; ++i) {
-                               ResumableStatement s = (ResumableStatement) resume_points [i];
+                               ResumableStatement s = resume_points [i];
                                need_skip_finally |= s is ExceptionStatement;
                                labels [i+1] = s.PrepareForEmit (ec);
                        }
@@ -706,8 +680,6 @@ namespace Mono.CSharp {
                                ig.Emit (OpCodes.Stloc, skip_finally);
                        }
 
-                       ig.BeginExceptionBlock ();
-
                        SymbolWriter.StartIteratorDispatcher (ec.ig);
                        ig.Emit (OpCodes.Ldloc, current_pc);
                        ig.Emit (OpCodes.Switch, labels);
@@ -725,33 +697,17 @@ namespace Mono.CSharp {
 
                        ig.Emit (OpCodes.Ldarg_0);
                        IntConstant.EmitInt (ig, (int) State.After);
-                       ig.Emit (OpCodes.Stfld, IteratorHost.PC.FieldBuilder);
-
-                       Label end = ig.DefineLabel ();
-                       LocalBuilder retval = ec.GetTemporaryLocal (TypeManager.int32_type);
+                       ig.Emit (OpCodes.Stfld, IteratorHost.PC.Spec.MetaInfo);
 
                        ig.MarkLabel (move_next_error);
-                       ig.Emit (OpCodes.Ldc_I4_0); 
-                       ig.Emit (OpCodes.Stloc, retval);
-                       ig.Emit (OpCodes.Leave_S, end);
+                       ig.Emit (OpCodes.Ldc_I4_0);
+                       ig.Emit (OpCodes.Ret);
 
                        ig.MarkLabel (move_next_ok);
                        ig.Emit (OpCodes.Ldc_I4_1);
-                       ig.Emit (OpCodes.Stloc, retval);
-                       //ig.Emit (OpCodes.Leave_S, end);            // SRE automatically emits a Leave on the BeginFaultBlock
+                       ig.Emit (OpCodes.Ret);
 
                        SymbolWriter.EndIteratorDispatcher (ec.ig);
-
-                       ig.BeginFaultBlock ();
-
-                       ig.Emit (OpCodes.Ldarg_0);
-                       ig.Emit (OpCodes.Callvirt, IteratorHost.Dispose);
-
-                       ig.EndExceptionBlock ();
-
-                       ig.MarkLabel (end);
-                       ig.Emit (OpCodes.Ldloc, retval);
-                       ig.Emit (OpCodes.Ret);
                }
 
                public void EmitDispose (EmitContext ec)
@@ -778,13 +734,13 @@ namespace Mono.CSharp {
                        if (labels != null) {
                                current_pc = ec.GetTemporaryLocal (TypeManager.uint32_type);
                                ig.Emit (OpCodes.Ldarg_0);
-                               ig.Emit (OpCodes.Ldfld, IteratorHost.PC.FieldBuilder);
+                               ig.Emit (OpCodes.Ldfld, IteratorHost.PC.Spec.MetaInfo);
                                ig.Emit (OpCodes.Stloc, current_pc);
                        }
 
                        ig.Emit (OpCodes.Ldarg_0);
                        IntConstant.EmitInt (ig, (int) State.After);
-                       ig.Emit (OpCodes.Stfld, IteratorHost.PC.FieldBuilder);
+                       ig.Emit (OpCodes.Stfld, IteratorHost.PC.Spec.MetaInfo);
 
                        if (labels != null) {
                                //SymbolWriter.StartIteratorDispatcher (ec.ig);
@@ -799,11 +755,11 @@ namespace Mono.CSharp {
                        ig.MarkLabel (end);
                }
 
-               ArrayList resume_points;
-               public int AddResumePoint (ResumableStatement stmt, Location loc)
+               public int AddResumePoint (ResumableStatement stmt)
                {
                        if (resume_points == null)
-                               resume_points = new ArrayList ();
+                               resume_points = new List<ResumableStatement> ();
+
                        resume_points.Add (stmt);
                        return resume_points.Count;
                }
@@ -818,12 +774,12 @@ namespace Mono.CSharp {
                        // Store the new current
                        ig.Emit (OpCodes.Ldarg_0);
                        expr.Emit (ec);
-                       ig.Emit (OpCodes.Stfld, IteratorHost.CurrentField.FieldBuilder);
+                       ig.Emit (OpCodes.Stfld, IteratorHost.CurrentField.Spec.MetaInfo);
 
                        // store resume program-counter
                        ig.Emit (OpCodes.Ldarg_0);
                        IntConstant.EmitInt (ig, resume_pc);
-                       ig.Emit (OpCodes.Stfld, IteratorHost.PC.FieldBuilder);
+                       ig.Emit (OpCodes.Stfld, IteratorHost.PC.Spec.MetaInfo);
 
                        // mark finally blocks as disabled
                        if (unwind_protect && skip_finally != null) {
@@ -845,56 +801,26 @@ namespace Mono.CSharp {
                        get { return true; }
                }
 
-               public override RootScopeInfo RootScope {
-                       get { return IteratorHost; }
-               }
-
-               public override ScopeInfo Scope {
+               public override AnonymousMethodStorey Storey {
                        get { return IteratorHost; }
                }
 
                //
                // Our constructor
                //
-               private Iterator (IMethodData m_container, DeclSpace host, GenericMethod generic,
-                                int modifiers, Type iterator_type, bool is_enumerable)
-                       : base (host, generic, m_container.ParameterInfo,
-                               new ToplevelBlock (m_container.ParameterInfo, m_container.Location),
-                               m_container.Block, TypeManager.bool_type, modifiers,
-                               m_container.Location)
+               private Iterator (CompilerContext ctx, IMethodData method, TypeContainer host, Type iterator_type, bool is_enumerable)
+                       : base (
+                               new ToplevelBlock (ctx, method.Block, ParametersCompiled.EmptyReadOnlyParameters, method.Block.StartLocation),
+                               TypeManager.bool_type,
+                               method.Location)
                {
-                       this.OriginalBlock = m_container.Block;
-                       this.OriginalMethod = m_container;
+                       this.OriginalMethod = method;
                        this.OriginalIteratorType = iterator_type;
                        this.IsEnumerable = is_enumerable;
+                       this.Host = host;
+                       this.type = method.ReturnType;
 
-                       Report.Debug (64, "NEW ITERATOR", host, generic, OriginalBlock,
-                                     Container, Block);
-
-                       IteratorHost = new IteratorHost (this);
-                       Block.CreateIteratorHost (IteratorHost);
-
-                       OriginalBlock.ReParent (Container.Toplevel);
-
-                       m_container.Block = Container.Toplevel;
-
-                       OriginalBlock.MakeIterator (this);
-               }
-
-               protected class TestStatement : Statement
-               {
-                       public override bool Resolve (EmitContext ec)
-                       {
-                               return true;
-                       }
-
-                       protected override void DoEmit (EmitContext ec)
-                       {
-                               ec.ig.Emit (OpCodes.Nop);
-                               ec.ig.Emit (OpCodes.Neg);
-                               ec.ig.Emit (OpCodes.Pop);
-                               ec.ig.Emit (OpCodes.Ret);
-                       }
+                       IteratorHost = Block.ChangeToIterator (this, method.Block);
                }
 
                public override string GetSignatureForError ()
@@ -902,136 +828,100 @@ namespace Mono.CSharp {
                        return OriginalMethod.GetSignatureForError ();
                }
 
-               public override bool Define (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       Report.Debug (64, "RESOLVE ITERATOR", this, Container, Block);
-
-                       Parameters parameters = OriginalMethod.ParameterInfo;
-                       for (int i = 0; i < parameters.Count; i++){
-                               Parameter.Modifier mod = parameters.ParameterModifier (i);
-                               if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0){
-                                       Report.Error (1623, Location,
-                                                     "Iterators cannot have ref or out parameters");
-                                       return false;
-                               }
-
-                               if ((mod & Parameter.Modifier.ARGLIST) != 0) {
-                                       Report.Error (1636, Location,
-                                                     "__arglist is not allowed in parameter list " +
-                                                     "of iterators");
-                                       return false;
-                               }
-
-                               if (parameters.ParameterType (i).IsPointer) {
-                                       Report.Error (1637, Location,
-                                                     "Iterators cannot have unsafe parameters or " +
-                                                     "yield types");
-                                       return false;
-                               }
-                       }
-
-                       if ((ModFlags & Modifiers.UNSAFE) != 0) {
-                               Report.Error (1629, Location, "Unsafe code may not appear in iterators");
-                               return false;
-                       }
-
-                       if (!base.Define (ec))
-                               return false;
-
-                       Report.Debug (64, "RESOLVE ITERATOR #1", this, method, method.Parent,
-                                     RootScope, ec);
+                       method = new AnonymousMethodMethod (Storey,
+                               this, Storey, null, TypeManager.system_boolean_expr,
+                               Modifiers.PUBLIC, OriginalMethod.GetSignatureForError (),
+                               new MemberName ("MoveNext", Location),
+                               ParametersCompiled.EmptyReadOnlyParameters);
 
-                       if (!RootScope.ResolveType ())
-                               return false;
-                       if (!RootScope.ResolveMembers ())
-                               return false;
-                       if (!RootScope.DefineMembers ())
-                               return false;
+                       if (Compatible (ec) == null)
+                               return null;
 
-                       ExpressionStatement scope_init = RootScope.GetScopeInitializer (ec);
-                       Container.AddStatement (new StatementExpression (scope_init));
-                       Expression cast = new ClassCast (scope_init, OriginalMethod.ReturnType);
-                       Container.AddStatement (new NoCheckReturn (cast));
+                       IteratorHost.DefineIteratorMembers ();
 
-                       return true;
+                       eclass = ExprClass.Value;
+                       return this;
                }
 
-               protected override Method DoCreateMethodHost (EmitContext ec)
+               public override void Emit (EmitContext ec)
                {
-                       Report.Debug (128, "CREATE METHOD HOST", this, IteratorHost);
+                       //
+                       // Load Iterator storey instance
+                       //
+                       method.Storey.Instance.Emit (ec);
 
-                       MemberCore mc = ec.ResolveContext as MemberCore;
+                       //
+                       // Initialize iterator PC when it's unitialized
+                       //
+                       if (IsEnumerable) {
+                               ILGenerator ig = ec.ig;
+                               ig.Emit (OpCodes.Dup);
+                               IntConstant.EmitInt (ig, (int)State.Uninitialized);
 
-                       IteratorHost.CaptureScopes ();
+                               FieldInfo field = IteratorHost.PC.Spec.MetaInfo;
+                               if (Storey.MemberName.IsGeneric)
+                                       field = TypeBuilder.GetField (Storey.Instance.Type, field);
 
-                       return new AnonymousMethodMethod (
-                               this, RootScope, null, TypeManager.system_boolean_expr,
-                               Modifiers.PUBLIC, mc.GetSignatureForError (),
-                               new MemberName ("MoveNext", Location),
-                               Parameters.EmptyReadOnlyParameters);
+                               ig.Emit (OpCodes.Stfld, field);
+                       }
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
-                       throw new NotSupportedException ();
-               }
-
-               public Type IteratorType {
-                       get { return IteratorHost.IteratorType; }
+                       throw new NotSupportedException ("ET");
                }
 
-               //
-               // This return statement tricks return into not flagging an error for being
-               // used in a Yields method
-               //
-               class NoCheckReturn : Statement {
-                       public Expression Expr;
-               
-                       public NoCheckReturn (Expression expr)
-                       {
-                               Expr = expr;
-                               loc = expr.Location;
-                       }
-
-                       public override bool Resolve (EmitContext ec)
-                       {
-                               Expr = Expr.Resolve (ec);
-                               if (Expr == null)
-                                       return false;
-
-                               ec.CurrentBranching.CurrentUsageVector.Goto ();
-
-                               return true;
-                       }
-
-                       protected override void DoEmit (EmitContext ec)
-                       {
-                               Expr.Emit (ec);
-                               ec.ig.Emit (OpCodes.Ret);
-                       }
-               }
-
-               public static Iterator CreateIterator (IMethodData method, DeclSpace parent,
-                                                      GenericMethod generic, int modifiers)
+               public static void CreateIterator (IMethodData method, TypeContainer parent, Modifiers modifiers, CompilerContext ctx)
                {
                        bool is_enumerable;
                        Type iterator_type;
 
                        Type ret = method.ReturnType;
                        if (ret == null)
-                               return null;
+                               return;
 
                        if (!CheckType (ret, out iterator_type, out is_enumerable)) {
-                               Report.Error (1624, method.Location,
+                               ctx.Report.Error (1624, method.Location,
                                              "The body of `{0}' cannot be an iterator block " +
                                              "because `{1}' is not an iterator interface type",
                                              method.GetSignatureForError (),
                                              TypeManager.CSharpName (ret));
-                               return null;
+                               return;
                        }
 
-                       return new Iterator (method, parent, generic, modifiers,
-                                            iterator_type, is_enumerable);
+                       ParametersCompiled parameters = method.ParameterInfo;
+                       for (int i = 0; i < parameters.Count; i++) {
+                               Parameter p = parameters [i];
+                               Parameter.Modifier mod = p.ModFlags;
+                               if ((mod & Parameter.Modifier.ISBYREF) != 0) {
+                                       ctx.Report.Error (1623, p.Location,
+                                               "Iterators cannot have ref or out parameters");
+                                       return;
+                               }
+
+                               if (p is ArglistParameter) {
+                                       ctx.Report.Error (1636, method.Location,
+                                               "__arglist is not allowed in parameter list of iterators");
+                                       return;
+                               }
+
+                               if (parameters.Types [i].IsPointer) {
+                                       ctx.Report.Error (1637, p.Location,
+                                                         "Iterators cannot have unsafe parameters or " +
+                                                         "yield types");
+                                       return;
+                               }
+                       }
+
+                       if ((modifiers & Modifiers.UNSAFE) != 0) {
+                               ctx.Report.Error (1629, method.Location, "Unsafe code may not appear in iterators");
+                               return;
+                       }
+
+                       Iterator iter = new Iterator (ctx, method, parent, iterator_type, is_enumerable);
+                       iter.Storey.DefineType ();
                }
 
                static bool CheckType (Type ret, out Type original_iterator_type, out bool is_enumerable)
@@ -1050,25 +940,25 @@ namespace Mono.CSharp {
                                return true;
                        }
 
-#if GMCS_SOURCE
-                       if (!ret.IsGenericType)
+                       if (!TypeManager.IsGenericType (ret))
                                return false;
 
                        Type[] args = TypeManager.GetTypeArguments (ret);
                        if (args.Length != 1)
                                return false;
 
-                       Type gt = ret.GetGenericTypeDefinition ();
+                       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;
-                       } else if (gt == TypeManager.generic_ienumerator_type) {
-                               original_iterator_type = args [0];
+                       }
+                       
+                       if (gt == TypeManager.generic_ienumerator_type) {
+                               original_iterator_type = TypeManager.TypeToCoreType (args [0]);
                                is_enumerable = false;
                                return true;
                        }
-#endif
 
                        return false;
                }