*** merged revision 57555 from mcs
[mono.git] / mcs / gmcs / expression.cs
index 01ca8d8526c76947fae34f0e0c4d760e792a0c6a..32dce23aa9767212dfac6dd09c71341471cf1ff0 100644 (file)
@@ -3,6 +3,7 @@
 //
 // Author:
 //   Miguel de Icaza (miguel@ximian.com)
+//   Marek Safar (marek.safar@seznam.cz)
 //
 // (C) 2001, 2002, 2003 Ximian, Inc.
 // (C) 2003, 2004 Novell, Inc.
@@ -94,7 +95,6 @@ namespace Mono.CSharp {
                public ParenthesizedExpression (Expression expr)
                {
                        this.Expr = expr;
-                       this.loc = expr.Location;
                }
 
                public override Expression DoResolve (EmitContext ec)
@@ -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 (
@@ -458,8 +458,9 @@ namespace Mono.CSharp {
 
                                // According to the specs, a variable is considered definitely assigned if you take
                                // its address.
-                               if ((variable != null) && (variable.VariableInfo != null))
+                               if ((variable != null) && (variable.VariableInfo != null)){
                                        variable.VariableInfo.SetAssigned (ec);
+                               }
 
                                type = TypeManager.GetPointerType (Expr.Type);
                                return this;
@@ -591,7 +592,7 @@ namespace Mono.CSharp {
                        if (Expr == null)
                                return null;
 
-                       if (TypeManager.IsNullableType (Expr.Type))
+                       if (TypeManager.IsNullableValueType (Expr.Type))
                                return new Nullable.LiftedUnaryOperator (Oper, Expr, loc).Resolve (ec);
 
                        eclass = ExprClass.Value;
@@ -615,15 +616,15 @@ 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);
                                        Expr.Emit (ec);
                                        ig.Emit (OpCodes.Sub_Ovf);
                                } else {
-                               Expr.Emit (ec);
-                               ig.Emit (OpCodes.Neg);
+                                       Expr.Emit (ec);
+                                       ig.Emit (OpCodes.Neg);
                                }
                                
                                break;
@@ -686,7 +687,7 @@ namespace Mono.CSharp {
                {
                        if (!prepared)
                                expr.Emit (ec);
-
+                       
                        LoadFromPtr (ec.ig, Type);
                }
 
@@ -699,29 +700,29 @@ namespace Mono.CSharp {
                                temporary.Store (ec);
                        }
                }
-
+               
                public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
                {
                        prepared = prepare_for_load;
-
+                       
                        expr.Emit (ec);
 
                        if (prepare_for_load)
                                ec.ig.Emit (OpCodes.Dup);
-
+                       
                        source.Emit (ec);
                        if (leave_copy) {
                                ec.ig.Emit (OpCodes.Dup);
                                temporary = new LocalTemporary (ec, expr.Type);
                                temporary.Store (ec);
                        }
-
+                       
                        StoreFromPtr (ec.ig, type);
-
+                       
                        if (temporary != null)
                                temporary.Emit (ec);
                }
-
+               
                public void AddressOf (EmitContext ec, AddressOp Mode)
                {
                        expr.Emit (ec);
@@ -852,7 +853,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 (
@@ -882,7 +883,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;
                        }
 
@@ -898,7 +899,7 @@ namespace Mono.CSharp {
 
                        eclass = ExprClass.Value;
 
-                       if (TypeManager.IsNullableType (expr.Type))
+                       if (TypeManager.IsNullableValueType (expr.Type))
                                return new Nullable.LiftedUnaryMutator (mode, expr, loc).Resolve (ec);
 
                        return ResolveOperator (ec);
@@ -1051,10 +1052,9 @@ namespace Mono.CSharp {
 
                public override Expression DoResolve (EmitContext ec)
                {
-                       probe_type_expr = ProbeType.ResolveAsTypeTerminal (ec);
+                       probe_type_expr = ProbeType.ResolveAsTypeTerminal (ec, false);
                        if (probe_type_expr == null)
                                return null;
-                       Type probe_type = probe_type_expr.ResolveType (ec);
 
                        expr = expr.Resolve (ec);
                        if (expr == null)
@@ -1182,6 +1182,9 @@ namespace Mono.CSharp {
                                        action = Action.AlwaysFalse;
                                else
                                        action = Action.Probe;
+                       } else if (etype.ContainsGenericParameters || probe_type.ContainsGenericParameters) {
+                               expr = new BoxedCast (expr, etype);
+                               action = Action.Probe;
                        } else {
                                action = Action.AlwaysFalse;
                                warning_never_matches = true;
@@ -1266,7 +1269,7 @@ namespace Mono.CSharp {
                                if (error){
                                        Report.Error (413, loc,
                                                      "The as operator requires that the `{0}' type parameter be constrained by a class",
-                                                     probe_type_expr);
+                                                     probe_type_expr.GetSignatureForError ());
                                        return null;
                                }
                        }
@@ -1286,9 +1289,20 @@ namespace Mono.CSharp {
                                return this;
                        }
 
+                       if (etype.ContainsGenericParameters || type.ContainsGenericParameters) {
+                               expr = new BoxedCast (expr, etype);
+                               do_isinst = true;
+                               return this;
+                       }
+
                        Error_CannotConvertType (etype, type, loc);
                        return null;
-               }                               
+               }
+       
+               public override bool GetAttributableValue (out object value)
+               {
+                       return expr.GetAttributableValue (out value);
+               }
        }
        
        /// <summary>
@@ -1311,6 +1325,10 @@ namespace Mono.CSharp {
                        this.target_type = cast_type;
                        this.expr = expr;
                        this.loc = loc;
+
+                       if (target_type == TypeManager.system_void_expr) {
+                               Report.Error (1547, loc, "Keyword `void' cannot be used in this context");
+                       }
                }
 
                public Expression TargetType {
@@ -1348,11 +1366,11 @@ namespace Mono.CSharp {
 
                Expression ResolveRest (EmitContext ec)
                {
-                       TypeExpr target = target_type.ResolveAsTypeTerminal (ec);
+                       TypeExpr target = target_type.ResolveAsTypeTerminal (ec, false);
                        if (target == null)
                                return null;
                        
-                       type = target.ResolveType (ec);
+                       type = target.Type;
 
                        if (type.IsAbstract && type.IsSealed) {
                                Report.Error (716, loc, "Cannot convert to static type `{0}'", TypeManager.CSharpName (type));
@@ -1363,9 +1381,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) {
@@ -1610,7 +1633,7 @@ namespace Mono.CSharp {
                                                        if (ll >= 0)
                                                                right = new ULongConstant ((ulong) ll, right.Location);
                                                } else {
-                                                       e = Convert.ImplicitNumericConversion (ec, right, l);
+                                                       e = Convert.ImplicitNumericConversion (right, l);
                                                        if (e != null)
                                                                right = e;
                                                }
@@ -1627,7 +1650,7 @@ namespace Mono.CSharp {
                                                if (ll > 0)
                                                        left = new ULongConstant ((ulong) ll, right.Location);
                                        } else {
-                                               e = Convert.ImplicitNumericConversion (ec, left, r);
+                                               e = Convert.ImplicitNumericConversion (left, r);
                                                if (e != null)
                                                        left = e;
                                        }
@@ -1889,10 +1912,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;
@@ -1924,7 +1947,7 @@ namespace Mono.CSharp {
                                // Simple constant folding
                                if (left is StringConstant && right is StringConstant)
                                        return new StringConstant (((StringConstant) left).Value + ((StringConstant) right).Value, left.Location);
-
+                               
                                if (l == TypeManager.string_type || r == TypeManager.string_type) {
 
                                        if (r == TypeManager.void_type || l == TypeManager.void_type) {
@@ -1947,7 +1970,7 @@ namespace Mono.CSharp {
                                                        return left;
                                                }
                                        }
-
+                                       
                                        // Otherwise, start a new concat expression
                                        return new StringConcat (ec, loc, left, right).Resolve (ec);
                                }
@@ -2028,9 +2051,9 @@ namespace Mono.CSharp {
                                        // Also, a standard conversion must exist from either one
                                        //
                                        bool left_to_right =
-                                               Convert.ImplicitStandardConversionExists (ec, left, r);
+                                               Convert.ImplicitStandardConversionExists (left, r);
                                        bool right_to_left = !left_to_right &&
-                                               Convert.ImplicitStandardConversionExists (ec, right, l);
+                                               Convert.ImplicitStandardConversionExists (right, l);
 
                                        if (!left_to_right && !right_to_left) {
                                                Error_OperatorCannotBeApplied ();
@@ -2169,13 +2192,13 @@ namespace Mono.CSharp {
                                // U operator - (E e, E f)
                                if (lie && rie){
                                        if (oper == Operator.Subtraction){
-                                       if (l == r){
-                                               type = TypeManager.EnumToUnderlying (l);
-                                               return this;
-                                       } 
-                                       Error_OperatorCannotBeApplied ();
-                                       return null;
-                               }
+                                               if (l == r){
+                                                       type = TypeManager.EnumToUnderlying (l);
+                                                       return this;
+                                               }
+                                               Error_OperatorCannotBeApplied ();
+                                               return null;
+                                       }
                                }
                                        
                                //
@@ -2382,6 +2405,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);
@@ -2415,7 +2441,8 @@ namespace Mono.CSharp {
                        eclass = ExprClass.Value;
                        Constant rc = right as Constant;
 
-                       if (lc != null && rc != null && (TypeManager.IsEnumType (left.Type) || TypeManager.IsEnumType (right.Type))) {
+                       // The conversion rules are ignored in enum context but why
+                       if (!ec.InEnumContext && lc != null && rc != null && (TypeManager.IsEnumType (left.Type) || TypeManager.IsEnumType (right.Type))) {
                                left = lc = EnumLiftUp (ec, lc, rc);
                                if (lc == null)
                                        return null;
@@ -2445,6 +2472,11 @@ namespace Mono.CSharp {
                                if (rc is EnumConstant &&
                                    lc != null && lc.IsZeroInteger)
                                        return rc;
+                       } else if (oper == Operator.LogicalAnd) {
+                               if (rc != null && rc.IsDefaultValue && rc.Type == TypeManager.bool_type)
+                                       return rc;
+                               if (lc != null && lc.IsDefaultValue && lc.Type == TypeManager.bool_type)
+                                       return lc;
                        }
 
                        if (rc != null && lc != null){
@@ -2455,23 +2487,30 @@ namespace Mono.CSharp {
                                        return e;
                        }
 
-                       if (TypeManager.IsNullableType (left.Type) || TypeManager.IsNullableType (right.Type))
+                       Type ltype = left.Type, rtype = right.Type;
+                       if (ltype.IsValueType && rtype.IsValueType &&
+                           (TypeManager.IsNullableType (ltype) || TypeManager.IsNullableType (rtype)))
                                return new Nullable.LiftedBinaryOperator (oper, left, right, loc).Resolve (ec);
 
-                       // Check CS0652 warning here (before resolving operator).
-                       if (oper == Operator.Equality ||
-                           oper == Operator.Inequality ||
-                           oper == Operator.LessThanOrEqual ||
-                           oper == Operator.LessThan ||
-                           oper == Operator.GreaterThanOrEqual ||
-                           oper == Operator.GreaterThan){
-                               CheckUselessComparison (left as Constant, right.Type);
-                               CheckUselessComparison (right as Constant, left.Type);
+                       // Comparison warnings
+                       if (oper == Operator.Equality || oper == Operator.Inequality ||
+                           oper == Operator.LessThanOrEqual || oper == Operator.LessThan ||
+                           oper == Operator.GreaterThanOrEqual || oper == Operator.GreaterThan){
+                               if (left.Equals (right)) {
+                                       Report.Warning (1718, 3, loc, "Comparison made to same variable; did you mean to compare something else?");
+                               }
+                               CheckUselessComparison (lc, right.Type);
+                               CheckUselessComparison (rc, left.Type);
                        }
 
                        return ResolveOperator (ec);
                }
 
+               public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
+               {
+                       return null;
+               }
+
                private void CheckUselessComparison (Constant c, Type type)
                {
                        if (c == null || !IsTypeIntegral (type)
@@ -2590,7 +2629,7 @@ namespace Mono.CSharp {
                                        right = left;
                                        left = swap;
                                }
-                                       
+                               
                                if (((Constant) right).IsZeroInteger) {
                                        left.Emit (ec);
                                        if (my_on_true)
@@ -2599,7 +2638,7 @@ namespace Mono.CSharp {
                                                ig.Emit (OpCodes.Brfalse, target);
                                        
                                        return;
-                               } else if (right is BoolConstant){
+                               } else if (right is BoolConstant) {
                                        left.Emit (ec);
                                        if (my_on_true != ((BoolConstant) right).Value)
                                                ig.Emit (OpCodes.Brtrue, target);
@@ -2612,45 +2651,45 @@ namespace Mono.CSharp {
                        } else if (oper == Operator.LogicalAnd) {
 
                                if (onTrue) {
-                                               Label tests_end = ig.DefineLabel ();
-                                               
+                                       Label tests_end = ig.DefineLabel ();
+                                       
                                        left.EmitBranchable (ec, tests_end, false);
                                        right.EmitBranchable (ec, target, true);
-                                                       ig.MarkLabel (tests_end);
-                                       } else {
+                                       ig.MarkLabel (tests_end);                                       
+                               } else {
                                        left.EmitBranchable (ec, target, false);
                                        right.EmitBranchable (ec, target, false);
-                                       }
-
+                               }
+                               
                                return;
-                                                               
+                               
                        } else if (oper == Operator.LogicalOr){
                                if (onTrue) {
                                        left.EmitBranchable (ec, target, true);
                                        right.EmitBranchable (ec, target, true);
-                                               
-                                       } else {
-                                               Label tests_end = ig.DefineLabel ();
+                                       
+                               } else {
+                                       Label tests_end = ig.DefineLabel ();
                                        left.EmitBranchable (ec, tests_end, true);
                                        right.EmitBranchable (ec, target, false);
                                        ig.MarkLabel (tests_end);
                                }
-                                               
+                               
                                return;
-
+                               
                        } else if (!(oper == Operator.LessThan        || oper == Operator.GreaterThan ||
                                     oper == Operator.LessThanOrEqual || oper == Operator.GreaterThanOrEqual ||
                                     oper == Operator.Equality        || oper == Operator.Inequality)) {
                                base.EmitBranchable (ec, target, onTrue);
                                return;
-                               }
-                               
+                       }
+                       
                        left.Emit (ec);
                        right.Emit (ec);
 
                        Type t = left.Type;
                        bool isUnsigned = is_unsigned (t) || t == TypeManager.double_type || t == TypeManager.float_type;
-
+                       
                        switch (oper){
                        case Operator.Equality:
                                if (onTrue)
@@ -2737,11 +2776,11 @@ namespace Mono.CSharp {
                        if (oper == Operator.LogicalAnd) {
                                Label load_zero = ig.DefineLabel ();
                                Label end = ig.DefineLabel ();
-
+                                                               
                                left.EmitBranchable (ec, load_zero, false);
-                                               right.Emit (ec);
-                                               ig.Emit (OpCodes.Br, end);
-
+                               right.Emit (ec);
+                               ig.Emit (OpCodes.Br, end);
+                               
                                ig.MarkLabel (load_zero);
                                ig.Emit (OpCodes.Ldc_I4_0);
                                ig.MarkLabel (end);
@@ -2749,11 +2788,11 @@ namespace Mono.CSharp {
                        } else if (oper == Operator.LogicalOr) {
                                Label load_one = ig.DefineLabel ();
                                Label end = ig.DefineLabel ();
-                               
-                               left.EmitBranchable (ec, load_one, true);
-                                               right.Emit (ec);
-                                               ig.Emit (OpCodes.Br, end);
 
+                               left.EmitBranchable (ec, load_one, true);
+                               right.Emit (ec);
+                               ig.Emit (OpCodes.Br, end);
+                               
                                ig.MarkLabel (load_one);
                                ig.Emit (OpCodes.Ldc_I4_1);
                                ig.MarkLabel (end);
@@ -2933,7 +2972,7 @@ namespace Mono.CSharp {
                                ig.Emit (OpCodes.Call, (ConstructorInfo) method);
                }
        }
-
+       
        //
        // Represents the operation a + b [+ c [+ d [+ ...]]], where a is a string
        // b, c, d... may be strings or objects.
@@ -3006,23 +3045,23 @@ namespace Mono.CSharp {
                        // This can get called multiple times, so we have to deal with that.
                        if (!emit_conv_done) {
                                emit_conv_done = true;
-                       for (int i = 0; i < operands.Count; i ++) {
-                               Expression e = (Expression) operands [i];
-                               is_strings_only &= e.Type == TypeManager.string_type;
-                       }
-                       
-                       for (int i = 0; i < operands.Count; i ++) {
-                               Expression e = (Expression) operands [i];
+                               for (int i = 0; i < operands.Count; i ++) {
+                                       Expression e = (Expression) operands [i];
+                                       is_strings_only &= e.Type == TypeManager.string_type;
+                               }
                                
-                               if (! is_strings_only && e.Type == TypeManager.string_type) {
-                                       // need to make sure this is an object, because the EmitParams
-                                       // method might look at the type of this expression, see it is a
-                                       // string and emit a string [] when we want an object [];
+                               for (int i = 0; i < operands.Count; i ++) {
+                                       Expression e = (Expression) operands [i];
                                        
+                                       if (! is_strings_only && e.Type == TypeManager.string_type) {
+                                               // need to make sure this is an object, because the EmitParams
+                                               // method might look at the type of this expression, see it is a
+                                               // string and emit a string [] when we want an object [];
+                                               
                                                e = new EmptyCast (e, TypeManager.object_type);
+                                       }
+                                       operands [i] = new Argument (e, Argument.AType.Expression);
                                }
-                               operands [i] = new Argument (e, Argument.AType.Expression);
-                       }
                        }
                        
                        //
@@ -3338,7 +3377,7 @@ namespace Mono.CSharp {
                        if (expr == null)
                                return null;
 
-                       if (TypeManager.IsNullableType (expr.Type))
+                       if (TypeManager.IsNullableValueType (expr.Type))
                                return new Nullable.LiftedConditional (expr, trueExpr, falseExpr, loc).Resolve (ec);
                        
                        if (expr.Type != TypeManager.bool_type){
@@ -3408,6 +3447,11 @@ namespace Mono.CSharp {
                        return this;
                }
 
+               public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
+               {
+                       return null;
+               }
+
                public override void Emit (EmitContext ec)
                {
                        ILGenerator ig = ec.ig;
@@ -3479,7 +3523,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;
@@ -3490,15 +3534,18 @@ 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 ());
                                        return null;
                                }
-
+                               
                                if (variable_info != null)
                                        variable_info.SetAssigned (ec);
                        }
@@ -3531,10 +3578,10 @@ namespace Mono.CSharp {
                                        ec.CaptureVariable (local_info);
                                }
                        }
-                       
+
                        return this;
                }
-
+               
                public override Expression DoResolve (EmitContext ec)
                {
                        return DoResolveBase (ec, null);
@@ -3636,12 +3683,12 @@ namespace Mono.CSharp {
                public void AddressOf (EmitContext ec, AddressOp mode)
                {
                        ILGenerator ig = ec.ig;
-                       
+
                        if (local_info.FieldBuilder == null){
                                //
                                // A local variable on the local CLR stack
                                //
-                       ig.Emit (OpCodes.Ldloca, local_info.LocalBuilder);
+                               ig.Emit (OpCodes.Ldloca, local_info.LocalBuilder);
                        } else {
                                //
                                // A local variable captured by anonymous methods or iterators
@@ -3662,12 +3709,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 {
@@ -3684,20 +3730,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; }
                }
@@ -3705,7 +3747,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)
@@ -3714,7 +3756,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;
                }
 
@@ -3740,21 +3782,26 @@ namespace Mono.CSharp {
                                ec.CurrentBranching.SetFieldAssigned (vi, field_name);
                }
 
-               protected void DoResolveBase (EmitContext ec)
+               protected bool 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);
-                                       return;
+                                               par.Name);
+                                       return false;
                                }
 
                                //
@@ -3766,6 +3813,8 @@ namespace Mono.CSharp {
                                        ec.CaptureParameter (name, type, idx);
                                }
                        }
+
+                       return true;
                }
 
                public override int GetHashCode()
@@ -3796,7 +3845,8 @@ namespace Mono.CSharp {
                //
                public override Expression DoResolve (EmitContext ec)
                {
-                       DoResolveBase (ec);
+                       if (!DoResolveBase (ec))
+                               return null;
 
                        if (is_out && ec.DoFlowAnalysis && (!ec.OmitStructFlowAnalysis || !vi.TypeInfo.IsStruct) && !IsAssigned (ec, loc))
                                return null;
@@ -3806,7 +3856,8 @@ namespace Mono.CSharp {
 
                override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
                {
-                       DoResolveBase (ec);
+                       if (!DoResolveBase (ec))
+                               return null;
 
                        SetAssigned (ec);
 
@@ -3858,11 +3909,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;
                        }
 
@@ -3894,15 +3942,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++;
@@ -3958,6 +4007,10 @@ namespace Mono.CSharp {
                        }
                }
 
+               public override string ToString ()
+               {
+                       return "ParameterReference[" + name + "]";
+               }
        }
        
        /// <summary>
@@ -4000,10 +4053,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;
@@ -4055,15 +4108,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
@@ -4096,20 +4151,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;
                }
@@ -4205,10 +4246,10 @@ namespace Mono.CSharp {
                                return null;
 
                        if (argument_expr is NullLiteral) {
-                       //
+                               //
                                // If the argument is null and one of the types to compare is 'object' and
                                // the other is a reference type, we prefer the other.
-                       //
+                               //
                                // This follows from the usual rules:
                                //   * There is an implicit conversion from 'null' to type 'object'
                                //   * There is an implicit conversion from 'null' to any reference type
@@ -4225,7 +4266,7 @@ namespace Mono.CSharp {
                                if (!q.IsValueType && p == TypeManager.object_type)
                                        return q;
                        }
-                       
+                                
                        if (argument_type == p)
                                return p;
 
@@ -4234,7 +4275,7 @@ namespace Mono.CSharp {
 
                        Expression p_tmp = new EmptyExpression (p);
                        Expression q_tmp = new EmptyExpression (q);
-                       
+
                        bool p_to_q = Convert.ImplicitConversionExists (ec, p_tmp, q);
                        bool q_to_p = Convert.ImplicitConversionExists (ec, q_tmp, p);
 
@@ -4257,7 +4298,6 @@ namespace Mono.CSharp {
                                if (q == TypeManager.ushort_type || q == TypeManager.uint32_type ||
                                    q == TypeManager.uint64_type)
                                        return p;
-
                        if (q == TypeManager.short_type)
                                if (p == TypeManager.ushort_type || p == TypeManager.uint32_type ||
                                    p == TypeManager.uint64_type)
@@ -4266,7 +4306,6 @@ namespace Mono.CSharp {
                        if (p == TypeManager.int32_type)
                                if (q == TypeManager.uint32_type || q == TypeManager.uint64_type)
                                        return p;
-
                        if (q == TypeManager.int32_type)
                                if (p == TypeManager.uint32_type || p == TypeManager.uint64_type)
                                        return q;
@@ -4280,6 +4319,45 @@ namespace Mono.CSharp {
 
                        return null;
                }
+
+               static Type MoreSpecific (Type p, Type q)
+               {
+                       if (p.IsGenericParameter && !q.IsGenericParameter)
+                               return q;
+                       if (!p.IsGenericParameter && q.IsGenericParameter)
+                               return p;
+
+                       if (p.IsGenericType) {
+                               Type[] pargs = TypeManager.GetTypeArguments (p);
+                               Type[] qargs = TypeManager.GetTypeArguments (q);
+
+                               bool p_specific_at_least_once = false;
+                               bool q_specific_at_least_once = false;
+
+                               for (int i = 0; i < pargs.Length; i++) {
+                                       Type specific = MoreSpecific (pargs [i], qargs [i]);
+                                       if (specific == pargs [i])
+                                               p_specific_at_least_once = true;
+                                       if (specific == qargs [i])
+                                               q_specific_at_least_once = true;
+                               }
+
+                               if (p_specific_at_least_once && !q_specific_at_least_once)
+                                       return p;
+                               if (!p_specific_at_least_once && q_specific_at_least_once)
+                                       return q;
+                       } else if (TypeManager.HasElementType (p)) {
+                               Type pe = TypeManager.GetElementType (p);
+                               Type qe = TypeManager.GetElementType (q);
+                               Type specific = MoreSpecific (pe, qe);
+                               if (specific == pe)
+                                       return p;
+                               if (specific == qe)
+                                       return q;
+                       }
+
+                       return null;
+               }
                
                /// <summary>
                ///   Determines "Better function" between candidate
@@ -4291,8 +4369,8 @@ namespace Mono.CSharp {
                ///     true  if candidate is better than the current best match
                /// </remarks>
                static bool BetterFunction (EmitContext ec, ArrayList args, int argument_count,
-                                          MethodBase candidate, bool candidate_params,
-                                          MethodBase best, bool best_params, Location loc)
+                                           MethodBase candidate, bool candidate_params,
+                                           MethodBase best, bool best_params, Location loc)
                {
                        ParameterData candidate_pd = TypeManager.GetParameterData (candidate);
                        ParameterData best_pd = TypeManager.GetParameterData (best);
@@ -4318,11 +4396,12 @@ namespace Mono.CSharp {
 
                                same = false;
                                Type better = BetterConversion (ec, a, ct, bt, loc);
+
                                // for each argument, the conversion to 'ct' should be no worse than 
                                // the conversion to 'bt'.
                                if (better == bt)
                                        return false;
-                               
+
                                // for at least one argument, the conversion to 'ct' should be better than 
                                // the conversion to 'bt'.
                                if (better == ct)
@@ -4332,20 +4411,7 @@ namespace Mono.CSharp {
                        if (better_at_least_one)
                                return true;
 
-                       if (!same)
-                               return false;
-
-                       //
-                       // If two methods have equal parameter types, but
-                       // only one of them is generic, the non-generic one wins.
-                       //
-                       if (TypeManager.IsGenericMethod (best) && !TypeManager.IsGenericMethod (candidate))
-                               return true;
-                       else if (!TypeManager.IsGenericMethod (best) && TypeManager.IsGenericMethod (candidate))
-                               return false;
-
                        //
-                       // Note that this is not just an optimization.  This handles the case
                        // This handles the case
                        //
                        //   Add (float f1, float f2, float f3);
@@ -4354,17 +4420,71 @@ namespace Mono.CSharp {
                        // The call Add (3, 4, 5) should be ambiguous.  Without this check, the
                        // first candidate would've chosen as better.
                        //
+                       if (!same)
+                               return false;
+
+                       //
+                       // The two methods have equal parameter types.  Now apply tie-breaking rules
+                       //
+                       if (TypeManager.IsGenericMethod (best) && !TypeManager.IsGenericMethod (candidate))
+                               return true;
+                       if (!TypeManager.IsGenericMethod (best) && TypeManager.IsGenericMethod (candidate))
+                               return false;
+
                        //
                        // This handles the following cases:
                        //
                        //   Trim () is better than Trim (params char[] chars)
                        //   Concat (string s1, string s2, string s3) is better than
                        //     Concat (string s1, params string [] srest)
+                       //   Foo (int, params int [] rest) is better than Foo (params int [] rest)
                        //
-                        return !candidate_params && best_params;
+                       if (!candidate_params && best_params)
+                               return true;
+                       if (candidate_params && !best_params)
+                               return false;
+
+                       int candidate_param_count = candidate_pd.Count;
+                       int best_param_count = best_pd.Count;
+
+                       if (candidate_param_count != best_param_count)
+                               // can only happen if (candidate_params && best_params)
+                               return candidate_param_count > best_param_count;
+
+                       //
+                       // now, both methods have the same number of parameters, and the parameters have the same types
+                       // Pick the "more specific" signature
+                       //
+
+                       MethodBase orig_candidate = TypeManager.DropGenericMethodArguments (candidate);
+                       MethodBase orig_best = TypeManager.DropGenericMethodArguments (best);
+
+                       ParameterData orig_candidate_pd = TypeManager.GetParameterData (orig_candidate);
+                       ParameterData orig_best_pd = TypeManager.GetParameterData (orig_best);
+
+                       bool specific_at_least_once = false;
+                       for (int j = 0; j < candidate_param_count; ++j) {
+                               Type ct = TypeManager.TypeToCoreType (orig_candidate_pd.ParameterType (j));
+                               Type bt = TypeManager.TypeToCoreType (orig_best_pd.ParameterType (j));
+                               if (ct.Equals (bt))
+                                       continue;
+                               Type specific = MoreSpecific (ct, bt);
+                               if (specific == bt)
+                                       return false;
+                               if (specific == ct)
+                                       specific_at_least_once = true;
+                       }
+
+                       if (specific_at_least_once)
+                               return true;
+
+                       // FIXME: handle lifted operators
+                       // ...
+
+                       return false;
                }
 
-               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;
@@ -4483,12 +4603,11 @@ namespace Mono.CSharp {
                                                      bool do_varargs)
                {
                        ParameterData pd = TypeManager.GetParameterData (candidate);
-                       
-                       int pd_count = pd.Count;
 
+                       int pd_count = pd.Count;
                        if (pd_count == 0)
                                return false;
-                       
+
                        int count = pd_count - 1;
                        if (do_varargs) {
                                if (pd.ParameterModifier (count) != Parameter.Modifier.ARGLIST)
@@ -4496,8 +4615,8 @@ namespace Mono.CSharp {
                                if (pd_count != arg_count)
                                        return false;
                        } else {
-                               if (pd.ParameterModifier (count) != Parameter.Modifier.PARAMS)
-                               return false;
+                               if (!pd.HasParams)
+                                       return false;
                        }
                        
                        if (count > arg_count)
@@ -4516,9 +4635,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) {
 
@@ -4526,8 +4645,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);
 
@@ -4567,7 +4686,7 @@ namespace Mono.CSharp {
                                                 ref MethodBase candidate)
                {
                        if (!me.HasTypeArguments &&
-                           !TypeManager.InferTypeArguments (ec, arguments, ref candidate))
+                           !TypeManager.InferTypeArguments (arguments, ref candidate))
                                return false;
 
                        return IsApplicable (ec, arguments, arg_count, candidate);
@@ -4577,8 +4696,8 @@ namespace Mono.CSharp {
                ///   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);
 
@@ -4591,39 +4710,34 @@ 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 (!TypeManager.IsEqual (a.Type, pd.ParameterType (i)) && !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 (!TypeManager.IsEqual (a.Type, pt) &&
+                                                   !Convert.ImplicitConversionExists (ec, a.Expr, pt))
                                                        return false;
+                                               continue;
                                        }
+                                       
+                                       if (pt != a.Type)
+                                               return false;
                                } else
                                        return false;
                        }
 
                        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) ||
+                               (TypeManager.IsSubclassOf (second_type, first_type) ||
                                 TypeManager.ImplementsInterface (second_type, first_type));
                }
                
@@ -4665,7 +4779,7 @@ namespace Mono.CSharp {
 
                        if (Arguments != null)
                                arg_count = Arguments.Count;
-  
+
                        if ((me.Name == "Invoke") &&
                                TypeManager.IsDelegateType (me.DeclaringType)) {
                                Error_InvokeOnDelegate (loc);
@@ -4674,45 +4788,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, me, Arguments, arg_count, ref methods [i]);
 
-                               if (!is_applicable &&
-                                       (IsParamsMethodApplicable (
-                                       ec, me, Arguments, arg_count, ref methods [i]))) {
+                               if (!is_applicable && IsParamsMethodApplicable (ec, me, Arguments, arg_count, ref methods [i])) {
                                        MethodBase candidate = methods [i];
                                        if (candidate_to_form == null)
                                                candidate_to_form = new PtrHashtable ();
@@ -4722,7 +4848,7 @@ namespace Mono.CSharp {
                                }
 
                                if (!is_applicable)
-                                        continue;
+                                       continue;
 
                                candidates.Add (methods [i]);
 
@@ -4735,6 +4861,9 @@ namespace Mono.CSharp {
                                }
                        }
 
+                       if (applicable_errors != Report.Errors)
+                               return null;
+                       
                        int candidate_top = candidates.Count;
 
                        if (applicable_type == null) {
@@ -4743,14 +4872,14 @@ 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);
 
                                        if (pd.Count != arg_count)
                                                continue;
 
-                                       if (!TypeManager.InferTypeArguments (ec, Arguments, ref c))
+                                       if (!TypeManager.InferTypeArguments (Arguments, ref c))
                                                continue;
 
                                        VerifyArgumentsCompat (ec, Arguments, arg_count,
@@ -4776,7 +4905,7 @@ namespace Mono.CSharp {
                                                if (pd.Count != arg_count)
                                                        continue;
 
-                                               if (TypeManager.InferTypeArguments (ec, Arguments, ref c))
+                                               if (TypeManager.InferTypeArguments (Arguments, ref c))
                                                        continue;
 
                                                Report.Error (
@@ -4878,8 +5007,8 @@ namespace Mono.CSharp {
 
                                 bool cand_params = candidate_to_form != null && candidate_to_form.Contains (candidate);
                                if (!BetterFunction (ec, Arguments, arg_count,
-                                                   method, method_params,
-                                                   candidate, cand_params,
+                                                    method, method_params,
+                                                    candidate, cand_params,
                                                     loc)) {
                                        Report.SymbolRelatedToPreviousError (candidate);
                                        ambiguous = candidate;
@@ -4918,21 +5047,25 @@ namespace Mono.CSharp {
                                                     method_params, null, may_fail, loc))
                                return null;
 
-                       if (method != null) {
-                               MethodBase the_method = method;
-                               if (the_method.Mono_IsInflatedMethod)
-                                       the_method = the_method.GetGenericMethodDefinition ();
-                               IMethodData data = TypeManager.GetMethod (the_method);
-                               if (data != null)
-                                       data.SetMemberIsUsed ();
-                       }
+                       if (method == null)
+                               return null;
+
+                       MethodBase the_method = TypeManager.DropGenericMethodArguments (method);
+                       if (the_method.IsGenericMethodDefinition &&
+                           !ConstraintChecker.CheckConstraints (ec, the_method, method, loc))
+                               return null;
+
+                       IMethodData data = TypeManager.GetMethod (the_method);
+                       if (data != null)
+                               data.SetMemberIsUsed ();
+
                        return method;
                }
 
                public static void Error_WrongNumArguments (Location loc, String name, int arg_count)
                {
                        Report.Error (1501, loc, "No overload for method `{0}' takes `{1}' arguments",
-                               name, arg_count);
+                               name, arg_count.ToString ());
                }
 
                 static void Error_InvokeOnDelegate (Location loc)
@@ -4951,21 +5084,20 @@ namespace Mono.CSharp {
                                Report.Error (1594, loc, "Delegate `{0}' has some invalid arguments",
                                        TypeManager.CSharpName (delegate_type));
 
-                       string par_desc = expected_par.ParameterDesc (idx);
+                       Parameter.Modifier mod = expected_par.ParameterModifier (idx);
 
-                       if (a.Modifier != expected_par.ParameterModifier (idx)) {
-                               if ((expected_par.ParameterModifier (idx) & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) == 0)
+                       string index = (idx + 1).ToString ();
+                       if (mod != Parameter.Modifier.ARGLIST && mod != a.Modifier) {
+                               if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) == 0)
                                        Report.Error (1615, loc, "Argument `{0}' should not be passed with the `{1}' keyword",
-                                               idx + 1, Parameter.GetModifierSignature (a.Modifier));
+                                               index, Parameter.GetModifierSignature (a.Modifier));
                                else
                                        Report.Error (1620, loc, "Argument `{0}' must be passed with the `{1}' keyword",
-                                               idx + 1, Parameter.GetModifierSignature (expected_par.ParameterModifier (idx)));
-                               return;
+                                               index, Parameter.GetModifierSignature (mod));
+                       } else {
+                               Report.Error (1503, loc, "Argument {0}: Cannot convert from `{1}' to `{2}'",
+                                       index, Argument.FullDesc (a), expected_par.ParameterDesc (idx));
                        }
-
-                       Report.Error (1503, loc,
-                                     String.Format ("Argument {0}: Cannot convert from `{1}' to `{2}'",
-                                                    idx + 1, Argument.FullDesc (a), par_desc));
                }
                
                public static bool VerifyArgumentsCompat (EmitContext ec, ArrayList Arguments,
@@ -4986,15 +5118,18 @@ namespace Mono.CSharp {
                                if (pm == Parameter.Modifier.PARAMS){
                                        if ((pm & ~Parameter.Modifier.PARAMS) != a.Modifier) {
                                                if (!may_fail)
-                                                       Error_InvalidArguments (
-                                                               loc, j, method, delegate_type,
-                                                               a, pd);
+                                                       Error_InvalidArguments (loc, j, method, delegate_type, a, pd);
                                                return false;
                                        }
 
                                        if (chose_params_expanded)
                                                parameter_type = TypeManager.GetElementType (parameter_type);
-                               } else if (pm == Parameter.Modifier.ARGLIST){
+                               } else if (pm == Parameter.Modifier.ARGLIST) {
+                                       if (!(a.Expr is Arglist)) {
+                                               if (!may_fail)
+                                                       Error_InvalidArguments (loc, j, method, delegate_type, a, pd);
+                                               return false;
+                                       }
                                        continue;
                                } else {
                                        //
@@ -5002,9 +5137,7 @@ namespace Mono.CSharp {
                                        //
                                        if (pd.ParameterModifier (j) != a.Modifier){
                                                if (!may_fail)
-                                                       Error_InvalidArguments (
-                                                               loc, j, method, delegate_type,
-                                                               a, pd);
+                                                       Error_InvalidArguments (loc, j, method, delegate_type, a, pd);
                                                return false;
                                        }
                                }
@@ -5055,8 +5188,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
@@ -5096,7 +5234,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;
@@ -5153,6 +5291,7 @@ namespace Mono.CSharp {
                                mg.InstanceExpression.CheckMarshallByRefAccess (ec.ContainerType);
 
                        eclass = ExprClass.Value;
+                       this.method = method;
                        return this;
                }
 
@@ -5297,14 +5436,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
@@ -5373,7 +5512,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
                                //
@@ -5387,7 +5526,7 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       if (IsMethodExcluded (method, ec))
+                       if (IsMethodExcluded (method))
                                return;
                        
                        if (!is_static){
@@ -5526,7 +5665,7 @@ namespace Mono.CSharp {
                        //
                        // First try to resolve it as a cast.
                        //
-                       TypeExpr te = expr.ResolveAsTypeStep (ec) as TypeExpr;
+                       TypeExpr te = expr.ResolveAsTypeTerminal (ec, true);
                        if ((te != null) && (te.eclass == ExprClass.Type)) {
                                Cast cast = new Cast (te, argument, loc);
                                return cast.Resolve (ec);
@@ -5573,7 +5712,7 @@ namespace Mono.CSharp {
                        //
                        // First try to resolve it as a cast.
                        //
-                       TypeExpr te = expr.ResolveAsTypeStep (ec) as TypeExpr;
+                       TypeExpr te = expr.ResolveAsTypeTerminal (ec, true);
                        if ((te != null) && (te.eclass == ExprClass.Type)) {
                                error201 ();
                                return null;
@@ -5729,6 +5868,29 @@ namespace Mono.CSharp {
                        return null;
                }
 
+               //
+               // Checks whether the type is an interface that has the
+               // [ComImport, CoClass] attributes and must be treated
+               // specially
+               //
+               public Expression CheckComImport (EmitContext ec)
+               {
+                       if (!type.IsInterface)
+                               return null;
+
+                       //
+                       // Turn the call into:
+                       // (the-interface-stated) (new class-referenced-in-coclassattribute ())
+                       //
+                       Type real_class = AttributeTester.GetCoClassAttribute (type);
+                       if (real_class == null)
+                               return null;
+
+                       New proxy = new New (new TypeExpression (real_class, loc), Arguments, loc);
+                       Cast cast = new Cast (new TypeExpression (type, loc), proxy, loc);
+                       return cast.Resolve (ec);
+               }
+               
                public override Expression DoResolve (EmitContext ec)
                {
                        //
@@ -5745,11 +5907,11 @@ namespace Mono.CSharp {
                                return this;
                        }
 
-                       TypeExpr texpr = RequestedType.ResolveAsTypeTerminal (ec);
+                       TypeExpr texpr = RequestedType.ResolveAsTypeTerminal (ec, false);
                        if (texpr == null)
                                return null;
 
-                       type = texpr.ResolveType (ec);
+                       type = texpr.Type;
 
                        if (Arguments == null) {
                                Expression c = Constantify (type);
@@ -5797,6 +5959,10 @@ namespace Mono.CSharp {
                        }
 
                        if (type.IsInterface || type.IsAbstract){
+                               RequestedType = CheckComImport (ec);
+                               if (RequestedType != null)
+                                       return RequestedType;
+                               
                                Report.SymbolRelatedToPreviousError (type);
                                Report.Error (144, loc, "Cannot create an instance of the abstract class or interface `{0}'", TypeManager.CSharpName (type));
                                return null;
@@ -5812,6 +5978,7 @@ namespace Mono.CSharp {
                        if (is_struct && Arguments == null)
                                return this;
 
+                       // For member-lookup, treat 'new Foo (bar)' as call to 'foo.ctor (bar)', where 'foo' is of type 'Foo'.
                        Expression ml = MemberLookupFinal (ec, type, type, ".ctor",
                                MemberTypes.Constructor, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
 
@@ -5821,7 +5988,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;
                        }
 
@@ -5835,7 +6002,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;
                        }
 
@@ -6062,16 +6229,21 @@ namespace Mono.CSharp {
                {
                        if (specified_dims) { 
                                Argument a = (Argument) arguments [idx];
-                               
+
                                if (!a.Resolve (ec, loc))
                                        return false;
-                               
-                               if (!(a.Expr is Constant)) {
-                                       Error (150, "A constant value is expected");
+
+                               Constant c = a.Expr as Constant;
+                               if (c != null) {
+                                       c = c.ToType (TypeManager.int32_type, a.Expr.Location);
+                               }
+
+                               if (c == null) {
+                                       Report.Error (150, a.Expr.Location, "A constant value is expected");
                                        return false;
                                }
-                               
-                               int value = (int) ((Constant) a.Expr).GetValue ();
+
+                               int value = (int) c.GetValue ();
                                
                                if (value != probe.Count) {
                                        Error_IncorrectArrayInitializer ();
@@ -6168,10 +6340,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)
@@ -6184,17 +6353,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);
@@ -6204,7 +6368,7 @@ namespace Mono.CSharp {
                                        return false;
                                }
 
-                               return ret;
+                               return true;
                        }
                }
 
@@ -6231,16 +6395,11 @@ namespace Mono.CSharp {
                        //
                        TypeExpr array_type_expr;
                        array_type_expr = new ComposedCast (requested_base_type, array_qualifier.ToString (), loc);
-                       array_type_expr = array_type_expr.ResolveAsTypeTerminal (ec);
+                       array_type_expr = array_type_expr.ResolveAsTypeTerminal (ec, false);
                        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.Type;
                        underlying_type = TypeManager.GetElementType (type);
                        dimensions = type.GetArrayRank ();
 
@@ -6295,11 +6454,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;
                                }
                                
@@ -6662,40 +6821,41 @@ namespace Mono.CSharp {
                                // This will never be true for array types that cannot be statically
                                // initialized. num_automatic_initializers will always be zero.  See
                                // CheckIndices.
-                                       if (num_automatic_initializers > max_automatic_initializers)
-                                               EmitStaticInitializers (ec);
+                               if (num_automatic_initializers > max_automatic_initializers)
+                                       EmitStaticInitializers (ec);
                                
                                if (dynamic_initializers)
                                        EmitDynamicInitializers (ec);
                        }
                }
-               
-               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;
                }
        }
        
@@ -6797,11 +6957,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");
                        }
@@ -6811,7 +6980,7 @@ namespace Mono.CSharp {
                {
                        ILGenerator ig = ec.ig;
 
-                       ec.EmitThis ();
+                       ec.EmitThis (false);
                        if (ec.TypeContainer is Struct)
                                ig.Emit (OpCodes.Ldobj, type);
                }
@@ -6832,7 +7001,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
@@ -6951,7 +7120,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)
@@ -6962,11 +7131,11 @@ namespace Mono.CSharp {
 
                public override Expression DoResolve (EmitContext ec)
                {
-                       TypeExpr texpr = QueriedType.ResolveAsTypeTerminal (ec);
+                       TypeExpr texpr = QueriedType.ResolveAsTypeTerminal (ec, false);
                        if (texpr == null)
                                return null;
 
-                       typearg = texpr.ResolveType (ec);
+                       typearg = texpr.Type;
 
                        if (typearg == TypeManager.void_type) {
                                Error (673, "System.Void cannot be used from C#. Use typeof (void) to get the void type object");
@@ -6991,8 +7160,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;
                }
        }
 
@@ -7030,7 +7201,7 @@ namespace Mono.CSharp {
 
                public override Expression DoResolve (EmitContext ec)
                {
-                       TypeExpr texpr = QueriedType.ResolveAsTypeTerminal (ec);
+                       TypeExpr texpr = QueriedType.ResolveAsTypeTerminal (ec, false);
                        if (texpr == null)
                                return null;
 
@@ -7039,7 +7210,7 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       type_queried = texpr.ResolveType (ec);
+                       type_queried = texpr.Type;
 
                        int size_of = GetTypeSize (type_queried);
                        if (size_of > 0) {
@@ -7086,13 +7257,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 (Namespace.Root, identifier, loc).ResolveAsTypeStep (ec, silent);
+                               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);
@@ -7110,10 +7281,10 @@ namespace Mono.CSharp {
                {
                        FullNamedExpression fne;
                        if (alias == "global") {
-                               fne = Namespace.Root;
+                               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);
@@ -7207,9 +7378,9 @@ namespace Mono.CSharp {
                        if (new_expr is Namespace) {
                                Namespace ns = (Namespace) new_expr;
                                string lookup_id = MemberName.MakeName (Identifier, args);
-                               FullNamedExpression retval = ns.Lookup (ec.DeclSpace, lookup_id, loc);
+                               FullNamedExpression retval = ns.Lookup (ec.DeclContainer, lookup_id, loc);
                                if ((retval != null) && (args != null))
-                                       retval = new ConstructedType (retval, args, loc).ResolveAsTypeStep (ec);
+                                       retval = new ConstructedType (retval, args, loc).ResolveAsTypeStep (ec, false);
                                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);
@@ -7221,19 +7392,25 @@ 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 = MemberLookup (
-                               ec, expr_type, expr_type, Identifier, loc);
+                               ec.ContainerType, expr_type, expr_type, Identifier, loc);
                        if ((member_lookup == null) && (args != null)) {
                                string lookup_id = MemberName.MakeName (Identifier, args);
                                member_lookup = MemberLookup (
-                                       ec, expr_type, expr_type, lookup_id, loc);
+                                       ec.ContainerType, expr_type, expr_type, lookup_id, loc);
                        }
                        if (member_lookup == null) {
                                MemberLookupFailed (
-                                       ec, expr_type, expr_type, Identifier, null, true, loc);
+                                       ec.ContainerType, expr_type, expr_type, Identifier, null, true, loc);
                                return null;
                        }
 
@@ -7257,7 +7434,7 @@ namespace Mono.CSharp {
                                        ct = new ConstructedType (
                                                member_lookup.Type, ct.TypeArguments, loc);
 
-                                       return ct.ResolveAsTypeStep (ec);
+                                       return ct.ResolveAsTypeStep (ec, false);
                                }
 
                                return member_lookup;
@@ -7304,38 +7481,36 @@ 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) {
-                               Report.Error (234, "No such name or typespace {0}", expr);
+                       if (new_expr == null)
                                return null;
-                       }
 
                        string lookup_id = MemberName.MakeName (Identifier, args);
 
                        if (new_expr is Namespace) {
                                Namespace ns = (Namespace) new_expr;
-                               FullNamedExpression retval = ns.Lookup (ec.DeclSpace, lookup_id, loc);
+                               FullNamedExpression retval = ns.Lookup (rc.DeclContainer, lookup_id, loc);
                                if ((retval != null) && (args != null))
-                                       retval = new ConstructedType (retval, args, loc).ResolveAsTypeStep (ec);
+                                       retval = new ConstructedType (retval, args, loc).ResolveAsTypeStep (rc, false);
                                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);
                                return retval;
                        }
 
-                       TypeExpr tnew_expr = new_expr.ResolveAsTypeTerminal (ec);
+                       TypeExpr tnew_expr = new_expr.ResolveAsTypeTerminal (rc, false);
                        if (tnew_expr == null)
                                return null;
 
-                       Type expr_type = tnew_expr.ResolveType (ec);
+                       Type expr_type = tnew_expr.Type;
 
                        if (expr_type.IsPointer){
                                Error (23, "The `.' operator can not be applied to pointer operands (" +
@@ -7344,11 +7519,11 @@ namespace Mono.CSharp {
                        }
 
                        Expression member_lookup = MemberLookup (
-                               ec, ec.ContainerType, expr_type, expr_type, lookup_id,
+                               rc.DeclContainer.TypeBuilder, expr_type, expr_type, lookup_id,
                                MemberTypes.NestedType, BindingFlags.Public | BindingFlags.NonPublic, loc);
                        if (member_lookup == null) {
                                int errors = Report.Errors;
-                               MemberLookupFailed (ec, expr_type, expr_type, lookup_id, null, false, loc);
+                               MemberLookupFailed (rc.DeclContainer.TypeBuilder, expr_type, expr_type, lookup_id, null, false, loc);
 
                                if (!silent && errors == Report.Errors) {
                                        Report.Error (426, loc, "The nested type `{0}' does not exist in the type `{1}'",
@@ -7358,11 +7533,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;
                        }
 
-                       TypeExpr texpr = member_lookup.ResolveAsTypeTerminal (ec);
+                       TypeExpr texpr = member_lookup.ResolveAsTypeTerminal (rc, false);
                        if (texpr == null)
                                return null;
 
@@ -7382,7 +7557,7 @@ namespace Mono.CSharp {
 
                        if (the_args != null) {
                                ConstructedType ctype = new ConstructedType (texpr.Type, the_args, loc);
-                               return ctype.ResolveAsTypeStep (ec);
+                               return ctype.ResolveAsTypeStep (rc, false);
                        }
 
                        return texpr;
@@ -7617,11 +7792,10 @@ namespace Mono.CSharp {
                                                Report.Error (1708, loc, "Fixed size buffers can only be accessed through locals or fields");
                                                return null;
                                        }
-// TODO: not sure whether it is correct
-//                                     if (!ec.InFixedInitializer) {
-//                                             Error (1666, "You cannot use fixed sized buffers contained in unfixed expressions. Try using the fixed statement");
-//                                             return null;
-//                                     }
+                                       if (!ec.InFixedInitializer && ec.ContainerType.IsValueType) {
+                                               Error (1666, "You cannot use fixed size buffers contained in unfixed expressions. Try using the fixed statement");
+                                               return null;
+                                       }
                                        return MakePointerAccess (ec, ff.ElementType);
                                }
                        }
@@ -7674,7 +7848,7 @@ namespace Mono.CSharp {
                        Type t = ea.Expr.Type;
                        if (t.GetArrayRank () != ea.Arguments.Count){
                                Report.Error (22, ea.Location, "Wrong number of indexes `{0}' inside [], expected `{1}'",
-                                         ea.Arguments.Count, t.GetArrayRank ());
+                                         ea.Arguments.Count.ToString (), t.GetArrayRank ().ToString ());
                                return null;
                        }
 
@@ -7748,7 +7922,11 @@ namespace Mono.CSharp {
                                ig.Emit (OpCodes.Ldelema, type);
                                ig.Emit (OpCodes.Ldobj, type);
                        } else if (type.IsGenericParameter)
+#if MS_COMPATIBLE
+                               ig.Emit (OpCodes.Ldelem, type);
+#else
                                ig.Emit (OpCodes.Ldelem_Any, type);
+#endif
                        else if (type.IsPointer)
                                ig.Emit (OpCodes.Ldelem_I);
                        else
@@ -7790,7 +7968,12 @@ namespace Mono.CSharp {
                                return OpCodes.Stobj;
                        } else if (t.IsGenericParameter) {
                                has_type_arg = true;
+#if MS_COMPATIBLE
+                               return OpCodes.Stelem;
+#else
                                return OpCodes.Stelem_Any;
+#endif
+
                        } else if (t.IsPointer)
                                return OpCodes.Stelem_I;
                        else
@@ -8075,6 +8258,21 @@ namespace Mono.CSharp {
                {
                        Indexers ix = empty;
 
+                       if (lookup_type.IsGenericParameter) {
+                               GenericConstraints gc = TypeManager.GetTypeParameterConstraints (lookup_type);
+                               if (gc == null)
+                                       return empty;
+
+                               if (gc.HasClassConstraint)
+                                       Append (ref ix, caller_type, GetIndexersForTypeOrInterface (caller_type, gc.ClassConstraint));
+
+                               Type[] ifaces = gc.InterfaceConstraints;
+                               foreach (Type itype in ifaces)
+                                       Append (ref ix, caller_type, GetIndexersForTypeOrInterface (caller_type, itype));
+
+                               return ix;
+                       }
+
                        Type copy = lookup_type;
                        while (copy != TypeManager.object_type && copy != null){
                                Append (ref ix, caller_type, GetIndexersForTypeOrInterface (caller_type, copy));
@@ -8202,6 +8400,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;
@@ -8315,17 +8526,25 @@ namespace Mono.CSharp {
                {
                        Emit (ec, false);
                }
+
+               public override string GetSignatureForError ()
+               {
+                       // FIXME: print the argument list of the indexer
+                       return instance_expr.GetSignatureForError () + ".this[...]";
+               }
        }
 
        /// <summary>
        ///   The base operator for method names
        /// </summary>
        public class BaseAccess : Expression {
-               string member;
+               public readonly string Identifier;
+               TypeArguments args;
                
-               public BaseAccess (string member, Location l)
+               public BaseAccess (string member, TypeArguments args, Location l)
                {
-                       this.member = member;
+                       this.Identifier = member;
+                       this.args = args;
                        loc = l;
                }
 
@@ -8376,11 +8595,10 @@ namespace Mono.CSharp {
                                return null;
                        }
                        
-                       member_lookup = MemberLookup (ec, ec.ContainerType, null, base_type,
-                                                     member, AllMemberTypes, AllBindingFlags,
-                                                     loc);
+                       member_lookup = MemberLookup (ec.ContainerType, null, base_type, Identifier,
+                                                     AllMemberTypes, AllBindingFlags, loc);
                        if (member_lookup == null) {
-                               MemberLookupFailed (ec, base_type, base_type, member, null, true, loc);
+                               MemberLookupFailed (ec.ContainerType, base_type, base_type, Identifier, null, true, loc);
                                return null;
                        }
 
@@ -8401,8 +8619,18 @@ namespace Mono.CSharp {
                                pe.IsBase = true;
                        }
 
-                       if (e is MethodGroupExpr)
-                               ((MethodGroupExpr) e).IsBase = true;
+                       MethodGroupExpr mg = e as MethodGroupExpr;
+                       if (mg != null)
+                               mg.IsBase = true;
+
+                       if (args != null) {
+                               if (mg != null)
+                                       return mg.ResolveGeneric (ec, args);
+
+                               Report.Error (307, loc, "`{0}' cannot be used with type arguments",
+                                             Identifier);
+                               return null;
+                       }
 
                        return e;
                }
@@ -8454,6 +8682,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 ()
                {
@@ -8581,17 +8812,13 @@ namespace Mono.CSharp {
                        return this;
                }
 
-               protected override TypeExpr DoResolveAsTypeStep (EmitContext ec)
+               protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
                {
-                       TypeExpr lexpr = left.ResolveAsTypeTerminal (ec);
+                       TypeExpr lexpr = left.ResolveAsTypeTerminal (ec, false);
                        if (lexpr == null)
                                return null;
 
-                       bool old = ec.TestObsoleteMethodUsage;
-                       ec.TestObsoleteMethodUsage = true;
-                       Type ltype = lexpr.ResolveType (ec);
-                       ec.TestObsoleteMethodUsage = old;
-
+                       Type ltype = lexpr.Type;
                        if ((ltype == TypeManager.void_type) && (dim != "*")) {
                                Report.Error (1547, Location,
                                              "Keyword 'void' cannot be used in this context");
@@ -8602,7 +8829,7 @@ namespace Mono.CSharp {
                                TypeExpr nullable = new NullableType (left, loc);
                                if (dim.Length > 1)
                                        nullable = new ComposedCast (nullable, dim.Substring (1), loc);
-                               return nullable.ResolveAsTypeTerminal (ec);
+                               return nullable.ResolveAsTypeTerminal (ec, false);
                        }
 
                        if (dim == "*" && !TypeManager.VerifyUnManaged (ltype, loc)) {
@@ -8618,7 +8845,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;
                        }
@@ -8634,15 +8861,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;
                }
        }
 
@@ -8768,11 +8996,11 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       TypeExpr texpr = t.ResolveAsTypeTerminal (ec);
+                       TypeExpr texpr = t.ResolveAsTypeTerminal (ec, false);
                        if (texpr == null)
                                return null;
 
-                       otype = texpr.ResolveType (ec);
+                       otype = texpr.Type;
 
                        if (!TypeManager.VerifyUnManaged (otype, loc))
                                return null;