2010-01-15 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / mcs / iterators.cs
index c4f13150711d2304c967bd596252403b285f420b..20d6741406bac6b9cae6b96914458872c908bfca 100644 (file)
 //
 
 using System;
-using System.Collections;
+using System.Collections.Generic;
 using System.Reflection;
 using System.Reflection.Emit;
 
 namespace Mono.CSharp {
 
-       public class Yield : ResumableStatement {
+       class Yield : ResumableStatement {
                Expression expr;
                bool unwind_protect;
-
+               Iterator iterator;
                int resume_pc;
 
                public Yield (Expression expr, Location l)
@@ -33,14 +33,14 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
-               public static bool CheckContext (EmitContext ec, Location loc)
+               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) {
-                               Report.Error (1621, loc,
+                               ec.Report.Error (1621, loc,
                                              "The yield statement cannot be used inside " +
                                              "anonymous method blocks");
                                return false;
@@ -54,7 +54,7 @@ namespace Mono.CSharp {
                        expr.MutateHoistedGenericType (storey);
                }
                
-               public override bool Resolve (EmitContext ec)
+               public override bool Resolve (BlockContext ec)
                {
                        expr = expr.Resolve (ec);
                        if (expr == null)
@@ -66,7 +66,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);
@@ -82,7 +82,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)
@@ -93,13 +93,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");
                }
@@ -109,14 +112,15 @@ 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);
+                       iterator.EmitYieldBreak (ec.ig, unwind_protect);
                }
 
                public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
@@ -147,7 +151,7 @@ namespace Mono.CSharp {
                        t.iterator = (Iterator) iterator.Clone (clonectx);
                }
 
