Implemented PipeSecurity. GetAccessControl, SetAccessControl, and ACL-containing...
[mono.git] / mcs / mcs / flowanalysis.cs
index bedc9d74e5018bd446dc65dd6c36b38df9262d88..5efeccb8fb612cac28e11157553ff4285aa1dc45 100644 (file)
@@ -1,20 +1,19 @@
 //
 // flowanalyis.cs: The control flow analysis code
 //
-// Author:
+// Authors:
 //   Martin Baulig (martin@ximian.com)
 //   Raja R Harinath (rharinath@novell.com)
+//   Marek Safar (marek.safar@gmail.com)
 //
 // Copyright 2001, 2002, 2003 Ximian, Inc.
 // Copyright 2003-2008 Novell, Inc.
+// Copyright 2011 Xamarin, Inc.
 //
 
 using System;
 using System.Text;
-using System.Collections;
-using System.Reflection;
-using System.Reflection.Emit;
-using System.Diagnostics;
+using System.Collections.Generic;
 
 namespace Mono.CSharp
 {
@@ -238,22 +237,27 @@ namespace Mono.CSharp
 
                        public bool IsFieldAssigned (VariableInfo var, string name)
                        {
-                               if (!var.IsParameter && IsUnreachable)
+                               if (/*!var.IsParameter &&*/ IsUnreachable)
                                        return true;
 
-                               return var.IsFieldAssigned (locals, name);
+                               return var.IsStructFieldAssigned (locals, name);
                        }
 
                        public void SetFieldAssigned (VariableInfo var, string name)
                        {
-                               if (!var.IsParameter && IsUnreachable)
+                               if (/*!var.IsParameter &&*/ IsUnreachable)
                                        return;
 
-                               var.SetFieldAssigned (locals, name);
+                               var.SetStructFieldAssigned (locals, name);
                        }
 
                        public bool IsUnreachable {
-                               get { return is_unreachable; }
+                               get {
+                                       return is_unreachable;
+                               }
+                               set {
+                                       is_unreachable = value;
+                               }
                        }
 
                        public void ResetBarrier ()
@@ -418,9 +422,9 @@ namespace Mono.CSharp
                        return Parent.CheckRethrow (loc);
                }
 
-               public virtual bool AddResumePoint (ResumableStatement stmt, Location loc, out int pc)
+               public virtual bool AddResumePoint (ResumableStatement stmt, out int pc)
                {
-                       return Parent.AddResumePoint (stmt, loc, out pc);
+                       return Parent.AddResumePoint (stmt, out pc);
                }
 
                // returns true if we crossed an unwind-protected region (try/catch/finally, lock, using, ...)
@@ -452,11 +456,15 @@ namespace Mono.CSharp
                        return CurrentUsageVector.IsAssigned (vi, false);
                }
 
-               public bool IsFieldAssigned (VariableInfo vi, string field_name)
+               public bool IsStructFieldAssigned (VariableInfo vi, string field_name)
                {
                        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);
@@ -467,6 +475,7 @@ namespace Mono.CSharp
                        CurrentUsageVector.SetFieldAssigned (vi, name);
                }
 
+#if DEBUG
                public override string ToString ()
                {
                        StringBuilder sb = new StringBuilder ();
@@ -489,6 +498,7 @@ namespace Mono.CSharp
                        sb.Append (")");
                        return sb.ToString ();
                }
