2009-09-04 Zoltan Varga <vargaz@gmail.com>
[mcs.git] / mcs / iterators.cs
blob29996c4ad9952d23daff1aee7176fa0c03e05ee7
1 //
2 // iterators.cs: Support for implementing iterators
3 //
4 // Author:
5 // Miguel de Icaza (miguel@ximian.com)
6 // Marek Safar (marek.safar@gmail.com)
7 //
8 // Dual licensed under the terms of the MIT X11 or GNU GPL
9 // Copyright 2003 Ximian, Inc.
10 // Copyright 2003-2008 Novell, Inc.
13 // TODO:
14 // Flow analysis for Yield.
17 using System;
18 using System.Collections;
19 using System.Reflection;
20 using System.Reflection.Emit;
22 namespace Mono.CSharp {
24 public class Yield : ResumableStatement {
25 Expression expr;
26 bool unwind_protect;
27 Iterator iterator;
28 int resume_pc;
30 public Yield (Expression expr, Location l)
32 this.expr = expr;
33 loc = l;
36 public static bool CheckContext (ResolveContext ec, Location loc)
39 // We can't use `ec.InUnsafe' here because it's allowed to have an iterator
40 // inside an unsafe class. See test-martin-29.cs for an example.
42 if (!ec.CurrentAnonymousMethod.IsIterator) {
43 Report.Error (1621, loc,
44 "The yield statement cannot be used inside " +
45 "anonymous method blocks");
46 return false;
49 return true;
52 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
54 expr.MutateHoistedGenericType (storey);
57 public override bool Resolve (BlockContext ec)
59 expr = expr.Resolve (ec);
60 if (expr == null)
61 return false;
63 Report.Debug (64, "RESOLVE YIELD #1", this, ec, expr, expr.GetType (),
64 ec.CurrentAnonymousMethod, ec.CurrentIterator);
66 if (!CheckContext (ec, loc))
67 return false;
69 iterator = ec.CurrentIterator;
70 if (expr.Type != iterator.OriginalIteratorType) {
71 expr = Convert.ImplicitConversionRequired (
72 ec, expr, iterator.OriginalIteratorType, loc);
73 if (expr == null)
74 return false;
77 if (!ec.CurrentBranching.CurrentUsageVector.IsUnreachable)
78 unwind_protect = ec.CurrentBranching.AddResumePoint (this, loc, out resume_pc);
80 return true;
83 protected override void DoEmit (EmitContext ec)
85 iterator.MarkYield (ec, expr, resume_pc, unwind_protect, resume_point);
88 protected override void CloneTo (CloneContext clonectx, Statement t)
90 Yield target = (Yield) t;
92 target.expr = expr.Clone (clonectx);
96 public class YieldBreak : ExitStatement
98 Iterator iterator;
100 public YieldBreak (Location l)
102 loc = l;
105 public override void Error_FinallyClause ()
107 Report.Error (1625, loc, "Cannot yield in the body of a finally clause");
110 protected override void CloneTo (CloneContext clonectx, Statement target)
112 throw new NotSupportedException ();
115 protected override bool DoResolve (BlockContext ec)
117 iterator = ec.CurrentIterator;
118 return Yield.CheckContext (ec, loc);
121 protected override void DoEmit (EmitContext ec)
123 iterator.EmitYieldBreak (ec.ig, unwind_protect);
126 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
128 // nothing to do
133 // Wraps method block into iterator wrapper block
135 class IteratorStatement : Statement
137 Iterator iterator;
138 Block original_block;
140 public IteratorStatement (Iterator iterator, Block original_block)
142 this.iterator = iterator;
143 this.original_block = original_block;
144 this.loc = iterator.Location;
147 protected override void CloneTo (CloneContext clonectx, Statement target)
149 IteratorStatement t = (IteratorStatement) target;
150 t.original_block = (ExplicitBlock) original_block.Clone (clonectx);
151 t.iterator = (Iterator) iterator.Clone (clonectx);
154 public override bool Resolve (BlockContext ec)
156 ec.StartFlowBranching (iterator);
157 bool ok = original_block.Resolve (ec);
158 ec.EndFlowBranching ();
159 return ok;
162 protected override void DoEmit (EmitContext ec)
164 iterator.EmitMoveNext (ec, original_block);
167 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
169 original_block.MutateHoistedGenericType (storey);
170 iterator.MutateHoistedGenericType (storey);
174 public class IteratorStorey : AnonymousMethodStorey
176 class IteratorMethod : Method
178 readonly IteratorStorey host;
180 public IteratorMethod (IteratorStorey host, FullNamedExpression returnType, int mod, MemberName name)
181 : base (host, null, returnType, mod | Modifiers.DEBUGGER_HIDDEN | Modifiers.COMPILER_GENERATED,
182 name, ParametersCompiled.EmptyReadOnlyParameters, null)
184 this.host = host;
186 Block = new ToplevelBlock (host.Iterator.Container.Toplevel, ParametersCompiled.EmptyReadOnlyParameters, Location);
189 public override EmitContext CreateEmitContext (ILGenerator ig)
191 EmitContext ec = new EmitContext (
192 this, ig, MemberType);
194 ec.CurrentAnonymousMethod = host.Iterator;
195 return ec;
199 class GetEnumeratorMethod : IteratorMethod
201 sealed class GetEnumeratorStatement : Statement
203 IteratorStorey host;
204 IteratorMethod host_method;
206 Expression new_storey;
208 public GetEnumeratorStatement (IteratorStorey host, IteratorMethod host_method)
210 this.host = host;
211 this.host_method = host_method;
212 loc = host_method.Location;
215 protected override void CloneTo (CloneContext clonectx, Statement target)
217 throw new NotSupportedException ();
220 public override bool Resolve (BlockContext ec)
222 TypeExpression storey_type_expr = new TypeExpression (host.TypeBuilder, loc);
223 ArrayList init = null;
224 if (host.hoisted_this != null) {
225 init = new ArrayList (host.hoisted_params == null ? 1 : host.HoistedParameters.Count + 1);
226 HoistedThis ht = host.hoisted_this;
227 FieldExpr from = new FieldExpr (ht.Field.FieldBuilder, loc);
228 from.InstanceExpression = CompilerGeneratedThis.Instance;
229 init.Add (new ElementInitializer (ht.Field.Name, from, loc));
232 if (host.hoisted_params != null) {
233 if (init == null)
234 init = new ArrayList (host.HoistedParameters.Count);
236 for (int i = 0; i < host.hoisted_params.Count; ++i) {
237 HoistedParameter hp = (HoistedParameter) host.hoisted_params [i];
238 HoistedParameter hp_cp = (HoistedParameter) host.hoisted_params_copy [i];
240 FieldExpr from = new FieldExpr (hp_cp.Field.FieldBuilder, loc);
241 from.InstanceExpression = CompilerGeneratedThis.Instance;
243 init.Add (new ElementInitializer (hp.Field.Name, from, loc));
247 if (init != null) {
248 new_storey = new NewInitialize (storey_type_expr, null,
249 new CollectionOrObjectInitializers (init, loc), loc);
250 } else {
251 new_storey = new New (storey_type_expr, null, loc);
254 new_storey = new_storey.Resolve (ec);
255 if (new_storey != null)
256 new_storey = Convert.ImplicitConversionRequired (ec, new_storey, host_method.MemberType, loc);
258 if (TypeManager.int_interlocked_compare_exchange == null) {
259 Type t = TypeManager.CoreLookupType ("System.Threading", "Interlocked", Kind.Class, true);
260 if (t != null) {
261 TypeManager.int_interlocked_compare_exchange = TypeManager.GetPredefinedMethod (
262 t, "CompareExchange", loc, TypeManager.int32_type,
263 TypeManager.int32_type, TypeManager.int32_type);
267 ec.CurrentBranching.CurrentUsageVector.Goto ();
268 return true;
271 protected override void DoEmit (EmitContext ec)
273 ILGenerator ig = ec.ig;
274 Label label_init = ig.DefineLabel ();
276 ig.Emit (OpCodes.Ldarg_0);
277 ig.Emit (OpCodes.Ldflda, host.PC.FieldBuilder);
278 IntConstant.EmitInt (ig, (int) Iterator.State.Start);
279 IntConstant.EmitInt (ig, (int) Iterator.State.Uninitialized);
280 ig.Emit (OpCodes.Call, TypeManager.int_interlocked_compare_exchange);
282 IntConstant.EmitInt (ig, (int) Iterator.State.Uninitialized);
283 ig.Emit (OpCodes.Bne_Un_S, label_init);
285 ig.Emit (OpCodes.Ldarg_0);
286 ig.Emit (OpCodes.Ret);
288 ig.MarkLabel (label_init);
290 new_storey.Emit (ec);
291 ig.Emit (OpCodes.Ret);
294 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
296 throw new NotSupportedException ();
300 public GetEnumeratorMethod (IteratorStorey host, FullNamedExpression returnType, MemberName name)
301 : base (host, returnType, 0, name)
303 Block.AddStatement (new GetEnumeratorStatement (host, this));
307 class DisposeMethod : IteratorMethod
309 sealed class DisposeMethodStatement : Statement
311 Iterator iterator;
313 public DisposeMethodStatement (Iterator iterator)
315 this.iterator = iterator;
316 this.loc = iterator.Location;
319 protected override void CloneTo (CloneContext clonectx, Statement target)
321 throw new NotSupportedException ();
324 public override bool Resolve (BlockContext ec)
326 return true;
329 protected override void DoEmit (EmitContext ec)
331 iterator.EmitDispose (ec);
334 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
336 throw new NotSupportedException ();
340 public DisposeMethod (IteratorStorey host)
341 : base (host, TypeManager.system_void_expr, Modifiers.PUBLIC, new MemberName ("Dispose", host.Location))
343 host.AddMethod (this);
345 Block = new ToplevelBlock (host.Iterator.Container, ParametersCompiled.EmptyReadOnlyParameters, Location);
346 Block.AddStatement (new DisposeMethodStatement (host.Iterator));
351 // Uses Method as method info
353 class DynamicMethodGroupExpr : MethodGroupExpr
355 readonly Method method;
357 public DynamicMethodGroupExpr (Method method, Location loc)
358 : base (null, loc)
360 this.method = method;
363 public override Expression DoResolve (ResolveContext ec)
365 Methods = new MethodBase [] { method.MethodBuilder };
366 type = method.Parent.TypeBuilder;
367 InstanceExpression = new CompilerGeneratedThis (type, Location);
368 return base.DoResolve (ec);
372 class DynamicFieldExpr : FieldExpr
374 readonly Field field;
376 public DynamicFieldExpr (Field field, Location loc)
377 : base (loc)
379 this.field = field;
382 public override Expression DoResolve (ResolveContext ec)
384 FieldInfo = field.FieldBuilder;
385 type = TypeManager.TypeToCoreType (FieldInfo.FieldType);
386 InstanceExpression = new CompilerGeneratedThis (type, Location);
387 return base.DoResolve (ec);
391 public readonly Iterator Iterator;
393 TypeExpr iterator_type_expr;
394 Field pc_field;
395 Field current_field;
397 TypeExpr enumerator_type;
398 TypeExpr enumerable_type;
399 TypeArguments generic_args;
400 TypeExpr generic_enumerator_type;
401 TypeExpr generic_enumerable_type;
403 ArrayList hoisted_params_copy;
404 int local_name_idx;
406 public IteratorStorey (Iterator iterator)
407 : base (iterator.Container.Toplevel, iterator.Host,
408 iterator.OriginalMethod as MemberBase, iterator.GenericMethod, "Iterator")
410 this.Iterator = iterator;
413 public Field PC {
414 get { return pc_field; }
417 public Field CurrentField {
418 get { return current_field; }
421 public ArrayList HoistedParameters {
422 get { return hoisted_params; }
425 protected override TypeExpr [] ResolveBaseTypes (out TypeExpr base_class)
427 iterator_type_expr = new TypeExpression (MutateType (Iterator.OriginalIteratorType), Location);
428 generic_args = new TypeArguments (iterator_type_expr);
430 ArrayList list = new ArrayList ();
431 if (Iterator.IsEnumerable) {
432 enumerable_type = new TypeExpression (
433 TypeManager.ienumerable_type, Location);
434 list.Add (enumerable_type);
436 if (TypeManager.generic_ienumerable_type != null) {
437 generic_enumerable_type = new GenericTypeExpr (
438 TypeManager.generic_ienumerable_type,
439 generic_args, Location);
440 list.Add (generic_enumerable_type);
444 enumerator_type = new TypeExpression (
445 TypeManager.ienumerator_type, Location);
446 list.Add (enumerator_type);
448 list.Add (new TypeExpression (TypeManager.idisposable_type, Location));
450 if (TypeManager.generic_ienumerator_type != null) {
451 generic_enumerator_type = new GenericTypeExpr (
452 TypeManager.generic_ienumerator_type,
453 generic_args, Location);
454 list.Add (generic_enumerator_type);
457 type_bases = list;
459 return base.ResolveBaseTypes (out base_class);
462 protected override string GetVariableMangledName (LocalInfo local_info)
464 return "<" + local_info.Name + ">__" + local_name_idx++.ToString ();
467 public void DefineIteratorMembers ()
469 pc_field = AddCompilerGeneratedField ("$PC", TypeManager.system_int32_expr);
470 current_field = AddCompilerGeneratedField ("$current", iterator_type_expr);
472 if (hoisted_params != null) {
474 // Iterators are independent, each GetEnumerator call has to
475 // create same enumerator therefore we have to keep original values
476 // around for re-initialization
478 // TODO: Do it for assigned/modified parameters only
480 hoisted_params_copy = new ArrayList (hoisted_params.Count);
481 foreach (HoistedParameter hp in hoisted_params) {
482 hoisted_params_copy.Add (new HoistedParameter (hp, "<$>" + hp.Field.Name));
486 if (generic_enumerator_type != null)
487 Define_Current (true);
489 Define_Current (false);
490 new DisposeMethod (this);
491 Define_Reset ();
493 if (Iterator.IsEnumerable) {
494 MemberName name = new MemberName (QualifiedAliasMember.GlobalAlias, "System", null, Location);
495 name = new MemberName (name, "Collections", Location);
496 name = new MemberName (name, "IEnumerable", Location);
497 name = new MemberName (name, "GetEnumerator", Location);
499 if (generic_enumerator_type != null) {
500 Method get_enumerator = new IteratorMethod (this, enumerator_type, 0, name);
502 name = new MemberName (name.Left.Left, "Generic", Location);
503 name = new MemberName (name, "IEnumerable", generic_args, Location);
504 name = new MemberName (name, "GetEnumerator", Location);
505 Method gget_enumerator = new GetEnumeratorMethod (this, generic_enumerator_type, name);
508 // Just call generic GetEnumerator implementation
510 get_enumerator.Block.AddStatement (
511 new Return (new Invocation (new DynamicMethodGroupExpr (gget_enumerator, Location), null), Location));
513 AddMethod (get_enumerator);
514 AddMethod (gget_enumerator);
515 } else {
516 AddMethod (new GetEnumeratorMethod (this, enumerator_type, name));
521 protected override void EmitHoistedParameters (EmitContext ec, ArrayList hoisted)
523 base.EmitHoistedParameters (ec, hoisted);
524 base.EmitHoistedParameters (ec, hoisted_params_copy);
527 void Define_Current (bool is_generic)
529 TypeExpr type;
531 MemberName name = new MemberName (QualifiedAliasMember.GlobalAlias, "System", null, Location);
532 name = new MemberName (name, "Collections", Location);
534 if (is_generic) {
535 name = new MemberName (name, "Generic", Location);
536 name = new MemberName (name, "IEnumerator", generic_args, Location);
537 type = iterator_type_expr;
538 } else {
539 name = new MemberName (name, "IEnumerator");
540 type = TypeManager.system_object_expr;
543 name = new MemberName (name, "Current", Location);
545 ToplevelBlock get_block = new ToplevelBlock (Location);
546 get_block.AddStatement (new Return (new DynamicFieldExpr (CurrentField, Location), Location));
548 Accessor getter = new Accessor (get_block, 0, null, null, Location);
550 Property current = new Property (
551 this, type, Modifiers.DEBUGGER_HIDDEN, name, null, getter, null, false);
552 AddProperty (current);
555 void Define_Reset ()
557 Method reset = new Method (
558 this, null, TypeManager.system_void_expr,
559 Modifiers.PUBLIC | Modifiers.DEBUGGER_HIDDEN,
560 new MemberName ("Reset", Location),
561 ParametersCompiled.EmptyReadOnlyParameters, null);
562 AddMethod (reset);
564 reset.Block = new ToplevelBlock (Location);
566 Type ex_type = TypeManager.CoreLookupType ("System", "NotSupportedException", Kind.Class, true);
567 if (ex_type == null)
568 return;
570 reset.Block.AddStatement (new Throw (new New (new TypeExpression (ex_type, Location), null, Location), Location));
575 // Iterators are implemented as hidden anonymous block
577 public class Iterator : AnonymousExpression {
578 public readonly IMethodData OriginalMethod;
579 AnonymousMethodMethod method;
580 public readonly TypeContainer Host;
581 public readonly bool IsEnumerable;
584 // The state as we generate the iterator
586 Label move_next_ok, move_next_error;
587 LocalBuilder skip_finally, current_pc;
589 public LocalBuilder SkipFinally {
590 get { return skip_finally; }
593 public LocalBuilder CurrentPC {
594 get { return current_pc; }
597 public Block Container {
598 get { return OriginalMethod.Block; }
601 public GenericMethod GenericMethod {
602 get { return OriginalMethod.GenericMethod; }
605 public readonly Type OriginalIteratorType;
607 readonly IteratorStorey IteratorHost;
609 public enum State {
610 Running = -3, // Used only in CurrentPC, never stored into $PC
611 Uninitialized = -2,
612 After = -1,
613 Start = 0
616 public void EmitYieldBreak (ILGenerator ig, bool unwind_protect)
618 ig.Emit (unwind_protect ? OpCodes.Leave : OpCodes.Br, move_next_error);
621 void EmitMoveNext_NoResumePoints (EmitContext ec, Block original_block)
623 ILGenerator ig = ec.ig;
625 ig.Emit (OpCodes.Ldarg_0);
626 ig.Emit (OpCodes.Ldfld, IteratorHost.PC.FieldBuilder);
628 ig.Emit (OpCodes.Ldarg_0);
629 IntConstant.EmitInt (ig, (int) State.After);
630 ig.Emit (OpCodes.Stfld, IteratorHost.PC.FieldBuilder);
632 // We only care if the PC is zero (start executing) or non-zero (don't do anything)
633 ig.Emit (OpCodes.Brtrue, move_next_error);
635 SymbolWriter.StartIteratorBody (ec.ig);
636 original_block.Emit (ec);
637 SymbolWriter.EndIteratorBody (ec.ig);
639 ig.MarkLabel (move_next_error);
640 ig.Emit (OpCodes.Ldc_I4_0);
641 ig.Emit (OpCodes.Ret);
644 internal void EmitMoveNext (EmitContext ec, Block original_block)
646 ILGenerator ig = ec.ig;
648 move_next_ok = ig.DefineLabel ();
649 move_next_error = ig.DefineLabel ();
651 if (resume_points == null) {
652 EmitMoveNext_NoResumePoints (ec, original_block);
653 return;
656 current_pc = ec.GetTemporaryLocal (TypeManager.uint32_type);
657 ig.Emit (OpCodes.Ldarg_0);
658 ig.Emit (OpCodes.Ldfld, IteratorHost.PC.FieldBuilder);
659 ig.Emit (OpCodes.Stloc, current_pc);
661 // We're actually in state 'running', but this is as good a PC value as any if there's an abnormal exit
662 ig.Emit (OpCodes.Ldarg_0);
663 IntConstant.EmitInt (ig, (int) State.After);
664 ig.Emit (OpCodes.Stfld, IteratorHost.PC.FieldBuilder);
666 Label [] labels = new Label [1 + resume_points.Count];
667 labels [0] = ig.DefineLabel ();
669 bool need_skip_finally = false;
670 for (int i = 0; i < resume_points.Count; ++i) {
671 ResumableStatement s = (ResumableStatement) resume_points [i];
672 need_skip_finally |= s is ExceptionStatement;
673 labels [i+1] = s.PrepareForEmit (ec);
676 if (need_skip_finally) {
677 skip_finally = ec.GetTemporaryLocal (TypeManager.bool_type);
678 ig.Emit (OpCodes.Ldc_I4_0);
679 ig.Emit (OpCodes.Stloc, skip_finally);
682 SymbolWriter.StartIteratorDispatcher (ec.ig);
683 ig.Emit (OpCodes.Ldloc, current_pc);
684 ig.Emit (OpCodes.Switch, labels);
686 ig.Emit (OpCodes.Br, move_next_error);
687 SymbolWriter.EndIteratorDispatcher (ec.ig);
689 ig.MarkLabel (labels [0]);
691 SymbolWriter.StartIteratorBody (ec.ig);
692 original_block.Emit (ec);
693 SymbolWriter.EndIteratorBody (ec.ig);
695 SymbolWriter.StartIteratorDispatcher (ec.ig);
697 ig.Emit (OpCodes.Ldarg_0);
698 IntConstant.EmitInt (ig, (int) State.After);
699 ig.Emit (OpCodes.Stfld, IteratorHost.PC.FieldBuilder);
701 ig.MarkLabel (move_next_error);
702 ig.Emit (OpCodes.Ldc_I4_0);
703 ig.Emit (OpCodes.Ret);
705 ig.MarkLabel (move_next_ok);
706 ig.Emit (OpCodes.Ldc_I4_1);
707 ig.Emit (OpCodes.Ret);
709 SymbolWriter.EndIteratorDispatcher (ec.ig);
712 public void EmitDispose (EmitContext ec)
714 ILGenerator ig = ec.ig;
716 Label end = ig.DefineLabel ();
718 Label [] labels = null;
719 int n_resume_points = resume_points == null ? 0 : resume_points.Count;
720 for (int i = 0; i < n_resume_points; ++i) {
721 ResumableStatement s = (ResumableStatement) resume_points [i];
722 Label ret = s.PrepareForDispose (ec, end);
723 if (ret.Equals (end) && labels == null)
724 continue;
725 if (labels == null) {
726 labels = new Label [resume_points.Count + 1];
727 for (int j = 0; j <= i; ++j)
728 labels [j] = end;
730 labels [i+1] = ret;
733 if (labels != null) {
734 current_pc = ec.GetTemporaryLocal (TypeManager.uint32_type);
735 ig.Emit (OpCodes.Ldarg_0);
736 ig.Emit (OpCodes.Ldfld, IteratorHost.PC.FieldBuilder);
737 ig.Emit (OpCodes.Stloc, current_pc);
740 ig.Emit (OpCodes.Ldarg_0);
741 IntConstant.EmitInt (ig, (int) State.After);
742 ig.Emit (OpCodes.Stfld, IteratorHost.PC.FieldBuilder);
744 if (labels != null) {
745 //SymbolWriter.StartIteratorDispatcher (ec.ig);
746 ig.Emit (OpCodes.Ldloc, current_pc);
747 ig.Emit (OpCodes.Switch, labels);
748 //SymbolWriter.EndIteratorDispatcher (ec.ig);
750 foreach (ResumableStatement s in resume_points)
751 s.EmitForDispose (ec, this, end, true);
754 ig.MarkLabel (end);
758 ArrayList resume_points;
759 public int AddResumePoint (ResumableStatement stmt)
761 if (resume_points == null)
762 resume_points = new ArrayList ();
763 resume_points.Add (stmt);
764 return resume_points.Count;
768 // Called back from Yield
770 public void MarkYield (EmitContext ec, Expression expr, int resume_pc, bool unwind_protect, Label resume_point)
772 ILGenerator ig = ec.ig;
774 // Store the new current
775 ig.Emit (OpCodes.Ldarg_0);
776 expr.Emit (ec);
777 ig.Emit (OpCodes.Stfld, IteratorHost.CurrentField.FieldBuilder);
779 // store resume program-counter
780 ig.Emit (OpCodes.Ldarg_0);
781 IntConstant.EmitInt (ig, resume_pc);
782 ig.Emit (OpCodes.Stfld, IteratorHost.PC.FieldBuilder);
784 // mark finally blocks as disabled
785 if (unwind_protect && skip_finally != null) {
786 ig.Emit (OpCodes.Ldc_I4_1);
787 ig.Emit (OpCodes.Stloc, skip_finally);
790 // Return ok
791 ig.Emit (unwind_protect ? OpCodes.Leave : OpCodes.Br, move_next_ok);
793 ig.MarkLabel (resume_point);
796 public override string ContainerType {
797 get { return "iterator"; }
800 public override bool IsIterator {
801 get { return true; }
804 public override AnonymousMethodStorey Storey {
805 get { return IteratorHost; }
809 // Our constructor
811 private Iterator (IMethodData method, TypeContainer host, Type iterator_type, bool is_enumerable)
812 : base (
813 new ToplevelBlock (method.Block, ParametersCompiled.EmptyReadOnlyParameters, method.Block.StartLocation),
814 TypeManager.bool_type,
815 method.Location)
817 this.OriginalMethod = method;
818 this.OriginalIteratorType = iterator_type;
819 this.IsEnumerable = is_enumerable;
820 this.Host = host;
821 this.type = method.ReturnType;
823 IteratorHost = Block.ChangeToIterator (this, method.Block);
826 public override string GetSignatureForError ()
828 return OriginalMethod.GetSignatureForError ();
831 public override Expression DoResolve (ResolveContext ec)
833 method = new AnonymousMethodMethod (Storey,
834 this, Storey, null, TypeManager.system_boolean_expr,
835 Modifiers.PUBLIC, OriginalMethod.GetSignatureForError (),
836 new MemberName ("MoveNext", Location),
837 ParametersCompiled.EmptyReadOnlyParameters);
839 if (!Compatible (ec))
840 return null;
842 IteratorHost.DefineIteratorMembers ();
844 eclass = ExprClass.Value;
845 return this;
848 public override void Emit (EmitContext ec)
851 // Load Iterator storey instance
853 method.Storey.Instance.Emit (ec);
856 // Initialize iterator PC when it's unitialized
858 if (IsEnumerable) {
859 ILGenerator ig = ec.ig;
860 ig.Emit (OpCodes.Dup);
861 IntConstant.EmitInt (ig, (int)State.Uninitialized);
863 FieldInfo field = IteratorHost.PC.FieldBuilder;
864 #if GMCS_SOURCE
865 if (Storey.MemberName.IsGeneric)
866 field = TypeBuilder.GetField (Storey.Instance.Type, field);
867 #endif
868 ig.Emit (OpCodes.Stfld, field);
872 public override Expression CreateExpressionTree (ResolveContext ec)
874 throw new NotSupportedException ("ET");
877 public static void CreateIterator (IMethodData method, TypeContainer parent, int modifiers)
879 bool is_enumerable;
880 Type iterator_type;
882 Type ret = method.ReturnType;
883 if (ret == null)
884 return;
886 if (!CheckType (ret, out iterator_type, out is_enumerable)) {
887 Report.Error (1624, method.Location,
888 "The body of `{0}' cannot be an iterator block " +
889 "because `{1}' is not an iterator interface type",
890 method.GetSignatureForError (),
891 TypeManager.CSharpName (ret));
892 return;
895 ParametersCompiled parameters = method.ParameterInfo;
896 for (int i = 0; i < parameters.Count; i++) {
897 Parameter p = parameters [i];
898 Parameter.Modifier mod = p.ModFlags;
899 if ((mod & Parameter.Modifier.ISBYREF) != 0) {
900 Report.Error (1623, p.Location,
901 "Iterators cannot have ref or out parameters");
902 return;
905 if (p is ArglistParameter) {
906 Report.Error (1636, method.Location,
907 "__arglist is not allowed in parameter list of iterators");
908 return;
911 if (parameters.Types [i].IsPointer) {
912 Report.Error (1637, p.Location,
913 "Iterators cannot have unsafe parameters or " +
914 "yield types");
915 return;
919 if ((modifiers & Modifiers.UNSAFE) != 0) {
920 Report.Error (1629, method.Location, "Unsafe code may not appear in iterators");
921 return;
924 Iterator iter = new Iterator (method, parent, iterator_type, is_enumerable);
925 iter.Storey.DefineType ();
928 static bool CheckType (Type ret, out Type original_iterator_type, out bool is_enumerable)
930 original_iterator_type = null;
931 is_enumerable = false;
933 if (ret == TypeManager.ienumerable_type) {
934 original_iterator_type = TypeManager.object_type;
935 is_enumerable = true;
936 return true;
938 if (ret == TypeManager.ienumerator_type) {
939 original_iterator_type = TypeManager.object_type;
940 is_enumerable = false;
941 return true;
944 if (!TypeManager.IsGenericType (ret))
945 return false;
947 Type[] args = TypeManager.GetTypeArguments (ret);
948 if (args.Length != 1)
949 return false;
951 Type gt = TypeManager.DropGenericTypeArguments (ret);
952 if (gt == TypeManager.generic_ienumerable_type) {
953 original_iterator_type = TypeManager.TypeToCoreType (args [0]);
954 is_enumerable = true;
955 return true;
958 if (gt == TypeManager.generic_ienumerator_type) {
959 original_iterator_type = TypeManager.TypeToCoreType (args [0]);
960 is_enumerable = false;
961 return true;
964 return false;