2003-05-27 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / mcs / expression.cs
index b922903ad40304873402e746416bef1ce4f07a92..f384ab46612781e8d1492713b5025535b9ef7f43 100755 (executable)
@@ -25,7 +25,7 @@ namespace Mono.CSharp {
                ArrayList args;
                MethodInfo mi;
 
-               StaticCallExpr (MethodInfo m, ArrayList a, Location l)
+               public StaticCallExpr (MethodInfo m, ArrayList a, Location l)
                {
                        mi = m;
                        args = a;
@@ -59,7 +59,13 @@ namespace Mono.CSharp {
                        MethodBase method;
                        
                        args = new ArrayList (1);
-                       args.Add (new Argument (e, Argument.AType.Expression));
+                       Argument a = new Argument (e, Argument.AType.Expression);
+
+                        // We need to resolve the arguments before sending them in !
+                        if (!a.Resolve (ec, loc))
+                                return null;
+
+                        args.Add (a);
                        method = Invocation.OverloadResolve (ec, (MethodGroupExpr) mg, args, loc);
 
                        if (method == null)
@@ -124,7 +130,7 @@ namespace Mono.CSharp {
                        return oper.ToString ();
                }
 
-               static string [] oper_names;
+               public static readonly string [] oper_names;
 
                static Unary ()
                {
@@ -155,7 +161,7 @@ namespace Mono.CSharp {
                static Expression TryReduceNegative (Constant expr)
                {
                        Expression e = null;
-                       
+
                        if (expr is IntConstant)
                                e = new IntConstant (-((IntConstant) expr).Value);
                        else if (expr is UIntConstant){
@@ -164,7 +170,7 @@ namespace Mono.CSharp {
                                if (value < 2147483649)
                                        return new IntConstant (-(int)value);
                                else
-                                       e = new LongConstant (value);
+                                       e = new LongConstant (-value);
                        }
                        else if (expr is LongConstant)
                                e = new LongConstant (-((LongConstant) expr).Value);
@@ -223,9 +229,30 @@ namespace Mono.CSharp {
                                      (expr_type == TypeManager.int64_type) ||
                                      (expr_type == TypeManager.uint64_type) ||
                                      (expr_type.IsSubclassOf (TypeManager.enum_type)))){
+
                                        result = null;
-                                       Error23 (expr_type);
-                                       return false;
+                                       if (Convert.ImplicitConversionExists (ec, e, TypeManager.int32_type)){
+                                               result = new Cast (new TypeExpr (TypeManager.int32_type, loc), e, loc);
+                                               result = result.Resolve (ec);
+                                       } else if (Convert.ImplicitConversionExists (ec, e, TypeManager.uint32_type)){
+                                               result = new Cast (new TypeExpr (TypeManager.uint32_type, loc), e, loc);
+                                               result = result.Resolve (ec);
+                                       } else if (Convert.ImplicitConversionExists (ec, e, TypeManager.int64_type)){
+                                               result = new Cast (new TypeExpr (TypeManager.int64_type, loc), e, loc);
+                                               result = result.Resolve (ec);
+                                       } else if (Convert.ImplicitConversionExists (ec, e, TypeManager.uint64_type)){
+                                               result = new Cast (new TypeExpr (TypeManager.uint64_type, loc), e, loc);
+                                               result = result.Resolve (ec);
+                                       }
+
+                                       if (result == null || !(result is Constant)){
+                                               result = null;
+                                               Error23 (expr_type);
+                                               return false;
+                                       }
+
+                                       expr_type = result.Type;
+                                       e = (Constant) result;
                                }
 
                                if (e is EnumConstant){
@@ -314,8 +341,11 @@ namespace Mono.CSharp {
                        switch (Oper){
                        case Operator.LogicalNot:
                                if (expr_type != TypeManager.bool_type) {
-                                       Error23 (Expr.Type);
-                                       return null;
+                                       Expr = ResolveBoolean (ec, Expr, loc);
+                                       if (Expr == null){
+                                               Error23 (expr_type);
+                                               return null;
+                                       }
                                }
                                
                                type = TypeManager.bool_type;
@@ -329,22 +359,22 @@ namespace Mono.CSharp {
                                      (expr_type.IsSubclassOf (TypeManager.enum_type)))){
                                        Expression e;
 
-                                       e = ConvertImplicit (ec, Expr, TypeManager.int32_type, loc);
+                                       e = Convert.ImplicitConversion (ec, Expr, TypeManager.int32_type, loc);
                                        if (e != null){
                                                type = TypeManager.int32_type;
                                                return this;
                                        }
-                                       e = ConvertImplicit (ec, Expr, TypeManager.uint32_type, loc);
+                                       e = Convert.ImplicitConversion (ec, Expr, TypeManager.uint32_type, loc);
                                        if (e != null){
                                                type = TypeManager.uint32_type;
                                                return this;
                                        }
-                                       e = ConvertImplicit (ec, Expr, TypeManager.int64_type, loc);
+                                       e = Convert.ImplicitConversion (ec, Expr, TypeManager.int64_type, loc);
                                        if (e != null){
                                                type = TypeManager.int64_type;
                                                return this;
                                        }
-                                       e = ConvertImplicit (ec, Expr, TypeManager.uint64_type, loc);
+                                       e = Convert.ImplicitConversion (ec, Expr, TypeManager.uint64_type, loc);
                                        if (e != null){
                                                type = TypeManager.uint64_type;
                                                return this;
@@ -369,10 +399,8 @@ namespace Mono.CSharp {
                                if (!TypeManager.VerifyUnManaged (Expr.Type, loc)){
                                        return null;
                                }
-                               
-                               string ptr_type_name = Expr.Type.FullName + "*";
-                               type = TypeManager.LookupType (ptr_type_name);
-                               
+
+                               type = TypeManager.GetPointerType (Expr.Type);
                                return this;
 
                        case Operator.Indirection:
@@ -382,9 +410,7 @@ namespace Mono.CSharp {
                                }
                                
                                if (!expr_type.IsPointer){
-                                       Error (
-                                               193,
-                                               "The * or -> operator can only be applied to pointers");
+                                       Error (193, "The * or -> operator can only be applied to pointers");
                                        return null;
                                }
                                
@@ -427,7 +453,7 @@ namespace Mono.CSharp {
                                //
                                //
                                // The following is inneficient, because we call
-                               // ConvertImplicit too many times.
+                               // ImplicitConversion too many times.
                                //
                                // It is also not clear if we should convert to Float
                                // or Double initially.
@@ -439,7 +465,7 @@ namespace Mono.CSharp {
                                        // bt wrote as a decimal interger literal
                                        //
                                        type = TypeManager.int64_type;
-                                       Expr = ConvertImplicit (ec, Expr, type, loc);
+                                       Expr = Convert.ImplicitConversion (ec, Expr, type, loc);
                                        return this;
                                }
 
@@ -458,21 +484,21 @@ namespace Mono.CSharp {
                                        return this;
                                }
                                
-                               expr = ConvertImplicit (ec, Expr, TypeManager.int32_type, loc);
+                               expr = Convert.ImplicitConversion (ec, Expr, TypeManager.int32_type, loc);
                                if (expr != null){
                                        Expr = expr;
                                        type = expr.Type;
                                        return this;
                                } 
 
-                               expr = ConvertImplicit (ec, Expr, TypeManager.int64_type, loc);
+                               expr = Convert.ImplicitConversion (ec, Expr, TypeManager.int64_type, loc);
                                if (expr != null){
                                        Expr = expr;
                                        type = expr.Type;
                                        return this;
                                }
 
-                               expr = ConvertImplicit (ec, Expr, TypeManager.double_type, loc);
+                               expr = Convert.ImplicitConversion (ec, Expr, TypeManager.double_type, loc);
                                if (expr != null){
                                        Expr = expr;
                                        type = expr.Type;
@@ -600,15 +626,15 @@ namespace Mono.CSharp {
                public void EmitAssign (EmitContext ec, Expression source)
                {
                        if (temporary != null){
-                               if (have_temporary){
+                               if (have_temporary)
                                        temporary.Emit (ec);
-                                       return;
+                               else {
+                                       expr.Emit (ec);
+                                       ec.ig.Emit (OpCodes.Dup);
+                                       temporary.Store (ec);
+                                       have_temporary = true;
                                }
-                               expr.Emit (ec);
-                               ec.ig.Emit (OpCodes.Dup);
-                               temporary.Store (ec);
-                               have_temporary = true;
-                       } else
+                       } else 
                                expr.Emit (ec);
 
                        source.Emit (ec);
@@ -642,6 +668,11 @@ namespace Mono.CSharp {
                {
                        temporary = new LocalTemporary (ec, type);
                }
+
+               public override string ToString ()
+               {
+                       return "*(" + expr + ")";
+               }
        }
        
        /// <summary>
@@ -783,7 +814,7 @@ namespace Mono.CSharp {
 
                                return null;
                        } else {
-                               expr.Error118 ("variable, indexer or property access");
+                               expr.Error_UnexpectedKind ("variable, indexer or property access");
                                return null;
                        }
 
@@ -814,8 +845,12 @@ namespace Mono.CSharp {
                //
                void LoadOneAndEmitOp (EmitContext ec, Type t)
                {
+                       //
+                       // Measure if getting the typecode and using that is more/less efficient
+                       // that comparing types.  t.GetTypeCode() is an internal call.
+                       //
                        ILGenerator ig = ec.ig;
-                       
+                                                    
                        if (t == TypeManager.uint64_type || t == TypeManager.int64_type)
                                LongConstant.EmitLong (ig, 1);
                        else if (t == TypeManager.double_type)
@@ -860,8 +895,33 @@ namespace Mono.CSharp {
                                else
                                        ig.Emit (OpCodes.Add);
                        }
+
+                       if (t == TypeManager.sbyte_type){
+                               if (ec.CheckState)
+                                       ig.Emit (OpCodes.Conv_Ovf_I1);
+                               else
+                                       ig.Emit (OpCodes.Conv_I1);
+                       } else if (t == TypeManager.byte_type){
+                               if (ec.CheckState)
+                                       ig.Emit (OpCodes.Conv_Ovf_U1);
+                               else
+                                       ig.Emit (OpCodes.Conv_U1);
+                       } else if (t == TypeManager.short_type){
+                               if (ec.CheckState)
+                                       ig.Emit (OpCodes.Conv_Ovf_I2);
+                               else
+                                       ig.Emit (OpCodes.Conv_I2);
+                       } else if (t == TypeManager.ushort_type || t == TypeManager.char_type){
+                               if (ec.CheckState)
+                                       ig.Emit (OpCodes.Conv_Ovf_U2);
+                               else
+                                       ig.Emit (OpCodes.Conv_U2);
+                       }
+                       
                }
 
+               static EmptyExpression empty_expr;
+               
                void EmitCode (EmitContext ec, bool is_expr)
                {
                        ILGenerator ig = ec.ig;
@@ -870,8 +930,45 @@ namespace Mono.CSharp {
 
                        ia.CacheTemporaries (ec);
 
-                       if (temp_storage == null)
+                       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 && ec.RemapToProxy == false) ||
+                                   (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);
+                       }
                        
                        switch (mode){
                        case Mode.PreIncrement:
@@ -995,6 +1092,10 @@ namespace Mono.CSharp {
                                return;
                        case Action.LeaveOnStack:
                                // the `e != null' rule.
+                               ig.Emit (OpCodes.Ldnull);
+                               ig.Emit (OpCodes.Ceq);
+                               ig.Emit (OpCodes.Ldc_I4_0);
+                               ig.Emit (OpCodes.Ceq);
                                return;
                        case Action.Probe:
                                ig.Emit (OpCodes.Isinst, probe_type);
@@ -1023,7 +1124,7 @@ namespace Mono.CSharp {
                        // First case, if at compile time, there is an implicit conversion
                        // then e != null (objects) or true (value types)
                        //
-                       e = ConvertImplicitStandard (ec, expr, probe_type, loc);
+                       e = Convert.ImplicitConversionStandard (ec, expr, probe_type, loc);
                        if (e != null){
                                expr = e;
                                if (etype.IsValueType)
@@ -1032,7 +1133,7 @@ namespace Mono.CSharp {
                                        action = Action.LeaveOnStack;
 
                                warning_always_matches = true;
-                       } else if (ExplicitReferenceConversionExists (etype, probe_type)){
+                       } else if (Convert.ExplicitReferenceConversionExists (etype, probe_type)){
                                //
                                // Second case: explicit reference convresion
                                //
@@ -1047,16 +1148,13 @@ namespace Mono.CSharp {
                        
                        if (RootContext.WarningLevel >= 1){
                                if (warning_always_matches)
-                                       Warning (
-                                               183,
-                                               "The expression is always of type `" +
-                                               TypeManager.CSharpName (probe_type) + "'");
+                                       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) + "'");
+                                               Warning (184,
+                                                        "The expression is never of type `" +
+                                                        TypeManager.CSharpName (probe_type) + "'");
                                }
                        }
 
@@ -1111,14 +1209,14 @@ namespace Mono.CSharp {
                        
                        }
                        
-                       e = ConvertImplicit (ec, expr, probe_type, loc);
+                       e = Convert.ImplicitConversion (ec, expr, probe_type, loc);
                        if (e != null){
                                expr = e;
                                do_isinst = false;
                                return this;
                        }
 
-                       if (ExplicitReferenceConversionExists (etype, probe_type)){
+                       if (Convert.ExplicitReferenceConversionExists (etype, probe_type)){
                                do_isinst = true;
                                return this;
                        }
@@ -1160,16 +1258,68 @@ namespace Mono.CSharp {
                        }
                }
 
+               bool CheckRange (EmitContext ec, long value, Type type, long min, long max)
+               {
+                       if (!ec.ConstantCheckState)
+                               return true;
+
+                       if ((value < min) || (value > max)) {
+                               Error (221, "Constant value `" + value + "' cannot be converted " +
+                                      "to a `" + TypeManager.CSharpName (type) + "' (use `unchecked' " +
+                                      "syntax to override)");
+                               return false;
+                       }
+
+                       return true;
+               }
+
+               bool CheckRange (EmitContext ec, ulong value, Type type, ulong max)
+               {
+                       if (!ec.ConstantCheckState)
+                               return true;
+
+                       if (value > max) {
+                               Error (221, "Constant value `" + value + "' cannot be converted " +
+                                      "to a `" + TypeManager.CSharpName (type) + "' (use `unchecked' " +
+                                      "syntax to override)");
+                               return false;
+                       }
+
+                       return true;
+               }
+
+               bool CheckUnsigned (EmitContext ec, long value, Type type)
+               {
+                       if (!ec.ConstantCheckState)
+                               return true;
+
+                       if (value < 0) {
+                               Error (221, "Constant value `" + value + "' cannot be converted " +
+                                      "to a `" + TypeManager.CSharpName (type) + "' (use `unchecked' " +
+                                      "syntax to override)");
+                               return false;
+                       }
+
+                       return true;
+               }
+
                /// <summary>
                ///   Attempts to do a compile-time folding of a constant cast.
                /// </summary>
                Expression TryReduce (EmitContext ec, Type target_type)
                {
-                       if (expr is ByteConstant){
-                               byte v = ((ByteConstant) expr).Value;
+                       Expression real_expr = expr;
+                       if (real_expr is EnumConstant)
+                               real_expr = ((EnumConstant) real_expr).Child;
+                               
+                       if (real_expr is ByteConstant){
+                               byte v = ((ByteConstant) real_expr).Value;
        
-                               if (target_type == TypeManager.sbyte_type)
+                               if (target_type == TypeManager.sbyte_type) {
+                                       if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
+                                               return null;
                                        return new SByteConstant ((sbyte) v);
+                               }
                                if (target_type == TypeManager.short_type)
                                        return new ShortConstant ((short) v);
                                if (target_type == TypeManager.ushort_type)
@@ -1191,67 +1341,107 @@ namespace Mono.CSharp {
                                if (target_type == TypeManager.decimal_type)
                                        return new DecimalConstant ((decimal) v);
                        }
-                       if (expr is SByteConstant){
-                               sbyte v = ((SByteConstant) expr).Value;
+                       if (real_expr is SByteConstant){
+                               sbyte v = ((SByteConstant) real_expr).Value;
        
-                               if (target_type == TypeManager.byte_type)
+                               if (target_type == TypeManager.byte_type) {
+                                       if (!CheckUnsigned (ec, v, target_type))
+                                               return null;
                                        return new ByteConstant ((byte) v);
+                               }
                                if (target_type == TypeManager.short_type)
                                        return new ShortConstant ((short) v);
-                               if (target_type == TypeManager.ushort_type)
+                               if (target_type == TypeManager.ushort_type) {
+                                       if (!CheckUnsigned (ec, v, target_type))
+                                               return null;
                                        return new UShortConstant ((ushort) v);
-                               if (target_type == TypeManager.int32_type)
+                               if (target_type == TypeManager.int32_type)
                                        return new IntConstant ((int) v);
-                               if (target_type == TypeManager.uint32_type)
+                               if (target_type == TypeManager.uint32_type) {
+                                       if (!CheckUnsigned (ec, v, target_type))
+                                               return null;
                                        return new UIntConstant ((uint) v);
-                               if (target_type == TypeManager.int64_type)
+                               if (target_type == TypeManager.int64_type)
                                        return new LongConstant ((long) v);
-                               if (target_type == TypeManager.uint64_type)
+                               if (target_type == TypeManager.uint64_type) {
+                                       if (!CheckUnsigned (ec, v, target_type))
+                                               return null;
                                        return new ULongConstant ((ulong) v);
+                               }
                                if (target_type == TypeManager.float_type)
                                        return new FloatConstant ((float) v);
                                if (target_type == TypeManager.double_type)
                                        return new DoubleConstant ((double) v);
-                               if (target_type == TypeManager.char_type)
+                               if (target_type == TypeManager.char_type) {
+                                       if (!CheckUnsigned (ec, v, target_type))
+                                               return null;
                                        return new CharConstant ((char) v);
+                               }
                                if (target_type == TypeManager.decimal_type)
                                        return new DecimalConstant ((decimal) v);
                        }
-                       if (expr is ShortConstant){
-                               short v = ((ShortConstant) expr).Value;
+                       if (real_expr is ShortConstant){
+                               short v = ((ShortConstant) real_expr).Value;
        
-                               if (target_type == TypeManager.byte_type)
+                               if (target_type == TypeManager.byte_type) {
+                                       if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
+                                               return null;
                                        return new ByteConstant ((byte) v);
-                               if (target_type == TypeManager.sbyte_type)
+                               }
+                               if (target_type == TypeManager.sbyte_type) {
+                                       if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
+                                               return null;
                                        return new SByteConstant ((sbyte) v);
-                               if (target_type == TypeManager.ushort_type)
+                               }
+                               if (target_type == TypeManager.ushort_type) {
+                                       if (!CheckUnsigned (ec, v, target_type))
+                                               return null;
                                        return new UShortConstant ((ushort) v);
+                               }
                                if (target_type == TypeManager.int32_type)
                                        return new IntConstant ((int) v);
-                               if (target_type == TypeManager.uint32_type)
+                               if (target_type == TypeManager.uint32_type) {
+                                       if (!CheckUnsigned (ec, v, target_type))
+                                               return null;
                                        return new UIntConstant ((uint) v);
+                               }
                                if (target_type == TypeManager.int64_type)
                                        return new LongConstant ((long) v);
-                               if (target_type == TypeManager.uint64_type)
+                               if (target_type == TypeManager.uint64_type) {
+                                       if (!CheckUnsigned (ec, v, target_type))
+                                               return null;
                                        return new ULongConstant ((ulong) v);
+                               }
                                if (target_type == TypeManager.float_type)
                                        return new FloatConstant ((float) v);
                                if (target_type == TypeManager.double_type)
                                        return new DoubleConstant ((double) v);
-                               if (target_type == TypeManager.char_type)
+                               if (target_type == TypeManager.char_type) {
+                                       if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
+                                               return null;
                                        return new CharConstant ((char) v);
+                               }
                                if (target_type == TypeManager.decimal_type)
                                        return new DecimalConstant ((decimal) v);
                        }
-                       if (expr is UShortConstant){
-                               ushort v = ((UShortConstant) expr).Value;
+                       if (real_expr is UShortConstant){
+                               ushort v = ((UShortConstant) real_expr).Value;
        
-                               if (target_type == TypeManager.byte_type)
+                               if (target_type == TypeManager.byte_type) {
+                                       if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
+                                               return null;
                                        return new ByteConstant ((byte) v);
-                               if (target_type == TypeManager.sbyte_type)
+                               }
+                               if (target_type == TypeManager.sbyte_type) {
+                                       if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
+                                               return null;
                                        return new SByteConstant ((sbyte) v);
-                               if (target_type == TypeManager.short_type)
+                               }
+                               if (target_type == TypeManager.short_type) {
+                                       if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
+                                               return null;
                                        return new ShortConstant ((short) v);
+                               }
                                if (target_type == TypeManager.int32_type)
                                        return new IntConstant ((int) v);
                                if (target_type == TypeManager.uint32_type)
@@ -1264,50 +1454,89 @@ namespace Mono.CSharp {
                                        return new FloatConstant ((float) v);
                                if (target_type == TypeManager.double_type)
                                        return new DoubleConstant ((double) v);
-                               if (target_type == TypeManager.char_type)
+                               if (target_type == TypeManager.char_type) {
+                                       if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
+                                               return null;
                                        return new CharConstant ((char) v);
+                               }
                                if (target_type == TypeManager.decimal_type)
                                        return new DecimalConstant ((decimal) v);
                        }
-                       if (expr is IntConstant){
-                               int v = ((IntConstant) expr).Value;
+                       if (real_expr is IntConstant){
+                               int v = ((IntConstant) real_expr).Value;
        
-                               if (target_type == TypeManager.byte_type)
+                               if (target_type == TypeManager.byte_type) {
+                                       if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
+                                               return null;
                                        return new ByteConstant ((byte) v);
-                               if (target_type == TypeManager.sbyte_type)
+                               }
+                               if (target_type == TypeManager.sbyte_type) {
+                                       if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
+                                               return null;
                                        return new SByteConstant ((sbyte) v);
-                               if (target_type == TypeManager.short_type)
+                               }
+                               if (target_type == TypeManager.short_type) {
+                                       if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
+                                               return null;
                                        return new ShortConstant ((short) v);
-                               if (target_type == TypeManager.ushort_type)
+                               }
+                               if (target_type == TypeManager.ushort_type) {
+                                       if (!CheckRange (ec, v, target_type, UInt16.MinValue, UInt16.MaxValue))
+                                               return null;
                                        return new UShortConstant ((ushort) v);
-                               if (target_type == TypeManager.uint32_type)
+                               }
+                               if (target_type == TypeManager.uint32_type) {
+                                       if (!CheckRange (ec, v, target_type, Int32.MinValue, Int32.MaxValue))
+                                               return null;
                                        return new UIntConstant ((uint) v);
+                               }
                                if (target_type == TypeManager.int64_type)
                                        return new LongConstant ((long) v);
-                               if (target_type == TypeManager.uint64_type)
+                               if (target_type == TypeManager.uint64_type) {
+                                       if (!CheckUnsigned (ec, v, target_type))
+                                               return null;
                                        return new ULongConstant ((ulong) v);
+                               }
                                if (target_type == TypeManager.float_type)
                                        return new FloatConstant ((float) v);
                                if (target_type == TypeManager.double_type)
                                        return new DoubleConstant ((double) v);
-                               if (target_type == TypeManager.char_type)
+                               if (target_type == TypeManager.char_type) {
+                                       if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
+                                               return null;
                                        return new CharConstant ((char) v);
+                               }
                                if (target_type == TypeManager.decimal_type)
                                        return new DecimalConstant ((decimal) v);
                        }
-                       if (expr is UIntConstant){
-                               uint v = ((UIntConstant) expr).Value;
+                       if (real_expr is UIntConstant){
+                               uint v = ((UIntConstant) real_expr).Value;
        
-                               if (target_type == TypeManager.byte_type)
+                               if (target_type == TypeManager.byte_type) {
+                                       if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
+                                               return null;
                                        return new ByteConstant ((byte) v);
-                               if (target_type == TypeManager.sbyte_type)
+                               }
+                               if (target_type == TypeManager.sbyte_type) {
+                                       if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
+                                               return null;
                                        return new SByteConstant ((sbyte) v);
-                               if (target_type == TypeManager.short_type)
+                               }
+                               if (target_type == TypeManager.short_type) {
+                                       if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
+                                               return null;
                                        return new ShortConstant ((short) v);
-                               if (target_type == TypeManager.ushort_type)
+                               }
+                               if (target_type == TypeManager.ushort_type) {
+                                       if (!CheckRange (ec, v, target_type, UInt16.MinValue, UInt16.MaxValue))
+                                               return null;
                                        return new UShortConstant ((ushort) v);
-                               if (target_type == TypeManager.int32_type)
+                               }
+                               if (target_type == TypeManager.int32_type) {
+                                       if (!CheckRange (ec, v, target_type, Int32.MinValue, Int32.MaxValue))
+                                               return null;
                                        return new IntConstant ((int) v);
+                               }
                                if (target_type == TypeManager.int64_type)
                                        return new LongConstant ((long) v);
                                if (target_type == TypeManager.uint64_type)
@@ -1316,65 +1545,116 @@ namespace Mono.CSharp {
                                        return new FloatConstant ((float) v);
                                if (target_type == TypeManager.double_type)
                                        return new DoubleConstant ((double) v);
-                               if (target_type == TypeManager.char_type)
+                               if (target_type == TypeManager.char_type) {
+                                       if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
+                                               return null;
                                        return new CharConstant ((char) v);
+                               }
                                if (target_type == TypeManager.decimal_type)
                                        return new DecimalConstant ((decimal) v);
                        }
-                       if (expr is LongConstant){
-                               long v = ((LongConstant) expr).Value;
+                       if (real_expr is LongConstant){
+                               long v = ((LongConstant) real_expr).Value;
        
-                               if (target_type == TypeManager.byte_type)
+                               if (target_type == TypeManager.byte_type) {
+                                       if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
+                                               return null;
                                        return new ByteConstant ((byte) v);
-                               if (target_type == TypeManager.sbyte_type)
+                               }
+                               if (target_type == TypeManager.sbyte_type) {
+                                       if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
+                                               return null;
                                        return new SByteConstant ((sbyte) v);
-                               if (target_type == TypeManager.short_type)
+                               }
+                               if (target_type == TypeManager.short_type) {
+                                       if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
+                                               return null;
                                        return new ShortConstant ((short) v);
-                               if (target_type == TypeManager.ushort_type)
+                               }
+                               if (target_type == TypeManager.ushort_type) {
+                                       if (!CheckRange (ec, v, target_type, UInt16.MinValue, UInt16.MaxValue))
+                                               return null;
                                        return new UShortConstant ((ushort) v);
-                               if (target_type == TypeManager.int32_type)
+                               }
+                               if (target_type == TypeManager.int32_type) {
+                                       if (!CheckRange (ec, v, target_type, Int32.MinValue, Int32.MaxValue))
+                                               return null;
                                        return new IntConstant ((int) v);
-                               if (target_type == TypeManager.uint32_type)
+                               }
+                               if (target_type == TypeManager.uint32_type) {
+                                       if (!CheckRange (ec, v, target_type, UInt32.MinValue, UInt32.MaxValue))
+                                               return null;
                                        return new UIntConstant ((uint) v);
-                               if (target_type == TypeManager.uint64_type)
+                               }
+                               if (target_type == TypeManager.uint64_type) {
+                                       if (!CheckUnsigned (ec, v, target_type))
+                                               return null;
                                        return new ULongConstant ((ulong) v);
+                               }
                                if (target_type == TypeManager.float_type)
                                        return new FloatConstant ((float) v);
                                if (target_type == TypeManager.double_type)
                                        return new DoubleConstant ((double) v);
-                               if (target_type == TypeManager.char_type)
+                               if (target_type == TypeManager.char_type) {
+                                       if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
+                                               return null;
                                        return new CharConstant ((char) v);
+                               }
                                if (target_type == TypeManager.decimal_type)
                                        return new DecimalConstant ((decimal) v);
                        }
-                       if (expr is ULongConstant){
-                               ulong v = ((ULongConstant) expr).Value;
+                       if (real_expr is ULongConstant){
+                               ulong v = ((ULongConstant) real_expr).Value;
        
-                               if (target_type == TypeManager.byte_type)
+                               if (target_type == TypeManager.byte_type) {
+                                       if (!CheckRange (ec, v, target_type, Byte.MaxValue))
+                                               return null;
                                        return new ByteConstant ((byte) v);
-                               if (target_type == TypeManager.sbyte_type)
+                               }
+                               if (target_type == TypeManager.sbyte_type) {
+                                       if (!CheckRange (ec, v, target_type, (ulong) SByte.MaxValue))
+                                               return null;
                                        return new SByteConstant ((sbyte) v);
-                               if (target_type == TypeManager.short_type)
+                               }
+                               if (target_type == TypeManager.short_type) {
+                                       if (!CheckRange (ec, v, target_type, (ulong) Int16.MaxValue))
+                                               return null;
                                        return new ShortConstant ((short) v);
-                               if (target_type == TypeManager.ushort_type)
+                               }
+                               if (target_type == TypeManager.ushort_type) {
+                                       if (!CheckRange (ec, v, target_type, UInt16.MaxValue))
+                                               return null;
                                        return new UShortConstant ((ushort) v);
-                               if (target_type == TypeManager.int32_type)
+                               }
+                               if (target_type == TypeManager.int32_type) {
+                                       if (!CheckRange (ec, v, target_type, Int32.MaxValue))
+                                               return null;
                                        return new IntConstant ((int) v);
-                               if (target_type == TypeManager.uint32_type)
+                               }
+                               if (target_type == TypeManager.uint32_type) {
+                                       if (!CheckRange (ec, v, target_type, UInt32.MaxValue))
+                                               return null;
                                        return new UIntConstant ((uint) v);
-                               if (target_type == TypeManager.int64_type)
+                               }
+                               if (target_type == TypeManager.int64_type) {
+                                       if (!CheckRange (ec, v, target_type, (ulong) Int64.MaxValue))
+                                               return null;
                                        return new LongConstant ((long) v);
+                               }
                                if (target_type == TypeManager.float_type)
                                        return new FloatConstant ((float) v);
                                if (target_type == TypeManager.double_type)
                                        return new DoubleConstant ((double) v);
-                               if (target_type == TypeManager.char_type)
+                               if (target_type == TypeManager.char_type) {
+                                       if (!CheckRange (ec, v, target_type, Char.MaxValue))
+                                               return null;
                                        return new CharConstant ((char) v);
+                               }
                                if (target_type == TypeManager.decimal_type)
                                        return new DecimalConstant ((decimal) v);
                        }
-                       if (expr is FloatConstant){
-                               float v = ((FloatConstant) expr).Value;
+                       if (real_expr is FloatConstant){
+                               float v = ((FloatConstant) real_expr).Value;
        
                                if (target_type == TypeManager.byte_type)
                                        return new ByteConstant ((byte) v);
@@ -1399,8 +1679,8 @@ namespace Mono.CSharp {
                                if (target_type == TypeManager.decimal_type)
                                        return new DecimalConstant ((decimal) v);
                        }
-                       if (expr is DoubleConstant){
-                               double v = ((DoubleConstant) expr).Value;
+                       if (real_expr is DoubleConstant){
+                               double v = ((DoubleConstant) real_expr).Value;
        
                                if (target_type == TypeManager.byte_type)
                                        return new ByteConstant ((byte) v);
@@ -1426,6 +1706,45 @@ namespace Mono.CSharp {
                                        return new DecimalConstant ((decimal) v);
                        }
 
+                       if (real_expr is CharConstant){
+                               char v = ((CharConstant) real_expr).Value;
+                               
+                               if (target_type == TypeManager.byte_type) {
+                                       if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
+                                               return null;
+                                       return new ByteConstant ((byte) v);
+                               }
+                               if (target_type == TypeManager.sbyte_type) {
+                                       if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
+                                               return null;
+                                       return new SByteConstant ((sbyte) v);
+                               }
+                               if (target_type == TypeManager.short_type) {
+                                       if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
+                                               return null;
+                                       return new ShortConstant ((short) v);
+                               }
+                               if (target_type == TypeManager.int32_type)
+                                       return new IntConstant ((int) v);
+                               if (target_type == TypeManager.uint32_type)
+                                       return new UIntConstant ((uint) v);
+                               if (target_type == TypeManager.int64_type)
+                                       return new LongConstant ((long) v);
+                               if (target_type == TypeManager.uint64_type)
+                                       return new ULongConstant ((ulong) v);
+                               if (target_type == TypeManager.float_type)
+                                       return new FloatConstant ((float) v);
+                               if (target_type == TypeManager.double_type)
+                                       return new DoubleConstant ((double) v);
+                               if (target_type == TypeManager.char_type) {
+                                       if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
+                                               return null;
+                                       return new CharConstant ((char) v);
+                               }
+                               if (target_type == TypeManager.decimal_type)
+                                       return new DecimalConstant ((decimal) v);
+                       }
+
                        return null;
                }
                
@@ -1443,15 +1762,15 @@ namespace Mono.CSharp {
                                return null;
 
                        eclass = ExprClass.Value;
-                       
+
                        if (expr is Constant){
                                Expression e = TryReduce (ec, type);
 
                                if (e != null)
                                        return e;
                        }
-                       
-                       expr = ConvertExplicit (ec, expr, type, loc);
+
+                       expr = Convert.ExplicitConversion (ec, expr, type, loc);
                        return expr;
                }
 
@@ -1495,8 +1814,8 @@ namespace Mono.CSharp {
                bool DelegateOperation;
 
                // This must be kept in sync with Operator!!!
-               static string [] oper_names;
-
+               public static readonly string [] oper_names;
+               
                static Binary ()
                {
                        oper_names = new string [(int) Operator.TOP];
@@ -1615,7 +1934,7 @@ namespace Mono.CSharp {
                        if (expr.Type == target_type)
                                return expr;
 
-                       return ConvertImplicit (ec, expr, target_type, loc);
+                       return Convert.ImplicitConversion (ec, expr, target_type, loc);
                }
 
                public static void Error_OperatorAmbiguous (Location loc, Operator oper, Type l, Type r)
@@ -1636,9 +1955,9 @@ namespace Mono.CSharp {
                        if (!check_user_conversions)
                                return false;
 
-                       if (ImplicitUserConversionExists (ec, l, t))
+                       if (Convert.ImplicitUserConversionExists (ec, l, t))
                                return true;
-                       else if (ImplicitUserConversionExists (ec, r, t))
+                       else if (Convert.ImplicitUserConversionExists (ec, r, t))
                                return true;
                        else
                                return false;
@@ -1661,9 +1980,9 @@ namespace Mono.CSharp {
                                // conveted to type double.
                                //
                                if (r != TypeManager.double_type)
-                                       right = ConvertImplicit (ec, right, TypeManager.double_type, loc);
+                                       right = Convert.ImplicitConversion (ec, right, TypeManager.double_type, loc);
                                if (l != TypeManager.double_type)
-                                       left = ConvertImplicit (ec, left, TypeManager.double_type, loc);
+                                       left = Convert.ImplicitConversion (ec, left, TypeManager.double_type, loc);
                                
                                type = TypeManager.double_type;
                        } else if (IsOfType (ec, l, r, TypeManager.float_type, check_user_conv)){
@@ -1672,9 +1991,9 @@ namespace Mono.CSharp {
                                // converted to type float.
                                //
                                if (r != TypeManager.double_type)
-                                       right = ConvertImplicit (ec, right, TypeManager.float_type, loc);
+                                       right = Convert.ImplicitConversion (ec, right, TypeManager.float_type, loc);
                                if (l != TypeManager.double_type)
-                                       left = ConvertImplicit (ec, left, TypeManager.float_type, loc);
+                                       left = Convert.ImplicitConversion (ec, left, TypeManager.float_type, loc);
                                type = TypeManager.float_type;
                        } else if (IsOfType (ec, l, r, TypeManager.uint64_type, check_user_conv)){
                                Expression e;
@@ -1689,7 +2008,7 @@ namespace Mono.CSharp {
                                                if (right is IntConstant){
                                                        IntConstant ic = (IntConstant) right;
                                                        
-                                                       e = TryImplicitIntConversion (l, ic);
+                                                       e = Convert.TryImplicitIntConversion (l, ic);
                                                        if (e != null)
                                                                right = e;
                                                } else if (right is LongConstant){
@@ -1698,7 +2017,7 @@ namespace Mono.CSharp {
                                                        if (ll > 0)
                                                                right = new ULongConstant ((ulong) ll);
                                                } else {
-                                                       e = ImplicitNumericConversion (ec, right, l, loc);
+                                                       e = Convert.ImplicitNumericConversion (ec, right, l, loc);
                                                        if (e != null)
                                                                right = e;
                                                }
@@ -1706,7 +2025,7 @@ namespace Mono.CSharp {
                                        other = right.Type;
                                } else {
                                        if (left is IntConstant){
-                                               e = TryImplicitIntConversion (r, (IntConstant) left);
+                                               e = Convert.TryImplicitIntConversion (r, (IntConstant) left);
                                                if (e != null)
                                                        left = e;
                                        } else if (left is LongConstant){
@@ -1715,7 +2034,7 @@ namespace Mono.CSharp {
                                                if (ll > 0)
                                                        left = new ULongConstant ((ulong) ll);
                                        } else {
-                                               e = ImplicitNumericConversion (ec, left, r, loc);
+                                               e = Convert.ImplicitNumericConversion (ec, left, r, loc);
                                                if (e != null)
                                                        left = e;
                                        }
@@ -1734,9 +2053,9 @@ namespace Mono.CSharp {
                                // to type long.
                                //
                                if (l != TypeManager.int64_type)
-                                       left = ConvertImplicit (ec, left, TypeManager.int64_type, loc);
+                                       left = Convert.ImplicitConversion (ec, left, TypeManager.int64_type, loc);
                                if (r != TypeManager.int64_type)
-                                       right = ConvertImplicit (ec, right, TypeManager.int64_type, loc);
+                                       right = Convert.ImplicitConversion (ec, right, TypeManager.int64_type, loc);
                                
                                type = TypeManager.int64_type;
                        } else if (IsOfType (ec, l, r, TypeManager.uint32_type, check_user_conv)){
@@ -1792,10 +2111,10 @@ namespace Mono.CSharp {
                                } 
                        } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
                                if (l != TypeManager.decimal_type)
-                                       left = ConvertImplicit (ec, left, TypeManager.decimal_type, loc);
+                                       left = Convert.ImplicitConversion (ec, left, TypeManager.decimal_type, loc);
 
                                if (r != TypeManager.decimal_type)
-                                       right = ConvertImplicit (ec, right, TypeManager.decimal_type, loc);
+                                       right = Convert.ImplicitConversion (ec, right, TypeManager.decimal_type, loc);
                                type = TypeManager.decimal_type;
                        } else {
                                left = ForceConversion (ec, left, TypeManager.int32_type);
@@ -1854,10 +2173,10 @@ namespace Mono.CSharp {
                        }
                        right = e;
 
-                       if (((e = ConvertImplicit (ec, left, TypeManager.int32_type, loc)) != null) ||
-                           ((e = ConvertImplicit (ec, left, TypeManager.uint32_type, loc)) != null) ||
-                           ((e = ConvertImplicit (ec, left, TypeManager.int64_type, loc)) != null) ||
-                           ((e = ConvertImplicit (ec, left, TypeManager.uint64_type, loc)) != null)){
+                       if (((e = Convert.ImplicitConversion (ec, left, TypeManager.int32_type, loc)) != null) ||
+                           ((e = Convert.ImplicitConversion (ec, left, TypeManager.uint32_type, loc)) != null) ||
+                           ((e = Convert.ImplicitConversion (ec, left, TypeManager.int64_type, loc)) != null) ||
+                           ((e = Convert.ImplicitConversion (ec, left, TypeManager.uint64_type, loc)) != null)){
                                left = e;
                                type = e.Type;
 
@@ -1875,36 +2194,54 @@ namespace Mono.CSharp {
                        bool overload_failed = false;
 
                        //
-                       // Step 1: Perform Operator Overload location
+                       // Special cases: string comapred to null
                        //
-                       Expression left_expr, right_expr;
+                       if (oper == Operator.Equality || oper == Operator.Inequality){
+                               if ((l == TypeManager.string_type && (right is NullLiteral)) ||
+                                   (r == TypeManager.string_type && (left is NullLiteral))){
+                                       Type = TypeManager.bool_type;
+                                       
+                                       return this;
+                               }
+                       }
+
+                       //
+                       // Do not perform operator overload resolution when both sides are
+                       // built-in types
+                       //
+                       if (!(TypeManager.IsCLRType (l) && TypeManager.IsCLRType (r))){
+                               //
+                               // Step 1: Perform Operator Overload location
+                               //
+                               Expression left_expr, right_expr;
                                
-                       string op = oper_names [(int) oper];
+                               string op = oper_names [(int) oper];
                                
-                       MethodGroupExpr union;
-                       left_expr = MemberLookup (ec, l, op, MemberTypes.Method, AllBindingFlags, loc);
-                       if (r != l){
-                               right_expr = MemberLookup (
-                                       ec, r, op, MemberTypes.Method, AllBindingFlags, loc);
-                               union = Invocation.MakeUnionSet (left_expr, right_expr, loc);
-                       } else
-                               union = (MethodGroupExpr) left_expr;
-
-                       if (union != null) {
-                               Arguments = new ArrayList ();
-                               Arguments.Add (new Argument (left, Argument.AType.Expression));
-                               Arguments.Add (new Argument (right, Argument.AType.Expression));
+                               MethodGroupExpr union;
+                               left_expr = MemberLookup (ec, l, op, MemberTypes.Method, AllBindingFlags, loc);
+                               if (r != l){
+                                       right_expr = MemberLookup (
+                                               ec, r, op, MemberTypes.Method, AllBindingFlags, loc);
+                                       union = Invocation.MakeUnionSet (left_expr, right_expr, loc);
+                               } else
+                                       union = (MethodGroupExpr) left_expr;
                                
-                               method = Invocation.OverloadResolve (ec, union, Arguments, loc);
-                               if (method != null) {
-                                       MethodInfo mi = (MethodInfo) method;
+                               if (union != null) {
+                                       Arguments = new ArrayList ();
+                                       Arguments.Add (new Argument (left, Argument.AType.Expression));
+                                       Arguments.Add (new Argument (right, Argument.AType.Expression));
                                        
-                                       type = mi.ReturnType;
-                                       return this;
-                               } else {
-                                       overload_failed = true;
+                                       method = Invocation.OverloadResolve (ec, union, Arguments, Location.Null);
+                                       if (method != null) {
+                                               MethodInfo mi = (MethodInfo) method;
+                                               
+                                               type = mi.ReturnType;
+                                               return this;
+                                       } else {
+                                               overload_failed = true;
+                                       }
                                }
-                       }       
+                       }
                        
                        //
                        // Step 2: Default operations on CLI native types.
@@ -1933,14 +2270,45 @@ namespace Mono.CSharp {
                                                        return new StringConstant (
                                                                ls.Value + rs.Value);
                                                }
-                                               
+
+                                               if (left is Binary){
+                                                       Binary b = (Binary) left;
+
+                                                       //
+                                                       // Call String.Concat (string, string, string) or
+                                                       // String.Concat (string, string, string, string)
+                                                       // if possible.
+                                                       //
+                                                       if (b.oper == Operator.Addition &&
+                                                           (b.method == TypeManager.string_concat_string_string_string ||
+                                                            b.method == TypeManager.string_concat_string_string_string_string)){
+                                                               ArrayList bargs = b.Arguments;
+                                                               int count = bargs.Count;
+                                                               
+                                                               if (count == 2){
+                                                                       Arguments = bargs;
+                                                                       Arguments.Add (new Argument (right, Argument.AType.Expression));
+                                                                       type = TypeManager.string_type;
+                                                                       method = TypeManager.string_concat_string_string_string;
+                                                                       
+                                                                       return this;
+                                                               } else if (count == 3){
+                                                                       Arguments = bargs;
+                                                                       Arguments.Add (new Argument (right, Argument.AType.Expression));
+                                                                       type = TypeManager.string_type;
+                                                                       method = TypeManager.string_concat_string_string_string_string;                                                                 
+                                                                       return this;
+                                                               }
+                                                       }
+                                               }
+
                                                // string + string
                                                method = TypeManager.string_concat_string_string;
                                        } else {
                                                // string + object
                                                method = TypeManager.string_concat_object_object;
-                                               right = ConvertImplicit (ec, right,
-                                                                        TypeManager.object_type, loc);
+                                               right = Convert.ImplicitConversion (
+                                                       ec, right, TypeManager.object_type, loc);
                                                if (right == null){
                                                        Error_OperatorCannotBeApplied (loc, OperName (oper), l, r);
                                                        return null;
@@ -1963,7 +2331,7 @@ namespace Mono.CSharp {
                                        }
                                        
                                        method = TypeManager.string_concat_object_object;
-                                       left = ConvertImplicit (ec, left, TypeManager.object_type, loc);
+                                       left = Convert.ImplicitConversion (ec, left, TypeManager.object_type, loc);
                                        if (left == null){
                                                Error_OperatorCannotBeApplied (loc, OperName (oper), l, r);
                                                return null;
@@ -2024,8 +2392,8 @@ namespace Mono.CSharp {
                                        //
                                        // Also, a standard conversion must exist from either one
                                        //
-                                       if (!(StandardConversionExists (left, r) ||
-                                             StandardConversionExists (right, l))){
+                                       if (!(Convert.ImplicitStandardConversionExists (left, r) ||
+                                             Convert.ImplicitStandardConversionExists (right, l))){
                                                Error_OperatorCannotBeApplied ();
                                                return null;
                                        }
@@ -2150,7 +2518,7 @@ namespace Mono.CSharp {
                                }
                                
                                if (!rie){
-                                       temp = ConvertImplicit (ec, right, l, loc);
+                                       temp = Convert.ImplicitConversion (ec, right, l, loc);
                                        if (temp != null)
                                                right = temp;
                                        else {
@@ -2158,7 +2526,7 @@ namespace Mono.CSharp {
                                                return null;
                                        }
                                } if (!lie){
-                                       temp = ConvertImplicit (ec, left, r, loc);
+                                       temp = Convert.ImplicitConversion (ec, left, r, loc);
                                        if (temp != null){
                                                left = temp;
                                                l = r;
@@ -2171,6 +2539,10 @@ namespace Mono.CSharp {
                                if (oper == Operator.Equality || oper == Operator.Inequality ||
                                    oper == Operator.LessThanOrEqual || oper == Operator.LessThan ||
                                    oper == Operator.GreaterThanOrEqual || oper == Operator.GreaterThan){
+                                       if (left.Type != right.Type){
+                                               Error_OperatorCannotBeApplied ();
+                                               return null;
+                                       }
                                        type = TypeManager.bool_type;
                                        return this;
                                }
@@ -2283,22 +2655,16 @@ namespace Mono.CSharp {
                        if (left == null || right == null)
                                return null;
 
-                       if (left.Type == null)
-                               throw new Exception (
-                                       "Resolve returned non null, but did not set the type! (" +
-                                       left + ") at Line: " + loc.Row);
-                       if (right.Type == null)
-                               throw new Exception (
-                                       "Resolve returned non null, but did not set the type! (" +
-                                       right + ") at Line: "+ loc.Row);
-
                        eclass = ExprClass.Value;
 
-                       if (left is Constant && right is Constant){
+                       Constant rc = right as Constant;
+                       Constant lc = left as Constant;
+
+                       if (rc != null & lc != null){
                                Expression e = ConstantFold.BinaryFold (
-                                       ec, oper, (Constant) left, (Constant) right, loc);
-                               if (e != null)
-                                       return e;
+                                       ec, oper, lc, rc, loc);
+                                       if (e != null)
+                                               return e;
                        }
 
                        return ResolveOperator (ec);
@@ -2452,7 +2818,8 @@ namespace Mono.CSharp {
                        left.Emit (ec);
                        right.Emit (ec);
 
-                       bool isUnsigned = is_unsigned (left.Type);
+                       Type t = left.Type;
+                       bool isUnsigned = is_unsigned (t);
 
                        switch (oper){
                        case Operator.Equality:
@@ -2496,6 +2863,9 @@ namespace Mono.CSharp {
                                break;
 
                        case Operator.LessThanOrEqual:
+                               if (t == TypeManager.double_type || t == TypeManager.float_type)
+                                       isUnsigned = true;
+
                                if (onTrue)
                                        if (isUnsigned)
                                                ig.Emit (OpCodes.Ble_Un, target);
@@ -2510,6 +2880,8 @@ namespace Mono.CSharp {
 
 
                        case Operator.GreaterThanOrEqual:
+                               if (t == TypeManager.double_type || t == TypeManager.float_type)
+                                       isUnsigned = true;
                                if (onTrue)
                                        if (isUnsigned)
                                                ig.Emit (OpCodes.Bge_Un, target);
@@ -2614,6 +2986,7 @@ namespace Mono.CSharp {
                        right.Emit (ec);
 
                        bool isUnsigned = is_unsigned (left.Type);
+
                        switch (oper){
                        case Operator.Multiply:
                                if (ec.CheckState){
@@ -2703,7 +3076,9 @@ namespace Mono.CSharp {
                                break;
 
                        case Operator.LessThanOrEqual:
-                               if (isUnsigned)
+                               Type lt = left.Type;
+                               
+                               if (isUnsigned || (lt == TypeManager.double_type || lt == TypeManager.float_type))
                                        ig.Emit (OpCodes.Cgt_Un);
                                else
                                        ig.Emit (OpCodes.Cgt);
@@ -2713,7 +3088,9 @@ namespace Mono.CSharp {
                                break;
 
                        case Operator.GreaterThanOrEqual:
-                               if (isUnsigned)
+                               Type le = left.Type;
+                               
+                               if (isUnsigned || (le == TypeManager.double_type || le == TypeManager.float_type))
                                        ig.Emit (OpCodes.Clt_Un);
                                else
                                        ig.Emit (OpCodes.Clt);
@@ -2757,8 +3134,7 @@ namespace Mono.CSharp {
                //
                // We assume that `l' is always a pointer
                //
-               public PointerArithmetic (bool is_addition, Expression l, Expression r, Type t,
-                                         Location loc)
+               public PointerArithmetic (bool is_addition, Expression l, Expression r, Type t, Location loc)
                {
                        type = t;
                        eclass = ExprClass.Variable;
@@ -2781,8 +3157,9 @@ namespace Mono.CSharp {
                        Type op_type = left.Type;
                        ILGenerator ig = ec.ig;
                        int size = GetTypeSize (op_type.GetElementType ());
+                       Type rtype = right.Type;
                        
-                       if (right.Type.IsPointer){
+                       if (rtype.IsPointer){
                                //
                                // handle (pointer - pointer)
                                //
@@ -2810,7 +3187,12 @@ namespace Mono.CSharp {
                                                ig.Emit (OpCodes.Sizeof, op_type);
                                        else 
                                                IntLiteral.EmitInt (ig, size);
+                                       if (rtype == TypeManager.int64_type)
+                                               ig.Emit (OpCodes.Conv_I8);
+                                       else if (rtype == TypeManager.uint64_type)
+                                               ig.Emit (OpCodes.Conv_U8);
                                        ig.Emit (OpCodes.Mul);
+                                       ig.Emit (OpCodes.Conv_I);
                                }
                                if (is_add)
                                        ig.Emit (OpCodes.Add);
@@ -2859,9 +3241,13 @@ namespace Mono.CSharp {
                        if (expr == null)
                                return null;
                        
-                       if (expr.Type != TypeManager.bool_type)
-                               expr = Expression.ConvertImplicitRequired (
-                                       ec, expr, TypeManager.bool_type, loc);
+                       if (expr.Type != TypeManager.bool_type){
+                               expr = Expression.ResolveBoolean (
+                                       ec, expr, loc);
+                               
+                               if (expr == null)
+                                       return null;
+                       }
                        
                        trueExpr = trueExpr.Resolve (ec);
                        falseExpr = falseExpr.Resolve (ec);
@@ -2889,12 +3275,12 @@ namespace Mono.CSharp {
                                // First, if an implicit conversion exists from trueExpr
                                // to falseExpr, then the result type is of type falseExpr.Type
                                //
-                               conv = ConvertImplicit (ec, trueExpr, false_type, loc);
+                               conv = Convert.ImplicitConversion (ec, trueExpr, false_type, loc);
                                if (conv != null){
                                        //
                                        // Check if both can convert implicitl to each other's type
                                        //
-                                       if (ConvertImplicit (ec, falseExpr, true_type, loc) != null){
+                                       if (Convert.ImplicitConversion (ec, falseExpr, true_type, loc) != null){
                                                Error (172,
                                                       "Can not compute type of conditional expression " +
                                                       "as `" + TypeManager.CSharpName (trueExpr.Type) +
@@ -2904,7 +3290,7 @@ namespace Mono.CSharp {
                                        }
                                        type = false_type;
                                        trueExpr = conv;
-                               } else if ((conv = ConvertImplicit(ec, falseExpr, true_type,loc))!= null){
+                               } else if ((conv = Convert.ImplicitConversion(ec, falseExpr, true_type,loc))!= null){
                                        type = true_type;
                                        falseExpr = conv;
                                } else {
@@ -2950,6 +3336,7 @@ namespace Mono.CSharp {
        public class LocalVariableReference : Expression, IAssignMethod, IMemoryLocation, IVariable {
                public readonly string Name;
                public readonly Block Block;
+               LocalInfo local_info;
                VariableInfo variable_info;
                bool is_readonly;
                
@@ -2964,77 +3351,57 @@ namespace Mono.CSharp {
                // Setting `is_readonly' to false will allow you to create a writable
                // reference to a read-only variable.  This is used by foreach and using.
                public LocalVariableReference (Block block, string name, Location l,
-                                              VariableInfo variable_info, bool is_readonly)
+                                              LocalInfo local_info, bool is_readonly)
                        : this (block, name, l)
                {
-                       this.variable_info = variable_info;
+                       this.local_info = local_info;
                        this.is_readonly = is_readonly;
                }
 
                public VariableInfo VariableInfo {
-                       get {
-                               if (variable_info == null) {
-                                       variable_info = Block.GetVariableInfo (Name);
-                                       is_readonly = variable_info.ReadOnly;
-                               }
-                               return variable_info;
-                       }
-               }
-
-               public bool IsAssigned (EmitContext ec, Location loc)
-               {
-                       return VariableInfo.IsAssigned (ec, loc);
-               }
-
-               public bool IsFieldAssigned (EmitContext ec, string name, Location loc)
-               {
-                       return VariableInfo.IsFieldAssigned (ec, name, loc);
-               }
-
-               public void SetAssigned (EmitContext ec)
-               {
-                       VariableInfo.SetAssigned (ec);
-               }
-
-               public void SetFieldAssigned (EmitContext ec, string name)
-               {
-                       VariableInfo.SetFieldAssigned (ec, name);
+                       get { return variable_info; }
                }
 
                public bool IsReadOnly {
                        get {
-                               if (variable_info == null) {
-                                       variable_info = Block.GetVariableInfo (Name);
-                                       is_readonly = variable_info.ReadOnly;
-                               }
                                return is_readonly;
                        }
                }
+
+               protected void DoResolveBase (EmitContext ec)
+               {
+                       if (local_info == null) {
+                               local_info = Block.GetLocalInfo (Name);
+                               is_readonly = local_info.ReadOnly;
+                       }
+
+                       variable_info = Block.GetVariableInfo (local_info);
+                       type = local_info.VariableType;
+               }
                
                public override Expression DoResolve (EmitContext ec)
                {
-                       VariableInfo vi = VariableInfo;
-                       Expression e;
-                       
-                       e = Block.GetConstantExpression (Name);
+                       DoResolveBase (ec);
+
+                       Expression e = Block.GetConstantExpression (Name);
                        if (e != null) {
-                               vi.Used = true;
+                               local_info.Used = true;
+                               eclass = ExprClass.Value;
                                return e;
                        }
 
-                       if (ec.DoFlowAnalysis && !IsAssigned (ec, loc))
+                       if ((variable_info != null) && !variable_info.IsAssigned (ec, loc))
                                return null;
 
-                       type = vi.VariableType;
                        return this;
                }
 
                override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
                {
-                       VariableInfo vi = VariableInfo;
+                       DoResolveBase (ec);
 
-                       if (ec.DoFlowAnalysis)
-                               ec.SetVariableAssigned (vi);
+                       if (variable_info != null)
+                               variable_info.SetAssigned (ec);
 
                        Expression e = DoResolve (ec);
 
@@ -3051,30 +3418,40 @@ namespace Mono.CSharp {
 
                public override void Emit (EmitContext ec)
                {
-                       VariableInfo vi = VariableInfo;
                        ILGenerator ig = ec.ig;
 
-                       ig.Emit (OpCodes.Ldloc, vi.LocalBuilder);
-                       vi.Used = true;
+                       if (local_info.LocalBuilder == null){
+                               ec.EmitThis ();
+                               ig.Emit (OpCodes.Ldfld, local_info.FieldBuilder);
+                       } else 
+                               ig.Emit (OpCodes.Ldloc, local_info.LocalBuilder);
+                       
+                       local_info.Used = true;
                }
                
                public void EmitAssign (EmitContext ec, Expression source)
                {
                        ILGenerator ig = ec.ig;
-                       VariableInfo vi = VariableInfo;
 
-                       vi.Assigned = true;
+                       local_info.Assigned = true;
 
-                       source.Emit (ec);
-                       
-                       ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
+                       if (local_info.LocalBuilder == null){
+                               ec.EmitThis ();
+                               source.Emit (ec);
+                               ig.Emit (OpCodes.Stfld, local_info.FieldBuilder);
+                       } else {
+                               source.Emit (ec);
+                               ig.Emit (OpCodes.Stloc, local_info.LocalBuilder);
+                       }
                }
                
                public void AddressOf (EmitContext ec, AddressOp mode)
                {
-                       VariableInfo vi = VariableInfo;
-
-                       ec.ig.Emit (OpCodes.Ldloca, vi.LocalBuilder);
+                       if (local_info.LocalBuilder == null){
+                               ec.EmitThis ();
+                               ec.ig.Emit (OpCodes.Ldflda, local_info.FieldBuilder);
+                       } else
+                               ec.ig.Emit (OpCodes.Ldloca, local_info.LocalBuilder);
                }
        }
 
@@ -3086,59 +3463,68 @@ namespace Mono.CSharp {
                Parameters pars;
                String name;
                int idx;
+               Block block;
+               VariableInfo vi;
                public Parameter.Modifier mod;
                public bool is_ref, is_out;
                
-               public ParameterReference (Parameters pars, int idx, string name, Location loc)
+               public ParameterReference (Parameters pars, Block block, int idx, string name, Location loc)
                {
                        this.pars = pars;
+                       this.block = block;
                        this.idx  = idx;
                        this.name = name;
                        this.loc = loc;
                        eclass = ExprClass.Variable;
                }
 
+               public VariableInfo VariableInfo {
+                       get { return vi; }
+               }
+
                public bool IsAssigned (EmitContext ec, Location loc)
                {
-                       if (!is_out || !ec.DoFlowAnalysis)
+                       if (!ec.DoFlowAnalysis || !is_out ||
+                           ec.CurrentBranching.IsAssigned (vi))
                                return true;
 
-                       if (!ec.CurrentBranching.IsParameterAssigned (idx)) {
-                               Report.Error (165, loc,
-                                             "Use of unassigned local variable `" + name + "'");
-                               return false;
-                       }
-
-                       return true;
+                       Report.Error (165, loc,
+                                     "Use of unassigned parameter `" + name + "'");
+                       return false;
                }
 
                public bool IsFieldAssigned (EmitContext ec, string field_name, Location loc)
                {
-                       if (!is_out || !ec.DoFlowAnalysis)
+                       if (!ec.DoFlowAnalysis || !is_out ||
+                           ec.CurrentBranching.IsFieldAssigned (vi, field_name))
                                return true;
 
-                       if (ec.CurrentBranching.IsParameterAssigned (idx))
-                               return true;
-
-                       if (!ec.CurrentBranching.IsParameterAssigned (idx, field_name)) {
-                               Report.Error (170, loc,
-                                             "Use of possibly unassigned field `" + field_name + "'");
-                               return false;
-                       }
-
-                       return true;
+                       Report.Error (170, loc,
+                                     "Use of possibly unassigned field `" + field_name + "'");
+                       return false;
                }
 
                public void SetAssigned (EmitContext ec)
                {
                        if (is_out && ec.DoFlowAnalysis)
-                               ec.CurrentBranching.SetParameterAssigned (idx);
+                               ec.CurrentBranching.SetAssigned (vi);
                }
 
                public void SetFieldAssigned (EmitContext ec, string field_name)
                {
                        if (is_out && ec.DoFlowAnalysis)
-                               ec.CurrentBranching.SetParameterAssigned (idx, field_name);
+                               ec.CurrentBranching.SetFieldAssigned (vi, field_name);
+               }
+
+               protected void DoResolveBase (EmitContext ec)
+               {
+                       type = pars.GetParameterInfo (ec.DeclSpace, idx, out mod);
+                       is_ref = (mod & Parameter.Modifier.ISBYREF) != 0;
+                       is_out = (mod & Parameter.Modifier.OUT) != 0;
+                       eclass = ExprClass.Variable;
+
+                       if (is_out)
+                               vi = block.ParameterMap [idx];
                }
 
                //
@@ -3155,10 +3541,7 @@ namespace Mono.CSharp {
                //
                public override Expression DoResolve (EmitContext ec)
                {
-                       type = pars.GetParameterInfo (ec.DeclSpace, idx, out mod);
-                       is_ref = (mod & Parameter.Modifier.ISBYREF) != 0;
-                       is_out = (mod & Parameter.Modifier.OUT) != 0;
-                       eclass = ExprClass.Variable;
+                       DoResolveBase (ec);
 
                        if (is_out && ec.DoFlowAnalysis && !IsAssigned (ec, loc))
                                return null;
@@ -3168,18 +3551,14 @@ namespace Mono.CSharp {
 
                override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
                {
-                       type = pars.GetParameterInfo (ec.DeclSpace, idx, out mod);
-                       is_ref = (mod & Parameter.Modifier.ISBYREF) != 0;
-                       is_out = (mod & Parameter.Modifier.OUT) != 0;
-                       eclass = ExprClass.Variable;
+                       DoResolveBase (ec);
 
-                       if (is_out && ec.DoFlowAnalysis)
-                               ec.SetParameterAssigned (idx);
+                       SetAssigned (ec);
 
                        return this;
                }
 
-               static void EmitLdArg (ILGenerator ig, int x)
+               static public void EmitLdArg (ILGenerator ig, int x)
                {
                        if (x <= 255){
                                switch (x){
@@ -3213,6 +3592,13 @@ namespace Mono.CSharp {
                public override void Emit (EmitContext ec)
                {
                        ILGenerator ig = ec.ig;
+                       
+                       if (ec.RemapToProxy){
+                               ig.Emit (OpCodes.Ldarg_0);
+                               ec.EmitArgument (idx);
+                               return;
+                       }
+                       
                        int arg_idx = idx;
 
                        if (!ec.IsStatic)
@@ -3233,6 +3619,14 @@ namespace Mono.CSharp {
                public void EmitAssign (EmitContext ec, Expression source)
                {
                        ILGenerator ig = ec.ig;
+                       
+                       if (ec.RemapToProxy){
+                               ig.Emit (OpCodes.Ldarg_0);
+                               source.Emit (ec);
+                               ec.EmitStoreArgument (idx);
+                               return;
+                       }
+                       
                        int arg_idx = idx;
 
                        if (!ec.IsStatic)
@@ -3255,6 +3649,11 @@ namespace Mono.CSharp {
 
                public void AddressOf (EmitContext ec, AddressOp mode)
                {
+                       if (ec.RemapToProxy){
+                               Report.Error (-1, "Report this: Taking the address of a remapped parameter not supported");
+                               return;
+                       }
+                       
                        int arg_idx = idx;
 
                        if (!ec.IsStatic)
@@ -3297,7 +3696,7 @@ namespace Mono.CSharp {
                public Type Type {
                        get {
                                if (ArgType == AType.Ref || ArgType == AType.Out)
-                                       return TypeManager.LookupType (Expr.Type.ToString () + "&");
+                                       return TypeManager.GetReferenceType (Expr.Type);
                                else
                                        return Expr.Type;
                        }
@@ -3572,7 +3971,7 @@ namespace Mono.CSharp {
                        }
 
                        if (q == null) {
-                               Expression tmp = ConvertImplicit (ec, argument_expr, p, loc);
+                               Expression tmp = Convert.ImplicitConversion (ec, argument_expr, p, loc);
                                
                                if (tmp != null)
                                        return 1;
@@ -3583,8 +3982,8 @@ namespace Mono.CSharp {
                        Expression p_tmp = new EmptyExpression (p);
                        Expression q_tmp = new EmptyExpression (q);
                        
-                       if (ImplicitConversionExists (ec, p_tmp, q) == true &&
-                           ImplicitConversionExists (ec, q_tmp, p) == false)
+                       if (Convert.ImplicitConversionExists (ec, p_tmp, q) == true &&
+                           Convert.ImplicitConversionExists (ec, q_tmp, p) == false)
                                return 1;
 
                        if (p == TypeManager.sbyte_type)
@@ -3827,14 +4226,14 @@ namespace Mono.CSharp {
                                if (a_mod == p_mod) {
 
                                        if (a_mod == Parameter.Modifier.NONE)
-                                               if (!ImplicitConversionExists (ec, a.Expr, pd.ParameterType (i)))
+                                               if (!Convert.ImplicitConversionExists (ec, a.Expr, pd.ParameterType (i)))
                                                        return false;
                                                                                
                                        if ((a_mod & Parameter.Modifier.ISBYREF) != 0) {
                                                Type pt = pd.ParameterType (i);
 
                                                if (!pt.IsByRef)
-                                                       pt = TypeManager.LookupType (pt.FullName + "&");
+                                                       pt = TypeManager.GetReferenceType (pt);
                                                
                                                if (pt != a.Type)
                                                        return false;
@@ -3849,7 +4248,7 @@ namespace Mono.CSharp {
                        for (int i = pd_count - 1; i < arg_count; i++) {
                                Argument a = (Argument) arguments [i];
                                
-                               if (!StandardConversionExists (a.Expr, element_type))
+                               if (!Convert.ImplicitStandardConversionExists (a.Expr, element_type))
                                        return false;
                        }
                        
@@ -3889,14 +4288,14 @@ namespace Mono.CSharp {
                                if (a_mod == p_mod ||
                                    (a_mod == Parameter.Modifier.NONE && p_mod == Parameter.Modifier.PARAMS)) {
                                        if (a_mod == Parameter.Modifier.NONE)
-                                               if (!ImplicitConversionExists (ec, a.Expr, pd.ParameterType (i)))
+                                               if (!Convert.ImplicitConversionExists (ec, a.Expr, pd.ParameterType (i)))
                                                        return false;
                                        
                                        if ((a_mod & Parameter.Modifier.ISBYREF) != 0) {
                                                Type pt = pd.ParameterType (i);
 
                                                if (!pt.IsByRef)
-                                                       pt = TypeManager.LookupType (pt.FullName + "&");
+                                                       pt = TypeManager.GetReferenceType (pt);
 
                                                if (pt != a.Type)
                                                        return false;
@@ -3929,7 +4328,6 @@ namespace Mono.CSharp {
                public static MethodBase OverloadResolve (EmitContext ec, MethodGroupExpr me,
                                                          ArrayList Arguments, Location loc)
                {
-                       ArrayList afm = new ArrayList ();
                        MethodBase method = null;
                        Type current_type = null;
                        int argument_count;
@@ -4050,6 +4448,23 @@ namespace Mono.CSharp {
                        return method;
                }
 
+               static void Error_InvalidArguments (Location loc, int idx, MethodBase method,
+                                                    Type delegate_type, string arg_sig, string par_desc)
+               {
+                       if (delegate_type == null) 
+                               Report.Error (1502, loc,
+                                             "The best overloaded match for method '" +
+                                             FullMethodDesc (method) +
+                                             "' has some invalid arguments");
+                       else
+                               Report.Error (1594, loc,
+                                             "Delegate '" + delegate_type.ToString () +
+                                             "' has some invalid arguments.");
+                       Report.Error (1503, loc,
+                                     String.Format ("Argument {0}: Cannot convert from '{1}' to '{2}'",
+                                                    idx, arg_sig, par_desc));
+               }
+               
                public static bool VerifyArgumentsCompat (EmitContext ec, ArrayList Arguments,
                                                          int argument_count,
                                                          MethodBase method, 
@@ -4064,33 +4479,37 @@ namespace Mono.CSharp {
                                Argument a = (Argument) Arguments [j];
                                Expression a_expr = a.Expr;
                                Type parameter_type = pd.ParameterType (j);
+                               Parameter.Modifier pm = pd.ParameterModifier (j);
+                               
+                               if (pm == Parameter.Modifier.PARAMS){
+                                       if (chose_params_expanded)
+                                               parameter_type = TypeManager.TypeToCoreType (parameter_type.GetElementType ());
+                               } else {
+                                       //
+                                       // Check modifiers
+                                       //
+                                       if (pd.ParameterModifier (j) != a.GetParameterModifier ()){
+                                               if (!Location.IsNull (loc))
+                                                       Error_InvalidArguments (
+                                                               loc, j, method, delegate_type,
+                                                               Argument.FullDesc (a), pd.ParameterDesc (j));
+                                               return false;
+                                       }
+                               }
 
-                               if (pd.ParameterModifier (j) == Parameter.Modifier.PARAMS &&
-                                   chose_params_expanded)
-                                       parameter_type = TypeManager.TypeToCoreType (parameter_type.GetElementType ());
-
+                               //
+                               // Check Type
+                               //
                                if (a.Type != parameter_type){
                                        Expression conv;
                                        
-                                       conv = ConvertImplicit (ec, a_expr, parameter_type, loc);
+                                       conv = Convert.ImplicitConversion (ec, a_expr, parameter_type, loc);
 
                                        if (conv == null) {
-                                               if (!Location.IsNull (loc)) {
-                                                       if (delegate_type == null) 
-                                                               Report.Error (1502, loc,
-                                                                      "The best overloaded match for method '" +
-                                                                      FullMethodDesc (method) +
-                                                                      "' has some invalid arguments");
-                                                       else
-                                                               Report.Error (1594, loc,
-                                                                             "Delegate '" + delegate_type.ToString () +
-                                                                             "' has some invalid arguments.");
-                                                       Report.Error (1503, loc,
-                                                        "Argument " + (j+1) +
-                                                        ": Cannot convert from '" + Argument.FullDesc (a) 
-                                                        + "' to '" + pd.ParameterDesc (j) + "'");
-                                               }
-                                               
+                                               if (!Location.IsNull (loc)) 
+                                                       Error_InvalidArguments (
+                                                               loc, j, method, delegate_type,
+                                                               Argument.FullDesc (a), pd.ParameterDesc (j));
                                                return false;
                                        }
                                        
@@ -4128,7 +4547,7 @@ namespace Mono.CSharp {
 
                        return true;
                }
-               
+
                public override Expression DoResolve (EmitContext ec)
                {
                        //
@@ -4138,6 +4557,8 @@ namespace Mono.CSharp {
                        if (expr is BaseAccess)
                                is_base = true;
 
+                       Expression old = expr;
+                       
                        expr = expr.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
                        if (expr == null)
                                return null;
@@ -4154,7 +4575,7 @@ namespace Mono.CSharp {
                        }
 
                        if (!(expr is MethodGroupExpr)){
-                               expr.Error118 (ResolveFlags.MethodGroup);
+                               expr.Error_UnexpectedKind (ResolveFlags.MethodGroup);
                                return null;
                        }
 
@@ -4200,6 +4621,11 @@ namespace Mono.CSharp {
                                return null;
                        }
 
+                       if ((method.Attributes & MethodAttributes.SpecialName) != 0){
+                               if (TypeManager.IsSpecialMethod (method))
+                                       Report.Error (571, loc, method.Name + ": can not call operator or accessor");
+                       }
+                       
                        eclass = ExprClass.Value;
                        return this;
                }
@@ -4426,8 +4852,7 @@ namespace Mono.CSharp {
                {
                        MethodGroupExpr mg = (MethodGroupExpr) this.expr;
 
-                       EmitCall (
-                               ec, is_base, method.IsStatic, mg.InstanceExpression, method, Arguments, loc);
+                       EmitCall (ec, is_base, method.IsStatic, mg.InstanceExpression, method, Arguments, loc);
                }
                
                public override void EmitStatement (EmitContext ec)
@@ -4459,7 +4884,7 @@ namespace Mono.CSharp {
        /// <summary>
        ///    Implements the new expression 
        /// </summary>
-       public class New : ExpressionStatement {
+       public class New : ExpressionStatement, IMemoryLocation {
                public readonly ArrayList Arguments;
                public readonly Expression RequestedType;
 
@@ -4516,9 +4941,20 @@ namespace Mono.CSharp {
                        // instead of Call + Push-temp on value types.
 //                     value_target = MyEmptyExpression;
                }
-               
+
                public override Expression DoResolve (EmitContext ec)
                {
+                       //
+                       // The New DoResolve might be called twice when initializing field
+                       // expressions (see EmitFieldInitializers, the call to
+                       // GetInitializerExpression will perform a resolve on the expression,
+                       // and later the assign will trigger another resolution
+                       //
+                       // This leads to bugs (#37014)
+                       //
+                       if (type != null)
+                               return this;
+                       
                        type = ec.DeclSpace.ResolveType (RequestedType, false, loc);
                        
                        if (type == null)
@@ -4530,9 +4966,7 @@ namespace Mono.CSharp {
                                return (new NewDelegate (type, Arguments, loc)).Resolve (ec);
 
                        if (type.IsInterface || type.IsAbstract){
-                               Error (
-                                       144, "It is not possible to create instances of interfaces " +
-                                       "or abstract classes");
+                               Error (144, "It is not possible to create instances of interfaces or abstract classes");
                                return null;
                        }
                        
@@ -4557,7 +4991,7 @@ namespace Mono.CSharp {
                        
                        if (! (ml is MethodGroupExpr)){
                                if (!is_struct){
-                                       ml.Error118 ("method group");
+                                       ml.Error_UnexpectedKind ("method group");
                                        return null;
                                }
                        }
@@ -4577,10 +5011,10 @@ namespace Mono.CSharp {
 
                        if (method == null) { 
                                 if (!is_struct || Arguments.Count > 0) {
-                                       Error (1501,
-                                              "New invocation: Can not find a constructor for " +
-                                              "this argument list");
-                                       return null;
+                                       Error (1501, String.Format (
+                                           "New invocation: Can not find a constructor in `{0}' for this argument list",
+                                           TypeManager.CSharpName (type)));
+                                       return null;
                                 }
                        }
                        return this;
@@ -4657,6 +5091,34 @@ namespace Mono.CSharp {
                        if (DoEmit (ec, false))
                                ec.ig.Emit (OpCodes.Pop);
                }
+
+               public void AddressOf (EmitContext ec, AddressOp Mode)
+               {
+                       if (!type.IsValueType){
+                               //
+                               // We throw an exception.  So far, I believe we only need to support
+                               // value types:
+                               // foreach (int j in new StructType ())
+                               // see bug 42390
+                               //
+                               throw new Exception ("AddressOf should not be used for classes");
+                       }
+
+                       if (!value_target_set)
+                               value_target = new LocalTemporary (ec, type);
+                                       
+                       IMemoryLocation ml = (IMemoryLocation) value_target;
+                       ml.AddressOf (ec, AddressOp.Store);
+                       if (method != null)
+                               Invocation.EmitArguments (ec, method, Arguments);
+
+                       if (method == null)
+                               ec.ig.Emit (OpCodes.Initobj, type);
+                       else 
+                               ec.ig.Emit (OpCodes.Call, (ConstructorInfo) method);
+                       
+                       ((IMemoryLocation) value_target).AddressOf (ec, Mode);
+               }
        }
 
        /// <summary>
@@ -4788,6 +5250,11 @@ namespace Mono.CSharp {
                                                Error_IncorrectArrayInitializer ();
                                                return false;
                                        }
+                                       if (specified_dims && (idx + 1 >= arguments.Count)){
+                                               Error (623, "Array initializers can only be used in a variable or field initializer, try using the new expression");
+                                               return false;
+                                       }
+                                       
                                        bool ret = CheckIndices (ec, (ArrayList) o, idx + 1, specified_dims);
                                        if (!ret)
                                                return false;
@@ -4805,7 +5272,7 @@ namespace Mono.CSharp {
                                        // Console.WriteLine ("I got: " + tmp);
                                        // Handle initialization from vars, fields etc.
 
-                                       Expression conv = ConvertImplicitRequired (
+                                       Expression conv = Convert.ImplicitConversionRequired (
                                                ec, tmp, underlying_type, loc);
                                        
                                        if (conv == null) 
@@ -4905,15 +5372,15 @@ namespace Mono.CSharp {
                        bool old_checked = ec.CheckState;
                        ec.CheckState = true;
                        
-                       target = ConvertImplicit (ec, source, TypeManager.int32_type, loc);
+                       target = Convert.ImplicitConversion (ec, source, TypeManager.int32_type, loc);
                        if (target == null){
-                               target = ConvertImplicit (ec, source, TypeManager.uint32_type, loc);
+                               target = Convert.ImplicitConversion (ec, source, TypeManager.uint32_type, loc);
                                if (target == null){
-                                       target = ConvertImplicit (ec, source, TypeManager.int64_type, loc);
+                                       target = Convert.ImplicitConversion (ec, source, TypeManager.int64_type, loc);
                                        if (target == null){
-                                               target = ConvertImplicit (ec, source, TypeManager.uint64_type, loc);
+                                               target = Convert.ImplicitConversion (ec, source, TypeManager.uint64_type, loc);
                                                if (target == null)
-                                                       Expression.Error_CannotConvertImplicit (loc, source.Type, TypeManager.int32_type);
+                                                       Convert.Error_CannotImplicitConversion (loc, source.Type, TypeManager.int32_type);
                                        }
                                }
                        } 
@@ -5025,7 +5492,7 @@ namespace Mono.CSharp {
                                                   AllBindingFlags, loc);
                                
                                if (!(ml is MethodGroupExpr)) {
-                                       ml.Error118 ("method group");
+                                       ml.Error_UnexpectedKind ("method group");
                                        return null;
                                }
                                
@@ -5312,17 +5779,18 @@ namespace Mono.CSharp {
                                                                //
                                                                n.DisableTemporaryValueType ();
                                                        }
-                                                                            
+
                                                        ig.Emit (OpCodes.Ldelema, etype);
                                                }
 
                                                e.Emit (ec);
-                                               
-                                               if (dims == 1)
-                                                       ArrayAccess.EmitStoreOpcode (ig, array_element_type);
-                                               else 
-                                                       ig.Emit (OpCodes.Call, set);
-                                       }
+
+                                                if (dims == 1)
+                                                        ArrayAccess.EmitStoreOpcode (ig, array_element_type);
+                                                else 
+                                                        ig.Emit (OpCodes.Call, set);
+                                                
+                                        }
                                }
                                
                                //
@@ -5395,7 +5863,34 @@ namespace Mono.CSharp {
                {
                        DoEmit (ec, true);
                }
-               
+
+               public object EncodeAsAttribute ()
+               {
+                       if (!is_one_dimensional){
+                               Report.Error (-211, Location, "attribute can not encode multi-dimensional arrays");
+                               return null;
+                       }
+
+                       if (array_data == null){
+                               Report.Error (-212, Location, "array should be initialized when passing it to an attribute");
+                               return null;
+                       }
+                       
+                       object [] ret = new object [array_data.Count];
+                       int i = 0;
+                       foreach (Expression e in array_data){
+                               object v;
+                               
+                               if (e is NullLiteral)
+                                       v = null;
+                               else {
+                                       if (!Attribute.GetAttributeArgumentExpression (e, Location, out v))
+                                               return null;
+                               }
+                               ret [i++] = v;
+                       }
+                       return ret;
+               }
        }
        
        /// <summary>
@@ -5404,7 +5899,7 @@ namespace Mono.CSharp {
        public class This : Expression, IAssignMethod, IMemoryLocation, IVariable {
 
                Block block;
-               VariableInfo vi;
+               VariableInfo variable_info;
                
                public This (Block block, Location loc)
                {
@@ -5417,57 +5912,54 @@ namespace Mono.CSharp {
                        this.loc = loc;
                }
 
-               public bool IsAssigned (EmitContext ec, Location loc)
-               {
-                       if (vi == null)
-                               return true;
-
-                       return vi.IsAssigned (ec, loc);
+               public VariableInfo VariableInfo {
+                       get { return variable_info; }
                }
 
-               public bool IsFieldAssigned (EmitContext ec, string field_name, Location loc)
+               public bool ResolveBase (EmitContext ec)
                {
-                       if (vi == null)
-                               return true;
+                       eclass = ExprClass.Variable;
+                       type = ec.ContainerType;
 
-                       return vi.IsFieldAssigned (ec, field_name, loc);
-               }
+                       if (ec.IsStatic) {
+                               Error (26, "Keyword this not valid in static code");
+                               return false;
+                       }
 
-               public void SetAssigned (EmitContext ec)
-               {
-                       if (vi != null)
-                               vi.SetAssigned (ec);
-               }
+                       if ((block != null) && (block.ThisVariable != null))
+                               variable_info = block.GetVariableInfo (block.ThisVariable);
 
-               public void SetFieldAssigned (EmitContext ec, string field_name)
-               {       
-                       if (vi != null)
-                               vi.SetFieldAssigned (ec, field_name);
+                       return true;
                }
 
                public override Expression DoResolve (EmitContext ec)
                {
-                       eclass = ExprClass.Variable;
-                       type = ec.ContainerType;
-
-                       if (ec.IsStatic){
-                               Error (26, "Keyword this not valid in static code");
+                       if (!ResolveBase (ec))
                                return null;
+
+                       if ((variable_info != null) && !variable_info.IsAssigned (ec)) {
+                               Error (188, "The this object cannot be used before all " +
+                                      "of its fields are assigned to");
+                               variable_info.SetAssigned (ec);
+                               return this;
                        }
 
-                       if (block != null)
-                               vi = block.ThisVariable;
+                       if (ec.IsFieldInitializer) {
+                               Error (27, "Keyword `this' can't be used outside a constructor, " +
+                                      "a method or a property.");
+                               return null;
+                       }
 
                        return this;
                }
 
                override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
                {
-                       DoResolve (ec);
+                       if (!ResolveBase (ec))
+                               return null;
 
-                       VariableInfo vi = ec.CurrentBlock.ThisVariable;
-                       if (vi != null)
-                               vi.SetAssigned (ec);
+                       if (variable_info != null)
+                               variable_info.SetAssigned (ec);
                        
                        if (ec.TypeContainer is Class){
                                Error (1604, "Cannot assign to `this'");
@@ -5567,8 +6059,9 @@ namespace Mono.CSharp {
                public override Expression DoResolve (EmitContext ec)
                {
                        if (!ec.InUnsafe) {
-                               Error (233, "Sizeof may only be used in an unsafe context " +
-                                      "(consider using System.Runtime.InteropServices.Marshal.Sizeof");
+                               Report.Error (
+                                       233, loc, "Sizeof may only be used in an unsafe context " +
+                                       "(consider using System.Runtime.InteropServices.Marshal.Sizeof");
                                return null;
                        }
                                
@@ -5577,7 +6070,7 @@ namespace Mono.CSharp {
                                return null;
 
                        if (!TypeManager.IsUnmanagedType (type_queried)){
-                               Report.Error (208, "Cannot take the size of an unmanaged type (" + TypeManager.CSharpName (type_queried) + ")");
+                               Report.Error (208, loc, "Cannot take the size of an unmanaged type (" + TypeManager.CSharpName (type_queried) + ")");
                                return null;
                        }
                        
@@ -5600,10 +6093,9 @@ namespace Mono.CSharp {
        /// <summary>
        ///   Implements the member access expression
        /// </summary>
-       public class MemberAccess : Expression, ITypeExpression {
+       public class MemberAccess : Expression {
                public readonly string Identifier;
                Expression expr;
-               Expression member_lookup;
                
                public MemberAccess (Expression expr, string id, Location l)
                {
@@ -5672,7 +6164,10 @@ namespace Mono.CSharp {
                                        Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
                                        
                                        if (c != null) {
-                                               object o = c.LookupConstantValue (ec);
+                                               object o = c.LookupConstantValue ();
+                                               if (o == null)
+                                                       return null;
+                                               
                                                object real_value = ((Constant) c.Expr).GetValue ();
 
                                                return Constantify (real_value, fi.FieldType);
@@ -5814,11 +6309,6 @@ namespace Mono.CSharp {
                                return member_lookup;
                        }
 
-                       if (member_lookup is TypeExpr){
-                               member_lookup.Resolve (ec, ResolveFlags.Type);
-                               return member_lookup;
-                       }
-                       
                        Console.WriteLine ("Left is: " + left);
                        Report.Error (-100, loc, "Support for [" + member_lookup + "] is not present yet");
                        Environment.Exit (0);
@@ -5829,6 +6319,7 @@ namespace Mono.CSharp {
                {
                        if (type != null)
                                throw new Exception ();
+
                        //
                        // Resolve the expression with flow analysis turned off, we'll do the definite
                        // assignment checks later.  This is because we don't know yet what the expression
@@ -5838,14 +6329,13 @@ namespace Mono.CSharp {
 
                        Expression original = expr;
                        expr = expr.Resolve (ec, flags | ResolveFlags.DisableFlowAnalysis);
-
                        if (expr == null)
                                return null;
 
                        if (expr is SimpleName){
                                SimpleName child_expr = (SimpleName) expr;
-                               
-                               Expression new_expr = new SimpleName (child_expr.Name + "." + Identifier, loc);
+
+                               Expression new_expr = new SimpleName (child_expr.Name, Identifier, loc);
 
                                return new_expr.Resolve (ec, flags);
                        }
@@ -5862,35 +6352,40 @@ namespace Mono.CSharp {
                        int errors = Report.Errors;
                        
                        Type expr_type = expr.Type;
-                       if ((expr is TypeExpr) && (expr_type.IsSubclassOf (TypeManager.enum_type))){
-                               
-                               Enum en = TypeManager.LookupEnum (expr_type);
-                               
-                               if (en != null) {
-                                       object value = en.LookupEnumValue (ec, Identifier, loc);
-
-                                       if (value != null){
-                                               Constant c = Constantify (value, en.UnderlyingType);
-                                               return new EnumConstant (c, expr_type);
+                       if (expr is TypeExpr){
+                               if (!ec.DeclSpace.CheckAccessLevel (expr_type)){
+                                       Error (122, "`" + expr_type + "' " +
+                                              "is inaccessible because of its protection level");
+                                       return null;
+                               }
+                                   
+                               if (expr_type == TypeManager.enum_type || expr_type.IsSubclassOf (TypeManager.enum_type)){
+                                       Enum en = TypeManager.LookupEnum (expr_type);
+                                       
+                                       if (en != null) {
+                                               object value = en.LookupEnumValue (ec, Identifier, loc);
+                                               
+                                               if (value != null){
+                                                       Constant c = Constantify (value, en.UnderlyingType);
+                                                       return new EnumConstant (c, expr_type);
+                                               }
                                        }
                                }
                        }
-
+                       
                        if (expr_type.IsPointer){
                                Error (23, "The `.' operator can not be applied to pointer operands (" +
                                       TypeManager.CSharpName (expr_type) + ")");
                                return null;
                        }
 
+                       Expression member_lookup;
                        member_lookup = MemberLookupFinal (ec, expr_type, expr_type, Identifier, loc);
                        if (member_lookup == null)
                                return null;
 
-                       if (member_lookup is TypeExpr){
-                               member_lookup.Resolve (ec, ResolveFlags.Type);
+                       if (member_lookup is TypeExpr)
                                return member_lookup;
-                       } else if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type)
-                               return null;
                        
                        member_lookup = ResolveMemberAccess (ec, member_lookup, expr, loc, original);
                        if (member_lookup == null)
@@ -5919,9 +6414,40 @@ namespace Mono.CSharp {
                                          ResolveFlags.SimpleName | ResolveFlags.Type);
                }
 
-               public Expression DoResolveType (EmitContext ec)
+               public override Expression ResolveAsTypeStep (EmitContext ec)
                {
-                       return DoResolve (ec, null, ResolveFlags.Type);
+                       Expression new_expr = expr.ResolveAsTypeStep (ec);
+
+                       if (new_expr == null)
+                               return null;
+
+                       if (new_expr is SimpleName){
+                               SimpleName child_expr = (SimpleName) new_expr;
+                               
+                               new_expr = new SimpleName (child_expr.Name, Identifier, loc);
+
+                               return new_expr.ResolveAsTypeStep (ec);
+                       }
+
+                       Type expr_type = new_expr.Type;
+                     
+                       if (expr_type.IsPointer){
+                               Error (23, "The `.' operator can not be applied to pointer operands (" +
+                                      TypeManager.CSharpName (expr_type) + ")");
+                               return null;
+                       }
+                       
+                       Expression member_lookup;
+                       member_lookup = MemberLookupFinal (ec, expr_type, expr_type, Identifier, loc);
+                       if (member_lookup == null)
+                               return null;
+
+                       if (member_lookup is TypeExpr){
+                               member_lookup.Resolve (ec, ResolveFlags.Type);
+                               return member_lookup;
+                       } 
+
+                       return null;                    
                }
 
                public override void Emit (EmitContext ec)
@@ -5950,10 +6476,13 @@ namespace Mono.CSharp {
 
                public override Expression DoResolve (EmitContext ec)
                {
+                       bool last_check = ec.CheckState;
                        bool last_const_check = ec.ConstantCheckState;
 
+                       ec.CheckState = true;
                        ec.ConstantCheckState = true;
                        Expr = Expr.Resolve (ec);
+                       ec.CheckState = last_check;
                        ec.ConstantCheckState = last_const_check;
                        
                        if (Expr == null)
@@ -5996,10 +6525,13 @@ namespace Mono.CSharp {
 
                public override Expression DoResolve (EmitContext ec)
                {
+                       bool last_check = ec.CheckState;
                        bool last_const_check = ec.ConstantCheckState;
 
+                       ec.CheckState = false;
                        ec.ConstantCheckState = false;
                        Expr = Expr.Resolve (ec);
+                       ec.CheckState = last_check;
                        ec.ConstantCheckState = last_const_check;
 
                        if (Expr == null)
@@ -6031,7 +6563,7 @@ namespace Mono.CSharp {
        ///   An Element Access expression.
        ///
        ///   During semantic analysis these are transformed into 
-       ///   IndexerAccess or ArrayAccess 
+       ///   IndexerAccess, ArrayAccess or a PointerArithmetic.
        /// </summary>
        public class ElementAccess : Expression {
                public ArrayList  Arguments;
@@ -6075,19 +6607,16 @@ namespace Mono.CSharp {
                        Type t = Expr.Type;
 
                        if (t == TypeManager.void_ptr_type){
-                               Error (
-                                       242,
-                                       "The array index operation is not valid for void pointers");
+                               Error (242, "The array index operation is not valid for void pointers");
                                return null;
                        }
                        if (Arguments.Count != 1){
-                               Error (
-                                       196,
-                                       "A pointer must be indexed by a single value");
+                               Error (196, "A pointer must be indexed by a single value");
                                return null;
                        }
-                       Expression p = new PointerArithmetic (true, Expr, ((Argument)Arguments [0]).Expr,
-                                                             t, loc);
+                       Expression p;
+
+                       p = new PointerArithmetic (true, Expr, ((Argument)Arguments [0]).Expr, t, loc);
                        return new Indirection (p, loc);
                }
                
@@ -6158,7 +6687,7 @@ namespace Mono.CSharp {
                        // As long as the type is valid
                        if (!(eclass == ExprClass.Variable || eclass == ExprClass.PropertyAccess ||
                              eclass == ExprClass.Value)) {
-                               ea.Expr.Error118 ("variable or value");
+                               ea.Expr.Error_UnexpectedKind ("variable or value");
                                return null;
                        }
 #endif
@@ -6257,6 +6786,7 @@ namespace Mono.CSharp {
                /// </summary>
                static public OpCode GetStoreOpcode (Type t, out bool is_stobj)
                {
+                       //Console.WriteLine (new System.Diagnostics.StackTrace ());
                        is_stobj = false;
                        t = TypeManager.TypeToCoreType (t);
                        if (TypeManager.IsEnumType (t) && t != TypeManager.enum_type)
@@ -6275,9 +6805,10 @@ namespace Mono.CSharp {
                                return OpCodes.Stelem_R4;
                        else if (t == TypeManager.double_type)
                                return OpCodes.Stelem_R8;
-                       else if (t == TypeManager.intptr_type)
-                               return OpCodes.Stelem_I;
-                       else if (t.IsValueType) {
+                       else if (t == TypeManager.intptr_type) {
+                                is_stobj = true;
+                                return OpCodes.Stobj;
+                       } else if (t.IsValueType) {
                                is_stobj = true;
                                return OpCodes.Stobj;
                        } else
@@ -6311,19 +6842,10 @@ namespace Mono.CSharp {
                        int arg_count = ea.Arguments.Count;
                        Type [] args = new Type [arg_count];
                        MethodInfo address;
-                       string ptr_type_name;
                        Type ret_type;
                        
-                       ptr_type_name = type.FullName + "&";
-                       ret_type = Type.GetType (ptr_type_name);
+                       ret_type = TypeManager.GetReferenceType (type);
                        
-                       //
-                       // It is a type defined by the source code we are compiling
-                       //
-                       if (ret_type == null){
-                               ret_type = mb.GetType (ptr_type_name);
-                       }
-
                        for (int i = 0; i < arg_count; i++){
                                //args [i++] = a.Type;
                                args [i] = TypeManager.int32_type;
@@ -6479,7 +7001,7 @@ namespace Mono.CSharp {
 
        
        class Indexers {
-               public ArrayList getters, setters;
+               public ArrayList properties;
                static Hashtable map;
 
                static Indexers ()
@@ -6487,35 +7009,24 @@ namespace Mono.CSharp {
                        map = new Hashtable ();
                }
 
-               Indexers (MemberInfo [] mi)
+               Indexers ()
+               {
+                       properties = new ArrayList ();
+               }
+                               
+               void Append (MemberInfo [] mi)
                {
                        foreach (PropertyInfo property in mi){
                                MethodInfo get, set;
                                
                                get = property.GetGetMethod (true);
-                               if (get != null){
-                                       if (getters == null)
-                                               getters = new ArrayList ();
-
-                                       getters.Add (get);
-                               }
-                               
                                set = property.GetSetMethod (true);
-                               if (set != null){
-                                       if (setters == null)
-                                               setters = new ArrayList ();
-                                       setters.Add (set);
-                               }
+                               properties.Add (new Pair (get, set));
                        }
                }
 
-               static private Indexers GetIndexersForTypeOrInterface (Type caller_type, Type lookup_type)
+               static private MemberInfo [] GetIndexersForTypeOrInterface (Type caller_type, Type lookup_type)
                {
-                       Indexers ix = (Indexers) map [lookup_type];
-                       
-                       if (ix != null)
-                               return ix;
-
                        string p_name = TypeManager.IndexerPropertyName (lookup_type);
 
                        MemberInfo [] mi = TypeManager.MemberLookup (
@@ -6526,10 +7037,7 @@ namespace Mono.CSharp {
                        if (mi == null || mi.Length == 0)
                                return null;
 
-                       ix = new Indexers (mi);
-                       map [lookup_type] = ix;
-
-                       return ix;
+                       return mi;
                }
                
                static public Indexers GetIndexersForType (Type caller_type, Type lookup_type, Location loc) 
@@ -6539,20 +7047,34 @@ namespace Mono.CSharp {
                        if (ix != null)
                                return ix;
 
-                       ix = GetIndexersForTypeOrInterface (caller_type, lookup_type);
-                       if (ix != null)
-                               return ix;
+                       Type copy = lookup_type;
+                       while (copy != TypeManager.object_type && copy != null){
+                               MemberInfo [] mi = GetIndexersForTypeOrInterface (caller_type, copy);
+
+                               if (mi != null){
+                                       if (ix == null)
+                                               ix = new Indexers ();
+
+                                       ix.Append (mi);
+                               }
+                                       
+                               copy = copy.BaseType;
+                       }
 
                        Type [] ifaces = TypeManager.GetInterfaces (lookup_type);
                        if (ifaces != null) {
                                foreach (Type itype in ifaces) {
-                                       ix = GetIndexersForTypeOrInterface (caller_type, itype);
-                                       if (ix != null)
-                                               return ix;
+                                       MemberInfo [] mi = GetIndexersForTypeOrInterface (caller_type, itype);
+                                       if (mi != null){
+                                               if (ix == null)
+                                                       ix = new Indexers ();
+                                       
+                                               ix.Append (mi);
+                                       }
                                }
                        }
 
-                       return null;
+                       return ix;
                }
        }
 
@@ -6564,7 +7086,6 @@ namespace Mono.CSharp {
                // Points to our "data" repository
                //
                MethodInfo get, set;
-               Indexers ilist;
                ArrayList set_arguments;
                bool is_base_indexer;
 
@@ -6598,6 +7119,7 @@ namespace Mono.CSharp {
 
                public override Expression DoResolve (EmitContext ec)
                {
+                       ArrayList AllGetters = new ArrayList();
                        if (!CommonResolve (ec))
                                return null;
 
@@ -6609,29 +7131,23 @@ namespace Mono.CSharp {
 
                        bool found_any = false, found_any_getters = false;
                        Type lookup_type = indexer_type;
-                       while (lookup_type != null) {
-                               ilist = Indexers.GetIndexersForType (current_type, lookup_type, loc);
-
-                               if (ilist == null) {
-                                       lookup_type = lookup_type.BaseType;
-                                       continue;
-                               }
 
+                       Indexers ilist;
+                       ilist = Indexers.GetIndexersForType (current_type, lookup_type, loc);
+                       if (ilist != null) {
                                found_any = true;
-
-                               //
-                               // Step 2: find the proper match
-                               //
-                               if (ilist.getters != null && ilist.getters.Count > 0) {
-                                       found_any_getters = true;
-                                       get = (MethodInfo) Invocation.OverloadResolve (
-                                               ec, new MethodGroupExpr (ilist.getters, loc), arguments, loc);
-
-                                       if (get != null)
-                                               break;
+                               if (ilist.properties != null) {
+                                       foreach (Pair o in ilist.properties) {
+                                               if (o.First != null)
+                                                       AllGetters.Add(o.First);
+                                       }
                                }
+                       }
 
-                               lookup_type = lookup_type.BaseType;
+                       if (AllGetters.Count > 0) {
+                               found_any_getters = true;
+                               get = (MethodInfo) Invocation.OverloadResolve (
+                                       ec, new MethodGroupExpr (AllGetters, loc), arguments, loc);
                        }
 
                        if (!found_any) {
@@ -6673,37 +7189,31 @@ namespace Mono.CSharp {
 
                public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
                {
+                       ArrayList AllSetters = new ArrayList();
                        if (!CommonResolve (ec))
                                return null;
 
                        Type right_type = right_side.Type;
 
                        bool found_any = false, found_any_setters = false;
-                       Type lookup_type = indexer_type;
-                       while (lookup_type != null) {
-                               ilist = Indexers.GetIndexersForType (current_type, lookup_type, loc);
-
-                               if (ilist == null) {
-                                       lookup_type = lookup_type.BaseType;
-                                       continue;
-                               }
 
+                       Indexers ilist = Indexers.GetIndexersForType (current_type, indexer_type, loc);
+                       if (ilist != null) {
                                found_any = true;
-
-                               if (ilist.setters != null && ilist.setters.Count > 0) {
-                                       found_any_setters = true;
-
-                                       set_arguments = (ArrayList) arguments.Clone ();
-                                       set_arguments.Add (new Argument (right_side, Argument.AType.Expression));
-                                       set = (MethodInfo) Invocation.OverloadResolve (
-                                               ec, new MethodGroupExpr (ilist.setters, loc), set_arguments, loc);
-
-                                       if (set != null)
-                                               break;
+                               if (ilist.properties != null) {
+                                       foreach (Pair o in ilist.properties) {
+                                               if (o.Second != null)
+                                                       AllSetters.Add(o.Second);
+                                       }
                                }
-
-
-                               lookup_type = lookup_type.BaseType;
+                       }
+                       if (AllSetters.Count > 0) {
+                               found_any_setters = true;
+                               set_arguments = (ArrayList) arguments.Clone ();
+                               set_arguments.Add (new Argument (right_side, Argument.AType.Expression));
+                               set = (MethodInfo) Invocation.OverloadResolve (
+                                       ec, new MethodGroupExpr (AllSetters, loc),
+                                       set_arguments, loc);
                        }
 
                        if (!found_any) {
@@ -6732,14 +7242,26 @@ namespace Mono.CSharp {
                                Report.Error (205, loc, "Cannot call an abstract base indexer: " + Invocation.FullMethodDesc (set));
                                return null;
                        }
-                       type = TypeManager.void_type;
+
+                       //
+                       // Now look for the actual match in the list of indexers to set our "return" type
+                       //
+                       type = TypeManager.void_type;   // default value
+                       foreach (Pair t in ilist.properties){
+                               if (t.Second == set){
+                                       if (t.First != null)
+                                               type = ((MethodInfo) t.First).ReturnType;
+                                       break;
+                               }
+                       }
+                       
                        eclass = ExprClass.IndexerAccess;
                        return this;
                }
                
                public override void Emit (EmitContext ec)
                {
-                       Invocation.EmitCall (ec, false, false, instance_expr, get, arguments, loc);
+                       Invocation.EmitCall (ec, is_base_indexer, false, instance_expr, get, arguments, loc);
                }
 
                //
@@ -6749,7 +7271,7 @@ namespace Mono.CSharp {
                //
                public void EmitAssign (EmitContext ec, Expression source)
                {
-                       Invocation.EmitCall (ec, false, false, instance_expr, set, set_arguments, loc);
+                       Invocation.EmitCall (ec, is_base_indexer, false, instance_expr, set, set_arguments, loc);
                }
        }
 
@@ -6905,7 +7427,7 @@ namespace Mono.CSharp {
                //
                // This is just because we might want to reuse this bad boy
                // instead of creating gazillions of EmptyExpressions.
-               // (CanConvertImplicit uses it)
+               // (CanImplicitConversion uses it)
                //
                public void SetType (Type t)
                {
@@ -6954,7 +7476,7 @@ namespace Mono.CSharp {
        //   the type specification, we just use this to construct the type
        //   one bit at a time.
        // </summary>
-       public class ComposedCast : Expression, ITypeExpression {
+       public class ComposedCast : Expression {
                Expression left;
                string dim;
                
@@ -6965,7 +7487,7 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
-               public Expression DoResolveType (EmitContext ec)
+               public override Expression ResolveAsTypeStep (EmitContext ec)
                {
                        Type ltype = ec.DeclSpace.ResolveType (left, false, loc);
                        if (ltype == null)
@@ -7008,7 +7530,7 @@ namespace Mono.CSharp {
 
                public override Expression DoResolve (EmitContext ec)
                {
-                       return DoResolveType (ec);
+                       return ResolveAsTypeStep (ec);
                }
 
                public override void Emit (EmitContext ec)
@@ -7034,16 +7556,8 @@ namespace Mono.CSharp {
                        Type array_type = array.Type.GetElementType ();
 
                        this.array = array;
-                       
-                       string array_ptr_type_name = array_type.FullName + "*";
-                       
-                       type = Type.GetType (array_ptr_type_name);
-                       if (type == null){
-                               ModuleBuilder mb = CodeGen.ModuleBuilder;
-                               
-                               type = mb.GetType (array_ptr_type_name);
-                       }
 
+                       type = TypeManager.GetPointerType (array_type);
                        eclass = ExprClass.Value;
                        loc = l;
                }
@@ -7121,7 +7635,7 @@ namespace Mono.CSharp {
                                return null;
                        
                        if (count.Type != TypeManager.int32_type){
-                               count = ConvertImplicitRequired (ec, count, TypeManager.int32_type, loc);
+                               count = Convert.ImplicitConversionRequired (ec, count, TypeManager.int32_type, loc);
                                if (count == null)
                                        return null;
                        }
@@ -7140,13 +7654,7 @@ namespace Mono.CSharp {
                        if (!TypeManager.VerifyUnManaged (otype, loc))
                                return null;
 
-                       string ptr_name = otype.FullName + "*";
-                       type = Type.GetType (ptr_name);
-                       if (type == null){
-                               ModuleBuilder mb = CodeGen.ModuleBuilder;
-                               
-                               type = mb.GetType (ptr_name);
-                       }
+                       type = TypeManager.GetPointerType (otype);
                        eclass = ExprClass.Value;
 
                        return this;