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.Emit;
21 namespace Mono.CSharp {
23 public class Yield : ResumableStatement {
29 public Yield (Expression expr, Location l)
35 public static bool CheckContext (ResolveContext ec, Location loc)
37 if (!ec.CurrentAnonymousMethod.IsIterator) {
38 ec.Report.Error (1621, loc,
39 "The yield statement cannot be used inside " +
40 "anonymous method blocks");
47 public override bool Resolve (BlockContext ec)
49 expr = expr.Resolve (ec);
53 Report.Debug (64, "RESOLVE YIELD #1", this, ec, expr, expr.GetType (),
54 ec.CurrentAnonymousMethod, ec.CurrentIterator);
56 if (!CheckContext (ec, loc))
59 iterator = ec.CurrentIterator;
60 if (expr.Type != iterator.OriginalIteratorType) {
61 expr = Convert.ImplicitConversionRequired (
62 ec, expr, iterator.OriginalIteratorType, loc);
67 if (!ec.CurrentBranching.CurrentUsageVector.IsUnreachable)
68 unwind_protect = ec.CurrentBranching.AddResumePoint (this, loc, out resume_pc);
73 protected override void DoEmit (EmitContext ec)
75 iterator.MarkYield (ec, expr, resume_pc, unwind_protect, resume_point);
78 protected override void CloneTo (CloneContext clonectx, Statement t)
80 Yield target = (Yield) t;
82 target.expr = expr.Clone (clonectx);
86 public class YieldBreak : ExitStatement
90 public YieldBreak (Location l)
95 public override void Error_FinallyClause (Report Report)
97 Report.Error (1625, loc, "Cannot yield in the body of a finally clause");
100 protected override void CloneTo (CloneContext clonectx, Statement target)
102 throw new NotSupportedException ();
105 protected override bool DoResolve (BlockContext ec)
107 iterator = ec.CurrentIterator;
108 return Yield.CheckContext (ec, loc);
111 protected override void DoEmit (EmitContext ec)
113 iterator.EmitYieldBreak (ec, unwind_protect);
117 public class IteratorStorey : AnonymousMethodStorey
119 class GetEnumeratorMethod : IteratorMethod
121 sealed class GetEnumeratorStatement : Statement
124 IteratorMethod host_method;
126 Expression new_storey;
128 public GetEnumeratorStatement (IteratorStorey host, IteratorMethod host_method)
131 this.host_method = host_method;
132 loc = host_method.Location;
135 protected override void CloneTo (CloneContext clonectx, Statement target)
137 throw new NotSupportedException ();
140 public override bool Resolve (BlockContext ec)
142 TypeExpression storey_type_expr = new TypeExpression (host.Definition, loc);
143 List<Expression> init = null;
144 if (host.hoisted_this != null) {
145 init = new List<Expression> (host.hoisted_params == null ? 1 : host.HoistedParameters.Count + 1);
146 HoistedThis ht = host.hoisted_this;
147 FieldExpr from = new FieldExpr (ht.Field, loc);
148 from.InstanceExpression = CompilerGeneratedThis.Instance;
149 init.Add (new ElementInitializer (ht.Field.Name, from, loc));
152 if (host.hoisted_params != null) {
154 init = new List<Expression> (host.HoistedParameters.Count);
156 for (int i = 0; i < host.hoisted_params.Count; ++i) {
157 HoistedParameter hp = (HoistedParameter) host.hoisted_params [i];
158 HoistedParameter hp_cp = (HoistedParameter) host.hoisted_params_copy [i];
160 FieldExpr from = new FieldExpr (hp_cp.Field, loc);
161 from.InstanceExpression = CompilerGeneratedThis.Instance;
163 init.Add (new ElementInitializer (hp.Field.Name, from, loc));
168 new_storey = new NewInitialize (storey_type_expr, null,
169 new CollectionOrObjectInitializers (init, loc), loc);
171 new_storey = new New (storey_type_expr, null, loc);
174 new_storey = new_storey.Resolve (ec);
175 if (new_storey != null)
176 new_storey = Convert.ImplicitConversionRequired (ec, new_storey, host_method.MemberType, loc);
178 if (TypeManager.int_interlocked_compare_exchange == null) {
179 TypeSpec t = TypeManager.CoreLookupType (ec.Compiler, "System.Threading", "Interlocked", MemberKind.Class, true);
181 var p = new ParametersImported (
183 new ParameterData (null, Parameter.Modifier.REF),
184 new ParameterData (null, Parameter.Modifier.NONE),
185 new ParameterData (null, Parameter.Modifier.NONE)
188 TypeManager.int32_type, TypeManager.int32_type, TypeManager.int32_type
191 var f = new MemberFilter ("CompareExchange", 0, MemberKind.Method, p, TypeManager.int32_type);
192 TypeManager.int_interlocked_compare_exchange = TypeManager.GetPredefinedMethod (t, f, loc);
196 ec.CurrentBranching.CurrentUsageVector.Goto ();
200 protected override void DoEmit (EmitContext ec)
202 Label label_init = ec.DefineLabel ();
204 ec.Emit (OpCodes.Ldarg_0);
205 ec.Emit (OpCodes.Ldflda, host.PC.Spec);
206 ec.EmitInt ((int) Iterator.State.Start);
207 ec.EmitInt ((int) Iterator.State.Uninitialized);
208 ec.Emit (OpCodes.Call, TypeManager.int_interlocked_compare_exchange);
210 ec.EmitInt ((int) Iterator.State.Uninitialized);
211 ec.Emit (OpCodes.Bne_Un_S, label_init);
213 ec.Emit (OpCodes.Ldarg_0);
214 ec.Emit (OpCodes.Ret);
216 ec.MarkLabel (label_init);
218 new_storey.Emit (ec);
219 ec.Emit (OpCodes.Ret);
223 public GetEnumeratorMethod (IteratorStorey host, FullNamedExpression returnType, MemberName name)
224 : base (host, returnType, Modifiers.DEBUGGER_HIDDEN, name)
226 Block.AddStatement (new GetEnumeratorStatement (host, this));
230 class DisposeMethod : IteratorMethod
232 sealed class DisposeMethodStatement : Statement
236 public DisposeMethodStatement (Iterator iterator)
238 this.iterator = iterator;
239 this.loc = iterator.Location;
242 protected override void CloneTo (CloneContext clonectx, Statement target)
244 throw new NotSupportedException ();
247 public override bool Resolve (BlockContext ec)
252 protected override void DoEmit (EmitContext ec)
254 iterator.EmitDispose (ec);
258 public DisposeMethod (IteratorStorey host)
259 : base (host, new TypeExpression (TypeManager.void_type, host.Location), Modifiers.PUBLIC | Modifiers.DEBUGGER_HIDDEN,
260 new MemberName ("Dispose", host.Location))
262 host.AddMethod (this);
264 Block.AddStatement (new DisposeMethodStatement (host.Iterator));
269 // Uses Method as method info
271 class DynamicMethodGroupExpr : MethodGroupExpr
273 readonly Method method;
275 public DynamicMethodGroupExpr (Method method, Location loc)
276 : base ((IList<MemberSpec>) null, null, loc)
278 this.method = method;
279 eclass = ExprClass.Unresolved;
282 protected override Expression DoResolve (ResolveContext ec)
284 Methods = new List<MemberSpec> (1) { method.Spec };
285 type = method.Parent.Definition;
286 InstanceExpression = new CompilerGeneratedThis (type, Location);
287 return base.DoResolve (ec);
291 class DynamicFieldExpr : FieldExpr
293 readonly Field field;
295 public DynamicFieldExpr (Field field, Location loc)
301 protected override Expression DoResolve (ResolveContext ec)
304 type = spec.MemberType;
305 InstanceExpression = new CompilerGeneratedThis (type, Location);
306 return base.DoResolve (ec);
310 public readonly Iterator Iterator;
312 TypeExpr iterator_type_expr;
316 TypeExpr enumerator_type;
317 TypeExpr enumerable_type;
318 TypeArguments generic_args;
319 TypeExpr generic_enumerator_type;
320 TypeExpr generic_enumerable_type;
322 List<HoistedParameter> hoisted_params_copy;
325 public IteratorStorey (Iterator iterator)
326 : base (iterator.Container.ParametersBlock, iterator.Host,
327 iterator.OriginalMethod as MemberBase, iterator.GenericMethod == null ? null : iterator.GenericMethod.CurrentTypeParameters, "Iterator")
329 this.Iterator = iterator;
333 get { return pc_field; }
336 public Field CurrentField {
337 get { return current_field; }
340 public IList<HoistedParameter> HoistedParameters {
341 get { return hoisted_params; }
344 protected override TypeExpr [] ResolveBaseTypes (out TypeExpr base_class)
346 var mtype = Iterator.OriginalIteratorType;
348 mtype = Mutator.Mutate (mtype);
350 iterator_type_expr = new TypeExpression (mtype, Location);
351 generic_args = new TypeArguments (iterator_type_expr);
353 var list = new List<FullNamedExpression> ();
354 if (Iterator.IsEnumerable) {
355 enumerable_type = new TypeExpression (
356 TypeManager.ienumerable_type, Location);
357 list.Add (enumerable_type);
359 if (TypeManager.generic_ienumerable_type != null) {
360 generic_enumerable_type = new GenericTypeExpr (
361 TypeManager.generic_ienumerable_type,
362 generic_args, Location);
363 list.Add (generic_enumerable_type);
367 enumerator_type = new TypeExpression (
368 TypeManager.ienumerator_type, Location);
369 list.Add (enumerator_type);
371 list.Add (new TypeExpression (TypeManager.idisposable_type, Location));
373 if (TypeManager.generic_ienumerator_type != null) {
374 generic_enumerator_type = new GenericTypeExpr (
375 TypeManager.generic_ienumerator_type,
376 generic_args, Location);
377 list.Add (generic_enumerator_type);
382 return base.ResolveBaseTypes (out base_class);
385 protected override string GetVariableMangledName (LocalVariable local_info)
387 return "<" + local_info.Name + ">__" + local_name_idx++.ToString ();
390 protected override bool DoDefineMembers ()
392 DefineIteratorMembers ();
393 return base.DoDefineMembers ();
396 void DefineIteratorMembers ()
398 pc_field = AddCompilerGeneratedField ("$PC", new TypeExpression (TypeManager.int32_type, Location));
399 current_field = AddCompilerGeneratedField ("$current", iterator_type_expr);
401 if (hoisted_params != null) {
403 // Iterators are independent, each GetEnumerator call has to
404 // create same enumerator therefore we have to keep original values
405 // around for re-initialization
407 // TODO: Do it for assigned/modified parameters only
409 hoisted_params_copy = new List<HoistedParameter> (hoisted_params.Count);
410 foreach (HoistedParameter hp in hoisted_params) {
411 hoisted_params_copy.Add (new HoistedParameter (hp, "<$>" + hp.Field.Name));
415 if (generic_enumerator_type != null)
416 Define_Current (true);
418 Define_Current (false);
419 new DisposeMethod (this);
422 if (Iterator.IsEnumerable) {
423 MemberName name = new MemberName (QualifiedAliasMember.GlobalAlias, "System", null, Location);
424 name = new MemberName (name, "Collections", Location);
425 name = new MemberName (name, "IEnumerable", Location);
426 name = new MemberName (name, "GetEnumerator", Location);
428 if (generic_enumerator_type != null) {
429 Method get_enumerator = new IteratorMethod (this, enumerator_type, 0, name);
431 name = new MemberName (name.Left.Left, "Generic", Location);
432 name = new MemberName (name, "IEnumerable", generic_args, Location);
433 name = new MemberName (name, "GetEnumerator", Location);
434 Method gget_enumerator = new GetEnumeratorMethod (this, generic_enumerator_type, name);
437 // Just call generic GetEnumerator implementation
439 get_enumerator.Block.AddStatement (
440 new Return (new Invocation (new DynamicMethodGroupExpr (gget_enumerator, Location), null), Location));
442 AddMethod (get_enumerator);
443 AddMethod (gget_enumerator);
445 AddMethod (new GetEnumeratorMethod (this, enumerator_type, name));
450 protected override void EmitHoistedParameters (EmitContext ec, IList<HoistedParameter> hoisted)
452 base.EmitHoistedParameters (ec, hoisted);
453 base.EmitHoistedParameters (ec, hoisted_params_copy);
456 void Define_Current (bool is_generic)
460 MemberName name = new MemberName (QualifiedAliasMember.GlobalAlias, "System", null, Location);
461 name = new MemberName (name, "Collections", Location);
464 name = new MemberName (name, "Generic", Location);
465 name = new MemberName (name, "IEnumerator", generic_args, Location);
466 type = iterator_type_expr;
468 name = new MemberName (name, "IEnumerator");
469 type = new TypeExpression (TypeManager.object_type, Location);
472 name = new MemberName (name, "Current", Location);
474 ToplevelBlock get_block = new ToplevelBlock (Compiler, Location);
475 get_block.AddStatement (new Return (new DynamicFieldExpr (CurrentField, Location), Location));
477 Property current = new Property (this, type, Modifiers.DEBUGGER_HIDDEN, name, null);
478 current.Get = new Property.GetMethod (current, 0, null, Location);
479 current.Get.Block = get_block;
481 AddProperty (current);
486 Method reset = new Method (
487 this, null, new TypeExpression (TypeManager.void_type, Location),
488 Modifiers.PUBLIC | Modifiers.DEBUGGER_HIDDEN,
489 new MemberName ("Reset", Location),
490 ParametersCompiled.EmptyReadOnlyParameters, null);
493 reset.Block = new ToplevelBlock (Compiler, Location);
495 TypeSpec ex_type = TypeManager.CoreLookupType (Compiler, "System", "NotSupportedException", MemberKind.Class, true);
499 reset.Block.AddStatement (new Throw (new New (new TypeExpression (ex_type, Location), null, Location), Location));
503 class IteratorMethod : Method
505 readonly IteratorStorey host;
507 public IteratorMethod (IteratorStorey host, FullNamedExpression returnType, Modifiers mod, MemberName name)
508 : base (host, null, returnType, mod | Modifiers.COMPILER_GENERATED,
509 name, ParametersCompiled.EmptyReadOnlyParameters, null)
513 Block = new ToplevelBlock (host.Compiler, ParametersCompiled.EmptyReadOnlyParameters, Location);
516 public override EmitContext CreateEmitContext (ILGenerator ig)
518 EmitContext ec = new EmitContext (this, ig, MemberType);
520 ec.CurrentAnonymousMethod = host.Iterator;
526 // Iterators are implemented as hidden anonymous block
528 public class Iterator : AnonymousExpression
530 sealed class MoveNextMethodStatement : Statement
534 public MoveNextMethodStatement (Iterator iterator)
536 this.iterator = iterator;
537 this.loc = iterator.Location;
540 protected override void CloneTo (CloneContext clonectx, Statement target)
542 throw new NotSupportedException ();
545 public override bool Resolve (BlockContext ec)
550 protected override void DoEmit (EmitContext ec)
552 iterator.EmitMoveNext (ec);
556 public readonly IMethodData OriginalMethod;
557 public readonly TypeContainer Host;
558 public readonly bool IsEnumerable;
559 List<ResumableStatement> resume_points;
562 // The state as we generate the iterator
564 Label move_next_ok, move_next_error;
565 LocalBuilder skip_finally, current_pc;
567 public LocalBuilder SkipFinally {
568 get { return skip_finally; }
571 public LocalBuilder CurrentPC {
572 get { return current_pc; }
575 public Block Container {
576 get { return OriginalMethod.Block; }
579 public GenericMethod GenericMethod {
580 get { return OriginalMethod.GenericMethod; }
583 public readonly TypeSpec OriginalIteratorType;
585 IteratorStorey IteratorHost;
588 Running = -3, // Used only in CurrentPC, never stored into $PC
594 public void EmitYieldBreak (EmitContext ec, bool unwind_protect)
596 ec.Emit (unwind_protect ? OpCodes.Leave : OpCodes.Br, move_next_error);
599 void EmitMoveNext_NoResumePoints (EmitContext ec, Block original_block)
601 ec.Emit (OpCodes.Ldarg_0);
602 ec.Emit (OpCodes.Ldfld, IteratorHost.PC.Spec);
604 ec.Emit (OpCodes.Ldarg_0);
605 ec.EmitInt ((int) State.After);
606 ec.Emit (OpCodes.Stfld, IteratorHost.PC.Spec);
608 // We only care if the PC is zero (start executing) or non-zero (don't do anything)
609 ec.Emit (OpCodes.Brtrue, move_next_error);
611 SymbolWriter.StartIteratorBody (ec);
612 original_block.Emit (ec);
613 SymbolWriter.EndIteratorBody (ec);
615 ec.MarkLabel (move_next_error);
616 ec.Emit (OpCodes.Ldc_I4_0);
617 ec.Emit (OpCodes.Ret);
620 void EmitMoveNext (EmitContext ec)
622 move_next_ok = ec.DefineLabel ();
623 move_next_error = ec.DefineLabel ();
625 if (resume_points == null) {
626 EmitMoveNext_NoResumePoints (ec, block);
630 current_pc = ec.GetTemporaryLocal (TypeManager.uint32_type);
631 ec.Emit (OpCodes.Ldarg_0);
632 ec.Emit (OpCodes.Ldfld, IteratorHost.PC.Spec);
633 ec.Emit (OpCodes.Stloc, current_pc);
635 // We're actually in state 'running', but this is as good a PC value as any if there's an abnormal exit
636 ec.Emit (OpCodes.Ldarg_0);
637 ec.EmitInt ((int) State.After);
638 ec.Emit (OpCodes.Stfld, IteratorHost.PC.Spec);
640 Label [] labels = new Label [1 + resume_points.Count];
641 labels [0] = ec.DefineLabel ();
643 bool need_skip_finally = false;
644 for (int i = 0; i < resume_points.Count; ++i) {
645 ResumableStatement s = resume_points [i];
646 need_skip_finally |= s is ExceptionStatement;
647 labels [i+1] = s.PrepareForEmit (ec);
650 if (need_skip_finally) {
651 skip_finally = ec.GetTemporaryLocal (TypeManager.bool_type);
652 ec.Emit (OpCodes.Ldc_I4_0);
653 ec.Emit (OpCodes.Stloc, skip_finally);
656 SymbolWriter.StartIteratorDispatcher (ec);
657 ec.Emit (OpCodes.Ldloc, current_pc);
658 ec.Emit (OpCodes.Switch, labels);
660 ec.Emit (OpCodes.Br, move_next_error);
661 SymbolWriter.EndIteratorDispatcher (ec);
663 ec.MarkLabel (labels [0]);
665 SymbolWriter.StartIteratorBody (ec);
667 SymbolWriter.EndIteratorBody (ec);
669 SymbolWriter.StartIteratorDispatcher (ec);
671 ec.Emit (OpCodes.Ldarg_0);
672 ec.EmitInt ((int) State.After);
673 ec.Emit (OpCodes.Stfld, IteratorHost.PC.Spec);
675 ec.MarkLabel (move_next_error);
677 ec.Emit (OpCodes.Ret);
679 ec.MarkLabel (move_next_ok);
680 ec.Emit (OpCodes.Ldc_I4_1);
681 ec.Emit (OpCodes.Ret);
683 SymbolWriter.EndIteratorDispatcher (ec);
686 public void EmitDispose (EmitContext ec)
688 Label end = ec.DefineLabel ();
690 Label [] labels = null;
691 int n_resume_points = resume_points == null ? 0 : resume_points.Count;
692 for (int i = 0; i < n_resume_points; ++i) {
693 ResumableStatement s = (ResumableStatement) resume_points [i];
694 Label ret = s.PrepareForDispose (ec, end);
695 if (ret.Equals (end) && labels == null)
697 if (labels == null) {
698 labels = new Label [resume_points.Count + 1];
699 for (int j = 0; j <= i; ++j)
705 if (labels != null) {
706 current_pc = ec.GetTemporaryLocal (TypeManager.uint32_type);
707 ec.Emit (OpCodes.Ldarg_0);
708 ec.Emit (OpCodes.Ldfld, IteratorHost.PC.Spec);
709 ec.Emit (OpCodes.Stloc, current_pc);
712 ec.Emit (OpCodes.Ldarg_0);
713 ec.EmitInt ((int) State.After);
714 ec.Emit (OpCodes.Stfld, IteratorHost.PC.Spec);
716 if (labels != null) {
717 //SymbolWriter.StartIteratorDispatcher (ec.ig);
718 ec.Emit (OpCodes.Ldloc, current_pc);
719 ec.Emit (OpCodes.Switch, labels);
720 //SymbolWriter.EndIteratorDispatcher (ec.ig);
722 foreach (ResumableStatement s in resume_points)
723 s.EmitForDispose (ec, this, end, true);
729 public int AddResumePoint (ResumableStatement stmt)
731 if (resume_points == null)
732 resume_points = new List<ResumableStatement> ();
734 resume_points.Add (stmt);
735 return resume_points.Count;
739 // Called back from Yield
741 public void MarkYield (EmitContext ec, Expression expr, int resume_pc, bool unwind_protect, Label resume_point)
743 // Store the new current
744 ec.Emit (OpCodes.Ldarg_0);
746 ec.Emit (OpCodes.Stfld, IteratorHost.CurrentField.Spec);
748 // store resume program-counter
749 ec.Emit (OpCodes.Ldarg_0);
750 ec.EmitInt (resume_pc);
751 ec.Emit (OpCodes.Stfld, IteratorHost.PC.Spec);
753 // mark finally blocks as disabled
754 if (unwind_protect && skip_finally != null) {
756 ec.Emit (OpCodes.Stloc, skip_finally);
760 ec.Emit (unwind_protect ? OpCodes.Leave : OpCodes.Br, move_next_ok);
762 ec.MarkLabel (resume_point);
768 public Iterator (ParametersBlock block, IMethodData method, TypeContainer host, TypeSpec iterator_type, bool is_enumerable)
769 : base (block, TypeManager.bool_type, block.StartLocation)
771 this.OriginalMethod = method;
772 this.OriginalIteratorType = iterator_type;
773 this.IsEnumerable = is_enumerable;
775 this.type = method.ReturnType;
778 public override string ContainerType {
779 get { return "iterator"; }
782 public override bool IsIterator {
786 public override AnonymousMethodStorey Storey {
787 get { return IteratorHost; }
790 public override string GetSignatureForError ()
792 return OriginalMethod.GetSignatureForError ();
795 protected override Expression DoResolve (ResolveContext ec)
797 IteratorHost = (IteratorStorey) block.TopBlock.AnonymousMethodStorey;
799 BlockContext ctx = new BlockContext (ec, block, ReturnType);
800 ctx.CurrentAnonymousMethod = this;
802 ctx.StartFlowBranching (this, ec.CurrentBranching);
804 ctx.EndFlowBranching ();
806 var move_next = new IteratorMethod (IteratorHost, new TypeExpression (TypeManager.bool_type, loc),
807 Modifiers.PUBLIC, new MemberName ("MoveNext", Location));
808 move_next.Block.AddStatement (new MoveNextMethodStatement (this));
809 IteratorHost.AddMethod (move_next);
811 eclass = ExprClass.Value;
815 public override void Emit (EmitContext ec)
818 // Load Iterator storey instance
820 IteratorHost.Instance.Emit (ec);
823 // Initialize iterator PC when it's unitialized
826 ec.Emit (OpCodes.Dup);
827 ec.EmitInt ((int)State.Uninitialized);
829 var field = IteratorHost.PC.Spec;
830 if (Storey.MemberName.IsGeneric) {
831 field = MemberCache.GetMember (Storey.Instance.Type, field);
834 ec.Emit (OpCodes.Stfld, field);
838 public override Expression CreateExpressionTree (ResolveContext ec)
840 throw new NotSupportedException ("ET");
843 public static void CreateIterator (IMethodData method, TypeContainer parent, Modifiers modifiers, CompilerContext ctx)
846 TypeSpec iterator_type;
848 TypeSpec ret = method.ReturnType;
852 if (!CheckType (ret, out iterator_type, out is_enumerable)) {
853 ctx.Report.Error (1624, method.Location,
854 "The body of `{0}' cannot be an iterator block " +
855 "because `{1}' is not an iterator interface type",
856 method.GetSignatureForError (),
857 TypeManager.CSharpName (ret));
861 ParametersCompiled parameters = method.ParameterInfo;
862 for (int i = 0; i < parameters.Count; i++) {
863 Parameter p = parameters [i];
864 Parameter.Modifier mod = p.ModFlags;
865 if ((mod & Parameter.Modifier.ISBYREF) != 0) {
866 ctx.Report.Error (1623, p.Location,
867 "Iterators cannot have ref or out parameters");
871 if (p is ArglistParameter) {
872 ctx.Report.Error (1636, method.Location,
873 "__arglist is not allowed in parameter list of iterators");
877 if (parameters.Types [i].IsPointer) {
878 ctx.Report.Error (1637, p.Location,
879 "Iterators cannot have unsafe parameters or " +
885 if ((modifiers & Modifiers.UNSAFE) != 0) {
886 ctx.Report.Error (1629, method.Location, "Unsafe code may not appear in iterators");
889 method.Block.WrapIntoIterator (method, parent, iterator_type, is_enumerable);
892 static bool CheckType (TypeSpec ret, out TypeSpec original_iterator_type, out bool is_enumerable)
894 original_iterator_type = null;
895 is_enumerable = false;
897 if (ret == TypeManager.ienumerable_type) {
898 original_iterator_type = TypeManager.object_type;
899 is_enumerable = true;
902 if (ret == TypeManager.ienumerator_type) {
903 original_iterator_type = TypeManager.object_type;
904 is_enumerable = false;
908 InflatedTypeSpec inflated = ret as InflatedTypeSpec;
909 if (inflated == null)
912 ret = inflated.GetDefinition ();
913 if (ret == TypeManager.generic_ienumerable_type) {
914 original_iterator_type = inflated.TypeArguments[0];
915 is_enumerable = true;
919 if (ret == TypeManager.generic_ienumerator_type) {
920 original_iterator_type = inflated.TypeArguments[0];
921 is_enumerable = false;