New tests.
[mono.git] / mcs / mcs / flowanalysis.cs
index e580956ff4e9b1b00965b0994a52a5d79b10735e..bc35e3591e9925c42d1eadcd95332a78336a3b06 100644 (file)
@@ -11,7 +11,7 @@
 
 using System;
 using System.Text;
-using System.Collections;
+using System.Collections.Generic;
 using System.Reflection;
 using System.Reflection.Emit;
 using System.Diagnostics;
@@ -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);
@@ -660,9 +668,7 @@ namespace Mono.CSharp
 
                public override bool AddResumePoint (ResumableStatement stmt, Location loc, out int pc)
                {
-                       pc = -1;
-                       Report.Error (-6, loc, "Internal Error: A yield in a non-iterator");
-                       return false;
+                       throw new InternalErrorException ("A yield in a non-iterator block");
                }
 
                public override bool AddBreakOrigin (UsageVector vector, Location loc)
@@ -694,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;
                        }
 
@@ -951,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);
                        }
@@ -1008,7 +1014,7 @@ namespace Mono.CSharp
        // </summary>
        public class TypeInfo
        {
-               public readonly Type Type;
+               public readonly TypeSpec Type;
 
                // <summary>
                //   Total number of bits a variable of this type consumes in the flow vector.
@@ -1036,13 +1042,24 @@ 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 Dictionary<TypeSpec, TypeInfo> type_hash;
+               
+               static TypeInfo ()
+               {
+                       Reset ();
+               }
+               
+               public static void Reset ()
+               {
+                       type_hash = new Dictionary<TypeSpec, TypeInfo> ();
+                       StructInfo.field_type_hash = new Dictionary<TypeSpec, StructInfo> ();
+               }
 
-               public static TypeInfo GetTypeInfo (Type type)
+               public static TypeInfo GetTypeInfo (TypeSpec type)
                {
-                       TypeInfo info = (TypeInfo) type_hash [type];
-                       if (info != null)
+                       TypeInfo info;
+                       if (type_hash.TryGetValue (type, out info))
                                return info;
 
                        info = new TypeInfo (type);
@@ -1052,16 +1069,16 @@ namespace Mono.CSharp
 
                public static TypeInfo GetTypeInfo (TypeContainer tc)
                {
-                       TypeInfo info = (TypeInfo) type_hash [tc.TypeBuilder];
-                       if (info != null)
+                       TypeInfo info;
+                       if (type_hash.TryGetValue (tc.Definition, out info))
                                return info;
 
                        info = new TypeInfo (tc);
-                       type_hash.Add (tc.TypeBuilder, info);
+                       type_hash.Add (tc.Definition, info);
                        return info;
                }
 
-               private TypeInfo (Type type)
+               private TypeInfo (TypeSpec type)
                {
                        this.Type = type;
 
@@ -1080,7 +1097,7 @@ namespace Mono.CSharp
 
                private TypeInfo (TypeContainer tc)
                {
-                       this.Type = tc.TypeBuilder;
+                       this.Type = tc.Definition;
 
                        struct_info = StructInfo.GetStructInfo (tc);
                        if (struct_info != null) {
@@ -1095,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;
@@ -1126,25 +1143,25 @@ 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];
+                               var 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 (field.MemberDefinition 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 ());
+                                                       field.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));
+                                                       field.GetSignatureForError ());
                                        }
                                        ok = false;
                                }
@@ -1159,9 +1176,9 @@ namespace Mono.CSharp
                                              Type, Offset, Length, TotalLength);
                }
 
