Author: jambunathan
Date: 2005-03-27 07:05:37 -0500 (Sun, 27 Mar 2005)
New Revision: 42283
Modified:
trunk/mcs/bmcs/ChangeLog
trunk/mcs/bmcs/constant.cs
trunk/mcs/bmcs/convert.cs
trunk/mcs/bmcs/ecore.cs
trunk/mcs/bmcs/literal.cs
Log:
* constant.cs: Added conversions between various constants
* literal.cs: Added conversions from Nothing to various primitive constants
* convert.cs (NothingToPrimitiveConstants, WideningConstantconversions): Added
(TryWideningIntConversion): Removed C# specific constant
conversions. This functionality is now taken care of by
WideningConstantConversions.
(WideningReferenceConversion): Added support for conversions from Nothing to
ValueTypes
(WideningNumericConversion): Call WideningConstantConversions
* ecore.cs (NullCast.Emit): Conversion from Nothing to ValueType
Modified: trunk/mcs/bmcs/ChangeLog
===================================================================
--- trunk/mcs/bmcs/ChangeLog 2005-03-27 01:15:14 UTC (rev 42282)
+++ trunk/mcs/bmcs/ChangeLog 2005-03-27 12:05:37 UTC (rev 42283)
@@ -1,5 +1,22 @@
2005-03-26 Jambunathan K <[EMAIL PROTECTED]>
+ * constant.cs: Added conversions between various constants
+ * literal.cs: Added conversions from Nothing to various primitive
constants
+
+ * convert.cs (NothingToPrimitiveConstants,
WideningConstantconversions): Added
+
+ (TryWideningIntConversion): Removed C# specific constant
+ conversions. This functionality is now taken care of by
+ WideningConstantConversions.
+
+ (WideningReferenceConversion): Added support for conversions from
Nothing to ValueTypes
+ (WideningNumericConversion): Call WideningConstantConversions
+
+ * ecore.cs (NullCast.Emit): Conversion from Nothing to ValueType
+
+
+2005-03-26 Jambunathan K <[EMAIL PROTECTED]>
+
* ecore.cs (ImplicitInvocation): Set eclass and type to that of the
child
* convert.cs (ImplicitVBConversion, ImplicitVBConversionRequired,
Modified: trunk/mcs/bmcs/constant.cs
===================================================================
--- trunk/mcs/bmcs/constant.cs 2005-03-27 01:15:14 UTC (rev 42282)
+++ trunk/mcs/bmcs/constant.cs 2005-03-27 12:05:37 UTC (rev 42283)
@@ -127,6 +127,66 @@
return c;
}
+ public ShortConstant ToShort (Location loc)
+ {
+ ShortConstant c = ConvertToShort ();
+
+ if (c == null)
+ Convert.Error_CannotConvertType (loc, Type,
TypeManager.short_type);
+
+ return c;
+ }
+
+ public ByteConstant ToByte (Location loc)
+ {
+ ByteConstant c = ConvertToByte ();
+
+ if (c == null)
+ Convert.Error_CannotConvertType (loc, Type,
TypeManager.byte_type);
+
+ return c;
+ }
+
+ public BoolConstant ToBoolean (Location loc)
+ {
+ BoolConstant c = ConvertToBoolean ();
+
+ if (c == null)
+ Convert.Error_CannotConvertType (loc, Type,
TypeManager.bool_type);
+
+ return c;
+ }
+
+ public CharConstant ToChar (Location loc)
+ {
+ CharConstant c = ConvertToChar ();
+
+ if (c == null)
+ Convert.Error_CannotConvertType (loc, Type,
TypeManager.char_type);
+
+ return c;
+ }
+
+ public virtual CharConstant ConvertToChar ()
+ {
+ return null;
+ }
+
+ public virtual BoolConstant ConvertToBoolean ()
+ {
+ return null;
+ }
+
+ public virtual ByteConstant ConvertToByte ()
+ {
+ return null;
+ }
+
+ public virtual ShortConstant ConvertToShort ()
+ {
+ return null;
+ }
+
public virtual DecimalConstant ConvertToDecimal ()
{
return null;
@@ -204,7 +264,47 @@
else
ec.ig.Emit (OpCodes.Ldc_I4_0);
}
+
+ public override DecimalConstant ConvertToDecimal ()
+ {
+ return Value ? new DecimalConstant (0) : new
DecimalConstant (-1);
+ }
+ public override DoubleConstant ConvertToDouble ()
+ {
+ return Value ? new DoubleConstant (0) : new
DoubleConstant (-1);
+ }
+
+ public override FloatConstant ConvertToFloat ()
+ {
+ return Value ? new FloatConstant (0) : new
FloatConstant (-1);
+ }
+
+ public override LongConstant ConvertToLong ()
+ {
+ return Value ? new LongConstant (0) : new LongConstant
(-1);
+ }
+
+ public override IntConstant ConvertToInt ()
+ {
+ return Value ? new IntConstant (0) : new IntConstant
(-1);
+ }
+
+ public override ShortConstant ConvertToShort ()
+ {
+ return Value ? new ShortConstant (0) : new
ShortConstant (-1);
+ }
+
+ public override ByteConstant ConvertToByte ()
+ {
+ return Value ? new ByteConstant (0) : new ByteConstant
(255);
+ }
+
+ public override BoolConstant ConvertToBoolean ()
+ {
+ return this;
+ }
+
public override bool IsNegative {
get {
return false;
@@ -251,25 +351,35 @@
return new FloatConstant (Value);
}
- public override ULongConstant ConvertToULong ()
+ public override LongConstant ConvertToLong ()
{
- return new ULongConstant (Value);
+ return new LongConstant (Value);
}
- public override LongConstant ConvertToLong ()
+ public override ShortConstant ConvertToShort ()
{
- return new LongConstant (Value);
+ return new ShortConstant (Value);
}
- public override UIntConstant ConvertToUInt ()
+ public override ByteConstant ConvertToByte ()
{
- return new UIntConstant (Value);
+ return this;
}
+ public override DecimalConstant ConvertToDecimal ()
+ {
+ return new DecimalConstant (Value);
+ }
+
public override IntConstant ConvertToInt ()
{
return new IntConstant (Value);
}
+
+ public override BoolConstant ConvertToBoolean ()
+ {
+ return new BoolConstant (Value != 0);
+ }
public override bool IsNegative {
get {
@@ -481,25 +591,43 @@
return new FloatConstant (Value);
}
- public override ULongConstant ConvertToULong ()
- {
- return null;
- }
-
public override LongConstant ConvertToLong ()
{
return new LongConstant (Value);
}
- public override UIntConstant ConvertToUInt ()
+ public override ShortConstant ConvertToShort ()
{
- return null;
+ return this;
}
public override IntConstant ConvertToInt ()
{
return new IntConstant (Value);
}
+
+ public override DecimalConstant ConvertToDecimal ()
+ {
+ return new DecimalConstant (Value);
+ }
+
+ public override ByteConstant ConvertToByte ()
+ {
+ byte val;
+
+ try {
+ val = System.Convert.ToByte (Value);
+ } catch (Exception ex) {
+ return null;
+ }
+
+ return new ByteConstant (val);
+ }
+
+ public override BoolConstant ConvertToBoolean ()
+ {
+ return new BoolConstant (Value != 0);
+ }
public override bool IsZeroInteger {
get { return Value == 0; }
@@ -670,30 +798,45 @@
return new FloatConstant (Value);
}
- public override ULongConstant ConvertToULong ()
+ public override LongConstant ConvertToLong ()
{
- if (Value < 0)
- return null;
+ return new LongConstant (Value);
+ }
- return new ULongConstant ((ulong) Value);
+ public override IntConstant ConvertToInt ()
+ {
+ return this;
}
- public override LongConstant ConvertToLong ()
+ public override ShortConstant ConvertToShort ()
{
- return new LongConstant (Value);
+ short val;
+
+ try {
+ val = System.Convert.ToInt16 (Value);
+ } catch (Exception ex) {
+ return null;
+ }
+
+ return new ShortConstant (val);
}
- public override UIntConstant ConvertToUInt ()
+ public override ByteConstant ConvertToByte ()
{
- if (Value < 0)
+ byte val;
+
+ try {
+ val = System.Convert.ToByte (Value);
+ } catch (Exception ex) {
return null;
+ }
- return new UIntConstant ((uint) Value);
+ return new ByteConstant (val);
}
- public override IntConstant ConvertToInt ()
+ public override BoolConstant ConvertToBoolean ()
{
- return this;
+ return new BoolConstant (Value != 0);
}
public override bool IsNegative {
@@ -820,12 +963,9 @@
return new FloatConstant (Value);
}
- public override ULongConstant ConvertToULong ()
+ public override DecimalConstant ConvertToDecimal()
{
- if (Value < 0)
- return null;
-
- return new ULongConstant ((ulong) Value);
+ return new DecimalConstant (Value);
}
public override LongConstant ConvertToLong ()
@@ -833,15 +973,49 @@
return this;
}
- public override UIntConstant ConvertToUInt ()
+ public override IntConstant ConvertToInt ()
{
- return null;
+ int val;
+
+ try {
+ val = System.Convert.ToInt32 (Value);
+ } catch (Exception ex) {
+ return null;
+ }
+
+ return new IntConstant (val);
}
- public override IntConstant ConvertToInt ()
+ public override ShortConstant ConvertToShort ()
{
- return null;
+ short val;
+
+ try {
+ val = System.Convert.ToInt16 (Value);
+ } catch (Exception ex) {
+ return null;
+ }
+
+ return new ShortConstant (val);
}
+
+ public override ByteConstant ConvertToByte ()
+ {
+ byte val;
+
+ try {
+ val = System.Convert.ToByte (Value);
+ } catch (Exception ex) {
+ return null;
+ }
+
+ return new ByteConstant (val);
+ }
+
+ public override BoolConstant ConvertToBoolean ()
+ {
+ return new BoolConstant (Value != 0);
+ }
public override bool IsNegative {
get {
@@ -957,21 +1131,69 @@
return this;
}
- public override LongConstant ConvertToLong ()
+ public override DecimalConstant ConvertToDecimal ()
{
- return null;
+ return new DecimalConstant (new System.Decimal (Value));
}
- public override UIntConstant ConvertToUInt ()
+ public override LongConstant ConvertToLong ()
{
- return null;
+ long val;
+
+ try {
+ val = System.Convert.ToInt64 (System.Math.Round
(Value));
+ } catch (Exception ex) {
+ return null;
+ }
+
+ return new LongConstant (val);
+
}
public override IntConstant ConvertToInt ()
{
- return null;
+ int val;
+
+ try {
+ val = System.Convert.ToInt32 (System.Math.Round
(Value));
+ } catch (Exception ex) {
+ return null;
+ }
+
+ return new IntConstant (val);
}
+ public override ShortConstant ConvertToShort ()
+ {
+ short val;
+
+ try {
+ val = System.Convert.ToInt16 (System.Math.Round
(Value));
+ } catch (Exception ex) {
+ return null;
+ }
+
+ return new ShortConstant (val);
+ }
+
+ public override ByteConstant ConvertToByte ()
+ {
+ byte val;
+
+ try {
+ val = System.Convert.ToByte (System.Math.Round
(Value));
+ } catch (Exception ex) {
+ return null;
+ }
+
+ return new ByteConstant (val);
+ }
+
+ public override BoolConstant ConvertToBoolean ()
+ {
+ return new BoolConstant (Value != 0);
+ }
+
public override bool IsNegative {
get {
return Value < 0;
@@ -1011,29 +1233,80 @@
public override FloatConstant ConvertToFloat ()
{
- return null;
+ float val;
+
+ try {
+ val = System.Convert.ToSingle (Value);
+ } catch (Exception ex) {
+ return null;
+ }
+
+ return new FloatConstant (val);
}
- public override ULongConstant ConvertToULong ()
+ public override DecimalConstant ConvertToDecimal ()
{
- return null;
+ return new DecimalConstant (new System.Decimal (Value));
}
public override LongConstant ConvertToLong ()
{
- return null;
+ long val;
+
+ try {
+ val = System.Convert.ToInt64 (System.Math.Round
(Value));
+ } catch (Exception ex) {
+ return null;
+ }
+
+ return new LongConstant (val);
+
}
- public override UIntConstant ConvertToUInt ()
+ public override IntConstant ConvertToInt ()
{
- return null;
+ int val;
+
+ try {
+ val = System.Convert.ToInt32 (System.Math.Round
(Value));
+ } catch (Exception ex) {
+ return null;
+ }
+
+ return new IntConstant (val);
}
- public override IntConstant ConvertToInt ()
+ public override ShortConstant ConvertToShort ()
{
- return null;
+ short val;
+
+ try {
+ val = System.Convert.ToInt16 (System.Math.Round
(Value));
+ } catch (Exception ex) {
+ return null;
+ }
+
+ return new ShortConstant (val);
}
+ public override ByteConstant ConvertToByte ()
+ {
+ byte val;
+
+ try {
+ val = System.Convert.ToByte (System.Math.Round
(Value));
+ } catch (Exception ex) {
+ return null;
+ }
+
+ return new ByteConstant (val);
+ }
+
+ public override BoolConstant ConvertToBoolean ()
+ {
+ return new BoolConstant (Value != 0);
+ }
+
public override bool IsNegative {
get {
return Value < 0;
@@ -1094,6 +1367,94 @@
ig.Emit (OpCodes.Newobj,
TypeManager.void_decimal_ctor_five_args);
}
+ public override DoubleConstant ConvertToDouble ()
+ {
+ double val;
+
+ try {
+ val = System.Convert.ToDouble (Value);
+ } catch (Exception ex) {
+ return null;
+ }
+
+ return new DoubleConstant (val);
+ }
+
+ public override FloatConstant ConvertToFloat ()
+ {
+ float val;
+
+ try {
+ val = System.Convert.ToSingle (Value);
+ } catch (Exception ex) {
+ return null;
+ }
+
+ return new FloatConstant (val);
+ }
+
+ public override DecimalConstant ConvertToDecimal ()
+ {
+ return this;
+ }
+
+ public override LongConstant ConvertToLong ()
+ {
+ long val;
+
+ try {
+ val = System.Convert.ToInt64 (Value);
+ } catch (Exception ex) {
+ return null;
+ }
+
+ return new LongConstant (val);
+ }
+
+ public override IntConstant ConvertToInt ()
+ {
+ int val;
+
+ try {
+ val = System.Convert.ToInt32 (Value);
+ } catch (Exception ex) {
+ return null;
+ }
+
+ return new IntConstant (val);
+ }
+
+ public override ShortConstant ConvertToShort ()
+ {
+ short val;
+
+ try {
+ val = System.Convert.ToInt16 (Value);
+ } catch (Exception ex) {
+ return null;
+ }
+
+ return new ShortConstant (val);
+ }
+
+ public override ByteConstant ConvertToByte ()
+ {
+ byte val;
+
+ try {
+ val = System.Convert.ToByte (Value);
+ } catch (Exception ex) {
+ return null;
+ }
+
+ return new ByteConstant (val);
+ }
+
+ public override BoolConstant ConvertToBoolean ()
+ {
+ return new BoolConstant (Value != 0);
+ }
+
public override bool IsNegative {
get {
return Value < 0;
Modified: trunk/mcs/bmcs/convert.cs
===================================================================
--- trunk/mcs/bmcs/convert.cs 2005-03-27 01:15:14 UTC (rev 42282)
+++ trunk/mcs/bmcs/convert.cs 2005-03-27 12:05:37 UTC (rev 42283)
@@ -205,6 +205,14 @@
if (!target_type.IsValueType)
return new NullCast (expr, target_type);
+
+ // VB.NET specific: Convert Nothing to value
types
+
+ Expression e = NothingToPrimitiveConstants
(expr, target_type);
+ if (e != null)
+ return e;
+
+ return new NullCast (expr, target_type);
}
// from any class-type S to any interface-type T.
@@ -398,22 +406,17 @@
// Attempt to do the implicit constant expression
conversions
if (expr is Constant){
+ Expression e;
+
+ e = WideningConstantConversions (target_type,
(Constant) expr);
+ if (e != null)
+ return e;
+
if (expr is IntConstant){
- Expression e;
-
e = TryWideningIntConversion
(target_type, (IntConstant) expr);
if (e != null)
return e;
- } else if (expr is LongConstant && target_type
== TypeManager.uint64_type){
- //
- // Try the implicit constant expression
conversion
- // from long to ulong, instead of a
nice routine,
- // we just inline it
- //
- long v = ((LongConstant) expr).Value;
- if (v >= 0)
- return new ULongConstant
((ulong) v);
}
}
@@ -1409,34 +1412,6 @@
{
int value = ic.Value;
- if (target_type == TypeManager.sbyte_type){
- if (value >= SByte.MinValue && value <=
SByte.MaxValue)
- return new SByteConstant ((sbyte)
value);
- } else if (target_type == TypeManager.byte_type){
- if (value >= Byte.MinValue && value <=
Byte.MaxValue)
- return new ByteConstant ((byte) value);
- } else if (target_type == TypeManager.short_type){
- if (value >= Int16.MinValue && value <=
Int16.MaxValue)
- return new ShortConstant ((short)
value);
- } else if (target_type == TypeManager.ushort_type){
- if (value >= UInt16.MinValue && value <=
UInt16.MaxValue)
- return new UShortConstant ((ushort)
value);
- } else if (target_type == TypeManager.uint32_type){
- if (value >= 0)
- return new UIntConstant ((uint) value);
- } else if (target_type == TypeManager.uint64_type){
- //
- // we can optimize this case: a positive int32
- // always fits on a uint64. But we need an
opcode
- // to do it.
- //
- if (value >= 0)
- return new ULongConstant ((ulong)
value);
- } else if (target_type == TypeManager.double_type)
- return new DoubleConstant ((double) value);
- else if (target_type == TypeManager.float_type)
- return new FloatConstant ((float) value);
-
if (value == 0 && ic is IntLiteral &&
TypeManager.IsEnumType (target_type)){
Type underlying = TypeManager.EnumToUnderlying
(target_type);
Constant e = (Constant) ic;
@@ -1463,6 +1438,84 @@
return null;
}
+ /// <summary>
+ /// Attempts to perform an implicit constant conversion of
the IntConstant
+ /// into a different data type using casts (See Implicit
Constant
+ /// Expression Conversions)
+ /// </summary>
+ static public Expression WideningConstantConversions (Type
target_type, Constant const_expr)
+ {
+ Constant ret_expr;
+
+ Type const_expr_type = const_expr.Type;
+ Location loc = const_expr.Location;
+
+ if (target_type == TypeManager.short_type){
+ if (const_expr_type == TypeManager.byte_type)
+ return const_expr.ToShort (loc);
+ }
+
+ if (target_type == TypeManager.int32_type){
+ if (const_expr_type == TypeManager.byte_type ||
+ const_expr_type ==
TypeManager.short_type)
+ return const_expr.ToInt (loc);
+ }
+
+ if (target_type == TypeManager.int64_type){
+ if (const_expr_type == TypeManager.byte_type ||
+ const_expr_type ==
TypeManager.short_type ||
+ const_expr_type ==
TypeManager.int32_type)
+ return const_expr.ToLong (loc);
+ }
+
+ if (target_type == TypeManager.double_type) {
+ if (const_expr_type == TypeManager.float_type)
+ return const_expr.ToDouble (loc);
+ }
+
+ return null;
+ }
+
+ static public Constant NothingToPrimitiveConstants (Expression
expr, Type target_type)
+ {
+ NullLiteral null_literal = (NullLiteral) expr;
+ Location loc = null_literal.Location;
+ Type real_target_type = target_type ;
+ Constant retval = null;
+
+ if (null_literal == null)
+ throw new Exception ("FIXME: I was expecting
that I would always get only NullLiterals");
+
+ if (target_type.IsSubclassOf(TypeManager.enum_type))
+ real_target_type = TypeManager.EnumToUnderlying
(target_type);
+
+ if (real_target_type == TypeManager.bool_type)
+ retval = null_literal.ToBoolean (loc);
+ else if (real_target_type == TypeManager.byte_type)
+ retval = null_literal.ToByte (loc);
+ else if (real_target_type == TypeManager.short_type)
+ retval = null_literal.ToShort (loc);
+ else if (real_target_type == TypeManager.int32_type)
+ retval = null_literal.ToInt (loc);
+ else if (real_target_type == TypeManager.int64_type)
+ null_literal.ToLong (loc);
+ else if (real_target_type == TypeManager.decimal_type)
+ retval = null_literal.ToDecimal (loc);
+ else if (real_target_type == TypeManager.float_type)
+ retval = null_literal.ToLong (loc);
+ else if (real_target_type == TypeManager.double_type)
+ retval = null_literal.ToDouble (loc);
+ else if (real_target_type == TypeManager.char_type)
+ retval = null_literal.ToChar (loc);
+ else if (real_target_type == TypeManager.string_type)
+ retval = new StringConstant (null);
+
+ if (real_target_type != target_type && retval != null)
+ retval = new EnumConstant (retval, target_type);
+
+ return retval;
+ }
+
static public void Error_CannotWideningConversion (Location
loc, Type source, Type target)
{
if (source.Name == target.Name){
Modified: trunk/mcs/bmcs/ecore.cs
===================================================================
--- trunk/mcs/bmcs/ecore.cs 2005-03-27 01:15:14 UTC (rev 42282)
+++ trunk/mcs/bmcs/ecore.cs 2005-03-27 12:05:37 UTC (rev 42283)
@@ -1609,7 +1609,19 @@
public override void Emit (EmitContext ec)
{
- child.Emit (ec);
+ if (!type.IsValueType)
+ child.Emit (ec);
+ else {
+ // VB.NET allows Nothing to be
converted to any value type
+
+ ILGenerator ig = ec.ig;
+
+ ig.Emit (OpCodes.Ldtoken, type);
+ ig.Emit (OpCodes.Call,
TypeManager.system_type_get_type_from_handle);
+ ig.Emit (OpCodes.Call,
TypeManager.activator_create_instance);
+ ig.Emit (OpCodes.Unbox, type);
+ ig.Emit (OpCodes.Ldobj, type);
+ }
}
public override bool IsNegative {
Modified: trunk/mcs/bmcs/literal.cs
===================================================================
--- trunk/mcs/bmcs/literal.cs 2005-03-27 01:15:14 UTC (rev 42282)
+++ trunk/mcs/bmcs/literal.cs 2005-03-27 12:05:37 UTC (rev 42283)
@@ -88,7 +88,52 @@
{
ec.ig.Emit (OpCodes.Ldnull);
}
+
+ public override CharConstant ConvertToChar ()
+ {
+ return new CharConstant ((char) 0);
+ }
+ public override BoolConstant ConvertToBoolean ()
+ {
+ return new BoolConstant (false);
+ }
+
+ public override ByteConstant ConvertToByte ()
+ {
+ return new ByteConstant ((byte) 0);
+ }
+
+ public override ShortConstant ConvertToShort ()
+ {
+ return new ShortConstant ((short) 0);
+ }
+
+ public override DecimalConstant ConvertToDecimal ()
+ {
+ return new DecimalConstant ((decimal) 0);
+ }
+
+ public override DoubleConstant ConvertToDouble ()
+ {
+ return new DoubleConstant ((double) 0);
+ }
+
+ public override FloatConstant ConvertToFloat ()
+ {
+ return new FloatConstant ((float) 0);
+ }
+
+ public override LongConstant ConvertToLong ()
+ {
+ return new LongConstant ((long) 0);
+ }
+
+ public override IntConstant ConvertToInt ()
+ {
+ return new IntConstant (0);
+ }
+
public override bool IsNegative {
get {
return false;
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches