// Martin Baulig (martin@ximian.com)
// Raja R Harinath (rharinath@novell.com)
//
-// (C) 2001, 2002, 2003 Ximian, Inc.
+// Copyright 2001, 2002, 2003 Ximian, Inc.
+// Copyright 2003-2008 Novell, Inc.
//
using System;
Switch,
// The toplevel block of a function
- Toplevel
+ Toplevel,
+
+ // An iterator block
+ Iterator
}
// <summary>
case BranchingType.Exception:
case BranchingType.Labeled:
case BranchingType.Toplevel:
+ case BranchingType.TryCatch:
throw new InvalidOperationException ();
case BranchingType.Switch:
case BranchingType.Embedded:
return new FlowBranchingContinuable (parent, type, SiblingType.Conditional, block, loc);
- case BranchingType.TryCatch:
- return new FlowBranchingTryCatch (parent, loc);
-
default:
return new FlowBranchingBlock (parent, type, SiblingType.Conditional, block, loc);
}
locals |= child.locals;
+ // throw away un-necessary information about variables in child blocks
+ if (locals.Count != CountLocals)
+ locals = new MyBitVector (locals, CountLocals);
+
if (overwrite)
is_unreachable = new_isunr;
else
protected abstract UsageVector Merge ();
- // <summary>
- // Merge a child branching.
- // </summary>
public UsageVector MergeChild (FlowBranching child)
{
- bool overwrite = false;
-
- switch (child.Type) {
- case BranchingType.Labeled:
- overwrite = true;
- break;
- case BranchingType.Block:
- if (child.Block != null && child.Block != child.Block.Explicit)
- overwrite = true;
- break;
- }
-
- Report.Debug (2, " MERGING CHILD", this, child);
- UsageVector result = CurrentUsageVector.MergeChild (child.Merge (), overwrite);
- Report.Debug (2, " MERGING CHILD DONE", this, result);
- return result;
+ return CurrentUsageVector.MergeChild (child.Merge (), true);
}
- public virtual bool InTryWithCatch ()
+ public virtual bool CheckRethrow (Location loc)
{
- return Parent.InTryWithCatch ();
+ return Parent.CheckRethrow (loc);
+ }
+
+ public virtual bool AddResumePoint (ResumableStatement stmt, Location loc, out int pc)
+ {
+ return Parent.AddResumePoint (stmt, loc, out pc);
}
// returns true if we crossed an unwind-protected region (try/catch/finally, lock, using, ...)
}
// returns true if we crossed an unwind-protected region (try/catch/finally, lock, using, ...)
- public virtual bool AddReturnOrigin (UsageVector vector, Location loc)
+ public virtual bool AddReturnOrigin (UsageVector vector, ExitStatement stmt)
{
- return Parent.AddReturnOrigin (vector, loc);
+ return Parent.AddReturnOrigin (vector, stmt);
}
// returns true if we crossed an unwind-protected region (try/catch/finally, lock, using, ...)
return Parent.AddGotoOrigin (vector, goto_stmt);
}
- // returns true if we crossed an unwind-protected region (try/catch/finally, lock, using, ...)
- public virtual bool StealFinallyClauses (ref ArrayList list)
- {
- return Parent.StealFinallyClauses (ref list);
- }
-
public bool IsAssigned (VariableInfo vi)
{
return CurrentUsageVector.IsAssigned (vi, false);
protected override void AddSibling (UsageVector sibling)
{
- if (sibling.Type == SiblingType.Block && sibling_list != null)
+ if (sibling_list != null && sibling_list.Type == SiblingType.Block)
throw new InternalErrorException ("Blocks don't have sibling flow paths");
sibling.Next = sibling_list;
sibling_list = sibling;
}
}
+ public class FlowBranchingIterator : FlowBranchingBlock
+ {
+ Iterator iterator;
+ public FlowBranchingIterator (FlowBranching parent, Iterator iterator)
+ : base (parent, BranchingType.Iterator, SiblingType.Block, null, iterator.Location)
+ {
+ this.iterator = iterator;
+ }
+
+ public override bool AddResumePoint (ResumableStatement stmt, Location loc, out int pc)
+ {
+ pc = iterator.AddResumePoint (stmt);
+ return false;
+ }
+ }
+
public class FlowBranchingToplevel : FlowBranchingBlock
{
UsageVector return_origins;
{
}
- public override bool InTryWithCatch ()
+ public override bool CheckRethrow (Location loc)
{
+ Report.Error (156, loc, "A throw statement with no arguments is not allowed outside of a catch clause");
return false;
}
+ public override bool AddResumePoint (ResumableStatement stmt, Location loc, out int pc)
+ {
+ throw new InternalErrorException ("A yield in a non-iterator block");
+ }
+
public override bool AddBreakOrigin (UsageVector vector, Location loc)
{
Report.Error (139, loc, "No enclosing loop out of which to break or continue");
return false;
}
- public override bool AddReturnOrigin (UsageVector vector, Location loc)
+ public override bool AddReturnOrigin (UsageVector vector, ExitStatement stmt)
{
vector = vector.Clone ();
- vector.Location = loc;
+ vector.Location = stmt.loc;
vector.Next = return_origins;
return_origins = vector;
return false;
}
- public override bool StealFinallyClauses (ref ArrayList list)
- {
- return false;
- }
-
public override bool AddGotoOrigin (UsageVector vector, Goto goto_stmt)
{
string name = goto_stmt.Target;
public class FlowBranchingTryCatch : FlowBranchingBlock
{
- public FlowBranchingTryCatch (FlowBranching parent, Location loc)
- : base (parent, BranchingType.Block, SiblingType.Try, null, loc)
+ TryCatch stmt;
+ public FlowBranchingTryCatch (FlowBranching parent, TryCatch stmt)
+ : base (parent, BranchingType.Block, SiblingType.Try, null, stmt.loc)
{
+ this.stmt = stmt;
}
- public override bool InTryWithCatch ()
+ public override bool CheckRethrow (Location loc)
{
+ return CurrentUsageVector.Next != null || Parent.CheckRethrow (loc);
+ }
+
+ public override bool AddResumePoint (ResumableStatement stmt, Location loc, out int pc)
+ {
+ int errors = Report.Errors;
+ Parent.AddResumePoint (stmt, loc, out pc);
+ if (errors == Report.Errors) {
+ if (CurrentUsageVector.Next == null)
+ Report.Error (1626, loc, "Cannot yield a value in the body of a try block with a catch clause");
+ else
+ Report.Error (1631, loc, "Cannot yield a value in the body of a catch clause");
+ }
return true;
}
public override bool AddBreakOrigin (UsageVector vector, Location loc)
{
Parent.AddBreakOrigin (vector, loc);
+ stmt.SomeCodeFollows ();
return true;
}
public override bool AddContinueOrigin (UsageVector vector, Location loc)
{
Parent.AddContinueOrigin (vector, loc);
+ stmt.SomeCodeFollows ();
return true;
}
- public override bool AddReturnOrigin (UsageVector vector, Location loc)
+ public override bool AddReturnOrigin (UsageVector vector, ExitStatement exit_stmt)
{
- Parent.AddReturnOrigin (vector, loc);
+ Parent.AddReturnOrigin (vector, exit_stmt);
+ stmt.SomeCodeFollows ();
return true;
}
Parent.AddGotoOrigin (vector, goto_stmt);
return true;
}
-
- public override bool StealFinallyClauses (ref ArrayList list)
- {
- Parent.StealFinallyClauses (ref list);
- return true;
- }
}
public class FlowBranchingException : FlowBranching
UsageVector try_vector;
UsageVector finally_vector;
- UsageVector break_origins;
- UsageVector continue_origins;
- UsageVector return_origins;
- GotoOrigin goto_origins;
-
- class GotoOrigin {
- public GotoOrigin Next;
- public Goto GotoStmt;
- public UsageVector Vector;
+ abstract class SavedOrigin {
+ public readonly SavedOrigin Next;
+ public readonly UsageVector Vector;
- public GotoOrigin (UsageVector vector, Goto goto_stmt, GotoOrigin next)
+ protected SavedOrigin (SavedOrigin next, UsageVector vector)
{
- Vector = vector;
- GotoStmt = goto_stmt;
Next = next;
+ Vector = vector.Clone ();
+ }
+
+ protected abstract void DoPropagateFinally (FlowBranching parent);
+ public void PropagateFinally (UsageVector finally_vector, FlowBranching parent)
+ {
+ if (finally_vector != null)
+ Vector.MergeChild (finally_vector, false);
+ DoPropagateFinally (parent);
}
}
- bool emit_finally;
+ class BreakOrigin : SavedOrigin {
+ Location Loc;
+ public BreakOrigin (SavedOrigin next, UsageVector vector, Location loc)
+ : base (next, vector)
+ {
+ Loc = loc;
+ }
+
+ protected override void DoPropagateFinally (FlowBranching parent)
+ {
+ parent.AddBreakOrigin (Vector, Loc);
+ }
+ }
+
+ class ContinueOrigin : SavedOrigin {
+ Location Loc;
+ public ContinueOrigin (SavedOrigin next, UsageVector vector, Location loc)
+ : base (next, vector)
+ {
+ Loc = loc;
+ }
+
+ protected override void DoPropagateFinally (FlowBranching parent)
+ {
+ parent.AddContinueOrigin (Vector, Loc);
+ }
+ }
+
+ class ReturnOrigin : SavedOrigin {
+ public ExitStatement Stmt;
+
+ public ReturnOrigin (SavedOrigin next, UsageVector vector, ExitStatement stmt)
+ : base (next, vector)
+ {
+ Stmt = stmt;
+ }
+
+ protected override void DoPropagateFinally (FlowBranching parent)
+ {
+ parent.AddReturnOrigin (Vector, Stmt);
+ }
+ }
+
+ class GotoOrigin : SavedOrigin {
+ public Goto Stmt;
+
+ public GotoOrigin (SavedOrigin next, UsageVector vector, Goto stmt)
+ : base (next, vector)
+ {
+ Stmt = stmt;
+ }
+
+ protected override void DoPropagateFinally (FlowBranching parent)
+ {
+ parent.AddGotoOrigin (Vector, Stmt);
+ }
+ }
+
+ SavedOrigin saved_origins;
public FlowBranchingException (FlowBranching parent,
ExceptionStatement stmt)
null, stmt.loc)
{
this.stmt = stmt;
- this.emit_finally = true;
}
protected override void AddSibling (UsageVector sibling)
get { return current_vector; }
}
+ public override bool CheckRethrow (Location loc)
+ {
+ if (!Parent.CheckRethrow (loc))
+ return false;
+ if (finally_vector == null)
+ return true;
+ Report.Error (724, loc, "A throw statement with no arguments is not allowed inside of a finally clause nested inside of the innermost catch clause");
+ return false;
+ }
+
+ public override bool AddResumePoint (ResumableStatement stmt, Location loc, out int pc)
+ {
+ int errors = Report.Errors;
+ Parent.AddResumePoint (this.stmt, loc, out pc);
+ if (errors == Report.Errors) {
+ if (finally_vector == null)
+ this.stmt.AddResumePoint (stmt, pc);
+ else
+ Report.Error (1625, loc, "Cannot yield in the body of a finally clause");
+ }
+ return true;
+ }
+
public override bool AddBreakOrigin (UsageVector vector, Location loc)
{
- vector = vector.Clone ();
if (finally_vector != null) {
int errors = Report.Errors;
Parent.AddBreakOrigin (vector, loc);
if (errors == Report.Errors)
Report.Error (157, loc, "Control cannot leave the body of a finally clause");
} else {
- vector.Location = loc;
- vector.Next = break_origins;
- break_origins = vector;
+ saved_origins = new BreakOrigin (saved_origins, vector, loc);
}
+
+ // either the loop test or a back jump will follow code
+ stmt.SomeCodeFollows ();
return true;
}
public override bool AddContinueOrigin (UsageVector vector, Location loc)
{
- vector = vector.Clone ();
if (finally_vector != null) {
int errors = Report.Errors;
Parent.AddContinueOrigin (vector, loc);
if (errors == Report.Errors)
Report.Error (157, loc, "Control cannot leave the body of a finally clause");
} else {
- vector.Location = loc;
- vector.Next = continue_origins;
- continue_origins = vector;
+ saved_origins = new ContinueOrigin (saved_origins, vector, loc);
}
+
+ // either the loop test or a back jump will follow code
+ stmt.SomeCodeFollows ();
return true;
}
- public override bool AddReturnOrigin (UsageVector vector, Location loc)
+ public override bool AddReturnOrigin (UsageVector vector, ExitStatement exit_stmt)
{
- vector = vector.Clone ();
if (finally_vector != null) {
int errors = Report.Errors;
- Parent.AddReturnOrigin (vector, loc);
+ Parent.AddReturnOrigin (vector, exit_stmt);
if (errors == Report.Errors)
- Report.Error (157, loc, "Control cannot leave the body of a finally clause");
+ exit_stmt.Error_FinallyClause ();
} else {
- vector.Location = loc;
- vector.Next = return_origins;
- return_origins = vector;
+ saved_origins = new ReturnOrigin (saved_origins, vector, exit_stmt);
}
+
+ // sets ec.NeedReturnLabel()
+ stmt.SomeCodeFollows ();
return true;
}
if (s != null)
throw new InternalErrorException ("Shouldn't get here");
- vector = vector.Clone ();
if (finally_vector != null) {
int errors = Report.Errors;
Parent.AddGotoOrigin (vector, goto_stmt);
if (errors == Report.Errors)
Report.Error (157, goto_stmt.loc, "Control cannot leave the body of a finally clause");
} else {
- goto_origins = new GotoOrigin (vector, goto_stmt, goto_origins);
+ saved_origins = new GotoOrigin (saved_origins, vector, goto_stmt);
}
return true;
}
- public override bool StealFinallyClauses (ref ArrayList list)
- {
- if (list == null)
- list = new ArrayList ();
- list.Add (stmt);
- emit_finally = false;
- base.StealFinallyClauses (ref list);
- return true;
- }
-
- public bool EmitFinally {
- get { return emit_finally; }
- }
-
protected override UsageVector Merge ()
{
UsageVector vector = try_vector.Clone ();
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);
- Parent.AddBreakOrigin (origin, origin.Location);
- }
-
- for (UsageVector origin = continue_origins; origin != null; origin = origin.Next) {
- if (finally_vector != null)
- origin.MergeChild (finally_vector, false);
- Parent.AddContinueOrigin (origin, origin.Location);
- }
-
- for (UsageVector origin = return_origins; origin != null; origin = origin.Next) {
- if (finally_vector != null)
- origin.MergeChild (finally_vector, false);
- Parent.AddReturnOrigin (origin, origin.Location);
- }
-
- for (GotoOrigin origin = goto_origins; origin != null; origin = origin.Next) {
- if (finally_vector != null)
- origin.Vector.MergeChild (finally_vector, false);
- Parent.AddGotoOrigin (origin.Vector, origin.GotoStmt);
- }
+ for (SavedOrigin origin = saved_origins; origin != null; origin = origin.Next)
+ origin.PropagateFinally (finally_vector, Parent);
return vector;
}
FieldInfo field = struct_info.Fields [i];
if (!branching.IsFieldAssigned (vi, field.Name)) {
- Report.Error (171, loc,
- "Field `{0}' must be fully assigned before control leaves the constructor",
- TypeManager.GetFullNameSignature (field));
+ FieldBase fb = TypeManager.GetField (field);
+ if (fb != null && (fb.ModFlags & Modifiers.BACKING_FIELD) != 0) {
+ Report.Error (843, loc,
+ "An automatically implemented property `{0}' must be fully assigned before control leaves the constructor. Consider calling default contructor",
+ fb.GetSignatureForError ());
+ } else {
+ Report.Error (171, loc,
+ "Field `{0}' must be fully assigned before control leaves the constructor",
+ TypeManager.GetFullNameSignature (field));
+ }
ok = false;
}
}
field_type_hash.Add (type, this);
- if (type is TypeBuilder) {
- TypeContainer tc = TypeManager.LookupTypeContainer (type);
-
- ArrayList fields = null;
- if (tc != null)
- fields = tc.Fields;
+ if (type.Module == CodeGen.Module.Builder) {
+ TypeContainer tc = TypeManager.LookupTypeContainer (TypeManager.DropGenericTypeArguments (type));
ArrayList public_fields = new ArrayList ();
ArrayList non_public_fields = new ArrayList ();
+ //
+ // TODO: tc != null is needed because FixedBuffers are not cached
+ //
+ if (tc != null) {
+ ArrayList fields = tc.Fields;
+
if (fields != null) {
foreach (FieldBase field in fields) {
if ((field.ModFlags & Modifiers.STATIC) != 0)
}
}
+ if (tc.Events != null) {
+ foreach (Event e in tc.Events) {
+ if ((e.ModFlags & Modifiers.STATIC) != 0)
+ continue;
+
+ EventField ef = e as EventField;
+ if (ef == null)
+ continue;
+
+ if ((ef.ModFlags & Modifiers.PUBLIC) != 0)
+ public_fields.Add (ef.FieldBuilder);
+ else
+ non_public_fields.Add (ef.FieldBuilder);
+ }
+ }
+ }
+
CountPublic = public_fields.Count;
CountNonPublic = non_public_fields.Count;
Count = CountPublic + CountNonPublic;
TypeManager.IsBuiltinType (type))
return null;
+ if (TypeManager.IsGenericParameter (type))
+ return null;
+
StructInfo info = (StructInfo) field_type_hash [type];
if (info != null)
return info;
readonly VariableInfo Parent;
VariableInfo[] sub_info;
+ bool is_ever_assigned;
+ public bool IsEverAssigned {
+ get { return is_ever_assigned; }
+ }
+
protected VariableInfo (string name, Type type, int offset)
{
this.Name = name;
}
public VariableInfo (Parameters ip, int i, int offset)
- : this (ip.ParameterName (i), TypeManager.GetElementType (ip.ParameterType (i)), offset)
+ : this (ip.FixedParameters [i].Name, ip.Types [i], offset)
{
this.IsParameter = true;
}
}
vector [Offset] = true;
+ is_ever_assigned = true;
return true;
}
vector [Offset] = true;
else
vector.SetRange (Offset, Length);
+ is_ever_assigned = true;
}
public bool IsFieldAssigned (EmitContext ec, string name, Location loc)
return;
vector [Offset + field_idx] = true;
+ is_ever_assigned = true;
}
public VariableInfo GetSubStruct (string name)
public MyBitVector (MyBitVector InheritsFrom, int Count)
{
if (InheritsFrom != null)
- shared = InheritsFrom.Shared;
+ shared = InheritsFrom.MakeShared (Count);
this.Count = Count;
}
- // Use this accessor to get a shareable copy of the underlying BitArray representation
- BitArray Shared {
- get {
- // Post-condition: vector == null
- if (shared == null) {
- shared = vector;
- vector = null;
- }
- return shared;
+ BitArray MakeShared (int new_count)
+ {
+ // Post-condition: vector == null
+
+ // ensure we don't leak out dirty bits from the BitVector we inherited from
+ if (new_count > Count &&
+ ((shared != null && shared.Count > Count) ||
+ (shared == null && vector == null)))
+ initialize_vector ();
+
+ if (vector != null) {
+ shared = vector;
+ vector = null;
}
+
+ return shared;
}
// <summary>
public bool this [int index] {
get {
if (index >= Count)
- throw new ArgumentOutOfRangeException ();
+ // FIXME: Disabled due to missing anonymous method flow analysis
+ // throw new ArgumentOutOfRangeException ();
+ return true;
if (vector != null)
return vector [index];
if (Count == o.Count) {
if (vector == null) {
if (shared == null) {
- shared = new_vector.Shared;
+ shared = new_vector.MakeShared (Count);
return this;
}
initialize_vector ();
// Don't clobber Empty
if (Count == 0)
return;
- shared = value ? null : Empty.Shared;
+ shared = value ? null : Empty.MakeShared (Count);
vector = null;
}