2002-11-16 Martin Baulig <martin@ximian.com>
[mono.git] / mcs / mcs / ecore.cs
index fc9927673f18d203d7680426724fdadd4075aec1..db2b070af308916492307c651c4912eb61fbd963 100755 (executable)
@@ -155,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.
@@ -311,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;
                                }
 
@@ -499,7 +512,7 @@ namespace Mono.CSharp {
                        else if (mi is FieldInfo)
                                return new FieldExpr ((FieldInfo) mi, loc);
                        else if (mi is PropertyInfo)
-                               return new PropertyExpr ((PropertyInfo) mi, loc);
+                               return new PropertyExpr (ec, (PropertyInfo) mi, loc);
                        else if (mi is Type){
                                return new TypeExpr ((System.Type) mi, loc);
                        }
@@ -541,6 +554,15 @@ namespace Mono.CSharp {
                        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)
@@ -646,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
                                // 
@@ -659,6 +685,14 @@ namespace Mono.CSharp {
                                if (expr_type.IsClass || expr_type.IsInterface)
                                        return new EmptyCast (expr, target_type);
                        } else if (expr_type.IsSubclassOf (target_type)) {
+                               //
+                               // Special case: enumeration to System.Enum.
+                               // System.Enum is not a value type, it is a class, so we need
+                               // a boxing conversion
+                               //
+                               if (expr_type.IsEnum)
+                                       return new BoxedCast (expr);
+                       
                                return new EmptyCast (expr, target_type);
                        } else {
 
@@ -672,16 +706,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
@@ -729,21 +764,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.
                ///
@@ -776,19 +796,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.
@@ -803,8 +812,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
@@ -823,8 +830,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
@@ -837,8 +842,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
@@ -856,8 +859,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
@@ -868,8 +869,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
@@ -884,8 +883,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
@@ -894,8 +891,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
@@ -906,8 +901,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
@@ -924,8 +917,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
@@ -956,13 +947,12 @@ namespace Mono.CSharp {
                                
                        } else if (expr_type.IsSubclassOf (target_type)) {
                                return true;
-                               
                        } else {
                                // Please remember that all code below actually comes
                                // 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;
                                }
@@ -1032,7 +1022,7 @@ namespace Mono.CSharp {
 
                        return false;
                }
-               
+
                /// <summary>
                ///  Determines if a standard implicit conversion exists from
                ///  expr_type to target_type
@@ -1040,6 +1030,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;
@@ -1202,6 +1195,10 @@ namespace Mono.CSharp {
                                if (i.Value == 0)
                                        return true;
                        }
+
+                       if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
+                               return true;
+
                        return false;
                }
 
@@ -1281,7 +1278,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)
                {
@@ -1289,10 +1286,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);
@@ -1313,16 +1311,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);
                                }
                        }
@@ -1334,9 +1330,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);
                                }
 
@@ -1429,7 +1423,7 @@ namespace Mono.CSharp {
                        //
                        if (apply_explicit_conv_rules)
                                return FindMostEncompassedType (tgt_types_set);
-                       else
+                       else 
                                return FindMostEncompassingType (tgt_types_set);
                }
                
@@ -1551,14 +1545,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){
@@ -1572,10 +1566,9 @@ namespace Mono.CSharp {
                                }
                        }
                        
-                       if (method == null || count > 1) {
-                               Report.Error (-11, loc, "Ambiguous user defined conversion");
+                       if (method == null || count > 1)
                                return null;
-                       }
+                       
                        
                        //
                        // This will do the conversion to the best match that we
@@ -1724,11 +1717,26 @@ namespace Mono.CSharp {
                                //
                                if (value >= 0)
                                        return new ULongConstant ((ulong) value);
-                       }
+                       } else if (target_type == TypeManager.double_type)
+                               return new DoubleConstant ((double) value);
+                       else if (target_type == TypeManager.float_type)
+                               return new FloatConstant ((float) 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;
                }
 
@@ -1760,7 +1768,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;
@@ -1769,13 +1777,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;
@@ -1783,6 +1791,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
@@ -1941,8 +1957,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,
@@ -1969,8 +1983,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.
@@ -2183,15 +2195,20 @@ 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;
 
                        //
                        // Unboxing conversion.
                        //
-                       if (expr_type == TypeManager.object_type && target_type.IsValueType)
+                       if (expr_type == TypeManager.object_type && target_type.IsValueType){
+                               if (expr is NullLiteral){
+                                       Report.Error (37, "Cannot convert null to value type `" + TypeManager.CSharpName (expr_type) + "'");
+                                       return null;
+                               }
                                return new UnboxCast (expr, target_type);
+                       }
 
                        //
                        // Enum types
@@ -2214,7 +2231,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);
@@ -2253,7 +2275,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;
                                                //
@@ -2285,7 +2307,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;
 
@@ -2700,7 +2722,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);
@@ -2746,6 +2768,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;
                }
