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