+#endif
 
                public string Name {
                        get { return String.Format ("{0} ({1}:{2}:{3})", GetType (), id, Type, Location); }
@@ -528,7 +538,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);
@@ -633,14 +643,15 @@ namespace Mono.CSharp
 
        public class FlowBranchingIterator : FlowBranchingBlock
        {
-               Iterator iterator;
+               readonly Iterator iterator;
+
                public FlowBranchingIterator (FlowBranching parent, Iterator iterator)
-                       : base (parent, BranchingType.Iterator, SiblingType.Block, null, iterator.Location)
+                       : base (parent, BranchingType.Iterator, SiblingType.Block, iterator.Block, iterator.Location)
                {
                        this.iterator = iterator;
                }
 
-               public override bool AddResumePoint (ResumableStatement stmt, Location loc, out int pc)
+               public override bool AddResumePoint (ResumableStatement stmt, out int pc)
                {
                        pc = iterator.AddResumePoint (stmt);
                        return false;
@@ -651,7 +662,7 @@ namespace Mono.CSharp
        {
                UsageVector return_origins;
 
-               public FlowBranchingToplevel (FlowBranching parent, ToplevelBlock stmt)
+               public FlowBranchingToplevel (FlowBranching parent, ParametersBlock stmt)
                        : base (parent, BranchingType.Toplevel, SiblingType.Conditional, stmt, stmt.loc)
                {
                }
@@ -662,7 +673,7 @@ namespace Mono.CSharp
                        return false;
                }
 
-               public override bool AddResumePoint (ResumableStatement stmt, Location loc, out int pc)
+               public override bool AddResumePoint (ResumableStatement stmt, out int pc)
                {
                        throw new InternalErrorException ("A yield in a non-iterator block");
                }
@@ -696,7 +707,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;
                        }
 
@@ -710,10 +721,10 @@ namespace Mono.CSharp
                protected override UsageVector Merge ()
                {
                        for (UsageVector origin = return_origins; origin != null; origin = origin.Next)
-                               Block.Toplevel.CheckOutParameters (origin, origin.Location);
+                               Block.ParametersBlock.CheckOutParameters (origin);
 
                        UsageVector vector = base.Merge ();
-                       Block.Toplevel.CheckOutParameters (vector, Block.loc);
+                       Block.ParametersBlock.CheckOutParameters (vector);
                        // Note: we _do_not_ merge in the return origins
                        return vector;
                }
@@ -726,11 +737,12 @@ namespace Mono.CSharp
 
        public class FlowBranchingTryCatch : FlowBranchingBlock
        {
-               TryCatch stmt;
+               readonly TryCatch tc;
+
                public FlowBranchingTryCatch (FlowBranching parent, TryCatch stmt)
                        : base (parent, BranchingType.Block, SiblingType.Try, null, stmt.loc)
                {
-                       this.stmt = stmt;
+                       this.tc = stmt;
                }
 
                public override bool CheckRethrow (Location loc)
@@ -738,37 +750,92 @@ namespace Mono.CSharp
                        return CurrentUsageVector.Next != null || Parent.CheckRethrow (loc);
                }
 
-               public override bool AddResumePoint (ResumableStatement stmt, Location loc, out int pc)
+               public override bool AddResumePoint (ResumableStatement stmt, out int pc)
                {
                        int errors = Report.Errors;
-                       Parent.AddResumePoint (stmt, loc, out pc);
+                       Parent.AddResumePoint (tc.IsTryCatchFinally ? stmt : tc, 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");
+                               if (stmt is AwaitStatement) {
+                                       if (CurrentUsageVector.Next != null) {
+                                               Report.Error (1985, stmt.loc, "The `await' operator cannot be used in the body of a catch clause");
+                                       } else {
+                                               this.tc.AddResumePoint (stmt, pc);
+                                       }
+                               } else {
+                                       if (CurrentUsageVector.Next == null)
+                                               Report.Error (1626, stmt.loc, "Cannot yield a value in the body of a try block with a catch clause");
+                                       else
+                                               Report.Error (1631, stmt.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);
+                       tc.SomeCodeFollows ();
+                       return true;
+               }
+
+               public override bool AddContinueOrigin (UsageVector vector, Location loc)
+               {
+                       Parent.AddContinueOrigin (vector, loc);
+                       tc.SomeCodeFollows ();
+                       return true;
+               }
+
+               public override bool AddReturnOrigin (UsageVector vector, ExitStatement exit_stmt)
+               {
+                       Parent.AddReturnOrigin (vector, exit_stmt);
+                       tc.SomeCodeFollows ();
+                       return true;
+               }
+
+               public override bool AddGotoOrigin (UsageVector vector, Goto goto_stmt)
+               {
+                       Parent.AddGotoOrigin (vector, goto_stmt);
+                       return true;
+               }
+       }
+
+       public  class FlowBranchingAsync : FlowBranchingBlock
+       {
+               readonly AsyncInitializer async_init;
+
+               public FlowBranchingAsync (FlowBranching parent, AsyncInitializer async_init)
+                       : base (parent, BranchingType.Block, SiblingType.Try, null, async_init.Location)
+               {
+                       this.async_init = async_init;
+               }
+/*
+               public override bool CheckRethrow (Location loc)
+               {
+                       return CurrentUsageVector.Next != null || Parent.CheckRethrow (loc);
+               }
+*/
+               public override bool AddResumePoint (ResumableStatement stmt, out int pc)
+               {
+                       pc = async_init.AddResumePoint (stmt);
                        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, ExitStatement exit_stmt)
                {
                        Parent.AddReturnOrigin (vector, exit_stmt);
-                       stmt.SomeCodeFollows ();
                        return true;
                }
 
@@ -779,7 +846,7 @@ namespace Mono.CSharp
                }
        }
 
-       public class FlowBranchingException : FlowBranching
+       public class FlowBranchingTryFinally : FlowBranching
        {
                ExceptionStatement stmt;
                UsageVector current_vector;
@@ -865,7 +932,7 @@ namespace Mono.CSharp
 
                SavedOrigin saved_origins;
 
-               public FlowBranchingException (FlowBranching parent,
+               public FlowBranchingTryFinally (FlowBranching parent,
                                               ExceptionStatement stmt)
                        : base (parent, BranchingType.Exception, SiblingType.Try,
                                null, stmt.loc)
@@ -902,15 +969,20 @@ namespace Mono.CSharp
                        return false;
                }
 
-               public override bool AddResumePoint (ResumableStatement stmt, Location loc, out int pc)
+               public override bool AddResumePoint (ResumableStatement stmt, out int pc)
                {
                        int errors = Report.Errors;
-                       Parent.AddResumePoint (this.stmt, loc, out pc);
+                       Parent.AddResumePoint (this.stmt, 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");
+                               else {
+                                       if (stmt is AwaitStatement) {
+                                               Report.Error (1984, stmt.loc, "The `await' operator cannot be used in the body of a finally clause");
+                                       } else {
+                                               Report.Error (1625, stmt.loc, "Cannot yield in the body of a finally clause");
+                                       }
+                               }
                        }
                        return true;
                }
@@ -953,7 +1025,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);
                        }
