コード例 #1
0
        // the sections mentioned in comments of this method are from C# specification v1.2
        public static Conversion GetImplicit(Operand op, Type to, bool onlyStandard)
        {
            Type from = Operand.GetType(op);

            if (to.Equals(from))
            {
                return(Direct.Instance);
            }

            // required for arrays created from TypeBuilder-s
            if (from != null && to.IsArray && from.IsArray)
            {
                if (to.GetArrayRank() == from.GetArrayRank())
                {
                    if (to.GetElementType().Equals(from.GetElementType()))
                    {
                        return(Direct.Instance);
                    }
                }
            }

            TypeCode tcFrom = Type.GetTypeCode(from);
            TypeCode tcTo   = Type.GetTypeCode(to);
            byte     ct     = convTable[(int)tcFrom][(int)tcTo];

            // section 6.1.2 - Implicit numeric conversions
            if ((from != null && (from.IsPrimitive || from == typeof(decimal))) && (to.IsPrimitive || to == typeof(decimal)))
            {
                if (ct <= I)
                {
                    if (from == typeof(decimal) || to == typeof(decimal))
                    {
                        // decimal is handled as user-defined conversion, but as it is a standard one, always enable UDC processing
                        onlyStandard = false;
                    }
                    else
                    {
                        return(Primitive.Instance);
                    }
                }
            }

            IntLiteral intLit = op as IntLiteral;

            // section 6.1.3 - Implicit enumeration conversions
            if (!onlyStandard && to.IsEnum && (object)intLit != null && intLit.Value == 0)
            {
                return(Primitive.Instance);
            }

            // section 6.1.4 - Implicit reference conversions
            if ((from == null || !from.IsValueType) && !to.IsValueType)
            {
                if (from == null)                 // from the null type to any reference type
                {
                    return(Direct.Instance);
                }

                if (to.IsAssignableFrom(from))                  // the rest
                {
                    return(Direct.Instance);
                }
            }

            if (from == null)                   // no other conversion from null type is possible
            {
                return(Invalid.Instance);
            }

            // section 6.1.5 - Boxing conversions
            if (from.IsValueType)
            {
                if (to.IsAssignableFrom(from))
                {
                    return(Boxing.Instance);
                }
            }

            // section 6.1.6 - Implicit constant expression conversions
            if ((object)intLit != null && from == typeof(int) && to.IsPrimitive)
            {
                int val = intLit.Value;

                switch (tcTo)
                {
                case TypeCode.SByte:
                    if (val >= sbyte.MinValue && val <= sbyte.MaxValue)
                    {
                        return(Direct.Instance);
                    }
                    break;

                case TypeCode.Byte:
                    if (val >= byte.MinValue && val <= byte.MaxValue)
                    {
                        return(Direct.Instance);
                    }
                    break;

                case TypeCode.Int16:
                    if (val >= short.MinValue && val <= short.MaxValue)
                    {
                        return(Direct.Instance);
                    }
                    break;

                case TypeCode.UInt16:
                    if (val >= ushort.MinValue && val <= ushort.MaxValue)
                    {
                        return(Direct.Instance);
                    }
                    break;

                case TypeCode.UInt32:
                    if (val >= 0)
                    {
                        return(Direct.Instance);
                    }
                    break;

                case TypeCode.UInt64:
                    if (val >= 0)
                    {
                        return(Primitive.Instance);
                    }
                    break;
                }
            }
            if (from == typeof(long))
            {
                LongLiteral longLit = op as LongLiteral;
                if ((object)longLit != null && longLit.Value > 0)
                {
                    return(Direct.Instance);
                }
            }

            // section 6.1.7 - User-defined implicit conversions (details in section 6.4.3)
            if (onlyStandard || from == typeof(object) || to == typeof(object) || from.IsInterface || to.IsInterface ||
                to.IsSubclassOf(from) || from.IsSubclassOf(to))
            {
                return(Invalid.Instance);                       // skip not-permitted conversion attempts (section 6.4.1)
            }
            List <UserDefined> candidates = null;

            FindCandidates(ref candidates, FindImplicitMethods(from, to), op, to, GetImplicit);

            if (candidates == null)
            {
                return(Invalid.Instance);
            }

            if (candidates.Count == 1)
            {
                return(candidates[0]);
            }

            return(UserDefined.FindImplicit(candidates, from, to));
        }
コード例 #2
0
ファイル: ExpTranslator.cs プロジェクト: melkordahrk/pytocs
 public CodeExpression VisitLongLiteral(LongLiteral l)
 {
     return(m.Number(l.Value));
 }
コード例 #3
0
ファイル: ExpNameDiscovery.cs プロジェクト: wheregone/pytocs
 public void VisitLongLiteral(LongLiteral l)
 {
     throw new NotImplementedException();
 }
コード例 #4
0
        ///
        /// Python's list comprehension will erase any variable used in generators.
        /// This is wrong, but we "respect" this bug here.
        ///
        //public DataType VisitGeneratorExp(GeneratorExp g)
        //{
        //    resolveList(g.generators);
        //    return new ListType(g.elt.Accept(this));
        //}

        public DataType VisitLongLiteral(LongLiteral l)
        {
            return(DataType.Int);
        }
コード例 #5
0
ファイル: ExpNameDiscovery.cs プロジェクト: uxmal/pytocs
 public void VisitLongLiteral(LongLiteral l)
 {
     throw new NotImplementedException();
 }
コード例 #6
0
        public void Visit(LongLiteral literal)
        {
            var arg = _visitStack.Peek().BoolQueryArg;

            arg.PropertyValue = new PropertyValue(literal.Value);
        }
コード例 #7
0
ファイル: ExpTranslator.cs プロジェクト: weimingtom/pytocs
 public CodeExpression VisitLongLiteral(LongLiteral l)
 {
     return(new CodePrimitiveExpression(l.Value));
 }