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;
138 InternalParameters parameters;
140 protected enum State {
146 static int proxy_count;
148 public void EmitYieldBreak (ILGenerator ig)
150 ig.Emit (OpCodes.Ldarg_0);
151 IntConstant.EmitInt (ig, (int) State.After);
152 ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
153 ig.Emit (OpCodes.Br, move_next_error);
156 public void EmitMoveNext (EmitContext ec)
158 ILGenerator ig = ec.ig;
160 move_next_ok = ig.DefineLabel ();
161 move_next_error = ig.DefineLabel ();
163 LocalBuilder retval = ec.GetTemporaryLocal (TypeManager.int32_type);
165 ig.BeginExceptionBlock ();
167 Label dispatcher = ig.DefineLabel ();
168 ig.Emit (OpCodes.Br, dispatcher);
170 ResumePoint entry_point = new ResumePoint (null);
171 resume_points.Add (entry_point);
172 entry_point.Define (ig);
174 ec.EmitTopBlock (original_block, parameters, Location);
177 ig.MarkLabel (dispatcher);
179 Label [] labels = new Label [resume_points.Count];
180 for (int i = 0; i < labels.Length; i++)
181 labels [i] = ((ResumePoint) resume_points [i]).Label;
183 ig.Emit (OpCodes.Ldarg_0);
184 ig.Emit (OpCodes.Ldfld, pc_field.FieldBuilder);
185 ig.Emit (OpCodes.Switch, labels);
187 Label end = ig.DefineLabel ();
189 ig.MarkLabel (move_next_error);
190 ig.Emit (OpCodes.Ldc_I4_0);
191 ig.Emit (OpCodes.Stloc, retval);
192 ig.Emit (OpCodes.Leave, end);
194 ig.MarkLabel (move_next_ok);
195 ig.Emit (OpCodes.Ldc_I4_1);
196 ig.Emit (OpCodes.Stloc, retval);
197 ig.Emit (OpCodes.Leave, end);
199 ig.BeginFaultBlock ();
201 ig.Emit (OpCodes.Ldarg_0);
202 ig.Emit (OpCodes.Callvirt, dispose.MethodBuilder);
204 ig.EndExceptionBlock ();
207 ig.Emit (OpCodes.Ldloc, retval);
208 ig.Emit (OpCodes.Ret);
211 public void EmitDispose (EmitContext ec)
213 ILGenerator ig = ec.ig;
215 Label end = ig.DefineLabel ();
216 Label dispatcher = ig.DefineLabel ();
217 ig.Emit (OpCodes.Br, dispatcher);
219 ec.RemapToProxy = true;
220 Label [] labels = new Label [resume_points.Count];
221 for (int i = 0; i < labels.Length; i++) {
222 ResumePoint point = (ResumePoint) resume_points [i];
224 if (point.FinallyBlocks == null) {
229 labels [i] = ig.DefineLabel ();
230 ig.MarkLabel (labels [i]);
232 ig.BeginExceptionBlock ();
233 ig.BeginFinallyBlock ();
235 foreach (ExceptionStatement stmt in point.FinallyBlocks) {
237 stmt.EmitFinally (ec);
240 ig.EndExceptionBlock ();
241 ig.Emit (OpCodes.Br, end);
243 ec.RemapToProxy = false;
245 ig.MarkLabel (dispatcher);
246 ig.Emit (OpCodes.Ldarg_0);
247 ig.Emit (OpCodes.Ldfld, pc_field.FieldBuilder);
248 ig.Emit (OpCodes.Switch, labels);
250 ig.Emit (OpCodes.Ldarg_0);
251 IntConstant.EmitInt (ig, (int) State.After);
252 ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
257 protected class ResumePoint
260 public readonly ExceptionStatement[] FinallyBlocks;
262 public ResumePoint (ArrayList list)
265 FinallyBlocks = new ExceptionStatement [list.Count];
266 list.CopyTo (FinallyBlocks, 0);
270 public void Define (ILGenerator ig)
272 Label = ig.DefineLabel ();
273 ig.MarkLabel (Label);
278 // Invoked when a local variable declaration needs to be mapped to
279 // a field in our proxy class
281 // Prefixes registered:
282 // v_ for EmitContext.MapVariable
285 public FieldBuilder MapVariable (string pfx, string name, Type t)
287 string full_name = pfx + name;
288 FieldBuilder fb = (FieldBuilder) fields [full_name];
292 fb = TypeBuilder.DefineField (full_name, t, FieldAttributes.Private);
293 fields.Add (full_name, fb);
298 // Called back from Yield
300 public void MarkYield (EmitContext ec, Expression expr,
301 ArrayList finally_blocks)
303 ILGenerator ig = ec.ig;
305 // Store the new current
306 ig.Emit (OpCodes.Ldarg_0);
308 ig.Emit (OpCodes.Stfld, current_field.FieldBuilder);
312 ig.Emit (OpCodes.Ldarg_0);
313 IntConstant.EmitInt (ig, pc);
314 ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
317 ig.Emit (OpCodes.Br, move_next_ok);
319 ResumePoint point = new ResumePoint (finally_blocks);
320 resume_points.Add (point);
324 public void MarkFinally (EmitContext ec, ArrayList finally_blocks)
326 ILGenerator ig = ec.ig;
330 ig.Emit (OpCodes.Ldarg_0);
331 IntConstant.EmitInt (ig, pc);
332 ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
334 ResumePoint point = new ResumePoint (finally_blocks);
335 resume_points.Add (point);
339 private static MemberName MakeProxyName (string name)
341 int pos = name.LastIndexOf ('.');
343 name = name.Substring (pos + 1);
345 return new MemberName ("<" + name + ">__" + (proxy_count++));
351 public Iterator (TypeContainer container, string name, Type return_type,
352 Type [] param_types, InternalParameters parameters,
353 int modifiers, ToplevelBlock block, Location loc)
354 : base (container.NamespaceEntry, container, MakeProxyName (name),
355 Modifiers.PRIVATE, null, loc)
357 this.container = container;
358 this.return_type = return_type;
359 this.param_types = param_types;
360 this.parameters = parameters;
361 this.original_name = name;
362 this.original_block = block;
363 this.block = new ToplevelBlock (loc);
365 fields = new Hashtable ();
367 is_static = (modifiers & Modifiers.STATIC) != 0;
370 public bool DefineIterator ()
372 ec = new EmitContext (this, Mono.CSharp.Location.Null, null, null, ModFlags);
374 if (!CheckType (return_type)) {
377 "The body of `{0}' cannot be an iterator block " +
378 "because '{1}' is not an iterator interface type",
379 original_name, TypeManager.CSharpName (return_type));
383 for (int i = 0; i < parameters.Count; i++){
384 Parameter.Modifier mod = parameters.ParameterModifier (i);
385 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0){
388 "Iterators cannot have ref or out parameters");
393 ArrayList list = new ArrayList ();
395 list.Add (new TypeExpression (
396 TypeManager.ienumerable_type, Location));
397 list.Add (new TypeExpression (TypeManager.ienumerator_type, Location));
398 list.Add (new TypeExpression (TypeManager.idisposable_type, Location));
400 iterator_type_expr = new TypeExpression (iterator_type, Location);
402 container.AddIterator (this);
409 // Returns the new block for the method, or null on failure
411 protected override bool DefineNestedTypes ()
414 Define_Constructor ();
421 Define_GetEnumerator ();
425 return base.DefineNestedTypes ();
433 public Field this_field;
434 public Field[] parameter_fields;
438 int first = is_static ? 0 : 1;
440 ArrayList args = new ArrayList ();
442 Type t = container.TypeBuilder;
443 args.Add (new Argument (
444 new ThisParameterReference (t, 0, Location)));
447 args.Add (new Argument (new BoolLiteral (false)));
449 for (int i = 0; i < parameters.Count; i++) {
450 Type t = parameters.ParameterType (i);
451 args.Add (new Argument (
452 new SimpleParameterReference (t, first + i, Location)));
455 Expression new_expr = new New (
456 new TypeExpression (TypeBuilder, Location), args, Location);
458 block.AddStatement (new NoCheckReturn (new_expr, Location));
461 void Define_Fields ()
463 Location loc = Location.Null;
465 pc_field = new Field (
466 this, TypeManager.system_int32_expr, Modifiers.PRIVATE, "PC",
470 current_field = new Field (
471 this, iterator_type_expr, Modifiers.PRIVATE, "current",
473 AddField (current_field);
476 this_field = new Field (
478 new TypeExpression (container.TypeBuilder, Location),
479 Modifiers.PRIVATE, "this", null, null, loc);
480 AddField (this_field);
483 parameter_fields = new Field [parameters.Count];
484 for (int i = 0; i < parameters.Count; i++) {
485 string fname = String.Format (
486 "field{0}_{1}", i, parameters.ParameterName (i));
488 parameter_fields [i] = new Field (
490 new TypeExpression (parameters.ParameterType (i), loc),
491 Modifiers.PRIVATE, fname, null, null, loc);
492 AddField (parameter_fields [i]);
496 void Define_Constructor ()
498 Parameters ctor_params;
500 ArrayList list = new ArrayList ();
503 list.Add (new Parameter (
504 new TypeExpression (container.TypeBuilder, Location),
505 "this", Parameter.Modifier.NONE, null));
506 list.Add (new Parameter (
507 TypeManager.system_boolean_expr, "initialized",
508 Parameter.Modifier.NONE, null));
510 Parameter[] old_fixed = parameters.Parameters.FixedParameters;
511 if (old_fixed != null)
512 list.AddRange (old_fixed);
514 Parameter[] fixed_params = new Parameter [list.Count];
515 list.CopyTo (fixed_params);
517 ctor_params = new Parameters (
518 fixed_params, parameters.Parameters.ArrayParameter,
521 Constructor ctor = new Constructor (
522 this, Name, Modifiers.PUBLIC, ctor_params,
523 new ConstructorBaseInitializer (
524 null, Parameters.EmptyReadOnlyParameters, Location),
526 AddConstructor (ctor);
528 ToplevelBlock block = ctor.Block = new ToplevelBlock (Location);
531 Type t = container.TypeBuilder;
533 Assign assign = new Assign (
534 new FieldExpression (this_field),
535 new SimpleParameterReference (t, 1, Location),
538 block.AddStatement (new StatementExpression (assign, Location));
541 int first = is_static ? 2 : 3;
543 for (int i = 0; i < parameters.Count; i++) {
544 Type t = parameters.ParameterType (i);
546 Assign assign = new Assign (
547 new FieldExpression (parameter_fields [i]),
548 new SimpleParameterReference (t, first + i, Location),
551 block.AddStatement (new StatementExpression (assign, Location));
554 State initial = is_enumerable ? State.Uninitialized : State.Running;
555 block.AddStatement (new SetState (this, initial, Location));
557 block.AddStatement (new If (
558 new SimpleParameterReference (
559 TypeManager.bool_type, first - 1, Location),
560 new SetState (this, State.Running, Location),
564 Statement Create_ThrowInvalidOperation ()
566 TypeExpr ex_type = new TypeExpression (
567 TypeManager.invalid_operation_exception_type, Location);
569 return new Throw (new New (ex_type, null, Location), Location);
572 Statement Create_ThrowNotSupported ()
574 TypeExpr ex_type = new TypeExpression (
575 TypeManager.not_supported_exception_type, Location);
577 return new Throw (new New (ex_type, null, Location), Location);
580 void Define_Current ()
582 ToplevelBlock get_block = new ToplevelBlock (Location);
583 MemberName left = new MemberName ("System.Collections.IEnumerator");
584 MemberName name = new MemberName (left, "Current");
586 get_block.AddStatement (new If (
588 Binary.Operator.LessThanOrEqual,
589 new FieldExpression (pc_field),
590 new IntLiteral ((int) State.Running), Location),
591 Create_ThrowInvalidOperation (),
593 new FieldExpression (current_field), Location),
596 Accessor getter = new Accessor (get_block, 0, null, Location);
598 Property current = new Property (
599 this, iterator_type_expr, 0,
600 false, name, null, getter, null, Location);
601 AddProperty (current);
604 void Define_MoveNext ()
606 Method move_next = new Method (
607 this, TypeManager.system_boolean_expr,
608 Modifiers.PUBLIC, false, new MemberName ("MoveNext"),
609 Parameters.EmptyReadOnlyParameters, null,
611 AddMethod (move_next);
613 ToplevelBlock block = move_next.Block = new ToplevelBlock (Location);
615 MoveNextMethod inline = new MoveNextMethod (this, Location);
616 block.AddStatement (inline);
619 void Define_GetEnumerator ()
621 MemberName left = new MemberName ("System.Collections.IEnumerable");
622 MemberName name = new MemberName (left, "GetEnumerator");
624 Method get_enumerator = new Method (
626 new TypeExpression (TypeManager.ienumerator_type, Location),
628 Parameters.EmptyReadOnlyParameters, null,
630 AddMethod (get_enumerator);
632 get_enumerator.Block = new ToplevelBlock (Location);
634 Expression ce = new MemberAccess (
635 new SimpleName ("System.Threading.Interlocked", Location),
636 "CompareExchange", Location);
638 Expression pc = new FieldExpression (pc_field);
639 Expression before = new IntLiteral ((int) State.Running);
640 Expression uninitialized = new IntLiteral ((int) State.Uninitialized);
642 ArrayList args = new ArrayList ();
643 args.Add (new Argument (pc, Argument.AType.Ref));
644 args.Add (new Argument (before, Argument.AType.Expression));
645 args.Add (new Argument (uninitialized, Argument.AType.Expression));
647 get_enumerator.Block.AddStatement (new If (
649 Binary.Operator.Equality,
650 new Invocation (ce, args, Location),
651 uninitialized, Location),
652 new Return (new This (block, Location), Location),
655 args = new ArrayList ();
657 args.Add (new Argument (new FieldExpression (this_field)));
659 args.Add (new Argument (new BoolLiteral (true)));
661 for (int i = 0; i < parameters.Count; i++)
662 args.Add (new Argument (
663 new FieldExpression (parameter_fields [i])));
665 Expression new_expr = new New (
666 new TypeExpression (TypeBuilder, Location), args, Location);
667 get_enumerator.Block.AddStatement (new Return (new_expr, Location));
670 protected class SimpleParameterReference : Expression
674 public SimpleParameterReference (Type type, int idx, Location loc)
679 eclass = ExprClass.Variable;
682 public override Expression DoResolve (EmitContext ec)
687 public override void Emit (EmitContext ec)
692 protected virtual void DoEmit (EmitContext ec)
694 ParameterReference.EmitLdArg (ec.ig, idx);
698 protected class ThisParameterReference : SimpleParameterReference
700 public ThisParameterReference (Type type, int idx, Location loc)
701 : base (type, idx, loc)
704 protected override void DoEmit (EmitContext ec)
707 if (ec.TypeContainer is Struct)
708 ec.ig.Emit (OpCodes.Ldobj, type);
712 protected class FieldExpression : Expression
716 public FieldExpression (Field field)
721 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
723 return DoResolve (ec);
726 public override Expression DoResolve (EmitContext ec)
728 FieldExpr fexpr = new FieldExpr (field.FieldBuilder, loc);
729 fexpr.InstanceExpression = ec.GetThis (loc);
730 return fexpr.Resolve (ec);
733 public override void Emit (EmitContext ec)
735 throw new InvalidOperationException ();
739 protected class MoveNextMethod : Statement {
742 public MoveNextMethod (Iterator iterator, Location loc)
745 this.iterator = iterator;
748 public override bool Resolve (EmitContext ec)
750 ec.CurrentBranching.CurrentUsageVector.Return ();
754 protected override void DoEmit (EmitContext ec)
756 int code_flags = Modifiers.METHOD_YIELDS;
757 if (iterator.is_static)
758 code_flags |= Modifiers.STATIC;
760 EmitContext new_ec = new EmitContext (
761 iterator.container, loc, ec.ig,
762 TypeManager.int32_type, code_flags);
764 new_ec.CurrentIterator = iterator;
766 iterator.EmitMoveNext (new_ec);
770 protected class DisposeMethod : Statement {
773 public DisposeMethod (Iterator iterator, Location loc)
776 this.iterator = iterator;
779 public override bool Resolve (EmitContext ec)
784 protected override void DoEmit (EmitContext ec)
786 iterator.EmitDispose (ec);
790 protected class StatementList : Statement {
791 ArrayList statements;
793 public StatementList (Location loc)
796 statements = new ArrayList ();
799 public void Add (Statement statement)
801 statements.Add (statement);
804 public override bool Resolve (EmitContext ec)
806 foreach (Statement stmt in statements) {
807 if (!stmt.Resolve (ec))
814 protected override void DoEmit (EmitContext ec)
816 foreach (Statement stmt in statements)
821 protected class SetState : Statement
826 public SetState (Iterator iterator, State state, Location loc)
828 this.iterator = iterator;
833 public override bool Resolve (EmitContext ec)
838 protected override void DoEmit (EmitContext ec)
840 ec.ig.Emit (OpCodes.Ldarg_0);
841 IntConstant.EmitInt (ec.ig, (int) state);
842 ec.ig.Emit (OpCodes.Stfld, iterator.pc_field.FieldBuilder);
848 Method reset = new Method (
849 this, TypeManager.system_void_expr, Modifiers.PUBLIC,
850 false, new MemberName ("Reset"),
851 Parameters.EmptyReadOnlyParameters, null, Location);
854 reset.Block = new ToplevelBlock (Location);
855 reset.Block.AddStatement (Create_ThrowNotSupported ());
858 void Define_Dispose ()
860 dispose = new Method (
861 this, TypeManager.system_void_expr, Modifiers.PUBLIC,
862 false, new MemberName ("Dispose"),
863 Parameters.EmptyReadOnlyParameters, null, Location);
866 dispose.Block = new ToplevelBlock (Location);
867 dispose.Block.AddStatement (new DisposeMethod (this, Location));
870 public ToplevelBlock Block {
871 get { return block; }
874 public Type IteratorType {
875 get { return iterator_type; }
879 // This return statement tricks return into not flagging an error for being
880 // used in a Yields method
882 class NoCheckReturn : Return {
883 public NoCheckReturn (Expression expr, Location loc) : base (expr, loc)
887 public override bool Resolve (EmitContext ec)
889 ec.InIterator = false;
890 bool ret_val = base.Resolve (ec);
891 ec.InIterator = true;
897 bool CheckType (Type t)
899 if (t == TypeManager.ienumerable_type) {
900 iterator_type = TypeManager.object_type;
901 is_enumerable = true;
903 } else if (t == TypeManager.ienumerator_type) {
904 iterator_type = TypeManager.object_type;
905 is_enumerable = false;