예제 #1
0
        internal static void EmitLiteral(ILGenerator ilg, Constant c, TypeSymbol t = null)
        {
            if (t == null)
            {
                t = c.Type;
            }
            switch (t.NativeType)
            {
            case NativeType.Boolean:
                if (c.Boolean == true)
                {
                    ilg.Emit(OpCodes.Ldc_I4_1);
                }
                else
                {
                    ilg.Emit(OpCodes.Ldc_I4_0);
                }
                break;

            case NativeType.SByte:
            case NativeType.Int16:
            case NativeType.Int32:
            {
                int?ordinal = c.Int;
                EmitConstant_I4(ilg, ordinal.Value);
            }
            break;

            case NativeType.Byte:
            case NativeType.Char:
            case NativeType.UInt16:
            case NativeType.UInt32:
            {
                uint?ordinal = c.UInt;
                EmitConstant_I4(ilg, unchecked ((int)ordinal.Value));
            }
            break;

            case NativeType.UInt64:
                ilg.Emit(OpCodes.Ldc_I8, c.ULong.Value);
                break;

            case NativeType.Int64:
                ilg.Emit(OpCodes.Ldc_I8, c.Long.Value);
                break;

            case NativeType.Single:
                ilg.Emit(OpCodes.Ldc_R4, c.Float.Value);
                break;

            case NativeType.Double:
                ilg.Emit(OpCodes.Ldc_R8, c.Double.Value);
                break;

            case NativeType.String:
                ilg.Emit(OpCodes.Ldstr, c.String);
                break;

            case NativeType.DateTime:
                EmitConstant_I4(ilg, c.DateTime.Value.Year);
                EmitConstant_I4(ilg, c.DateTime.Value.Month);
                EmitConstant_I4(ilg, c.DateTime.Value.Day);
                ilg.Emit(OpCodes.Newobj, (Compilation.Get(WellKnownMembers.XSharp___Date_ctor) as ConstructorSymbol).Constructor);
                break;

            case NativeType.Decimal:
            {
                int[] bits = decimal.GetBits(c.Decimal.Value);
                EmitConstant_I4(ilg, bits.Length);
                ilg.Emit(OpCodes.Newarr, typeof(int));
                for (int i = 0; i < bits.Length; i++)
                {
                    ilg.Emit(OpCodes.Dup);
                    EmitConstant_I4(ilg, i);
                    EmitConstant_I4(ilg, bits[i]);
                    ilg.Emit(OpCodes.Stelem_I4);
                }
                ilg.Emit(OpCodes.Newobj, (Compilation.Get(WellKnownMembers.System_Decimal_ctor) as ConstructorSymbol).Constructor);
            }
            break;

            case NativeType.Object:
                ilg.Emit(OpCodes.Ldnull);
                break;

            case NativeType.Usual:
                EmitDefault(ilg, t);
                break;

            default:
                if (c.Type.IsEnum)
                {
                    EmitLiteral(ilg, c, c.Type.EnumUnderlyingType);
                    break;
                }
                throw new InternalError();
            }
        }