-               protected class StructInfo {
-                       public readonly Type Type;
-                       public readonly FieldInfo[] Fields;
+               class StructInfo {
+                       public readonly TypeSpec Type;
+                       public readonly FieldSpec[] Fields;
                        public readonly TypeInfo[] StructFields;
                        public readonly int Count;
                        public readonly int CountPublic;
@@ -1170,71 +1187,50 @@ namespace Mono.CSharp
                        public readonly int TotalLength;
                        public readonly bool HasStructFields;
 
-                       private static Hashtable field_type_hash = new Hashtable ();
-                       private Hashtable struct_field_hash;
-                       private Hashtable field_hash;
+                       public static Dictionary<TypeSpec, StructInfo> field_type_hash;
+                       private Dictionary<string, TypeInfo> struct_field_hash;
+                       private Dictionary<string, int> field_hash;
 
                        protected bool InTransit = false;
 
                        // Private constructor.  To save memory usage, we only need to create one instance
                        // of this class per struct type.
-                       private StructInfo (Type type)
+                       private StructInfo (TypeSpec type)
                        {
                                this.Type = type;
 
                                field_type_hash.Add (type, this);
 
-                               if (type is TypeBuilder) {
-                                       TypeContainer tc = TypeManager.LookupTypeContainer (type);
+                               TypeContainer tc = type.MemberDefinition as TypeContainer;
 
-                                       ArrayList fields = null;
-                                       if (tc != null)
-                                               fields = tc.Fields;
+                               var public_fields = new List<FieldSpec> ();
+                               var non_public_fields = new List<FieldSpec> ();
 
-                                       ArrayList public_fields = new ArrayList ();
-                                       ArrayList non_public_fields = new ArrayList ();
+                               if (tc != null) {
+                                       var fields = tc.Fields;
 
                                        if (fields != null) {
                                                foreach (FieldBase field in fields) {
                                                        if ((field.ModFlags & Modifiers.STATIC) != 0)
                                                                continue;
                                                        if ((field.ModFlags & Modifiers.PUBLIC) != 0)
-                                                               public_fields.Add (field.FieldBuilder);
+                                                               public_fields.Add (field.Spec);
                                                        else
-                                                               non_public_fields.Add (field.FieldBuilder);
+                                                               non_public_fields.Add (field.Spec);
                                                }
                                        }
+                               }
 
-                                       CountPublic = public_fields.Count;
-                                       CountNonPublic = non_public_fields.Count;
-                                       Count = CountPublic + CountNonPublic;
-
-                                       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;
+                               CountPublic = public_fields.Count;
+                               CountNonPublic = non_public_fields.Count;
+                               Count = CountPublic + CountNonPublic;
 
-                                       Fields = new FieldInfo [0];
-#endif
-                               } else {
-                                       FieldInfo[] public_fields = type.GetFields (
-                                               BindingFlags.Instance|BindingFlags.Public);
-                                       FieldInfo[] non_public_fields = type.GetFields (
-                                               BindingFlags.Instance|BindingFlags.NonPublic);
-
-                                       CountPublic = public_fields.Length;
-                                       CountNonPublic = non_public_fields.Length;
-                                       Count = CountPublic + CountNonPublic;
-
-                                       Fields = new FieldInfo [Count];
-                                       public_fields.CopyTo (Fields, 0);
-                                       non_public_fields.CopyTo (Fields, CountPublic);
-                               }
+                               Fields = new FieldSpec[Count];
+                               public_fields.CopyTo (Fields, 0);
+                               non_public_fields.CopyTo (Fields, CountPublic);
 
-                               struct_field_hash = new Hashtable ();
-                               field_hash = new Hashtable ();
+                               struct_field_hash = new Dictionary<string, TypeInfo> ();
+                               field_hash = new Dictionary<string, int> ();
 
                                Length = 0;
                                StructFields = new TypeInfo [Count];
@@ -1243,16 +1239,12 @@ namespace Mono.CSharp
                                InTransit = true;
 
                                for (int i = 0; i < Count; i++) {
-                                       FieldInfo field = (FieldInfo) Fields [i];
+                                       var field = Fields [i];
 
-                                       sinfo [i] = GetStructInfo (field.FieldType);
+                                       sinfo [i] = GetStructInfo (field.MemberType);
                                        if (sinfo [i] == null)
                                                field_hash.Add (field.Name, ++Length);
                                        else if (sinfo [i].InTransit) {
-                                               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));
                                                sinfo [i] = null;
                                                return;
                                        }
@@ -1262,7 +1254,7 @@ namespace Mono.CSharp
 
                                TotalLength = Length + 1;
                                for (int i = 0; i < Count; i++) {
-                                       FieldInfo field = (FieldInfo) Fields [i];
+                                       var field = Fields [i];
 
                                        if (sinfo [i] == null)
                                                continue;
@@ -1278,26 +1270,34 @@ namespace Mono.CSharp
 
                        public int this [string name] {
                                get {
-                                       if (field_hash.Contains (name))
-                                               return (int) field_hash [name];
-                                       else
+                                       int val;
+                                       if (!field_hash.TryGetValue (name, out val))
                                                return 0;
+
+                                       return val;
                                }
                        }
 
                        public TypeInfo GetStructField (string name)
                        {
-                               return (TypeInfo) struct_field_hash [name];
+                               TypeInfo ti;
+                               if (struct_field_hash.TryGetValue (name, out ti))
+                                       return ti;
+
+                               return null;
                        }
 
-                       public static StructInfo GetStructInfo (Type type)
+                       public static StructInfo GetStructInfo (TypeSpec type)
                        {
                                if (!TypeManager.IsValueType (type) || TypeManager.IsEnumType (type) ||
                                    TypeManager.IsBuiltinType (type))
                                        return null;
 
-                               StructInfo info = (StructInfo) field_type_hash [type];
-                               if (info != null)
+                               if (TypeManager.IsGenericParameter (type))
+                                       return null;
+
+                               StructInfo info;
+                               if (field_type_hash.TryGetValue (type, out info))
                                        return info;
 
                                return new StructInfo (type);
@@ -1305,11 +1305,11 @@ namespace Mono.CSharp
 
                        public static StructInfo GetStructInfo (TypeContainer tc)
                        {
-                               StructInfo info = (StructInfo) field_type_hash [tc.TypeBuilder];
-                               if (info != null)
+                               StructInfo info;
+                               if (field_type_hash.TryGetValue (tc.Definition, out info))
                                        return info;
 
-                               return new StructInfo (tc.TypeBuilder);
+                               return new StructInfo (tc.Definition);
                        }
                }
        }
@@ -1351,7 +1351,7 @@ namespace Mono.CSharp
                        get { return is_ever_assigned; }
                }
 
-               protected VariableInfo (string name, Type type, int offset)
+               protected VariableInfo (string name, TypeSpec type, int offset)
                {
                        this.Name = name;
                        this.Offset = offset;
@@ -1396,25 +1396,25 @@ namespace Mono.CSharp
                        this.IsParameter = false;
                }
 
-               public VariableInfo (Parameters ip, int i, int offset)
-                       : this (ip.ParameterName (i), TypeManager.GetElementType (ip.ParameterType (i)), 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;
@@ -1462,7 +1462,7 @@ namespace Mono.CSharp
                        return true;
                }
 
-               public void SetAssigned (EmitContext ec)
+               public void SetAssigned (ResolveContext ec)
                {
                        if (ec.DoFlowAnalysis)
                                ec.CurrentBranching.SetAssigned (this);
@@ -1477,14 +1477,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;
@@ -1500,7 +1500,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);
@@ -1547,31 +1547,37 @@ namespace Mono.CSharp
                // Invariant: vector == null || shared == null
                //            i.e., at most one of 'vector' and 'shared' can be non-null.  They can both be null -- that means all-ones
                // The object in 'shared' cannot be modified, while 'vector' can be freely modified
-               BitArray vector, shared;
+               System.Collections.BitArray vector, shared;
 
                MyBitVector ()
                {
-                       shared = new BitArray (0, false);
+                       shared = new System.Collections.BitArray (0, false);
                }
 
                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;
+               System.Collections.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>
@@ -1580,7 +1586,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];
@@ -1610,7 +1618,7 @@ namespace Mono.CSharp
                        if (Count == 0 || new_vector.Count == 0)
                                return this;
 
-                       BitArray o = new_vector.vector != null ? new_vector.vector : new_vector.shared;
+                       var o = new_vector.vector != null ? new_vector.vector : new_vector.shared;
 
                        if (o == null) {
                                int n = new_vector.Count;
@@ -1654,7 +1662,7 @@ namespace Mono.CSharp
                        if (Count == 0)
                                return this;
 
-                       BitArray o = new_vector.vector != null ? new_vector.vector : new_vector.shared;
+                       var o = new_vector.vector != null ? new_vector.vector : new_vector.shared;
 
                        if (o == null) {
                                for (int i = new_vector.Count; i < Count; ++i)
@@ -1670,7 +1678,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 ();
@@ -1756,7 +1764,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;
                }
 
@@ -1764,11 +1772,11 @@ namespace Mono.CSharp
                {
                        // Post-condition: vector != null
                        if (shared == null) {
-                               vector = new BitArray (Count, true);
+                               vector = new System.Collections.BitArray (Count, true);
                                return;
                        }
 
-                       vector = new BitArray (shared);
+                       vector = new System.Collections.BitArray (shared);
                        if (Count != vector.Count)
                                vector.Length = Count;
                        shared = null;
@@ -1776,7 +1784,7 @@ namespace Mono.CSharp
 
                StringBuilder Dump (StringBuilder sb)
                {
-                       BitArray dump = vector == null ? shared : vector;
+                       var dump = vector == null ? shared : vector;
                        if (dump == null)
                                return sb.Append ("/");
                        if (dump == shared)