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 {
34 ArrayList finally_blocks;
36 public Yield (Expression expr, Location l)
42 public static bool CheckContext (EmitContext ec, Location loc)
45 Report.Error (1625, loc, "Cannot yield in the body of a " +
51 Report.Error (1629, loc, "Unsafe code may not appear in iterators");
55 Report.Error (1631, loc, "Cannot yield a value in the body of a catch clause");
59 AnonymousContainer am = ec.CurrentAnonymousMethod;
60 if ((am != null) && !am.IsIterator){
61 Report.Error (1621, loc, "The yield statement cannot be used inside anonymous method blocks");
65 if (ec.CurrentBranching.InTryWithCatch ()) {
66 Report.Error (1626, loc, "Cannot yield a value in the body of a " +
67 "try block with a catch clause");
73 public override bool Resolve (EmitContext ec)
75 expr = expr.Resolve (ec);
79 if (!CheckContext (ec, loc))
82 Iterator iterator = ec.CurrentIterator;
84 if (expr.Type != iterator.IteratorType){
85 expr = Convert.ImplicitConversionRequired (
86 ec, expr, iterator.IteratorType, loc);
91 ec.CurrentBranching.StealFinallyClauses (ref finally_blocks);
95 protected override void DoEmit (EmitContext ec)
97 ec.CurrentIterator.MarkYield (ec, expr, finally_blocks);
101 public class YieldBreak : Statement {
103 public YieldBreak (Location l)
108 public override bool Resolve (EmitContext ec)
110 if (!Yield.CheckContext (ec, loc))
113 ec.CurrentBranching.CurrentUsageVector.Goto ();
117 protected override void DoEmit (EmitContext ec)
119 ec.CurrentIterator.EmitYieldBreak (ec.ig);
123 public class Iterator : Class {
124 protected ToplevelBlock original_block;
125 protected ToplevelBlock block;
127 Type original_iterator_type;
128 TypeExpr iterator_type_expr;
130 public readonly bool IsStatic;
133 // The state as we generate the iterator
135 Label move_next_ok, move_next_error;
136 ArrayList resume_points = new ArrayList ();
140 // Context from the original method
142 GenericMethod generic_method;
143 TypeContainer container;
144 TypeExpr current_type;
146 InternalParameters parameters;
147 InternalParameters original_parameters;
148 IMethodData orig_method;
150 MethodInfo dispose_method;
151 MoveNextMethod move_next_method;
155 Expression enumerator_type;
156 Expression enumerable_type;
157 Expression generic_enumerator_type;
158 Expression generic_enumerable_type;
159 TypeArguments generic_args;
161 protected enum State {
167 static int proxy_count;
169 public void EmitYieldBreak (ILGenerator ig)
171 ig.Emit (OpCodes.Ldarg_0);
172 IntConstant.EmitInt (ig, (int) State.After);
173 ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
174 ig.Emit (OpCodes.Br, move_next_error);
177 public void EmitMoveNext (EmitContext ec)
179 ILGenerator ig = ec.ig;
181 move_next_ok = ig.DefineLabel ();
182 move_next_error = ig.DefineLabel ();
184 LocalBuilder retval = ec.GetTemporaryLocal (TypeManager.int32_type);
186 ig.BeginExceptionBlock ();
188 Label dispatcher = ig.DefineLabel ();
189 ig.Emit (OpCodes.Br, dispatcher);
191 ResumePoint entry_point = new ResumePoint (null);
192 resume_points.Add (entry_point);
193 entry_point.Define (ig);
195 ec.EmitTopBlock (orig_method, original_block, parameters);
199 ig.MarkLabel (dispatcher);
201 Label [] labels = new Label [resume_points.Count];
202 for (int i = 0; i < labels.Length; i++)
203 labels [i] = ((ResumePoint) resume_points [i]).Label;
205 ig.Emit (OpCodes.Ldarg_0);
206 ig.Emit (OpCodes.Ldfld, pc_field.FieldBuilder);
207 ig.Emit (OpCodes.Switch, labels);
209 Label end = ig.DefineLabel ();
211 ig.MarkLabel (move_next_error);
212 ig.Emit (OpCodes.Ldc_I4_0);
213 ig.Emit (OpCodes.Stloc, retval);
214 ig.Emit (OpCodes.Leave, end);
216 ig.MarkLabel (move_next_ok);
217 ig.Emit (OpCodes.Ldc_I4_1);
218 ig.Emit (OpCodes.Stloc, retval);
219 ig.Emit (OpCodes.Leave, end);
221 ig.BeginFaultBlock ();
223 ig.Emit (OpCodes.Ldarg_0);
224 ig.Emit (OpCodes.Callvirt, dispose_method);
226 ig.EndExceptionBlock ();
229 ig.Emit (OpCodes.Ldloc, retval);
230 ig.Emit (OpCodes.Ret);
233 public void EmitDispose (EmitContext ec)
235 ILGenerator ig = ec.ig;
237 Label end = ig.DefineLabel ();
238 Label dispatcher = ig.DefineLabel ();
239 ig.Emit (OpCodes.Br, dispatcher);
241 Label [] labels = new Label [resume_points.Count];
242 for (int i = 0; i < labels.Length; i++) {
243 ResumePoint point = (ResumePoint) resume_points [i];
245 if (point.FinallyBlocks == null) {
250 labels [i] = ig.DefineLabel ();
251 ig.MarkLabel (labels [i]);
253 ig.BeginExceptionBlock ();
254 ig.BeginFinallyBlock ();
256 foreach (ExceptionStatement stmt in point.FinallyBlocks) {
258 stmt.EmitFinally (ec);
261 ig.EndExceptionBlock ();
262 ig.Emit (OpCodes.Br, end);
265 ig.MarkLabel (dispatcher);
266 ig.Emit (OpCodes.Ldarg_0);
267 ig.Emit (OpCodes.Ldfld, pc_field.FieldBuilder);
268 ig.Emit (OpCodes.Switch, labels);
270 ig.Emit (OpCodes.Ldarg_0);
271 IntConstant.EmitInt (ig, (int) State.After);
272 ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
277 protected class ResumePoint
280 public readonly ExceptionStatement[] FinallyBlocks;
282 public ResumePoint (ArrayList list)
285 FinallyBlocks = new ExceptionStatement [list.Count];
286 list.CopyTo (FinallyBlocks, 0);
290 public void Define (ILGenerator ig)
292 Label = ig.DefineLabel ();
293 ig.MarkLabel (Label);
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, GenericMethod generic, Location loc)
341 int pos = name.LastIndexOf ('.');
343 name = name.Substring (pos + 1);
345 string proxy_name = "<" + name + ">__" + (proxy_count++);
347 if (generic != null) {
348 TypeArguments args = new TypeArguments (loc);
349 foreach (TypeParameter tparam in generic.CurrentTypeParameters)
350 args.Add (new SimpleName (tparam.Name, loc));
351 return new MemberName (proxy_name, args, loc);
353 return new MemberName (proxy_name, loc);
359 public Iterator (IMethodData m_container, TypeContainer container, GenericMethod generic,
360 InternalParameters parameters, int modifiers)
361 : base (container.NamespaceEntry, container,
362 MakeProxyName (m_container.MethodName.Name, generic, m_container.Location),
363 (modifiers & Modifiers.UNSAFE) | Modifiers.PRIVATE, null)
365 this.orig_method = m_container;
367 this.generic_method = generic;
368 this.container = container;
369 this.original_parameters = parameters;
370 this.original_block = orig_method.Block;
371 this.block = new ToplevelBlock (orig_method.Block, parameters.Parameters, orig_method.Location);
373 if (generic != null) {
374 ArrayList constraints = new ArrayList ();
375 foreach (TypeParameter tparam in generic.TypeParameters)
376 constraints.Add (tparam.Constraints);
378 SetParameterInfo (constraints);
381 IsStatic = (modifiers & Modifiers.STATIC) != 0;
384 public AnonymousContainer Host {
385 get { return move_next_method; }
388 public bool DefineIterator ()
390 ec = new EmitContext (this, Location, null, null, ModFlags);
391 ec.CurrentAnonymousMethod = move_next_method;
392 ec.InIterator = true;
395 Report.Error (1624, Location,
396 "The body of `{0}' cannot be an iterator block because `{1}' is not an iterator interface type",
397 orig_method.GetSignatureForError (), TypeManager.CSharpName (orig_method.ReturnType));
401 for (int i = 0; i < original_parameters.Count; i++){
402 Parameter.Modifier mod = original_parameters.ParameterModifier (i);
403 if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0){
406 "Iterators cannot have ref or out parameters");
410 if ((mod & Parameter.Modifier.ARGLIST) != 0) {
411 Report.Error (1636, Location, "__arglist is not allowed in parameter list of iterators");
415 if (original_parameters.ParameterType (i).IsPointer) {
416 Report.Error (1637, Location, "Iterators cannot have unsafe parameters or yield types");
421 if (container.CurrentType != null)
422 this_type = container.CurrentType;
424 this_type = container.TypeBuilder;
426 container.AddIterator (this);
428 orig_method.Block = block;
432 MethodInfo FetchMethodDispose ()
434 MemberList dispose_list;
436 dispose_list = FindMembers (
438 MemberTypes.Method, BindingFlags.Public | BindingFlags.Instance,
439 Type.FilterName, "Dispose");
441 if (dispose_list.Count != 1)
442 throw new InternalErrorException ("Cannot find Dipose() method.");
444 return (MethodInfo) dispose_list [0];
447 protected override bool DoDefineMembers ()
449 ec.InIterator = true;
450 ec.CurrentAnonymousMethod = move_next_method;
451 ec.capture_context = cc;
453 if (!base.DoDefineMembers ())
456 dispose_method = FetchMethodDispose ();
457 if (dispose_method == null)
463 public override bool Define ()
468 ec.InIterator = true;
469 ec.CurrentAnonymousMethod = move_next_method;
470 ec.capture_context = cc;
472 ec.TypeContainer = ec.TypeContainer.Parent;
474 if (ec.TypeContainer.CurrentType != null)
475 ec.ContainerType = ec.TypeContainer.CurrentType;
477 ec.ContainerType = ec.TypeContainer.TypeBuilder;
479 ec.ig = move_next_method.method.MethodBuilder.GetILGenerator ();
486 if (!ec.ResolveTopBlock (null, original_block, parameters, orig_method, out unreachable))
489 if (!ec.ResolveTopBlock (null, block, parameters, orig_method, out unreachable))
492 original_block.CompleteContexts ();
494 cc.EmitAnonymousHelperClasses (ec);
499 TypeExpr InflateType (Type it)
501 if (generic_method == null)
502 return new TypeExpression (it, Location);
504 if (it.IsGenericParameter && (it.DeclaringMethod != null)) {
505 int pos = it.GenericParameterPosition;
506 it = CurrentTypeParameters [pos].Type;
507 } else if (it.IsGenericInstance) {
508 Type[] args = it.GetGenericArguments ();
510 TypeArguments inflated = new TypeArguments (Location);
511 foreach (Type t in args)
512 inflated.Add (InflateType (t));
514 return new ConstructedType (it, inflated, Location);
515 } else if (it.IsArray) {
516 TypeExpr et_expr = InflateType (it.GetElementType ());
517 int rank = it.GetArrayRank ();
519 Type et = et_expr.ResolveAsTypeTerminal (ec).Type;
520 it = et.MakeArrayType (rank);
523 return new TypeExpression (it, Location);
526 Parameter InflateParameter (Parameter param)
528 TypeExpr te = InflateType (param.ParameterType);
529 return new Parameter (
530 te, param.Name, param.ModFlags, param.OptAttributes, param.Location);
533 InternalParameters InflateParameters (Parameters parameters, EmitContext ec)
535 Parameter[] fixed_params = null;
536 if (parameters.FixedParameters != null) {
537 fixed_params = new Parameter [parameters.FixedParameters.Length];
538 for (int i = 0; i < fixed_params.Length; i++)
539 fixed_params [i] = InflateParameter (parameters.FixedParameters [i]);
542 Parameters new_params;
543 if (parameters.ArrayParameter != null) {
544 Parameter array_param = InflateParameter (parameters.ArrayParameter);
545 new_params = new Parameters (fixed_params, array_param);
547 new_params = new Parameters (fixed_params, parameters.HasArglist);
549 Type [] types = new_params.GetParameterInfo (ec);
550 return new InternalParameters (types, new_params);
553 protected override TypeExpr [] GetClassBases (out TypeExpr base_class)
555 iterator_type_expr = InflateType (original_iterator_type);
557 generic_args = new TypeArguments (Location);
558 generic_args.Add (iterator_type_expr);
560 ArrayList list = new ArrayList ();
562 enumerable_type = new TypeExpression (
563 TypeManager.ienumerable_type, Location);
564 list.Add (enumerable_type);
566 generic_enumerable_type = new ConstructedType (
567 TypeManager.generic_ienumerable_type,
568 generic_args, Location);
569 list.Add (generic_enumerable_type);
572 enumerator_type = new TypeExpression (
573 TypeManager.ienumerator_type, Location);
574 list.Add (enumerator_type);
576 list.Add (new TypeExpression (TypeManager.idisposable_type, Location));
578 generic_enumerator_type = new ConstructedType (
579 TypeManager.generic_ienumerator_type,
580 generic_args, Location);
581 list.Add (generic_enumerator_type);
585 return base.GetClassBases (out base_class);
589 // Returns the new block for the method, or null on failure
591 protected override bool DefineNestedTypes ()
593 if (CurrentType != null)
594 current_type = new TypeExpression (CurrentType, Location);
596 current_type = new TypeExpression (TypeBuilder, Location);
598 parameters = InflateParameters (original_parameters.Parameters, ec);
601 Define_Current (false);
602 Define_Current (true);
607 Define_Constructor ();
612 Define_GetEnumerator (false);
613 Define_GetEnumerator (true);
616 return base.DefineNestedTypes ();
625 original_block.SetHaveAnonymousMethods (Location, move_next_method);
626 block.SetHaveAnonymousMethods (Location, move_next_method);
628 cc = original_block.CaptureContext;
630 int first = IsStatic ? 0 : 1;
632 ArrayList args = new ArrayList ();
635 args.Add (new Argument (
636 new ThisParameterReference (t, Location)));
637 cc.CaptureThis (move_next_method);
640 args.Add (new Argument (new BoolLiteral (false)));
642 for (int i = 0; i < parameters.Count; i++) {
643 Type t = original_parameters.ParameterType (i);
644 Type inflated = parameters.ParameterType (i);
645 string name = parameters.ParameterName (i);
647 args.Add (new Argument (
648 new SimpleParameterReference (t, first + i, Location)));
650 cc.AddParameterToContext (move_next_method, name, inflated, first + i);
654 if (generic_method != null) {
655 TypeArguments new_args = new TypeArguments (Location);
656 if (Parent.IsGeneric) {
657 foreach (TypeParameter tparam in Parent.TypeParameters)
658 new_args.Add (new TypeParameterExpr (tparam, Location));
660 foreach (TypeParameter tparam in generic_method.TypeParameters)
661 new_args.Add (new TypeParameterExpr (tparam, Location));
662 ConstructedType ct = new ConstructedType (CurrentType, new_args, Location);
663 proxy_type = ct.ResolveAsTypeTerminal (ec);
665 proxy_type = current_type;
667 Expression new_expr = new New (proxy_type, args, Location);
668 block.AddStatement (new NoCheckReturn (new_expr, Location));
671 void Define_Fields ()
673 pc_field = new Field (
674 this, TypeManager.system_int32_expr, Modifiers.PRIVATE, "$PC",
675 null, null, Location);
678 current_field = new Field (
679 this, iterator_type_expr, Modifiers.PRIVATE, "$current",
680 null, null, Location);
681 AddField (current_field);
684 void Define_Constructor ()
686 Parameters ctor_params;
688 ArrayList list = new ArrayList ();
691 list.Add (new Parameter (
692 new TypeExpression (this_type, Location),
693 "this", Parameter.Modifier.NONE,
695 list.Add (new Parameter (
696 TypeManager.system_boolean_expr, "initialized",
697 Parameter.Modifier.NONE, null, Location));
699 Parameter[] old_fixed = parameters.Parameters.FixedParameters;
700 if (old_fixed != null)
701 list.AddRange (old_fixed);
703 Parameter[] fixed_params = new Parameter [list.Count];
704 list.CopyTo (fixed_params);
706 ctor_params = new Parameters (fixed_params, parameters.Parameters.ArrayParameter);
708 ctor = new Constructor (
709 this, MemberName.Name, Modifiers.PUBLIC, ctor_params,
710 new ConstructorBaseInitializer (null, Location),
712 AddConstructor (ctor);
714 ctor.Block = new ToplevelBlock (block, parameters.Parameters, Location);
716 int first = IsStatic ? 2 : 3;
718 State initial = is_enumerable ? State.Uninitialized : State.Running;
719 ctor.Block.AddStatement (new SetState (this, initial, Location));
721 ctor.Block.AddStatement (new If (
722 new SimpleParameterReference (
723 TypeManager.bool_type, first - 1, Location),
724 new SetState (this, State.Running, Location),
727 ctor.Block.AddStatement (new InitScope (this, Location));
730 Statement Create_ThrowInvalidOperation ()
732 TypeExpr ex_type = new TypeExpression (
733 TypeManager.invalid_operation_exception_type, Location);
735 return new Throw (new New (ex_type, null, Location), Location);
738 Statement Create_ThrowNotSupported ()
740 TypeExpr ex_type = new TypeExpression (
741 TypeManager.not_supported_exception_type, Location);
743 return new Throw (new New (ex_type, null, Location), Location);
746 void Define_Current (bool is_generic)
751 left = new MemberName (
752 "System.Collections.Generic.IEnumerator",
753 generic_args, Location);
754 type = iterator_type_expr;
756 left = new MemberName ("System.Collections.IEnumerator", Location);
757 type = TypeManager.system_object_expr;
760 MemberName name = new MemberName (left, "Current", null, Location);
762 ToplevelBlock get_block = new ToplevelBlock (
763 block, parameters.Parameters, Location);
765 get_block.AddStatement (new If (
767 Binary.Operator.LessThanOrEqual,
768 new FieldExpression (this, pc_field),
769 new IntLiteral ((int) State.Running)),
770 Create_ThrowInvalidOperation (),
772 new FieldExpression (this, current_field), Location),
775 Accessor getter = new Accessor (get_block, 0, null, Location);
777 Property current = new Property (
778 this, type, 0, false, name, null, getter, null);
779 AddProperty (current);
782 void Define_MoveNext ()
784 move_next_method = new MoveNextMethod (this, Location);
786 original_block.ReParent (block, move_next_method);
788 move_next_method.CreateMethod (ec);
790 AddMethod (move_next_method.method);
793 void Define_GetEnumerator (bool is_generic)
798 left = new MemberName (
799 "System.Collections.Generic.IEnumerable",
800 generic_args, Location);
801 type = generic_enumerator_type;
803 left = new MemberName ("System.Collections.IEnumerable", Location);
804 type = enumerator_type;
807 MemberName name = new MemberName (left, "GetEnumerator", Location);
809 Method get_enumerator = new Method (
810 this, null, type, 0, false, name,
811 Parameters.EmptyReadOnlyParameters, null);
812 AddMethod (get_enumerator);
814 get_enumerator.Block = new ToplevelBlock (
815 block, parameters.Parameters, Location);
817 get_enumerator.Block.SetHaveAnonymousMethods (Location, move_next_method);
819 Expression ce = new MemberAccess (
820 new SimpleName ("System.Threading.Interlocked", Location),
821 "CompareExchange", Location);
823 Expression pc = new FieldExpression (this, pc_field);
824 Expression before = new IntLiteral ((int) State.Running);
825 Expression uninitialized = new IntLiteral ((int) State.Uninitialized);
827 ArrayList args = new ArrayList ();
828 args.Add (new Argument (pc, Argument.AType.Ref));
829 args.Add (new Argument (before, Argument.AType.Expression));
830 args.Add (new Argument (uninitialized, Argument.AType.Expression));
832 get_enumerator.Block.AddStatement (new If (
834 Binary.Operator.Equality,
835 new Invocation (ce, args),
837 new Return (new ThisParameterReference (type.Type, Location),
841 args = new ArrayList ();
843 args.Add (new Argument (new CapturedThisReference (this, Location)));
846 args.Add (new Argument (new BoolLiteral (true)));
848 for (int i = 0; i < parameters.Count; i++) {
849 Expression cp = new CapturedParameterReference (
850 this, parameters.ParameterType (i),
851 parameters.ParameterName (i), Location);
852 args.Add (new Argument (cp));
855 Expression new_expr = new New (current_type, args, Location);
856 get_enumerator.Block.AddStatement (new Return (new_expr, Location));
859 protected class SimpleParameterReference : Expression
863 public SimpleParameterReference (Type type, int idx, Location loc)
868 eclass = ExprClass.Variable;
871 public override Expression DoResolve (EmitContext ec)
876 public override void Emit (EmitContext ec)
881 protected virtual void DoEmit (EmitContext ec)
883 ParameterReference.EmitLdArg (ec.ig, idx);
887 protected class ThisParameterReference : SimpleParameterReference, IMemoryLocation
889 public ThisParameterReference (Type type, Location loc)
890 : base (type, 0, loc)
893 protected override void DoEmit (EmitContext ec)
896 if (ec.TypeContainer is Struct)
897 ec.ig.Emit (OpCodes.Ldobj, type);
900 public void AddressOf (EmitContext ec, AddressOp mode)
902 if (ec.TypeContainer is Struct)
903 ec.ig.Emit (OpCodes.Ldarga, 0);
905 ec.ig.Emit (OpCodes.Ldarg, 0);
909 protected class CapturedParameterReference : Expression
914 public CapturedParameterReference (Iterator iterator, Type type,
915 string name, Location loc)
917 this.iterator = iterator;
921 eclass = ExprClass.Variable;
924 public override Expression DoResolve (EmitContext ec)
929 public override void Emit (EmitContext ec)
931 ec.CurrentAnonymousMethod = iterator.move_next_method;
933 iterator.cc.EmitParameter (ec, name);
937 protected class CapturedThisReference : Expression
939 public CapturedThisReference (Iterator iterator, Location loc)
942 this.type = iterator.this_type;
943 eclass = ExprClass.Variable;
946 public override Expression DoResolve (EmitContext ec)
951 public override void Emit (EmitContext ec)
957 protected class FieldExpression : Expression
962 public FieldExpression (Iterator iterator, Field field)
964 this.iterator = iterator;
966 this.loc = iterator.Location;
969 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
971 FieldExpr fexpr = new FieldExpr (field.FieldBuilder, loc);
972 fexpr.InstanceExpression = new ThisParameterReference (
973 iterator.this_type, loc);
974 return fexpr.ResolveLValue (ec, right_side, loc);
977 public override Expression DoResolve (EmitContext ec)
979 FieldExpr fexpr = new FieldExpr (field.FieldBuilder, loc);
980 fexpr.InstanceExpression = new ThisParameterReference (
981 iterator.this_type, loc);
982 return fexpr.Resolve (ec);
985 public override void Emit (EmitContext ec)
987 throw new InvalidOperationException ();
991 protected class MoveNextMethod : AnonymousContainer
995 public MoveNextMethod (Iterator iterator, Location loc)
996 : base (iterator.parameters.Parameters, iterator.original_block, loc)
998 this.iterator = iterator;
1001 protected override bool CreateMethodHost (EmitContext ec)
1003 method = new Method (
1004 iterator, null, TypeManager.system_boolean_expr,
1005 Modifiers.PUBLIC, false, new MemberName ("MoveNext", loc),
1006 Parameters.EmptyReadOnlyParameters, null);
1008 method.Block = Block;
1010 MoveNextStatement inline = new MoveNextStatement (iterator, loc);
1011 Block.AddStatement (inline);
1016 public bool CreateMethod (EmitContext ec)
1018 return CreateMethodHost (ec);
1021 public override Iterator Iterator {
1022 get { return iterator; }
1025 public override bool IsIterator {
1026 get { return true; }
1029 public override void CreateScopeType (EmitContext ec, ScopeInfo scope)
1031 scope.ScopeTypeBuilder = iterator.TypeBuilder;
1032 scope.ScopeConstructor = iterator.ctor.ConstructorBuilder;
1035 public override void Emit (EmitContext ec)
1037 throw new InternalErrorException ();
1041 protected class MoveNextStatement : Statement {
1044 public MoveNextStatement (Iterator iterator, Location loc)
1047 this.iterator = iterator;
1050 public override bool Resolve (EmitContext ec)
1055 protected override void DoEmit (EmitContext ec)
1057 ec.CurrentAnonymousMethod = iterator.move_next_method;
1058 ec.InIterator = true;
1060 iterator.EmitMoveNext (ec);
1064 protected class DisposeMethod : Statement {
1067 public DisposeMethod (Iterator iterator, Location loc)
1070 this.iterator = iterator;
1073 public override bool Resolve (EmitContext ec)
1078 protected override void DoEmit (EmitContext ec)
1080 iterator.EmitDispose (ec);
1084 protected class StatementList : Statement {
1085 ArrayList statements;
1087 public StatementList (Location loc)
1090 statements = new ArrayList ();
1093 public void Add (Statement statement)
1095 statements.Add (statement);
1098 public override bool Resolve (EmitContext ec)
1100 foreach (Statement stmt in statements) {
1101 if (!stmt.Resolve (ec))
1108 protected override void DoEmit (EmitContext ec)
1110 foreach (Statement stmt in statements)
1115 protected class SetState : Statement
1120 public SetState (Iterator iterator, State state, Location loc)
1122 this.iterator = iterator;
1127 public override bool Resolve (EmitContext ec)
1132 protected override void DoEmit (EmitContext ec)
1134 ec.ig.Emit (OpCodes.Ldarg_0);
1135 IntConstant.EmitInt (ec.ig, (int) state);
1136 ec.ig.Emit (OpCodes.Stfld, iterator.pc_field.FieldBuilder);
1140 protected class InitScope : Statement
1144 public InitScope (Iterator iterator, Location loc)
1146 this.iterator = iterator;
1150 public override bool Resolve (EmitContext ec)
1155 protected override void DoEmit (EmitContext ec)
1157 iterator.cc.EmitInitScope (ec);
1161 void Define_Reset ()
1163 Method reset = new Method (
1164 this, null, TypeManager.system_void_expr, Modifiers.PUBLIC,
1165 false, new MemberName ("Reset", Location),
1166 Parameters.EmptyReadOnlyParameters, null);
1169 reset.Block = new ToplevelBlock (Location);
1170 reset.Block = new ToplevelBlock (block, parameters.Parameters, Location);
1171 reset.Block.SetHaveAnonymousMethods (Location, move_next_method);
1173 reset.Block.AddStatement (Create_ThrowNotSupported ());
1176 void Define_Dispose ()
1178 dispose = new Method (
1179 this, null, TypeManager.system_void_expr, Modifiers.PUBLIC,
1180 false, new MemberName ("Dispose", Location),
1181 Parameters.EmptyReadOnlyParameters, null);
1182 AddMethod (dispose);
1184 dispose.Block = new ToplevelBlock (block, parameters.Parameters, Location);
1185 dispose.Block.SetHaveAnonymousMethods (Location, move_next_method);
1187 dispose.Block.AddStatement (new DisposeMethod (this, Location));
1190 public Type IteratorType {
1191 get { return iterator_type_expr.Type; }
1195 // This return statement tricks return into not flagging an error for being
1196 // used in a Yields method
1198 class NoCheckReturn : Statement {
1199 public Expression Expr;
1201 public NoCheckReturn (Expression expr, Location l)
1207 public override bool Resolve (EmitContext ec)
1209 Expr = Expr.Resolve (ec);
1213 ec.CurrentBranching.CurrentUsageVector.Return ();
1218 protected override void DoEmit (EmitContext ec)
1221 ec.ig.Emit (OpCodes.Ret);
1227 Type ret = orig_method.ReturnType;
1229 if (ret == TypeManager.ienumerable_type) {
1230 original_iterator_type = TypeManager.object_type;
1231 is_enumerable = true;
1234 if (ret == TypeManager.ienumerator_type) {
1235 original_iterator_type = TypeManager.object_type;
1236 is_enumerable = false;
1240 if (!ret.IsGenericInstance)
1243 Type[] args = TypeManager.GetTypeArguments (ret);
1244 if (args.Length != 1)
1247 Type gt = ret.GetGenericTypeDefinition ();
1248 if (gt == TypeManager.generic_ienumerable_type) {
1249 original_iterator_type = args [0];
1250 is_enumerable = true;
1252 } else if (gt == TypeManager.generic_ienumerator_type) {
1253 original_iterator_type = args [0];
1254 is_enumerable = false;