Пример #1
0
        public static void Reset(bool full_flag)
        {
            CSharpParser.yacc_verbose_flag = 0;
            Location.Reset();

            if (!full_flag)
            {
                return;
            }

            RootContext.Reset(full_flag);
            TypeManager.Reset();
            ReferenceContainer.Reset();
            PointerContainer.Reset();
            Parameter.Reset();

            Unary.Reset();
            UnaryMutator.Reset();
            Binary.Reset();
            ConstantFold.Reset();
            CastFromDecimal.Reset();
            StringConcat.Reset();

            NamespaceEntry.Reset();
            Attribute.Reset();
            AnonymousTypeClass.Reset();
            AnonymousMethodBody.Reset();
            AnonymousMethodStorey.Reset();
            SymbolWriter.Reset();
            Switch.Reset();
            Linq.QueryBlock.TransparentParameter.Reset();
            Convert.Reset();
            TypeInfo.Reset();
        }
Пример #2
0
        public BuiltinTypes()
        {
            Object    = new BuiltinTypeSpec(MemberKind.Class, "System", "Object", BuiltinTypeSpec.Type.Object);
            ValueType = new BuiltinTypeSpec(MemberKind.Class, "System", "ValueType", BuiltinTypeSpec.Type.ValueType);
            Attribute = new BuiltinTypeSpec(MemberKind.Class, "System", "Attribute", BuiltinTypeSpec.Type.Attribute);

            Int    = new BuiltinTypeSpec(MemberKind.Struct, "System", "Int32", BuiltinTypeSpec.Type.Int);
            Long   = new BuiltinTypeSpec(MemberKind.Struct, "System", "Int64", BuiltinTypeSpec.Type.Long);
            UInt   = new BuiltinTypeSpec(MemberKind.Struct, "System", "UInt32", BuiltinTypeSpec.Type.UInt);
            ULong  = new BuiltinTypeSpec(MemberKind.Struct, "System", "UInt64", BuiltinTypeSpec.Type.ULong);
            Byte   = new BuiltinTypeSpec(MemberKind.Struct, "System", "Byte", BuiltinTypeSpec.Type.Byte);
            SByte  = new BuiltinTypeSpec(MemberKind.Struct, "System", "SByte", BuiltinTypeSpec.Type.SByte);
            Short  = new BuiltinTypeSpec(MemberKind.Struct, "System", "Int16", BuiltinTypeSpec.Type.Short);
            UShort = new BuiltinTypeSpec(MemberKind.Struct, "System", "UInt16", BuiltinTypeSpec.Type.UShort);

            IEnumerator = new BuiltinTypeSpec(MemberKind.Interface, "System.Collections", "IEnumerator", BuiltinTypeSpec.Type.IEnumerator);
            IEnumerable = new BuiltinTypeSpec(MemberKind.Interface, "System.Collections", "IEnumerable", BuiltinTypeSpec.Type.IEnumerable);
            IDisposable = new BuiltinTypeSpec(MemberKind.Interface, "System", "IDisposable", BuiltinTypeSpec.Type.IDisposable);

            Char    = new BuiltinTypeSpec(MemberKind.Struct, "System", "Char", BuiltinTypeSpec.Type.Char);
            String  = new BuiltinTypeSpec(MemberKind.Class, "System", "String", BuiltinTypeSpec.Type.String);
            Float   = new BuiltinTypeSpec(MemberKind.Struct, "System", "Single", BuiltinTypeSpec.Type.Float);
            Double  = new BuiltinTypeSpec(MemberKind.Struct, "System", "Double", BuiltinTypeSpec.Type.Double);
            Decimal = new BuiltinTypeSpec(MemberKind.Struct, "System", "Decimal", BuiltinTypeSpec.Type.Decimal);
            Bool    = new BuiltinTypeSpec(MemberKind.Struct, "System", "Boolean", BuiltinTypeSpec.Type.Bool);
            IntPtr  = new BuiltinTypeSpec(MemberKind.Struct, "System", "IntPtr", BuiltinTypeSpec.Type.IntPtr);
            UIntPtr = new BuiltinTypeSpec(MemberKind.Struct, "System", "UIntPtr", BuiltinTypeSpec.Type.UIntPtr);

            MulticastDelegate = new BuiltinTypeSpec(MemberKind.Class, "System", "MulticastDelegate", BuiltinTypeSpec.Type.MulticastDelegate);
            Delegate          = new BuiltinTypeSpec(MemberKind.Class, "System", "Delegate", BuiltinTypeSpec.Type.Delegate);
            Enum               = new BuiltinTypeSpec(MemberKind.Class, "System", "Enum", BuiltinTypeSpec.Type.Enum);
            Array              = new BuiltinTypeSpec(MemberKind.Class, "System", "Array", BuiltinTypeSpec.Type.Array);
            Void               = new BuiltinTypeSpec(MemberKind.Void, "System", "Void", BuiltinTypeSpec.Type.Other);
            Type               = new BuiltinTypeSpec(MemberKind.Class, "System", "Type", BuiltinTypeSpec.Type.Type);
            Exception          = new BuiltinTypeSpec(MemberKind.Class, "System", "Exception", BuiltinTypeSpec.Type.Exception);
            RuntimeFieldHandle = new BuiltinTypeSpec(MemberKind.Struct, "System", "RuntimeFieldHandle", BuiltinTypeSpec.Type.Other);
            RuntimeTypeHandle  = new BuiltinTypeSpec(MemberKind.Struct, "System", "RuntimeTypeHandle", BuiltinTypeSpec.Type.Other);

            // TODO: Maybe I should promote it to different kind for faster compares
            Dynamic = new BuiltinTypeSpec("dynamic", BuiltinTypeSpec.Type.Dynamic);

            OperatorsBinaryStandard = Binary.CreateStandardOperatorsTable(this);
            OperatorsBinaryEquality = Binary.CreateEqualityOperatorsTable(this);
            OperatorsBinaryUnsafe   = Binary.CreatePointerOperatorsTable(this);
            OperatorsUnary          = Unary.CreatePredefinedOperatorsTable(this);
            OperatorsUnaryMutator   = UnaryMutator.CreatePredefinedOperatorsTable(this);

            BinaryPromotionsTypes = ConstantFold.CreateBinaryPromotionsTypes(this);
            SwitchUserTypes       = Switch.CreateSwitchUserTypes(this);

            types = new BuiltinTypeSpec[] {
                Object, ValueType, Attribute,
                Int, UInt, Long, ULong, Float, Double, Char, Short, Decimal, Bool, SByte, Byte, UShort, String,
                Enum, Delegate, MulticastDelegate, Void, Array, Type, IEnumerator, IEnumerable, IDisposable,
                IntPtr, UIntPtr, RuntimeFieldHandle, RuntimeTypeHandle, Exception
            };
        }
