loc = l;
}
- public static bool CheckContext (EmitContext ec, Location loc, bool isYieldBreak)
+ public static bool CheckContext (EmitContext ec, Location loc)
{
for (Block block = ec.CurrentBlock; block != null; block = block.Parent) {
if (!block.Unsafe)
Report.Debug (64, "RESOLVE YIELD #1", this, ec, expr, expr.GetType (),
ec.CurrentAnonymousMethod, ec.CurrentIterator);
- if (!CheckContext (ec, loc, false))
+ if (!CheckContext (ec, loc))
return false;
Iterator iterator = ec.CurrentIterator;
return false;
}
- unwind_protect = ec.CurrentBranching.AddResumePoint (this, loc, out resume_pc);
+ if (!ec.CurrentBranching.CurrentUsageVector.IsUnreachable)
+ unwind_protect = ec.CurrentBranching.AddResumePoint (this, loc, out resume_pc);
return true;
}
Report.Error (1625, loc, "Cannot yield in the body of a finally clause");
}
+ protected override void CloneTo (CloneContext clonectx, Statement target)
+ {
+ throw new NotSupportedException ();
+ }
+
protected override bool DoResolve (EmitContext ec)
{
- return Yield.CheckContext (ec, loc, true);
+ return Yield.CheckContext (ec, loc);
}
protected override void DoEmit (EmitContext ec)
this.loc = iterator.Location;
}
+ protected override void CloneTo (CloneContext clonectx, Statement target)
+ {
+ IteratorStatement t = (IteratorStatement) target;
+ t.original_block = (ExplicitBlock) original_block.Clone (clonectx);
+ t.iterator = (Iterator) iterator.Clone (clonectx);
+ }
+
public override bool Resolve (EmitContext ec)
{
ec.StartFlowBranching (iterator);
public class IteratorStorey : AnonymousMethodStorey
{
+ class IteratorMethod : Method
+ {
+ readonly IteratorStorey host;
+
+ public IteratorMethod (IteratorStorey host, FullNamedExpression returnType, int mod, MemberName name)
+ : base (host, null, returnType, mod | Modifiers.DEBUGGER_HIDDEN | Modifiers.COMPILER_GENERATED,
+ name, Parameters.EmptyReadOnlyParameters, null)
+ {
+ this.host = host;
+
+ Block = new ToplevelBlock (host.Iterator.Container.Toplevel, null, Location);
+ }
+
+ 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;
+ }
+ }
+
+ class GetEnumeratorMethod : IteratorMethod
+ {
+ sealed class GetEnumeratorStatement : Statement
+ {
+ IteratorStorey host;
+ Expression type;
+
+ Expression cast;
+
+ public GetEnumeratorStatement (IteratorStorey host, Expression type)
+ {
+ this.host = host;
+ this.type = type;
+ loc = host.Location;
+ }
+
+ protected override void CloneTo (CloneContext clonectx, Statement target)
+ {
+ throw new NotSupportedException ();
+ }
+
+ public override bool Resolve (EmitContext ec)
+ {
+ type = type.ResolveAsTypeTerminal (ec, false);
+ if ((type == null) || (type.Type == null))
+ return false;
+
+ TypeExpression storey_type_expr = new TypeExpression (host.TypeBuilder, loc);
+ Expression new_storey;
+ ArrayList init = null;
+ if (host.hoisted_this != null) {
+ init = new ArrayList (host.hoisted_params == null ? 1 : host.HoistedParameters.Count + 1);
+ HoistedThis ht = host.hoisted_this;
+ FieldExpr from = new FieldExpr (ht.Field.FieldBuilder, loc);
+ from.InstanceExpression = CompilerGeneratedThis.Instance;
+ init.Add (new ElementInitializer (ht.Field.Name, from, loc));
+ }
+
+ if (host.hoisted_params != null) {
+ if (init == null)
+ init = new ArrayList (host.HoistedParameters.Count);
+
+ foreach (HoistedParameter hp in host.HoistedParameters) {
+ FieldExpr from = new FieldExpr (hp.Field.FieldBuilder, loc);
+ from.InstanceExpression = CompilerGeneratedThis.Instance;
+ init.Add (new ElementInitializer (hp.Field.Name, from, loc));
+ }
+ }
+
+ if (init != null) {
+ new_storey = new NewInitialize (storey_type_expr, new ArrayList (0),
+ new CollectionOrObjectInitializers (init, loc), loc);
+ } else {
+ new_storey = new New (storey_type_expr, new ArrayList (0), loc);
+ }
+
+ new_storey = new_storey.Resolve (ec);
+ if (new_storey != null)
+ cast = Convert.ImplicitConversionRequired (ec, new_storey, type.Type, loc);
+
+ if (TypeManager.int_interlocked_compare_exchange == null) {
+ Type t = TypeManager.CoreLookupType ("System.Threading", "Interlocked", Kind.Class, true);
+ if (t != null) {
+ TypeManager.int_interlocked_compare_exchange = TypeManager.GetPredefinedMethod (
+ t, "CompareExchange", loc, TypeManager.int32_type,
+ TypeManager.int32_type, TypeManager.int32_type);
+ }
+ }
+
+ 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.Start);
+ ig.Emit (OpCodes.Ldc_I4, (int) Iterator.State.Uninitialized);
+ ig.Emit (OpCodes.Call, TypeManager.int_interlocked_compare_exchange);
+
+ 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);
+
+ cast.Emit (ec);
+ ig.Emit (OpCodes.Ret);
+ }
+
+ public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
+ {
+ throw new NotSupportedException ();
+ }
+ }
+
+ public GetEnumeratorMethod (IteratorStorey host, FullNamedExpression returnType, MemberName name)
+ : base (host, returnType, 0, name)
+ {
+ Block.AddStatement (new GetEnumeratorStatement (host, type_name));
+ }
+ }
+
+ class DisposeMethod : IteratorMethod
+ {
+ sealed class DisposeMethodStatement : Statement
+ {
+ Iterator iterator;
+
+ public DisposeMethodStatement (Iterator iterator)
+ {
+ this.iterator = iterator;
+ this.loc = iterator.Location;
+ }
+
+ protected override void CloneTo (CloneContext clonectx, Statement target)
+ {
+ throw new NotSupportedException ();
+ }
+
+ public override bool Resolve (EmitContext ec)
+ {
+ return true;
+ }
+
+ protected override void DoEmit (EmitContext ec)
+ {
+ iterator.EmitDispose (ec);
+ }
+
+ public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
+ {
+ throw new NotSupportedException ();
+ }
+ }
+
+ public DisposeMethod (IteratorStorey host)
+ : base (host, TypeManager.system_void_expr, Modifiers.PUBLIC, new MemberName ("Dispose", host.Location))
+ {
+ host.AddMethod (this);
+
+ Block = new ToplevelBlock (host.Iterator.Container, null, Location);
+ Block.AddStatement (new DisposeMethodStatement (host.Iterator));
+ }
+ }
+
+ //
+ // Uses Method as method info
+ //
+ class DynamicMethodGroupExpr : MethodGroupExpr
+ {
+ readonly Method method;
+
+ public DynamicMethodGroupExpr (Method method, Location loc)
+ : base (null, loc)
+ {
+ this.method = method;
+ }
+
+ public override Expression DoResolve (EmitContext ec)
+ {
+ Methods = new MethodBase [] { method.MethodBuilder };
+ type = method.Parent.TypeBuilder;
+ InstanceExpression = new CompilerGeneratedThis (type, Location);
+ return base.DoResolve (ec);
+ }
+ }
+
public readonly Iterator Iterator;
TypeExpr iterator_type_expr;
TypeExpr enumerator_type;
TypeExpr enumerable_type;
+#if GMCS_SOURCE
TypeArguments generic_args;
TypeExpr generic_enumerator_type;
-#if GMCS_SOURCE
TypeExpr generic_enumerable_type;
+#else
+ const TypeArguments generic_args = null;
#endif
int local_name_idx;
iterator.OriginalMethod as MemberBase, iterator.GenericMethod, "Iterator")
{
this.Iterator = iterator;
+ HasHoistedVariables = true;
}
public Field PC {
get { return hoisted_params; }
}
- public override TypeExpr [] GetClassBases (out TypeExpr base_class)
+ protected override TypeExpr [] ResolveBaseTypes (out TypeExpr base_class)
{
iterator_type_expr = new TypeExpression (MutateType (Iterator.OriginalIteratorType), Location);
type_bases = list;
- return base.GetClassBases (out base_class);
+ return base.ResolveBaseTypes (out base_class);
}
protected override string GetVariableMangledName (LocalInfo local_info)
Define_Reset ();
if (Iterator.IsEnumerable) {
- new GetEnumeratorMethod (this, false);
+ MemberName name = new MemberName (
+ new MemberName ("System.Collections.IEnumerable", Location), "GetEnumerator", Location);
+
#if GMCS_SOURCE
- new GetEnumeratorMethod (this, true);
+ Method get_enumerator = new IteratorMethod (this, enumerator_type, 0, name);
+
+ name = new MemberName (
+ new MemberName ("System.Collections.Generic.IEnumerable", generic_args, Location), "GetEnumerator", Location);
+ Method gget_enumerator = new GetEnumeratorMethod (this, generic_enumerator_type, name);
+
+ //
+ // Just call generic GetEnumerator implementation
+ //
+ get_enumerator.Block.AddStatement (
+ new Return (new Invocation (new DynamicMethodGroupExpr (gget_enumerator, Location), new ArrayList (0)), Location));
+
+ AddMethod (get_enumerator);
+ AddMethod (gget_enumerator);
+#else
+ AddMethod (new GetEnumeratorMethod (this, enumerator_type, name));
#endif
}
ToplevelBlock get_block = new ToplevelBlock (Location);
get_block.AddStatement (new CurrentBlock (this, is_generic));
- Accessor getter = new Accessor (get_block, 0, null, Location);
+ Accessor getter = new Accessor (get_block, 0, null, null, Location);
Property current = new Property (
- this, type, Modifiers.DEBUGGER_HIDDEN, false, name, null, getter, null, false);
+ this, type, Modifiers.DEBUGGER_HIDDEN, name, null, getter, null, false);
AddProperty (current);
}
Method reset = new Method (
this, null, TypeManager.system_void_expr,
Modifiers.PUBLIC | Modifiers.DEBUGGER_HIDDEN,
- false, new MemberName ("Reset", Location),
+ new MemberName ("Reset", Location),
Parameters.EmptyReadOnlyParameters, null);
AddMethod (reset);
return new Throw (new New (ex_type, null, Location), Location);
}
- protected class GetEnumeratorMethod : Method
- {
- public IteratorStorey Host;
-
- static MemberName GetMemberName (IteratorStorey 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 (IteratorStorey host, bool is_generic)
- : base (host, null, is_generic ?
- host.generic_enumerator_type : host.enumerator_type,
- Modifiers.DEBUGGER_HIDDEN, 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_name));
- }
-
- 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
- {
- IteratorStorey host;
- Expression type;
-
- Expression cast;
-
- public GetEnumeratorStatement (IteratorStorey 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;
-
- TypeExpression storey_type_expr = new TypeExpression (host.TypeBuilder, loc);
- Expression new_storey;
- if (host.hoisted_params != null) {
- ArrayList init = new ArrayList (host.HoistedParameters.Count);
- foreach (HoistedParameter hp in host.HoistedParameters) {
- FieldExpr from = new FieldExpr (hp.Field.FieldBuilder, loc);
- from.InstanceExpression = CompilerGeneratedThis.Instance;
- init.Add (new ElementInitializer (hp.Field.Name, from, loc));
- }
-
- new_storey = new NewInitialize (storey_type_expr, new ArrayList (0),
- new CollectionOrObjectInitializers (init, loc), loc);
- } else {
- new_storey = new New (storey_type_expr, new ArrayList (0), loc);
- }
-
- new_storey = new_storey.Resolve (ec);
- if (new_storey != null)
- cast = Convert.ImplicitConversionRequired (ec, new_storey, type.Type, loc);
-
- if (TypeManager.int_interlocked_compare_exchange == null) {
- Type t = TypeManager.CoreLookupType ("System.Threading", "Interlocked", Kind.Class, true);
- if (t != null) {
- TypeManager.int_interlocked_compare_exchange = TypeManager.GetPredefinedMethod (
- t, "CompareExchange", loc, TypeManager.GetReferenceType (TypeManager.int32_type),
- TypeManager.int32_type, TypeManager.int32_type);
- }
- }
-
- 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.Start);
- ig.Emit (OpCodes.Ldc_I4, (int) Iterator.State.Uninitialized);
- ig.Emit (OpCodes.Call, TypeManager.int_interlocked_compare_exchange);
-
- 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);
-
- cast.Emit (ec);
- ig.Emit (OpCodes.Ret);
- }
-
- public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
- {
- throw new NotSupportedException ();
- }
- }
- }
-
- protected class DisposeMethod : Method
- {
- readonly IteratorStorey Host;
-
- public DisposeMethod (IteratorStorey host)
- : base (host, null, TypeManager.system_void_expr,
- Modifiers.PUBLIC | Modifiers.DEBUGGER_HIDDEN | Modifiers.COMPILER_GENERATED,
- false, new MemberName ("Dispose", host.Location),
- Parameters.EmptyReadOnlyParameters, null)
- {
- this.Host = host;
-
- host.AddMethod (this);
-
- Block = new ToplevelBlock (host.Iterator.Container, 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;
- }
-
- //public override void Emit ()
- //{
- // if (Parent.MemberName.IsGeneric)
- // block.MutateHoistedGenericType (Host.Iterator.Storey);
-
- // base.Emit ();
- //}
-
- 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);
- }
-
- public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
- {
- throw new NotSupportedException ();
- }
- }
- }
-
protected class CurrentBlock : Statement {
IteratorStorey host;
bool is_generic;
loc = host.Location;
}
+ protected override void CloneTo (CloneContext clonectx, Statement target)
+ {
+ throw new NotSupportedException ();
+ }
+
public override bool Resolve (EmitContext ec)
{
// We emit a 'ret', so prevent the enclosing TopLevelBlock from emitting one too
//
public class Iterator : AnonymousExpression {
public readonly IMethodData OriginalMethod;
-
+ AnonymousMethodMethod method;
+ public readonly TypeContainer Host;
public readonly bool IsEnumerable;
//
ArrayList resume_points;
- public int AddResumePoint (ResumableStatement stmt, Location loc)
+ public int AddResumePoint (ResumableStatement stmt)
{
if (resume_points == null)
resume_points = new ArrayList ();
//
// Our constructor
//
- private Iterator (IMethodData method, DeclSpace host, Type iterator_type, bool is_enumerable)
- : base (host,
+ private Iterator (IMethodData method, TypeContainer host, Type iterator_type, bool is_enumerable)
+ : base (
new ToplevelBlock (method.Block, Parameters.EmptyReadOnlyParameters, method.Block.StartLocation),
TypeManager.bool_type,
method.Location)
this.OriginalMethod = method;
this.OriginalIteratorType = iterator_type;
this.IsEnumerable = is_enumerable;
+ this.Host = host;
IteratorHost = Block.ChangeToIterator (this, method.Block);
}
throw new NotSupportedException ("ET");
}
- public static void CreateIterator (IMethodData method, DeclSpace parent, int modifiers)
+ public static void CreateIterator (IMethodData method, TypeContainer parent, int modifiers)
{
bool is_enumerable;
Type iterator_type;
return;
}
- if (p.ParameterType.IsPointer) {
+ if (parameters.Types [i].IsPointer) {
Report.Error (1637, p.Location,
"Iterators cannot have unsafe parameters or " +
"yield types");