[asp.net] FormsAuthenticationTicket.FromByteArray stops early if it is passed a null...
[mono.git] / mcs / mcs / iterators.cs
index 5362bba08bcfb75ea0a8366906d77dc9f60e1f97..37a9b00b4d6d39fec8b5aae6876558e6fed27b10 100644 (file)
 
 using System;
 using System.Collections.Generic;
-using System.Reflection;
+
+#if STATIC
+using IKVM.Reflection.Emit;
+#else
 using System.Reflection.Emit;
+#endif
 
 namespace Mono.CSharp {
 
-       class Yield : ResumableStatement {
+       public class Yield : ResumableStatement {
                Expression expr;
                bool unwind_protect;
                Iterator iterator;
@@ -35,10 +39,6 @@ namespace Mono.CSharp {
 
                public static bool CheckContext (ResolveContext ec, Location loc)
                {
-                       //
-                       // 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) {
                                ec.Report.Error (1621, loc,
                                              "The yield statement cannot be used inside " +
@@ -49,11 +49,6 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
-               {
-                       expr.MutateHoistedGenericType (storey);
-               }
-               
                public override bool Resolve (BlockContext ec)
                {
                        expr = expr.Resolve (ec);
@@ -120,81 +115,12 @@ namespace Mono.CSharp {
 
                protected override void DoEmit (EmitContext ec)
                {
-                       iterator.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 (BlockContext 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, Modifiers mod, MemberName name)
-                               : base (host, null, returnType, mod | Modifiers.DEBUGGER_HIDDEN | Modifiers.COMPILER_GENERATED,
-                                 name, ParametersCompiled.EmptyReadOnlyParameters, null)
-                       {
-                               this.host = host;
-
-                               Block = new ToplevelBlock (Compiler, host.Iterator.Container.Toplevel, ParametersCompiled.EmptyReadOnlyParameters, Location);
-                       }
-
-                       public override EmitContext CreateEmitContext (ILGenerator ig)
-                       {
-                               EmitContext ec = new EmitContext (this, ig, MemberType);
-
-                               ec.CurrentAnonymousMethod = host.Iterator;
-                               return ec;
-                       }
-               }
-
                class GetEnumeratorMethod : IteratorMethod
                {
                        sealed class GetEnumeratorStatement : Statement
@@ -218,7 +144,7 @@ namespace Mono.CSharp {
 
                                public override bool Resolve (BlockContext ec)
                                {
-                                       TypeExpression storey_type_expr = new TypeExpression (host.TypeBuilder, loc);
+                                       TypeExpression storey_type_expr = new TypeExpression (host.Definition, loc);
                                        List<Expression> init = null;
                                        if (host.hoisted_this != null) {
                                                init = new List<Expression> (host.hoisted_params == null ? 1 : host.HoistedParameters.Count + 1);
@@ -254,13 +180,20 @@ namespace Mono.CSharp {
                                        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 (ec.Compiler, "System.Threading", "Interlocked", Kind.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 t = ec.Module.PredefinedTypes.Interlocked.Resolve (loc);
+                                       if (t != null) {
+                                               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);
                                        }
 
                                        ec.CurrentBranching.CurrentUsageVector.Goto ();
@@ -269,35 +202,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.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);
+                                       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));
                        }
@@ -329,19 +256,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 (Compiler, host.Iterator.Container, ParametersCompiled.EmptyReadOnlyParameters, Location);
                                Block.AddStatement (new DisposeMethodStatement (host.Iterator));
                        }
                }
@@ -354,7 +276,7 @@ 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;
@@ -362,8 +284,8 @@ namespace Mono.CSharp {
 
                        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);
                        }
@@ -382,7 +304,7 @@ namespace Mono.CSharp {
                        protected override Expression DoResolve (ResolveContext ec)
                        {
                                spec = field.Spec;
-                               type = TypeManager.TypeToCoreType (spec.FieldType);
+                               type = spec.MemberType;
                                InstanceExpression = new CompilerGeneratedThis (type, Location);
                                return base.DoResolve (ec);
                        }
@@ -404,8 +326,8 @@ namespace Mono.CSharp {
                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;
                }
@@ -424,7 +346,11 @@ namespace Mono.CSharp {
 
                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);
+
+                       iterator_type_expr = new TypeExpression (mtype, Location);
                        generic_args = new TypeArguments (iterator_type_expr);
 
                        var list = new List<FullNamedExpression> ();
@@ -447,10 +373,9 @@ namespace Mono.CSharp {
 
                        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);
+                       var ienumerator_generic = Module.PredefinedTypes.IEnumeratorGeneric;
+                       if (ienumerator_generic.Define ()) {
+                               generic_enumerator_type = new GenericTypeExpr (ienumerator_generic.TypeSpec, generic_args, Location);
                                list.Add (generic_enumerator_type);
                        }
 
@@ -459,14 +384,20 @@ namespace Mono.CSharp {
                        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 ()
+               {
+                       DefineIteratorMembers ();
+                       return base.DoDefineMembers ();
+               }
+
+               void DefineIteratorMembers ()
                {
-                       pc_field = AddCompilerGeneratedField ("$PC", TypeManager.system_int32_expr);
+                       pc_field = AddCompilerGeneratedField ("$PC", new TypeExpression (TypeManager.int32_type, Location));
                        current_field = AddCompilerGeneratedField ("$current", iterator_type_expr);
 
                        if (hoisted_params != null) {
@@ -537,7 +468,7 @@ namespace Mono.CSharp {
                                type = iterator_type_expr;
                        } else {
                                name = new MemberName (name, "IEnumerator");
-                               type = TypeManager.system_object_expr;
+                               type = new TypeExpression (TypeManager.object_type, Location);
                        }
 
                        name = new MemberName (name, "Current", Location);
@@ -545,17 +476,17 @@ namespace Mono.CSharp {
                        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),
                                ParametersCompiled.EmptyReadOnlyParameters, null);
@@ -563,7 +494,7 @@ namespace Mono.CSharp {
 
                        reset.Block = new ToplevelBlock (Compiler, Location);
 
-                       Type ex_type = TypeManager.CoreLookupType (Compiler, "System", "NotSupportedException", Kind.Class, true);
+                       TypeSpec ex_type = Module.PredefinedTypes.NotSupportedException.Resolve (Location);
                        if (ex_type == null)
                                return;
 
@@ -571,12 +502,60 @@ namespace Mono.CSharp {
                }
        }
 
+       class IteratorMethod : Method
+       {
+               readonly IteratorStorey host;
+
+               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;
@@ -603,9 +582,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
@@ -614,58 +593,54 @@ namespace Mono.CSharp {
                        Start = 0
                }
 
-               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.Spec.MetaInfo);
+                       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.Spec.MetaInfo);
+                       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.Spec.MetaInfo);
-                       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.Spec.MetaInfo);
+                       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) {
@@ -676,45 +651,43 @@ namespace Mono.CSharp {
 
                        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.Spec.MetaInfo);
+                       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;
@@ -733,26 +706,26 @@ namespace Mono.CSharp {
 
                        if (labels != null) {
                                current_pc = ec.GetTemporaryLocal (TypeManager.uint32_type);
-                               ig.Emit (OpCodes.Ldarg_0);
-                               ig.Emit (OpCodes.Ldfld, IteratorHost.PC.Spec.MetaInfo);
-                               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.Spec.MetaInfo);
+                       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);
                }
 
                public int AddResumePoint (ResumableStatement stmt)
@@ -769,58 +742,51 @@ 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.Spec.MetaInfo);
+                       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.Spec.MetaInfo);
+                       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);
-
-                       ig.MarkLabel (resume_point);
-               }
+                       ec.Emit (unwind_protect ? OpCodes.Leave : OpCodes.Br, move_next_ok);
 
-               public override string ContainerType {
-                       get { return "iterator"; }
-               }
-
-               public override bool IsIterator {
-                       get { return true; }
-               }
-
-               public override AnonymousMethodStorey Storey {
-                       get { return IteratorHost; }
+                       ec.MarkLabel (resume_point);
                }
 
                //
                // Our constructor
                //
-               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)
+               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;
+               }
 
