// <summary>
// The type of a FlowBranching.
// </summary>
- public enum BranchingType {
+ public enum BranchingType : byte {
// Normal (conditional or toplevel) block.
Block,
+ // Conditional.
+ Conditional,
+
// A loop block.
- LoopBlock,
+ Loop,
// Try/Catch block.
Exception,
// <summary>
// The type of one sibling of a branching.
// </summary>
- public enum SiblingType {
+ public enum SiblingType : byte {
+ Block,
Conditional,
SwitchSection,
Try,
// current code block may return to its enclosing block before reaching
// its end.
// </summary>
- public enum FlowReturns {
+ public enum FlowReturns : byte {
Undefined = 0,
// It can never return.
// The code always returns, ie. there's an unconditional return / break
// statement in it.
- Always,
+ Always
+ }
- // The code always throws an exception.
- Exception,
+ public sealed class Reachability
+ {
+ FlowReturns returns, breaks, throws, barrier, reachable;
+
+ public FlowReturns Returns {
+ get { return returns; }
+ }
+ public FlowReturns Breaks {
+ get { return breaks; }
+ }
+ public FlowReturns Throws {
+ get { return throws; }
+ }
+ public FlowReturns Barrier {
+ get { return barrier; }
+ }
+ public FlowReturns Reachable {
+ get { return reachable; }
+ }
+
+ public Reachability (FlowReturns returns, FlowReturns breaks,
+ FlowReturns throws, FlowReturns barrier)
+ {
+ this.returns = returns;
+ 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;
+ }
+
+ // <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)
+ {
+ 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 ();
+ }
+ }
+
+ public static FlowReturns OrFlowReturns (FlowReturns a, FlowReturns 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 ();
+ }
+ }
+
+ public static void And (ref Reachability a, Reachability b, bool do_break)
+ {
+ if (a == null) {
+ a = b.Clone ();
+ return;
+ }
+
+ //
+ // `break' does not "break" in a Switch or a LoopBlock
+ //
+ bool a_breaks = do_break && a.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 = a.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 = !a.AlwaysBreaks && a.AlwaysHasBarrier;
+ b_has_barrier = !b.AlwaysBreaks && b.AlwaysHasBarrier;
+ }
+
+ bool a_unreachable = a_breaks || a.AlwaysThrows || a_has_barrier;
+ bool b_unreachable = b_breaks || b.AlwaysThrows || b_has_barrier;
+
+ //
+ // Do all code paths always return ?
+ //
+ if (a.AlwaysReturns) {
+ if (b.AlwaysReturns || b_unreachable)
+ a.returns = FlowReturns.Always;
+ else
+ a.returns = FlowReturns.Sometimes;
+ } else if (b.AlwaysReturns) {
+ if (a.AlwaysReturns || a_unreachable)
+ a.returns = FlowReturns.Always;
+ else
+ a.returns = FlowReturns.Sometimes;
+ } else if (!a.MayReturn) {
+ if (b.MayReturn)
+ a.returns = FlowReturns.Sometimes;
+ else
+ a.returns = FlowReturns.Never;
+ } else if (!b.MayReturn) {
+ if (a.MayReturn)
+ a.returns = FlowReturns.Sometimes;
+ else
+ a.returns = FlowReturns.Never;
+ }
+
+ a.breaks = AndFlowReturns (a.breaks, b.breaks);
+ a.throws = AndFlowReturns (a.throws, b.throws);
+ a.barrier = AndFlowReturns (a.barrier, b.barrier);
+
+ a.reachable = AndFlowReturns (a.reachable, b.reachable);
+ }
+
+ public void Or (Reachability b)
+ {
+ returns = OrFlowReturns (returns, b.returns);
+ breaks = OrFlowReturns (breaks, b.breaks);
+ throws = OrFlowReturns (throws, b.throws);
+ barrier = OrFlowReturns (barrier, b.barrier);
+
+ update ();
+ }
+
+ public static Reachability Never ()
+ {
+ return new Reachability (
+ FlowReturns.Never, FlowReturns.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;
+ else
+ reachable = FlowReturns.Sometimes;
+ }
+
+ 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; }
+ }
+
+ public bool AlwaysThrows {
+ get { return throws == FlowReturns.Always; }
+ }
+
+ public bool MayThrow {
+ get { return throws != FlowReturns.Never; }
+ }
+
+ public bool AlwaysHasBarrier {
+ get { return barrier == FlowReturns.Always; }
+ }
+
+ public bool MayHaveBarrier {
+ get { return barrier != FlowReturns.Never; }
+ }
- // The current code block is unreachable. This happens if it's immediately
- // following a FlowReturns.Always block.
- Unreachable
+ public bool IsUnreachable {
+ 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 ();
+ }
+
+ static string ShortName (FlowReturns returns)
+ {
+ switch (returns) {
+ case FlowReturns.Never:
+ return "N";
+ case FlowReturns.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),
+ ShortName (reachable));
+ }
}
public static FlowBranching CreateBranching (FlowBranching parent, BranchingType type, Block block, Location loc)
{
switch (type) {
case BranchingType.Exception:
- return new FlowBranchingException (parent, type, block, loc);
+ return new FlowBranchingException (parent, block, loc);
case BranchingType.Switch:
return new FlowBranchingBlock (parent, type, SiblingType.SwitchSection, block, loc);
+ case BranchingType.SwitchSection:
+ return new FlowBranchingBlock (parent, type, SiblingType.Block, block, loc);
+
+ case BranchingType.Block:
+ return new FlowBranchingBlock (parent, type, SiblingType.Block, block, loc);
+
+ case BranchingType.Loop:
+ return new FlowBranchingLoop (parent, block, loc);
+
default:
return new FlowBranchingBlock (parent, type, SiblingType.Conditional, block, loc);
}
// </summary>
public bool Infinite;
- // <summary>
- // If we may leave the current loop.
- // </summary>
- public bool MayLeaveLoop;
-
//
// Private
//
static int next_id = 0;
int id;
- // <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)
- {
- if (a == FlowReturns.Undefined)
- return b;
- if (b == FlowReturns.Unreachable)
- return a;
-
- 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) || (b == FlowReturns.Exception))
- return FlowReturns.Always;
- else
- return FlowReturns.Sometimes;
-
- case FlowReturns.Exception:
- if (b == FlowReturns.Exception)
- return FlowReturns.Exception;
- else if (b == FlowReturns.Always)
- return FlowReturns.Always;
- else
- return FlowReturns.Sometimes;
- }
-
- return b;
- }
-
// <summary>
// The vector contains a BitArray with information about which local variables
// and parameters are already initialized at the current code position.
// </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.
// </summary>
public readonly UsageVector InheritsFrom;
+ // <summary>
+ // This is used to construct a list of UsageVector's.
+ // </summary>
+ public UsageVector Next;
+
//
// Private.
//
MyBitVector locals, parameters;
- FlowReturns RealReturns, RealBreaks, RealReachable;
- bool is_finally;
+ Reachability reachability;
static int next_id = 0;
int id;
//
// 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;
this.CountLocals = num_locals;
- this.RealReturns = FlowReturns.Never;
- this.RealBreaks = FlowReturns.Never;
- this.RealReachable = FlowReturns.Always;
if (parent != null) {
+ if (num_locals > 0)
locals = new MyBitVector (parent.locals, CountLocals);
+
if (num_params > 0)
parameters = new MyBitVector (parent.parameters, num_params);
- RealReturns = parent.Returns;
- RealBreaks = parent.Breaks;
+
+ reachability = parent.Reachability.Clone ();
} else {
+ if (num_locals > 0)
locals = new MyBitVector (null, CountLocals);
+
if (num_params > 0)
parameters = new MyBitVector (null, num_params);
+
+ reachability = Reachability.Never ();
}
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, Block block,
+ Location loc)
+ {
+ this.Type = SiblingType.Block;
+ this.Location = loc;
+ this.Block = block;
+
+ this.reachability = reachability;
+ this.parameters = parameters;
+ this.locals = locals;
+
+ id = ++next_id;
+ }
+
// <summary>
// This does a deep copy of the usage vector.
// </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 ();
+
if (parameters != null)
retval.parameters = parameters.Clone ();
- retval.RealReturns = RealReturns;
- retval.RealBreaks = RealBreaks;
- retval.RealReachable = RealReachable;
+
+ retval.reachability = reachability.Clone ();
return retval;
}
public bool IsAssigned (VariableInfo var)
{
- if (!var.IsParameter && AlwaysBreaks)
+ if (!var.IsParameter && Reachability.IsUnreachable)
return true;
return var.IsAssigned (var.IsParameter ? parameters : locals);
public void SetAssigned (VariableInfo var)
{
- if (!var.IsParameter && AlwaysBreaks)
+ if (!var.IsParameter && Reachability.IsUnreachable)
return;
+ IsDirty = true;
var.SetAssigned (var.IsParameter ? parameters : locals);
}
public bool IsFieldAssigned (VariableInfo var, string name)
{
- if (!var.IsParameter && AlwaysBreaks)
+ if (!var.IsParameter && Reachability.IsUnreachable)
return true;
return var.IsFieldAssigned (var.IsParameter ? parameters : locals, name);
public void SetFieldAssigned (VariableInfo var, string name)
{
- if (!var.IsParameter && AlwaysBreaks)
+ if (!var.IsParameter && Reachability.IsUnreachable)
return;
+ IsDirty = true;
var.SetFieldAssigned (var.IsParameter ? parameters : locals, name);
}
- // <summary>
- // Specifies when the current block returns.
- // If this is FlowReturns.Unreachable, then control can never reach the
- // end of the method (so that we don't need to emit a return statement).
- // The same applies for FlowReturns.Exception, but in this case the return
- // value will never be used.
- // </summary>
- public FlowReturns Returns {
- get {
- return RealReturns;
- }
- }
-
- // <summary>
- // Specifies whether control may return to our containing block
- // before reaching the end of this block. This happens if there
- // is a break/continue/goto/return in it.
- // This can also be used to find out whether the statement immediately
- // following the current block may be reached or not.
- // </summary>
- public FlowReturns Breaks {
- get {
- return RealBreaks;
- }
- }
-
- public FlowReturns Reachable {
+ public Reachability Reachability {
get {
- return RealReachable;
+ return reachability;
}
}
- public bool AlwaysBreaks {
- get {
- return (Breaks == FlowReturns.Always) ||
- (Breaks == FlowReturns.Exception) ||
- (Breaks == FlowReturns.Unreachable);
+ public void Return ()
+ {
+ if (!reachability.IsUnreachable) {
+ IsDirty = true;
+ reachability.SetReturns ();
}
}
- public bool MayBreak {
- get {
- return Breaks != FlowReturns.Never;
+ public void Break ()
+ {
+ if (!reachability.IsUnreachable) {
+ IsDirty = true;
+ reachability.SetBreaks ();
}
}
- public bool AlwaysReturns {
- get {
- return (Returns == FlowReturns.Always) ||
- (Returns == FlowReturns.Exception);
+ public void Throw ()
+ {
+ if (!reachability.IsUnreachable) {
+ IsDirty = true;
+ reachability.SetThrows ();
}
}
- public bool MayReturn {
- get {
- return (Returns == FlowReturns.Sometimes) ||
- (Returns == FlowReturns.Always);
+ public void Goto ()
+ {
+ if (!reachability.IsUnreachable) {
+ IsDirty = true;
+ reachability.SetBarrier ();
}
}
- public void Break ()
+ // <summary>
+ // Merges a child branching.
+ // </summary>
+ public UsageVector MergeChild (FlowBranching branching)
{
- RealBreaks = FlowReturns.Always;
- }
+ UsageVector result = branching.Merge ();
- public void Return ()
- {
- RealReturns = FlowReturns.Always;
- }
+ Report.Debug (2, " MERGING CHILD", this, IsDirty,
+ result.ParameterVector, result.LocalVector,
+ result.Reachability, reachability, Type);
- public bool IsUnreachable {
- get {
- return (Reachable == FlowReturns.Exception) ||
- (Reachable == FlowReturns.Unreachable);
- }
- }
+ Reachability new_r = result.Reachability;
- public void Unreachable ()
- {
- // If we're already unreachable, don't modify the reason why.
- if (!IsUnreachable)
- RealReachable = FlowReturns.Unreachable;
- }
+ if (branching.Type == BranchingType.Loop) {
+ bool may_leave_loop = new_r.MayBreak;
+ new_r.ResetBreaks ();
- public void NeverReachable ()
- {
- // If we're already unreachable, don't modify the reason why.
- if (!IsUnreachable)
- RealReachable = FlowReturns.Never;
+ 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 ();
}
- public void Throw ()
- {
- // If we're already unreachable, don't modify the reason why.
- if (!IsUnreachable)
- RealReachable = FlowReturns.Exception;
+ new_r.SetBarrier ();
+ } else {
+ if (new_r.Returns == FlowReturns.Always) {
+ // We're either finite or we may leave the loop.
+ new_r.SetReturnsSometimes ();
}
-
- // <summary>
- // Merges a child branching.
- // </summary>
- public FlowReturns MergeChild (MyBitVector new_params, MyBitVector new_locals,
- FlowReturns new_returns, FlowReturns new_breaks,
- FlowReturns new_reachable)
- {
- Report.Debug (2, "MERGING CHILD", this, new_params, new_locals, new_returns, new_breaks,
- new_reachable);
-
- RealReturns = new_returns;
- RealBreaks = new_breaks;
- RealReachable = new_reachable;
+ if (new_r.Throws == FlowReturns.Always) {
+ // We're either finite or we may leave the loop.
+ new_r.SetThrowsSometimes ();
+ }
+ }
+ } else if (branching.Type == BranchingType.Switch)
+ new_r.ResetBreaks ();
//
// We've now either reached the point after the branching or we will
// we need to look at (see above).
//
- Report.Debug (2, "MERGING CHILD #1", this, Returns, Breaks, Reachable, new_locals, new_params);
-
- if ((Reachable == FlowReturns.Always) || (Reachable == FlowReturns.Sometimes) ||
- (Reachable == FlowReturns.Never)) {
- if ((Returns == FlowReturns.Always) || (Breaks == FlowReturns.Always))
- RealReachable = FlowReturns.Never;
- if ((Type == SiblingType.SwitchSection) && (Reachable != FlowReturns.Never)) {
- Report.Error (163, Location, "Control cannot fall through from one " +
+ if ((Type == SiblingType.SwitchSection) && !new_r.IsUnreachable) {
+ Report.Error (163, Location,
+ "Control cannot fall through from one " +
"case label to another");
+ return result;
}
- if (new_locals != null)
- locals.Or (new_locals);
+ if (locals != null && result.LocalVector != null)
+ locals.Or (result.LocalVector);
+
+ if (result.ParameterVector != null)
+ parameters.Or (result.ParameterVector);
+
+ reachability.Or (new_r);
- if (new_params != null)
- parameters.Or (new_params);
+ 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);
}
+ }
- Report.Debug (2, "MERGING CHILD DONE", this);
+ 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);
+ }
- return Returns;
+ if (f_vector != null && f_vector.LocalVector != null)
+ MyBitVector.Or (ref locals, f_vector.LocalVector);
}
// <summary>
// 8 Console.WriteLine (a);
//
// </summary>
- public void MergeJumpOrigins (ICollection origin_vectors)
+ public void MergeJumpOrigins (UsageVector o_vectors)
{
- Report.Debug (1, "MERGING JUMP ORIGIN", this);
+ Report.Debug (1, " MERGING JUMP ORIGINS", this);
- RealBreaks = FlowReturns.Never;
- RealReturns = FlowReturns.Never;
- if (Reachable != FlowReturns.Always)
- RealReachable = FlowReturns.Always;
+ reachability = Reachability.Never ();
- if (origin_vectors == null)
+ if (o_vectors == null)
return;
- foreach (UsageVector vector in origin_vectors) {
+ bool first = true;
+
+ for (UsageVector vector = o_vectors; vector != null;
+ vector = vector.Next) {
Report.Debug (1, " MERGING JUMP ORIGIN", vector);
+ 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);
- RealBreaks = AndFlowReturns (RealBreaks, vector.Breaks);
- RealReturns = AndFlowReturns (RealReturns, vector.Returns);
- RealReachable = AndFlowReturns (RealReachable, vector.Reachable);
}
- Report.Debug (1, "MERGING JUMP ORIGIN DONE", this);
+ Reachability.And (ref reachability, vector.Reachability, true);
+ }
+
+ 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 (ICollection finally_vectors)
+ public void MergeFinallyOrigins (UsageVector f_origins)
{
- Report.Debug (1, "MERGING FINALLY ORIGIN", this);
+ Report.Debug (1, " MERGING FINALLY ORIGIN", this);
- RealBreaks = FlowReturns.Never;
+ reachability = Reachability.Never ();
- foreach (UsageVector vector in finally_vectors) {
+ for (UsageVector vector = f_origins; vector != null; vector = vector.Next) {
Report.Debug (1, " MERGING FINALLY ORIGIN", vector);
if (parameters != null)
parameters.And (vector.parameters);
- RealBreaks = AndFlowReturns (Breaks, vector.Breaks);
+
+ Reachability.And (ref reachability, vector.Reachability, true);
}
- is_finally = true;
+ Report.Debug (1, " MERGING FINALLY ORIGIN DONE", this);
+ }
+
+ public void MergeBreakOrigins (UsageVector o_vectors)
+ {
+ Report.Debug (1, " MERGING BREAK ORIGINS", this);
+
+ if (o_vectors == null)
+ return;
+
+ bool first = true;
+
+ for (UsageVector vector = o_vectors; vector != null;
+ vector = vector.Next) {
+ Report.Debug (1, " MERGING BREAK ORIGIN", vector);
+
+ 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);
+ }
+ }
- Report.Debug (1, "MERGING FINALLY ORIGIN DONE", this);
+ Report.Debug (1, " MERGING BREAK ORIGINS DONE", this);
}
public void CheckOutParameters (FlowBranching branching)
// </summary>
public void Or (UsageVector new_vector)
{
+ IsDirty = true;
locals.Or (new_vector.locals);
if (parameters != null)
parameters.Or (new_vector.parameters);
// </summary>
public void AndLocals (UsageVector new_vector)
{
+ IsDirty = true;
locals.And (new_vector.locals);
}
// </summary>
public MyBitVector Locals {
get {
+ if (locals != null)
return locals.Clone ();
+ else
+ return null;
}
}
StringBuilder sb = new StringBuilder ();
sb.Append ("Vector (");
- sb.Append (id);
+ sb.Append (Type);
sb.Append (",");
- sb.Append (Returns);
+ sb.Append (id);
sb.Append (",");
- sb.Append (Breaks);
+ sb.Append (IsDirty);
sb.Append (",");
- sb.Append (Reachable);
+ sb.Append (reachability);
if (parameters != null) {
sb.Append (" - ");
sb.Append (parameters);
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);
+ Report.Debug (1, " CREATED SIBLING", CurrentUsageVector);
+ }
+
+ public void CreateSibling ()
+ {
+ CreateSibling (null, SiblingType.Conditional);
}
protected abstract void AddSibling (UsageVector uv);
- public abstract void Break ();
- public abstract void Return ();
- public abstract void Goto ();
- public abstract void Throw ();
- public abstract void Label (ArrayList origin_vectors);
+ 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>
// Check whether all `out' parameters have been assigned.
continue;
Report.Error (177, loc, "The out parameter `" +
- param_map.VariableNames [i] + "' must be " +
- "assigned before control leave the current method.");
+ var.Name + "' must be " +
+ "assigned before control leaves the current method.");
}
}
- protected class MergeResult
- {
- public MyBitVector Parameters;
- public MyBitVector Locals;
- public FlowReturns Returns;
- public FlowReturns Breaks;
- public FlowReturns Reachable;
- public bool MayLeaveLoop;
-
- public MergeResult (MyBitVector parameters, MyBitVector locals, FlowReturns returns, FlowReturns breaks,
- FlowReturns reachable, bool may_leave_loop)
+ protected UsageVector Merge (UsageVector sibling_list)
{
- this.Parameters = parameters;
- this.Locals = locals;
- this.Returns = returns;
- this.Breaks = breaks;
- this.Reachable = reachable;
- this.MayLeaveLoop = may_leave_loop;
- }
- }
+ if (sibling_list.Next == null)
+ return sibling_list;
- protected MergeResult Merge (ArrayList children)
- {
MyBitVector locals = null;
MyBitVector parameters = null;
- FlowReturns returns = FlowReturns.Undefined;
- FlowReturns breaks = FlowReturns.Undefined;
- FlowReturns reachable = FlowReturns.Undefined;
-
- Report.Debug (2, "MERGING CHILDREN", this, Type, children.Count);
-
- foreach (UsageVector child in children) {
- Report.Debug (2, " MERGING CHILD", child, child.AlwaysBreaks, child.AlwaysReturns,
- child.IsUnreachable, child.Locals, child.Parameters,
- child.Returns, child.Breaks, child.Reachable);
+ Reachability reachability = null;
- reachable = AndFlowReturns (reachable, child.Reachable);
+ Report.Debug (2, " MERGING SIBLINGS", this, Name);
- // Ignore unreachable children.
- if (child.IsUnreachable)
- continue;
+ 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);
- returns = AndFlowReturns (returns, child.Returns);
- breaks = AndFlowReturns (breaks, child.Breaks);
+ Reachability.And (ref reachability, child.Reachability, do_break);
// 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.
//
- if (!child.AlwaysReturns && !child.AlwaysBreaks)
+ 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;
+
+ Report.Debug (2, " MERGING SIBLING #1", reachability,
+ Type, child.Type, child.Reachability.IsUnreachable,
+ do_break_2, always_throws, unreachable);
+
+ if (!unreachable && (child.LocalVector != null))
MyBitVector.And (ref locals, child.LocalVector);
// An `out' parameter must be assigned in all branches which do
// not always throw an exception.
- if ((child.Type != SiblingType.Catch) &&
- (child.ParameterVector != null) && (child.Breaks != FlowReturns.Exception))
+ if ((child.ParameterVector != null) && !child.Reachability.AlwaysThrows)
MyBitVector.And (ref parameters, child.ParameterVector);
- }
-
- Report.Debug (2, "MERGING CHILDREN DONE", Type, parameters, locals, returns, breaks, reachable,
- Infinite, MayLeaveLoop, this);
- if (Infinite && !MayLeaveLoop) {
- Report.Debug (1, "INFINITE", returns, breaks, this);
-
- if (returns == FlowReturns.Never) {
- // We're actually infinite.
- breaks = FlowReturns.Unreachable;
- returns = FlowReturns.Unreachable;
- } else if ((returns == FlowReturns.Sometimes) || (returns == FlowReturns.Always)) {
- // 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).
- returns = FlowReturns.Always;
- }
+ Report.Debug (2, " MERGING SIBLING #2", parameters, locals);
}
- if (returns == FlowReturns.Undefined)
- returns = FlowReturns.Never;
- if (breaks == FlowReturns.Undefined)
- breaks = FlowReturns.Never;
+ if (reachability == null)
+ reachability = Reachability.Never ();
- return new MergeResult (parameters, locals, returns, breaks, reachable, MayLeaveLoop);
+ Report.Debug (2, " MERGING SIBLINGS DONE", parameters, locals,
+ reachability, Infinite);
+
+ return new UsageVector (
+ parameters, locals, reachability, null, Location);
}
- protected abstract MergeResult Merge ();
+ protected abstract UsageVector Merge ();
// <summary>
// Merge a child branching.
// </summary>
- public FlowReturns MergeChild (FlowBranching child)
+ public UsageVector MergeChild (FlowBranching child)
{
- MergeResult result = child.Merge ();
-
- CurrentUsageVector.MergeChild (
- result.Parameters, result.Locals, result.Returns, result.Breaks, result.Reachable);
-
- if ((child.Type != BranchingType.LoopBlock) && (child.Type != BranchingType.SwitchSection))
- MayLeaveLoop |= child.MayLeaveLoop;
-
- if (result.Reachable == FlowReturns.Exception)
- return FlowReturns.Exception;
- else
- return result.Returns;
+ return CurrentUsageVector.MergeChild (child);
}
// <summary>
// Does the toplevel merging.
// </summary>
- public FlowReturns MergeTopBlock ()
+ public Reachability MergeTopBlock ()
{
if ((Type != BranchingType.Block) || (Block == null))
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);
- MergeResult result = Merge ();
- vector.MergeChild (result.Parameters, result.Locals, result.Returns, result.Breaks, result.Reachable);
+ Report.Debug (4, "MERGE TOP BLOCK", Location, vector, result.Reachability);
- if (vector.Reachable != FlowReturns.Exception)
+ if (vector.Reachability.Throws != FlowReturns.Always)
CheckOutParameters (vector.Parameters, Location);
- else
- return FlowReturns.Exception;
- return result.Returns;
+ return result.Reachability;
+ }
+
+ //
+ // Checks whether we're in a `try' block.
+ //
+ public virtual bool InTryOrCatch (bool is_return)
+ {
+ 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;
}
- public virtual bool InTryBlock ()
+ //
+ // Checks whether we're in a `catch' block.
+ //
+ public virtual bool InCatch ()
{
if (Parent != null)
- return Parent.InTryBlock ();
+ return Parent.InCatch ();
+ else
+ return false;
+ }
+
+ //
+ // Checks whether we're in a `finally' block.
+ //
+ public virtual bool InFinally (bool is_return)
+ {
+ if (!is_return &&
+ ((Type == BranchingType.Loop) || (Type == BranchingType.Switch)))
+ return false;
+ else if (Parent != null)
+ return Parent.InFinally (is_return);
+ else
+ return false;
+ }
+
+ public virtual bool InLoop ()
+ {
+ if (Type == BranchingType.Loop)
+ return true;
+ else if (Parent != null)
+ return Parent.InLoop ();
+ else
+ return false;
+ }
+
+ public virtual bool InSwitch ()
+ {
+ if (Type == BranchingType.Switch)
+ return true;
+ else if (Parent != null)
+ return Parent.InSwitch ();
+ else
+ return false;
+ }
+
+ public virtual bool BreakCrossesTryCatchBoundary ()
+ {
+ if ((Type == BranchingType.Loop) || (Type == BranchingType.Switch))
+ return false;
+ else if (Parent != null)
+ return Parent.BreakCrossesTryCatchBoundary ();
else
return false;
}
{
if (Parent != null)
Parent.AddFinallyVector (vector);
- else
+ else if ((Block == null) || !Block.IsDestructor)
+ throw new NotSupportedException ();
+ }
+
+ public virtual void AddBreakVector (UsageVector vector)
+ {
+ if (Parent != null)
+ Parent.AddBreakVector (vector);
+ else if ((Block == null) || !Block.IsDestructor)
throw new NotSupportedException ();
}
sb.Append (")");
return sb.ToString ();
}
+
+ public string Name {
+ get {
+ return String.Format ("{0} ({1}:{2}:{3})",
+ GetType (), id, Type, Location);
+ }
+ }
}
public class FlowBranchingBlock : FlowBranching
{
- UsageVector current_vector;
- ArrayList siblings = new ArrayList ();
+ UsageVector sibling_list = null;
- public FlowBranchingBlock (FlowBranching parent, BranchingType type, SiblingType stype,
- Block block, Location loc)
+ public FlowBranchingBlock (FlowBranching parent, BranchingType type,
+ SiblingType stype, Block block, Location loc)
: base (parent, type, stype, block, loc)
{ }
public override UsageVector CurrentUsageVector {
- get { return current_vector; }
+ get { return sibling_list; }
}
protected override void AddSibling (UsageVector sibling)
{
- siblings.Add (sibling);
- current_vector = sibling;
+ sibling.Next = sibling_list;
+ sibling_list = sibling;
}
- public override void Break ()
+ public override LabeledStatement LookupLabel (string name, Location loc)
{
- if (Type == BranchingType.SwitchSection)
- CurrentUsageVector.NeverReachable ();
- else {
- if (Type == BranchingType.LoopBlock)
- MayLeaveLoop = true;
- CurrentUsageVector.Break ();
- }
+ 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 Return ()
+ public override void Label (UsageVector origin_vectors)
{
- CurrentUsageVector.Return ();
+ if (!CurrentUsageVector.Reachability.IsUnreachable) {
+ UsageVector vector = CurrentUsageVector.Clone ();
+ vector.Next = origin_vectors;
+ origin_vectors = vector;
}
- public override void Goto ()
- {
- CurrentUsageVector.Unreachable ();
+ CurrentUsageVector.MergeJumpOrigins (origin_vectors);
}
- public override void Throw ()
+ protected override UsageVector Merge ()
{
- CurrentUsageVector.Throw ();
+ return Merge (sibling_list);
}
+ }
+
+ public class FlowBranchingLoop : FlowBranchingBlock
+ {
+ UsageVector break_origins;
- public override void Label (ArrayList origin_vectors)
+ public FlowBranchingLoop (FlowBranching parent, Block block, Location loc)
+ : base (parent, BranchingType.Loop, SiblingType.Conditional, block, loc)
+ { }
+
+ public override void AddBreakVector (UsageVector vector)
{
- CurrentUsageVector.MergeJumpOrigins (origin_vectors);
+ vector = vector.Clone ();
+ vector.Next = break_origins;
+ break_origins = vector;
}
- protected override MergeResult Merge ()
+ protected override UsageVector Merge ()
{
- MergeResult result = Merge (siblings);
- if (Type == BranchingType.LoopBlock)
- result.MayLeaveLoop = false;
- return result;
+ UsageVector vector = base.Merge ();
+
+ vector.MergeBreakOrigins (break_origins);
+
+ return vector;
}
}
public class FlowBranchingException : FlowBranching
{
- ArrayList finally_vectors;
-
- bool has_params;
UsageVector current_vector;
- UsageVector try_vector;
- ArrayList catch_vectors = new ArrayList ();
+ UsageVector catch_vectors;
UsageVector finally_vector;
+ UsageVector finally_origins;
+ bool in_try;
- public FlowBranchingException (FlowBranching parent, BranchingType type, Block block, Location loc)
- : base (parent, type, SiblingType.Try, block, loc)
- {
- finally_vectors = new ArrayList ();
- has_params = current_vector.HasParameters;
- }
+ public FlowBranchingException (FlowBranching parent, Block block, Location loc)
+ : base (parent, BranchingType.Exception, SiblingType.Try, block, loc)
+ { }
protected override void AddSibling (UsageVector sibling)
{
if (sibling.Type == SiblingType.Try) {
- try_vector = sibling;
- catch_vectors.Add (sibling);
- } else if (sibling.Type == SiblingType.Catch)
- catch_vectors.Add (sibling);
- else if (sibling.Type == SiblingType.Finally) {
- // sibling.MergeFinallyOrigins (finally_vectors);
+ sibling.Next = catch_vectors;
+ catch_vectors = sibling;
+ in_try = true;
+ } else if (sibling.Type == SiblingType.Catch) {
+ sibling.Next = catch_vectors;
+ catch_vectors = sibling;
+ in_try = false;
+ } else if (sibling.Type == SiblingType.Finally) {
+ sibling.MergeFinallyOrigins (finally_origins);
finally_vector = sibling;
+ in_try = false;
} else
throw new InvalidOperationException ();
get { return current_vector; }
}
- public override bool InTryBlock ()
- {
- return true;
- }
-
- public override void AddFinallyVector (UsageVector vector)
+ public override bool InTryOrCatch (bool is_return)
{
- finally_vectors.Add (vector.Clone ());
+ return finally_vector == null;
}
- public override void Break ()
+ public override bool InCatch ()
{
- CurrentUsageVector.Break ();
+ return !in_try && (finally_vector == null);
}
- public override void Return ()
+ public override bool InFinally (bool is_return)
{
- CurrentUsageVector.Return ();
+ return finally_vector != null;
}
- public override void Goto ()
+ public override bool BreakCrossesTryCatchBoundary ()
{
- CurrentUsageVector.Unreachable ();
+ return true;
}
- public override void Throw ()
+ public override void AddFinallyVector (UsageVector vector)
{
- CurrentUsageVector.Throw ();
+ vector = vector.Clone ();
+ vector.Next = finally_origins;
+ finally_origins = vector;
}
- public override void Label (ArrayList origin_vectors)
+ public override LabeledStatement LookupLabel (string name, Location loc)
{
- CurrentUsageVector.MergeJumpOrigins (origin_vectors);
- }
+ if (current_vector.Block == null)
+ return base.LookupLabel (name, loc);
- protected void MergeFinally (MyBitVector f_params, ref MergeResult result)
- {
- foreach (UsageVector vector in finally_vectors) {
- MyBitVector temp_params = f_params.Clone ();
- temp_params.Or (vector.Parameters);
+ LabeledStatement s = current_vector.Block.LookupLabel (name);
+ if (s != null)
+ return s;
- CheckOutParameters (temp_params, Location);
+ 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);
}
- protected override MergeResult Merge ()
+ public override void Label (UsageVector origin_vectors)
{
- MergeResult result = Merge (catch_vectors);
+ CurrentUsageVector.MergeJumpOrigins (origin_vectors);
+ }
- if (has_params) {
- if (finally_vector != null) {
- MergeFinally (finally_vector.Parameters, ref result);
- MyBitVector.Or (ref result.Parameters, finally_vector.ParameterVector);
- } else
- MergeFinally (result.Parameters, ref result);
- }
+ protected override UsageVector Merge ()
+ {
+ UsageVector vector = Merge (catch_vectors);
- if (finally_vector != null)
- MyBitVector.Or (ref result.Locals, finally_vector.LocalVector);
+ vector.MergeFinally (this, finally_vector, finally_origins);
- return result;
+ return vector;
}
}
Fields = new FieldInfo [Count];
public_fields.CopyTo (Fields, 0);
non_public_fields.CopyTo (Fields, CountPublic);
+ } else if (type is GenericTypeParameterBuilder) {
+ CountPublic = CountNonPublic = Count = 0;
+
+ Fields = new FieldInfo [0];
} else {
FieldInfo[] public_fields = type.GetFields (
BindingFlags.Instance|BindingFlags.Public);
// <summary>
public readonly int Length;
- // <summary>
- // Type and name of all the variables.
- // Note that this is null for variables for which we do not need to compute
- // assignment info.
- // </summary>
- public readonly Type[] VariableTypes;
- public readonly string[] VariableNames;
-
VariableInfo[] map;
public VariableMap (InternalParameters ip)
{
Count = ip != null ? ip.Count : 0;
- map = new VariableInfo [Count];
- VariableNames = new string [Count];
- VariableTypes = new Type [Count];
+
+ // Dont bother allocating anything!
+ if (Count == 0)
+ return;
+
Length = 0;
for (int i = 0; i < Count; i++) {
if ((mod & Parameter.Modifier.OUT) == 0)
continue;
- VariableNames [i] = ip.ParameterName (i);
- VariableTypes [i] = TypeManager.GetElementType (ip.ParameterType (i));
+ // 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);
- map [i] = new VariableInfo (VariableNames [i], VariableTypes [i], i, Length);
Length += map [i].Length;
}
}
public VariableMap (VariableMap parent, LocalInfo[] locals)
{
int offset = 0, start = 0;
- if (parent != null) {
+ if (parent != null && parent.map != null) {
offset = parent.Length;
start = parent.Count;
}
Count = locals.Length + start;
+
+ if (Count == 0)
+ return;
+
map = new VariableInfo [Count];
- VariableNames = new string [Count];
- VariableTypes = new Type [Count];
Length = offset;
- if (parent != null) {
+ if (parent != null && parent.map != null) {
parent.map.CopyTo (map, 0);
- parent.VariableNames.CopyTo (VariableNames, 0);
- parent.VariableTypes.CopyTo (VariableTypes, 0);
}
for (int i = start; i < Count; i++) {
if (li.VariableType == null)
continue;
- VariableNames [i] = li.Name;
- VariableTypes [i] = li.VariableType;
-
map [i] = li.VariableInfo = new VariableInfo (li, Length);
Length += map [i].Length;
}
// </summary>
public VariableInfo this [int index] {
get {
+ if (map == null)
+ return null;
+
return map [index];
}
}
public override string ToString ()
{
- StringBuilder sb = new StringBuilder ("MyBitVector (");
+ StringBuilder sb = new StringBuilder ("{");
BitArray vector = Vector;
- sb.Append (Count);
- sb.Append (",");
if (!IsDirty)
- sb.Append ("INHERITED - ");
+ sb.Append ("=");
for (int i = 0; i < vector.Count; i++) {
- if (i > 0)
- sb.Append (",");
- sb.Append (vector [i]);
+ sb.Append (vector [i] ? "1" : "0");
}
- sb.Append (")");
+ sb.Append ("}");
return sb.ToString ();
}
}