namespace Mono.CSharp
{
+ public enum TriState : byte {
+ // Never < Sometimes < Always
+ Never,
+ Sometimes,
+ Always
+ }
+
// <summary>
// A new instance of this class is created every time a new block is resolved
// and if there's branching in the block's control flow.
// A loop block.
Loop,
+ // The statement embedded inside a loop
+ Embedded,
+
+ // part of a block headed by a jump target
+ Labeled,
+
// Try/Catch block.
Exception,
Switch,
// Switch section.
- SwitchSection
+ SwitchSection,
+
+ // The toplevel block of a function
+ Toplevel
}
// <summary>
Finally
}
- // <summary>
- // This is used in the control flow analysis code to specify whether the
- // current code block may return to its enclosing block before reaching
- // its end.
- // </summary>
- public enum FlowReturns : byte {
- Undefined = 0,
-
- // It can never return.
- Never,
-
- // This means that the block contains a conditional return statement
- // somewhere.
- Sometimes,
-
- // The code always returns, ie. there's an unconditional return / break
- // statement in it.
- Always
- }
-
public sealed class Reachability
{
- FlowReturns returns, breaks, throws, barrier;
+ TriState returns, throws, barrier;
- public FlowReturns Returns {
+ public TriState Returns {
get { return returns; }
}
- public FlowReturns Breaks {
- get { return breaks; }
- }
- public FlowReturns Throws {
+ public TriState Throws {
get { return throws; }
}
- public FlowReturns Barrier {
+ public TriState Barrier {
get { return barrier; }
}
- public Reachability (FlowReturns returns, FlowReturns breaks,
- FlowReturns throws, FlowReturns barrier)
+
+ Reachability (TriState returns, TriState throws, TriState barrier)
{
this.returns = returns;
- this.breaks = breaks;
this.throws = throws;
this.barrier = barrier;
}
public Reachability Clone ()
{
- return new Reachability (returns, breaks, throws, barrier);
+ return new Reachability (returns, throws, barrier);
}
- // <summary>
- // Performs an `And' operation on the FlowReturns status
- // (for instance, a block only returns Always if all its siblings
- // always return).
- // </summary>
- public static FlowReturns AndFlowReturns (FlowReturns a, FlowReturns b)
+ public static TriState TriState_Meet (TriState a, TriState b)
{
- if (a == FlowReturns.Undefined)
- return b;
-
- switch (a) {
- case FlowReturns.Never:
- if (b == FlowReturns.Never)
- return FlowReturns.Never;
- else
- return FlowReturns.Sometimes;
-
- case FlowReturns.Sometimes:
- return FlowReturns.Sometimes;
-
- case FlowReturns.Always:
- if (b == FlowReturns.Always)
- return FlowReturns.Always;
- else
- return FlowReturns.Sometimes;
-
- default:
- throw new ArgumentException ();
- }
+ // (1) if both are Never, return Never
+ // (2) if both are Always, return Always
+ // (3) otherwise, return Sometimes
+ // note that (3) => (3') if both are Sometimes, return Sometimes
+ return a == b ? a : TriState.Sometimes;
}
- public static FlowReturns OrFlowReturns (FlowReturns a, FlowReturns b)
+ public static TriState TriState_Max (TriState a, TriState b)
{
- if (a == FlowReturns.Undefined)
- return b;
-
- switch (a) {
- case FlowReturns.Never:
- return b;
-
- case FlowReturns.Sometimes:
- if (b == FlowReturns.Always)
- return FlowReturns.Always;
- else
- return FlowReturns.Sometimes;
-
- case FlowReturns.Always:
- return FlowReturns.Always;
-
- default:
- throw new ArgumentException ();
- }
+ return ((byte) a > (byte) b) ? a : b;
}
- public static void And (ref Reachability a, Reachability b, bool do_break)
+ public void Meet (Reachability b)
{
- if (a == null) {
- a = b.Clone ();
- return;
- }
+ if ((AlwaysReturns && b.AlwaysHasBarrier) || (AlwaysHasBarrier && b.AlwaysReturns))
+ returns = TriState.Always;
+ else
+ returns = TriState_Meet (returns, b.returns);
- a.And (b, do_break);
+ throws = TriState_Meet (throws, b.throws);
+ barrier = TriState_Meet (barrier, b.barrier);
}
- public void And (Reachability b, bool do_break)
+ public void Or (Reachability b)
{
- //
- // `break' does not "break" in a Switch or a LoopBlock
- //
- bool a_breaks = do_break && AlwaysBreaks;
- bool b_breaks = do_break && b.AlwaysBreaks;
-
- bool a_has_barrier, b_has_barrier;
- if (do_break) {
- //
- // This is the normal case: the code following a barrier
- // cannot be reached.
- //
- a_has_barrier = AlwaysHasBarrier;
- b_has_barrier = b.AlwaysHasBarrier;
- } else {
- //
- // Special case for Switch and LoopBlocks: we can reach the
- // code after the barrier via the `break'.
- //
- a_has_barrier = !AlwaysBreaks && AlwaysHasBarrier;
- b_has_barrier = !b.AlwaysBreaks && b.AlwaysHasBarrier;
- }
-
- bool a_unreachable = a_breaks || AlwaysThrows || a_has_barrier;
- bool b_unreachable = b_breaks || b.AlwaysThrows || b_has_barrier;
-
- //
- // Do all code paths always return ?
- //
- if (AlwaysReturns) {
- if (b.AlwaysReturns || b_unreachable)
- returns = FlowReturns.Always;
- else
- returns = FlowReturns.Sometimes;
- } else if (b.AlwaysReturns) {
- if (AlwaysReturns || a_unreachable)
- returns = FlowReturns.Always;
- else
- returns = FlowReturns.Sometimes;
- } else if (!MayReturn) {
- if (b.MayReturn)
- returns = FlowReturns.Sometimes;
- else
- returns = FlowReturns.Never;
- } else if (!b.MayReturn) {
- if (MayReturn)
- returns = FlowReturns.Sometimes;
- else
- returns = FlowReturns.Never;
- }
-
- breaks = AndFlowReturns (breaks, b.breaks);
- throws = AndFlowReturns (throws, b.throws);
- barrier = AndFlowReturns (barrier, b.barrier);
-
- if (a_unreachable && b_unreachable)
- barrier = FlowReturns.Always;
- else if (a_unreachable || b_unreachable)
- barrier = FlowReturns.Sometimes;
- else
- barrier = FlowReturns.Never;
+ returns = TriState_Max (returns, b.returns);
+ throws = TriState_Max (throws, b.throws);
+ barrier = TriState_Max (barrier, b.barrier);
}
- public void Or (Reachability b)
+ public static Reachability Always ()
{
- returns = OrFlowReturns (returns, b.returns);
- breaks = OrFlowReturns (breaks, b.breaks);
- throws = OrFlowReturns (throws, b.throws);
- barrier = OrFlowReturns (barrier, b.barrier);
+ return new Reachability (TriState.Never, TriState.Never, TriState.Never);
}
- public static Reachability Never ()
- {
- return new Reachability (
- FlowReturns.Never, FlowReturns.Never,
- FlowReturns.Never, FlowReturns.Never);
+ TriState Unreachable {
+ get { return TriState_Max (returns, TriState_Max (throws, barrier)); }
}
- public FlowReturns Reachable {
+ TriState Reachable {
get {
- if ((returns == FlowReturns.Always) ||
- (breaks == FlowReturns.Always) ||
- (throws == FlowReturns.Always) ||
- (barrier == FlowReturns.Always))
- return FlowReturns.Never;
- else if ((returns == FlowReturns.Never) &&
- (breaks == FlowReturns.Never) &&
- (throws == FlowReturns.Never) &&
- (barrier == FlowReturns.Never))
- return FlowReturns.Always;
- else
- return FlowReturns.Sometimes;
+ TriState unreachable = Unreachable;
+ if (unreachable == TriState.Sometimes)
+ return TriState.Sometimes;
+ return unreachable == TriState.Always ? TriState.Never : TriState.Always;
}
}
- public bool AlwaysBreaks {
- get { return breaks == FlowReturns.Always; }
- }
-
- public bool MayBreak {
- get { return breaks != FlowReturns.Never; }
- }
-
public bool AlwaysReturns {
- get { return returns == FlowReturns.Always; }
- }
-
- public bool MayReturn {
- get { return returns != FlowReturns.Never; }
+ get { return returns == TriState.Always; }
}
public bool AlwaysThrows {
- get { return throws == FlowReturns.Always; }
- }
-
- public bool MayThrow {
- get { return throws != FlowReturns.Never; }
+ get { return throws == TriState.Always; }
}
public bool AlwaysHasBarrier {
- get { return barrier == FlowReturns.Always; }
- }
-
- public bool MayHaveBarrier {
- get { return barrier != FlowReturns.Never; }
+ get { return barrier == TriState.Always; }
}
public bool IsUnreachable {
- get { return Reachable == FlowReturns.Never; }
+ get { return Unreachable == TriState.Always; }
}
public void SetReturns ()
{
- returns = FlowReturns.Always;
- }
-
- public void SetReturnsSometimes ()
- {
- returns = FlowReturns.Sometimes;
- }
-
- public void SetBreaks ()
- {
- breaks = FlowReturns.Always;
- }
-
- public void ResetBreaks ()
- {
- breaks = FlowReturns.Never;
+ returns = TriState.Always;
}
public void SetThrows ()
{
- throws = FlowReturns.Always;
- }
-
- public void SetThrowsSometimes ()
- {
- throws = FlowReturns.Sometimes;
+ throws = TriState.Always;
}
public void SetBarrier ()
{
- barrier = FlowReturns.Always;
- }
-
- public void ResetBarrier ()
- {
- barrier = FlowReturns.Never;
+ barrier = TriState.Always;
}
- static string ShortName (FlowReturns returns)
+ static string ShortName (TriState returns)
{
switch (returns) {
- case FlowReturns.Never:
+ case TriState.Never:
return "N";
- case FlowReturns.Sometimes:
+ case TriState.Sometimes:
return "S";
default:
return "A";
public override string ToString ()
{
- return String.Format ("[{0}:{1}:{2}:{3}:{4}]",
- ShortName (returns), ShortName (breaks),
- ShortName (throws), ShortName (barrier),
+ return String.Format ("[{0}:{1}:{2}:{3}]",
+ ShortName (returns), ShortName (throws), ShortName (barrier),
ShortName (Reachable));
}
}
{
switch (type) {
case BranchingType.Exception:
+ case BranchingType.Labeled:
+ case BranchingType.Toplevel:
throw new InvalidOperationException ();
case BranchingType.Switch:
- return new FlowBranchingSwitch (parent, block, loc);
+ return new FlowBranchingBreakable (parent, type, SiblingType.SwitchSection, block, loc);
case BranchingType.SwitchSection:
return new FlowBranchingBlock (parent, type, SiblingType.Block, block, loc);
return new FlowBranchingBlock (parent, type, SiblingType.Block, block, loc);
case BranchingType.Loop:
- return new FlowBranchingLoop (parent, block, loc);
+ return new FlowBranchingBreakable (parent, type, SiblingType.Conditional, block, loc);
+
+ case BranchingType.Embedded:
+ return new FlowBranchingContinuable (parent, type, SiblingType.Conditional, block, loc);
default:
return new FlowBranchingBlock (parent, type, SiblingType.Conditional, block, loc);
// </summary>
public readonly Location Location;
- // <summary>
- // If this is an infinite loop.
- // </summary>
- public bool Infinite;
-
//
// Private
//
// <summary>
// Start location of this branching.
// </summary>
- public readonly Location Location;
+ public Location Location;
// <summary>
// This is only valid for SwitchSection, Try, Catch and Finally.
if (num_params > 0)
parameters = new MyBitVector (null, num_params);
- reachability = Reachability.Never ();
+ reachability = Reachability.Always ();
}
id = ++next_id;
return retval;
}
- public bool IsAssigned (VariableInfo var)
+ public bool IsAssigned (VariableInfo var, bool ignoreReachability)
{
- if (!var.IsParameter && Reachability.IsUnreachable)
+ if (!ignoreReachability && !var.IsParameter && Reachability.IsUnreachable)
return true;
return var.IsAssigned (var.IsParameter ? parameters : locals);
}
public Reachability Reachability {
- get {
- return reachability;
- }
+ get { return reachability; }
}
public void Return ()
}
}
- public void Break ()
- {
- if (!reachability.IsUnreachable) {
- IsDirty = true;
- reachability.SetBreaks ();
- }
- }
-
public void Throw ()
{
if (!reachability.IsUnreachable) {
IsDirty = true;
reachability.SetThrows ();
+ reachability.SetBarrier ();
}
}
// <summary>
// Merges a child branching.
// </summary>
- public UsageVector MergeChild (FlowBranching branching)
+ public UsageVector MergeChild (UsageVector child, bool implicit_block)
{
- UsageVector result = branching.Merge ();
-
- Report.Debug (2, " MERGING CHILD", this, branching, IsDirty,
- result.ParameterVector, result.LocalVector,
- result.Reachability, reachability, Type);
-
- Reachability new_r = result.Reachability;
-
- if (branching.Type == BranchingType.Loop) {
- bool may_leave_loop = new_r.MayBreak;
- new_r.ResetBreaks ();
-
- if (branching.Infinite && !may_leave_loop) {
- if (new_r.Returns == FlowReturns.Sometimes) {
- // If we're an infinite loop and do not break,
- // the code after the loop can never be reached.
- // However, if we may return from the loop,
- // then we do always return (or stay in the
- // loop forever).
- new_r.SetReturns ();
- }
-
- new_r.SetBarrier ();
- }
-
- if (may_leave_loop)
- new_r.ResetBarrier ();
- } else if (branching.Type == BranchingType.Switch) {
- if (new_r.MayBreak || new_r.MayReturn)
- new_r.ResetBarrier ();
+ Report.Debug (2, " MERGING CHILD EFFECTS", this, child, IsDirty, reachability, Type);
- new_r.ResetBreaks ();
- }
+ Reachability new_r = child.Reachability;
//
// We've now either reached the point after the branching or we will
Report.Error (163, Location,
"Control cannot fall through from one " +
"case label to another");
- return result;
+ return child;
}
- if (locals != null && result.LocalVector != null)
- locals.Or (result.LocalVector);
+ if (locals != null && child.LocalVector != null)
+ locals.Or (child.LocalVector);
- if (result.ParameterVector != null)
- parameters.Or (result.ParameterVector);
+ if (child.ParameterVector != null)
+ parameters.Or (child.ParameterVector);
- if ((branching.Type == BranchingType.Block) && branching.Block.Implicit)
+ if (implicit_block)
reachability = new_r.Clone ();
else
reachability.Or (new_r);
- Report.Debug (2, " MERGING CHILD DONE", this, result,
- new_r, reachability);
-
IsDirty = true;
- return result;
- }
-
- protected void MergeFinally (FlowBranching branching, UsageVector f_origins,
- MyBitVector f_params)
- {
- for (UsageVector vector = f_origins; vector != null; vector = vector.Next) {
- MyBitVector temp_params = f_params.Clone ();
- temp_params.Or (vector.Parameters);
- }
- }
-
- public void MergeFinally (FlowBranching branching, UsageVector f_vector,
- UsageVector f_origins)
- {
- if (parameters != null) {
- if (f_vector != null) {
- MergeFinally (branching, f_origins, f_vector.Parameters);
- MyBitVector.Or (ref parameters, f_vector.ParameterVector);
- } else
- MergeFinally (branching, f_origins, parameters);
- }
-
- if (f_vector != null && f_vector.LocalVector != null)
- MyBitVector.Or (ref locals, f_vector.LocalVector);
+ return child;
}
// <summary>
{
Report.Debug (1, " MERGING JUMP ORIGINS", this);
- reachability = Reachability.Never ();
-
- if (o_vectors == null) {
- reachability.SetBarrier ();
+ if (o_vectors == null)
return;
- }
-
- bool first = true;
-
- for (UsageVector vector = o_vectors; vector != null;
- vector = vector.Next) {
- Report.Debug (1, " MERGING JUMP ORIGIN", vector,
- first, locals, vector.Locals);
-
- if (first) {
- if (locals != null && vector.Locals != null)
- locals.Or (vector.locals);
-
- if (parameters != null)
- parameters.Or (vector.parameters);
- first = false;
- } else {
- if (locals != null)
- locals.And (vector.locals);
- if (parameters != null)
- parameters.And (vector.parameters);
- }
-
- Reachability.And (ref reachability, vector.Reachability, true);
- Report.Debug (1, " MERGING JUMP ORIGIN #1", vector);
+ UsageVector vector = o_vectors;
+ if (reachability.IsUnreachable) {
+ Report.Debug (1, " MERGING JUMP ORIGIN INTO UNREACHABLE", this, vector);
+ if (locals != null && vector.Locals != null)
+ locals.Or (vector.locals);
+ if (parameters != null)
+ parameters.Or (vector.parameters);
+ reachability.Meet (vector.Reachability);
+ vector = vector.Next;
}
- Report.Debug (1, " MERGING JUMP ORIGINS DONE", this);
- }
-
- // <summary>
- // This is used at the beginning of a finally block if there were
- // any return statements in the try block or one of the catch blocks.
- // </summary>
- public void MergeFinallyOrigins (UsageVector f_origins)
- {
- Report.Debug (1, " MERGING FINALLY ORIGIN", this);
-
- reachability = Reachability.Never ();
-
- for (UsageVector vector = f_origins; vector != null; vector = vector.Next) {
- Report.Debug (1, " MERGING FINALLY ORIGIN", vector);
+ for (; vector != null; vector = vector.Next) {
+ Report.Debug (1, " MERGING JUMP ORIGIN", this, vector);
+ if (locals != null)
+ locals.And (vector.locals);
if (parameters != null)
parameters.And (vector.parameters);
+ reachability.Meet (vector.Reachability);
- Reachability.And (ref reachability, vector.Reachability, true);
+ Report.Debug (1, " MERGING JUMP ORIGIN #1", vector);
}
- Report.Debug (1, " MERGING FINALLY ORIGIN DONE", this);
+ Report.Debug (1, " MERGING JUMP ORIGINS DONE", this);
}
- public void MergeBreakOrigins (FlowBranching branching, UsageVector o_vectors)
+ public void MergeOrigins (UsageVector o_vectors)
{
Report.Debug (1, " MERGING BREAK ORIGINS", this);
if (o_vectors == null)
return;
- bool first = branching.Infinite;
-
- for (UsageVector vector = o_vectors; vector != null;
- vector = vector.Next) {
- Report.Debug (1, " MERGING BREAK ORIGIN", vector, first);
-
- if (first) {
- if (locals != null && vector.Locals != null)
- locals.Or (vector.locals);
-
- if (parameters != null)
- parameters.Or (vector.parameters);
- first = false;
- } else {
- if (locals != null && vector.Locals != null)
- locals.And (vector.locals);
- if (parameters != null)
- parameters.And (vector.parameters);
- }
+ UsageVector vector = o_vectors;
- reachability.And (vector.Reachability, false);
+ if (reachability.IsUnreachable) {
+ Report.Debug (1, " MERGING BREAK ORIGIN INTO UNREACHABLE", vector);
+ locals = vector.Locals;
+ parameters = vector.Parameters;
+ reachability.Meet (vector.Reachability);
+ vector = vector.Next;
}
- Report.Debug (1, " MERGING BREAK ORIGINS DONE", this);
- }
+ for (; vector != null; vector = vector.Next) {
+ Report.Debug (1, " MERGING BREAK ORIGIN", vector);
+ if (locals != null && vector.locals != null)
+ locals.And (vector.locals);
+ if (parameters != null && vector.parameters != null)
+ parameters.And (vector.parameters);
+ reachability.Meet (vector.Reachability);
+ }
- public void CheckOutParameters (FlowBranching branching)
- {
- if (parameters != null)
- branching.CheckOutParameters (parameters, branching.Location);
+ Report.Debug (1, " MERGING BREAK ORIGINS DONE", this);
}
// <summary>
}
public bool HasParameters {
- get {
- return parameters != null;
- }
+ get { return parameters != null; }
}
public bool HasLocals {
- get {
- return locals != null;
- }
+ get { return locals != null; }
}
// <summary>
// Returns a deep copy of the parameters.
// </summary>
public MyBitVector Parameters {
- get {
- if (parameters != null)
- return parameters.Clone ();
- else
- return null;
- }
+ get { return parameters == null ? null : parameters.Clone (); }
}
// <summary>
// Returns a deep copy of the locals.
// </summary>
public MyBitVector Locals {
- get {
- if (locals != null)
- return locals.Clone ();
- else
- return null;
- }
+ get { return locals == null ? null : locals.Clone (); }
}
public MyBitVector ParameterVector {
- get {
- return parameters;
- }
+ get { return parameters; }
}
public MyBitVector LocalVector {
- get {
- return locals;
- }
+ get { return locals; }
}
//
// </summary>
public void CheckOutParameters (MyBitVector parameters, Location loc)
{
+ if (parameters == null)
+ return;
+
for (int i = 0; i < param_map.Count; i++) {
VariableInfo var = param_map [i];
Report.Debug (2, " MERGING SIBLINGS", this, Name);
for (UsageVector child = sibling_list; child != null; child = child.Next) {
- bool do_break = (Type != BranchingType.Switch) &&
- (Type != BranchingType.Loop);
-
- Report.Debug (2, " MERGING SIBLING ", child,
- child.ParameterVector, child.LocalVector,
- reachability, child.Reachability, do_break);
+ Report.Debug (2, " MERGING SIBLING ", reachability, child);
- Reachability.And (ref reachability, child.Reachability, do_break);
+ if (reachability == null)
+ reachability = child.Reachability.Clone ();
+ else
+ reachability.Meet (child.Reachability);
// A local variable is initialized after a flow branching if it
// has been initialized in all its branches which do neither
// Here, `a' is initialized in line 3 and we must not look at
// line 5 since it always returns.
//
- bool do_break_2 = (child.Type != SiblingType.Block) &&
- (child.Type != SiblingType.SwitchSection);
- bool always_throws = (child.Type != SiblingType.Try) &&
- child.Reachability.AlwaysThrows;
- bool unreachable = always_throws ||
- (do_break_2 && child.Reachability.AlwaysBreaks) ||
- child.Reachability.AlwaysReturns ||
- child.Reachability.AlwaysHasBarrier;
+ bool unreachable = child.Reachability.IsUnreachable;
Report.Debug (2, " MERGING SIBLING #1", reachability,
- Type, child.Type, child.Reachability.IsUnreachable,
- do_break_2, always_throws, unreachable);
+ Type, child.Type, child.Reachability.IsUnreachable, unreachable);
if (!unreachable && (child.LocalVector != null))
MyBitVector.And (ref locals, child.LocalVector);
}
if (reachability == null)
- reachability = Reachability.Never ();
+ throw new InternalErrorException ("Cannot happen: the loop above runs at least twice");
- Report.Debug (2, " MERGING SIBLINGS DONE", parameters, locals,
- reachability, Infinite);
+ Report.Debug (2, " MERGING SIBLINGS DONE", parameters, locals, reachability);
- return new UsageVector (
- parameters, locals, reachability, null, Location);
+ return new UsageVector (parameters, locals, reachability, null, Location);
}
protected abstract UsageVector Merge ();
// </summary>
public UsageVector MergeChild (FlowBranching child)
{
- return CurrentUsageVector.MergeChild (child);
+ bool implicit_block = child.Type == BranchingType.Block && child.Block.Implicit;
+ Report.Debug (2, " MERGING CHILD", this, child);
+ UsageVector result = CurrentUsageVector.MergeChild (child.Merge (), implicit_block);
+ Report.Debug (2, " MERGING CHILD DONE", this, result);
+ return result;
}
// <summary>
// </summary>
public Reachability MergeTopBlock ()
{
- if ((Type != BranchingType.Block) || (Block == null))
+ if ((Type != BranchingType.Toplevel) || (Block == null))
throw new NotSupportedException ();
- UsageVector vector = new UsageVector (
- SiblingType.Block, null, Block, Location,
- param_map.Length, local_map.Length);
-
- UsageVector result = vector.MergeChild (this);
+ UsageVector result = Merge ();
- Report.Debug (4, "MERGE TOP BLOCK", Location, vector, result.Reachability);
+ Report.Debug (4, "MERGE TOP BLOCK", Location, result);
- if ((vector.Reachability.Throws != FlowReturns.Always) &&
- (vector.Reachability.Barrier != FlowReturns.Always))
- CheckOutParameters (vector.Parameters, Location);
+ if (!result.Reachability.AlwaysThrows && !result.Reachability.AlwaysHasBarrier)
+ CheckOutParameters (result.Parameters, Location);
return result.Reachability;
}
- //
- // Checks whether we're in a `try' block.
- //
- public virtual bool InTryOrCatch (bool is_return)
+ public virtual bool InTryWithCatch ()
{
- if ((Block != null) && Block.IsDestructor)
- return true;
- else if (!is_return &&
- ((Type == BranchingType.Loop) || (Type == BranchingType.Switch)))
- return false;
- else if (Parent != null)
- return Parent.InTryOrCatch (is_return);
- else
- return false;
+ return Parent != null && Parent.InTryWithCatch ();
}
- public virtual bool InTryWithCatch ()
+ // returns true if we crossed an unwind-protected region (try/catch/finally, lock, using, ...)
+ public virtual bool AddBreakOrigin (UsageVector vector, Location loc)
{
if (Parent != null)
- return Parent.InTryWithCatch ();
- return false;
- }
+ return Parent.AddBreakOrigin (vector, loc);
- public virtual bool InLoop ()
- {
- if (Type == BranchingType.Loop)
- return true;
- else if (Parent != null)
- return Parent.InLoop ();
- else
- return false;
+ Report.Error (139, loc, "No enclosing loop out of which to break or continue");
+ return false;
}
- public virtual bool InSwitch ()
+ // returns true if we crossed an unwind-protected region (try/catch/finally, lock, using, ...)
+ public virtual bool AddContinueOrigin (UsageVector vector, Location loc)
{
- if (Type == BranchingType.Switch)
- return true;
- else if (Parent != null)
- return Parent.InSwitch ();
- else
- return false;
- }
+ if (Parent != null)
+ return Parent.AddContinueOrigin (vector, loc);
- public virtual bool BreakCrossesTryCatchBoundary ()
- {
- if ((Type == BranchingType.Loop) || (Type == BranchingType.Switch))
- return false;
- else if (Parent != null)
- return Parent.BreakCrossesTryCatchBoundary ();
- else
- return false;
+ Report.Error (139, loc, "No enclosing loop out of which to break or continue");
+ return false;
}
- public virtual void AddFinallyVector (UsageVector vector)
+ // returns true if we crossed an unwind-protected region (try/catch/finally, lock, using, ...)
+ public virtual bool AddReturnOrigin (UsageVector vector, Location loc)
{
if (Parent != null)
- Parent.AddFinallyVector (vector);
- else if ((Block == null) || !Block.IsDestructor)
- throw new NotSupportedException ();
- }
+ return Parent.AddReturnOrigin (vector, loc);
- public virtual void AddBreakVector (UsageVector vector)
- {
- if (Parent != null)
- Parent.AddBreakVector (vector);
- else if ((Block == null) || !Block.IsDestructor)
- throw new NotSupportedException ();
+ CheckOutParameters (vector.Parameters, loc);
+ return false;
}
public virtual void StealFinallyClauses (ref ArrayList list)
public bool IsAssigned (VariableInfo vi)
{
- return CurrentUsageVector.IsAssigned (vi);
+ return CurrentUsageVector.IsAssigned (vi, false);
}
public bool IsFieldAssigned (VariableInfo vi, string field_name)
{
- if (CurrentUsageVector.IsAssigned (vi))
- return true;
-
- return CurrentUsageVector.IsFieldAssigned (vi, field_name);
+ return CurrentUsageVector.IsAssigned (vi, false) || CurrentUsageVector.IsFieldAssigned (vi, field_name);
}
public void SetAssigned (VariableInfo vi)
}
public string Name {
- get {
- return String.Format ("{0} ({1}:{2}:{3})",
- GetType (), id, Type, Location);
- }
+ get { return String.Format ("{0} ({1}:{2}:{3})", GetType (), id, Type, Location); }
}
}
}
}
- public class FlowBranchingLoop : FlowBranchingBlock
+ public class FlowBranchingBreakable : FlowBranchingBlock
{
UsageVector break_origins;
- public FlowBranchingLoop (FlowBranching parent, Block block, Location loc)
- : base (parent, BranchingType.Loop, SiblingType.Conditional, block, loc)
+ public FlowBranchingBreakable (FlowBranching parent, BranchingType type, SiblingType stype, Block block, Location loc)
+ : base (parent, type, stype, block, loc)
{ }
- public override void AddBreakVector (UsageVector vector)
+ public override bool AddBreakOrigin (UsageVector vector, Location loc)
{
vector = vector.Clone ();
vector.Next = break_origins;
break_origins = vector;
+ return false;
}
protected override UsageVector Merge ()
{
UsageVector vector = base.Merge ();
-
- vector.MergeBreakOrigins (this, break_origins);
-
+ vector.MergeOrigins (break_origins);
return vector;
}
}
- public class FlowBranchingSwitch : FlowBranchingBlock
+ public class FlowBranchingContinuable : FlowBranchingBlock
{
- UsageVector break_origins;
+ UsageVector continue_origins;
- public FlowBranchingSwitch (FlowBranching parent, Block block, Location loc)
- : base (parent, BranchingType.Switch, SiblingType.SwitchSection, block, loc)
+ public FlowBranchingContinuable (FlowBranching parent, BranchingType type, SiblingType stype, Block block, Location loc)
+ : base (parent, type, stype, block, loc)
{ }
- public override void AddBreakVector (UsageVector vector)
+ public override bool AddContinueOrigin (UsageVector vector, Location loc)
{
vector = vector.Clone ();
- vector.Next = break_origins;
- break_origins = vector;
+ vector.Next = continue_origins;
+ continue_origins = vector;
+ return false;
}
protected override UsageVector Merge ()
{
UsageVector vector = base.Merge ();
+ vector.MergeOrigins (continue_origins);
+ return vector;
+ }
+ }
- vector.MergeBreakOrigins (this, break_origins);
+ public class FlowBranchingLabeled : FlowBranchingBlock
+ {
+ LabeledStatement stmt;
+ public FlowBranchingLabeled (FlowBranching parent, LabeledStatement stmt)
+ : base (parent, BranchingType.Labeled, SiblingType.Conditional, null, stmt.loc)
+ {
+ this.stmt = stmt;
+ }
+ }
- return vector;
+ public class FlowBranchingToplevel : FlowBranchingBlock
+ {
+ public FlowBranchingToplevel (FlowBranching parent, ToplevelBlock stmt)
+ : base (parent, BranchingType.Toplevel, SiblingType.Conditional, stmt, stmt.loc)
+ {
}
}
+
public class FlowBranchingException : FlowBranching
{
ExceptionStatement stmt;
UsageVector current_vector;
UsageVector catch_vectors;
UsageVector finally_vector;
- UsageVector finally_origins;
+
+ UsageVector break_origins;
+ UsageVector continue_origins;
+ UsageVector return_origins;
+
bool emit_finally;
public FlowBranchingException (FlowBranching parent,
protected override void AddSibling (UsageVector sibling)
{
- if (sibling.Type == SiblingType.Try) {
- sibling.Next = catch_vectors;
- catch_vectors = sibling;
- } else if (sibling.Type == SiblingType.Catch) {
+ switch (sibling.Type) {
+ case SiblingType.Try:
+ case SiblingType.Catch:
sibling.Next = catch_vectors;
catch_vectors = sibling;
- } else if (sibling.Type == SiblingType.Finally) {
- sibling.MergeFinallyOrigins (finally_origins);
+ break;
+ case SiblingType.Finally:
finally_vector = sibling;
- } else
+ break;
+ default:
throw new InvalidOperationException ();
-
+ }
current_vector = sibling;
}
get { return current_vector; }
}
- public override bool InTryOrCatch (bool is_return)
- {
- return finally_vector == null;
- }
-
public override bool InTryWithCatch ()
{
if (finally_vector == null) {
return true;
}
- if (Parent != null)
- return Parent.InTryWithCatch ();
+ return base.InTryWithCatch ();
+ }
- return false;
+ public override bool AddBreakOrigin (UsageVector vector, Location loc)
+ {
+ if (finally_vector != null) {
+ Report.Error (157, loc, "Control cannot leave the body of a finally clause");
+ } else {
+ vector = vector.Clone ();
+ vector.Location = loc;
+ vector.Next = break_origins;
+ break_origins = vector;
+ }
+ return true;
}
- public override bool BreakCrossesTryCatchBoundary ()
+ public override bool AddContinueOrigin (UsageVector vector, Location loc)
{
+ if (finally_vector != null) {
+ Report.Error (157, loc, "Control cannot leave the body of a finally clause");
+ } else {
+ vector = vector.Clone ();
+ vector.Location = loc;
+ vector.Next = continue_origins;
+ continue_origins = vector;
+ }
return true;
}
- public override void AddFinallyVector (UsageVector vector)
+ public override bool AddReturnOrigin (UsageVector vector, Location loc)
{
- vector = vector.Clone ();
- vector.Next = finally_origins;
- finally_origins = vector;
+ if (finally_vector != null) {
+ Report.Error (157, loc, "Control cannot leave the body of a finally clause");
+ } else {
+ vector = vector.Clone ();
+ vector.Location = loc;
+ vector.Next = return_origins;
+ return_origins = vector;
+ }
+ return true;
}
public override void StealFinallyClauses (ref ArrayList list)
{
UsageVector vector = Merge (catch_vectors);
- vector.MergeFinally (this, finally_vector, finally_origins);
+ if (finally_vector != null)
+ vector.MergeChild (finally_vector, false);
+
+ for (UsageVector origin = break_origins; origin != null; origin = origin.Next) {
+ if (finally_vector != null)
+ origin.MergeChild (finally_vector, false);
+ if (!origin.Reachability.IsUnreachable)
+ Parent.AddBreakOrigin (origin, origin.Location);
+ }
+
+ for (UsageVector origin = continue_origins; origin != null; origin = origin.Next) {
+ if (finally_vector != null)
+ origin.MergeChild (finally_vector, false);
+ if (!origin.Reachability.IsUnreachable)
+ Parent.AddContinueOrigin (origin, origin.Location);
+ }
+
+ for (UsageVector origin = return_origins; origin != null; origin = origin.Next) {
+ if (finally_vector != null)
+ origin.MergeChild (finally_vector, false);
+ if (!origin.Reachability.IsUnreachable)
+ Parent.AddReturnOrigin (origin, origin.Location);
+ }
return vector;
}
VariableInfo[] map;
- public VariableMap (InternalParameters ip)
+ public VariableMap (Parameters ip)
{
Count = ip != null ? ip.Count : 0;
for (int i = 0; i < Count; i++) {
Parameter.Modifier mod = ip.ParameterModifier (i);
- if ((mod & Parameter.Modifier.OUT) == 0)
+ if ((mod & Parameter.Modifier.OUT) != Parameter.Modifier.OUT)
continue;
-
+
// Dont allocate till we find an out var.
if (map == null)
map = new VariableInfo [Count];
map [i] = new VariableInfo (ip.ParameterName (i),
TypeManager.GetElementType (ip.ParameterType (i)), i, Length);
-
+
Length += map [i].Length;
}
}
// we won't use the inherited vector anymore, but our own copy of it.
// </summary>
public bool IsDirty {
- get {
- return is_dirty;
- }
+ get { return is_dirty; }
set {
if (!is_dirty)
// </summary>
public void Or (MyBitVector new_vector)
{
+ // Treat null 'new_vector' as all false, just like the And() below
+ if (new_vector == null)
+ return;
BitArray new_array = new_vector.Vector;
initialize_vector ();