var da_false = new DefiniteAssignmentBitSet (fc.DefiniteAssignmentOnFalse);
- fc.DefiniteAssignment = fc.DefiniteAssignmentOnTrue;
+ fc.BranchDefiniteAssignment (fc.DefiniteAssignmentOnTrue);
+ var labels = fc.CopyLabelStack ();
var res = TrueStatement.FlowAnalysis (fc);
+ fc.SetLabelStack (labels);
+
if (FalseStatement == null) {
+
var c = expr as Constant;
if (c != null && !c.IsDefaultValue)
return true_returns;
if (true_returns) {
fc.DefiniteAssignment = da_false;
- return FalseStatement.FlowAnalysis (fc);
+
+ res = FalseStatement.FlowAnalysis (fc);
+ fc.SetLabelStack (labels);
+ return res;
}
var da_true = fc.DefiniteAssignment;
fc.DefiniteAssignment = da_false;
+
res &= FalseStatement.FlowAnalysis (fc);
+ fc.SetLabelStack (labels);
+
if (!TrueStatement.IsUnreachable) {
if (false_returns || FalseStatement.IsUnreachable)
fc.DefiniteAssignment = da_true;
ec.Emit (OpCodes.Br, ec.LoopBegin);
ec.MarkLabel (while_loop);
+ expr.EmitPrepare (ec);
+
Statement.Emit (ec);
ec.MarkLabel (ec.LoopBegin);
{
expr.FlowAnalysisConditional (fc);
- fc.DefiniteAssignment = fc.DefiniteAssignmentOnTrue;
var da_false = new DefiniteAssignmentBitSet (fc.DefiniteAssignmentOnFalse);
+ fc.BranchDefiniteAssignment (fc.DefiniteAssignmentOnTrue);
+
Statement.FlowAnalysis (fc);
//
public class Return : ExitStatement
{
Expression expr;
+ bool expr_returns;
public Return (Expression expr, Location l)
{
// Special case hoisted return value (happens in try/finally scenario)
//
if (ec.TryFinallyUnwind != null) {
- exit_label = TryFinally.EmitRedirectedReturn (ec, async_body);
+ exit_label = TryFinally.EmitRedirectedReturn (ec, async_body, unwind_protect);
}
var async_return = (IAssignMethod)storey.HoistedReturnValue;
expr.Emit (ec);
if (ec.TryFinallyUnwind != null)
- exit_label = TryFinally.EmitRedirectedReturn (ec, async_body);
+ exit_label = TryFinally.EmitRedirectedReturn (ec, async_body, unwind_protect);
}
ec.Emit (OpCodes.Leave, exit_label);
if (expr != null)
expr.FlowAnalysis (fc);
- base.DoFlowAnalysis (fc);
+ if (!expr_returns)
+ base.DoFlowAnalysis (fc);
+
return true;
}
public override Reachability MarkReachable (Reachability rc)
{
base.MarkReachable (rc);
+
+ if (Expr != null) {
+ rc = Expr.MarkReachable (rc);
+ expr_returns = rc.IsUnreachable;
+ }
+
return Reachability.CreateUnreachable ();
}
if (ec.TryFinallyUnwind != null && IsLeavingFinally (label.Block)) {
var async_body = (AsyncInitializer) ec.CurrentAnonymousMethod;
- l = TryFinally.EmitRedirectedJump (ec, async_body, l, label.Block);
+ l = TryFinally.EmitRedirectedJump (ec, async_body, l, label.Block, unwind_protect);
}
ec.Emit (unwind_protect ? OpCodes.Leave : OpCodes.Br, l);
}
}
+ public static Expression ConvertType (ResolveContext rc, Expression expr)
+ {
+ var et = rc.BuiltinTypes.Exception;
+ if (Convert.ImplicitConversionExists (rc, expr, et))
+ expr = Convert.ImplicitConversion (rc, expr, et, expr.Location);
+ else {
+ rc.Report.Error (155, expr.Location, "The type caught or thrown must be derived from System.Exception");
+ expr = EmptyCast.Create (expr, et);
+ }
+
+ return expr;
+ }
+
public override bool Resolve (BlockContext ec)
{
if (expr == null) {
if (expr == null)
return false;
- var et = ec.BuiltinTypes.Exception;
- if (Convert.ImplicitConversionExists (ec, expr, et))
- expr = Convert.ImplicitConversion (ec, expr, et, loc);
- else
- ec.Report.Error (155, expr.Location, "The type caught or thrown must be derived from System.Exception");
+ expr = ConvertType (ec, expr);
return true;
}
if (ec.TryFinallyUnwind != null) {
var async_body = (AsyncInitializer) ec.CurrentAnonymousMethod;
- l = TryFinally.EmitRedirectedJump (ec, async_body, l, enclosing_loop.Statement as Block);
+ l = TryFinally.EmitRedirectedJump (ec, async_body, l, enclosing_loop.Statement as Block, unwind_protect);
}
ec.Emit (unwind_protect ? OpCodes.Leave : OpCodes.Br, l);
if (ec.TryFinallyUnwind != null) {
var async_body = (AsyncInitializer) ec.CurrentAnonymousMethod;
- l = TryFinally.EmitRedirectedJump (ec, async_body, l, enclosing_loop.Statement as Block);
+ l = TryFinally.EmitRedirectedJump (ec, async_body, l, enclosing_loop.Statement as Block, unwind_protect);
}
ec.Emit (unwind_protect ? OpCodes.Leave : OpCodes.Br, l);
public override Reachability MarkReachable (Reachability rc)
{
- var init = initializer as ExpressionStatement;
- if (init != null)
- init.MarkReachable (rc);
-
- return base.MarkReachable (rc);
+ base.MarkReachable (rc);
+ return initializer == null ? rc : initializer.MarkReachable (rc);
}
protected override void CloneTo (CloneContext clonectx, Statement target)
public override void Emit (EmitContext ec)
{
+ if (!Variable.IsUsed)
+ ec.Report.Warning (219, 3, loc, "The constant `{0}' is never used", Variable.Name);
+
// Nothing to emit, not even sequence point
}
}
}
+ public bool Created {
+ get {
+ return builder != null;
+ }
+ }
+
public bool IsDeclared {
get {
return type != null;
}
}
+ public bool IsUsed {
+ get {
+ return (flags & Flags.Used) != 0;
+ }
+ }
+
public bool IsFixed {
get {
return (flags & Flags.FixedVariable) != 0;
public Expression CreateReferenceExpression (ResolveContext rc, Location loc)
{
- if (IsConstant && const_value != null)
+ if (IsConstant && const_value != null) {
+ SetIsUsed ();
return Constant.CreateConstantFromValue (Type, const_value.GetValue (), loc);
+ }
return new LocalVariableReference (this, loc);
}
AwaitBlock = 1 << 13,
FinallyBlock = 1 << 14,
CatchBlock = 1 << 15,
+ HasReferenceToStoreyForInstanceLambdas = 1 << 16,
Iterator = 1 << 20,
NoFlowAnalysis = 1 << 21,
InitializationEmitted = 1 << 22
public override void Emit (EmitContext ec)
{
- if (Parent != null) {
- // TODO: It's needed only when scope has variable (normal or lifted)
- ec.BeginScope (GetDebugSymbolScopeIndex ());
+ // TODO: It's needed only when scope has variable (normal or lifted)
+ var scopeIndex = GetDebugSymbolScopeIndex ();
+ if (scopeIndex > 0) {
+ ec.BeginScope (scopeIndex);
}
EmitScopeInitialization (ec);
DoEmit (ec);
- if (Parent != null)
+ if (scopeIndex > 0)
ec.EndScope ();
if (ec.EmitAccurateDebugInfo && HasReachableClosingBrace && !(this is ParametersBlock) &&
//
storey.CreateContainer ();
storey.DefineContainer ();
+ storey.ExpandBaseInterfaces ();
if (Original.Explicit.HasCapturedThis && Original.ParametersBlock.TopBlock.ThisReferencesFromChildrenBlock != null) {
break;
}
}
-
+
//
// We are the first storey on path and 'this' has to be hoisted
//
//
// If we are state machine with no parent. We can hook into parent without additional
- // reference and capture this directly
+ // reference and capture this directly
//
ExplicitBlock parent_storey_block = pb;
while (parent_storey_block.Parent != null) {
storey.Parent.PartialContainer.AddCompilerGeneratedClass (storey);
}
- public int GetDebugSymbolScopeIndex ()
+ public void DisableDebugScopeIndex ()
+ {
+ debug_scope_index = -1;
+ }
+
+ public virtual int GetDebugSymbolScopeIndex ()
{
if (debug_scope_index == 0)
debug_scope_index = ++ParametersBlock.debug_scope_index;
#region Properties
+ public bool HasReferenceToStoreyForInstanceLambdas {
+ get {
+ return (flags & Flags.HasReferenceToStoreyForInstanceLambdas) != 0;
+ }
+ set {
+ flags = value ? flags | Flags.HasReferenceToStoreyForInstanceLambdas : flags & ~Flags.HasReferenceToStoreyForInstanceLambdas;
+ }
+ }
+
public bool IsAsync {
get {
return (flags & Flags.HasAsyncModifier) != 0;
return res;
}
+ public override int GetDebugSymbolScopeIndex ()
+ {
+ return 0;
+ }
+
public LabeledStatement GetLabel (string name, Block block)
{
//
continue;
}
- if (constant_label != null && constant_label != sl)
+ if (section_rc.IsUnreachable) {
+ //
+ // Common case. Previous label section end is unreachable as
+ // it ends with break, return, etc. For next section revert
+ // to reachable again unless we have constant switch block
+ //
+ section_rc = constant_label != null && constant_label != sl ?
+ Reachability.CreateUnreachable () :
+ new Reachability ();
+ } else if (prev_label != null) {
+ //
+ // Error case as control cannot fall through from one case label
+ //
+ sl.SectionStart = false;
+ s = new MissingBreak (prev_label);
+ s.MarkReachable (rc);
+ block.Statements.Insert (i - 1, s);
+ ++i;
+ } else if (constant_label != null && constant_label != sl) {
+ //
+ // Special case for the first unreachable label in constant
+ // switch block
+ //
section_rc = Reachability.CreateUnreachable ();
- else if (section_rc.IsUnreachable) {
- section_rc = new Reachability ();
- } else {
- if (prev_label != null) {
- sl.SectionStart = false;
- s = new MissingBreak (prev_label);
- s.MarkReachable (rc);
- block.Statements.Insert (i - 1, s);
- ++i;
- }
}
prev_label = sl;
ec.EmitInt (first_resume_pc);
ec.Emit (OpCodes.Sub);
- var labels = new Label[resume_points.Count - System.Math.Max (first_catch_resume_pc, 0)];
+ var labels = new Label [first_catch_resume_pc > 0 ? first_catch_resume_pc : resume_points.Count];
for (int i = 0; i < labels.Length; ++i)
labels[i] = resume_points[i].PrepareForEmit (ec);
ec.Emit (OpCodes.Switch, labels);
public override bool Resolve (BlockContext ec)
{
if (ec.CurrentIterator != null)
- ec.Report.Error (1629, loc, "Unsafe code may not appear in iterators");
+ Expression.UnsafeInsideIteratorError (ec, loc);
using (ec.Set (ResolveContext.Options.UnsafeScope))
return Block.Resolve (ec);
{
ExplicitBlock fini;
List<DefiniteAssignmentBitSet> try_exit_dat;
- List<Label> redirected_jumps;
+ List<Tuple<Label, bool>> redirected_jumps;
Label? start_fin_label;
public TryFinally (Statement stmt, ExplicitBlock fini, Location loc)
ec.Emit (OpCodes.Stloc, temp);
var exception_field = ec.GetTemporaryField (type);
+ exception_field.AutomaticallyReuse = false;
ec.EmitThis ();
ec.Emit (OpCodes.Ldloc, temp);
exception_field.EmitAssignFromStack (ec);
ec.Emit (OpCodes.Throw);
ec.MarkLabel (skip_throw);
- exception_field.IsAvailableForReuse = true;
+ exception_field.PrepareCleanup (ec);
EmitUnwindFinallyTable (ec);
}
return false;
}
- public static Label EmitRedirectedJump (EmitContext ec, AsyncInitializer initializer, Label label, Block labelBlock)
+ public static Label EmitRedirectedJump (EmitContext ec, AsyncInitializer initializer, Label label, Block labelBlock, bool unwindProtect)
{
int idx;
if (labelBlock != null) {
if (labelBlock != null && !fin.IsParentBlock (labelBlock))
break;
- fin.EmitRedirectedExit (ec, label, initializer, set_return_state);
+ fin.EmitRedirectedExit (ec, label, initializer, set_return_state, unwindProtect);
set_return_state = false;
if (fin.start_fin_label == null) {
return label;
}
- public static Label EmitRedirectedReturn (EmitContext ec, AsyncInitializer initializer)
+ public static Label EmitRedirectedReturn (EmitContext ec, AsyncInitializer initializer, bool unwindProtect)
{
- return EmitRedirectedJump (ec, initializer, initializer.BodyEnd, null);
+ return EmitRedirectedJump (ec, initializer, initializer.BodyEnd, null, unwindProtect);
}
- void EmitRedirectedExit (EmitContext ec, Label label, AsyncInitializer initializer, bool setReturnState)
+ void EmitRedirectedExit (EmitContext ec, Label label, AsyncInitializer initializer, bool setReturnState, bool unwindProtect)
{
if (redirected_jumps == null) {
- redirected_jumps = new List<Label> ();
+ redirected_jumps = new List<Tuple<Label, bool>> ();
// Add fallthrough label
- redirected_jumps.Add (ec.DefineLabel ());
+ redirected_jumps.Add (Tuple.Create (ec.DefineLabel (), false));
if (setReturnState)
initializer.HoistedReturnState = ec.GetTemporaryField (ec.Module.Compiler.BuiltinTypes.Int, true);
}
- int index = redirected_jumps.IndexOf (label);
+ int index = redirected_jumps.FindIndex (l => l.Item1 == label);
if (index < 0) {
- redirected_jumps.Add (label);
+ redirected_jumps.Add (Tuple.Create (label, unwindProtect));
index = redirected_jumps.Count - 1;
}
var initializer = (AsyncInitializer)ec.CurrentAnonymousMethod;
initializer.HoistedReturnState.EmitLoad (ec);
- ec.Emit (OpCodes.Switch, redirected_jumps.ToArray ());
+
+ var jumps_table = new Label [redirected_jumps.Count];
+ List<Tuple<Label, Label>> leave_redirect = null;
+ for (int i = 0; i < jumps_table.Length; ++i) {
+ var val = redirected_jumps [i];
+
+ if (val.Item2) {
+ if (leave_redirect == null)
+ leave_redirect = new List<Tuple<Label, Label>> ();
+ var label = ec.DefineLabel ();
+ leave_redirect.Add (Tuple.Create (label, val.Item1));
+ jumps_table [i] = label;
+ } else {
+ jumps_table [i] = val.Item1;
+ }
+ }
+
+ ec.Emit (OpCodes.Switch, jumps_table);
+
+ if (leave_redirect != null) {
+ foreach (var entry in leave_redirect) {
+ ec.MarkLabel (entry.Item1);
+ ec.Emit (OpCodes.Leave, entry.Item2);
+ }
+ }
// Mark fallthrough label
- ec.MarkLabel (redirected_jumps [0]);
+ ec.MarkLabel (jumps_table [0]);
}
protected override bool DoFlowAnalysis (FlowAnalysisContext fc)
ec.Emit (OpCodes.Br, end);
ec.MarkLabel (labels [i + 1]);
+
+ ec.EmitInt (0);
+ ec.Emit (OpCodes.Stloc, state_variable);
+
c = catch_sm [i];
ec.AsyncThrowVariable = c.Variable;
c.Block.Emit (ec);
ec.LoopBegin = ec.DefineLabel ();
ec.LoopEnd = ec.DefineLabel ();
- if (!(Statement is Block))
- ec.BeginCompilerScope (variable.Block.Explicit.GetDebugSymbolScopeIndex ());
+ ec.BeginCompilerScope (variable.Block.Explicit.GetDebugSymbolScopeIndex ());
+ body.Explicit.DisableDebugScopeIndex ();
variable.CreateBuilder (ec);
Statement.Emit (ec);
- if (!(Statement is Block))
- ec.EndScope ();
+ ec.EndScope ();
ec.LoopBegin = old_begin;
ec.LoopEnd = old_end;