Negative header ids should be valid
[mono.git] / mcs / mcs / iterators.cs
index 1d1a14758005c8be04bcbfcc52fd83d843dafb23..dcbf10edf722bb4a6bf635406f9e62d75466939f 100644 (file)
 // Copyright 2011 Xamarin Inc.
 //
 
-// TODO:
-//    Flow analysis for Yield.
-//
-
 using System;
 using System.Collections.Generic;
+using Mono.CompilerServices.SymbolWriter;
 
 #if STATIC
 using IKVM.Reflection.Emit;
@@ -63,7 +60,7 @@ namespace Mono.CSharp
                        machine_initializer = bc.CurrentAnonymousMethod as T;
 
                        if (!bc.CurrentBranching.CurrentUsageVector.IsUnreachable)
-                               unwind_protect = bc.CurrentBranching.AddResumePoint (this, out resume_pc);
+                               unwind_protect = bc.CurrentBranching.AddResumePoint (this, this, out resume_pc);
 
                        return true;
                }
@@ -159,9 +156,10 @@ namespace Mono.CSharp
 
                Field pc_field;
                StateMachineMethod method;
+               int local_name_idx;
 
-               protected StateMachine (Block block, TypeDefinition parent, MemberBase host, TypeParameters tparams, string name)
-                       : base (block, parent, host, tparams, name)
+               protected StateMachine (ParametersBlock block, TypeDefinition parent, MemberBase host, TypeParameters tparams, string name, MemberKind kind)
+                       : base (block, parent, host, tparams, name, kind)
                {
                }
 
@@ -196,6 +194,14 @@ namespace Mono.CSharp
 
                        return base.DoDefineMembers ();
                }
+
+               protected override string GetVariableMangledName (LocalVariable local_info)
+               {
+                       if (local_info.IsCompilerGenerated)
+                               return base.GetVariableMangledName (local_info);
+
+                       return "<" + local_info.Name + ">__" + local_name_idx++.ToString ("X");
+               }
        }
 
        class IteratorStorey : StateMachine
@@ -289,10 +295,23 @@ namespace Mono.CSharp
                                }
                        }
 
-                       public GetEnumeratorMethod (IteratorStorey host, FullNamedExpression returnType, MemberName name)
+                       GetEnumeratorMethod (IteratorStorey host, FullNamedExpression returnType, MemberName name)
                                : base (host, null, returnType, Modifiers.DEBUGGER_HIDDEN, name)
                        {
-                               Block.AddStatement (new GetEnumeratorStatement (host, this));
+                       }
+
+                       public static GetEnumeratorMethod Create (IteratorStorey host, FullNamedExpression returnType, MemberName name)
+                       {
+                               return Create (host, returnType, name, null);
+                       }
+
+                       public static GetEnumeratorMethod Create (IteratorStorey host, FullNamedExpression returnType, MemberName name, Statement statement)
+                       {
+                               var m = new GetEnumeratorMethod (host, returnType, name);
+                               var stmt = statement ?? new GetEnumeratorStatement (host, m);
+                               m.block.AddStatement (stmt);
+                               m.block.IsCompilerGenerated = true;
+                               return m;
                        }
                }
 
@@ -332,6 +351,7 @@ namespace Mono.CSharp
                                host.Members.Add (this);
 
                                Block.AddStatement (new DisposeMethodStatement (host.Iterator));
+                               Block.IsCompilerGenerated = true;
                        }
                }
 
@@ -384,17 +404,13 @@ namespace Mono.CSharp
                TypeExpr iterator_type_expr;
                Field current_field;
                Field disposing_field;
-               int local_name_idx;
 
-               TypeExpr enumerator_type;
-               TypeExpr enumerable_type;
-               TypeArguments generic_args;
-               TypeExpr generic_enumerator_type;
-               TypeExpr generic_enumerable_type;
+               TypeSpec generic_enumerator_type;
+               TypeSpec generic_enumerable_type;
 
                public IteratorStorey (Iterator iterator)
                        : base (iterator.Container.ParametersBlock, iterator.Host,
-                         iterator.OriginalMethod as MemberBase, iterator.OriginalMethod.CurrentTypeParameters, "Iterator")
+                         iterator.OriginalMethod as MemberBase, iterator.OriginalMethod.CurrentTypeParameters, "Iterator", MemberKind.Class)
                {
                        this.Iterator = iterator;
                }
