**** Merged from MCS ****
[mono.git] / mcs / gmcs / iterators.cs
1 //
2 // iterators.cs: Support for implementing iterators
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) 2003 Ximian, Inc.
8 //
9 // TODO:
10 //    Flow analysis for Yield.
11 //    Emit calls to parent object constructor.
12 //
13 // Generics note:
14 //    Current should be defined to return T, and IEnumerator.Current returns object
15 //
16
17 using System;
18 using System.Collections;
19 using System.Reflection;
20 using System.Reflection.Emit;
21
22 namespace Mono.CSharp {
23
24         public interface IIteratorContainer {
25
26                 //
27                 // Invoked if a yield statement is found in the body
28                 //
29                 void SetYields ();
30         }
31         
32         public class Yield : Statement {
33                 public Expression expr;
34                 ArrayList finally_blocks;
35                 
36                 public Yield (Expression expr, Location l)
37                 {
38                         this.expr = expr;
39                         loc = l;
40                 }
41
42                 public static bool CheckContext (EmitContext ec, Location loc)
43                 {
44                         if (ec.CurrentBranching.InFinally (true)){
45                                 Report.Error (1625, loc, "Cannot yield in the body of a " +
46                                               "finally clause");
47                                 return false;
48                         }
49                         if (ec.CurrentBranching.InCatch ()){
50                                 Report.Error (1631, loc, "Cannot yield in the body of a " +
51                                               "catch clause");
52                                 return false;
53                         }
54                         if (ec.InAnonymousMethod){
55                                 Report.Error (1621, loc, "yield statement can not appear " +
56                                               "inside an anonymoud method");
57                                 return false;
58                         }
59
60                         //
61                         // FIXME: Missing check for Yield inside try block that contains catch clauses
62                         //
63                         return true;
64                 }
65                 
66                 public override bool Resolve (EmitContext ec)
67                 {
68                         expr = expr.Resolve (ec);
69                         if (expr == null)
70                                 return false;
71                         if (!CheckContext (ec, loc))
72                                 return false;
73
74                         Iterator iterator = ec.CurrentIterator;
75                         if (expr.Type != iterator.IteratorType){
76                                 expr = Convert.ImplicitConversionRequired (
77                                         ec, expr, iterator.IteratorType, loc);
78                                 if (expr == null)
79                                         return false;
80                         }
81
82                         ec.CurrentBranching.StealFinallyClauses (ref finally_blocks);
83                         return true;
84                 }
85
86                 protected override void DoEmit (EmitContext ec)
87                 {
88                         ec.CurrentIterator.MarkYield (ec, expr, finally_blocks);
89                 }
90         }
91
92         public class YieldBreak : Statement {
93
94                 public YieldBreak (Location l)
95                 {
96                         loc = l;
97                 }
98
99                 public override bool Resolve (EmitContext ec)
100                 {
101                         if (!Yield.CheckContext (ec, loc))
102                                 return false;
103
104                         ec.CurrentBranching.CurrentUsageVector.Goto ();
105                         return true;
106                 }
107
108                 protected override void DoEmit (EmitContext ec)
109                 {
110                         ec.CurrentIterator.EmitYieldBreak (ec.ig);
111                 }
112         }
113
114         public class Iterator : Class {
115                 string original_name;
116                 Block original_block;
117                 Block block;
118
119                 Type iterator_type;
120                 TypeExpr iterator_type_expr;
121                 bool is_enumerable;
122                 bool is_static;
123
124                 Hashtable fields;
125
126                 //
127                 // The state as we generate the iterator
128                 //
129                 Label move_next_ok, move_next_error;
130                 ArrayList resume_points = new ArrayList ();
131                 int pc;
132                 
133                 //
134                 // Context from the original method
135                 //
136                 TypeContainer container;
137                 Type return_type;
138                 Type [] param_types;
139                 InternalParameters parameters;
140
141                 Expression enumerator_type;
142                 Expression enumerable_type;
143                 Expression generic_enumerator_type;
144                 Expression generic_enumerable_type;
145                 TypeArguments generic_args;
146
147                 protected enum State {
148                         Uninitialized   = -2,
149                         After,
150                         Running
151                 }
152
153                 static int proxy_count;
154
155                 public void EmitYieldBreak (ILGenerator ig)
156                 {
157                         ig.Emit (OpCodes.Ldarg_0);
158                         IntConstant.EmitInt (ig, (int) State.After);
159                         ig.Emit (OpCodes.Br, move_next_error);
160                 }
161
162                 public void EmitMoveNext (EmitContext ec)
163                 {
164                         ILGenerator ig = ec.ig;
165
166                         move_next_ok = ig.DefineLabel ();
167                         move_next_error = ig.DefineLabel ();
168
169                         LocalBuilder retval = ec.GetTemporaryLocal (TypeManager.int32_type);
170
171                         ig.BeginExceptionBlock ();
172
173                         Label dispatcher = ig.DefineLabel ();
174                         ig.Emit (OpCodes.Br, dispatcher);
175
176                         ResumePoint entry_point = new ResumePoint (null);
177                         resume_points.Add (entry_point);
178                         entry_point.Define (ig);
179
180                         ec.EmitTopBlock (original_block, parameters, Location);
181                         EmitYieldBreak (ig);
182
183                         ig.MarkLabel (dispatcher);
184
185                         Label [] labels = new Label [resume_points.Count];
186                         for (int i = 0; i < labels.Length; i++)
187                                 labels [i] = ((ResumePoint) resume_points [i]).Label;
188
189                         ig.Emit (OpCodes.Ldarg_0);
190                         ig.Emit (OpCodes.Ldfld, pc_field.FieldBuilder);
191                         ig.Emit (OpCodes.Switch, labels);
192
193                         Label end = ig.DefineLabel ();
194
195                         ig.MarkLabel (move_next_error);
196                         ig.Emit (OpCodes.Ldc_I4_0); 
197                         ig.Emit (OpCodes.Stloc, retval);
198                         ig.Emit (OpCodes.Leave, end);
199
200                         ig.MarkLabel (move_next_ok);
201                         ig.Emit (OpCodes.Ldc_I4_1);
202                         ig.Emit (OpCodes.Stloc, retval);
203                         ig.Emit (OpCodes.Leave, end);
204
205                         ig.BeginFaultBlock ();
206
207                         ig.Emit (OpCodes.Ldarg_0);
208                         ig.Emit (OpCodes.Callvirt, dispose.MethodBuilder);
209
210                         ig.EndExceptionBlock ();
211
212                         ig.MarkLabel (end);
213                         ig.Emit (OpCodes.Ldloc, retval);
214                         ig.Emit (OpCodes.Ret);
215                 }
216
217                 public void EmitDispose (EmitContext ec)
218                 {
219                         ILGenerator ig = ec.ig;
220
221                         Label end = ig.DefineLabel ();
222                         Label dispatcher = ig.DefineLabel ();
223                         ig.Emit (OpCodes.Br, dispatcher);
224
225                         Label [] labels = new Label [resume_points.Count];
226                         for (int i = 0; i < labels.Length; i++) {
227                                 ResumePoint point = (ResumePoint) resume_points [i];
228
229                                 if (point.FinallyBlocks == null) {
230                                         labels [i] = end;
231                                         continue;
232                                 }
233
234                                 labels [i] = ig.DefineLabel ();
235                                 ig.MarkLabel (labels [i]);
236
237                                 ig.BeginExceptionBlock ();
238                                 ig.BeginFinallyBlock ();
239
240                                 foreach (ExceptionStatement stmt in point.FinallyBlocks) {
241                                         if (stmt != null)
242                                                 stmt.EmitFinally (ec);
243                                 }
244
245                                 ig.EndExceptionBlock ();
246                                 ig.Emit (OpCodes.Br, end);
247                         }
248
249                         ig.MarkLabel (dispatcher);
250                         ig.Emit (OpCodes.Ldarg_0);
251                         ig.Emit (OpCodes.Ldfld, pc_field.FieldBuilder);
252                         ig.Emit (OpCodes.Switch, labels);
253
254                         ig.Emit (OpCodes.Ldarg_0);
255                         IntConstant.EmitInt (ig, (int) State.After);
256                         ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
257
258                         ig.MarkLabel (end);
259                 }
260
261                 protected class ResumePoint
262                 {
263                         public Label Label;
264                         public readonly ExceptionStatement[] FinallyBlocks;
265
266                         public ResumePoint (ArrayList list)
267                         {
268                                 if (list != null) {
269                                         FinallyBlocks = new ExceptionStatement [list.Count];
270                                         list.CopyTo (FinallyBlocks, 0);
271                                 }
272                         }
273
274                         public void Define (ILGenerator ig)
275                         {
276                                 Label = ig.DefineLabel ();
277                                 ig.MarkLabel (Label);
278                         }
279                 }
280
281                 // 
282                 // Invoked when a local variable declaration needs to be mapped to
283                 // a field in our proxy class
284                 //
285                 // Prefixes registered:
286                 //   v_   for EmitContext.MapVariable
287                 //   s_   for Storage
288                 //
289                 public FieldBuilder MapVariable (string pfx, string name, Type t)
290                 {
291                         string full_name = pfx + name;
292                         FieldBuilder fb = (FieldBuilder) fields [full_name];
293                         if (fb != null)
294                                 return fb;
295
296                         fb = TypeBuilder.DefineField (full_name, t, FieldAttributes.Private);
297                         fields.Add (full_name, fb);
298                         return fb;
299                 }
300
301                 //
302                 // Called back from Yield
303                 //
304                 public void MarkYield (EmitContext ec, Expression expr,
305                                        ArrayList finally_blocks)
306                 {
307                         ILGenerator ig = ec.ig;
308
309                         // Store the new current
310                         ig.Emit (OpCodes.Ldarg_0);
311                         expr.Emit (ec);
312                         ig.Emit (OpCodes.Stfld, current_field.FieldBuilder);
313
314                         // increment pc
315                         pc++;
316                         ig.Emit (OpCodes.Ldarg_0);
317                         IntConstant.EmitInt (ig, pc);
318                         ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
319
320                         // Return ok
321                         ig.Emit (OpCodes.Br, move_next_ok);
322
323                         ResumePoint point = new ResumePoint (finally_blocks);
324                         resume_points.Add (point);
325                         point.Define (ig);
326                 }
327
328                 public void MarkFinally (EmitContext ec, ArrayList finally_blocks)
329                 {
330                         ILGenerator ig = ec.ig;
331
332                         // increment pc
333                         pc++;
334                         ig.Emit (OpCodes.Ldarg_0);
335                         IntConstant.EmitInt (ig, pc);
336                         ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
337
338                         ResumePoint point = new ResumePoint (finally_blocks);
339                         resume_points.Add (point);
340                         point.Define (ig);
341                 }
342
343                 private static MemberName MakeProxyName (string name)
344                 {
345                         int pos = name.LastIndexOf ('.');
346                         if (pos > 0)
347                                 name = name.Substring (pos + 1);
348
349                         return new MemberName ("<" + name + ">__" + (proxy_count++));
350                 }
351
352                 //
353                 // Our constructor
354                 //
355                 public Iterator (TypeContainer container, string name, Type return_type,
356                                  Type [] param_types, InternalParameters parameters,
357                                  int modifiers, Block block, Location loc)
358                         : base (container.NamespaceEntry, container, MakeProxyName (name),
359                                 Modifiers.PRIVATE, null, loc)
360                 {
361                         this.container = container;
362                         this.return_type = return_type;
363                         this.param_types = param_types;
364                         this.parameters = parameters;
365                         this.original_name = name;
366                         this.original_block = block;
367                         this.block = new Block (null);
368
369                         fields = new Hashtable ();
370
371                         is_static = (modifiers & Modifiers.STATIC) != 0;
372                 }
373
374                 public bool DefineIterator ()
375                 {
376                         if (!CheckType (return_type)) {
377                                 Report.Error (
378                                         1624, Location,
379                                         "The body of `{0}' cannot be an iterator block " +
380                                         "because '{1}' is not an iterator interface type",
381                                         original_name, TypeManager.CSharpName (return_type));
382                                 return false;
383                         }
384
385                         for (int i = 0; i < parameters.Count; i++){
386                                 Parameter.Modifier mod = parameters.ParameterModifier (i);
387                                 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0){
388                                         Report.Error (
389                                                 1623, Location,
390                                                 "Iterators cannot have ref or out parameters");
391                                         return false;
392                                 }
393                         }
394
395                         generic_args = new TypeArguments (Location);
396                         generic_args.Add (new TypeExpression (iterator_type, Location));
397
398                         ArrayList list = new ArrayList ();
399                         if (is_enumerable) {
400                                 enumerable_type = new TypeExpression (
401                                         TypeManager.ienumerable_type, Location);
402                                 list.Add (enumerable_type);
403
404                                 generic_enumerable_type = new ConstructedType (
405                                         TypeManager.generic_ienumerable_type,
406                                         generic_args, Location);
407                                 list.Add (generic_enumerable_type);
408                         }
409
410                         enumerator_type = new TypeExpression (
411                                 TypeManager.ienumerator_type, Location);
412                         list.Add (enumerator_type);
413
414                         list.Add (new TypeExpression (TypeManager.idisposable_type, Location));
415
416                         generic_enumerator_type = new ConstructedType (
417                                 TypeManager.generic_ienumerator_type,
418                                 generic_args, Location);
419                         list.Add (generic_enumerator_type);
420
421                         iterator_type_expr = new TypeExpression (iterator_type, Location);
422
423                         container.AddIterator (this);
424
425                         Bases = list;
426                         return true;
427                 }
428
429                 //
430                 // Returns the new block for the method, or null on failure
431                 //
432                 protected override bool DefineNestedTypes ()
433                 {
434                         Define_Fields ();
435                         Define_Constructor ();
436                         Define_Current (false);
437                         Define_Current (true);
438                         Define_MoveNext ();
439                         Define_Reset ();
440                         Define_Dispose ();
441
442                         if (is_enumerable) {
443                                 Define_GetEnumerator (false);
444                                 Define_GetEnumerator (true);
445                         }
446
447                         Create_Block ();
448
449                         return base.DefineNestedTypes ();
450                 }
451
452
453                 Field pc_field;
454                 Field current_field;
455                 Method dispose;
456
457                 public Field this_field;
458                 public Field[] parameter_fields;
459
460                 void Create_Block ()
461                 {
462                         int first = is_static ? 0 : 1;
463
464                         ArrayList args = new ArrayList ();
465                         if (!is_static) {
466                                 Type t = container.TypeBuilder;
467                                 args.Add (new Argument (
468                                         new ThisParameterReference (t, 0, Location)));
469                         }
470
471                         args.Add (new Argument (new BoolLiteral (false)));
472
473                         for (int i = 0; i < parameters.Count; i++) {
474                                 Type t = parameters.ParameterType (i);
475                                 args.Add (new Argument (
476                                         new SimpleParameterReference (t, first + i, Location)));
477                         }
478
479                         Expression new_expr = new New (
480                                 new TypeExpression (TypeBuilder, Location), args, Location);
481
482                         block.AddStatement (new NoCheckReturn (new_expr, Location));
483                 }
484
485                 void Define_Fields ()
486                 {
487                         Location loc = Location.Null;
488
489                         pc_field = new Field (
490                                 this, TypeManager.system_int32_expr, Modifiers.PRIVATE, "PC",
491                                 null, null, loc);
492                         AddField (pc_field);
493
494                         current_field = new Field (
495                                 this, iterator_type_expr, Modifiers.PRIVATE, "current",
496                                 null, null, loc);
497                         AddField (current_field);
498
499                         if (!is_static) {
500                                 this_field = new Field (
501                                         this,
502                                         new TypeExpression (container.TypeBuilder, Location),
503                                         Modifiers.PRIVATE, "this", null, null, loc);
504                                 AddField (this_field);
505                         }
506
507                         parameter_fields = new Field [parameters.Count];
508                         for (int i = 0; i < parameters.Count; i++) {
509                                 string fname = String.Format (
510                                         "field{0}_{1}", i, parameters.ParameterName (i));
511
512                                 parameter_fields [i] = new Field (
513                                         this,
514                                         new TypeExpression (parameters.ParameterType (i), loc),
515                                         Modifiers.PRIVATE, fname, null, null, loc);
516                                 AddField (parameter_fields [i]);
517                         }
518                 }
519
520                 void Define_Constructor ()
521                 {
522                         Parameters ctor_params;
523
524                         ArrayList list = new ArrayList ();
525
526                         if (!is_static)
527                                 list.Add (new Parameter (
528                                         new TypeExpression (container.TypeBuilder, Location),
529                                         "this", Parameter.Modifier.NONE, null));
530                         list.Add (new Parameter (
531                                 TypeManager.system_boolean_expr, "initialized",
532                                 Parameter.Modifier.NONE, null));
533
534                         Parameter[] old_fixed = parameters.Parameters.FixedParameters;
535                         if (old_fixed != null)
536                                 list.AddRange (old_fixed);
537
538                         Parameter[] fixed_params = new Parameter [list.Count];
539                         list.CopyTo (fixed_params);
540
541                         ctor_params = new Parameters (
542                                 fixed_params, parameters.Parameters.ArrayParameter,
543                                 Location);
544
545                         Constructor ctor = new Constructor (
546                                 this, Name, Modifiers.PUBLIC, ctor_params,
547                                 new ConstructorBaseInitializer (
548                                         null, Parameters.EmptyReadOnlyParameters, Location),
549                                 Location);
550                         AddConstructor (ctor);
551
552                         Block block = ctor.Block = new Block (null);
553
554                         if (!is_static) {
555                                 Type t = container.TypeBuilder;
556
557                                 Assign assign = new Assign (
558                                         new FieldExpression (this_field),
559                                         new SimpleParameterReference (t, 1, Location),
560                                         Location);
561
562                                 block.AddStatement (new StatementExpression (assign, Location));
563                         }
564
565                         int first = is_static ? 2 : 3;
566
567                         for (int i = 0; i < parameters.Count; i++) {
568                                 Type t = parameters.ParameterType (i);
569
570                                 Assign assign = new Assign (
571                                         new FieldExpression (parameter_fields [i]),
572                                         new SimpleParameterReference (t, first + i, Location),
573                                         Location);
574
575                                 block.AddStatement (new StatementExpression (assign, Location));
576                         }
577
578                         State initial = is_enumerable ? State.Uninitialized : State.Running;
579                         block.AddStatement (new SetState (this, initial, Location));
580
581                         block.AddStatement (new If (
582                                 new SimpleParameterReference (
583                                         TypeManager.bool_type, first - 1, Location),
584                                 new SetState (this, State.Running, Location),
585                                 Location));
586                 }
587
588                 Statement Create_ThrowInvalidOperation ()
589                 {
590                         TypeExpr ex_type = new TypeExpression (
591                                 TypeManager.invalid_operation_exception_type, Location);
592
593                         return new Throw (new New (ex_type, null, Location), Location);
594                 }
595
596                 Statement Create_ThrowNotSupported ()
597                 {
598                         TypeExpr ex_type = new TypeExpression (
599                                 TypeManager.not_supported_exception_type, Location);
600
601                         return new Throw (new New (ex_type, null, Location), Location);
602                 }
603
604                 void Define_Current (bool is_generic)
605                 {
606                         MemberName left;
607                         Expression type;
608                         if (is_generic) {
609                                 left = new MemberName (
610                                         "System.Collections.Generic.IEnumerator",
611                                         generic_args);
612                                 type = iterator_type_expr;
613                         } else {
614                                 left = new MemberName ("System.Collections.IEnumerator");
615                                 type = TypeManager.system_object_expr;
616                         }
617
618                         MemberName name = new MemberName (left, "Current", null);
619
620                         Block get_block = new Block (null);
621
622                         get_block.AddStatement (new If (
623                                 new Binary (
624                                         Binary.Operator.LessThanOrEqual,
625                                         new FieldExpression (pc_field),
626                                         new IntLiteral ((int) State.Running), Location),
627                                 Create_ThrowInvalidOperation (),
628                                 new Return (
629                                         new FieldExpression (current_field), Location),
630                                 Location));
631
632                         Accessor getter = new Accessor (get_block, null);
633
634                         Property current = new Property (
635                                 this, type, 0, false, name, null, getter, null, Location);
636                         AddProperty (current);
637                 }
638
639                 void Define_MoveNext ()
640                 {
641                         Method move_next = new Method (
642                                 this, null, TypeManager.system_boolean_expr,
643                                 Modifiers.PUBLIC, false, new MemberName ("MoveNext"),
644                                 Parameters.EmptyReadOnlyParameters, null,
645                                 Location.Null);
646                         AddMethod (move_next);
647
648                         Block block = move_next.Block = new Block (null);
649
650                         MoveNextMethod inline = new MoveNextMethod (this, Location);
651                         block.AddStatement (inline);
652                 }
653
654                 void Define_GetEnumerator (bool is_generic)
655                 {
656                         MemberName left;
657                         Expression type;
658                         if (is_generic) {
659                                 left = new MemberName (
660                                         "System.Collections.Generic.IEnumerable",
661                                         generic_args);
662                                 type = generic_enumerator_type;
663                         } else {
664                                 left = new MemberName ("System.Collections.IEnumerable");
665                                 type = enumerator_type;
666                         }
667
668                         MemberName name = new MemberName (left, "GetEnumerator", null);
669
670                         Method get_enumerator = new Method (
671                                 this, null, type, 0, false, name,
672                                 Parameters.EmptyReadOnlyParameters, null,
673                                 Location.Null);
674                         AddMethod (get_enumerator);
675
676                         get_enumerator.Block = new Block (null);
677
678                         Expression ce = new MemberAccess (
679                                 new SimpleName ("System.Threading.Interlocked", Location),
680                                 "CompareExchange", Location);
681
682                         Expression pc = new FieldExpression (pc_field);
683                         Expression before = new IntLiteral ((int) State.Running);
684                         Expression uninitialized = new IntLiteral ((int) State.Uninitialized);
685
686                         ArrayList args = new ArrayList ();
687                         args.Add (new Argument (pc, Argument.AType.Ref));
688                         args.Add (new Argument (before, Argument.AType.Expression));
689                         args.Add (new Argument (uninitialized, Argument.AType.Expression));
690
691                         get_enumerator.Block.AddStatement (new If (
692                                 new Binary (
693                                         Binary.Operator.Equality,
694                                         new Invocation (ce, args, Location),
695                                         uninitialized, Location),
696                                 new Return (new This (block, Location), Location),
697                                 Location));
698
699                         args = new ArrayList ();
700                         if (!is_static)
701                                 args.Add (new Argument (new FieldExpression (this_field)));
702
703                         args.Add (new Argument (new BoolLiteral (true)));
704
705                         for (int i = 0; i < parameters.Count; i++)
706                                 args.Add (new Argument (
707                                                   new FieldExpression (parameter_fields [i])));
708
709                         Expression new_expr = new New (
710                                 new TypeExpression (TypeBuilder, Location), args, Location);
711                         get_enumerator.Block.AddStatement (new Return (new_expr, Location));
712                 }
713
714                 protected class SimpleParameterReference : Expression
715                 {
716                         int idx;
717
718                         public SimpleParameterReference (Type type, int idx, Location loc)
719                         {
720                                 this.idx = idx;
721                                 this.loc = loc;
722                                 this.type = type;
723                                 eclass = ExprClass.Variable;
724                         }
725
726                         public override Expression DoResolve (EmitContext ec)
727                         {
728                                 return this;
729                         }
730
731                         public override void Emit (EmitContext ec)
732                         {
733                                 DoEmit (ec);
734                         }
735
736                         protected virtual void DoEmit (EmitContext ec)
737                         {
738                                 ParameterReference.EmitLdArg (ec.ig, idx);
739                         }
740                 }
741
742                 protected class ThisParameterReference : SimpleParameterReference
743                 {
744                         public ThisParameterReference (Type type, int idx, Location loc)
745                                 : base (type, idx, loc)
746                         { }
747
748                         protected override void DoEmit (EmitContext ec)
749                         {
750                                 base.DoEmit (ec);
751                                 if (ec.TypeContainer is Struct)
752                                         ec.ig.Emit (OpCodes.Ldobj, type);
753                         }
754                 }
755
756                 protected class FieldExpression : Expression
757                 {
758                         Field field;
759
760                         public FieldExpression (Field field)
761                         {
762                                 this.field = field;
763                         }
764
765                         public override Expression DoResolve (EmitContext ec)
766                         {
767                                 FieldExpr fexpr = new FieldExpr (field.FieldBuilder, loc);
768                                 fexpr.InstanceExpression = ec.GetThis (loc);
769                                 return fexpr.Resolve (ec);
770                         }
771
772                         public override void Emit (EmitContext ec)
773                         {
774                                 throw new InvalidOperationException ();
775                         }
776                 }
777
778                 protected class MoveNextMethod : Statement {
779                         Iterator iterator;
780
781                         public MoveNextMethod (Iterator iterator, Location loc)
782                         {
783                                 this.loc = loc;
784                                 this.iterator = iterator;
785                         }
786
787                         public override bool Resolve (EmitContext ec)
788                         {
789                                 ec.CurrentBranching.CurrentUsageVector.Return ();
790                                 return true;
791                         }
792
793                         protected override void DoEmit (EmitContext ec)
794                         {
795                                 int code_flags = Modifiers.METHOD_YIELDS;
796                                 if (iterator.is_static)
797                                         code_flags |= Modifiers.STATIC;
798
799                                 EmitContext new_ec = new EmitContext (
800                                         iterator.container, loc, ec.ig,
801                                         TypeManager.int32_type, code_flags);
802
803                                 new_ec.CurrentIterator = iterator;
804
805                                 iterator.EmitMoveNext (new_ec);
806                         }
807                 }
808
809                 protected class DisposeMethod : Statement {
810                         Iterator iterator;
811
812                         public DisposeMethod (Iterator iterator, Location loc)
813                         {
814                                 this.loc = loc;
815                                 this.iterator = iterator;
816                         }
817
818                         public override bool Resolve (EmitContext ec)
819                         {
820                                 return true;
821                         }
822
823                         protected override void DoEmit (EmitContext ec)
824                         {
825                                 iterator.EmitDispose (ec);
826                         }
827                 }
828
829                 protected class StatementList : Statement {
830                         ArrayList statements;
831
832                         public StatementList (Location loc)
833                         {
834                                 this.loc = loc;
835                                 statements = new ArrayList ();
836                         }
837
838                         public void Add (Statement statement)
839                         {
840                                 statements.Add (statement);
841                         }
842
843                         public override bool Resolve (EmitContext ec)
844                         {
845                                 foreach (Statement stmt in statements) {
846                                         if (!stmt.Resolve (ec))
847                                                 return false;
848                                 }
849
850                                 return true;
851                         }
852
853                         protected override void DoEmit (EmitContext ec)
854                         {
855                                 foreach (Statement stmt in statements)
856                                         stmt.Emit (ec);
857                         }
858                 }
859
860                 protected class SetState : Statement
861                 {
862                         Iterator iterator;
863                         State state;
864
865                         public SetState (Iterator iterator, State state, Location loc)
866                         {
867                                 this.iterator = iterator;
868                                 this.state = state;
869                                 this.loc = loc;
870                         }
871
872                         public override bool Resolve (EmitContext ec)
873                         {
874                                 return true;
875                         }
876
877                         protected override void DoEmit (EmitContext ec)
878                         {
879                                 ec.ig.Emit (OpCodes.Ldarg_0);
880                                 IntConstant.EmitInt (ec.ig, (int) state);
881                                 ec.ig.Emit (OpCodes.Stfld, iterator.pc_field.FieldBuilder);
882                         }
883                 }
884
885                 void Define_Reset ()
886                 {
887                         Method reset = new Method (
888                                 this, null, TypeManager.system_void_expr, Modifiers.PUBLIC,
889                                 false, new MemberName ("Reset"),
890                                 Parameters.EmptyReadOnlyParameters, null, Location);
891                         AddMethod (reset);
892
893                         reset.Block = new Block (null);
894                         reset.Block.AddStatement (Create_ThrowNotSupported ());
895                 }
896
897                 void Define_Dispose ()
898                 {
899                         dispose = new Method (
900                                 this, null, TypeManager.system_void_expr, Modifiers.PUBLIC,
901                                 false, new MemberName ("Dispose"),
902                                 Parameters.EmptyReadOnlyParameters, null, Location);
903                         AddMethod (dispose);
904
905                         dispose.Block = new Block (null);
906                         dispose.Block.AddStatement (new DisposeMethod (this, Location));
907                 }
908
909                 public Block Block {
910                         get { return block; }
911                 }
912
913                 public Type IteratorType {
914                         get { return iterator_type; }
915                 }
916
917                 //
918                 // This return statement tricks return into not flagging an error for being
919                 // used in a Yields method
920                 //
921                 class NoCheckReturn : Return {
922                         public NoCheckReturn (Expression expr, Location loc) : base (expr, loc)
923                         {
924                         }
925
926                         public override bool Resolve (EmitContext ec)
927                         {
928                                 ec.InIterator = false;
929                                 bool ret_val = base.Resolve (ec);
930                                 ec.InIterator = true;
931
932                                 return ret_val;
933                         }
934                 }
935
936                 bool CheckType (Type t)
937                 {
938                         if (t == TypeManager.ienumerable_type) {
939                                 iterator_type = TypeManager.object_type;
940                                 is_enumerable = true;
941                                 return true;
942                         } else if (t == TypeManager.ienumerator_type) {
943                                 iterator_type = TypeManager.object_type;
944                                 is_enumerable = false;
945                                 return true;
946                         }
947
948                         if (!t.IsGenericInstance)
949                                 return false;
950
951                         Type[] args = TypeManager.GetTypeArguments (t);
952                         if (args.Length != 1)
953                                 return false;
954
955                         Type gt = t.GetGenericTypeDefinition ();
956                         if (gt == TypeManager.generic_ienumerable_type) {
957                                 iterator_type = args [0];
958                                 is_enumerable = true;
959                                 return true;
960                         } else if (gt == TypeManager.generic_ienumerator_type) {
961                                 iterator_type = args [0];
962                                 is_enumerable = false;
963                                 return true;
964                         }
965
966                         return false;
967                 }
968         }
969 }
970