2002-08-11 Martin Baulig <martin@gnome.org>
[mono.git] / mcs / mcs / ecore.cs
index f38f220b1f54ee1594318ce0cf058fb561f992f3..4b2c3a9c01120a132d5bcc78a40d3094e4125c1e 100755 (executable)
@@ -36,6 +36,31 @@ namespace Mono.CSharp {
                Nothing, 
        }
 
+       /// <remarks>
+       ///   This is used to tell Resolve in which types of expressions we're
+       ///   interested.
+       /// </remarks>
+       [Flags]
+       public enum ResolveFlags {
+               // Returns Value, Variable, PropertyAccess, EventAccess or IndexerAccess.
+               VariableOrValue         = 1,
+
+               // Returns a type expression.
+               Type                    = 2,
+
+               // Returns a method group.
+               MethodGroup             = 4,
+
+               // Allows SimpleNames to be returned.
+               // This is used by MemberAccess to construct long names that can not be
+               // partially resolved (namespace-qualified names for example).
+               SimpleName              = 8,
+
+               // Disable control flow analysis while resolving the expression.
+               // This is used when resolving the instance expression of a field expression.
+               DisableFlowAnalysis     = 16
+       }
+
        //
        // This is just as a hint to AddressOf of what will be done with the
        // address.
@@ -64,12 +89,85 @@ namespace Mono.CSharp {
                void AddressOf (EmitContext ec, AddressOp mode);
        }
 
