2002-05-11 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / mcs / ecore.cs
index 831ea4cf4169bfbded4629fe3b0f020bdc1c2d14..ac11dafc20b92b2f0bdb89a8749b6a68099db201 100755 (executable)
@@ -36,15 +36,32 @@ namespace Mono.CSharp {
                Nothing, 
        }
 
+       //
+       // This is just as a hint to AddressOf of what will be done with the
+       // address.
+       [Flags]
+       public enum AddressOp {
+               Store = 1,
+               Load  = 2,
+               LoadStore = 3
+       };
+       
        /// <summary>
        ///   This interface is implemented by variables
        /// </summary>
        public interface IMemoryLocation {
                /// <summary>
                ///   The AddressOf method should generate code that loads
-               ///   the address of the object and leaves it on the stack
+               ///   the address of the object and leaves it on the stack.
+               ///
+               ///   The `mode' argument is used to notify the expression
+               ///   of whether this will be used to read from the address or
+               ///   write to the address.
+               ///
+               ///   This is just a hint that can be used to provide good error
+               ///   reporting, and should have no other side effects. 
                /// </summary>
-               void AddressOf (EmitContext ec);
+               void AddressOf (EmitContext ec, AddressOp mode);
        }
 
        /// <remarks>
@@ -85,7 +102,7 @@ namespace Mono.CSharp {
                        Report.Warning (warning, s);
                }
 
