// Copyright 2011 Xamarin Inc.
//
-// TODO:
-// Flow analysis for Yield.
-//
-
using System;
using System.Collections.Generic;
using Mono.CompilerServices.SymbolWriter;
protected bool unwind_protect;
protected T machine_initializer;
int resume_pc;
+ ExceptionStatement inside_try_block;
protected YieldStatement (Expression expr, Location l)
{
machine_initializer.InjectYield (ec, expr, resume_pc, unwind_protect, resume_point);
}
+ protected override bool DoFlowAnalysis (FlowAnalysisContext fc)
+ {
+ expr.FlowAnalysis (fc);
+
+ RegisterResumePoint ();
+
+ return false;
+ }
+
public override bool Resolve (BlockContext bc)
{
expr = expr.Resolve (bc);
return false;
machine_initializer = bc.CurrentAnonymousMethod as T;
-
- if (!bc.CurrentBranching.CurrentUsageVector.IsUnreachable)
- unwind_protect = bc.CurrentBranching.AddResumePoint (this, out resume_pc);
-
+ inside_try_block = bc.CurrentTryBlock;
return true;
}
+
+ public void RegisterResumePoint ()
+ {
+ if (inside_try_block == null) {
+ resume_pc = machine_initializer.AddResumePoint (this);
+ } else {
+ resume_pc = inside_try_block.AddResumePoint (this, resume_pc, machine_initializer);
+ unwind_protect = true;
+ inside_try_block = null;
+ }
+ }
}
public class Yield : YieldStatement<Iterator>
{
}
- public static bool CheckContext (ResolveContext ec, Location loc)
+ public static bool CheckContext (BlockContext bc, Location loc)
{
- if (!ec.CurrentAnonymousMethod.IsIterator) {
- ec.Report.Error (1621, loc,
+ if (!bc.CurrentAnonymousMethod.IsIterator) {
+ bc.Report.Error (1621, loc,
"The yield statement cannot be used inside anonymous method blocks");
return false;
}
+ if (bc.HasSet (ResolveContext.Options.FinallyScope)) {
+ bc.Report.Error (1625, loc, "Cannot yield in the body of a finally clause");
+ return false;
+ }
+
return true;
}
if (!CheckContext (bc, loc))
return false;
+ if (bc.HasAny (ResolveContext.Options.TryWithCatchScope)) {
+ bc.Report.Error (1626, loc, "Cannot yield a value in the body of a try block with a catch clause");
+ }
+
+ if (bc.HasSet (ResolveContext.Options.CatchScope)) {
+ bc.Report.Error (1631, loc, "Cannot yield a value in the body of a catch clause");
+ }
+
if (!base.Resolve (bc))
return false;
loc = l;
}
- public override void Error_FinallyClause (Report Report)
- {
- Report.Error (1625, loc, "Cannot yield in the body of a finally clause");
+ protected override bool IsLocalExit {
+ get {
+ return false;
+ }
}
protected override void CloneTo (CloneContext clonectx, Statement target)
throw new NotSupportedException ();
}
- protected override bool DoResolve (BlockContext ec)
+ protected override bool DoResolve (BlockContext bc)
{
- iterator = ec.CurrentIterator;
- return Yield.CheckContext (ec, loc);
+ iterator = bc.CurrentIterator;
+ return Yield.CheckContext (bc, loc);
}
protected override void DoEmit (EmitContext ec)
{
iterator.EmitYieldBreak (ec, unwind_protect);
}
+
+ protected override bool DoFlowAnalysis (FlowAnalysisContext fc)
+ {
+ return true;
+ }
+
+ public override Reachability MarkReachable (Reachability rc)
+ {
+ base.MarkReachable (rc);
+ return Reachability.CreateUnreachable ();
+ }
public override object Accept (StructuralVisitor visitor)
{
for (int i = 0; i < host.hoisted_params.Count; ++i) {
HoistedParameter hp = host.hoisted_params [i];
- HoistedParameter hp_cp = host.hoisted_params_copy [i];
+ HoistedParameter hp_cp = host.hoisted_params_copy [i] ?? hp;
FieldExpr from = new FieldExpr (hp_cp.Field, loc);
from.InstanceExpression = new CompilerGeneratedThis (ec.CurrentType, loc);
if (new_storey != null)
new_storey = Convert.ImplicitConversionRequired (ec, new_storey, host_method.MemberType, loc);
- ec.CurrentBranching.CurrentUsageVector.Goto ();
return true;
}
new_storey.Emit (ec);
ec.Emit (OpCodes.Ret);
}
+
+ protected override bool DoFlowAnalysis (FlowAnalysisContext fc)
+ {
+ throw new NotImplementedException ();
+ }
+
+ public override Reachability MarkReachable (Reachability rc)
+ {
+ base.MarkReachable (rc);
+ return Reachability.CreateUnreachable ();
+ }
}
GetEnumeratorMethod (IteratorStorey host, FullNamedExpression returnType, MemberName name)
- : base (host, null, returnType, Modifiers.DEBUGGER_HIDDEN, name)
+ : base (host, null, returnType, Modifiers.DEBUGGER_HIDDEN, name, ToplevelBlock.Flags.CompilerGenerated | ToplevelBlock.Flags.NoFlowAnalysis)
{
}
var m = new GetEnumeratorMethod (host, returnType, name);
var stmt = statement ?? new GetEnumeratorStatement (host, m);
m.block.AddStatement (stmt);
- m.block.IsCompilerGenerated = true;
return m;
}
}
ec.CurrentAnonymousMethod = iterator;
iterator.EmitDispose (ec);
}
+
+ protected override bool DoFlowAnalysis (FlowAnalysisContext fc)
+ {
+ throw new NotImplementedException ();
+ }
}
public DisposeMethod (IteratorStorey host)
: base (host, null, new TypeExpression (host.Compiler.BuiltinTypes.Void, host.Location), Modifiers.PUBLIC | Modifiers.DEBUGGER_HIDDEN,
- new MemberName ("Dispose", host.Location))
+ new MemberName ("Dispose", host.Location), ToplevelBlock.Flags.CompilerGenerated | ToplevelBlock.Flags.NoFlowAnalysis)
{
host.Members.Add (this);
Block.AddStatement (new DisposeMethodStatement (host.Iterator));
- Block.IsCompilerGenerated = true;
}
}
get { return hoisted_params; }
}
+ protected override Constructor DefineDefaultConstructor (bool is_static)
+ {
+ var ctor = base.DefineDefaultConstructor (is_static);
+ ctor.ModFlags |= Modifiers.DEBUGGER_HIDDEN;
+ return ctor;
+ }
+
protected override TypeSpec[] ResolveBaseTypes (out FullNamedExpression base_class)
{
var mtype = Iterator.OriginalIteratorType;
current_field = AddCompilerGeneratedField ("$current", iterator_type_expr);
disposing_field = AddCompilerGeneratedField ("$disposing", new TypeExpression (Compiler.BuiltinTypes.Bool, Location));
- if (hoisted_params != null) {
+ if (Iterator.IsEnumerable && hoisted_params != null) {
//
// Iterators are independent, each GetEnumerator call has to
// create same enumerator therefore we have to keep original values
// around for re-initialization
//
- // TODO: Do it for assigned/modified parameters only
- //
hoisted_params_copy = new List<HoistedParameter> (hoisted_params.Count);
foreach (HoistedParameter hp in hoisted_params) {
- hoisted_params_copy.Add (new HoistedParameter (hp, "<$>" + hp.Field.Name));
+
+ //
+ // Don't create field copy for unmodified captured parameters
+ //
+ HoistedParameter hp_copy;
+ if (hp.IsAssigned) {
+ hp_copy = new HoistedParameter (hp, "<$>" + hp.Field.Name);
+ } else {
+ hp_copy = null;
+ }
+
+ hoisted_params_copy.Add (hp_copy);
}
}
var name = new MemberName ("Current", null, explicit_iface, Location);
- ToplevelBlock get_block = new ToplevelBlock (Compiler, Location) {
- IsCompilerGenerated = true
- };
+ ToplevelBlock get_block = new ToplevelBlock (Compiler, ParametersCompiled.EmptyReadOnlyParameters, Location,
+ Block.Flags.CompilerGenerated | Block.Flags.NoFlowAnalysis);
get_block.AddStatement (new Return (new DynamicFieldExpr (CurrentField, Location), Location));
Property current = new Property (this, type, Modifiers.DEBUGGER_HIDDEN | Modifiers.COMPILER_GENERATED, name, null);
ParametersCompiled.EmptyReadOnlyParameters, null);
Members.Add (reset);
- reset.Block = new ToplevelBlock (Compiler, Location) {
- IsCompilerGenerated = true
- };
+ reset.Block = new ToplevelBlock (Compiler, reset.ParameterInfo, Location,
+ Block.Flags.CompilerGenerated | Block.Flags.NoFlowAnalysis);
TypeSpec ex_type = Module.PredefinedTypes.NotSupportedException.Resolve ();
if (ex_type == null)
reset.Block.AddStatement (new Throw (new New (new TypeExpression (ex_type, Location), null, Location), Location));
}
- protected override void EmitHoistedParameters (EmitContext ec, IList<HoistedParameter> hoisted)
+ protected override void EmitHoistedParameters (EmitContext ec, List<HoistedParameter> hoisted)
{
base.EmitHoistedParameters (ec, hoisted);
- base.EmitHoistedParameters (ec, hoisted_params_copy);
+ if (hoisted_params_copy != null)
+ base.EmitHoistedParameters (ec, hoisted_params_copy);
}
}
{
readonly StateMachineInitializer expr;
- public StateMachineMethod (StateMachine host, StateMachineInitializer expr, FullNamedExpression returnType, Modifiers mod, MemberName name)
+ public StateMachineMethod (StateMachine host, StateMachineInitializer expr, FullNamedExpression returnType,
+ Modifiers mod, MemberName name, ToplevelBlock.Flags blockFlags)
: base (host, returnType, mod | Modifiers.COMPILER_GENERATED,
name, ParametersCompiled.EmptyReadOnlyParameters, null)
{
this.expr = expr;
- Block = new ToplevelBlock (host.Compiler, ParametersCompiled.EmptyReadOnlyParameters, Location.Null);
+ Block = new ToplevelBlock (host.Compiler, ParametersCompiled.EmptyReadOnlyParameters, Location.Null, blockFlags);
}
public override EmitContext CreateEmitContext (ILGenerator ig, SourceMethodBuilder sourceMethod)
// Don't create sequence point
DoEmit (ec);
}
+
+ protected override bool DoFlowAnalysis (FlowAnalysisContext fc)
+ {
+ return state_machine.ReturnType.Kind != MemberKind.Void;
+ }
+
+ public override Reachability MarkReachable (Reachability rc)
+ {
+ base.MarkReachable (rc);
+
+ if (state_machine.ReturnType.Kind != MemberKind.Void)
+ rc = Reachability.CreateUnreachable ();
+
+ return rc;
+ }
}
public readonly TypeDefinition Host;
throw new NotSupportedException ("ET");
}
- protected virtual BlockContext CreateBlockContext (ResolveContext rc)
+ protected virtual BlockContext CreateBlockContext (BlockContext bc)
{
- var ctx = new BlockContext (rc, block, ((BlockContext) rc).ReturnType);
+ var ctx = new BlockContext (bc, block, bc.ReturnType);
ctx.CurrentAnonymousMethod = this;
+
+ ctx.AssignmentInfoOffset = bc.AssignmentInfoOffset;
+ ctx.EnclosingLoop = bc.EnclosingLoop;
+ ctx.EnclosingLoopOrSwitch = bc.EnclosingLoopOrSwitch;
+ ctx.Switch = bc.Switch;
+
return ctx;
}
- protected override Expression DoResolve (ResolveContext ec)
+ protected override Expression DoResolve (ResolveContext rc)
{
- var ctx = CreateBlockContext (ec);
+ var bc = (BlockContext) rc;
+ var ctx = CreateBlockContext (bc);
Block.Resolve (ctx);
- //
- // Explicit return is required for Task<T> state machine
- //
- var task_storey = storey as AsyncTaskStorey;
- if (task_storey == null || (task_storey.ReturnType != null && !task_storey.ReturnType.IsGenericTask))
- ctx.CurrentBranching.CurrentUsageVector.Goto ();
-
- ctx.EndFlowBranching ();
-
- if (!ec.IsInProbingMode) {
- var move_next = new StateMachineMethod (storey, this, new TypeExpression (ReturnType, loc), Modifiers.PUBLIC, new MemberName ("MoveNext", loc));
+ if (!rc.IsInProbingMode) {
+ var move_next = new StateMachineMethod (storey, this, new TypeExpression (ReturnType, loc), Modifiers.PUBLIC, new MemberName ("MoveNext", loc), 0);
move_next.Block.AddStatement (new MoveNextBodyStatement (this));
storey.AddEntryMethod (move_next);
}
+ bc.AssignmentInfoOffset = ctx.AssignmentInfoOffset;
eclass = ExprClass.Value;
return this;
}
throw new NotSupportedException ();
}
+ protected override bool DoFlowAnalysis (FlowAnalysisContext fc)
+ {
+ throw new NotSupportedException ();
+ }
+
protected override void DoEmit (EmitContext ec)
{
//
//
ec.CurrentAnonymousMethod = iterator;
- using (ec.With (BuilderContext.Options.OmitDebugInfo, false)) {
+ using (ec.With (BuilderContext.Options.OmitDebugInfo, !ec.HasMethodSymbolBuilder)) {
block.EmitFinallyBody (ec);
}
}
Modifiers.COMPILER_GENERATED, new MemberName (CompilerGeneratedContainer.MakeName (null, null, "Finally", finally_hosts_counter++), loc),
ParametersCompiled.EmptyReadOnlyParameters, null);
- method.Block = new ToplevelBlock (method.Compiler, method.ParameterInfo, loc);
- method.Block.IsCompilerGenerated = true;
+ method.Block = new ToplevelBlock (method.Compiler, method.ParameterInfo, loc,
+ ToplevelBlock.Flags.CompilerGenerated | ToplevelBlock.Flags.NoFlowAnalysis);
method.Block.AddStatement (new TryFinallyBlockProxyStatement (this, block));
- storey.AddMember (method);
+ // Cannot it add to storey because it'd be emitted before nested
+ // anonoymous methods which could capture shared variable
+
return method;
}
public void EmitDispose (EmitContext ec)
{
+ if (resume_points == null)
+ return;
+
Label end = ec.DefineLabel ();
Label[] labels = null;
- int n_resume_points = resume_points == null ? 0 : resume_points.Count;
- for (int i = 0; i < n_resume_points; ++i) {
+ for (int i = 0; i < resume_points.Count; ++i) {
ResumableStatement s = resume_points[i];
Label ret = s.PrepareForDispose (ec, end);
if (ret.Equals (end) && labels == null)
ec.MarkLabel (resume_point);
}
- protected override BlockContext CreateBlockContext (ResolveContext rc)
- {
- var bc = base.CreateBlockContext (rc);
- bc.StartFlowBranching (this, rc.CurrentBranching);
- return bc;
- }
-
public static void CreateIterator (IMethodData method, TypeDefinition parent, Modifiers modifiers)
{
bool is_enumerable;
"The body of `{0}' cannot be an iterator block " +
"because `{1}' is not an iterator interface type",
method.GetSignatureForError (),
- TypeManager.CSharpName (ret));
+ ret.GetSignatureForError ());
return;
}