2 // iterators.cs: Support for implementing iterators
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2003 Ximian, Inc.
10 // Flow analysis for Yield.
11 // Emit calls to base object constructor.
14 // Current should be defined to return T, and IEnumerator.Current returns object
18 using System.Collections;
19 using System.Reflection;
20 using System.Reflection.Emit;
22 namespace Mono.CSharp {
24 public interface IIteratorContainer {
27 // Invoked if a yield statement is found in the body
32 public class Yield : Statement {
33 public Expression expr;
34 ArrayList finally_blocks;
36 public Yield (Expression expr, Location l)
42 public static bool CheckContext (EmitContext ec, Location loc)
44 if (ec.CurrentBranching.InFinally (true)){
45 Report.Error (1625, loc, "Cannot yield in the body of a " +
50 Report.Error (1629, loc, "Unsafe code may not appear in iterators");
53 if (ec.CurrentBranching.InCatch ()){
54 Report.Error (1631, loc, "Cannot yield in the body of a " +
58 if (ec.CurrentAnonymousMethod != null){
59 Report.Error (1621, loc, "yield statement can not appear inside an anonymoud method");
64 // FIXME: Missing check for Yield inside try block that contains catch clauses
69 public override bool Resolve (EmitContext ec)
71 expr = expr.Resolve (ec);
74 if (!CheckContext (ec, loc))
77 Iterator iterator = ec.CurrentIterator;
78 if (expr.Type != iterator.IteratorType){
79 expr = Convert.ImplicitConversionRequired (
80 ec, expr, iterator.IteratorType, loc);
85 ec.CurrentBranching.StealFinallyClauses (ref finally_blocks);
89 protected override void DoEmit (EmitContext ec)
91 ec.CurrentIterator.MarkYield (ec, expr, finally_blocks);
95 public class YieldBreak : Statement {
97 public YieldBreak (Location l)
102 public override bool Resolve (EmitContext ec)
104 if (!Yield.CheckContext (ec, loc))
107 ec.CurrentBranching.CurrentUsageVector.Goto ();
111 protected override void DoEmit (EmitContext ec)
113 ec.CurrentIterator.EmitYieldBreak (ec.ig);
117 public class Iterator : Class {
118 ToplevelBlock original_block;
120 string original_name;
123 TypeExpr iterator_type_expr;
130 // The state as we generate the iterator
132 Label move_next_ok, move_next_error;
133 ArrayList resume_points = new ArrayList ();
137 // Context from the original method
139 TypeContainer container;
142 InternalParameters parameters;
144 protected enum State {
150 static int proxy_count;
152 public void EmitYieldBreak (ILGenerator ig)
154 ig.Emit (OpCodes.Ldarg_0);
155 IntConstant.EmitInt (ig, (int) State.After);
156 ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
157 ig.Emit (OpCodes.Br, move_next_error);
160 public void EmitMoveNext (EmitContext ec)
162 ILGenerator ig = ec.ig;
164 move_next_ok = ig.DefineLabel ();
165 move_next_error = ig.DefineLabel ();
167 LocalBuilder retval = ec.GetTemporaryLocal (TypeManager.int32_type);
169 ig.BeginExceptionBlock ();
171 Label dispatcher = ig.DefineLabel ();
172 ig.Emit (OpCodes.Br, dispatcher);
174 ResumePoint entry_point = new ResumePoint (null);
175 resume_points.Add (entry_point);
176 entry_point.Define (ig);
178 ec.EmitTopBlock (original_block, parameters, Location);
181 ig.MarkLabel (dispatcher);
183 Label [] labels = new Label [resume_points.Count];
184 for (int i = 0; i < labels.Length; i++)
185 labels [i] = ((ResumePoint) resume_points [i]).Label;
187 ig.Emit (OpCodes.Ldarg_0);
188 ig.Emit (OpCodes.Ldfld, pc_field.FieldBuilder);
189 ig.Emit (OpCodes.Switch, labels);
191 Label end = ig.DefineLabel ();
193 ig.MarkLabel (move_next_error);
194 ig.Emit (OpCodes.Ldc_I4_0);
195 ig.Emit (OpCodes.Stloc, retval);
196 ig.Emit (OpCodes.Leave, end);
198 ig.MarkLabel (move_next_ok);
199 ig.Emit (OpCodes.Ldc_I4_1);
200 ig.Emit (OpCodes.Stloc, retval);
201 ig.Emit (OpCodes.Leave, end);
203 ig.BeginFaultBlock ();
205 ig.Emit (OpCodes.Ldarg_0);
206 ig.Emit (OpCodes.Callvirt, dispose.MethodBuilder);
208 ig.EndExceptionBlock ();
211 ig.Emit (OpCodes.Ldloc, retval);
212 ig.Emit (OpCodes.Ret);
215 public void EmitDispose (EmitContext ec)
217 ILGenerator ig = ec.ig;
219 Label end = ig.DefineLabel ();
220 Label dispatcher = ig.DefineLabel ();
221 ig.Emit (OpCodes.Br, dispatcher);
223 ec.RemapToProxy = true;
224 Label [] labels = new Label [resume_points.Count];
225 for (int i = 0; i < labels.Length; i++) {
226 ResumePoint point = (ResumePoint) resume_points [i];
228 if (point.FinallyBlocks == null) {
233 labels [i] = ig.DefineLabel ();
234 ig.MarkLabel (labels [i]);
236 ig.BeginExceptionBlock ();
237 ig.BeginFinallyBlock ();
239 foreach (ExceptionStatement stmt in point.FinallyBlocks) {
241 stmt.EmitFinally (ec);
244 ig.EndExceptionBlock ();
245 ig.Emit (OpCodes.Br, end);
247 ec.RemapToProxy = false;
249 ig.MarkLabel (dispatcher);
250 ig.Emit (OpCodes.Ldarg_0);
251 ig.Emit (OpCodes.Ldfld, pc_field.FieldBuilder);
252 ig.Emit (OpCodes.Switch, labels);
254 ig.Emit (OpCodes.Ldarg_0);
255 IntConstant.EmitInt (ig, (int) State.After);
256 ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
261 protected class ResumePoint
264 public readonly ExceptionStatement[] FinallyBlocks;
266 public ResumePoint (ArrayList list)
269 FinallyBlocks = new ExceptionStatement [list.Count];
270 list.CopyTo (FinallyBlocks, 0);
274 public void Define (ILGenerator ig)
276 Label = ig.DefineLabel ();
277 ig.MarkLabel (Label);
282 // Invoked when a local variable declaration needs to be mapped to
283 // a field in our proxy class
285 // Prefixes registered:
286 // v_ for EmitContext.MapVariable
289 public FieldBuilder MapVariable (string pfx, string name, Type t)
291 string full_name = pfx + name;
292 FieldBuilder fb = (FieldBuilder) fields [full_name];
296 fb = TypeBuilder.DefineField (full_name, t, FieldAttributes.Private);
297 fields.Add (full_name, fb);
302 // Called back from Yield
304 public void MarkYield (EmitContext ec, Expression expr,
305 ArrayList finally_blocks)
307 ILGenerator ig = ec.ig;
309 // Store the new current
310 ig.Emit (OpCodes.Ldarg_0);
312 ig.Emit (OpCodes.Stfld, current_field.FieldBuilder);
316 ig.Emit (OpCodes.Ldarg_0);
317 IntConstant.EmitInt (ig, pc);
318 ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
321 ig.Emit (OpCodes.Br, move_next_ok);
323 ResumePoint point = new ResumePoint (finally_blocks);
324 resume_points.Add (point);
328 public void MarkFinally (EmitContext ec, ArrayList finally_blocks)
330 ILGenerator ig = ec.ig;
334 ig.Emit (OpCodes.Ldarg_0);
335 IntConstant.EmitInt (ig, pc);
336 ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
338 ResumePoint point = new ResumePoint (finally_blocks);
339 resume_points.Add (point);
343 private static MemberName MakeProxyName (string name)
345 int pos = name.LastIndexOf ('.');
347 name = name.Substring (pos + 1);
349 return new MemberName ("<" + name + ">__" + (proxy_count++));
355 public Iterator (TypeContainer container, string name, Type return_type,
356 Type [] param_types, InternalParameters parameters,
357 int modifiers, ToplevelBlock block, Location loc)
358 : base (container.NamespaceEntry, container, MakeProxyName (name),
359 (modifiers & Modifiers.UNSAFE) | Modifiers.PRIVATE, null, loc)
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 ToplevelBlock (loc);
369 fields = new Hashtable ();
371 is_static = (modifiers & Modifiers.STATIC) != 0;
374 public bool DefineIterator ()
376 ec = new EmitContext (this, Mono.CSharp.Location.Null, null, null, ModFlags);
378 if (!CheckType (return_type)) {
381 "The body of `{0}' cannot be an iterator block " +
382 "because '{1}' is not an iterator interface type",
383 original_name, TypeManager.CSharpName (return_type));
387 for (int i = 0; i < parameters.Count; i++){
388 Parameter.Modifier mod = parameters.ParameterModifier (i);
389 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0){
392 "Iterators cannot have ref or out parameters");
397 ArrayList list = new ArrayList ();
399 list.Add (new TypeExpression (
400 TypeManager.ienumerable_type, Location));
401 list.Add (new TypeExpression (TypeManager.ienumerator_type, Location));
402 list.Add (new TypeExpression (TypeManager.idisposable_type, Location));
404 iterator_type_expr = new TypeExpression (iterator_type, Location);
406 container.AddIterator (this);
413 // Returns the new block for the method, or null on failure
415 protected override bool DefineNestedTypes ()
418 Define_Constructor ();
425 Define_GetEnumerator ();
429 return base.DefineNestedTypes ();
437 public Field this_field;
438 public Field[] parameter_fields;
442 int first = is_static ? 0 : 1;
444 ArrayList args = new ArrayList ();
446 Type t = container.TypeBuilder;
447 args.Add (new Argument (
448 new ThisParameterReference (t, 0, Location)));
451 args.Add (new Argument (new BoolLiteral (false)));
453 for (int i = 0; i < parameters.Count; i++) {
454 Type t = parameters.ParameterType (i);
455 args.Add (new Argument (
456 new SimpleParameterReference (t, first + i, Location)));
459 Expression new_expr = new New (
460 new TypeExpression (TypeBuilder, Location), args, Location);
462 block.AddStatement (new NoCheckReturn (new_expr, Location));
465 void Define_Fields ()
467 Location loc = Location.Null;
469 pc_field = new Field (
470 this, TypeManager.system_int32_expr, Modifiers.PRIVATE, "PC",
474 current_field = new Field (
475 this, iterator_type_expr, Modifiers.PRIVATE, "current",
477 AddField (current_field);
480 this_field = new Field (
482 new TypeExpression (container.TypeBuilder, Location),
483 Modifiers.PRIVATE, "this", null, null, loc);
484 AddField (this_field);
487 parameter_fields = new Field [parameters.Count];
488 for (int i = 0; i < parameters.Count; i++) {
489 string fname = String.Format (
490 "field{0}_{1}", i, parameters.ParameterName (i));
492 parameter_fields [i] = new Field (
494 new TypeExpression (parameters.ParameterType (i), loc),
495 Modifiers.PRIVATE, fname, null, null, loc);
496 AddField (parameter_fields [i]);
500 void Define_Constructor ()
502 Parameters ctor_params;
504 ArrayList list = new ArrayList ();
507 list.Add (new Parameter (
508 new TypeExpression (container.TypeBuilder, Location),
509 "this", Parameter.Modifier.NONE, null));
510 list.Add (new Parameter (
511 TypeManager.system_boolean_expr, "initialized",
512 Parameter.Modifier.NONE, null));
514 Parameter[] old_fixed = parameters.Parameters.FixedParameters;
515 if (old_fixed != null)
516 list.AddRange (old_fixed);
518 Parameter[] fixed_params = new Parameter [list.Count];
519 list.CopyTo (fixed_params);
521 ctor_params = new Parameters (
522 fixed_params, parameters.Parameters.ArrayParameter,
525 Constructor ctor = new Constructor (
526 this, Name, Modifiers.PUBLIC, ctor_params,
527 new ConstructorBaseInitializer (
528 null, Parameters.EmptyReadOnlyParameters, Location),
530 AddConstructor (ctor);
532 ToplevelBlock block = ctor.Block = new ToplevelBlock (Location);
535 Type t = container.TypeBuilder;
537 Assign assign = new Assign (
538 new FieldExpression (this_field),
539 new SimpleParameterReference (t, 1, Location),
542 block.AddStatement (new StatementExpression (assign, Location));
545 int first = is_static ? 2 : 3;
547 for (int i = 0; i < parameters.Count; i++) {
548 Type t = parameters.ParameterType (i);
550 Assign assign = new Assign (
551 new FieldExpression (parameter_fields [i]),
552 new SimpleParameterReference (t, first + i, Location),
555 block.AddStatement (new StatementExpression (assign, Location));
558 State initial = is_enumerable ? State.Uninitialized : State.Running;
559 block.AddStatement (new SetState (this, initial, Location));
561 block.AddStatement (new If (
562 new SimpleParameterReference (
563 TypeManager.bool_type, first - 1, Location),
564 new SetState (this, State.Running, Location),
568 Statement Create_ThrowInvalidOperation ()
570 TypeExpr ex_type = new TypeExpression (
571 TypeManager.invalid_operation_exception_type, Location);
573 return new Throw (new New (ex_type, null, Location), Location);
576 Statement Create_ThrowNotSupported ()
578 TypeExpr ex_type = new TypeExpression (
579 TypeManager.not_supported_exception_type, Location);
581 return new Throw (new New (ex_type, null, Location), Location);
584 void Define_Current ()
586 ToplevelBlock get_block = new ToplevelBlock (Location);
587 MemberName left = new MemberName ("System.Collections.IEnumerator");
588 MemberName name = new MemberName (left, "Current");
590 get_block.AddStatement (new If (
592 Binary.Operator.LessThanOrEqual,
593 new FieldExpression (pc_field),
594 new IntLiteral ((int) State.Running), Location),
595 Create_ThrowInvalidOperation (),
597 new FieldExpression (current_field), Location),
600 Accessor getter = new Accessor (get_block, 0, null, Location);
602 Property current = new Property (
603 this, iterator_type_expr, 0,
604 false, name, null, getter, null, Location);
605 AddProperty (current);
608 void Define_MoveNext ()
610 Method move_next = new Method (
611 this, TypeManager.system_boolean_expr,
612 Modifiers.PUBLIC, false, new MemberName ("MoveNext"),
613 Parameters.EmptyReadOnlyParameters, null,
615 AddMethod (move_next);
617 ToplevelBlock block = move_next.Block = new ToplevelBlock (Location);
619 MoveNextMethod inline = new MoveNextMethod (this, Location);
620 block.AddStatement (inline);
623 void Define_GetEnumerator ()
625 MemberName left = new MemberName ("System.Collections.IEnumerable");
626 MemberName name = new MemberName (left, "GetEnumerator");
628 Method get_enumerator = new Method (
630 new TypeExpression (TypeManager.ienumerator_type, Location),
632 Parameters.EmptyReadOnlyParameters, null,
634 AddMethod (get_enumerator);
636 get_enumerator.Block = new ToplevelBlock (Location);
638 Expression ce = new MemberAccess (
639 new SimpleName ("System.Threading.Interlocked", Location),
640 "CompareExchange", Location);
642 Expression pc = new FieldExpression (pc_field);
643 Expression before = new IntLiteral ((int) State.Running);
644 Expression uninitialized = new IntLiteral ((int) State.Uninitialized);
646 ArrayList args = new ArrayList ();
647 args.Add (new Argument (pc, Argument.AType.Ref));
648 args.Add (new Argument (before, Argument.AType.Expression));
649 args.Add (new Argument (uninitialized, Argument.AType.Expression));
651 get_enumerator.Block.AddStatement (new If (
653 Binary.Operator.Equality,
654 new Invocation (ce, args, Location),
655 uninitialized, Location),
656 new Return (new This (block, Location), Location),
659 args = new ArrayList ();
661 args.Add (new Argument (new FieldExpression (this_field)));
663 args.Add (new Argument (new BoolLiteral (true)));
665 for (int i = 0; i < parameters.Count; i++)
666 args.Add (new Argument (
667 new FieldExpression (parameter_fields [i])));
669 Expression new_expr = new New (
670 new TypeExpression (TypeBuilder, Location), args, Location);
671 get_enumerator.Block.AddStatement (new Return (new_expr, Location));
674 protected class SimpleParameterReference : Expression
678 public SimpleParameterReference (Type type, int idx, Location loc)
683 eclass = ExprClass.Variable;
686 public override Expression DoResolve (EmitContext ec)
691 public override void Emit (EmitContext ec)
696 protected virtual void DoEmit (EmitContext ec)
698 ParameterReference.EmitLdArg (ec.ig, idx);
702 protected class ThisParameterReference : SimpleParameterReference
704 public ThisParameterReference (Type type, int idx, Location loc)
705 : base (type, idx, loc)
708 protected override void DoEmit (EmitContext ec)
711 if (ec.TypeContainer is Struct)
712 ec.ig.Emit (OpCodes.Ldobj, type);
716 protected class FieldExpression : Expression
720 public FieldExpression (Field field)
725 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
727 return DoResolve (ec);
730 public override Expression DoResolve (EmitContext ec)
732 FieldExpr fexpr = new FieldExpr (field.FieldBuilder, loc);
733 fexpr.InstanceExpression = ec.GetThis (loc);
734 return fexpr.Resolve (ec);
737 public override void Emit (EmitContext ec)
739 throw new InvalidOperationException ();
743 protected class MoveNextMethod : Statement {
746 public MoveNextMethod (Iterator iterator, Location loc)
749 this.iterator = iterator;
752 public override bool Resolve (EmitContext ec)
754 ec.CurrentBranching.CurrentUsageVector.Return ();
758 protected override void DoEmit (EmitContext ec)
760 int code_flags = Modifiers.METHOD_YIELDS;
761 if (iterator.is_static)
762 code_flags |= Modifiers.STATIC;
764 code_flags |= iterator.ModFlags & Modifiers.UNSAFE;
766 EmitContext new_ec = new EmitContext (
767 iterator.container, loc, ec.ig,
768 TypeManager.int32_type, code_flags);
770 new_ec.CurrentIterator = iterator;
772 iterator.EmitMoveNext (new_ec);
776 protected class DisposeMethod : Statement {
779 public DisposeMethod (Iterator iterator, Location loc)
782 this.iterator = iterator;
785 public override bool Resolve (EmitContext ec)
790 protected override void DoEmit (EmitContext ec)
792 iterator.EmitDispose (ec);
796 protected class StatementList : Statement {
797 ArrayList statements;
799 public StatementList (Location loc)
802 statements = new ArrayList ();
805 public void Add (Statement statement)
807 statements.Add (statement);
810 public override bool Resolve (EmitContext ec)
812 foreach (Statement stmt in statements) {
813 if (!stmt.Resolve (ec))
820 protected override void DoEmit (EmitContext ec)
822 foreach (Statement stmt in statements)
827 protected class SetState : Statement
832 public SetState (Iterator iterator, State state, Location loc)
834 this.iterator = iterator;
839 public override bool Resolve (EmitContext ec)
844 protected override void DoEmit (EmitContext ec)
846 ec.ig.Emit (OpCodes.Ldarg_0);
847 IntConstant.EmitInt (ec.ig, (int) state);
848 ec.ig.Emit (OpCodes.Stfld, iterator.pc_field.FieldBuilder);
854 Method reset = new Method (
855 this, TypeManager.system_void_expr, Modifiers.PUBLIC,
856 false, new MemberName ("Reset"),
857 Parameters.EmptyReadOnlyParameters, null, Location);
860 reset.Block = new ToplevelBlock (Location);
861 reset.Block.AddStatement (Create_ThrowNotSupported ());
864 void Define_Dispose ()
866 dispose = new Method (
867 this, TypeManager.system_void_expr, Modifiers.PUBLIC,
868 false, new MemberName ("Dispose"),
869 Parameters.EmptyReadOnlyParameters, null, Location);
872 dispose.Block = new ToplevelBlock (Location);
873 dispose.Block.AddStatement (new DisposeMethod (this, Location));
876 public ToplevelBlock Block {
877 get { return block; }
880 public Type IteratorType {
881 get { return iterator_type; }
885 // This return statement tricks return into not flagging an error for being
886 // used in a Yields method
888 class NoCheckReturn : Return {
889 public NoCheckReturn (Expression expr, Location loc) : base (expr, loc)
893 public override bool Resolve (EmitContext ec)
895 ec.InIterator = false;
896 bool ret_val = base.Resolve (ec);
897 ec.InIterator = true;
903 bool CheckType (Type t)
905 if (t == TypeManager.ienumerable_type) {
906 iterator_type = TypeManager.object_type;
907 is_enumerable = true;
909 } else if (t == TypeManager.ienumerator_type) {
910 iterator_type = TypeManager.object_type;
911 is_enumerable = false;