Exemplo n.º 1
0
            public SwitchBlock(Operand expression)
            {
                this.expression = expression;

                Type exprType = expression.Type;

                if (Array.IndexOf(validTypes, exprType) != -1)
                {
                    govType = exprType;
                }
                else if (exprType.IsEnum)
                {
                    govType = Enum.GetUnderlyingType(exprType);
                }
                else
                {
                    // if a single implicit coversion from expression to one of the valid types exists, it's ok
                    foreach (Type t in validTypes)
                    {
                        Conversion tmp = Conversion.GetImplicit(expression, t, false);
                        if (tmp.IsValid)
                        {
                            if (conv == null)
                            {
                                conv    = tmp;
                                govType = t;
                            }
                            else
                            {
                                throw new AmbiguousMatchException(Properties.Messages.ErrAmbiguousSwitchExpression);
                            }
                        }
                    }
                }
            }
Exemplo n.º 2
0
        public static ApplicableFunction ValidateCandidate(IMemberInfo candidate, Operand[] args)
        {
            Type[] cTypes = candidate.ParameterTypes;

            if (cTypes.Length == args.Length)
            {
                Conversion[] conversions = new Conversion[args.Length];

                for (int i = 0; i < cTypes.Length; i++)
                {
                    conversions[i] = Conversion.GetImplicit(args[i], cTypes[i], false);
                    if (!conversions[i].IsValid)
                    {
                        return(null);
                    }
                }

                return(new ApplicableFunction(candidate, cTypes, cTypes, Operand.GetTypes(args), conversions));
            }

            if (candidate.IsParameterArray && args.Length >= cTypes.Length - 1)
            {
                Type[] expandedTypes = new Type[args.Length];
                Array.Copy(cTypes, expandedTypes, cTypes.Length - 1);
                Type varType = cTypes[cTypes.Length - 1].GetElementType();

                for (int i = cTypes.Length - 1; i < expandedTypes.Length; i++)
                {
                    expandedTypes[i] = varType;
                }

                Conversion[] conversions = new Conversion[args.Length];

                for (int i = 0; i < expandedTypes.Length; i++)
                {
                    conversions[i] = Conversion.GetImplicit(args[i], expandedTypes[i], false);
                    if (!conversions[i].IsValid)
                    {
                        return(null);
                    }
                }

                return(new ApplicableFunction(candidate, cTypes, expandedTypes, Operand.GetTypes(args), conversions));
            }

            return(null);
        }
Exemplo n.º 3
0
        static Better GetBetterConversion(Type from, Type left, Type right)
        {
            if (left == right)
            {
                return(Better.Neither);
            }
            if (from == left)
            {
                return(Better.Left);
            }
            if (from == right)
            {
                return(Better.Right);
            }

            Conversion lrConv = Conversion.GetImplicit(left, right, false);
            Conversion rlConv = Conversion.GetImplicit(right, left, false);

            if (lrConv.IsValid && !rlConv.IsValid)
            {
                return(Better.Left);
            }
            if (rlConv.IsValid && !lrConv.IsValid)
            {
                return(Better.Right);
            }

            if (BetterSign(left, right))
            {
                return(Better.Left);
            }
            if (BetterSign(right, left))
            {
                return(Better.Right);
            }

            return(Better.Neither);
        }
Exemplo n.º 4
0
        internal void Convert(Operand op, Type to, bool allowExplicit)
        {
            Conversion conv = allowExplicit ? Conversion.GetExplicit(op, to, false) : Conversion.GetImplicit(op, to, false);

            conv.Emit(this, (object)op == null ? null : op.Type, to);
        }