-               static public void error30 (Location loc, Type source, Type target)
+               static public void Error_CannotConvertType (Location loc, Type source, Type target)
                {
                        Report.Error (30, loc, "Cannot convert type '" +
                                      TypeManager.CSharpName (source) + "' to '" +
@@ -139,13 +156,14 @@ namespace Mono.CSharp {
                        Expression e = DoResolve (ec);
 
                        if (e != null){
+
                                if (e is SimpleName){
                                        SimpleName s = (SimpleName) e;
-                                       
+
                                        Report.Error (
-                                               103, s.Location,
-                                               "The name `" + s.Name + "' could not be found in `" +
-                                               ec.TypeContainer.Name + "'");
+                                                     103, s.Location,
+                                                     "The name `" + s.Name + "' could not be found in `" +
+                                                     ec.DeclSpace.Name + "'");
                                        return null;
                                }
                                
@@ -214,11 +232,11 @@ namespace Mono.CSharp {
                        if (e != null){
                                if (e is SimpleName){
                                        SimpleName s = (SimpleName) e;
-                                       
+
                                        Report.Error (
                                                103, s.Location,
                                                "The name `" + s.Name + "' could not be found in `" +
-                                               ec.TypeContainer.Name + "'");
+                                               ec.DeclSpace.Name + "'");
                                        return null;
                                }
 
@@ -295,10 +313,12 @@ namespace Mono.CSharp {
                                return new ByteConstant ((byte)v);
                        else if (t == TypeManager.char_type)
                                return new CharConstant ((char)v);
+                       else if (t == TypeManager.bool_type)
+                               return new BoolConstant ((bool) v);
                        else if (TypeManager.IsEnumType (t)){
-                               Expression e = Constantify (v, v.GetType ());
+                               Constant e = Constantify (v, v.GetType ());
 
-                               return new EnumConstant ((Constant) e, t);
+                               return new EnumConstant (e, t);
                        } else
                                throw new Exception ("Unknown type for constant (" + t +
                                                     "), details: " + v);
@@ -322,67 +342,6 @@ namespace Mono.CSharp {
                        return null;
                }
 
-               //
-               // Returns whether the array of memberinfos contains the given method
-               //
-               static bool ArrayContainsMethod (MemberInfo [] array, MethodBase new_method)
-               {
-                       Type [] new_args = TypeManager.GetArgumentTypes (new_method);
-
-                       foreach (MethodBase method in array){
-                               if (method.Name != new_method.Name)
-                                       continue;
-                               
-                               Type [] old_args = TypeManager.GetArgumentTypes (method);
-                               int old_count = old_args.Length;
-                               int i;
-
-                               if (new_args.Length != old_count)
-                                       continue;
-                               
-                               for (i = 0; i < old_count; i++){
-                                       if (old_args [i] != new_args [i])
-                                               break;
-                               }
-                               if (i != old_count)
-                                       continue;
-
-                               if (!(method is MethodInfo && new_method is MethodInfo))
-                                       return true;
-                               
-                               if (((MethodInfo) method).ReturnType == ((MethodInfo) new_method).ReturnType)
-                                       return true;
-                       }
-                       return false;
-               }
-               
-               //
-               // We copy methods from `new_members' into `target_list' if the signature
-               // for the method from in the new list does not exist in the target_list
-               //
-               // The name is assumed to be the same.
-               //
-               public static ArrayList CopyNewMethods (ArrayList target_list, MemberInfo [] new_members)
-               {
-                       if (target_list == null){
-                               target_list = new ArrayList ();
-
-                               target_list.AddRange (new_members);
-                               return target_list;
-                       }
-                       
-                       MemberInfo [] target_array = new MemberInfo [target_list.Count];
-                       target_list.CopyTo (target_array, 0);
-                       
-                       foreach (MemberInfo mi in new_members){
-                               MethodBase new_method = (MethodBase) mi;
-
-                               if (!ArrayContainsMethod (target_array, new_method))
-                                       target_list.Add (new_method);
-                       }
-                       return target_list;
-               }
-               
                //
                // FIXME: Probably implement a cache for (t,name,current_access_set)?
                //
@@ -414,69 +373,20 @@ namespace Mono.CSharp {
                public static Expression MemberLookup (EmitContext ec, Type t, string name,
                                                       MemberTypes mt, BindingFlags bf, Location loc)
                {
-                       Type source_type = ec.ContainerType;
-
-                       if (source_type != null){
-                               if (source_type == t || source_type.IsSubclassOf (t))
-                                       bf |= BindingFlags.NonPublic;
-                       }
+                       MemberInfo [] mi = TypeManager.MemberLookup (ec.ContainerType, t, mt, bf, name);
 
-                       //
-                       // Lookup for members starting in the type requested and going
-                       // up the hierarchy until a match is found.
-                       //
-                       // As soon as a non-method match is found, we return.
-                       //
-                       // If methods are found though, then the search proceeds scanning
-                       // for more public methods in the hierarchy with signatures that
-                       // do not match any of the signatures found so far.
-                       //
-                       ArrayList method_list = null;
-                       Type current_type = t;
-                       bool searching = true;
-                       do {
-                               MemberInfo [] mi;
-
-                               mi = RootContext.TypeManager.FindMembers (
-                                       current_type, mt, bf | BindingFlags.DeclaredOnly,
-                                       System.Type.FilterName, name);
-                               
-                               if (current_type == TypeManager.object_type)
-                                       searching = false;
-                               else {
-                                       current_type = current_type.BaseType;
-
-                                       //
-                                       // This happens with interfaces, they have a null
-                                       // basetype
-                                       //
-                                       if (current_type == null)
-                                               searching = false;
-                               }
-
-                               if (mi == null)
-                                       continue;
-                               
-                               int count = mi.Length;
+                       if (mi == null)
+                               return null;
 
-                               if (count == 0)
-                                       continue;
-                               
-                               if (count == 1 && !(mi [0] is MethodBase))
-                                       return Expression.ExprClassFromMemberInfo (ec, mi [0], loc);
+                       int count = mi.Length;
 
-                               //
-                               // We found methods, turn the search into "method scan"
-                               // mode.
-                               //
-                               method_list = CopyNewMethods (method_list, mi);
-                               mt &= (MemberTypes.Method | MemberTypes.Constructor);
-                       } while (searching);
+                       if (count > 1)
+                               return new MethodGroupExpr (mi, loc);
 
-                       if (method_list != null && method_list.Count > 0)
-                               return new MethodGroupExpr (method_list);
+                       if (mi [0] is MethodBase)
+                               return new MethodGroupExpr (mi, loc);
 
-                       return null;
+                       return ExprClassFromMemberInfo (ec, mi [0], loc);
                }
 
                public const MemberTypes AllMemberTypes =
@@ -497,6 +407,11 @@ namespace Mono.CSharp {
                        return MemberLookup (ec, t, name, AllMemberTypes, AllBindingFlags, loc);
                }
 
+               public static Expression MethodLookup (EmitContext ec, Type t, string name, Location loc)
+               {
+                       return MemberLookup (ec, t, name, MemberTypes.Method, AllBindingFlags, loc);
+               }
+
                /// <summary>
                ///   This is a wrapper for MemberLookup that is not used to "probe", but
                ///   to find a final definition.  If the final definition is not found, we
@@ -528,10 +443,17 @@ namespace Mono.CSharp {
                        return null;
                }
                
+               static EmptyExpression MyEmptyExpr;
                static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
                {
                        Type expr_type = expr.Type;
 
+                       if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
+                               // if we are a method group, emit a warning
+
+                               expr.Emit (null);
+                       }
+                       
                        if (target_type == TypeManager.object_type) {
                                //
                                // A pointer type cannot be converted to object
@@ -541,7 +463,7 @@ namespace Mono.CSharp {
 
                                if (expr_type.IsValueType)
                                        return new BoxedCast (expr);
-                               if (expr_type.IsClass)
+                               if (expr_type.IsClass || expr_type.IsInterface)
                                        return new EmptyCast (expr, target_type);
                        } else if (expr_type.IsSubclassOf (target_type)) {
                                return new EmptyCast (expr, target_type);
@@ -578,10 +500,15 @@ namespace Mono.CSharp {
                                        if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
 
                                                Type expr_element_type = expr_type.GetElementType ();
+
+                                               if (MyEmptyExpr == null)
+                                                       MyEmptyExpr = new EmptyExpression ();
+                                               
+                                               MyEmptyExpr.SetType (expr_element_type);
                                                Type target_element_type = target_type.GetElementType ();
 
                                                if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
-                                                       if (StandardConversionExists (expr_element_type,
+                                                       if (StandardConversionExists (MyEmptyExpr,
                                                                                      target_element_type))
                                                                return new EmptyCast (expr, target_type);
                                        }
@@ -813,13 +740,15 @@ namespace Mono.CSharp {
                ///  Determines if a standard implicit conversion exists from
                ///  expr_type to target_type
                /// </summary>
-               public static bool StandardConversionExists (Type expr_type, Type target_type)
+               public static bool StandardConversionExists (Expression expr, Type target_type)
                {
+                       Type expr_type = expr.Type;
+                       
                        if (expr_type == target_type)
                                return true;
 
                        // First numeric conversions 
-                       
+
                        if (expr_type == TypeManager.sbyte_type){
                                //
                                // From sbyte to short, int, long, float, double.
@@ -928,14 +857,15 @@ namespace Mono.CSharp {
 
                        if (target_type == TypeManager.object_type) {
                                if ((expr_type.IsClass) ||
-                                   (expr_type.IsValueType))
+                                   (expr_type.IsValueType) ||
+                                   (expr_type.IsInterface))
                                        return true;
                                
                        } else if (expr_type.IsSubclassOf (target_type)) {
                                return true;
                                
                        } else {
-                               // Please remember that all code below actuall comes
+                               // Please remember that all code below actually comes
                                // from ImplicitReferenceConversion so make sure code remains in sync
                                
                                // from any class-type S to any interface-type T.
@@ -955,10 +885,15 @@ namespace Mono.CSharp {
                                        if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
                                                
                                                Type expr_element_type = expr_type.GetElementType ();
+
+                                               if (MyEmptyExpr == null)
+                                                       MyEmptyExpr = new EmptyExpression ();
+                                               
+                                               MyEmptyExpr.SetType (expr_element_type);
                                                Type target_element_type = target_type.GetElementType ();
                                                
                                                if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
-                                                       if (StandardConversionExists (expr_element_type,
+                                                       if (StandardConversionExists (MyEmptyExpr,
                                                                                      target_element_type))
                                                                return true;
                                        }
@@ -980,87 +915,291 @@ namespace Mono.CSharp {
                                                return true;
                                
                                // from the null type to any reference-type.
-                               // FIXME : How do we do this ?
+                               if (expr is NullLiteral && !target_type.IsValueType)
+                                       return true;
+                               
+                       }
+
+                       if (expr is IntConstant){
+                               int value = ((IntConstant) expr).Value;
+
+                               if (target_type == TypeManager.sbyte_type){
+                                       if (value >= SByte.MinValue && value <= SByte.MaxValue)
+                                               return true;
+                               } else if (target_type == TypeManager.byte_type){
+                                       if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
+                                               return true;
+                               } else if (target_type == TypeManager.short_type){
+                                       if (value >= Int16.MinValue && value <= Int16.MaxValue)
+                                               return true;
+                               } else if (target_type == TypeManager.ushort_type){
+                                       if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
+                                               return true;
+                               } else if (target_type == TypeManager.uint32_type){
+                                       if (value >= 0)
+                                               return true;
+                               } else if (target_type == TypeManager.uint64_type){
+                                        //
+                                        // we can optimize this case: a positive int32
+                                        // always fits on a uint64.  But we need an opcode
+                                        // to do it.
+                                        //
+                                       if (value >= 0)
+                                               return true;
+                               }
+                               
+                               if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
+                                       return true;
+                       }
 
+                       if (expr is LongConstant && target_type == TypeManager.uint64_type){
+                               //
+                               // Try the implicit constant expression conversion
+                               // from long to ulong, instead of a nice routine,
+                               // we just inline it
+                               //
+                               long v = ((LongConstant) expr).Value;
+                               if (v > 0)
+                                       return true;
                        }
+                       
+                       if (target_type.IsSubclassOf (TypeManager.enum_type) && expr is IntLiteral){
+                               IntLiteral i = (IntLiteral) expr;
 
+                               if (i.Value == 0)
+                                       return true;
+                       }
                        return false;
                }
 
-               static EmptyExpression MyEmptyExpr;
+               //
+               // Used internally by FindMostEncompassedType, this is used
+               // to avoid creating lots of objects in the tight loop inside
+               // FindMostEncompassedType
+               //
+               static EmptyExpression priv_fmet_param;
+               
                /// <summary>
-               ///   Tells whether an implicit conversion exists from expr_type to
-               ///   target_type
+               ///  Finds "most encompassed type" according to the spec (13.4.2)
+               ///  amongst the methods in the MethodGroupExpr
                /// </summary>
-               public bool ImplicitConversionExists (EmitContext ec, Type expr_type, Type target_type,
-                                                     Location l)
+               static Type FindMostEncompassedType (ArrayList types)
                {
-                       if (MyEmptyExpr == null)
-                               MyEmptyExpr = new EmptyExpression (expr_type);
-                       else
-                               MyEmptyExpr.SetType (expr_type);
+                       Type best = null;
+
+                       if (priv_fmet_param == null)
+                               priv_fmet_param = new EmptyExpression ();
 
-                       return ConvertImplicit (ec, MyEmptyExpr, target_type, l) != null;
+                       foreach (Type t in types){
+                               priv_fmet_param.SetType (t);
+                               
+                               if (best == null) {
+                                       best = t;
+                                       continue;
+                               }
+                               
+                               if (StandardConversionExists (priv_fmet_param, best))
+                                       best = t;
+                       }
+
+                       return best;
                }
+
+               //
+               // Used internally by FindMostEncompassingType, this is used
+               // to avoid creating lots of objects in the tight loop inside
+               // FindMostEncompassingType
+               //
+               static EmptyExpression priv_fmee_ret;
                
                /// <summary>
-               ///  Finds "most encompassed type" according to the spec (13.4.2)
-               ///  amongst the methods in the MethodGroupExpr which convert from a
-               ///  type encompassing source_type
+               ///  Finds "most encompassing type" according to the spec (13.4.2)
+               ///  amongst the types in the given set
                /// </summary>
-               static Type FindMostEncompassedType (MethodGroupExpr me, Type source_type)
+               static Type FindMostEncompassingType (ArrayList types)
                {
                        Type best = null;
+
+                       if (priv_fmee_ret == null)
+                               priv_fmee_ret = new EmptyExpression ();
+
+                       foreach (Type t in types){
+                               priv_fmee_ret.SetType (best);
+
+                               if (best == null) {
+                                       best = t;
+                                       continue;
+                               }
+
+                               if (StandardConversionExists (priv_fmee_ret, t))
+                                       best = t;
+                       }
                        
-                       for (int i = me.Methods.Length; i > 0; ) {
-                               i--;
+                       return best;
+               }
 
-                               MethodBase mb = me.Methods [i];
+               //
+               // Used to avoid creating too many objects
+               //
+               static EmptyExpression priv_fms_expr;
+               
+               /// <summary>
+               ///   Finds the most specific source Sx according to the rules of the spec (13.4.4)
+               ///   by making use of FindMostEncomp* methods. Applies the correct rules separately
+               ///   for explicit and implicit conversion operators.
+               /// </summary>
+               static public Type FindMostSpecificSource (MethodGroupExpr me, Type source_type,
+                                                          bool apply_explicit_conv_rules,
+                                                          Location loc)
+               {
+                       ArrayList src_types_set = new ArrayList ();
+                       
+                       if (priv_fms_expr == null)
+                               priv_fms_expr = new EmptyExpression ();
+                       
+                       //
+                       // If any operator converts from S then Sx = S
+                       //
+                       foreach (MethodBase mb in me.Methods){
                                ParameterData pd = Invocation.GetParameterData (mb);
                                Type param_type = pd.ParameterType (0);
 
-                               if (StandardConversionExists (source_type, param_type)) {
-                                       if (best == null)
-                                               best = param_type;
+                               if (param_type == source_type)
+                                       return param_type;
+
+                               if (apply_explicit_conv_rules) {
+                                       //
+                                       // From the spec :
+                                       // Find the set of applicable user-defined conversion operators, U.  This set
+                                       // consists of the
+                                       // user-defined implicit or explicit conversion operators declared by
+                                       // the classes or structs in D that convert from a type encompassing
+                                       // or encompassed by S to a type encompassing or encompassed by T
+                                       //
+                                       priv_fms_expr.SetType (param_type);
+                                       if (StandardConversionExists (priv_fms_expr, source_type))
+                                               src_types_set.Add (param_type);
+                                       else {
+                                               priv_fms_expr.SetType (source_type);
+                                               if (StandardConversionExists (priv_fms_expr, param_type))
+                                                       src_types_set.Add (param_type);
+                                       }
+                               } else {
+                                       //
+                                       // Only if S is encompassed by param_type
+                                       //
+                                       priv_fms_expr.SetType (source_type);
+                                       if (StandardConversionExists (priv_fms_expr, param_type))
+                                               src_types_set.Add (param_type);
+                               }
+                       }
+                       
+                       //
+                       // Explicit Conv rules
+                       //
+                       if (apply_explicit_conv_rules) {
+                               ArrayList candidate_set = new ArrayList ();
+
+                               foreach (Type param_type in src_types_set){
+                                       priv_fms_expr.SetType (source_type);
                                        
-                                       if (StandardConversionExists (param_type, best))
-                                               best = param_type;
+                                       if (StandardConversionExists (priv_fms_expr, param_type))
+                                               candidate_set.Add (param_type);
                                }
+
+                               if (candidate_set.Count != 0)
+                                       return FindMostEncompassedType (candidate_set);
                        }
 
-                       return best;
+                       //
+                       // Final case
+                       //
+                       if (apply_explicit_conv_rules)
+                               return FindMostEncompassingType (src_types_set);
+                       else
+                               return FindMostEncompassedType (src_types_set);
                }
+
+               //
+               // Useful in avoiding proliferation of objects
+               //
+               static EmptyExpression priv_fmt_expr;
                
                /// <summary>
-               ///  Finds "most encompassing type" according to the spec (13.4.2)
-               ///  amongst the methods in the MethodGroupExpr which convert to a
-               ///  type encompassed by target_type
+               ///  Finds the most specific target Tx according to section 13.4.4
                /// </summary>
-               static Type FindMostEncompassingType (MethodGroupExpr me, Type target)
+               static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
+                                                          bool apply_explicit_conv_rules,
+                                                          Location loc)
                {
-                       Type best = null;
+                       ArrayList tgt_types_set = new ArrayList ();
                        
-                       for (int i = me.Methods.Length; i > 0; ) {
-                               i--;
-                               
-                               MethodInfo mi = (MethodInfo) me.Methods [i];
+                       if (priv_fmt_expr == null)
+                               priv_fmt_expr = new EmptyExpression ();
+                       
+                       //
+                       // If any operator converts to T then Tx = T
+                       //
+                       foreach (MethodInfo mi in me.Methods){
                                Type ret_type = mi.ReturnType;
-                               
-                               if (StandardConversionExists (ret_type, target)) {
-                                       if (best == null)
-                                               best = ret_type;
 
-                                       if (!StandardConversionExists (ret_type, best))
-                                               best = ret_type;
+                               if (ret_type == target)
+                                       return ret_type;
+
+                               if (apply_explicit_conv_rules) {
+                                       //
+                                       // From the spec :
+                                       // Find the set of applicable user-defined conversion operators, U.
+                                       //
+                                       // This set consists of the
+                                       // user-defined implicit or explicit conversion operators declared by
+                                       // the classes or structs in D that convert from a type encompassing
+                                       // or encompassed by S to a type encompassing or encompassed by T
+                                       //
+                                       priv_fms_expr.SetType (ret_type);
+                                       if (StandardConversionExists (priv_fms_expr, target))
+                                               tgt_types_set.Add (ret_type);
+                                       else {
+                                               priv_fms_expr.SetType (target);
+                                               if (StandardConversionExists (priv_fms_expr, ret_type))
+                                                       tgt_types_set.Add (ret_type);
+                                       }
+                               } else {
+                                       //
+                                       // Only if T is encompassed by param_type
+                                       //
+                                       priv_fms_expr.SetType (ret_type);
+                                       if (StandardConversionExists (priv_fms_expr, target))
+                                               tgt_types_set.Add (ret_type);
                                }
-                               
                        }
-                       
-                       return best;
 
+                       //
+                       // Explicit conv rules
+                       //
+                       if (apply_explicit_conv_rules) {
+                               ArrayList candidate_set = new ArrayList ();
+
+                               foreach (Type ret_type in tgt_types_set){
+                                       priv_fmt_expr.SetType (ret_type);
+                                       
+                                       if (StandardConversionExists (priv_fmt_expr, target))
+                                               candidate_set.Add (ret_type);
+                               }
+
+                               if (candidate_set.Count != 0)
+                                       return FindMostEncompassingType (candidate_set);
+                       }
+                       
+                       //
+                       // Okay, final case !
+                       //
+                       if (apply_explicit_conv_rules)
+                               return FindMostEncompassedType (tgt_types_set);
+                       else
+                               return FindMostEncompassingType (tgt_types_set);
                }
                
-
                /// <summary>
                ///  User-defined Implicit conversions
                /// </summary>
@@ -1078,132 +1217,157 @@ namespace Mono.CSharp {
                {
                        return UserDefinedConversion (ec, source, target, loc, true);
                }
-               
+
                /// <summary>
-               ///   User-defined conversions
+               ///   Computes the MethodGroup for the user-defined conversion
+               ///   operators from source_type to target_type.  `look_for_explicit'
+               ///   controls whether we should also include the list of explicit
+               ///   operators
                /// </summary>
-               static public Expression UserDefinedConversion (EmitContext ec, Expression source,
-                                                               Type target, Location loc,
-                                                               bool look_for_explicit)
+               static MethodGroupExpr GetConversionOperators (EmitContext ec,
+                                                              Type source_type, Type target_type,
+                                                              Location loc, bool look_for_explicit)
                {
-                       Expression mg1 = null, mg2 = null, mg3 = null, mg4 = null;
+                       Expression mg1 = null, mg2 = null;
                        Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
-                       Expression e;
-                       MethodBase method = null;
-                       Type source_type = source.Type;
-
                        string op_name;
-                       
-                       // If we have a boolean type, we need to check for the True operator
 
+                       //
                        // FIXME : How does the False operator come into the picture ?
-                       // FIXME : This doesn't look complete and very correct !
-                       if (target == TypeManager.bool_type)
+                       // This doesn't look complete and very correct !
+                       //
+                       if (target_type == TypeManager.bool_type && !look_for_explicit)
                                op_name = "op_True";
                        else
                                op_name = "op_Implicit";
                        
-                       mg1 = MemberLookup (ec, source_type, op_name, loc);
-
-                       if (source_type.BaseType != null)
-                               mg2 = MemberLookup (ec, source_type.BaseType, op_name, loc);
+                       MethodGroupExpr union3;
                        
-                       mg3 = MemberLookup (ec, target, op_name, loc);
+                       mg1 = MethodLookup (ec, source_type, op_name, loc);
+                       if (source_type.BaseType != null)
+                               mg2 = MethodLookup (ec, source_type.BaseType, op_name, loc);
 
-                       if (target.BaseType != null)
-                               mg4 = MemberLookup (ec, target.BaseType, op_name, loc);
+                       if (mg1 == null)
+                               union3 = (MethodGroupExpr) mg2;
+                       else if (mg2 == null)
+                               union3 = (MethodGroupExpr) mg1;
+                       else
+                               union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
 
-                       MethodGroupExpr union1 = Invocation.MakeUnionSet (mg1, mg2);
-                       MethodGroupExpr union2 = Invocation.MakeUnionSet (mg3, mg4);
+                       mg1 = MethodLookup (ec, target_type, op_name, loc);
+                       if (mg1 != null){
+                               if (union3 != null)
+                                       union3 = Invocation.MakeUnionSet (union3, mg1, loc);
+                               else
+                                       union3 = (MethodGroupExpr) mg1;
+                       }
 
-                       MethodGroupExpr union3 = Invocation.MakeUnionSet (union1, union2);
+                       if (target_type.BaseType != null)
+                               mg1 = MethodLookup (ec, target_type.BaseType, op_name, loc);
+                       
+                       if (mg1 != null){
+                               if (union3 != null)
+                                       union3 = Invocation.MakeUnionSet (union3, mg1, loc);
+                               else
+                                       union3 = (MethodGroupExpr) mg1;
+                       }
 
                        MethodGroupExpr union4 = null;
 
                        if (look_for_explicit) {
-
                                op_name = "op_Explicit";
-                               
-                               mg5 = MemberLookup (ec, source_type, op_name, loc);
 
+                               mg5 = MemberLookup (ec, source_type, op_name, loc);
                                if (source_type.BaseType != null)
-                                       mg6 = MemberLookup (ec, source_type.BaseType, op_name, loc);
-                               
-                               mg7 = MemberLookup (ec, target, op_name, loc);
+                                       mg6 = MethodLookup (ec, source_type.BaseType, op_name, loc);
                                
-                               if (target.BaseType != null)
-                                       mg8 = MemberLookup (ec, target.BaseType, op_name, loc);
+                               mg7 = MemberLookup (ec, target_type, op_name, loc);
+                               if (target_type.BaseType != null)
+                                       mg8 = MethodLookup (ec, target_type.BaseType, op_name, loc);
                                
-                               MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6);
-                               MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8);
+                               MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
+                               MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
 
-                               union4 = Invocation.MakeUnionSet (union5, union6);
+                               union4 = Invocation.MakeUnionSet (union5, union6, loc);
                        }
                        
-                       MethodGroupExpr union = Invocation.MakeUnionSet (union3, union4);
-
-                       if (union != null) {
+                       return Invocation.MakeUnionSet (union3, union4, loc);
+               }
+               
+               /// <summary>
+               ///   User-defined conversions
+               /// </summary>
+               static public Expression UserDefinedConversion (EmitContext ec, Expression source,
+                                                               Type target, Location loc,
+                                                               bool look_for_explicit)
+               {
+                       MethodGroupExpr union;
+                       Type source_type = source.Type;
+                       MethodBase method = null;
+                       
+                       union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
+                       if (union == null)
+                               return null;
+                       
+                       Type most_specific_source, most_specific_target;
 
-                               Type most_specific_source, most_specific_target;
+#if BLAH
+                       foreach (MethodBase m in union.Methods){
+                               Console.WriteLine ("Name: " + m.Name);
+                               Console.WriteLine ("    : " + ((MethodInfo)m).ReturnType);
+                       }
+#endif
+                       
+                       most_specific_source = FindMostSpecificSource (union, source_type, look_for_explicit, loc);
+                       if (most_specific_source == null)
+                               return null;
 
-                               most_specific_source = FindMostEncompassedType (union, source_type);
-                               if (most_specific_source == null)
-                                       return null;
+                       most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
+                       if (most_specific_target == null) 
+                               return null;
+                       
+                       Console.WriteLine ("S:T" + most_specific_source + ":" + most_specific_target);
+                       int count = 0;
 
-                               most_specific_target = FindMostEncompassingType (union, target);
-                               if (most_specific_target == null) 
-                                       return null;
-                               
-                               int count = 0;
+                       foreach (MethodBase mb in union.Methods){
+                               ParameterData pd = Invocation.GetParameterData (mb);
+                               MethodInfo mi = (MethodInfo) mb;
                                
-                               for (int i = union.Methods.Length; i > 0;) {
-                                       i--;
-
-                                       MethodBase mb = union.Methods [i];
-                                       ParameterData pd = Invocation.GetParameterData (mb);
-                                       MethodInfo mi = (MethodInfo) union.Methods [i];
-
-                                       if (pd.ParameterType (0) == most_specific_source &&
-                                           mi.ReturnType == most_specific_target) {
-                                               method = mb;
-                                               count++;
-                                       }
+                               if (pd.ParameterType (0) == most_specific_source &&
+                                   mi.ReturnType == most_specific_target) {
+                                       method = mb;
+                                       count++;
                                }
-
-                               if (method == null || count > 1) {
-                                       Report.Error (-11, loc, "Ambiguous user defined conversion");
-                                       return null;
-                               }
-                               
-                               //
-                               // This will do the conversion to the best match that we
-                               // found.  Now we need to perform an implict standard conversion
-                               // if the best match was not the type that we were requested
-                               // by target.
-                               //
-                               if (look_for_explicit)
-                                       source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
-                               else
-                                       source = ConvertImplicitStandard (ec, source,
-                                                                         most_specific_source, loc);
-
-                               if (source == null)
-                                       return null;
-
-                               e =  new UserCast ((MethodInfo) method, source);
-                               
-                               if (e.Type != target){
-                                       if (!look_for_explicit)
-                                               e = ConvertImplicitStandard (ec, e, target, loc);
-                                       else
-                                               e = ConvertExplicitStandard (ec, e, target, loc);
-
-                                       return e;
-                               } else
-                                       return e;
                        }
                        
-                       return null;
+                       if (method == null || count > 1) {
+                               Report.Error (-11, loc, "Ambiguous user defined conversion");
+                               return null;
+                       }
+                       
+                       //
+                       // This will do the conversion to the best match that we
+                       // found.  Now we need to perform an implict standard conversion
+                       // if the best match was not the type that we were requested
+                       // by target.
+                       //
+                       if (look_for_explicit)
+                               source = ConvertExplicitStandard (ec, source, most_specific_source, loc);
+                       else
+                               source = ConvertImplicitStandard (ec, source, most_specific_source, loc);
+
+                       if (source == null)
+                               return null;
+
+                       Expression e;
+                       e =  new UserCast ((MethodInfo) method, source);
+                       if (e.Type != target){
+                               if (!look_for_explicit)
+                                       e = ConvertImplicitStandard (ec, e, target, loc);
+                               else
+                                       e = ConvertExplicitStandard (ec, e, target, loc);
+                       } 
+                       return e;
                }
                
                /// <summary>
@@ -1303,7 +1467,7 @@ namespace Mono.CSharp {
                        int value = ic.Value;
 
                        //
-                       // FIXME: This should really return constants instead of EmptyCasts
+                       // FIXME: This could return constants instead of EmptyCasts
                        //
                        if (target_type == TypeManager.sbyte_type){
                                if (value >= SByte.MinValue && value <= SByte.MaxValue)
@@ -1336,6 +1500,15 @@ namespace Mono.CSharp {
                        return null;
                }
 
+               static public void Error_CannotConvertImplicit (Location loc, Type source, Type target)
+               {
+                       string msg = "Cannot convert implicitly from `"+
+                               TypeManager.CSharpName (source) + "' to `" +
+                               TypeManager.CSharpName (target) + "'";
+
+                       Error (29, loc, msg);
+               }
+
                /// <summary>
                ///   Attemptes to implicityly convert `target' into `type', using
                ///   ConvertImplicit.  If there is no implicit conversion, then
@@ -1356,11 +1529,7 @@ namespace Mono.CSharp {
                                       "float type, use F suffix to create a float literal");
                        }
                        
-                       string msg = "Cannot convert implicitly from `"+
-                               TypeManager.CSharpName (source.Type) + "' to `" +
-                               TypeManager.CSharpName (target_type) + "'";
-
-                       Error (29, loc, msg);
+                       Error_CannotConvertImplicit (loc, source.Type, target_type);
 
                        return null;
                }
@@ -1382,141 +1551,141 @@ namespace Mono.CSharp {
 
                        if (TypeManager.IsEnumType (real_target_type))
                                real_target_type = TypeManager.EnumToUnderlying (real_target_type);
-                       
+
                        if (expr_type == TypeManager.sbyte_type){
                                //
                                // From sbyte to byte, ushort, uint, ulong, char
                                //
                                if (real_target_type == TypeManager.byte_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
                                if (real_target_type == TypeManager.ushort_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
                                if (real_target_type == TypeManager.uint32_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
                                if (real_target_type == TypeManager.uint64_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
                                if (real_target_type == TypeManager.char_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
                        } else if (expr_type == TypeManager.byte_type){
                                //
                                // From byte to sbyte and char
                                //
                                if (real_target_type == TypeManager.sbyte_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
                                if (real_target_type == TypeManager.char_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
                        } else if (expr_type == TypeManager.short_type){
                                //
                                // From short to sbyte, byte, ushort, uint, ulong, char
                                //
                                if (real_target_type == TypeManager.sbyte_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
                                if (real_target_type == TypeManager.byte_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
                                if (real_target_type == TypeManager.ushort_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
                                if (real_target_type == TypeManager.uint32_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
                                if (real_target_type == TypeManager.uint64_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
                                if (real_target_type == TypeManager.char_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
                        } else if (expr_type == TypeManager.ushort_type){
                                //
                                // From ushort to sbyte, byte, short, char
                                //
                                if (real_target_type == TypeManager.sbyte_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
                                if (real_target_type == TypeManager.byte_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
                                if (real_target_type == TypeManager.short_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
                                if (real_target_type == TypeManager.char_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
                        } else if (expr_type == TypeManager.int32_type){
                                //
                                // From int to sbyte, byte, short, ushort, uint, ulong, char
                                //
                                if (real_target_type == TypeManager.sbyte_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
                                if (real_target_type == TypeManager.byte_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
                                if (real_target_type == TypeManager.short_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
                                if (real_target_type == TypeManager.ushort_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
                                if (real_target_type == TypeManager.uint32_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
                                if (real_target_type == TypeManager.uint64_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
                                if (real_target_type == TypeManager.char_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
                        } else if (expr_type == TypeManager.uint32_type){
                                //
                                // From uint to sbyte, byte, short, ushort, int, char
                                //
                                if (real_target_type == TypeManager.sbyte_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
                                if (real_target_type == TypeManager.byte_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
                                if (real_target_type == TypeManager.short_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
                                if (real_target_type == TypeManager.ushort_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
                                if (real_target_type == TypeManager.int32_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
                                if (real_target_type == TypeManager.char_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
                        } else if (expr_type == TypeManager.int64_type){
                                //
                                // From long to sbyte, byte, short, ushort, int, uint, ulong, char
                                //
                                if (real_target_type == TypeManager.sbyte_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
                                if (real_target_type == TypeManager.byte_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
                                if (real_target_type == TypeManager.short_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
                                if (real_target_type == TypeManager.ushort_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
                                if (real_target_type == TypeManager.int32_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
                                if (real_target_type == TypeManager.uint32_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
                                if (real_target_type == TypeManager.uint64_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
                                if (real_target_type == TypeManager.char_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
                        } else if (expr_type == TypeManager.uint64_type){
                                //
                                // From ulong to sbyte, byte, short, ushort, int, uint, long, char
                                //
                                if (real_target_type == TypeManager.sbyte_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
                                if (real_target_type == TypeManager.byte_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
                                if (real_target_type == TypeManager.short_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
                                if (real_target_type == TypeManager.ushort_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
                                if (real_target_type == TypeManager.int32_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
                                if (real_target_type == TypeManager.uint32_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
                                if (real_target_type == TypeManager.int64_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
                                if (real_target_type == TypeManager.char_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
                        } else if (expr_type == TypeManager.char_type){
                                //
                                // From char to sbyte, byte, short
                                //
                                if (real_target_type == TypeManager.sbyte_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
                                if (real_target_type == TypeManager.byte_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
                                if (real_target_type == TypeManager.short_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
                        } else if (expr_type == TypeManager.float_type){
                                //
                                // From float to sbyte, byte, short,
@@ -1524,23 +1693,23 @@ namespace Mono.CSharp {
                                // or decimal
                                //
                                if (real_target_type == TypeManager.sbyte_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
                                if (real_target_type == TypeManager.byte_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
                                if (real_target_type == TypeManager.short_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
                                if (real_target_type == TypeManager.ushort_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
                                if (real_target_type == TypeManager.int32_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
                                if (real_target_type == TypeManager.uint32_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
                                if (real_target_type == TypeManager.int64_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
                                if (real_target_type == TypeManager.uint64_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
                                if (real_target_type == TypeManager.char_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
                                if (real_target_type == TypeManager.decimal_type)
                                        return InternalTypeConstructor (ec, expr, target_type);
                        } else if (expr_type == TypeManager.double_type){
@@ -1550,25 +1719,25 @@ namespace Mono.CSharp {
                                // char, float or decimal
                                //
                                if (real_target_type == TypeManager.sbyte_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
                                if (real_target_type == TypeManager.byte_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
                                if (real_target_type == TypeManager.short_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
                                if (real_target_type == TypeManager.ushort_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
                                if (real_target_type == TypeManager.int32_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
                                if (real_target_type == TypeManager.uint32_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
                                if (real_target_type == TypeManager.int64_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
                                if (real_target_type == TypeManager.uint64_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
                                if (real_target_type == TypeManager.char_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
                                if (real_target_type == TypeManager.float_type)
-                                       return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
+                                       return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
                                if (real_target_type == TypeManager.decimal_type)
                                        return InternalTypeConstructor (ec, expr, target_type);
                        } 
@@ -1610,11 +1779,11 @@ namespace Mono.CSharp {
                        }
                            
                        //
-                       // From any class type S to any interface T, provides S is not sealed
+                       // From any class type S to any interface T, provided S is not sealed
                        // and provided S does not implement T.
                        //
                        if (target_type.IsInterface && !source_type.IsSealed &&
-                           !target_type.IsAssignableFrom (source_type))
+                           !TypeManager.ImplementsInterface (source_type, target_type))
                                return true;
 
                        //
@@ -1622,9 +1791,10 @@ namespace Mono.CSharp {
                        // sealed, or provided T implements S.
                        //
                        if (source_type.IsInterface &&
-                           (!target_type.IsSealed || source_type.IsAssignableFrom (target_type)))
+                           (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
                                return true;
-
+                       
+                       
                        // From an array type S with an element type Se to an array type T with an 
                        // element type Te provided all the following are true:
                        //     * S and T differe only in element type, in other words, S and T
@@ -1677,7 +1847,7 @@ namespace Mono.CSharp {
                {
                        Type source_type = source.Type;
                        bool target_is_value_type = target_type.IsValueType;
-                       
+
                        //
                        // From object to any reference type
                        //
@@ -1695,9 +1865,6 @@ namespace Mono.CSharp {
                        // From any interface type S to any interface T provided S is not derived from T
                        //
                        if (source_type.IsInterface && target_type.IsInterface){
-
-                               Type [] ifaces = source_type.GetInterfaces ();
-
                                if (TypeManager.ImplementsInterface (source_type, target_type))
                                        return null;
                                else
@@ -1709,7 +1876,6 @@ namespace Mono.CSharp {
                        // and provided S does not implement T.
                        //
                        if (target_type.IsInterface && !source_type.IsSealed) {
-                               
                                if (TypeManager.ImplementsInterface (source_type, target_type))
                                        return null;
                                else
@@ -1722,11 +1888,7 @@ namespace Mono.CSharp {
                        // sealed, or provided T implements S.
                        //
                        if (source_type.IsInterface) {
-
-                               if (target_type.IsSealed)
-                                       return null;
-                               
-                               if (TypeManager.ImplementsInterface (target_type, source_type))
+                               if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type))
                                        return new ClassCast (source, target_type);
                                else
                                        return null;
@@ -1877,12 +2039,12 @@ namespace Mono.CSharp {
                        if (ne != null)
                                return ne;
 
-                       error30 (loc, expr_type, target_type);
+                       Error_CannotConvertType (loc, expr_type, target_type);
                        return null;
                }
 
                /// <summary>
-               ///   Same as ConverExplicit, only it doesn't include user defined conversions
+               ///   Same as ConvertExplicit, only it doesn't include user defined conversions
                /// </summary>
                static public Expression ConvertExplicitStandard (EmitContext ec, Expression expr,
                                                                  Type target_type, Location l)
@@ -1900,7 +2062,7 @@ namespace Mono.CSharp {
                        if (ne != null)
                                return ne;
 
-                       error30 (l, expr.Type, target_type);
+                       Error_CannotConvertType (l, expr.Type, target_type);
                        return null;
                }
 
@@ -1945,7 +2107,7 @@ namespace Mono.CSharp {
                               "' where a `" + expected + "' was expected");
                }
 
-               static void error31 (Location l, string val, Type t)
+               static void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
                {
                        Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
                                      TypeManager.CSharpName (t));
@@ -2221,7 +2383,7 @@ namespace Mono.CSharp {
 
                                s = v.ToString ();
                        }
-                       error31 (loc, s, target_type);
+                       Error_ConstantValueCannotBeConverted (loc, s, target_type);
                        return null;
                }
 
@@ -2471,6 +2633,36 @@ namespace Mono.CSharp {
                {
                        return Child.AsString ();
                }
+
+               public override DoubleConstant ConvertToDouble ()
+               {
+                       return Child.ConvertToDouble ();
+               }
+
+               public override FloatConstant ConvertToFloat ()
+               {
+                       return Child.ConvertToFloat ();
+               }
+
+               public override ULongConstant ConvertToULong ()
+               {
+                       return Child.ConvertToULong ();
+               }
+
+               public override LongConstant ConvertToLong ()
+               {
+                       return Child.ConvertToLong ();
+               }
+
+               public override UIntConstant ConvertToUInt ()
+               {
+                       return Child.ConvertToUInt ();
+               }
+
+               public override IntConstant ConvertToInt ()
+               {
+                       return Child.ConvertToInt ();
+               }
        }
 
        /// <summary>
@@ -2551,11 +2743,13 @@ namespace Mono.CSharp {
                }
 
                Mode mode;
+               bool checked_state;
                
-               public ConvCast (Expression child, Type return_type, Mode m)
+               public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
                        : base (child, return_type)
                {
                        mode = m;
+                       checked_state = ec.CheckState;
                }
 
                public override Expression DoResolve (EmitContext ec)
@@ -2572,7 +2766,7 @@ namespace Mono.CSharp {
                        
                        base.Emit (ec);
 
-                       if (ec.CheckState){
+                       if (checked_state){
                                switch (mode){
                                case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
                                case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
@@ -2889,12 +3083,18 @@ namespace Mono.CSharp {
                
                public override Expression DoResolve (EmitContext ec)
                {
-                       return SimpleNameResolve (ec, false);
+                       return SimpleNameResolve (ec, null, false);
                }
 
+               public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
+               {
+                       return SimpleNameResolve (ec, right_side, false);
+               }
+               
+
                public Expression DoResolveAllowStatic (EmitContext ec)
                {
-                       return SimpleNameResolve (ec, true);
+                       return SimpleNameResolve (ec, null, true);
                }
 
                /// <remarks>
@@ -2914,26 +3114,66 @@ namespace Mono.CSharp {
                ///   Type is both an instance variable and a Type;  Type.GetType
                ///   is the static method not an instance method of type.
                /// </remarks>
-               Expression SimpleNameResolve (EmitContext ec, bool allow_static)
+               Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static)
                {
-                       Expression e;
+                       Expression e = null;
 
                        //
                        // Stage 1: Performed by the parser (binding to locals or parameters).
                        //
+                       if (!ec.OnlyLookupTypes){
+                               Block current_block = ec.CurrentBlock;
+                               if (current_block != null && current_block.IsVariableDefined (Name)){
+                                       LocalVariableReference var;
+                                       
+                                       var = new LocalVariableReference (ec.CurrentBlock, Name, Location);
 
-                       //
-                       // Stage 2: Lookup members 
-                       //
-                       e = MemberLookup (ec, ec.TypeContainer.TypeBuilder, Name, Location);
+                                       if (right_side != null)
+                                               return var.ResolveLValue (ec, right_side);
+                                       else
+                                               return var.Resolve (ec);
+                               }
+                       
+                               //
+                               // Stage 2: Lookup members 
+                               //
+
+                               //
+                               // For enums, the TypeBuilder is not ec.DeclSpace.TypeBuilder
+                               // Hence we have two different cases
+                               //
+
+                               DeclSpace lookup_ds = ec.DeclSpace;
+                               do {
+                                       if (lookup_ds.TypeBuilder == null)
+                                               break;
+
+                                       e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, Location);
+                                       if (e != null)
+                                               break;
+
+                                       //
+                                       // Classes/structs keep looking, enums break
+                                       //
+                                       if (lookup_ds is TypeContainer)
+                                               lookup_ds = ((TypeContainer) lookup_ds).Parent;
+                                       else
+                                               break;
+                               } while (lookup_ds != null);
+                               
+                               if (e == null && ec.ContainerType != null)
+                                       e = MemberLookup (ec, ec.ContainerType, Name, Location);
+                       }
+
+                       // Continuation of stage 2
                        if (e == null){
                                //
                                // Stage 3: Lookup symbol in the various namespaces. 
                                //
-                               DeclSpace ds = ec.TypeContainer;
+                               DeclSpace ds = ec.DeclSpace;
                                Type t;
                                string alias_value;
-                               
+
                                if ((t = RootContext.LookupType (ds, Name, true, Location)) != null)
                                        return new TypeExpr (t);
                                
@@ -2966,6 +3206,9 @@ namespace Mono.CSharp {
                        if (e is TypeExpr)
                                return e;
 
+                       if (ec.OnlyLookupTypes)
+                               return null;
+                       
                        if (e is FieldExpr){
                                FieldExpr fe = (FieldExpr) e;
                                FieldInfo fi = fe.FieldInfo;
@@ -2998,6 +3241,35 @@ namespace Mono.CSharp {
                                        }
                                }
 
+                               if (fi.IsLiteral) {
+                                       Type t = fi.FieldType;
+                                       Type decl_type = fi.DeclaringType;
+                                       object o;
+
+                                       if (fi is FieldBuilder)
+                                               o = TypeManager.GetValue ((FieldBuilder) fi);
+                                       else
+                                               o = fi.GetValue (fi);
+                                       
+                                       if (decl_type.IsSubclassOf (TypeManager.enum_type)) {
+                                               Expression enum_member = MemberLookup (
+                                                       ec, decl_type, "value__", MemberTypes.Field,
+                                                       AllBindingFlags, Location); 
+
+                                               Enum en = TypeManager.LookupEnum (decl_type);
+
+                                               Constant c;
+                                               if (en != null)
+                                                       c = Constantify (o, en.UnderlyingType);
+                                               else 
+                                                       c = Constantify (o, enum_member.Type);
+                                               
+                                               return new EnumConstant (c, decl_type);
+                                       }
+                                       
+                                       Expression exp = Constantify (o, t);
+                               }
+                                       
                                return e;
                        }                               
 
@@ -3009,7 +3281,7 @@ namespace Mono.CSharp {
                                EventExpr ee = (EventExpr) e;
 
                                Expression ml = MemberLookup (
-                                       ec, ec.TypeContainer.TypeBuilder, ee.EventInfo.Name,
+                                       ec, ec.DeclSpace.TypeBuilder, ee.EventInfo.Name,
                                        MemberTypes.Event, AllBindingFlags, Location);
 
                                if (ml != null) {
@@ -3069,7 +3341,7 @@ namespace Mono.CSharp {
 
                        Error (103, Location, "The name `" + Name +
                               "' does not exist in the class `" +
-                              ec.TypeContainer.Name + "'");
+                              ec.DeclSpace.Name + "'");
                }
        }
        
@@ -3101,21 +3373,36 @@ namespace Mono.CSharp {
        /// </summary>
        public class MethodGroupExpr : Expression {
                public MethodBase [] Methods;
+               Location loc;
                Expression instance_expression = null;
                
-               public MethodGroupExpr (MemberInfo [] mi)
+               public MethodGroupExpr (MemberInfo [] mi, Location l)
                {
                        Methods = new MethodBase [mi.Length];
                        mi.CopyTo (Methods, 0);
                        eclass = ExprClass.MethodGroup;
+                       type = TypeManager.object_type;
+                       loc = l;
                }
 
-               public MethodGroupExpr (ArrayList l)
+               public MethodGroupExpr (ArrayList list, Location l)
                {
-                       Methods = new MethodBase [l.Count];
+                       Methods = new MethodBase [list.Count];
 
-                       l.CopyTo (Methods, 0);
+                       try {
+                               list.CopyTo (Methods, 0);
+                       } catch {
+                               foreach (MemberInfo m in list){
+                                       if (!(m is MethodBase)){
+                                               Console.WriteLine ("Name " + m.Name);
+                                               Console.WriteLine ("Found a: " + m.GetType ().FullName);
+                                       }
+                               }
+                               throw;
+                       }
+                       loc = l;
                        eclass = ExprClass.MethodGroup;
+                       type = TypeManager.object_type;
                }
                
                //
@@ -3136,20 +3423,22 @@ namespace Mono.CSharp {
                        return this;
                }
 
+               public void ReportUsageError ()
+               {
+                       Report.Error (654, loc, "Method `" + Methods [0].DeclaringType + "." +
+                                     Methods [0].Name + "()' is referenced without parentheses");
+               }
+
                override public void Emit (EmitContext ec)
                {
-                       throw new Exception ("This should never be reached");
+                       ReportUsageError ();
                }
 
                bool RemoveMethods (bool keep_static)
                {
                        ArrayList smethods = new ArrayList ();
-                       int top = Methods.Length;
-                       int i;
-                       
-                       for (i = 0; i < top; i++){
-                               MethodBase mb = Methods [i];
 
+                       foreach (MethodBase mb in Methods){
                                if (mb.IsStatic == keep_static)
                                        smethods.Add (mb);
                        }
@@ -3215,6 +3504,20 @@ namespace Mono.CSharp {
                        return this;
                }
 
+               void Report_AssignToReadonly (bool is_instance)
+               {
+                       string msg;
+                       
+                       if (is_instance)
+                               msg = "Readonly field can not be assigned outside " +
+                               "of constructor or variable initializer";
+                       else
+                               msg = "A static readonly field can only be assigned in " +
+                               "a static constructor";
+
+                       Report.Error (is_instance ? 191 : 198, loc, msg);
+               }
+               
                override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
                {
                        Expression e = DoResolve (ec);
@@ -3232,9 +3535,7 @@ namespace Mono.CSharp {
                        if (ec.IsConstructor)
                                return this;
 
-                       Report.Error (191, loc,
-                                     "Readonly field can not be assigned outside " +
-                                     "of constructor or variable initializer");
+                       Report_AssignToReadonly (true);
                        
                        return null;
                }
@@ -3245,8 +3546,9 @@ namespace Mono.CSharp {
                        bool is_volatile = false;
                                
                        if (FieldInfo is FieldBuilder){
-                               Field f = TypeManager.GetField (FieldInfo);
-                               if (f != null && (f.ModFlags & Modifiers.VOLATILE) != 0)
+                               FieldBase f = TypeManager.GetField (FieldInfo);
+
+                               if ((f.ModFlags & Modifiers.VOLATILE) != 0)
                                        is_volatile = true;
                                
                                f.status |= Field.Status.USED;
@@ -3272,7 +3574,7 @@ namespace Mono.CSharp {
                                        } else
                                                ml = (IMemoryLocation) InstanceExpression;
 
-                                       ml.AddressOf (ec);
+                                       ml.AddressOf (ec, AddressOp.Load);
                                } else 
                                        InstanceExpression.Emit (ec);
 
@@ -3285,8 +3587,15 @@ namespace Mono.CSharp {
 
                public void EmitAssign (EmitContext ec, Expression source)
                {
-                       bool is_static = FieldInfo.IsStatic;
+                       FieldAttributes fa = FieldInfo.Attributes;
+                       bool is_static = (fa & FieldAttributes.Static) != 0;
+                       bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
                        ILGenerator ig = ec.ig;
+
+                       if (is_readonly && !ec.IsConstructor){
+                               Report_AssignToReadonly (!is_static);
+                               return;
+                       }
                        
                        if (!is_static){
                                Expression instance = InstanceExpression;
@@ -3295,7 +3604,7 @@ namespace Mono.CSharp {
                                        if (instance is IMemoryLocation){
                                                IMemoryLocation ml = (IMemoryLocation) instance;
 
-                                               ml.AddressOf (ec);
+                                               ml.AddressOf (ec, AddressOp.Store);
                                        } else
                                                throw new Exception ("The " + instance + " of type " +
                                                                     instance.Type +
@@ -3307,8 +3616,9 @@ namespace Mono.CSharp {
                        source.Emit (ec);
 
                        if (FieldInfo is FieldBuilder){
-                               Field f = TypeManager.GetField (FieldInfo);
-                               if (f != null && (f.ModFlags & Modifiers.VOLATILE) != 0)
+                               FieldBase f = TypeManager.GetField (FieldInfo);
+                               
+                               if ((f.ModFlags & Modifiers.VOLATILE) != 0)
                                        ig.Emit (OpCodes.Volatile);
                        }
                        
@@ -3318,37 +3628,29 @@ namespace Mono.CSharp {
                                ig.Emit (OpCodes.Stfld, FieldInfo);
 
                        if (FieldInfo is FieldBuilder){
-                               Field f = TypeManager.GetField (FieldInfo);
+                               FieldBase f = TypeManager.GetField (FieldInfo);
 
                                f.status |= Field.Status.ASSIGNED;
                        }
                }
                
-               public void AddressOf (EmitContext ec)
+               public void AddressOf (EmitContext ec, AddressOp mode)
                {
                        ILGenerator ig = ec.ig;
                        
                        if (FieldInfo is FieldBuilder){
-                               Field f = TypeManager.GetField (FieldInfo);
-                               if (f != null && (f.ModFlags & Modifiers.VOLATILE) != 0)
+                               FieldBase f = TypeManager.GetField (FieldInfo);
+                               if ((f.ModFlags & Modifiers.VOLATILE) != 0)
                                        ig.Emit (OpCodes.Volatile);
                        }
 
-                       //
-                       // FIXME:
-                       //
-                       // Mhm.  We do not know what we are being used for:
-                       // READING or WRITING the field.
-                       //
-                       // I think we want an extra argument to AddressOf to pass
-                       // this semantic information.
-                       //
-                       // For now: just flag both assigned and used.
-                       //
                        if (FieldInfo is FieldBuilder){
-                               Field f = TypeManager.GetField (FieldInfo);
+                               FieldBase f = TypeManager.GetField (FieldInfo);
 
-                               f.status |= Field.Status.ASSIGNED | Field.Status.USED;
+                               if ((mode & AddressOp.Store) != 0)
+                                       f.status |= Field.Status.ASSIGNED;
+                               if ((mode & AddressOp.Load) != 0)
+                                       f.status |= Field.Status.USED;
                        }
 
                        //
@@ -3399,9 +3701,9 @@ namespace Mono.CSharp {
                        Accessors = TypeManager.GetAccessors (pi);
 
                        if (Accessors != null)
-                               for (int i = 0; i < Accessors.Length; i++){
-                                       if (Accessors [i] != null)
-                                               if (Accessors [i].IsStatic)
+                               foreach (MethodInfo mi in Accessors){
+                                       if (mi != null)
+                                               if (mi.IsStatic)
                                                        IsStatic = true;
                                }
                        else
@@ -3452,7 +3754,22 @@ namespace Mono.CSharp {
 
                override public void Emit (EmitContext ec)
                {
-                       Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, Accessors [0], null);
+                       MethodInfo method = Accessors [0];
+
+                       //
+                       // Special case: length of single dimension array is turned into ldlen
+                       //
+                       if (method == TypeManager.int_array_get_length){
+                               Type iet = instance_expr.Type;
+
+                               if (iet.GetArrayRank () == 1){
+                                       instance_expr.Emit (ec);
+                                       ec.ig.Emit (OpCodes.Ldlen);
+                                       return;
+                               }
+                       }
+
+                       Invocation.EmitCall (ec, IsBase, IsStatic, instance_expr, method, null);
                        
                }