Fix #77628.
[mono.git] / mcs / mcs / expression.cs
index 0c928279ab3bac940f6c64a3660fa4a70ab31080..9005f6641e63b54bd625e0bb7dea0b21d758e0bc 100644 (file)
@@ -359,7 +359,7 @@ namespace Mono.CSharp {
                        
                        op_name = oper_names [(int) Oper];
 
-                       mg = MemberLookup (ec, expr_type, op_name, MemberTypes.Method, AllBindingFlags, loc);
+                       mg = MemberLookup (ec.ContainerType, expr_type, op_name, MemberTypes.Method, AllBindingFlags, loc);
                        
                        if (mg != null) {
                                Expression e = StaticCallExpr.MakeSimpleCall (
@@ -613,7 +613,7 @@ namespace Mono.CSharp {
                                throw new Exception ("This should be caught by Resolve");
                                
                        case Operator.UnaryNegation:
-                               if (ec.CheckState) {
+                               if (ec.CheckState && type != TypeManager.float_type && type != TypeManager.double_type) {
                                        ig.Emit (OpCodes.Ldc_I4_0);
                                        if (type == TypeManager.int64_type)
                                                ig.Emit (OpCodes.Conv_U8);
@@ -850,7 +850,7 @@ namespace Mono.CSharp {
                        else 
                                op_name = "op_Decrement";
 
-                       mg = MemberLookup (ec, expr_type, op_name, MemberTypes.Method, AllBindingFlags, loc);
+                       mg = MemberLookup (ec.ContainerType, expr_type, op_name, MemberTypes.Method, AllBindingFlags, loc);
 
                        if (mg != null) {
                                method = StaticCallExpr.MakeSimpleCall (
@@ -880,7 +880,7 @@ namespace Mono.CSharp {
                                if (expr == null)
                                        return null;
                        } else {
-                               expr.Error_UnexpectedKind (ec, "variable, indexer or property access", loc);
+                               expr.Error_UnexpectedKind (ec.DeclContainer, "variable, indexer or property access", loc);
                                return null;
                        }
 
@@ -1252,7 +1252,12 @@ namespace Mono.CSharp {
 
                        Error_CannotConvertType (etype, probe_type, loc);
                        return null;
-               }                               
+               }
+       
+               public override bool GetAttributableValue (out object value)
+               {
+                       return expr.GetAttributableValue (out value);
+               }
        }
        
        /// <summary>
@@ -1327,9 +1332,14 @@ namespace Mono.CSharp {
 
                        Constant c = expr as Constant;
                        if (c != null) {
-                               c = c.TryReduce (ec, type, loc);
-                               if (c != null)
-                                       return c;
+                               try {
+                                       c = c.TryReduce (ec, type, loc);
+                                       if (c != null)
+                                               return c;
+                               }
+                               catch (OverflowException) {
+                                       return null;
+                               }
                        }
 
                        if (type.IsPointer && !ec.InUnsafe) {
@@ -1832,10 +1842,10 @@ namespace Mono.CSharp {
                                string op = oper_names [(int) oper];
                                
                                MethodGroupExpr union;
-                               left_operators = MemberLookup (ec, l, op, MemberTypes.Method, AllBindingFlags, loc);
+                               left_operators = MemberLookup (ec.ContainerType, l, op, MemberTypes.Method, AllBindingFlags, loc);
                                if (r != l){
                                        right_operators = MemberLookup (
-                                               ec, r, op, MemberTypes.Method, AllBindingFlags, loc);
+                                               ec.ContainerType, r, op, MemberTypes.Method, AllBindingFlags, loc);
                                        union = Invocation.MakeUnionSet (left_operators, right_operators, loc);
                                } else
                                        union = (MethodGroupExpr) left_operators;
@@ -2310,6 +2320,9 @@ namespace Mono.CSharp {
 
                public override Expression DoResolve (EmitContext ec)
                {
+                       if (left == null)
+                               return null;
+
                        if ((oper == Operator.Subtraction) && (left is ParenthesizedExpression)) {
                                left = ((ParenthesizedExpression) left).Expr;
                                left = left.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.Type);
@@ -3407,7 +3420,7 @@ namespace Mono.CSharp {
                                local_info = Block.GetLocalInfo (Name);
 
                                // is out param
-                               if (lvalue_right_side == EmptyExpression.Null)
+                               if (lvalue_right_side == EmptyExpression.OutAccess)
                                        local_info.Used = true;
 
                                is_readonly = local_info.ReadOnly;
@@ -3418,9 +3431,12 @@ namespace Mono.CSharp {
                        VariableInfo variable_info = local_info.VariableInfo;
                        if (lvalue_right_side != null){
                                if (is_readonly){
-                                       if (lvalue_right_side is LocalVariableReference || lvalue_right_side == EmptyExpression.Null)
+                                       if (lvalue_right_side is LocalVariableReference || lvalue_right_side == EmptyExpression.OutAccess)
                                                Report.Error (1657, loc, "Cannot pass `{0}' as a ref or out argument because it is a `{1}'",
                                                        Name, local_info.GetReadOnlyContext ());
+                                       else if (lvalue_right_side == EmptyExpression.LValueMemberAccess)
+                                               Report.Error (1654, loc, "Cannot assign to members of `{0}' because it is a `{1}'",
+                                                       Name, local_info.GetReadOnlyContext ());
                                        else
                                                Report.Error (1656, loc, "Cannot assign to `{0}' because it is a `{1}'",
                                                        Name, local_info.GetReadOnlyContext ());
@@ -3590,12 +3606,11 @@ namespace Mono.CSharp {
        ///   representation.
        /// </summary>
        public class ParameterReference : Expression, IAssignMethod, IMemoryLocation, IVariable {
-               Parameters pars;
-               String name;
+               Parameter par;
+               string name;
                int idx;
                Block block;
                VariableInfo vi;
-               public Parameter.Modifier mod;
                public bool is_ref, is_out, prepared;
 
                public bool IsOut {
@@ -3612,20 +3627,16 @@ namespace Mono.CSharp {
 
                LocalTemporary temp;
                
-               public ParameterReference (Parameters pars, Block block, int idx, string name, Location loc)
+               public ParameterReference (Parameter par, Block block, int idx, Location loc)
                {
-                       this.pars = pars;
+                       this.par = par;
+                       this.name = par.Name;
                        this.block = block;
                        this.idx  = idx;
-                       this.name = name;
                        this.loc = loc;
                        eclass = ExprClass.Variable;
                }
 
-               public ParameterReference (InternalParameters pars, Block block, int idx, Location loc)
-                       : this (pars.Parameters, block, idx, pars.ParameterName (idx), loc)
-               { }
-
                public VariableInfo VariableInfo {
                        get { return vi; }
                }
@@ -3633,7 +3644,7 @@ namespace Mono.CSharp {
                public bool VerifyFixed ()
                {
                        // A parameter is fixed if it's a value parameter (i.e., no modifier like out, ref, param).
-                       return mod == Parameter.Modifier.NONE;
+                       return par.ModFlags == Parameter.Modifier.NONE;
                }
 
                public bool IsAssigned (EmitContext ec, Location loc)
@@ -3642,7 +3653,7 @@ namespace Mono.CSharp {
                                return true;
 
                        Report.Error (269, loc,
-                                     "Use of unassigned out parameter `{0}'", name);
+                                     "Use of unassigned out parameter `{0}'", par.Name);
                        return false;
                }
 
@@ -3670,18 +3681,23 @@ namespace Mono.CSharp {
 
                protected void DoResolveBase (EmitContext ec)
                {
-                       type = pars.GetParameterInfo (ec, idx, out mod);
+                       if (!par.Resolve (ec)) {
+                               //TODO:
+                       }
+
+                       type = par.ParameterType;
+                       Parameter.Modifier mod = par.ModFlags;
                        is_ref = (mod & Parameter.Modifier.ISBYREF) != 0;
-                       is_out = (mod & Parameter.Modifier.OUT) != 0;
+                       is_out = (mod & Parameter.Modifier.OUT) == Parameter.Modifier.OUT;
                        eclass = ExprClass.Variable;
 
                        if (is_out)
                                vi = block.ParameterMap [idx];
 
                        if (ec.CurrentAnonymousMethod != null){
-                               if (is_ref){
+                               if (is_ref && !block.Toplevel.IsLocalParameter (name)){
                                        Report.Error (1628, Location, "Cannot use ref or out parameter `{0}' inside an anonymous method block",
-                                               name);
+                                               par.Name);
                                        return;
                                }
 
@@ -3786,11 +3802,8 @@ namespace Mono.CSharp {
                        ILGenerator ig = ec.ig;
                        int arg_idx = idx;
 
-                       if (ec.HaveCaptureInfo && ec.IsParameterCaptured (name)){
-                               if (leave_copy)
-                                       throw new InternalErrorException ();
-                               
-                               ec.EmitParameter (name);
+                       if (ec.HaveCaptureInfo && ec.IsParameterCaptured (name)){                               
+                               ec.EmitParameter (name, leave_copy, prepared, ref temp);
                                return;
                        }
 
@@ -3822,15 +3835,16 @@ namespace Mono.CSharp {
                
                public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
                {
+                       prepared = prepare_for_load;
                        if (ec.HaveCaptureInfo && ec.IsParameterCaptured (name)){
-                               ec.EmitAssignParameter (name, source, leave_copy, prepare_for_load);
+                               ec.EmitAssignParameter (name, source, leave_copy, prepare_for_load, ref temp);
                                return;
                        }
 
                        ILGenerator ig = ec.ig;
                        int arg_idx = idx;
                        
-                       prepared = prepare_for_load;
+                       
                        
                        if (!ec.MethodIsStatic)
                                arg_idx++;
@@ -3932,10 +3946,10 @@ namespace Mono.CSharp {
                        get {
                                switch (ArgType) {
                                        case AType.Out:
-                                               return Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF;
+                                               return Parameter.Modifier.OUT;
 
                                        case AType.Ref:
-                                               return Parameter.Modifier.REF | Parameter.Modifier.ISBYREF;
+                                               return Parameter.Modifier.REF;
 
                                        default:
                                                return Parameter.Modifier.NONE;
@@ -3983,15 +3997,17 @@ namespace Mono.CSharp {
                                        return false;
                                }
 
+                               int errors = Report.Errors;
                                Expr = Expr.DoResolveLValue (ec, Expr);
-                               if (Expr == null)
+                               if (Expr == null && errors == Report.Errors)
                                        Error_LValueRequired (loc);
                        } else if (ArgType == AType.Out) {
+                               int errors = Report.Errors;
                                ec.InRefOutArgumentResolving = true;
-                               Expr = Expr.DoResolveLValue (ec, EmptyExpression.Null);
+                               Expr = Expr.DoResolveLValue (ec, EmptyExpression.OutAccess);
                                ec.InRefOutArgumentResolving = false;
 
-                               if (Expr == null)
+                               if (Expr == null && errors == Report.Errors)
                                        Error_LValueRequired (loc);
                        }
                        else
@@ -4024,20 +4040,6 @@ namespace Mono.CSharp {
                                        }
                                }
                        }
-
-                       if (Expr.eclass != ExprClass.Variable){
-                               //
-                               // We just probe to match the CSC output
-                               //
-                               if (Expr.eclass == ExprClass.PropertyAccess ||
-                                   Expr.eclass == ExprClass.IndexerAccess){
-                                       Report.Error (206, loc, "A property or indexer `{0}' may not be passed as an out or ref parameter",
-                                               Expr.GetSignatureForError ());
-                               } else {
-                                       Error_LValueRequired (loc);
-                               }
-                               return false;
-                       }
                                
                        return true;
                }
@@ -4278,7 +4280,7 @@ namespace Mono.CSharp {
                         return !candidate_params && best_params;
                }
 
-               static bool IsOverride (MethodBase cand_method, MethodBase base_method)
+               internal static bool IsOverride (MethodBase cand_method, MethodBase base_method)
                {
                        if (!IsAncestralType (base_method.DeclaringType, cand_method.DeclaringType))
                                return false;
@@ -4364,26 +4366,18 @@ namespace Mono.CSharp {
                        return union;
                }
 
-               public static bool IsParamsMethodApplicable (EmitContext ec, MethodGroupExpr me,
+               public static bool IsParamsMethodApplicable (EmitContext ec,
                                                             ArrayList arguments, int arg_count,
-                                                            ref MethodBase candidate)
+                                                            MethodBase candidate)
                {
                        return IsParamsMethodApplicable (
-                               ec, me, arguments, arg_count, false, ref candidate) ||
+                               ec, arguments, arg_count, candidate, false) ||
                                IsParamsMethodApplicable (
-                                       ec, me, arguments, arg_count, true, ref candidate);
+                                       ec, arguments, arg_count, candidate, true);
 
 
                }
 
-               static bool IsParamsMethodApplicable (EmitContext ec, MethodGroupExpr me,
-                                                     ArrayList arguments, int arg_count,
-                                                     bool do_varargs, ref MethodBase candidate)
-               {
-                       return IsParamsMethodApplicable (
-                               ec, arguments, arg_count, candidate, do_varargs);
-               }
-
                /// <summary>
                ///   Determines if the candidate method, if a params method, is applicable
                ///   in its expanded form to the given set of arguments
@@ -4405,7 +4399,7 @@ namespace Mono.CSharp {
                                if (pd_count != arg_count)
                                        return false;
                        } else {
-                               if (pd.ParameterModifier (count) != Parameter.Modifier.PARAMS)
+                               if (!pd.HasParams)
                                        return false;
                        }
                        
@@ -4425,9 +4419,9 @@ namespace Mono.CSharp {
                                Argument a = (Argument) arguments [i];
 
                                Parameter.Modifier a_mod = a.Modifier & 
-                                       (unchecked (~(Parameter.Modifier.OUT | Parameter.Modifier.REF)));
+                                       (unchecked (~(Parameter.Modifier.OUTMASK | Parameter.Modifier.REFMASK)));
                                Parameter.Modifier p_mod = pd.ParameterModifier (i) &
-                                       (unchecked (~(Parameter.Modifier.OUT | Parameter.Modifier.REF)));
+                                       (unchecked (~(Parameter.Modifier.OUTMASK | Parameter.Modifier.REFMASK)));
 
                                if (a_mod == p_mod) {
 
@@ -4435,8 +4429,8 @@ namespace Mono.CSharp {
                                                if (!Convert.ImplicitConversionExists (ec,
                                                                                        a.Expr,
                                                                                        pd.ParameterType (i)))
-                                                       return false;
-                                                                               
+                               return false;
+
                                        if ((a_mod & Parameter.Modifier.ISBYREF) != 0) {
                                                Type pt = pd.ParameterType (i);
 
@@ -4471,19 +4465,12 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               public static bool IsApplicable (EmitContext ec, MethodGroupExpr me,
-                                                ArrayList arguments, int arg_count,
-                                                ref MethodBase candidate)
-               {
-                       return IsApplicable (ec, arguments, arg_count, candidate);
-               }
-
                /// <summary>
                ///   Determines if the candidate method is applicable (section 14.4.2.1)
                ///   to the given set of arguments
                /// </summary>
-               static bool IsApplicable (EmitContext ec, ArrayList arguments, int arg_count,
-                                         MethodBase candidate)
+               public static bool IsApplicable (EmitContext ec, ArrayList arguments, int arg_count,
+                       MethodBase candidate)
                {
                        ParameterData pd = TypeManager.GetParameterData (candidate);
 
@@ -4496,28 +4483,22 @@ namespace Mono.CSharp {
                                Argument a = (Argument) arguments [i];
 
                                Parameter.Modifier a_mod = a.Modifier &
-                                       unchecked (~(Parameter.Modifier.OUT | Parameter.Modifier.REF));
+                                       ~(Parameter.Modifier.OUTMASK | Parameter.Modifier.REFMASK);
+
                                Parameter.Modifier p_mod = pd.ParameterModifier (i) &
-                                       unchecked (~(Parameter.Modifier.OUT | Parameter.Modifier.REF));
+                                       ~(Parameter.Modifier.OUTMASK | Parameter.Modifier.REFMASK | Parameter.Modifier.PARAMS);
 
-                               if (a_mod == p_mod ||
-                                   (a_mod == Parameter.Modifier.NONE && p_mod == Parameter.Modifier.PARAMS)) {
-                                       if (a_mod == Parameter.Modifier.NONE) {
-                                                if (!Convert.ImplicitConversionExists (ec,
-                                                                                       a.Expr,
-                                                                                       pd.ParameterType (i)))
-                                                       return false;
-                                        }
-                                       
-                                       if ((a_mod & Parameter.Modifier.ISBYREF) != 0) {
-                                               Type pt = pd.ParameterType (i);
+                               if (a_mod == p_mod) {
+                                       Type pt = pd.ParameterType (i);
 
-                                               if (!pt.IsByRef)
-                                                       pt = TypeManager.GetReferenceType (pt);
-                                                
-                                               if (pt != a.Type)
+                                       if (a_mod == Parameter.Modifier.NONE) {
+                                               if (!Convert.ImplicitConversionExists (ec, a.Expr, pt))
                                                        return false;
+                                               continue;
                                        }
+                                       
+                                       if (pt != a.Type)
+                                               return false;
                                } else
                                        return false;
                        }
@@ -4525,7 +4506,7 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               static private bool IsAncestralType (Type first_type, Type second_type)
+               static internal bool IsAncestralType (Type first_type, Type second_type)
                {
                        return first_type != second_type &&
                                (second_type.IsSubclassOf (first_type) ||
@@ -4579,45 +4560,57 @@ namespace Mono.CSharp {
 
                        MethodBase[] methods = me.Methods;
 
+                       int nmethods = methods.Length;
+
+                       if (!me.IsBase) {
+                               //
+                               // Methods marked 'override' don't take part in 'applicable_type'
+                               // computation, nor in the actual overload resolution.
+                               // However, they still need to be emitted instead of a base virtual method.
+                               // So, we salt them away into the 'candidate_overrides' array.
+                               //
+                               // In case of reflected methods, we replace each overriding method with
+                               // its corresponding base virtual method.  This is to improve compatibility
+                               // with non-C# libraries which change the visibility of overrides (#75636)
+                               //
+                               int j = 0;
+                               for (int i = 0; i < methods.Length; ++i) {
+                                       MethodBase m = methods [i];
+                                       if (TypeManager.IsOverride (m)) {
+                                               if (candidate_overrides == null)
+                                                       candidate_overrides = new ArrayList ();
+                                               candidate_overrides.Add (m);
+                                               m = TypeManager.TryGetBaseDefinition (m);
+                                       }
+                                       if (m != null)
+                                               methods [j++] = m;
+                               }
+                               nmethods = j;
+                       }
+
+                       int applicable_errors = Report.Errors;
+                       
                        //
                        // First we construct the set of applicable methods
                        //
                        bool is_sorted = true;
-                       for (int i = 0; i < methods.Length; i++){
+                       for (int i = 0; i < nmethods; i++){
                                Type decl_type = methods [i].DeclaringType;
 
                                //
                                // If we have already found an applicable method
                                // we eliminate all base types (Section 14.5.5.1)
                                //
-                               if ((applicable_type != null) &&
-                                       IsAncestralType (decl_type, applicable_type))
-                                       continue;
-
-                               //
-                               // Methods marked 'override' don't take part in 'applicable_type'
-                               // computation, nor in the actual overload resolution.
-                               // However, they still need to be emitted instead of a base virtual method.
-                               // We avoid doing the 'applicable' test here, since it'll anyway be applied
-                               // to the base virtual function, and IsOverride is much faster than IsApplicable.
-                               //
-                               if (!me.IsBase && TypeManager.IsOverride (methods [i])) {
-                                       if (candidate_overrides == null)
-                                               candidate_overrides = new ArrayList ();
-                                       candidate_overrides.Add (methods [i]);
+                               if (applicable_type != null && IsAncestralType (decl_type, applicable_type))
                                        continue;
-                               }
 
                                //
                                // Check if candidate is applicable (section 14.4.2.1)
                                //   Is candidate applicable in normal form?
                                //
-                               bool is_applicable = IsApplicable (
-                                       ec, me, Arguments, arg_count, ref methods [i]);
+                               bool is_applicable = IsApplicable (ec, Arguments, arg_count, methods [i]);
 
-                               if (!is_applicable &&
-                                       (IsParamsMethodApplicable (
-                                       ec, me, Arguments, arg_count, ref methods [i]))) {
+                               if (!is_applicable && IsParamsMethodApplicable (ec, Arguments, arg_count, methods [i])) {
                                        MethodBase candidate = methods [i];
                                        if (candidate_to_form == null)
                                                candidate_to_form = new PtrHashtable ();
@@ -4640,6 +4633,9 @@ namespace Mono.CSharp {
                                }
                        }
 
+                       if (applicable_errors != Report.Errors)
+                               return null;
+                       
                        int candidate_top = candidates.Count;
 
                        if (applicable_type == null) {
@@ -4648,7 +4644,7 @@ namespace Mono.CSharp {
                                // return by providing info about the closest match
                                //
                                int errors = Report.Errors;
-                               for (int i = 0; i < methods.Length; ++i) {
+                               for (int i = 0; i < nmethods; ++i) {
                                        MethodBase c = (MethodBase) methods [i];
                                        ParameterData pd = TypeManager.GetParameterData (c);
 
@@ -4935,8 +4931,13 @@ namespace Mono.CSharp {
                        return true;
                }
 
+               private bool resolved = false;
                public override Expression DoResolve (EmitContext ec)
                {
+                       if (resolved)
+                               return this.method == null ? null : this;
+
+                       resolved = true;
                        //
                        // First, resolve the expression that is used to
                        // trigger the invocation
@@ -4972,7 +4973,7 @@ namespace Mono.CSharp {
                        }
 
                        MethodGroupExpr mg = (MethodGroupExpr) expr;
-                       method = OverloadResolve (ec, mg, Arguments, false, loc);
+                       MethodBase method = OverloadResolve (ec, mg, Arguments, false, loc);
 
                        if (method == null)
                                return null;
@@ -5029,6 +5030,7 @@ namespace Mono.CSharp {
                                mg.InstanceExpression.CheckMarshallByRefAccess (ec.ContainerType);
 
                        eclass = ExprClass.Value;
+                       this.method = method;
                        return this;
                }
 
@@ -5173,14 +5175,14 @@ namespace Mono.CSharp {
                /// <summary>
                /// This checks the ConditionalAttribute on the method 
                /// </summary>
-               static bool IsMethodExcluded (MethodBase method, EmitContext ec)
+               static bool IsMethodExcluded (MethodBase method)
                {
                        if (method.IsConstructor)
                                return false;
 
                        IMethodData md = TypeManager.GetMethod (method);
                        if (md != null)
-                               return md.IsExcluded (ec);
+                               return md.IsExcluded ();
 
                        // For some methods (generated by delegate class) GetMethod returns null
                        // because they are not included in builder_to_method table
@@ -5249,7 +5251,7 @@ namespace Mono.CSharp {
                                        method = TypeManager.void_array_copyto_array_int;
                        }
 
-                       if (ec.TestObsoleteMethodUsage) {
+                       if (!ec.IsInObsoleteScope) {
                                //
                                // This checks ObsoleteAttribute on the method and on the declaring type
                                //
@@ -5264,7 +5266,7 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       if (IsMethodExcluded (method, ec))
+                       if (IsMethodExcluded (method))
                                return; 
                        
                        if (!is_static){
@@ -5691,7 +5693,7 @@ namespace Mono.CSharp {
                        MethodGroupExpr mg = ml as MethodGroupExpr;
 
                        if (mg == null) {
-                               ml.Error_UnexpectedKind (ec, "method group", loc);
+                               ml.Error_UnexpectedKind (ec.DeclContainer, "method group", loc);
                                return null;
                        }
 
@@ -5705,7 +5707,7 @@ namespace Mono.CSharp {
                        method = Invocation.OverloadResolve (ec, mg, Arguments, false, loc);
                        if (method == null) {
                                if (almostMatchedMembers.Count != 0)
-                                       MemberLookupFailed (ec, type, type, ".ctor", null, true, loc);
+                                       MemberLookupFailed (ec.ContainerType, type, type, ".ctor", null, true, loc);
                                return null;
                        }
 
@@ -6022,10 +6024,7 @@ namespace Mono.CSharp {
                public bool ValidateInitializers (EmitContext ec, Type array_type)
                {
                        if (initializers == null) {
-                               if (expect_initializers)
-                                       return false;
-                               else
-                                       return true;
+                               return !expect_initializers;
                        }
                        
                        if (underlying_type == null)
@@ -6038,17 +6037,12 @@ namespace Mono.CSharp {
                        array_data = new ArrayList ();
                        bounds = new Hashtable ();
                        
-                       bool ret;
-
                        if (arguments != null) {
-                               ret = CheckIndices (ec, initializers, 0, true);
-                               return ret;
+                               return CheckIndices (ec, initializers, 0, true);
                        } else {
                                arguments = new ArrayList ();
 
-                               ret = CheckIndices (ec, initializers, 0, false);
-                               
-                               if (!ret)
+                               if (!CheckIndices (ec, initializers, 0, false))
                                        return false;
                                
                                UpdateIndices (ec);
@@ -6058,7 +6052,7 @@ namespace Mono.CSharp {
                                        return false;
                                }
 
-                               return ret;
+                               return true;
                        }
                }
 
@@ -6089,12 +6083,7 @@ namespace Mono.CSharp {
                        if (array_type_expr == null)
                                return false;
 
-                       type = array_type_expr.ResolveType (ec);
-                       
-                       if (!type.IsArray) {
-                               Error (622, "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
-                               return false;
-                       }
+                       type = array_type_expr.ResolveType (ec);                
                        underlying_type = TypeManager.GetElementType (type);
                        dimensions = type.GetArrayRank ();
 
@@ -6149,11 +6138,11 @@ namespace Mono.CSharp {
                        if (is_builtin_type) {
                                Expression ml;
                                
-                               ml = MemberLookup (ec, type, ".ctor", MemberTypes.Constructor,
+                               ml = MemberLookup (ec.ContainerType, type, ".ctor", MemberTypes.Constructor,
                                                   AllBindingFlags, loc);
                                
                                if (!(ml is MethodGroupExpr)) {
-                                       ml.Error_UnexpectedKind (ec, "method group", loc);
+                                       ml.Error_UnexpectedKind (ec.DeclContainer, "method group", loc);
                                        return null;
                                }
                                
@@ -6521,32 +6510,33 @@ namespace Mono.CSharp {
                        }
                }
 
-               public object EncodeAsAttribute ()
+               public override bool GetAttributableValue (out object value)
                {
                        if (!is_one_dimensional){
-                               Report.Error (-211, Location, "attribute can not encode multi-dimensional arrays");
-                               return null;
+//                             Report.Error (-211, Location, "attribute can not encode multi-dimensional arrays");
+                               return base.GetAttributableValue (out value);
                        }
 
-                       if (array_data == null){
-                               Report.Error (-212, Location, "array should be initialized when passing it to an attribute");
-                               return null;
+                       if (array_data == null) {
+                               Constant c = (Constant)((Argument)arguments [0]).Expr;
+                               if (c.IsDefaultValue) {
+                                       value = new object [0];
+                                       return true;
+                               }
+//                             Report.Error (-212, Location, "array should be initialized when passing it to an attribute");
+                               return base.GetAttributableValue (out value);
                        }
                        
                        object [] ret = new object [array_data.Count];
-                       int i = 0;
-                       foreach (Expression e in array_data){
-                               object v;
-                               
-                               if (e is NullLiteral)
-                                       v = null;
-                               else {
-                                       if (!Attribute.GetAttributeArgumentExpression (e, Location, array_element_type, out v))
-                                               return null;
+                       for (int i = 0; i < ret.Length; ++i)
+                       {
+                               if (!((Expression)array_data [i]).GetAttributableValue (out ret [i])) {
+                                       value = null;
+                                       return false;
                                }
-                               ret [i++] = v;
                        }
-                       return ret;
+                       value = ret;
+                       return true;
                }
        }
        
@@ -6644,11 +6634,20 @@ namespace Mono.CSharp {
                        ILGenerator ig = ec.ig;
                        
                        if (ec.TypeContainer is Struct){
-                               ec.EmitThis ();
+                               ec.EmitThis (false);
                                source.Emit (ec);
-                               if (leave_copy)
+                               
+                               LocalTemporary t = null;
+                               if (leave_copy) {
+                                       t = new LocalTemporary (ec, type);
                                        ec.ig.Emit (OpCodes.Dup);
+                                       t.Store (ec);
+                               }
+
                                ig.Emit (OpCodes.Stobj, type);
+                               
+                               if (leave_copy)
+                                       t.Emit (ec);
                        } else {
                                throw new Exception ("how did you get here");
                        }
@@ -6658,7 +6657,7 @@ namespace Mono.CSharp {
                {
                        ILGenerator ig = ec.ig;
 
-                       ec.EmitThis ();
+                       ec.EmitThis (false);
                        if (ec.TypeContainer is Struct)
                                ig.Emit (OpCodes.Ldobj, type);
                }
@@ -6679,7 +6678,7 @@ namespace Mono.CSharp {
 
                public void AddressOf (EmitContext ec, AddressOp mode)
                {
-                       ec.EmitThis ();
+                       ec.EmitThis (true);
 
                        // FIMXE
                        // FIGURE OUT WHY LDARG_S does not work
@@ -6798,7 +6797,7 @@ namespace Mono.CSharp {
        ///   Implements the typeof operator
        /// </summary>
        public class TypeOf : Expression {
-               public Expression QueriedType;
+               readonly Expression QueriedType;
                protected Type typearg;
                
                public TypeOf (Expression queried_type, Location l)
@@ -6838,8 +6837,10 @@ namespace Mono.CSharp {
                        ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
                }
 
-               public Type TypeArg { 
-                       get { return typearg; }
+               public override bool GetAttributableValue (out object value)
+               {
+                       value = typearg;
+                       return true;
                }
        }
 
@@ -6928,13 +6929,13 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
-               public override FullNamedExpression ResolveAsTypeStep (EmitContext ec, bool silent)
+               public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
                {
                        if (alias == "global")
                                return new MemberAccess (RootNamespace.Global, identifier, loc).ResolveAsTypeStep (ec, silent);
 
                        int errors = Report.Errors;
-                       FullNamedExpression fne = ec.DeclSpace.NamespaceEntry.LookupAlias (alias);
+                       FullNamedExpression fne = ec.DeclContainer.NamespaceEntry.LookupAlias (alias);
                        if (fne == null) {
                                if (errors == Report.Errors)
                                        Report.Error (432, loc, "Alias `{0}' not found", alias);
@@ -6955,7 +6956,7 @@ namespace Mono.CSharp {
                                fne = RootNamespace.Global;
                        } else {
                                int errors = Report.Errors;
-                               fne = ec.DeclSpace.NamespaceEntry.LookupAlias (alias);
+                               fne = ec.DeclContainer.NamespaceEntry.LookupAlias (alias);
                                if (fne == null) {
                                        if (errors == Report.Errors)
                                                Report.Error (432, loc, "Alias `{0}' not found", alias);
@@ -7040,7 +7041,7 @@ namespace Mono.CSharp {
 
                        if (new_expr is Namespace) {
                                Namespace ns = (Namespace) new_expr;
-                               FullNamedExpression retval = ns.Lookup (ec.DeclSpace, Identifier, loc);
+                               FullNamedExpression retval = ns.Lookup (ec.DeclContainer, Identifier, loc);
                                if (retval == null)
                                        Report.Error (234, loc, "The type or namespace name `{0}' does not exist in the namespace `{1}'. Are you missing an assembly reference?",
                                                Identifier, ns.FullName);
@@ -7052,7 +7053,13 @@ namespace Mono.CSharp {
                                Error (23, "The `.' operator can not be applied to pointer operands (" +
                                       TypeManager.CSharpName (expr_type) + ")");
                                return null;
+                       } else if (expr_type == TypeManager.void_type) {
+                               Error (23, "The `.' operator can not be applied to operands of type 'void'");
+                               return null;
+                       } else if (expr_type == TypeManager.anonymous_method_type){
+                               Error (23, "The `.' operator can not be applied to anonymous methods");
                        }
+                       
 
                        Expression member_lookup;
                        member_lookup = MemberLookupFinal (ec, expr_type, expr_type, Identifier, loc);
@@ -7103,21 +7110,21 @@ namespace Mono.CSharp {
                        return DoResolve (ec, right_side);
                }
 
-               public override FullNamedExpression ResolveAsTypeStep (EmitContext ec, bool silent)
+               public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
                {
                        return ResolveNamespaceOrType (ec, silent);
                }
 
-               public FullNamedExpression ResolveNamespaceOrType (EmitContext ec, bool silent)
+               public FullNamedExpression ResolveNamespaceOrType (IResolveContext rc, bool silent)
                {
-                       FullNamedExpression new_expr = expr.ResolveAsTypeStep (ec, silent);
+                       FullNamedExpression new_expr = expr.ResolveAsTypeStep (rc, silent);
 
                        if (new_expr == null)
                                return null;
 
                        if (new_expr is Namespace) {
                                Namespace ns = (Namespace) new_expr;
-                               FullNamedExpression retval = ns.Lookup (ec.DeclSpace, Identifier, loc);
+                               FullNamedExpression retval = ns.Lookup (rc.DeclContainer, Identifier, loc);
                                if (!silent && retval == null)
                                        Report.Error (234, loc, "The type or namespace name `{0}' does not exist in the namespace `{1}'. Are you missing an assembly reference?",
                                                Identifier, ns.FullName);
@@ -7132,10 +7139,10 @@ namespace Mono.CSharp {
                                return null;
                        }
                        
-                       Expression member_lookup = MemberLookup (ec, expr_type, expr_type, Identifier, loc);
+                       Expression member_lookup = MemberLookup (rc.DeclContainer.TypeBuilder, expr_type, expr_type, Identifier, loc);
                        if (member_lookup == null) {
                                int errors = Report.Errors;
-                               MemberLookupFailed (ec, expr_type, expr_type, Identifier, null, false, loc);
+                               MemberLookupFailed (rc.DeclContainer.TypeBuilder, expr_type, expr_type, Identifier, null, false, loc);
 
                                if (!silent && errors == Report.Errors) {
                                        Report.Error (426, loc, "The nested type `{0}' does not exist in the type `{1}'",
@@ -7145,11 +7152,11 @@ namespace Mono.CSharp {
                        }
 
                        if (!(member_lookup is TypeExpr)) {
-                               new_expr.Error_UnexpectedKind (ec, "type", loc);
+                               new_expr.Error_UnexpectedKind (rc.DeclContainer, "type", loc);
                                return null;
                        } 
 
-                       member_lookup = member_lookup.Resolve (ec, ResolveFlags.Type);
+                       member_lookup = member_lookup.ResolveAsTypeTerminal (rc, silent);
                        return (member_lookup as TypeExpr);
                }
 
@@ -7956,6 +7963,19 @@ namespace Mono.CSharp {
 
                public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
                {
+                       if (right_side == EmptyExpression.OutAccess) {
+                               Report.Error (206, loc, "A property or indexer `{0}' may not be passed as an out or ref parameter",
+                                             GetSignatureForError ());
+                               return null;
+                       }
+
+                       // if the indexer returns a value type, and we try to set a field in it
+                       if (right_side == EmptyExpression.LValueMemberAccess) {
+                               Report.Error (1612, loc, "Cannot modify the return value of `{0}' because it is not a variable",
+                                             GetSignatureForError ());
+                               return null;
+                       }
+
                        ArrayList AllSetters = new ArrayList();
                        if (!CommonResolve (ec))
                                return null;
@@ -8069,6 +8089,12 @@ namespace Mono.CSharp {
                {
                        Emit (ec, false);
                }
+
+               public override string GetSignatureForError ()
+               {
+                       // FIXME: print the argument list of the indexer
+                       return instance_expr.GetSignatureForError () + ".this[...]";
+               }
        }
 
        /// <summary>
@@ -8130,10 +8156,10 @@ namespace Mono.CSharp {
                                return null;
                        }
                        
-                       member_lookup = MemberLookup (ec, ec.ContainerType, null, base_type, member,
+                       member_lookup = MemberLookup (ec.ContainerType, null, base_type, member,
                                                      AllMemberTypes, AllBindingFlags, loc);
                        if (member_lookup == null) {
-                               MemberLookupFailed (ec, base_type, base_type, member, null, true, loc);
+                               MemberLookupFailed (ec.ContainerType, base_type, base_type, member, null, true, loc);
                                return null;
                        }
 
@@ -8207,6 +8233,9 @@ namespace Mono.CSharp {
        public class EmptyExpression : Expression {
                public static readonly EmptyExpression Null = new EmptyExpression ();
 
+               public static readonly EmptyExpression OutAccess = new EmptyExpression ();
+               public static readonly EmptyExpression LValueMemberAccess = new EmptyExpression ();
+
                static EmptyExpression temp = new EmptyExpression ();
                public static EmptyExpression Grab ()
                {
@@ -8323,17 +8352,13 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
-               public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
+               public override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
                {
                        TypeExpr lexpr = left.ResolveAsTypeTerminal (ec, false);
                        if (lexpr == null)
                                return null;
 
-                       bool old = ec.TestObsoleteMethodUsage;
-                       ec.TestObsoleteMethodUsage = false;
                        Type ltype = lexpr.ResolveType (ec);
-                       ec.TestObsoleteMethodUsage = old;
-
                        if ((ltype == TypeManager.void_type) && (dim != "*")) {
                                Report.Error (1547, Location,
                                              "Keyword 'void' cannot be used in this context");
@@ -8349,7 +8374,7 @@ namespace Mono.CSharp {
                                throw new InternalErrorException ("Couldn't create computed type " + ltype + dim);
                        }
 
-                       if (!ec.InUnsafe && type.IsPointer){
+                       if (type.IsPointer && !ec.IsInUnsafeScope){
                                UnsafeError (loc);
                                return null;
                        }
@@ -8365,15 +8390,16 @@ namespace Mono.CSharp {
                }
 
                public override string Name {
-                       get {
-                               return left + dim;
-                       }
+                       get { return left + dim; }
                }
 
                public override string FullName {
-                       get {
-                               return type.FullName;
-                       }
+                       get { return type.FullName; }
+               }
+
+               public override string GetSignatureForError ()
+               {
+                       return left.GetSignatureForError () + dim;
                }
        }