@@ -995,8 +1067,7 @@ namespace Mono.CSharp
        }
 
        // <summary>
-       //   This is used by the flow analysis code to keep track of the type of local variables
-       //   and variables.
+       //   This is used by the flow analysis code to keep track of the type of local variables.
        //
        //   The flow code uses a BitVector to keep track of whether a variable has been assigned
        //   or not.  This is easy for fundamental types (int, char etc.) or reference types since
@@ -1010,8 +1081,6 @@ namespace Mono.CSharp
        // </summary>
        public class TypeInfo
        {
-               public readonly Type Type;
-
                // <summary>
                //   Total number of bits a variable of this type consumes in the flow vector.
                // </summary>
@@ -1031,83 +1100,44 @@ namespace Mono.CSharp
                // <summary>
                //   If this is a struct.
                // </summary>
-               public readonly bool IsStruct;       
+               public readonly bool IsStruct;
 
                // <summary>
                //   If this is a struct, all fields which are structs theirselves.
                // </summary>
                public TypeInfo[] SubStructInfo;
 
-               protected readonly StructInfo struct_info;
-               private static Hashtable type_hash = new Hashtable ();
-
-               public static TypeInfo GetTypeInfo (Type type)
-               {
-                       TypeInfo info = (TypeInfo) type_hash [type];
-                       if (info != null)
-                               return info;
-
-                       info = new TypeInfo (type);
-                       type_hash.Add (type, info);
-                       return info;
-               }
+               readonly StructInfo struct_info;
+               private static Dictionary<TypeSpec, TypeInfo> type_hash;
 
-               public static TypeInfo GetTypeInfo (TypeContainer tc)
+               static readonly TypeInfo simple_type = new TypeInfo (1);
+               
+               static TypeInfo ()
                {
-                       TypeInfo info = (TypeInfo) type_hash [tc.TypeBuilder];
-                       if (info != null)
-                               return info;
-
-                       info = new TypeInfo (tc);
-                       type_hash.Add (tc.TypeBuilder, info);
-                       return info;
+                       Reset ();
                }
-
-               private TypeInfo (Type type)
+               
+               public static void Reset ()
                {
-                       this.Type = type;
-
-                       struct_info = StructInfo.GetStructInfo (type);
-                       if (struct_info != null) {
-                               Length = struct_info.Length;
-                               TotalLength = struct_info.TotalLength;
-                               SubStructInfo = struct_info.StructFields;
-                               IsStruct = true;
-                       } else {
-                               Length = 0;
-                               TotalLength = 1;
-                               IsStruct = false;
-                       }
+                       type_hash = new Dictionary<TypeSpec, TypeInfo> ();
+                       StructInfo.field_type_hash = new Dictionary<TypeSpec, StructInfo> ();
                }
 
-               private TypeInfo (TypeContainer tc)
+               TypeInfo (int totalLength)
                {
-                       this.Type = tc.TypeBuilder;
-
-                       struct_info = StructInfo.GetStructInfo (tc);
-                       if (struct_info != null) {
-                               Length = struct_info.Length;
-                               TotalLength = struct_info.TotalLength;
-                               SubStructInfo = struct_info.StructFields;
-                               IsStruct = true;
-                       } else {
-                               Length = 0;
-                               TotalLength = 1;
-                               IsStruct = false;
-                       }
+                       this.TotalLength = totalLength;
                }
-
-               protected TypeInfo (StructInfo struct_info, int offset)
+               
+               TypeInfo (StructInfo struct_info, int offset)
                {
                        this.struct_info = struct_info;
                        this.Offset = offset;
                        this.Length = struct_info.Length;
                        this.TotalLength = struct_info.TotalLength;
                        this.SubStructInfo = struct_info.StructFields;
-                       this.Type = struct_info.Type;
                        this.IsStruct = true;
                }
-
+               
                public int GetFieldIndex (string name)
                {
                        if (struct_info == null)
@@ -1116,7 +1146,7 @@ namespace Mono.CSharp
                        return struct_info [name];
                }
 
-               public TypeInfo GetSubStruct (string name)
+               public TypeInfo GetStructField (string name)
                {
                        if (struct_info == null)
                                return null;
@@ -1124,29 +1154,49 @@ namespace Mono.CSharp
                        return struct_info.GetStructField (name);
                }
 
+               public static TypeInfo GetTypeInfo (TypeSpec type)
+               {
+                       if (!type.IsStruct)
+                               return simple_type;
+
+                       TypeInfo info;
+                       if (type_hash.TryGetValue (type, out info))
+                               return info;
+
+                       var struct_info = StructInfo.GetStructInfo (type);
+                       if (struct_info != null) {
+                               info = new TypeInfo (struct_info, 0);
+                       } else {
+                               info = simple_type;
+                       }
+
+                       type_hash.Add (type, info);
+                       return info;
+               }
+
                // <summary>
                //   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,
-                                                       "An automatically implemented property `{0}' must be fully assigned before control leaves the constructor. Consider calling default contructor",
-                                                       fb.GetSignatureForError ());
+                               var field = struct_info.Fields [i];
+
+                               if (!branching.IsStructFieldAssigned (vi, field.Name)) {
+                                       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 the default struct contructor from a constructor initializer",
+                                                       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;
                                }
