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;
140 TypeExpr current_type;
144 InternalParameters parameters;
146 MethodInfo dispose_method;
148 Expression enumerator_type;
149 Expression enumerable_type;
150 Expression generic_enumerator_type;
151 Expression generic_enumerable_type;
152 TypeArguments generic_args;
154 protected enum State {
160 static int proxy_count;
162 public void EmitYieldBreak (ILGenerator ig)
164 ig.Emit (OpCodes.Ldarg_0);
165 IntConstant.EmitInt (ig, (int) State.After);
166 ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
167 ig.Emit (OpCodes.Br, move_next_error);
170 public void EmitMoveNext (EmitContext ec)
172 ILGenerator ig = ec.ig;
174 move_next_ok = ig.DefineLabel ();
175 move_next_error = ig.DefineLabel ();
177 LocalBuilder retval = ec.GetTemporaryLocal (TypeManager.int32_type);
179 ig.BeginExceptionBlock ();
181 Label dispatcher = ig.DefineLabel ();
182 ig.Emit (OpCodes.Br, dispatcher);
184 ResumePoint entry_point = new ResumePoint (null);
185 resume_points.Add (entry_point);
186 entry_point.Define (ig);
188 ec.EmitTopBlock (original_block, parameters, Location);
191 ig.MarkLabel (dispatcher);
193 Label [] labels = new Label [resume_points.Count];
194 for (int i = 0; i < labels.Length; i++)
195 labels [i] = ((ResumePoint) resume_points [i]).Label;
197 ig.Emit (OpCodes.Ldarg_0);
198 ig.Emit (OpCodes.Ldfld, pc_field.FieldBuilder);
199 ig.Emit (OpCodes.Switch, labels);
201 Label end = ig.DefineLabel ();
203 ig.MarkLabel (move_next_error);
204 ig.Emit (OpCodes.Ldc_I4_0);
205 ig.Emit (OpCodes.Stloc, retval);
206 ig.Emit (OpCodes.Leave, end);
208 ig.MarkLabel (move_next_ok);
209 ig.Emit (OpCodes.Ldc_I4_1);
210 ig.Emit (OpCodes.Stloc, retval);
211 ig.Emit (OpCodes.Leave, end);
213 ig.BeginFaultBlock ();
215 ig.Emit (OpCodes.Ldarg_0);
216 ig.Emit (OpCodes.Callvirt, dispose_method);
218 ig.EndExceptionBlock ();
221 ig.Emit (OpCodes.Ldloc, retval);
222 ig.Emit (OpCodes.Ret);
225 public void EmitDispose (EmitContext ec)
227 ILGenerator ig = ec.ig;
229 Label end = ig.DefineLabel ();
230 Label dispatcher = ig.DefineLabel ();
231 ig.Emit (OpCodes.Br, dispatcher);
233 ec.RemapToProxy = true;
234 Label [] labels = new Label [resume_points.Count];
235 for (int i = 0; i < labels.Length; i++) {
236 ResumePoint point = (ResumePoint) resume_points [i];
238 if (point.FinallyBlocks == null) {
243 labels [i] = ig.DefineLabel ();
244 ig.MarkLabel (labels [i]);
246 ig.BeginExceptionBlock ();
247 ig.BeginFinallyBlock ();
249 foreach (ExceptionStatement stmt in point.FinallyBlocks) {
251 stmt.EmitFinally (ec);
254 ig.EndExceptionBlock ();
255 ig.Emit (OpCodes.Br, end);
257 ec.RemapToProxy = false;
259 ig.MarkLabel (dispatcher);
260 ig.Emit (OpCodes.Ldarg_0);
261 ig.Emit (OpCodes.Ldfld, pc_field.FieldBuilder);
262 ig.Emit (OpCodes.Switch, labels);
264 ig.Emit (OpCodes.Ldarg_0);
265 IntConstant.EmitInt (ig, (int) State.After);
266 ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
271 protected class ResumePoint
274 public readonly ExceptionStatement[] FinallyBlocks;
276 public ResumePoint (ArrayList list)
279 FinallyBlocks = new ExceptionStatement [list.Count];
280 list.CopyTo (FinallyBlocks, 0);
284 public void Define (ILGenerator ig)
286 Label = ig.DefineLabel ();
287 ig.MarkLabel (Label);
292 // Invoked when a local variable declaration needs to be mapped to
293 // a field in our proxy class
295 // Prefixes registered:
296 // v_ for EmitContext.MapVariable
299 public FieldBuilder MapVariable (string pfx, string name, Type t)
301 string full_name = pfx + name;
302 FieldBuilder fb = (FieldBuilder) fields [full_name];
306 fb = TypeBuilder.DefineField (full_name, t, FieldAttributes.Private);
307 fields.Add (full_name, fb);
312 // Called back from Yield
314 public void MarkYield (EmitContext ec, Expression expr,
315 ArrayList finally_blocks)
317 ILGenerator ig = ec.ig;
319 // Store the new current
320 ig.Emit (OpCodes.Ldarg_0);
322 ig.Emit (OpCodes.Stfld, current_field.FieldBuilder);
326 ig.Emit (OpCodes.Ldarg_0);
327 IntConstant.EmitInt (ig, pc);
328 ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
331 ig.Emit (OpCodes.Br, move_next_ok);
333 ResumePoint point = new ResumePoint (finally_blocks);
334 resume_points.Add (point);
338 public void MarkFinally (EmitContext ec, ArrayList finally_blocks)
340 ILGenerator ig = ec.ig;
344 ig.Emit (OpCodes.Ldarg_0);
345 IntConstant.EmitInt (ig, pc);
346 ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
348 ResumePoint point = new ResumePoint (finally_blocks);
349 resume_points.Add (point);
353 private static MemberName MakeProxyName (string name)
355 int pos = name.LastIndexOf ('.');
357 name = name.Substring (pos + 1);
359 return new MemberName ("<" + name + ">__" + (proxy_count++));
365 public Iterator (TypeContainer container, string name, Type return_type,
366 Type [] param_types, InternalParameters parameters,
367 int modifiers, ToplevelBlock block, Location loc)
368 : base (container.NamespaceEntry, container, MakeProxyName (name),
369 (modifiers & Modifiers.UNSAFE) | Modifiers.PRIVATE, null, loc)
371 this.container = container;
372 this.return_type = return_type;
373 this.param_types = param_types;
374 this.parameters = parameters;
375 this.original_name = name;
376 this.original_block = block;
377 this.block = new ToplevelBlock (loc);
379 fields = new Hashtable ();
381 is_static = (modifiers & Modifiers.STATIC) != 0;
384 public bool DefineIterator ()
386 ec = new EmitContext (this, Mono.CSharp.Location.Null, null, null, ModFlags);
388 if (!CheckType (return_type)) {
391 "The body of `{0}' cannot be an iterator block " +
392 "because '{1}' is not an iterator interface type",
393 original_name, TypeManager.CSharpName (return_type));
397 for (int i = 0; i < parameters.Count; i++){
398 Parameter.Modifier mod = parameters.ParameterModifier (i);
399 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0){
402 "Iterators cannot have ref or out parameters");
407 if (container.CurrentType != null)
408 this_type = container.CurrentType;
410 this_type = container.TypeBuilder;
412 generic_args = new TypeArguments (Location);
413 generic_args.Add (new TypeExpression (iterator_type, Location));
415 ArrayList list = new ArrayList ();
417 enumerable_type = new TypeExpression (
418 TypeManager.ienumerable_type, Location);
419 list.Add (enumerable_type);
421 generic_enumerable_type = new ConstructedType (
422 TypeManager.generic_ienumerable_type,
423 generic_args, Location);
424 list.Add (generic_enumerable_type);
427 enumerator_type = new TypeExpression (
428 TypeManager.ienumerator_type, Location);
429 list.Add (enumerator_type);
431 list.Add (new TypeExpression (TypeManager.idisposable_type, Location));
433 generic_enumerator_type = new ConstructedType (
434 TypeManager.generic_ienumerator_type,
435 generic_args, Location);
436 list.Add (generic_enumerator_type);
438 iterator_type_expr = new TypeExpression (iterator_type, Location);
440 container.AddIterator (this);
446 MethodInfo FetchMethodDispose ()
448 MemberList dispose_list;
450 dispose_list = FindMembers (
452 MemberTypes.Method, BindingFlags.Public | BindingFlags.Instance,
453 Type.FilterName, "Dispose");
455 if (dispose_list.Count != 1)
456 throw new InternalErrorException ("Cannot find Dipose() method.");
458 return (MethodInfo) dispose_list [0];
461 protected override bool DoDefineMembers ()
463 if (!base.DoDefineMembers ())
466 dispose_method = FetchMethodDispose ();
467 if (dispose_method == null)
474 // Returns the new block for the method, or null on failure
476 protected override bool DefineNestedTypes ()
478 if (CurrentType != null)
479 current_type = new TypeExpression (CurrentType, Location);
481 current_type = new TypeExpression (TypeBuilder, Location);
484 Define_Constructor ();
485 Define_Current (false);
486 Define_Current (true);
492 Define_GetEnumerator (false);
493 Define_GetEnumerator (true);
498 return base.DefineNestedTypes ();
506 public Field this_field;
507 public Field[] parameter_fields;
511 int first = is_static ? 0 : 1;
513 ArrayList args = new ArrayList ();
516 args.Add (new Argument (
517 new ThisParameterReference (t, 0, Location)));
520 args.Add (new Argument (new BoolLiteral (false)));
522 for (int i = 0; i < parameters.Count; i++) {
523 Type t = parameters.ParameterType (i);
524 args.Add (new Argument (
525 new SimpleParameterReference (t, first + i, Location)));
528 Expression new_expr = new New (current_type, args, Location);
530 block.AddStatement (new NoCheckReturn (new_expr, Location));
533 void Define_Fields ()
535 Location loc = Location.Null;
537 pc_field = new Field (
538 this, TypeManager.system_int32_expr, Modifiers.PRIVATE, "PC",
542 current_field = new Field (
543 this, iterator_type_expr, Modifiers.PRIVATE, "current",
545 AddField (current_field);
548 this_field = new Field (
549 this, new TypeExpression (this_type, loc),
550 Modifiers.PRIVATE, "this", null, null, loc);
551 AddField (this_field);
554 parameter_fields = new Field [parameters.Count];
555 for (int i = 0; i < parameters.Count; i++) {
556 string fname = String.Format (
557 "field{0}_{1}", i, parameters.ParameterName (i));
559 parameter_fields [i] = new Field (
561 new TypeExpression (parameters.ParameterType (i), loc),
562 Modifiers.PRIVATE, fname, null, null, loc);
563 AddField (parameter_fields [i]);
567 void Define_Constructor ()
569 Parameters ctor_params;
571 ArrayList list = new ArrayList ();
574 list.Add (new Parameter (
575 new TypeExpression (this_type, Location),
576 "this", Parameter.Modifier.NONE, null));
577 list.Add (new Parameter (
578 TypeManager.system_boolean_expr, "initialized",
579 Parameter.Modifier.NONE, null));
581 Parameter[] old_fixed = parameters.Parameters.FixedParameters;
582 if (old_fixed != null)
583 list.AddRange (old_fixed);
585 Parameter[] fixed_params = new Parameter [list.Count];
586 list.CopyTo (fixed_params);
588 ctor_params = new Parameters (
589 fixed_params, parameters.Parameters.ArrayParameter,
592 Constructor ctor = new Constructor (
593 this, Name, Modifiers.PUBLIC, ctor_params,
594 new ConstructorBaseInitializer (
595 null, Parameters.EmptyReadOnlyParameters, Location),
597 AddConstructor (ctor);
599 ToplevelBlock block = ctor.Block = new ToplevelBlock (Location);
604 Assign assign = new Assign (
605 new FieldExpression (this_field),
606 new SimpleParameterReference (t, 1, Location),
609 block.AddStatement (new StatementExpression (assign, Location));
612 int first = is_static ? 2 : 3;
614 for (int i = 0; i < parameters.Count; i++) {
615 Type t = parameters.ParameterType (i);
617 Assign assign = new Assign (
618 new FieldExpression (parameter_fields [i]),
619 new SimpleParameterReference (t, first + i, Location),
622 block.AddStatement (new StatementExpression (assign, Location));
625 State initial = is_enumerable ? State.Uninitialized : State.Running;
626 block.AddStatement (new SetState (this, initial, Location));
628 block.AddStatement (new If (
629 new SimpleParameterReference (
630 TypeManager.bool_type, first - 1, Location),
631 new SetState (this, State.Running, Location),
635 Statement Create_ThrowInvalidOperation ()
637 TypeExpr ex_type = new TypeExpression (
638 TypeManager.invalid_operation_exception_type, Location);
640 return new Throw (new New (ex_type, null, Location), Location);
643 Statement Create_ThrowNotSupported ()
645 TypeExpr ex_type = new TypeExpression (
646 TypeManager.not_supported_exception_type, Location);
648 return new Throw (new New (ex_type, null, Location), Location);
651 void Define_Current (bool is_generic)
656 left = new MemberName (
657 "System.Collections.Generic.IEnumerator",
659 type = iterator_type_expr;
661 left = new MemberName ("System.Collections.IEnumerator");
662 type = TypeManager.system_object_expr;
665 MemberName name = new MemberName (left, "Current", null);
667 ToplevelBlock get_block = new ToplevelBlock (Location);
669 get_block.AddStatement (new If (
671 Binary.Operator.LessThanOrEqual,
672 new FieldExpression (pc_field),
673 new IntLiteral ((int) State.Running), Location),
674 Create_ThrowInvalidOperation (),
676 new FieldExpression (current_field), Location),
679 Accessor getter = new Accessor (get_block, 0, null, Location);
681 Property current = new Property (
682 this, type, 0, false, name, null, getter, null, Location);
683 AddProperty (current);
686 void Define_MoveNext ()
688 Method move_next = new Method (
689 this, null, TypeManager.system_boolean_expr,
690 Modifiers.PUBLIC, false, new MemberName ("MoveNext"),
691 Parameters.EmptyReadOnlyParameters, null,
693 AddMethod (move_next);
695 ToplevelBlock block = move_next.Block = new ToplevelBlock (Location);
697 MoveNextMethod inline = new MoveNextMethod (this, Location);
698 block.AddStatement (inline);
701 void Define_GetEnumerator (bool is_generic)
706 left = new MemberName (
707 "System.Collections.Generic.IEnumerable",
709 type = generic_enumerator_type;
711 left = new MemberName ("System.Collections.IEnumerable");
712 type = enumerator_type;
715 MemberName name = new MemberName (left, "GetEnumerator", null);
717 Method get_enumerator = new Method (
718 this, null, type, 0, false, name,
719 Parameters.EmptyReadOnlyParameters, null,
721 AddMethod (get_enumerator);
723 get_enumerator.Block = new ToplevelBlock (Location);
725 Expression ce = new MemberAccess (
726 new SimpleName ("System.Threading.Interlocked", Location),
727 "CompareExchange", Location);
729 Expression pc = new FieldExpression (pc_field);
730 Expression before = new IntLiteral ((int) State.Running);
731 Expression uninitialized = new IntLiteral ((int) State.Uninitialized);
733 ArrayList args = new ArrayList ();
734 args.Add (new Argument (pc, Argument.AType.Ref));
735 args.Add (new Argument (before, Argument.AType.Expression));
736 args.Add (new Argument (uninitialized, Argument.AType.Expression));
738 get_enumerator.Block.AddStatement (new If (
740 Binary.Operator.Equality,
741 new Invocation (ce, args, Location),
742 uninitialized, Location),
743 new Return (new This (block, Location), Location),
746 args = new ArrayList ();
748 args.Add (new Argument (new FieldExpression (this_field)));
750 args.Add (new Argument (new BoolLiteral (true)));
752 for (int i = 0; i < parameters.Count; i++)
753 args.Add (new Argument (
754 new FieldExpression (parameter_fields [i])));
756 Expression new_expr = new New (current_type, args, Location);
757 get_enumerator.Block.AddStatement (new Return (new_expr, Location));
760 protected class SimpleParameterReference : Expression
764 public SimpleParameterReference (Type type, int idx, Location loc)
769 eclass = ExprClass.Variable;
772 public override Expression DoResolve (EmitContext ec)
777 public override void Emit (EmitContext ec)
782 protected virtual void DoEmit (EmitContext ec)
784 ParameterReference.EmitLdArg (ec.ig, idx);
788 protected class ThisParameterReference : SimpleParameterReference
790 public ThisParameterReference (Type type, int idx, Location loc)
791 : base (type, idx, loc)
794 protected override void DoEmit (EmitContext ec)
797 if (ec.TypeContainer is Struct)
798 ec.ig.Emit (OpCodes.Ldobj, type);
802 protected class FieldExpression : Expression
806 public FieldExpression (Field field)
811 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
813 return DoResolve (ec);
816 public override Expression DoResolve (EmitContext ec)
818 FieldExpr fexpr = new FieldExpr (field.FieldBuilder, loc);
819 fexpr.InstanceExpression = ec.GetThis (loc);
820 return fexpr.Resolve (ec);
823 public override void Emit (EmitContext ec)
825 throw new InvalidOperationException ();
829 protected class MoveNextMethod : Statement {
832 public MoveNextMethod (Iterator iterator, Location loc)
835 this.iterator = iterator;
838 public override bool Resolve (EmitContext ec)
840 ec.CurrentBranching.CurrentUsageVector.Return ();
844 protected override void DoEmit (EmitContext ec)
846 int code_flags = Modifiers.METHOD_YIELDS;
847 if (iterator.is_static)
848 code_flags |= Modifiers.STATIC;
850 code_flags |= iterator.ModFlags & Modifiers.UNSAFE;
852 EmitContext new_ec = new EmitContext (
853 iterator.container, loc, ec.ig,
854 TypeManager.int32_type, code_flags);
856 new_ec.CurrentIterator = iterator;
858 iterator.EmitMoveNext (new_ec);
862 protected class DisposeMethod : Statement {
865 public DisposeMethod (Iterator iterator, Location loc)
868 this.iterator = iterator;
871 public override bool Resolve (EmitContext ec)
876 protected override void DoEmit (EmitContext ec)
878 iterator.EmitDispose (ec);
882 protected class StatementList : Statement {
883 ArrayList statements;
885 public StatementList (Location loc)
888 statements = new ArrayList ();
891 public void Add (Statement statement)
893 statements.Add (statement);
896 public override bool Resolve (EmitContext ec)
898 foreach (Statement stmt in statements) {
899 if (!stmt.Resolve (ec))
906 protected override void DoEmit (EmitContext ec)
908 foreach (Statement stmt in statements)
913 protected class SetState : Statement
918 public SetState (Iterator iterator, State state, Location loc)
920 this.iterator = iterator;
925 public override bool Resolve (EmitContext ec)
930 protected override void DoEmit (EmitContext ec)
932 ec.ig.Emit (OpCodes.Ldarg_0);
933 IntConstant.EmitInt (ec.ig, (int) state);
934 ec.ig.Emit (OpCodes.Stfld, iterator.pc_field.FieldBuilder);
940 Method reset = new Method (
941 this, null, TypeManager.system_void_expr, Modifiers.PUBLIC,
942 false, new MemberName ("Reset"),
943 Parameters.EmptyReadOnlyParameters, null, Location);
946 reset.Block = new ToplevelBlock (Location);
947 reset.Block.AddStatement (Create_ThrowNotSupported ());
950 void Define_Dispose ()
952 dispose = new Method (
953 this, null, TypeManager.system_void_expr, Modifiers.PUBLIC,
954 false, new MemberName ("Dispose"),
955 Parameters.EmptyReadOnlyParameters, null, Location);
958 dispose.Block = new ToplevelBlock (Location);
959 dispose.Block.AddStatement (new DisposeMethod (this, Location));
962 public ToplevelBlock Block {
963 get { return block; }
966 public Type IteratorType {
967 get { return iterator_type; }
971 // This return statement tricks return into not flagging an error for being
972 // used in a Yields method
974 class NoCheckReturn : Return {
975 public NoCheckReturn (Expression expr, Location loc) : base (expr, loc)
979 public override bool Resolve (EmitContext ec)
981 ec.InIterator = false;
982 bool ret_val = base.Resolve (ec);
983 ec.InIterator = true;
989 bool CheckType (Type t)
991 if (t == TypeManager.ienumerable_type) {
992 iterator_type = TypeManager.object_type;
993 is_enumerable = true;
995 } else if (t == TypeManager.ienumerator_type) {
996 iterator_type = TypeManager.object_type;
997 is_enumerable = false;
1001 if (!t.IsGenericInstance)
1004 Type[] args = TypeManager.GetTypeArguments (t);
1005 if (args.Length != 1)
1008 Type gt = t.GetGenericTypeDefinition ();
1009 if (gt == TypeManager.generic_ienumerable_type) {
1010 iterator_type = args [0];
1011 is_enumerable = true;
1013 } else if (gt == TypeManager.generic_ienumerator_type) {
1014 iterator_type = args [0];
1015 is_enumerable = false;