-               public override bool Resolve (EmitContext ec)
+               public override bool Resolve (BlockContext ec)
                {
                        ec.StartFlowBranching (iterator);
                        bool ok = original_block.Resolve (ec);
@@ -173,19 +177,18 @@ namespace Mono.CSharp {
                {
                        readonly IteratorStorey host;
 
-                       public IteratorMethod (IteratorStorey host, FullNamedExpression returnType, int mod, MemberName name)
+                       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 (host.Iterator.Container.Toplevel, ParametersCompiled.EmptyReadOnlyParameters, Location);
+                               Block = new ToplevelBlock (Compiler, host.Iterator.Container.Toplevel, ParametersCompiled.EmptyReadOnlyParameters, Location);
                        }
 
                        public override EmitContext CreateEmitContext (ILGenerator ig)
                        {
-                               EmitContext ec = new EmitContext (
-                                       this, ig, MemberType);
+                               EmitContext ec = new EmitContext (this, ig, MemberType);
 
                                ec.CurrentAnonymousMethod = host.Iterator;
                                return ec;
@@ -213,27 +216,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;
+                                       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));
@@ -252,7 +255,7 @@ 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);
+                                               Type 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,
@@ -270,10 +273,10 @@ namespace Mono.CSharp {
                                        Label label_init = ig.DefineLabel ();
 
                                        ig.Emit (OpCodes.Ldarg_0);
-                                       ig.Emit (OpCodes.Ldflda, host.PC.FieldBuilder);
+                                       ig.Emit (OpCodes.Ldflda, host.PC.Spec.MetaInfo);
                                        IntConstant.EmitInt (ig, (int) Iterator.State.Start);
                                        IntConstant.EmitInt (ig, (int) Iterator.State.Uninitialized);
-                                       ig.Emit (OpCodes.Call, TypeManager.int_interlocked_compare_exchange);
+                                       ig.Emit (OpCodes.Call, (MethodInfo) TypeManager.int_interlocked_compare_exchange.MetaInfo);
 
                                        IntConstant.EmitInt (ig, (int) Iterator.State.Uninitialized);
                                        ig.Emit (OpCodes.Bne_Un_S, label_init);
@@ -317,7 +320,7 @@ namespace Mono.CSharp {
                                        throw new NotSupportedException ();
                                }
 
-                               public override bool Resolve (EmitContext ec)
+                               public override bool Resolve (BlockContext ec)
                                {
                                        return true;
                                }
@@ -338,7 +341,7 @@ namespace Mono.CSharp {
                        {
                                host.AddMethod (this);
 
-                               Block = new ToplevelBlock (host.Iterator.Container, ParametersCompiled.EmptyReadOnlyParameters, Location);
+                               Block = new ToplevelBlock (Compiler, host.Iterator.Container, ParametersCompiled.EmptyReadOnlyParameters, Location);
                                Block.AddStatement (new DisposeMethodStatement (host.Iterator));
                        }
                }
@@ -354,11 +357,12 @@ namespace Mono.CSharp {
                                : base (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 };
+                               Methods = new [] { method.Spec };
                                type = method.Parent.TypeBuilder;
                                InstanceExpression = new CompilerGeneratedThis (type, Location);
                                return base.DoResolve (ec);
@@ -375,10 +379,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 = TypeManager.TypeToCoreType (spec.FieldType);
                                InstanceExpression = new CompilerGeneratedThis (type, Location);
                                return base.DoResolve (ec);
                        }
@@ -396,7 +400,7 @@ namespace Mono.CSharp {
                TypeExpr generic_enumerator_type;
                TypeExpr generic_enumerable_type;
 
-               ArrayList hoisted_params_copy;
+               List<HoistedParameter> hoisted_params_copy;
                int local_name_idx;
 
                public IteratorStorey (Iterator iterator)
@@ -414,7 +418,7 @@ namespace Mono.CSharp {
                        get { return current_field; }
                }
 
-               public ArrayList HoistedParameters {
+               public IList<HoistedParameter> HoistedParameters {
                        get { return hoisted_params; }
                }
 
@@ -423,7 +427,7 @@ namespace Mono.CSharp {
                        iterator_type_expr = new TypeExpression (MutateType (Iterator.OriginalIteratorType), Location);
                        generic_args = new TypeArguments (iterator_type_expr);
 
-                       ArrayList list = new ArrayList ();
+                       var list = new List<FullNamedExpression> ();
                        if (Iterator.IsEnumerable) {
                                enumerable_type = new TypeExpression (
                                        TypeManager.ienumerable_type, Location);
@@ -473,7 +477,7 @@ 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));
                                }
@@ -514,7 +518,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               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);
@@ -538,7 +542,7 @@ namespace Mono.CSharp {
 
                        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);
@@ -557,9 +561,9 @@ namespace Mono.CSharp {
                                ParametersCompiled.EmptyReadOnlyParameters, null);
                        AddMethod (reset);
 
-                       reset.Block = new ToplevelBlock (Location);
+                       reset.Block = new ToplevelBlock (Compiler, Location);
 
-                       Type ex_type = TypeManager.CoreLookupType ("System", "NotSupportedException", Kind.Class, true);
+                       Type ex_type = TypeManager.CoreLookupType (Compiler, "System", "NotSupportedException", MemberKind.Class, true);
                        if (ex_type == null)
                                return;
 
@@ -575,6 +579,7 @@ namespace Mono.CSharp {
                AnonymousMethodMethod method;
                public readonly TypeContainer Host;
                public readonly bool IsEnumerable;
+               List<ResumableStatement> resume_points;
 
                //
                // The state as we generate the iterator
@@ -619,11 +624,11 @@ namespace Mono.CSharp {
                        ILGenerator ig = ec.ig;
 
                        ig.Emit (OpCodes.Ldarg_0);
-                       ig.Emit (OpCodes.Ldfld, IteratorHost.PC.FieldBuilder);
+                       ig.Emit (OpCodes.Ldfld, IteratorHost.PC.Spec.MetaInfo);
 
                        ig.Emit (OpCodes.Ldarg_0);
                        IntConstant.EmitInt (ig, (int) State.After);
-                       ig.Emit (OpCodes.Stfld, IteratorHost.PC.FieldBuilder);
+                       ig.Emit (OpCodes.Stfld, IteratorHost.PC.Spec.MetaInfo);
 
                        // We only care if the PC is zero (start executing) or non-zero (don't do anything)
                        ig.Emit (OpCodes.Brtrue, move_next_error);
@@ -651,20 +656,20 @@ namespace Mono.CSharp {
 
                        current_pc = ec.GetTemporaryLocal (TypeManager.uint32_type);
                        ig.Emit (OpCodes.Ldarg_0);
-                       ig.Emit (OpCodes.Ldfld, IteratorHost.PC.FieldBuilder);
+                       ig.Emit (OpCodes.Ldfld, IteratorHost.PC.Spec.MetaInfo);
                        ig.Emit (OpCodes.Stloc, current_pc);
 
                        // We're actually in state 'running', but this is as good a PC value as any if there's an abnormal exit
                        ig.Emit (OpCodes.Ldarg_0);
                        IntConstant.EmitInt (ig, (int) State.After);
-                       ig.Emit (OpCodes.Stfld, IteratorHost.PC.FieldBuilder);
+                       ig.Emit (OpCodes.Stfld, IteratorHost.PC.Spec.MetaInfo);
 
                        Label [] labels = new Label [1 + resume_points.Count];
                        labels [0] = ig.DefineLabel ();
 
                        bool need_skip_finally = false;
                        for (int i = 0; i < resume_points.Count; ++i) {
-                               ResumableStatement s = (ResumableStatement) resume_points [i];
+                               ResumableStatement s = resume_points [i];
                                need_skip_finally |= s is ExceptionStatement;
                                labels [i+1] = s.PrepareForEmit (ec);
                        }
@@ -692,7 +697,7 @@ namespace Mono.CSharp {
 
                        ig.Emit (OpCodes.Ldarg_0);
                        IntConstant.EmitInt (ig, (int) State.After);
-                       ig.Emit (OpCodes.Stfld, IteratorHost.PC.FieldBuilder);
+                       ig.Emit (OpCodes.Stfld, IteratorHost.PC.Spec.MetaInfo);
 
                        ig.MarkLabel (move_next_error);
                        ig.Emit (OpCodes.Ldc_I4_0);
@@ -729,13 +734,13 @@ namespace Mono.CSharp {
                        if (labels != null) {
                                current_pc = ec.GetTemporaryLocal (TypeManager.uint32_type);
                                ig.Emit (OpCodes.Ldarg_0);
-                               ig.Emit (OpCodes.Ldfld, IteratorHost.PC.FieldBuilder);
+                               ig.Emit (OpCodes.Ldfld, IteratorHost.PC.Spec.MetaInfo);
                                ig.Emit (OpCodes.Stloc, current_pc);
                        }
 
                        ig.Emit (OpCodes.Ldarg_0);
                        IntConstant.EmitInt (ig, (int) State.After);
-                       ig.Emit (OpCodes.Stfld, IteratorHost.PC.FieldBuilder);
+                       ig.Emit (OpCodes.Stfld, IteratorHost.PC.Spec.MetaInfo);
 
                        if (labels != null) {
                                //SymbolWriter.StartIteratorDispatcher (ec.ig);
@@ -750,12 +755,11 @@ namespace Mono.CSharp {
                        ig.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;
                }
@@ -770,12 +774,12 @@ namespace Mono.CSharp {
                        // Store the new current
                        ig.Emit (OpCodes.Ldarg_0);
                        expr.Emit (ec);
-                       ig.Emit (OpCodes.Stfld, IteratorHost.CurrentField.FieldBuilder);
+                       ig.Emit (OpCodes.Stfld, IteratorHost.CurrentField.Spec.MetaInfo);
 
                        // store resume program-counter
                        ig.Emit (OpCodes.Ldarg_0);
                        IntConstant.EmitInt (ig, resume_pc);
-                       ig.Emit (OpCodes.Stfld, IteratorHost.PC.FieldBuilder);
+                       ig.Emit (OpCodes.Stfld, IteratorHost.PC.Spec.MetaInfo);
 
                        // mark finally blocks as disabled
                        if (unwind_protect && skip_finally != null) {
@@ -804,9 +808,9 @@ namespace Mono.CSharp {
                //
                // Our constructor
                //
-               private Iterator (IMethodData method, TypeContainer host, Type iterator_type, bool is_enumerable)
+               private Iterator (CompilerContext ctx, IMethodData method, TypeContainer host, Type iterator_type, bool is_enumerable)
                        : base (
-                               new ToplevelBlock (method.Block, ParametersCompiled.EmptyReadOnlyParameters, method.Block.StartLocation),
+                               new ToplevelBlock (ctx, method.Block, ParametersCompiled.EmptyReadOnlyParameters, method.Block.StartLocation),
                                TypeManager.bool_type,
                                method.Location)
                {
@@ -814,6 +818,7 @@ namespace Mono.CSharp {
                        this.OriginalIteratorType = iterator_type;
                        this.IsEnumerable = is_enumerable;
                        this.Host = host;
+                       this.type = method.ReturnType;
 
                        IteratorHost = Block.ChangeToIterator (this, method.Block);
                }
@@ -823,7 +828,7 @@ namespace Mono.CSharp {
                        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,
@@ -831,13 +836,12 @@ namespace Mono.CSharp {
                                new MemberName ("MoveNext", Location),
                                ParametersCompiled.EmptyReadOnlyParameters);
 
-                       if (!Compatible (ec))
+                       if (Compatible (ec) == null)
                                return null;
 
                        IteratorHost.DefineIteratorMembers ();
 
                        eclass = ExprClass.Value;
-                       type = ec.ReturnType;
                        return this;
                }
 
@@ -856,21 +860,20 @@ namespace Mono.CSharp {
                                ig.Emit (OpCodes.Dup);
                                IntConstant.EmitInt (ig, (int)State.Uninitialized);
 
-                               FieldInfo field = IteratorHost.PC.FieldBuilder;
-#if GMCS_SOURCE
+                               FieldInfo field = IteratorHost.PC.Spec.MetaInfo;
                                if (Storey.MemberName.IsGeneric)
                                        field = TypeBuilder.GetField (Storey.Instance.Type, field);
-#endif
+
                                ig.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;
@@ -880,7 +883,7 @@ namespace Mono.CSharp {
                                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 (),
@@ -893,19 +896,19 @@ namespace Mono.CSharp {
                                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 (p is ArglistParameter) {
-                                       Report.Error (1636, method.Location,
+                                       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;
@@ -913,11 +916,11 @@ namespace Mono.CSharp {
                        }
 
                        if ((modifiers & Modifiers.UNSAFE) != 0) {
-                               Report.Error (1629, method.Location, "Unsafe code may not appear in iterators");
+                               ctx.Report.Error (1629, method.Location, "Unsafe code may not appear in iterators");
                                return;
                        }
 
-                       Iterator iter = new Iterator (method, parent, iterator_type, is_enumerable);
+                       Iterator iter = new Iterator (ctx, method, parent, iterator_type, is_enumerable);
                        iter.Storey.DefineType ();
                }