namespace Mono.CSharp {
- public interface IIteratorContainer {
-
- //
- // Invoked if a yield statement is found in the body
- //
- void SetYields ();
- }
-
public class Yield : Statement {
Expression expr;
ArrayList finally_blocks;
Report.Error (1625, loc, "Cannot yield in the body of a " +
"finally clause");
return false;
- }
-
- if (ec.InUnsafe) {
+ }
+
+ for (Block block = ec.CurrentBlock; block != null; block = block.Parent) {
+ if (!block.Unsafe)
+ continue;
+
Report.Error (1629, loc, "Unsafe code may not appear in iterators");
return false;
}
- AnonymousContainer am = ec.CurrentAnonymousMethod;
- if ((am != null) && !am.IsIterator){
- Report.Error (1621, loc, "The yield statement cannot be used inside anonymous method blocks");
+ //
+ // We can't use `ec.InUnsafe' here because it's allowed to have an iterator
+ // inside an unsafe class. See test-martin-29.cs for an example.
+ //
+ if (!ec.CurrentAnonymousMethod.IsIterator) {
+ Report.Error (1621, loc,
+ "The yield statement cannot be used inside " +
+ "anonymous method blocks");
return false;
}
if (ec.CurrentBranching.InTryWithCatch () && (!isYieldBreak || !ec.InCatch)) {
if (!ec.InCatch)
- Report.Error (1626, loc, "Cannot yield a value in the body of a " +
- "try block with a catch clause");
+ Report.Error (1626, loc, "Cannot yield a value in the body " +
+ "of a try block with a catch clause");
else
- Report.Error (1631, loc, "Cannot yield a value in the body of a catch clause");
+ Report.Error (1631, loc, "Cannot yield a value in the body " +
+ "of a catch clause");
return false;
}
+
return true;
}
public override bool Resolve (EmitContext ec)
{
+ Report.Debug (64, "RESOLVE YIELD", this, ec, expr, expr.GetType ());
expr = expr.Resolve (ec);
if (expr == null)
return false;
+ Report.Debug (64, "RESOLVE YIELD #1", this, ec, expr, expr.GetType (),
+ ec.CurrentAnonymousMethod, ec.CurrentIterator);
+
if (!CheckContext (ec, loc, false))
return false;
Iterator iterator = ec.CurrentIterator;
-
- if (expr.Type != iterator.IteratorType){
+ if (expr.Type != iterator.IteratorType) {
expr = Convert.ImplicitConversionRequired (
ec, expr, iterator.IteratorType, loc);
if (expr == null)
{
ec.CurrentIterator.MarkYield (ec, expr, finally_blocks);
}
+
+ protected override void CloneTo (CloneContext clonectx, Statement t)
+ {
+ Yield target = (Yield) t;
+
+ target.expr = expr.Clone (clonectx);
+ }
}
public class YieldBreak : Statement {
}
}
- public class Iterator : Class {
- protected ToplevelBlock original_block;
- protected ToplevelBlock block;
+ public class IteratorHost : RootScopeInfo
+ {
+ public readonly Iterator Iterator;
- Type iterator_type;
TypeExpr iterator_type_expr;
- bool is_enumerable;
+ Field pc_field;
+ Field current_field;
+ MethodInfo dispose_method;
+
+ TypeExpr enumerator_type;
+ TypeExpr enumerable_type;
+ TypeArguments generic_args;
+ TypeExpr generic_enumerator_type;
+#if GMCS_SOURCE
+ TypeExpr generic_enumerable_type;
+#endif
+
+ public IteratorHost (Iterator iterator)
+ : base (iterator.Container.Toplevel, iterator.Host, iterator.GenericMethod,
+ iterator.Location)
+ {
+ this.Iterator = iterator;
+ }
+
+ public override bool IsIterator {
+ get { return true; }
+ }
+
+ public MethodInfo Dispose {
+ get { return dispose_method; }
+ }
+
+ public Field PC {
+ get { return pc_field; }
+ }
+
+ public Field CurrentField {
+ get { return current_field; }
+ }
+
+ public Type IteratorType {
+ get { return iterator_type_expr.Type; }
+ }
+
+ public override TypeExpr [] GetClassBases (out TypeExpr base_class)
+ {
+ iterator_type_expr = InflateType (Iterator.OriginalIteratorType);
+
+#if GMCS_SOURCE
+ generic_args = new TypeArguments (Location);
+ generic_args.Add (iterator_type_expr);
+#endif
+
+ ArrayList list = new ArrayList ();
+ if (Iterator.IsEnumerable) {
+ enumerable_type = new TypeExpression (
+ TypeManager.ienumerable_type, Location);
+ list.Add (enumerable_type);
+
+#if GMCS_SOURCE
+ generic_enumerable_type = new ConstructedType (
+ TypeManager.generic_ienumerable_type,
+ generic_args, Location);
+ list.Add (generic_enumerable_type);
+#endif
+ }
+
+ enumerator_type = new TypeExpression (
+ TypeManager.ienumerator_type, Location);
+ list.Add (enumerator_type);
+
+ list.Add (new TypeExpression (TypeManager.idisposable_type, Location));
+
+#if GMCS_SOURCE
+ generic_enumerator_type = new ConstructedType (
+ TypeManager.generic_ienumerator_type,
+ generic_args, Location);
+ list.Add (generic_enumerator_type);
+#endif
+
+ Bases = list;
+
+ return base.GetClassBases (out base_class);
+ }
+
+ protected override bool DoResolveMembers ()
+ {
+ pc_field = CaptureVariable ("$PC", TypeManager.system_int32_expr);
+ current_field = CaptureVariable ("$current", iterator_type_expr);
+
+#if GMCS_SOURCE
+ Define_Current (true);
+#endif
+ Define_Current (false);
+ new DisposeMethod (this);
+ Define_Reset ();
+
+ if (Iterator.IsEnumerable) {
+ new GetEnumeratorMethod (this, false);
+#if GMCS_SOURCE
+ new GetEnumeratorMethod (this, true);
+#endif
+ }
+
+ return base.DoResolveMembers ();
+ }
+
+ public void CaptureScopes ()
+ {
+ Report.Debug (128, "DEFINE ITERATOR HOST", this, scopes);
+
+ foreach (ScopeInfo si in scopes)
+ CaptureScope (si);
+
+ foreach (ScopeInfo si in scopes) {
+ if (!si.Define ())
+ throw new InternalErrorException ();
+ if (si.DefineType () == null)
+ throw new InternalErrorException ();
+ if (!si.ResolveType ())
+ throw new InternalErrorException ();
+ if (!si.ResolveMembers ())
+ throw new InternalErrorException ();
+ if (!si.DefineMembers ())
+ throw new InternalErrorException ();
+ }
+ }
+
+ protected override bool DoDefineMembers ()
+ {
+ if (!base.DoDefineMembers ())
+ return false;
+
+ FetchMethodDispose ();
+
+ return true;
+ }
+
+ protected override void EmitScopeConstructor (EmitContext ec)
+ {
+ ec.ig.Emit (OpCodes.Ldarg_0);
+ ec.ig.Emit (OpCodes.Ldarg_1);
+ ec.ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
+ base.EmitScopeConstructor (ec);
+ }
+
+ void FetchMethodDispose ()
+ {
+ MemberList dispose_list;
+
+ dispose_list = FindMembers (
+ CurrentType != null ? CurrentType : TypeBuilder,
+ MemberTypes.Method, BindingFlags.Public | BindingFlags.Instance,
+ Type.FilterName, "Dispose");
+
+ if (dispose_list.Count != 1)
+ throw new InternalErrorException ("Cannot find Dipose() method.");
+
+ dispose_method = (MethodInfo) dispose_list [0];
+ }
+
+ void Define_Current (bool is_generic)
+ {
+ MemberName left;
+ Expression type;
+
+ if (is_generic) {
+ left = new MemberName (
+ "System.Collections.Generic.IEnumerator",
+ generic_args, Location);
+ type = iterator_type_expr;
+ } else {
+ left = new MemberName ("System.Collections.IEnumerator", Location);
+ type = TypeManager.system_object_expr;
+ }
+
+ MemberName name = new MemberName (left, "Current", null, Location);
+
+ ToplevelBlock get_block = new ToplevelBlock (Location);
+ get_block.AddStatement (new CurrentBlock (this, is_generic));
+
+ Accessor getter = new Accessor (get_block, 0, null, Location);
+
+ Property current = new Property (
+ this, type, 0, false, name, null, getter, null, false);
+ AddProperty (current);
+ }
+
+ void Define_Reset ()
+ {
+ Method reset = new Method (
+ this, null, TypeManager.system_void_expr, Modifiers.PUBLIC,
+ false, new MemberName ("Reset", Location),
+ Parameters.EmptyReadOnlyParameters, null);
+ AddMethod (reset);
+
+ reset.Block = new ToplevelBlock (Location);
+ reset.Block.AddStatement (Create_ThrowNotSupported ());
+ }
+
+ Statement Create_ThrowNotSupported ()
+ {
+ TypeExpr ex_type = new TypeExpression (
+ TypeManager.not_supported_exception_type, Location);
+
+ return new Throw (new New (ex_type, null, Location), Location);
+ }
+
+ ConstructorInfo GetInvalidOperationException ()
+ {
+ return TypeManager.GetConstructor (
+ TypeManager.invalid_operation_exception_type, Type.EmptyTypes);
+ }
+
+ protected override ScopeInitializer CreateScopeInitializer ()
+ {
+ return new IteratorHostInitializer (this);
+ }
+
+ protected class IteratorHostInitializer : RootScopeInitializer
+ {
+ new public readonly IteratorHost Host;
+ protected Iterator.State state;
+
+ public IteratorHostInitializer (IteratorHost host)
+ : base (host)
+ {
+ this.Host = host;
+ }
+
+ protected override bool DoResolveInternal (EmitContext ec)
+ {
+ if (this is EnumeratorScopeInitializer)
+ state = Iterator.State.Running;
+ else if (Host.Iterator.IsEnumerable)
+ state = Iterator.State.Uninitialized;
+ else
+ state = Iterator.State.Running;
+
+ return base.DoResolveInternal (ec);
+ }
+
+ protected override void EmitScopeConstructor (EmitContext ec)
+ {
+ ec.ig.Emit (OpCodes.Ldc_I4, (int) state);
+ base.EmitScopeConstructor (ec);
+ }
+ }
+
+ protected class GetEnumeratorMethod : Method
+ {
+ public IteratorHost Host;
+
+ static MemberName GetMemberName (IteratorHost host, bool is_generic)
+ {
+ MemberName left;
+ if (is_generic) {
+ left = new MemberName (
+ "System.Collections.Generic.IEnumerable",
+ host.generic_args, host.Location);
+ } else {
+ left = new MemberName (
+ "System.Collections.IEnumerable", host.Location);
+ }
+
+ return new MemberName (left, "GetEnumerator", host.Location);
+ }
+
+ public GetEnumeratorMethod (IteratorHost host, bool is_generic)
+ : base (host, null, is_generic ?
+ host.generic_enumerator_type : host.enumerator_type,
+ 0, false, GetMemberName (host, is_generic),
+ Parameters.EmptyReadOnlyParameters, null)
+ {
+ this.Host = host;
+
+ host.AddMethod (this);
+
+ Block = new ToplevelBlock (host.Iterator.Container.Toplevel, null, Location);
+ Block.AddStatement (new GetEnumeratorStatement (host, Type));
+ }
+
+ public override EmitContext CreateEmitContext (DeclSpace tc, ILGenerator ig)
+ {
+ EmitContext ec = new EmitContext (
+ this, tc, this.ds, Location, ig, MemberType, ModFlags, false);
+
+ ec.CurrentAnonymousMethod = Host.Iterator;
+ return ec;
+ }
+
+ protected class GetEnumeratorStatement : Statement
+ {
+ IteratorHost host;
+ Expression type;
+
+ ExpressionStatement initializer;
+ Expression cast;
+ MethodInfo ce;
+
+ public GetEnumeratorStatement (IteratorHost host, Expression type)
+ {
+ this.host = host;
+ this.type = type;
+ loc = host.Location;
+ }
+
+ public override bool Resolve (EmitContext ec)
+ {
+ type = type.ResolveAsTypeTerminal (ec, false);
+ if ((type == null) || (type.Type == null))
+ return false;
+
+ initializer = host.GetEnumeratorInitializer (ec);
+ if (initializer == null)
+ return false;
+
+ cast = new ClassCast (initializer, type.Type);
+
+ ce = TypeManager.int_interlocked_compare_exchange;
+
+ ec.CurrentBranching.CurrentUsageVector.Goto ();
+ return true;
+ }
+
+ protected override void DoEmit (EmitContext ec)
+ {
+ ILGenerator ig = ec.ig;
+ Label label_init = ig.DefineLabel ();
+
+ ig.Emit (OpCodes.Ldarg_0);
+ ig.Emit (OpCodes.Ldflda, host.PC.FieldBuilder);
+ ig.Emit (OpCodes.Ldc_I4, (int) Iterator.State.Running);
+ ig.Emit (OpCodes.Ldc_I4, (int) Iterator.State.Uninitialized);
+ ig.Emit (OpCodes.Call, ce);
+
+ ig.Emit (OpCodes.Ldc_I4, (int) Iterator.State.Uninitialized);
+ ig.Emit (OpCodes.Bne_Un, label_init);
+
+ ig.Emit (OpCodes.Ldarg_0);
+ ig.Emit (OpCodes.Ret);
+
+ ig.MarkLabel (label_init);
+
+ initializer.EmitStatement (ec);
+ cast.Emit (ec);
+ ig.Emit (OpCodes.Ret);
+ }
+ }
+ }
+
+ protected class DisposeMethod : Method
+ {
+ public IteratorHost Host;
+
+ public DisposeMethod (IteratorHost host)
+ : base (host, null, TypeManager.system_void_expr, Modifiers.PUBLIC,
+ false, new MemberName ("Dispose", host.Location),
+ Parameters.EmptyReadOnlyParameters, null)
+ {
+ this.Host = host;
+
+ host.AddMethod (this);
+
+ Block = new ToplevelBlock (host.Iterator.Block, null, Location);
+ Block.AddStatement (new DisposeMethodStatement (Host.Iterator));
+
+ Report.Debug (64, "DISPOSE METHOD", host, Block);
+ }
+
+ public override EmitContext CreateEmitContext (DeclSpace tc, ILGenerator ig)
+ {
+ EmitContext ec = new EmitContext (
+ this, tc, this.ds, Location, ig, MemberType, ModFlags, false);
+
+ ec.CurrentAnonymousMethod = Host.Iterator;
+ return ec;
+ }
+
+ protected class DisposeMethodStatement : Statement
+ {
+ Iterator iterator;
+
+ public DisposeMethodStatement (Iterator iterator)
+ {
+ this.iterator = iterator;
+ this.loc = iterator.Location;
+ }
+
+ public override bool Resolve (EmitContext ec)
+ {
+ return true;
+ }
+
+ protected override void DoEmit (EmitContext ec)
+ {
+ iterator.EmitDispose (ec);
+ }
+ }
+ }
+
+ protected ScopeInitializer GetEnumeratorInitializer (EmitContext ec)
+ {
+ ScopeInitializer init = new EnumeratorScopeInitializer (this);
+ if (init.Resolve (ec) == null)
+ throw new InternalErrorException ();
+ return init;
+ }
+
+ protected class EnumeratorScopeInitializer : IteratorHostInitializer
+ {
+ IteratorHost host;
+
+ public EnumeratorScopeInitializer (IteratorHost host)
+ : base (host)
+ {
+ this.host = host;
+ }
+
+ protected override bool DoResolveInternal (EmitContext ec)
+ {
+ type = host.IsGeneric ? host.CurrentType : host.TypeBuilder;
+ return base.DoResolveInternal (ec);
+ }
+
+ protected override void DoEmit (EmitContext ec)
+ {
+ DoEmitInstance (ec);
+ }
+
+ protected override bool IsGetEnumerator {
+ get { return true; }
+ }
+
+ protected override void EmitParameterReference (EmitContext ec,
+ CapturedParameter cp)
+ {
+ ec.ig.Emit (OpCodes.Ldarg_0);
+ ec.ig.Emit (OpCodes.Ldfld, cp.Field.FieldBuilder);
+ }
+ }
+
+ protected class CurrentBlock : Statement {
+ IteratorHost host;
+ bool is_generic;
+
+ public CurrentBlock (IteratorHost host, bool is_generic)
+ {
+ this.host = host;
+ this.is_generic = is_generic;
+ loc = host.Location;
+ }
+
+ public override bool Resolve (EmitContext ec)
+ {
+ ec.CurrentBranching.CurrentUsageVector.Goto ();
+ return true;
+ }
+
+ protected override void DoEmit (EmitContext ec)
+ {
+ ILGenerator ig = ec.ig;
+ Label label_ok = ig.DefineLabel ();
+
+ ig.Emit (OpCodes.Ldarg_0);
+ ig.Emit (OpCodes.Ldfld, host.PC.FieldBuilder);
+ ig.Emit (OpCodes.Ldc_I4, (int) Iterator.State.Running);
+ ig.Emit (OpCodes.Bgt, label_ok);
+
+ ig.Emit (OpCodes.Newobj, host.GetInvalidOperationException ());
+ ig.Emit (OpCodes.Throw);
+
+ ig.MarkLabel (label_ok);
+ ig.Emit (OpCodes.Ldarg_0);
+ ig.Emit (OpCodes.Ldfld, host.CurrentField.FieldBuilder);
+ if (!is_generic)
+ ig.Emit (OpCodes.Box, host.CurrentField.MemberType);
+ ig.Emit (OpCodes.Ret);
+ }
+ }
+ }
+
+ public class Iterator : AnonymousContainer {
+ protected readonly ToplevelBlock OriginalBlock;
+ protected readonly IMethodData OriginalMethod;
+ protected ToplevelBlock block;
+
+ public readonly bool IsEnumerable;
public readonly bool IsStatic;
//
ArrayList resume_points = new ArrayList ();
int pc;
- //
- // Context from the original method
- //
- TypeContainer container;
- Type this_type;
- Parameters parameters;
- IMethodData orig_method;
-
- MoveNextMethod move_next_method;
- Constructor ctor;
- CaptureContext cc;
- EmitContext ec;
+ public readonly Type OriginalIteratorType;
+ public readonly IteratorHost IteratorHost;
- protected enum State {
+ public enum State {
Uninitialized = -2,
After,
Running
}
- static int proxy_count;
-
public void EmitYieldBreak (ILGenerator ig)
{
ig.Emit (OpCodes.Ldarg_0);
IntConstant.EmitInt (ig, (int) State.After);
- ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
+ ig.Emit (OpCodes.Stfld, IteratorHost.PC.FieldBuilder);
ig.Emit (OpCodes.Br, move_next_error);
}
- public void EmitMoveNext (EmitContext ec)
+ internal void EmitMoveNext (EmitContext ec, Block original_block)
{
ILGenerator ig = ec.ig;
resume_points.Add (entry_point);
entry_point.Define (ig);
- ec.EmitTopBlock (orig_method, original_block);
+ original_block.Emit (ec);
EmitYieldBreak (ig);
labels [i] = ((ResumePoint) resume_points [i]).Label;
ig.Emit (OpCodes.Ldarg_0);
- ig.Emit (OpCodes.Ldfld, pc_field.FieldBuilder);
+ ig.Emit (OpCodes.Ldfld, IteratorHost.PC.FieldBuilder);
ig.Emit (OpCodes.Switch, labels);
Label end = ig.DefineLabel ();
ig.MarkLabel (move_next_error);
- ig.Emit (OpCodes.Ldc_I4_0);
+ ig.Emit (OpCodes.Ldc_I4_0);
ig.Emit (OpCodes.Stloc, retval);
ig.Emit (OpCodes.Leave, end);
ig.BeginFaultBlock ();
ig.Emit (OpCodes.Ldarg_0);
- ig.Emit (OpCodes.Callvirt, dispose.MethodBuilder);
+ ig.Emit (OpCodes.Callvirt, IteratorHost.Dispose);
ig.EndExceptionBlock ();
ig.EndExceptionBlock ();
ig.Emit (OpCodes.Br, end);
}
-
+
ig.MarkLabel (dispatcher);
ig.Emit (OpCodes.Ldarg_0);
- ig.Emit (OpCodes.Ldfld, pc_field.FieldBuilder);
+ ig.Emit (OpCodes.Ldfld, IteratorHost.PC.FieldBuilder);
ig.Emit (OpCodes.Switch, labels);
ig.Emit (OpCodes.Ldarg_0);
IntConstant.EmitInt (ig, (int) State.After);
- ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
+ ig.Emit (OpCodes.Stfld, IteratorHost.PC.FieldBuilder);
ig.MarkLabel (end);
}
// Store the new current
ig.Emit (OpCodes.Ldarg_0);
expr.Emit (ec);
- ig.Emit (OpCodes.Stfld, current_field.FieldBuilder);
+ ig.Emit (OpCodes.Stfld, IteratorHost.CurrentField.FieldBuilder);
// increment pc
pc++;
ig.Emit (OpCodes.Ldarg_0);
IntConstant.EmitInt (ig, pc);
- ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
+ ig.Emit (OpCodes.Stfld, IteratorHost.PC.FieldBuilder);
// Return ok
ig.Emit (OpCodes.Br, move_next_ok);
pc++;
ig.Emit (OpCodes.Ldarg_0);
IntConstant.EmitInt (ig, pc);
- ig.Emit (OpCodes.Stfld, pc_field.FieldBuilder);
+ ig.Emit (OpCodes.Stfld, IteratorHost.PC.FieldBuilder);
ResumePoint point = new ResumePoint (finally_blocks);
resume_points.Add (point);
point.Define (ig);
}
- private static MemberName MakeProxyName (string name, Location loc)
- {
- int pos = name.LastIndexOf ('.');
- if (pos > 0)
- name = name.Substring (pos + 1);
+ public override string ContainerType {
+ get { return "iterator"; }
+ }
+
+ public override bool IsIterator {
+ get { return true; }
+ }
- return new MemberName ("<" + name + ">__" + (proxy_count++), loc);
+ public override RootScopeInfo RootScope {
+ get { return IteratorHost; }
+ }
+
+ public override ScopeInfo Scope {
+ get { return IteratorHost; }
}
//
// Our constructor
//
- public Iterator (IMethodData m_container, DeclSpace parent,
- int modifiers)
- : base (parent.NamespaceEntry, parent, MakeProxyName (m_container.MethodName.Name, m_container.Location),
- (modifiers & Modifiers.UNSAFE) | Modifiers.PRIVATE, null)
+ private Iterator (IMethodData m_container, DeclSpace host, GenericMethod generic,
+ int modifiers, Type iterator_type, bool is_enumerable)
+ : base (host, generic, m_container.ParameterInfo,
+ new ToplevelBlock (m_container.ParameterInfo, m_container.Location),
+ m_container.Block, TypeManager.bool_type, modifiers,
+ m_container.Location)
{
- this.orig_method = m_container;
+ this.OriginalBlock = m_container.Block;
+ this.OriginalMethod = m_container;
+ this.OriginalIteratorType = iterator_type;
+ this.IsEnumerable = is_enumerable;
- this.container = parent.PartialContainer;
- this.parameters = m_container.ParameterInfo;
- this.original_block = orig_method.Block;
- this.block = new ToplevelBlock (orig_method.Block, parameters, orig_method.Location);
+ Report.Debug (64, "NEW ITERATOR", host, generic, OriginalBlock,
+ Container, Block);
- IsStatic = (modifiers & Modifiers.STATIC) != 0;
- }
+ IteratorHost = new IteratorHost (this);
+ Block.CreateIteratorHost (IteratorHost);
+
+ OriginalBlock.ReParent (Container.Toplevel);
- public AnonymousContainer Host {
- get { return move_next_method; }
+ m_container.Block = Container.Toplevel;
+
+ OriginalBlock.MakeIterator (this);
}
- public bool DefineIterator ()
+ protected class TestStatement : Statement
{
- ec = new EmitContext (this, this, Mono.CSharp.Location.Null, null, null, ModFlags);
- ec.CurrentAnonymousMethod = move_next_method;
- ec.CurrentIterator = this;
- ec.InIterator = true;
-
- if (!CheckType ()) {
- Report.Error (1624, Location,
- "The body of `{0}' cannot be an iterator block because `{1}' is not an iterator interface type",
- orig_method.GetSignatureForError (), TypeManager.CSharpName (orig_method.ReturnType));
- return false;
+ public override bool Resolve (EmitContext ec)
+ {
+ return true;
+ }
+
+ protected override void DoEmit (EmitContext ec)
+ {
+ ec.ig.Emit (OpCodes.Nop);
+ ec.ig.Emit (OpCodes.Neg);
+ ec.ig.Emit (OpCodes.Pop);
+ ec.ig.Emit (OpCodes.Ret);
}
+ }
+
+ public override string GetSignatureForError ()
+ {
+ return OriginalMethod.GetSignatureForError ();
+ }
+
+ public override bool Define (EmitContext ec)
+ {
+ Report.Debug (64, "RESOLVE ITERATOR", this, Container, Block);
+ Parameters parameters = OriginalMethod.ParameterInfo;
for (int i = 0; i < parameters.Count; i++){
Parameter.Modifier mod = parameters.ParameterModifier (i);
if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0){
- Report.Error (
- 1623, Location,
- "Iterators cannot have ref or out parameters");
+ Report.Error (1623, Location,
+ "Iterators cannot have ref or out parameters");
return false;
}
if ((mod & Parameter.Modifier.ARGLIST) != 0) {
- Report.Error (1636, Location, "__arglist is not allowed in parameter list of iterators");
+ Report.Error (1636, Location,
+ "__arglist is not allowed in parameter list " +
+ "of iterators");
return false;
}
if (parameters.ParameterType (i).IsPointer) {
- Report.Error (1637, Location, "Iterators cannot have unsafe parameters or yield types");
+ Report.Error (1637, Location,
+ "Iterators cannot have unsafe parameters or " +
+ "yield types");
return false;
}
}
- this_type = container.TypeBuilder;
-
- ArrayList list = new ArrayList ();
- if (is_enumerable)
- list.Add (new TypeExpression (
- TypeManager.ienumerable_type, Location));
- list.Add (new TypeExpression (TypeManager.ienumerator_type, Location));
- list.Add (new TypeExpression (TypeManager.idisposable_type, Location));
-
- iterator_type_expr = new TypeExpression (iterator_type, Location);
-
- container.AddIterator (this);
-
- Bases = list;
- orig_method.Block = block;
- return true;
- }
-
- protected override bool DoDefineMembers ()
- {
- ec.InIterator = true;
- ec.CurrentIterator = this;
- ec.CurrentAnonymousMethod = move_next_method;
- ec.capture_context = cc;
-
- if (!base.DoDefineMembers ())
+ if ((ModFlags & Modifiers.UNSAFE) != 0) {
+ Report.Error (1629, Location, "Unsafe code may not appear in iterators");
return false;
+ }
- return true;
- }
-
- public override bool Define ()
- {
- if (!base.Define ())
+ if (!base.Define (ec))
return false;
- ec.InIterator = true;
- ec.CurrentIterator = this;
- ec.CurrentAnonymousMethod = move_next_method;
- ec.capture_context = cc;
- ec.TypeContainer = ec.TypeContainer.Parent;
-
- ec.ContainerType = ec.TypeContainer.TypeBuilder;
+ Report.Debug (64, "RESOLVE ITERATOR #1", this, method, method.Parent,
+ RootScope, ec);
- ec.ig = move_next_method.method.MethodBuilder.GetILGenerator ();
-
- if (!ctor.Define ())
+ if (!RootScope.ResolveType ())
return false;
-
- bool unreachable;
-
- if (!ec.ResolveTopBlock (null, original_block, parameters, orig_method, out unreachable))
+ if (!RootScope.ResolveMembers ())
return false;
-
- if (!ec.ResolveTopBlock (null, block, parameters, orig_method, out unreachable))
+ if (!RootScope.DefineMembers ())
return false;
- original_block.CompleteContexts ();
-
- cc.EmitAnonymousHelperClasses (ec);
+ ExpressionStatement scope_init = RootScope.GetScopeInitializer (ec);
+ Container.AddStatement (new StatementExpression (scope_init));
+ Expression cast = new ClassCast (scope_init, OriginalMethod.ReturnType);
+ Container.AddStatement (new NoCheckReturn (cast));
return true;
}
- //
- // Returns the new block for the method, or null on failure
- //
- protected override bool DefineNestedTypes ()
- {
- Define_Fields ();
- Define_Current ();
- Define_MoveNext ();
- Define_Reset ();
- Define_Dispose ();
-
- Create_Block ();
-
- Define_Constructor ();
-
- if (is_enumerable)
- Define_GetEnumerator ();
-
- return base.DefineNestedTypes ();
- }
-
- Field pc_field;
- Field current_field;
- Method dispose;
-
- void Create_Block ()
- {
- original_block.SetHaveAnonymousMethods (Location, move_next_method);
- block.SetHaveAnonymousMethods (Location, move_next_method);
-
- cc = original_block.CaptureContext;
-
- int first = IsStatic ? 0 : 1;
-
- ArrayList args = new ArrayList ();
- if (!IsStatic) {
- Type t = container.TypeBuilder;
- args.Add (new Argument (
- new ThisParameterReference (t, Location)));
- cc.CaptureThis (move_next_method);
- }
-
- args.Add (new Argument (new BoolLiteral (false, Location)));
-
- for (int i = 0; i < parameters.Count; i++) {
- Type t = parameters.ParameterType (i);
- string name = parameters.ParameterName (i);
-
- args.Add (new Argument (
- new SimpleParameterReference (t, first + i, Location)));
-
- cc.AddParameterToContext (move_next_method, name, t, first + i);
- }
-
- Expression new_expr = new New (
- new TypeExpression (TypeBuilder, Location), args, Location);
-
- block.AddStatement (new NoCheckReturn (new_expr, Location));
- }
-
- void Define_Fields ()
- {
- pc_field = new Field (
- this, TypeManager.system_int32_expr, Modifiers.PRIVATE, "$PC",
- null, Location);
- AddField (pc_field);
-
- current_field = new Field (
- this, iterator_type_expr, Modifiers.PRIVATE, "$current",
- null, Location);
- AddField (current_field);
- }
-
- void Define_Constructor ()
- {
- Parameters ctor_params;
-
- ArrayList list = new ArrayList ();
-
- if (!IsStatic)
- list.Add (new Parameter (
- new TypeExpression (container.TypeBuilder, Location),
- "this", Parameter.Modifier.NONE,
- null, Location));
- list.Add (new Parameter (
- TypeManager.bool_type, "initialized",
- Parameter.Modifier.NONE, null, Location));
-
- Parameter[] old_fixed = parameters.FixedParameters;
- list.AddRange (old_fixed);
-
- Parameter[] fixed_params = new Parameter [list.Count];
- list.CopyTo (fixed_params);
-
- ctor_params = new Parameters (fixed_params);
-
- ctor = new Constructor (
- this, Name, Modifiers.PUBLIC, ctor_params,
- new GeneratedBaseInitializer (Location),
- Location);
- AddConstructor (ctor);
-
- ctor.Block = new ToplevelBlock (block, parameters, Location);
-
- int first = IsStatic ? 2 : 3;
-
- State initial = is_enumerable ? State.Uninitialized : State.Running;
- ctor.Block.AddStatement (new SetState (this, initial, Location));
-
- ctor.Block.AddStatement (new If (
- new SimpleParameterReference (
- TypeManager.bool_type, first - 1, Location),
- new SetState (this, State.Running, Location),
- Location));
-
- ctor.Block.AddStatement (new InitScope (this, Location));
- }
-
- Statement Create_ThrowInvalidOperation ()
- {
- TypeExpr ex_type = new TypeExpression (
- TypeManager.invalid_operation_exception_type, Location);
-
- return new Throw (new New (ex_type, null, Location), Location);
- }
-
- Statement Create_ThrowNotSupported ()
- {
- TypeExpr ex_type = new TypeExpression (
- TypeManager.not_supported_exception_type, Location);
-
- return new Throw (new New (ex_type, null, Location), Location);
- }
-
- void Define_Current ()
- {
- ToplevelBlock get_block = new ToplevelBlock (
- block, parameters, Location);
- MemberName left = new MemberName ("System.Collections.IEnumerator");
- MemberName name = new MemberName (left, "Current", Location);
-
- get_block.AddStatement (new If (
- new Binary (
- Binary.Operator.LessThanOrEqual,
- new FieldExpression (this, pc_field),
- new IntLiteral ((int) State.Running, pc_field.Location)),
- Create_ThrowInvalidOperation (),
- new Return (
- new FieldExpression (this, current_field), Location),
- Location));
-
- Accessor getter = new Accessor (get_block, 0, null, Location);
-
- Property current = new Property (
- this, iterator_type_expr, 0,
- false, name, null, getter, null);
- AddProperty (current);
- }
-
- void Define_MoveNext ()
- {
- move_next_method = new MoveNextMethod (this, Location);
-
- original_block.ReParent (block, move_next_method);
-
- move_next_method.CreateMethod (ec);
-
- AddMethod (move_next_method.method);
- }
-
- void Define_GetEnumerator ()
- {
- MemberName left = new MemberName ("System.Collections.IEnumerable");
-
- MemberName name = new MemberName (left, "GetEnumerator", Location);
-
- Method get_enumerator = new Method (
- this,
- new TypeExpression (TypeManager.ienumerator_type, Location),
- 0, false, name,
- Parameters.EmptyReadOnlyParameters, null);
-
- //
- // We call append instead of add, as we need to make sure that
- // this method is resolved after the MoveNext method, as that one
- // triggers the computation of the AnonymousMethod Scope, which is
- // required during the code generation of the enumerator
- //
- AppendMethod (get_enumerator);
-
- get_enumerator.Block = new ToplevelBlock (
- block, parameters, Location);
-
- get_enumerator.Block.SetHaveAnonymousMethods (Location, move_next_method);
-
- Expression ce = new MemberAccess (
- new SimpleName ("System.Threading.Interlocked", Location),
- "CompareExchange");
-
- Expression pc = new FieldExpression (this, pc_field);
- Expression before = new IntLiteral ((int) State.Running, Location);
- Expression uninitialized = new IntLiteral ((int) State.Uninitialized, Location);
-
- ArrayList args = new ArrayList ();
- args.Add (new Argument (pc, Argument.AType.Ref));
- args.Add (new Argument (before, Argument.AType.Expression));
- args.Add (new Argument (uninitialized, Argument.AType.Expression));
-
- get_enumerator.Block.AddStatement (new If (
- new Binary (
- Binary.Operator.Equality,
- new Invocation (ce, args),
- uninitialized),
- new Return (new ThisParameterReference (
- TypeManager.ienumerator_type, Location),
- Location),
- Location));
-
- args = new ArrayList ();
- if (!IsStatic) {
- args.Add (new Argument (new CapturedThisReference (this, Location)));
- }
-
- args.Add (new Argument (new BoolLiteral (true, Location)));
-
- for (int i = 0; i < parameters.Count; i++) {
- Expression cp = new CapturedParameterReference (
- this, parameters.ParameterType (i),
- parameters.ParameterName (i), Location);
- args.Add (new Argument (cp));
- }
-
- Expression new_expr = new New (
- new TypeExpression (TypeBuilder, Location), args, Location);
- get_enumerator.Block.AddStatement (new Return (new_expr, Location));
- }
-
- protected class SimpleParameterReference : Expression
- {
- int idx;
-
- public SimpleParameterReference (Type type, int idx, Location loc)
- {
- this.idx = idx;
- this.loc = loc;
- this.type = type;
- eclass = ExprClass.Variable;
- }
-
- public override Expression DoResolve (EmitContext ec)
- {
- return this;
- }
-
- public override void Emit (EmitContext ec)
- {
- DoEmit (ec);
- }
-
- protected virtual void DoEmit (EmitContext ec)
- {
- ParameterReference.EmitLdArg (ec.ig, idx);
- }
- }
-
- protected class ThisParameterReference : SimpleParameterReference, IMemoryLocation
+ protected override Method DoCreateMethodHost (EmitContext ec)
{
- public ThisParameterReference (Type type, Location loc)
- : base (type, 0, loc)
- { }
+ Report.Debug (128, "CREATE METHOD HOST", this, IteratorHost);
- protected override void DoEmit (EmitContext ec)
- {
- base.DoEmit (ec);
- if (ec.TypeContainer is Struct)
- ec.ig.Emit (OpCodes.Ldobj, type);
- }
+ IteratorHost.CaptureScopes ();
- public void AddressOf (EmitContext ec, AddressOp mode)
- {
- if (ec.TypeContainer is Struct)
- ec.ig.Emit (OpCodes.Ldarga, 0);
- else
- ec.ig.Emit (OpCodes.Ldarg, 0);
- }
+ return new AnonymousMethodMethod (
+ this, RootScope, null, TypeManager.system_boolean_expr,
+ Modifiers.PUBLIC, new MemberName ("MoveNext", Location),
+ Parameters.EmptyReadOnlyParameters);
}
- protected class CapturedParameterReference : Expression
+ public override Expression Resolve (EmitContext ec)
{
- Iterator iterator;
- string name;
-
- public CapturedParameterReference (Iterator iterator, Type type,
- string name, Location loc)
- {
- this.iterator = iterator;
- this.loc = loc;
- this.type = type;
- this.name = name;
- eclass = ExprClass.Variable;
- }
-
- public override Expression DoResolve (EmitContext ec)
- {
- return this;
- }
-
- public override void Emit (EmitContext ec)
- {
- ec.CurrentAnonymousMethod = iterator.move_next_method;
-
- LocalTemporary dummy = null;
-
- iterator.cc.EmitParameter (ec, name, false, false, ref dummy);
- }
- }
-
- protected class CapturedThisReference : Expression
- {
- public CapturedThisReference (Iterator iterator, Location loc)
- {
- this.loc = loc;
- this.type = iterator.this_type;
- eclass = ExprClass.Variable;
- }
-
- public override Expression DoResolve (EmitContext ec)
- {
- return this;
- }
-
- public override void Emit (EmitContext ec)
- {
- ec.EmitThis (false);
- }
- }
-
- protected class FieldExpression : Expression
- {
- Iterator iterator;
- Field field;
-
- public FieldExpression (Iterator iterator, Field field)
- {
- this.iterator = iterator;
- this.field = field;
- this.loc = iterator.Location;
- }
-
- public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
- {
- FieldExpr fexpr = new FieldExpr (field.FieldBuilder, loc);
- fexpr.InstanceExpression = new ThisParameterReference (
- iterator.this_type, loc);
- return fexpr.ResolveLValue (ec, right_side, loc);
- }
-
- public override Expression DoResolve (EmitContext ec)
- {
- FieldExpr fexpr = new FieldExpr (field.FieldBuilder, loc);
- fexpr.InstanceExpression = new ThisParameterReference (
- iterator.this_type, loc);
- return fexpr.Resolve (ec);
- }
-
- public override void Emit (EmitContext ec)
- {
- throw new InvalidOperationException ();
- }
- }
-
- protected class MoveNextMethod : AnonymousContainer
- {
- Iterator iterator;
-
- public MoveNextMethod (Iterator iterator, Location loc)
- : base (iterator.parameters, iterator.original_block, loc)
- {
- this.iterator = iterator;
- }
-
- protected override bool CreateMethodHost (EmitContext ec)
- {
- method = new Method (
- iterator, TypeManager.system_boolean_expr,
- Modifiers.PUBLIC, false, new MemberName ("MoveNext", loc),
- Parameters.EmptyReadOnlyParameters, null);
-
- method.Block = Block;
-
- MoveNextStatement inline = new MoveNextStatement (iterator, loc);
- Block.AddStatement (inline);
-
- return true;
- }
-
- public bool CreateMethod (EmitContext ec)
- {
- return CreateMethodHost (ec);
- }
-
- public void ComputeHost ()
- {
- ComputeMethodHost ();
- }
-
- public override bool IsIterator {
- get { return true; }
- }
-
- public override void CreateScopeType (EmitContext ec, ScopeInfo scope)
- {
- scope.ScopeTypeBuilder = iterator.TypeBuilder;
- scope.ScopeConstructor = iterator.ctor.ConstructorBuilder;
- }
-
- public override void Emit (EmitContext ec)
- {
- throw new InternalErrorException ();
- }
+ throw new NotSupportedException ();
}
protected class MoveNextStatement : Statement {
this.iterator = iterator;
}
- protected override void DoEmit (EmitContext ec)
- {
- iterator.move_next_method.ComputeHost ();
-
- ec.CurrentIterator = iterator;
- ec.CurrentAnonymousMethod = iterator.move_next_method;
- ec.InIterator = true;
-
- iterator.EmitMoveNext (ec);
- }
- }
-
- protected class DisposeMethod : Statement {
- Iterator iterator;
-
- public DisposeMethod (Iterator iterator, Location loc)
- {
- this.loc = loc;
- this.iterator = iterator;
- }
-
public override bool Resolve (EmitContext ec)
{
- return true;
+ return iterator.OriginalBlock.Resolve (ec);
}
protected override void DoEmit (EmitContext ec)
{
- iterator.EmitDispose (ec);
+ iterator.EmitMoveNext (ec, iterator.Block);
}
}
- protected class StatementList : Statement {
- ArrayList statements;
-
- public StatementList (Location loc)
- {
- this.loc = loc;
- statements = new ArrayList ();
- }
-
- public void Add (Statement statement)
- {
- statements.Add (statement);
- }
-
- public override bool Resolve (EmitContext ec)
- {
- foreach (Statement stmt in statements) {
- if (!stmt.Resolve (ec))
- return false;
- }
-
- return true;
- }
-
- protected override void DoEmit (EmitContext ec)
- {
- foreach (Statement stmt in statements)
- stmt.Emit (ec);
- }
- }
-
- protected class SetState : Statement
- {
- Iterator iterator;
- State state;
-
- public SetState (Iterator iterator, State state, Location loc)
- {
- this.iterator = iterator;
- this.state = state;
- this.loc = loc;
- }
-
- public override bool Resolve (EmitContext ec)
- {
- return true;
- }
-
- protected override void DoEmit (EmitContext ec)
- {
- ec.ig.Emit (OpCodes.Ldarg_0);
- IntConstant.EmitInt (ec.ig, (int) state);
- ec.ig.Emit (OpCodes.Stfld, iterator.pc_field.FieldBuilder);
- }
- }
-
- protected class InitScope : Statement
- {
- Iterator iterator;
-
- public InitScope (Iterator iterator, Location loc)
- {
- this.iterator = iterator;
- this.loc = loc;
- }
-
- public override bool Resolve (EmitContext ec)
- {
- return true;
- }
-
- protected override void DoEmit (EmitContext ec)
- {
- iterator.cc.EmitInitScope (ec);
- }
- }
-
- void Define_Reset ()
- {
- Method reset = new Method (
- this, TypeManager.system_void_expr, Modifiers.PUBLIC,
- false, new MemberName ("Reset", Location),
- Parameters.EmptyReadOnlyParameters, null);
- AddMethod (reset);
-
- reset.Block = new ToplevelBlock (Location);
- reset.Block = new ToplevelBlock (block, parameters, Location);
- reset.Block.SetHaveAnonymousMethods (Location, move_next_method);
-
- reset.Block.AddStatement (Create_ThrowNotSupported ());
- }
-
- void Define_Dispose ()
- {
- dispose = new Method (
- this, TypeManager.system_void_expr, Modifiers.PUBLIC,
- false, new MemberName ("Dispose", Location),
- Parameters.EmptyReadOnlyParameters, null);
- AddMethod (dispose);
-
- dispose.Block = new ToplevelBlock (block, parameters, Location);
- dispose.Block.SetHaveAnonymousMethods (Location, move_next_method);
-
- dispose.Block.AddStatement (new DisposeMethod (this, Location));
- }
-
public Type IteratorType {
- get { return iterator_type; }
+ get { return IteratorHost.IteratorType; }
}
//
class NoCheckReturn : Statement {
public Expression Expr;
- public NoCheckReturn (Expression expr, Location l)
+ public NoCheckReturn (Expression expr)
{
Expr = expr;
- loc = l;
+ loc = expr.Location;
}
public override bool Resolve (EmitContext ec)
if (Expr == null)
return false;
- ec.CurrentBranching.CurrentUsageVector.Return ();
+ ec.CurrentBranching.CurrentUsageVector.Goto ();
return true;
}
}
}
- bool CheckType ()
+ public static Iterator CreateIterator (IMethodData method, DeclSpace parent,
+ GenericMethod generic, int modifiers)
+ {
+ bool is_enumerable;
+ Type iterator_type;
+
+ Type ret = method.ReturnType;
+ if (ret == null)
+ return null;
+
+ if (!CheckType (ret, out iterator_type, out is_enumerable)) {
+ Report.Error (1624, method.Location,
+ "The body of `{0}' cannot be an iterator block " +
+ "because `{1}' is not an iterator interface type",
+ method.GetSignatureForError (),
+ TypeManager.CSharpName (ret));
+ return null;
+ }
+
+ return new Iterator (method, parent, generic, modifiers,
+ iterator_type, is_enumerable);
+ }
+
+ static bool CheckType (Type ret, out Type original_iterator_type, out bool is_enumerable)
{
- Type ret = orig_method.ReturnType;
+ original_iterator_type = null;
+ is_enumerable = false;
if (ret == TypeManager.ienumerable_type) {
- iterator_type = TypeManager.object_type;
+ original_iterator_type = TypeManager.object_type;
is_enumerable = true;
return true;
}
if (ret == TypeManager.ienumerator_type) {
- iterator_type = TypeManager.object_type;
+ original_iterator_type = TypeManager.object_type;
+ is_enumerable = false;
+ return true;
+ }
+
+#if GMCS_SOURCE
+ if (!ret.IsGenericType)
+ return false;
+
+ Type[] args = TypeManager.GetTypeArguments (ret);
+ if (args.Length != 1)
+ return false;
+
+ Type gt = ret.GetGenericTypeDefinition ();
+ if (gt == TypeManager.generic_ienumerable_type) {
+ original_iterator_type = args [0];
+ is_enumerable = true;
+ return true;
+ } else if (gt == TypeManager.generic_ienumerator_type) {
+ original_iterator_type = args [0];
is_enumerable = false;
return true;
}
+#endif
return false;
}