-                       IteratorHost = Block.ChangeToIterator (this, method.Block);
+               public override string ContainerType {
+                       get { return "iterator"; }
+               }
+
+               public override bool IsIterator {
+                       get { return true; }
+               }
+
+               public override AnonymousMethodStorey Storey {
+                       get { return IteratorHost; }
                }
 
                public override string GetSignatureForError ()
@@ -830,16 +796,19 @@ namespace Mono.CSharp {
 
                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),
-                               ParametersCompiled.EmptyReadOnlyParameters);
+                       IteratorHost = (IteratorStorey) block.TopBlock.AnonymousMethodStorey;
 
-                       if (Compatible (ec) == null)
-                               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;
                        return this;
@@ -850,21 +819,21 @@ 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.Spec.MetaInfo;
-                               if (Storey.MemberName.IsGeneric)
-                                       field = TypeBuilder.GetField (Storey.Instance.Type, field);
+                               var field = IteratorHost.PC.Spec;
+                               if (Storey.MemberName.IsGeneric) {
+                                       field = MemberCache.GetMember (Storey.Instance.Type, field);
+                               }
 
-                               ig.Emit (OpCodes.Stfld, field);
+                               ec.Emit (OpCodes.Stfld, field);
                        }
                }
 
@@ -876,9 +845,9 @@ namespace Mono.CSharp {
                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;
 
@@ -917,14 +886,12 @@ namespace Mono.CSharp {
 
                        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 ();
+                       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;
@@ -940,22 +907,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 = TypeManager.TypeToCoreType (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 = TypeManager.TypeToCoreType (args [0]);
+                       if (ret == TypeManager.generic_ienumerator_type) {
+                               original_iterator_type = inflated.TypeArguments[0];
                                is_enumerable = false;
                                return true;
                        }