@@ -1157,123 +1207,49 @@ namespace Mono.CSharp
 
                public override string ToString ()
                {
-                       return String.Format ("TypeInfo ({0}:{1}:{2}:{3})",
-                                             Type, Offset, Length, TotalLength);
+                       return String.Format ("TypeInfo ({0}:{1}:{2})",
+                                             Offset, Length, TotalLength);
                }
 
-               protected class StructInfo {
-                       public readonly Type Type;
-                       public readonly FieldInfo[] Fields;
+               class StructInfo
+               {
+                       readonly List<FieldSpec> fields;
                        public readonly TypeInfo[] StructFields;
-                       public readonly int Count;
-                       public readonly int CountPublic;
-                       public readonly int CountNonPublic;
                        public readonly int Length;
                        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;
+                       bool InTransit;
 
-                       // Private constructor.  To save memory usage, we only need to create one instance
-                       // of this class per struct type.
-                       private StructInfo (Type type)
+                       //
+                       // We only need one instance per type
+                       //
+                       StructInfo (TypeSpec type)
                        {
-                               this.Type = type;
-
                                field_type_hash.Add (type, this);
 
-                               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)
-                                                               continue;
-                                                       if ((field.ModFlags & Modifiers.PUBLIC) != 0)
-                                                               public_fields.Add (field.FieldBuilder);
-                                                       else
-                                                               non_public_fields.Add (field.FieldBuilder);
-                                               }
-                                       }
-
-                                       if (tc.Events != null) {
-                                               foreach (Event e in tc.Events) {
-                                                       if ((e.ModFlags & Modifiers.STATIC) != 0)
-                                                               continue;
+                               fields = MemberCache.GetAllFieldsForDefiniteAssignment (type);
 
-                                                       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);
-                                               }
-                                       }
-                                       }
+                               struct_field_hash = new Dictionary<string, TypeInfo> ();
+                               field_hash = new Dictionary<string, int> (fields.Count);
 
