2005-12-23 Miguel de Icaza <miguel@novell.com>
[mono.git] / mcs / mcs / expression.cs
index e8ee1d6ea42ed28403ee196abab8593d6fa7e56d..b2cb0c8f7aff2259f29db7959d0f70994b76f595 100644 (file)
@@ -3,6 +3,7 @@
 //
 // Author:
 //   Miguel de Icaza (miguel@ximian.com)
+//   Marek Safar (marek.safar@seznam.cz)
 //
 // (C) 2001, 2002, 2003 Ximian, Inc.
 // (C) 2003, 2004 Novell, Inc.
@@ -2309,6 +2310,9 @@ namespace Mono.CSharp {
 
                public override Expression DoResolve (EmitContext ec)
                {
+                       if (left == null)
+                               return null;
+
                        if ((oper == Operator.Subtraction) && (left is ParenthesizedExpression)) {
                                left = ((ParenthesizedExpression) left).Expr;
                                left = left.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.Type);
@@ -2373,6 +2377,11 @@ namespace Mono.CSharp {
                                if (rc is EnumConstant &&
                                    lc != null && lc.IsZeroInteger)
                                        return rc;
+                       } else if (oper == Operator.LogicalAnd) {
+                               if (rc != null && rc.IsDefaultValue && rc.Type == TypeManager.bool_type)
+                                       return rc;
+                               if (lc != null && lc.IsDefaultValue && lc.Type == TypeManager.bool_type)
+                                       return lc;
                        }
 
                        if (rc != null && lc != null){
@@ -3584,12 +3593,11 @@ namespace Mono.CSharp {
        ///   representation.
        /// </summary>
        public class ParameterReference : Expression, IAssignMethod, IMemoryLocation, IVariable {
-               Parameters pars;
-               String name;
+               Parameter par;
+               string name;
                int idx;
                Block block;
                VariableInfo vi;
-               public Parameter.Modifier mod;
                public bool is_ref, is_out, prepared;
 
                public bool IsOut {
@@ -3606,20 +3614,16 @@ namespace Mono.CSharp {
 
                LocalTemporary temp;
                
-               public ParameterReference (Parameters pars, Block block, int idx, string name, Location loc)
+               public ParameterReference (Parameter par, Block block, int idx, Location loc)
                {
-                       this.pars = pars;
+                       this.par = par;
+                       this.name = par.Name;
                        this.block = block;
                        this.idx  = idx;
-                       this.name = name;
                        this.loc = loc;
                        eclass = ExprClass.Variable;
                }
 
-               public ParameterReference (InternalParameters pars, Block block, int idx, Location loc)
-                       : this (pars.Parameters, block, idx, pars.ParameterName (idx), loc)
-               { }
-
                public VariableInfo VariableInfo {
                        get { return vi; }
                }
@@ -3627,7 +3631,7 @@ namespace Mono.CSharp {
                public bool VerifyFixed ()
                {
                        // A parameter is fixed if it's a value parameter (i.e., no modifier like out, ref, param).
-                       return mod == Parameter.Modifier.NONE;
+                       return par.ModFlags == Parameter.Modifier.NONE;
                }
 
                public bool IsAssigned (EmitContext ec, Location loc)
@@ -3636,7 +3640,7 @@ namespace Mono.CSharp {
                                return true;
 
                        Report.Error (269, loc,
-                                     "Use of unassigned out parameter `{0}'", name);
+                                     "Use of unassigned out parameter `{0}'", par.Name);
                        return false;
                }
 
@@ -3664,9 +3668,14 @@ namespace Mono.CSharp {
 
                protected void DoResolveBase (EmitContext ec)
                {
-                       type = pars.GetParameterInfo (ec, idx, out mod);
+                       if (!par.Resolve (ec)) {
+                               //TODO:
+                       }
+
+                       type = par.ParameterType;
+                       Parameter.Modifier mod = par.ModFlags;
                        is_ref = (mod & Parameter.Modifier.ISBYREF) != 0;
-                       is_out = (mod & Parameter.Modifier.OUT) != 0;
+                       is_out = (mod & Parameter.Modifier.OUT) == Parameter.Modifier.OUT;
                        eclass = ExprClass.Variable;
 
                        if (is_out)
@@ -3675,7 +3684,7 @@ namespace Mono.CSharp {
                        if (ec.CurrentAnonymousMethod != null){
                                if (is_ref){
                                        Report.Error (1628, Location, "Cannot use ref or out parameter `{0}' inside an anonymous method block",
-                                               name);
+                                               par.Name);
                                        return;
                                }
 
@@ -3780,11 +3789,8 @@ namespace Mono.CSharp {
                        ILGenerator ig = ec.ig;
                        int arg_idx = idx;
 
-                       if (ec.HaveCaptureInfo && ec.IsParameterCaptured (name)){
-                               if (leave_copy)
-                                       throw new InternalErrorException ();
-
-                               ec.EmitParameter (name);
+                       if (ec.HaveCaptureInfo && ec.IsParameterCaptured (name)){                               
+                               ec.EmitParameter (name, leave_copy, prepared, ref temp);
                                return;
                        }
 
@@ -3816,15 +3822,16 @@ namespace Mono.CSharp {
                
                public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
                {
+                       prepared = prepare_for_load;
                        if (ec.HaveCaptureInfo && ec.IsParameterCaptured (name)){
-                               ec.EmitAssignParameter (name, source, leave_copy, prepare_for_load);
+                               ec.EmitAssignParameter (name, source, leave_copy, prepare_for_load, ref temp);
                                return;
                        }
 
                        ILGenerator ig = ec.ig;
                        int arg_idx = idx;
                        
-                       prepared = prepare_for_load;
+                       
                        
                        if (!ec.MethodIsStatic)
                                arg_idx++;
@@ -3880,6 +3887,10 @@ namespace Mono.CSharp {
                        }
                }
 
+               public override string ToString ()
+               {
+                       return "ParameterReference[" + name + "]";
+               }
        }
        
        /// <summary>
@@ -3922,10 +3933,10 @@ namespace Mono.CSharp {
                        get {
                                switch (ArgType) {
                                        case AType.Out:
-                                               return Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF;
+                                               return Parameter.Modifier.OUT;
 
                                        case AType.Ref:
-                                               return Parameter.Modifier.REF | Parameter.Modifier.ISBYREF;
+                                               return Parameter.Modifier.REF;
 
                                        default:
                                                return Parameter.Modifier.NONE;
@@ -4268,7 +4279,7 @@ namespace Mono.CSharp {
                         return !candidate_params && best_params;
                }
 
-               static bool IsOverride (MethodBase cand_method, MethodBase base_method)
+               internal static bool IsOverride (MethodBase cand_method, MethodBase base_method)
                {
                        if (!IsAncestralType (base_method.DeclaringType, cand_method.DeclaringType))
                                return false;
@@ -4354,24 +4365,16 @@ namespace Mono.CSharp {
                        return union;
                }
 
-               public static bool IsParamsMethodApplicable (EmitContext ec, MethodGroupExpr me,
+               public static bool IsParamsMethodApplicable (EmitContext ec,
                                                             ArrayList arguments, int arg_count,
-                                                            ref MethodBase candidate)
+                                                            MethodBase candidate)
                {
                        return IsParamsMethodApplicable (
-                               ec, me, arguments, arg_count, false, ref candidate) ||
+                               ec, arguments, arg_count, candidate, false) ||
                                IsParamsMethodApplicable (
-                                       ec, me, arguments, arg_count, true, ref candidate);
-
+                                       ec, arguments, arg_count, candidate, true);
 
-               }
 
-               static bool IsParamsMethodApplicable (EmitContext ec, MethodGroupExpr me,
-                                                     ArrayList arguments, int arg_count,
-                                                     bool do_varargs, ref MethodBase candidate)
-               {
-                       return IsParamsMethodApplicable (
-                               ec, arguments, arg_count, candidate, do_varargs);
                }
 
                /// <summary>
@@ -4395,7 +4398,7 @@ namespace Mono.CSharp {
                                if (pd_count != arg_count)
                                        return false;
                        } else {
-                               if (pd.ParameterModifier (count) != Parameter.Modifier.PARAMS)
+                               if (!pd.HasParams)
                                        return false;
                        }
                        
@@ -4415,9 +4418,9 @@ namespace Mono.CSharp {
                                Argument a = (Argument) arguments [i];
 
                                Parameter.Modifier a_mod = a.Modifier & 
-                                       (unchecked (~(Parameter.Modifier.OUT | Parameter.Modifier.REF)));
+                                       (unchecked (~(Parameter.Modifier.OUTMASK | Parameter.Modifier.REFMASK)));
                                Parameter.Modifier p_mod = pd.ParameterModifier (i) &
-                                       (unchecked (~(Parameter.Modifier.OUT | Parameter.Modifier.REF)));
+                                       (unchecked (~(Parameter.Modifier.OUTMASK | Parameter.Modifier.REFMASK)));
 
                                if (a_mod == p_mod) {
 
@@ -4425,8 +4428,8 @@ namespace Mono.CSharp {
                                                if (!Convert.ImplicitConversionExists (ec,
                                                                                        a.Expr,
                                                                                        pd.ParameterType (i)))
-                                                       return false;
-                                                                               
+                               return false;
+
                                        if ((a_mod & Parameter.Modifier.ISBYREF) != 0) {
                                                Type pt = pd.ParameterType (i);
 
@@ -4461,19 +4464,12 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               public static bool IsApplicable (EmitContext ec, MethodGroupExpr me,
-                                                ArrayList arguments, int arg_count,
-                                                ref MethodBase candidate)
-               {
-                       return IsApplicable (ec, arguments, arg_count, candidate);
-               }
-
                /// <summary>
                ///   Determines if the candidate method is applicable (section 14.4.2.1)
                ///   to the given set of arguments
                /// </summary>
-               static bool IsApplicable (EmitContext ec, ArrayList arguments, int arg_count,
-                                         MethodBase candidate)
+               public static bool IsApplicable (EmitContext ec, ArrayList arguments, int arg_count,
+                       MethodBase candidate)
                {
                        ParameterData pd = TypeManager.GetParameterData (candidate);
 
@@ -4486,28 +4482,22 @@ namespace Mono.CSharp {
                                Argument a = (Argument) arguments [i];
 
                                Parameter.Modifier a_mod = a.Modifier &
-                                       unchecked (~(Parameter.Modifier.OUT | Parameter.Modifier.REF));
+                                       ~(Parameter.Modifier.OUTMASK | Parameter.Modifier.REFMASK);
+
                                Parameter.Modifier p_mod = pd.ParameterModifier (i) &
-                                       unchecked (~(Parameter.Modifier.OUT | Parameter.Modifier.REF));
+                                       ~(Parameter.Modifier.OUTMASK | Parameter.Modifier.REFMASK | Parameter.Modifier.PARAMS);
 
-                               if (a_mod == p_mod ||
-                                   (a_mod == Parameter.Modifier.NONE && p_mod == Parameter.Modifier.PARAMS)) {
-                                       if (a_mod == Parameter.Modifier.NONE) {
-                                                if (!Convert.ImplicitConversionExists (ec,
-                                                                                       a.Expr,
-                                                                                       pd.ParameterType (i)))
-                                                       return false;
-                                        }
-                                       
-                                       if ((a_mod & Parameter.Modifier.ISBYREF) != 0) {
-                                               Type pt = pd.ParameterType (i);
+                               if (a_mod == p_mod) {
+                                       Type pt = pd.ParameterType (i);
 
-                                               if (!pt.IsByRef)
-                                                       pt = TypeManager.GetReferenceType (pt);
-                                                
-                                               if (pt != a.Type)
+                                       if (a_mod == Parameter.Modifier.NONE) {
+                                               if (!Convert.ImplicitConversionExists (ec, a.Expr, pt))
                                                        return false;
+                                               continue;
                                        }
+                                       
+                                       if (pt != a.Type)
+                                               return false;
                                } else
                                        return false;
                        }
@@ -4515,7 +4505,7 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               static private bool IsAncestralType (Type first_type, Type second_type)
+               static internal bool IsAncestralType (Type first_type, Type second_type)
                {
                        return first_type != second_type &&
                                (second_type.IsSubclassOf (first_type) ||
@@ -4603,11 +4593,11 @@ namespace Mono.CSharp {
                                //   Is candidate applicable in normal form?
                                //
                                bool is_applicable = IsApplicable (
-                                       ec, me, Arguments, arg_count, ref methods [i]);
+                                       ec, Arguments, arg_count, methods [i]);
 
                                if (!is_applicable &&
                                        (IsParamsMethodApplicable (
-                                       ec, me, Arguments, arg_count, ref methods [i]))) {
+                                       ec, Arguments, arg_count, methods [i]))) {
                                        MethodBase candidate = methods [i];
                                        if (candidate_to_form == null)
                                                candidate_to_form = new PtrHashtable ();
@@ -4802,7 +4792,7 @@ namespace Mono.CSharp {
                public static void Error_WrongNumArguments (Location loc, String name, int arg_count)
                {
                        Report.Error (1501, loc, "No overload for method `{0}' takes `{1}' arguments",
-                               name, arg_count);
+                               name, arg_count.ToString ());
                }
 
                 static void Error_InvokeOnDelegate (Location loc)
@@ -4823,16 +4813,17 @@ namespace Mono.CSharp {
 
                        Parameter.Modifier mod = expected_par.ParameterModifier (idx);
 
+                       string index = (idx + 1).ToString ();
                        if (mod != Parameter.Modifier.ARGLIST && mod != a.Modifier) {
                                if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) == 0)
                                        Report.Error (1615, loc, "Argument `{0}' should not be passed with the `{1}' keyword",
-                                               idx + 1, Parameter.GetModifierSignature (a.Modifier));
+                                               index, Parameter.GetModifierSignature (a.Modifier));
                                else
                                        Report.Error (1620, loc, "Argument `{0}' must be passed with the `{1}' keyword",
-                                               idx + 1, Parameter.GetModifierSignature (mod));
+                                               index, Parameter.GetModifierSignature (mod));
                        } else {
                                Report.Error (1503, loc, "Argument {0}: Cannot convert from `{1}' to `{2}'",
-                                       idx + 1, Argument.FullDesc (a), expected_par.ParameterDesc (idx));
+                                       index, Argument.FullDesc (a), expected_par.ParameterDesc (idx));
                        }
                }
                
@@ -5585,6 +5576,29 @@ namespace Mono.CSharp {
                        return null;
                }
 
+               //
+               // Checks whether the type is an interface that has the
+               // [ComImport, CoClass] attributes and must be treated
+               // specially
+               //
+               public Expression CheckComImport (EmitContext ec)
+               {
+                       if (!type.IsInterface)
+                               return null;
+
+                       //
+                       // Turn the call into:
+                       // (the-interface-stated) (new class-referenced-in-coclassattribute ())
+                       //
+                       Type real_class = AttributeTester.GetCoClassAttribute (type);
+                       if (real_class == null)
+                               return null;
+
+                       New proxy = new New (new TypeExpression (real_class, loc), Arguments, loc);
+                       Cast cast = new Cast (new TypeExpression (type, loc), proxy, loc);
+                       return cast.Resolve (ec);
+               }
+               
                public override Expression DoResolve (EmitContext ec)
                {
                        //
@@ -5628,6 +5642,10 @@ namespace Mono.CSharp {
                        }
 
                        if (type.IsInterface || type.IsAbstract){
+                               RequestedType = CheckComImport (ec);
+                               if (RequestedType != null)
+                                       return RequestedType;
+                               
                                Report.SymbolRelatedToPreviousError (type);
                                Report.Error (144, loc, "Cannot create an instance of the abstract class or interface `{0}'", TypeManager.CSharpName (type));
                                return null;
@@ -6051,12 +6069,7 @@ namespace Mono.CSharp {
                        if (array_type_expr == null)
                                return false;
 
-                       type = array_type_expr.ResolveType (ec);
-                       
-                       if (!type.IsArray) {
-                               Error (622, "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
-                               return false;
-                       }
+                       type = array_type_expr.ResolveType (ec);                
                        underlying_type = TypeManager.GetElementType (type);
                        dimensions = type.GetArrayRank ();
 
@@ -7074,10 +7087,8 @@ namespace Mono.CSharp {
                {
                        FullNamedExpression new_expr = expr.ResolveAsTypeStep (ec, silent);
 
-                       if (new_expr == null) {
-                               Report.Error (234, "No such name or typespace {0}", expr);
+                       if (new_expr == null)
                                return null;
-                       }
 
                        if (new_expr is Namespace) {
                                Namespace ns = (Namespace) new_expr;
@@ -7346,11 +7357,10 @@ namespace Mono.CSharp {
                                                Report.Error (1708, loc, "Fixed size buffers can only be accessed through locals or fields");
                                                return null;
                                        }
-// TODO: not sure whether it is correct
-//                                     if (!ec.InFixedInitializer) {
-//                                             Error (1666, "You cannot use fixed sized buffers contained in unfixed expressions. Try using the fixed statement");
-//                                             return null;
-//                                     }
+                                       if (!ec.InFixedInitializer && ec.ContainerType.IsValueType) {
+                                               Error (1666, "You cannot use fixed size buffers contained in unfixed expressions. Try using the fixed statement");
+                                               return null;
+                                       }
                                        return MakePointerAccess (ec, ff.ElementType);
                                }
                        }
@@ -7403,7 +7413,7 @@ namespace Mono.CSharp {
                        Type t = ea.Expr.Type;
                        if (t.GetArrayRank () != ea.Arguments.Count){
                                Report.Error (22, ea.Location, "Wrong number of indexes `{0}' inside [], expected `{1}'",
-                                         ea.Arguments.Count, t.GetArrayRank ());
+                                         ea.Arguments.Count.ToString (), t.GetArrayRank ().ToString ());
                                return null;
                        }
 
@@ -8330,15 +8340,16 @@ namespace Mono.CSharp {
                }
 
                public override string Name {
-                       get {
-                               return left + dim;
-                       }
+                       get { return left + dim; }
                }
 
                public override string FullName {
-                       get {
-                               return type.FullName;
-                       }
+                       get { return type.FullName; }
+               }
+
+               public override string GetSignatureForError ()
+               {
+                       return left.GetSignatureForError () + dim;
                }
        }