+ public override void Emit (EmitContext ec)
+ {
+ ec.EmitInt (Value);
+ }
+
+ public override string AsString ()
+ {
+ return Value.ToString ();
+ }
+
+ public override object GetValue ()
+ {
+ return Value;
+ }
+
+ public override Constant Increment ()
+ {
+ return new ShortConstant (checked((short)(Value + 1)), loc);
+ }
+
+ public override bool IsDefaultValue {
+ get {
+ return Value == 0;
+ }
+ }
+
+ public override bool IsZeroInteger {
+ get { return Value == 0; }
+ }
+
+ public override bool IsNegative {
+ get {
+ return Value < 0;
+ }
+ }
+
+ public override bool IsOneInteger {
+ get {
+ return Value == 1;
+ }
+ }
+
+ public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
+ {
+ if (target_type == TypeManager.byte_type) {
+ if (in_checked_context){
+ if (Value < Byte.MinValue || Value > Byte.MaxValue)
+ throw new OverflowException ();
+ }
+ return new ByteConstant ((byte) Value, Location);
+ }
+ if (target_type == TypeManager.sbyte_type) {
+ if (in_checked_context){
+ if (Value < SByte.MinValue || Value > SByte.MaxValue)
+ throw new OverflowException ();
+ }
+ return new SByteConstant ((sbyte) Value, Location);
+ }
+ if (target_type == TypeManager.ushort_type) {
+ if (in_checked_context && Value < 0)
+ throw new OverflowException ();
+
+ return new UShortConstant ((ushort) Value, Location);
+ }
+ if (target_type == TypeManager.int32_type)
+ return new IntConstant ((int) Value, Location);
+ if (target_type == TypeManager.uint32_type) {
+ if (in_checked_context && Value < 0)
+ throw new OverflowException ();
+ return new UIntConstant ((uint) Value, Location);
+ }
+ if (target_type == TypeManager.int64_type)
+ return new LongConstant ((long) Value, Location);
+ if (target_type == TypeManager.uint64_type) {
+ if (in_checked_context && Value < 0)
+ throw new OverflowException ();
+ return new ULongConstant ((ulong) Value, Location);
+ }
+ if (target_type == TypeManager.float_type)
+ return new FloatConstant ((float) Value, Location);
+ if (target_type == TypeManager.double_type)
+ return new DoubleConstant ((double) Value, Location);
+ if (target_type == TypeManager.char_type) {
+ if (in_checked_context){
+ if (Value < Char.MinValue)
+ throw new OverflowException ();
+ }
+ return new CharConstant ((char) Value, Location);
+ }
+ if (target_type == TypeManager.decimal_type)
+ return new DecimalConstant ((decimal) Value, Location);
+
+ return null;
+ }
+
+ }
+
+ public class UShortConstant : IntegralConstant {
+ public readonly ushort Value;
+
+ public UShortConstant (ushort v, Location loc):
+ base (loc)
+ {
+ Value = v;
+ }
+
+ protected override Expression DoResolve (ResolveContext rc)
+ {
+ type = TypeManager.ushort_type;
+ eclass = ExprClass.Value;
+ return this;
+ }
+
+ public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+ {
+ enc.Stream.Write (Value);
+ }
+
+ public override void Emit (EmitContext ec)
+ {
+ ec.EmitInt (Value);
+ }
+
+ public override string AsString ()
+ {
+ return Value.ToString ();
+ }
+
+ public override object GetValue ()
+ {
+ return Value;
+ }
+
+ public override Constant Increment ()
+ {
+ return new UShortConstant (checked((ushort)(Value + 1)), loc);
+ }
+
+ public override bool IsDefaultValue {
+ get {
+ return Value == 0;
+ }
+ }
+
+ public override bool IsNegative {
+ get {
+ return false;
+ }
+ }
+
+ public override bool IsOneInteger {
+ get {
+ return Value == 1;
+ }
+ }
+
+ public override bool IsZeroInteger {
+ get { return Value == 0; }
+ }
+
+ public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
+ {
+ if (target_type == TypeManager.byte_type) {
+ if (in_checked_context){
+ if (Value > Byte.MaxValue)
+ throw new OverflowException ();
+ }
+ return new ByteConstant ((byte) Value, Location);
+ }
+ if (target_type == TypeManager.sbyte_type) {
+ if (in_checked_context){
+ if (Value > SByte.MaxValue)
+ throw new OverflowException ();
+ }
+ return new SByteConstant ((sbyte) Value, Location);
+ }
+ if (target_type == TypeManager.short_type) {
+ if (in_checked_context){
+ if (Value > Int16.MaxValue)
+ throw new OverflowException ();
+ }
+ return new ShortConstant ((short) Value, Location);
+ }
+ if (target_type == TypeManager.int32_type)
+ return new IntConstant ((int) Value, Location);
+ if (target_type == TypeManager.uint32_type)
+ return new UIntConstant ((uint) Value, Location);
+ if (target_type == TypeManager.int64_type)
+ return new LongConstant ((long) Value, Location);
+ if (target_type == TypeManager.uint64_type)
+ return new ULongConstant ((ulong) Value, Location);
+ if (target_type == TypeManager.float_type)
+ return new FloatConstant ((float) Value, Location);
+ if (target_type == TypeManager.double_type)
+ return new DoubleConstant ((double) Value, Location);
+ if (target_type == TypeManager.char_type) {
+ if (in_checked_context){
+ if (Value > Char.MaxValue)
+ throw new OverflowException ();
+ }
+ return new CharConstant ((char) Value, Location);
+ }
+ if (target_type == TypeManager.decimal_type)
+ return new DecimalConstant ((decimal) Value, Location);
+
+ return null;
+ }
+ }
+
+ public class IntConstant : IntegralConstant {
+ public readonly int Value;
+
+ public IntConstant (int v, Location loc):
+ base (loc)
+ {
+ Value = v;
+ }
+
+ protected override Expression DoResolve (ResolveContext rc)
+ {
+ type = TypeManager.int32_type;
+ eclass = ExprClass.Value;
+ return this;
+ }
+
+ public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+ {
+ enc.Stream.Write (Value);
+ }
+
+ public override void Emit (EmitContext ec)
+ {
+ ec.EmitInt (Value);
+ }
+
+ public override string AsString ()
+ {
+ return Value.ToString ();
+ }
+
+ public override object GetValue ()
+ {
+ return Value;
+ }
+
+ public override Constant Increment ()
+ {
+ return new IntConstant (checked(Value + 1), loc);
+ }
+
+ public override bool IsDefaultValue {
+ get {
+ return Value == 0;
+ }
+ }
+
+ public override bool IsNegative {
+ get {
+ return Value < 0;
+ }
+ }
+
+ public override bool IsOneInteger {
+ get {
+ return Value == 1;
+ }
+ }
+
+ public override bool IsZeroInteger {
+ get { return Value == 0; }
+ }
+
+ public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
+ {
+ if (target_type == TypeManager.byte_type) {
+ if (in_checked_context){
+ if (Value < Byte.MinValue || Value > Byte.MaxValue)
+ throw new OverflowException ();
+ }
+ return new ByteConstant ((byte) Value, Location);
+ }
+ if (target_type == TypeManager.sbyte_type) {
+ if (in_checked_context){
+ if (Value < SByte.MinValue || Value > SByte.MaxValue)
+ throw new OverflowException ();
+ }
+ return new SByteConstant ((sbyte) Value, Location);
+ }
+ if (target_type == TypeManager.short_type) {
+ if (in_checked_context){
+ if (Value < Int16.MinValue || Value > Int16.MaxValue)
+ throw new OverflowException ();
+ }
+ return new ShortConstant ((short) Value, Location);
+ }
+ if (target_type == TypeManager.ushort_type) {
+ if (in_checked_context){
+ if (Value < UInt16.MinValue || Value > UInt16.MaxValue)
+ throw new OverflowException ();
+ }
+ return new UShortConstant ((ushort) Value, Location);
+ }
+ if (target_type == TypeManager.uint32_type) {
+ if (in_checked_context){
+ if (Value < UInt32.MinValue)
+ throw new OverflowException ();
+ }
+ return new UIntConstant ((uint) Value, Location);
+ }
+ if (target_type == TypeManager.int64_type)
+ return new LongConstant ((long) Value, Location);
+ if (target_type == TypeManager.uint64_type) {
+ if (in_checked_context && Value < 0)
+ throw new OverflowException ();
+ return new ULongConstant ((ulong) Value, Location);
+ }
+ if (target_type == TypeManager.float_type)
+ return new FloatConstant ((float) Value, Location);
+ if (target_type == TypeManager.double_type)
+ return new DoubleConstant ((double) Value, Location);
+ if (target_type == TypeManager.char_type) {
+ if (in_checked_context){
+ if (Value < Char.MinValue || Value > Char.MaxValue)
+ throw new OverflowException ();
+ }
+ return new CharConstant ((char) Value, Location);
+ }
+ if (target_type == TypeManager.decimal_type)
+ return new DecimalConstant ((decimal) Value, Location);
+
+ return null;
+ }
+
+ public override Constant ConvertImplicitly (ResolveContext rc, TypeSpec type)
+ {
+ if (this.type == type)
+ return this;
+
+ Constant c = TryImplicitIntConversion (type);
+ if (c != null)
+ return c.Resolve (rc);
+
+ return base.ConvertImplicitly (rc, type);
+ }
+
+ /// <summary>
+ /// Attempts to perform an implicit constant conversion of the IntConstant
+ /// into a different data type using casts (See Implicit Constant
+ /// Expression Conversions)
+ /// </summary>
+ Constant TryImplicitIntConversion (TypeSpec target_type)
+ {
+ if (target_type == TypeManager.sbyte_type) {
+ if (Value >= SByte.MinValue && Value <= SByte.MaxValue)
+ return new SByteConstant ((sbyte) Value, loc);
+ }
+ else if (target_type == TypeManager.byte_type) {
+ if (Value >= Byte.MinValue && Value <= Byte.MaxValue)
+ return new ByteConstant ((byte) Value, loc);
+ }
+ else if (target_type == TypeManager.short_type) {
+ if (Value >= Int16.MinValue && Value <= Int16.MaxValue)
+ return new ShortConstant ((short) Value, loc);
+ }
+ else if (target_type == TypeManager.ushort_type) {
+ if (Value >= UInt16.MinValue && Value <= UInt16.MaxValue)
+ return new UShortConstant ((ushort) Value, loc);
+ }
+ else if (target_type == TypeManager.uint32_type) {
+ if (Value >= 0)
+ return new UIntConstant ((uint) Value, loc);
+ }
+ 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, loc);
+ }
+ else if (target_type == TypeManager.double_type)
+ return new DoubleConstant ((double) Value, loc);
+ else if (target_type == TypeManager.float_type)
+ return new FloatConstant ((float) Value, loc);
+
+ return null;
+ }
+ }
+
+ public class UIntConstant : IntegralConstant {
+ public readonly uint Value;
+
+ public UIntConstant (uint v, Location loc):
+ base (loc)
+ {
+ Value = v;
+ }
+
+ protected override Expression DoResolve (ResolveContext rc)
+ {
+ type = TypeManager.uint32_type;
+ eclass = ExprClass.Value;
+ return this;
+ }
+
+ public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+ {
+ enc.Stream.Write (Value);
+ }
+
+ public override void Emit (EmitContext ec)
+ {
+ ec.EmitInt (unchecked ((int) Value));
+ }
+
+ public override string AsString ()
+ {
+ return Value.ToString ();
+ }
+
+ public override object GetValue ()
+ {
+ return Value;
+ }
+
+ public override Constant Increment ()
+ {
+ return new UIntConstant (checked(Value + 1), loc);
+ }
+
+ public override bool IsDefaultValue {
+ get {
+ return Value == 0;
+ }
+ }
+
+ public override bool IsNegative {
+ get {
+ return false;
+ }
+ }
+
+ public override bool IsOneInteger {
+ get {
+ return Value == 1;
+ }
+ }
+
+ public override bool IsZeroInteger {
+ get { return Value == 0; }
+ }
+
+ public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
+ {
+ if (target_type == TypeManager.byte_type) {
+ if (in_checked_context){
+ if (Value < Char.MinValue || Value > Char.MaxValue)
+ throw new OverflowException ();
+ }
+ return new ByteConstant ((byte) Value, Location);
+ }
+ if (target_type == TypeManager.sbyte_type) {
+ if (in_checked_context){
+ if (Value > SByte.MaxValue)
+ throw new OverflowException ();
+ }
+ return new SByteConstant ((sbyte) Value, Location);
+ }
+ if (target_type == TypeManager.short_type) {
+ if (in_checked_context){
+ if (Value > Int16.MaxValue)
+ throw new OverflowException ();
+ }
+ return new ShortConstant ((short) Value, Location);
+ }
+ if (target_type == TypeManager.ushort_type) {
+ if (in_checked_context){
+ if (Value < UInt16.MinValue || Value > UInt16.MaxValue)
+ throw new OverflowException ();
+ }
+ return new UShortConstant ((ushort) Value, Location);
+ }
+ if (target_type == TypeManager.int32_type) {
+ if (in_checked_context){
+ if (Value > Int32.MaxValue)
+ throw new OverflowException ();
+ }
+ return new IntConstant ((int) Value, Location);
+ }
+ if (target_type == TypeManager.int64_type)
+ return new LongConstant ((long) Value, Location);
+ if (target_type == TypeManager.uint64_type)
+ return new ULongConstant ((ulong) Value, Location);
+ if (target_type == TypeManager.float_type)
+ return new FloatConstant ((float) Value, Location);
+ if (target_type == TypeManager.double_type)
+ return new DoubleConstant ((double) Value, Location);
+ if (target_type == TypeManager.char_type) {
+ if (in_checked_context){
+ if (Value < Char.MinValue || Value > Char.MaxValue)
+ throw new OverflowException ();
+ }
+ return new CharConstant ((char) Value, Location);
+ }
+ if (target_type == TypeManager.decimal_type)
+ return new DecimalConstant ((decimal) Value, Location);
+
+ return null;
+ }
+
+ }
+
+ public class LongConstant : IntegralConstant {
+ public readonly long Value;
+
+ public LongConstant (long v, Location loc):
+ base (loc)
+ {
+ Value = v;
+ }
+
+ protected override Expression DoResolve (ResolveContext rc)
+ {
+ type = TypeManager.int64_type;
+ eclass = ExprClass.Value;
+ return this;
+ }
+
+ public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+ {
+ enc.Stream.Write (Value);
+ }
+
+ public override void Emit (EmitContext ec)
+ {
+ ec.EmitLong (Value);
+ }
+
+ public override string AsString ()
+ {
+ return Value.ToString ();
+ }
+
+ public override object GetValue ()
+ {
+ return Value;
+ }
+
+ public override Constant Increment ()
+ {
+ return new LongConstant (checked(Value + 1), loc);
+ }
+
+ public override bool IsDefaultValue {
+ get {
+ return Value == 0;
+ }
+ }
+
+ public override bool IsNegative {
+ get {
+ return Value < 0;
+ }
+ }
+
+ public override bool IsOneInteger {
+ get {
+ return Value == 1;
+ }
+ }
+
+ public override bool IsZeroInteger {
+ get { return Value == 0; }
+ }
+
+ public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
+ {
+ if (target_type == TypeManager.byte_type) {
+ if (in_checked_context){
+ if (Value < Byte.MinValue || Value > Byte.MaxValue)
+ throw new OverflowException ();
+ }
+ return new ByteConstant ((byte) Value, Location);
+ }
+ if (target_type == TypeManager.sbyte_type) {
+ if (in_checked_context){
+ if (Value < SByte.MinValue || Value > SByte.MaxValue)
+ throw new OverflowException ();
+ }
+ return new SByteConstant ((sbyte) Value, Location);
+ }
+ if (target_type == TypeManager.short_type) {
+ if (in_checked_context){
+ if (Value < Int16.MinValue || Value > Int16.MaxValue)
+ throw new OverflowException ();
+ }
+ return new ShortConstant ((short) Value, Location);
+ }
+ if (target_type == TypeManager.ushort_type) {
+ if (in_checked_context){
+ if (Value < UInt16.MinValue || Value > UInt16.MaxValue)
+ throw new OverflowException ();
+ }
+ return new UShortConstant ((ushort) Value, Location);
+ }
+ if (target_type == TypeManager.int32_type) {
+ if (in_checked_context){
+ if (Value < Int32.MinValue || Value > Int32.MaxValue)
+ throw new OverflowException ();
+ }
+ return new IntConstant ((int) Value, Location);
+ }
+ if (target_type == TypeManager.uint32_type) {
+ if (in_checked_context){
+ if (Value < UInt32.MinValue || Value > UInt32.MaxValue)
+ throw new OverflowException ();
+ }
+ return new UIntConstant ((uint) Value, Location);
+ }
+ if (target_type == TypeManager.uint64_type) {
+ if (in_checked_context && Value < 0)
+ throw new OverflowException ();
+ return new ULongConstant ((ulong) Value, Location);
+ }
+ if (target_type == TypeManager.float_type)
+ return new FloatConstant ((float) Value, Location);
+ if (target_type == TypeManager.double_type)
+ return new DoubleConstant ((double) Value, Location);
+ if (target_type == TypeManager.char_type) {
+ if (in_checked_context){
+ if (Value < Char.MinValue || Value > Char.MaxValue)
+ throw new OverflowException ();
+ }
+ return new CharConstant ((char) Value, Location);
+ }
+ if (target_type == TypeManager.decimal_type)
+ return new DecimalConstant ((decimal) Value, Location);
+
+ return null;
+ }
+
+ public override Constant ConvertImplicitly (ResolveContext rc, TypeSpec type)
+ {
+ if (Value >= 0 && type == TypeManager.uint64_type) {
+ return new ULongConstant ((ulong) Value, loc).Resolve (rc);
+ }
+
+ return base.ConvertImplicitly (rc, type);
+ }
+ }
+
+ public class ULongConstant : IntegralConstant {
+ public readonly ulong Value;
+
+ public ULongConstant (ulong v, Location loc):
+ base (loc)
+ {
+ Value = v;
+ }
+
+ protected override Expression DoResolve (ResolveContext rc)
+ {
+ type = TypeManager.uint64_type;
+ eclass = ExprClass.Value;
+ return this;
+ }
+
+ public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+ {
+ enc.Stream.Write (Value);
+ }
+
+ public override void Emit (EmitContext ec)
+ {
+ ec.EmitLong (unchecked ((long) Value));
+ }
+
+ public override string AsString ()
+ {
+ return Value.ToString ();
+ }
+
+ public override object GetValue ()
+ {
+ return Value;
+ }
+
+ public override Constant Increment ()
+ {
+ return new ULongConstant (checked(Value + 1), loc);
+ }
+
+ public override bool IsDefaultValue {
+ get {
+ return Value == 0;
+ }
+ }
+
+ public override bool IsNegative {
+ get {
+ return false;
+ }
+ }
+
+ public override bool IsOneInteger {
+ get {
+ return Value == 1;
+ }
+ }
+
+ public override bool IsZeroInteger {
+ get { return Value == 0; }
+ }
+
+ public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
+ {
+ if (target_type == TypeManager.byte_type) {
+ if (in_checked_context && Value > Byte.MaxValue)
+ throw new OverflowException ();
+ return new ByteConstant ((byte) Value, Location);
+ }
+ if (target_type == TypeManager.sbyte_type) {
+ if (in_checked_context && Value > ((ulong) SByte.MaxValue))
+ throw new OverflowException ();
+ return new SByteConstant ((sbyte) Value, Location);
+ }
+ if (target_type == TypeManager.short_type) {
+ if (in_checked_context && Value > ((ulong) Int16.MaxValue))
+ throw new OverflowException ();
+ return new ShortConstant ((short) Value, Location);
+ }
+ if (target_type == TypeManager.ushort_type) {
+ if (in_checked_context && Value > UInt16.MaxValue)
+ throw new OverflowException ();
+ return new UShortConstant ((ushort) Value, Location);
+ }
+ if (target_type == TypeManager.int32_type) {
+ if (in_checked_context && Value > UInt32.MaxValue)
+ throw new OverflowException ();
+ return new IntConstant ((int) Value, Location);
+ }
+ if (target_type == TypeManager.uint32_type) {
+ if (in_checked_context && Value > UInt32.MaxValue)
+ throw new OverflowException ();
+ return new UIntConstant ((uint) Value, Location);
+ }
+ if (target_type == TypeManager.int64_type) {
+ if (in_checked_context && Value > Int64.MaxValue)
+ throw new OverflowException ();
+ return new LongConstant ((long) Value, Location);
+ }
+ if (target_type == TypeManager.float_type)
+ return new FloatConstant ((float) Value, Location);
+ if (target_type == TypeManager.double_type)
+ return new DoubleConstant ((double) Value, Location);
+ if (target_type == TypeManager.char_type) {
+ if (in_checked_context && Value > Char.MaxValue)
+ throw new OverflowException ();
+ return new CharConstant ((char) Value, Location);
+ }
+ if (target_type == TypeManager.decimal_type)
+ return new DecimalConstant ((decimal) Value, Location);
+
+ return null;
+ }
+
+ }
+
+ public class FloatConstant : Constant {
+ public float Value;
+
+ public FloatConstant (float v, Location loc):
+ base (loc)
+ {
+ Value = v;
+ }
+
+ protected override Expression DoResolve (ResolveContext rc)
+ {
+ type = TypeManager.float_type;
+ eclass = ExprClass.Value;
+ return this;
+ }
+
+ public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+ {
+ enc.Stream.Write (Value);
+ }
+
+ public override void Emit (EmitContext ec)
+ {
+ ec.Emit (OpCodes.Ldc_R4, Value);
+ }
+
+ public override string AsString ()
+ {
+ return Value.ToString ();
+ }
+
+ public override object GetValue ()
+ {
+ return Value;
+ }
+
+ public override bool IsDefaultValue {
+ get {
+ return Value == 0;
+ }
+ }
+
+ public override bool IsNegative {
+ get {
+ return Value < 0;
+ }
+ }
+
+ public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
+ {
+ if (target_type == TypeManager.byte_type) {
+ if (in_checked_context){
+ if (Value < byte.MinValue || Value > byte.MaxValue || float.IsNaN (Value))
+ throw new OverflowException ();
+ }
+ return new ByteConstant ((byte) Value, Location);
+ }
+ if (target_type == TypeManager.sbyte_type) {
+ if (in_checked_context){
+ if (Value < sbyte.MinValue || Value > sbyte.MaxValue || float.IsNaN (Value))
+ throw new OverflowException ();
+ }
+ return new SByteConstant ((sbyte) Value, Location);
+ }
+ if (target_type == TypeManager.short_type) {
+ if (in_checked_context){
+ if (Value < short.MinValue || Value > short.MaxValue || float.IsNaN (Value))
+ throw new OverflowException ();
+ }
+ return new ShortConstant ((short) Value, Location);
+ }
+ if (target_type == TypeManager.ushort_type) {
+ if (in_checked_context){
+ if (Value < ushort.MinValue || Value > ushort.MaxValue || float.IsNaN (Value))
+ throw new OverflowException ();
+ }
+ return new UShortConstant ((ushort) Value, Location);
+ }
+ if (target_type == TypeManager.int32_type) {
+ if (in_checked_context){
+ if (Value < int.MinValue || Value > int.MaxValue || float.IsNaN (Value))
+ throw new OverflowException ();
+ }
+ return new IntConstant ((int) Value, Location);
+ }
+ if (target_type == TypeManager.uint32_type) {
+ if (in_checked_context){
+ if (Value < uint.MinValue || Value > uint.MaxValue || float.IsNaN (Value))
+ throw new OverflowException ();
+ }
+ return new UIntConstant ((uint) Value, Location);
+ }
+ if (target_type == TypeManager.int64_type) {
+ if (in_checked_context){
+ if (Value < long.MinValue || Value > long.MaxValue || float.IsNaN (Value))
+ throw new OverflowException ();
+ }
+ return new LongConstant ((long) Value, Location);
+ }
+ if (target_type == TypeManager.uint64_type) {
+ if (in_checked_context){
+ if (Value < ulong.MinValue || Value > ulong.MaxValue || float.IsNaN (Value))
+ throw new OverflowException ();
+ }
+ return new ULongConstant ((ulong) Value, Location);
+ }
+ if (target_type == TypeManager.double_type)
+ return new DoubleConstant ((double) Value, Location);
+ if (target_type == TypeManager.char_type) {
+ if (in_checked_context){
+ if (Value < (float) char.MinValue || Value > (float) char.MaxValue || float.IsNaN (Value))
+ throw new OverflowException ();
+ }
+ return new CharConstant ((char) Value, Location);
+ }
+ if (target_type == TypeManager.decimal_type)
+ return new DecimalConstant ((decimal) Value, Location);
+
+ return null;
+ }
+
+ }
+
+ public class DoubleConstant : Constant {
+ public double Value;
+
+ public DoubleConstant (double v, Location loc):
+ base (loc)
+ {
+ Value = v;
+ }
+
+ protected override Expression DoResolve (ResolveContext rc)
+ {
+ type = TypeManager.double_type;
+ eclass = ExprClass.Value;
+ return this;
+ }
+
+ public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+ {
+ enc.Stream.Write (Value);
+ }
+
+ public override void Emit (EmitContext ec)
+ {
+ ec.Emit (OpCodes.Ldc_R8, Value);
+ }
+
+ public override string AsString ()
+ {
+ return Value.ToString ();
+ }
+
+ public override object GetValue ()
+ {
+ return Value;
+ }
+
+ public override bool IsDefaultValue {
+ get {
+ return Value == 0;
+ }
+ }
+
+ public override bool IsNegative {
+ get {
+ return Value < 0;
+ }
+ }
+
+ public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
+ {
+ if (target_type == TypeManager.byte_type) {
+ if (in_checked_context){
+ if (Value < Byte.MinValue || Value > Byte.MaxValue || double.IsNaN (Value))
+ throw new OverflowException ();
+ }
+ return new ByteConstant ((byte) Value, Location);
+ }
+ if (target_type == TypeManager.sbyte_type) {
+ if (in_checked_context){
+ if (Value < SByte.MinValue || Value > SByte.MaxValue || double.IsNaN (Value))
+ throw new OverflowException ();
+ }
+ return new SByteConstant ((sbyte) Value, Location);
+ }
+ if (target_type == TypeManager.short_type) {
+ if (in_checked_context){
+ if (Value < short.MinValue || Value > short.MaxValue || double.IsNaN (Value))
+ throw new OverflowException ();
+ }
+ return new ShortConstant ((short) Value, Location);
+ }
+ if (target_type == TypeManager.ushort_type) {
+ if (in_checked_context){
+ if (Value < ushort.MinValue || Value > ushort.MaxValue || double.IsNaN (Value))
+ throw new OverflowException ();
+ }
+ return new UShortConstant ((ushort) Value, Location);
+ }
+ if (target_type == TypeManager.int32_type) {
+ if (in_checked_context){
+ if (Value < int.MinValue || Value > int.MaxValue || double.IsNaN (Value))
+ throw new OverflowException ();
+ }
+ return new IntConstant ((int) Value, Location);
+ }
+ if (target_type == TypeManager.uint32_type) {
+ if (in_checked_context){
+ if (Value < uint.MinValue || Value > uint.MaxValue || double.IsNaN (Value))
+ throw new OverflowException ();
+ }
+ return new UIntConstant ((uint) Value, Location);
+ }
+ if (target_type == TypeManager.int64_type) {
+ if (in_checked_context){
+ if (Value < long.MinValue || Value > long.MaxValue || double.IsNaN (Value))
+ throw new OverflowException ();
+ }
+ return new LongConstant ((long) Value, Location);
+ }
+ if (target_type == TypeManager.uint64_type) {
+ if (in_checked_context){
+ if (Value < ulong.MinValue || Value > ulong.MaxValue || double.IsNaN (Value))
+ throw new OverflowException ();
+ }
+ return new ULongConstant ((ulong) Value, Location);
+ }
+ if (target_type == TypeManager.float_type)
+ return new FloatConstant ((float) Value, Location);
+ if (target_type == TypeManager.char_type) {
+ if (in_checked_context){
+ if (Value < (double) char.MinValue || Value > (double) char.MaxValue || double.IsNaN (Value))
+ throw new OverflowException ();
+ }
+ return new CharConstant ((char) Value, Location);
+ }
+ if (target_type == TypeManager.decimal_type)
+ return new DecimalConstant ((decimal) Value, Location);
+
+ return null;
+ }
+
+ }
+
+ public class DecimalConstant : Constant {
+ public readonly decimal Value;
+
+ public DecimalConstant (decimal d, Location loc):
+ base (loc)
+ {
+ Value = d;
+ }
+
+ override public string AsString ()
+ {
+ return Value.ToString () + "M";
+ }
+
+ protected override Expression DoResolve (ResolveContext rc)
+ {
+ type = TypeManager.decimal_type;
+ eclass = ExprClass.Value;
+ return this;
+ }
+
+ public override object GetValue ()
+ {
+ return (object) Value;
+ }
+
+ public override void Emit (EmitContext ec)
+ {
+ int [] words = decimal.GetBits (Value);
+ int power = (words [3] >> 16) & 0xff;
+
+ if (power == 0) {
+ if (Value <= int.MaxValue && Value >= int.MinValue) {
+ if (TypeManager.void_decimal_ctor_int_arg == null) {
+ TypeManager.void_decimal_ctor_int_arg = TypeManager.GetPredefinedConstructor (
+ TypeManager.decimal_type, loc, TypeManager.int32_type);
+
+ if (TypeManager.void_decimal_ctor_int_arg == null)
+ return;
+ }
+
+ ec.EmitInt ((int) Value);
+ ec.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_int_arg);
+ return;
+ }
+
+ if (Value <= long.MaxValue && Value >= long.MinValue) {
+ if (TypeManager.void_decimal_ctor_long_arg == null) {
+ TypeManager.void_decimal_ctor_long_arg = TypeManager.GetPredefinedConstructor (
+ TypeManager.decimal_type, loc, TypeManager.int64_type);
+
+ if (TypeManager.void_decimal_ctor_long_arg == null)
+ return;
+ }
+
+ ec.EmitLong ((long) Value);
+ ec.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_long_arg);
+ return;
+ }
+ }
+
+ ec.EmitInt (words [0]);
+ ec.EmitInt (words [1]);
+ ec.EmitInt (words [2]);
+
+ // sign
+ ec.EmitInt (words [3] >> 31);
+
+ // power
+ ec.EmitInt (power);
+
+ if (TypeManager.void_decimal_ctor_five_args == null) {
+ TypeManager.void_decimal_ctor_five_args = TypeManager.GetPredefinedConstructor (
+ TypeManager.decimal_type, loc, TypeManager.int32_type, TypeManager.int32_type,
+ TypeManager.int32_type, TypeManager.bool_type, TypeManager.byte_type);
+
+ if (TypeManager.void_decimal_ctor_five_args == null)
+ return;
+ }
+
+ ec.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_five_args);
+ }
+
+ public override bool IsDefaultValue {
+ get {
+ return Value == 0;
+ }
+ }
+
+ public override bool IsNegative {
+ get {
+ return Value < 0;
+ }
+ }
+
+ public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
+ {
+ if (target_type == TypeManager.sbyte_type)
+ return new SByteConstant ((sbyte)Value, loc);
+ if (target_type == TypeManager.byte_type)
+ return new ByteConstant ((byte)Value, loc);
+ if (target_type == TypeManager.short_type)
+ return new ShortConstant ((short)Value, loc);
+ if (target_type == TypeManager.ushort_type)
+ return new UShortConstant ((ushort)Value, loc);
+ if (target_type == TypeManager.int32_type)
+ return new IntConstant ((int)Value, loc);
+ if (target_type == TypeManager.uint32_type)
+ return new UIntConstant ((uint)Value, loc);
+ if (target_type == TypeManager.int64_type)
+ return new LongConstant ((long)Value, loc);
+ if (target_type == TypeManager.uint64_type)
+ return new ULongConstant ((ulong)Value, loc);
+ if (target_type == TypeManager.char_type)
+ return new CharConstant ((char)Value, loc);
+ if (target_type == TypeManager.float_type)
+ return new FloatConstant ((float)Value, loc);
+ if (target_type == TypeManager.double_type)
+ return new DoubleConstant ((double)Value, loc);
+
+ return null;
+ }
+
+ }
+
+ public class StringConstant : Constant {
+ public readonly string Value;
+
+ public StringConstant (string s, Location loc):
+ base (loc)
+ {
+ Value = s;
+ }
+
+ // FIXME: Escape the string.
+ override public string AsString ()
+ {
+ return "\"" + Value + "\"";
+ }
+
+ protected override Expression DoResolve (ResolveContext rc)
+ {
+ type = TypeManager.string_type;
+ eclass = ExprClass.Value;
+ return this;
+ }
+
+ public override object GetValue ()
+ {
+ return Value;
+ }
+
+ public override void Emit (EmitContext ec)
+ {
+ if (Value == null) {
+ ec.Emit (OpCodes.Ldnull);
+ return;
+ }
+
+ //
+ // Use string.Empty for both literals and constants even if
+ // it's not allowed at language level
+ //
+ if (Value.Length == 0 && RootContext.Optimize && !TypeManager.IsEqual (ec.CurrentType, TypeManager.string_type)) {
+ if (TypeManager.string_empty == null)
+ TypeManager.string_empty = TypeManager.GetPredefinedField (TypeManager.string_type, "Empty", loc, TypeManager.string_type);
+
+ if (TypeManager.string_empty != null) {
+ ec.Emit (OpCodes.Ldsfld, TypeManager.string_empty);
+ return;
+ }
+ }
+
+ ec.Emit (OpCodes.Ldstr, Value);
+ }
+
+ public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+ {
+ // cast to object
+ if (type != targetType)
+ enc.Encode (type);
+
+ enc.Encode (Value);
+ }
+
+ public override bool IsDefaultValue {
+ get {
+ return Value == null;
+ }
+ }
+
+ public override bool IsNegative {
+ get {
+ return false;
+ }
+ }
+
+ public override bool IsNull {
+ get {
+ return IsDefaultValue;
+ }
+ }
+
+ public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
+ {
+ return null;
+ }
+ }
+
+ //
+ // Null constant can have its own type, think of `default (Foo)'
+ //
+ public class NullConstant : Constant
+ {
+ public NullConstant (TypeSpec type, Location loc)
+ : base (loc)
+ {
+ eclass = ExprClass.Value;
+ this.type = type;
+ }
+
+ public override string AsString ()
+ {
+ return GetSignatureForError ();
+ }
+
+ public override Expression CreateExpressionTree (ResolveContext ec)
+ {
+ if (type == InternalType.Null || type == TypeManager.object_type) {
+ // Optimized version, also avoids referencing literal internal type
+ Arguments args = new Arguments (1);
+ args.Add (new Argument (this));
+ return CreateExpressionFactoryCall (ec, "Constant", args);
+ }
+
+ return base.CreateExpressionTree (ec);
+ }
+
+ protected override Expression DoResolve (ResolveContext ec)
+ {
+ return this;
+ }
+
+ public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
+ {
+ // Type it as string cast
+ if (targetType == TypeManager.object_type || targetType == TypeManager.null_type)
+ enc.Encode (TypeManager.string_type);
+
+ var ac = targetType as ArrayContainer;
+ if (ac != null) {
+ if (ac.Rank != 1)
+ base.EncodeAttributeValue (rc, enc, targetType);
+ else
+ enc.Stream.Write (uint.MaxValue);
+ } else {
+ enc.Stream.Write (byte.MaxValue);
+ }
+ }
+
+ public override void Emit (EmitContext ec)
+ {
+ ec.Emit (OpCodes.Ldnull);
+
+ // Only to make verifier happy
+ if (TypeManager.IsGenericParameter (type))
+ ec.Emit (OpCodes.Unbox_Any, type);
+ }
+
+ public override string ExprClassName {
+ get {
+ return GetSignatureForError ();
+ }
+ }
+
+ public override string GetSignatureForError ()
+ {
+ return "null";
+ }
+
+ public override Constant ConvertExplicitly (bool inCheckedContext, TypeSpec targetType)
+ {
+ if (targetType.IsPointer) {
+ if (IsLiteral || this is NullPointer)
+ return new EmptyConstantCast (new NullPointer (loc), targetType);
+
+ return null;
+ }
+
+ // Exlude internal compiler types
+ if (targetType == InternalType.AnonymousMethod)
+ return null;
+
+ if (!IsLiteral && !Convert.ImplicitStandardConversionExists (this, targetType))
+ return null;
+
+ if (TypeManager.IsReferenceType (targetType))
+ return new NullConstant (targetType, loc);
+
+ if (TypeManager.IsNullableType (targetType))
+ return Nullable.LiftedNull.Create (targetType, loc);
+
+ return null;
+ }
+
+ public override Constant ConvertImplicitly (ResolveContext rc, TypeSpec targetType)
+ {
+ return ConvertExplicitly (false, targetType);
+ }
+
+ public override object GetValue ()
+ {
+ return null;
+ }
+
+ public override bool IsDefaultValue {
+ get { return true; }
+ }
+
+ public override bool IsNegative {
+ get { return false; }
+ }
+
+ public override bool IsNull {
+ get { return true; }
+ }
+
+ public override bool IsZeroInteger {
+ get { return true; }
+ }
+ }
+
+ /// <summary>
+ /// The value is constant, but when emitted has a side effect. This is
+ /// used by BitwiseAnd to ensure that the second expression is invoked
+ /// regardless of the value of the left side.
+ /// </summary>
+ public class SideEffectConstant : Constant {
+ public Constant value;
+ Expression side_effect;
+
+ public SideEffectConstant (Constant value, Expression side_effect, Location loc) : base (loc)
+ {
+ this.value = value;
+ while (side_effect is SideEffectConstant)
+ side_effect = ((SideEffectConstant) side_effect).side_effect;
+ this.side_effect = side_effect;
+ }
+
+ public override string AsString ()
+ {
+ return value.AsString ();
+ }
+
+ protected override Expression DoResolve (ResolveContext rc)
+ {
+ value = value.Resolve (rc);
+
+ type = value.Type;
+ eclass = ExprClass.Value;
+ return this;
+ }
+
+ public override object GetValue ()
+ {
+ return value.GetValue ();
+ }
+
+ public override void Emit (EmitContext ec)
+ {
+ side_effect.EmitSideEffect (ec);
+ value.Emit (ec);
+ }
+
+ public override void EmitSideEffect (EmitContext ec)
+ {
+ side_effect.EmitSideEffect (ec);
+ value.EmitSideEffect (ec);
+ }
+
+ public override bool IsDefaultValue {
+ get { return value.IsDefaultValue; }
+ }
+
+ public override bool IsNegative {
+ get { return value.IsNegative; }
+ }
+
+ public override bool IsZeroInteger {
+ get { return value.IsZeroInteger; }
+ }
+
+ public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
+ {
+ Constant new_value = value.ConvertExplicitly (in_checked_context, target_type);
+ return new_value == null ? null : new SideEffectConstant (new_value, side_effect, new_value.Location);
+ }
+ }
+}