-                                       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;
-
-                                       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);
-                               }
-
-                               struct_field_hash = new Hashtable ();
-                               field_hash = new Hashtable ();
-
-                               Length = 0;
-                               StructFields = new TypeInfo [Count];
-                               StructInfo[] sinfo = new StructInfo [Count];
+                               StructFields = new TypeInfo[fields.Count];
+                               StructInfo[] sinfo = new StructInfo[fields.Count];
 
                                InTransit = true;
 
-                               for (int i = 0; i < Count; i++) {
-                                       FieldInfo field = (FieldInfo) Fields [i];
+                               for (int i = 0; i < fields.Count; i++) {
+                                       var field = fields [i];
+
+                                       if (field.MemberType.IsStruct)
+                                               sinfo [i] = GetStructInfo (field.MemberType);
 
-                                       sinfo [i] = GetStructInfo (field.FieldType);
                                        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;
                                        }
@@ -1282,59 +1258,62 @@ namespace Mono.CSharp
                                InTransit = false;
 
                                TotalLength = Length + 1;
-                               for (int i = 0; i < Count; i++) {
-                                       FieldInfo field = (FieldInfo) Fields [i];
+                               for (int i = 0; i < fields.Count; i++) {
+                                       var field = fields [i];
 
                                        if (sinfo [i] == null)
                                                continue;
 
                                        field_hash.Add (field.Name, TotalLength);
 
-                                       HasStructFields = true;
                                        StructFields [i] = new TypeInfo (sinfo [i], TotalLength);
                                        struct_field_hash.Add (field.Name, StructFields [i]);
                                        TotalLength += sinfo [i].TotalLength;
                                }
                        }
 
+                       public int Count {
+                               get {
+                                       return fields.Count;
+                               }
+                       }
+
+                       public List<FieldSpec> Fields {
+                               get {
+                                       return fields;
+                               }
+                       }
+
                        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;
-
-                               if (TypeManager.IsGenericParameter (type))
+                               if (type.BuiltinType > 0)
                                        return null;
 
-                               StructInfo info = (StructInfo) field_type_hash [type];
-                               if (info != null)
+                               StructInfo info;
+                               if (field_type_hash.TryGetValue (type, out info))
                                        return info;
 
                                return new StructInfo (type);
                        }
-
-                       public static StructInfo GetStructInfo (TypeContainer tc)
-                       {
-                               StructInfo info = (StructInfo) field_type_hash [tc.TypeBuilder];
-                               if (info != null)
-                                       return info;
-
-                               return new StructInfo (tc.TypeBuilder);
-                       }
                }
        }
 
@@ -1345,13 +1324,13 @@ namespace Mono.CSharp
        //   it has been assigned or not, but for structs, we need this information for each of its fields.
        // </summary>
        public class VariableInfo {
-               public readonly string Name;
-               public readonly TypeInfo TypeInfo;
+               readonly string Name;
+               readonly TypeInfo TypeInfo;
 
                // <summary>
                //   The bit offset of this variable in the flow vector.
                // </summary>
-               public readonly int Offset;
+               readonly int Offset;
 
                // <summary>
                //   The number of bits this variable needs in the flow vector.
@@ -1365,17 +1344,9 @@ namespace Mono.CSharp
                // </summary>
                public readonly bool IsParameter;
 
-               public readonly LocalInfo LocalInfo;
-
-               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)
+               VariableInfo (string name, TypeSpec type, int offset)
                {
                        this.Name = name;
                        this.Offset = offset;
@@ -1386,16 +1357,14 @@ namespace Mono.CSharp
                        Initialize ();
                }
 
