3afe93ce5ffe1bf89df1833938932a96f7abb294
[mono.git] / mcs / mcs / iterators.cs
1 //
2 // iterators.cs: Support for implementing iterators
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //   Marek Safar (marek.safar@gmail.com)
7 //
8 // Dual licensed under the terms of the MIT X11 or GNU GPL
9 // Copyright 2003 Ximian, Inc.
10 // Copyright 2003-2008 Novell, Inc.
11 // Copyright 2011 Xamarin Inc.
12 //
13
14 using System;
15 using System.Collections.Generic;
16 using Mono.CompilerServices.SymbolWriter;
17
18 #if STATIC
19 using IKVM.Reflection.Emit;
20 #else
21 using System.Reflection.Emit;
22 #endif
23
24 namespace Mono.CSharp
25 {
26         public abstract class YieldStatement<T> : ResumableStatement where T : StateMachineInitializer
27         {
28                 protected Expression expr;
29                 protected bool unwind_protect;
30                 protected T machine_initializer;
31                 int resume_pc;
32                 ExceptionStatement inside_try_block;
33                 TryCatch inside_catch_block;
34
35                 protected YieldStatement (Expression expr, Location l)
36                 {
37                         this.expr = expr;
38                         loc = l;
39                 }
40
41                 public Expression Expr {
42                         get { return this.expr; }
43                 }
44                 
45                 protected override void CloneTo (CloneContext clonectx, Statement t)
46                 {
47                         var target = (YieldStatement<T>) t;
48                         target.expr = expr.Clone (clonectx);
49                 }
50
51                 protected override void DoEmit (EmitContext ec)
52                 {
53                         machine_initializer.InjectYield (ec, expr, resume_pc, unwind_protect, resume_point);
54                 }
55
56                 protected override bool DoFlowAnalysis (FlowAnalysisContext fc)
57                 {
58                         expr.FlowAnalysis (fc);
59
60                         RegisterResumePoint ();
61
62                         return false;
63                 }
64
65                 public override bool Resolve (BlockContext bc)
66                 {
67                         expr = expr.Resolve (bc);
68                         if (expr == null)
69                                 return false;
70
71                         machine_initializer = bc.CurrentAnonymousMethod as T;
72                         inside_try_block = bc.CurrentTryBlock;
73                         inside_catch_block = bc.CurrentTryCatch;
74                         return true;
75                 }
76
77                 public void RegisterResumePoint ()
78                 {
79                         if (resume_pc != 0)
80                                 return;
81
82                         if (inside_try_block == null) {
83                                 resume_pc = machine_initializer.AddResumePoint (this);
84                         } else {
85                                 resume_pc = inside_try_block.AddResumePoint (this, resume_pc, machine_initializer, inside_catch_block);
86                                 unwind_protect = true;
87                                 inside_try_block = null;
88                         }
89                 }
90         }
91
92         public class Yield : YieldStatement<Iterator>
93         {
94                 public Yield (Expression expr, Location loc)
95                         : base (expr, loc)
96                 {
97                 }
98
99                 public static bool CheckContext (BlockContext bc, Location loc)
100                 {
101                         if (!bc.CurrentAnonymousMethod.IsIterator) {
102                                 bc.Report.Error (1621, loc,
103                                         "The yield statement cannot be used inside anonymous method blocks");
104                                 return false;
105                         }
106
107                         if (bc.HasSet (ResolveContext.Options.FinallyScope)) {
108                                 bc.Report.Error (1625, loc, "Cannot yield in the body of a finally clause");
109                                 return false;
110                         }
111
112                         return true;
113                 }
114
115                 public override bool Resolve (BlockContext bc)
116                 {
117                         if (!CheckContext (bc, loc))
118                                 return false;
119
120                         if (bc.HasAny (ResolveContext.Options.TryWithCatchScope)) {
121                                 bc.Report.Error (1626, loc, "Cannot yield a value in the body of a try block with a catch clause");
122                         }
123
124                         if (bc.HasSet (ResolveContext.Options.CatchScope)) {
125                                 bc.Report.Error (1631, loc, "Cannot yield a value in the body of a catch clause");
126                         }
127
128                         if (!base.Resolve (bc))
129                                 return false;
130
131                         var otype = bc.CurrentIterator.OriginalIteratorType;
132                         if (expr.Type != otype) {
133                                 expr = Convert.ImplicitConversionRequired (bc, expr, otype, loc);
134                                 if (expr == null)
135                                         return false;
136                         }
137
138                         return true;
139                 }
140                 
141                 public override object Accept (StructuralVisitor visitor)
142                 {
143                         return visitor.Visit (this);
144                 }
145         }
146
147         public class YieldBreak : ExitStatement
148         {
149                 Iterator iterator;
150
151                 public YieldBreak (Location l)
152                 {
153                         loc = l;
154                 }
155
156                 protected override bool IsLocalExit {
157                         get {
158                                 return false;
159                         }
160                 }
161
162                 protected override void CloneTo (CloneContext clonectx, Statement target)
163                 {
164                         throw new NotSupportedException ();
165                 }
166
167                 protected override bool DoResolve (BlockContext bc)
168                 {
169                         iterator = bc.CurrentIterator;
170                         return Yield.CheckContext (bc, loc);
171                 }
172
173                 protected override void DoEmit (EmitContext ec)
174                 {
175                         iterator.EmitYieldBreak (ec, unwind_protect);
176                 }
177
178                 protected override bool DoFlowAnalysis (FlowAnalysisContext fc)
179                 {
180                         return true;
181                 }
182
183                 public override Reachability MarkReachable (Reachability rc)
184                 {
185                         base.MarkReachable (rc);
186                         return Reachability.CreateUnreachable ();
187                 }
188                 
189                 public override object Accept (StructuralVisitor visitor)
190                 {
191                         return visitor.Visit (this);
192                 }
193         }
194
195         public abstract class StateMachine : AnonymousMethodStorey
196         {
197                 public enum State
198                 {
199                         Running = -3, // Used only in CurrentPC, never stored into $PC
200                         Uninitialized = -2,
201                         After = -1,
202                         Start = 0
203                 }
204
205                 Field pc_field;
206                 StateMachineMethod method;
207
208                 protected StateMachine (ParametersBlock block, TypeDefinition parent, MemberBase host, TypeParameters tparams, string name, MemberKind kind)
209                         : base (block, parent, host, tparams, name, kind)
210                 {
211                         OriginalTypeParameters = tparams;
212                 }
213
214                 #region Properties
215
216                 public TypeParameters OriginalTypeParameters { get; private set; }
217
218                 public StateMachineMethod StateMachineMethod {
219                         get {
220                                 return method;
221                         }
222                 }
223
224                 public Field PC {
225                         get {
226                                 return pc_field;
227                         }
228                 }
229
230                 #endregion
231
232                 public void AddEntryMethod (StateMachineMethod method)
233                 {
234                         if (this.method != null)
235                                 throw new InternalErrorException ();
236
237                         this.method = method;
238                         Members.Add (method);
239                 }
240
241                 protected override bool DoDefineMembers ()
242                 {
243                         pc_field = AddCompilerGeneratedField ("$PC", new TypeExpression (Compiler.BuiltinTypes.Int, Location));
244
245                         return base.DoDefineMembers ();
246                 }
247
248                 protected override string GetVariableMangledName (ResolveContext rc, LocalVariable local_info)
249                 {
250                         if (local_info.IsCompilerGenerated)
251                                 return base.GetVariableMangledName (rc, local_info);
252
253                         //
254                         // Special format which encodes original variable name and
255                         // it's scope to support lifted variables debugging. This
256                         // is same what csc does and allows to correctly set fields
257                         // scope information (like ambiguity, out of scope, etc).
258                         //
259                         var id = rc.CurrentBlock.Explicit.GetDebugSymbolScopeIndex ();
260                         return "<" + local_info.Name + ">__" + id;
261                 }
262         }
263
264         class IteratorStorey : StateMachine
265         {
266                 class GetEnumeratorMethod : StateMachineMethod
267                 {
268                         sealed class GetEnumeratorStatement : Statement
269                         {
270                                 readonly IteratorStorey host;
271                                 readonly StateMachineMethod host_method;
272
273                                 Expression new_storey;
274
275                                 public GetEnumeratorStatement (IteratorStorey host, StateMachineMethod host_method)
276                                 {
277                                         this.host = host;
278                                         this.host_method = host_method;
279                                         loc = host_method.Location;
280                                 }
281
282                                 protected override void CloneTo (CloneContext clonectx, Statement target)
283                                 {
284                                         throw new NotSupportedException ();
285                                 }
286
287                                 public override bool Resolve (BlockContext ec)
288                                 {
289                                         TypeExpression storey_type_expr = new TypeExpression (host.Definition, loc);
290                                         List<Expression> init = null;
291                                         if (host.hoisted_this != null) {
292                                                 init = new List<Expression> (host.hoisted_params == null ? 1 : host.HoistedParameters.Count + 1);
293                                                 HoistedThis ht = host.hoisted_this;
294                                                 FieldExpr from = new FieldExpr (ht.Field, loc);
295                                                 from.InstanceExpression = new CompilerGeneratedThis (ec.CurrentType, loc);
296                                                 init.Add (new ElementInitializer (ht.Field.Name, from, loc));
297                                         }
298
299                                         if (host.hoisted_params != null) {
300                                                 if (init == null)
301                                                         init = new List<Expression> (host.HoistedParameters.Count);
302
303                                                 for (int i = 0; i < host.hoisted_params.Count; ++i) {
304                                                         HoistedParameter hp = host.hoisted_params [i];
305                                                         HoistedParameter hp_cp = host.hoisted_params_copy [i] ?? hp;
306
307                                                         FieldExpr from = new FieldExpr (hp_cp.Field, loc);
308                                                         from.InstanceExpression = new CompilerGeneratedThis (ec.CurrentType, loc);
309
310                                                         init.Add (new ElementInitializer (hp.Field.Name, from, loc));
311                                                 }
312                                         }
313
314                                         if (init != null) {
315                                                 new_storey = new NewInitialize (storey_type_expr, null,
316                                                         new CollectionOrObjectInitializers (init, loc), loc);
317                                         } else {
318                                                 new_storey = new New (storey_type_expr, null, loc);
319                                         }
320
321                                         new_storey = new_storey.Resolve (ec);
322                                         if (new_storey != null)
323                                                 new_storey = Convert.ImplicitConversionRequired (ec, new_storey, host_method.MemberType, loc);
324
325                                         return true;
326                                 }
327
328                                 protected override void DoEmit (EmitContext ec)
329                                 {
330                                         Label label_init = ec.DefineLabel ();
331
332                                         ec.EmitThis ();
333                                         ec.Emit (OpCodes.Ldflda, host.PC.Spec);
334                                         ec.EmitInt ((int) State.Start);
335                                         ec.EmitInt ((int) State.Uninitialized);
336
337                                         var m = ec.Module.PredefinedMembers.InterlockedCompareExchange.Resolve (loc);
338                                         if (m != null)
339                                                 ec.Emit (OpCodes.Call, m);
340
341                                         ec.EmitInt ((int) State.Uninitialized);
342                                         ec.Emit (OpCodes.Bne_Un_S, label_init);
343
344                                         ec.EmitThis ();
345                                         ec.Emit (OpCodes.Ret);
346
347                                         ec.MarkLabel (label_init);
348
349                                         new_storey.Emit (ec);
350                                         ec.Emit (OpCodes.Ret);
351                                 }
352
353                                 protected override bool DoFlowAnalysis (FlowAnalysisContext fc)
354                                 {
355                                         throw new NotImplementedException ();
356                                 }
357
358                                 public override Reachability MarkReachable (Reachability rc)
359                                 {
360                                         base.MarkReachable (rc);
361                                         return Reachability.CreateUnreachable ();
362                                 }
363                         }
364
365                         GetEnumeratorMethod (IteratorStorey host, FullNamedExpression returnType, MemberName name)
366                                 : base (host, null, returnType, Modifiers.DEBUGGER_HIDDEN, name, ToplevelBlock.Flags.CompilerGenerated | ToplevelBlock.Flags.NoFlowAnalysis)
367                         {
368                         }
369
370                         public static GetEnumeratorMethod Create (IteratorStorey host, FullNamedExpression returnType, MemberName name)
371                         {
372                                 return Create (host, returnType, name, null);
373                         }
374
375                         public static GetEnumeratorMethod Create (IteratorStorey host, FullNamedExpression returnType, MemberName name, Statement statement)
376                         {
377                                 var m = new GetEnumeratorMethod (host, returnType, name);
378                                 var stmt = statement ?? new GetEnumeratorStatement (host, m);
379                                 m.block.AddStatement (stmt);
380                                 return m;
381                         }
382                 }
383
384                 class DisposeMethod : StateMachineMethod
385                 {
386                         sealed class DisposeMethodStatement : Statement
387                         {
388                                 Iterator iterator;
389
390                                 public DisposeMethodStatement (Iterator iterator)
391                                 {
392                                         this.iterator = iterator;
393                                         this.loc = iterator.Location;
394                                 }
395
396                                 protected override void CloneTo (CloneContext clonectx, Statement target)
397                                 {
398                                         throw new NotSupportedException ();
399                                 }
400
401                                 public override bool Resolve (BlockContext ec)
402                                 {
403                                         return true;
404                                 }
405
406                                 protected override void DoEmit (EmitContext ec)
407                                 {
408                                         ec.CurrentAnonymousMethod = iterator;
409                                         iterator.EmitDispose (ec);
410                                 }
411
412                                 protected override bool DoFlowAnalysis (FlowAnalysisContext fc)
413                                 {
414                                         throw new NotImplementedException ();
415                                 }
416                         }
417
418                         public DisposeMethod (IteratorStorey host)
419                                 : base (host, null, new TypeExpression (host.Compiler.BuiltinTypes.Void, host.Location), Modifiers.PUBLIC | Modifiers.DEBUGGER_HIDDEN,
420                                         new MemberName ("Dispose", host.Location), ToplevelBlock.Flags.CompilerGenerated | ToplevelBlock.Flags.NoFlowAnalysis)
421                         {
422                                 host.Members.Add (this);
423
424                                 Block.AddStatement (new DisposeMethodStatement (host.Iterator));
425                         }
426                 }
427
428                 //
429                 // Uses Method as method info
430                 //
431                 class DynamicMethodGroupExpr : MethodGroupExpr
432                 {
433                         readonly Method method;
434
435                         public DynamicMethodGroupExpr (Method method, Location loc)
436                                 : base ((IList<MemberSpec>) null, null, loc)
437                         {
438                                 this.method = method;
439                                 eclass = ExprClass.Unresolved;
440                         }
441
442                         protected override Expression DoResolve (ResolveContext ec)
443                         {
444                                 Methods = new List<MemberSpec> (1) { method.Spec };
445                                 type = method.Parent.Definition;
446                                 InstanceExpression = new CompilerGeneratedThis (type, Location);
447                                 return base.DoResolve (ec);
448                         }
449                 }
450
451                 class DynamicFieldExpr : FieldExpr
452                 {
453                         readonly Field field;
454
455                         public DynamicFieldExpr (Field field, Location loc)
456                                 : base (loc)
457                         {
458                                 this.field = field;
459                         }
460
461                         protected override Expression DoResolve (ResolveContext ec)
462                         {
463                                 spec = field.Spec;
464                                 type = spec.MemberType;
465                                 InstanceExpression = new CompilerGeneratedThis (type, Location);
466                                 return base.DoResolve (ec);
467                         }
468                 }
469
470                 public readonly Iterator Iterator;
471
472                 List<HoistedParameter> hoisted_params_copy;
473
474                 TypeExpr iterator_type_expr;
475                 Field current_field;
476                 Field disposing_field;
477
478                 TypeSpec generic_enumerator_type;
479                 TypeSpec generic_enumerable_type;
480
481                 public IteratorStorey (Iterator iterator)
482                         : base (iterator.Container.ParametersBlock, iterator.Host,
483                           iterator.OriginalMethod as MemberBase, iterator.OriginalMethod.CurrentTypeParameters, "Iterator", MemberKind.Class)
484                 {
485                         this.Iterator = iterator;
486                 }
487
488                 public Field CurrentField {
489                         get {
490                                 return current_field;
491                         }
492                 }
493
494                 public Field DisposingField {
495                         get {
496                                 return disposing_field;
497                         }
498                 }
499
500                 public IList<HoistedParameter> HoistedParameters {
501                         get { return hoisted_params; }
502                 }
503
504                 protected override Constructor DefineDefaultConstructor (bool is_static)
505                 {
506                         var ctor = base.DefineDefaultConstructor (is_static);
507                         ctor.ModFlags |= Modifiers.DEBUGGER_HIDDEN;
508                         return ctor;
509                 }
510
511                 protected override TypeSpec[] ResolveBaseTypes (out FullNamedExpression base_class)
512                 {
513                         var mtype = Iterator.OriginalIteratorType;
514                         if (Mutator != null)
515                                 mtype = Mutator.Mutate (mtype);
516
517                         iterator_type_expr = new TypeExpression (mtype, Location);
518
519                         var ifaces = new List<TypeSpec> (5);
520                         if (Iterator.IsEnumerable) {
521                                 ifaces.Add (Compiler.BuiltinTypes.IEnumerable);
522
523                                 if (Module.PredefinedTypes.IEnumerableGeneric.Define ()) {
524                                         generic_enumerable_type = Module.PredefinedTypes.IEnumerableGeneric.TypeSpec.MakeGenericType (Module, new[] { mtype });
525                                         ifaces.Add (generic_enumerable_type);
526                                 }
527                         }
528
529                         ifaces.Add (Compiler.BuiltinTypes.IEnumerator);
530                         ifaces.Add (Compiler.BuiltinTypes.IDisposable);
531
532                         var ienumerator_generic = Module.PredefinedTypes.IEnumeratorGeneric;
533                         if (ienumerator_generic.Define ()) {
534                                 generic_enumerator_type = ienumerator_generic.TypeSpec.MakeGenericType (Module, new [] { mtype });
535                                 ifaces.Add (generic_enumerator_type);
536                         }
537
538                         base_class = null;
539
540                         base_type = Compiler.BuiltinTypes.Object;
541                         return ifaces.ToArray ();
542                 }
543
544                 protected override bool DoDefineMembers ()
545                 {
546                         current_field = AddCompilerGeneratedField ("$current", iterator_type_expr);
547                         disposing_field = AddCompilerGeneratedField ("$disposing", new TypeExpression (Compiler.BuiltinTypes.Bool, Location));
548
549                         if (Iterator.IsEnumerable && hoisted_params != null) {
550                                 //
551                                 // Iterators are independent, each GetEnumerator call has to
552                                 // create same enumerator therefore we have to keep original values
553                                 // around for re-initialization
554                                 //
555                                 hoisted_params_copy = new List<HoistedParameter> (hoisted_params.Count);
556                                 foreach (HoistedParameter hp in hoisted_params) {
557
558                                         //
559                                         // Don't create field copy for unmodified captured parameters
560                                         //
561                                         HoistedParameter hp_copy;
562                                         if (hp.IsAssigned) {
563                                                 hp_copy = new HoistedParameter (hp, "<$>" + hp.Field.Name);
564                                         } else {
565                                                 hp_copy = null;
566                                         }
567
568                                         hoisted_params_copy.Add (hp_copy);
569                                 }
570                         }
571
572                         if (generic_enumerator_type != null)
573                                 Define_Current (true);
574
575                         Define_Current (false);
576                         new DisposeMethod (this);
577                         Define_Reset ();
578
579                         if (Iterator.IsEnumerable) {
580                                 FullNamedExpression explicit_iface = new TypeExpression (Compiler.BuiltinTypes.IEnumerable, Location);
581                                 var name = new MemberName ("GetEnumerator", null, explicit_iface, Location.Null);
582
583                                 if (generic_enumerator_type != null) {
584                                         explicit_iface = new TypeExpression (generic_enumerable_type, Location);
585                                         var gname = new MemberName ("GetEnumerator", null, explicit_iface, Location.Null);
586                                         Method gget_enumerator = GetEnumeratorMethod.Create (this, new TypeExpression (generic_enumerator_type, Location), gname);
587
588                                         //
589                                         // Just call generic GetEnumerator implementation
590                                         //
591                                         var stmt = new Return (new Invocation (new DynamicMethodGroupExpr (gget_enumerator, Location), null), Location);
592                                         Method get_enumerator = GetEnumeratorMethod.Create (this, new TypeExpression (Compiler.BuiltinTypes.IEnumerator, Location), name, stmt);
593
594                                         Members.Add (get_enumerator);
595                                         Members.Add (gget_enumerator);
596                                 } else {
597                                         Members.Add (GetEnumeratorMethod.Create (this, new TypeExpression (Compiler.BuiltinTypes.IEnumerator, Location), name));
598                                 }
599                         }
600
601                         return base.DoDefineMembers ();
602                 }
603
604                 void Define_Current (bool is_generic)
605                 {
606                         TypeExpr type;
607                         FullNamedExpression explicit_iface;
608
609                         if (is_generic) {
610                                 explicit_iface = new TypeExpression (generic_enumerator_type, Location);
611                                 type = iterator_type_expr;
612                         } else {
613                                 explicit_iface = new TypeExpression (Module.Compiler.BuiltinTypes.IEnumerator, Location);
614                                 type = new TypeExpression (Compiler.BuiltinTypes.Object, Location);
615                         }
616
617                         var name = new MemberName ("Current", null, explicit_iface, Location);
618
619                         ToplevelBlock get_block = new ToplevelBlock (Compiler, ParametersCompiled.EmptyReadOnlyParameters, Location,
620                                 Block.Flags.CompilerGenerated | Block.Flags.NoFlowAnalysis);
621                         get_block.AddStatement (new Return (new DynamicFieldExpr (CurrentField, Location), Location));
622                                 
623                         Property current = new Property (this, type, Modifiers.DEBUGGER_HIDDEN | Modifiers.COMPILER_GENERATED, name, null);
624                         current.Get = new Property.GetMethod (current, Modifiers.COMPILER_GENERATED, null, Location);
625                         current.Get.Block = get_block;
626
627                         Members.Add (current);
628                 }
629
630                 void Define_Reset ()
631                 {
632                         Method reset = new Method (
633                                 this, new TypeExpression (Compiler.BuiltinTypes.Void, Location),
634                                 Modifiers.PUBLIC | Modifiers.DEBUGGER_HIDDEN | Modifiers.COMPILER_GENERATED,
635                                 new MemberName ("Reset", Location),
636                                 ParametersCompiled.EmptyReadOnlyParameters, null);
637                         Members.Add (reset);
638
639                         reset.Block = new ToplevelBlock (Compiler, reset.ParameterInfo, Location,
640                                 Block.Flags.CompilerGenerated | Block.Flags.NoFlowAnalysis);
641
642                         TypeSpec ex_type = Module.PredefinedTypes.NotSupportedException.Resolve ();
643                         if (ex_type == null)
644                                 return;
645
646                         reset.Block.AddStatement (new Throw (new New (new TypeExpression (ex_type, Location), null, Location), Location));
647                 }
648
649                 protected override void EmitHoistedParameters (EmitContext ec, List<HoistedParameter> hoisted)
650                 {
651                         base.EmitHoistedParameters (ec, hoisted);
652                         if (hoisted_params_copy != null)
653                                 base.EmitHoistedParameters (ec, hoisted_params_copy);
654                 }
655         }
656
657         public class StateMachineMethod : Method
658         {
659                 readonly StateMachineInitializer expr;
660
661                 public StateMachineMethod (StateMachine host, StateMachineInitializer expr, FullNamedExpression returnType,
662                         Modifiers mod, MemberName name, ToplevelBlock.Flags blockFlags)
663                         : base (host, returnType, mod | Modifiers.COMPILER_GENERATED,
664                           name, ParametersCompiled.EmptyReadOnlyParameters, null)
665                 {
666                         this.expr = expr;
667                         Block = new ToplevelBlock (host.Compiler, ParametersCompiled.EmptyReadOnlyParameters, Location.Null, blockFlags);
668                 }
669
670                 public override EmitContext CreateEmitContext (ILGenerator ig, SourceMethodBuilder sourceMethod)
671                 {
672                         EmitContext ec = new EmitContext (this, ig, MemberType, sourceMethod);
673                         ec.CurrentAnonymousMethod = expr;
674
675                         if (expr is AsyncInitializer)
676                                 ec.With (BuilderContext.Options.AsyncBody, true);
677
678                         return ec;
679                 }
680         }
681
682         public abstract class StateMachineInitializer : AnonymousExpression
683         {
684                 sealed class MoveNextBodyStatement : Statement
685                 {
686                         readonly StateMachineInitializer state_machine;
687
688                         public MoveNextBodyStatement (StateMachineInitializer stateMachine)
689                         {
690                                 this.state_machine = stateMachine;
691                                 this.loc = stateMachine.Location;
692                         }
693
694                         protected override void CloneTo (CloneContext clonectx, Statement target)
695                         {
696                                 throw new NotSupportedException ();
697                         }
698
699                         public override bool Resolve (BlockContext ec)
700                         {
701                                 return true;
702                         }
703
704                         protected override void DoEmit (EmitContext ec)
705                         {
706                                 state_machine.EmitMoveNext (ec);
707                         }
708
709                         public override void Emit (EmitContext ec)
710                         {
711                                 // Don't create sequence point
712                                 DoEmit (ec);
713                         }
714
715                         protected override bool DoFlowAnalysis (FlowAnalysisContext fc)
716                         {
717                                 return state_machine.ReturnType.Kind != MemberKind.Void;
718                         }
719
720                         public override Reachability MarkReachable (Reachability rc)
721                         {
722                                 base.MarkReachable (rc);
723
724                                 if (state_machine.ReturnType.Kind != MemberKind.Void)
725                                         rc = Reachability.CreateUnreachable ();
726
727                                 return rc;
728                         }
729                 }
730
731                 public readonly TypeDefinition Host;
732                 protected StateMachine storey;
733
734                 //
735                 // The state as we generate the machine
736                 //
737                 protected Label move_next_ok;
738                 protected Label move_next_error;
739                 LocalBuilder skip_finally;
740                 protected LocalBuilder current_pc;
741                 protected List<ResumableStatement> resume_points;
742
743                 protected StateMachineInitializer (ParametersBlock block, TypeDefinition host, TypeSpec returnType)
744                         : base (block, returnType, block.StartLocation)
745                 {
746                         this.Host = host;
747                 }
748
749                 #region Properties
750
751                 public Label BodyEnd { get; set; }
752
753                 public LocalBuilder CurrentPC
754                 {
755                         get {
756                                 return current_pc;
757                         }
758                 }
759
760                 public LocalBuilder SkipFinally {
761                         get {
762                                 return skip_finally;
763                         }
764                 }
765
766                 public override AnonymousMethodStorey Storey {
767                         get {
768                                 return storey;
769                         }
770                 }
771
772                 #endregion
773
774                 public int AddResumePoint (ResumableStatement stmt)
775                 {
776                         if (resume_points == null)
777                                 resume_points = new List<ResumableStatement> ();
778
779                         resume_points.Add (stmt);
780                         return resume_points.Count;
781                 }
782
783                 public override Expression CreateExpressionTree (ResolveContext ec)
784                 {
785                         throw new NotSupportedException ("ET");
786                 }
787
788                 protected virtual BlockContext CreateBlockContext (BlockContext bc)
789                 {
790                         var ctx = new BlockContext (bc, block, bc.ReturnType);
791                         ctx.CurrentAnonymousMethod = this;
792
793                         ctx.AssignmentInfoOffset = bc.AssignmentInfoOffset;
794                         ctx.EnclosingLoop = bc.EnclosingLoop;
795                         ctx.EnclosingLoopOrSwitch = bc.EnclosingLoopOrSwitch;
796                         ctx.Switch = bc.Switch;
797
798                         return ctx;
799                 }
800
801                 protected override Expression DoResolve (ResolveContext rc)
802                 {
803                         var bc = (BlockContext) rc;
804                         var ctx = CreateBlockContext (bc);
805
806                         Block.Resolve (ctx);
807
808                         if (!rc.IsInProbingMode) {
809                                 var move_next = new StateMachineMethod (storey, this, new TypeExpression (ReturnType, loc), Modifiers.PUBLIC, new MemberName ("MoveNext", loc), 0);
810                                 move_next.Block.AddStatement (new MoveNextBodyStatement (this));
811                                 storey.AddEntryMethod (move_next);
812                         }
813
814                         bc.AssignmentInfoOffset = ctx.AssignmentInfoOffset;
815                         eclass = ExprClass.Value;
816                         return this;
817                 }
818
819                 public override void Emit (EmitContext ec)
820                 {
821                         //
822                         // Load state machine instance
823                         //
824                         storey.Instance.Emit (ec);
825                 }
826
827                 void EmitMoveNext_NoResumePoints (EmitContext ec)
828                 {
829                         ec.EmitThis ();
830                         ec.Emit (OpCodes.Ldfld, storey.PC.Spec);
831
832                         ec.EmitThis ();
833                         ec.EmitInt ((int) IteratorStorey.State.After);
834                         ec.Emit (OpCodes.Stfld, storey.PC.Spec);
835
836                         // We only care if the PC is zero (start executing) or non-zero (don't do anything)
837                         ec.Emit (OpCodes.Brtrue, move_next_error);
838
839                         BodyEnd = ec.DefineLabel ();
840
841                         var async_init = this as AsyncInitializer;
842                         if (async_init != null)
843                                 ec.BeginExceptionBlock ();
844
845                         block.EmitEmbedded (ec);
846
847                         if (async_init != null)
848                                 async_init.EmitCatchBlock (ec);
849
850                         ec.MarkLabel (BodyEnd);
851
852                         EmitMoveNextEpilogue (ec);
853
854                         ec.MarkLabel (move_next_error);
855
856                         if (ReturnType.Kind != MemberKind.Void) {
857                                 ec.EmitInt (0);
858                                 ec.Emit (OpCodes.Ret);
859                         }
860
861                         ec.MarkLabel (move_next_ok);
862                 }
863
864                 void EmitMoveNext (EmitContext ec)
865                 {
866                         move_next_ok = ec.DefineLabel ();
867                         move_next_error = ec.DefineLabel ();
868
869                         if (resume_points == null) {
870                                 EmitMoveNext_NoResumePoints (ec);
871                                 return;
872                         }
873                         
874                         current_pc = ec.GetTemporaryLocal (ec.BuiltinTypes.UInt);
875                         ec.EmitThis ();
876                         ec.Emit (OpCodes.Ldfld, storey.PC.Spec);
877                         ec.Emit (OpCodes.Stloc, current_pc);
878
879                         // We're actually in state 'running', but this is as good a PC value as any if there's an abnormal exit
880                         ec.EmitThis ();
881                         ec.EmitInt ((int) IteratorStorey.State.After);
882                         ec.Emit (OpCodes.Stfld, storey.PC.Spec);
883
884                         Label[] labels = new Label[1 + resume_points.Count];
885                         labels[0] = ec.DefineLabel ();
886
887                         bool need_skip_finally = false;
888                         for (int i = 0; i < resume_points.Count; ++i) {
889                                 ResumableStatement s = resume_points[i];
890                                 need_skip_finally |= s is ExceptionStatement;
891                                 labels[i + 1] = s.PrepareForEmit (ec);
892                         }
893
894                         if (need_skip_finally) {
895                                 skip_finally = ec.GetTemporaryLocal (ec.BuiltinTypes.Bool);
896                                 ec.EmitInt (0);
897                                 ec.Emit (OpCodes.Stloc, skip_finally);
898                         }
899
900                         var async_init = this as AsyncInitializer;
901                         if (async_init != null)
902                                 ec.BeginExceptionBlock ();
903
904                         ec.Emit (OpCodes.Ldloc, current_pc);
905                         ec.Emit (OpCodes.Switch, labels);
906
907                         ec.Emit (async_init != null ? OpCodes.Leave : OpCodes.Br, move_next_error);
908
909                         ec.MarkLabel (labels[0]);
910
911                         BodyEnd = ec.DefineLabel ();
912
913                         block.EmitEmbedded (ec);
914
915                         ec.MarkLabel (BodyEnd);
916
917                         if (async_init != null) {
918                                 async_init.EmitCatchBlock (ec);
919                         }
920
921                         ec.Mark (Block.Original.EndLocation);
922                         ec.EmitThis ();
923                         ec.EmitInt ((int) IteratorStorey.State.After);
924                         ec.Emit (OpCodes.Stfld, storey.PC.Spec);
925
926                         EmitMoveNextEpilogue (ec);
927
928                         ec.MarkLabel (move_next_error);
929                         
930                         if (ReturnType.Kind != MemberKind.Void) {
931                                 ec.EmitInt (0);
932                                 ec.Emit (OpCodes.Ret);
933                         }
934
935                         ec.MarkLabel (move_next_ok);
936
937                         if (ReturnType.Kind != MemberKind.Void) {
938                                 ec.EmitInt (1);
939                                 ec.Emit (OpCodes.Ret);
940                         }
941                 }
942
943                 protected virtual void EmitMoveNextEpilogue (EmitContext ec)
944                 {
945                 }
946
947                 public void EmitLeave (EmitContext ec, bool unwind_protect)
948                 {
949                         // Return ok
950                         ec.Emit (unwind_protect ? OpCodes.Leave : OpCodes.Br, move_next_ok);
951                 }
952
953                 //
954                 // Called back from YieldStatement
955                 //
956                 public virtual void InjectYield (EmitContext ec, Expression expr, int resume_pc, bool unwind_protect, Label resume_point)
957                 {
958                         //
959                         // Guard against being disposed meantime
960                         //
961                         Label disposed = ec.DefineLabel ();
962                         var iterator = storey as IteratorStorey;
963                         if (iterator != null) {
964                                 ec.EmitThis ();
965                                 ec.Emit (OpCodes.Ldfld, iterator.DisposingField.Spec);
966                                 ec.Emit (OpCodes.Brtrue_S, disposed);
967                         }
968
969                         //
970                         // store resume program-counter
971                         //
972                         ec.EmitThis ();
973                         ec.EmitInt (resume_pc);
974                         ec.Emit (OpCodes.Stfld, storey.PC.Spec);
975
976                         if (iterator != null) {
977                                 ec.MarkLabel (disposed);
978                         }
979
980                         // mark finally blocks as disabled
981                         if (unwind_protect && skip_finally != null) {
982                                 ec.EmitInt (1);
983                                 ec.Emit (OpCodes.Stloc, skip_finally);
984                         }
985                 }
986
987                 public void SetStateMachine (StateMachine stateMachine)
988                 {
989                         this.storey = stateMachine;
990                 }
991         }
992
993         //
994         // Iterators are implemented as state machine blocks
995         //
996         public class Iterator : StateMachineInitializer
997         {
998                 sealed class TryFinallyBlockProxyStatement : Statement
999                 {
1000                         TryFinallyBlock block;
1001                         Iterator iterator;
1002
1003                         public TryFinallyBlockProxyStatement (Iterator iterator, TryFinallyBlock block)
1004                         {
1005                                 this.iterator = iterator;
1006                                 this.block = block;
1007                         }
1008
1009                         protected override void CloneTo (CloneContext clonectx, Statement target)
1010                         {
1011                                 throw new NotSupportedException ();
1012                         }
1013
1014                         protected override bool DoFlowAnalysis (FlowAnalysisContext fc)
1015                         {
1016                                 throw new NotSupportedException ();
1017                         }
1018
1019                         protected override void DoEmit (EmitContext ec)
1020                         {
1021                                 //
1022                                 // Restore redirection for any captured variables
1023                                 //
1024                                 ec.CurrentAnonymousMethod = iterator;
1025
1026                                 using (ec.With (BuilderContext.Options.OmitDebugInfo, !ec.HasMethodSymbolBuilder)) {
1027                                         block.EmitFinallyBody (ec);
1028                                 }
1029                         }
1030                 }
1031
1032                 public readonly IMethodData OriginalMethod;
1033                 public readonly bool IsEnumerable;
1034                 public readonly TypeSpec OriginalIteratorType;
1035                 int finally_hosts_counter;
1036
1037                 public Iterator (ParametersBlock block, IMethodData method, TypeDefinition host, TypeSpec iterator_type, bool is_enumerable)
1038                         : base (block, host, host.Compiler.BuiltinTypes.Bool)
1039                 {
1040                         this.OriginalMethod = method;
1041                         this.OriginalIteratorType = iterator_type;
1042                         this.IsEnumerable = is_enumerable;
1043                         this.type = method.ReturnType;
1044                 }
1045
1046                 #region Properties
1047
1048                 public ToplevelBlock Container {
1049                         get { return OriginalMethod.Block; }
1050                 }
1051
1052                 public override string ContainerType {
1053                         get { return "iterator"; }
1054                 }
1055
1056                 public override bool IsIterator {
1057                         get { return true; }
1058                 }
1059
1060                 #endregion
1061
1062                 public Method CreateFinallyHost (TryFinallyBlock block)
1063                 {
1064                         var method = new Method (storey, new TypeExpression (storey.Compiler.BuiltinTypes.Void, loc),
1065                                 Modifiers.COMPILER_GENERATED, new MemberName (CompilerGeneratedContainer.MakeName (null, null, "Finally", finally_hosts_counter++), loc),
1066                                 ParametersCompiled.EmptyReadOnlyParameters, null);
1067
1068                         method.Block = new ToplevelBlock (method.Compiler, method.ParameterInfo, loc,
1069                                 ToplevelBlock.Flags.CompilerGenerated | ToplevelBlock.Flags.NoFlowAnalysis);
1070
1071                         method.Block.AddStatement (new TryFinallyBlockProxyStatement (this, block));
1072
1073                         // Cannot it add to storey because it'd be emitted before nested
1074                         // anonoymous methods which could capture shared variable
1075
1076                         return method;
1077                 }
1078
1079                 public void EmitYieldBreak (EmitContext ec, bool unwind_protect)
1080                 {
1081                         ec.Emit (unwind_protect ? OpCodes.Leave : OpCodes.Br, move_next_error);
1082                 }
1083
1084                 public override string GetSignatureForError ()
1085                 {
1086                         return OriginalMethod.GetSignatureForError ();
1087                 }
1088
1089                 public override void Emit (EmitContext ec)
1090                 {
1091                         //
1092                         // Load Iterator storey instance
1093                         //
1094                         storey.Instance.Emit (ec);
1095
1096                         //
1097                         // Initialize iterator PC when it's unitialized
1098                         //
1099                         if (IsEnumerable) {
1100                                 ec.Emit (OpCodes.Dup);
1101                                 ec.EmitInt ((int)IteratorStorey.State.Uninitialized);
1102
1103                                 var field = storey.PC.Spec;
1104                                 if (storey.MemberName.IsGeneric) {
1105                                         field = MemberCache.GetMember (Storey.Instance.Type, field);
1106                                 }
1107
1108                                 ec.Emit (OpCodes.Stfld, field);
1109                         }
1110                 }
1111
1112                 public void EmitDispose (EmitContext ec)
1113                 {
1114                         if (resume_points == null)
1115                                 return;
1116
1117                         Label end = ec.DefineLabel ();
1118
1119                         Label[] labels = null;
1120                         for (int i = 0; i < resume_points.Count; ++i) {
1121                                 ResumableStatement s = resume_points[i];
1122                                 Label ret = s.PrepareForDispose (ec, end);
1123                                 if (ret.Equals (end) && labels == null)
1124                                         continue;
1125                                 if (labels == null) {
1126                                         labels = new Label[resume_points.Count + 1];
1127                                         for (int j = 0; j <= i; ++j)
1128                                                 labels[j] = end;
1129                                 }
1130
1131                                 labels[i + 1] = ret;
1132                         }
1133
1134                         if (labels != null) {
1135                                 current_pc = ec.GetTemporaryLocal (ec.BuiltinTypes.UInt);
1136                                 ec.EmitThis ();
1137                                 ec.Emit (OpCodes.Ldfld, storey.PC.Spec);
1138                                 ec.Emit (OpCodes.Stloc, current_pc);
1139                         }
1140
1141                         ec.EmitThis ();
1142                         ec.EmitInt (1);
1143                         ec.Emit (OpCodes.Stfld, ((IteratorStorey) storey).DisposingField.Spec);
1144
1145                         ec.EmitThis ();
1146                         ec.EmitInt ((int) IteratorStorey.State.After);
1147                         ec.Emit (OpCodes.Stfld, storey.PC.Spec);
1148
1149                         if (labels != null) {
1150                                 //SymbolWriter.StartIteratorDispatcher (ec.ig);
1151                                 ec.Emit (OpCodes.Ldloc, current_pc);
1152                                 ec.Emit (OpCodes.Switch, labels);
1153                                 //SymbolWriter.EndIteratorDispatcher (ec.ig);
1154
1155                                 foreach (ResumableStatement s in resume_points)
1156                                         s.EmitForDispose (ec, current_pc, end, true);
1157                         }
1158
1159                         ec.MarkLabel (end);
1160                 }
1161
1162                 public override void EmitStatement (EmitContext ec)
1163                 {
1164                         throw new NotImplementedException ();
1165                 }
1166
1167                 public override void InjectYield (EmitContext ec, Expression expr, int resume_pc, bool unwind_protect, Label resume_point)
1168                 {
1169                         // Store the new value into current
1170                         var fe = new FieldExpr (((IteratorStorey) storey).CurrentField, loc);
1171                         fe.InstanceExpression = new CompilerGeneratedThis (storey.CurrentType, loc);
1172                         fe.EmitAssign (ec, expr, false, false);
1173
1174                         base.InjectYield (ec, expr, resume_pc, unwind_protect, resume_point);
1175
1176                         EmitLeave (ec, unwind_protect);
1177
1178                         ec.MarkLabel (resume_point);
1179                 }
1180
1181                 public static void CreateIterator (IMethodData method, TypeDefinition parent, Modifiers modifiers)
1182                 {
1183                         bool is_enumerable;
1184                         TypeSpec iterator_type;
1185
1186                         TypeSpec ret = method.ReturnType;
1187                         if (ret == null)
1188                                 return;
1189
1190                         if (!CheckType (ret, parent, out iterator_type, out is_enumerable)) {
1191                                 parent.Compiler.Report.Error (1624, method.Location,
1192                                               "The body of `{0}' cannot be an iterator block " +
1193                                               "because `{1}' is not an iterator interface type",
1194                                               method.GetSignatureForError (),
1195                                               ret.GetSignatureForError ());
1196                                 return;
1197                         }
1198
1199                         ParametersCompiled parameters = method.ParameterInfo;
1200                         for (int i = 0; i < parameters.Count; i++) {
1201                                 Parameter p = parameters [i];
1202                                 Parameter.Modifier mod = p.ModFlags;
1203                                 if ((mod & Parameter.Modifier.RefOutMask) != 0) {
1204                                         parent.Compiler.Report.Error (1623, p.Location,
1205                                                 "Iterators cannot have ref or out parameters");
1206                                         return;
1207                                 }
1208
1209                                 if (p is ArglistParameter) {
1210                                         parent.Compiler.Report.Error (1636, method.Location,
1211                                                 "__arglist is not allowed in parameter list of iterators");
1212                                         return;
1213                                 }
1214
1215                                 if (parameters.Types [i].IsPointer) {
1216                                         parent.Compiler.Report.Error (1637, p.Location,
1217                                                 "Iterators cannot have unsafe parameters or yield types");
1218                                         return;
1219                                 }
1220                         }
1221
1222                         if ((modifiers & Modifiers.UNSAFE) != 0) {
1223                                 Expression.UnsafeInsideIteratorError (parent.Compiler.Report, method.Location);
1224                         }
1225
1226                         method.Block = method.Block.ConvertToIterator (method, parent, iterator_type, is_enumerable);
1227                 }
1228
1229                 static bool CheckType (TypeSpec ret, TypeContainer parent, out TypeSpec original_iterator_type, out bool is_enumerable)
1230                 {
1231                         original_iterator_type = null;
1232                         is_enumerable = false;
1233
1234                         if (ret.BuiltinType == BuiltinTypeSpec.Type.IEnumerable) {
1235                                 original_iterator_type = parent.Compiler.BuiltinTypes.Object;
1236                                 is_enumerable = true;
1237                                 return true;
1238                         }
1239                         if (ret.BuiltinType == BuiltinTypeSpec.Type.IEnumerator) {
1240                                 original_iterator_type = parent.Compiler.BuiltinTypes.Object;
1241                                 is_enumerable = false;
1242                                 return true;
1243                         }
1244
1245                         InflatedTypeSpec inflated = ret as InflatedTypeSpec;
1246                         if (inflated == null)
1247                                 return false;
1248
1249                         var member_definition = inflated.MemberDefinition;
1250                         PredefinedType ptype = parent.Module.PredefinedTypes.IEnumerableGeneric;
1251
1252                         if (ptype.Define () && ptype.TypeSpec.MemberDefinition == member_definition) {
1253                                 original_iterator_type = inflated.TypeArguments[0];
1254                                 is_enumerable = true;
1255                                 return true;
1256                         }
1257
1258                         ptype = parent.Module.PredefinedTypes.IEnumeratorGeneric;
1259                         if (ptype.Define () && ptype.TypeSpec.MemberDefinition == member_definition) {
1260                                 original_iterator_type = inflated.TypeArguments[0];
1261                                 is_enumerable = false;
1262                                 return true;
1263                         }
1264
1265                         return false;
1266                 }
1267         }
1268 }
1269