@@ -422,33 +438,30 @@ namespace Mono.CSharp
                                mtype = Mutator.Mutate (mtype);
 
                        iterator_type_expr = new TypeExpression (mtype, Location);
-                       generic_args = new TypeArguments (iterator_type_expr);
 
-                       var list = new List<FullNamedExpression> ();
+                       var ifaces = new List<TypeSpec> (5);
                        if (Iterator.IsEnumerable) {
-                               enumerable_type = new TypeExpression (Compiler.BuiltinTypes.IEnumerable, Location);
-                               list.Add (enumerable_type);
+                               ifaces.Add (Compiler.BuiltinTypes.IEnumerable);
 
                                if (Module.PredefinedTypes.IEnumerableGeneric.Define ()) {
-                                       generic_enumerable_type = new GenericTypeExpr (Module.PredefinedTypes.IEnumerableGeneric.TypeSpec, generic_args, Location);
-                                       list.Add (generic_enumerable_type);
+                                       generic_enumerable_type = Module.PredefinedTypes.IEnumerableGeneric.TypeSpec.MakeGenericType (Module, new[] { mtype });
+                                       ifaces.Add (generic_enumerable_type);
                                }
                        }
 
-                       enumerator_type = new TypeExpression (Compiler.BuiltinTypes.IEnumerator, Location);
-                       list.Add (enumerator_type);
-
-                       list.Add (new TypeExpression (Compiler.BuiltinTypes.IDisposable, Location));
+                       ifaces.Add (Compiler.BuiltinTypes.IEnumerator);
+                       ifaces.Add (Compiler.BuiltinTypes.IDisposable);
 
                        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);
+                               generic_enumerator_type = ienumerator_generic.TypeSpec.MakeGenericType (Module, new [] { mtype });
+                               ifaces.Add (generic_enumerator_type);
                        }
 
-                       type_bases = list;
+                       base_class = null;
 
-                       return base.ResolveBaseTypes (out base_class);
+                       base_type = Compiler.BuiltinTypes.Object;
+                       return ifaces.ToArray ();
                }
 
                protected override bool DoDefineMembers ()
@@ -479,25 +492,23 @@ namespace Mono.CSharp
 
                        if (Iterator.IsEnumerable) {
                                FullNamedExpression explicit_iface = new TypeExpression (Compiler.BuiltinTypes.IEnumerable, Location);
-                               var name = new MemberName ("GetEnumerator", null, explicit_iface, Location);
+                               var name = new MemberName ("GetEnumerator", null, explicit_iface, Location.Null);
 
                                if (generic_enumerator_type != null) {
-                                       Method get_enumerator = new StateMachineMethod (this, null, enumerator_type, 0, name);
-
-                                       explicit_iface = new GenericTypeExpr (Module.PredefinedTypes.IEnumerableGeneric.Resolve (), generic_args, Location);
-                                       name = new MemberName ("GetEnumerator", null, explicit_iface, Location);
-                                       Method gget_enumerator = new GetEnumeratorMethod (this, generic_enumerator_type, name);
+                                       explicit_iface = new TypeExpression (generic_enumerable_type, Location);
+                                       var gname = new MemberName ("GetEnumerator", null, explicit_iface, Location.Null);
+                                       Method gget_enumerator = GetEnumeratorMethod.Create (this, new TypeExpression (generic_enumerator_type, Location), gname);
 
                                        //
                                        // Just call generic GetEnumerator implementation
                                        //
-                                       get_enumerator.Block.AddStatement (
-                                               new Return (new Invocation (new DynamicMethodGroupExpr (gget_enumerator, Location), null), Location));
+                                       var stmt = new Return (new Invocation (new DynamicMethodGroupExpr (gget_enumerator, Location), null), Location);
+                                       Method get_enumerator = GetEnumeratorMethod.Create (this, new TypeExpression (Compiler.BuiltinTypes.IEnumerator, Location), name, stmt);
 
                                        Members.Add (get_enumerator);
                                        Members.Add (gget_enumerator);
                                } else {
-                                       Members.Add (new GetEnumeratorMethod (this, enumerator_type, name));
+                                       Members.Add (GetEnumeratorMethod.Create (this, new TypeExpression (Compiler.BuiltinTypes.IEnumerator, Location), name));
                                }
                        }
 
