Merge pull request #642 from Ventero/CleanCopyLocal
[mono.git] / mcs / mcs / constant.cs
index 3cf689ddd81fc60b9fbafbda936eb4733fb4fc43..397d72c9a4ae7653be65733999a337aaf1567695 100644 (file)
@@ -3,11 +3,11 @@
 //
 // Author:
 //   Miguel de Icaza (miguel@ximian.com)
-//   Marek Safar (marek.safar@seznam.cz)
+//   Marek Safar (marek.safar@gmail.com)
 //
 // Copyright 2001-2003 Ximian, Inc.
 // Copyright 2003-2008 Novell, Inc.
-// Copyright 2011 Xamarin Inc
+// Copyright 2011-2013 Xamarin Inc
 //
 
 using System;
@@ -64,7 +64,7 @@ namespace Mono.CSharp {
                                BuiltinTypeSpec.IsPrimitiveTypeOrDecimal (target) &&
                                BuiltinTypeSpec.IsPrimitiveTypeOrDecimal (type)) {
                                ec.Report.Error (31, loc, "Constant value `{0}' cannot be converted to a `{1}'",
-                                       GetValueAsLiteral (), TypeManager.CSharpName (target));
+                                       GetValueAsLiteral (), target.GetSignatureForError ());
                        } else {
                                base.Error_ValueCannotBeConverted (ec, target, expl);
                        }
@@ -89,7 +89,7 @@ namespace Mono.CSharp {
                        if (this.type == type)
                                return this;
 
-                       if (Convert.ImplicitNumericConversion (this, type) == null
+                       if (!Convert.ImplicitNumericConversionExists (this.type, type)
                                return null;
 
                        bool fail;                      
@@ -100,20 +100,15 @@ namespace Mono.CSharp {
                                // reached, by calling Convert.ImplicitStandardConversionExists
                                //
                                throw new InternalErrorException ("Missing constant conversion between `{0}' and `{1}'",
-                                 TypeManager.CSharpName (Type), TypeManager.CSharpName (type));
+                                Type.GetSignatureForError (), type.GetSignatureForError ());
                        }
 
-                       return CreateConstant (type, constant_value, loc);
+                       return CreateConstantFromValue (type, constant_value, loc);
                }
 
                //
                //  Returns a constant instance based on Type
                //
-               public static Constant CreateConstant (TypeSpec t, object v, Location loc)
-               {
-                       return CreateConstantFromValue (t, v, loc);
-               }
-
                public static Constant CreateConstantFromValue (TypeSpec t, object v, Location loc)
                {
                        switch (t.BuiltinType) {
@@ -251,10 +246,12 @@ namespace Mono.CSharp {
                        return this;
                }
 
-               /// <summary>
-               ///   Attempts to do a compile-time folding of a constant cast.
-               /// </summary>
-               public Constant TryReduce (ResolveContext ec, TypeSpec target_type)
+               //
+               // Attempts to do a compile-time folding of a constant cast and handles
+               // error reporting for constant overlows only, on normal conversion
+               // errors returns null
+               // 
+               public Constant Reduce (ResolveContext ec, TypeSpec target_type)
                {
                        try {
                                return TryReduceConstant (ec, target_type);
@@ -271,6 +268,15 @@ namespace Mono.CSharp {
                        }
                }
 
+               public Constant TryReduce (ResolveContext rc, TypeSpec targetType)
+               {
+                       try {
+                               return TryReduceConstant (rc, targetType);
+                       } catch (OverflowException) {
+                               return null;
+                       }
+               }
+
                Constant TryReduceConstant (ResolveContext ec, TypeSpec target_type)
                {
                        if (Type == target_type) {
@@ -390,7 +396,7 @@ namespace Mono.CSharp {
                        catch
                        {
                                ec.Report.Error (31, loc, "Constant value `{0}' cannot be converted to a `{1}'",
-                                       GetValue ().ToString (), TypeManager.CSharpName (target));
+                                       GetValue ().ToString (), target.GetSignatureForError ());
                        }
                }