More tests.
[mono.git] / mcs / mcs / flowanalysis.cs
index e318d92ca6eec502fd915e3cf599ef9eed2806a7..c9f58f9fc1b1e3267e5bbe16be1056193bfeb053 100644 (file)
@@ -314,6 +314,10 @@ namespace Mono.CSharp
 
                                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
@@ -453,6 +457,10 @@ namespace Mono.CSharp
                        return CurrentUsageVector.IsAssigned (vi, false) || CurrentUsageVector.IsFieldAssigned (vi, field_name);
                }
 
+               protected static Report Report {
+                       get { return RootContext.ToplevelTypes.Compiler.Report; }
+               }
+
                public void SetAssigned (VariableInfo vi)
                {
                        CurrentUsageVector.SetAssigned (vi);
@@ -524,7 +532,7 @@ namespace Mono.CSharp
                        return false;
                }
                
-               public static void Error_UnknownLabel (Location loc, string label)
+               public static void Error_UnknownLabel (Location loc, string label, Report Report)
                {
                        Report.Error(159, loc, "The label `{0}:' could not be found within the scope of the goto statement",
                                label);
@@ -692,7 +700,7 @@ namespace Mono.CSharp
                                throw new InternalErrorException ("Shouldn't get here");
 
                        if (Parent == null) {
-                               Error_UnknownLabel (goto_stmt.loc, name);
+                               Error_UnknownLabel (goto_stmt.loc, name, Report);
                                return false;
                        }
 
@@ -949,7 +957,7 @@ namespace Mono.CSharp
                                int errors = Report.Errors;
                                Parent.AddReturnOrigin (vector, exit_stmt);
                                if (errors == Report.Errors)
-                                       exit_stmt.Error_FinallyClause ();
+                                       exit_stmt.Error_FinallyClause (Report);
                        } else {
                                saved_origins = new ReturnOrigin (saved_origins, vector, exit_stmt);
                        }
@@ -1034,8 +1042,19 @@ namespace Mono.CSharp
                // </summary>
                public TypeInfo[] SubStructInfo;
 
-               protected readonly StructInfo struct_info;
-               private static Hashtable type_hash = new Hashtable ();
+               readonly StructInfo struct_info;
+               private static Hashtable type_hash;
+               
+               static TypeInfo ()
+               {
+                       Reset ();
+               }
+               
+               public static void Reset ()
+               {
+                       type_hash = new Hashtable ();
+                       StructInfo.field_type_hash = new Hashtable ();
+               }
 
                public static TypeInfo GetTypeInfo (Type type)
                {
@@ -1093,7 +1112,7 @@ namespace Mono.CSharp
                        }
                }
 
