public sealed class Reachability
{
- FlowReturns returns, breaks, throws, barrier, reachable;
+ FlowReturns returns, breaks, throws, barrier;
public FlowReturns Returns {
get { return returns; }
public FlowReturns Barrier {
get { return barrier; }
}
- public FlowReturns Reachable {
- get { return reachable; }
- }
-
public Reachability (FlowReturns returns, FlowReturns breaks,
FlowReturns throws, FlowReturns barrier)
{
this.breaks = breaks;
this.throws = throws;
this.barrier = barrier;
-
- update ();
}
public Reachability Clone ()
{
- Reachability cloned = new Reachability (returns, breaks, throws, barrier);
- cloned.reachable = reachable;
- return cloned;
+ return new Reachability (returns, breaks, throws, barrier);
}
// <summary>
a.throws = AndFlowReturns (a.throws, b.throws);
a.barrier = AndFlowReturns (a.barrier, b.barrier);
- a.reachable = AndFlowReturns (a.reachable, b.reachable);
+ if (a_unreachable && b_unreachable)
+ a.barrier = FlowReturns.Always;
+ else if (a_unreachable || b_unreachable)
+ a.barrier = FlowReturns.Sometimes;
+ else
+ a.barrier = FlowReturns.Never;
}
public void Or (Reachability b)
breaks = OrFlowReturns (breaks, b.breaks);
throws = OrFlowReturns (throws, b.throws);
barrier = OrFlowReturns (barrier, b.barrier);
-
- update ();
}
public static Reachability Never ()
FlowReturns.Never, FlowReturns.Never);
}
- void update ()
- {
- if ((returns == FlowReturns.Always) || (breaks == FlowReturns.Always) ||
- (throws == FlowReturns.Always) || (barrier == FlowReturns.Always))
- reachable = FlowReturns.Never;
- else if ((returns == FlowReturns.Never) && (breaks == FlowReturns.Never) &&
- (throws == FlowReturns.Never) && (barrier == FlowReturns.Never))
- reachable = FlowReturns.Always;
+ public FlowReturns 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
- reachable = FlowReturns.Sometimes;
+ return FlowReturns.Sometimes;
+ }
}
public bool AlwaysBreaks {
}
public bool IsUnreachable {
- get { return reachable == FlowReturns.Never; }
+ get { return Reachable == FlowReturns.Never; }
}
public void SetReturns ()
{
returns = FlowReturns.Always;
- update ();
}
public void SetReturnsSometimes ()
{
returns = FlowReturns.Sometimes;
- update ();
}
public void SetBreaks ()
{
breaks = FlowReturns.Always;
- update ();
}
public void ResetBreaks ()
{
breaks = FlowReturns.Never;
- update ();
}
public void SetThrows ()
{
throws = FlowReturns.Always;
- update ();
}
public void SetThrowsSometimes ()
{
throws = FlowReturns.Sometimes;
- update ();
}
public void SetBarrier ()
{
barrier = FlowReturns.Always;
- update ();
+ }
+
+ public void ResetBarrier ()
+ {
+ barrier = FlowReturns.Never;
}
static string ShortName (FlowReturns returns)
return String.Format ("[{0}:{1}:{2}:{3}:{4}]",
ShortName (returns), ShortName (breaks),
ShortName (throws), ShortName (barrier),
- ShortName (reachable));
+ ShortName (Reachable));
}
}
{
switch (type) {
case BranchingType.Exception:
- return new FlowBranchingException (parent, block, loc);
+ throw new InvalidOperationException ();
case BranchingType.Switch:
return new FlowBranchingBlock (parent, type, SiblingType.SwitchSection, block, loc);
// </summary>
public readonly Location Location;
+ // <summary>
+ // This is only valid for SwitchSection, Try, Catch and Finally.
+ // </summary>
+ public readonly Block Block;
+
// <summary>
// If this is true, then the usage vector has been modified and must be
// merged when we're done with this branching.
//
// Normally, you should not use any of these constructors.
//
- public UsageVector (SiblingType type, UsageVector parent, Location loc, int num_params, int num_locals)
+ public UsageVector (SiblingType type, UsageVector parent,
+ Block block, Location loc,
+ int num_params, int num_locals)
{
this.Type = type;
+ this.Block = block;
this.Location = loc;
this.InheritsFrom = parent;
this.CountParameters = num_params;
id = ++next_id;
}
- public UsageVector (SiblingType type, UsageVector parent, Location loc)
- : this (type, parent, loc, parent.CountParameters, parent.CountLocals)
+ public UsageVector (SiblingType type, UsageVector parent,
+ Block block, Location loc)
+ : this (type, parent, block, loc,
+ parent.CountParameters, parent.CountLocals)
{ }
public UsageVector (MyBitVector parameters, MyBitVector locals,
- Reachability reachability, Location loc)
+ Reachability reachability, Block block,
+ Location loc)
{
this.Type = SiblingType.Block;
this.Location = loc;
+ this.Block = block;
this.reachability = reachability;
this.parameters = parameters;
// </summary>
public UsageVector Clone ()
{
- UsageVector retval = new UsageVector (Type, null, Location, CountParameters, CountLocals);
+ UsageVector retval = new UsageVector (
+ Type, null, Block, Location,
+ CountParameters, CountLocals);
if (retval.locals != null)
retval.locals = locals.Clone ();
{
UsageVector result = branching.Merge ();
- Report.Debug (2, " MERGING CHILD", this, IsDirty,
+ Report.Debug (2, " MERGING CHILD", this, branching, IsDirty,
result.ParameterVector, result.LocalVector,
result.Reachability, reachability, Type);
new_r.SetThrowsSometimes ();
}
}
- } else if (branching.Type == BranchingType.Switch)
+ } else if (branching.Type == BranchingType.Switch) {
+ if (new_r.MayBreak || new_r.MayReturn)
+ new_r.ResetBarrier ();
+
new_r.ResetBreaks ();
+ }
//
// We've now either reached the point after the branching or we will
reachability = Reachability.Never ();
- if (o_vectors == null)
+ if (o_vectors == null) {
+ reachability.SetBarrier ();
return;
+ }
bool first = true;
local_map = Block.LocalMap;
UsageVector parent_vector = parent != null ? parent.CurrentUsageVector : null;
- vector = new UsageVector (stype, parent_vector, loc, param_map.Length, local_map.Length);
-
-
+ vector = new UsageVector (
+ stype, parent_vector, Block, loc,
+ param_map.Length, local_map.Length);
} else {
param_map = Parent.param_map;
local_map = Parent.local_map;
- vector = new UsageVector (stype, Parent.CurrentUsageVector, loc);
+ vector = new UsageVector (
+ stype, Parent.CurrentUsageVector, null, loc);
}
AddSibling (vector);
// <summary>
// Creates a sibling of the current usage vector.
// </summary>
- public virtual void CreateSibling (SiblingType type)
+ public virtual void CreateSibling (Block block, SiblingType type)
{
- AddSibling (new UsageVector (type, Parent.CurrentUsageVector, Location));
+ UsageVector vector = new UsageVector (
+ type, Parent.CurrentUsageVector, block, Location);
+ AddSibling (vector);
Report.Debug (1, " CREATED SIBLING", CurrentUsageVector);
}
+ public void CreateSibling ()
+ {
+ CreateSibling (null, SiblingType.Conditional);
+ }
+
protected abstract void AddSibling (UsageVector uv);
+ public virtual LabeledStatement LookupLabel (string name, Location loc)
+ {
+ if (Parent != null)
+ return Parent.LookupLabel (name, loc);
+
+ Report.Error (
+ 159, loc,
+ "No such label `" + name + "' in this scope");
+ return null;
+ }
+
public abstract void Label (UsageVector origin_vectors);
// <summary>
Report.Error (177, loc, "The out parameter `" +
var.Name + "' must be " +
- "assigned before control leave the current method.");
+ "assigned before control leaves the current method.");
}
}
Report.Debug (2, " MERGING SIBLINGS DONE", parameters, locals,
reachability, Infinite);
- return new UsageVector (parameters, locals, reachability, Location);
+ return new UsageVector (
+ parameters, locals, reachability, null, Location);
}
protected abstract UsageVector Merge ();
throw new NotSupportedException ();
UsageVector vector = new UsageVector (
- SiblingType.Conditional, null, Location, param_map.Length, local_map.Length);
+ SiblingType.Conditional, null, Block, Location,
+ param_map.Length, local_map.Length);
UsageVector result = vector.MergeChild (this);
Report.Debug (4, "MERGE TOP BLOCK", Location, vector, result.Reachability);
- if (vector.Reachability.Throws != FlowReturns.Always)
+ if ((vector.Reachability.Throws != FlowReturns.Always) &&
+ (vector.Reachability.Barrier != FlowReturns.Always))
CheckOutParameters (vector.Parameters, Location);
return result.Reachability;
throw new NotSupportedException ();
}
+ public virtual void StealFinallyClauses (ref ArrayList list)
+ {
+ if (Parent != null)
+ Parent.StealFinallyClauses (ref list);
+ }
+
public bool IsAssigned (VariableInfo vi)
{
return CurrentUsageVector.IsAssigned (vi);
sibling_list = sibling;
}
+ public override LabeledStatement LookupLabel (string name, Location loc)
+ {
+ if (Block == null)
+ return base.LookupLabel (name, loc);
+
+ LabeledStatement s = Block.LookupLabel (name);
+ if (s != null)
+ return s;
+
+ return base.LookupLabel (name, loc);
+ }
+
public override void Label (UsageVector origin_vectors)
{
if (!CurrentUsageVector.Reachability.IsUnreachable) {
public class FlowBranchingException : FlowBranching
{
+ ExceptionStatement stmt;
UsageVector current_vector;
UsageVector catch_vectors;
UsageVector finally_vector;
UsageVector finally_origins;
+ bool emit_finally;
bool in_try;
- public FlowBranchingException (FlowBranching parent, Block block, Location loc)
- : base (parent, BranchingType.Exception, SiblingType.Try, block, loc)
- { }
+ public FlowBranchingException (FlowBranching parent,
+ ExceptionStatement stmt)
+ : base (parent, BranchingType.Exception, SiblingType.Try,
+ null, stmt.loc)
+ {
+ this.stmt = stmt;
+ this.emit_finally = true;
+ }
protected override void AddSibling (UsageVector sibling)
{
finally_origins = vector;
}
+ public override void StealFinallyClauses (ref ArrayList list)
+ {
+ if (list == null)
+ list = new ArrayList ();
+ list.Add (stmt);
+ emit_finally = false;
+ base.StealFinallyClauses (ref list);
+ }
+
+ public bool EmitFinally {
+ get { return emit_finally; }
+ }
+
+ public override LabeledStatement LookupLabel (string name, Location loc)
+ {
+ if (current_vector.Block == null)
+ return base.LookupLabel (name, loc);
+
+ LabeledStatement s = current_vector.Block.LookupLabel (name);
+ if (s != null)
+ return s;
+
+ if (finally_vector != null) {
+ Report.Error (
+ 157, loc, "Control can not leave the body " +
+ "of the finally block");
+ return null;
+ }
+
+ return base.LookupLabel (name, loc);
+ }
+
public override void Label (UsageVector origin_vectors)
{
CurrentUsageVector.MergeJumpOrigins (origin_vectors);