2007-02-06 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / ecore.cs
index 292b30684bfab5a7f0f2f7e2b37fe18e144c0452..c93d9f12c6dc4828ab5a3dd65a8a5d81f787a15f 100644 (file)
@@ -266,11 +266,8 @@ namespace Mono.CSharp {
 
                        FullNamedExpression fne = ResolveAsTypeStep (ec, silent);
 
-                       if (fne == null){
-                               if (!silent && errors == Report.Errors)
-                                       Report.Error (118, loc, "Expecting a type.");
+                       if (fne == null)
                                return null;
-                       }
 
                        if (fne.eclass != ExprClass.Type) {
                                if (!silent && errors == Report.Errors)
@@ -297,8 +294,8 @@ namespace Mono.CSharp {
 
                protected static void Error_CannotAccessProtected (Location loc, MemberInfo m, Type qualifier, Type container)
                {
-                       Report.Error (1540, loc, "Cannot access protected member `{0}' via a qualifier of type `{1}';"
-                               + " the qualifier must be of type `{2}' (or derived from it)", 
+                       Report.Error (1540, loc, "Cannot access protected member `{0}' via a qualifier of type `{1}'."
+                               + " The qualifier must be of type `{2}' or derived from it", 
                                TypeManager.GetFullNameSignature (m),
                                TypeManager.CSharpName (qualifier),
                                TypeManager.CSharpName (container));
@@ -316,7 +313,7 @@ namespace Mono.CSharp {
                        Report.Error (1547, loc, "Keyword `void' cannot be used in this context");
                }
 
-               public virtual void Error_ValueCannotBeConverted (Location loc, Type target, bool expl)
+               public virtual void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type target, bool expl)
                {
                        if (Type.FullName == target.FullName){
                                Report.ExtraInformation (loc,
@@ -335,15 +332,20 @@ namespace Mono.CSharp {
                        Expression e = (this is EnumConstant) ? ((EnumConstant)this).Child : this;
                        bool b = Convert.ExplicitNumericConversion (e, target) != null;
 
-                       if (b || Convert.ExplicitReferenceConversionExists (Type, target) || Convert.ExplicitUnsafe (e, target) != null) {
-                               Report.Error (266, loc, "Cannot implicitly convert type `{0}' to `{1}'. An explicit conversion exists (are you missing a cast?)",
+                       if (b ||
+                           Convert.ExplicitReferenceConversionExists (Type, target) ||
+                           Convert.ExplicitUnsafe (e, target) != null ||
+                           (ec != null && Convert.UserDefinedConversion (ec, this, target, Location.Null, true) != null))
+                       {
+                               Report.Error (266, loc, "Cannot implicitly convert type `{0}' to `{1}'. " +
+                                             "An explicit conversion exists (are you missing a cast?)",
                                        TypeManager.CSharpName (Type), TypeManager.CSharpName (target));
                                return;
                        }
 
                        if (Type != TypeManager.string_type && this is Constant && !(this is EmptyConstantCast)) {
                                Report.Error (31, loc, "Constant value `{0}' cannot be converted to a `{1}'",
-                                       GetSignatureForError (), TypeManager.CSharpName (target));
+                                       ((Constant)(this)).GetValue ().ToString (), TypeManager.CSharpName (target));
                                return;
                        }
 
@@ -353,12 +355,17 @@ namespace Mono.CSharp {
                                TypeManager.CSharpName (target));
                }
 
-               protected static void Error_TypeDoesNotContainDefinition (Location loc, Type type, string name)
+               public static void Error_TypeDoesNotContainDefinition (Location loc, Type type, string name)
                {
                        Report.Error (117, loc, "`{0}' does not contain a definition for `{1}'",
                                TypeManager.CSharpName (type), name);
                }
 
+               protected static void Error_ValueAssignment (Location loc)
+               {
+                       Report.Error (131, loc, "The left-hand side of an assignment must be a variable, a property or an indexer");
+               }
+
                ResolveFlags ExprClassToResolveFlags
                {
                        get {
@@ -489,7 +496,7 @@ namespace Mono.CSharp {
                                        if (out_access)
                                                Report.Error (1510, loc, "A ref or out argument must be an assignable variable");
                                        else
-                                               Report.Error (131, loc, "The left-hand side of an assignment or mutating operation must be a variable, property or indexer");
+                                               Error_ValueAssignment (loc);
                                }
                                return null;
                        }
@@ -535,64 +542,6 @@ namespace Mono.CSharp {
                        type = null;
                }
 
-               /// <summary>
-               ///   Returns a literalized version of a literal FieldInfo
-               /// </summary>
-               ///
-               /// <remarks>
-               ///   The possible return values are:
-               ///      IntConstant, UIntConstant
-               ///      LongLiteral, ULongConstant
-               ///      FloatConstant, DoubleConstant
-               ///      StringConstant
-               ///
-               ///   The value returned is already resolved.
-               /// </remarks>
-               public static Constant Constantify (object v, Type t)
-               {
-                       if (t == TypeManager.int32_type)
-                               return new IntConstant ((int) v, Location.Null);
-                       else if (t == TypeManager.uint32_type)
-                               return new UIntConstant ((uint) v, Location.Null);
-                       else if (t == TypeManager.int64_type)
-                               return new LongConstant ((long) v, Location.Null);
-                       else if (t == TypeManager.uint64_type)
-                               return new ULongConstant ((ulong) v, Location.Null);
-                       else if (t == TypeManager.float_type)
-                               return new FloatConstant ((float) v, Location.Null);
-                       else if (t == TypeManager.double_type)
-                               return new DoubleConstant ((double) v, Location.Null);
-                       else if (t == TypeManager.string_type)
-                               return new StringConstant ((string) v, Location.Null);
-                       else if (t == TypeManager.short_type)
-                               return new ShortConstant ((short)v, Location.Null);
-                       else if (t == TypeManager.ushort_type)
-                               return new UShortConstant ((ushort)v, Location.Null);
-                       else if (t == TypeManager.sbyte_type)
-                               return new SByteConstant ((sbyte)v, Location.Null);
-                       else if (t == TypeManager.byte_type)
-                               return new ByteConstant ((byte)v, Location.Null);
-                       else if (t == TypeManager.char_type)
-                               return new CharConstant ((char)v, Location.Null);
-                       else if (t == TypeManager.bool_type)
-                               return new BoolConstant ((bool) v, Location.Null);
-                       else if (t == TypeManager.decimal_type)
-                               return new DecimalConstant ((decimal) v, Location.Null);
-                       else if (TypeManager.IsEnumType (t)){
-                               Type real_type = TypeManager.TypeToCoreType (v.GetType ());
-                               if (real_type == t)
-                                       real_type = System.Enum.GetUnderlyingType (real_type);
-
-                               Constant e = Constantify (v, real_type);
-
-                               return new EnumConstant (e, t);
-                       } else if (v == null && !TypeManager.IsValueType (t))
-                               return new NullLiteral (Location.Null);
-                       else
-                               throw new Exception ("Unknown type for constant (" + t +
-                                                    "), details: " + v);
-               }
-
                /// <summary>
                ///   Returns a fully formed expression after a MemberLookup
                /// </summary>
@@ -749,10 +698,10 @@ namespace Mono.CSharp {
                                             name, AllMemberTypes, AllBindingFlags, loc);
                }
 
-               public static Expression MethodLookup (EmitContext ec, Type queried_type,
+               public static Expression MethodLookup (Type container_type, Type queried_type,
                                                       string name, Location loc)
                {
-                       return MemberLookup (ec.ContainerType, null, queried_type, name,
+                       return MemberLookup (container_type, null, queried_type, name,
                                             MemberTypes.Method, AllBindingFlags, loc);
                }
 
@@ -821,7 +770,6 @@ namespace Mono.CSharp {
                                                // then we are in this situation.
                                                Error_CannotAccessProtected (loc, m, qualifier_type, container_type);
                                        } else {
-                                               Report.SymbolRelatedToPreviousError (m);
                                                ErrorIsInaccesible (loc, TypeManager.GetFullNameSignature (m));
                                        }
                                }
@@ -905,7 +853,7 @@ namespace Mono.CSharp {
                                return new Nullable.OperatorTrueOrFalse (e, is_true, loc).Resolve (ec);
 #endif
 
-                       operator_group = MethodLookup (ec, e.Type, is_true ? "op_True" : "op_False", loc);
+                       operator_group = MethodLookup (ec.ContainerType, e.Type, is_true ? "op_True" : "op_False", loc);
                        if (operator_group == null)
                                return null;
 
@@ -943,7 +891,7 @@ namespace Mono.CSharp {
                        //
                        converted = Expression.GetOperatorTrue (ec, e, loc);
                        if (converted == null){
-                               e.Error_ValueCannotBeConverted (loc, TypeManager.bool_type, false);
+                               e.Error_ValueCannotBeConverted (ec, loc, TypeManager.bool_type, false);
                                return null;
                        }
                        return converted;
@@ -1162,7 +1110,7 @@ namespace Mono.CSharp {
                                        target = Convert.ImplicitConversion (ec, source, TypeManager.uint64_type, loc);
 
                                if (target == null) {
-                                       source.Error_ValueCannotBeConverted (loc, TypeManager.int32_type, false);
+                                       source.Error_ValueCannotBeConverted (ec, loc, TypeManager.int32_type, false);
                                        return null;
                                }
                        }
@@ -1443,9 +1391,10 @@ namespace Mono.CSharp {
                        return child.GetValue ();
                }
 
-               public override Constant Reduce (bool inCheckedContext, Type target_type)
+               public override Constant ConvertExplicitly (bool inCheckedContext, Type target_type)
                {
-                       return child.Reduce (inCheckedContext, target_type);
+                       // FIXME: check that 'type' can be converted to 'target_type' first
+                       return child.ConvertExplicitly (inCheckedContext, target_type);
                }
 
                public override Constant Increment ()
@@ -1453,13 +1402,11 @@ namespace Mono.CSharp {
                        return child.Increment ();
                }
 
-               public override bool IsDefaultValue
-               {
+               public override bool IsDefaultValue {
                        get { return child.IsDefaultValue; }
                }
 
-               public override bool IsNegative
-               {
+               public override bool IsNegative {
                        get { return child.IsNegative; }
                }
 
@@ -1468,9 +1415,12 @@ namespace Mono.CSharp {
                        child.Emit (ec);
                }
 
-               public override Constant ToType (Type type)
+               public override Constant ConvertImplicitly (Type target_type)
                {
-                       return child.ToType (type);
+                       // FIXME: Do we need to check user conversions?
+                       if (!Convert.ImplicitStandardConversionExists (this, target_type))
+                               return null;
+                       return child.ConvertImplicitly (target_type);
                }
        }
 
@@ -1530,37 +1480,8 @@ namespace Mono.CSharp {
                
                public override string AsString ()
                {
-                       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 ();
+                       string value = System.Enum.GetName (type, Child.GetValue ());
+                       return value == null ? "0" : value;
                }
 
                public override Constant Increment()
@@ -1584,15 +1505,15 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override Constant Reduce(bool inCheckedContext, Type target_type)
+               public override Constant ConvertExplicitly(bool inCheckedContext, Type target_type)
                {
                        if (Child.Type == target_type)
                                return Child;
 
-                       return Child.Reduce (inCheckedContext, target_type);
+                       return Child.ConvertExplicitly (inCheckedContext, target_type);
                }
 
-               public override Constant ToType (Type type)
+               public override Constant ConvertImplicitly (Type type)
                {
                        if (Type == type) {
                                // This is workaround of mono bug. It can be removed when the latest corlib spreads enough
@@ -1600,7 +1521,7 @@ namespace Mono.CSharp {
                                        return this;
 
                                if (type.UnderlyingSystemType != Child.Type)
-                                       Child = Child.ToType (type.UnderlyingSystemType);
+                                       Child = Child.ConvertImplicitly (type.UnderlyingSystemType);
                                return this;
                        }
 
@@ -1608,7 +1529,7 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       return Child.ToType (type);
+                       return Child.ConvertImplicitly(type);
                }
 
        }
@@ -2007,14 +1928,19 @@ namespace Mono.CSharp {
                public static string RemoveGenericArity (string name)
                {
                        int start = 0;
-                       StringBuilder sb = new StringBuilder ();
-                       while (start < name.Length) {
+                       StringBuilder sb = null;
+                       do {
                                int pos = name.IndexOf ('`', start);
                                if (pos < 0) {
+                                       if (start == 0)
+                                               return name;
+
                                        sb.Append (name.Substring (start));
                                        break;
                                }
 
+                               if (sb == null)
+                                       sb = new StringBuilder ();
                                sb.Append (name.Substring (start, pos-start));
 
                                pos++;
@@ -2022,7 +1948,7 @@ namespace Mono.CSharp {
                                        pos++;
 
                                start = pos;
-                       }
+                       } while (start < name.Length);
 
                        return sb.ToString ();
                }
@@ -2048,7 +1974,7 @@ namespace Mono.CSharp {
                {
                        return resolved_to != null && resolved_to.Type != null && 
                                resolved_to.Type.Name == Name &&
-                               (ec.DeclContainer.LookupType (Name, loc, /* ignore_cs0104 = */ true) != null);
+                               (ec.DeclContainer.LookupNamespaceOrType (Name, loc, /* ignore_cs0104 = */ true) != null);
                }
 
                public override Expression DoResolve (EmitContext ec)
@@ -2126,7 +2052,7 @@ namespace Mono.CSharp {
                                return fne.ResolveAsTypeStep (ec, silent);
 
                        int errors = Report.Errors;
-                       fne = ec.DeclContainer.LookupType (Name, loc, /*ignore_cs0104=*/ false);
+                       fne = ec.DeclContainer.LookupNamespaceOrType (Name, loc, /*ignore_cs0104=*/ false);
 
                        if (fne != null) {
                                if (fne.Type == null)
@@ -2176,9 +2102,9 @@ namespace Mono.CSharp {
 
                // TODO: I am still not convinced about this. If someone else will need it
                // implement this as virtual property in MemberCore hierarchy
-               string GetMemberType (MemberCore mc)
+               public static string GetMemberType (MemberCore mc)
                {
-                       if (mc is PropertyBase)
+                       if (mc is Property)
                                return "property";
                        if (mc is Indexer)
                                return "indexer";
@@ -2188,6 +2114,8 @@ namespace Mono.CSharp {
                                return "method";
                        if (mc is EnumMember)
                                return "enum";
+                       if (mc is Event)
+                               return "event";
 
                        return "type";
                }
@@ -2879,7 +2807,10 @@ namespace Mono.CSharp {
                        Methods = new MethodBase [mi.Length];
                        mi.CopyTo (Methods, 0);
                        eclass = ExprClass.MethodGroup;
-                       type = TypeManager.object_type;
+
+                       // Set the type to something that will never be useful, which will
+                       // trigger the proper conversions.
+                       type = typeof (MethodGroupExpr);
                        loc = l;
                }
 
@@ -3193,7 +3124,7 @@ namespace Mono.CSharp {
                                                ic.CheckObsoleteness (loc);
                                }
 
-                               return ic.Value;
+                               return ic.CreateConstantReference (loc);
                        }
                        
                        if (t.IsPointer && !ec.InUnsafe) {
@@ -3592,8 +3523,6 @@ namespace Mono.CSharp {
                LocalTemporary temp;
                bool prepared;
 
-               internal static PtrHashtable AccessorTable = new PtrHashtable (); 
-
                public PropertyExpr (Type containerType, PropertyInfo pi, Location l)
                {
                        PropertyInfo = pi;
@@ -3673,7 +3602,7 @@ namespace Mono.CSharp {
                // We also perform the permission checking here, as the PropertyInfo does not
                // hold the information for the accessibility of its setter/getter
                //
-               // TODO: can use TypeManager.GetProperty to boost performance
+               // TODO: Refactor to use some kind of cache together with GetPropertyFromAccessor
                void ResolveAccessors (Type containerType)
                {
                        FindAccessors (containerType);
@@ -3684,7 +3613,6 @@ namespace Mono.CSharp {
                                if (md != null)
                                        md.SetMemberIsUsed ();
 
-                               AccessorTable [getter] = PropertyInfo;
                                is_static = getter.IsStatic;
                        }
 
@@ -3694,7 +3622,6 @@ namespace Mono.CSharp {
                                if (md != null)
                                        md.SetMemberIsUsed ();
 
-                               AccessorTable [setter] = PropertyInfo;
                                is_static = setter.IsStatic;
                        }
                }
@@ -3711,10 +3638,10 @@ namespace Mono.CSharp {
                                return false;
                        }
 
-                       if (lvalue_instance)
+                       InstanceExpression = InstanceExpression.DoResolve (ec);
+                       if (lvalue_instance && InstanceExpression != null)
                                InstanceExpression = InstanceExpression.ResolveLValue (ec, EmptyExpression.LValueMemberAccess, loc);
-                       else
-                               InstanceExpression = InstanceExpression.DoResolve (ec);
+
                        if (InstanceExpression == null)
                                return false;
 
@@ -3722,8 +3649,9 @@ namespace Mono.CSharp {
 
                        if (must_do_cs1540_check && (InstanceExpression != EmptyExpression.Null) &&
                            !TypeManager.IsInstantiationOfSameGenericType (InstanceExpression.Type, ec.ContainerType) &&
-                           TypeManager.IsFamilyAccessible (ec.ContainerType, PropertyInfo.DeclaringType) &&
-                           !TypeManager.IsFamilyAccessible (InstanceExpression.Type, ec.ContainerType)) {
+                           !TypeManager.IsNestedChildOf (ec.ContainerType, InstanceExpression.Type) &&
+                           !TypeManager.IsSubclassOf (InstanceExpression.Type, ec.ContainerType)) {
+                               Report.SymbolRelatedToPreviousError (PropertyInfo);
                                Error_CannotAccessProtected (loc, PropertyInfo, InstanceExpression.Type, ec.ContainerType);
                                return false;
                        }
@@ -3965,8 +3893,6 @@ namespace Mono.CSharp {
                bool is_static;
                MethodInfo add_accessor, remove_accessor;
 
-               internal static PtrHashtable AccessorTable = new PtrHashtable (); 
-               
                public EventExpr (EventInfo ei, Location loc)
                {
                        EventInfo = ei;
@@ -3975,11 +3901,6 @@ namespace Mono.CSharp {
 
                        add_accessor = TypeManager.GetAddMethod (ei);
                        remove_accessor = TypeManager.GetRemoveMethod (ei);
-                       if (add_accessor != null)
-                               AccessorTable [add_accessor] = ei;
-                       if (remove_accessor != null)
-                               AccessorTable [remove_accessor] = ei;
-                       
                        if (add_accessor.IsStatic || remove_accessor.IsStatic)
                                is_static = true;
 
@@ -4024,15 +3945,13 @@ namespace Mono.CSharp {
 
                        if (EventInfo.DeclaringType == ec.ContainerType ||
                            TypeManager.IsNestedChildOf(ec.ContainerType, EventInfo.DeclaringType)) {
-                               MemberInfo mi = TypeManager.GetPrivateFieldOfEvent (EventInfo);
+                               EventField mi = TypeManager.GetEventField (EventInfo);
 
                                if (mi != null) {
-                                       MemberExpr ml = (MemberExpr) ExprClassFromMemberInfo (ec.ContainerType, mi, loc);
+                                       if (!ec.IsInObsoleteScope)
+                                               mi.CheckObsoleteness (loc);
 
-                                       if (ml == null) {
-                                               Report.Error (-200, loc, "Internal error!!");
-                                               return null;
-                                       }
+                                       FieldExpr ml = new FieldExpr (mi.FieldBuilder, loc);
 
                                        InstanceExpression = null;
                                
@@ -4112,7 +4031,11 @@ namespace Mono.CSharp {
 
                public void EmitAddOrRemove (EmitContext ec, Expression source)
                {
-                       BinaryDelegate source_del = (BinaryDelegate) source;
+                       BinaryDelegate source_del = source as BinaryDelegate;
+                       if (source_del == null) {
+                               Emit (ec);
+                               return;
+                       }
                        Expression handler = source_del.Right;
                        
                        Argument arg = new Argument (handler, Argument.AType.Expression);