-               protected VariableInfo (VariableInfo parent, TypeInfo type)
+               VariableInfo (VariableInfo parent, TypeInfo type)
                {
                        this.Name = parent.Name;
                        this.TypeInfo = type;
                        this.Offset = parent.Offset + type.Offset;
-                       this.Parent = parent;
                        this.Length = type.TotalLength;
 
                        this.IsParameter = parent.IsParameter;
-                       this.LocalInfo = parent.LocalInfo;
 
                        Initialize ();
                }
@@ -1413,35 +1382,21 @@ namespace Mono.CSharp
                                sub_info = new VariableInfo [0];
                }
 
-               public VariableInfo (LocalInfo local_info, int offset)
-                       : this (local_info.Name, local_info.VariableType, offset)
+               public VariableInfo (LocalVariable local_info, int offset)
+                       : this (local_info.Name, local_info.Type, offset)
                {
-                       this.LocalInfo = local_info;
                        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)
-               {
-                       if (IsAssigned (ec))
-                               return true;
-
-                       Report.Error (165, loc,
-                                     "Use of unassigned local variable `" + Name + "'");
-                       ec.CurrentBranching.SetAssigned (this);
-                       return false;
+                       return !ec.DoFlowAnalysis || ec.CurrentBranching.IsAssigned (this);
                }
 
                public bool IsAssigned (MyBitVector vector)
@@ -1452,69 +1407,54 @@ namespace Mono.CSharp
                        if (vector [Offset])
                                return true;
 
-                       // FIXME: Fix SetFieldAssigned to set the whole range like SetAssigned below. Then, get rid of this stanza
-                       for (VariableInfo parent = Parent; parent != null; parent = parent.Parent) {
-                               if (vector [parent.Offset]) {
-                                       // 'parent' is assigned, but someone forgot to note that all its components are assigned too
-                                       parent.SetAssigned (vector);
-                                       return true;
-                               }
-                       }
-
-                       // Return unless this is a struct.
+                       // Unless this is a struct
                        if (!TypeInfo.IsStruct)
                                return false;
 
-                       // Ok, so each field must be assigned.
-                       for (int i = 0; i < TypeInfo.Length; i++) {
-                               if (!vector [Offset + i + 1])
+                       //
+                       // Following case cannot be handled fully by SetStructFieldAssigned
+                       // because we may encounter following case
+                       // 
+                       // struct A { B b }
+                       // struct B { int value; }
+                       //
+                       // setting a.b.value is propagated only to B's vector and not upwards to possible parents
+                       //
+                       //
+                       // Each field must be assigned
+                       //
+                       for (int i = Offset + 1; i <= TypeInfo.Length + Offset; i++) {
+                               if (!vector[i])
                                        return false;
                        }
 
                        // Ok, now check all fields which are structs.
                        for (int i = 0; i < sub_info.Length; i++) {
-                               VariableInfo sinfo = sub_info [i];
+                               VariableInfo sinfo = sub_info[i];
                                if (sinfo == null)
                                        continue;
 
                                if (!sinfo.IsAssigned (vector))
                                        return false;
                        }
-
+                       
                        vector [Offset] = true;
-                       is_ever_assigned = true;
                        return true;
                }
 
-               public void SetAssigned (EmitContext ec)
-               {
-                       if (ec.DoFlowAnalysis)
-                               ec.CurrentBranching.SetAssigned (this);
-               }
+               public bool IsEverAssigned { get; set; }
 
-               public void SetAssigned (MyBitVector vector)
+               public bool IsStructFieldAssigned (ResolveContext ec, string name)
                {
-                       if (Length == 1)
-                               vector [Offset] = true;
-                       else
-                               vector.SetRange (Offset, Length);
-                       is_ever_assigned = true;
+                       return !ec.DoFlowAnalysis || ec.CurrentBranching.IsStructFieldAssigned (this, name);
                }
 
