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 parent 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;
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.InAnonymousMethod){
55 Report.Error (1621, loc, "yield statement can not appear " +
56 "inside an anonymoud method");
61 // FIXME: Missing check for Yield inside try block that contains catch clauses
66 public override bool Resolve (EmitContext ec)
68 expr = expr.Resolve (ec);
71 if (!CheckContext (ec, loc))
74 in_exc = ec.CurrentBranching.InTryOrCatch (false);
75 Type iterator_type = ec.CurrentIterator.IteratorType;
76 if (expr.Type != iterator_type){
77 expr = Convert.ImplicitConversionRequired (ec, expr, iterator_type, loc);
84 protected override void DoEmit (EmitContext ec)
86 ec.CurrentIterator.MarkYield (ec, expr, in_exc);
90 public class YieldBreak : Statement {
92 public YieldBreak (Location l)
97 public override bool Resolve (EmitContext ec)
99 if (!Yield.CheckContext (ec, loc))
102 ec.CurrentBranching.CurrentUsageVector.Goto ();
106 protected override void DoEmit (EmitContext ec)
108 ec.CurrentIterator.EmitYieldBreak (ec.ig, true);
112 public class Iterator : Class {
113 Block original_block;
117 TypeExpr iterator_type_expr;
122 // The state as we generate the iterator
124 ArrayList resume_labels = new ArrayList ();
128 // Context from the original method
130 TypeContainer container;
133 InternalParameters parameters;
135 static int proxy_count;
137 public void EmitYieldBreak (ILGenerator ig, bool add_return)
139 ig.Emit (OpCodes.Ldarg_0);
140 IntConstant.EmitInt (ig, -1);
141 ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
143 ig.Emit (OpCodes.Ldc_I4_0);
144 ig.Emit (OpCodes.Ret);
148 public void EmitMoveNext (EmitContext ec)
150 ILGenerator ig = ec.ig;
152 Label dispatcher = ig.DefineLabel ();
153 ig.Emit (OpCodes.Br, dispatcher);
154 Label entry_point = ig.DefineLabel ();
155 ig.MarkLabel (entry_point);
156 resume_labels.Add (entry_point);
158 ec.EmitTopBlock (original_block, parameters, Location);
160 EmitYieldBreak (ig, true);
163 // FIXME: Split the switch in blocks that can be consumed
166 ig.MarkLabel (dispatcher);
168 Label [] labels = new Label [resume_labels.Count];
169 resume_labels.CopyTo (labels);
170 ig.Emit (OpCodes.Ldarg_0);
171 ig.Emit (OpCodes.Ldfld, pc_field.FieldBuilder);
172 ig.Emit (OpCodes.Switch, labels);
173 ig.Emit (OpCodes.Ldc_I4_0);
174 ig.Emit (OpCodes.Ret);
178 // Invoked when a local variable declaration needs to be mapped to
179 // a field in our proxy class
181 // Prefixes registered:
182 // v_ for EmitContext.MapVariable
185 public FieldBuilder MapVariable (string pfx, string name, Type t)
187 return TypeBuilder.DefineField (pfx + name, t, FieldAttributes.Public);
191 // Called back from Yield
193 public void MarkYield (EmitContext ec, Expression expr, bool in_exc)
195 ILGenerator ig = ec.ig;
197 // Store the new current
198 ig.Emit (OpCodes.Ldarg_0);
200 ig.Emit (OpCodes.Stfld, current_field.FieldBuilder);
204 ig.Emit (OpCodes.Ldarg_0);
205 IntConstant.EmitInt (ig, pc);
206 ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
209 ig.Emit (OpCodes.Ldc_I4_1);
211 // Find out how to "leave"
212 if (in_exc || !ec.IsLastStatement){
213 Type old = ec.ReturnType;
214 ec.ReturnType = TypeManager.int32_type;
215 ig.Emit (OpCodes.Stloc, ec.TemporaryReturn ());
220 ec.NeedReturnLabel ();
221 ig.Emit (OpCodes.Leave, ec.ReturnLabel);
222 } else if (ec.IsLastStatement){
223 ig.Emit (OpCodes.Ret);
225 ec.NeedReturnLabel ();
226 ig.Emit (OpCodes.Br, ec.ReturnLabel);
229 Label resume_point = ig.DefineLabel ();
230 ig.MarkLabel (resume_point);
231 resume_labels.Add (resume_point);
237 public Iterator (TypeContainer container, string name, Type return_type,
238 Type [] param_types, InternalParameters parameters,
239 int modifiers, Block block, Location loc)
240 : base (container.NamespaceEntry, container,
241 "<Iterator:" + name + (proxy_count++) + ":>",
242 Modifiers.PRIVATE, null, loc)
244 this.container = container;
245 this.return_type = return_type;
246 this.param_types = param_types;
247 this.parameters = parameters;
248 this.original_block = block;
249 this.block = new Block (null);
251 is_static = (modifiers & Modifiers.STATIC) != 0;
253 container.AddIterator (this);
256 public bool Define ()
258 if (!CheckType (return_type)) {
261 "The body of `{0}' cannot be an iterator block " +
262 "because '{1}' is not an iterator interface type",
263 Name, TypeManager.CSharpName (return_type));
267 for (int i = 0; i < parameters.Count; i++){
268 Parameter.Modifier mod = parameters.ParameterModifier (i);
269 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0){
272 "Iterators cannot have ref or out parameters");
277 ArrayList list = new ArrayList ();
279 list.Add (new TypeExpression (
280 TypeManager.ienumerable_type, Location));
281 list.Add (new TypeExpression (TypeManager.ienumerator_type, Location));
282 list.Add (new TypeExpression (TypeManager.idisposable_type, Location));
284 iterator_type_expr = new TypeExpression (iterator_type, Location);
291 // Returns the new block for the method, or null on failure
293 protected override bool DoDefineType ()
296 Define_Constructor ();
303 Define_GetEnumerator ();
313 public Field this_field;
314 public Field[] parameter_fields;
318 int first = is_static ? 0 : 1;
320 ArrayList args = new ArrayList ();
322 Type t = container.TypeBuilder;
323 args.Add (new Argument (
324 new SimpleParameterReference (t, 0, Location),
325 Argument.AType.Expression));
328 for (int i = 0; i < parameters.Count; i++) {
329 Type t = parameters.ParameterType (i);
330 args.Add (new Argument (
331 new SimpleParameterReference (t, first + i, Location),
332 Argument.AType.Expression));
335 Expression new_expr = new New (
336 new TypeExpression (TypeBuilder, Location), args, Location);
338 block.AddStatement (new NoCheckReturn (new_expr, Location));
341 void Define_Fields ()
343 Location loc = Location.Null;
345 pc_field = new Field (
346 TypeManager.system_int32_expr, Modifiers.PRIVATE, "PC",
350 current_field = new Field (
351 iterator_type_expr, Modifiers.PRIVATE, "current",
353 AddField (current_field);
356 this_field = new Field (
357 new TypeExpression (container.TypeBuilder, Location),
358 Modifiers.PRIVATE, "this", null, null, loc);
359 AddField (this_field);
362 parameter_fields = new Field [parameters.Count];
363 for (int i = 0; i < parameters.Count; i++) {
364 string fname = String.Format (
365 "field{0}_{1}", i, parameters.ParameterName (i));
367 parameter_fields [i] = new Field (
368 new TypeExpression (parameters.ParameterType (i), loc),
369 Modifiers.PRIVATE, fname, null, null, loc);
370 AddField (parameter_fields [i]);
374 void Define_Constructor ()
376 Parameters ctor_params;
379 Parameter this_param = new Parameter (
380 new TypeExpression (container.TypeBuilder, Location),
381 "this", Parameter.Modifier.NONE, null);
383 Parameter[] old_fixed = parameters.Parameters.FixedParameters;
384 Parameter[] fixed_params = new Parameter [old_fixed.Length + 1];
385 fixed_params [0] = this_param;
386 old_fixed.CopyTo (fixed_params, 1);
388 ctor_params = new Parameters (
389 fixed_params, parameters.Parameters.ArrayParameter,
392 ctor_params = parameters.Parameters;
394 Constructor ctor = new Constructor (
395 this, Name, Modifiers.PUBLIC, ctor_params,
396 new ConstructorBaseInitializer (
397 null, Parameters.EmptyReadOnlyParameters, Location),
399 AddConstructor (ctor);
401 Block block = ctor.Block = new Block (null);
404 Type t = container.TypeBuilder;
406 Assign assign = new Assign (
407 new FieldExpression (this_field),
408 new SimpleParameterReference (t, 1, Location),
411 block.AddStatement (new StatementExpression (assign, Location));
414 int first = is_static ? 1 : 2;
416 for (int i = 0; i < parameters.Count; i++) {
417 Type t = parameters.ParameterType (i);
419 Assign assign = new Assign (
420 new FieldExpression (parameter_fields [i]),
421 new SimpleParameterReference (t, first + i, Location),
424 block.AddStatement (new StatementExpression (assign, Location));
428 Statement Create_ThrowInvalidOperation ()
430 TypeExpr ex_type = new TypeExpression (
431 TypeManager.invalid_operation_exception_type, Location);
433 return new Throw (new New (ex_type, null, Location), Location);
436 Statement Create_ThrowNotSupported ()
438 TypeExpr ex_type = new TypeExpression (
439 TypeManager.not_supported_exception_type, Location);
441 return new Throw (new New (ex_type, null, Location), Location);
444 void Define_Current ()
446 Block get_block = new Block (null);
448 get_block.AddStatement (new If (
450 Binary.Operator.LessThanOrEqual,
451 new FieldExpression (pc_field),
452 new IntLiteral (0), Location),
453 Create_ThrowInvalidOperation (),
455 new FieldExpression (current_field), Location),
458 Accessor getter = new Accessor (get_block, null);
460 Property current = new Property (
461 this, iterator_type_expr, Modifiers.PUBLIC,
462 false, "Current", null, getter, null, Location);
463 AddProperty (current);
466 void Define_MoveNext ()
468 Method move_next = new Method (
469 this, TypeManager.system_boolean_expr,
470 Modifiers.PUBLIC, false, "MoveNext",
471 Parameters.EmptyReadOnlyParameters, null,
473 AddMethod (move_next);
475 Block block = move_next.Block = new Block (null);
477 MoveNextMethod inline = new MoveNextMethod (this, Location);
478 block.AddStatement (inline);
481 void Define_GetEnumerator ()
483 Method get_enumerator = new Method (
485 new TypeExpression (TypeManager.ienumerator_type, Location),
486 Modifiers.PUBLIC, false, "GetEnumerator",
487 Parameters.EmptyReadOnlyParameters, null,
489 AddMethod (get_enumerator);
491 get_enumerator.Block = new Block (null);
493 This the_this = new This (block, Location);
494 get_enumerator.Block.AddStatement (new Return (the_this, Location));
497 protected class SimpleParameterReference : Expression
501 public SimpleParameterReference (Type type, int idx, Location loc)
506 eclass = ExprClass.Variable;
509 public override Expression DoResolve (EmitContext ec)
514 public override void Emit (EmitContext ec)
516 ParameterReference.EmitLdArg (ec.ig, idx);
520 protected class FieldExpression : Expression
524 public FieldExpression (Field field)
529 public override Expression DoResolve (EmitContext ec)
531 FieldExpr fexpr = new FieldExpr (field.FieldBuilder, loc);
532 fexpr.InstanceExpression = ec.GetThis (loc);
533 return fexpr.Resolve (ec);
536 public override void Emit (EmitContext ec)
538 throw new InvalidOperationException ();
542 public class MoveNextMethod : Statement {
545 public MoveNextMethod (Iterator iterator, Location loc)
548 this.iterator = iterator;
551 public override bool Resolve (EmitContext ec)
553 ec.CurrentBranching.CurrentUsageVector.Return ();
557 protected override void DoEmit (EmitContext ec)
559 int code_flags = Modifiers.METHOD_YIELDS;
560 if (iterator.is_static)
561 code_flags |= Modifiers.STATIC;
563 EmitContext new_ec = new EmitContext (
564 iterator, loc, ec.ig, iterator.return_type,
567 new_ec.CurrentIterator = iterator;
569 iterator.EmitMoveNext (new_ec);
573 protected class DoYieldBreak : Statement
578 public DoYieldBreak (Iterator iterator, bool add_return,
581 this.iterator = iterator;
582 this.add_return = add_return;
586 public override bool Resolve (EmitContext ec)
589 ec.CurrentBranching.CurrentUsageVector.Return ();
593 protected override void DoEmit (EmitContext ec)
595 iterator.EmitYieldBreak (ec.ig, add_return);
601 Method reset = new Method (
602 this, TypeManager.system_void_expr, Modifiers.PUBLIC,
603 false, "Reset", Parameters.EmptyReadOnlyParameters,
607 reset.Block = new Block (null);
608 reset.Block.AddStatement (Create_ThrowNotSupported ());
611 void Define_Dispose ()
613 Method dispose = new Method (
614 this, TypeManager.system_void_expr, Modifiers.PUBLIC,
615 false, "Dispose", Parameters.EmptyReadOnlyParameters,
619 dispose.Block = new Block (null);
620 dispose.Block.AddStatement (new DoYieldBreak (this, false, Location));
621 dispose.Block.AddStatement (new Return (null, Location));
625 get { return block; }
628 public Type IteratorType {
629 get { return iterator_type; }
633 // This return statement tricks return into not flagging an error for being
634 // used in a Yields method
636 class NoCheckReturn : Return {
637 public NoCheckReturn (Expression expr, Location loc) : base (expr, loc)
641 public override bool Resolve (EmitContext ec)
643 ec.InIterator = false;
644 bool ret_val = base.Resolve (ec);
645 ec.InIterator = true;
651 bool CheckType (Type t)
653 if (t == TypeManager.ienumerable_type) {
654 iterator_type = TypeManager.object_type;
655 is_enumerable = true;
657 } else if (t == TypeManager.ienumerator_type) {
658 iterator_type = TypeManager.object_type;
659 is_enumerable = false;