// 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.
// </summary>
- public class FlowBranching
+ public abstract class FlowBranching
{
// <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;
+
+ 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 Reachability (FlowReturns returns, FlowReturns breaks,
+ FlowReturns throws, FlowReturns barrier)
+ {
+ this.returns = returns;
+ this.breaks = breaks;
+ this.throws = throws;
+ this.barrier = barrier;
+ }
+
+ public Reachability Clone ()
+ {
+ return new Reachability (returns, breaks, 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)
+ {
+ 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;
+ }
+
+ a.And (b, do_break);
+ }
+
+ public void And (Reachability b, bool do_break)
+ {
+ //
+ // `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;
+ }
- // The current code block is unreachable. This happens if it's immediately
- // following a FlowReturns.Always block.
- Unreachable
+ 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);
+ }
+
+ public static Reachability Never ()
+ {
+ return new Reachability (
+ FlowReturns.Never, FlowReturns.Never,
+ FlowReturns.Never, FlowReturns.Never);
+ }
+
+ 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
+ return 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; }
+ }
+
+ public bool IsUnreachable {
+ get { return Reachable == FlowReturns.Never; }
+ }
+
+ public void SetReturns ()
+ {
+ returns = FlowReturns.Always;
+ }
+
+ public void SetReturnsSometimes ()
+ {
+ returns = FlowReturns.Sometimes;
+ }
+
+ public void SetBreaks ()
+ {
+ breaks = FlowReturns.Always;
+ }
+
+ public void ResetBreaks ()
+ {
+ breaks = FlowReturns.Never;
+ }
+
+ public void SetThrows ()
+ {
+ throws = FlowReturns.Always;
+ }
+
+ public void SetThrowsSometimes ()
+ {
+ throws = FlowReturns.Sometimes;
+ }
+
+ public void SetBarrier ()
+ {
+ barrier = FlowReturns.Always;
+ }
+
+ public void ResetBarrier ()
+ {
+ barrier = FlowReturns.Never;
+ }
+
+ 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:
+ throw new InvalidOperationException ();
+
+ case BranchingType.Switch:
+ return new FlowBranchingSwitch (parent, 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>
// </summary>
public readonly Location Location;
- // <summary>
- // A list of UsageVectors. A new vector is added each time control flow may
- // take a different path.
- // </summary>
- public UsageVector[] Siblings;
-
// <summary>
// If this is an infinite loop.
// </summary>
public bool Infinite;
- // <summary>
- // If we may leave the current loop.
- // </summary>
- public bool MayLeaveLoop;
-
//
// Private
//
VariableMap param_map, local_map;
- ArrayList finally_vectors;
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 (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 class UsageVector {
+ // <summary>
+ // The type of this branching.
+ // </summary>
+ public readonly SiblingType Type;
+
+ // <summary>
+ // Start location of this branching.
+ // </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 real_returns, real_breaks;
- bool is_finally;
+ Reachability reachability;
static int next_id = 0;
int id;
//
// Normally, you should not use any of these constructors.
//
- public UsageVector (UsageVector parent, 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.real_returns = FlowReturns.Never;
- this.real_breaks = FlowReturns.Never;
if (parent != null) {
- locals = new MyBitVector (parent.locals, CountLocals);
+ if (num_locals > 0)
+ locals = new MyBitVector (parent.locals, CountLocals);
+
if (num_params > 0)
parameters = new MyBitVector (parent.parameters, num_params);
- real_returns = parent.Returns;
- real_breaks = parent.Breaks;
+
+ reachability = parent.Reachability.Clone ();
} else {
- locals = new MyBitVector (null, CountLocals);
+ 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 (UsageVector parent)
- : this (parent, 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 (null, CountParameters, CountLocals);
+ UsageVector retval = new UsageVector (
+ Type, null, Block, Location,
+ CountParameters, CountLocals);
- retval.locals = locals.Clone ();
+ if (retval.locals != null)
+ retval.locals = locals.Clone ();
+
if (parameters != null)
retval.parameters = parameters.Clone ();
- retval.real_returns = real_returns;
- retval.real_breaks = real_breaks;
+
+ 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 real_returns;
- }
-
- set {
- real_returns = value;
- }
- }
-
- // <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 {
+ public Reachability Reachability {
get {
- return real_breaks;
- }
-
- set {
- real_breaks = value;
+ 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 ();
}
}
// <summary>
- // Merge a child branching.
+ // Merges a child branching.
// </summary>
- public FlowReturns MergeChildren (FlowBranching branching, UsageVector[] children)
+ public UsageVector MergeChild (FlowBranching branching)
{
- MyBitVector new_locals = null;
- MyBitVector new_params = null;
-
- FlowReturns new_returns = FlowReturns.Never;
- FlowReturns new_breaks = FlowReturns.Never;
- bool new_returns_set = false, new_breaks_set = false;
-
- Report.Debug (2, "MERGING CHILDREN", branching, branching.Type,
- this, children.Length);
-
- foreach (UsageVector child in children) {
- Report.Debug (2, " MERGING CHILD", child, child.is_finally);
-
- if (!child.is_finally) {
- if (child.Breaks != FlowReturns.Unreachable) {
- // If Returns is already set, perform an
- // `And' operation on it, otherwise just set just.
- if (!new_returns_set) {
- new_returns = child.Returns;
- new_returns_set = true;
- } else
- new_returns = AndFlowReturns (
- new_returns, child.Returns);
+ 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 ();
}
- // If Breaks is already set, perform an
- // `And' operation on it, otherwise just set just.
- if (!new_breaks_set) {
- new_breaks = child.Breaks;
- new_breaks_set = true;
- } else
- new_breaks = AndFlowReturns (
- new_breaks, child.Breaks);
+ new_r.SetBarrier ();
}
- // Ignore unreachable children.
- if (child.Returns == FlowReturns.Unreachable)
- continue;
-
- // A local variable is initialized after a flow branching if it
- // has been initialized in all its branches which do neither
- // always return or always throw an exception.
- //
- // If a branch may return, but does not always return, then we
- // can treat it like a never-returning branch here: control will
- // only reach the code position after the branching if we did not
- // return here.
- //
- // It's important to distinguish between always and sometimes
- // returning branches here:
- //
- // 1 int a;
- // 2 if (something) {
- // 3 return;
- // 4 a = 5;
- // 5 }
- // 6 Console.WriteLine (a);
- //
- // The if block in lines 3-4 always returns, so we must not look
- // at the initialization of `a' in line 4 - thus it'll still be
- // uninitialized in line 6.
- //
- // On the other hand, the following is allowed:
- //
- // 1 int a;
- // 2 if (something)
- // 3 a = 5;
- // 4 else
- // 5 return;
- // 6 Console.WriteLine (a);
- //
- // Here, `a' is initialized in line 3 and we must not look at
- // line 5 since it always returns.
- //
- if (child.is_finally) {
- if (new_locals == null)
- new_locals = locals.Clone ();
- new_locals.Or (child.locals);
-
- if (parameters != null) {
- if (new_params == null)
- new_params = parameters.Clone ();
- new_params.Or (child.parameters);
- }
- } else {
- if (!child.AlwaysReturns && !child.AlwaysBreaks) {
- if (new_locals != null)
- new_locals.And (child.locals);
- else {
- new_locals = locals.Clone ();
- new_locals.Or (child.locals);
- }
- } else if (children.Length == 1) {
- new_locals = locals.Clone ();
- new_locals.Or (child.locals);
- }
-
- // An `out' parameter must be assigned in all branches which do
- // not always throw an exception.
- if (parameters != null) {
- bool and_params = child.Breaks != FlowReturns.Exception;
- if (branching.Type == BranchingType.Exception)
- and_params &= child.Returns != FlowReturns.Never;
- if (and_params) {
- if (new_params != null)
- new_params.And (child.parameters);
- else {
- new_params = parameters.Clone ();
- new_params.Or (child.parameters);
- }
- } else if ((children.Length == 1) || (new_params == null)) {
- new_params = parameters.Clone ();
- new_params.Or (child.parameters);
- }
- }
- }
- }
+ if (may_leave_loop)
+ new_r.ResetBarrier ();
+ } else if (branching.Type == BranchingType.Switch) {
+ if (new_r.MayBreak || new_r.MayReturn)
+ new_r.ResetBarrier ();
- Returns = new_returns;
- if ((branching.Type == BranchingType.Block) ||
- (branching.Type == BranchingType.Exception) ||
- (new_breaks == FlowReturns.Unreachable) ||
- (new_breaks == FlowReturns.Exception))
- Breaks = new_breaks;
- else if (branching.Type == BranchingType.SwitchSection)
- Breaks = new_returns;
- else if (branching.Type == BranchingType.Switch){
- if (new_breaks == FlowReturns.Always)
- Breaks = FlowReturns.Always;
+ new_r.ResetBreaks ();
}
//
// we need to look at (see above).
//
- if (((new_breaks != FlowReturns.Always) &&
- (new_breaks != FlowReturns.Exception) &&
- (new_breaks != FlowReturns.Unreachable)) ||
- (children.Length == 1)) {
- if (new_locals != null)
- locals.Or (new_locals);
-
- if (new_params != null)
- parameters.Or (new_params);
+ if ((Type == SiblingType.SwitchSection) && !new_r.IsUnreachable) {
+ Report.Error (163, Location,
+ "Control cannot fall through from one " +
+ "case label to another");
+ return result;
}
- Report.Debug (2, "MERGING CHILDREN DONE", branching.Type,
- new_params, new_locals, new_returns, new_breaks,
- branching.Infinite, branching.MayLeaveLoop, this);
-
- if (branching.Type == BranchingType.SwitchSection) {
- if ((new_breaks != FlowReturns.Always) &&
- (new_breaks != FlowReturns.Exception) &&
- (new_breaks != FlowReturns.Unreachable))
- Report.Error (163, branching.Location,
- "Control cannot fall through from one " +
- "case label to another");
- }
+ if (locals != null && result.LocalVector != null)
+ locals.Or (result.LocalVector);
- if (branching.Infinite && !branching.MayLeaveLoop) {
- Report.Debug (1, "INFINITE", new_returns, new_breaks,
- Returns, Breaks, this);
+ if (result.ParameterVector != null)
+ parameters.Or (result.ParameterVector);
- // We're actually infinite.
- if (new_returns == FlowReturns.Never) {
- Breaks = FlowReturns.Unreachable;
- return FlowReturns.Unreachable;
- }
+ if ((branching.Type == BranchingType.Block) && branching.Block.Implicit)
+ reachability = new_r.Clone ();
+ else
+ reachability.Or (new_r);
- // 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).
- if ((new_returns == FlowReturns.Sometimes) ||
- (new_returns == FlowReturns.Always)) {
- Returns = FlowReturns.Always;
- return FlowReturns.Always;
- }
- }
+ Report.Debug (2, " MERGING CHILD DONE", this, result,
+ new_r, reachability);
- if (branching.Type == BranchingType.LoopBlock) {
- Report.Debug (2, "MERGING LOOP BLOCK DONE", branching,
- branching.Infinite, branching.MayLeaveLoop,
- new_breaks, new_returns);
+ IsDirty = true;
- // If we may leave the loop, then we do not always return.
- if (branching.MayLeaveLoop && (new_returns == FlowReturns.Always)) {
- Returns = FlowReturns.Sometimes;
- return FlowReturns.Sometimes;
- }
+ return result;
+ }
- // A `break' in a loop does not "break" in the outer block.
- Breaks = FlowReturns.Never;
+ 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);
}
+ }
- return new_returns;
+ 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);
}
// <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);
- real_breaks = FlowReturns.Never;
- real_returns = FlowReturns.Never;
+ reachability = Reachability.Never ();
- foreach (UsageVector vector in origin_vectors) {
- Report.Debug (1, " MERGING JUMP ORIGIN", vector);
+ if (o_vectors == null) {
+ reachability.SetBarrier ();
+ return;
+ }
- locals.And (vector.locals);
- if (parameters != null)
- parameters.And (vector.parameters);
- Breaks = AndFlowReturns (Breaks, vector.Breaks);
- Returns = AndFlowReturns (Returns, vector.Returns);
+ 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);
}
- Report.Debug (1, "MERGING JUMP ORIGIN DONE", this);
+ 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);
- real_breaks = FlowReturns.Never;
+ reachability = Reachability.Never ();
- foreach (UsageVector vector in finally_vectors) {
- Report.Debug (1, " MERGING FINALLY ORIGIN", vector);
+ for (UsageVector vector = f_origins; vector != null; vector = vector.Next) {
+ Report.Debug (1, " MERGING FINALLY ORIGIN", vector);
if (parameters != null)
parameters.And (vector.parameters);
- Breaks = 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 (FlowBranching branching, 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);
+ }
- Report.Debug (1, "MERGING FINALLY ORIGIN DONE", this);
+ reachability.And (vector.Reachability, false);
+ }
+
+ 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);
}
+ public bool HasParameters {
+ get {
+ return parameters != null;
+ }
+ }
+
+ public bool HasLocals {
+ get {
+ return locals != null;
+ }
+ }
+
// <summary>
// Returns a deep copy of the parameters.
// </summary>
// </summary>
public MyBitVector Locals {
get {
- return locals.Clone ();
+ if (locals != null)
+ return locals.Clone ();
+ else
+ return null;
+ }
+ }
+
+ public MyBitVector ParameterVector {
+ get {
+ return parameters;
+ }
+ }
+
+ public MyBitVector LocalVector {
+ get {
+ return locals;
}
}
StringBuilder sb = new StringBuilder ();
sb.Append ("Vector (");
+ sb.Append (Type);
+ sb.Append (",");
sb.Append (id);
sb.Append (",");
- sb.Append (Returns);
+ sb.Append (IsDirty);
sb.Append (",");
- sb.Append (Breaks);
+ sb.Append (reachability);
if (parameters != null) {
sb.Append (" - ");
sb.Append (parameters);
}
}
- FlowBranching (BranchingType type, Location loc)
- {
- this.Block = null;
- this.Location = loc;
- this.Type = type;
- id = ++next_id;
- }
-
- // <summary>
- // Creates a new flow branching for `block'.
- // This is used from Block.Resolve to create the top-level branching of
- // the block.
- // </summary>
- public FlowBranching (Block block, Location loc)
- : this (BranchingType.Block, loc)
- {
- Block = block;
- Parent = null;
-
- param_map = block.ParameterMap;
- local_map = block.LocalMap;
-
- UsageVector vector = new UsageVector (null, param_map.Length, local_map.Length);
-
- AddSibling (vector);
- }
-
// <summary>
// Creates a new flow branching which is contained in `parent'.
// You should only pass non-null for the `block' argument if this block
// introduces any new variables - in this case, we need to create a new
// usage vector with a different size than our parent's one.
// </summary>
- public FlowBranching (FlowBranching parent, BranchingType type,
- Block block, Location loc)
- : this (type, loc)
+ protected FlowBranching (FlowBranching parent, BranchingType type, SiblingType stype,
+ Block block, Location loc)
{
Parent = parent;
Block = block;
+ Location = loc;
+ Type = type;
+ id = ++next_id;
UsageVector vector;
if (Block != null) {
param_map = Block.ParameterMap;
local_map = Block.LocalMap;
- vector = new UsageVector (parent.CurrentUsageVector, param_map.Length,
- local_map.Length);
+ UsageVector parent_vector = parent != null ? parent.CurrentUsageVector : null;
+ 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 (Parent.CurrentUsageVector);
+ vector = new UsageVector (
+ stype, Parent.CurrentUsageVector, null, loc);
}
AddSibling (vector);
-
- switch (Type) {
- case BranchingType.Exception:
- finally_vectors = new ArrayList ();
- break;
-
- default:
- break;
- }
}
- void AddSibling (UsageVector uv)
- {
- if (Siblings != null) {
- UsageVector[] ns = new UsageVector [Siblings.Length + 1];
- for (int i = 0; i < Siblings.Length; ++i)
- ns [i] = Siblings [i];
- Siblings = ns;
- } else {
- Siblings = new UsageVector [1];
- }
- Siblings [Siblings.Length - 1] = uv;
- }
+ public abstract UsageVector CurrentUsageVector {
+ get;
+ }
// <summary>
- // Returns the branching's current usage vector.
+ // Creates a sibling of the current usage vector.
// </summary>
- public UsageVector CurrentUsageVector
+ public virtual void CreateSibling (Block block, SiblingType type)
{
- get {
- return Siblings [Siblings.Length - 1];
- }
+ UsageVector vector = new UsageVector (
+ type, Parent.CurrentUsageVector, block, Location);
+ AddSibling (vector);
+
+ Report.Debug (1, " CREATED SIBLING", CurrentUsageVector);
}
- // <summary>
- // Creates a sibling of the current usage vector.
- // </summary>
- public void CreateSibling (SiblingType type)
+ public void CreateSibling ()
{
- AddSibling (new UsageVector (Parent.CurrentUsageVector));
+ CreateSibling (null, SiblingType.Conditional);
+ }
+
+ protected abstract void AddSibling (UsageVector uv);
- Report.Debug (1, "CREATED SIBLING", CurrentUsageVector);
+ public virtual LabeledStatement LookupLabel (string name, Location loc)
+ {
+ if (Parent != null)
+ return Parent.LookupLabel (name, loc);
- if (type == SiblingType.Finally)
- CurrentUsageVector.MergeFinallyOrigins (finally_vectors);
+ 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.
// </summary>
public void CheckOutParameters (MyBitVector parameters, Location loc)
{
- if (InTryBlock ())
- return;
-
for (int i = 0; i < param_map.Count; i++) {
VariableInfo var = param_map [i];
if (var.IsAssigned (parameters))
continue;
- Report.Error (177, loc, "The out parameter `" +
- param_map.VariableNames [i] + "' must be " +
- "assigned before control leave the current method.");
+ Report.Error (177, loc, "The out parameter `{0}' must be assigned to before control leaves the current method",
+ var.Name);
+ }
+ }
+
+ protected UsageVector Merge (UsageVector sibling_list)
+ {
+ if (sibling_list.Next == null)
+ return sibling_list;
+
+ MyBitVector locals = null;
+ MyBitVector parameters = null;
+
+ Reachability reachability = null;
+
+ 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);
+
+ 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
+ // always return or always throw an exception.
+ //
+ // If a branch may return, but does not always return, then we
+ // can treat it like a never-returning branch here: control will
+ // only reach the code position after the branching if we did not
+ // return here.
+ //
+ // It's important to distinguish between always and sometimes
+ // returning branches here:
+ //
+ // 1 int a;
+ // 2 if (something) {
+ // 3 return;
+ // 4 a = 5;
+ // 5 }
+ // 6 Console.WriteLine (a);
+ //
+ // The if block in lines 3-4 always returns, so we must not look
+ // at the initialization of `a' in line 4 - thus it'll still be
+ // uninitialized in line 6.
+ //
+ // On the other hand, the following is allowed:
+ //
+ // 1 int a;
+ // 2 if (something)
+ // 3 a = 5;
+ // 4 else
+ // 5 return;
+ // 6 Console.WriteLine (a);
+ //
+ // 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;
+
+ 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.ParameterVector != null) && !child.Reachability.AlwaysThrows)
+ MyBitVector.And (ref parameters, child.ParameterVector);
+
+ Report.Debug (2, " MERGING SIBLING #2", parameters, locals);
}
+
+ if (reachability == null)
+ reachability = Reachability.Never ();
+
+ Report.Debug (2, " MERGING SIBLINGS DONE", parameters, locals,
+ reachability, Infinite);
+
+ return new UsageVector (
+ parameters, locals, reachability, null, Location);
}
+ protected abstract UsageVector Merge ();
+
// <summary>
// Merge a child branching.
// </summary>
- public FlowReturns MergeChild (FlowBranching child)
+ public UsageVector MergeChild (FlowBranching child)
{
- FlowReturns returns = CurrentUsageVector.MergeChildren (child, child.Siblings);
-
- if ((child.Type != BranchingType.LoopBlock) &&
- (child.Type != BranchingType.SwitchSection))
- MayLeaveLoop |= child.MayLeaveLoop;
-
- return 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 (null, param_map.Length, local_map.Length);
+ UsageVector vector = new UsageVector (
+ SiblingType.Block, null, Block, Location,
+ param_map.Length, local_map.Length);
- Report.Debug (1, "MERGING TOP BLOCK", Location, vector);
+ UsageVector result = vector.MergeChild (this);
- vector.MergeChildren (this, Siblings);
+ Report.Debug (4, "MERGE TOP BLOCK", Location, vector, result.Reachability);
- if (Siblings.Length == 1)
- Siblings [0] = vector;
- else {
- Siblings = null;
- AddSibling (vector);
- }
+ if ((vector.Reachability.Throws != FlowReturns.Always) &&
+ (vector.Reachability.Barrier != FlowReturns.Always))
+ CheckOutParameters (vector.Parameters, Location);
- Report.Debug (1, "MERGING TOP BLOCK DONE", Location, vector);
+ return result.Reachability;
+ }
- if (vector.Breaks != FlowReturns.Exception) {
- if (!vector.AlwaysBreaks)
- CheckOutParameters (CurrentUsageVector.Parameters, Location);
- return vector.AlwaysBreaks ? FlowReturns.Always : vector.Returns;
- } else
- return FlowReturns.Exception;
+ //
+ // 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 bool InTryBlock ()
+ public virtual bool InTryWithCatch ()
{
- if (finally_vectors != null)
+ if (Parent != null)
+ return Parent.InTryWithCatch ();
+ return false;
+ }
+
+ public virtual bool InLoop ()
+ {
+ if (Type == BranchingType.Loop)
return true;
else if (Parent != null)
- return Parent.InTryBlock ();
+ return Parent.InLoop ();
else
return false;
}
- public void AddFinallyVector (UsageVector vector)
+ public virtual bool InSwitch ()
{
- if (finally_vectors != null) {
- finally_vectors.Add (vector.Clone ());
- return;
- }
+ 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;
+ }
+ public virtual void AddFinallyVector (UsageVector vector)
+ {
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 ();
}
+ public virtual void StealFinallyClauses (ref ArrayList list)
+ {
+ if (Parent != null)
+ Parent.StealFinallyClauses (ref list);
+ }
+
public bool IsAssigned (VariableInfo vi)
{
return CurrentUsageVector.IsAssigned (vi);
CurrentUsageVector.SetFieldAssigned (vi, name);
}
- public bool IsReachable ()
- {
- bool reachable;
-
- switch (Type) {
- case BranchingType.SwitchSection:
- // The code following a switch block is reachable unless the switch
- // block always returns.
- reachable = !CurrentUsageVector.AlwaysReturns;
- break;
-
- case BranchingType.LoopBlock:
- // The code following a loop is reachable unless the loop always
- // returns or it's an infinite loop without any `break's in it.
- reachable = !CurrentUsageVector.AlwaysReturns &&
- (CurrentUsageVector.Breaks != FlowReturns.Unreachable);
- break;
-
- default:
- // The code following a block or exception is reachable unless the
- // block either always returns or always breaks.
- if (MayLeaveLoop)
- reachable = true;
- else
- reachable = !CurrentUsageVector.AlwaysBreaks &&
- !CurrentUsageVector.AlwaysReturns;
- break;
- }
-
- Report.Debug (1, "REACHABLE", this, Type, CurrentUsageVector.Returns,
- CurrentUsageVector.Breaks, CurrentUsageVector, MayLeaveLoop,
- reachable);
-
- return reachable;
- }
-
public override string ToString ()
{
- StringBuilder sb = new StringBuilder ("FlowBranching (");
+ StringBuilder sb = new StringBuilder ();
+ sb.Append (GetType ());
+ sb.Append (" (");
sb.Append (id);
sb.Append (",");
sb.Append (Block.StartLocation);
}
sb.Append (" - ");
- sb.Append (Siblings.Length);
- sb.Append (" - ");
+ // sb.Append (Siblings.Length);
+ // sb.Append (" - ");
sb.Append (CurrentUsageVector);
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 sibling_list = null;
+
+ public FlowBranchingBlock (FlowBranching parent, BranchingType type,
+ SiblingType stype, Block block, Location loc)
+ : base (parent, type, stype, block, loc)
+ { }
+
+ public override UsageVector CurrentUsageVector {
+ get { return sibling_list; }
+ }
+
+ protected override void AddSibling (UsageVector sibling)
+ {
+ sibling.Next = sibling_list;
+ 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) {
+ UsageVector vector = CurrentUsageVector.Clone ();
+ vector.Next = origin_vectors;
+ origin_vectors = vector;
+ }
+
+ CurrentUsageVector.MergeJumpOrigins (origin_vectors);
+ }
+
+ protected override UsageVector Merge ()
+ {
+ return Merge (sibling_list);
+ }
+ }
+
+ public class FlowBranchingLoop : FlowBranchingBlock
+ {
+ UsageVector break_origins;
+
+ public FlowBranchingLoop (FlowBranching parent, Block block, Location loc)
+ : base (parent, BranchingType.Loop, SiblingType.Conditional, block, loc)
+ { }
+
+ public override void AddBreakVector (UsageVector vector)
+ {
+ vector = vector.Clone ();
+ vector.Next = break_origins;
+ break_origins = vector;
+ }
+
+ protected override UsageVector Merge ()
+ {
+ UsageVector vector = base.Merge ();
+
+ vector.MergeBreakOrigins (this, break_origins);
+
+ return vector;
+ }
+ }
+
+ public class FlowBranchingSwitch : FlowBranchingBlock
+ {
+ UsageVector break_origins;
+
+ public FlowBranchingSwitch (FlowBranching parent, Block block, Location loc)
+ : base (parent, BranchingType.Switch, SiblingType.SwitchSection, block, loc)
+ { }
+
+ public override void AddBreakVector (UsageVector vector)
+ {
+ vector = vector.Clone ();
+ vector.Next = break_origins;
+ break_origins = vector;
+ }
+
+ protected override UsageVector Merge ()
+ {
+ UsageVector vector = base.Merge ();
+
+ vector.MergeBreakOrigins (this, break_origins);
+
+ return vector;
+ }
+ }
+
+ public class FlowBranchingException : FlowBranching
+ {
+ ExceptionStatement stmt;
+ UsageVector current_vector;
+ UsageVector catch_vectors;
+ UsageVector finally_vector;
+ UsageVector finally_origins;
+ bool emit_finally;
+
+ 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)
+ {
+ if (sibling.Type == SiblingType.Try) {
+ sibling.Next = catch_vectors;
+ catch_vectors = sibling;
+ } else if (sibling.Type == SiblingType.Catch) {
+ sibling.Next = catch_vectors;
+ catch_vectors = sibling;
+ } else if (sibling.Type == SiblingType.Finally) {
+ sibling.MergeFinallyOrigins (finally_origins);
+ finally_vector = sibling;
+ } else
+ throw new InvalidOperationException ();
+
+ current_vector = sibling;
+ }
+
+ public override UsageVector CurrentUsageVector {
+ get { return current_vector; }
+ }
+
+ public override bool InTryOrCatch (bool is_return)
+ {
+ return finally_vector == null;
+ }
+
+ public override bool InTryWithCatch ()
+ {
+ if (finally_vector == null) {
+ Try t = stmt as Try;
+ if (t != null && t.HasCatch)
+ return true;
+ }
+
+ if (Parent != null)
+ return Parent.InTryWithCatch ();
+
+ return false;
+ }
+
+ public override bool BreakCrossesTryCatchBoundary ()
+ {
+ return true;
+ }
+
+ public override void AddFinallyVector (UsageVector vector)
+ {
+ vector = vector.Clone ();
+ vector.Next = finally_origins;
+ 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 cannot leave the body of a finally clause");
+ return null;
+ }
+
+ return base.LookupLabel (name, loc);
+ }
+
+ public override void Label (UsageVector origin_vectors)
+ {
+ CurrentUsageVector.MergeJumpOrigins (origin_vectors);
+ }
+
+ protected override UsageVector Merge ()
+ {
+ UsageVector vector = Merge (catch_vectors);
+
+ vector.MergeFinally (this, finally_vector, finally_origins);
+
+ return vector;
+ }
}
// <summary>
if (!branching.IsFieldAssigned (vi, field.Name)) {
Report.Error (171, loc,
- "Field `" + TypeManager.CSharpName (Type) +
- "." + field.Name + "' must be fully initialized " +
- "before control leaves the constructor");
+ "Field `{0}' must be fully assigned before control leaves the constructor",
+ TypeManager.GetFullNameSignature (field));
ok = false;
}
}
if (type is TypeBuilder) {
TypeContainer tc = TypeManager.LookupTypeContainer (type);
- ArrayList fields = tc.Fields;
+ ArrayList fields = null;
+ if (tc != null)
+ fields = tc.Fields;
ArrayList public_fields = new ArrayList ();
ArrayList non_public_fields = new ArrayList ();
if (fields != null) {
- foreach (Field field in fields) {
+ foreach (FieldMember field in fields) {
if ((field.ModFlags & Modifiers.STATIC) != 0)
continue;
if ((field.ModFlags & Modifiers.PUBLIC) != 0)
field_hash.Add (field.Name, ++Length);
else if (sinfo [i].InTransit) {
Report.Error (523, String.Format (
- "Struct member '{0}.{1}' of type '{2}' causes " +
+ "Struct member `{0}.{1}' of type `{2}' causes " +
"a cycle in the structure layout",
type, field.Name, sinfo [i].Type));
sinfo [i] = null;
public bool IsAssigned (EmitContext ec)
{
- return !ec.DoFlowAnalysis || ec.CurrentBranching.IsAssigned (this);
+ return !ec.DoFlowAnalysis ||
+ ec.OmitStructFlowAnalysis && TypeInfo.IsStruct ||
+ ec.CurrentBranching.IsAssigned (this);
}
public bool IsAssigned (EmitContext ec, Location loc)
public bool IsFieldAssigned (EmitContext ec, string name, Location loc)
{
- if (!ec.DoFlowAnalysis || ec.CurrentBranching.IsFieldAssigned (this, name))
+ if (!ec.DoFlowAnalysis ||
+ ec.OmitStructFlowAnalysis && TypeInfo.IsStruct ||
+ ec.CurrentBranching.IsFieldAssigned (this, name))
return true;
Report.Error (170, loc,
// <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)
+ public VariableMap (Parameters 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++) {
Parameter.Modifier mod = ip.ParameterModifier (i);
- if ((mod & Parameter.Modifier.OUT) == 0)
+ if ((mod & Parameter.Modifier.OUT) != Parameter.Modifier.OUT)
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];
}
}
}
}
-
// <summary>
// This is a special bit vector which can inherit from another bit vector doing a
// copy-on-write strategy. The inherited vector may have a smaller size than the
// </summary>
public void And (MyBitVector new_vector)
{
- BitArray new_array = new_vector.Vector;
+ BitArray new_array;
+
+ if (new_vector != null)
+ new_array = new_vector.Vector;
+ else
+ new_array = new BitArray (Count, false);
initialize_vector ();
vector [i] = false;
}
+ public static void And (ref MyBitVector target, MyBitVector vector)
+ {
+ if (target != null)
+ target.And (vector);
+ else
+ target = vector.Clone ();
+ }
+
+ public static void Or (ref MyBitVector target, MyBitVector vector)
+ {
+ if (target != null)
+ target.Or (vector);
+ else
+ target = vector.Clone ();
+ }
+
// <summary>
// This does a deep copy of the bit vector.
// </summary>
{
if (vector != null)
return;
-
+
vector = new BitArray (Count, false);
if (InheritsFrom != null)
Vector = InheritsFrom.Vector;
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 ();
}
}