-               public bool IsFieldAssigned (EmitContext ec, string name, Location loc)
+               public bool IsFullyInitialized (BlockContext bc, Location loc)
                {
-                       if (!ec.DoFlowAnalysis ||
-                               ec.OmitStructFlowAnalysis && TypeInfo.IsStruct ||
-                               ec.CurrentBranching.IsFieldAssigned (this, name))
-                               return true;
-
-                       Report.Error (170, loc,
-                                     "Use of possibly unassigned field `" + name + "'");
-                       ec.CurrentBranching.SetFieldAssigned (this, name);
-                       return false;
+                       return TypeInfo.IsFullyInitialized (bc, this, loc);
                }
 
-               public bool IsFieldAssigned (MyBitVector vector, string field_name)
+               public bool IsStructFieldAssigned (MyBitVector vector, string field_name)
                {
                        int field_idx = TypeInfo.GetFieldIndex (field_name);
 
@@ -1524,26 +1464,65 @@ namespace Mono.CSharp
                        return vector [Offset + field_idx];
                }
 
-               public void SetFieldAssigned (EmitContext ec, string name)
+               public void SetStructFieldAssigned (ResolveContext ec, string name)
                {
                        if (ec.DoFlowAnalysis)
                                ec.CurrentBranching.SetFieldAssigned (this, name);
                }
 
-               public void SetFieldAssigned (MyBitVector vector, string field_name)
+               public void SetAssigned (ResolveContext ec)
+               {
+                       if (ec.DoFlowAnalysis)
+                               ec.CurrentBranching.SetAssigned (this);
+               }
+
+               public void SetAssigned (MyBitVector vector)
+               {
+                       if (Length == 1)
+                               vector[Offset] = true;
+                       else
+                               vector.SetRange (Offset, Length);
+
+                       IsEverAssigned = true;
+               }
+
+               public void SetStructFieldAssigned (MyBitVector vector, string field_name)
                {
+                       if (vector[Offset])
+                               return;
+
                        int field_idx = TypeInfo.GetFieldIndex (field_name);
 
                        if (field_idx == 0)
                                return;
 
-                       vector [Offset + field_idx] = true;
-                       is_ever_assigned = true;
+                       var complex_field = TypeInfo.GetStructField (field_name);
+                       if (complex_field != null) {
+                               vector.SetRange (Offset + complex_field.Offset, complex_field.TotalLength);
+                       } else {
+                               vector[Offset + field_idx] = true;
+                       }
+
+                       IsEverAssigned = true;
+
+                       //
+                       // Each field must be assigned
+                       //
+                       for (int i = Offset + 1; i < TypeInfo.TotalLength + Offset; i++) {
+                               if (!vector[i])
+                                       return;
+                       }
+
+                       //
+                       // Set master struct flag to assigned when all tested struct
+                       // fields have been assigned
+                       //
+                       vector[Offset] = true;
                }
 
-               public VariableInfo GetSubStruct (string name)
+               public VariableInfo GetStructFieldInfo (string fieldName)
                {
-                       TypeInfo type = TypeInfo.GetSubStruct (name);
+                       TypeInfo type = TypeInfo.GetStructField (fieldName);
 
                        if (type == null)
                                return null;
@@ -1571,11 +1550,11 @@ 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)
@@ -1586,7 +1565,7 @@ namespace Mono.CSharp
                        this.Count = Count;
                }
 
-               BitArray MakeShared (int new_count)
+               System.Collections.BitArray MakeShared (int new_count)
                {
                        // Post-condition: vector == null
 
@@ -1642,7 +1621,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;
@@ -1686,7 +1665,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)
@@ -1762,7 +1741,7 @@ namespace Mono.CSharp
                public void SetRange (int offset, int length)
                {
                        if (offset > Count || offset + length > Count)
-                               throw new ArgumentOutOfRangeException ();
+                               throw new ArgumentOutOfRangeException ("flow-analysis");
 
                        if (shared == null && vector == null)
                                return;
@@ -1796,11 +1775,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;
@@ -1808,7 +1787,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)