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 " +
49 if (ec.CurrentBranching.InCatch ()){
50 Report.Error (1631, loc, "Cannot yield in the body of a " +
54 if (ec.CurrentAnonymousMethod != null){
55 Report.Error (1621, loc, "yield statement can not appear inside an anonymoud method");
60 // FIXME: Missing check for Yield inside try block that contains catch clauses
65 public override bool Resolve (EmitContext ec)
67 expr = expr.Resolve (ec);
70 if (!CheckContext (ec, loc))
73 Iterator iterator = ec.CurrentIterator;
74 if (expr.Type != iterator.IteratorType){
75 expr = Convert.ImplicitConversionRequired (
76 ec, expr, iterator.IteratorType, loc);
81 ec.CurrentBranching.StealFinallyClauses (ref finally_blocks);
85 protected override void DoEmit (EmitContext ec)
87 ec.CurrentIterator.MarkYield (ec, expr, finally_blocks);
91 public class YieldBreak : Statement {
93 public YieldBreak (Location l)
98 public override bool Resolve (EmitContext ec)
100 if (!Yield.CheckContext (ec, loc))
103 ec.CurrentBranching.CurrentUsageVector.Goto ();
107 protected override void DoEmit (EmitContext ec)
109 ec.CurrentIterator.EmitYieldBreak (ec.ig);
113 public class Iterator : Class {
114 ToplevelBlock original_block;
116 string original_name;
119 TypeExpr iterator_type_expr;
126 // The state as we generate the iterator
128 Label move_next_ok, move_next_error;
129 ArrayList resume_points = new ArrayList ();
133 // Context from the original method
135 TypeContainer container;
136 TypeExpr current_type;
140 InternalParameters parameters;
142 MethodInfo dispose_method;
144 Expression enumerator_type;
145 Expression enumerable_type;
146 Expression generic_enumerator_type;
147 Expression generic_enumerable_type;
148 TypeArguments generic_args;
150 protected enum State {
156 static int proxy_count;
158 public void EmitYieldBreak (ILGenerator ig)
160 ig.Emit (OpCodes.Ldarg_0);
161 IntConstant.EmitInt (ig, (int) State.After);
162 ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
163 ig.Emit (OpCodes.Br, move_next_error);
166 public void EmitMoveNext (EmitContext ec)
168 ILGenerator ig = ec.ig;
170 move_next_ok = ig.DefineLabel ();
171 move_next_error = ig.DefineLabel ();
173 LocalBuilder retval = ec.GetTemporaryLocal (TypeManager.int32_type);
175 ig.BeginExceptionBlock ();
177 Label dispatcher = ig.DefineLabel ();
178 ig.Emit (OpCodes.Br, dispatcher);
180 ResumePoint entry_point = new ResumePoint (null);
181 resume_points.Add (entry_point);
182 entry_point.Define (ig);
184 ec.EmitTopBlock (original_block, parameters, Location);
187 ig.MarkLabel (dispatcher);
189 Label [] labels = new Label [resume_points.Count];
190 for (int i = 0; i < labels.Length; i++)
191 labels [i] = ((ResumePoint) resume_points [i]).Label;
193 ig.Emit (OpCodes.Ldarg_0);
194 ig.Emit (OpCodes.Ldfld, pc_field.FieldBuilder);
195 ig.Emit (OpCodes.Switch, labels);
197 Label end = ig.DefineLabel ();
199 ig.MarkLabel (move_next_error);
200 ig.Emit (OpCodes.Ldc_I4_0);
201 ig.Emit (OpCodes.Stloc, retval);
202 ig.Emit (OpCodes.Leave, end);
204 ig.MarkLabel (move_next_ok);
205 ig.Emit (OpCodes.Ldc_I4_1);
206 ig.Emit (OpCodes.Stloc, retval);
207 ig.Emit (OpCodes.Leave, end);
209 ig.BeginFaultBlock ();
211 ig.Emit (OpCodes.Ldarg_0);
212 ig.Emit (OpCodes.Callvirt, dispose_method);
214 ig.EndExceptionBlock ();
217 ig.Emit (OpCodes.Ldloc, retval);
218 ig.Emit (OpCodes.Ret);
221 public void EmitDispose (EmitContext ec)
223 ILGenerator ig = ec.ig;
225 Label end = ig.DefineLabel ();
226 Label dispatcher = ig.DefineLabel ();
227 ig.Emit (OpCodes.Br, dispatcher);
229 ec.RemapToProxy = true;
230 Label [] labels = new Label [resume_points.Count];
231 for (int i = 0; i < labels.Length; i++) {
232 ResumePoint point = (ResumePoint) resume_points [i];
234 if (point.FinallyBlocks == null) {
239 labels [i] = ig.DefineLabel ();
240 ig.MarkLabel (labels [i]);
242 ig.BeginExceptionBlock ();
243 ig.BeginFinallyBlock ();
245 foreach (ExceptionStatement stmt in point.FinallyBlocks) {
247 stmt.EmitFinally (ec);
250 ig.EndExceptionBlock ();
251 ig.Emit (OpCodes.Br, end);
253 ec.RemapToProxy = false;
255 ig.MarkLabel (dispatcher);
256 ig.Emit (OpCodes.Ldarg_0);
257 ig.Emit (OpCodes.Ldfld, pc_field.FieldBuilder);
258 ig.Emit (OpCodes.Switch, labels);
260 ig.Emit (OpCodes.Ldarg_0);
261 IntConstant.EmitInt (ig, (int) State.After);
262 ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
267 protected class ResumePoint
270 public readonly ExceptionStatement[] FinallyBlocks;
272 public ResumePoint (ArrayList list)
275 FinallyBlocks = new ExceptionStatement [list.Count];
276 list.CopyTo (FinallyBlocks, 0);
280 public void Define (ILGenerator ig)
282 Label = ig.DefineLabel ();
283 ig.MarkLabel (Label);
288 // Invoked when a local variable declaration needs to be mapped to
289 // a field in our proxy class
291 // Prefixes registered:
292 // v_ for EmitContext.MapVariable
295 public FieldBuilder MapVariable (string pfx, string name, Type t)
297 string full_name = pfx + name;
298 FieldBuilder fb = (FieldBuilder) fields [full_name];
302 fb = TypeBuilder.DefineField (full_name, t, FieldAttributes.Private);
303 fields.Add (full_name, fb);
308 // Called back from Yield
310 public void MarkYield (EmitContext ec, Expression expr,
311 ArrayList finally_blocks)
313 ILGenerator ig = ec.ig;
315 // Store the new current
316 ig.Emit (OpCodes.Ldarg_0);
318 ig.Emit (OpCodes.Stfld, current_field.FieldBuilder);
322 ig.Emit (OpCodes.Ldarg_0);
323 IntConstant.EmitInt (ig, pc);
324 ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
327 ig.Emit (OpCodes.Br, move_next_ok);
329 ResumePoint point = new ResumePoint (finally_blocks);
330 resume_points.Add (point);
334 public void MarkFinally (EmitContext ec, ArrayList finally_blocks)
336 ILGenerator ig = ec.ig;
340 ig.Emit (OpCodes.Ldarg_0);
341 IntConstant.EmitInt (ig, pc);
342 ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
344 ResumePoint point = new ResumePoint (finally_blocks);
345 resume_points.Add (point);
349 private static MemberName MakeProxyName (string name)
351 int pos = name.LastIndexOf ('.');
353 name = name.Substring (pos + 1);
355 return new MemberName ("<" + name + ">__" + (proxy_count++));
361 public Iterator (TypeContainer container, string name, Type return_type,
362 Type [] param_types, InternalParameters parameters,
363 int modifiers, ToplevelBlock block, Location loc)
364 : base (container.NamespaceEntry, container, MakeProxyName (name),
365 Modifiers.PRIVATE, null, loc)
367 this.container = container;
368 this.return_type = return_type;
369 this.param_types = param_types;
370 this.parameters = parameters;
371 this.original_name = name;
372 this.original_block = block;
373 this.block = new ToplevelBlock (loc);
375 fields = new Hashtable ();
377 is_static = (modifiers & Modifiers.STATIC) != 0;
380 public bool DefineIterator ()
382 ec = new EmitContext (this, Mono.CSharp.Location.Null, null, null, ModFlags);
384 if (!CheckType (return_type)) {
387 "The body of `{0}' cannot be an iterator block " +
388 "because '{1}' is not an iterator interface type",
389 original_name, TypeManager.CSharpName (return_type));
393 for (int i = 0; i < parameters.Count; i++){
394 Parameter.Modifier mod = parameters.ParameterModifier (i);
395 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0){
398 "Iterators cannot have ref or out parameters");
403 if (container.CurrentType != null)
404 this_type = container.CurrentType;
406 this_type = container.TypeBuilder;
408 generic_args = new TypeArguments (Location);
409 generic_args.Add (new TypeExpression (iterator_type, Location));
411 ArrayList list = new ArrayList ();
413 enumerable_type = new TypeExpression (
414 TypeManager.ienumerable_type, Location);
415 list.Add (enumerable_type);
417 generic_enumerable_type = new ConstructedType (
418 TypeManager.generic_ienumerable_type,
419 generic_args, Location);
420 list.Add (generic_enumerable_type);
423 enumerator_type = new TypeExpression (
424 TypeManager.ienumerator_type, Location);
425 list.Add (enumerator_type);
427 list.Add (new TypeExpression (TypeManager.idisposable_type, Location));
429 generic_enumerator_type = new ConstructedType (
430 TypeManager.generic_ienumerator_type,
431 generic_args, Location);
432 list.Add (generic_enumerator_type);
434 iterator_type_expr = new TypeExpression (iterator_type, Location);
436 container.AddIterator (this);
442 MethodInfo FetchMethodDispose ()
444 MemberList dispose_list;
446 dispose_list = FindMembers (
448 MemberTypes.Method, BindingFlags.Public | BindingFlags.Instance,
449 Type.FilterName, "Dispose");
451 if (dispose_list.Count != 1)
452 throw new InternalErrorException ("Cannot find Dipose() method.");
454 return (MethodInfo) dispose_list [0];
457 protected override bool DoDefineMembers ()
459 if (!base.DoDefineMembers ())
462 dispose_method = FetchMethodDispose ();
463 if (dispose_method == null)
470 // Returns the new block for the method, or null on failure
472 protected override bool DefineNestedTypes ()
474 if (CurrentType != null)
475 current_type = new TypeExpression (CurrentType, Location);
477 current_type = new TypeExpression (TypeBuilder, Location);
480 Define_Constructor ();
481 Define_Current (false);
482 Define_Current (true);
488 Define_GetEnumerator (false);
489 Define_GetEnumerator (true);
494 return base.DefineNestedTypes ();
502 public Field this_field;
503 public Field[] parameter_fields;
507 int first = is_static ? 0 : 1;
509 ArrayList args = new ArrayList ();
512 args.Add (new Argument (
513 new ThisParameterReference (t, 0, Location)));
516 args.Add (new Argument (new BoolLiteral (false)));
518 for (int i = 0; i < parameters.Count; i++) {
519 Type t = parameters.ParameterType (i);
520 args.Add (new Argument (
521 new SimpleParameterReference (t, first + i, Location)));
524 Expression new_expr = new New (current_type, args, Location);
526 block.AddStatement (new NoCheckReturn (new_expr, Location));
529 void Define_Fields ()
531 Location loc = Location.Null;
533 pc_field = new Field (
534 this, TypeManager.system_int32_expr, Modifiers.PRIVATE, "PC",
538 current_field = new Field (
539 this, iterator_type_expr, Modifiers.PRIVATE, "current",
541 AddField (current_field);
544 this_field = new Field (
545 this, new TypeExpression (this_type, loc),
546 Modifiers.PRIVATE, "this", null, null, loc);
547 AddField (this_field);
550 parameter_fields = new Field [parameters.Count];
551 for (int i = 0; i < parameters.Count; i++) {
552 string fname = String.Format (
553 "field{0}_{1}", i, parameters.ParameterName (i));
555 parameter_fields [i] = new Field (
557 new TypeExpression (parameters.ParameterType (i), loc),
558 Modifiers.PRIVATE, fname, null, null, loc);
559 AddField (parameter_fields [i]);
563 void Define_Constructor ()
565 Parameters ctor_params;
567 ArrayList list = new ArrayList ();
570 list.Add (new Parameter (
571 new TypeExpression (this_type, Location),
572 "this", Parameter.Modifier.NONE, null));
573 list.Add (new Parameter (
574 TypeManager.system_boolean_expr, "initialized",
575 Parameter.Modifier.NONE, null));
577 Parameter[] old_fixed = parameters.Parameters.FixedParameters;
578 if (old_fixed != null)
579 list.AddRange (old_fixed);
581 Parameter[] fixed_params = new Parameter [list.Count];
582 list.CopyTo (fixed_params);
584 ctor_params = new Parameters (
585 fixed_params, parameters.Parameters.ArrayParameter,
588 Constructor ctor = new Constructor (
589 this, Name, Modifiers.PUBLIC, ctor_params,
590 new ConstructorBaseInitializer (
591 null, Parameters.EmptyReadOnlyParameters, Location),
593 AddConstructor (ctor);
595 ToplevelBlock block = ctor.Block = new ToplevelBlock (Location);
600 Assign assign = new Assign (
601 new FieldExpression (this_field),
602 new SimpleParameterReference (t, 1, Location),
605 block.AddStatement (new StatementExpression (assign, Location));
608 int first = is_static ? 2 : 3;
610 for (int i = 0; i < parameters.Count; i++) {
611 Type t = parameters.ParameterType (i);
613 Assign assign = new Assign (
614 new FieldExpression (parameter_fields [i]),
615 new SimpleParameterReference (t, first + i, Location),
618 block.AddStatement (new StatementExpression (assign, Location));
621 State initial = is_enumerable ? State.Uninitialized : State.Running;
622 block.AddStatement (new SetState (this, initial, Location));
624 block.AddStatement (new If (
625 new SimpleParameterReference (
626 TypeManager.bool_type, first - 1, Location),
627 new SetState (this, State.Running, Location),
631 Statement Create_ThrowInvalidOperation ()
633 TypeExpr ex_type = new TypeExpression (
634 TypeManager.invalid_operation_exception_type, Location);
636 return new Throw (new New (ex_type, null, Location), Location);
639 Statement Create_ThrowNotSupported ()
641 TypeExpr ex_type = new TypeExpression (
642 TypeManager.not_supported_exception_type, Location);
644 return new Throw (new New (ex_type, null, Location), Location);
647 void Define_Current (bool is_generic)
652 left = new MemberName (
653 "System.Collections.Generic.IEnumerator",
655 type = iterator_type_expr;
657 left = new MemberName ("System.Collections.IEnumerator");
658 type = TypeManager.system_object_expr;
661 MemberName name = new MemberName (left, "Current", null);
663 ToplevelBlock get_block = new ToplevelBlock (Location);
665 get_block.AddStatement (new If (
667 Binary.Operator.LessThanOrEqual,
668 new FieldExpression (pc_field),
669 new IntLiteral ((int) State.Running), Location),
670 Create_ThrowInvalidOperation (),
672 new FieldExpression (current_field), Location),
675 Accessor getter = new Accessor (get_block, 0, null, Location);
677 Property current = new Property (
678 this, type, 0, false, name, null, getter, null, Location);
679 AddProperty (current);
682 void Define_MoveNext ()
684 Method move_next = new Method (
685 this, null, TypeManager.system_boolean_expr,
686 Modifiers.PUBLIC, false, new MemberName ("MoveNext"),
687 Parameters.EmptyReadOnlyParameters, null,
689 AddMethod (move_next);
691 ToplevelBlock block = move_next.Block = new ToplevelBlock (Location);
693 MoveNextMethod inline = new MoveNextMethod (this, Location);
694 block.AddStatement (inline);
697 void Define_GetEnumerator (bool is_generic)
702 left = new MemberName (
703 "System.Collections.Generic.IEnumerable",
705 type = generic_enumerator_type;
707 left = new MemberName ("System.Collections.IEnumerable");
708 type = enumerator_type;
711 MemberName name = new MemberName (left, "GetEnumerator", null);
713 Method get_enumerator = new Method (
714 this, null, type, 0, false, name,
715 Parameters.EmptyReadOnlyParameters, null,
717 AddMethod (get_enumerator);
719 get_enumerator.Block = new ToplevelBlock (Location);
721 Expression ce = new MemberAccess (
722 new SimpleName ("System.Threading.Interlocked", Location),
723 "CompareExchange", Location);
725 Expression pc = new FieldExpression (pc_field);
726 Expression before = new IntLiteral ((int) State.Running);
727 Expression uninitialized = new IntLiteral ((int) State.Uninitialized);
729 ArrayList args = new ArrayList ();
730 args.Add (new Argument (pc, Argument.AType.Ref));
731 args.Add (new Argument (before, Argument.AType.Expression));
732 args.Add (new Argument (uninitialized, Argument.AType.Expression));
734 get_enumerator.Block.AddStatement (new If (
736 Binary.Operator.Equality,
737 new Invocation (ce, args, Location),
738 uninitialized, Location),
739 new Return (new This (block, Location), Location),
742 args = new ArrayList ();
744 args.Add (new Argument (new FieldExpression (this_field)));
746 args.Add (new Argument (new BoolLiteral (true)));
748 for (int i = 0; i < parameters.Count; i++)
749 args.Add (new Argument (
750 new FieldExpression (parameter_fields [i])));
752 Expression new_expr = new New (current_type, args, Location);
753 get_enumerator.Block.AddStatement (new Return (new_expr, Location));
756 protected class SimpleParameterReference : Expression
760 public SimpleParameterReference (Type type, int idx, Location loc)
765 eclass = ExprClass.Variable;
768 public override Expression DoResolve (EmitContext ec)
773 public override void Emit (EmitContext ec)
778 protected virtual void DoEmit (EmitContext ec)
780 ParameterReference.EmitLdArg (ec.ig, idx);
784 protected class ThisParameterReference : SimpleParameterReference
786 public ThisParameterReference (Type type, int idx, Location loc)
787 : base (type, idx, loc)
790 protected override void DoEmit (EmitContext ec)
793 if (ec.TypeContainer is Struct)
794 ec.ig.Emit (OpCodes.Ldobj, type);
798 protected class FieldExpression : Expression
802 public FieldExpression (Field field)
807 public override Expression DoResolve (EmitContext ec)
809 FieldExpr fexpr = new FieldExpr (field.FieldBuilder, loc);
810 fexpr.InstanceExpression = ec.GetThis (loc);
811 return fexpr.Resolve (ec);
814 public override void Emit (EmitContext ec)
816 throw new InvalidOperationException ();
820 protected class MoveNextMethod : Statement {
823 public MoveNextMethod (Iterator iterator, Location loc)
826 this.iterator = iterator;
829 public override bool Resolve (EmitContext ec)
831 ec.CurrentBranching.CurrentUsageVector.Return ();
835 protected override void DoEmit (EmitContext ec)
837 int code_flags = Modifiers.METHOD_YIELDS;
838 if (iterator.is_static)
839 code_flags |= Modifiers.STATIC;
841 EmitContext new_ec = new EmitContext (
842 iterator.container, loc, ec.ig,
843 TypeManager.int32_type, code_flags);
845 new_ec.CurrentIterator = iterator;
847 iterator.EmitMoveNext (new_ec);
851 protected class DisposeMethod : Statement {
854 public DisposeMethod (Iterator iterator, Location loc)
857 this.iterator = iterator;
860 public override bool Resolve (EmitContext ec)
865 protected override void DoEmit (EmitContext ec)
867 iterator.EmitDispose (ec);
871 protected class StatementList : Statement {
872 ArrayList statements;
874 public StatementList (Location loc)
877 statements = new ArrayList ();
880 public void Add (Statement statement)
882 statements.Add (statement);
885 public override bool Resolve (EmitContext ec)
887 foreach (Statement stmt in statements) {
888 if (!stmt.Resolve (ec))
895 protected override void DoEmit (EmitContext ec)
897 foreach (Statement stmt in statements)
902 protected class SetState : Statement
907 public SetState (Iterator iterator, State state, Location loc)
909 this.iterator = iterator;
914 public override bool Resolve (EmitContext ec)
919 protected override void DoEmit (EmitContext ec)
921 ec.ig.Emit (OpCodes.Ldarg_0);
922 IntConstant.EmitInt (ec.ig, (int) state);
923 ec.ig.Emit (OpCodes.Stfld, iterator.pc_field.FieldBuilder);
929 Method reset = new Method (
930 this, null, TypeManager.system_void_expr, Modifiers.PUBLIC,
931 false, new MemberName ("Reset"),
932 Parameters.EmptyReadOnlyParameters, null, Location);
935 reset.Block = new ToplevelBlock (Location);
936 reset.Block.AddStatement (Create_ThrowNotSupported ());
939 void Define_Dispose ()
941 dispose = new Method (
942 this, null, TypeManager.system_void_expr, Modifiers.PUBLIC,
943 false, new MemberName ("Dispose"),
944 Parameters.EmptyReadOnlyParameters, null, Location);
947 dispose.Block = new ToplevelBlock (Location);
948 dispose.Block.AddStatement (new DisposeMethod (this, Location));
951 public ToplevelBlock Block {
952 get { return block; }
955 public Type IteratorType {
956 get { return iterator_type; }
960 // This return statement tricks return into not flagging an error for being
961 // used in a Yields method
963 class NoCheckReturn : Return {
964 public NoCheckReturn (Expression expr, Location loc) : base (expr, loc)
968 public override bool Resolve (EmitContext ec)
970 ec.InIterator = false;
971 bool ret_val = base.Resolve (ec);
972 ec.InIterator = true;
978 bool CheckType (Type t)
980 if (t == TypeManager.ienumerable_type) {
981 iterator_type = TypeManager.object_type;
982 is_enumerable = true;
984 } else if (t == TypeManager.ienumerator_type) {
985 iterator_type = TypeManager.object_type;
986 is_enumerable = false;
990 if (!t.IsGenericInstance)
993 Type[] args = TypeManager.GetTypeArguments (t);
994 if (args.Length != 1)
997 Type gt = t.GetGenericTypeDefinition ();
998 if (gt == TypeManager.generic_ienumerable_type) {
999 iterator_type = args [0];
1000 is_enumerable = true;
1002 } else if (gt == TypeManager.generic_ienumerator_type) {
1003 iterator_type = args [0];
1004 is_enumerable = false;