@@ -510,7 +521,7 @@ namespace Mono.CSharp
                        FullNamedExpression explicit_iface;
 
                        if (is_generic) {
-                               explicit_iface = new GenericTypeExpr (Module.PredefinedTypes.IEnumeratorGeneric.Resolve (), generic_args, Location);
+                               explicit_iface = new TypeExpression (generic_enumerator_type, Location);
                                type = iterator_type_expr;
                        } else {
                                explicit_iface = new TypeExpression (Module.Compiler.BuiltinTypes.IEnumerator, Location);
@@ -519,11 +530,13 @@ namespace Mono.CSharp
 
                        var name = new MemberName ("Current", null, explicit_iface, Location);
 
-                       ToplevelBlock get_block = new ToplevelBlock (Compiler, Location);
+                       ToplevelBlock get_block = new ToplevelBlock (Compiler, Location) {
+                               IsCompilerGenerated = true
+                       };
                        get_block.AddStatement (new Return (new DynamicFieldExpr (CurrentField, Location), Location));
                                
-                       Property current = new Property (this, type, Modifiers.DEBUGGER_HIDDEN, name, null);
-                       current.Get = new Property.GetMethod (current, 0, null, Location);
+                       Property current = new Property (this, type, Modifiers.DEBUGGER_HIDDEN | Modifiers.COMPILER_GENERATED, name, null);
+                       current.Get = new Property.GetMethod (current, Modifiers.COMPILER_GENERATED, null, Location);
                        current.Get.Block = get_block;
 
                        Members.Add (current);
@@ -533,12 +546,14 @@ namespace Mono.CSharp
                {
                        Method reset = new Method (
                                this, new TypeExpression (Compiler.BuiltinTypes.Void, Location),
-                               Modifiers.PUBLIC | Modifiers.DEBUGGER_HIDDEN,
+                               Modifiers.PUBLIC | Modifiers.DEBUGGER_HIDDEN | Modifiers.COMPILER_GENERATED,
                                new MemberName ("Reset", Location),
                                ParametersCompiled.EmptyReadOnlyParameters, null);
                        Members.Add (reset);
 
-                       reset.Block = new ToplevelBlock (Compiler, Location);
+                       reset.Block = new ToplevelBlock (Compiler, Location) {
+                               IsCompilerGenerated = true
+                       };
 
                        TypeSpec ex_type = Module.PredefinedTypes.NotSupportedException.Resolve ();
                        if (ex_type == null)
@@ -547,16 +562,11 @@ namespace Mono.CSharp
                        reset.Block.AddStatement (new Throw (new New (new TypeExpression (ex_type, Location), null, Location), Location));
                }
 
-               protected override void EmitHoistedParameters (EmitContext ec, IList<HoistedParameter> hoisted)
+               protected override void EmitHoistedParameters (EmitContext ec, List<HoistedParameter> hoisted)
                {
                        base.EmitHoistedParameters (ec, hoisted);
                        base.EmitHoistedParameters (ec, hoisted_params_copy);
                }
-
-               protected override string GetVariableMangledName (LocalVariable local_info)
-               {
-                       return "<" + local_info.Name + ">__" + local_name_idx++.ToString ("X");
-               }
        }
 
        public class StateMachineMethod : Method
@@ -568,12 +578,12 @@ namespace Mono.CSharp
                          name, ParametersCompiled.EmptyReadOnlyParameters, null)
                {
                        this.expr = expr;
-                       Block = new ToplevelBlock (host.Compiler, ParametersCompiled.EmptyReadOnlyParameters, Location);
+                       Block = new ToplevelBlock (host.Compiler, ParametersCompiled.EmptyReadOnlyParameters, Location.Null);
                }
 
-               public override EmitContext CreateEmitContext (ILGenerator ig)
+               public override EmitContext CreateEmitContext (ILGenerator ig, SourceMethodBuilder sourceMethod)
                {
-                       EmitContext ec = new EmitContext (this, ig, MemberType);
+                       EmitContext ec = new EmitContext (this, ig, MemberType, sourceMethod);
                        ec.CurrentAnonymousMethod = expr;
 
                        if (expr is AsyncInitializer)
@@ -609,6 +619,12 @@ namespace Mono.CSharp
                        {
                                state_machine.EmitMoveNext (ec);
                        }
+
+                       public override void Emit (EmitContext ec)
+                       {
+                               // Don't create sequence point
+                               DoEmit (ec);
+                       }
                }
 
                public readonly TypeDefinition Host;
