This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / gmcs / expression.cs
index 04a75b5a54b401b56c75fddc01bce9ec0b0da66d..889d3eba69703e357e38bd15c0fb18a984ce0a12 100755 (executable)
@@ -2139,10 +2139,6 @@ namespace Mono.CSharp {
                                    (other == TypeManager.int32_type) ||
                                    (other == TypeManager.int64_type))
                                        Error_OperatorAmbiguous (loc, oper, l, r);
-                               else {
-                                       left = ForceConversion (ec, left, TypeManager.uint64_type);
-                                       right = ForceConversion (ec, right, TypeManager.uint64_type);
-                               }
                                type = TypeManager.uint64_type;
                        } else if (IsOfType (ec, l, r, TypeManager.int64_type, check_user_conv)){
                                //
@@ -4220,12 +4216,9 @@ namespace Mono.CSharp {
                /// </summary>
                static int BetterConversion (EmitContext ec, Argument a, Type p, Type q, Location loc)
                {
-                       Type argument_type = TypeManager.TypeToCoreType (a.Type);
+                       Type argument_type = a.Type;
                        Expression argument_expr = a.Expr;
 
-                       // p = TypeManager.TypeToCoreType (p);
-                       // q = TypeManager.TypeToCoreType (q);
-
                        if (argument_type == null)
                                throw new Exception ("Expression of type " + a.Expr +
                                                      " does not resolve its type");
@@ -4270,6 +4263,85 @@ namespace Mono.CSharp {
                        if (argument_type == q)
                                return 0;
 
+                       //
+                       // Now probe whether an implicit constant expression conversion
+                       // can be used.
+                       //
+                       // An implicit constant expression conversion permits the following
+                       // conversions:
+                       //
+                       //    * A constant-expression of type `int' can be converted to type
+                       //      sbyte, byute, short, ushort, uint, ulong provided the value of
+                       //      of the expression is withing the range of the destination type.
+                       //
+                       //    * A constant-expression of type long can be converted to type
+                       //      ulong, provided the value of the constant expression is not negative
+                       //
+                       // FIXME: Note that this assumes that constant folding has
+                       // taken place.  We dont do constant folding yet.
+                       //
+
+                       if (argument_expr is IntConstant){
+                               IntConstant ei = (IntConstant) argument_expr;
+                               int value = ei.Value;
+
+                               if (p == TypeManager.sbyte_type){
+                                       if (value >= SByte.MinValue && value <= SByte.MaxValue)
+                                               return 1;
+                               } else if (p == TypeManager.byte_type){
+                                       if (q == TypeManager.sbyte_type &&
+                                           value >= SByte.MinValue && value <= SByte.MaxValue)
+                                               return 0;
+                                       else if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
+                                               return 1;
+                               } else if (p == TypeManager.short_type){
+                                       if (value >= Int16.MinValue && value <= Int16.MaxValue)
+                                               return 1;
+                               } else if (p == TypeManager.ushort_type){
+                                       if (q == TypeManager.short_type &&
+                                           value >= Int16.MinValue && value <= Int16.MaxValue)
+                                               return 0;
+                                       else if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
+                                               return 1;
+                               } else if (p == TypeManager.int32_type){
+                                       if (value >= Int32.MinValue && value <= Int32.MaxValue)
+                                               return 1;
+                               } else if (p == TypeManager.uint32_type){
+                                       //
+                                       // we can optimize this case: a positive int32
+                                       // always fits on a uint32
+                                       //
+                                       if (value >= 0)
+                                               return 1;
+                               } else if (p == TypeManager.uint64_type){
+                                       //
+                                       // we can optimize this case: a positive int32
+                                       // always fits on a uint64
+                                       //
+
+                                        //
+                                        // This special case is needed because csc behaves like this.
+                                        // int -> uint is better than int -> ulong!
+                                        //
+                                        if (q == TypeManager.uint32_type)
+                                                return 0;
+                                        
+                                       if (q == TypeManager.int64_type)
+                                               return 0;
+                                       else if (value >= 0)
+                                               return 1;
+                               } else if (p == TypeManager.int64_type){
+                                       return 1;
+                               }
+                       } else if (argument_type == TypeManager.int64_type && argument_expr is LongConstant){
+                               LongConstant lc = (LongConstant) argument_expr;
+                               
+                               if (p == TypeManager.uint64_type){
+                                       if (lc.Value > 0)
+                                               return 1;
+                               }
+                       }
+
                        if (q == null) {
                                Expression tmp = Convert.ImplicitConversion (ec, argument_expr, p, loc);
                                
@@ -4316,7 +4388,7 @@ namespace Mono.CSharp {
                ///     0 if candidate ain't better
                ///     1 if candidate is better than the current best match
                /// </remarks>
-               static int BetterFunction (EmitContext ec, MethodGroupExpr me, ArrayList args,
+               static int BetterFunction (EmitContext ec, ArrayList args,
                                           MethodBase candidate, bool candidate_params,
                                            MethodBase best, bool best_params,
                                           Location loc)
@@ -4371,8 +4443,7 @@ namespace Mono.CSharp {
                                for (int j = 0; j < argument_count; ++j) {
 
                                        Argument a = (Argument) args [j];
-                                       Type t = TypeManager.TypeToCoreType (
-                                               candidate_pd.ParameterType (j));
+                                       Type t = candidate_pd.ParameterType (j);
 
                                        if (candidate_pd.ParameterModifier (j) == Parameter.Modifier.PARAMS)
                                                if (candidate_params)
@@ -4393,16 +4464,14 @@ namespace Mono.CSharp {
                        best_pd = GetParameterData (best);
 
                        int rating1 = 0, rating2 = 0;
-
+                       
                        for (int j = 0; j < argument_count; ++j) {
                                int x, y;
                                
                                Argument a = (Argument) args [j];
 
-                               Type ct = TypeManager.TypeToCoreType (
-                                       candidate_pd.ParameterType (j));
-                               Type bt = TypeManager.TypeToCoreType (
-                                       best_pd.ParameterType (j));
+                               Type ct = candidate_pd.ParameterType (j);
+                               Type bt = best_pd.ParameterType (j);
 
                                if (candidate_pd.ParameterModifier (j) == Parameter.Modifier.PARAMS)
                                        if (candidate_params)
@@ -4796,7 +4865,7 @@ namespace Mono.CSharp {
                                 if (method != null)
                                         method_params = (bool) candidate_to_form [method];
                                 
-                                int x = BetterFunction (ec, me, Arguments,
+                                int x = BetterFunction (ec, Arguments,
                                                         candidate, cand_params,
                                                        method, method_params,
                                                         loc);
@@ -4890,7 +4959,7 @@ namespace Mono.CSharp {
 //                                         continue;
                                 
                                 bool cand_params = (bool) candidate_to_form [candidate];
-                               int x = BetterFunction (ec, me, Arguments,
+                               int x = BetterFunction (ec, Arguments,
                                                         method, best_params,
                                                         candidate, cand_params,
                                                        loc);
@@ -7515,7 +7584,7 @@ namespace Mono.CSharp {
                        member_lookup = MemberLookup (
                                ec, expr_type, expr_type, Identifier, loc);
                        if ((member_lookup == null) && (args != null)) {
-                               string lookup_id = MemberName.MakeName (Identifier, args);
+                               string lookup_id = Identifier + "!" + args.Count;
                                member_lookup = MemberLookup (
                                        ec, expr_type, expr_type, lookup_id, loc);
                        }
@@ -7598,8 +7667,6 @@ namespace Mono.CSharp {
                                else
                                        fname = full_expr.Identifier;
 
-                               fname = MemberName.MakeName (fname, args);
-
                                if (full_expr.Expr is SimpleName) {
                                        string full_name = String.Concat (((SimpleName) full_expr.Expr).Name, ".", fname);
                                        Type fully_qualified = ec.DeclSpace.FindType (loc, full_name);
@@ -7633,7 +7700,10 @@ namespace Mono.CSharp {
 
                        Expression member_lookup;
                        string lookup_id;
-                       lookup_id = MemberName.MakeName (Identifier, args);
+                       if (args != null)
+                               lookup_id = Identifier + "!" + args.Count;
+                       else
+                               lookup_id = Identifier;
                        member_lookup = MemberLookupFinal (
                                ec, expr_type, expr_type, lookup_id, loc);
                        if (member_lookup == null)
@@ -7675,7 +7745,10 @@ namespace Mono.CSharp {
 
                public override string ToString ()
                {
-                       return expr + "." + MemberName.MakeName (Identifier, args);
+                       if (args != null)
+                               return expr + "." + Identifier + "!" + args.Count;
+                       else
+                               return expr + "." + Identifier;
                }
        }
 
@@ -8925,14 +8998,6 @@ namespace Mono.CSharp {
                                        return null;
                        }
 
-                       Constant c = count as Constant;
-                       // TODO: because we don't have property IsNegative
-                       if (c != null && c.ConvertToUInt () == null) {
-                                // "Cannot use a negative size with stackalloc"
-                               Report.Error_T (247, loc);
-                               return null;
-                       }
-
                        if (ec.CurrentBranching.InCatch () ||
                            ec.CurrentBranching.InFinally (true)) {
                                Error (255,