2 // iterators.cs: Support for implementing iterators
5 // Miguel de Icaza (miguel@ximian.com)
6 // Marek Safar (marek.safar@gmail.com)
8 // Dual licensed under the terms of the MIT X11 or GNU GPL
9 // Copyright 2003 Ximian, Inc.
10 // Copyright 2003-2008 Novell, Inc.
14 // Flow analysis for Yield.
18 using System.Collections.Generic;
19 using System.Reflection;
20 using System.Reflection.Emit;
22 namespace Mono.CSharp {
24 public class Yield : ResumableStatement {
30 public Yield (Expression expr, Location l)
36 public static bool CheckContext (ResolveContext ec, Location loc)
38 if (!ec.CurrentAnonymousMethod.IsIterator) {
39 ec.Report.Error (1621, loc,
40 "The yield statement cannot be used inside " +
41 "anonymous method blocks");
48 public override bool Resolve (BlockContext ec)
50 expr = expr.Resolve (ec);
54 Report.Debug (64, "RESOLVE YIELD #1", this, ec, expr, expr.GetType (),
55 ec.CurrentAnonymousMethod, ec.CurrentIterator);
57 if (!CheckContext (ec, loc))
60 iterator = ec.CurrentIterator;
61 if (expr.Type != iterator.OriginalIteratorType) {
62 expr = Convert.ImplicitConversionRequired (
63 ec, expr, iterator.OriginalIteratorType, loc);
68 if (!ec.CurrentBranching.CurrentUsageVector.IsUnreachable)
69 unwind_protect = ec.CurrentBranching.AddResumePoint (this, loc, out resume_pc);
74 protected override void DoEmit (EmitContext ec)
76 iterator.MarkYield (ec, expr, resume_pc, unwind_protect, resume_point);
79 protected override void CloneTo (CloneContext clonectx, Statement t)
81 Yield target = (Yield) t;
83 target.expr = expr.Clone (clonectx);
87 public class YieldBreak : ExitStatement
91 public YieldBreak (Location l)
96 public override void Error_FinallyClause (Report Report)
98 Report.Error (1625, loc, "Cannot yield in the body of a finally clause");
101 protected override void CloneTo (CloneContext clonectx, Statement target)
103 throw new NotSupportedException ();
106 protected override bool DoResolve (BlockContext ec)
108 iterator = ec.CurrentIterator;
109 return Yield.CheckContext (ec, loc);
112 protected override void DoEmit (EmitContext ec)
114 iterator.EmitYieldBreak (ec, unwind_protect);
118 public class IteratorStorey : AnonymousMethodStorey
120 class GetEnumeratorMethod : IteratorMethod
122 sealed class GetEnumeratorStatement : Statement
125 IteratorMethod host_method;
127 Expression new_storey;
129 public GetEnumeratorStatement (IteratorStorey host, IteratorMethod host_method)
132 this.host_method = host_method;
133 loc = host_method.Location;
136 protected override void CloneTo (CloneContext clonectx, Statement target)
138 throw new NotSupportedException ();
141 public override bool Resolve (BlockContext ec)
143 TypeExpression storey_type_expr = new TypeExpression (host.Definition, loc);
144 List<Expression> init = null;
145 if (host.hoisted_this != null) {
146 init = new List<Expression> (host.hoisted_params == null ? 1 : host.HoistedParameters.Count + 1);
147 HoistedThis ht = host.hoisted_this;
148 FieldExpr from = new FieldExpr (ht.Field, loc);
149 from.InstanceExpression = CompilerGeneratedThis.Instance;
150 init.Add (new ElementInitializer (ht.Field.Name, from, loc));
153 if (host.hoisted_params != null) {
155 init = new List<Expression> (host.HoistedParameters.Count);
157 for (int i = 0; i < host.hoisted_params.Count; ++i) {
158 HoistedParameter hp = (HoistedParameter) host.hoisted_params [i];
159 HoistedParameter hp_cp = (HoistedParameter) host.hoisted_params_copy [i];
161 FieldExpr from = new FieldExpr (hp_cp.Field, loc);
162 from.InstanceExpression = CompilerGeneratedThis.Instance;
164 init.Add (new ElementInitializer (hp.Field.Name, from, loc));
169 new_storey = new NewInitialize (storey_type_expr, null,
170 new CollectionOrObjectInitializers (init, loc), loc);
172 new_storey = new New (storey_type_expr, null, loc);
175 new_storey = new_storey.Resolve (ec);
176 if (new_storey != null)
177 new_storey = Convert.ImplicitConversionRequired (ec, new_storey, host_method.MemberType, loc);
179 if (TypeManager.int_interlocked_compare_exchange == null) {
180 TypeSpec t = TypeManager.CoreLookupType (ec.Compiler, "System.Threading", "Interlocked", MemberKind.Class, true);
182 var p = new ParametersImported (
184 new ParameterData (null, Parameter.Modifier.REF),
185 new ParameterData (null, Parameter.Modifier.NONE),
186 new ParameterData (null, Parameter.Modifier.NONE)
189 TypeManager.int32_type, TypeManager.int32_type, TypeManager.int32_type
192 var f = new MemberFilter ("CompareExchange", 0, MemberKind.Method, p, TypeManager.int32_type);
193 TypeManager.int_interlocked_compare_exchange = TypeManager.GetPredefinedMethod (t, f, loc);
197 ec.CurrentBranching.CurrentUsageVector.Goto ();
201 protected override void DoEmit (EmitContext ec)
203 Label label_init = ec.DefineLabel ();
205 ec.Emit (OpCodes.Ldarg_0);
206 ec.Emit (OpCodes.Ldflda, host.PC.Spec);
207 ec.EmitInt ((int) Iterator.State.Start);
208 ec.EmitInt ((int) Iterator.State.Uninitialized);
209 ec.Emit (OpCodes.Call, TypeManager.int_interlocked_compare_exchange);
211 ec.EmitInt ((int) Iterator.State.Uninitialized);
212 ec.Emit (OpCodes.Bne_Un_S, label_init);
214 ec.Emit (OpCodes.Ldarg_0);
215 ec.Emit (OpCodes.Ret);
217 ec.MarkLabel (label_init);
219 new_storey.Emit (ec);
220 ec.Emit (OpCodes.Ret);
224 public GetEnumeratorMethod (IteratorStorey host, FullNamedExpression returnType, MemberName name)
225 : base (host, returnType, Modifiers.DEBUGGER_HIDDEN, name)
227 Block.AddStatement (new GetEnumeratorStatement (host, this));
231 class DisposeMethod : IteratorMethod
233 sealed class DisposeMethodStatement : Statement
237 public DisposeMethodStatement (Iterator iterator)
239 this.iterator = iterator;
240 this.loc = iterator.Location;
243 protected override void CloneTo (CloneContext clonectx, Statement target)
245 throw new NotSupportedException ();
248 public override bool Resolve (BlockContext ec)
253 protected override void DoEmit (EmitContext ec)
255 iterator.EmitDispose (ec);
259 public DisposeMethod (IteratorStorey host)
260 : base (host, new TypeExpression (TypeManager.void_type, host.Location), Modifiers.PUBLIC | Modifiers.DEBUGGER_HIDDEN,
261 new MemberName ("Dispose", host.Location))
263 host.AddMethod (this);
265 Block.AddStatement (new DisposeMethodStatement (host.Iterator));
270 // Uses Method as method info
272 class DynamicMethodGroupExpr : MethodGroupExpr
274 readonly Method method;
276 public DynamicMethodGroupExpr (Method method, Location loc)
277 : base ((IList<MemberSpec>) null, null, loc)
279 this.method = method;
280 eclass = ExprClass.Unresolved;
283 protected override Expression DoResolve (ResolveContext ec)
285 Methods = new List<MemberSpec> (1) { method.Spec };
286 type = method.Parent.Definition;
287 InstanceExpression = new CompilerGeneratedThis (type, Location);
288 return base.DoResolve (ec);
292 class DynamicFieldExpr : FieldExpr
294 readonly Field field;
296 public DynamicFieldExpr (Field field, Location loc)
302 protected override Expression DoResolve (ResolveContext ec)
305 type = spec.MemberType;
306 InstanceExpression = new CompilerGeneratedThis (type, Location);
307 return base.DoResolve (ec);
311 public readonly Iterator Iterator;
313 TypeExpr iterator_type_expr;
317 TypeExpr enumerator_type;
318 TypeExpr enumerable_type;
319 TypeArguments generic_args;
320 TypeExpr generic_enumerator_type;
321 TypeExpr generic_enumerable_type;
323 List<HoistedParameter> hoisted_params_copy;
326 public IteratorStorey (Iterator iterator)
327 : base (iterator.Container.ParametersBlock, iterator.Host,
328 iterator.OriginalMethod as MemberBase, iterator.GenericMethod == null ? null : iterator.GenericMethod.CurrentTypeParameters, "Iterator")
330 this.Iterator = iterator;
334 get { return pc_field; }
337 public Field CurrentField {
338 get { return current_field; }
341 public IList<HoistedParameter> HoistedParameters {
342 get { return hoisted_params; }
345 protected override TypeExpr [] ResolveBaseTypes (out TypeExpr base_class)
347 var mtype = Iterator.OriginalIteratorType;
349 mtype = Mutator.Mutate (mtype);
351 iterator_type_expr = new TypeExpression (mtype, Location);
352 generic_args = new TypeArguments (iterator_type_expr);
354 var list = new List<FullNamedExpression> ();
355 if (Iterator.IsEnumerable) {
356 enumerable_type = new TypeExpression (
357 TypeManager.ienumerable_type, Location);
358 list.Add (enumerable_type);
360 if (TypeManager.generic_ienumerable_type != null) {
361 generic_enumerable_type = new GenericTypeExpr (
362 TypeManager.generic_ienumerable_type,
363 generic_args, Location);
364 list.Add (generic_enumerable_type);
368 enumerator_type = new TypeExpression (
369 TypeManager.ienumerator_type, Location);
370 list.Add (enumerator_type);
372 list.Add (new TypeExpression (TypeManager.idisposable_type, Location));
374 if (TypeManager.generic_ienumerator_type != null) {
375 generic_enumerator_type = new GenericTypeExpr (
376 TypeManager.generic_ienumerator_type,
377 generic_args, Location);
378 list.Add (generic_enumerator_type);
383 return base.ResolveBaseTypes (out base_class);
386 protected override string GetVariableMangledName (LocalVariable local_info)
388 return "<" + local_info.Name + ">__" + local_name_idx++.ToString ();
391 protected override bool DoDefineMembers ()
393 DefineIteratorMembers ();
394 return base.DoDefineMembers ();
397 void DefineIteratorMembers ()
399 pc_field = AddCompilerGeneratedField ("$PC", new TypeExpression (TypeManager.int32_type, Location));
400 current_field = AddCompilerGeneratedField ("$current", iterator_type_expr);
402 if (hoisted_params != null) {
404 // Iterators are independent, each GetEnumerator call has to
405 // create same enumerator therefore we have to keep original values
406 // around for re-initialization
408 // TODO: Do it for assigned/modified parameters only
410 hoisted_params_copy = new List<HoistedParameter> (hoisted_params.Count);
411 foreach (HoistedParameter hp in hoisted_params) {
412 hoisted_params_copy.Add (new HoistedParameter (hp, "<$>" + hp.Field.Name));
416 if (generic_enumerator_type != null)
417 Define_Current (true);
419 Define_Current (false);
420 new DisposeMethod (this);
423 if (Iterator.IsEnumerable) {
424 MemberName name = new MemberName (QualifiedAliasMember.GlobalAlias, "System", null, Location);
425 name = new MemberName (name, "Collections", Location);
426 name = new MemberName (name, "IEnumerable", Location);
427 name = new MemberName (name, "GetEnumerator", Location);
429 if (generic_enumerator_type != null) {
430 Method get_enumerator = new IteratorMethod (this, enumerator_type, 0, name);
432 name = new MemberName (name.Left.Left, "Generic", Location);
433 name = new MemberName (name, "IEnumerable", generic_args, Location);
434 name = new MemberName (name, "GetEnumerator", Location);
435 Method gget_enumerator = new GetEnumeratorMethod (this, generic_enumerator_type, name);
438 // Just call generic GetEnumerator implementation
440 get_enumerator.Block.AddStatement (
441 new Return (new Invocation (new DynamicMethodGroupExpr (gget_enumerator, Location), null), Location));
443 AddMethod (get_enumerator);
444 AddMethod (gget_enumerator);
446 AddMethod (new GetEnumeratorMethod (this, enumerator_type, name));
451 protected override void EmitHoistedParameters (EmitContext ec, IList<HoistedParameter> hoisted)
453 base.EmitHoistedParameters (ec, hoisted);
454 base.EmitHoistedParameters (ec, hoisted_params_copy);
457 void Define_Current (bool is_generic)
461 MemberName name = new MemberName (QualifiedAliasMember.GlobalAlias, "System", null, Location);
462 name = new MemberName (name, "Collections", Location);
465 name = new MemberName (name, "Generic", Location);
466 name = new MemberName (name, "IEnumerator", generic_args, Location);
467 type = iterator_type_expr;
469 name = new MemberName (name, "IEnumerator");
470 type = new TypeExpression (TypeManager.object_type, Location);
473 name = new MemberName (name, "Current", Location);
475 ToplevelBlock get_block = new ToplevelBlock (Compiler, Location);
476 get_block.AddStatement (new Return (new DynamicFieldExpr (CurrentField, Location), Location));
478 Property current = new Property (this, type, Modifiers.DEBUGGER_HIDDEN, name, null);
479 current.Get = new Property.GetMethod (current, 0, null, Location);
480 current.Get.Block = get_block;
482 AddProperty (current);
487 Method reset = new Method (
488 this, null, new TypeExpression (TypeManager.void_type, Location),
489 Modifiers.PUBLIC | Modifiers.DEBUGGER_HIDDEN,
490 new MemberName ("Reset", Location),
491 ParametersCompiled.EmptyReadOnlyParameters, null);
494 reset.Block = new ToplevelBlock (Compiler, Location);
496 TypeSpec ex_type = TypeManager.CoreLookupType (Compiler, "System", "NotSupportedException", MemberKind.Class, true);
500 reset.Block.AddStatement (new Throw (new New (new TypeExpression (ex_type, Location), null, Location), Location));
504 class IteratorMethod : Method
506 readonly IteratorStorey host;
508 public IteratorMethod (IteratorStorey host, FullNamedExpression returnType, Modifiers mod, MemberName name)
509 : base (host, null, returnType, mod | Modifiers.COMPILER_GENERATED,
510 name, ParametersCompiled.EmptyReadOnlyParameters, null)
514 Block = new ToplevelBlock (host.Compiler, ParametersCompiled.EmptyReadOnlyParameters, Location);
517 public override EmitContext CreateEmitContext (ILGenerator ig)
519 EmitContext ec = new EmitContext (this, ig, MemberType);
521 ec.CurrentAnonymousMethod = host.Iterator;
527 // Iterators are implemented as hidden anonymous block
529 public class Iterator : AnonymousExpression
531 sealed class MoveNextMethodStatement : Statement
535 public MoveNextMethodStatement (Iterator iterator)
537 this.iterator = iterator;
538 this.loc = iterator.Location;
541 protected override void CloneTo (CloneContext clonectx, Statement target)
543 throw new NotSupportedException ();
546 public override bool Resolve (BlockContext ec)
551 protected override void DoEmit (EmitContext ec)
553 iterator.EmitMoveNext (ec);
557 public readonly IMethodData OriginalMethod;
558 public readonly TypeContainer Host;
559 public readonly bool IsEnumerable;
560 List<ResumableStatement> resume_points;
563 // The state as we generate the iterator
565 Label move_next_ok, move_next_error;
566 LocalBuilder skip_finally, current_pc;
568 public LocalBuilder SkipFinally {
569 get { return skip_finally; }
572 public LocalBuilder CurrentPC {
573 get { return current_pc; }
576 public Block Container {
577 get { return OriginalMethod.Block; }
580 public GenericMethod GenericMethod {
581 get { return OriginalMethod.GenericMethod; }
584 public readonly TypeSpec OriginalIteratorType;
586 IteratorStorey IteratorHost;
589 Running = -3, // Used only in CurrentPC, never stored into $PC
595 public void EmitYieldBreak (EmitContext ec, bool unwind_protect)
597 ec.Emit (unwind_protect ? OpCodes.Leave : OpCodes.Br, move_next_error);
600 void EmitMoveNext_NoResumePoints (EmitContext ec, Block original_block)
602 ec.Emit (OpCodes.Ldarg_0);
603 ec.Emit (OpCodes.Ldfld, IteratorHost.PC.Spec);
605 ec.Emit (OpCodes.Ldarg_0);
606 ec.EmitInt ((int) State.After);
607 ec.Emit (OpCodes.Stfld, IteratorHost.PC.Spec);
609 // We only care if the PC is zero (start executing) or non-zero (don't do anything)
610 ec.Emit (OpCodes.Brtrue, move_next_error);
612 SymbolWriter.StartIteratorBody (ec);
613 original_block.Emit (ec);
614 SymbolWriter.EndIteratorBody (ec);
616 ec.MarkLabel (move_next_error);
617 ec.Emit (OpCodes.Ldc_I4_0);
618 ec.Emit (OpCodes.Ret);
621 void EmitMoveNext (EmitContext ec)
623 move_next_ok = ec.DefineLabel ();
624 move_next_error = ec.DefineLabel ();
626 if (resume_points == null) {
627 EmitMoveNext_NoResumePoints (ec, block);
631 current_pc = ec.GetTemporaryLocal (TypeManager.uint32_type);
632 ec.Emit (OpCodes.Ldarg_0);
633 ec.Emit (OpCodes.Ldfld, IteratorHost.PC.Spec);
634 ec.Emit (OpCodes.Stloc, current_pc);
636 // We're actually in state 'running', but this is as good a PC value as any if there's an abnormal exit
637 ec.Emit (OpCodes.Ldarg_0);
638 ec.EmitInt ((int) State.After);
639 ec.Emit (OpCodes.Stfld, IteratorHost.PC.Spec);
641 Label [] labels = new Label [1 + resume_points.Count];
642 labels [0] = ec.DefineLabel ();
644 bool need_skip_finally = false;
645 for (int i = 0; i < resume_points.Count; ++i) {
646 ResumableStatement s = resume_points [i];
647 need_skip_finally |= s is ExceptionStatement;
648 labels [i+1] = s.PrepareForEmit (ec);
651 if (need_skip_finally) {
652 skip_finally = ec.GetTemporaryLocal (TypeManager.bool_type);
653 ec.Emit (OpCodes.Ldc_I4_0);
654 ec.Emit (OpCodes.Stloc, skip_finally);
657 SymbolWriter.StartIteratorDispatcher (ec);
658 ec.Emit (OpCodes.Ldloc, current_pc);
659 ec.Emit (OpCodes.Switch, labels);
661 ec.Emit (OpCodes.Br, move_next_error);
662 SymbolWriter.EndIteratorDispatcher (ec);
664 ec.MarkLabel (labels [0]);
666 SymbolWriter.StartIteratorBody (ec);
668 SymbolWriter.EndIteratorBody (ec);
670 SymbolWriter.StartIteratorDispatcher (ec);
672 ec.Emit (OpCodes.Ldarg_0);
673 ec.EmitInt ((int) State.After);
674 ec.Emit (OpCodes.Stfld, IteratorHost.PC.Spec);
676 ec.MarkLabel (move_next_error);
678 ec.Emit (OpCodes.Ret);
680 ec.MarkLabel (move_next_ok);
681 ec.Emit (OpCodes.Ldc_I4_1);
682 ec.Emit (OpCodes.Ret);
684 SymbolWriter.EndIteratorDispatcher (ec);
687 public void EmitDispose (EmitContext ec)
689 Label end = ec.DefineLabel ();
691 Label [] labels = null;
692 int n_resume_points = resume_points == null ? 0 : resume_points.Count;
693 for (int i = 0; i < n_resume_points; ++i) {
694 ResumableStatement s = (ResumableStatement) resume_points [i];
695 Label ret = s.PrepareForDispose (ec, end);
696 if (ret.Equals (end) && labels == null)
698 if (labels == null) {
699 labels = new Label [resume_points.Count + 1];
700 for (int j = 0; j <= i; ++j)
706 if (labels != null) {
707 current_pc = ec.GetTemporaryLocal (TypeManager.uint32_type);
708 ec.Emit (OpCodes.Ldarg_0);
709 ec.Emit (OpCodes.Ldfld, IteratorHost.PC.Spec);
710 ec.Emit (OpCodes.Stloc, current_pc);
713 ec.Emit (OpCodes.Ldarg_0);
714 ec.EmitInt ((int) State.After);
715 ec.Emit (OpCodes.Stfld, IteratorHost.PC.Spec);
717 if (labels != null) {
718 //SymbolWriter.StartIteratorDispatcher (ec.ig);
719 ec.Emit (OpCodes.Ldloc, current_pc);
720 ec.Emit (OpCodes.Switch, labels);
721 //SymbolWriter.EndIteratorDispatcher (ec.ig);
723 foreach (ResumableStatement s in resume_points)
724 s.EmitForDispose (ec, this, end, true);
730 public int AddResumePoint (ResumableStatement stmt)
732 if (resume_points == null)
733 resume_points = new List<ResumableStatement> ();
735 resume_points.Add (stmt);
736 return resume_points.Count;
740 // Called back from Yield
742 public void MarkYield (EmitContext ec, Expression expr, int resume_pc, bool unwind_protect, Label resume_point)
744 // Store the new current
745 ec.Emit (OpCodes.Ldarg_0);
747 ec.Emit (OpCodes.Stfld, IteratorHost.CurrentField.Spec);
749 // store resume program-counter
750 ec.Emit (OpCodes.Ldarg_0);
751 ec.EmitInt (resume_pc);
752 ec.Emit (OpCodes.Stfld, IteratorHost.PC.Spec);
754 // mark finally blocks as disabled
755 if (unwind_protect && skip_finally != null) {
757 ec.Emit (OpCodes.Stloc, skip_finally);
761 ec.Emit (unwind_protect ? OpCodes.Leave : OpCodes.Br, move_next_ok);
763 ec.MarkLabel (resume_point);
769 public Iterator (ParametersBlock block, IMethodData method, TypeContainer host, TypeSpec iterator_type, bool is_enumerable)
770 : base (block, TypeManager.bool_type, block.StartLocation)
772 this.OriginalMethod = method;
773 this.OriginalIteratorType = iterator_type;
774 this.IsEnumerable = is_enumerable;
776 this.type = method.ReturnType;
779 public override string ContainerType {
780 get { return "iterator"; }
783 public override bool IsIterator {
787 public override AnonymousMethodStorey Storey {
788 get { return IteratorHost; }
791 public override string GetSignatureForError ()
793 return OriginalMethod.GetSignatureForError ();
796 protected override Expression DoResolve (ResolveContext ec)
798 IteratorHost = (IteratorStorey) block.TopBlock.AnonymousMethodStorey;
800 BlockContext ctx = new BlockContext (ec, block, ReturnType);
801 ctx.CurrentAnonymousMethod = this;
803 ctx.StartFlowBranching (this, ec.CurrentBranching);
805 ctx.EndFlowBranching ();
807 var move_next = new IteratorMethod (IteratorHost, new TypeExpression (TypeManager.bool_type, loc),
808 Modifiers.PUBLIC, new MemberName ("MoveNext", Location));
809 move_next.Block.AddStatement (new MoveNextMethodStatement (this));
810 IteratorHost.AddMethod (move_next);
812 eclass = ExprClass.Value;
816 public override void Emit (EmitContext ec)
819 // Load Iterator storey instance
821 IteratorHost.Instance.Emit (ec);
824 // Initialize iterator PC when it's unitialized
827 ec.Emit (OpCodes.Dup);
828 ec.EmitInt ((int)State.Uninitialized);
830 var field = IteratorHost.PC.Spec;
831 if (Storey.MemberName.IsGeneric) {
832 field = MemberCache.GetMember (Storey.Instance.Type, field);
835 ec.Emit (OpCodes.Stfld, field);
839 public override Expression CreateExpressionTree (ResolveContext ec)
841 throw new NotSupportedException ("ET");
844 public static void CreateIterator (IMethodData method, TypeContainer parent, Modifiers modifiers, CompilerContext ctx)
847 TypeSpec iterator_type;
849 TypeSpec ret = method.ReturnType;
853 if (!CheckType (ret, out iterator_type, out is_enumerable)) {
854 ctx.Report.Error (1624, method.Location,
855 "The body of `{0}' cannot be an iterator block " +
856 "because `{1}' is not an iterator interface type",
857 method.GetSignatureForError (),
858 TypeManager.CSharpName (ret));
862 ParametersCompiled parameters = method.ParameterInfo;
863 for (int i = 0; i < parameters.Count; i++) {
864 Parameter p = parameters [i];
865 Parameter.Modifier mod = p.ModFlags;
866 if ((mod & Parameter.Modifier.ISBYREF) != 0) {
867 ctx.Report.Error (1623, p.Location,
868 "Iterators cannot have ref or out parameters");
872 if (p is ArglistParameter) {
873 ctx.Report.Error (1636, method.Location,
874 "__arglist is not allowed in parameter list of iterators");
878 if (parameters.Types [i].IsPointer) {
879 ctx.Report.Error (1637, p.Location,
880 "Iterators cannot have unsafe parameters or " +
886 if ((modifiers & Modifiers.UNSAFE) != 0) {
887 ctx.Report.Error (1629, method.Location, "Unsafe code may not appear in iterators");
890 method.Block.WrapIntoIterator (method, parent, iterator_type, is_enumerable);
893 static bool CheckType (TypeSpec ret, out TypeSpec original_iterator_type, out bool is_enumerable)
895 original_iterator_type = null;
896 is_enumerable = false;
898 if (ret == TypeManager.ienumerable_type) {
899 original_iterator_type = TypeManager.object_type;
900 is_enumerable = true;
903 if (ret == TypeManager.ienumerator_type) {
904 original_iterator_type = TypeManager.object_type;
905 is_enumerable = false;
909 InflatedTypeSpec inflated = ret as InflatedTypeSpec;
910 if (inflated == null)
913 ret = inflated.GetDefinition ();
914 if (ret == TypeManager.generic_ienumerable_type) {
915 original_iterator_type = inflated.TypeArguments[0];
916 is_enumerable = true;
920 if (ret == TypeManager.generic_ienumerator_type) {
921 original_iterator_type = inflated.TypeArguments[0];
922 is_enumerable = false;