Пример #3
0
        // <summary>
        //   This routine will attempt to simplify the unary expression when the
        //   argument is a constant.
        // </summary>
        private ConstantExpression TryReduceConstant(ParseContext ec, ConstantExpression e)
        {
            if (e is EmptyConstantCastExpression)
            {
                return(TryReduceConstant(ec, ((EmptyConstantCastExpression)e).Child));
            }

            if (e is SideEffectConstantExpression)
            {
                ConstantExpression r = TryReduceConstant(ec, ((SideEffectConstantExpression)e).ConstantValue);
                return(r == null ? null : new SideEffectConstantExpression(r, e, r.Span));
            }

            var exprType = e.Type;

            switch (Operator)
            {
            case MSAst.ExpressionType.UnaryPlus:
                // Unary numeric promotions
                if (exprType == TypeManager.CoreTypes.Byte)
                {
                    return(new ConstantExpression <int>(((byte)e.Value), e.Span));
                }
                if (exprType == TypeManager.CoreTypes.SByte)
                {
                    return(new ConstantExpression <int>(((sbyte)e.Value), e.Span));
                }
                if (exprType == TypeManager.CoreTypes.Int16)
                {
                    return(new ConstantExpression <int>(((short)e.Value), e.Span));
                }
                if (exprType == TypeManager.CoreTypes.UInt16)
                {
                    return(new ConstantExpression <int>(((ushort)e.Value), e.Span));
                }
                if (exprType == TypeManager.CoreTypes.Char)
                {
                    return(new ConstantExpression <int>(((char)e.Value), e.Span));
                }

                // Predefined operators
                if (exprType == TypeManager.CoreTypes.UInt32 || exprType == TypeManager.CoreTypes.UInt32 ||
                    exprType == TypeManager.CoreTypes.Int64 || exprType == TypeManager.CoreTypes.UInt64 ||
                    exprType == TypeManager.CoreTypes.Single || exprType == TypeManager.CoreTypes.Double ||
                    exprType == TypeManager.CoreTypes.Decimal)
                {
                    return(e);
                }

                return(null);

            case MSAst.ExpressionType.Negate:
                // Unary numeric promotions
                if (exprType == TypeManager.CoreTypes.Byte)
                {
                    return(new ConstantExpression <int>(-((byte)e.Value), e.Span));
                }
                if (exprType == TypeManager.CoreTypes.SByte)
                {
                    return(new ConstantExpression <int>(-((sbyte)e.Value), e.Span));
                }
                if (exprType == TypeManager.CoreTypes.Int16)
                {
                    return(new ConstantExpression <int>(-((short)e.Value), e.Span));
                }
                if (exprType == TypeManager.CoreTypes.UInt16)
                {
                    return(new ConstantExpression <int>(-((ushort)e.Value), e.Span));
                }
                if (exprType == TypeManager.CoreTypes.Char)
                {
                    return(new ConstantExpression <int>(-((char)e.Value), e.Span));
                }

                // Predefined operators
                if (exprType == TypeManager.CoreTypes.UInt32)
                {
                    var value = ((int)e.Value);
                    if (value == int.MinValue)
                    {
                        if (ec.ConstantCheckState)
                        {
                            ConstantFold.Error_CompileTimeOverflow(ec, Span);
                            return(null);
                        }
                        return(e);
                    }
                    return(new ConstantExpression <int>(-value, e.Span));
                }
                if (exprType == TypeManager.CoreTypes.Int64)
                {
                    var value = (long)e.Value;
                    if (value == long.MinValue)
                    {
                        if (ec.ConstantCheckState)
                        {
                            ConstantFold.Error_CompileTimeOverflow(ec, Span);
                            return(null);
                        }
                        return(e);
                    }
                    return(new ConstantExpression <long>(-value, e.Span));
                }

                if (exprType == TypeManager.CoreTypes.UInt32)
                {
                    return(new ConstantExpression <long>(-((uint)e.Value), e.Span));
                }

                if (exprType == TypeManager.CoreTypes.UInt64)
                {
                    return(null);
                }

                if (exprType == TypeManager.CoreTypes.Single)
                {
                    return(new ConstantExpression <float>(-((float)e.Value), e.Span));
                }
                if (exprType == TypeManager.CoreTypes.Double)
                {
                    return(new ConstantExpression <double>(-((double)e.Value), e.Span));
                }
                if (exprType == TypeManager.CoreTypes.Decimal)
                {
                    return(new ConstantExpression <decimal>(-((decimal)e.Value), e.Span));
                }

                return(null);

            case MSAst.ExpressionType.Not:
                if (exprType != TypeManager.CoreTypes.Boolean)
                {
                    return(null);
                }

                var b = (bool)e.Value;
                return(new ConstantExpression <bool>(!b, e.Span));

            case MSAst.ExpressionType.OnesComplement:
                // Unary numeric promotions
                if (exprType == TypeManager.CoreTypes.Byte)
                {
                    return(new ConstantExpression <int>(~((byte)e.Value), e.Span));
                }
                if (exprType == TypeManager.CoreTypes.SByte)
                {
                    return(new ConstantExpression <int>(~((sbyte)e.Value), e.Span));
                }
                if (exprType == TypeManager.CoreTypes.Int16)
                {
                    return(new ConstantExpression <int>(~((short)e.Value), e.Span));
                }
                if (exprType == TypeManager.CoreTypes.UInt16)
                {
                    return(new ConstantExpression <int>(~((ushort)e.Value), e.Span));
                }
                if (exprType == TypeManager.CoreTypes.Char)
                {
                    return(new ConstantExpression <int>(~((char)e.Value), e.Span));
                }

                // Predefined operators
                if (exprType == TypeManager.CoreTypes.Int32)
                {
                    return(new ConstantExpression <int>(~((int)e.Value), e.Span));
                }
                if (exprType == TypeManager.CoreTypes.UInt32)
                {
                    return(new ConstantExpression <uint>(~((uint)e.Value), e.Span));
                }
                if (exprType == TypeManager.CoreTypes.Int64)
                {
                    return(new ConstantExpression <long>(~((long)e.Value), e.Span));
                }
                if (exprType == TypeManager.CoreTypes.UInt64)
                {
                    return(new ConstantExpression <ulong>(~((ulong)e.Value), e.Span));
                }
                if (e is EnumConstantExpression)
                {
                    e = TryReduceConstant(ec, ((EnumConstantExpression)e).Child);
                    if (e != null)
                    {
                        e = new EnumConstantExpression(e, exprType);
                    }
                    return(e);
                }
                return(null);
            }
            throw new Exception("Can not constant fold: " + Operator);
        }