2004-09-04 Marek Safar <marek.safar@seznam.cz>
[mono.git] / mcs / mcs / expression.cs
index e2ad1a9d529416b7e21a8c6ec3576d349381acdf..2d34749aec33fefbea17a5d4d2a7e96b5483550a 100755 (executable)
@@ -46,13 +46,13 @@ namespace Mono.CSharp {
                public override void Emit (EmitContext ec)
                {
                        if (args != null) 
-                               Invocation.EmitArguments (ec, mi, args);
+                               Invocation.EmitArguments (ec, mi, args, false, null);
 
                        ec.ig.Emit (OpCodes.Call, mi);
                        return;
                }
                
-               static public Expression MakeSimpleCall (EmitContext ec, MethodGroupExpr mg,
+               static public StaticCallExpr MakeSimpleCall (EmitContext ec, MethodGroupExpr mg,
                                                         Expression e, Location loc)
                {
                        ArrayList args;
@@ -66,7 +66,8 @@ namespace Mono.CSharp {
                                 return null;
 
                         args.Add (a);
-                       method = Invocation.OverloadResolve (ec, (MethodGroupExpr) mg, args, loc);
+                       method = Invocation.OverloadResolve (
+                               ec, (MethodGroupExpr) mg, args, false, loc);
 
                        if (method == null)
                                return null;
@@ -80,6 +81,10 @@ namespace Mono.CSharp {
                        if (TypeManager.TypeToCoreType (type) != TypeManager.void_type)
                                ec.ig.Emit (OpCodes.Pop);
                }
+               
+               public MethodInfo Method {
+                       get { return mi; }
+               }
        }
 
        public class ParenthesizedExpression : Expression
@@ -643,7 +648,7 @@ namespace Mono.CSharp {
        public class Indirection : Expression, IMemoryLocation, IAssignMethod {
                Expression expr;
                LocalTemporary temporary;
-               bool have_temporary;
+               bool prepared;
                
                public Indirection (Expression expr, Location l)
                {
@@ -659,54 +664,47 @@ namespace Mono.CSharp {
                
                public override void Emit (EmitContext ec)
                {
-                       ILGenerator ig = ec.ig;
-
-                       if (temporary != null){
-                               if (have_temporary) {
-                                       temporary.Emit (ec);
-                               } else {
-                                       expr.Emit (ec);
-                                       ec.ig.Emit (OpCodes.Dup);
-                                       temporary.Store (ec);
-                                       have_temporary = true;
-                               }
-                       } else
+                       if (!prepared)
                                expr.Emit (ec);
                        
-                       LoadFromPtr (ig, Type);
+                       LoadFromPtr (ec.ig, Type);
                }
 
-               public void EmitAssign (EmitContext ec, Expression source)
+               public void Emit (EmitContext ec, bool leave_copy)
                {
-                       if (temporary != null){
-                               if (have_temporary)
-                                       temporary.Emit (ec);
-                               else {
-                                       expr.Emit (ec);
-                                       ec.ig.Emit (OpCodes.Dup);
-                                       temporary.Store (ec);
-                                       have_temporary = true;
-                               }
-                       } else 
-                               expr.Emit (ec);
+                       Emit (ec);
+                       if (leave_copy) {
+                               ec.ig.Emit (OpCodes.Dup);
+                               temporary = new LocalTemporary (ec, expr.Type);
+                               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)
                {
-                       if (temporary != null){
-                               if (have_temporary){
-                                       temporary.Emit (ec);
-                                       return;
-                               }
-                               expr.Emit (ec);
-                               ec.ig.Emit (OpCodes.Dup);
-                               temporary.Store (ec);
-                               have_temporary = true;
-                       } else
-                               expr.Emit (ec);
+                       expr.Emit (ec);
                }
 
                public override Expression DoResolve (EmitContext ec)
@@ -716,12 +714,7 @@ namespace Mono.CSharp {
                        //
                        return this;
                }
-
-               public new void CacheTemporaries (EmitContext ec)
-               {
-                       temporary = new LocalTemporary (ec, expr.Type);
-               }
-
+               
                public override string ToString ()
                {
                        return "*(" + expr + ")";
@@ -757,13 +750,15 @@ namespace Mono.CSharp {
                }
                
                Mode mode;
+               bool is_expr = false;
+               bool recurse = false;
+               
                Expression expr;
-               LocalTemporary temp_storage;
 
                //
                // This is expensive for the simplest case.
                //
-               Expression method;
+               StaticCallExpr method;
                        
                public UnaryMutator (Mode m, Expression e, Location l)
                {
@@ -855,9 +850,7 @@ namespace Mono.CSharp {
                        } else if (expr.eclass == ExprClass.IndexerAccess){
                                IndexerAccess ia = (IndexerAccess) expr;
                                
-                               temp_storage = new LocalTemporary (ec, expr.Type);
-                               
-                               expr = ia.ResolveLValue (ec, temp_storage);
+                               expr = ia.ResolveLValue (ec, this);
                                if (expr == null)
                                        return null;
 
@@ -975,114 +968,39 @@ namespace Mono.CSharp {
                        }
                        
                }
-
-               static EmptyExpression empty_expr;
                
                void EmitCode (EmitContext ec, bool is_expr)
                {
-                       ILGenerator ig = ec.ig;
-                       IAssignMethod ia = (IAssignMethod) expr;
-                       Type expr_type = expr.Type;
-
-                       ia.CacheTemporaries (ec);
+                       recurse = true;
+                       this.is_expr = is_expr;
+                       ((IAssignMethod) expr).EmitAssign (ec, this, is_expr && (mode == Mode.PreIncrement || mode == Mode.PreDecrement), true);
+               }
+               
 
+               public override void Emit (EmitContext ec)
+               {
                        //
-                       // NOTE: We should probably handle three cases:
-                       //
-                       //     * method invocation required.
-                       //     * direct stack manipulation possible
-                       //     * the object requires an "instance" field
+                       // We use recurse to allow ourselfs to be the source
+                       // of an assignment. This little hack prevents us from
+                       // having to allocate another expression
                        //
-                       if (temp_storage == null){
-                               //
-                               // Temporary improvement: if we are dealing with something that does
-                               // not require complicated instance setup, avoid using a temporary
-                               //
-                               // For now: only localvariables when not remapped
-                               //
-
-                               if (method == null &&
-                                   ((expr is LocalVariableReference) ||(expr is FieldExpr && ((FieldExpr) expr).FieldInfo.IsStatic))){
-                                       if (empty_expr == null)
-                                               empty_expr = new EmptyExpression ();
-                                       
-                                       switch (mode){
-                                       case Mode.PreIncrement:
-                                       case Mode.PreDecrement:
-                                               expr.Emit (ec);
-                                       
-                                               LoadOneAndEmitOp (ec, expr_type);
-                                               if (is_expr)
-                                                       ig.Emit (OpCodes.Dup);
-                                               ia.EmitAssign (ec, empty_expr);
-                                               break;
-                                               
-                                       case Mode.PostIncrement:
-                                       case Mode.PostDecrement:
-                                               expr.Emit (ec);
-                                               if (is_expr)
-                                                       ig.Emit (OpCodes.Dup);
-                                               
-                                               LoadOneAndEmitOp (ec, expr_type);
-                                               ia.EmitAssign (ec, empty_expr);
-                                               break;
-                                       }
-                                       return;
-                               }
-                               temp_storage = new LocalTemporary (ec, expr_type);
+                       if (recurse) {
+                               ((IAssignMethod) expr).Emit (ec, is_expr && (mode == Mode.PostIncrement  || mode == Mode.PostDecrement));
+                               if (method == null)
+                                       LoadOneAndEmitOp (ec, expr.Type);
+                               else
+                                       ec.ig.Emit (OpCodes.Call, method.Method);
+                               recurse = false;
+                               return;
                        }
                        
-                       switch (mode){
-                       case Mode.PreIncrement:
-                       case Mode.PreDecrement:
-                               if (method == null){
-                                       expr.Emit (ec);
-                                       
-                                       LoadOneAndEmitOp (ec, expr_type);
-                               } else 
-                                       method.Emit (ec);
-                               
-                               temp_storage.Store (ec);
-                               ia.EmitAssign (ec, temp_storage);
-                               if (is_expr)
-                                       temp_storage.Emit (ec);
-                               break;
-                               
-                       case Mode.PostIncrement:
-                       case Mode.PostDecrement:
-                               if (is_expr)
-                                       expr.Emit (ec);
-                               
-                               if (method == null){
-                                       if (!is_expr)
-                                               expr.Emit (ec);
-                                       else
-                                               ig.Emit (OpCodes.Dup);
-                                       
-                                       LoadOneAndEmitOp (ec, expr_type);
-                               } else {
-                                       method.Emit (ec);
-                               }
-                               
-                               temp_storage.Store (ec);
-                               ia.EmitAssign (ec, temp_storage);
-                               break;
-                       }
-
-                       temp_storage.Release (ec);
-               }
-
-               public override void Emit (EmitContext ec)
-               {
                        EmitCode (ec, true);
-                       
                }
                
                public override void EmitStatement (EmitContext ec)
                {
                        EmitCode (ec, false);
                }
-
        }
 
        /// <summary>
@@ -1118,6 +1036,8 @@ namespace Mono.CSharp {
                        if (probe_type == null)
                                return null;
 
+                       CheckObsoleteAttribute (probe_type);
+
                        expr = expr.Resolve (ec);
                        if (expr == null)
                                return null;
@@ -1241,16 +1161,11 @@ namespace Mono.CSharp {
                                warning_never_matches = true;
                        }
                        
-                       if (RootContext.WarningLevel >= 1){
-                               if (warning_always_matches)
-                                       Warning (183, "The expression is always of type `" +
-                                                TypeManager.CSharpName (probe_type) + "'");
-                               else if (warning_never_matches){
-                                       if (!(probe_type.IsInterface || expr.Type.IsInterface))
-                                               Warning (184,
-                                                        "The expression is never of type `" +
-                                                        TypeManager.CSharpName (probe_type) + "'");
-                               }
+                       if (warning_always_matches)
+                               Warning (183, "The given expression is always of the provided ('{0}') type", TypeManager.CSharpName (probe_type));
+                       else if (warning_never_matches){
+                               if (!(probe_type.IsInterface || expr.Type.IsInterface))
+                                       Warning (184, "The given expression is never of the provided ('{0}') type", TypeManager.CSharpName (probe_type));
                        }
 
                        return this;
@@ -1854,6 +1769,13 @@ namespace Mono.CSharp {
                        if (type == null)
                                return null;
 
+                       CheckObsoleteAttribute (type);
+
+                       if (type.IsAbstract && type.IsSealed) {
+                               Report.Error (716, loc, "Cannot convert to static type '{0}'", TypeManager.CSharpName (type));
+                               return null;
+                       }
+
                        eclass = ExprClass.Value;
 
                        if (expr is Constant){
@@ -1863,6 +1785,10 @@ namespace Mono.CSharp {
                                        return e;
                        }
 
+                       if (type.IsPointer && !ec.InUnsafe) {
+                               UnsafeError (loc);
+                               return null;
+                       }
                        expr = Convert.ExplicitConversion (ec, expr, type, loc);
                        return expr;
                }
@@ -2098,7 +2024,7 @@ namespace Mono.CSharp {
                                                } else if (right is LongConstant){
                                                        long ll = ((LongConstant) right).Value;
 
-                                                       if (ll > 0)
+                                                       if (ll >= 0)
                                                                right = new ULongConstant ((ulong) ll);
                                                } else {
                                                        e = Convert.ImplicitNumericConversion (ec, right, l, loc);
@@ -2130,6 +2056,10 @@ namespace Mono.CSharp {
                                    (other == TypeManager.int32_type) ||
                                    (other == TypeManager.int64_type))
                                        Error_OperatorAmbiguous (loc, oper, l, r);
+                               else {
+                                       left = ForceConversion (ec, left, TypeManager.uint64_type);
+                                       right = ForceConversion (ec, right, TypeManager.uint64_type);
+                               }
                                type = TypeManager.uint64_type;
                        } else if (IsOfType (ec, l, r, TypeManager.int64_type, check_user_conv)){
                                //
@@ -2297,8 +2227,6 @@ namespace Mono.CSharp {
                        Type l = left.Type;
                        Type r = right.Type;
 
-                       bool overload_failed = false;
-
                        //
                        // Special cases: string comapred to null
                        //
@@ -2344,13 +2272,13 @@ namespace Mono.CSharp {
                                        args.Add (new Argument (left, Argument.AType.Expression));
                                        args.Add (new Argument (right, Argument.AType.Expression));
                                        
-                                       MethodBase method = Invocation.OverloadResolve (ec, union, args, Location.Null);
+                                       MethodBase method = Invocation.OverloadResolve (
+                                               ec, union, args, true, Location.Null);
+
                                        if (method != null) {
                                                MethodInfo mi = (MethodInfo) method;
                                                
                                                return new BinaryMethod (mi.ReturnType, method, args);
-                                       } else {
-                                               overload_failed = true;
                                        }
                                }
                        }
@@ -2474,7 +2402,8 @@ namespace Mono.CSharp {
                        //
                        if (oper == Operator.Addition || oper == Operator.Subtraction) {
                                if (l.IsSubclassOf (TypeManager.delegate_type)){
-                                       if (right.eclass == ExprClass.MethodGroup && RootContext.V2){
+                                       if ((right.eclass == ExprClass.MethodGroup) &&
+                                           (RootContext.Version != LanguageVersion.ISO_1)){
                                                Expression tmp = Convert.ImplicitConversionRequired (ec, right, l, loc);
                                                if (tmp == null)
                                                        return null;
@@ -2529,16 +2458,16 @@ namespace Mono.CSharp {
                                                if (r == l)
                                                        return new PointerArithmetic (
                                                                false, left, right, TypeManager.int64_type,
-                                                               loc);
+                                                               loc).Resolve (ec);
                                        } else {
                                                Expression t = Make32or64 (ec, right);
                                                if (t != null)
-                                                       return new PointerArithmetic (oper == Operator.Addition, left, t, l, loc);
+                                                       return new PointerArithmetic (oper == Operator.Addition, left, t, l, loc).Resolve (ec);
                                        }
                                } else if (r.IsPointer && oper == Operator.Addition){
                                        Expression t = Make32or64 (ec, left);
                                        if (t != null)
-                                               return new PointerArithmetic (true, right, t, r, loc);
+                                               return new PointerArithmetic (true, right, t, r, loc).Resolve (ec);
                                }
                        }
                        
@@ -2675,14 +2604,6 @@ namespace Mono.CSharp {
                                }
                        }
                        
-                       //
-                       // We are dealing with numbers
-                       //
-                       if (overload_failed){
-                               Error_OperatorCannotBeApplied ();
-                               return null;
-                       }
-
                        //
                        // This will leave left or right set to null if there is an error
                        //
@@ -2703,13 +2624,16 @@ namespace Mono.CSharp {
                            oper == Operator.BitwiseOr ||
                            oper == Operator.ExclusiveOr){
                                if (l == r){
-                                       if (!((l == TypeManager.int32_type) ||
-                                             (l == TypeManager.uint32_type) ||
-                                             (l == TypeManager.short_type) ||
-                                             (l == TypeManager.ushort_type) ||
-                                             (l == TypeManager.int64_type) ||
-                                             (l == TypeManager.uint64_type))){
+                                       if (((l == TypeManager.int32_type) ||
+                                            (l == TypeManager.uint32_type) ||
+                                            (l == TypeManager.short_type) ||
+                                            (l == TypeManager.ushort_type) ||
+                                            (l == TypeManager.int64_type) ||
+                                            (l == TypeManager.uint64_type))){
                                                type = l;
+                                       } else {
+                                               Error_OperatorCannotBeApplied ();
+                                               return null;
                                        }
                                } else {
                                        Error_OperatorCannotBeApplied ();
@@ -3127,7 +3051,7 @@ namespace Mono.CSharp {
                        ILGenerator ig = ec.ig;
                        
                        if (Arguments != null) 
-                               Invocation.EmitArguments (ec, method, Arguments);
+                               Invocation.EmitArguments (ec, method, Arguments, false, null);
                        
                        if (method is MethodInfo)
                                ig.Emit (OpCodes.Call, (MethodInfo) method);
@@ -3262,7 +3186,7 @@ namespace Mono.CSharp {
                                break;
                        }
                        
-                       Invocation.EmitArguments (ec, concat_method, operands);
+                       Invocation.EmitArguments (ec, concat_method, operands, false, null);
                        ec.ig.Emit (OpCodes.Call, concat_method);
                }
        }
@@ -3291,7 +3215,7 @@ namespace Mono.CSharp {
                {
                        ILGenerator ig = ec.ig;
                        
-                       Invocation.EmitArguments (ec, method, args);
+                       Invocation.EmitArguments (ec, method, args, false, null);
                        
                        ig.Emit (OpCodes.Call, (MethodInfo) method);
                        ig.Emit (OpCodes.Castclass, type);
@@ -3357,7 +3281,9 @@ namespace Mono.CSharp {
                        ArrayList arguments = new ArrayList ();
                        arguments.Add (new Argument (left_temp, Argument.AType.Expression));
                        arguments.Add (new Argument (right, Argument.AType.Expression));
-                       method = Invocation.OverloadResolve (ec, (MethodGroupExpr) operator_group, arguments, loc) as MethodInfo;
+                       method = Invocation.OverloadResolve (
+                               ec, (MethodGroupExpr) operator_group, arguments, false, loc)
+                               as MethodInfo;
                        if ((method == null) || (method.ReturnType != type)) {
                                Error19 ();
                                return null;
@@ -3407,7 +3333,6 @@ namespace Mono.CSharp {
                public PointerArithmetic (bool is_addition, Expression l, Expression r, Type t, Location loc)
                {
                        type = t;
-                       eclass = ExprClass.Variable;
                        this.loc = loc;
                        left = l;
                        right = r;
@@ -3416,9 +3341,13 @@ namespace Mono.CSharp {
 
                public override Expression DoResolve (EmitContext ec)
                {
-                       //
-                       // We are born fully resolved
-                       //
+                       eclass = ExprClass.Variable;
+                       
+                       if (left.Type == TypeManager.void_ptr_type) {
+                               Error (242, "The operation in question is undefined on void pointers");
+                               return null;
+                       }
+                       
                        return this;
                }
 
@@ -3426,7 +3355,8 @@ namespace Mono.CSharp {
                {
                        Type op_type = left.Type;
                        ILGenerator ig = ec.ig;
-                       int size = GetTypeSize (TypeManager.GetElementType (op_type));
+                       Type element = TypeManager.GetElementType (op_type);
+                       int size = GetTypeSize (element);
                        Type rtype = right.Type;
                        
                        if (rtype.IsPointer){
@@ -3439,7 +3369,7 @@ namespace Mono.CSharp {
 
                                if (size != 1){
                                        if (size == 0)
-                                               ig.Emit (OpCodes.Sizeof, op_type);
+                                               ig.Emit (OpCodes.Sizeof, element);
                                        else 
                                                IntLiteral.EmitInt (ig, size);
                                        ig.Emit (OpCodes.Div);
@@ -3454,7 +3384,7 @@ namespace Mono.CSharp {
                                right.Emit (ec);
                                if (size != 1){
                                        if (size == 0)
-                                               ig.Emit (OpCodes.Sizeof, op_type);
+                                               ig.Emit (OpCodes.Sizeof, element);
                                        else 
                                                IntLiteral.EmitInt (ig, size);
                                        if (rtype == TypeManager.int64_type)
@@ -3657,7 +3587,7 @@ namespace Mono.CSharp {
                        if (e != null) {
                                local_info.Used = true;
                                eclass = ExprClass.Value;
-                               return e;
+                               return e.Resolve (ec);
                        }
 
                        VariableInfo variable_info = local_info.VariableInfo; 
@@ -3698,6 +3628,8 @@ namespace Mono.CSharp {
                                return null;
                        }
 
+                       CheckObsoleteAttribute (e.Type);
+
                        if (local_info.LocalBuilder == null)
                                return ec.RemapLocalLValue (local_info, right_side);
                        
@@ -3716,12 +3648,19 @@ namespace Mono.CSharp {
                        ig.Emit (OpCodes.Ldloc, local_info.LocalBuilder);
                }
                
-               public void EmitAssign (EmitContext ec, Expression source)
+               public void Emit (EmitContext ec, bool leave_copy)
+               {
+                       Emit (ec);
+                       if (leave_copy)
+                               ec.ig.Emit (OpCodes.Dup);
+               }
+               
+               public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
                {
-                       ILGenerator ig = ec.ig;
-
                        source.Emit (ec);
-                       ig.Emit (OpCodes.Stloc, local_info.LocalBuilder);
+                       if (leave_copy)
+                               ec.ig.Emit (OpCodes.Dup);
+                       ec.ig.Emit (OpCodes.Stloc, local_info.LocalBuilder);
                }
                
                public void AddressOf (EmitContext ec, AddressOp mode)
@@ -3748,7 +3687,8 @@ namespace Mono.CSharp {
                Block block;
                VariableInfo vi;
                public Parameter.Modifier mod;
-               public bool is_ref, is_out;
+               public bool is_ref, is_out, prepared;
+               LocalTemporary temp;
                
                public ParameterReference (Parameters pars, Block block, int idx, string name, Location loc)
                {
@@ -3883,6 +3823,11 @@ namespace Mono.CSharp {
                }
                
                public override void Emit (EmitContext ec)
+               {
+                       Emit (ec, false);
+               }
+               
+               public void Emit (EmitContext ec, bool leave_copy)
                {
                        ILGenerator ig = ec.ig;
                        
@@ -3893,33 +3838,56 @@ namespace Mono.CSharp {
 
                        EmitLdArg (ig, arg_idx);
 
-                       if (!is_ref)
-                               return;
-
-                       //
-                       // If we are a reference, we loaded on the stack a pointer
-                       // Now lets load the real value
-                       //
-                       LoadFromPtr (ig, type);
+                       if (is_ref) {
+                               if (prepared)
+                                       ec.ig.Emit (OpCodes.Dup);
+       
+                               //
+                               // If we are a reference, we loaded on the stack a pointer
+                               // Now lets load the real value
+                               //
+                               LoadFromPtr (ig, type);
+                       }
+                       
+                       if (leave_copy) {
+                               ec.ig.Emit (OpCodes.Dup);
+                               
+                               if (is_ref) {
+                                       temp = new LocalTemporary (ec, type);
+                                       temp.Store (ec);
+                               }
+                       }
                }
-
-               public void EmitAssign (EmitContext ec, Expression source)
+               
+               public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
                {
                        ILGenerator ig = ec.ig;
-                       
                        int arg_idx = idx;
-
+                       
+                       prepared = prepare_for_load;
+                       
                        if (!ec.IsStatic)
                                arg_idx++;
 
-                       if (is_ref)
+                       if (is_ref && !prepared)
                                EmitLdArg (ig, arg_idx);
                        
                        source.Emit (ec);
 
-                       if (is_ref)
+                       if (leave_copy)
+                               ec.ig.Emit (OpCodes.Dup);
+                       
+                       if (is_ref) {
+                               if (leave_copy) {
+                                       temp = new LocalTemporary (ec, type);
+                                       temp.Store (ec);
+                               }
+                               
                                StoreFromPtr (ig, type);
-                       else {
+                               
+                               if (temp != null)
+                                       temp.Emit (ec);
+                       } else {
                                if (arg_idx <= 255)
                                        ig.Emit (OpCodes.Starg_S, (byte) arg_idx);
                                else
@@ -3956,7 +3924,8 @@ namespace Mono.CSharp {
                public enum AType : byte {
                        Expression,
                        Ref,
-                       Out
+                       Out,
+                       ArgList
                };
 
                public readonly AType ArgType;
@@ -3968,6 +3937,12 @@ namespace Mono.CSharp {
                        this.ArgType = type;
                }
 
+               public Argument (Expression expr)
+               {
+                       this.Expr = expr;
+                       this.ArgType = AType.Expression;
+               }
+
                public Type Type {
                        get {
                                if (ArgType == AType.Ref || ArgType == AType.Out)
@@ -3993,6 +3968,9 @@ namespace Mono.CSharp {
 
                public static string FullDesc (Argument a)
                {
+                       if (a.ArgType == AType.ArgList)
+                               return "__arglist";
+
                        return (a.ArgType == AType.Ref ? "ref " :
                                (a.ArgType == AType.Out ? "out " : "")) +
                                TypeManager.CSharpName (a.Expr.Type);
@@ -4154,8 +4132,7 @@ namespace Mono.CSharp {
 
                                return (ParameterData) ip;
                        } else {
-                               ParameterInfo [] pi = mb.GetParameters ();
-                               ReflectionParameters rp = new ReflectionParameters (pi);
+                               ReflectionParameters rp = new ReflectionParameters (mb);
                                method_parameter_cache [mb] = rp;
 
                                return (ParameterData) rp;
@@ -4177,6 +4154,9 @@ namespace Mono.CSharp {
                                throw new Exception ("Expression of type " + a.Expr +
                                                      " does not resolve its type");
 
+                       if (p == null || q == null)
+                               throw new InternalErrorException ("BetterConversion Got a null conversion");
+
                        //
                        // This is a special case since csc behaves this way.
                        //
@@ -4197,16 +4177,15 @@ namespace Mono.CSharp {
                         // I can't find this anywhere in the spec but we can interpret this
                         // to mean that null can be of any type you wish in such a context
                         //
-                        if (p != null && q != null) {
-                                if (argument_expr is NullLiteral &&
-                                    !p.IsValueType &&
-                                    q == TypeManager.object_type)
-                                        return 1;
-                                else if (argument_expr is NullLiteral &&
-                                         !q.IsValueType &&
-                                         p == TypeManager.object_type)
-                                        return 0;
-                        }
+                       if (argument_expr is NullLiteral &&
+                           !p.IsValueType &&
+                           q == TypeManager.object_type)
+                               return 1;
+                       else if (argument_expr is NullLiteral &&
+                                !q.IsValueType &&
+                                p == TypeManager.object_type)
+                               return 0;
+
                                 
                        if (p == q)
                                return 0;
@@ -4217,94 +4196,6 @@ namespace Mono.CSharp {
                        if (argument_type == q)
                                return 0;
 
-                       //
-                       // Now probe whether an implicit constant expression conversion
-                       // can be used.
-                       //
-                       // An implicit constant expression conversion permits the following
-                       // conversions:
-                       //
-                       //    * A constant-expression of type `int' can be converted to type
-                       //      sbyte, byute, short, ushort, uint, ulong provided the value of
-                       //      of the expression is withing the range of the destination type.
-                       //
-                       //    * A constant-expression of type long can be converted to type
-                       //      ulong, provided the value of the constant expression is not negative
-                       //
-                       // FIXME: Note that this assumes that constant folding has
-                       // taken place.  We dont do constant folding yet.
-                       //
-
-                       if (argument_expr is IntConstant){
-                               IntConstant ei = (IntConstant) argument_expr;
-                               int value = ei.Value;
-
-                               if (p == TypeManager.sbyte_type){
-                                       if (value >= SByte.MinValue && value <= SByte.MaxValue)
-                                               return 1;
-                               } else if (p == TypeManager.byte_type){
-                                       if (q == TypeManager.sbyte_type &&
-                                           value >= SByte.MinValue && value <= SByte.MaxValue)
-                                               return 0;
-                                       else if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
-                                               return 1;
-                               } else if (p == TypeManager.short_type){
-                                       if (value >= Int16.MinValue && value <= Int16.MaxValue)
-                                               return 1;
-                               } else if (p == TypeManager.ushort_type){
-                                       if (q == TypeManager.short_type &&
-                                           value >= Int16.MinValue && value <= Int16.MaxValue)
-                                               return 0;
-                                       else if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
-                                               return 1;
-                               } else if (p == TypeManager.int32_type){
-                                       if (value >= Int32.MinValue && value <= Int32.MaxValue)
-                                               return 1;
-                               } else if (p == TypeManager.uint32_type){
-                                       //
-                                       // we can optimize this case: a positive int32
-                                       // always fits on a uint32
-                                       //
-                                       if (value >= 0)
-                                               return 1;
-                               } else if (p == TypeManager.uint64_type){
-                                       //
-                                       // we can optimize this case: a positive int32
-                                       // always fits on a uint64
-                                       //
-
-                                        //
-                                        // This special case is needed because csc behaves like this.
-                                        // int -> uint is better than int -> ulong!
-                                        //
-                                        if (q == TypeManager.uint32_type)
-                                                return 0;
-                                        
-                                       if (q == TypeManager.int64_type)
-                                               return 0;
-                                       else if (value >= 0)
-                                               return 1;
-                               } else if (p == TypeManager.int64_type){
-                                       return 1;
-                               }
-                       } else if (argument_type == TypeManager.int64_type && argument_expr is LongConstant){
-                               LongConstant lc = (LongConstant) argument_expr;
-                               
-                               if (p == TypeManager.uint64_type){
-                                       if (lc.Value > 0)
-                                               return 1;
-                               }
-                       }
-
-                       if (q == null) {
-                               Expression tmp = Convert.ImplicitConversion (ec, argument_expr, p, loc);
-                               
-                               if (tmp != null)
-                                       return 1;
-                               else
-                                       return 0;
-                       }
-
                        Expression p_tmp = new EmptyExpression (p);
                        Expression q_tmp = new EmptyExpression (q);
                        
@@ -4342,20 +4233,13 @@ namespace Mono.CSharp {
                ///     0 if candidate ain't better
                ///     1 if candidate is better than the current best match
                /// </remarks>
-               static int BetterFunction (EmitContext ec, ArrayList args,
+               static int BetterFunction (EmitContext ec, ArrayList args, int argument_count,
                                           MethodBase candidate, bool candidate_params,
-                                           MethodBase best, bool best_params,
-                                          Location loc)
+                                          MethodBase best, bool best_params, Location loc)
                {
                        ParameterData candidate_pd = GetParameterData (candidate);
-                       ParameterData best_pd;
-                       int argument_count;
+                       ParameterData best_pd = GetParameterData (best);
                
-                       if (args == null)
-                               argument_count = 0;
-                       else
-                               argument_count = args.Count;
-
                        int cand_count = candidate_pd.Count;
                        
                        //
@@ -4380,41 +4264,13 @@ namespace Mono.CSharp {
                        // Trim (); is better than Trim (params char[] chars);
                         //
                        if (cand_count == 0 && argument_count == 0)
-                               return best == null || best_params ? 1 : 0;
+                               return best_params ? 1 : 0;
 
-                       if (candidate_pd.ParameterModifier (cand_count - 1) != Parameter.Modifier.PARAMS)
+                       if ((candidate_pd.ParameterModifier (cand_count - 1) != Parameter.Modifier.PARAMS) &&
+                           (candidate_pd.ParameterModifier (cand_count - 1) != Parameter.Modifier.ARGLIST))
                                if (cand_count != argument_count)
                                        return 0;
 
-                       if (best == null) {
-                               int x = 0;
-
-                               if (argument_count == 0 && cand_count == 1 &&
-                                   candidate_pd.ParameterModifier (cand_count - 1) == Parameter.Modifier.PARAMS)
-                                       return 1;
-                               
-                               for (int j = 0; j < argument_count; ++j) {
-
-                                       Argument a = (Argument) args [j];
-                                       Type t = candidate_pd.ParameterType (j);
-
-                                       if (candidate_pd.ParameterModifier (j) == Parameter.Modifier.PARAMS)
-                                               if (candidate_params)
-                                                       t = TypeManager.GetElementType (t);
-
-                                       x = BetterConversion (ec, a, t, null, loc);
-                                       
-                                       if (x <= 0)
-                                               break;
-                               }
-
-                               if (x > 0)
-                                       return 1;
-                               else
-                                       return 0;
-                       }
-
-                       best_pd = GetParameterData (best);
 
                        int rating1 = 0, rating2 = 0;
                        
@@ -4538,30 +4394,52 @@ namespace Mono.CSharp {
                        return union;
                }
 
+               static bool IsParamsMethodApplicable (EmitContext ec, MethodGroupExpr me,
+                                                     ArrayList arguments, int arg_count,
+                                                     ref MethodBase candidate)
+               {
+                       return IsParamsMethodApplicable (
+                               ec, me, arguments, arg_count, false, ref candidate) ||
+                               IsParamsMethodApplicable (
+                                       ec, me, arguments, arg_count, true, ref candidate);
+
+
+               }
+
+               static bool IsParamsMethodApplicable (EmitContext ec, MethodGroupExpr me,
+                                                     ArrayList arguments, int arg_count,
+                                                     bool do_varargs, ref MethodBase candidate)
+               {
+                       return IsParamsMethodApplicable (
+                               ec, arguments, arg_count, candidate, do_varargs);
+               }
+
                /// <summary>
                ///   Determines if the candidate method, if a params method, is applicable
                ///   in its expanded form to the given set of arguments
                /// </summary>
-               static bool IsParamsMethodApplicable (EmitContext ec, ArrayList arguments, MethodBase candidate)
+               static bool IsParamsMethodApplicable (EmitContext ec, ArrayList arguments,
+                                                     int arg_count, MethodBase candidate,
+                                                     bool do_varargs)
                {
-                       int arg_count;
-                       
-                       if (arguments == null)
-                               arg_count = 0;
-                       else
-                               arg_count = arguments.Count;
-                       
                        ParameterData pd = 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)
+                                       return false;
+                               if (pd_count != arg_count)
+                                       return false;
+                       } else {
+                               if (pd.ParameterModifier (count) != Parameter.Modifier.PARAMS)
+                                       return false;
+                       }
                        
-                       if (pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS)
-                               return false;
-                       
-                       if (pd_count - 1 > arg_count)
+                       if (count > arg_count)
                                return false;
                        
                        if (pd_count == 1 && arg_count == 0)
@@ -4572,7 +4450,7 @@ namespace Mono.CSharp {
                        // remains is when the number of parameters is
                        // less than or equal to the argument count.
                        //
-                       for (int i = 0; i < pd_count - 1; ++i) {
+                       for (int i = 0; i < count; ++i) {
 
                                Argument a = (Argument) arguments [i];
 
@@ -4603,11 +4481,19 @@ namespace Mono.CSharp {
                                
                        }
 
+                       if (do_varargs) {
+                               Argument a = (Argument) arguments [count];
+                               if (!(a.Expr is Arglist))
+                                       return false;
+
+                               return true;
+                       }
+
                        Type element_type = TypeManager.GetElementType (pd.ParameterType (pd_count - 1));
 
                        for (int i = pd_count - 1; i < arg_count; i++) {
                                Argument a = (Argument) arguments [i];
-                               
+
                                if (!Convert.ImplicitConversionExists (ec, a.Expr, element_type))
                                        return false;
                        }
@@ -4615,20 +4501,20 @@ namespace Mono.CSharp {
                        return true;
                }
 
+               static bool IsApplicable (EmitContext ec, MethodGroupExpr me,
+                                         ArrayList arguments, int arg_count,
+                                         ref MethodBase candidate)
+               {
+                       return IsApplicable (ec, arguments, arg_count, candidate);
+               }
+
                /// <summary>
                ///   Determines if the candidate method is applicable (section 14.4.2.1)
                ///   to the given set of arguments
                /// </summary>
-               static bool IsApplicable (EmitContext ec, ArrayList arguments, MethodBase candidate)
+               static bool IsApplicable (EmitContext ec, ArrayList arguments, int arg_count,
+                                         MethodBase candidate)
                {
-                       int arg_count;
-
-                       if (arguments == null)
-                               arg_count = 0;
-                       else
-                               arg_count = arguments.Count;
-
-
                        ParameterData pd = GetParameterData (candidate);
 
                        if (arg_count != pd.Count)
@@ -4669,7 +4555,14 @@ namespace Mono.CSharp {
 
                        return true;
                }
-               
+
+               static private bool IsAncestralType (Type first_type, Type second_type)
+               {
+                       return first_type != second_type &&
+                               (second_type.IsSubclassOf (first_type) ||
+                                TypeManager.ImplementsInterface (second_type, first_type));
+               }
+               
                /// <summary>
                ///   Find the Applicable Function Members (7.4.2.1)
                ///
@@ -4687,11 +4580,13 @@ namespace Mono.CSharp {
                ///
                /// </summary>
                public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
-                                                         ArrayList Arguments, Location loc)
+                                                         ArrayList Arguments, bool may_fail,
+                                                         Location loc)
                {
                        MethodBase method = null;
+                       bool method_params = false;
                        Type applicable_type = null;
-                       int argument_count;
+                       int arg_count = 0;
                        ArrayList candidates = new ArrayList ();
 
                         //
@@ -4701,160 +4596,202 @@ namespace Mono.CSharp {
                         //
                         // false is normal form, true is expanded form
                         //
-                        Hashtable candidate_to_form = new PtrHashtable ();
+                        Hashtable candidate_to_form = null;
 
+                       if (Arguments != null)
+                               arg_count = Arguments.Count;
 
-                        //
-                        // First we construct the set of applicable methods
-                        //
-                        // We start at the top of the type hierarchy and
-                        // go down to find applicable methods
-                        //
-                        applicable_type = me.DeclaringType;
-                        
-                        if (me.Name == "Invoke" && TypeManager.IsDelegateType (applicable_type)) {
+                        if ((me.Name == "Invoke") &&
+                           TypeManager.IsDelegateType (me.DeclaringType)) {
                                 Error_InvokeOnDelegate (loc);
                                 return null;
                         }
 
-                        bool found_applicable = false;
+                       MethodBase[] methods = me.Methods;
 
-                       foreach (MethodBase candidate in me.Methods){
-                                Type decl_type = candidate.DeclaringType;
+                        //
+                        // First we construct the set of applicable methods
+                        //
+                       bool is_sorted = true;
+                       for (int i = 0; i < methods.Length; 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 (decl_type != applicable_type &&
-                                    (applicable_type.IsSubclassOf (decl_type) ||
-                                     TypeManager.ImplementsInterface (applicable_type, decl_type)) &&
-                                    found_applicable)
-                                                continue;
-
+                                if ((applicable_type != null) &&
+                                   IsAncestralType (decl_type, applicable_type))
+                                       continue;
 
+                               //
                                // Check if candidate is applicable (section 14.4.2.1)
-                               if (IsApplicable (ec, Arguments, candidate)) {
-                                        // Candidate is applicable in normal form
-                                        candidates.Add (candidate);
-                                        applicable_type = candidate.DeclaringType;
-                                        found_applicable = true;
-                                        candidate_to_form [candidate] = false;
-                                } else if (IsParamsMethodApplicable (ec, Arguments, candidate)) {
-                                                // Candidate is applicable in expanded form
-                                                candidates.Add (candidate);
-                                                applicable_type = candidate.DeclaringType;
-                                                found_applicable = true; 
-                                                candidate_to_form [candidate] = true;
-                                        }
-                                }
-                        
+                               //   Is candidate applicable in normal form?
+                               //
+                               bool is_applicable = IsApplicable (
+                                       ec, me, Arguments, arg_count, ref methods [i]);
 
-                        //
-                        // Now we actually find the best method
-                        //
-                       int candidate_top = candidates.Count;
-                       for (int ix = 0; ix < candidate_top; ix++){
-                               MethodBase candidate = (MethodBase) candidates [ix];
+                               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 ();
+                                       candidate_to_form [candidate] = candidate;
+                                       // Candidate is applicable in expanded form
+                                       is_applicable = true;
+                               }
 
-                                bool cand_params = (bool) candidate_to_form [candidate];
-                                bool method_params = false;
+                               if (!is_applicable)
+                                       continue;
 
-                                if (method != null)
-                                        method_params = (bool) candidate_to_form [method];
-                                
-                                int x = BetterFunction (ec, Arguments,
-                                                        candidate, cand_params,
-                                                        method, method_params,
-                                                        loc);
-                                
-                                if (x == 0)
-                                        continue;
-                                
-                                method = candidate;
-                        }
+                               candidates.Add (methods [i]);
 
-                       if (Arguments == null)
-                               argument_count = 0;
-                       else
-                               argument_count = Arguments.Count;
-                       
+                               if (applicable_type == null)
+                                       applicable_type = decl_type;
+                               else if (applicable_type != decl_type) {
+                                       is_sorted = false;
+                                       if (IsAncestralType (applicable_type, decl_type))
+                                               applicable_type = decl_type;
+                               }
+                       }
+
+                       int candidate_top = candidates.Count;
 
-                       if (method == null) {
+                       if (candidate_top == 0) {
                                //
                                // Okay so we have failed to find anything so we
                                // return by providing info about the closest match
                                //
-                               for (int i = 0; i < me.Methods.Length; ++i) {
-
-                                       MethodBase c = (MethodBase) me.Methods [i];
+                               for (int i = 0; i < methods.Length; ++i) {
+                                       MethodBase c = (MethodBase) methods [i];
                                        ParameterData pd = GetParameterData (c);
 
-                                       if (pd.Count != argument_count)
+                                       if (pd.Count != arg_count)
                                                continue;
 
-                                       VerifyArgumentsCompat (ec, Arguments, argument_count, c, false,
-                                                              null, loc);
-                                        break;
+                                       VerifyArgumentsCompat (ec, Arguments, arg_count,
+                                                              c, false, null, may_fail, loc);
+                                       break;
                                }
 
-                                if (!Location.IsNull (loc)) {
+                                if (!may_fail) {
                                         string report_name = me.Name;
                                         if (report_name == ".ctor")
                                                 report_name = me.DeclaringType.ToString ();
                                         
-                                        Error_WrongNumArguments (loc, report_name, argument_count);
+                                       Error_WrongNumArguments (
+                                               loc, report_name, arg_count);
+                                       return null;
                                 }
                                 
                                return null;
                        }
 
+                       if (!is_sorted) {
+                               //
+                               // At this point, applicable_type is _one_ of the most derived types
+                               // in the set of types containing the methods in this MethodGroup.
+                               // Filter the candidates so that they only contain methods from the
+                               // most derived types.
+                               //
+
+                               int finalized = 0; // Number of finalized candidates
+
+                               do {
+                                       // Invariant: applicable_type is a most derived type
+
+                                       // We'll try to complete Section 14.5.5.1 for 'applicable_type' by 
+                                       // eliminating all it's base types.  At the same time, we'll also move
+                                       // every unrelated type to the end of the array, and pick the next
+                                       // 'applicable_type'.
+
+                                       Type next_applicable_type = null;
+                                       int j = finalized; // where to put the next finalized candidate
+                                       int k = finalized; // where to put the next undiscarded candidate
+                                       for (int i = finalized; i < candidate_top; ++i) {
+                                               Type decl_type = ((MethodBase) candidates[i]).DeclaringType;
+
+                                               if (decl_type == applicable_type) {
+                                                       candidates[k++] = candidates[j];
+                                                       candidates[j++] = candidates[i];
+                                                       continue;
+                                               }
+
+                                               if (IsAncestralType (decl_type, applicable_type))
+                                                       continue;
+
+                                               if (next_applicable_type != null &&
+                                                   IsAncestralType (decl_type, next_applicable_type))
+                                                       continue;
+
+                                               candidates[k++] = candidates[i];
+
+                                               if (next_applicable_type == null ||
+                                                   IsAncestralType (next_applicable_type, decl_type))
+                                                       next_applicable_type = decl_type;
+                                       }
+
+                                       applicable_type = next_applicable_type;
+                                       finalized = j;
+                                       candidate_top = k;
+                               } while (applicable_type != null);
+                       }
+
+                        //
+                        // Now we actually find the best method
+                        //
+
+                       method = (MethodBase) candidates[0];
+                       method_params = candidate_to_form != null && candidate_to_form.Contains (method);
+                       for (int ix = 1; ix < candidate_top; ix++){
+                               MethodBase candidate = (MethodBase) candidates [ix];
+                               bool cand_params = candidate_to_form != null && candidate_to_form.Contains (candidate);
+                                
+                               if (BetterFunction (ec, Arguments, arg_count, 
+                                                   candidate, cand_params,
+                                                   method, method_params, loc) != 0) {
+                                       method = candidate;
+                                       method_params = cand_params;
+                               }
+                       }
+
                        //
                        // Now check that there are no ambiguities i.e the selected method
                        // should be better than all the others
                        //
-                        bool best_params = (bool) candidate_to_form [method];
-
+                       bool ambiguous = false;
                        for (int ix = 0; ix < candidate_top; ix++){
                                MethodBase candidate = (MethodBase) candidates [ix];
 
                                 if (candidate == method)
                                         continue;
-                                               
-                               //
-                               // If a normal method is applicable in
-                               // the sense that it has the same
-                               // number of arguments, then the
-                               // expanded params method is never
-                               // applicable so we debar the params
-                               // method.
-                               //
-                                // if ((IsParamsMethodApplicable (ec, Arguments, candidate) &&
-//                                      IsApplicable (ec, Arguments, method)))
-//                                         continue;
-                                
-                                bool cand_params = (bool) candidate_to_form [candidate];
-                               int x = BetterFunction (ec, Arguments,
-                                                        method, best_params,
-                                                        candidate, cand_params,
-                                                       loc);
-
-                               if (x != 1) {
-                                       Report.Error (
-                                               121, loc,
-                                               "Ambiguous call when selecting function due to implicit casts");
-                                       return null;
-                               }
+
+                                bool cand_params = candidate_to_form != null && candidate_to_form.Contains (candidate);
+                               if (BetterFunction (ec, Arguments, arg_count,
+                                                   method, method_params,
+                                                   candidate, cand_params,
+                                                   loc) != 1) {
+                                       Report.SymbolRelatedToPreviousError (candidate);
+                                       ambiguous = true;
+                               }
+                       }
+
+                       if (ambiguous) {
+                               Report.SymbolRelatedToPreviousError (method);
+                               Report.Error (121, loc, "Ambiguous call when selecting function due to implicit casts");                                        
+                               return null;
                        }
 
+
                        //
                        // And now check if the arguments are all
                        // compatible, perform conversions if
                        // necessary etc. and return if everything is
                        // all right
                        //
-                        if (!VerifyArgumentsCompat (ec, Arguments, argument_count, method,
-                                                    best_params, null, loc))
+                        if (!VerifyArgumentsCompat (ec, Arguments, arg_count, method,
+                                                    method_params, null, may_fail, loc))
                                return null;
 
                        return method;
@@ -4891,16 +4828,15 @@ namespace Mono.CSharp {
                }
                
                public static bool VerifyArgumentsCompat (EmitContext ec, ArrayList Arguments,
-                                                         int argument_count,
-                                                         MethodBase method, 
+                                                         int arg_count, MethodBase method, 
                                                          bool chose_params_expanded,
-                                                         Type delegate_type,
+                                                         Type delegate_type, bool may_fail,
                                                          Location loc)
                {
                        ParameterData pd = GetParameterData (method);
                        int pd_count = pd.Count;
-                       
-                       for (int j = 0; j < argument_count; j++) {
+
+                       for (int j = 0; j < arg_count; j++) {
                                Argument a = (Argument) Arguments [j];
                                Expression a_expr = a.Expr;
                                Type parameter_type = pd.ParameterType (j);
@@ -4908,7 +4844,7 @@ namespace Mono.CSharp {
                                
                                if (pm == Parameter.Modifier.PARAMS){
                                        if ((pm & ~Parameter.Modifier.PARAMS) != a.GetParameterModifier ()) {
-                                               if (!Location.IsNull (loc))
+                                               if (!may_fail)
                                                        Error_InvalidArguments (
                                                                loc, j, method, delegate_type,
                                                                Argument.FullDesc (a), pd.ParameterDesc (j));
@@ -4917,12 +4853,14 @@ namespace Mono.CSharp {
 
                                        if (chose_params_expanded)
                                                parameter_type = TypeManager.GetElementType (parameter_type);
+                               } else if (pm == Parameter.Modifier.ARGLIST){
+                                       continue;
                                } else {
                                        //
                                        // Check modifiers
                                        //
                                        if (pd.ParameterModifier (j) != a.GetParameterModifier ()){
-                                               if (!Location.IsNull (loc))
+                                               if (!may_fail)
                                                        Error_InvalidArguments (
                                                                loc, j, method, delegate_type,
                                                                Argument.FullDesc (a), pd.ParameterDesc (j));
@@ -4933,13 +4871,13 @@ namespace Mono.CSharp {
                                //
                                // Check Type
                                //
-                               if (a.Type != parameter_type){
+                               if (!a.Type.Equals (parameter_type)){
                                        Expression conv;
                                        
                                        conv = Convert.ImplicitConversion (ec, a_expr, parameter_type, loc);
 
                                        if (conv == null) {
-                                               if (!Location.IsNull (loc)) 
+                                               if (!may_fail)
                                                        Error_InvalidArguments (
                                                                loc, j, method, delegate_type,
                                                                Argument.FullDesc (a), pd.ParameterDesc (j));
@@ -4960,7 +4898,7 @@ namespace Mono.CSharp {
                                
                                if (a_mod != p_mod &&
                                    pd.ParameterModifier (pd_count - 1) != Parameter.Modifier.PARAMS) {
-                                       if (!Location.IsNull (loc)) {
+                                       if (!may_fail) {
                                                Report.Error (1502, loc,
                                                       "The best overloaded match for method '" + FullMethodDesc (method)+
                                                       "' has some invalid arguments");
@@ -5017,19 +4955,28 @@ namespace Mono.CSharp {
                        }
 
                        MethodGroupExpr mg = (MethodGroupExpr) expr;
-                       method = OverloadResolve (ec, mg, Arguments, loc);
+                       method = OverloadResolve (ec, mg, Arguments, false, loc);
 
-                       if (method == null){
-                               Error (-6,
-                                      "Could not find any applicable function for this argument list");
+                       if (method == null)
                                return null;
-                       }
-
+                       
                        MethodInfo mi = method as MethodInfo;
                        if (mi != null) {
                                type = TypeManager.TypeToCoreType (mi.ReturnType);
-                               if (!mi.IsStatic && !mg.IsExplicitImpl && (mg.InstanceExpression == null))
+                               if (!mi.IsStatic && !mg.IsExplicitImpl && (mg.InstanceExpression == null)) {
                                        SimpleName.Error_ObjectRefRequired (ec, loc, mi.Name);
+                                       return null;
+                               }
+
+                               Expression iexpr = mg.InstanceExpression;
+                               if (mi.IsStatic && (iexpr != null) && !(iexpr is This)) {
+                                       if (mg.IdenticalTypeName)
+                                               mg.InstanceExpression = null;
+                                       else {
+                                               MemberAccess.error176 (loc, mi.Name);
+                                               return null;
+                                       }
+                               }
                        }
 
                        if (type.IsPointer){
@@ -5052,7 +4999,7 @@ namespace Mono.CSharp {
                                if (TypeManager.IsSpecialMethod (method))
                                        Report.Error (571, loc, method.Name + ": can not call operator or accessor");
                        }
-                       
+
                        eclass = ExprClass.Value;
                        return this;
                }
@@ -5099,14 +5046,24 @@ namespace Mono.CSharp {
                ///   emission of the arguments is known not to contain
                ///   a `params' field (for example in constructors or other routines
                ///   that keep their arguments in this structure)
+               ///   
+               ///   if `dup_args' is true, a copy of the arguments will be left
+               ///   on the stack. If `dup_args' is true, you can specify `this_arg'
+               ///   which will be duplicated before any other args. Only EmitCall
+               ///   should be using this interface.
                /// </summary>
-               public static void EmitArguments (EmitContext ec, MethodBase mb, ArrayList arguments)
+               public static void EmitArguments (EmitContext ec, MethodBase mb, ArrayList arguments, bool dup_args, LocalTemporary this_arg)
                {
                        ParameterData pd;
                        if (mb != null)
                                pd = GetParameterData (mb);
                        else
                                pd = null;
+                       
+                       LocalTemporary [] temps = null;
+                       
+                       if (dup_args)
+                               temps = new LocalTemporary [arguments.Count];
 
                        //
                        // If we are calling a params method with no arguments, special case it
@@ -5143,6 +5100,18 @@ namespace Mono.CSharp {
                                }
                                            
                                a.Emit (ec);
+                               if (dup_args) {
+                                       ec.ig.Emit (OpCodes.Dup);
+                                       (temps [i] = new LocalTemporary (ec, a.Type)).Store (ec);
+                               }
+                       }
+                       
+                       if (dup_args) {
+                               if (this_arg != null)
+                                       this_arg.Emit (ec);
+                               
+                               for (int i = 0; i < top; i ++)
+                                       temps [i].Emit (ec);
                        }
 
                        if (pd != null && pd.Count > top &&
@@ -5154,6 +5123,40 @@ namespace Mono.CSharp {
                        }
                }
 
+               static Type[] GetVarargsTypes (EmitContext ec, MethodBase mb,
+                                              ArrayList arguments)
+               {
+                       ParameterData pd = GetParameterData (mb);
+
+                       if (arguments == null)
+                               return new Type [0];
+
+                       Argument a = (Argument) arguments [pd.Count - 1];
+                       Arglist list = (Arglist) a.Expr;
+
+                       return list.ArgumentTypes;
+               }
+
+               /// <summary>
+               /// This checks the ConditionalAttribute on the method 
+               /// </summary>
+               static bool IsMethodExcluded (MethodBase method, EmitContext ec)
+               {
+                       if (method.IsConstructor)
+                               return false;
+
+                       IMethodData md = TypeManager.GetMethod (method);
+                       if (md != null)
+                               return md.IsExcluded (ec);
+
+                       // For some methods (generated by delegate class) GetMethod returns null
+                       // because they are not included in builder_to_method table
+                       if (method.DeclaringType is TypeBuilder)
+                               return false;
+
+                       return AttributeTester.IsConditionalMethodExcluded (method);
+               }
+
                /// <remarks>
                ///   is_base tells whether we want to force the use of the `call'
                ///   opcode instead of using callvirt.  Call is required to call
@@ -5172,10 +5175,25 @@ namespace Mono.CSharp {
                public static void EmitCall (EmitContext ec, bool is_base,
                                             bool is_static, Expression instance_expr,
                                             MethodBase method, ArrayList Arguments, Location loc)
+               {
+                       EmitCall (ec, is_base, is_static, instance_expr, method, Arguments, loc, false, false);
+               }
+               
+               // `dup_args' leaves an extra copy of the arguments on the stack
+               // `omit_args' does not leave any arguments at all.
+               // So, basically, you could make one call with `dup_args' set to true,
+               // and then another with `omit_args' set to true, and the two calls
+               // would have the same set of arguments. However, each argument would
+               // only have been evaluated once.
+               public static void EmitCall (EmitContext ec, bool is_base,
+                                            bool is_static, Expression instance_expr,
+                                            MethodBase method, ArrayList Arguments, Location loc,
+                                            bool dup_args, bool omit_args)
                {
                        ILGenerator ig = ec.ig;
                        bool struct_call = false;
                        bool this_call = false;
+                       LocalTemporary this_arg = null;
 
                        Type decl_type = method.DeclaringType;
 
@@ -5199,36 +5217,44 @@ namespace Mono.CSharp {
                        }
 
                        //
-                       // This checks the `ConditionalAttribute' on the method, and the
-                       // ObsoleteAttribute
+                       // This checks ObsoleteAttribute on the method and on the declaring type
                        //
-                       TypeManager.MethodFlags flags = TypeManager.GetMethodFlags (method, loc);
-                       if ((flags & TypeManager.MethodFlags.IsObsoleteError) != 0)
-                               return;
-                       if ((flags & TypeManager.MethodFlags.ShouldIgnore) != 0)
-                               return;
+                       ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (method);
+                       if (oa != null)
+                               AttributeTester.Report_ObsoleteMessage (oa, TypeManager.CSharpSignature (method), loc);
+
+
+                       oa = AttributeTester.GetObsoleteAttribute (method.DeclaringType);
+                       if (oa != null) {
+                               AttributeTester.Report_ObsoleteMessage (oa, method.DeclaringType.FullName, loc);
+                       }
+
+                       if (IsMethodExcluded (method, ec))
+                return; 
                        
                        if (!is_static){
-                               if (decl_type.IsValueType)
+                               this_call = instance_expr == null;
+                               if (decl_type.IsValueType || (!this_call && instance_expr.Type.IsValueType))
                                        struct_call = true;
+                               
                                //
                                // If this is ourselves, push "this"
                                //
-                               if (instance_expr == null) {
-                                       this_call = true;
+                               if (!omit_args) {
+                               Type t = null;
+                               if (this_call) {
                                        ig.Emit (OpCodes.Ldarg_0);
+                                       t = decl_type;
                                } else {
                                        //
                                        // Push the instance expression
                                        //
-                                       if (instance_expr.Type.IsValueType){
+                                       if (instance_expr.Type.IsValueType) {
                                                //
                                                // Special case: calls to a function declared in a 
                                                // reference-type with a value-type argument need
-                                               // to have their value boxed.  
-
-                                               struct_call = true;
-                                               if (decl_type.IsValueType){
+                                               // to have their value boxed.
+                                               if (decl_type.IsValueType) {
                                                        //
                                                        // If the expression implements IMemoryLocation, then
                                                        // we can optimize and use AddressOf on the
@@ -5236,45 +5262,63 @@ namespace Mono.CSharp {
                                                        //
                                                        // If not we have to use some temporary storage for
                                                        // it.
-                                                       if (instance_expr is IMemoryLocation){
+                                                       if (instance_expr is IMemoryLocation) {
                                                                ((IMemoryLocation)instance_expr).
                                                                        AddressOf (ec, AddressOp.LoadStore);
-                                                       }
-                                                       else {
-                                                               Type t = instance_expr.Type;
-                                                               
+                                                       } else {
+                                                               LocalTemporary temp = new LocalTemporary (ec, instance_expr.Type);
                                                                instance_expr.Emit (ec);
-                                                               LocalBuilder temp = ig.DeclareLocal (t);
-                                                               ig.Emit (OpCodes.Stloc, temp);
-                                                               ig.Emit (OpCodes.Ldloca, temp);
+                                                               temp.Store (ec);
+                                                               temp.AddressOf (ec, AddressOp.Load);
                                                        }
+                                                       
+                                                       // avoid the overhead of doing this all the time.
+                                                       if (dup_args)
+                                                               t = TypeManager.GetReferenceType (instance_expr.Type);
                                                } else {
                                                        instance_expr.Emit (ec);
                                                        ig.Emit (OpCodes.Box, instance_expr.Type);
+                                                       t = TypeManager.object_type;
                                                } 
-                                       } else
+                                       } else {
                                                instance_expr.Emit (ec);
+                                               t = instance_expr.Type;
+                                       }
+                               }
+                               
+                               if (dup_args) {
+                                       this_arg = new LocalTemporary (ec, t);
+                                       ig.Emit (OpCodes.Dup);
+                                       this_arg.Store (ec);
                                }
+                               }
+                       }
+
+                       if (!omit_args)
+                               EmitArguments (ec, method, Arguments, dup_args, this_arg);
+
+                       OpCode call_op;
+                       if (is_static || struct_call || is_base || (this_call && !method.IsVirtual))
+                               call_op = OpCodes.Call;
+                       else
+                               call_op = OpCodes.Callvirt;
+
+                       if ((method.CallingConvention & CallingConventions.VarArgs) != 0) {
+                               Type[] varargs_types = GetVarargsTypes (ec, method, Arguments);
+                               ig.EmitCall (call_op, (MethodInfo) method, varargs_types);
+                               return;
                        }
 
-                       EmitArguments (ec, method, Arguments);
                        //
                        // If you have:
                        // this.DoFoo ();
                        // and DoFoo is not virtual, you can omit the callvirt,
                        // because you don't need the null checking behavior.
                        //
-                       if (is_static || struct_call || is_base || (this_call && !method.IsVirtual)){
-                               if (method is MethodInfo) {
-                                       ig.Emit (OpCodes.Call, (MethodInfo) method);
-                               } else
-                                       ig.Emit (OpCodes.Call, (ConstructorInfo) method);
-                       } else {
-                               if (method is MethodInfo)
-                                       ig.Emit (OpCodes.Callvirt, (MethodInfo) method);
-                               else
-                                       ig.Emit (OpCodes.Callvirt, (ConstructorInfo) method);
-                       }
+                       if (method is MethodInfo)
+                               ig.Emit (call_op, (MethodInfo) method);
+                       else
+                               ig.Emit (call_op, (ConstructorInfo) method);
                }
                
                public override void Emit (EmitContext ec)
@@ -5503,6 +5547,8 @@ namespace Mono.CSharp {
                        if (type == null)
                                return null;
                        
+                       CheckObsoleteAttribute (type);
+
                        bool IsDelegate = TypeManager.IsDelegateType (type);
                        
                        if (IsDelegate){
@@ -5513,6 +5559,11 @@ namespace Mono.CSharp {
                                return RequestedType;
                        }
 
+                       if (type.IsAbstract && type.IsSealed) {
+                               Report.Error (712, loc, "Cannot create an instance of the static class '{0}'", TypeManager.CSharpName (type));
+                               return null;
+                       }
+
                        if (type.IsInterface || type.IsAbstract){
                                Error (144, "It is not possible to create instances of interfaces or abstract classes");
                                return null;
@@ -5529,7 +5580,8 @@ namespace Mono.CSharp {
                                return this;
                        
                        Expression ml;
-                       ml = MemberLookupFinal (ec, null, type, ".ctor",
+                       // For member-lookup, treat 'new Foo (bar)' as call to 'foo.ctor (bar)', where 'foo' is of type 'Foo'.
+                       ml = MemberLookupFinal (ec, type, type, ".ctor",
                                                MemberTypes.Constructor,
                                                AllBindingFlags | BindingFlags.DeclaredOnly, loc);
 
@@ -5551,7 +5603,8 @@ namespace Mono.CSharp {
                                        }
                                }
 
-                               method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml, Arguments, loc);
+                               method = Invocation.OverloadResolve (
+                                       ec, (MethodGroupExpr) ml, Arguments, false, loc);
                                
                        }
 
@@ -5610,7 +5663,7 @@ namespace Mono.CSharp {
                        }
 
                        if (method != null)
-                               Invocation.EmitArguments (ec, method, Arguments);
+                               Invocation.EmitArguments (ec, method, Arguments, false, null);
 
                        if (is_value_type){
                                if (method == null)
@@ -5657,7 +5710,7 @@ namespace Mono.CSharp {
                        IMemoryLocation ml = (IMemoryLocation) value_target;
                        ml.AddressOf (ec, AddressOp.Store);
                        if (method != null)
-                               Invocation.EmitArguments (ec, method, Arguments);
+                               Invocation.EmitArguments (ec, method, Arguments, false, null);
 
                        if (method == null)
                                ec.ig.Emit (OpCodes.Initobj, type);
@@ -5678,7 +5731,7 @@ namespace Mono.CSharp {
        ///   initialization data and the other which does not need dimensions
        ///   specified but where initialization data is mandatory.
        /// </remarks>
-       public class ArrayCreation : ExpressionStatement {
+       public class ArrayCreation : Expression {
                Expression requested_base_type;
                ArrayList initializers;
 
@@ -5988,10 +6041,12 @@ namespace Mono.CSharp {
 
                        if (type == null)
                                return false;
-
-                       underlying_type = type;
-                       if (underlying_type.IsArray)
-                               underlying_type = TypeManager.GetElementType (underlying_type);
+                       
+                       if (!type.IsArray) {
+                               Error (622, "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
+                               return false;
+                       }
+                       underlying_type = TypeManager.GetElementType (type);
                        dimensions = type.GetArrayRank ();
 
                        return true;
@@ -6029,6 +6084,11 @@ namespace Mono.CSharp {
                        
                        array_element_type = TypeManager.GetElementType (type);
 
+                       if (array_element_type.IsAbstract && array_element_type.IsSealed) {
+                               Report.Error (719, loc, "'{0}': array elements cannot be of static type", TypeManager.CSharpName (array_element_type));
+                               return null;
+                       }
+
                        if (arg_count == 1) {
                                is_one_dimensional = true;
                                eclass = ExprClass.Value;
@@ -6054,7 +6114,8 @@ namespace Mono.CSharp {
                                        return null;
                                }
                                
-                               new_method = Invocation.OverloadResolve (ec, (MethodGroupExpr) ml, arguments, loc);
+                               new_method = Invocation.OverloadResolve (
+                                       ec, (MethodGroupExpr) ml, arguments, false, loc);
 
                                if (new_method == null) {
                                        Error (-6, "New invocation: Can not find a constructor for " +
@@ -6241,7 +6302,7 @@ namespace Mono.CSharp {
                //
                // Emits the initializers for the array
                //
-               void EmitStaticInitializers (EmitContext ec, bool is_expression)
+               void EmitStaticInitializers (EmitContext ec)
                {
                        //
                        // First, the static data
@@ -6253,8 +6314,7 @@ namespace Mono.CSharp {
 
                        fb = RootContext.MakeStaticData (data);
 
-                       if (is_expression)
-                               ig.Emit (OpCodes.Dup);
+                       ig.Emit (OpCodes.Dup);
                        ig.Emit (OpCodes.Ldtoken, fb);
                        ig.Emit (OpCodes.Call,
                                 TypeManager.void_initializearray_array_fieldhandle);
@@ -6266,7 +6326,7 @@ namespace Mono.CSharp {
                //
                // This always expect the top value on the stack to be the array
                //
-               void EmitDynamicInitializers (EmitContext ec, bool is_expression)
+               void EmitDynamicInitializers (EmitContext ec)
                {
                        ILGenerator ig = ec.ig;
                        int dims = bounds.Count;
@@ -6313,8 +6373,7 @@ namespace Mono.CSharp {
                                            num_automatic_initializers <= max_automatic_initializers) {
                                                Type etype = e.Type;
                                                
-                                               if (is_expression || i != top - 1)
-                                                       ig.Emit (OpCodes.Dup);
+                                               ig.Emit (OpCodes.Dup);
 
                                                for (int idx = 0; idx < dims; idx++) 
                                                        IntConstant.EmitInt (ig, current_pos [idx]);
@@ -6342,9 +6401,14 @@ namespace Mono.CSharp {
 
                                                e.Emit (ec);
 
-                                               if (dims == 1)
-                                                       ArrayAccess.EmitStoreOpcode (ig, array_element_type);
-                                               else 
+                                               if (dims == 1) {
+                                                       bool is_stobj;
+                                                       OpCode op = ArrayAccess.GetStoreOpcode (etype, out is_stobj);
+                                                       if (is_stobj)
+                                                               ig.Emit (OpCodes.Stobj, etype);
+                                                       else
+                                                               ig.Emit (op);
+                                               } else 
                                                        ig.Emit (OpCodes.Call, set);
 
                                        }
@@ -6377,7 +6441,7 @@ namespace Mono.CSharp {
                        }
                }
                
-               void DoEmit (EmitContext ec, bool is_statement)
+               public override void Emit (EmitContext ec)
                {
                        ILGenerator ig = ec.ig;
                        
@@ -6401,22 +6465,12 @@ namespace Mono.CSharp {
                                // initialized. num_automatic_initializers will always be zero.  See
                                // CheckIndices.
                                if (num_automatic_initializers > max_automatic_initializers)
-                                       EmitStaticInitializers (ec, dynamic_initializers || !is_statement);
+                                       EmitStaticInitializers (ec);
                                
                                if (dynamic_initializers)
-                                       EmitDynamicInitializers (ec, !is_statement);
+                                       EmitDynamicInitializers (ec);
                        }
                }
-               
-               public override void Emit (EmitContext ec)
-               {
-                       DoEmit (ec, false);
-               }
-
-               public override void EmitStatement (EmitContext ec)
-               {
-                       DoEmit (ec, true);
-               }
 
                public object EncodeAsAttribute ()
                {
@@ -6438,7 +6492,7 @@ namespace Mono.CSharp {
                                if (e is NullLiteral)
                                        v = null;
                                else {
-                                       if (!Attribute.GetAttributeArgumentExpression (e, Location, out v))
+                                       if (!Attribute.GetAttributeArgumentExpression (e, Location, array_element_type, out v))
                                                return null;
                                }
                                ret [i++] = v;
@@ -6541,28 +6595,36 @@ namespace Mono.CSharp {
                        return this;
                }
 
-               public override void Emit (EmitContext ec)
+               public void Emit (EmitContext ec, bool leave_copy)
                {
-                       ILGenerator ig = ec.ig;
-
-                       ec.EmitThis ();
-                       if (ec.TypeContainer is Struct)
-                               ig.Emit (OpCodes.Ldobj, type);
+                       Emit (ec);
+                       if (leave_copy)
+                               ec.ig.Emit (OpCodes.Dup);
                }
-
-               public void EmitAssign (EmitContext ec, Expression source)
+               
+               public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
                {
                        ILGenerator ig = ec.ig;
                        
                        if (ec.TypeContainer is Struct){
                                ec.EmitThis ();
                                source.Emit (ec);
+                               if (leave_copy)
+                                       ec.ig.Emit (OpCodes.Dup);
                                ig.Emit (OpCodes.Stobj, type);
                        } else {
-                               source.Emit (ec);
-                               ig.Emit (OpCodes.Starg, 0);
+                               throw new Exception ("how did you get here");
                        }
                }
+               
+               public override void Emit (EmitContext ec)
+               {
+                       ILGenerator ig = ec.ig;
+
+                       ec.EmitThis ();
+                       if (ec.TypeContainer is Struct)
+                               ig.Emit (OpCodes.Ldobj, type);
+               }
 
                public void AddressOf (EmitContext ec, AddressOp mode)
                {
@@ -6579,6 +6641,85 @@ namespace Mono.CSharp {
                }
        }
 
+       /// <summary>
+       ///   Represents the `__arglist' construct
+       /// </summary>
+       public class ArglistAccess : Expression
+       {
+               public ArglistAccess (Location loc)
+               {
+                       this.loc = loc;
+               }
+
+               public bool ResolveBase (EmitContext ec)
+               {
+                       eclass = ExprClass.Variable;
+                       type = TypeManager.runtime_argument_handle_type;
+                       return true;
+               }
+
+               public override Expression DoResolve (EmitContext ec)
+               {
+                       if (!ResolveBase (ec))
+                               return null;
+
+                       if (ec.IsFieldInitializer || !ec.CurrentBlock.HasVarargs) {
+                               Error (190, "The __arglist construct is valid only within " +
+                                      "a variable argument method.");
+                               return null;
+                       }
+
+                       return this;
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       ec.ig.Emit (OpCodes.Arglist);
+               }
+       }
+
+       /// <summary>
+       ///   Represents the `__arglist (....)' construct
+       /// </summary>
+       public class Arglist : Expression
+       {
+               public readonly Argument[] Arguments;
+
+               public Arglist (Argument[] args, Location l)
+               {
+                       Arguments = args;
+                       loc = l;
+               }
+
+               public Type[] ArgumentTypes {
+                       get {
+                               Type[] retval = new Type [Arguments.Length];
+                               for (int i = 0; i < Arguments.Length; i++)
+                                       retval [i] = Arguments [i].Type;
+                               return retval;
+                       }
+               }
+
+               public override Expression DoResolve (EmitContext ec)
+               {
+                       eclass = ExprClass.Variable;
+                       type = TypeManager.runtime_argument_handle_type;
+
+                       foreach (Argument arg in Arguments) {
+                               if (!arg.Resolve (ec, loc))
+                                       return null;
+                       }
+
+                       return this;
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       foreach (Argument arg in Arguments)
+                               arg.Emit (ec);
+               }
+       }
+
        //
        // This produces the value that renders an instance, used by the iterators code
        //
@@ -6628,6 +6769,8 @@ namespace Mono.CSharp {
                                return null;
                        }
 
+                       CheckObsoleteAttribute (typearg);
+
                        type = TypeManager.type_type;
                        eclass = ExprClass.Type;
                        return this;
@@ -6688,6 +6831,8 @@ namespace Mono.CSharp {
                        if (type_queried == null)
                                return null;
 
+                       CheckObsoleteAttribute (type_queried);
+
                        if (!TypeManager.IsUnmanagedType (type_queried)){
                                Report.Error (208, loc, "Cannot take the size of an unmanaged type (" + TypeManager.CSharpName (type_queried) + ")");
                                return null;
@@ -6729,7 +6874,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               static void error176 (Location loc, string name)
+               public static void error176 (Location loc, string name)
                {
                        Report.Error (176, loc, "Static member `" +
                                      name + "' cannot be accessed " +
@@ -6737,21 +6882,13 @@ namespace Mono.CSharp {
                                      "type name instead");
                }
 
-               static bool IdenticalNameAndTypeName (EmitContext ec, Expression left_original, Location loc)
+               public static bool IdenticalNameAndTypeName (EmitContext ec, Expression left_original, Expression left, Location loc)
                {
-                       if (left_original == null)
+                       SimpleName sn = left_original as SimpleName;
+                       if (sn == null || left == null || left.Type.Name != sn.Name)
                                return false;
 
-                       if (!(left_original is SimpleName))
-                               return false;
-
-                       SimpleName sn = (SimpleName) left_original;
-
-                       Type t = RootContext.LookupType (ec.DeclSpace, sn.Name, true, loc);
-                       if (t != null)
-                               return true;
-
-                       return false;
+                       return RootContext.LookupType (ec.DeclSpace, sn.Name, true, loc) != null;
                }
                
                public static Expression ResolveMemberAccess (EmitContext ec, Expression member_lookup,
@@ -6805,7 +6942,7 @@ namespace Mono.CSharp {
                                        
                                        if (decl_type.IsSubclassOf (TypeManager.enum_type)) {
                                                if (left_is_explicit && !left_is_type &&
-                                                   !IdenticalNameAndTypeName (ec, left_original, loc)) {
+                                                   !IdenticalNameAndTypeName (ec, left_original, member_lookup, loc)) {
                                                        error176 (loc, fe.FieldInfo.Name);
                                                        return null;
                                                }                                       
@@ -6859,6 +6996,7 @@ namespace Mono.CSharp {
                                                // accessors and private field etc so there's no need
                                                // to transform ourselves.
                                                //
+                                               ee.InstanceExpression = left;
                                                return ee;
                                        }
 
@@ -6871,38 +7009,40 @@ namespace Mono.CSharp {
 
                                        if (!left_is_explicit)
                                                left = null;
-                                       
+
+                                       ee.InstanceExpression = left;
+
                                        return ResolveMemberAccess (ec, ml, left, loc, left_original);
                                }
                        }
 
                        if (member_lookup is IMemberExpr) {
                                IMemberExpr me = (IMemberExpr) member_lookup;
+                               MethodGroupExpr mg = me as MethodGroupExpr;
 
                                if (left_is_type){
-                                       MethodGroupExpr mg = me as MethodGroupExpr;
                                        if ((mg != null) && left_is_explicit && left.Type.IsInterface)
                                                mg.IsExplicitImpl = left_is_explicit;
 
                                        if (!me.IsStatic){
                                                if ((ec.IsFieldInitializer || ec.IsStatic) &&
-                                                   IdenticalNameAndTypeName (ec, left_original, loc))
+                                                   IdenticalNameAndTypeName (ec, left_original, member_lookup, loc))
                                                        return member_lookup;
-
+                                               
                                                SimpleName.Error_ObjectRefRequired (ec, loc, me.Name);
                                                return null;
                                        }
 
                                } else {
-                                       if (!me.IsInstance){
-                                               if (IdenticalNameAndTypeName (ec, left_original, loc))
+                                       if (!me.IsInstance) {
+                                               if (IdenticalNameAndTypeName (ec, left_original, left, loc))
                                                        return member_lookup;
 
                                                if (left_is_explicit) {
                                                        error176 (loc, me.Name);
                                                        return null;
                                                }
-                                       }
+                                       }                       
 
                                        //
                                        // Since we can not check for instance objects in SimpleName,
@@ -6925,6 +7065,9 @@ namespace Mono.CSharp {
                                                }
                                        }
 
+                                       if ((mg != null) && IdenticalNameAndTypeName (ec, left_original, left, loc))
+                                               mg.IdenticalTypeName = true;
+
                                        me.InstanceExpression = left;
                                }
 
@@ -6933,7 +7076,7 @@ namespace Mono.CSharp {
 
                        Console.WriteLine ("Left is: " + left);
                        Report.Error (-100, loc, "Support for [" + member_lookup + "] is not present yet");
-                       Environment.Exit (0);
+                       Environment.Exit (1);
                        return null;
                }
                
@@ -6950,7 +7093,7 @@ namespace Mono.CSharp {
                        //
 
                        Expression original = expr;
-                       expr = expr.Resolve (ec, flags | ResolveFlags.DisableFlowAnalysis);
+                       expr = expr.Resolve (ec, flags | ResolveFlags.Intermediate | ResolveFlags.DisableFlowAnalysis);
                        if (expr == null)
                                return null;
 
@@ -6974,8 +7117,7 @@ namespace Mono.CSharp {
                        Type expr_type = expr.Type;
                        if (expr is TypeExpr){
                                if (!ec.DeclSpace.CheckAccessLevel (expr_type)){
-                                       Error (122, "`" + expr_type + "' " +
-                                              "is inaccessible because of its protection level");
+                                       Report.Error (122, loc, "'{0}' is inaccessible due to its protection level", expr_type);
                                        return null;
                                }
 
@@ -6986,9 +7128,28 @@ namespace Mono.CSharp {
                                                object value = en.LookupEnumValue (ec, Identifier, loc);
                                                
                                                if (value != null){
+                                                       MemberCore mc = en.GetDefinition (Identifier);
+                                                       ObsoleteAttribute oa = mc.GetObsoleteAttribute (en);
+                                                       if (oa != null) {
+                                                               AttributeTester.Report_ObsoleteMessage (oa, mc.GetSignatureForError (), Location);
+                                                       }
+                                                       oa = en.GetObsoleteAttribute (en);
+                                                       if (oa != null) {
+                                                               AttributeTester.Report_ObsoleteMessage (oa, en.GetSignatureForError (), Location);
+                                                       }
+
                                                        Constant c = Constantify (value, en.UnderlyingType);
                                                        return new EnumConstant (c, expr_type);
                                                }
+                                       } else {
+                                               CheckObsoleteAttribute (expr_type);
+
+                                               FieldInfo fi = expr_type.GetField (Identifier);
+                                               if (fi != null) {
+                                                       ObsoleteAttribute oa = AttributeTester.GetMemberObsoleteAttribute (fi);
+                                                       if (oa != null)
+                                                               AttributeTester.Report_ObsoleteMessage (oa, TypeManager.GetFullNameSignature (fi), Location);
+                                               }
                                        }
                                }
                        }
@@ -7247,7 +7408,7 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               Expression MakePointerAccess ()
+               Expression MakePointerAccess (EmitContext ec)
                {
                        Type t = Expr.Type;
 
@@ -7261,8 +7422,10 @@ namespace Mono.CSharp {
                        }
                        Expression p;
 
-                       p = new PointerArithmetic (true, Expr, ((Argument)Arguments [0]).Expr, t, loc);
-                       return new Indirection (p, loc);
+                       p = new PointerArithmetic (true, Expr, ((Argument)Arguments [0]).Expr, t, loc).Resolve (ec);
+                       if (p == null)
+                               return null;
+                       return new Indirection (p, loc).Resolve (ec);
                }
                
                public override Expression DoResolve (EmitContext ec)
@@ -7286,7 +7449,7 @@ namespace Mono.CSharp {
                        if (t.IsArray)
                                return (new ArrayAccess (this, loc)).Resolve (ec);
                        else if (t.IsPointer)
-                               return MakePointerAccess ();
+                               return MakePointerAccess (ec);
                        else
                                return (new IndexerAccess (this, loc)).Resolve (ec);
                }
@@ -7300,7 +7463,7 @@ namespace Mono.CSharp {
                        if (t.IsArray)
                                return (new ArrayAccess (this, loc)).ResolveLValue (ec, right_side);
                        else if (t.IsPointer)
-                               return MakePointerAccess ();
+                               return MakePointerAccess (ec);
                        else
                                return (new IndexerAccess (this, loc)).ResolveLValue (ec, right_side);
                }
@@ -7320,7 +7483,8 @@ namespace Mono.CSharp {
                //
                ElementAccess ea;
 
-               LocalTemporary [] cached_locations;
+               LocalTemporary temp;
+               bool prepared;
                
                public ArrayAccess (ElementAccess ea_data, Location l)
                {
@@ -7419,20 +7583,6 @@ namespace Mono.CSharp {
                                ig.Emit (OpCodes.Ldelem_Ref);
                }
 
-               /// <summary>
-               ///    Emits the right opcode to store an object of Type `t'
-               ///    from an array of T.  
-               /// </summary>
-               static public void EmitStoreOpcode (ILGenerator ig, Type t)
-               {
-                       bool is_stobj;
-                       OpCode op = GetStoreOpcode (t, out is_stobj);
-                       if (is_stobj)
-                               ig.Emit (OpCodes.Stobj, t);
-                       else
-                               ig.Emit (op);
-               }
-
                /// <summary>
                ///    Returns the right opcode to store an object of Type `t'
                ///    from an array of T.  
@@ -7442,7 +7592,7 @@ namespace Mono.CSharp {
                        //Console.WriteLine (new System.Diagnostics.StackTrace ());
                        is_stobj = false;
                        t = TypeManager.TypeToCoreType (t);
-                       if (TypeManager.IsEnumType (t) && t != TypeManager.enum_type)
+                       if (TypeManager.IsEnumType (t))
                                t = TypeManager.EnumToUnderlying (t);
                        if (t == TypeManager.byte_type || t == TypeManager.sbyte_type ||
                            t == TypeManager.bool_type)
@@ -7524,101 +7674,111 @@ namespace Mono.CSharp {
                {
                        ILGenerator ig = ec.ig;
                        
-                       if (cached_locations == null){
-                               ea.Expr.Emit (ec);
-                               foreach (Argument a in ea.Arguments){
-                                       Type argtype = a.Expr.Type;
-                                       
-                                       a.Expr.Emit (ec);
-                                       
-                                       if (argtype == TypeManager.int64_type)
-                                               ig.Emit (OpCodes.Conv_Ovf_I);
-                                       else if (argtype == TypeManager.uint64_type)
-                                               ig.Emit (OpCodes.Conv_Ovf_I_Un);
-                               }
-                               return;
-                       }
-
-                       if (cached_locations [0] == null){
-                               cached_locations [0] = new LocalTemporary (ec, ea.Expr.Type);
-                               ea.Expr.Emit (ec);
-                               ig.Emit (OpCodes.Dup);
-                               cached_locations [0].Store (ec);
+                       ea.Expr.Emit (ec);
+                       foreach (Argument a in ea.Arguments){
+                               Type argtype = a.Expr.Type;
                                
-                               int j = 1;
+                               a.Expr.Emit (ec);
                                
-                               foreach (Argument a in ea.Arguments){
-                                       Type argtype = a.Expr.Type;
-                                       
-                                       cached_locations [j] = new LocalTemporary (ec, TypeManager.intptr_type /* a.Expr.Type */);
-                                       a.Expr.Emit (ec);
-                                       if (argtype == TypeManager.int64_type)
-                                               ig.Emit (OpCodes.Conv_Ovf_I);
-                                       else if (argtype == TypeManager.uint64_type)
-                                               ig.Emit (OpCodes.Conv_Ovf_I_Un);
-
-                                       ig.Emit (OpCodes.Dup);
-                                       cached_locations [j].Store (ec);
-                                       j++;
-                               }
-                               return;
+                               if (argtype == TypeManager.int64_type)
+                                       ig.Emit (OpCodes.Conv_Ovf_I);
+                               else if (argtype == TypeManager.uint64_type)
+                                       ig.Emit (OpCodes.Conv_Ovf_I_Un);
                        }
-
-                       foreach (LocalTemporary lt in cached_locations)
-                               lt.Emit (ec);
                }
 
-               public new void CacheTemporaries (EmitContext ec)
-               {
-                       cached_locations = new LocalTemporary [ea.Arguments.Count + 1];
-               }
-               
-               public override void Emit (EmitContext ec)
+               public void Emit (EmitContext ec, bool leave_copy)
                {
                        int rank = ea.Expr.Type.GetArrayRank ();
                        ILGenerator ig = ec.ig;
 
-                       LoadArrayAndArguments (ec);
-                       
-                       if (rank == 1)
-                               EmitLoadOpcode (ig, type);
-                       else {
-                               MethodInfo method;
+                       if (!prepared) {
+                               LoadArrayAndArguments (ec);
                                
-                               method = FetchGetMethod ();
-                               ig.Emit (OpCodes.Call, method);
+                               if (rank == 1)
+                                       EmitLoadOpcode (ig, type);
+                               else {
+                                       MethodInfo method;
+                                       
+                                       method = FetchGetMethod ();
+                                       ig.Emit (OpCodes.Call, method);
+                               }
+                       } else
+                               LoadFromPtr (ec.ig, this.type);
+                       
+                       if (leave_copy) {
+                               ec.ig.Emit (OpCodes.Dup);
+                               temp = new LocalTemporary (ec, this.type);
+                               temp.Store (ec);
                        }
                }
+               
+               public override void Emit (EmitContext ec)
+               {
+                       Emit (ec, false);
+               }
 
-               public void EmitAssign (EmitContext ec, Expression source)
+               public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
                {
                        int rank = ea.Expr.Type.GetArrayRank ();
                        ILGenerator ig = ec.ig;
                        Type t = source.Type;
+                       prepared = prepare_for_load;
 
-                       LoadArrayAndArguments (ec);
-
-                       //
-                       // The stobj opcode used by value types will need
-                       // an address on the stack, not really an array/array
-                       // pair
-                       //
-                       if (rank == 1){
-                               if (t == TypeManager.enum_type || t == TypeManager.decimal_type ||
-                                   (t.IsSubclassOf (TypeManager.value_type) && !TypeManager.IsEnumType (t) && !TypeManager.IsBuiltinType (t)))
-                                       ig.Emit (OpCodes.Ldelema, t);
+                       if (prepare_for_load) {
+                               AddressOf (ec, AddressOp.LoadStore);
+                               ec.ig.Emit (OpCodes.Dup);
+                               source.Emit (ec);
+                               if (leave_copy) {
+                                       ec.ig.Emit (OpCodes.Dup);
+                                       temp = new LocalTemporary (ec, this.type);
+                                       temp.Store (ec);
+                               }
+                               StoreFromPtr (ec.ig, t);
+                               
+                               if (temp != null)
+                                       temp.Emit (ec);
+                               
+                               return;
                        }
                        
-                       source.Emit (ec);
+                       LoadArrayAndArguments (ec);
 
-                       if (rank == 1)
-                               EmitStoreOpcode (ig, t);
-                       else {
+                       if (rank == 1) {
+                               bool is_stobj;
+                               OpCode op = GetStoreOpcode (t, out is_stobj);
+                               //
+                               // The stobj opcode used by value types will need
+                               // an address on the stack, not really an array/array
+                               // pair
+                               //
+                               if (is_stobj)
+                                       ig.Emit (OpCodes.Ldelema, t);
+                               
+                               source.Emit (ec);
+                               if (leave_copy) {
+                                       ec.ig.Emit (OpCodes.Dup);
+                                       temp = new LocalTemporary (ec, this.type);
+                                       temp.Store (ec);
+                               }
+                               
+                               if (is_stobj)
+                                       ig.Emit (OpCodes.Stobj, t);
+                               else
+                                       ig.Emit (op);
+                       } else {
                                ModuleBuilder mb = CodeGen.Module.Builder;
                                int arg_count = ea.Arguments.Count;
                                Type [] args = new Type [arg_count + 1];
                                MethodInfo set;
                                
+                               source.Emit (ec);
+                               if (leave_copy) {
+                                       ec.ig.Emit (OpCodes.Dup);
+                                       temp = new LocalTemporary (ec, this.type);
+                                       temp.Store (ec);
+                               }
+                               
                                for (int i = 0; i < arg_count; i++){
                                        //args [i++] = a.Type;
                                        args [i] = TypeManager.int32_type;
@@ -7634,6 +7794,9 @@ namespace Mono.CSharp {
                                
                                ig.Emit (OpCodes.Call, set);
                        }
+                       
+                       if (temp != null)
+                               temp.Emit (ec);
                }
 
                public void AddressOf (EmitContext ec, AddressOp mode)
@@ -7697,7 +7860,7 @@ namespace Mono.CSharp {
                        MemberInfo [] mi = TypeManager.MemberLookup (
                                caller_type, caller_type, lookup_type, MemberTypes.Property,
                                BindingFlags.Public | BindingFlags.Instance |
-                               BindingFlags.DeclaredOnly, p_name);
+                               BindingFlags.DeclaredOnly, p_name, null);
 
                        if (mi == null || mi.Length == 0)
                                return null;
@@ -7816,7 +7979,8 @@ namespace Mono.CSharp {
                        if (AllGetters.Count > 0) {
                                found_any_getters = true;
                                get = (MethodInfo) Invocation.OverloadResolve (
-                                       ec, new MethodGroupExpr (AllGetters, loc), arguments, loc);
+                                       ec, new MethodGroupExpr (AllGetters, loc),
+                                       arguments, false, loc);
                        }
 
                        if (!found_any) {
@@ -7880,7 +8044,7 @@ namespace Mono.CSharp {
                                set_arguments.Add (new Argument (right_side, Argument.AType.Expression));
                                set = (MethodInfo) Invocation.OverloadResolve (
                                        ec, new MethodGroupExpr (AllSetters, loc),
-                                       set_arguments, loc);
+                                       set_arguments, false, loc);
                        }
 
                        if (!found_any) {
@@ -7925,19 +8089,53 @@ namespace Mono.CSharp {
                        return this;
                }
                
-               public override void Emit (EmitContext ec)
+               bool prepared = false;
+               LocalTemporary temp;
+               
+               public void Emit (EmitContext ec, bool leave_copy)
                {
-                       Invocation.EmitCall (ec, is_base_indexer, false, instance_expr, get, arguments, loc);
+                       Invocation.EmitCall (ec, is_base_indexer, false, instance_expr, get, arguments, loc, prepared, false);
+                       if (leave_copy) {
+                               ec.ig.Emit (OpCodes.Dup);
+                               temp = new LocalTemporary (ec, Type);
+                               temp.Store (ec);
+                       }
                }
-
+               
                //
                // source is ignored, because we already have a copy of it from the
                // LValue resolution and we have already constructed a pre-cached
                // version of the arguments (ea.set_arguments);
                //
-               public void EmitAssign (EmitContext ec, Expression source)
+               public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
                {
-                       Invocation.EmitCall (ec, is_base_indexer, false, instance_expr, set, set_arguments, loc);
+                       prepared = prepare_for_load;
+                       Argument a = (Argument) set_arguments [set_arguments.Count - 1];
+                       
+                       if (prepared) {
+                               source.Emit (ec);
+                               if (leave_copy) {
+                                       ec.ig.Emit (OpCodes.Dup);
+                                       temp = new LocalTemporary (ec, Type);
+                                       temp.Store (ec);
+                               }
+                       } else if (leave_copy) {
+                               temp = new LocalTemporary (ec, Type);
+                               source.Emit (ec);
+                               temp.Store (ec);
+                               a.Expr = temp;
+                       }
+                       
+                       Invocation.EmitCall (ec, is_base_indexer, false, instance_expr, set, set_arguments, loc, false, prepared);
+                       
+                       if (temp != null)
+                               temp.Emit (ec);
+               }
+               
+               
+               public override void Emit (EmitContext ec)
+               {
+                       Emit (ec, false);
                }
        }
 
@@ -8022,6 +8220,9 @@ namespace Mono.CSharp {
 
                                pe.IsBase = true;
                        }
+                       
+                       if (e is MethodGroupExpr)
+                               ((MethodGroupExpr) e).IsBase = true;
 
                        return e;
                }
@@ -8308,6 +8509,13 @@ namespace Mono.CSharp {
                                        return null;
                        }
 
+                       Constant c = count as Constant;
+                       // TODO: because we don't have property IsNegative
+                       if (c != null && c.ConvertToUInt () == null) {
+                               Report.Error (247, loc, "Cannot use a negative size with stackalloc");
+                               return null;
+                       }
+
                        if (ec.CurrentBranching.InCatch () ||
                            ec.CurrentBranching.InFinally (true)) {
                                Error (255,