2002-10-08 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / mcs / ecore.cs
index f02bf11d1b3c17db02d123fc1294ec8792ad9ff4..027ea74aa31fb171d13700d5f9b388ceefff1e03 100755 (executable)
@@ -54,7 +54,14 @@ namespace Mono.CSharp {
                // 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
+               SimpleName              = 8,
+
+               // Mask of all the expression class flags.
+               MaskExprClass           = 15,
+
+               // Disable control flow analysis while resolving the expression.
+               // This is used when resolving the instance expression of a field expression.
+               DisableFlowAnalysis     = 16
        }
 
        //
@@ -85,6 +92,42 @@ 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.
@@ -112,6 +155,13 @@ namespace Mono.CSharp {
                        get;
                }
 
+               /// <summary>
+               ///   The type which declares this member.
+               /// </summary>
+               Type DeclaringType {
+                       get;
+               }
+
                /// <summary>
                ///   The instance expression associated with this member, if it's a
                ///   non-static member.
@@ -121,6 +171,17 @@ namespace Mono.CSharp {
                }
        }
 
+       /// <summary>
+       ///   Expression which resolves to a type.
+       /// </summary>
+       public interface ITypeExpression
+       {
+               /// <summary>
+               ///   Resolve the expression, but only lookup types.
+               /// </summary>
+               Expression DoResolveType (EmitContext ec);
+       }
+
        /// <remarks>
        ///   Base class for expressions
        /// </remarks>
@@ -228,13 +289,28 @@ namespace Mono.CSharp {
                /// </remarks>
                public Expression Resolve (EmitContext ec, ResolveFlags flags)
                {
-                       Expression e;
+                       // Are we doing a types-only search ?
+                       if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type) {
+                               ITypeExpression type_expr = this as ITypeExpression;
 
+                               if (type_expr == null)
+                                       return null;
+
+                               return type_expr.DoResolveType (ec);
+                       }
+
+                       bool old_do_flow_analysis = ec.DoFlowAnalysis;
+                       if ((flags & ResolveFlags.DisableFlowAnalysis) != 0)
+                               ec.DoFlowAnalysis = false;
+
+                       Expression 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;
 
@@ -242,10 +318,16 @@ namespace Mono.CSharp {
                                SimpleName s = (SimpleName) e;
 
                                if ((flags & ResolveFlags.SimpleName) == 0) {
-                                       Report.Error (
-                                               103, loc,
-                                               "The name `" + s.Name + "' could not be found in `" +
-                                               ec.DeclSpace.Name + "'");
+
+                                       object lookup = TypeManager.MemberLookup (
+                                               ec.ContainerType, ec.ContainerType, AllMemberTypes,
+                                               AllBindingFlags | BindingFlags.NonPublic, s.Name);
+                                       if (lookup != null)
+                                               Error (122, "`" + s.Name + "' " +
+                                                      "is inaccessible because of its protection level");
+                                       else
+                                               Error (103, "The name `" + s.Name + "' could not be " +
+                                                      "found in `" + ec.DeclSpace.Name + "'");
                                        return null;
                                }
 
@@ -469,7 +551,23 @@ 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);
+               }
+
+               //
+               // Lookup type `t' for code in class `invocation_type'.  Note that it's important
+               // to set `invocation_type' correctly since this method also checks whether the
+               // invoking class is allowed to access the member in class `t'.  When you want to
+               // explicitly do a lookup in the base class, you must set both `t' and `invocation_type'
+               // to the base class (although a derived class can access protected members of its base
+               // class it cannot do so through an instance of the base class (error CS1540)).
+               // 
+
+               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;
@@ -500,12 +598,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>
@@ -527,7 +627,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;
@@ -568,8 +668,12 @@ namespace Mono.CSharp {
 
                                expr.Emit (null);
                        }
