[fix] #631810: Form.DialogResult needs to call its close events *before* closing.
[mono.git] / mcs / mcs / iterators.cs
index b8feb0c40adaeff6fb6431402004b98cb426883c..0cfccd0759e30e75ebce4eef3323a71557d7da67 100644 (file)
@@ -15,7 +15,7 @@
 //
 
 using System;
-using System.Collections;
+using System.Collections.Generic;
 using System.Reflection;
 using System.Reflection.Emit;
 
@@ -24,7 +24,7 @@ namespace Mono.CSharp {
        public class Yield : ResumableStatement {
                Expression expr;
                bool unwind_protect;
-
+               Iterator iterator;
                int resume_pc;
 
                public Yield (Expression expr, Location l)
@@ -33,22 +33,10 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
-               public static bool CheckContext (EmitContext ec, Location loc)
+               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;
@@ -57,12 +45,7 @@ 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)
                {
                        expr = expr.Resolve (ec);
                        if (expr == null)
@@ -74,7 +57,7 @@ namespace Mono.CSharp {
                        if (!CheckContext (ec, loc))
                                return false;
 
-                       Iterator iterator = ec.CurrentIterator;
+                       iterator = ec.CurrentIterator;
                        if (expr.Type != iterator.OriginalIteratorType) {
                                expr = Convert.ImplicitConversionRequired (
                                        ec, expr, iterator.OriginalIteratorType, loc);
@@ -90,7 +73,7 @@ namespace Mono.CSharp {
 
                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)
@@ -101,13 +84,16 @@ 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");
                }
@@ -117,89 +103,20 @@ namespace Mono.CSharp {
                        throw new NotSupportedException ();
                }
 
-               protected override bool DoResolve (EmitContext ec)
+               protected override bool DoResolve (BlockContext ec)
                {
+                       iterator = ec.CurrentIterator;
                        return Yield.CheckContext (ec, loc);
                }
 
                protected override void DoEmit (EmitContext ec)
                {
-                       ec.CurrentIterator.EmitYieldBreak (ec.ig, unwind_protect);
-               }
-
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
-               {
-                       // nothing to do
-               }
-       }
-
-       //
-       // Wraps method block into iterator wrapper block
-       //
-       class IteratorStatement : Statement
-       {
-               Iterator iterator;
-               Block 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);
-                       bool ok = original_block.Resolve (ec);
-                       ec.EndFlowBranching ();
-                       return ok;
-               }
-
-               protected override void DoEmit (EmitContext ec)
-               {
-                       iterator.EmitMoveNext (ec, original_block);
-               }
-
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
-               {
-                       original_block.MutateHoistedGenericType (storey);
-                       iterator.MutateHoistedGenericType (storey);
+                       iterator.EmitYieldBreak (ec, unwind_protect);
                }
        }
 
        public class IteratorStorey : AnonymousMethodStorey
        {
-               class IteratorMethod : Method
-               {
-                       readonly IteratorStorey host;
-
-                       public IteratorMethod (IteratorStorey host, FullNamedExpression returnType, int mod, MemberName name)
-                               : base (host, null, returnType, mod | Modifiers.DEBUGGER_HIDDEN | Modifiers.COMPILER_GENERATED,
-                                 name, Parameters.EmptyReadOnlyParameters, null)
-                       {
-                               this.host = host;
-
-                               Block = new ToplevelBlock (host.Iterator.Container.Toplevel, Parameters.EmptyReadOnlyParameters, Location);
-                       }
-
-                       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;
-                       }
-               }
-
                class GetEnumeratorMethod : IteratorMethod
                {
                        sealed class GetEnumeratorStatement : Statement
@@ -221,27 +138,27 @@ namespace Mono.CSharp {
                                        throw new NotSupportedException ();
                                }
 
-                               public override bool Resolve (EmitContext ec)
+                               public override bool Resolve (BlockContext ec)
                                {
-                                       TypeExpression storey_type_expr = new TypeExpression (host.TypeBuilder, loc);
-                                       ArrayList init = null;
+                                       TypeExpression storey_type_expr = new TypeExpression (host.Definition, loc);
+                                       List<Expression> init = null;
                                        if (host.hoisted_this != null) {
-                                               init = new ArrayList (host.hoisted_params == null ? 1 : host.HoistedParameters.Count + 1);
+                                               init = new List<Expression> (host.hoisted_params == null ? 1 : host.HoistedParameters.Count + 1);
                                                HoistedThis ht = host.hoisted_this;
-                                               FieldExpr from = new FieldExpr (ht.Field.FieldBuilder, loc);
+                                               FieldExpr from = new FieldExpr (ht.Field, loc);
                                                from.InstanceExpression = CompilerGeneratedThis.Instance;
                                                init.Add (new ElementInitializer (ht.Field.Name, from, loc));
                                        }
 
                                        if (host.hoisted_params != null) {
                                                if (init == null)
-                                                       init = new ArrayList (host.HoistedParameters.Count);
+                                                       init = new List<Expression> (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);
+                                                       FieldExpr from = new FieldExpr (hp_cp.Field, loc);
                                                        from.InstanceExpression = CompilerGeneratedThis.Instance;
 
                                                        init.Add (new ElementInitializer (hp.Field.Name, from, loc));
@@ -249,10 +166,10 @@ namespace Mono.CSharp {
                                        }
 
                                        if (init != null) {
-                                               new_storey = new NewInitialize (storey_type_expr, new ArrayList (0),
+                                               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);
@@ -260,11 +177,20 @@ namespace Mono.CSharp {
                                                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);
+                                               TypeSpec t = TypeManager.CoreLookupType (ec.Compiler, "System.Threading", "Interlocked", MemberKind.Class, true);
                                                if (t != null) {
-                                                       TypeManager.int_interlocked_compare_exchange = TypeManager.GetPredefinedMethod (
-                                                               t, "CompareExchange", loc, TypeManager.int32_type,
-                                                               TypeManager.int32_type, TypeManager.int32_type);
+                                                       var p = new ParametersImported (
+                                                               new[] {
+                                                                       new ParameterData (null, Parameter.Modifier.REF),
+                                                                       new ParameterData (null, Parameter.Modifier.NONE),
+                                                                       new ParameterData (null, Parameter.Modifier.NONE)
+                                                               },
+                                                               new[] {
+                                                                       TypeManager.int32_type, TypeManager.int32_type, TypeManager.int32_type
+                                                               },
+                                                               false);
+                                                       var f = new MemberFilter ("CompareExchange", 0, MemberKind.Method, p, TypeManager.int32_type);
+                                                       TypeManager.int_interlocked_compare_exchange = TypeManager.GetPredefinedMethod (t, f, loc);
                                                }
                                        }
 
@@ -274,35 +200,29 @@ namespace Mono.CSharp {
 
                                protected override void DoEmit (EmitContext ec)
                                {
-                                       ILGenerator ig = ec.ig;
-                                       Label label_init = ig.DefineLabel ();
+                                       Label label_init = ec.DefineLabel ();
 
-                                       ig.Emit (OpCodes.Ldarg_0);
-                                       ig.Emit (OpCodes.Ldflda, host.PC.FieldBuilder);
-                                       IntConstant.EmitInt (ig, (int) Iterator.State.Start);
-                                       IntConstant.EmitInt (ig, (int) Iterator.State.Uninitialized);
-                                       ig.Emit (OpCodes.Call, TypeManager.int_interlocked_compare_exchange);
+                                       ec.Emit (OpCodes.Ldarg_0);
+                                       ec.Emit (OpCodes.Ldflda, host.PC.Spec);
+                                       ec.EmitInt ((int) Iterator.State.Start);
+                                       ec.EmitInt ((int) Iterator.State.Uninitialized);
+                                       ec.Emit (OpCodes.Call, TypeManager.int_interlocked_compare_exchange);
 
-                                       IntConstant.EmitInt (ig, (int) Iterator.State.Uninitialized);
-                                       ig.Emit (OpCodes.Bne_Un_S, label_init);
+                                       ec.EmitInt ((int) Iterator.State.Uninitialized);
+                                       ec.Emit (OpCodes.Bne_Un_S, label_init);
 
-                                       ig.Emit (OpCodes.Ldarg_0);
-                                       ig.Emit (OpCodes.Ret);
+                                       ec.Emit (OpCodes.Ldarg_0);
+                                       ec.Emit (OpCodes.Ret);
 
-                                       ig.MarkLabel (label_init);
+                                       ec.MarkLabel (label_init);
 
                                        new_storey.Emit (ec);
-                                       ig.Emit (OpCodes.Ret);
-                               }
-
-                               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
-                               {
-                                       throw new NotSupportedException ();
+                                       ec.Emit (OpCodes.Ret);
                                }
                        }
 
                        public GetEnumeratorMethod (IteratorStorey host, FullNamedExpression returnType, MemberName name)
-                               : base (host, returnType, 0, name)
+                               : base (host, returnType, Modifiers.DEBUGGER_HIDDEN, name)
                        {
                                Block.AddStatement (new GetEnumeratorStatement (host, this));
                        }
@@ -325,7 +245,7 @@ namespace Mono.CSharp {
                                        throw new NotSupportedException ();
                                }
 
-                               public override bool Resolve (EmitContext ec)
+                               public override bool Resolve (BlockContext ec)
                                {
                                        return true;
                                }
@@ -334,19 +254,14 @@ namespace Mono.CSharp {
                                {
                                        iterator.EmitDispose (ec);
                                }
-
-                               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
-                               {
-                                       throw new NotSupportedException ();
-                               }
                        }
 
                        public DisposeMethod (IteratorStorey host)
-                               : base (host, TypeManager.system_void_expr, Modifiers.PUBLIC, new MemberName ("Dispose", host.Location))
+                               : base (host, new TypeExpression (TypeManager.void_type, host.Location), Modifiers.PUBLIC | Modifiers.DEBUGGER_HIDDEN,
+                                       new MemberName ("Dispose", host.Location))
                        {
                                host.AddMethod (this);
 
-                               Block = new ToplevelBlock (host.Iterator.Container, Parameters.EmptyReadOnlyParameters, Location);
                                Block.AddStatement (new DisposeMethodStatement (host.Iterator));
                        }
                }
@@ -359,15 +274,16 @@ namespace Mono.CSharp {
                        readonly Method method;
 
                        public DynamicMethodGroupExpr (Method method, Location loc)
-                               : base (null, loc)
+                               : base ((IList<MemberSpec>) null, null, loc)
                        {
                                this.method = method;
+                               eclass = ExprClass.Unresolved;
                        }
 
-                       public override Expression DoResolve (EmitContext ec)
+                       protected override Expression DoResolve (ResolveContext ec)
                        {
-                               Methods = new MethodBase [] { method.MethodBuilder };
-                               type = method.Parent.TypeBuilder;
+                               Methods = new List<MemberSpec> (1) { method.Spec };
+                               type = method.Parent.Definition;
                                InstanceExpression = new CompilerGeneratedThis (type, Location);
                                return base.DoResolve (ec);
                        }
@@ -383,10 +299,10 @@ namespace Mono.CSharp {
                                this.field = field;
                        }
 
-                       public override Expression DoResolve (EmitContext ec)
+                       protected override Expression DoResolve (ResolveContext ec)
                        {
-                               FieldInfo = field.FieldBuilder;
-                               type = TypeManager.TypeToCoreType (FieldInfo.FieldType);
+                               spec = field.Spec;
+                               type = spec.MemberType;
                                InstanceExpression = new CompilerGeneratedThis (type, Location);
                                return base.DoResolve (ec);
                        }
@@ -400,23 +316,18 @@ namespace Mono.CSharp {
 
                TypeExpr enumerator_type;
                TypeExpr enumerable_type;
-#if GMCS_SOURCE
                TypeArguments generic_args;
                TypeExpr generic_enumerator_type;
                TypeExpr generic_enumerable_type;
-#else
-               const TypeArguments generic_args = null;
-#endif
 
-               ArrayList hoisted_params_copy;
+               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")
+                       : base (iterator.Container.ParametersBlock, iterator.Host,
+                         iterator.OriginalMethod as MemberBase, iterator.GenericMethod == null ? null : iterator.GenericMethod.CurrentTypeParameters, "Iterator")
                {
                        this.Iterator = iterator;
-                       HasHoistedVariables = true;
                }
 
                public Field PC {
@@ -427,30 +338,31 @@ namespace Mono.CSharp {
                        get { return current_field; }
                }
 
-               public ArrayList HoistedParameters {
+               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);
+                       var mtype = Iterator.OriginalIteratorType;
+                       if (Mutator != null)
+                               mtype = Mutator.Mutate (mtype);
 
-#if GMCS_SOURCE
+                       iterator_type_expr = new TypeExpression (mtype, Location);
                        generic_args = new TypeArguments (iterator_type_expr);
-#endif
 
-                       ArrayList list = new ArrayList ();
+                       var list = new List<FullNamedExpression> ();
                        if (Iterator.IsEnumerable) {
                                enumerable_type = new TypeExpression (
                                        TypeManager.ienumerable_type, Location);
                                list.Add (enumerable_type);
 
-#if GMCS_SOURCE
-                               generic_enumerable_type = new GenericTypeExpr (
-                                       TypeManager.generic_ienumerable_type,
-                                       generic_args, Location);
-                               list.Add (generic_enumerable_type);
-#endif
+                               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 (
@@ -459,26 +371,32 @@ namespace Mono.CSharp {
 
                        list.Add (new TypeExpression (TypeManager.idisposable_type, Location));
 
-#if GMCS_SOURCE
-                       generic_enumerator_type = new GenericTypeExpr (
-                               TypeManager.generic_ienumerator_type,
-                               generic_args, Location);
-                       list.Add (generic_enumerator_type);
-#endif
+                       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)
+               protected override string GetVariableMangledName (LocalVariable local_info)
                {
                        return "<" + local_info.Name + ">__" + local_name_idx++.ToString ();
                }
 
-               public void DefineIteratorMembers ()
+               protected override bool DoDefineMembers ()
                {
-                       pc_field = AddCompilerGeneratedField ("$PC", TypeManager.system_int32_expr);
+                       DefineIteratorMembers ();
+                       return base.DoDefineMembers ();
+               }
+
+               void DefineIteratorMembers ()
+               {
+                       pc_field = AddCompilerGeneratedField ("$PC", new TypeExpression (TypeManager.int32_type, Location));
                        current_field = AddCompilerGeneratedField ("$current", iterator_type_expr);
 
                        if (hoisted_params != null) {
@@ -489,45 +407,48 @@ namespace Mono.CSharp {
                                //
                                // TODO: Do it for assigned/modified parameters only
                                //
-                               hoisted_params_copy = new ArrayList (hoisted_params.Count);
+                               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));
                                }
                        }
 
-#if GMCS_SOURCE
-                       Define_Current (true);
-#endif
+                       if (generic_enumerator_type != null)
+                               Define_Current (true);
+
                        Define_Current (false);
                        new DisposeMethod (this);
                        Define_Reset ();
 
                        if (Iterator.IsEnumerable) {
-                               MemberName name = new MemberName (
-                                       new MemberName ("System.Collections.IEnumerable", Location), "GetEnumerator", Location);
-
-#if GMCS_SOURCE
-                               Method get_enumerator = new IteratorMethod (this, enumerator_type, 0, name);
-
-                               name = new MemberName (
-                                       new MemberName ("System.Collections.Generic.IEnumerable", generic_args, Location), "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), new ArrayList (0)), Location));
-
-                               AddMethod (get_enumerator);
-                               AddMethod (gget_enumerator);
-#else
-                               AddMethod (new GetEnumeratorMethod (this, enumerator_type, name));
-#endif
+                               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)
+               protected override void EmitHoistedParameters (EmitContext ec, IList<HoistedParameter> hoisted)
                {
                        base.EmitHoistedParameters (ec, hoisted);
                        base.EmitHoistedParameters (ec, hoisted_params_copy);
@@ -535,55 +456,108 @@ namespace Mono.CSharp {
 
                void Define_Current (bool is_generic)
                {
-                       MemberName left;
                        TypeExpr type;
 
+                       MemberName name = new MemberName (QualifiedAliasMember.GlobalAlias, "System", null, Location);
+                       name = new MemberName (name, "Collections", Location);
+
                        if (is_generic) {
-                               left = new MemberName (
-                                       "System.Collections.Generic.IEnumerator",
-                                       generic_args, Location);
+                               name = new MemberName (name, "Generic", Location);
+                               name = new MemberName (name, "IEnumerator", generic_args, Location);
                                type = iterator_type_expr;
                        } else {
-                               left = new MemberName ("System.Collections.IEnumerator", Location);
-                               type = TypeManager.system_object_expr;
+                               name = new MemberName (name, "IEnumerator");
+                               type = new TypeExpression (TypeManager.object_type, Location);
                        }
 
-                       MemberName name = new MemberName (left, "Current", Location);
+                       name = new MemberName (name, "Current", Location);
 
-                       ToplevelBlock get_block = new ToplevelBlock (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);
+                       current.Get = new Property.GetMethod (current, 0, null, Location);
+                       current.Get.Block = get_block;
 
-                       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,
+                               this, null, new TypeExpression (TypeManager.void_type, Location),
                                Modifiers.PUBLIC | Modifiers.DEBUGGER_HIDDEN,
                                new MemberName ("Reset", Location),
-                               Parameters.EmptyReadOnlyParameters, null);
+                               ParametersCompiled.EmptyReadOnlyParameters, null);
                        AddMethod (reset);
 
-                       reset.Block = new ToplevelBlock (Location);
+                       reset.Block = new ToplevelBlock (Compiler, Location);
+
+                       TypeSpec ex_type = TypeManager.CoreLookupType (Compiler, "System", "NotSupportedException", MemberKind.Class, true);
+                       if (ex_type == null)
+                               return;
+
+                       reset.Block.AddStatement (new Throw (new New (new TypeExpression (ex_type, Location), null, Location), Location));
+               }
+       }
+
+       class IteratorMethod : Method
+       {
+               readonly IteratorStorey host;
 
-                       TypeExpr ex_type = new TypeLookupExpression ("System.NotSupportedException");
-                       reset.Block.AddStatement (new Throw (new New (ex_type, null, Location), Location));
+               public IteratorMethod (IteratorStorey host, FullNamedExpression returnType, Modifiers mod, MemberName name)
+                       : base (host, null, returnType, mod | Modifiers.COMPILER_GENERATED,
+                         name, ParametersCompiled.EmptyReadOnlyParameters, null)
+               {
+                       this.host = host;
+
+                       Block = new ToplevelBlock (host.Compiler, ParametersCompiled.EmptyReadOnlyParameters, Location);
+               }
+
+               public override EmitContext CreateEmitContext (ILGenerator ig)
+               {
+                       EmitContext ec = new EmitContext (this, ig, MemberType);
+
+                       ec.CurrentAnonymousMethod = host.Iterator;
+                       return ec;
                }
        }
 
        //
        // Iterators are implemented as hidden anonymous block
        //
-       public class Iterator : AnonymousExpression {
+       public class Iterator : AnonymousExpression
+       {
+               sealed class MoveNextMethodStatement : Statement
+               {
+                       Iterator iterator;
+
+                       public MoveNextMethodStatement (Iterator iterator)
+                       {
+                               this.iterator = iterator;
+                               this.loc = iterator.Location;
+                       }
+
+                       protected override void CloneTo (CloneContext clonectx, Statement target)
+                       {
+                               throw new NotSupportedException ();
+                       }
+
+                       public override bool Resolve (BlockContext ec)
+                       {
+                               return true;
+                       }
+
+                       protected override void DoEmit (EmitContext ec)
+                       {
+                               iterator.EmitMoveNext (ec);
+                       }
+               }
+
                public readonly IMethodData OriginalMethod;
-               AnonymousMethodMethod method;
                public readonly TypeContainer Host;
                public readonly bool IsEnumerable;
+               List<ResumableStatement> resume_points;
 
                //
                // The state as we generate the iterator
@@ -607,9 +581,9 @@ namespace Mono.CSharp {
                        get { return OriginalMethod.GenericMethod; }
                }
 
-               public readonly Type OriginalIteratorType;
+               public readonly TypeSpec OriginalIteratorType;
 
-               readonly IteratorStorey IteratorHost;
+               IteratorStorey IteratorHost;
 
                public enum State {
                        Running = -3, // Used only in CurrentPC, never stored into $PC
@@ -618,112 +592,101 @@ namespace Mono.CSharp {
                        Start = 0
                }
 
-               public override void AddStoreyReference (AnonymousMethodStorey storey)
-               {
-                       // do nothing
-               }
-
-               public void EmitYieldBreak (ILGenerator ig, bool unwind_protect)
+               public void EmitYieldBreak (EmitContext ec, bool unwind_protect)
                {
-                       ig.Emit (unwind_protect ? OpCodes.Leave : OpCodes.Br, move_next_error);
+                       ec.Emit (unwind_protect ? OpCodes.Leave : OpCodes.Br, move_next_error);
                }
 
                void EmitMoveNext_NoResumePoints (EmitContext ec, Block original_block)
                {
-                       ILGenerator ig = ec.ig;
-
-                       ig.Emit (OpCodes.Ldarg_0);
-                       ig.Emit (OpCodes.Ldfld, IteratorHost.PC.FieldBuilder);
+                       ec.Emit (OpCodes.Ldarg_0);
+                       ec.Emit (OpCodes.Ldfld, IteratorHost.PC.Spec);
 
-                       ig.Emit (OpCodes.Ldarg_0);
-                       IntConstant.EmitInt (ig, (int) State.After);
-                       ig.Emit (OpCodes.Stfld, IteratorHost.PC.FieldBuilder);
+                       ec.Emit (OpCodes.Ldarg_0);
+                       ec.EmitInt ((int) State.After);
+                       ec.Emit (OpCodes.Stfld, IteratorHost.PC.Spec);
 
                        // We only care if the PC is zero (start executing) or non-zero (don't do anything)
-                       ig.Emit (OpCodes.Brtrue, move_next_error);
+                       ec.Emit (OpCodes.Brtrue, move_next_error);
 
-                       SymbolWriter.StartIteratorBody (ec.ig);
+                       SymbolWriter.StartIteratorBody (ec);
                        original_block.Emit (ec);
-                       SymbolWriter.EndIteratorBody (ec.ig);
+                       SymbolWriter.EndIteratorBody (ec);
 
-                       ig.MarkLabel (move_next_error);
-                       ig.Emit (OpCodes.Ldc_I4_0);
-                       ig.Emit (OpCodes.Ret);
+                       ec.MarkLabel (move_next_error);
+                       ec.Emit (OpCodes.Ldc_I4_0);
+                       ec.Emit (OpCodes.Ret);
                }
 
-               internal void EmitMoveNext (EmitContext ec, Block original_block)
+               void EmitMoveNext (EmitContext ec)
                {
-                       ILGenerator ig = ec.ig;
-
-                       move_next_ok = ig.DefineLabel ();
-                       move_next_error = ig.DefineLabel ();
+                       move_next_ok = ec.DefineLabel ();
+                       move_next_error = ec.DefineLabel ();
 
                        if (resume_points == null) {
-                               EmitMoveNext_NoResumePoints (ec, original_block);
+                               EmitMoveNext_NoResumePoints (ec, block);
                                return;
                        }
 
                        current_pc = ec.GetTemporaryLocal (TypeManager.uint32_type);
-                       ig.Emit (OpCodes.Ldarg_0);
-                       ig.Emit (OpCodes.Ldfld, IteratorHost.PC.FieldBuilder);
-                       ig.Emit (OpCodes.Stloc, current_pc);
+                       ec.Emit (OpCodes.Ldarg_0);
+                       ec.Emit (OpCodes.Ldfld, IteratorHost.PC.Spec);
+                       ec.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);
+                       ec.Emit (OpCodes.Ldarg_0);
+                       ec.EmitInt ((int) State.After);
+                       ec.Emit (OpCodes.Stfld, IteratorHost.PC.Spec);
 
                        Label [] labels = new Label [1 + resume_points.Count];
-                       labels [0] = ig.DefineLabel ();
+                       labels [0] = ec.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);
                        }
 
                        if (need_skip_finally) {
                                skip_finally = ec.GetTemporaryLocal (TypeManager.bool_type);
-                               ig.Emit (OpCodes.Ldc_I4_0);
-                               ig.Emit (OpCodes.Stloc, skip_finally);
+                               ec.Emit (OpCodes.Ldc_I4_0);
+                               ec.Emit (OpCodes.Stloc, skip_finally);
                        }
 
-                       SymbolWriter.StartIteratorDispatcher (ec.ig);
-                       ig.Emit (OpCodes.Ldloc, current_pc);
-                       ig.Emit (OpCodes.Switch, labels);
+                       SymbolWriter.StartIteratorDispatcher (ec);
+                       ec.Emit (OpCodes.Ldloc, current_pc);
+                       ec.Emit (OpCodes.Switch, labels);
 
-                       ig.Emit (OpCodes.Br, move_next_error);
-                       SymbolWriter.EndIteratorDispatcher (ec.ig);
+                       ec.Emit (OpCodes.Br, move_next_error);
+                       SymbolWriter.EndIteratorDispatcher (ec);
 
-                       ig.MarkLabel (labels [0]);
+                       ec.MarkLabel (labels [0]);
 
-                       SymbolWriter.StartIteratorBody (ec.ig);
-                       original_block.Emit (ec);
-                       SymbolWriter.EndIteratorBody (ec.ig);
+                       SymbolWriter.StartIteratorBody (ec);
+                       block.Emit (ec);
+                       SymbolWriter.EndIteratorBody (ec);
 
-                       SymbolWriter.StartIteratorDispatcher (ec.ig);
+                       SymbolWriter.StartIteratorDispatcher (ec);
 
-                       ig.Emit (OpCodes.Ldarg_0);
-                       IntConstant.EmitInt (ig, (int) State.After);
-                       ig.Emit (OpCodes.Stfld, IteratorHost.PC.FieldBuilder);
+                       ec.Emit (OpCodes.Ldarg_0);
+                       ec.EmitInt ((int) State.After);
+                       ec.Emit (OpCodes.Stfld, IteratorHost.PC.Spec);
 
-                       ig.MarkLabel (move_next_error);
-                       ig.Emit (OpCodes.Ldc_I4_0);
-                       ig.Emit (OpCodes.Ret);
+                       ec.MarkLabel (move_next_error);
+                       ec.EmitInt (0);
+                       ec.Emit (OpCodes.Ret);
 
-                       ig.MarkLabel (move_next_ok);
-                       ig.Emit (OpCodes.Ldc_I4_1);
-                       ig.Emit (OpCodes.Ret);
+                       ec.MarkLabel (move_next_ok);
+                       ec.Emit (OpCodes.Ldc_I4_1);
+                       ec.Emit (OpCodes.Ret);
 
-                       SymbolWriter.EndIteratorDispatcher (ec.ig);
+                       SymbolWriter.EndIteratorDispatcher (ec);
                }
 
                public void EmitDispose (EmitContext ec)
                {
-                       ILGenerator ig = ec.ig;
-
-                       Label end = ig.DefineLabel ();
+                       Label end = ec.DefineLabel ();
 
                        Label [] labels = null;
                        int n_resume_points = resume_points == null ? 0 : resume_points.Count;
@@ -742,34 +705,33 @@ 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.Stloc, current_pc);
+                               ec.Emit (OpCodes.Ldarg_0);
+                               ec.Emit (OpCodes.Ldfld, IteratorHost.PC.Spec);
+                               ec.Emit (OpCodes.Stloc, current_pc);
                        }
 
-                       ig.Emit (OpCodes.Ldarg_0);
-                       IntConstant.EmitInt (ig, (int) State.After);
-                       ig.Emit (OpCodes.Stfld, IteratorHost.PC.FieldBuilder);
+                       ec.Emit (OpCodes.Ldarg_0);
+                       ec.EmitInt ((int) State.After);
+                       ec.Emit (OpCodes.Stfld, IteratorHost.PC.Spec);
 
                        if (labels != null) {
                                //SymbolWriter.StartIteratorDispatcher (ec.ig);
-                               ig.Emit (OpCodes.Ldloc, current_pc);
-                               ig.Emit (OpCodes.Switch, labels);
+                               ec.Emit (OpCodes.Ldloc, current_pc);
+                               ec.Emit (OpCodes.Switch, labels);
                                //SymbolWriter.EndIteratorDispatcher (ec.ig);
 
                                foreach (ResumableStatement s in resume_points)
                                        s.EmitForDispose (ec, this, end, true);
                        }
 
-                       ig.MarkLabel (end);
+                       ec.MarkLabel (end);
                }
 
-
-               ArrayList resume_points;
                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;
                }
@@ -779,28 +741,39 @@ namespace Mono.CSharp {
                //
                public void MarkYield (EmitContext ec, Expression expr, int resume_pc, bool unwind_protect, Label resume_point)
                {
-                       ILGenerator ig = ec.ig;
-
                        // Store the new current
-                       ig.Emit (OpCodes.Ldarg_0);
+                       ec.Emit (OpCodes.Ldarg_0);
                        expr.Emit (ec);
-                       ig.Emit (OpCodes.Stfld, IteratorHost.CurrentField.FieldBuilder);
+                       ec.Emit (OpCodes.Stfld, IteratorHost.CurrentField.Spec);
 
                        // store resume program-counter
-                       ig.Emit (OpCodes.Ldarg_0);
-                       IntConstant.EmitInt (ig, resume_pc);
-                       ig.Emit (OpCodes.Stfld, IteratorHost.PC.FieldBuilder);
+                       ec.Emit (OpCodes.Ldarg_0);
+                       ec.EmitInt (resume_pc);
+                       ec.Emit (OpCodes.Stfld, IteratorHost.PC.Spec);
 
                        // mark finally blocks as disabled
                        if (unwind_protect && skip_finally != null) {
-                               ig.Emit (OpCodes.Ldc_I4_1);
-                               ig.Emit (OpCodes.Stloc, skip_finally);
+                               ec.EmitInt (1);
+                               ec.Emit (OpCodes.Stloc, skip_finally);
                        }
 
                        // Return ok
-                       ig.Emit (unwind_protect ? OpCodes.Leave : OpCodes.Br, move_next_ok);
+                       ec.Emit (unwind_protect ? OpCodes.Leave : OpCodes.Br, move_next_ok);
 
-                       ig.MarkLabel (resume_point);
+                       ec.MarkLabel (resume_point);
+               }
+
+               //
+               // Our constructor
+               //
+               public Iterator (ParametersBlock block, IMethodData method, TypeContainer host, TypeSpec iterator_type, bool is_enumerable)
+                       : base (block, TypeManager.bool_type, block.StartLocation)
+               {
+                       this.OriginalMethod = method;
+                       this.OriginalIteratorType = iterator_type;
+                       this.IsEnumerable = is_enumerable;
+                       this.Host = host;
+                       this.type = method.ReturnType;
                }
 
                public override string ContainerType {
@@ -815,43 +788,28 @@ namespace Mono.CSharp {
                        get { return IteratorHost; }
                }
 
-               //
-               // Our constructor
-               //
-               private Iterator (IMethodData method, TypeContainer host, Type iterator_type, bool is_enumerable)
-                       : base (
-                               new ToplevelBlock (method.Block, Parameters.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);
-               }
-
                public override string GetSignatureForError ()
                {
                        return OriginalMethod.GetSignatureForError ();
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               protected override Expression DoResolve (ResolveContext ec)
                {
-                       method = new AnonymousMethodMethod (Storey,
-                               this, Storey, null, TypeManager.system_boolean_expr,
-                               Modifiers.PUBLIC, OriginalMethod.GetSignatureForError (),
-                               new MemberName ("MoveNext", Location),
-                               Parameters.EmptyReadOnlyParameters);
+                       IteratorHost = (IteratorStorey) block.TopBlock.AnonymousMethodStorey;
 
-                       if (!Compatible (ec))
-                               return null;
+                       BlockContext ctx = new BlockContext (ec, block, ReturnType);
+                       ctx.CurrentAnonymousMethod = this;
 
-                       IteratorHost.DefineIteratorMembers ();
+                       ctx.StartFlowBranching (this, ec.CurrentBranching);
+                       Block.Resolve (ctx);
+                       ctx.EndFlowBranching ();
+
+                       var move_next = new IteratorMethod (IteratorHost, new TypeExpression (TypeManager.bool_type, loc),
+                               Modifiers.PUBLIC, new MemberName ("MoveNext", Location));
+                       move_next.Block.AddStatement (new MoveNextMethodStatement (this));
+                       IteratorHost.AddMethod (move_next);
 
                        eclass = ExprClass.Value;
-                       type = ec.ReturnType;
                        return this;
                }
 
@@ -860,41 +818,40 @@ namespace Mono.CSharp {
                        //
                        // Load Iterator storey instance
                        //
-                       method.Storey.Instance.Emit (ec);
+                       IteratorHost.Instance.Emit (ec);
 
                        //
                        // Initialize iterator PC when it's unitialized
                        //
                        if (IsEnumerable) {
-                               ILGenerator ig = ec.ig;
-                               ig.Emit (OpCodes.Dup);
-                               IntConstant.EmitInt (ig, (int)State.Uninitialized);
+                               ec.Emit (OpCodes.Dup);
+                               ec.EmitInt ((int)State.Uninitialized);
 
-                               FieldInfo field = IteratorHost.PC.FieldBuilder;
-#if GMCS_SOURCE
-                               if (Storey.MemberName.IsGeneric)
-                                       field = TypeBuilder.GetField (Storey.Instance.Type, field);
-#endif
-                               ig.Emit (OpCodes.Stfld, field);
+                               var field = IteratorHost.PC.Spec;
+                               if (Storey.MemberName.IsGeneric) {
+                                       field = MemberCache.GetMember (Storey.Instance.Type, field);
+                               }
+
+                               ec.Emit (OpCodes.Stfld, field);
                        }
                }
 
-               public override Expression CreateExpressionTree (EmitContext ec)
+               public override Expression CreateExpressionTree (ResolveContext ec)
                {
                        throw new NotSupportedException ("ET");
                }
 
-               public static void CreateIterator (IMethodData method, TypeContainer parent, int modifiers)
+               public static void CreateIterator (IMethodData method, TypeContainer parent, Modifiers modifiers, CompilerContext ctx)
                {
                        bool is_enumerable;
-                       Type iterator_type;
+                       TypeSpec iterator_type;
 
-                       Type ret = method.ReturnType;
+                       TypeSpec ret = method.ReturnType;
                        if (ret == 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 (),
@@ -902,24 +859,24 @@ 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;
                                if ((mod & Parameter.Modifier.ISBYREF) != 0) {
-                                       Report.Error (1623, p.Location,
+                                       ctx.Report.Error (1623, p.Location,
                                                "Iterators cannot have ref or out parameters");
                                        return;
                                }
 
-                               if ((mod & Parameter.Modifier.ARGLIST) != 0) {
-                                       Report.Error (1636, method.Location,
+                               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) {
-                                       Report.Error (1637, p.Location,
+                                       ctx.Report.Error (1637, p.Location,
                                                          "Iterators cannot have unsafe parameters or " +
                                                          "yield types");
                                        return;
@@ -927,15 +884,13 @@ namespace Mono.CSharp {
                        }
 
                        if ((modifiers & Modifiers.UNSAFE) != 0) {
-                               Report.Error (1629, method.Location, "Unsafe code may not appear in iterators");
-                               return;
+                               ctx.Report.Error (1629, method.Location, "Unsafe code may not appear in iterators");
                        }
 
-                       Iterator iter = new Iterator (method, parent, iterator_type, is_enumerable);
-                       iter.Storey.DefineType ();
+                       method.Block.WrapIntoIterator (method, parent, iterator_type, is_enumerable);
                }
 
-               static bool CheckType (Type ret, out Type original_iterator_type, out bool is_enumerable)
+               static bool CheckType (TypeSpec ret, out TypeSpec original_iterator_type, out bool is_enumerable)
                {
                        original_iterator_type = null;
                        is_enumerable = false;
@@ -951,22 +906,19 @@ namespace Mono.CSharp {
                                return true;
                        }
 
-                       if (!TypeManager.IsGenericType (ret))
-                               return false;
-
-                       Type[] args = TypeManager.GetTypeArguments (ret);
-                       if (args.Length != 1)
+                       InflatedTypeSpec inflated = ret as InflatedTypeSpec;
+                       if (inflated == null)
                                return false;
 
-                       Type gt = TypeManager.DropGenericTypeArguments (ret);
-                       if (gt == TypeManager.generic_ienumerable_type) {
-                               original_iterator_type = args [0];
+                       ret = inflated.GetDefinition ();
+                       if (ret == TypeManager.generic_ienumerable_type) {
+                               original_iterator_type = inflated.TypeArguments[0];
                                is_enumerable = true;
                                return true;
                        }
                        
-                       if (gt == TypeManager.generic_ienumerator_type) {
-                               original_iterator_type = args [0];
+                       if (ret == TypeManager.generic_ienumerator_type) {
+                               original_iterator_type = inflated.TypeArguments[0];
                                is_enumerable = false;
                                return true;
                        }