*** empty log message ***
[mono.git] / mcs / mcs / expression.cs
index bcaf2284897774e225aa04ea91cb87d09587a42c..47062869ba4d7debd08537c82e3f4ba17dfd3f9d 100755 (executable)
@@ -15,9 +15,9 @@
 //
 
 namespace CIR {
+       using System;
        using System.Collections;
        using System.Diagnostics;
-       using System;
        using System.Reflection;
        using System.Reflection.Emit;
        using System.Text;
@@ -119,8 +119,32 @@ namespace CIR {
                //   expression).
                // </remarks>
                
-               public abstract Expression Resolve (TypeContainer tc);
+               public abstract Expression DoResolve (TypeContainer tc);
+
+
+               //
+               // Currently Resolve wraps DoResolve to perform sanity
+               // checking and assertion checking on what we expect from Resolve
+               //
+
+               public Expression Resolve (TypeContainer tc)
+               {
+                       Expression e = DoResolve (tc);
 
+                       if (e != null){
+                               if (e.ExprClass == ExprClass.Invalid)
+                                       throw new Exception ("Expression " + e +
+                                                            " ExprClass is Invalid after resolve");
+
+                               if (e.ExprClass != ExprClass.MethodGroup)
+                                       if (e.type == null)
+                                               throw new Exception ("Expression " + e +
+                                                                    " did not set its type after Resolve");
+                       }
+
+                       return e;
+               }
+                      
                // <summary>
                //   Emits the code for the expression
                // </summary>
@@ -144,15 +168,60 @@ namespace CIR {
                        type = null;
                }
 
+               // <summary>
+               //   Returns a literalized version of a literal FieldInfo
+               // </summary>
+               static Expression Literalize (FieldInfo fi)
+               {
+                       Type t = fi.FieldType;
+                       object v = fi.GetValue (fi);
+
+                       if (t == TypeManager.int32_type)
+                               return new IntLiteral ((int) v);
+                       else if (t == TypeManager.uint32_type)
+                               return new UIntLiteral ((uint) v);
+                       else if (t == TypeManager.int64_type)
+                               return new LongLiteral ((long) v);
+                       else if (t == TypeManager.uint64_type)
+                               return new ULongLiteral ((ulong) v);
+                       else if (t == TypeManager.float_type)
+                               return new FloatLiteral ((float) v);
+                       else if (t == TypeManager.double_type)
+                               return new DoubleLiteral ((double) v);
+                       else if (t == TypeManager.string_type)
+                               return new StringLiteral ((string) v);
+                       else if (t == TypeManager.short_type)
+                               return new IntLiteral ((int) ((short)v));
+                       else if (t == TypeManager.ushort_type)
+                               return new IntLiteral ((int) ((ushort)v));
+                       else if (t == TypeManager.sbyte_type)
+                               return new IntLiteral ((int) ((sbyte)v));
+                       else if (t == TypeManager.byte_type)
+                               return new IntLiteral ((int) ((byte)v));
+                       else if (t == TypeManager.char_type)
+                               return new IntLiteral ((int) ((char)v));
+                       else
+                               throw new Exception ("Unknown type for literal (" + v.GetType () +
+                                                    "), details: " + fi);
+               }
+
                // 
                // Returns a fully formed expression after a MemberLookup
                //
-               static Expression ExprClassFromMemberInfo (MemberInfo mi)
+               static Expression ExprClassFromMemberInfo (TypeContainer tc, MemberInfo mi)
                {
                        if (mi is EventInfo){
                                return new EventExpr ((EventInfo) mi);
                        } else if (mi is FieldInfo){
-                               return new FieldExpr ((FieldInfo) mi);
+                               FieldInfo fi = (FieldInfo) mi;
+
+                               if (fi.IsLiteral){
+                                       Expression e = Literalize (fi);
+                                       e.Resolve (tc);
+
+                                       return e;
+                               } else
+                                       return new FieldExpr (fi);
                        } else if (mi is PropertyInfo){
                                return new PropertyExpr ((PropertyInfo) mi);
                        } else if (mi is Type)
@@ -201,7 +270,7 @@ namespace CIR {
                                return null;
                        
                        if (mi.Length == 1 && !(mi [0] is MethodBase))
-                               return Expression.ExprClassFromMemberInfo (mi [0]);
+                               return Expression.ExprClassFromMemberInfo (tc, mi [0]);
                        
                        for (int i = 0; i < mi.Length; i++)
                                if (!(mi [i] is MethodBase)){
@@ -256,9 +325,13 @@ namespace CIR {
                        string right = name.Substring (dot_pos + 1);
                        Type t;
 
-                       if ((t = tc.LookupType (left, false)) != null)
+                       if ((t = tc.LookupType (left, false)) != null){
+                               Expression e;
+                               
                                left_e = new TypeExpr (t);
-                       else {
+                               e = new MemberAccess (left_e, right);
+                               return e.Resolve (tc);
+                       } else {
                                //
                                // FIXME: IMplement:
                                
@@ -305,21 +378,53 @@ namespace CIR {
                                        return new EmptyCast (expr, target_type);
                                if (expr_type.IsValueType)
                                        return new BoxedCast (expr);
-                       } else if (expr_type.IsSubclassOf (target_type))
+                       } else if (expr_type.IsSubclassOf (target_type)) {
                                return new EmptyCast (expr, target_type);
-                       else 
-                               // FIXME: missing implicit reference conversions:
-                               // 
+                       } else {
                                // from any class-type S to any interface-type T.
+                               if (expr_type.IsClass && target_type.IsInterface) {
+                                       Type [] interfaces = expr_type.FindInterfaces (Module.FilterTypeName,
+                                                                                      target_type.FullName);
+                                       if (interfaces != null)
+                                               return new EmptyCast (expr, target_type);
+                               }       
+
                                // from any interface type S to interface-type T.
+                               // FIXME : Is it right to use IsAssignableFrom ?
+                               if (expr_type.IsInterface && target_type.IsInterface)
+                                       if (target_type.IsAssignableFrom (expr_type))
+                                               return new EmptyCast (expr, target_type);
+                               
+                               
                                // from an array-type S to an array-type of type T
+                               if (expr_type.IsArray && target_type.IsArray) {
+                                       
+                                       throw new Exception ("Implement array conversion");
+                                       
+                               }
+                               
                                // from an array-type to System.Array
+                               if (expr_type.IsArray && target_type.IsAssignableFrom (expr_type))
+                                       return new EmptyCast (expr, target_type);
+                               
                                // from any delegate type to System.Delegate
+                               if (expr_type.IsSubclassOf (TypeManager.delegate_type) &&
+                                   target_type == TypeManager.delegate_type)
+                                       if (target_type.IsAssignableFrom (expr_type))
+                                               return new EmptyCast (expr, target_type);
+                                       
                                // from any array-type or delegate type into System.ICloneable.
+                               if (expr_type.IsArray || expr_type.IsSubclassOf (TypeManager.delegate_type))
+                                       if (target_type == TypeManager.cloneable_interface)
+                                               throw new Exception ("Implement conversion to System.ICloneable");
+                               
                                // from the null type to any reference-type.
-                                    
+                               // FIXME : How do we do this ?
+
                                return null;
 
+                       }
+                       
                        return null;
                }
 
@@ -332,34 +437,44 @@ namespace CIR {
                        ArrayList args = new ArrayList ();
 
                        args.Add (new Argument (expr, Argument.AType.Expression));
-                       
+
                        Expression ne = new New (target.FullName, args,
-                                                new Location ("FIXME", 1, 1));
+                                                new Location (-1));
 
                        return ne.Resolve (tc);
                }
 
-               static int level = 0;
-               
                // <summary>
-               //   Converts implicitly the resolved expression `expr' into the
-               //   `target_type'.  It returns a new expression that can be used
-               //   in a context that expects a `target_type'. 
+               //   Implicit Numeric Conversions.
+               //
+               //   expr is the expression to convert, returns a new expression of type
+               //   target_type or null if an implicit conversion is not possible.
+               //
                // </summary>
-               static public Expression ConvertImplicit (TypeContainer tc, Expression expr, Type target_type)
+               static public Expression ImplicitNumericConversion (TypeContainer tc, Expression expr,
+                                                                   Type target_type, Location l)
                {
                        Type expr_type = expr.Type;
-
-                       Console.WriteLine ("ConvertImplicit " + expr_type + " => " + target_type);
-                       if (level != 0)
-                               throw new Exception ("Lame Loop Detector Triggered");
-                       
-                       if (expr_type == target_type)
-                               return expr;
                        
                        //
-                       // Step 1: Built-in conversions.
-                       //
+                       // Attempt to do the implicit constant expression conversions
+
+                       if (expr is IntLiteral){
+                               Expression e;
+                               
+                               e = TryImplicitIntConversion (target_type, (IntLiteral) expr);
+                               if (e != null)
+                                       return e;
+                       } else if (expr is LongLiteral){
+                               //
+                               // Try the implicit constant expression conversion
+                               // from long to ulong, instead of a nice routine,
+                               // we just inline it
+                               //
+                               if (((LongLiteral) expr).Value > 0)
+                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
+                       }
+                       
                        if (expr_type == TypeManager.sbyte_type){
                                //
                                // From sbyte to short, int, long, float, double.
@@ -367,7 +482,7 @@ namespace CIR {
                                if (target_type == TypeManager.int32_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
                                if (target_type == TypeManager.int64_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
+                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
                                if (target_type == TypeManager.double_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
                                if (target_type == TypeManager.float_type)
@@ -415,10 +530,11 @@ namespace CIR {
                                //
                                // From ushort to int, uint, long, ulong, float, double
                                //
-                               if ((target_type == TypeManager.uint32_type) ||
-                                   (target_type == TypeManager.uint64_type))
+                               if (target_type == TypeManager.uint32_type)
                                        return new EmptyCast (expr, target_type);
-                                       
+
+                               if (target_type == TypeManager.uint64_type)
+                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
                                if (target_type == TypeManager.int32_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
                                if (target_type == TypeManager.int64_type)
@@ -446,7 +562,7 @@ namespace CIR {
                                // From uint to long, ulong, float, double
                                //
                                if (target_type == TypeManager.int64_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
+                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
                                if (target_type == TypeManager.uint64_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
                                if (target_type == TypeManager.double_type)
@@ -460,7 +576,7 @@ namespace CIR {
                        } else if ((expr_type == TypeManager.uint64_type) ||
                                   (expr_type == TypeManager.int64_type)){
                                //
-                               // From long to float, double
+                               // From long/ulong to float, double
                                //
                                if (target_type == TypeManager.double_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
@@ -488,27 +604,145 @@ namespace CIR {
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
                                if (target_type == TypeManager.decimal_type)
                                        return InternalTypeConstructor (tc, expr, target_type);
-                       } 
+                       } else if (expr_type == TypeManager.float_type){
+                               //
+                               // float to double
+                               //
+                               if (target_type == TypeManager.double_type)
+                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
+                       }
 
-                       Expression e;
+                       return null;
+               }
+
+               // <summary>
+               //   User-defined implicit conversions
+               // </summary>
+               static public Expression ImplicitUserConversion (TypeContainer tc, Expression source,
+                                                                Type target, Location l)
+               {
+                       Expression mg1, mg2;
+                       MethodBase method;
+                       ArrayList arguments;
+                       
+                       mg1 = MemberLookup (tc, source.Type, "op_Implicit", false);
+                       mg2 = MemberLookup (tc, target, "op_Implicit", false);
+                       
+                       MethodGroupExpr union = Invocation.MakeUnionSet (mg1, mg2);
+
+                       if (union != null) {
+                               arguments = new ArrayList ();
+                               arguments.Add (new Argument (source, Argument.AType.Expression));
+
+                               method = Invocation.OverloadResolve (tc, union, arguments, l, true);
+
+                               if (method != null) { 
+                                       MethodInfo mi = (MethodInfo) method;
+                                       
+                                       if (mi.ReturnType == target)
+                                               return new UserImplicitCast (mi, arguments);
+                               }
+                       }
+                       
+                       // If we have a boolean type, we need to check for the True
+                       // and False operators too.
+                       
+                       if (target == TypeManager.bool_type) {
+
+                               mg1 = MemberLookup (tc, source.Type, "op_True", false);
+                               mg2 = MemberLookup (tc, target, "op_True", false);
+                               
+                               union = Invocation.MakeUnionSet (mg1, mg2);
+
+                               if (union == null)
+                                       return null;
+
+                               arguments = new ArrayList ();
+                               arguments.Add (new Argument (source, Argument.AType.Expression));
+                       
+                               method = Invocation.OverloadResolve (tc, union, arguments, l, true);
+                               if (method != null) {
+                                       MethodInfo mi = (MethodInfo) method;
+
+                                       if (mi.ReturnType == target) 
+                                               return new UserImplicitCast (mi, arguments);
+                               }
+                       }
                        
+                       return null;
+               }
+               
+               // <summary>
+               //   Converts implicitly the resolved expression `expr' into the
+               //   `target_type'.  It returns a new expression that can be used
+               //   in a context that expects a `target_type'. 
+               // </summary>
+               static public Expression ConvertImplicit (TypeContainer tc, Expression expr,
+                                                         Type target_type, Location l)
+               {
+                       Type expr_type = expr.Type;
+                       Expression e;
+
+                       if (expr_type == target_type)
+                               return expr;
+
+                       e = ImplicitNumericConversion (tc, expr, target_type, l);
+                       if (e != null)
+                               return e;
+
                        e = ImplicitReferenceConversion (expr, target_type);
-                       if (e != null){
+                       if (e != null)
                                return e;
-                       }
 
-                       level++;
-                       e = UserImplicitCast.CanConvert (tc, expr, target_type);
-                       level--;
+                       e = ImplicitUserConversion (tc, expr, target_type, l);
                        if (e != null)
                                return e;
-                       
-                       //
-                       //  Could not find an implicit cast.
-                       //
+
+                       if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
+                               IntLiteral i = (IntLiteral) expr;
+
+                               if (i.Value == 0)
+                                       return new EmptyCast (expr, target_type);
+                       }
                        return null;
                }
 
+               
+               // <summary>
+               //   Attempts to apply the `Standard Implicit
+               //   Conversion' rules to the expression `expr' into
+               //   the `target_type'.  It returns a new expression
+               //   that can be used in a context that expects a
+               //   `target_type'.
+               //
+               //   This is different from `ConvertImplicit' in that the
+               //   user defined implicit conversions are excluded. 
+               // </summary>
+               static public Expression ConvertImplicitStandard (TypeContainer tc, Expression expr,
+                                                                 Type target_type, Location l)
+               {
+                       Type expr_type = expr.Type;
+                       Expression e;
+
+                       if (expr_type == target_type)
+                               return expr;
+
+                       e = ImplicitNumericConversion (tc, expr, target_type, l);
+                       if (e != null)
+                               return e;
+
+                       e = ImplicitReferenceConversion (expr, target_type);
+                       if (e != null)
+                               return e;
+
+                       if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
+                               IntLiteral i = (IntLiteral) expr;
+
+                               if (i.Value == 0)
+                                       return new EmptyCast (expr, target_type);
+                       }
+                       return null;
+               }
                // <summary>
                //   Attemps to perform an implict constant conversion of the IntLiteral
                //   into a different data type using casts (See Implicit Constant
@@ -560,27 +794,10 @@ namespace CIR {
                {
                        Expression e;
                        
-                       e = ConvertImplicit (tc, target, type);
+                       e = ConvertImplicit (tc, target, type, l);
                        if (e != null)
                                return e;
                        
-                       //
-                       // Attempt to do the implicit constant expression conversions
-
-                       if (target is IntLiteral){
-                               e = TryImplicitIntConversion (type, (IntLiteral) target);
-                               if (e != null)
-                                       return e;
-                       } else if (target is LongLiteral){
-                               //
-                               // Try the implicit constant expression conversion
-                               // from long to ulong, instead of a nice routine,
-                               // we just inline it
-                               //
-                               if (((LongLiteral) target).Value > 0)
-                                       return target;
-                       }
-                       
                        string msg = "Can not convert implicitly from `"+
                                TypeManager.CSharpName (target.Type) + "' to `" +
                                TypeManager.CSharpName (type) + "'";
@@ -609,7 +826,7 @@ namespace CIR {
                                if (target_type == TypeManager.uint32_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
                                if (target_type == TypeManager.uint64_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
+                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
                                if (target_type == TypeManager.char_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
                        } else if (expr_type == TypeManager.byte_type){
@@ -633,7 +850,7 @@ namespace CIR {
                                if (target_type == TypeManager.uint32_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
                                if (target_type == TypeManager.uint64_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
+                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
                                if (target_type == TypeManager.char_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
                        } else if (expr_type == TypeManager.ushort_type){
@@ -663,7 +880,7 @@ namespace CIR {
                                if (target_type == TypeManager.uint32_type)
                                        return new EmptyCast (expr, target_type);
                                if (target_type == TypeManager.uint64_type)
-                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
+                                       return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
                                if (target_type == TypeManager.char_type)
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
                        } else if (expr_type == TypeManager.uint32_type){
@@ -792,6 +1009,24 @@ namespace CIR {
 
                        return null;
                }
+
+               // <summary>
+               //   Implements Explicit Reference conversions
+               // </summary>
+               static Expression ConvertReferenceExplicit (TypeContainer tc, Expression expr,
+                                                         Type target_type)
+               {
+                       Type expr_type = expr.Type;
+                       bool target_is_value_type = target_type.IsValueType;
+                       
+                       //
+                       // From object to any reference type
+                       //
+                       if (expr_type == TypeManager.object_type && !target_is_value_type)
+                               return new ClassCast (expr, expr_type);
+
+                       return null;
+               }
                
                // <summary>
                //   Performs an explicit conversion of the expression `expr' whose
@@ -800,7 +1035,7 @@ namespace CIR {
                static public Expression ConvertExplicit (TypeContainer tc, Expression expr,
                                                          Type target_type)
                {
-                       Expression ne = ConvertImplicit (tc, expr, target_type);
+                       Expression ne = ConvertImplicit (tc, expr, target_type, Location.Null);
 
                        if (ne != null)
                                return ne;
@@ -809,8 +1044,11 @@ namespace CIR {
                        if (ne != null)
                                return ne;
 
+                       ne = ConvertReferenceExplicit (tc, expr, target_type);
+                       if (ne != null)
+                               return ne;
                        
-                       return expr;
+                       return null;
                }
 
                static string ExprClassName (ExprClass c)
@@ -843,9 +1081,14 @@ namespace CIR {
                // <summary>
                //   Reports that we were expecting `expr' to be of class `expected'
                // </summary>
-               protected void report118 (TypeContainer tc, Expression expr, string expected)
+               protected void report118 (TypeContainer tc, Location l, Expression expr, string expected)
                {
-                       Error (tc, 118, "Expression denotes a '" + ExprClassName (expr.ExprClass) +
+                       string kind = "Unknown";
+                       
+                       if (expr != null)
+                               kind = ExprClassName (expr.ExprClass);
+
+                       Error (tc, 118, l, "Expression denotes a '" + kind +
                               "' where an " + expected + " was expected");
                }
        }
@@ -892,7 +1135,7 @@ namespace CIR {
                        this.child = child;
                }
 
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (TypeContainer tc)
                {
                        // This should never be invoked, we are born in fully
                        // initialized state.
@@ -919,7 +1162,7 @@ namespace CIR {
                {
                }
 
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (TypeContainer tc)
                {
                        // This should never be invoked, we are born in fully
                        // initialized state.
@@ -960,7 +1203,7 @@ namespace CIR {
                        second_valid = true;
                }
 
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (TypeContainer tc)
                {
                        // This should never be invoked, we are born in fully
                        // initialized state.
@@ -979,6 +1222,34 @@ namespace CIR {
                
        }
 
+       // <summary>
+       //   This kind of cast is used to encapsulate a child and cast it
+       //   to the class requested
+       // </summary>
+       public class ClassCast : EmptyCast {
+               public ClassCast (Expression child, Type return_type)
+                       : base (child, return_type)
+                       
+               {
+               }
+
+               public override Expression DoResolve (TypeContainer tc)
+               {
+                       // This should never be invoked, we are born in fully
+                       // initialized state.
+
+                       return this;
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       base.Emit (ec);
+
+                       ec.ig.Emit (OpCodes.Castclass, type);
+               }                       
+               
+       }
+       
        // <summary>
        //   Unary expressions.  
        // </summary>
@@ -1060,7 +1331,7 @@ namespace CIR {
                        if (expr.Type == target_type)
                                return expr;
 
-                       return ConvertImplicit (tc, expr, target_type);
+                       return ConvertImplicit (tc, expr, target_type, new Location (-1));
                }
 
                void report23 (Report r, Type t)
@@ -1109,12 +1380,16 @@ namespace CIR {
                                op_name = "op_" + oper;
 
                        mg = MemberLookup (tc, expr_type, op_name, false);
+
+                       if (mg == null && expr_type != TypeManager.object_type)
+                               mg = MemberLookup (tc, expr_type.BaseType, op_name, false);
                        
                        if (mg != null) {
                                Arguments = new ArrayList ();
                                Arguments.Add (new Argument (expr, Argument.AType.Expression));
                                
-                               method = Invocation.OverloadResolve (tc, (MethodGroupExpr) mg, Arguments, location);
+                               method = Invocation.OverloadResolve (tc, (MethodGroupExpr) mg,
+                                                                    Arguments, location);
                                if (method != null) {
                                        MethodInfo mi = (MethodInfo) method;
 
@@ -1208,6 +1483,8 @@ namespace CIR {
                                // It is also not clear if we should convert to Float
                                // or Double initially.
                                //
+                               Location l = new Location (-1);
+                               
                                if (expr_type == TypeManager.uint32_type){
                                        //
                                        // FIXME: handle exception to this rule that
@@ -1215,7 +1492,7 @@ namespace CIR {
                                        // bt written as a decimal interger literal
                                        //
                                        type = TypeManager.int64_type;
-                                       expr = ConvertImplicit (tc, expr, type);
+                                       expr = ConvertImplicit (tc, expr, type, l);
                                        return this;
                                }
 
@@ -1229,21 +1506,21 @@ namespace CIR {
                                        return null;
                                }
 
-                               e = ConvertImplicit (tc, expr, TypeManager.int32_type);
+                               e = ConvertImplicit (tc, expr, TypeManager.int32_type, l);
                                if (e != null){
                                        expr = e;
                                        type = e.Type;
                                        return this;
                                } 
 
-                               e = ConvertImplicit (tc, expr, TypeManager.int64_type);
+                               e = ConvertImplicit (tc, expr, TypeManager.int64_type, l);
                                if (e != null){
                                        expr = e;
                                        type = e.Type;
                                        return this;
                                }
 
-                               e = ConvertImplicit (tc, expr, TypeManager.double_type);
+                               e = ConvertImplicit (tc, expr, TypeManager.double_type, l);
                                if (e != null){
                                        expr = e;
                                        type = e.Type;
@@ -1278,7 +1555,8 @@ namespace CIR {
                                        //
                                        throw new Exception ("Implement me");
                                } else {
-                                       report118 (tc, expr, "variable, indexer or property access");
+                                       report118 (tc, location, expr,
+                                                  "variable, indexer or property access");
                                }
                        }
 
@@ -1296,13 +1574,14 @@ namespace CIR {
 
                }
 
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (TypeContainer tc)
                {
                        expr = expr.Resolve (tc);
 
                        if (expr == null)
                                return null;
-                       
+
+                       eclass = ExprClass.Value;
                        return ResolveOperator (tc);
                }
 
@@ -1454,7 +1733,7 @@ namespace CIR {
                        }
                }
                
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (TypeContainer tc)
                {
                        probe_type = tc.LookupType (ProbeType, false);
 
@@ -1471,12 +1750,16 @@ namespace CIR {
 
                public override void Emit (EmitContext ec)
                {
+                       ILGenerator ig = ec.ig;
+                       
                        expr.Emit (ec);
                        
                        if (Oper == Operator.Is){
-                               ec.ig.Emit (OpCodes.Isinst, probe_type);
+                               ig.Emit (OpCodes.Isinst, probe_type);
+                               ig.Emit (OpCodes.Ldnull);
+                               ig.Emit (OpCodes.Cgt_Un);
                        } else {
-                               throw new Exception ("Implement as");
+                               ig.Emit (OpCodes.Isinst, probe_type);
                        }
                }
        }
@@ -1512,7 +1795,7 @@ namespace CIR {
                        }
                }
                
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (TypeContainer tc)
                {
                        expr = expr.Resolve (tc);
                        if (expr == null)
@@ -1525,7 +1808,7 @@ namespace CIR {
                                return null;
 
                        expr = ConvertExplicit (tc, expr, type);
-                       
+
                        return expr;
                }
 
@@ -1647,7 +1930,7 @@ namespace CIR {
                        if (expr.Type == target_type)
                                return expr;
 
-                       return ConvertImplicit (tc, expr, target_type);
+                       return ConvertImplicit (tc, expr, target_type, new Location (-1));
                }
                
                //
@@ -1662,9 +1945,9 @@ namespace CIR {
                                // conveted to type double.
                                //
                                if (r != TypeManager.double_type)
-                                       right = ConvertImplicit (tc, right, TypeManager.double_type);
+                                       right = ConvertImplicit (tc, right, TypeManager.double_type, location);
                                if (l != TypeManager.double_type)
-                                       left = ConvertImplicit (tc, left, TypeManager.double_type);
+                                       left = ConvertImplicit (tc, left, TypeManager.double_type, location);
                                
                                type = TypeManager.double_type;
                        } else if (l == TypeManager.float_type || r == TypeManager.float_type){
@@ -1673,22 +1956,34 @@ namespace CIR {
                                // converd to type float.
                                //
                                if (r != TypeManager.double_type)
-                                       right = ConvertImplicit (tc, right, TypeManager.float_type);
+                                       right = ConvertImplicit (tc, right, TypeManager.float_type, location);
                                if (l != TypeManager.double_type)
-                                       left = ConvertImplicit (tc, left, TypeManager.float_type);
+                                       left = ConvertImplicit (tc, left, TypeManager.float_type, location);
                                type = TypeManager.float_type;
                        } else if (l == TypeManager.uint64_type || r == TypeManager.uint64_type){
+                               Expression e;
+                               Type other;
                                //
                                // If either operand is of type ulong, the other operand is
                                // converted to type ulong.  or an error ocurrs if the other
                                // operand is of type sbyte, short, int or long
                                //
-                               Type other = null;
                                
-                               if (l == TypeManager.uint64_type)
-                                       other = r;
-                               else if (r == TypeManager.uint64_type)
-                                       other = l;
+                               if (l == TypeManager.uint64_type){
+                                       if (r != TypeManager.uint64_type && right is IntLiteral){
+                                               e = TryImplicitIntConversion (l, (IntLiteral) right);
+                                               if (e != null)
+                                                       right = e;
+                                       }
+                                       other = right.Type;
+                               } else {
+                                       if (left is IntLiteral){
+                                               e = TryImplicitIntConversion (r, (IntLiteral) left);
+                                               if (e != null)
+                                                       left = e;
+                                       }
+                                       other = left.Type;
+                               }
 
                                if ((other == TypeManager.sbyte_type) ||
                                    (other == TypeManager.short_type) ||
@@ -1696,7 +1991,7 @@ namespace CIR {
                                    (other == TypeManager.int64_type)){
                                        string oper = OperName ();
                                        
-                                       Error (tc, 34, "Operator `" + OperName ()
+                                       Error (tc, 34, location, "Operator `" + OperName ()
                                               + "' is ambiguous on operands of type `"
                                               + TypeManager.CSharpName (l) + "' "
                                               + "and `" + TypeManager.CSharpName (r)
@@ -1709,9 +2004,9 @@ namespace CIR {
                                // to type long.
                                //
                                if (l != TypeManager.int64_type)
-                                       left = ConvertImplicit (tc, left, TypeManager.int64_type);
+                                       left = ConvertImplicit (tc, left, TypeManager.int64_type, location);
                                if (r != TypeManager.int64_type)
-                                       right = ConvertImplicit (tc, right, TypeManager.int64_type);
+                                       right = ConvertImplicit (tc, right, TypeManager.int64_type, location);
 
                                type = TypeManager.int64_type;
                        } else if (l == TypeManager.uint32_type || r == TypeManager.uint32_type){
@@ -1739,14 +2034,14 @@ namespace CIR {
                                        // operand is converd to type uint
                                        //
                                        left = ForceConversion (tc, left, TypeManager.uint32_type);
-                                       right = ForceConversion (tc, left, TypeManager.uint32_type);
+                                       right = ForceConversion (tc, right, TypeManager.uint32_type);
                                        type = TypeManager.uint32_type;
                                } 
                        } else if (l == TypeManager.decimal_type || r == TypeManager.decimal_type){
                                if (l != TypeManager.decimal_type)
-                                       left = ConvertImplicit (tc, left, TypeManager.decimal_type);
+                                       left = ConvertImplicit (tc, left, TypeManager.decimal_type, location);
                                if (r != TypeManager.decimal_type)
-                                       right = ConvertImplicit (tc, right, TypeManager.decimal_type);
+                                       right = ConvertImplicit (tc, right, TypeManager.decimal_type, location);
 
                                type = TypeManager.decimal_type;
                        } else {
@@ -1778,11 +2073,14 @@ namespace CIR {
                        }
                        right = e;
 
-                       if (((e = ConvertImplicit (tc, left, TypeManager.int32_type)) != null) ||
-                           ((e = ConvertImplicit (tc, left, TypeManager.uint32_type)) != null) ||
-                           ((e = ConvertImplicit (tc, left, TypeManager.int64_type)) != null) ||
-                           ((e = ConvertImplicit (tc, left, TypeManager.uint64_type)) != null)){
+                       Location loc = location;
+                       
+                       if (((e = ConvertImplicit (tc, left, TypeManager.int32_type, loc)) != null) ||
+                           ((e = ConvertImplicit (tc, left, TypeManager.uint32_type, loc)) != null) ||
+                           ((e = ConvertImplicit (tc, left, TypeManager.int64_type, loc)) != null) ||
+                           ((e = ConvertImplicit (tc, left, TypeManager.uint64_type, loc)) != null)){
                                left = e;
+                               type = e.Type;
 
                                return this;
                        }
@@ -1804,15 +2102,21 @@ namespace CIR {
 
                        left_expr = MemberLookup (tc, l, op, false);
 
+                       if (left_expr == null && l != TypeManager.object_type)
+                               left_expr = MemberLookup (tc, l.BaseType, op, false);
+                       
                        right_expr = MemberLookup (tc, r, op, false);
+                       if (right_expr != null && r != TypeManager.object_type)
+                               right_expr = MemberLookup (tc, r.BaseType, op, false);
+                       
 
                        MethodGroupExpr union = Invocation.MakeUnionSet (left_expr, right_expr);
 
-                       Arguments = new ArrayList ();
-                       Arguments.Add (new Argument (left, Argument.AType.Expression));
-                       Arguments.Add (new Argument (right, Argument.AType.Expression));
-                       
                        if (union != null) {
+                               Arguments = new ArrayList ();
+                               Arguments.Add (new Argument (left, Argument.AType.Expression));
+                               Arguments.Add (new Argument (right, Argument.AType.Expression));
+                       
                                method = Invocation.OverloadResolve (tc, union, Arguments, location);
                                if (method != null) {
                                        MethodInfo mi = (MethodInfo) method;
@@ -1829,14 +2133,62 @@ namespace CIR {
                        // Only perform numeric promotions on:
                        // +, -, *, /, %, &, |, ^, ==, !=, <, >, <=, >=
                        //
-                       if (oper == Operator.LeftShift || oper == Operator.RightShift){
+                       if (oper == Operator.Addition){
+                               //
+                               // If any of the arguments is a string, cast to string
+                               //
+                               if (l == TypeManager.string_type){
+                                       if (r == TypeManager.string_type){
+                                               // string + string
+                                               method = TypeManager.string_concat_string_string;
+                                       } else {
+                                               // string + object
+                                               method = TypeManager.string_concat_object_object;
+                                               right = ConvertImplicit (tc, right,
+                                                                        TypeManager.object_type, location);
+                                       }
+                                       type = TypeManager.string_type;
+
+                                       Arguments = new ArrayList ();
+                                       Arguments.Add (new Argument (left, Argument.AType.Expression));
+                                       Arguments.Add (new Argument (right, Argument.AType.Expression));
+
+                                       return this;
+                                       
+                               } else if (r == TypeManager.string_type){
+                                       // object + string
+                                       method = TypeManager.string_concat_object_object;
+                                       Arguments = new ArrayList ();
+                                       Arguments.Add (new Argument (left, Argument.AType.Expression));
+                                       Arguments.Add (new Argument (right, Argument.AType.Expression));
+
+                                       left = ConvertImplicit (tc, left, TypeManager.object_type, location);
+                                       type = TypeManager.string_type;
+
+                                       return this;
+                               }
+
+                               //
+                               // FIXME: is Delegate operator + (D x, D y) handled?
+                               //
+                       }
+                       
+                       if (oper == Operator.LeftShift || oper == Operator.RightShift)
                                return CheckShiftArguments (tc);
-                       } else if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
 
+                       if (oper == Operator.LogicalOr || oper == Operator.LogicalAnd){
                                if (l != TypeManager.bool_type || r != TypeManager.bool_type)
                                        error19 (tc);
-                       } else
-                               DoNumericPromotions (tc, l, r);
+
+                               type = TypeManager.bool_type;
+                               return this;
+                       } 
+
+                       //
+                       // We are dealing with numbers
+                       //
+
+                       DoNumericPromotions (tc, l, r);
 
                        if (left == null || right == null)
                                return null;
@@ -1851,6 +2203,7 @@ namespace CIR {
                                        error19 (tc);
                                        return null;
                                }
+                               type = l;
                        }
 
                        if (oper == Operator.Equality ||
@@ -1865,7 +2218,7 @@ namespace CIR {
                        return this;
                }
                
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (TypeContainer tc)
                {
                        left = left.Resolve (tc);
                        right = right.Resolve (tc);
@@ -1873,6 +2226,17 @@ namespace CIR {
                        if (left == null || right == null)
                                return null;
 
+                       if (left.Type == null)
+                               throw new Exception (
+                                       "Resolve returned non null, but did not set the type! (" +
+                                       left + ")");
+                       if (right.Type == null)
+                               throw new Exception (
+                                       "Resolve returned non null, but did not set the type! (" +
+                                       right + ")");
+
+                       eclass = ExprClass.Value;
+
                        return ResolveOperator (tc);
                }
 
@@ -2104,12 +2468,14 @@ namespace CIR {
 
        public class Conditional : Expression {
                Expression expr, trueExpr, falseExpr;
+               Location l;
                
-               public Conditional (Expression expr, Expression trueExpr, Expression falseExpr)
+               public Conditional (Expression expr, Expression trueExpr, Expression falseExpr, Location l)
                {
                        this.expr = expr;
                        this.trueExpr = trueExpr;
                        this.falseExpr = falseExpr;
+                       this.l = l;
                }
 
                public Expression Expr {
@@ -2130,15 +2496,62 @@ namespace CIR {
                        }
                }
 
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (TypeContainer tc)
                {
-                       // FIXME: Implement;
-                       throw new Exception ("Unimplemented");
-                       // return this;
+                       expr = expr.Resolve (tc);
+
+                       if (expr.Type != TypeManager.bool_type)
+                               expr = Expression.ConvertImplicitRequired (
+                                       tc, expr, TypeManager.bool_type, l);
+                       
+                       trueExpr = trueExpr.Resolve (tc);
+                       falseExpr = falseExpr.Resolve (tc);
+
+                       if (expr == null || trueExpr == null || falseExpr == null)
+                               return null;
+                       
+                       if (trueExpr.Type == falseExpr.Type)
+                               type = trueExpr.Type;
+                       else {
+                               Expression conv;
+
+                               //
+                               // First, if an implicit conversion exists from trueExpr
+                               // to falseExpr, then the result type is of type falseExpr.Type
+                               //
+                               conv = ConvertImplicit (tc, trueExpr, falseExpr.Type, l);
+                               if (conv != null){
+                                       type = falseExpr.Type;
+                                       trueExpr = conv;
+                               } else if ((conv = ConvertImplicit (tc,falseExpr,trueExpr.Type,l)) != null){
+                                       type = trueExpr.Type;
+                                       falseExpr = conv;
+                               } else {
+                                       Error (tc, 173, l, "The type of the conditional expression can " +
+                                              "not be computed because there is no implicit conversion" +
+                                              " from `" + TypeManager.CSharpName (trueExpr.Type) + "'" +
+                                              " and `" + TypeManager.CSharpName (falseExpr.Type) + "'");
+                                       return null;
+                               }
+                       }
+
+                       eclass = ExprClass.Value;
+                       return this;
                }
 
                public override void Emit (EmitContext ec)
                {
+                       ILGenerator ig = ec.ig;
+                       Label false_target = ig.DefineLabel ();
+                       Label end_target = ig.DefineLabel ();
+
+                       expr.Emit (ec);
+                       ig.Emit (OpCodes.Brfalse, false_target);
+                       trueExpr.Emit (ec);
+                       ig.Emit (OpCodes.Br, end_target);
+                       ig.MarkLabel (false_target);
+                       falseExpr.Emit (ec);
+                       ig.MarkLabel (end_target);
                }
        }
 
@@ -2232,7 +2645,7 @@ namespace CIR {
                // simple_names and qualified_identifiers are placed on
                // the tree equally.
                //
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (TypeContainer tc)
                {
                        if (Name.IndexOf (".") != -1)
                                return ResolveMemberAccess (tc, Name);
@@ -2282,7 +2695,7 @@ namespace CIR {
                        }
                }
                
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (TypeContainer tc)
                {
                        VariableInfo vi = Block.GetVariableInfo (Name);
 
@@ -2296,6 +2709,8 @@ namespace CIR {
                        ILGenerator ig = ec.ig;
                        int idx = vi.Idx;
 
+                       vi.Used = true;
+                       
                        switch (idx){
                        case 0:
                                ig.Emit (OpCodes.Ldloc_0);
@@ -2327,7 +2742,8 @@ namespace CIR {
                        ILGenerator ig = ec.ig;
                        VariableInfo vi = VariableInfo;
                        int idx = vi.Idx;
-                                       
+
+                       vi.Assigned = true;
                        switch (idx){
                        case 0:
                                ig.Emit (OpCodes.Stloc_0);
@@ -2358,6 +2774,9 @@ namespace CIR {
                {
                        VariableInfo vi = VariableInfo;
                        int idx = vi.Idx;
+
+                       vi.Used = true;
+                       vi.Assigned = true;
                        
                        if (idx <= 255)
                                ec.ig.Emit (OpCodes.Ldloca_S, (byte) idx);
@@ -2379,7 +2798,7 @@ namespace CIR {
                        eclass = ExprClass.Variable;
                }
 
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (TypeContainer tc)
                {
                        Type [] types = Pars.GetParameterInfo (tc);
 
@@ -2493,90 +2912,6 @@ namespace CIR {
                        }
                }
 
-               /// <summary>
-               ///   Computes whether Argument `a' and the Type t of the  ParameterInfo `pi' are
-               ///   compatible, and if so, how good is the match (in terms of
-               ///   "better conversions" (7.4.2.3).
-               ///
-               ///   0   is the best possible match.
-               ///   -1  represents a type mismatch.
-               ///   -2  represents a ref/out mismatch.
-               /// </summary>
-               static int Badness (Argument a, Type t)
-               {
-                       Expression argument_expr = a.Expr;
-                       Type argument_type = argument_expr.Type;
-
-                       if (argument_type == null){
-                               throw new Exception ("Expression of type " + a.Expr + " does not resolve its type");
-                       }
-                       
-                       if (t == argument_type) 
-                               return 0;
-
-                       //
-                       // Now probe whether an implicit constant expression conversion
-                       // can be used.
-                       //
-                       // An implicit constant expression conversion permits the following
-                       // conversions:
-                       //
-                       //    * A constant-expression of type `int' can be converted to type
-                       //      sbyte, byute, short, ushort, uint, ulong provided the value of
-                       //      of the expression is withing the range of the destination type.
-                       //
-                       //    * A constant-expression of type long can be converted to type
-                       //      ulong, provided the value of the constant expression is not negative
-                       //
-                       // FIXME: Note that this assumes that constant folding has
-                       // taken place.  We dont do constant folding yet.
-                       //
-
-                       if (argument_type == TypeManager.int32_type && argument_expr is IntLiteral){
-                               IntLiteral ei = (IntLiteral) argument_expr;
-                               int value = ei.Value;
-                               
-                               if (t == TypeManager.sbyte_type){
-                                       if (value >= SByte.MinValue && value <= SByte.MaxValue)
-                                               return 1;
-                               } else if (t == TypeManager.byte_type){
-                                       if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
-                                               return 1;
-                               } else if (t == TypeManager.short_type){
-                                       if (value >= Int16.MinValue && value <= Int16.MaxValue)
-                                               return 1;
-                               } else if (t == TypeManager.ushort_type){
-                                       if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
-                                               return 1;
-                               } else if (t == TypeManager.uint32_type){
-                                       //
-                                       // we can optimize this case: a positive int32
-                                       // always fits on a uint32
-                                       //
-                                       if (value >= 0)
-                                               return 1;
-                               } else if (t == TypeManager.uint64_type){
-                                       //
-                                       // we can optimize this case: a positive int32
-                                       // always fits on a uint64
-                                       //
-                                       if (value >= 0)
-                                               return 1;
-                               }
-                       } else if (argument_type == TypeManager.int64_type && argument_expr is LongLiteral){
-                               LongLiteral ll = (LongLiteral) argument_expr;
-
-                               if (t == TypeManager.uint64_type)
-                                       if (ll.Value > 0)
-                                               return 1;
-                       }
-                       
-                       // FIXME: Implement user-defined implicit conversions here.
-                       // FIXME: Implement better conversion here.
-                       
-                       return -1;
-               }
-
                // <summary>
                //   Returns the Parameters (a ParameterData interface) for the
                //   Method `mb'
@@ -2604,6 +2939,12 @@ namespace CIR {
                        }
                }
 
+               // <summary>
+               //   Tells whether a user defined conversion from Type `from' to
+               //   Type `to' exists.
+               //
+               //   FIXME: we could implement a cache here. 
+               // </summary>
                static bool ConversionExists (TypeContainer tc, Type from, Type to)
                {
                        // Locate user-defined implicit operators
@@ -2648,7 +2989,7 @@ namespace CIR {
                //  Returns : 1 if a->p is better
                //            0 if a->q or neither is better 
                // </summary>
-               static int BetterConversion (TypeContainer tc, Argument a, Type p, Type q)
+               static int BetterConversion (TypeContainer tc, Argument a, Type p, Type q, bool use_standard)
                {
                        
                        Type argument_type = a.Expr.Type;
@@ -2684,7 +3025,7 @@ namespace CIR {
                        // taken place.  We dont do constant folding yet.
                        //
 
-                       if (argument_type == TypeManager.int32_type && argument_expr is IntLiteral){
+                       if (argument_expr is IntLiteral){
                                IntLiteral ei = (IntLiteral) argument_expr;
                                int value = ei.Value;
                                
@@ -2728,7 +3069,10 @@ namespace CIR {
 
                                Expression tmp;
 
-                               tmp = ConvertImplicit (tc, argument_expr, p);
+                               if (use_standard)
+                                       tmp = ConvertImplicitStandard (tc, argument_expr, p, Location.Null);
+                               else
+                                       tmp = ConvertImplicit (tc, argument_expr, p, Location.Null);
 
                                if (tmp != null)
                                        return 1;
@@ -2736,11 +3080,11 @@ namespace CIR {
                                        return 0;
 
                        }
-                       
+
                        if (ConversionExists (tc, p, q) == true &&
                            ConversionExists (tc, q, p) == false)
                                return 1;
-                       
+
                        if (p == TypeManager.sbyte_type)
                                if (q == TypeManager.byte_type || q == TypeManager.ushort_type ||
                                    q == TypeManager.uint32_type || q == TypeManager.uint64_type)
@@ -2767,7 +3111,9 @@ namespace CIR {
                //  0 if candidate ain't better
                //  1 if candidate is better than the current best match
                // </summary>
-               static int BetterFunction (TypeContainer tc, ArrayList args, MethodBase candidate, MethodBase best)
+               static int BetterFunction (TypeContainer tc, ArrayList args,
+                                          MethodBase candidate, MethodBase best,
+                                          bool use_standard)
                {
                        ParameterData candidate_pd = GetParameterData (candidate);
                        ParameterData best_pd;
@@ -2789,11 +3135,11 @@ namespace CIR {
                                                
                                                Argument a = (Argument) args [j];
                                                
-                                               x = BetterConversion (tc, a, candidate_pd.ParameterType (j), null);
+                                               x = BetterConversion (
+                                                       tc, a, candidate_pd.ParameterType (j), null,
+                                                       use_standard);
                                                
-                                               if (x > 0)
-                                                       continue;
-                                               else 
+                                               if (x <= 0)
                                                        break;
                                        }
                                        
@@ -2818,9 +3164,9 @@ namespace CIR {
                                        Argument a = (Argument) args [j];
 
                                        x = BetterConversion (tc, a, candidate_pd.ParameterType (j),
-                                                             best_pd.ParameterType (j));
+                                                             best_pd.ParameterType (j), use_standard);
                                        y = BetterConversion (tc, a, best_pd.ParameterType (j),
-                                                             candidate_pd.ParameterType (j));
+                                                             candidate_pd.ParameterType (j), use_standard);
                                        
                                        rating1 += x;
                                        rating2 += y;
@@ -2885,7 +3231,7 @@ namespace CIR {
                        return null;
 
                }
-               
+
                // <summary>
                //   Find the Applicable Function Members (7.4.2.1)
                //
@@ -2895,12 +3241,19 @@ namespace CIR {
                //
                //   Arguments: ArrayList containing resolved Argument objects.
                //
+               //   loc: The location if we want an error to be reported, or a Null
+               //        location for "probing" purposes.
+               //
+               //   inside_user_defined: controls whether OverloadResolve should use the 
+               //   ConvertImplicit or ConvertImplicitStandard during overload resolution.
+               //
                //   Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
                //            that is the best match of me on Arguments.
                //
                // </summary>
                public static MethodBase OverloadResolve (TypeContainer tc, MethodGroupExpr me,
-                                                         ArrayList Arguments, Location loc)
+                                                         ArrayList Arguments, Location loc,
+                                                         bool use_standard)
                {
                        ArrayList afm = new ArrayList ();
                        int best_match_idx = -1;
@@ -2912,7 +3265,7 @@ namespace CIR {
                                MethodBase candidate  = me.Methods [i];
                                int x;
 
-                               x = BetterFunction (tc, Arguments, candidate, method);
+                               x = BetterFunction (tc, Arguments, candidate, method, use_standard);
                                
                                if (x == 0)
                                        continue;
@@ -2959,32 +3312,48 @@ namespace CIR {
                                j--;
                                Argument a = (Argument) Arguments [j];
                                Expression a_expr = a.Expr;
+                               Type parameter_type = pd.ParameterType (j);
                                
-                               Expression conv = ConvertImplicit (tc, a_expr, pd.ParameterType (j));
-
-                               if (conv == null) {
-                                       Error (tc, 1502, loc,
-                                              "The best overloaded match for method '" + FullMethodDesc (method) +
-                                              "' has some invalid arguments");
-                                       Error (tc, 1503, loc,
-                                              "Argument " + (j+1) +
-                                              " : Cannot convert from '" + TypeManager.CSharpName (a_expr.Type)
-                                              + "' to '" + TypeManager.CSharpName (pd.ParameterType (j)) + "'");
-                                       return null;
-                               }
+                               if (a_expr.Type != parameter_type){
+                                       Expression conv;
 
-                               //
-                               // Update the argument with the implicit conversion
-                               //
-                               if (a_expr != conv)
-                                       a.Expr = conv;
+                                       if (use_standard)
+                                               conv = ConvertImplicitStandard (tc, a_expr, parameter_type,
+                                                                               Location.Null);
+                                       else
+                                               conv = ConvertImplicit (tc, a_expr, parameter_type,
+                                                                       Location.Null);
+
+                                       if (conv == null){
+                                               if (!Location.IsNull (loc)) {
+                                                       Error (tc, 1502, loc,
+                                                              "The best overloaded match for method '" + FullMethodDesc (method) +
+                                                              "' has some invalid arguments");
+                                                       Error (tc, 1503, loc,
+                                                              "Argument " + (j+1) +
+                                                              ": Cannot convert from '" + TypeManager.CSharpName (a_expr.Type)
+                                                              + "' to '" + TypeManager.CSharpName (pd.ParameterType (j)) + "'");
+                                               }
+                                               return null;
+                                       }
+                                       //
+                                       // Update the argument with the implicit conversion
+                                       //
+                                       if (a_expr != conv)
+                                               a.Expr = conv;
+                               }
                        }
                        
                        return method;
                }
 
+               public static MethodBase OverloadResolve (TypeContainer tc, MethodGroupExpr me,
+                                                         ArrayList Arguments, Location loc)
+               {
+                       return OverloadResolve (tc, me, Arguments, loc, false);
+               }
                        
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (TypeContainer tc)
                {
                        //
                        // First, resolve the expression that is used to
@@ -2995,7 +3364,7 @@ namespace CIR {
                                return null;
 
                        if (!(this.expr is MethodGroupExpr)){
-                               report118 (tc, this.expr, "method group");
+                               report118 (tc, Location, this.expr, "method group");
                                return null;
                        }
 
@@ -3012,7 +3381,8 @@ namespace CIR {
                                }
                        }
 
-                       method = OverloadResolve (tc, (MethodGroupExpr) this.expr, Arguments, Location);
+                       method = OverloadResolve (tc, (MethodGroupExpr) this.expr, Arguments,
+                                                 Location);
 
                        if (method == null){
                                Error (tc, -6, Location,
@@ -3023,6 +3393,7 @@ namespace CIR {
                        if (method is MethodInfo)
                                type = ((MethodInfo)method).ReturnType;
 
+                       eclass = ExprClass.Value;
                        return this;
                }
 
@@ -3128,7 +3499,7 @@ namespace CIR {
                        Location      = loc;
                }
                
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (TypeContainer tc)
                {
                        type = tc.LookupType (RequestedType, false);
 
@@ -3144,7 +3515,7 @@ namespace CIR {
                                //
                                // FIXME: Find proper error
                                //
-                               report118 (tc, ml, "method group");
+                               report118 (tc, Location, ml, "method group");
                                return null;
                        }
                        
@@ -3158,14 +3529,16 @@ namespace CIR {
                                }
                        }
 
-                       method = Invocation.OverloadResolve (tc, (MethodGroupExpr) ml, Arguments, Location);
+                       method = Invocation.OverloadResolve (tc, (MethodGroupExpr) ml, Arguments,
+                                                            Location);
 
                        if (method == null) {
                                Error (tc, -6, Location,
                                       "New invocation: Can not find a constructor for this argument list");
                                return null;
                        }
-                       
+
+                       eclass = ExprClass.Value;
                        return this;
                }
 
@@ -3186,7 +3559,7 @@ namespace CIR {
        // Represents the `this' construct
        //
        public class This : Expression, LValue {
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (TypeContainer tc)
                {
                        eclass = ExprClass.Variable;
                        type = tc.TypeBuilder;
@@ -3219,29 +3592,34 @@ namespace CIR {
                }
        }
 
+       // <summary>
+       //   Implements the typeof operator
+       // </summary>
        public class TypeOf : Expression {
                public readonly string QueriedType;
+               Type typearg;
                
                public TypeOf (string queried_type)
                {
                        QueriedType = queried_type;
                }
 
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (TypeContainer tc)
                {
-                       type = tc.LookupType (QueriedType, false);
+                       typearg = tc.LookupType (QueriedType, false);
 
-                       if (type == null)
+                       if (typearg == null)
                                return null;
-                       
+
+                       type = TypeManager.type_type;
                        eclass = ExprClass.Type;
                        return this;
                }
 
                public override void Emit (EmitContext ec)
                {
-                       throw new Exception ("Implement me");
-                       // FIXME: Implement.
+                       ec.ig.Emit (OpCodes.Ldtoken, typearg);
+                       ec.ig.Emit (OpCodes.Call, TypeManager.system_type_get_type_from_handle);
                }
        }
 
@@ -3253,7 +3631,7 @@ namespace CIR {
                        this.QueriedType = queried_type;
                }
 
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (TypeContainer tc)
                {
                        // FIXME: Implement;
                        throw new Exception ("Unimplemented");
@@ -3283,7 +3661,7 @@ namespace CIR {
                        }
                }
                
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (TypeContainer tc)
                {
                        Expression new_expression = expr.Resolve (tc);
 
@@ -3325,7 +3703,7 @@ namespace CIR {
 
                public override void Emit (EmitContext ec)
                {
-                       throw new Exception ("Implement me");
+                       throw new Exception ("Should not happen I think");
                }
 
        }
@@ -3346,7 +3724,7 @@ namespace CIR {
                        eclass = ExprClass.Namespace;
                }
 
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (TypeContainer tc)
                {
                        return this;
                }
@@ -3367,7 +3745,7 @@ namespace CIR {
                        eclass = ExprClass.Type;
                }
 
-               override public Expression Resolve (TypeContainer tc)
+               override public Expression DoResolve (TypeContainer tc)
                {
                        return this;
                }
@@ -3407,7 +3785,7 @@ namespace CIR {
                        }
                }
                
-               override public Expression Resolve (TypeContainer tc)
+               override public Expression DoResolve (TypeContainer tc)
                {
                        return this;
                }
@@ -3418,31 +3796,6 @@ namespace CIR {
                }
        }
        
-       public class BuiltinTypeAccess : Expression {
-               public readonly string AccessBase;
-               public readonly string Method;
-               
-               public BuiltinTypeAccess (string type, string method)
-               {
-                       System.Console.WriteLine ("DUDE! This type should be fully resolved!");
-                       AccessBase = type;
-                       Method = method;
-               }
-
-               public override Expression Resolve (TypeContainer tc)
-               {
-                       // FIXME: Implement;
-                       throw new Exception ("Unimplemented");
-                       // return this;
-               }
-
-               public override void Emit (EmitContext ec)
-               {
-                       throw new Exception ("Unimplemented");
-               }
-       }
-
-
        //   Fully resolved expression that evaluates to a Field
        // </summary>
        public class FieldExpr : Expression, LValue {
@@ -3456,7 +3809,7 @@ namespace CIR {
                        type = fi.FieldType;
                }
 
-               override public Expression Resolve (TypeContainer tc)
+               override public Expression DoResolve (TypeContainer tc)
                {
                        if (!FieldInfo.IsStatic){
                                if (Instance == null){
@@ -3527,7 +3880,7 @@ namespace CIR {
                        type = pi.PropertyType;
                }
 
-               override public Expression Resolve (TypeContainer tc)
+               override public Expression DoResolve (TypeContainer tc)
                {
                        // We are born in resolved state. 
                        return this;
@@ -3541,7 +3894,7 @@ namespace CIR {
        }
 
        // <summary>
-       //   Fully resolved expression that evaluates to a Property
+       //   Fully resolved expression that evaluates to a Expression
        // </summary>
        public class EventExpr : Expression {
                public readonly EventInfo EventInfo;
@@ -3552,7 +3905,7 @@ namespace CIR {
                        eclass = ExprClass.EventAccess;
                }
 
-               override public Expression Resolve (TypeContainer tc)
+               override public Expression DoResolve (TypeContainer tc)
                {
                        // We are born in resolved state. 
                        return this;
@@ -3574,7 +3927,7 @@ namespace CIR {
                        Expr = e;
                }
 
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (TypeContainer tc)
                {
                        Expr = Expr.Resolve (tc);
 
@@ -3606,7 +3959,7 @@ namespace CIR {
                        Expr = e;
                }
 
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (TypeContainer tc)
                {
                        Expr = Expr.Resolve (tc);
 
@@ -3640,7 +3993,7 @@ namespace CIR {
                        Arguments = e_list;
                }
 
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (TypeContainer tc)
                {
                        // FIXME: Implement;
                        throw new Exception ("Unimplemented");
@@ -3674,7 +4027,7 @@ namespace CIR {
                        
                }
 
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (TypeContainer tc)
                {
                        // FIXME: Implement;
                        throw new Exception ("Unimplemented");
@@ -3699,7 +4052,7 @@ namespace CIR {
                        eclass = ExprClass.Value;
                }
 
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (TypeContainer tc)
                {
                        //
                        // We are born in a fully resolved state
@@ -3707,81 +4060,19 @@ namespace CIR {
                        return this;
                }
 
-               public static Expression CanConvert (TypeContainer tc, Expression source, Type target)
-               {
-                       Expression mg1, mg2;
-                       MethodBase method;
-                       ArrayList arguments;
-                       
-                       mg1 = MemberLookup (tc, source.Type, "op_Implicit", false);
-                       mg2 = MemberLookup (tc, target, "op_Implicit", false);
-                       
-                       MethodGroupExpr union = Invocation.MakeUnionSet (mg1, mg2);
-
-                       if (union != null) {
-                               arguments = new ArrayList ();
-                               arguments.Add (new Argument (source, Argument.AType.Expression));
-                               
-                               method = Invocation.OverloadResolve (tc, union, arguments,
-                                                                    new Location ("FIXME", 1, 1));
-
-                               if (method != null) {
-                                       MethodInfo mi = (MethodInfo) method;
-                                       
-                                       if (mi.ReturnType == target)
-                                               return new UserImplicitCast (mi, arguments);
-                               }
-                       }
-                       
-                       // If we have a boolean type, we need to check for the True
-                       // and False operators too.
-                       
-                       if (target == TypeManager.bool_type) {
-
-                               mg1 = MemberLookup (tc, source.Type, "op_True", false);
-                               mg2 = MemberLookup (tc, target, "op_True", false);
-                               
-                               union = Invocation.MakeUnionSet (mg1, mg2);
-
-                               if (union == null)
-                                       return null;
-
-                               arguments = new ArrayList ();
-                               arguments.Add (new Argument (source, Argument.AType.Expression));
-                       
-                               method = Invocation.OverloadResolve (tc, union, arguments,
-                                                                    new Location ("FIXME", 1, 1));
-                               if (method != null) {
-                                       MethodInfo mi = (MethodInfo) method;
-
-                                       if (mi.ReturnType == target) 
-                                               return new UserImplicitCast (mi, arguments);
-                               }
-                       }
-                       
-                       return null;
-               }
-               
                public override void Emit (EmitContext ec)
                {
                        ILGenerator ig = ec.ig;
                        
-                       if (method != null) {
-
-                               // Note that operators are static anyway
-                               
-                               if (arguments != null) 
-                                       Invocation.EmitArguments (ec, method, arguments);
+                       // Note that operators are static anyway
                                
-                               if (method is MethodInfo)
-                                       ig.Emit (OpCodes.Call, (MethodInfo) method);
-                               else
-                                       ig.Emit (OpCodes.Call, (ConstructorInfo) method);
-
-                               return;
-                       }
-
-                       throw new Exception ("Implement me");
+                       if (arguments != null) 
+                               Invocation.EmitArguments (ec, method, arguments);
+                       
+                       if (method is MethodInfo)
+                               ig.Emit (OpCodes.Call, (MethodInfo) method);
+                       else
+                               ig.Emit (OpCodes.Call, (ConstructorInfo) method);
                }
 
        }