-                       
-                       if (target_type == TypeManager.object_type) {
+
+                       //
+                       // notice that it is possible to write "ValueType v = 1", the ValueType here
+                       // is an abstract class, and not really a value type, so we apply the same rules.
+                       //
+                       if (target_type == TypeManager.object_type || target_type == TypeManager.value_type) {
                                //
                                // A pointer type cannot be converted to object
                                // 
@@ -580,9 +684,9 @@ namespace Mono.CSharp {
                                        return new BoxedCast (expr);
                                if (expr_type.IsClass || expr_type.IsInterface)
                                        return new EmptyCast (expr, target_type);
-                       } else if (expr_type.IsSubclassOf (target_type)) {
+                       } else if (expr_type.IsSubclassOf (target_type)) 
                                return new EmptyCast (expr, target_type);
-                       else {
+                       else {
 
                                // This code is kind of mirrored inside StandardConversionExists
                                // with the small distinction that we only probe there
@@ -594,16 +698,17 @@ namespace Mono.CSharp {
                                        return new EmptyCast (expr, target_type);
 
                                // from any class-type S to any interface-type T.
-                               if (expr_type.IsClass && target_type.IsInterface) {
-                                       if (TypeManager.ImplementsInterface (expr_type, target_type))
-                                               return new EmptyCast (expr, target_type);
-                                       else
-                                               return null;
+                               if (target_type.IsInterface) {
+                                       if (TypeManager.ImplementsInterface (expr_type, target_type)){
+                                               if (expr_type.IsClass)
+                                                       return new EmptyCast (expr, target_type);
+                                               else if (expr_type.IsValueType)
+                                                       return new BoxedCast (expr);
+                                       }
                                }
 
                                // from any interface type S to interface-type T.
                                if (expr_type.IsInterface && target_type.IsInterface) {
-
                                        if (TypeManager.ImplementsInterface (expr_type, target_type))
                                                return new EmptyCast (expr, target_type);
                                        else
@@ -651,21 +756,6 @@ namespace Mono.CSharp {
                        return null;
                }
 
-               /// <summary>
-               ///   Handles expressions like this: decimal d; d = 1;
-               ///   and changes them into: decimal d; d = new System.Decimal (1);
-               /// </summary>
-               static Expression InternalTypeConstructor (EmitContext ec, Expression expr, Type target)
-               {
-                       ArrayList args = new ArrayList ();
-
-                       args.Add (new Argument (expr, Argument.AType.Expression));
-
-                       Expression ne = new New (new TypeExpr (target, Location.Null), args, Location.Null);
-
-                       return ne.Resolve (ec);
-               }
-
                /// <summary>
                ///   Implicit Numeric Conversions.
                ///
@@ -698,19 +788,8 @@ namespace Mono.CSharp {
                                        return new ULongConstant ((ulong) v);
                        }
 
-                       //
-                       // If we have an enumeration, extract the underlying type,
-                       // use this during the comparission, but wrap around the original
-                       // target_type
-                       //
-                       Type real_target_type = target_type;
-
-                       if (TypeManager.IsEnumType (real_target_type))
-                               real_target_type = TypeManager.EnumToUnderlying (real_target_type);
+                       Type real_target_type = target_type;
 
-                       if (expr_type == real_target_type)
-                               return new EmptyCast (expr, target_type);
-                       
                        if (expr_type == TypeManager.sbyte_type){
                                //
                                // From sbyte to short, int, long, float, double.
@@ -725,8 +804,6 @@ namespace Mono.CSharp {
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
                                if (real_target_type == TypeManager.short_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
-                               if (real_target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
                        } else if (expr_type == TypeManager.byte_type){
                                //
                                // From byte to short, ushort, int, uint, long, ulong, float, double
@@ -745,8 +822,6 @@ namespace Mono.CSharp {
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
                                if (real_target_type == TypeManager.double_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
-                               if (real_target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
                        } else if (expr_type == TypeManager.short_type){
                                //
                                // From short to int, long, float, double
@@ -759,8 +834,6 @@ namespace Mono.CSharp {
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
                                if (real_target_type == TypeManager.float_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
-                               if (real_target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
                        } else if (expr_type == TypeManager.ushort_type){
                                //
                                // From ushort to int, uint, long, ulong, float, double
@@ -778,8 +851,6 @@ namespace Mono.CSharp {
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
                                if (real_target_type == TypeManager.float_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
-                               if (real_target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
                        } else if (expr_type == TypeManager.int32_type){
                                //
                                // From int to long, float, double
@@ -790,8 +861,6 @@ namespace Mono.CSharp {
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
                                if (real_target_type == TypeManager.float_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
-                               if (real_target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
                        } else if (expr_type == TypeManager.uint32_type){
                                //
                                // From uint to long, ulong, float, double
@@ -806,8 +875,6 @@ namespace Mono.CSharp {
                                if (real_target_type == TypeManager.float_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
                                                               OpCodes.Conv_R4);
-                               if (real_target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
                        } else if (expr_type == TypeManager.int64_type){
                                //
                                // From long/ulong to float, double
@@ -816,8 +883,6 @@ namespace Mono.CSharp {
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
                                if (real_target_type == TypeManager.float_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);     
-                               if (real_target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
                        } else if (expr_type == TypeManager.uint64_type){
                                //
                                // From ulong to float, double
@@ -828,8 +893,6 @@ namespace Mono.CSharp {
                                if (real_target_type == TypeManager.float_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
                                                               OpCodes.Conv_R4);        
-                               if (real_target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
                        } else if (expr_type == TypeManager.char_type){
                                //
                                // From char to ushort, int, uint, long, ulong, float, double
@@ -846,8 +909,6 @@ namespace Mono.CSharp {
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
                                if (real_target_type == TypeManager.double_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
-                               if (real_target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
                        } else if (expr_type == TypeManager.float_type){
                                //
                                // float to double
@@ -884,7 +945,7 @@ namespace Mono.CSharp {
                                // from ImplicitReferenceConversion so make sure code remains in sync
                                
                                // from any class-type S to any interface-type T.
-                               if (expr_type.IsClass && target_type.IsInterface) {
+                               if (target_type.IsInterface) {
                                        if (TypeManager.ImplementsInterface (expr_type, target_type))
                                                return true;
                                }
@@ -954,7 +1015,7 @@ namespace Mono.CSharp {
 
                        return false;
                }
-               
+
                /// <summary>
                ///  Determines if a standard implicit conversion exists from
                ///  expr_type to target_type
@@ -962,6 +1023,9 @@ namespace Mono.CSharp {
                public static bool StandardConversionExists (Expression expr, Type target_type)
                {
                        Type expr_type = expr.Type;
+
+                       if (expr_type == TypeManager.void_type)
+                               return false;
                        
                        if (expr_type == target_type)
                                return true;
@@ -1124,6 +1188,10 @@ namespace Mono.CSharp {
                                if (i.Value == 0)
                                        return true;
                        }
+
+                       if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
+                               return true;
+
                        return false;
                }
 
@@ -1203,7 +1271,7 @@ namespace Mono.CSharp {
                ///   by making use of FindMostEncomp* methods. Applies the correct rules separately
                ///   for explicit and implicit conversion operators.
                /// </summary>
-               static public Type FindMostSpecificSource (MethodGroupExpr me, Type source_type,
+               static public Type FindMostSpecificSource (MethodGroupExpr me, Expression source,
                                                           bool apply_explicit_conv_rules,
                                                           Location loc)
                {
@@ -1211,10 +1279,11 @@ namespace Mono.CSharp {
                        
                        if (priv_fms_expr == null)
                                priv_fms_expr = new EmptyExpression ();
-                       
+
                        //
                        // If any operator converts from S then Sx = S
                        //
+                       Type source_type = source.Type;
                        foreach (MethodBase mb in me.Methods){
                                ParameterData pd = Invocation.GetParameterData (mb);
                                Type param_type = pd.ParameterType (0);
@@ -1235,16 +1304,14 @@ namespace Mono.CSharp {
                                        if (StandardConversionExists (priv_fms_expr, source_type))
                                                src_types_set.Add (param_type);
                                        else {
-                                               priv_fms_expr.SetType (source_type);
-                                               if (StandardConversionExists (priv_fms_expr, param_type))
+                                               if (StandardConversionExists (source, param_type))
                                                        src_types_set.Add (param_type);
                                        }
                                } else {
                                        //
                                        // Only if S is encompassed by param_type
                                        //
-                                       priv_fms_expr.SetType (source_type);
-                                       if (StandardConversionExists (priv_fms_expr, param_type))
+                                       if (StandardConversionExists (source, param_type))
                                                src_types_set.Add (param_type);
                                }
                        }
@@ -1256,9 +1323,7 @@ namespace Mono.CSharp {
                                ArrayList candidate_set = new ArrayList ();
 
                                foreach (Type param_type in src_types_set){
-                                       priv_fms_expr.SetType (source_type);
-                                       
-                                       if (StandardConversionExists (priv_fms_expr, param_type))
+                                       if (StandardConversionExists (source, param_type))
                                                candidate_set.Add (param_type);
                                }
 
@@ -1351,7 +1416,7 @@ namespace Mono.CSharp {
                        //
                        if (apply_explicit_conv_rules)
                                return FindMostEncompassedType (tgt_types_set);
-                       else
+                       else 
                                return FindMostEncompassingType (tgt_types_set);
                }
                
@@ -1473,14 +1538,14 @@ namespace Mono.CSharp {
                        }
 #endif
                        
-                       most_specific_source = FindMostSpecificSource (union, source_type, look_for_explicit, loc);
+                       most_specific_source = FindMostSpecificSource (union, source, look_for_explicit, loc);
                        if (most_specific_source == null)
                                return null;
 
                        most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
                        if (most_specific_target == null) 
                                return null;
-                       
+
                        int count = 0;
 
                        foreach (MethodBase mb in union.Methods){
@@ -1648,9 +1713,21 @@ namespace Mono.CSharp {
                                        return new ULongConstant ((ulong) value);
                        }
                        
-                       if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type))
-                               return new EnumConstant (ic, target_type);
-
+                       if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
+                               Type underlying = TypeManager.EnumToUnderlying (target_type);
+                               Constant e = (Constant) ic;
+                               
+                               //
+                               // Possibly, we need to create a different 0 literal before passing
+                               // to EnumConstant
+                               //n
+                               if (underlying == TypeManager.int64_type)
+                                       e = new LongLiteral (0);
+                               else if (underlying == TypeManager.uint64_type)
+                                       e = new ULongLiteral (0);
+
+                               return new EnumConstant (e, target_type);
+                       }
                        return null;
                }
 
@@ -1682,7 +1759,7 @@ namespace Mono.CSharp {
                                              "Double literal cannot be implicitly converted to " +
                                              "float type, use F suffix to create a float literal");
                        }
-                       
+
                        Error_CannotConvertImplicit (loc, source.Type, target_type);
 
                        return null;
@@ -1691,13 +1768,13 @@ namespace Mono.CSharp {
                /// <summary>
                ///   Performs the explicit numeric conversions
                /// </summary>
-               static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type)
+               static Expression ConvertNumericExplicit (EmitContext ec, Expression expr, Type target_type, Location loc)
                {
                        Type expr_type = expr.Type;
 
                        //
                        // If we have an enumeration, extract the underlying type,
-                       // use this during the comparission, but wrap around the original
+                       // use this during the comparison, but wrap around the original
                        // target_type
                        //
                        Type real_target_type = target_type;
@@ -1705,6 +1782,14 @@ namespace Mono.CSharp {
                        if (TypeManager.IsEnumType (real_target_type))
                                real_target_type = TypeManager.EnumToUnderlying (real_target_type);
 
+                       if (StandardConversionExists (expr, real_target_type)){
+                               Expression ce = ConvertImplicitStandard (ec, expr, real_target_type, loc);
+
+                               if (real_target_type != target_type)
+                                       return new EmptyCast (ce, target_type);
+                               return ce;
+                       }
+                       
                        if (expr_type == TypeManager.sbyte_type){
                                //
                                // From sbyte to byte, ushort, uint, ulong, char
@@ -1863,8 +1948,6 @@ namespace Mono.CSharp {
                                        return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
                                if (real_target_type == TypeManager.char_type)
                                        return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
-                               if (real_target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
                        } else if (expr_type == TypeManager.double_type){
                                //
                                // From double to byte, byte, short,
@@ -1891,8 +1974,6 @@ namespace Mono.CSharp {
                                        return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
                                if (real_target_type == TypeManager.float_type)
                                        return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
-                               if (real_target_type == TypeManager.decimal_type)
-                                       return InternalTypeConstructor (ec, expr, target_type);
                        } 
 
                        // decimal is taken care of by the op_Explicit methods.
@@ -2105,7 +2186,7 @@ namespace Mono.CSharp {
                        if (ne != null)
                                return ne;
 
-                       ne = ConvertNumericExplicit (ec, expr, target_type);
+                       ne = ConvertNumericExplicit (ec, expr, target_type, loc);
                        if (ne != null)
                                return ne;
 
@@ -2136,7 +2217,12 @@ namespace Mono.CSharp {
                                if (t != null)
                                        return t;
                                
-                               return ConvertNumericExplicit (ec, e, target_type);
+                               t = ConvertNumericExplicit (ec, e, target_type, loc);
+                               if (t != null)
+                                       return t;
+                               
+                               Error_CannotConvertType (loc, expr_type, target_type);
+                               return null;
                        }
                        
                        ne = ConvertReferenceExplicit (expr, target_type);
@@ -2175,7 +2261,7 @@ namespace Mono.CSharp {
                                                if (ci != null)
                                                        return ci;
 
-                                               ce = ConvertNumericExplicit (ec, e, target_type);
+                                               ce = ConvertNumericExplicit (ec, e, target_type, loc);
                                                if (ce != null)
                                                        return ce;
                                                //
@@ -2207,7 +2293,7 @@ namespace Mono.CSharp {
                        if (ne != null)
                                return ne;
 
-                       ne = ConvertNumericExplicit (ec, expr, target_type);
+                       ne = ConvertNumericExplicit (ec, expr, target_type, l);
                        if (ne != null)
                                return ne;
 
@@ -2622,7 +2708,7 @@ namespace Mono.CSharp {
                //
                public static void StoreFromPtr (ILGenerator ig, Type type)
                {
-                       if (type.IsEnum)
+                       if (TypeManager.IsEnumType (type))
                                type = TypeManager.EnumToUnderlying (type);
                        if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
                                ig.Emit (OpCodes.Stind_I4);
@@ -2668,6 +2754,8 @@ namespace Mono.CSharp {
                                 t == TypeManager.char_type ||
                                 t == TypeManager.ushort_type)
                                return 2;
+                       else if (t == TypeManager.decimal_type)
+                               return 16;
                        else
                                return 0;
                }
@@ -3274,7 +3362,7 @@ namespace Mono.CSharp {
        ///   The downside of this is that we might be hitting `LookupType' too many
        ///   times with this scheme.
        /// </remarks>
-       public class SimpleName : Expression {
+       public class SimpleName : Expression, ITypeExpression {
                public readonly string Name;
                
                public SimpleName (string name, Location l)
@@ -3331,6 +3419,52 @@ namespace Mono.CSharp {
                        return SimpleNameResolve (ec, null, true);
                }
 
+               public Expression DoResolveType (EmitContext ec)
+               {
+                       //
+                       // Stage 3: Lookup symbol in the various namespaces. 
+                       //
+                       DeclSpace ds = ec.DeclSpace;
+                       Type t;
+                       string alias_value;
+
+                       if (ec.ResolvingTypeTree){
+                               int errors = Report.Errors;
+                               Type dt = ec.DeclSpace.FindType (loc, Name);
+                               if (Report.Errors != errors)
+                                       return null;
+                               
+                               if (dt != null)
+                                       return new TypeExpr (dt, loc);
+                       }
+
+                       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
+                       // or a namespace.
+                       //
+                       // Since we are cheating: we only do the Alias lookup for
+                       // namespaces if the name does not include any dots in it
+                       //
+                               
+                       alias_value = ec.DeclSpace.LookupAlias (Name);
+                               
+                       if (Name.IndexOf ('.') == -1 && alias_value != null) {
+                               if ((t = RootContext.LookupType (ds, alias_value, true, loc)) != null)
+                                       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, loc);
+                       }
+                               
+                       // No match, maybe our parent can compose us
+                       // into something meaningful.
+                       return this;
+               }
+
                /// <remarks>
                ///   7.5.2: Simple Names. 
                ///
@@ -3355,122 +3489,102 @@ namespace Mono.CSharp {
                        //
                        // Stage 1: Performed by the parser (binding to locals or parameters).
                        //
-                       if (!ec.OnlyLookupTypes){
-                               Block current_block = ec.CurrentBlock;
-                               if (current_block != null && current_block.IsVariableDefined (Name)){
-                                       LocalVariableReference var;
+                       Block current_block = ec.CurrentBlock;
+                       if (current_block != null && current_block.IsVariableDefined (Name)){
+                               LocalVariableReference var;
+
+                               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;
                                        
-                                       var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
+                                       param = new ParameterReference (pars, idx, Name, loc);
 
                                        if (right_side != null)
-                                               return var.ResolveLValue (ec, right_side);
+                                               return param.ResolveLValue (ec, right_side);
                                        else
-                                               return var.Resolve (ec);
+                                               return param.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);
+                       //
+                       // Stage 2: Lookup members 
+                       //
 
-                                       if (par != null) {
-                                               ParameterReference param;
-                                       
-                                               param = new ParameterReference (pars, idx, Name, loc);
+                       //
+                       // For enums, the TypeBuilder is not ec.DeclSpace.TypeBuilder
+                       // Hence we have two different cases
+                       //
 
-                                               if (right_side != null)
-                                                       return param.ResolveLValue (ec, right_side);
-                                               else
-                                                       return param.Resolve (ec);
-                                       }
-                               }
+                       DeclSpace lookup_ds = ec.DeclSpace;
+                       do {
+                               if (lookup_ds.TypeBuilder == null)
+                                       break;
 
-                               //
-                               // Stage 2: Lookup members 
-                               //
+                               e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
+                               if (e != null)
+                                       break;
 
                                //
-                               // For enums, the TypeBuilder is not ec.DeclSpace.TypeBuilder
-                               // Hence we have two different cases
+                               // Classes/structs keep looking, enums break
                                //
+                               if (lookup_ds is TypeContainer)
+                                       lookup_ds = ((TypeContainer) lookup_ds).Parent;
+                               else
+                                       break;
+                       } while (lookup_ds != null);
+                               
+                       if (e == null && ec.ContainerType != null)
+                               e = MemberLookup (ec, ec.ContainerType, Name, loc);
 
-                               DeclSpace lookup_ds = ec.DeclSpace;
-                               do {
-                                       if (lookup_ds.TypeBuilder == null)
-                                               break;
+                       if (e == null)
+                               return DoResolveType (ec);
 
-                                       e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
-                                       if (e != null)
-                                               break;
+                       if (e is TypeExpr)
+                               return e;
 
-                                       //
-                                       // Classes/structs keep looking, enums break
-                                       //
-                                       if (lookup_ds is TypeContainer)
-                                               lookup_ds = ((TypeContainer) lookup_ds).Parent;
-                                       else
-                                               break;
-                               } while (lookup_ds != null);
-                               
-                               if (e == null && ec.ContainerType != null)
-                                       e = MemberLookup (ec, ec.ContainerType, Name, loc);
-                       }
+                       if (e is IMemberExpr) {
+                               e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
+                               if (e == null)
+                                       return null;
 
-                       // Continuation of stage 2
-                       if (e == null){
-                               //
-                               // Stage 3: Lookup symbol in the various namespaces. 
-                               //
-                               DeclSpace ds = ec.DeclSpace;
-                               Type t;
-                               string alias_value;
+                               IMemberExpr me = e as IMemberExpr;
+                               if (me == null)
+                                       return e;
 
-                               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
-                               // or a namespace.
-                               //
-                               // Since we are cheating: we only do the Alias lookup for
-                               // namespaces if the name does not include any dots in it
-                               //
-                               
-                               alias_value = ec.DeclSpace.LookupAlias (Name);
-                               
-                               if (Name.IndexOf ('.') == -1 && alias_value != null) {
-                                       if ((t = RootContext.LookupType (ds, alias_value, true, loc))
-                                           != null)
-                                               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, loc);
-                               }
-                               
-                               if (ec.ResolvingTypeTree){
-                                       Type dt = ec.DeclSpace.FindType (Name);
-                                       if (dt != null)
-                                               return new TypeExpr (dt, loc);
-                               }
-                               
-                               // No match, maybe our parent can compose us
-                               // into something meaningful.
-                               return 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;
 
-                       //
-                       // Stage 2 continues here. 
-                       // 
-                       if (e is TypeExpr)
-                               return e;
+                               if (!me.IsStatic &&
+                                   TypeManager.IsNestedChildOf (me.InstanceExpression.Type, me.DeclaringType)) {
+                                       Error (38, "Cannot access nonstatic member `" + me.Name + "' of " +
+                                              "outer type `" + me.DeclaringType + "' via nested type `" +
+                                              me.InstanceExpression.Type + "'");
+                                       return null;
+                               }
 
-                       if (ec.OnlyLookupTypes)
-                               return null;
+                               if (right_side != null)
+                                       e = e.DoResolveLValue (ec, right_side);
+                               else
+                                       e = e.DoResolve (ec);
 
-                       if (e is IMemberExpr)
-                               return MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
+                               return e;                               
+                       }
 
                        if (ec.IsStatic || ec.IsFieldInitializer){
                                if (allow_static)
@@ -3502,7 +3616,7 @@ namespace Mono.CSharp {
        /// <summary>
        ///   Fully resolved expression that evaluates to a type
        /// </summary>
-       public class TypeExpr : Expression {
+       public class TypeExpr : Expression, ITypeExpression {
                public TypeExpr (Type t, Location l)
                {
                        Type = t;
@@ -3510,6 +3624,11 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
+               public virtual Expression DoResolveType (EmitContext ec)
+               {
+                       return this;
+               }
+
                override public Expression DoResolve (EmitContext ec)
                {
                        return this;
@@ -3519,31 +3638,41 @@ namespace Mono.CSharp {
                {
                        throw new Exception ("Should never be called");
                }
+
+               public override string ToString ()
+               {
+                       return Type.ToString ();
+               }
        }
 
        /// <summary>
        ///   Used to create types from a fully qualified name.  These are just used
-       ///   by the parser to setup the core types.  A TypeExpression is always
+       ///   by the parser to setup the core types.  A TypeLookupExpression is always
        ///   classified as a type.
        /// </summary>
-       public class TypeExpression : TypeExpr {
+       public class TypeLookupExpression : TypeExpr {
                string name;
                
-               public TypeExpression (string name) : base (null, Location.Null)
+               public TypeLookupExpression (string name) : base (null, Location.Null)
                {
                        this.name = name;
                }
 
-               public override Expression DoResolve (EmitContext ec)
+               public override Expression DoResolveType (EmitContext ec)
                {
                        if (type == null)
                                type = RootContext.LookupType (ec.DeclSpace, name, false, Location.Null);
                        return this;
                }
 
+               public override Expression DoResolve (EmitContext ec)
+               {
+                       return DoResolveType (ec);
+               }
+
                public override void Emit (EmitContext ec)
                {
-                       throw new Exception ("Should never be called");                 
+                       throw new Exception ("Should never be called");
                }
 
                public override string ToString ()
@@ -3560,6 +3689,7 @@ namespace Mono.CSharp {
        public class MethodGroupExpr : Expression, IMemberExpr {
                public MethodBase [] Methods;
                Expression instance_expression = null;
+               bool is_explicit_impl = false;
                
                public MethodGroupExpr (MemberInfo [] mi, Location l)
                {
@@ -3589,6 +3719,12 @@ namespace Mono.CSharp {
                        eclass = ExprClass.MethodGroup;
                        type = TypeManager.object_type;
                }
+
+               public Type DeclaringType {
+                       get {
+                               return Methods [0].DeclaringType;
+                       }
+               }
                
                //
                // `A method group may have associated an instance expression' 
@@ -3603,6 +3739,16 @@ namespace Mono.CSharp {
                        }
                }
 
+               public bool IsExplicitImpl {
+                       get {
+                               return is_explicit_impl;
+                       }
+
+                       set {
+                               is_explicit_impl = value;
+                       }
+               }
+
                public string Name {
                        get {
                                return Methods [0].Name;
@@ -3631,6 +3777,12 @@ namespace Mono.CSharp {
                
                override public Expression DoResolve (EmitContext ec)
                {
+                       if (instance_expression != null) {
+                               instance_expression = instance_expression.DoResolve (ec);
+                               if (instance_expression == null)
+                                       return null;
+                       }
+
                        return this;
                }
 
@@ -3715,6 +3867,12 @@ namespace Mono.CSharp {
                        }
                }
 
+               public Type DeclaringType {
+                       get {
+                               return FieldInfo.DeclaringType;
+                       }
+               }
+
                public Expression InstanceExpression {
                        get {
                                return instance_expr;
@@ -3734,11 +3892,20 @@ namespace Mono.CSharp {
                                                             "Of the FieldExpr to set this\n");
                                }
 
-                               instance_expr = instance_expr.Resolve (ec);
+                               // 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;
                }
 
@@ -3758,6 +3925,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)
@@ -3782,7 +3953,7 @@ namespace Mono.CSharp {
                {
                        ILGenerator ig = ec.ig;
                        bool is_volatile = false;
-                               
+
                        if (FieldInfo is FieldBuilder){
                                FieldBase f = TypeManager.GetField (FieldInfo);
 
@@ -3974,6 +4145,12 @@ namespace Mono.CSharp {
                        }
                }
                
+               public Type DeclaringType {
+                       get {
+                               return PropertyInfo.DeclaringType;
+                       }
+               }
+
                //
                // The instance expression associated with this expression
                //
@@ -4009,7 +4186,35 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       type = PropertyInfo.PropertyType;
+                       if ((instance_expr == null) && ec.IsStatic && !is_static) {
+                               SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
+                               return null;
+                       }
+
+                       if (instance_expr != null) {
+                               instance_expr = instance_expr.DoResolve (ec);
+                               if (instance_expr == null)
+                                       return null;
+                       }
+
+                       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;
+                       }
+
+                       if (instance_expr != null) {
+                               instance_expr = instance_expr.DoResolve (ec);
+                               if (instance_expr == null)
+                                       return null;
+                       }
 
                        return this;
                }
@@ -4021,7 +4226,8 @@ namespace Mono.CSharp {
                        //
                        // Special case: length of single dimension array is turned into ldlen
                        //
-                       if (method == TypeManager.int_array_get_length){
+                       if ((method == TypeManager.system_int_array_get_length) ||
+                           (method == TypeManager.int_array_get_length)){
                                Type iet = instance_expr.Type;
 
                                //
@@ -4104,6 +4310,12 @@ namespace Mono.CSharp {
                        }
                }
 
+               public Type DeclaringType {
+                       get {
+                               return EventInfo.DeclaringType;
+                       }
+               }
+
                public Expression InstanceExpression {
                        get {
                                return instance_expr;
@@ -4116,13 +4328,18 @@ namespace Mono.CSharp {
 
                public override Expression DoResolve (EmitContext ec)
                {
-                       // We are born fully resolved
+                       if (instance_expr != null) {
+                               instance_expr = instance_expr.DoResolve (ec);
+                               if (instance_expr == null)
+                                       return null;
+                       }
+
                        return this;
                }
 
                public override void Emit (EmitContext ec)
                {
-                       throw new Exception ("Should not happen I think");
+                       Report.Error (70, loc, "The event `" + Name + "' can only appear on the left hand side of += or -= (except on the defining type)");
                }
 
                public void EmitAddOrRemove (EmitContext ec, Expression source)