📄 constant.cs
字号:
return new CharConstant ((char) Value, Location); } if (target_type == TypeManager.decimal_type) return new DecimalConstant ((decimal) Value, Location); return null; } } public class LongConstant : Constant { public readonly long Value; public LongConstant (long v, Location loc): base (loc) { type = TypeManager.int64_type; eclass = ExprClass.Value; Value = v; } public override void Emit (EmitContext ec) { ILGenerator ig = ec.ig; EmitLong (ig, Value); } static public void EmitLong (ILGenerator ig, long l) { if ((l >> 32) == 0){ IntLiteral.EmitInt (ig, unchecked ((int) l)); ig.Emit (OpCodes.Conv_U8); } else { ig.Emit (OpCodes.Ldc_I8, l); } } public override string AsString () { return Value.ToString (); } public override object GetValue () { return Value; } public override DoubleConstant ConvertToDouble () { return new DoubleConstant (Value, loc); } public override FloatConstant ConvertToFloat () { return new FloatConstant (Value, loc); } public override ULongConstant ConvertToULong () { if (Value < 0) return null; return new ULongConstant ((ulong) Value, loc); } public override LongConstant ConvertToLong () { return this; } public override UIntConstant ConvertToUInt () { return null; } public override IntConstant ConvertToInt () { return null; } 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 IsZeroInteger { get { return Value == 0; } } public override Constant Reduce (EmitContext ec, Type target_type) { if (target_type == TypeManager.byte_type) { CheckRange (ec, Value, target_type, Byte.MinValue, Byte.MaxValue); return new ByteConstant ((byte) Value, Location); } if (target_type == TypeManager.sbyte_type) { CheckRange (ec, Value, target_type, SByte.MinValue, SByte.MaxValue); return new SByteConstant ((sbyte) Value, Location); } if (target_type == TypeManager.short_type) { CheckRange (ec, Value, target_type, Int16.MinValue, Int16.MaxValue); return new ShortConstant ((short) Value, Location); } if (target_type == TypeManager.ushort_type) { CheckRange (ec, Value, target_type, UInt16.MinValue, UInt16.MaxValue); return new UShortConstant ((ushort) Value, Location); } if (target_type == TypeManager.int32_type) { CheckRange (ec, Value, target_type, Int32.MinValue, Int32.MaxValue); return new IntConstant ((int) Value, Location); } if (target_type == TypeManager.uint32_type) { CheckRange (ec, Value, target_type, UInt32.MinValue, UInt32.MaxValue); return new UIntConstant ((uint) Value, Location); } if (target_type == TypeManager.uint64_type) { CheckUnsigned (ec, Value, target_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) { CheckRange (ec, Value, target_type, Char.MinValue, Char.MaxValue); return new CharConstant ((char) Value, Location); } if (target_type == TypeManager.decimal_type) return new DecimalConstant ((decimal) Value, Location); return null; } } public class ULongConstant : Constant { public readonly ulong Value; public ULongConstant (ulong v, Location loc): base (loc) { type = TypeManager.uint64_type; eclass = ExprClass.Value; Value = v; } public override void Emit (EmitContext ec) { ILGenerator ig = ec.ig; LongLiteral.EmitLong (ig, unchecked ((long) Value)); } public override string AsString () { return Value.ToString (); } public override object GetValue () { return Value; } public override DoubleConstant ConvertToDouble () { return new DoubleConstant (Value, loc); } public override FloatConstant ConvertToFloat () { return new FloatConstant (Value, loc); } public override ULongConstant ConvertToULong () { return this; } public override LongConstant ConvertToLong () { return null; } public override UIntConstant ConvertToUInt () { return null; } public override IntConstant ConvertToInt () { return null; } 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 IsZeroInteger { get { return Value == 0; } } public override Constant Reduce (EmitContext ec, Type target_type) { if (target_type == TypeManager.byte_type) { CheckRange (ec, Value, target_type, Byte.MaxValue); return new ByteConstant ((byte) Value, Location); } if (target_type == TypeManager.sbyte_type) { CheckRange (ec, Value, target_type, (ulong) SByte.MaxValue); return new SByteConstant ((sbyte) Value, Location); } if (target_type == TypeManager.short_type) { CheckRange (ec, Value, target_type, (ulong) Int16.MaxValue); return new ShortConstant ((short) Value, Location); } if (target_type == TypeManager.ushort_type) { CheckRange (ec, Value, target_type, UInt16.MaxValue); return new UShortConstant ((ushort) Value, Location); } if (target_type == TypeManager.int32_type) { CheckRange (ec, Value, target_type, Int32.MaxValue); return new IntConstant ((int) Value, Location); } if (target_type == TypeManager.uint32_type) { CheckRange (ec, Value, target_type, UInt32.MaxValue); return new UIntConstant ((uint) Value, Location); } if (target_type == TypeManager.int64_type) { CheckRange (ec, Value, target_type, (ulong) Int64.MaxValue); 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) { CheckRange (ec, Value, target_type, Char.MaxValue); 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 readonly float Value; public FloatConstant (float v, Location loc): base (loc) { type = TypeManager.float_type; eclass = ExprClass.Value; Value = v; } public override void Emit (EmitContext ec) { ec.ig.Emit (OpCodes.Ldc_R4, Value); } public override string AsString () { return Value.ToString (); } public override object GetValue () { return Value; } public override DoubleConstant ConvertToDouble () { return new DoubleConstant (Value, loc); } public override FloatConstant ConvertToFloat () { return this; } public override LongConstant ConvertToLong () { return null; } public override UIntConstant ConvertToUInt () { return null; } public override IntConstant ConvertToInt () { return null; } public override Constant Increment () { return new FloatConstant (checked(Value + 1), loc); } public override bool IsDefaultValue { get { return Value == 0; } } public override bool IsNegative { get { return Value < 0; } } public override Constant Reduce (EmitContext ec, Type target_type) { if (target_type == TypeManager.byte_type) return new ByteConstant ((byte) Value, Location); if (target_type == TypeManager.sbyte_type) return new SByteConstant ((sbyte) Value, Location); if (target_type == TypeManager.short_type) return new ShortConstant ((short) Value, Location); if (target_type == TypeManager.ushort_type) return new UShortConstant ((ushort) 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.double_type) return new DoubleConstant ((double) Value, Location); if (target_type == TypeManager.char_type) 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 readonly double Value; public DoubleConstant (double v, Location loc): base (loc) { type = TypeManager.double_type; eclass = ExprClass.Value; Value = v; } public override void Emit (EmitContext ec) { ec.ig.Emit (OpCodes.Ldc_R8, Value); } public override string AsString () { return Value.ToString (); } public override object GetValue () { return Value; } public override DoubleConstant ConvertToDouble () { return this; } public override FloatConstant ConvertToFloat () { return null; } public override ULongConstant ConvertToULong () { return null; } public override LongConstant ConvertToLong () { return null; } public override UIntConstant ConvertToUInt () { return null; } public override IntConstant ConvertToInt () { return null; } public override Constant Increment () { return new DoubleConstant (checked(Value + 1), loc); } public override bool IsDefaultValue { get { return Value == 0; } } public override bool IsNegative { get { return Value < 0; } } public override Constant Reduce (EmitContext ec, Type target_type) { if (target_type == TypeManager.byte_type) { CheckRange (ec, Value, target_type, Byte.MinValue, Byte.MaxValue); return new ByteConstant ((byte) Value, Location); } if (target_type == TypeManager.sbyte_type) { CheckRange (ec, Value, target_type, SByte.MinValue, SByte.MaxValue); return new SByteConstant ((sbyte) Value, Location); } if (target_type == TypeManager.short_type) return new ShortConstant ((short) Value, Location); if (target_type == TypeManager.ushort_type) return new UShortConstant ((ushort) 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.char_type) { CheckRange (ec, Value, target_type, char.MinValue, char.MaxValue); 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) { type = TypeManager.decimal_type; eclass = ExprClass.Value; Value = d; } override public string AsString () { return Value.ToString (); } public override object GetValue () { return (object) Value; } public override void Emit (EmitContext ec) { ILGenerator ig = ec.ig; int [] words = Decimal.GetBits (Value); int power = (words [3] >> 16) & 0xff; if (power == 0 && Value <= int.MaxValue && Value >= int.MinValue) { IntConstant.EmitInt (ig, (int)Value); ig.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_int_arg); return; } // // FIXME: we could optimize this, and call a better // constructor // IntConstant.EmitInt (ig, words [0]); IntConstant.EmitInt (ig, words [1]); IntConstant.EmitInt (ig, words [2]); // sign IntConstant.EmitInt (ig, words [3] >> 31); // power IntConstant.EmitInt (ig, power); ig.Emit (OpCodes.Newobj, TypeManager.void_decimal_ctor_five_args); } public override Constant Increment () { return new DecimalConstant (checked (Value + 1), loc); } public override bool IsDefaultValue { get { return Value == 0; } } public override bool IsNegative { get { return Value < 0; } } public override Constant Reduce (EmitContext ec, Type 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) { type = TypeManager.string_type; eclass = ExprClass.Value; Value = s; } // FIXME: Escape the string. override public string AsString () { return "\"" + Value + "\""; } public override object GetValue () { return Value; } public override void Emit (EmitContext ec) { if (Value == null) ec.ig.Emit (OpCodes.Ldnull); else ec.ig.Emit (OpCodes.Ldstr, Value); } public override Constant Increment () { throw new NotSupportedException (); } public override bool IsDefaultValue { get { return Value == null; } } public override bool IsNegative { get { return false; } } public override Constant Reduce (EmitContext ec, Type target_type) { return null; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -