merge -r 58060:58217
[mono.git] / mcs / gmcs / expression.cs
index cdc6f98d2b41451448a04e160e24878f400fdd26..3985cd5818cf8a0f8661dba3622bf71368784743 100644 (file)
@@ -3,6 +3,7 @@
 //
 // Author:
 //   Miguel de Icaza (miguel@ximian.com)
+//   Marek Safar (marek.safar@seznam.cz)
 //
 // (C) 2001, 2002, 2003 Ximian, Inc.
 // (C) 2003, 2004 Novell, Inc.
@@ -91,10 +92,9 @@ namespace Mono.CSharp {
        {
                public Expression Expr;
 
-               public ParenthesizedExpression (Expression expr, Location loc)
+               public ParenthesizedExpression (Expression expr)
                {
                        this.Expr = expr;
-                       this.loc = loc;
                }
 
                public override Expression DoResolve (EmitContext ec)
@@ -107,6 +107,13 @@ namespace Mono.CSharp {
                {
                        throw new Exception ("Should not happen");
                }
+
+               public override Location Location
+               {
+                       get {
+                               return Expr.Location;
+                       }
+               }
        }
        
        /// <summary>
@@ -188,37 +195,37 @@ namespace Mono.CSharp {
                        Expression e = null;
 
                        if (expr is IntConstant)
-                               e = new IntConstant (-((IntConstant) expr).Value);
+                               e = new IntConstant (-((IntConstant) expr).Value, expr.Location);
                        else if (expr is UIntConstant){
                                uint value = ((UIntConstant) expr).Value;
 
                                if (value < 2147483649)
-                                       return new IntConstant (-(int)value);
+                                       return new IntConstant (-(int)value, expr.Location);
                                else
-                                       e = new LongConstant (-value);
+                                       e = new LongConstant (-value, expr.Location);
                        }
                        else if (expr is LongConstant)
-                               e = new LongConstant (-((LongConstant) expr).Value);
+                               e = new LongConstant (-((LongConstant) expr).Value, expr.Location);
                        else if (expr is ULongConstant){
                                ulong value = ((ULongConstant) expr).Value;
 
                                if (value < 9223372036854775809)
-                                       return new LongConstant(-(long)value);
+                                       return new LongConstant(-(long)value, expr.Location);
                        }
                        else if (expr is FloatConstant)
-                               e = new FloatConstant (-((FloatConstant) expr).Value);
+                               e = new FloatConstant (-((FloatConstant) expr).Value, expr.Location);
                        else if (expr is DoubleConstant)
-                               e = new DoubleConstant (-((DoubleConstant) expr).Value);
+                               e = new DoubleConstant (-((DoubleConstant) expr).Value, expr.Location);
                        else if (expr is DecimalConstant)
-                               e = new DecimalConstant (-((DecimalConstant) expr).Value);
+                               e = new DecimalConstant (-((DecimalConstant) expr).Value, expr.Location);
                        else if (expr is ShortConstant)
-                               e = new IntConstant (-((ShortConstant) expr).Value);
+                               e = new IntConstant (-((ShortConstant) expr).Value, expr.Location);
                        else if (expr is UShortConstant)
-                               e = new IntConstant (-((UShortConstant) expr).Value);
+                               e = new IntConstant (-((UShortConstant) expr).Value, expr.Location);
                        else if (expr is SByteConstant)
-                               e = new IntConstant (-((SByteConstant) expr).Value);
+                               e = new IntConstant (-((SByteConstant) expr).Value, expr.Location);
                        else if (expr is ByteConstant)
-                               e = new IntConstant (-((ByteConstant) expr).Value);
+                               e = new IntConstant (-((ByteConstant) expr).Value, expr.Location);
                        return e;
                }
 
@@ -255,7 +262,7 @@ namespace Mono.CSharp {
                                }
                                
                                BoolConstant b = (BoolConstant) e;
-                               result = new BoolConstant (!(b.Value));
+                               result = new BoolConstant (!(b.Value), b.Location);
                                return true;
                                
                        case Operator.OnesComplement:
@@ -304,13 +311,13 @@ namespace Mono.CSharp {
                                }
 
                                if (expr_type == TypeManager.int32_type){
-                                       result = new IntConstant (~ ((IntConstant) e).Value);
+                                       result = new IntConstant (~ ((IntConstant) e).Value, e.Location);
                                } else if (expr_type == TypeManager.uint32_type){
-                                       result = new UIntConstant (~ ((UIntConstant) e).Value);
+                                       result = new UIntConstant (~ ((UIntConstant) e).Value, e.Location);
                                } else if (expr_type == TypeManager.int64_type){
-                                       result = new LongConstant (~ ((LongConstant) e).Value);
+                                       result = new LongConstant (~ ((LongConstant) e).Value, e.Location);
                                } else if (expr_type == TypeManager.uint64_type){
-                                       result = new ULongConstant (~ ((ULongConstant) e).Value);
+                                       result = new ULongConstant (~ ((ULongConstant) e).Value, e.Location);
                                } else {
                                        result = null;
                                        Error23 (expr_type);
@@ -394,28 +401,24 @@ namespace Mono.CSharp {
                                        Expression e;
 
                                        e = Convert.ImplicitConversion (ec, Expr, TypeManager.int32_type, loc);
-                                       if (e != null){
-                                               type = TypeManager.int32_type;
-                                               return this;
-                                       }
+                                       if (e != null)
+                                               goto ok;
                                        e = Convert.ImplicitConversion (ec, Expr, TypeManager.uint32_type, loc);
-                                       if (e != null){
-                                               type = TypeManager.uint32_type;
-                                               return this;
-                                       }
+                                       if (e != null)
+                                               goto ok;
                                        e = Convert.ImplicitConversion (ec, Expr, TypeManager.int64_type, loc);
-                                       if (e != null){
-                                               type = TypeManager.int64_type;
-                                               return this;
-                                       }
+                                       if (e != null)
+                                               goto ok;
                                        e = Convert.ImplicitConversion (ec, Expr, TypeManager.uint64_type, loc);
-                                       if (e != null){
-                                               type = TypeManager.uint64_type;
-                                               return this;
-                                       }
+                                       if (e != null)
+                                               goto ok;
                                        Error23 (expr_type);
                                        return null;
+                               ok:
+                                       Expr = e;
+                                       expr_type = e.Type;
                                }
+
                                type = expr_type;
                                return this;
 
@@ -1031,7 +1034,7 @@ namespace Mono.CSharp {
        public abstract class Probe : Expression {
                public Expression ProbeType;
                protected Expression expr;
-               protected Type probe_type;
+               protected TypeExpr probe_type_expr;
                
                public Probe (Expression expr, Expression probe_type, Location l)
                {
@@ -1048,12 +1051,11 @@ namespace Mono.CSharp {
 
                public override Expression DoResolve (EmitContext ec)
                {
-                       TypeExpr texpr = ProbeType.ResolveAsTypeTerminal (ec);
-                       if (texpr == null)
+                       probe_type_expr = ProbeType.ResolveAsTypeTerminal (ec);
+                       if (probe_type_expr == null)
+                               return null;
+                       if (probe_type_expr.ResolveType (ec) == null)
                                return null;
-                       probe_type = texpr.Type;
-
-                       CheckObsoleteAttribute (probe_type);
 
                        expr = expr.Resolve (ec);
                        if (expr == null)
@@ -1105,7 +1107,7 @@ namespace Mono.CSharp {
                                ig.Emit (OpCodes.Ceq);
                                return;
                        case Action.Probe:
-                               ig.Emit (OpCodes.Isinst, probe_type);
+                               ig.Emit (OpCodes.Isinst, probe_type_expr.Type);
                                ig.Emit (OpCodes.Ldnull);
                                ig.Emit (OpCodes.Cgt_Un);
                                return;
@@ -1135,7 +1137,7 @@ namespace Mono.CSharp {
                                return;
                        case Action.Probe:
                                expr.Emit (ec);
-                               ig.Emit (OpCodes.Isinst, probe_type);
+                               ig.Emit (OpCodes.Isinst, probe_type_expr.Type);
                                ig.Emit (onTrue ? OpCodes.Brtrue : OpCodes.Brfalse, target);
                                return;
                        }
@@ -1160,6 +1162,7 @@ namespace Mono.CSharp {
                        // First case, if at compile time, there is an implicit conversion
                        // then e != null (objects) or true (value types)
                        //
+                       Type probe_type = probe_type_expr.Type;
                        e = Convert.ImplicitConversionStandard (ec, expr, probe_type, loc);
                        if (e != null && !(e is NullCast)){
                                expr = e;
@@ -1206,6 +1209,7 @@ namespace Mono.CSharp {
                }
 
                bool do_isinst = false;
+               Expression resolved_type;
                
                public override void Emit (EmitContext ec)
                {
@@ -1214,7 +1218,7 @@ namespace Mono.CSharp {
                        expr.Emit (ec);
 
                        if (do_isinst)
-                               ig.Emit (OpCodes.Isinst, probe_type);
+                               ig.Emit (OpCodes.Isinst, probe_type_expr.Type);
                }
 
                static void Error_CannotConvertType (Type source, Type target, Location loc)
@@ -1226,30 +1230,56 @@ namespace Mono.CSharp {
                
                public override Expression DoResolve (EmitContext ec)
                {
-                       Expression e = base.DoResolve (ec);
+                       if (resolved_type == null) {
+                               resolved_type = base.DoResolve (ec);
 
-                       if (e == null)
-                               return null;
+                               if (resolved_type == null)
+                                       return null;
+                       }
 
-                       type = probe_type;
+                       type = probe_type_expr.Type;
                        eclass = ExprClass.Value;
                        Type etype = expr.Type;
 
-                       if (TypeManager.IsValueType (probe_type)){
+                       if (type.IsValueType) {
                                Report.Error (77, loc, "The as operator must be used with a reference type (`" +
-                                             TypeManager.CSharpName (probe_type) + "' is a value type)");
+                                             TypeManager.CSharpName (type) + "' is a value type)");
                                return null;
                        
                        }
+
+                       //
+                       // If the type is a type parameter, ensure
+                       // that it is constrained by a class
+                       //
+                       TypeParameterExpr tpe = probe_type_expr as TypeParameterExpr;
+                       if (tpe != null){
+                               Constraints constraints = tpe.TypeParameter.Constraints;
+                               bool error = false;
+                               
+                               if (constraints == null)
+                                       error = true;
+                               else {
+                                       if (!constraints.HasClassConstraint)
+                                               if ((constraints.Attributes & GenericParameterAttributes.ReferenceTypeConstraint) == 0)
+                                                       error = true;
+                               }
+                               if (error){
+                                       Report.Error (413, loc,
+                                                     "The as operator requires that the `{0}' type parameter be constrained by a class",
+                                                     probe_type_expr.GetSignatureForError ());
+                                       return null;
+                               }
+                       }
                        
-                       e = Convert.ImplicitConversion (ec, expr, probe_type, loc);
+                       Expression e = Convert.ImplicitConversion (ec, expr, type, loc);
                        if (e != null){
                                expr = e;
                                do_isinst = false;
                                return this;
                        }
 
-                       if (Convert.ExplicitReferenceConversionExists (etype, probe_type)){
+                       if (Convert.ExplicitReferenceConversionExists (etype, type)){
                                if (etype.IsGenericParameter)
                                        expr = new BoxedCast (expr, etype);
 
@@ -1257,7 +1287,7 @@ namespace Mono.CSharp {
                                return this;
                        }
 
-                       Error_CannotConvertType (etype, probe_type, loc);
+                       Error_CannotConvertType (etype, type, loc);
                        return null;
                }                               
        }
@@ -1272,6 +1302,11 @@ namespace Mono.CSharp {
                Expression target_type;
                Expression expr;
                        
+               public Cast (Expression cast_type, Expression expr)
+                       : this (cast_type, expr, cast_type.Location)
+               {
+               }
+
                public Cast (Expression cast_type, Expression expr, Location loc)
                {
                        this.target_type = cast_type;
@@ -1293,496 +1328,6 @@ namespace Mono.CSharp {
                                expr = value;
                        }
                }
-
-               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)
-               {
-                       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 (!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)
-                                       return new UShortConstant ((ushort) 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)
-                                       return new CharConstant ((char) v);
-                               if (target_type == TypeManager.decimal_type)
-                                       return new DecimalConstant ((decimal) v);
-                       }
-                       if (real_expr is SByteConstant){
-                               sbyte v = ((SByteConstant) real_expr).Value;
-       
-                               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 (!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 (!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 (!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 (!CheckUnsigned (ec, v, target_type))
-                                               return null;
-                                       return new CharConstant ((char) v);
-                               }
-                               if (target_type == TypeManager.decimal_type)
-                                       return new DecimalConstant ((decimal) v);
-                       }
-                       if (real_expr is ShortConstant){
-                               short v = ((ShortConstant) 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.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 (!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 (!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 (!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 (real_expr is UShortConstant){
-                               ushort v = ((UShortConstant) 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);
-                       }
-                       if (real_expr is IntConstant){
-                               int v = ((IntConstant) 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.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 (!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 (!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 (!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 (real_expr is UIntConstant){
-                               uint v = ((UIntConstant) real_expr).Value;
-       
-                               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 (!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.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 (!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)
-                                       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);
-                       }
-                       if (real_expr is LongConstant){
-                               long v = ((LongConstant) 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.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 (!CheckRange (ec, v, target_type, Int32.MinValue, Int32.MaxValue))
-                                               return null;
-                                       return new IntConstant ((int) v);
-                               }
-                               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 (!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 (!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 (real_expr is ULongConstant){
-                               ulong v = ((ULongConstant) real_expr).Value;
-       
-                               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 (!CheckRange (ec, v, target_type, (ulong) SByte.MaxValue))
-                                               return null;
-                                       return new SByteConstant ((sbyte) v);
-                               }
-                               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 (!CheckRange (ec, v, target_type, UInt16.MaxValue))
-                                               return null;
-                                       return new UShortConstant ((ushort) v);
-                               }
-                               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 (!CheckRange (ec, v, target_type, UInt32.MaxValue))
-                                               return null;
-                                       return new UIntConstant ((uint) v);
-                               }
-                               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 (!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 (real_expr is FloatConstant){
-                               float v = ((FloatConstant) real_expr).Value;
-       
-                               if (target_type == TypeManager.byte_type)
-                                       return new ByteConstant ((byte) v);
-                               if (target_type == TypeManager.sbyte_type)
-                                       return new SByteConstant ((sbyte) v);
-                               if (target_type == TypeManager.short_type)
-                                       return new ShortConstant ((short) v);
-                               if (target_type == TypeManager.ushort_type)
-                                       return new UShortConstant ((ushort) 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.double_type)
-                                       return new DoubleConstant ((double) v);
-                               if (target_type == TypeManager.char_type)
-                                       return new CharConstant ((char) v);
-                               if (target_type == TypeManager.decimal_type)
-                                       return new DecimalConstant ((decimal) v);
-                       }
-                       if (real_expr is DoubleConstant){
-                               double v = ((DoubleConstant) real_expr).Value;
-       
-                               if (target_type == TypeManager.byte_type){
-                                       return new ByteConstant ((byte) v);
-                               } if (target_type == TypeManager.sbyte_type)
-                                       return new SByteConstant ((sbyte) v);
-                               if (target_type == TypeManager.short_type)
-                                       return new ShortConstant ((short) v);
-                               if (target_type == TypeManager.ushort_type)
-                                       return new UShortConstant ((ushort) 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.char_type)
-                                       return new CharConstant ((char) v);
-                               if (target_type == TypeManager.decimal_type)
-                                       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;
-               }
                
                public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
                {
@@ -1808,9 +1353,7 @@ namespace Mono.CSharp {
                        if (target == null)
                                return null;
                        
-                       type = target.Type;
-
-                       CheckObsoleteAttribute (type);
+                       type = target.ResolveType (ec);
 
                        if (type.IsAbstract && type.IsSealed) {
                                Report.Error (716, loc, "Cannot convert to static type `{0}'", TypeManager.CSharpName (type));
@@ -1819,11 +1362,11 @@ namespace Mono.CSharp {
 
                        eclass = ExprClass.Value;
 
-                       if (expr is Constant){
-                               Expression e = TryReduce (ec, type);
-
-                               if (e != null)
-                                       return e;
+                       Constant c = expr as Constant;
+                       if (c != null) {
+                               c = c.TryReduce (ec, type, loc);
+                               if (c != null)
+                                       return c;
                        }
 
                        if (type.IsPointer && !ec.InUnsafe) {
@@ -1891,12 +1434,12 @@ namespace Mono.CSharp {
                        oper_names [(int) Operator.LogicalAnd] = "op_LogicalAnd";
                }
 
-               public Binary (Operator oper, Expression left, Expression right, Location loc)
+               public Binary (Operator oper, Expression left, Expression right)
                {
                        this.oper = oper;
                        this.left = left;
                        this.right = right;
-                       this.loc = loc;
+                       this.loc = left.Location;
                }
 
                public Operator Oper {
@@ -1930,7 +1473,7 @@ namespace Mono.CSharp {
                /// <summary>
                ///   Returns a stringified representation of the Operator
                /// </summary>
-               static string OperName (Operator oper)
+               public static string OperName (Operator oper)
                {
                        switch (oper){
                        case Operator.Multiply:
@@ -2023,7 +1566,7 @@ namespace Mono.CSharp {
                // type, otherwise ConvertImplict() already finds the user-defined conversion for us,
                // so we don't explicitly check for performance reasons.
                //
-               bool DoNumericPromotions (EmitContext ec, Type l, Type r, bool check_user_conv)
+               bool DoNumericPromotions (EmitContext ec, Type l, Type r, Expression lexpr, Expression rexpr, bool check_user_conv)
                {
                        if (IsOfType (ec, l, r, TypeManager.double_type, check_user_conv)){
                                //
@@ -2066,9 +1609,9 @@ namespace Mono.CSharp {
                                                        long ll = ((LongConstant) right).Value;
 
                                                        if (ll >= 0)
-                                                               right = new ULongConstant ((ulong) ll);
+                                                               right = new ULongConstant ((ulong) ll, right.Location);
                                                } else {
-                                                       e = Convert.ImplicitNumericConversion (ec, right, l, loc);
+                                                       e = Convert.ImplicitNumericConversion (ec, right, l);
                                                        if (e != null)
                                                                right = e;
                                                }
@@ -2083,9 +1626,9 @@ namespace Mono.CSharp {
                                                long ll = ((LongConstant) left).Value;
                                                
                                                if (ll > 0)
-                                                       left = new ULongConstant ((ulong) ll);
+                                                       left = new ULongConstant ((ulong) ll, right.Location);
                                        } else {
-                                               e = Convert.ImplicitNumericConversion (ec, left, r, loc);
+                                               e = Convert.ImplicitNumericConversion (ec, left, r);
                                                if (e != null)
                                                        left = e;
                                        }
@@ -2127,7 +1670,7 @@ namespace Mono.CSharp {
                                                int val = ic.Value;
                                                
                                                if (val >= 0){
-                                                       right = new UIntConstant ((uint) val);
+                                                       right = new UIntConstant ((uint) val, ic.Location);
                                                        type = l;
                                                        
                                                        return true;
@@ -2140,7 +1683,7 @@ namespace Mono.CSharp {
                                                int val = ic.Value;
                                                
                                                if (val >= 0){
-                                                       left = new UIntConstant ((uint) val);
+                                                       left = new UIntConstant ((uint) val, ic.Location);
                                                        type = r;
                                                        return true;
                                                }
@@ -2175,6 +1718,12 @@ namespace Mono.CSharp {
                                left = ForceConversion (ec, left, TypeManager.int32_type);
                                right = ForceConversion (ec, right, TypeManager.int32_type);
 
+                               bool strConv =
+                                       Convert.ImplicitConversionExists (ec, lexpr, TypeManager.string_type) &&
+                                       Convert.ImplicitConversionExists (ec, rexpr, TypeManager.string_type);
+                               if (strConv && left != null && right != null)
+                                       Error_OperatorAmbiguous (loc, oper, l, r);
+
                                type = TypeManager.int32_type;
                        }
 
@@ -2182,14 +1731,19 @@ namespace Mono.CSharp {
                }
 
                static public void Error_OperatorCannotBeApplied (Location loc, string name, Type l, Type r)
+               {
+                       Error_OperatorCannotBeApplied (loc, name, TypeManager.CSharpName (l), TypeManager.CSharpName (r));
+               }
+
+               public static void Error_OperatorCannotBeApplied (Location loc, string name, string left, string right)
                {
                        Report.Error (19, loc, "Operator `{0}' cannot be applied to operands of type `{1}' and `{2}'",
-                              name, TypeManager.CSharpName (l), TypeManager.CSharpName (r));
+                               name, left, right);
                }
                
                void Error_OperatorCannotBeApplied ()
                {
-                       Error_OperatorCannotBeApplied (loc, OperName (oper), left.Type, right.Type);
+                       Error_OperatorCannotBeApplied (Location, OperName (oper), left.GetSignatureForError (), right.GetSignatureForError ());
                }
 
                static bool is_unsigned (Type t)
@@ -2248,10 +1802,10 @@ namespace Mono.CSharp {
                                type = e.Type;
 
                                if (type == TypeManager.int32_type || type == TypeManager.uint32_type){
-                                       right = new Binary (Binary.Operator.BitwiseAnd, right, new IntLiteral (31), loc);
+                                       right = new Binary (Binary.Operator.BitwiseAnd, right, new IntConstant (31, loc));
                                        right = right.DoResolve (ec);
                                } else {
-                                       right = new Binary (Binary.Operator.BitwiseAnd, right, new IntLiteral (63), loc);
+                                       right = new Binary (Binary.Operator.BitwiseAnd, right, new IntConstant (63, loc));
                                        right = right.DoResolve (ec);
                                }
 
@@ -2271,6 +1825,13 @@ namespace Mono.CSharp {
                                t == TypeManager.delegate_type || t.IsSubclassOf (TypeManager.delegate_type);
                }
 
+               static void Warning_UnintendedReferenceComparison (Location loc, string side, Type type)
+               {
+                       Report.Warning ((side == "left" ? 252 : 253), 2, loc,
+                               "Possible unintended reference comparison; to get a value comparison, " +
+                               "cast the {0} hand side to type `{1}'.", side, TypeManager.CSharpName (type));
+               }
+
                Expression ResolveOperator (EmitContext ec)
                {
                        Type l = left.Type;
@@ -2283,7 +1844,7 @@ namespace Mono.CSharp {
                                                return null;
                                        }
 
-                                       left = new BoxedCast (left);
+                                       left = new BoxedCast (left, TypeManager.object_type);
                                        Type = TypeManager.bool_type;
                                        return this;
                                }
@@ -2294,7 +1855,7 @@ namespace Mono.CSharp {
                                                return null;
                                        }
 
-                                       right = new BoxedCast (right);
+                                       right = new BoxedCast (right, TypeManager.object_type);
                                        Type = TypeManager.bool_type;
                                        return this;
                                }
@@ -2321,22 +1882,21 @@ namespace Mono.CSharp {
                        // Do not perform operator overload resolution when both sides are
                        // built-in types
                        //
+                       Expression left_operators = null, right_operators = null;
                        if (!(TypeManager.IsPrimitiveType (l) && TypeManager.IsPrimitiveType (r))){
                                //
                                // Step 1: Perform Operator Overload location
                                //
-                               Expression left_expr, right_expr;
-                               
                                string op = oper_names [(int) oper];
                                
                                MethodGroupExpr union;
-                               left_expr = MemberLookup (ec, l, op, MemberTypes.Method, AllBindingFlags, loc);
+                               left_operators = MemberLookup (ec, l, op, MemberTypes.Method, AllBindingFlags, loc);
                                if (r != l){
-                                       right_expr = MemberLookup (
+                                       right_operators = MemberLookup (
                                                ec, r, op, MemberTypes.Method, AllBindingFlags, loc);
-                                       union = Invocation.MakeUnionSet (left_expr, right_expr, loc);
+                                       union = Invocation.MakeUnionSet (left_operators, right_operators, loc);
                                } else
-                                       union = (MethodGroupExpr) left_expr;
+                                       union = (MethodGroupExpr) left_operators;
                                
                                if (union != null) {
                                        ArrayList args = new ArrayList (2);
@@ -2364,7 +1924,7 @@ namespace Mono.CSharp {
                                
                                // Simple constant folding
                                if (left is StringConstant && right is StringConstant)
-                                       return new StringConstant (((StringConstant) left).Value + ((StringConstant) right).Value);
+                                       return new StringConstant (((StringConstant) left).Value + ((StringConstant) right).Value, left.Location);
 
                                if (l == TypeManager.string_type || r == TypeManager.string_type) {
 
@@ -2459,26 +2019,47 @@ namespace Mono.CSharp {
                                // For this to be used, both arguments have to be reference-types.
                                // Read the rationale on the spec (14.9.6)
                                //
-                               // Also, if at compile time we know that the classes do not inherit
-                               // one from the other, then we catch the error there.
-                               //
                                if (!(l.IsValueType || r.IsValueType)){
                                        type = TypeManager.bool_type;
 
                                        if (l == r)
                                                return this;
                                        
-                                       if (l.IsSubclassOf (r) || r.IsSubclassOf (l))
-                                               return this;
-
                                        //
                                        // Also, a standard conversion must exist from either one
                                        //
-                                       if (!(Convert.ImplicitStandardConversionExists (ec, left, r) ||
-                                             Convert.ImplicitStandardConversionExists (ec, right, l))){
+                                       bool left_to_right =
+                                               Convert.ImplicitStandardConversionExists (ec, left, r);
+                                       bool right_to_left = !left_to_right &&
+                                               Convert.ImplicitStandardConversionExists (ec, right, l);
+
+                                       if (!left_to_right && !right_to_left) {
                                                Error_OperatorCannotBeApplied ();
                                                return null;
                                        }
+
+                                       if (left_to_right && left_operators != null &&
+                                           RootContext.WarningLevel >= 2) {
+                                               ArrayList args = new ArrayList (2);
+                                               args.Add (new Argument (left, Argument.AType.Expression));
+                                               args.Add (new Argument (left, Argument.AType.Expression));
+                                               MethodBase method = Invocation.OverloadResolve (
+                                                       ec, (MethodGroupExpr) left_operators, args, true, Location.Null);
+                                               if (method != null)
+                                                       Warning_UnintendedReferenceComparison (loc, "right", l);
+                                       }
+
+                                       if (right_to_left && right_operators != null &&
+                                           RootContext.WarningLevel >= 2) {
+                                               ArrayList args = new ArrayList (2);
+                                               args.Add (new Argument (right, Argument.AType.Expression));
+                                               args.Add (new Argument (right, Argument.AType.Expression));
+                                               MethodBase method = Invocation.OverloadResolve (
+                                                       ec, (MethodGroupExpr) right_operators, args, true, Location.Null);
+                                               if (method != null)
+                                                       Warning_UnintendedReferenceComparison (loc, "left", r);
+                                       }
+
                                        //
                                        // We are going to have to convert to an object to compare
                                        //
@@ -2718,7 +2299,7 @@ namespace Mono.CSharp {
                        // This will leave left or right set to null if there is an error
                        //
                        bool check_user_conv = is_user_defined (l) && is_user_defined (r);
-                       DoNumericPromotions (ec, l, r, check_user_conv);
+                       DoNumericPromotions (ec, l, r, left, right, check_user_conv);
                        if (left == null || right == null){
                                Error_OperatorCannotBeApplied (loc, OperName (oper), l, r);
                                return null;
@@ -2763,6 +2344,43 @@ namespace Mono.CSharp {
                        return this;
                }
 
+               Constant EnumLiftUp (EmitContext ec, Constant left, Constant right)
+               {
+                       switch (oper) {
+                               case Operator.BitwiseOr:
+                               case Operator.BitwiseAnd:
+                               case Operator.ExclusiveOr:
+                               case Operator.Equality:
+                               case Operator.Inequality:
+                               case Operator.LessThan:
+                               case Operator.LessThanOrEqual:
+                               case Operator.GreaterThan:
+                               case Operator.GreaterThanOrEqual:
+                                       if (left is EnumConstant)
+                                               return left;
+
+                                       if (left.IsZeroInteger)
+                                               return new EnumConstant (left, right.Type);
+
+                                       break;
+
+                               case Operator.Addition:
+                               case Operator.Subtraction:
+                                       return left;
+
+                               case Operator.Multiply:
+                               case Operator.Division:
+                               case Operator.Modulus:
+                               case Operator.LeftShift:
+                               case Operator.RightShift:
+                                       if (right is EnumConstant || left is EnumConstant)
+                                               break;
+                                       return left;
+                       }
+                       Error_OperatorCannotBeApplied (loc, Binary.OperName (oper), left.Type, right.Type);
+                       return null;
+               }
+
                public override Expression DoResolve (EmitContext ec)
                {
                        if ((oper == Operator.Subtraction) && (left is ParenthesizedExpression)) {
@@ -2772,7 +2390,7 @@ namespace Mono.CSharp {
                                        return null;
 
                                if (left.eclass == ExprClass.Type) {
-                                       Error (75, "To cast a negative value, you must enclose the value in parentheses");
+                                       Report.Error (75, loc, "To cast a negative value, you must enclose the value in parentheses");
                                        return null;
                                }
                        } else
@@ -2796,9 +2414,19 @@ namespace Mono.CSharp {
                                return null;
 
                        eclass = ExprClass.Value;
-
                        Constant rc = right as Constant;
 
+                       // The conversion rules are ignored in enum context but why
+                       if (!ec.InEnumContext && lc != null && rc != null && (TypeManager.IsEnumType (left.Type) || TypeManager.IsEnumType (right.Type))) {
+                               left = lc = EnumLiftUp (ec, lc, rc);
+                               if (lc == null)
+                                       return null;
+
+                               right = rc = EnumLiftUp (ec, rc, lc);
+                               if (rc == null)
+                                       return null;
+                       }
+
                        if (oper == Operator.BitwiseAnd) {
                                if (rc != null && rc.IsZeroInteger) {
                                        return lc is EnumConstant ?
@@ -2812,20 +2440,134 @@ namespace Mono.CSharp {
                                                lc;
                                }
                        }
+                       else if (oper == Operator.BitwiseOr) {
+                               if (lc is EnumConstant &&
+                                   rc != null && rc.IsZeroInteger)
+                                       return lc;
+                               if (rc is EnumConstant &&
+                                   lc != null && lc.IsZeroInteger)
+                                       return rc;
+                       } else if (oper == Operator.LogicalAnd) {
+                               if (rc != null && rc.IsDefaultValue && rc.Type == TypeManager.bool_type)
+                                       return rc;
+                               if (lc != null && lc.IsDefaultValue && lc.Type == TypeManager.bool_type)
+                                       return lc;
+                       }
 
                        if (rc != null && lc != null){
+                               int prev_e = Report.Errors;
                                Expression e = ConstantFold.BinaryFold (
                                        ec, oper, lc, rc, loc);
-                               if (e != null)
+                               if (e != null || Report.Errors != prev_e)
                                        return e;
                        }
 
                        if (TypeManager.IsNullableType (left.Type) || TypeManager.IsNullableType (right.Type))
                                return new Nullable.LiftedBinaryOperator (oper, left, right, loc).Resolve (ec);
 
+                       // Comparison warnings
+                       if (oper == Operator.Equality || oper == Operator.Inequality ||
+                           oper == Operator.LessThanOrEqual || oper == Operator.LessThan ||
+                           oper == Operator.GreaterThanOrEqual || oper == Operator.GreaterThan){
+                               if (left.Equals (right)) {
+                                       Report.Warning (1718, 3, loc, "Comparison made to same variable; did you mean to compare something else?");
+                               }
+                               CheckUselessComparison (lc, right.Type);
+                               CheckUselessComparison (rc, left.Type);
+                       }
+
                        return ResolveOperator (ec);
                }
 
+               private void CheckUselessComparison (Constant c, Type type)
+               {
+                       if (c == null || !IsTypeIntegral (type)
+                               || c is StringConstant
+                               || c is BoolConstant
+                               || c is CharConstant
+                               || c is FloatConstant
+                               || c is DoubleConstant
+                               || c is DecimalConstant
+                               )
+                               return;
+
+                       long value = 0;
+
+                       if (c is ULongConstant) {
+                               ulong uvalue = ((ULongConstant) c).Value;
+                               if (uvalue > long.MaxValue) {
+                                       if (type == TypeManager.byte_type ||
+                                           type == TypeManager.sbyte_type ||
+                                           type == TypeManager.short_type ||
+                                           type == TypeManager.ushort_type ||
+                                           type == TypeManager.int32_type ||
+                                           type == TypeManager.uint32_type ||
+                                           type == TypeManager.int64_type)
+                                               WarnUselessComparison (type);
+                                       return;
+                               }
+                               value = (long) uvalue;
+                       }
+                       else if (c is ByteConstant)
+                               value = ((ByteConstant) c).Value;
+                       else if (c is SByteConstant)
+                               value = ((SByteConstant) c).Value;
+                       else if (c is ShortConstant)
+                               value = ((ShortConstant) c).Value;
+                       else if (c is UShortConstant)
+                               value = ((UShortConstant) c).Value;
+                       else if (c is IntConstant)
+                               value = ((IntConstant) c).Value;
+                       else if (c is UIntConstant)
+                               value = ((UIntConstant) c).Value;
+                       else if (c is LongConstant)
+                               value = ((LongConstant) c).Value;
+
+                       if (value != 0) {
+                               if (IsValueOutOfRange (value, type))
+                                       WarnUselessComparison (type);
+                               return;
+                       }
+               }
+
+               private bool IsValueOutOfRange (long value, Type type)
+               {
+                       if (IsTypeUnsigned (type) && value < 0)
+                               return true;
+                       return type == TypeManager.sbyte_type && (value >= 0x80 || value < -0x80) ||
+                               type == TypeManager.byte_type && value >= 0x100 ||
+                               type == TypeManager.short_type && (value >= 0x8000 || value < -0x8000) ||
+                               type == TypeManager.ushort_type && value >= 0x10000 ||
+                               type == TypeManager.int32_type && (value >= 0x80000000 || value < -0x80000000) ||
+                               type == TypeManager.uint32_type && value >= 0x100000000;
+               }
+
+               private static bool IsTypeIntegral (Type type)
+               {
+                       return type == TypeManager.uint64_type ||
+                               type == TypeManager.int64_type ||
+                               type == TypeManager.uint32_type ||
+                               type == TypeManager.int32_type ||
+                               type == TypeManager.ushort_type ||
+                               type == TypeManager.short_type ||
+                               type == TypeManager.sbyte_type ||
+                               type == TypeManager.byte_type;
+               }
+
+               private static bool IsTypeUnsigned (Type type)
+               {
+                       return type == TypeManager.uint64_type ||
+                               type == TypeManager.uint32_type ||
+                               type == TypeManager.ushort_type ||
+                               type == TypeManager.byte_type;
+               }
+
+               private void WarnUselessComparison (Type type)
+               {
+                       Report.Warning (652, 2, loc, "Comparison to integral constant is useless; the constant is outside the range of type `{0}'",
+                               TypeManager.CSharpName (type));
+               }
+
                /// <remarks>
                ///   EmitBranchable is called from Statement.EmitBoolExpression in the
                ///   context of a conditional bool expression.  This function will return
@@ -3239,7 +2981,7 @@ namespace Mono.CSharp {
                        if (operand is StringConstant && operands.Count != 0) {
                                StringConstant last_operand = operands [operands.Count - 1] as StringConstant;
                                if (last_operand != null) {
-                                       operands [operands.Count - 1] = new StringConstant (last_operand.Value + ((StringConstant) operand).Value);
+                                       operands [operands.Count - 1] = new StringConstant (last_operand.Value + ((StringConstant) operand).Value, last_operand.Location);
                                        return;
                                }
                        }
@@ -3396,7 +3138,7 @@ namespace Mono.CSharp {
 
                protected void Error19 ()
                {
-                       Binary.Error_OperatorCannotBeApplied (loc, is_and ? "&&" : "||", type, type);
+                       Binary.Error_OperatorCannotBeApplied (loc, is_and ? "&&" : "||", left.GetSignatureForError (), right.GetSignatureForError ());
                }
 
                protected void Error218 ()
@@ -3534,7 +3276,7 @@ namespace Mono.CSharp {
 
                                Constant right_const = right as Constant;
                                if (right_const != null && size != 0) {
-                                       Expression ex = ConstantFold.BinaryFold (ec, Binary.Operator.Multiply, new IntConstant (size), right_const, loc);
+                                       Expression ex = ConstantFold.BinaryFold (ec, Binary.Operator.Multiply, new IntConstant (size, right.Location), right_const, loc);
                                        if (ex == null)
                                                return;
                                        ex.Emit (ec);
@@ -3570,12 +3312,12 @@ namespace Mono.CSharp {
        public class Conditional : Expression {
                Expression expr, trueExpr, falseExpr;
                
-               public Conditional (Expression expr, Expression trueExpr, Expression falseExpr, Location l)
+               public Conditional (Expression expr, Expression trueExpr, Expression falseExpr)
                {
                        this.expr = expr;
                        this.trueExpr = trueExpr;
                        this.falseExpr = falseExpr;
-                       this.loc = l;
+                       this.loc = expr.Location;
                }
 
                public Expression Expr {
@@ -3614,6 +3356,11 @@ namespace Mono.CSharp {
                                        return null;
                        }
                        
+                       Assign ass = expr as Assign;
+                       if (ass != null && ass.Source is Constant) {
+                               Report.Warning (665, 3, loc, "Assignment in conditional expression is always constant; did you mean to use == instead of = ?");
+                       }
+
                        trueExpr = trueExpr.Resolve (ec);
                        falseExpr = falseExpr.Resolve (ec);
 
@@ -3727,6 +3474,12 @@ namespace Mono.CSharp {
                        }
                }
 
+               public bool VerifyAssigned (EmitContext ec)
+               {
+                       VariableInfo variable_info = local_info.VariableInfo;
+                       return variable_info == null || variable_info.IsAssigned (ec, loc);
+               }
+
                protected Expression DoResolveBase (EmitContext ec, Expression lvalue_right_side)
                {
                        if (local_info == null) {
@@ -3764,7 +3517,7 @@ namespace Mono.CSharp {
                                return e.Resolve (ec);
                        }
 
-                       if ((variable_info != null) && !variable_info.IsAssigned (ec, loc))
+                       if (!VerifyAssigned (ec))
                                return null;
 
                        if (lvalue_right_side == null)
@@ -3796,11 +3549,7 @@ namespace Mono.CSharp {
 
                override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
                {
-                       Expression ret = DoResolveBase (ec, right_side);
-                       if (ret != null)
-                               CheckObsoleteAttribute (ret.Type);
-
-                       return ret;
+                       return DoResolveBase (ec, right_side);
                }
 
                public bool VerifyFixed ()
@@ -4056,7 +3805,7 @@ namespace Mono.CSharp {
                {
                        DoResolveBase (ec);
 
-                       if (is_out && ec.DoFlowAnalysis && !IsAssigned (ec, loc))
+                       if (is_out && ec.DoFlowAnalysis && (!ec.OmitStructFlowAnalysis || !vi.TypeInfo.IsStruct) && !IsAssigned (ec, loc))
                                return null;
 
                        return this;
@@ -4119,7 +3868,7 @@ namespace Mono.CSharp {
                        if (ec.HaveCaptureInfo && ec.IsParameterCaptured (name)){
                                if (leave_copy)
                                        throw new InternalErrorException ();
-
+                               
                                ec.EmitParameter (name);
                                return;
                        }
@@ -4216,6 +3965,10 @@ namespace Mono.CSharp {
                        }
                }
 
+               public override string ToString ()
+               {
+                       return "ParameterReference[" + name + "]";
+               }
        }
        
        /// <summary>
@@ -4424,11 +4177,11 @@ namespace Mono.CSharp {
                // FIXME: only allow expr to be a method invocation or a
                // delegate invocation (7.5.5)
                //
-               public Invocation (Expression expr, ArrayList arguments, Location l)
+               public Invocation (Expression expr, ArrayList arguments)
                {
                        this.expr = expr;
                        Arguments = arguments;
-                       loc = l;
+                       loc = expr.Location;
                }
 
                public Expression Expr {
@@ -4438,7 +4191,7 @@ namespace Mono.CSharp {
                }
 
                /// <summary>
-               ///   Determines "better conversion" as specified in 7.4.2.3
+               ///   Determines "better conversion" as specified in 14.4.2.3
                ///
                ///    Returns : p    if a->p is better,
                ///              q    if a->q is better,
@@ -4622,7 +4375,7 @@ namespace Mono.CSharp {
                         return !candidate_params && best_params;
                }
 
-               static bool IsOverride (MethodBase cand_method, MethodBase base_method)
+               internal static bool IsOverride (MethodBase cand_method, MethodBase base_method)
                {
                        if (!IsAncestralType (base_method.DeclaringType, cand_method.DeclaringType))
                                return false;
@@ -4856,7 +4609,7 @@ 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 (!Convert.ImplicitConversionExists (ec,
+                                                if (!TypeManager.IsEqual (a.Type, pd.ParameterType (i)) && !Convert.ImplicitConversionExists (ec,
                                                                                        a.Expr,
                                                                                        pd.ParameterType (i)))
                                                        return false;
@@ -5000,6 +4753,7 @@ namespace Mono.CSharp {
                                // Okay so we have failed to find anything so we
                                // return by providing info about the closest match
                                //
+                               int errors = Report.Errors;
                                for (int i = 0; i < methods.Length; ++i) {
                                        MethodBase c = (MethodBase) methods [i];
                                        ParameterData pd = TypeManager.GetParameterData (c);
@@ -5012,11 +4766,16 @@ namespace Mono.CSharp {
 
                                        VerifyArgumentsCompat (ec, Arguments, arg_count,
                                                c, false, null, may_fail, loc);
-                                        break;
-                               }
 
-                               if (!may_fail) {
+                                       if (!may_fail && errors == Report.Errors)
+                                               throw new InternalErrorException (
+                                                       "VerifyArgumentsCompat and IsApplicable do not agree; " +
+                                                       "likely reason: ImplicitConversion and ImplicitConversionExists have gone out of sync");
 
+                                       break;
+                               }
+
+                               if (!may_fail && errors == Report.Errors) {
                                        string report_name = me.Name;
                                        if (report_name == ".ctor")
                                                report_name = me.DeclaringType.ToString ();
@@ -5039,9 +4798,7 @@ namespace Mono.CSharp {
                                                return null;
                                        }
 
-                                       Error_WrongNumArguments (
-                                               loc, report_name, arg_count);
-                                       return null;
+                                       Error_WrongNumArguments (loc, report_name, arg_count);
                                }
                                 
                                return null;
@@ -5119,7 +4876,6 @@ namespace Mono.CSharp {
                                        method_params = cand_params;
                                }
                        }
-
                        //
                        // Now check that there are no ambiguities i.e the selected method
                        // should be better than all the others
@@ -5187,7 +4943,7 @@ namespace Mono.CSharp {
                public static void Error_WrongNumArguments (Location loc, String name, int arg_count)
                {
                        Report.Error (1501, loc, "No overload for method `{0}' takes `{1}' arguments",
-                               name, arg_count);
+                               name, arg_count.ToString ());
                }
 
                 static void Error_InvokeOnDelegate (Location loc)
@@ -5206,21 +4962,20 @@ namespace Mono.CSharp {
                                Report.Error (1594, loc, "Delegate `{0}' has some invalid arguments",
                                        TypeManager.CSharpName (delegate_type));
 
-                       string par_desc = expected_par.ParameterDesc (idx);
+                       Parameter.Modifier mod = expected_par.ParameterModifier (idx);
 
-                       if (a.Modifier != expected_par.ParameterModifier (idx)) {
-                               if ((expected_par.ParameterModifier (idx) & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) == 0)
+                       string index = (idx + 1).ToString ();
+                       if (mod != Parameter.Modifier.ARGLIST && mod != a.Modifier) {
+                               if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) == 0)
                                        Report.Error (1615, loc, "Argument `{0}' should not be passed with the `{1}' keyword",
-                                               idx + 1, Parameter.GetModifierSignature (a.Modifier));
+                                               index, Parameter.GetModifierSignature (a.Modifier));
                                else
                                        Report.Error (1620, loc, "Argument `{0}' must be passed with the `{1}' keyword",
-                                               idx + 1, Parameter.GetModifierSignature (expected_par.ParameterModifier (idx)));
-                               return;
+                                               index, Parameter.GetModifierSignature (mod));
+                       } else {
+                               Report.Error (1503, loc, "Argument {0}: Cannot convert from `{1}' to `{2}'",
+                                       index, Argument.FullDesc (a), expected_par.ParameterDesc (idx));
                        }
-
-                       Report.Error (1503, loc,
-                                     String.Format ("Argument {0}: Cannot convert from `{1}' to `{2}'",
-                                                    idx + 1, Argument.FullDesc (a), par_desc));
                }
                
                public static bool VerifyArgumentsCompat (EmitContext ec, ArrayList Arguments,
@@ -5241,15 +4996,18 @@ namespace Mono.CSharp {
                                if (pm == Parameter.Modifier.PARAMS){
                                        if ((pm & ~Parameter.Modifier.PARAMS) != a.Modifier) {
                                                if (!may_fail)
-                                                       Error_InvalidArguments (
-                                                               loc, j, method, delegate_type,
-                                                               a, pd);
+                                                       Error_InvalidArguments (loc, j, method, delegate_type, a, pd);
                                                return false;
                                        }
 
                                        if (chose_params_expanded)
                                                parameter_type = TypeManager.GetElementType (parameter_type);
-                               } else if (pm == Parameter.Modifier.ARGLIST){
+                               } else if (pm == Parameter.Modifier.ARGLIST) {
+                                       if (!(a.Expr is Arglist)) {
+                                               if (!may_fail)
+                                                       Error_InvalidArguments (loc, j, method, delegate_type, a, pd);
+                                               return false;
+                                       }
                                        continue;
                                } else {
                                        //
@@ -5257,9 +5015,7 @@ namespace Mono.CSharp {
                                        //
                                        if (pd.ParameterModifier (j) != a.Modifier){
                                                if (!may_fail)
-                                                       Error_InvalidArguments (
-                                                               loc, j, method, delegate_type,
-                                                               a, pd);
+                                                       Error_InvalidArguments (loc, j, method, delegate_type, a, pd);
                                                return false;
                                        }
                                }
@@ -5400,12 +5156,8 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       if ((method.Attributes & MethodAttributes.SpecialName) != 0){
-                               if (TypeManager.LookupDeclSpace (method.DeclaringType) != null || TypeManager.IsSpecialMethod (method)) {
-                                       Report.Error (571, loc, "`{0}': cannot explicitly call operator or accessor",
-                                               TypeManager.CSharpSignature (method, true));
-                                       return null;
-                               }
+                       if ((method.Attributes & MethodAttributes.SpecialName) != 0 && IsSpecialMethodInvocation (method)) {
+                               return null;
                        }
                        
                        if (mg.InstanceExpression != null)
@@ -5415,6 +5167,32 @@ namespace Mono.CSharp {
                        return this;
                }
 
+               bool IsSpecialMethodInvocation (MethodBase method)
+               {
+                       IMethodData md = TypeManager.GetMethod (method);
+                       if (md != null) {
+                               if (!(md is AbstractPropertyEventMethod) && !(md is Operator))
+                                       return false;
+                       } else {
+                               if (!TypeManager.IsSpecialMethod (method))
+                                       return false;
+
+                               int args = TypeManager.GetParameterData (method).Count;
+                               if (method.Name.StartsWith ("get_") && args > 0)
+                                       return false;
+                               else if (method.Name.StartsWith ("set_") && args > 2)
+                                       return false;
+
+                               // TODO: check operators and events as well ?
+                       }
+
+                       Report.SymbolRelatedToPreviousError (method);
+                       Report.Error (571, loc, "`{0}': cannot explicitly call operator or accessor",
+                               TypeManager.CSharpSignature (method, true));
+       
+                       return true;
+               }
+
                // <summary>
                //   Emits the list of arguments as an array
                // </summary>
@@ -5465,33 +5243,12 @@ namespace Mono.CSharp {
                /// </summary>
                public static void EmitArguments (EmitContext ec, MethodBase mb, ArrayList arguments, bool dup_args, LocalTemporary this_arg)
                {
-                       ParameterData pd;
-                       if (mb != null)
-                               pd = TypeManager.GetParameterData (mb);
-                       else
-                               pd = null;
-                       
+                       ParameterData pd = mb == null ? null : TypeManager.GetParameterData (mb);
+                       int top = arguments == null ? 0 : arguments.Count;
                        LocalTemporary [] temps = null;
                        
-                       if (dup_args)
-                               temps = new LocalTemporary [arguments.Count];
-
-                       //
-                       // If we are calling a params method with no arguments, special case it
-                       //
-                       if (arguments == null){
-                               if (pd != null && pd.Count > 0 &&
-                                   pd.ParameterModifier (0) == Parameter.Modifier.PARAMS){
-                                       ILGenerator ig = ec.ig;
-
-                                       IntConstant.EmitInt (ig, 0);
-                                       ig.Emit (OpCodes.Newarr, TypeManager.GetElementType (pd.ParameterType (0)));
-                               }
-
-                               return;
-                       }
-
-                       int top = arguments.Count;
+                       if (dup_args && top != 0)
+                               temps = new LocalTemporary [top];
 
                        for (int i = 0; i < top; i++){
                                Argument a = (Argument) arguments [i];
@@ -5659,57 +5416,54 @@ namespace Mono.CSharp {
                                //
                                if (!omit_args) {
                                        Type t = null;
-                                       if (this_call) {
-                                               ig.Emit (OpCodes.Ldarg_0);
-                                               t = decl_type;
-                                       } else {
-                                               Type iexpr_type = instance_expr.Type;
+                                       Type iexpr_type = instance_expr.Type;
 
+                                       //
+                                       // Push the instance expression
+                                       //
+                                       if (TypeManager.IsValueType (iexpr_type)) {
                                                //
-                                               // Push the instance expression
-                                               //
-                                               if (TypeManager.IsValueType (iexpr_type)) {
+                                               // Special case: calls to a function declared in a 
+                                               // reference-type with a value-type argument need
+                                               // to have their value boxed.
+                                               if (decl_type.IsValueType ||
+                                                   iexpr_type.IsGenericParameter) {
                                                        //
-                                                       // Special case: calls to a function declared in a 
-                                                       // reference-type with a value-type argument need
-                                                       // to have their value boxed.
-                                                       if (decl_type.IsValueType ||
-                                                           iexpr_type.IsGenericParameter) {
-                                                               //
-                                                               // If the expression implements IMemoryLocation, then
-                                                               // we can optimize and use AddressOf on the
-                                                               // return.
-                                                               //
-                                                               // If not we have to use some temporary storage for
-                                                               // it.
-                                                               if (instance_expr is IMemoryLocation) {
-                                                                       ((IMemoryLocation)instance_expr).
-                                                                               AddressOf (ec, AddressOp.LoadStore);
-                                                               } else {
-                                                                       LocalTemporary temp = new LocalTemporary (ec, iexpr_type);
-                                                                       instance_expr.Emit (ec);
-                                                                       temp.Store (ec);
-                                                                       temp.AddressOf (ec, AddressOp.Load);
-                                                               }
-
-                                                               // avoid the overhead of doing this all the time.
-                                                               if (dup_args)
-                                                                       t = TypeManager.GetReferenceType (iexpr_type);
+                                                       // If the expression implements IMemoryLocation, then
+                                                       // we can optimize and use AddressOf on the
+                                                       // return.
+                                                       //
+                                                       // If not we have to use some temporary storage for
+                                                       // it.
+                                                       if (instance_expr is IMemoryLocation) {
+                                                               ((IMemoryLocation)instance_expr).
+                                                                       AddressOf (ec, AddressOp.LoadStore);
                                                        } else {
+                                                               LocalTemporary temp = new LocalTemporary (ec, iexpr_type);
                                                                instance_expr.Emit (ec);
-                                                               ig.Emit (OpCodes.Box, instance_expr.Type);
-                                                               t = TypeManager.object_type;
+                                                               temp.Store (ec);
+                                                               temp.AddressOf (ec, AddressOp.Load);
                                                        }
+
+                                                       // avoid the overhead of doing this all the time.
+                                                       if (dup_args)
+                                                               t = TypeManager.GetReferenceType (iexpr_type);
                                                } else {
                                                        instance_expr.Emit (ec);
-                                                       t = instance_expr.Type;
+                                                       ig.Emit (OpCodes.Box, instance_expr.Type);
+                                                       t = TypeManager.object_type;
                                                }
+                                       } else {
+                                               instance_expr.Emit (ec);
+                                               t = instance_expr.Type;
                                        }
 
                                        if (dup_args) {
-                                               this_arg = new LocalTemporary (ec, t);
                                                ig.Emit (OpCodes.Dup);
-                                               this_arg.Store (ec);
+                                               if (Arguments != null && Arguments.Count != 0) {
+                                                       this_arg = new LocalTemporary (ec, t);
+                                                       this_arg.Store (ec);
+                                               }
                                        }
                                }
                        }
@@ -5771,11 +5525,11 @@ namespace Mono.CSharp {
                Expression expr;
                Expression argument;
 
-               public InvocationOrCast (Expression expr, Expression argument, Location loc)
+               public InvocationOrCast (Expression expr, Expression argument)
                {
                        this.expr = expr;
                        this.argument = argument;
-                       this.loc = loc;
+                       this.loc = expr.Location;
                }
 
                public override Expression DoResolve (EmitContext ec)
@@ -5954,38 +5708,61 @@ namespace Mono.CSharp {
                /// <summary>
                /// Converts complex core type syntax like 'new int ()' to simple constant
                /// </summary>
-               Expression Constantify (Type t)
+               public static Constant Constantify (Type t)
                {
                        if (t == TypeManager.int32_type)
-                               return new IntConstant (0);
+                               return new IntConstant (0, Location.Null);
                        if (t == TypeManager.uint32_type)
-                               return new UIntConstant (0);
+                               return new UIntConstant (0, Location.Null);
                        if (t == TypeManager.int64_type)
-                               return new LongConstant (0);
+                               return new LongConstant (0, Location.Null);
                        if (t == TypeManager.uint64_type)
-                               return new ULongConstant (0);
+                               return new ULongConstant (0, Location.Null);
                        if (t == TypeManager.float_type)
-                               return new FloatConstant (0);
+                               return new FloatConstant (0, Location.Null);
                        if (t == TypeManager.double_type)
-                               return new DoubleConstant (0);
+                               return new DoubleConstant (0, Location.Null);
                        if (t == TypeManager.short_type)
-                               return new ShortConstant (0);
+                               return new ShortConstant (0, Location.Null);
                        if (t == TypeManager.ushort_type)
-                               return new UShortConstant (0);
+                               return new UShortConstant (0, Location.Null);
                        if (t == TypeManager.sbyte_type)
-                               return new SByteConstant (0);
+                               return new SByteConstant (0, Location.Null);
                        if (t == TypeManager.byte_type)
-                               return new ByteConstant (0);
+                               return new ByteConstant (0, Location.Null);
                        if (t == TypeManager.char_type)
-                               return new CharConstant ('\0');
+                               return new CharConstant ('\0', Location.Null);
                        if (t == TypeManager.bool_type)
-                               return new BoolConstant (false);
+                               return new BoolConstant (false, Location.Null);
                        if (t == TypeManager.decimal_type)
-                               return new DecimalConstant (0);
+                               return new DecimalConstant (0, Location.Null);
 
                        return null;
                }
 
+               //
+               // Checks whether the type is an interface that has the
+               // [ComImport, CoClass] attributes and must be treated
+               // specially
+               //
+               public Expression CheckComImport (EmitContext ec)
+               {
+                       if (!type.IsInterface)
+                               return null;
+
+                       //
+                       // Turn the call into:
+                       // (the-interface-stated) (new class-referenced-in-coclassattribute ())
+                       //
+                       Type real_class = AttributeTester.GetCoClassAttribute (type);
+                       if (real_class == null)
+                               return null;
+
+                       New proxy = new New (new TypeExpression (real_class, loc), Arguments, loc);
+                       Cast cast = new Cast (new TypeExpression (type, loc), proxy, loc);
+                       return cast.Resolve (ec);
+               }
+               
                public override Expression DoResolve (EmitContext ec)
                {
                        //
@@ -6001,26 +5778,20 @@ namespace Mono.CSharp {
                                        return RequestedType;
                                return this;
                        }
-                       
+
                        TypeExpr texpr = RequestedType.ResolveAsTypeTerminal (ec);
                        if (texpr == null)
                                return null;
 
+                       type = texpr.ResolveType (ec);
+
                        if (Arguments == null) {
                                Expression c = Constantify (type);
                                if (c != null)
                                        return c;
                        }
-                       
-                       type = texpr.Type;
-                       if (type == null)
-                               return null;
-                       
-                       CheckObsoleteAttribute (type);
 
-                       bool IsDelegate = TypeManager.IsDelegateType (type);
-                       
-                       if (IsDelegate){
+                       if (TypeManager.IsDelegateType (type)) {
                                RequestedType = (new NewDelegate (type, Arguments, loc)).Resolve (ec);
                                if (RequestedType != null)
                                        if (!(RequestedType is DelegateCreation))
@@ -6054,11 +5825,17 @@ namespace Mono.CSharp {
                        }
 
                        if (type.IsAbstract && type.IsSealed) {
+                               Report.SymbolRelatedToPreviousError (type);
                                Report.Error (712, loc, "Cannot create an instance of the static class `{0}'", TypeManager.CSharpName (type));
                                return null;
                        }
 
                        if (type.IsInterface || type.IsAbstract){
+                               RequestedType = CheckComImport (ec);
+                               if (RequestedType != null)
+                                       return RequestedType;
+                               
+                               Report.SymbolRelatedToPreviousError (type);
                                Report.Error (144, loc, "Cannot create an instance of the abstract class or interface `{0}'", TypeManager.CSharpName (type));
                                return null;
                        }
@@ -6073,46 +5850,31 @@ namespace Mono.CSharp {
                        if (is_struct && Arguments == null)
                                return this;
 
-                       Expression ml;
-                       ml = MemberLookupFinal (ec, type, type, ".ctor",
-                                               // For member-lookup, treat 'new Foo (bar)' as call to 'foo.ctor (bar)', where 'foo' is of type 'Foo'.
-                                               MemberTypes.Constructor,
-                                               AllBindingFlags | BindingFlags.DeclaredOnly, loc);
+                       Expression ml = MemberLookupFinal (ec, type, type, ".ctor",
+                               MemberTypes.Constructor, AllBindingFlags | BindingFlags.DeclaredOnly, loc);
 
                        if (ml == null)
                                return null;
-                       
-                       if (! (ml is MethodGroupExpr)){
-                               if (!is_struct){
-                                       ml.Error_UnexpectedKind (ec, "method group", loc);
-                                       return null;
-                               }
+
+                       MethodGroupExpr mg = ml as MethodGroupExpr;
+
+                       if (mg == null) {
+                               ml.Error_UnexpectedKind (ec, "method group", loc);
+                               return null;
                        }
 
-                       if (ml != null) {
-                               if (Arguments != null){
-                                       foreach (Argument a in Arguments){
-                                               if (!a.Resolve (ec, loc))
-                                                       return null;
-                                       }
+                       if (Arguments != null){
+                               foreach (Argument a in Arguments){
+                                       if (!a.Resolve (ec, loc))
+                                               return null;
                                }
-
-                               method = Invocation.OverloadResolve (
-                                       ec, (MethodGroupExpr) ml, Arguments, true, loc);
-                               
                        }
 
+                       method = Invocation.OverloadResolve (ec, mg, Arguments, false, loc);
                        if (method == null) {
-                               if (almostMatchedMembers.Count != 0) {
+                               if (almostMatchedMembers.Count != 0)
                                        MemberLookupFailed (ec, type, type, ".ctor", null, true, loc);
-                                       return null;
-                               }
-
-                               if (!is_struct || Arguments.Count > 0) {
-                                       Invocation.Error_WrongNumArguments (loc, TypeManager.CSharpName (type),
-                                               Arguments == null ? 0 : Arguments.Count);
-                                       return null;
-                               }
+                               return null;
                        }
 
                        return this;
@@ -6338,16 +6100,21 @@ namespace Mono.CSharp {
                {
                        if (specified_dims) { 
                                Argument a = (Argument) arguments [idx];
-                               
+
                                if (!a.Resolve (ec, loc))
                                        return false;
-                               
-                               if (!(a.Expr is Constant)) {
-                                       Error (150, "A constant value is expected");
+
+                               Constant c = a.Expr as Constant;
+                               if (c != null) {
+                                       c = c.ToType (TypeManager.int32_type, a.Expr.Location);
+                               }
+
+                               if (c == null) {
+                                       Report.Error (150, a.Expr.Location, "A constant value is expected");
                                        return false;
                                }
-                               
-                               int value = (int) ((Constant) a.Expr).GetValue ();
+
+                               int value = (int) c.GetValue ();
                                
                                if (value != probe.Count) {
                                        Error_IncorrectArrayInitializer ();
@@ -6423,7 +6190,7 @@ namespace Mono.CSharp {
                        int i = 0;
                        for (ArrayList probe = initializers; probe != null;) {
                                if (probe.Count > 0 && probe [0] is ArrayList) {
-                                       Expression e = new IntConstant (probe.Count);
+                                       Expression e = new IntConstant (probe.Count, Location.Null);
                                        arguments.Add (new Argument (e, Argument.AType.Expression));
 
                                        bounds [i++] =  probe.Count;
@@ -6431,7 +6198,7 @@ namespace Mono.CSharp {
                                        probe = (ArrayList) probe [0];
                                        
                                } else {
-                                       Expression e = new IntConstant (probe.Count);
+                                       Expression e = new IntConstant (probe.Count, Location.Null);
                                        arguments.Add (new Argument (e, Argument.AType.Expression));
 
                                        bounds [i++] = probe.Count;
@@ -6511,7 +6278,7 @@ namespace Mono.CSharp {
                        if (array_type_expr == null)
                                return false;
 
-                       type = array_type_expr.Type;
+                       type = array_type_expr.ResolveType (ec);
 
                        if (!type.IsArray) {
                                Error (622, "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
@@ -6843,7 +6610,7 @@ namespace Mono.CSharp {
                                        if (e is StringConstant || e is DecimalConstant || !(e is Constant) ||
                                            num_automatic_initializers <= max_automatic_initializers) {
                                                Type etype = e.Type;
-                                               
+
                                                ig.Emit (OpCodes.Dup);
 
                                                for (int idx = 0; idx < dims; idx++) 
@@ -6853,7 +6620,8 @@ namespace Mono.CSharp {
                                                // If we are dealing with a struct, get the
                                                // address of it, so we can store it.
                                                //
-                                               if ((dims == 1) && etype.IsValueType &&
+                                               if ((dims == 1) && 
+                                                   TypeManager.IsValueType (etype) &&
                                                    (!TypeManager.IsBuiltinOrEnum (etype) ||
                                                     etype == TypeManager.decimal_type)) {
                                                        if (e is New){
@@ -6999,8 +6767,7 @@ namespace Mono.CSharp {
 
                public bool VerifyFixed ()
                {
-                       // Treat 'this' as a value parameter for the purpose of fixed variable determination.
-                       return true;
+                       return !TypeManager.IsValueType (Type);
                }
 
                public bool ResolveBase (EmitContext ec)
@@ -7031,7 +6798,7 @@ namespace Mono.CSharp {
                        if (!ResolveBase (ec))
                                return null;
 
-                       if ((variable_info != null) && !variable_info.IsAssigned (ec)) {
+                       if ((variable_info != null) && !(type.IsValueType && ec.OmitStructFlowAnalysis) && !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;
@@ -7242,7 +7009,7 @@ namespace Mono.CSharp {
                        if (texpr == null)
                                return null;
 
-                       typearg = texpr.Type;
+                       typearg = texpr.ResolveType (ec);
 
                        if (typearg == TypeManager.void_type) {
                                Error (673, "System.Void cannot be used from C#. Use typeof (void) to get the void type object");
@@ -7253,7 +7020,6 @@ namespace Mono.CSharp {
                                UnsafeError (loc);
                                return null;
                        }
-                       CheckObsoleteAttribute (typearg);
 
                        type = TypeManager.type_type;
                        // Even though what is returned is a type object, it's treated as a value by the compiler.
@@ -7316,11 +7082,11 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       type_queried = texpr.Type;
+                       type_queried = texpr.ResolveType (ec);
 
                        int size_of = GetTypeSize (type_queried);
                        if (size_of > 0) {
-                               return new IntConstant (size_of);
+                               return new IntConstant (size_of, loc);
                        }
 
                        if (!ec.InUnsafe) {
@@ -7329,8 +7095,6 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       CheckObsoleteAttribute (type_queried);
-
                        if (!TypeManager.VerifyUnManaged (type_queried, loc)){
                                return null;
                        }
@@ -7351,19 +7115,103 @@ namespace Mono.CSharp {
                }
        }
 
+       /// <summary>
+       ///   Implements the qualified-alias-member (::) expression.
+       /// </summary>
+       public class QualifiedAliasMember : Expression
+       {
+               string alias, identifier;
+
+               public QualifiedAliasMember (string alias, string identifier, Location l)
+               {
+                       this.alias = alias;
+                       this.identifier = identifier;
+                       loc = l;
+               }
+
+               public override FullNamedExpression ResolveAsTypeStep (EmitContext ec, bool silent)
+               {
+                       if (alias == "global")
+                               return new MemberAccess (RootNamespace.Global, identifier, loc).ResolveAsTypeStep (ec, silent);
+
+                       int errors = Report.Errors;
+                       FullNamedExpression fne = ec.DeclSpace.NamespaceEntry.LookupAlias (alias);
+                       if (fne == null) {
+                               if (errors == Report.Errors)
+                                       Report.Error (432, loc, "Alias `{0}' not found", alias);
+                               return null;
+                       }
+                       if (fne.eclass != ExprClass.Namespace) {
+                               if (!silent)
+                                       Report.Error (431, loc, "`{0}' cannot be used with '::' since it denotes a type", alias);
+                               return null;
+                       }
+                       return new MemberAccess (fne, identifier, loc).ResolveAsTypeStep (ec, silent);
+               }
+
+               public override Expression DoResolve (EmitContext ec)
+               {
+                       FullNamedExpression fne;
+                       if (alias == "global") {
+                               fne = RootNamespace.Global;
+                       } else {
+                               int errors = Report.Errors;
+                               fne = ec.DeclSpace.NamespaceEntry.LookupAlias (alias);
+                               if (fne == null) {
+                                       if (errors == Report.Errors)
+                                               Report.Error (432, loc, "Alias `{0}' not found", alias);
+                                       return null;
+                               }
+                       }
+
+                       Expression retval = new MemberAccess (fne, identifier, loc).DoResolve (ec);
+                       if (retval == null)
+                               return null;
+
+                       if (!(retval is FullNamedExpression)) {
+                               Report.Error (687, loc, "The expression `{0}::{1}' did not resolve to a namespace or a type", alias, identifier);
+                               return null;
+                       }
+
+                       // We defer this check till the end to match the behaviour of CSC
+                       if (fne.eclass != ExprClass.Namespace) {
+                               Report.Error (431, loc, "`{0}' cannot be used with '::' since it denotes a type", alias);
+                               return null;
+                       }
+                       return retval;
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       throw new InternalErrorException ("QualifiedAliasMember found in resolved tree");
+               }
+
+
+               public override string ToString ()
+               {
+                       return alias + "::" + identifier;
+               }
+
+               public override string GetSignatureForError ()
+               {
+                       return ToString ();
+               }
+       }
+
        /// <summary>
        ///   Implements the member access expression
        /// </summary>
        public class MemberAccess : Expression {
-               public string Identifier;
+               public readonly string Identifier;
                Expression expr;
                TypeArguments args;
                
+               // TODO: Location can be removed
                public MemberAccess (Expression expr, string id, Location l)
                {
                        this.expr = expr;
                        Identifier = id;
-                       loc = l;
+                       loc = expr.Location;
                }
 
                public MemberAccess (Expression expr, string id, TypeArguments args,
@@ -7377,6 +7225,8 @@ namespace Mono.CSharp {
                        get { return expr; }
                }
 
+               // TODO: this method has very poor performace for Enum fields and
+               // probably for other constants as well
                Expression DoResolve (EmitContext ec, Expression right_side)
                {
                        if (type != null)
@@ -7392,7 +7242,7 @@ namespace Mono.CSharp {
                        SimpleName original = expr as SimpleName;
                        Expression new_expr = expr.Resolve (ec,
                                ResolveFlags.VariableOrValue | ResolveFlags.Type |
-                               ResolveFlags.Intermediate | ResolveFlags.DisableFlowAnalysis);
+                               ResolveFlags.Intermediate | ResolveFlags.DisableStructFlowAnalysis);
 
                        if (new_expr == null)
                                return null;
@@ -7409,54 +7259,7 @@ namespace Mono.CSharp {
                                return retval;
                        }
 
-                       //
-                       // TODO: I mailed Ravi about this, and apparently we can get rid
-                       // of this and put it in the right place.
-                       // 
-                       // Handle enums here when they are in transit.
-                       // Note that we cannot afford to hit MemberLookup in this case because
-                       // it will fail to find any members at all
-                       //
-
                        Type expr_type = new_expr.Type;
-                       if (new_expr is TypeExpr){
-                               if (!ec.DeclSpace.CheckAccessLevel (expr_type)){
-                                       ErrorIsInaccesible (loc, TypeManager.CSharpName (expr_type));
-                                       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 (Identifier, loc);
-                                               if (value != null){
-                                                       MemberCore mc = en.GetDefinition (Identifier);
-                                                       ObsoleteAttribute oa = mc.GetObsoleteAttribute (en);
-                                                       if (oa != null) {
-                                                               AttributeTester.Report_ObsoleteMessage (oa, mc.GetSignatureForError (), Location);
-                                                       }
-                                                       oa = en.GetObsoleteAttribute (en);
-                                                       if (oa != null) {
-                                                               AttributeTester.Report_ObsoleteMessage (oa, en.GetSignatureForError (), Location);
-                                                       }
-
-                                                       Constant c = Constantify (value, en.UnderlyingType);
-                                                       return new EnumConstant (c, expr_type);
-                                               }
-                                       } else {
-                                               CheckObsoleteAttribute (expr_type);
-
-                                               FieldInfo fi = expr_type.GetField (Identifier);
-                                               if (fi != null) {
-                                                       ObsoleteAttribute oa = AttributeTester.GetMemberObsoleteAttribute (fi);
-                                                       if (oa != null)
-                                                               AttributeTester.Report_ObsoleteMessage (oa, TypeManager.GetFullNameSignature (fi), Location);
-                                               }
-                                       }
-                               }
-                       }
-
                        if (expr_type.IsPointer){
                                Error (23, "The `.' operator can not be applied to pointer operands (" +
                                       TypeManager.CSharpName (expr_type) + ")");
@@ -7516,15 +7319,22 @@ namespace Mono.CSharp {
                                return mg.ResolveGeneric (ec, args);
                        }
 
+                       if (original != null && !TypeManager.IsValueType (expr_type)) {
+                               me = member_lookup as MemberExpr;
+                               if (me != null && me.IsInstance) {
+                                       LocalVariableReference var = new_expr as LocalVariableReference;
+                                       if (var != null && !var.VerifyAssigned (ec))
+                                               return null;
+                               }
+                       }
+
                        // The following DoResolve/DoResolveLValue will do the definite assignment
                        // check.
 
                        if (right_side != null)
-                               member_lookup = member_lookup.DoResolveLValue (ec, right_side);
+                               return member_lookup.DoResolveLValue (ec, right_side);
                        else
-                               member_lookup = member_lookup.DoResolve (ec);
-
-                       return member_lookup;
+                               return member_lookup.DoResolve (ec);
                }
 
                public override Expression DoResolve (EmitContext ec)
@@ -7566,7 +7376,7 @@ namespace Mono.CSharp {
                        if (tnew_expr == null)
                                return null;
 
-                       Type expr_type = tnew_expr.Type;
+                       Type expr_type = tnew_expr.ResolveType (ec);
 
                        if (expr_type.IsPointer){
                                Error (23, "The `.' operator can not be applied to pointer operands (" +
@@ -7743,11 +7553,11 @@ namespace Mono.CSharp {
                public ArrayList  Arguments;
                public Expression Expr;
                
-               public ElementAccess (Expression e, ArrayList e_list, Location l)
+               public ElementAccess (Expression e, ArrayList e_list)
                {
                        Expr = e;
 
-                       loc  = l;
+                       loc  = e.Location;
                        
                        if (e_list == null)
                                return;
@@ -7848,11 +7658,10 @@ namespace Mono.CSharp {
                                                Report.Error (1708, loc, "Fixed size buffers can only be accessed through locals or fields");
                                                return null;
                                        }
-// TODO: not sure whether it is correct
-//                                     if (!ec.InFixedInitializer) {
-//                                             Error (1666, "You cannot use fixed sized buffers contained in unfixed expressions. Try using the fixed statement");
-//                                             return null;
-//                                     }
+                                       if (!ec.InFixedInitializer && ec.ContainerType.IsValueType) {
+                                               Error (1666, "You cannot use fixed size buffers contained in unfixed expressions. Try using the fixed statement");
+                                               return null;
+                                       }
                                        return MakePointerAccess (ec, ff.ElementType);
                                }
                        }
@@ -7905,7 +7714,7 @@ namespace Mono.CSharp {
                        Type t = ea.Expr.Type;
                        if (t.GetArrayRank () != ea.Arguments.Count){
                                Report.Error (22, ea.Location, "Wrong number of indexes `{0}' inside [], expected `{1}'",
-                                         ea.Arguments.Count, t.GetArrayRank ());
+                                         ea.Arguments.Count.ToString (), t.GetArrayRank ().ToString ());
                                return null;
                        }
 
@@ -7980,6 +7789,8 @@ namespace Mono.CSharp {
                                ig.Emit (OpCodes.Ldobj, type);
                        } else if (type.IsGenericParameter)
                                ig.Emit (OpCodes.Ldelem_Any, type);
+                       else if (type.IsPointer)
+                               ig.Emit (OpCodes.Ldelem_I);
                        else
                                ig.Emit (OpCodes.Ldelem_Ref);
                }
@@ -8020,7 +7831,9 @@ namespace Mono.CSharp {
                        } else if (t.IsGenericParameter) {
                                has_type_arg = true;
                                return OpCodes.Stelem_Any;
-                       } else
+                       } else if (t.IsPointer)
+                               return OpCodes.Stelem_I;
+                       else
                                return OpCodes.Stelem_Ref;
                }
 
@@ -8302,6 +8115,21 @@ namespace Mono.CSharp {
                {
                        Indexers ix = empty;
 
+                       if (lookup_type.IsGenericParameter) {
+                               GenericConstraints gc = TypeManager.GetTypeParameterConstraints (lookup_type);
+                               if (gc == null)
+                                       return empty;
+
+                               if (gc.HasClassConstraint)
+                                       Append (ref ix, caller_type, GetIndexersForTypeOrInterface (caller_type, gc.ClassConstraint));
+
+                               Type[] ifaces = gc.InterfaceConstraints;
+                               foreach (Type itype in ifaces)
+                                       Append (ref ix, caller_type, GetIndexersForTypeOrInterface (caller_type, itype));
+
+                               return ix;
+                       }
+
                        Type copy = lookup_type;
                        while (copy != TypeManager.object_type && copy != null){
                                Append (ref ix, caller_type, GetIndexersForTypeOrInterface (caller_type, copy));
@@ -8785,6 +8613,11 @@ namespace Mono.CSharp {
                Expression left;
                string dim;
                
+               public ComposedCast (Expression left, string dim)
+                       : this (left, dim, left.Location)
+               {
+               }
+
                public ComposedCast (Expression left, string dim, Location l)
                {
                        this.left = left;
@@ -8809,7 +8642,10 @@ namespace Mono.CSharp {
                        if (lexpr == null)
                                return null;
 
-                       Type ltype = lexpr.Type;
+                       bool old = ec.TestObsoleteMethodUsage;
+                       ec.TestObsoleteMethodUsage = true;
+                       Type ltype = lexpr.ResolveType (ec);
+                       ec.TestObsoleteMethodUsage = old;
 
                        if ((ltype == TypeManager.void_type) && (dim != "*")) {
                                Report.Error (1547, Location,
@@ -8865,7 +8701,7 @@ namespace Mono.CSharp {
                }
        }
 
-       public class FixedBufferPtr: Expression {
+       public class FixedBufferPtr : Expression {
                Expression array;
 
                public FixedBufferPtr (Expression array, Type array_type, Location l)
@@ -8991,7 +8827,7 @@ namespace Mono.CSharp {
                        if (texpr == null)
                                return null;
 
-                       otype = texpr.Type;
+                       otype = texpr.ResolveType (ec);
 
                        if (!TypeManager.VerifyUnManaged (otype, loc))
                                return null;