@@ -3418,9 +3442,20 @@ namespace Mono.CSharp {
                        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.
@@ -3439,12 +3474,6 @@ namespace Mono.CSharp {
                                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;
@@ -3475,7 +3504,7 @@ namespace Mono.CSharp {
                        // Stage 1: Performed by the parser (binding to locals or parameters).
                        //
                        Block current_block = ec.CurrentBlock;
-                       if (current_block != null && current_block.IsVariableDefined (Name)){
+                       if (current_block != null && current_block.GetVariableInfo (Name) != null){
                                LocalVariableReference var;
 
                                var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
@@ -3555,6 +3584,14 @@ namespace Mono.CSharp {
                                if (!me.IsStatic && (me.InstanceExpression == null))
                                        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 (right_side != null)
                                        e = e.DoResolveLValue (ec, right_side);
                                else
@@ -3601,7 +3638,7 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
-               public Expression DoResolveType (EmitContext ec)
+               public virtual Expression DoResolveType (EmitContext ec)
                {
                        return this;
                }
@@ -3615,6 +3652,11 @@ namespace Mono.CSharp {
                {
                        throw new Exception ("Should never be called");
                }
+
+               public override string ToString ()
+               {
+                       return Type.ToString ();
+               }
        }
 
        /// <summary>
@@ -3661,6 +3703,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)
                {
@@ -3690,6 +3733,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' 
@@ -3704,6 +3753,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;
@@ -3732,6 +3791,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;
                }
 
@@ -3816,6 +3881,12 @@ namespace Mono.CSharp {
                        }
                }
 
+               public Type DeclaringType {
+                       get {
+                               return FieldInfo.DeclaringType;
+                       }
+               }
+
                public Expression InstanceExpression {
                        get {
                                return instance_expr;
@@ -4009,17 +4080,13 @@ namespace Mono.CSharp {
                        // Handle initonly fields specially: make a copy and then
                        // get the address of the copy.
                        //
-                       if (FieldInfo.IsInitOnly){
-                               if (ec.IsConstructor) {
-                                       ig.Emit (OpCodes.Ldsflda, FieldInfo);
-                               } else {
-                                       LocalBuilder local;
+                       if (FieldInfo.IsInitOnly && !ec.IsConstructor){
+                               LocalBuilder local;
                                
-                                       Emit (ec);
-                                       local = ig.DeclareLocal (type);
-                                       ig.Emit (OpCodes.Stloc, local);
-                                       ig.Emit (OpCodes.Ldloca, local);
-                               }
+                               Emit (ec);
+                               local = ig.DeclareLocal (type);
+                               ig.Emit (OpCodes.Stloc, local);
+                               ig.Emit (OpCodes.Ldloca, local);
                                return;
                        }
 
@@ -4044,30 +4111,26 @@ namespace Mono.CSharp {
        /// </summary>
        public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
                public readonly PropertyInfo PropertyInfo;
+
+               //
+               // This is set externally by the  `BaseAccess' class
+               //
                public bool IsBase;
-               MethodInfo [] Accessors;
+               MethodInfo getter, setter;
                bool is_static;
                
                Expression instance_expr;
 
-               public PropertyExpr (PropertyInfo pi, Location l)
+               public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
                {
                        PropertyInfo = pi;
                        eclass = ExprClass.PropertyAccess;
                        is_static = false;
                        loc = l;
-                       Accessors = TypeManager.GetAccessors (pi);
 
-                       if (Accessors != null)
-                               foreach (MethodInfo mi in Accessors){
-                                       if (mi != null)
-                                               if (mi.IsStatic)
-                                                       is_static = true;
-                               }
-                       else
-                               Accessors = new MethodInfo [2];
-                       
                        type = TypeManager.TypeToCoreType (pi.PropertyType);
+
+                       ResolveAccessors (ec);
                }
 
                public string Name {
@@ -4088,6 +4151,12 @@ namespace Mono.CSharp {
                        }
                }
                
+               public Type DeclaringType {
+                       get {
+                               return PropertyInfo.DeclaringType;
+                       }
+               }
+
                //
                // The instance expression associated with this expression
                //
@@ -4103,7 +4172,7 @@ namespace Mono.CSharp {
 
                public bool VerifyAssignable ()
                {
-                       if (!PropertyInfo.CanWrite){
+                       if (setter == null) {
                                Report.Error (200, loc, 
                                              "The property `" + PropertyInfo.Name +
                                              "' can not be assigned to, as it has not set accessor");
@@ -4113,14 +4182,91 @@ namespace Mono.CSharp {
                        return true;
                }
 
+               //
+               // We also perform the permission checking here, as the PropertyInfo does not
+               // hold the information for the accessibility of its setter/getter
+               //
+               void ResolveAccessors (EmitContext ec)
+               {
+                       BindingFlags flags = BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
+                       MemberInfo [] group;
+                       
+                       group = TypeManager.MemberLookup (ec.ContainerType, PropertyInfo.DeclaringType,
+                                                         MemberTypes.Method, flags, "get_" + PropertyInfo.Name);
+
+                       //
+                       // The first method is the closest to us
+                       //
+                       if (group != null && group.Length > 0){
+                               getter = (MethodInfo) group [0];
+
+                               if (getter.IsStatic)
+                                       is_static = true;
+                       }                       
+
+                       //
+                       // The first method is the closest to us
+                       //
+                       group = TypeManager.MemberLookup (ec.ContainerType, PropertyInfo.DeclaringType,
+                                                         MemberTypes.Method, flags, "set_" + PropertyInfo.Name);
+                       if (group != null && group.Length > 0){
+                               setter = (MethodInfo) group [0];
+                               if (setter.IsStatic)
+                                       is_static = true;
+                       }
+
+                       if (setter == null && getter == null){
+                               Error (122, "`" + PropertyInfo.Name + "' " +
+                                      "is inaccessible because of its protection level");
+                               
+                       }
+               }
+
+               bool InstanceResolve (EmitContext ec)
+               {
+                       if ((instance_expr == null) && ec.IsStatic && !is_static) {
+                               SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
+                               return false;
+                       }
+
+                       if (instance_expr != null) {
+                               instance_expr = instance_expr.DoResolve (ec);
+                               if (instance_expr == null)
+                                       return false;
+                       }
+
+                       return true;
+               }
+               
                override public Expression DoResolve (EmitContext ec)
                {
-                       if (!PropertyInfo.CanRead){
+                       if (getter == null){
+                               //
+                               // The following condition happens if the PropertyExpr was
+                               // created, but is invalid (ie, the property is inaccessible),
+                               // and we did not want to embed the knowledge about this in
+                               // the caller routine.  This only avoids double error reporting.
+                               //
+                               if (setter == null)
+                                       return null;
+                               
                                Report.Error (154, loc, 
                                              "The property `" + PropertyInfo.Name +
                                              "' can not be used in " +
                                              "this context because it lacks a get accessor");
                                return null;
+                       } 
+
+                       if (!InstanceResolve (ec))
+                               return null;
+
+                       //
+                       // Only base will allow this invocation to happen.
+                       //
+                       if (IsBase && getter.IsAbstract){
+                               Report.Error (205, loc, "Cannot call an abstract base property: " +
+                                             PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
+                               return null;
                        }
 
                        return this;
@@ -4128,7 +4274,16 @@ namespace Mono.CSharp {
 
                override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
                {
-                       if (!PropertyInfo.CanWrite){
+                       if (setter == null){
+                               //
+                               // The following condition happens if the PropertyExpr was
+                               // created, but is invalid (ie, the property is inaccessible),
+                               // and we did not want to embed the knowledge about this in
+                               // the caller routine.  This only avoids double error reporting.
+                               //
+                               if (getter == null)
+                                       return null;
+                               
                                Report.Error (154, loc, 
                                              "The property `" + PropertyInfo.Name +
                                              "' can not be used in " +
@@ -4136,17 +4291,27 @@ namespace Mono.CSharp {
                                return null;
                        }
 
+                       if (!InstanceResolve (ec))
+                               return null;
+                       
+                       //
+                       // Only base will allow this invocation to happen.
+                       //
+                       if (IsBase && setter.IsAbstract){
+                               Report.Error (205, loc, "Cannot call an abstract base property: " +
+                                             PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
+                               return null;
+                       }
                        return this;
                }
 
                override public void Emit (EmitContext ec)
                {
-                       MethodInfo method = Accessors [0];
-
                        //
-                       // Special case: length of single dimension array is turned into ldlen
+                       // Special case: length of single dimension array property is turned into ldlen
                        //
-                       if (method == TypeManager.int_array_get_length){
+                       if ((getter == TypeManager.system_int_array_get_length) ||
+                           (getter == TypeManager.int_array_get_length)){
                                Type iet = instance_expr.Type;
 
                                //
@@ -4160,7 +4325,7 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, method, null, loc);
+                       Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, getter, null, loc);
                        
                }
 
@@ -4173,7 +4338,7 @@ namespace Mono.CSharp {
                        ArrayList args = new ArrayList ();
 
                        args.Add (arg);
-                       Invocation.EmitCall (ec, false, IsStatic, instance_expr, Accessors [1], args, loc);
+                       Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, setter, args, loc);
                }
 
                override public void EmitStatement (EmitContext ec)
@@ -4205,9 +4370,11 @@ namespace Mono.CSharp {
                        if (add_accessor.IsStatic || remove_accessor.IsStatic)
                                is_static = true;
 
-                       if (EventInfo is MyEventBuilder)
-                               type = ((MyEventBuilder) EventInfo).EventType;
-                       else
+                       if (EventInfo is MyEventBuilder){
+                               MyEventBuilder eb = (MyEventBuilder) EventInfo;
+                               type = eb.EventType;
+                               eb.SetUsed ();
+                       } else
                                type = EventInfo.EventHandlerType;
                }
 
@@ -4229,6 +4396,12 @@ namespace Mono.CSharp {
                        }
                }
 
+               public Type DeclaringType {
+                       get {
+                               return EventInfo.DeclaringType;
+                       }
+               }
+
                public Expression InstanceExpression {
                        get {
                                return instance_expr;
@@ -4241,13 +4414,19 @@ 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)