-               protected TypeInfo (StructInfo struct_info, int offset)
+               TypeInfo (StructInfo struct_info, int offset)
                {
                        this.struct_info = struct_info;
                        this.Offset = offset;
@@ -1124,23 +1143,24 @@ namespace Mono.CSharp
                //   A struct's constructor must always assign all fields.
                //   This method checks whether it actually does so.
                // </summary>
-               public bool IsFullyInitialized (FlowBranching branching, VariableInfo vi, Location loc)
+               public bool IsFullyInitialized (BlockContext ec, VariableInfo vi, Location loc)
                {
                        if (struct_info == null)
                                return true;
 
                        bool ok = true;
+                       FlowBranching branching = ec.CurrentBranching;
                        for (int i = 0; i < struct_info.Count; i++) {
                                FieldInfo field = struct_info.Fields [i];
 
                                if (!branching.IsFieldAssigned (vi, field.Name)) {
                                        FieldBase fb = TypeManager.GetField (field);
-                                       if (fb != null && (fb.ModFlags & Modifiers.BACKING_FIELD) != 0) {
-                                               Report.Error (843, loc,
+                                       if (fb is Property.BackingField) {
+                                               ec.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,
+                                               ec.Report.Error (171, loc,
                                                        "Field `{0}' must be fully assigned before control leaves the constructor",
                                                        TypeManager.GetFullNameSignature (field));
                                        }
@@ -1157,7 +1177,7 @@ namespace Mono.CSharp
                                              Type, Offset, Length, TotalLength);
                }
 
-               protected class StructInfo {
+               class StructInfo {
                        public readonly Type Type;
                        public readonly FieldInfo[] Fields;
                        public readonly TypeInfo[] StructFields;
@@ -1168,7 +1188,7 @@ namespace Mono.CSharp
                        public readonly int TotalLength;
                        public readonly bool HasStructFields;
 
-                       private static Hashtable field_type_hash = new Hashtable ();
+                       public static Hashtable field_type_hash;
                        private Hashtable struct_field_hash;
                        private Hashtable field_hash;
 
@@ -1182,7 +1202,7 @@ namespace Mono.CSharp
 
                                field_type_hash.Add (type, this);
 
-                               if (type.Module == CodeGen.Module.Builder) {
+                               if (TypeManager.IsBeingCompiled (type)) {
                                        TypeContainer tc = TypeManager.LookupTypeContainer (TypeManager.DropGenericTypeArguments (type));
 
                                        ArrayList public_fields = new ArrayList ();
@@ -1204,22 +1224,6 @@ namespace Mono.CSharp
                                                                non_public_fields.Add (field.FieldBuilder);
                                                }
                                        }
-
-                                       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;
@@ -1229,12 +1233,10 @@ namespace Mono.CSharp
                                        Fields = new FieldInfo [Count];
                                        public_fields.CopyTo (Fields, 0);
                                        non_public_fields.CopyTo (Fields, CountPublic);
-#if GMCS_SOURCE
                                } else if (type is GenericTypeParameterBuilder) {
                                        CountPublic = CountNonPublic = Count = 0;
 
                                        Fields = new FieldInfo [0];
-#endif
                                } else {
                                        FieldInfo[] public_fields = type.GetFields (
                                                BindingFlags.Instance|BindingFlags.Public);
@@ -1266,7 +1268,7 @@ namespace Mono.CSharp
                                        if (sinfo [i] == null)
                                                field_hash.Add (field.Name, ++Length);
                                        else if (sinfo [i].InTransit) {
-                                               Report.Error (523, String.Format (
+                                               RootContext.ToplevelTypes.Compiler.Report.Error (523, String.Format (
                                                                      "Struct member `{0}.{1}' of type `{2}' causes " +
                                                                      "a cycle in the structure layout",
                                                                      type, field.Name, sinfo [i].Type));
@@ -1416,25 +1418,25 @@ namespace Mono.CSharp
                        this.IsParameter = false;
                }
 
-               public VariableInfo (Parameters ip, int i, int offset)
+               public VariableInfo (ParametersCompiled ip, int i, int offset)
                        : this (ip.FixedParameters [i].Name, ip.Types [i], offset)
                {
                        this.IsParameter = true;
                }
 
-               public bool IsAssigned (EmitContext ec)
+               public bool IsAssigned (ResolveContext ec)
                {
                        return !ec.DoFlowAnalysis ||
                                ec.OmitStructFlowAnalysis && TypeInfo.IsStruct ||
                                ec.CurrentBranching.IsAssigned (this);
                }
 
-               public bool IsAssigned (EmitContext ec, Location loc)
+               public bool IsAssigned (ResolveContext ec, Location loc)
                {
                        if (IsAssigned (ec))
                                return true;
 
-                       Report.Error (165, loc,
+                       ec.Report.Error (165, loc,
                                      "Use of unassigned local variable `" + Name + "'");
                        ec.CurrentBranching.SetAssigned (this);
                        return false;
@@ -1482,7 +1484,7 @@ namespace Mono.CSharp
                        return true;
                }
 
-               public void SetAssigned (EmitContext ec)
+               public void SetAssigned (ResolveContext ec)
                {
                        if (ec.DoFlowAnalysis)
                                ec.CurrentBranching.SetAssigned (this);
@@ -1497,14 +1499,14 @@ namespace Mono.CSharp
                        is_ever_assigned = true;
                }
 
-               public bool IsFieldAssigned (EmitContext ec, string name, Location loc)
+               public bool IsFieldAssigned (ResolveContext ec, string name, Location loc)
                {
                        if (!ec.DoFlowAnalysis ||
                                ec.OmitStructFlowAnalysis && TypeInfo.IsStruct ||
                                ec.CurrentBranching.IsFieldAssigned (this, name))
                                return true;
 
-                       Report.Error (170, loc,
+                       ec.Report.Error (170, loc,
                                      "Use of possibly unassigned field `" + name + "'");
                        ec.CurrentBranching.SetFieldAssigned (this, name);
                        return false;
@@ -1520,7 +1522,7 @@ namespace Mono.CSharp
                        return vector [Offset + field_idx];
                }
 
-               public void SetFieldAssigned (EmitContext ec, string name)
+               public void SetFieldAssigned (ResolveContext ec, string name)
                {
                        if (ec.DoFlowAnalysis)
                                ec.CurrentBranching.SetFieldAssigned (this, name);
@@ -1577,21 +1579,27 @@ namespace Mono.CSharp
                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>
@@ -1600,7 +1608,9 @@ namespace Mono.CSharp
                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];
@@ -1690,7 +1700,7 @@ namespace Mono.CSharp
                        if (Count == o.Count) {
                                if (vector == null) {
                                        if (shared == null) {
-                                               shared = new_vector.Shared;
+                                               shared = new_vector.MakeShared (Count);
                                                return this;
                                        }
                                        initialize_vector ();
@@ -1776,7 +1786,7 @@ namespace Mono.CSharp
                        // Don't clobber Empty
                        if (Count == 0)
                                return;
-                       shared = value ? null : Empty.Shared;
+                       shared = value ? null : Empty.MakeShared (Count);
                        vector = null;
                }