+       /// <summary>
+       ///   This interface is implemented by variables
+       /// </summary>
+       public interface IVariable {
+               /// <summary>
+               ///   Checks whether the variable has already been assigned at
+               ///   the current position of the method's control flow and
+               ///   reports an appropriate error message if not.
+               ///
+               ///   If the variable is a struct, then this call checks whether
+               ///   all of its fields (including all private ones) have been
+               ///   assigned.
+               /// </summary>
+               bool IsAssigned (EmitContext ec, Location loc);
+
+               /// <summary>
+               ///   Checks whether field `name' in this struct has been assigned.
+               /// </summary>
+               bool IsFieldAssigned (EmitContext ec, string name, Location loc);
+
+               /// <summary>
+               ///   Tells the flow analysis code that the variable has already
+               ///   been assigned at the current code position.
+               ///
+               ///   If the variable is a struct, this call marks all its fields
+               ///   (including private fields) as being assigned.
+               /// </summary>
+               void SetAssigned (EmitContext ec);
+
+               /// <summary>
+               ///   Tells the flow analysis code that field `name' in this struct
+               ///   has already been assigned atthe current code position.
+               /// </summary>
+               void SetFieldAssigned (EmitContext ec, string name);
+       }
+
+       /// <summary>
+       ///   This interface denotes an expression which evaluates to a member
+       ///   of a struct or a class.
+       /// </summary>
+       public interface IMemberExpr
+       {
+               /// <summary>
+               ///   The name of this member.
+               /// </summary>
+               string Name {
+                       get;
+               }
+
+               /// <summary>
+               ///   Whether this is an instance member.
+               /// </summary>
+               bool IsInstance {
+                       get;
+               }
+
+               /// <summary>
+               ///   Whether this is a static member.
+               /// </summary>
+               bool IsStatic {
+                       get;
+               }
+
+               /// <summary>
+               ///   The instance expression associated with this member, if it's a
+               ///   non-static member.
+               /// </summary>
+               Expression InstanceExpression {
+                       get; set;
+               }
+       }
+
        /// <remarks>
        ///   Base class for expressions
        /// </remarks>
        public abstract class Expression {
                public ExprClass eclass;
-               protected Type      type;
+               protected Type type;
+               protected Location loc;
                
                public Type Type {
                        get {
@@ -81,25 +179,42 @@ namespace Mono.CSharp {
                        }
                }
 
+               public Location Location {
+                       get {
+                               return loc;
+                       }
+               }
+
                /// <summary>
                ///   Utility wrapper routine for Error, just to beautify the code
                /// </summary>
-               static protected void Error (int error, string s)
+               public void Error (int error, string s)
                {
-                       Report.Error (error, s);
+                       if (!Location.IsNull (loc))
+                               Report.Error (error, loc, s);
+                       else
+                               Report.Error (error, s);
                }
 
-               static protected void Error (int error, Location loc, string s)
+               /// <summary>
+               ///   Utility wrapper routine for Warning, just to beautify the code
+               /// </summary>
+               public void Warning (int warning, string s)
                {
-                       Report.Error (error, loc, s);
+                       if (!Location.IsNull (loc))
+                               Report.Warning (warning, loc, s);
+                       else
+                               Report.Warning (warning, s);
                }
-               
+
                /// <summary>
-               ///   Utility wrapper routine for Warning, just to beautify the code
+               ///   Utility wrapper routine for Warning, only prints the warning if
+               ///   warnings of level `level' are enabled.
                /// </summary>
-               static protected void Warning (int warning, string s)
+               public void Warning (int warning, int level, string s)
                {
-                       Report.Warning (warning, s);
+                       if (level <= RootContext.WarningLevel)
+                               Warning (warning, s);
                }
 
                static public void Error_CannotConvertType (Location loc, Type source, Type target)
@@ -151,72 +266,95 @@ namespace Mono.CSharp {
                ///   Currently Resolve wraps DoResolve to perform sanity
                ///   checking and assertion checking on what we expect from Resolve.
                /// </remarks>
-               public Expression Resolve (EmitContext ec)
+               public Expression Resolve (EmitContext ec, ResolveFlags flags)
                {
-                       Expression e = DoResolve (ec);
+                       Expression e;
 
-                       if (e != null){
+                       bool old_do_flow_analysis = ec.DoFlowAnalysis;
+                       if ((flags & ResolveFlags.DisableFlowAnalysis) != 0)
+                               ec.DoFlowAnalysis = false;
 
-                               if (e is SimpleName){
-                                       SimpleName s = (SimpleName) e;
+                       if (this is SimpleName)
+                               e = ((SimpleName) this).DoResolveAllowStatic (ec);
+                       else 
+                               e = DoResolve (ec);
+
+                       ec.DoFlowAnalysis = old_do_flow_analysis;
+
+                       if (e == null)
+                               return null;
+
+                       if (e is SimpleName){
+                               SimpleName s = (SimpleName) e;
 
+                               if ((flags & ResolveFlags.SimpleName) == 0) {
                                        Report.Error (
-                                                     103, s.Location,
-                                                     "The name `" + s.Name + "' could not be found in `" +
-                                                     ec.DeclSpace.Name + "'");
+                                               103, loc,
+                                               "The name `" + s.Name + "' could not be found in `" +
+                                               ec.DeclSpace.Name + "'");
                                        return null;
                                }
-                               
-                               if (e.eclass == ExprClass.Invalid)
-                                       throw new Exception ("Expression " + e.GetType () +
-                                                            " ExprClass is Invalid after resolve");
 
-                               if (e.eclass != ExprClass.MethodGroup)
-                                       if (e.type == null)
-                                               throw new Exception (
-                                                       "Expression " + e.GetType () +
-                                                       " did not set its type after Resolve\n" +
-                                                       "called from: " + this.GetType ());
+                               return s;
                        }
 
-                       return e;
-               }
+                       if ((e is TypeExpr) || (e is ComposedCast)) {
+                               if ((flags & ResolveFlags.Type) == 0) {
+                                       e.Error118 (flags);
+                                       return null;
+                               }
 
-               /// <summary>
-               ///   Performs expression resolution and semantic analysis, but
-               ///   allows SimpleNames to be returned.
-               /// </summary>
-               ///
-               /// <remarks>
-               ///   This is used by MemberAccess to construct long names that can not be
-               ///   partially resolved (namespace-qualified names for example).
-               /// </remarks>
-               public Expression ResolveWithSimpleName (EmitContext ec)
-               {
-                       Expression e;
+                               return e;
+                       }
 
-                       if (this is SimpleName)
-                               e = ((SimpleName) this).DoResolveAllowStatic (ec);
-                       else 
-                               e = DoResolve (ec);
+                       switch (e.eclass) {
+                       case ExprClass.Type:
+                               if ((flags & ResolveFlags.VariableOrValue) == 0) {
+                                       e.Error118 (flags);
+                                       return null;
+                               }
+                               break;
 
-                       if (e != null){
-                               if (e is SimpleName)
-                                       return e;
+                       case ExprClass.MethodGroup:
+                               if ((flags & ResolveFlags.MethodGroup) == 0) {
+                                       ((MethodGroupExpr) e).ReportUsageError ();
+                                       return null;
+                               }
+                               break;
 
-                               if (e.eclass == ExprClass.Invalid)
-                                       throw new Exception ("Expression " + e +
-                                                            " ExprClass is Invalid after resolve");
+                       case ExprClass.Value:
+                       case ExprClass.Variable:
+                       case ExprClass.PropertyAccess:
+                       case ExprClass.EventAccess:
+                       case ExprClass.IndexerAccess:
+                               if ((flags & ResolveFlags.VariableOrValue) == 0) {
+                                       e.Error118 (flags);
+                                       return null;
+                               }
+                               break;
 
-                               if (e.eclass != ExprClass.MethodGroup)
-                                       if (e.type == null)
-                                               throw new Exception ("Expression " + e +
-                                                                    " did not set its type after Resolve");
+                       default:
+                               throw new Exception ("Expression " + e.GetType () +
+                                                    " ExprClass is Invalid after resolve");
                        }
 
+                       if (e.type == null)
+                               throw new Exception (
+                                       "Expression " + e.GetType () +
+                                       " did not set its type after Resolve\n" +
+                                       "called from: " + this.GetType ());
+
                        return e;
                }
-               
+
+               /// <summary>
+               ///   Resolves an expression and performs semantic analysis on it.
+               /// </summary>
+               public Expression Resolve (EmitContext ec)
+               {
+                       return Resolve (ec, ResolveFlags.VariableOrValue);
+               }
+
                /// <summary>
                ///   Resolves an expression for LValue assignment
                /// </summary>
@@ -234,7 +372,7 @@ namespace Mono.CSharp {
                                        SimpleName s = (SimpleName) e;
 
                                        Report.Error (
-                                               103, s.Location,
+                                               103, loc,
                                                "The name `" + s.Name + "' could not be found in `" +
                                                ec.DeclSpace.Name + "'");
                                        return null;
@@ -244,10 +382,14 @@ namespace Mono.CSharp {
                                        throw new Exception ("Expression " + e +
                                                             " ExprClass is Invalid after resolve");
 
-                               if (e.eclass != ExprClass.MethodGroup)
-                                       if (e.type == null)
-                                               throw new Exception ("Expression " + e +
-                                                                    " did not set its type after Resolve");
+                               if (e.eclass == ExprClass.MethodGroup) {
+                                       ((MethodGroupExpr) e).ReportUsageError ();
+                                       return null;
+                               }
+
+                               if (e.type == null)
+                                       throw new Exception ("Expression " + e +
+                                                            " did not set its type after Resolve");
                        }
 
                        return e;
@@ -336,7 +478,7 @@ namespace Mono.CSharp {
                        else if (mi is PropertyInfo)
                                return new PropertyExpr ((PropertyInfo) mi, loc);
                        else if (mi is Type){
-                               return new TypeExpr ((System.Type) mi);
+                               return new TypeExpr ((System.Type) mi, loc);
                        }
 
                        return null;
@@ -373,7 +515,14 @@ namespace Mono.CSharp {
                public static Expression MemberLookup (EmitContext ec, Type t, string name,
                                                       MemberTypes mt, BindingFlags bf, Location loc)
                {
-                       MemberInfo [] mi = TypeManager.MemberLookup (ec.ContainerType, t, mt, bf, name);
+                       return MemberLookup (ec, ec.ContainerType, t, name, mt, bf, loc);
+               }
+
+               public static Expression MemberLookup (EmitContext ec, Type invocation_type, Type t,
+                                                      string name, MemberTypes mt, BindingFlags bf,
+                                                      Location loc)
+               {
+                       MemberInfo [] mi = TypeManager.MemberLookup (invocation_type, t, mt, bf, name);
 
                        if (mi == null)
                                return null;
@@ -404,12 +553,14 @@ namespace Mono.CSharp {
 
                public static Expression MemberLookup (EmitContext ec, Type t, string name, Location loc)
                {
-                       return MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
+                       return MemberLookup (ec, ec.ContainerType, t, name,
+                                            AllMemberTypes, AllBindingFlags, loc);
                }
 
                public static Expression MethodLookup (EmitContext ec, Type t, string name, Location loc)
                {
-                       return MemberLookup (ec, t, name, MemberTypes.Method, AllBindingFlags, loc);
+                       return MemberLookup (ec, ec.ContainerType, t, name,
+                                            MemberTypes.Method, AllBindingFlags, loc);
                }
 
                /// <summary>
@@ -431,7 +582,7 @@ namespace Mono.CSharp {
 
                        int errors = Report.Errors;
 
-                       e = MemberLookup (ec, t, name, mt, bf, loc);
+                       e = MemberLookup (ec, ec.ContainerType, t, name, mt, bf, loc);
 
                        if (e != null)
                                return e;
@@ -565,7 +716,7 @@ namespace Mono.CSharp {
 
                        args.Add (new Argument (expr, Argument.AType.Expression));
 
-                       Expression ne = new New (new TypeExpr (target), args, new Location (-1));
+                       Expression ne = new New (new TypeExpr (target, Location.Null), args, Location.Null);
 
                        return ne.Resolve (ec);
                }
@@ -1418,7 +1569,7 @@ namespace Mono.CSharp {
                                return null;
 
                        Expression e;
-                       e =  new UserCast ((MethodInfo) method, source);
+                       e =  new UserCast ((MethodInfo) method, source, loc);
                        if (e.Type != target){
                                if (!look_for_explicit)
                                        e = ConvertImplicitStandard (ec, e, target, loc);
@@ -1564,7 +1715,7 @@ namespace Mono.CSharp {
                                TypeManager.CSharpName (source) + "' to `" +
                                TypeManager.CSharpName (target) + "'";
 
-                       Error (29, loc, msg);
+                       Report.Error (29, loc, msg);
                }
 
                /// <summary>
@@ -1582,9 +1733,9 @@ namespace Mono.CSharp {
                                return e;
 
                        if (source is DoubleLiteral && target_type == TypeManager.float_type){
-                               Error (664, loc,
-                                      "Double literal cannot be implicitly converted to " +
-                                      "float type, use F suffix to create a float literal");
+                               Report.Error (664, loc,
+                                             "Double literal cannot be implicitly converted to " +
+                                             "float type, use F suffix to create a float literal");
                        }
                        
                        Error_CannotConvertImplicit (loc, source.Type, target_type);
@@ -2153,17 +2304,52 @@ namespace Mono.CSharp {
                /// <summary>
                ///   Reports that we were expecting `expr' to be of class `expected'
                /// </summary>
-               public static void Error118 (Location loc, Expression expr, string expected)
+               public void Error118 (string expected)
                {
                        string kind = "Unknown";
                        
-                       if (expr != null)
-                               kind = ExprClassName (expr.eclass);
+                       kind = ExprClassName (eclass);
 
-                       Error (118, loc, "Expression denotes a `" + kind +
+                       Error (118, "Expression denotes a `" + kind +
                               "' where a `" + expected + "' was expected");
                }
 
+               public void Error118 (ResolveFlags flags)
+               {
+                       ArrayList valid = new ArrayList (10);
+
+                       if ((flags & ResolveFlags.VariableOrValue) != 0) {
+                               valid.Add ("variable");
+                               valid.Add ("value");
+                       }
+
+                       if ((flags & ResolveFlags.Type) != 0)
+                               valid.Add ("type");
+
+                       if ((flags & ResolveFlags.MethodGroup) != 0)
+                               valid.Add ("method group");
+
+                       if ((flags & ResolveFlags.SimpleName) != 0)
+                               valid.Add ("simple name");
+
+                       if (valid.Count == 0)
+                               valid.Add ("unknown");
+
+                       StringBuilder sb = new StringBuilder ();
+                       for (int i = 0; i < valid.Count; i++) {
+                               if (i > 0)
+                                       sb.Append (", ");
+                               else if (i == valid.Count)
+                                       sb.Append (" or ");
+                               sb.Append (valid [i]);
+                       }
+
+                       string kind = ExprClassName (eclass);
+
+                       Error (119, "Expression denotes a `" + kind + "' where " +
+                              "a `" + sb.ToString () + "' was expected");
+               }
+               
                static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
                {
                        Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
@@ -3145,51 +3331,38 @@ namespace Mono.CSharp {
        /// </remarks>
        public class SimpleName : Expression {
                public readonly string Name;
-               public readonly Location Location;
                
                public SimpleName (string name, Location l)
                {
                        Name = name;
-                       Location = l;
+                       loc = l;
                }
 
-               public static void Error_ObjectRefRequired (Location l, string name)
+               public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
                {
-                       Report.Error (
-                               120, l,
-                               "An object reference is required " +
-                               "for the non-static field `"+name+"'");
+                       if (ec.IsFieldInitializer)
+                               Report.Error (
+                                       236, l,
+                                       "A field initializer cannot reference the non-static field, " +
+                                       "method or property `"+name+"'");
+                       else
+                               Report.Error (
+                                       120, l,
+                                       "An object reference is required " +
+                                       "for the non-static field `"+name+"'");
                }
                
                //
                // Checks whether we are trying to access an instance
                // property, method or field from a static body.
                //
-               Expression MemberStaticCheck (Expression e)
+               Expression MemberStaticCheck (EmitContext ec, Expression e)
                {
-                       if (e is FieldExpr){
-                               FieldInfo fi = ((FieldExpr) e).FieldInfo;
+                       if (e is IMemberExpr){
+                               IMemberExpr member = (IMemberExpr) e;
                                
-                               if (!fi.IsStatic){
-                                       Error_ObjectRefRequired (Location, Name);
-                                       return null;
-                               }
-                       } else if (e is MethodGroupExpr){
-                               MethodGroupExpr mg = (MethodGroupExpr) e;
-
-                               if (!mg.RemoveInstanceMethods ()){
-                                       Error_ObjectRefRequired (Location, mg.Methods [0].Name);
-                                       return null;
-                               }
-                               return e;
-                       } else if (e is PropertyExpr){
-                               if (!((PropertyExpr) e).IsStatic){
-                                       Error_ObjectRefRequired (Location, Name);
-                                       return null;
-                               }
-                       } else if (e is EventExpr) {
-                               if (!((EventExpr) e).IsStatic) {
-                                       Error_ObjectRefRequired (Location, Name);
+                               if (!member.IsStatic){
+                                       Error_ObjectRefRequired (ec, loc, Name);
                                        return null;
                                }
                        }
@@ -3242,14 +3415,33 @@ namespace Mono.CSharp {
                                if (current_block != null && current_block.IsVariableDefined (Name)){
                                        LocalVariableReference var;
                                        
-                                       var = new LocalVariableReference (ec.CurrentBlock, Name, Location);
+                                       var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
 
                                        if (right_side != null)
                                                return var.ResolveLValue (ec, right_side);
                                        else
                                                return var.Resolve (ec);
                                }
-                       
+
+                               if (current_block != null){
+                                       int idx = -1;
+                                       Parameter par = null;
+                                       Parameters pars = current_block.Parameters;
+                                       if (pars != null)
+                                               par = pars.GetParameterByName (Name, out idx);
+
+                                       if (par != null) {
+                                               ParameterReference param;
+                                       
+                                               param = new ParameterReference (pars, idx, Name, loc);
+
+                                               if (right_side != null)
+                                                       return param.ResolveLValue (ec, right_side);
+                                               else
+                                                       return param.Resolve (ec);
+                                       }
+                               }
+
                                //
                                // Stage 2: Lookup members 
                                //
@@ -3264,7 +3456,7 @@ namespace Mono.CSharp {
                                        if (lookup_ds.TypeBuilder == null)
                                                break;
 
-                                       e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, Location);
+                                       e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
                                        if (e != null)
                                                break;
 
@@ -3278,7 +3470,7 @@ namespace Mono.CSharp {
                                } while (lookup_ds != null);
                                
                                if (e == null && ec.ContainerType != null)
-                                       e = MemberLookup (ec, ec.ContainerType, Name, Location);
+                                       e = MemberLookup (ec, ec.ContainerType, Name, loc);
                        }
 
                        // Continuation of stage 2
@@ -3290,8 +3482,8 @@ namespace Mono.CSharp {
                                Type t;
                                string alias_value;
 
-                               if ((t = RootContext.LookupType (ds, Name, true, Location)) != null)
-                                       return new TypeExpr (t);
+                               if ((t = RootContext.LookupType (ds, Name, true, loc)) != null)
+                                       return new TypeExpr (t, loc);
                                
                                //
                                // Stage 2 part b: Lookup up if we are an alias to a type
@@ -3304,25 +3496,25 @@ namespace Mono.CSharp {
                                alias_value = ec.DeclSpace.LookupAlias (Name);
                                
                                if (Name.IndexOf ('.') == -1 && alias_value != null) {
-                                       if ((t = RootContext.LookupType (ds, alias_value, true, Location))
+                                       if ((t = RootContext.LookupType (ds, alias_value, true, loc))
                                            != null)
-                                               return new TypeExpr (t);
+                                               return new TypeExpr (t, loc);
                                        
                                // we have alias value, but it isn't Type, so try if it's namespace
-                                       return new SimpleName (alias_value, Location);
+                                       return new SimpleName (alias_value, loc);
                                }
                                
                                if (ec.ResolvingTypeTree){
                                        Type dt = ec.DeclSpace.FindType (Name);
                                        if (dt != null)
-                                               return new TypeExpr (dt);
+                                               return new TypeExpr (dt, loc);
                                }
                                
                                // No match, maybe our parent can compose us
                                // into something meaningful.
                                return this;
                        }
-                       
+
                        //
                        // Stage 2 continues here. 
                        // 
@@ -3331,149 +3523,37 @@ namespace Mono.CSharp {
 
                        if (ec.OnlyLookupTypes)
                                return null;
-                       
-                       if (e is FieldExpr){
-                               FieldExpr fe = (FieldExpr) e;
-                               FieldInfo fi = fe.FieldInfo;
-
-                               if (fi.FieldType.IsPointer && !ec.InUnsafe){
-                                       UnsafeError (Location);
-                               }
-                               
-                               if (ec.IsStatic){
-                                       if (!allow_static && !fi.IsStatic){
-                                               Error_ObjectRefRequired (Location, Name);
-                                               return null;
-                                       }
-                               } else {
-                                       // If we are not in static code and this
-                                       // field is not static, set the instance to `this'.
-
-                                       if (!fi.IsStatic)
-                                               fe.InstanceExpression = ec.This;
-                               }
-
-                               
-                               if (fi is FieldBuilder) {
-                                       Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
-                                       
-                                       if (c != null) {
-                                               object o = c.LookupConstantValue (ec);
-                                               if (o == null)
-                                                       return null;
-                                               object real_value = ((Constant)c.Expr).GetValue ();
-                                               return Constantify (real_value, fi.FieldType);
-                                       }
-                               }
 
-                               if (fi.IsLiteral) {
-                                       Type t = fi.FieldType;
-                                       Type decl_type = fi.DeclaringType;
-                                       object o;
-
-                                       if (fi is FieldBuilder)
-                                               o = TypeManager.GetValue ((FieldBuilder) fi);
-                                       else
-                                               o = fi.GetValue (fi);
-                                       
-                                       if (decl_type.IsSubclassOf (TypeManager.enum_type)) {
-                                               Expression enum_member = MemberLookup (
-                                                       ec, decl_type, "value__", MemberTypes.Field,
-                                                       AllBindingFlags, Location); 
-
-                                               Enum en = TypeManager.LookupEnum (decl_type);
-
-                                               Constant c;
-                                               if (en != null)
-                                                       c = Constantify (o, en.UnderlyingType);
-                                               else 
-                                                       c = Constantify (o, enum_member.Type);
-                                               
-                                               return new EnumConstant (c, decl_type);
-                                       }
-                                       
-                                       Expression exp = Constantify (o, t);
-                               }
-                                       
-                               return e;
-                       }
-
-                       if (e is PropertyExpr) {
-                               PropertyExpr pe = (PropertyExpr) e;
+                       if (e is IMemberExpr) {
+                               e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
+                               if (e == null)
+                                       return null;
 
-                               if (ec.IsStatic){
-                                       if (allow_static)
-                                               return e;
+                               IMemberExpr me = e as IMemberExpr;
+                               if (me == null)
+                                       return e;
 
-                                       return MemberStaticCheck (e);
-                               } else {
-                                       // If we are not in static code and this
-                                       // field is not static, set the instance to `this'.
+                               // This fails if ResolveMemberAccess() was unable to decide whether
+                               // it's a field or a type of the same name.
+                               if (!me.IsStatic && (me.InstanceExpression == null))
+                                       return e;
 
-                                       if (!pe.IsStatic)
-                                               pe.InstanceExpression = ec.This;
-                               }
+                               if (right_side != null)
+                                       e = e.DoResolveLValue (ec, right_side);
+                               else
+                                       e = e.DoResolve (ec);
 
-                               return e;
+                               return e;                               
                        }
 
-                       if (e is EventExpr) {
-                               //
-                               // If the event is local to this class, we transform ourselves into
-                               // a FieldExpr
-                               //
-                               EventExpr ee = (EventExpr) e;
-
-                               Expression ml = MemberLookup (
-                                       ec, ec.ContainerType, ee.EventInfo.Name,
-                                       MemberTypes.Event, AllBindingFlags | BindingFlags.DeclaredOnly, Location);
-
-                               if (ml != null) {
-                                       MemberInfo mi = GetFieldFromEvent ((EventExpr) ml);
-
-                                       if (mi == null) {
-                                               //
-                                               // If this happens, then we have an event with its own
-                                               // accessors and private field etc so there's no need
-                                               // to transform ourselves : we should instead flag an error
-                                               //
-                                               Assign.error70 (ee.EventInfo, Location);
-                                               return null;
-                                       }
-
-                                       ml = ExprClassFromMemberInfo (ec, mi, Location);
-                                       
-                                       if (ml == null) {
-                                               Report.Error (-200, Location, "Internal error!!");
-                                               return null;
-                                       }
-
-                                       Expression instance_expr;
-                                       
-                                       FieldInfo fi = ((FieldExpr) ml).FieldInfo;
-
-                                       if (fi.IsStatic)
-                                               instance_expr = null;
-                                       else {
-                                               instance_expr = ec.This;
-                                               instance_expr = instance_expr.Resolve (ec);
-                                       } 
-                                       
-                                       return MemberAccess.ResolveMemberAccess (ec, ml, instance_expr, Location, null);
-                               }
-                       }
-                               
-                       
-                       if (ec.IsStatic){
+                       if (ec.IsStatic || ec.IsFieldInitializer){
                                if (allow_static)
                                        return e;
 
-                               return MemberStaticCheck (e);
+                               return MemberStaticCheck (ec, e);
                        } else
                                return e;
                }
-
-               
                
                public override void Emit (EmitContext ec)
                {
@@ -3482,7 +3562,7 @@ namespace Mono.CSharp {
                        // find the name as a namespace
                        //
 
-                       Error (103, Location, "The name `" + Name +
+                       Error (103, "The name `" + Name +
                               "' does not exist in the class `" +
                               ec.DeclSpace.Name + "'");
                }
@@ -3497,10 +3577,11 @@ namespace Mono.CSharp {
        ///   Fully resolved expression that evaluates to a type
        /// </summary>
        public class TypeExpr : Expression {
-               public TypeExpr (Type t)
+               public TypeExpr (Type t, Location l)
                {
                        Type = t;
                        eclass = ExprClass.Type;
+                       loc = l;
                }
 
                override public Expression DoResolve (EmitContext ec)
@@ -3522,7 +3603,7 @@ namespace Mono.CSharp {
        public class TypeExpression : TypeExpr {
                string name;
                
-               public TypeExpression (string name) : base (null)
+               public TypeExpression (string name) : base (null, Location.Null)
                {
                        this.name = name;
                }
@@ -3550,9 +3631,8 @@ namespace Mono.CSharp {
        ///  
        ///   This is a fully resolved expression that evaluates to a type
        /// </summary>
-       public class MethodGroupExpr : Expression {
+       public class MethodGroupExpr : Expression, IMemberExpr {
                public MethodBase [] Methods;
-               Location loc;
                Expression instance_expression = null;
                
                public MethodGroupExpr (MemberInfo [] mi, Location l)
@@ -3596,6 +3676,32 @@ namespace Mono.CSharp {
                                instance_expression = value;
                        }
                }
+
+               public string Name {
+                       get {
+                               return Methods [0].Name;
+                       }
+               }
+
+               public bool IsInstance {
+                       get {
+                               foreach (MethodBase mb in Methods)
+                                       if (!mb.IsStatic)
+                                               return true;
+
+                               return false;
+                       }
+               }
+
+               public bool IsStatic {
+                       get {
+                               foreach (MethodBase mb in Methods)
+                                       if (mb.IsStatic)
+                                               return true;
+
+                               return false;
+                       }
+               }
                
                override public Expression DoResolve (EmitContext ec)
                {
@@ -3653,10 +3759,9 @@ namespace Mono.CSharp {
        /// <summary>
        ///   Fully resolved expression that evaluates to a Field
        /// </summary>
-       public class FieldExpr : Expression, IAssignMethod, IMemoryLocation {
+       public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr {
                public readonly FieldInfo FieldInfo;
-               public Expression InstanceExpression;
-               Location loc;
+               Expression instance_expr;
                
                public FieldExpr (FieldInfo fi, Location l)
                {
@@ -3666,20 +3771,57 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
+               public string Name {
+                       get {
+                               return FieldInfo.Name;
+                       }
+               }
+
+               public bool IsInstance {
+                       get {
+                               return !FieldInfo.IsStatic;
+                       }
+               }
+
+               public bool IsStatic {
+                       get {
+                               return FieldInfo.IsStatic;
+                       }
+               }
+
+               public Expression InstanceExpression {
+                       get {
+                               return instance_expr;
+                       }
+
+                       set {
+                               instance_expr = value;
+                       }
+               }
+
                override public Expression DoResolve (EmitContext ec)
                {
                        if (!FieldInfo.IsStatic){
-                               if (InstanceExpression == null){
+                               if (instance_expr == null){
                                        throw new Exception ("non-static FieldExpr without instance var\n" +
                                                             "You have to assign the Instance variable\n" +
                                                             "Of the FieldExpr to set this\n");
                                }
 
-                               InstanceExpression = InstanceExpression.Resolve (ec);
-                               if (InstanceExpression == null)
+                               // Resolve the field's instance expression while flow analysis is turned
+                               // off: when accessing a field "a.b", we must check whether the field
+                               // "a.b" is initialized, not whether the whole struct "a" is initialized.
+                               instance_expr = instance_expr.Resolve (ec, ResolveFlags.VariableOrValue |
+                                                                      ResolveFlags.DisableFlowAnalysis);
+                               if (instance_expr == null)
                                        return null;
                        }
 
+                       // If the instance expression is a local variable or parameter.
+                       IVariable var = instance_expr as IVariable;
+                       if ((var != null) && !var.IsFieldAssigned (ec, FieldInfo.Name, loc))
+                               return null;
+
                        return this;
                }
 
@@ -3699,6 +3841,10 @@ namespace Mono.CSharp {
                
                override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
                {
+                       IVariable var = instance_expr as IVariable;
+                       if (var != null)
+                               var.SetFieldAssigned (ec, FieldInfo.Name);
+
                        Expression e = DoResolve (ec);
 
                        if (e == null)
@@ -3723,7 +3869,7 @@ namespace Mono.CSharp {
                {
                        ILGenerator ig = ec.ig;
                        bool is_volatile = false;
-                               
+
                        if (FieldInfo is FieldBuilder){
                                FieldBase f = TypeManager.GetField (FieldInfo);
 
@@ -3739,23 +3885,23 @@ namespace Mono.CSharp {
                                
                                ig.Emit (OpCodes.Ldsfld, FieldInfo);
                        } else {
-                               if (InstanceExpression.Type.IsValueType){
+                               if (instance_expr.Type.IsValueType){
                                        IMemoryLocation ml;
                                        LocalTemporary tempo = null;
                                        
-                                       if (!(InstanceExpression is IMemoryLocation)){
+                                       if (!(instance_expr is IMemoryLocation)){
                                                tempo = new LocalTemporary (
-                                                       ec, InstanceExpression.Type);
+                                                       ec, instance_expr.Type);
 
                                                InstanceExpression.Emit (ec);
                                                tempo.Store (ec);
                                                ml = tempo;
                                        } else
-                                               ml = (IMemoryLocation) InstanceExpression;
+                                               ml = (IMemoryLocation) instance_expr;
 
                                        ml.AddressOf (ec, AddressOp.Load);
                                } else 
-                                       InstanceExpression.Emit (ec);
+                                       instance_expr.Emit (ec);
 
                                if (is_volatile)
                                        ig.Emit (OpCodes.Volatile);
@@ -3777,7 +3923,7 @@ namespace Mono.CSharp {
                        }
                        
                        if (!is_static){
-                               Expression instance = InstanceExpression;
+                               Expression instance = instance_expr;
 
                                if (instance.Type.IsValueType){
                                        if (instance is IMemoryLocation){
@@ -3853,10 +3999,10 @@ namespace Mono.CSharp {
                        if (FieldInfo.IsStatic)
                                ig.Emit (OpCodes.Ldsflda, FieldInfo);
                        else {
-                               if (InstanceExpression is IMemoryLocation)
-                                       ((IMemoryLocation)InstanceExpression).AddressOf (ec, AddressOp.LoadStore);
+                               if (instance_expr is IMemoryLocation)
+                                       ((IMemoryLocation)instance_expr).AddressOf (ec, AddressOp.LoadStore);
                                else
-                                       InstanceExpression.Emit (ec);
+                                       instance_expr.Emit (ec);
                                ig.Emit (OpCodes.Ldflda, FieldInfo);
                        }
                }
@@ -3869,20 +4015,19 @@ namespace Mono.CSharp {
        ///   This is not an LValue because we need to re-write the expression, we
        ///   can not take data from the stack and store it.  
        /// </summary>
-       public class PropertyExpr : ExpressionStatement, IAssignMethod {
+       public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
                public readonly PropertyInfo PropertyInfo;
-               public readonly bool IsStatic;
                public bool IsBase;
                MethodInfo [] Accessors;
-               Location loc;
+               bool is_static;
                
                Expression instance_expr;
-               
+
                public PropertyExpr (PropertyInfo pi, Location l)
                {
                        PropertyInfo = pi;
                        eclass = ExprClass.PropertyAccess;
-                       IsStatic = false;
+                       is_static = false;
                        loc = l;
                        Accessors = TypeManager.GetAccessors (pi);
 
@@ -3890,7 +4035,7 @@ namespace Mono.CSharp {
                                foreach (MethodInfo mi in Accessors){
                                        if (mi != null)
                                                if (mi.IsStatic)
-                                                       IsStatic = true;
+                                                       is_static = true;
                                }
                        else
                                Accessors = new MethodInfo [2];
@@ -3898,6 +4043,24 @@ namespace Mono.CSharp {
                        type = TypeManager.TypeToCoreType (pi.PropertyType);
                }
 
+               public string Name {
+                       get {
+                               return PropertyInfo.Name;
+                       }
+               }
+
+               public bool IsInstance {
+                       get {
+                               return !is_static;
+                       }
+               }
+
+               public bool IsStatic {
+                       get {
+                               return is_static;
+                       }
+               }
+               
                //
                // The instance expression associated with this expression
                //
@@ -3933,7 +4096,18 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       type = PropertyInfo.PropertyType;
+                       return this;
+               }
+
+               override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
+               {
+                       if (!PropertyInfo.CanWrite){
+                               Report.Error (154, loc, 
+                                             "The property `" + PropertyInfo.Name +
+                                             "' can not be used in " +
+                                             "this context because it lacks a set accessor");
+                               return null;
+                       }
 
                        return this;
                }
@@ -3985,13 +4159,11 @@ namespace Mono.CSharp {
        /// <summary>
        ///   Fully resolved expression that evaluates to an Event
        /// </summary>
-       public class EventExpr : Expression {
+       public class EventExpr : Expression, IMemberExpr {
                public readonly EventInfo EventInfo;
-               Location loc;
-               public Expression InstanceExpression;
-
-               public readonly bool IsStatic;
+               public Expression instance_expr;
 
+               bool is_static;
                MethodInfo add_accessor, remove_accessor;
                
                public EventExpr (EventInfo ei, Location loc)
@@ -4004,7 +4176,7 @@ namespace Mono.CSharp {
                        remove_accessor = TypeManager.GetRemoveMethod (ei);
                        
                        if (add_accessor.IsStatic || remove_accessor.IsStatic)
-                                       IsStatic = true;
+                               is_static = true;
 
                        if (EventInfo is MyEventBuilder)
                                type = ((MyEventBuilder) EventInfo).EventType;
@@ -4012,6 +4184,34 @@ namespace Mono.CSharp {
                                type = EventInfo.EventHandlerType;
                }
 
+               public string Name {
+                       get {
+                               return EventInfo.Name;
+                       }
+               }
+
+               public bool IsInstance {
+                       get {
+                               return !is_static;
+                       }
+               }
+
+               public bool IsStatic {
+                       get {
+                               return is_static;
+                       }
+               }
+
+               public Expression InstanceExpression {
+                       get {
+                               return instance_expr;
+                       }
+
+                       set {
+                               instance_expr = value;
+                       }
+               }
+
                public override Expression DoResolve (EmitContext ec)
                {
                        // We are born fully resolved
@@ -4034,10 +4234,10 @@ namespace Mono.CSharp {
                        
                        if (((Binary) source).Oper == Binary.Operator.Addition)
                                Invocation.EmitCall (
-                                       ec, false, IsStatic, InstanceExpression, add_accessor, args, loc);
+                                       ec, false, IsStatic, instance_expr, add_accessor, args, loc);
                        else
                                Invocation.EmitCall (
-                                       ec, false, IsStatic, InstanceExpression, remove_accessor, args, loc);
+                                       ec, false, IsStatic, instance_expr, remove_accessor, args, loc);
                }
        }
 }