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;
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 (EmitContext ec, Location loc)
38 for (Block block = ec.CurrentBlock; block != null; block = block.Parent) {
42 Report.Error (1629, loc, "Unsafe code may not appear in iterators");
47 // We can't use `ec.InUnsafe' here because it's allowed to have an iterator
48 // inside an unsafe class. See test-martin-29.cs for an example.
50 if (!ec.CurrentAnonymousMethod.IsIterator) {
51 Report.Error (1621, loc,
52 "The yield statement cannot be used inside " +
53 "anonymous method blocks");
60 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
62 expr.MutateHoistedGenericType (storey);
65 public override bool Resolve (EmitContext ec)
67 expr = expr.Resolve (ec);
71 Report.Debug (64, "RESOLVE YIELD #1", this, ec, expr, expr.GetType (),
72 ec.CurrentAnonymousMethod, ec.CurrentIterator);
74 if (!CheckContext (ec, loc))
77 Iterator iterator = ec.CurrentIterator;
78 if (expr.Type != iterator.OriginalIteratorType) {
79 expr = Convert.ImplicitConversionRequired (
80 ec, expr, iterator.OriginalIteratorType, loc);
85 if (!ec.CurrentBranching.CurrentUsageVector.IsUnreachable)
86 unwind_protect = ec.CurrentBranching.AddResumePoint (this, loc, out resume_pc);
91 protected override void DoEmit (EmitContext ec)
93 ec.CurrentIterator.MarkYield (ec, expr, resume_pc, unwind_protect, resume_point);
96 protected override void CloneTo (CloneContext clonectx, Statement t)
98 Yield target = (Yield) t;
100 target.expr = expr.Clone (clonectx);
104 public class YieldBreak : ExitStatement {
105 public YieldBreak (Location l)
110 public override void Error_FinallyClause ()
112 Report.Error (1625, loc, "Cannot yield in the body of a finally clause");
115 protected override void CloneTo (CloneContext clonectx, Statement target)
117 throw new NotSupportedException ();
120 protected override bool DoResolve (EmitContext ec)
122 return Yield.CheckContext (ec, loc);
125 protected override void DoEmit (EmitContext ec)
127 ec.CurrentIterator.EmitYieldBreak (ec.ig, unwind_protect);
130 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
137 // Wraps method block into iterator wrapper block
139 class IteratorStatement : Statement
142 ExplicitBlock original_block;
144 public IteratorStatement (Iterator iterator, ExplicitBlock original_block)
146 this.iterator = iterator;
147 this.original_block = original_block;
148 this.loc = iterator.Location;
151 protected override void CloneTo (CloneContext clonectx, Statement target)
153 IteratorStatement t = (IteratorStatement) target;
154 t.original_block = (ExplicitBlock) original_block.Clone (clonectx);
155 t.iterator = (Iterator) iterator.Clone (clonectx);
158 public override bool Resolve (EmitContext ec)
160 ec.StartFlowBranching (iterator);
161 bool ok = original_block.Resolve (ec);
162 ec.EndFlowBranching ();
166 protected override void DoEmit (EmitContext ec)
168 iterator.EmitMoveNext (ec, original_block);
171 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
173 original_block.MutateHoistedGenericType (storey);
174 iterator.MutateHoistedGenericType (storey);
178 public class IteratorStorey : AnonymousMethodStorey
180 class IteratorMethod : Method
182 readonly IteratorStorey host;
184 public IteratorMethod (IteratorStorey host, FullNamedExpression returnType, int mod, MemberName name)
185 : base (host, null, returnType, mod | Modifiers.DEBUGGER_HIDDEN | Modifiers.COMPILER_GENERATED,
186 name, Parameters.EmptyReadOnlyParameters, null)
190 Block = new ToplevelBlock (host.Iterator.Container.Toplevel, null, Location);
193 public override EmitContext CreateEmitContext (DeclSpace tc, ILGenerator ig)
195 EmitContext ec = new EmitContext (
196 this, tc, this.ds, Location, ig, MemberType, ModFlags, false);
198 ec.CurrentAnonymousMethod = host.Iterator;
203 class GetEnumeratorMethod : IteratorMethod
205 sealed class GetEnumeratorStatement : Statement
208 IteratorMethod host_method;
210 Expression new_storey;
212 public GetEnumeratorStatement (IteratorStorey host, IteratorMethod host_method)
215 this.host_method = host_method;
216 loc = host_method.Location;
219 protected override void CloneTo (CloneContext clonectx, Statement target)
221 throw new NotSupportedException ();
224 public override bool Resolve (EmitContext ec)
226 TypeExpression storey_type_expr = new TypeExpression (host.TypeBuilder, loc);
227 ArrayList init = null;
228 if (host.hoisted_this != null) {
229 init = new ArrayList (host.hoisted_params == null ? 1 : host.HoistedParameters.Count + 1);
230 HoistedThis ht = host.hoisted_this;
231 FieldExpr from = new FieldExpr (ht.Field.FieldBuilder, loc);
232 from.InstanceExpression = CompilerGeneratedThis.Instance;
233 init.Add (new ElementInitializer (ht.Field.Name, from, loc));
236 if (host.hoisted_params != null) {
238 init = new ArrayList (host.HoistedParameters.Count);
240 for (int i = 0; i < host.hoisted_params.Count; ++i) {
241 HoistedParameter hp = (HoistedParameter) host.hoisted_params [i];
242 HoistedParameter hp_cp = (HoistedParameter) host.hoisted_params_copy [i];
244 FieldExpr from = new FieldExpr (hp_cp.Field.FieldBuilder, loc);
245 from.InstanceExpression = CompilerGeneratedThis.Instance;
247 init.Add (new ElementInitializer (hp.Field.Name, from, loc));
252 new_storey = new NewInitialize (storey_type_expr, new ArrayList (0),
253 new CollectionOrObjectInitializers (init, loc), loc);
255 new_storey = new New (storey_type_expr, new ArrayList (0), loc);
258 new_storey = new_storey.Resolve (ec);
259 if (new_storey != null)
260 new_storey = Convert.ImplicitConversionRequired (ec, new_storey, host_method.MemberType, loc);
262 if (TypeManager.int_interlocked_compare_exchange == null) {
263 Type t = TypeManager.CoreLookupType ("System.Threading", "Interlocked", Kind.Class, true);
265 TypeManager.int_interlocked_compare_exchange = TypeManager.GetPredefinedMethod (
266 t, "CompareExchange", loc, TypeManager.int32_type,
267 TypeManager.int32_type, TypeManager.int32_type);
271 ec.CurrentBranching.CurrentUsageVector.Goto ();
275 protected override void DoEmit (EmitContext ec)
277 ILGenerator ig = ec.ig;
278 Label label_init = ig.DefineLabel ();
280 ig.Emit (OpCodes.Ldarg_0);
281 ig.Emit (OpCodes.Ldflda, host.PC.FieldBuilder);
282 IntConstant.EmitInt (ig, (int) Iterator.State.Start);
283 IntConstant.EmitInt (ig, (int) Iterator.State.Uninitialized);
284 ig.Emit (OpCodes.Call, TypeManager.int_interlocked_compare_exchange);
286 IntConstant.EmitInt (ig, (int) Iterator.State.Uninitialized);
287 ig.Emit (OpCodes.Bne_Un_S, label_init);
289 ig.Emit (OpCodes.Ldarg_0);
290 ig.Emit (OpCodes.Ret);
292 ig.MarkLabel (label_init);
294 new_storey.Emit (ec);
295 ig.Emit (OpCodes.Ret);
298 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
300 throw new NotSupportedException ();
304 public GetEnumeratorMethod (IteratorStorey host, FullNamedExpression returnType, MemberName name)
305 : base (host, returnType, 0, name)
307 Block.AddStatement (new GetEnumeratorStatement (host, this));
311 class DisposeMethod : IteratorMethod
313 sealed class DisposeMethodStatement : Statement
317 public DisposeMethodStatement (Iterator iterator)
319 this.iterator = iterator;
320 this.loc = iterator.Location;
323 protected override void CloneTo (CloneContext clonectx, Statement target)
325 throw new NotSupportedException ();
328 public override bool Resolve (EmitContext ec)
333 protected override void DoEmit (EmitContext ec)
335 iterator.EmitDispose (ec);
338 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
340 throw new NotSupportedException ();
344 public DisposeMethod (IteratorStorey host)
345 : base (host, TypeManager.system_void_expr, Modifiers.PUBLIC, new MemberName ("Dispose", host.Location))
347 host.AddMethod (this);
349 Block = new ToplevelBlock (host.Iterator.Container, null, Location);
350 Block.AddStatement (new DisposeMethodStatement (host.Iterator));
355 // Uses Method as method info
357 class DynamicMethodGroupExpr : MethodGroupExpr
359 readonly Method method;
361 public DynamicMethodGroupExpr (Method method, Location loc)
364 this.method = method;
367 public override Expression DoResolve (EmitContext ec)
369 Methods = new MethodBase [] { method.MethodBuilder };
370 type = method.Parent.TypeBuilder;
371 InstanceExpression = new CompilerGeneratedThis (type, Location);
372 return base.DoResolve (ec);
376 class DynamicFieldExpr : FieldExpr
378 readonly Field field;
380 public DynamicFieldExpr (Field field, Location loc)
386 public override Expression DoResolve (EmitContext ec)
388 FieldInfo = field.FieldBuilder;
389 type = TypeManager.TypeToCoreType (FieldInfo.FieldType);
390 InstanceExpression = new CompilerGeneratedThis (type, Location);
391 return base.DoResolve (ec);
395 public readonly Iterator Iterator;
397 TypeExpr iterator_type_expr;
401 TypeExpr enumerator_type;
402 TypeExpr enumerable_type;
404 TypeArguments generic_args;
405 TypeExpr generic_enumerator_type;
406 TypeExpr generic_enumerable_type;
408 const TypeArguments generic_args = null;
411 ArrayList hoisted_params_copy;
414 public IteratorStorey (Iterator iterator)
415 : base (iterator.Container.Toplevel, iterator.Host,
416 iterator.OriginalMethod as MemberBase, iterator.GenericMethod, "Iterator")
418 this.Iterator = iterator;
419 HasHoistedVariables = true;
423 get { return pc_field; }
426 public Field CurrentField {
427 get { return current_field; }
430 public ArrayList HoistedParameters {
431 get { return hoisted_params; }
434 protected override TypeExpr [] ResolveBaseTypes (out TypeExpr base_class)
436 iterator_type_expr = new TypeExpression (MutateType (Iterator.OriginalIteratorType), Location);
439 generic_args = new TypeArguments (iterator_type_expr);
442 ArrayList list = new ArrayList ();
443 if (Iterator.IsEnumerable) {
444 enumerable_type = new TypeExpression (
445 TypeManager.ienumerable_type, Location);
446 list.Add (enumerable_type);
449 generic_enumerable_type = new GenericTypeExpr (
450 TypeManager.generic_ienumerable_type,
451 generic_args, Location);
452 list.Add (generic_enumerable_type);
456 enumerator_type = new TypeExpression (
457 TypeManager.ienumerator_type, Location);
458 list.Add (enumerator_type);
460 list.Add (new TypeExpression (TypeManager.idisposable_type, Location));
463 generic_enumerator_type = new GenericTypeExpr (
464 TypeManager.generic_ienumerator_type,
465 generic_args, Location);
466 list.Add (generic_enumerator_type);
471 return base.ResolveBaseTypes (out base_class);
474 protected override string GetVariableMangledName (LocalInfo local_info)
476 return "<" + local_info.Name + ">__" + local_name_idx++.ToString ();
479 public void DefineIteratorMembers ()
481 pc_field = AddCompilerGeneratedField ("$PC", TypeManager.system_int32_expr);
482 current_field = AddCompilerGeneratedField ("$current", iterator_type_expr);
484 if (hoisted_params != null) {
486 // Iterators are independent, each GetEnumerator call has to
487 // create same enumerator therefore we have to keep original values
488 // around for re-initialization
490 // TODO: Do it for assigned/modified parameters only
492 hoisted_params_copy = new ArrayList (hoisted_params.Count);
493 foreach (HoistedParameter hp in hoisted_params) {
494 hoisted_params_copy.Add (new HoistedParameter (hp, "<$>" + hp.Field.Name));
499 Define_Current (true);
501 Define_Current (false);
502 new DisposeMethod (this);
505 if (Iterator.IsEnumerable) {
506 MemberName name = new MemberName (
507 new MemberName ("System.Collections.IEnumerable", Location), "GetEnumerator", Location);
510 Method get_enumerator = new IteratorMethod (this, enumerator_type, 0, name);
512 name = new MemberName (
513 new MemberName ("System.Collections.Generic.IEnumerable", generic_args, Location), "GetEnumerator", Location);
514 Method gget_enumerator = new GetEnumeratorMethod (this, generic_enumerator_type, name);
517 // Just call generic GetEnumerator implementation
519 get_enumerator.Block.AddStatement (
520 new Return (new Invocation (new DynamicMethodGroupExpr (gget_enumerator, Location), new ArrayList (0)), Location));
522 AddMethod (get_enumerator);
523 AddMethod (gget_enumerator);
525 AddMethod (new GetEnumeratorMethod (this, enumerator_type, name));
530 protected override void EmitHoistedParameters (EmitContext ec, ArrayList hoisted)
532 base.EmitHoistedParameters (ec, hoisted);
533 base.EmitHoistedParameters (ec, hoisted_params_copy);
536 void Define_Current (bool is_generic)
542 left = new MemberName (
543 "System.Collections.Generic.IEnumerator",
544 generic_args, Location);
545 type = iterator_type_expr;
547 left = new MemberName ("System.Collections.IEnumerator", Location);
548 type = TypeManager.system_object_expr;
551 MemberName name = new MemberName (left, "Current", Location);
553 ToplevelBlock get_block = new ToplevelBlock (Location);
554 get_block.AddStatement (new Return (new DynamicFieldExpr (CurrentField, Location), Location));
556 Accessor getter = new Accessor (get_block, 0, null, null, Location);
558 Property current = new Property (
559 this, type, Modifiers.DEBUGGER_HIDDEN, name, null, getter, null, false);
560 AddProperty (current);
565 Method reset = new Method (
566 this, null, TypeManager.system_void_expr,
567 Modifiers.PUBLIC | Modifiers.DEBUGGER_HIDDEN,
568 new MemberName ("Reset", Location),
569 Parameters.EmptyReadOnlyParameters, null);
572 reset.Block = new ToplevelBlock (Location);
574 TypeExpr ex_type = new TypeLookupExpression ("System.NotSupportedException");
575 reset.Block.AddStatement (new Throw (new New (ex_type, null, Location), Location));
580 // Iterators are implemented as hidden anonymous block
582 public class Iterator : AnonymousExpression {
583 public readonly IMethodData OriginalMethod;
584 AnonymousMethodMethod method;
585 public readonly TypeContainer Host;
586 public readonly bool IsEnumerable;
589 // The state as we generate the iterator
591 Label move_next_ok, move_next_error;
592 LocalBuilder skip_finally, current_pc;
594 public LocalBuilder SkipFinally {
595 get { return skip_finally; }
598 public LocalBuilder CurrentPC {
599 get { return current_pc; }
602 public Block Container {
603 get { return OriginalMethod.Block; }
606 public GenericMethod GenericMethod {
607 get { return OriginalMethod.GenericMethod; }
610 public readonly Type OriginalIteratorType;
612 readonly IteratorStorey IteratorHost;
615 Running = -3, // Used only in CurrentPC, never stored into $PC
621 public override void AddStoreyReference (AnonymousMethodStorey storey)
626 public void EmitYieldBreak (ILGenerator ig, bool unwind_protect)
628 ig.Emit (unwind_protect ? OpCodes.Leave : OpCodes.Br, move_next_error);
631 void EmitMoveNext_NoResumePoints (EmitContext ec, Block original_block)
633 ILGenerator ig = ec.ig;
635 ig.Emit (OpCodes.Ldarg_0);
636 ig.Emit (OpCodes.Ldfld, IteratorHost.PC.FieldBuilder);
638 ig.Emit (OpCodes.Ldarg_0);
639 IntConstant.EmitInt (ig, (int) State.After);
640 ig.Emit (OpCodes.Stfld, IteratorHost.PC.FieldBuilder);
642 // We only care if the PC is zero (start executing) or non-zero (don't do anything)
643 ig.Emit (OpCodes.Brtrue, move_next_error);
645 SymbolWriter.StartIteratorBody (ec.ig);
646 original_block.Emit (ec);
647 SymbolWriter.EndIteratorBody (ec.ig);
649 ig.MarkLabel (move_next_error);
650 ig.Emit (OpCodes.Ldc_I4_0);
651 ig.Emit (OpCodes.Ret);
654 internal void EmitMoveNext (EmitContext ec, Block original_block)
656 ILGenerator ig = ec.ig;
658 move_next_ok = ig.DefineLabel ();
659 move_next_error = ig.DefineLabel ();
661 if (resume_points == null) {
662 EmitMoveNext_NoResumePoints (ec, original_block);
666 current_pc = ec.GetTemporaryLocal (TypeManager.uint32_type);
667 ig.Emit (OpCodes.Ldarg_0);
668 ig.Emit (OpCodes.Ldfld, IteratorHost.PC.FieldBuilder);
669 ig.Emit (OpCodes.Stloc, current_pc);
671 // We're actually in state 'running', but this is as good a PC value as any if there's an abnormal exit
672 ig.Emit (OpCodes.Ldarg_0);
673 IntConstant.EmitInt (ig, (int) State.After);
674 ig.Emit (OpCodes.Stfld, IteratorHost.PC.FieldBuilder);
676 Label [] labels = new Label [1 + resume_points.Count];
677 labels [0] = ig.DefineLabel ();
679 bool need_skip_finally = false;
680 for (int i = 0; i < resume_points.Count; ++i) {
681 ResumableStatement s = (ResumableStatement) resume_points [i];
682 need_skip_finally |= s is ExceptionStatement;
683 labels [i+1] = s.PrepareForEmit (ec);
686 if (need_skip_finally) {
687 skip_finally = ec.GetTemporaryLocal (TypeManager.bool_type);
688 ig.Emit (OpCodes.Ldc_I4_0);
689 ig.Emit (OpCodes.Stloc, skip_finally);
692 SymbolWriter.StartIteratorDispatcher (ec.ig);
693 ig.Emit (OpCodes.Ldloc, current_pc);
694 ig.Emit (OpCodes.Switch, labels);
696 ig.Emit (OpCodes.Br, move_next_error);
697 SymbolWriter.EndIteratorDispatcher (ec.ig);
699 ig.MarkLabel (labels [0]);
701 SymbolWriter.StartIteratorBody (ec.ig);
702 original_block.Emit (ec);
703 SymbolWriter.EndIteratorBody (ec.ig);
705 SymbolWriter.StartIteratorDispatcher (ec.ig);
707 ig.Emit (OpCodes.Ldarg_0);
708 IntConstant.EmitInt (ig, (int) State.After);
709 ig.Emit (OpCodes.Stfld, IteratorHost.PC.FieldBuilder);
711 ig.MarkLabel (move_next_error);
712 ig.Emit (OpCodes.Ldc_I4_0);
713 ig.Emit (OpCodes.Ret);
715 ig.MarkLabel (move_next_ok);
716 ig.Emit (OpCodes.Ldc_I4_1);
717 ig.Emit (OpCodes.Ret);
719 SymbolWriter.EndIteratorDispatcher (ec.ig);
722 public void EmitDispose (EmitContext ec)
724 ILGenerator ig = ec.ig;
726 Label end = ig.DefineLabel ();
728 Label [] labels = null;
729 int n_resume_points = resume_points == null ? 0 : resume_points.Count;
730 for (int i = 0; i < n_resume_points; ++i) {
731 ResumableStatement s = (ResumableStatement) resume_points [i];
732 Label ret = s.PrepareForDispose (ec, end);
733 if (ret.Equals (end) && labels == null)
735 if (labels == null) {
736 labels = new Label [resume_points.Count + 1];
737 for (int j = 0; j <= i; ++j)
743 if (labels != null) {
744 current_pc = ec.GetTemporaryLocal (TypeManager.uint32_type);
745 ig.Emit (OpCodes.Ldarg_0);
746 ig.Emit (OpCodes.Ldfld, IteratorHost.PC.FieldBuilder);
747 ig.Emit (OpCodes.Stloc, current_pc);
750 ig.Emit (OpCodes.Ldarg_0);
751 IntConstant.EmitInt (ig, (int) State.After);
752 ig.Emit (OpCodes.Stfld, IteratorHost.PC.FieldBuilder);
754 if (labels != null) {
755 //SymbolWriter.StartIteratorDispatcher (ec.ig);
756 ig.Emit (OpCodes.Ldloc, current_pc);
757 ig.Emit (OpCodes.Switch, labels);
758 //SymbolWriter.EndIteratorDispatcher (ec.ig);
760 foreach (ResumableStatement s in resume_points)
761 s.EmitForDispose (ec, this, end, true);
768 ArrayList resume_points;
769 public int AddResumePoint (ResumableStatement stmt)
771 if (resume_points == null)
772 resume_points = new ArrayList ();
773 resume_points.Add (stmt);
774 return resume_points.Count;
778 // Called back from Yield
780 public void MarkYield (EmitContext ec, Expression expr, int resume_pc, bool unwind_protect, Label resume_point)
782 ILGenerator ig = ec.ig;
784 // Store the new current
785 ig.Emit (OpCodes.Ldarg_0);
787 ig.Emit (OpCodes.Stfld, IteratorHost.CurrentField.FieldBuilder);
789 // store resume program-counter
790 ig.Emit (OpCodes.Ldarg_0);
791 IntConstant.EmitInt (ig, resume_pc);
792 ig.Emit (OpCodes.Stfld, IteratorHost.PC.FieldBuilder);
794 // mark finally blocks as disabled
795 if (unwind_protect && skip_finally != null) {
796 ig.Emit (OpCodes.Ldc_I4_1);
797 ig.Emit (OpCodes.Stloc, skip_finally);
801 ig.Emit (unwind_protect ? OpCodes.Leave : OpCodes.Br, move_next_ok);
803 ig.MarkLabel (resume_point);
806 public override string ContainerType {
807 get { return "iterator"; }
810 public override bool IsIterator {
814 public override AnonymousMethodStorey Storey {
815 get { return IteratorHost; }
821 private Iterator (IMethodData method, TypeContainer host, Type iterator_type, bool is_enumerable)
823 new ToplevelBlock (method.Block, Parameters.EmptyReadOnlyParameters, method.Block.StartLocation),
824 TypeManager.bool_type,
827 this.OriginalMethod = method;
828 this.OriginalIteratorType = iterator_type;
829 this.IsEnumerable = is_enumerable;
832 IteratorHost = Block.ChangeToIterator (this, method.Block);
835 public override string GetSignatureForError ()
837 return OriginalMethod.GetSignatureForError ();
840 public override Expression DoResolve (EmitContext ec)
842 method = new AnonymousMethodMethod (Storey,
843 this, Storey, null, TypeManager.system_boolean_expr,
844 Modifiers.PUBLIC, OriginalMethod.GetSignatureForError (),
845 new MemberName ("MoveNext", Location),
846 Parameters.EmptyReadOnlyParameters);
848 if (!Compatible (ec))
851 IteratorHost.DefineIteratorMembers ();
853 eclass = ExprClass.Value;
854 type = ec.ReturnType;
858 public override void Emit (EmitContext ec)
861 // Load Iterator storey instance
863 method.Storey.Instance.Emit (ec);
866 // Initialize iterator PC when it's unitialized
869 ILGenerator ig = ec.ig;
870 ig.Emit (OpCodes.Dup);
871 IntConstant.EmitInt (ig, (int)State.Uninitialized);
873 FieldInfo field = IteratorHost.PC.FieldBuilder;
875 if (Storey.MemberName.IsGeneric)
876 field = TypeBuilder.GetField (Storey.Instance.Type, field);
878 ig.Emit (OpCodes.Stfld, field);
882 public override Expression CreateExpressionTree (EmitContext ec)
884 throw new NotSupportedException ("ET");
887 public static void CreateIterator (IMethodData method, TypeContainer parent, int modifiers)
892 Type ret = method.ReturnType;
896 if (!CheckType (ret, out iterator_type, out is_enumerable)) {
897 Report.Error (1624, method.Location,
898 "The body of `{0}' cannot be an iterator block " +
899 "because `{1}' is not an iterator interface type",
900 method.GetSignatureForError (),
901 TypeManager.CSharpName (ret));
905 Parameters parameters = method.ParameterInfo;
906 for (int i = 0; i < parameters.Count; i++) {
907 Parameter p = parameters [i];
908 Parameter.Modifier mod = p.ModFlags;
909 if ((mod & Parameter.Modifier.ISBYREF) != 0) {
910 Report.Error (1623, p.Location,
911 "Iterators cannot have ref or out parameters");
915 if ((mod & Parameter.Modifier.ARGLIST) != 0) {
916 Report.Error (1636, method.Location,
917 "__arglist is not allowed in parameter list of iterators");
921 if (parameters.Types [i].IsPointer) {
922 Report.Error (1637, p.Location,
923 "Iterators cannot have unsafe parameters or " +
929 if ((modifiers & Modifiers.UNSAFE) != 0) {
930 Report.Error (1629, method.Location, "Unsafe code may not appear in iterators");
934 Iterator iter = new Iterator (method, parent, iterator_type, is_enumerable);
935 iter.Storey.DefineType ();
938 static bool CheckType (Type ret, out Type original_iterator_type, out bool is_enumerable)
940 original_iterator_type = null;
941 is_enumerable = false;
943 if (ret == TypeManager.ienumerable_type) {
944 original_iterator_type = TypeManager.object_type;
945 is_enumerable = true;
948 if (ret == TypeManager.ienumerator_type) {
949 original_iterator_type = TypeManager.object_type;
950 is_enumerable = false;
954 if (!TypeManager.IsGenericType (ret))
957 Type[] args = TypeManager.GetTypeArguments (ret);
958 if (args.Length != 1)
961 Type gt = TypeManager.DropGenericTypeArguments (ret);
962 if (gt == TypeManager.generic_ienumerable_type) {
963 original_iterator_type = args [0];
964 is_enumerable = true;
968 if (gt == TypeManager.generic_ienumerator_type) {
969 original_iterator_type = args [0];
970 is_enumerable = false;