2004-12-08 Martin Baulig <martin@ximian.com>
[mono.git] / mcs / gmcs / expression.cs
old mode 100755 (executable)
new mode 100644 (file)
index cd9bc2d..87e0a95
@@ -323,11 +323,22 @@ namespace Mono.CSharp {
 
                Expression ResolveOperator (EmitContext ec)
                {
-                       Type expr_type = Expr.Type;
+                       //
+                       // Step 1: Default operations on CLI native types.
+                       //
+
+                       // Attempt to use a constant folding operation.
+                       if (Expr is Constant){
+                               Expression result;
+                               
+                               if (Reduce (ec, (Constant) Expr, out result))
+                                       return result;
+                       }
 
                        //
-                       // Step 1: Perform Operator Overload location
+                       // Step 2: Perform Operator Overload location
                        //
+                       Type expr_type = Expr.Type;
                        Expression mg;
                        string op_name;
                        
@@ -353,18 +364,6 @@ namespace Mono.CSharp {
                        if (expr_type == null)
                                return null;
                        
-                       //
-                       // Step 2: Default operations on CLI native types.
-                       //
-
-                       // Attempt to use a constant folding operation.
-                       if (Expr is Constant){
-                               Expression result;
-                               
-                               if (Reduce (ec, (Constant) Expr, out result))
-                                       return result;
-                       }
-
                        switch (Oper){
                        case Operator.LogicalNot:
                                if (expr_type != TypeManager.bool_type) {
@@ -446,6 +445,7 @@ namespace Mono.CSharp {
                                                return null;
                                        }
                                        lr.local_info.AddressTaken = true;
+                                       lr.local_info.Used = true;
                                }
 
                                // According to the specs, a variable is considered definitely assigned if you take
@@ -1162,6 +1162,9 @@ namespace Mono.CSharp {
 
                                warning_always_matches = true;
                        } else if (Convert.ExplicitReferenceConversionExists (etype, probe_type)){
+                               if (etype.IsGenericParameter)
+                                       expr = new BoxedCast (expr, etype);
+
                                //
                                // Second case: explicit reference convresion
                                //
@@ -1174,15 +1177,15 @@ namespace Mono.CSharp {
                                warning_never_matches = true;
                        }
                        
-                               if (warning_always_matches)
+                       if (warning_always_matches)
                                Warning (183, "The given expression is always of the provided ('{0}') type", TypeManager.CSharpName (probe_type));
-                               else if (warning_never_matches){
-                                       if (!(probe_type.IsInterface || expr.Type.IsInterface))
+                       else if (warning_never_matches){
+                               if (!(probe_type.IsInterface || expr.Type.IsInterface))
                                        Warning (184, "The given expression is never of the provided ('{0}') type", TypeManager.CSharpName (probe_type));
                        }
 
                        return this;
-               }                               
+               }
        }
 
        /// <summary>
@@ -1240,6 +1243,9 @@ namespace Mono.CSharp {
                        }
 
                        if (Convert.ExplicitReferenceConversionExists (etype, probe_type)){
+                               if (etype.IsGenericParameter)
+                                       expr = new BoxedCast (expr, etype);
+
                                do_isinst = true;
                                return this;
                        }
@@ -2705,16 +2711,27 @@ namespace Mono.CSharp {
                                }
                        } else
                                left = left.Resolve (ec);
-                       right = right.Resolve (ec);
 
-                       if (left == null || right == null)
+                       if (left == null)
+                               return null;
+
+                       Constant lc = left as Constant;
+                       if (lc != null && lc.Type == TypeManager.bool_type && 
+                               ((oper == Operator.LogicalAnd && (bool)lc.GetValue () == false) ||
+                                (oper == Operator.LogicalOr && (bool)lc.GetValue () == true))) {
+
+                               // TODO: make a sence to resolve unreachable expression as we do for statement
+                               Report.Warning (429, 4, loc, "Unreachable expression code detected");
+                               return left;
+                       }
+
+                       right = right.Resolve (ec);
+                       if (right == null)
                                return null;
 
                        eclass = ExprClass.Value;
 
                        Constant rc = right as Constant;
-                       Constant lc = left as Constant;
-
                        if (rc != null & lc != null){
                                Expression e = ConstantFold.BinaryFold (
                                        ec, oper, lc, rc, loc);
@@ -3610,6 +3627,11 @@ namespace Mono.CSharp {
                {
                        if (local_info == null) {
                                local_info = Block.GetLocalInfo (Name);
+
+                               // is out param
+                               if (lvalue_right_side == EmptyExpression.Null)
+                                       local_info.Used = true;
+
                                is_readonly = local_info.ReadOnly;
                        }
 
@@ -4167,8 +4189,9 @@ namespace Mono.CSharp {
 
                                        if (instance.GetType () != typeof (This)){
                                                if (fe.InstanceExpression.Type.IsSubclassOf (TypeManager.mbr_type)){
-                                                       Report.Error (197, loc,
-                                                                     "Can not pass a type that derives from MarshalByRefObject with out or ref");
+                                                       Report.SymbolRelatedToPreviousError (fe.InstanceExpression.Type);
+                                                       Report.Error (197, loc, "Cannot pass '{0}' as ref or out or take its address because it is a member of a marshal-by-reference class",
+                                                               fe.Name);
                                                        return false;
                                                }
                                        }
@@ -4884,7 +4907,7 @@ namespace Mono.CSharp {
                                                        "method `{0}' cannot be infered from " +
                                                        "the usage. Try specifying the type " +
                                                        "arguments explicitly.", report_name);
-                                               break;
+                                               return null;
                                        }
 
                                        Error_WrongNumArguments (
@@ -5128,7 +5151,7 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               static bool InferType (Type pt, Type at, ref Type[] infered)
+               static bool InferType (Type pt, Type at, Type[] infered)
                {
                        if (pt.IsGenericParameter && (pt.DeclaringMethod != null)) {
                                int pos = pt.GenericParameterPosition;
@@ -5151,16 +5174,19 @@ namespace Mono.CSharp {
                                return true;
                        }
 
-                       if (!pt.ContainsGenericParameters)
-                               return true;
+                       if (!pt.ContainsGenericParameters) {
+                               if (at.ContainsGenericParameters)
+                                       return InferType (at, pt, infered);
+                               else
+                                       return true;
+                       }
 
                        if (at.IsArray) {
                                if (!pt.IsArray ||
                                    (at.GetArrayRank () != pt.GetArrayRank ()))
                                        return false;
 
-                               return InferType (pt.GetElementType (), at.GetElementType (),
-                                                 ref infered);
+                               return InferType (pt.GetElementType (), at.GetElementType (), infered);
                        }
 
                        if (pt.IsArray) {
@@ -5168,36 +5194,63 @@ namespace Mono.CSharp {
                                    (pt.GetArrayRank () != at.GetArrayRank ()))
                                        return false;
 
-                               return InferType (pt.GetElementType (), at.GetElementType (),
-                                                 ref infered);
+                               return InferType (pt.GetElementType (), at.GetElementType (), infered);
                        }
 
-                       if (!at.IsGenericInstance)
-                               return false;
+                       if (pt.IsByRef && at.IsByRef)
+                               return InferType (pt.GetElementType (), at.GetElementType (), infered);
+                       ArrayList list = new ArrayList ();
+                       if (at.IsGenericInstance)
+                               list.Add (at);
+                       else {
+                               for (Type bt = at.BaseType; bt != null; bt = bt.BaseType)
+                                       list.Add (bt);
+
+                               list.AddRange (TypeManager.GetInterfaces (at));
+                       }
+
+                       bool found_one = false;
+
+                       foreach (Type type in list) {
+                               if (!type.IsGenericInstance)
+                                       continue;
+
+                               Type[] infered_types = new Type [infered.Length];
+
+                               if (!InferGenericInstance (pt, type, infered_types))
+                                       continue;
+
+                               for (int i = 0; i < infered_types.Length; i++) {
+                                       if (infered [i] == null) {
+                                               infered [i] = infered_types [i];
+                                               continue;
+                                       }
 
+                                       if (infered [i] != infered_types [i])
+                                               return false;
+                               }
+
+                               found_one = true;
+                       }
+
+                       return found_one;
+               }
+
+               static bool InferGenericInstance (Type pt, Type at, Type[] infered_types)
+               {
                        Type[] at_args = at.GetGenericArguments ();
                        Type[] pt_args = pt.GetGenericArguments ();
 
                        if (at_args.Length != pt_args.Length)
                                return false;
 
-                       Type[] infered_types = new Type [at_args.Length];
-
-                       for (int i = 0; i < at_args.Length; i++)
-                               if (!InferType (pt_args [i], at_args [i], ref infered_types))
-                                       return false;
-
-                       for (int i = 0; i < infered_types.Length; i++)
-                               if (infered_types [i] == null)
+                       for (int i = 0; i < at_args.Length; i++) {
+                               if (!InferType (pt_args [i], at_args [i], infered_types))
                                        return false;
+                       }
 
                        for (int i = 0; i < infered_types.Length; i++) {
-                               if (infered [i] == null) {
-                                       infered [i] = infered_types [i];
-                                       continue;
-                               }
-
-                               if (infered [i] != infered_types [i])
+                               if (infered_types [i] == null)
                                        return false;
                        }
 
@@ -5250,7 +5303,7 @@ namespace Mono.CSharp {
                                Type pt = pd.ParameterType (i);
                                Type at = a.Type;
 
-                               if (!InferType (pt, at, ref infered_types))
+                               if (!InferType (pt, at, infered_types))
                                        return false;
                        }
 
@@ -5262,7 +5315,7 @@ namespace Mono.CSharp {
                                if ((a.Expr is NullLiteral) || (a.Expr is MethodGroupExpr))
                                        continue;
 
-                               if (!InferType (element_type, a.Type, ref infered_types))
+                               if (!InferType (element_type, a.Type, infered_types))
                                        return false;
                        }
 
@@ -5274,8 +5327,7 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               public static bool InferTypeArguments (Type[] param_types, Type[] arg_types,
-                                                      ref Type[] infered_types)
+               public static bool InferTypeArguments (Type[] param_types, Type[] arg_types, Type[] infered_types)
                {
                        if (infered_types == null)
                                return false;
@@ -5284,8 +5336,7 @@ namespace Mono.CSharp {
                                if (arg_types [i] == null)
                                        continue;
 
-                               if (!InferType (param_types [i], arg_types [i],
-                                               ref infered_types))
+                               if (!InferType (param_types [i], arg_types [i], infered_types))
                                        return false;
                        }
 
@@ -5313,6 +5364,17 @@ namespace Mono.CSharp {
                                return false;
 
                        Type[] method_args = method.GetGenericArguments ();
+
+                       bool is_open = false;
+                       for (int i = 0; i < method_args.Length; i++) {
+                               if (method_args [i].IsGenericParameter) {
+                                       is_open = true;
+                                       break;
+                               }
+                       }
+                       if (!is_open)
+                               return true;
+
                        Type[] infered_types = new Type [method_args.Length];
 
                        Type[] param_types = new Type [pd.Count];
@@ -5328,7 +5390,7 @@ namespace Mono.CSharp {
                                arg_types [i] = a.Type;
                        }
 
-                       if (!InferTypeArguments (param_types, arg_types, ref infered_types))
+                       if (!InferTypeArguments (param_types, arg_types, infered_types))
                                return false;
 
                        method = method.BindGenericParameters (infered_types);
@@ -5356,7 +5418,7 @@ namespace Mono.CSharp {
                                arg_types [i] = apd.ParameterType (i);
                        }
 
-                       if (!InferTypeArguments (param_types, arg_types, ref infered_types))
+                       if (!InferTypeArguments (param_types, arg_types, infered_types))
                                return false;
 
                        method = method.BindGenericParameters (infered_types);
@@ -5458,6 +5520,9 @@ namespace Mono.CSharp {
                                }
                        }
                        
+                       if (mg.InstanceExpression != null)
+                               mg.InstanceExpression.CheckMarshallByRefAccess (ec.ContainerType);
+
                        eclass = ExprClass.Value;
                        return this;
                }
@@ -6023,7 +6088,7 @@ namespace Mono.CSharp {
                        if (IsDelegate){
                                RequestedType = (new NewDelegate (type, Arguments, loc)).Resolve (ec);
                                if (RequestedType != null)
-                                       if (!(RequestedType is NewDelegate))
+                                       if (!(RequestedType is DelegateCreation))
                                                throw new Exception ("NewDelegate.Resolve returned a non NewDelegate: " + RequestedType.GetType ());
                                return RequestedType;
                        }
@@ -7417,6 +7482,8 @@ namespace Mono.CSharp {
                        return RootContext.LookupType (ec.DeclSpace, sn.Name, true, loc) != null;
                }
                
+               // TODO: possible optimalization
+               // Cache resolved constant result in FieldBuilder <-> expresion map
                public static Expression ResolveMemberAccess (EmitContext ec, Expression member_lookup,
                                                              Expression left, Location loc,
                                                              Expression left_original)
@@ -7442,7 +7509,10 @@ namespace Mono.CSharp {
                                FieldInfo fi = fe.FieldInfo.Mono_GetGenericFieldDefinition ();
                                Type decl_type = fi.DeclaringType;
 
-                               if (fi is FieldBuilder) {
+                               bool is_emitted = fi is FieldBuilder;
+                               Type t = fi.FieldType;
+
+                               if (is_emitted) {
                                        Const c = TypeManager.LookupConstant ((FieldBuilder) fi);
                                        
                                        if (c != null) {
@@ -7452,16 +7522,21 @@ namespace Mono.CSharp {
 
                                                object real_value = ((Constant) c.Expr).GetValue ();
 
-                                               return Constantify (real_value, fi.FieldType);
+                                               return Constantify (real_value, t);
                                        }
                                }
 
+                               // IsInitOnly is because of MS compatibility, I don't know why but they emit decimal constant as InitOnly
+                               if (fi.IsInitOnly && !is_emitted && t == TypeManager.decimal_type) {
+                                       object[] attrs = fi.GetCustomAttributes (TypeManager.decimal_constant_attribute_type, false);
+                                       if (attrs.Length == 1)
+                                               return new DecimalConstant (((System.Runtime.CompilerServices.DecimalConstantAttribute) attrs [0]).Value);
+                               }
+
                                if (fi.IsLiteral) {
-                                       Type t = fi.FieldType;
-                                       
                                        object o;
 
-                                       if (fi is FieldBuilder)
+                                       if (is_emitted)
                                                o = TypeManager.GetValue ((FieldBuilder) fi);
                                        else
                                                o = fi.GetValue (fi);
@@ -7498,7 +7573,7 @@ namespace Mono.CSharp {
                                        return exp;
                                }
 
-                               if (fi.FieldType.IsPointer && !ec.InUnsafe){
+                               if (t.IsPointer && !ec.InUnsafe){
                                        UnsafeError (loc);
                                        return null;
                                }
@@ -8641,6 +8716,8 @@ namespace Mono.CSharp {
                                UnsafeError (loc);
                                return null;
                        }
+
+                       instance_expr.CheckMarshallByRefAccess (ec.ContainerType);
                        
                        eclass = ExprClass.IndexerAccess;
                        return this;
@@ -8711,6 +8788,8 @@ namespace Mono.CSharp {
                                }
                        }
                        
+                       instance_expr.CheckMarshallByRefAccess (ec.ContainerType);
+
                        eclass = ExprClass.IndexerAccess;
                        return this;
                }
@@ -8951,6 +9030,12 @@ namespace Mono.CSharp {
                        loc = l;
                }
 
+               public Expression Source {
+                       get {
+                               return source;
+                       }
+               }
+                       
                public override Expression DoResolve (EmitContext ec)
                {
                        //