📄 constant.cs
字号:
{ type = TypeManager.sbyte_type; eclass = ExprClass.Value; Value = v; } public override void Emit (EmitContext ec) { IntLiteral.EmitInt (ec.ig, 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 () { if (Value >= 0) return new ULongConstant ((ulong) Value, loc); return null; } public override LongConstant ConvertToLong () { return new LongConstant (Value, loc); } public override UIntConstant ConvertToUInt () { return null; } public override IntConstant ConvertToInt () { return new IntConstant (Value, loc); } public override Constant Increment () { return new SByteConstant (checked((sbyte)(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) { CheckUnsigned (ec, Value, target_type); return new ByteConstant ((byte) Value, Location); } if (target_type == TypeManager.short_type) return new ShortConstant ((short) Value, Location); if (target_type == TypeManager.ushort_type) { CheckUnsigned (ec, Value, target_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) { CheckUnsigned (ec, Value, target_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) { 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) { CheckUnsigned (ec, Value, target_type); return new CharConstant ((char) Value, Location); } if (target_type == TypeManager.decimal_type) return new DecimalConstant ((decimal) Value, Location); return null; } } public class ShortConstant : Constant { public readonly short Value; public ShortConstant (short v, Location loc): base (loc) { type = TypeManager.short_type; eclass = ExprClass.Value; Value = v; } public override void Emit (EmitContext ec) { IntLiteral.EmitInt (ec.ig, 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 null; } public override LongConstant ConvertToLong () { return new LongConstant (Value, loc); } public override UIntConstant ConvertToUInt () { return null; } public override IntConstant ConvertToInt () { return new IntConstant (Value, loc); } 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 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.ushort_type) { CheckUnsigned (ec, Value, target_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) { CheckUnsigned (ec, Value, target_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) { 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 UShortConstant : Constant { public readonly ushort Value; public UShortConstant (ushort v, Location loc): base (loc) { type = TypeManager.ushort_type; eclass = ExprClass.Value; Value = v; } public override void Emit (EmitContext ec) { IntLiteral.EmitInt (ec.ig, 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 new ULongConstant (Value, loc); } public override LongConstant ConvertToLong () { return new LongConstant (Value, loc); } public override UIntConstant ConvertToUInt () { return new UIntConstant (Value, loc); } public override IntConstant ConvertToInt () { return new IntConstant (Value, loc); } 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 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.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) { 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 IntConstant : Constant { public readonly int Value; public IntConstant (int v, Location loc): base (loc) { type = TypeManager.int32_type; eclass = ExprClass.Value; Value = v; } static public void EmitInt (ILGenerator ig, int i) { switch (i){ case -1: ig.Emit (OpCodes.Ldc_I4_M1); break; case 0: ig.Emit (OpCodes.Ldc_I4_0); break; case 1: ig.Emit (OpCodes.Ldc_I4_1); break; case 2: ig.Emit (OpCodes.Ldc_I4_2); break; case 3: ig.Emit (OpCodes.Ldc_I4_3); break; case 4: ig.Emit (OpCodes.Ldc_I4_4); break; case 5: ig.Emit (OpCodes.Ldc_I4_5); break; case 6: ig.Emit (OpCodes.Ldc_I4_6); break; case 7: ig.Emit (OpCodes.Ldc_I4_7); break; case 8: ig.Emit (OpCodes.Ldc_I4_8); break; default: if (i >= -128 && i <= 127){ ig.Emit (OpCodes.Ldc_I4_S, (sbyte) i); } else ig.Emit (OpCodes.Ldc_I4, i); break; } } public override void Emit (EmitContext ec) { EmitInt (ec.ig, Value); } public override string AsString () { return Value.ToString (); } public override object GetValue () { return Value; } public override DecimalConstant ConvertToDecimal() { return new DecimalConstant (Value, loc); } 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 new LongConstant (Value, loc); } public override UIntConstant ConvertToUInt () { if (Value < 0) return null; return new UIntConstant ((uint) Value, loc); } public override IntConstant ConvertToInt () { return this; } 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 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.uint32_type) { CheckRange (ec, Value, target_type, Int32.MinValue, Int32.MaxValue); return new UIntConstant ((uint) Value, Location); } if (target_type == TypeManager.int64_type) return new LongConstant ((long) 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 UIntConstant : Constant { public readonly uint Value; public UIntConstant (uint v, Location loc): base (loc) { type = TypeManager.uint32_type; eclass = ExprClass.Value; Value = v; } public override void Emit (EmitContext ec) { IntLiteral.EmitInt (ec.ig, unchecked ((int) 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 new ULongConstant (Value, loc); } public override LongConstant ConvertToLong () { return new LongConstant (Value, loc); } public override UIntConstant ConvertToUInt () { return this; } public override IntConstant ConvertToInt () { return null; } 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 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, Char.MinValue, Char.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.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) { CheckRange (ec, Value, target_type, Char.MinValue, Char.MaxValue);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -