Example #1
0
        private static void EmitLiteral(FieldInfo fi, FleeIlGenerator ilg, IServiceProvider services)
        {
            var            value = RuntimeHelpers.GetObjectValue(fi.GetValue(null));
            var            t     = value.GetType();
            LiteralElement elem;

            switch (Type.GetTypeCode(t))
            {
            case TypeCode.Boolean:
                elem = new BooleanLiteralElement((bool)value);
                goto IL_F4;

            case TypeCode.Char:
            case TypeCode.SByte:
            case TypeCode.Byte:
            case TypeCode.Int16:
            case TypeCode.UInt16:
            case TypeCode.Int32:
                elem = new Int32LiteralElement(Convert.ToInt32(RuntimeHelpers.GetObjectValue(value)));
                goto IL_F4;

            case TypeCode.UInt32:
                elem = new UInt32LiteralElement((uint)value);
                goto IL_F4;

            case TypeCode.Int64:
                elem = new Int64LiteralElement((long)value);
                goto IL_F4;

            case TypeCode.UInt64:
                elem = new UInt64LiteralElement((ulong)value);
                goto IL_F4;

            case TypeCode.Single:
                elem = new SingleLiteralElement((float)value);
                goto IL_F4;

            case TypeCode.Double:
                elem = new DoubleLiteralElement((double)value);
                goto IL_F4;

            case TypeCode.String:
                elem = new StringLiteralElement((string)value);
                goto IL_F4;
            }
            elem = null;
            Debug.Fail("Unsupported constant type");
IL_F4:
            elem.Emit(ilg, services);
        }
Example #2
0
        public static LiteralElement Create(string image, bool isHex, bool negated, IServiceProvider services)
        {
            var            comparison = StringComparison.OrdinalIgnoreCase;
            var            flag       = !isHex;
            LiteralElement Create;

            if (flag)
            {
                var realElement = RealLiteralElement.CreateFromInteger(image, services);
                var flag2       = realElement != null;
                if (flag2)
                {
                    Create = realElement;
                    return(Create);
                }
            }
            var hasUSuffix  = image.EndsWith("u", comparison) & !image.EndsWith("lu", comparison);
            var hasLSuffix  = image.EndsWith("l", comparison) & !image.EndsWith("ul", comparison);
            var hasULSuffix = image.EndsWith("ul", comparison) | image.EndsWith("lu", comparison);
            var hasSuffix   = hasUSuffix | hasLSuffix | hasULSuffix;
            var numStyles   = NumberStyles.Integer;

            if (isHex)
            {
                numStyles = NumberStyles.AllowHexSpecifier;
                image     = image.Remove(0, 2);
            }
            var flag3 = !hasSuffix;

            if (flag3)
            {
                LiteralElement constant = Int32LiteralElement.TryCreate(image, isHex, negated);
                var            flag4    = constant != null;
                if (flag4)
                {
                    Create = constant;
                }
                else
                {
                    constant = UInt32LiteralElement.TryCreate(image, numStyles);
                    var flag5 = constant != null;
                    if (flag5)
                    {
                        Create = constant;
                    }
                    else
                    {
                        constant = Int64LiteralElement.TryCreate(image, isHex, negated);
                        var flag6 = constant != null;
                        if (flag6)
                        {
                            Create = constant;
                        }
                        else
                        {
                            Create = new UInt64LiteralElement(image, numStyles);
                        }
                    }
                }
            }
            else
            {
                var flag7 = hasUSuffix;
                if (flag7)
                {
                    image = image.Remove(image.Length - 1);
                    LiteralElement constant = UInt32LiteralElement.TryCreate(image, numStyles);
                    var            flag8    = constant != null;
                    if (flag8)
                    {
                        Create = constant;
                    }
                    else
                    {
                        Create = new UInt64LiteralElement(image, numStyles);
                    }
                }
                else
                {
                    var flag9 = hasLSuffix;
                    if (flag9)
                    {
                        image = image.Remove(image.Length - 1);
                        LiteralElement constant = Int64LiteralElement.TryCreate(image, isHex, negated);
                        var            flag10   = constant != null;
                        if (flag10)
                        {
                            Create = constant;
                        }
                        else
                        {
                            Create = new UInt64LiteralElement(image, numStyles);
                        }
                    }
                    else
                    {
                        Debug.Assert(hasULSuffix, "expecting ul suffix");
                        image  = image.Remove(image.Length - 2);
                        Create = new UInt64LiteralElement(image, numStyles);
                    }
                }
            }
            return(Create);
        }