@@ -682,8 +698,6 @@ namespace Mono.CSharp
 
                protected override Expression DoResolve (ResolveContext ec)
                {
-                       storey = (StateMachine) block.Parent.ParametersBlock.AnonymousMethodStorey;
-
                        var ctx = CreateBlockContext (ec);
 
                        Block.Resolve (ctx);
@@ -710,12 +724,12 @@ namespace Mono.CSharp
                public override void Emit (EmitContext ec)
                {
                        //
-                       // Load Iterator storey instance
+                       // Load state machine instance
                        //
                        storey.Instance.Emit (ec);
                }
 
-               void EmitMoveNext_NoResumePoints (EmitContext ec, Block original_block)
+               void EmitMoveNext_NoResumePoints (EmitContext ec)
                {
                        ec.EmitThis ();
                        ec.Emit (OpCodes.Ldfld, storey.PC.Spec);
@@ -729,9 +743,7 @@ namespace Mono.CSharp
 
                        iterator_body_end = ec.DefineLabel ();
 
-                       SymbolWriter.StartIteratorBody (ec);
-                       original_block.Emit (ec);
-                       SymbolWriter.EndIteratorBody (ec);
+                       block.EmitEmbedded (ec);
 
                        ec.MarkLabel (iterator_body_end);
 
@@ -751,7 +763,7 @@ namespace Mono.CSharp
                        move_next_error = ec.DefineLabel ();
 
                        if (resume_points == null) {
-                               EmitMoveNext_NoResumePoints (ec, block);
+                               EmitMoveNext_NoResumePoints (ec);
                                return;
                        }
                        
@@ -785,22 +797,16 @@ namespace Mono.CSharp
                        if (async_init != null)
                                ec.BeginExceptionBlock ();
 
-                       SymbolWriter.StartIteratorDispatcher (ec);
                        ec.Emit (OpCodes.Ldloc, current_pc);
                        ec.Emit (OpCodes.Switch, labels);
 
                        ec.Emit (async_init != null ? OpCodes.Leave : OpCodes.Br, move_next_error);
-                       SymbolWriter.EndIteratorDispatcher (ec);
 
                        ec.MarkLabel (labels[0]);
 
                        iterator_body_end = ec.DefineLabel ();
 
-                       SymbolWriter.StartIteratorBody (ec);
-                       block.Emit (ec);
-                       SymbolWriter.EndIteratorBody (ec);
-
-                       SymbolWriter.StartIteratorDispatcher (ec);
+                       block.EmitEmbedded (ec);
 
                        ec.MarkLabel (iterator_body_end);
 
@@ -820,6 +826,7 @@ namespace Mono.CSharp
                                ec.EndExceptionBlock ();
                        }
 
+                       ec.Mark (Block.Original.EndLocation);
                        ec.EmitThis ();
                        ec.EmitInt ((int) IteratorStorey.State.After);
                        ec.Emit (OpCodes.Stfld, storey.PC.Spec);
@@ -839,8 +846,6 @@ namespace Mono.CSharp
                                ec.EmitInt (1);
                                ec.Emit (OpCodes.Ret);
                        }
-
-                       SymbolWriter.EndIteratorDispatcher (ec);
                }
 
                protected virtual void EmitMoveNextEpilogue (EmitContext ec)
@@ -886,16 +891,51 @@ namespace Mono.CSharp
                                ec.Emit (OpCodes.Stloc, skip_finally);
                        }
                }
+
+               public void SetStateMachine (StateMachine stateMachine)
+               {
+                       this.storey = stateMachine;
+               }
        }
 
        //
-       // Iterators are implemented as hidden anonymous block
+       // Iterators are implemented as state machine blocks
        //
        public class Iterator : StateMachineInitializer
        {
+               sealed class TryFinallyBlockProxyStatement : Statement
+               {
+                       TryFinallyBlock block;
+                       Iterator iterator;
+
+                       public TryFinallyBlockProxyStatement (Iterator iterator, TryFinallyBlock block)
+                       {
+                               this.iterator = iterator;
+                               this.block = block;
+                       }
+
+                       protected override void CloneTo (CloneContext clonectx, Statement target)
+                       {
+                               throw new NotSupportedException ();
+                       }
+
+                       protected override void DoEmit (EmitContext ec)
+                       {
+                               //
+                               // Restore redirection for any captured variables
+                               //
+                               ec.CurrentAnonymousMethod = iterator;
+
+                               using (ec.With (BuilderContext.Options.OmitDebugInfo, !ec.HasMethodSymbolBuilder)) {
+                                       block.EmitFinallyBody (ec);
+                               }
+                       }
+               }
+
                public readonly IMethodData OriginalMethod;
                public readonly bool IsEnumerable;
                public readonly TypeSpec OriginalIteratorType;
+               int finally_hosts_counter;
 
                public Iterator (ParametersBlock block, IMethodData method, TypeDefinition host, TypeSpec iterator_type, bool is_enumerable)
                        : base (block, host, host.Compiler.BuiltinTypes.Bool)
@@ -906,7 +946,9 @@ namespace Mono.CSharp
                        this.type = method.ReturnType;
                }
 
-               public Block Container {
+               #region Properties
+
+               public ToplevelBlock Container {
                        get { return OriginalMethod.Block; }
                }
 
@@ -918,6 +960,24 @@ namespace Mono.CSharp
                        get { return true; }
                }
 
+               #endregion
+
+               public Method CreateFinallyHost (TryFinallyBlock block)
+               {
+                       var method = new Method (storey, new TypeExpression (storey.Compiler.BuiltinTypes.Void, loc),
+                               Modifiers.COMPILER_GENERATED, new MemberName (CompilerGeneratedContainer.MakeName (null, null, "Finally", finally_hosts_counter++), loc),
+                               ParametersCompiled.EmptyReadOnlyParameters, null);
+
+                       method.Block = new ToplevelBlock (method.Compiler, method.ParameterInfo, loc);
+                       method.Block.IsCompilerGenerated = true;
+                       method.Block.AddStatement (new TryFinallyBlockProxyStatement (this, block));
+
+                       // Cannot it add to storey because it'd be emitted before nested
+                       // anonoymous methods which could capture shared variable
+
+                       return method;
+               }
+
                public void EmitYieldBreak (EmitContext ec, bool unwind_protect)
                {
                        ec.Emit (unwind_protect ? OpCodes.Leave : OpCodes.Br, move_next_error);
@@ -953,11 +1013,13 @@ namespace Mono.CSharp
 
                public void EmitDispose (EmitContext ec)
                {
+                       if (resume_points == null)
+                               return;
+
                        Label end = ec.DefineLabel ();
 
                        Label[] labels = null;
-                       int n_resume_points = resume_points == null ? 0 : resume_points.Count;
-                       for (int i = 0; i < n_resume_points; ++i) {
+                       for (int i = 0; i < resume_points.Count; ++i) {
                                ResumableStatement s = resume_points[i];
                                Label ret = s.PrepareForDispose (ec, end);
                                if (ret.Equals (end) && labels == null)
@@ -1047,7 +1109,7 @@ namespace Mono.CSharp
                        for (int i = 0; i < parameters.Count; i++) {
                                Parameter p = parameters [i];
                                Parameter.Modifier mod = p.ModFlags;
-                               if ((mod & Parameter.Modifier.ISBYREF) != 0) {
+                               if ((mod & Parameter.Modifier.RefOutMask) != 0) {
                                        parent.Compiler.Report.Error (1623, p.Location,
                                                "Iterators cannot have ref or out parameters");
                                        return;
@@ -1070,7 +1132,7 @@ namespace Mono.CSharp
                                parent.Compiler.Report.Error (1629, method.Location, "Unsafe code may not appear in iterators");
                        }
 
-                       method.Block.WrapIntoIterator (method, parent, iterator_type, is_enumerable);
+                       method.Block = method.Block.ConvertToIterator (method, parent, iterator_type, is_enumerable);
                }
 
                static bool CheckType (TypeSpec ret, TypeContainer parent, out TypeSpec original_iterator_type, out bool is_enumerable)