public MemberDescription(TypeDescription declaringType, ConstructorInfo constructor)
        {
            if (declaringType == null)
            {
                throw new ArgumentNullException("declaringType");
            }
            if (constructor == null)
            {
                throw new ArgumentNullException("constructor");
            }

            this.Name                  = constructor.Name;
            this.DeclaringType         = declaringType;
            this.ResultType            = declaringType;
            this.member                = constructor;
            this.parameters            = constructor.GetParameters();
            this.parametersByName      = this.parameters.ToDictionary(GetParameterName, StringComparer.Ordinal);
            this.returnParameter       = null;
            this.hashCode              = constructor.GetHashCode();
            this.GenericArgumentsCount = constructor.IsGenericMethod ? constructor.GetGenericArguments().Length : 0;

            this.IsConstructor = true;
            this.IsStatic      = constructor.IsStatic;
        }
        public MemberDescription(TypeDescription declaringType, FieldInfo field)
        {
            if (declaringType == null)
            {
                throw new ArgumentNullException("declaringType");
            }
            if (field == null)
            {
                throw new ArgumentNullException("field");
            }

            this.member        = field;
            this.hashCode      = field.GetHashCode();
            this.Name          = field.Name;
            this.DeclaringType = declaringType;
            this.ResultType    = field.FieldType;
            var constantValue = (field.Attributes & (FieldAttributes.HasDefault | FieldAttributes.Literal)) != 0 ?
#if NETSTANDARD
                                (field.IsStatic ? field.GetValue(null) : null) :
#else
                                field.GetRawConstantValue() :
#endif
                                null;

            if (declaringType.IsEnum && constantValue != null)
            {
                this.ConstantValueExpression = Expression.Constant(Enum.ToObject(declaringType, constantValue), declaringType);
            }
            else
            {
                this.ConstantValueExpression = constantValue != null?Expression.Constant(constantValue) : null;
            }

            this.IsStatic          = field.IsStatic;
            this.IsPropertyOrField = true;
        }
Exemple #3
0
        public static bool TryBind(SyntaxTreeNode node, BindingContext bindingContext, TypeDescription expectedType, out Expression boundExpression, out Exception bindingError)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (bindingContext == null)
            {
                throw new ArgumentNullException("bindingContext");
            }
            if (expectedType == null)
            {
                throw new ArgumentNullException("expectedType");
            }

            boundExpression = null;
            bindingError    = null;

            var target             = default(Expression);
            var arguments          = node.GetArguments(throwOnError: false);
            var methodName         = node.GetMethodName(throwOnError: true);
            var useNullPropagation = node.GetUseNullPropagation(throwOnError: false);

            var methodMember = default(MemberDescription);

            if (bindingContext.TryResolveMember(methodName, out methodMember))
            {
                if (methodMember.IsMethod == false)
                {
                    bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_CALLMEMBERISNOTMETHOD, methodMember.Name, methodMember.DeclaringType), node);
                    return(false);
                }

                var targetNode = node.GetExpression(throwOnError: true);

                if (AnyBinder.TryBind(targetNode, bindingContext, TypeDescription.ObjectType, out target, out bindingError) == false)
                {
                    return(false);
                }

                float methodQuality;
                if (methodMember.TryMakeCall(target, arguments, bindingContext, out boundExpression, out methodQuality) == false)
                {
                    bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETOBINDMETHOD, methodMember.Name, target.Type, arguments.Count), node);
                    return(false);
                }

                return(true);
            }


            var methodRef = default(TypeReference);

            if (BindingContext.TryGetMethodReference(methodName, out methodRef) == false)
            {
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETORESOLVENAME, methodName), node);
                return(false);
            }

            var targetType = default(Type);

            if (TryBindTarget(node, bindingContext, out target, out targetType, out bindingError) == false)
            {
                return(false);
            }

            var isStatic = target == null;
            var selectedMethodQuality = MemberDescription.QUALITY_INCOMPATIBLE;
            var hasGenericParameters  = methodRef.IsGenericType;
            var genericArguments      = default(Type[]);

            if (hasGenericParameters)
            {
                genericArguments = new Type[methodRef.TypeArguments.Count];
                for (var i = 0; i < genericArguments.Length; i++)
                {
                    var typeArgument = methodRef.TypeArguments[i];
                    if (bindingContext.TryResolveType(typeArgument, out genericArguments[i]) == false)
                    {
                        bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETORESOLVETYPE, typeArgument), node);
                        return(false);
                    }
                }
            }

            var targetTypeDescription = TypeDescription.GetTypeDescription(targetType);
            var foundMethod           = default(MethodInfo);

            foreach (var memberDescription in targetTypeDescription.GetMembers(methodRef.Name))
            {
                if (memberDescription.IsMethod == false)
                {
                    continue;
                }

                var methodDescription = memberDescription;
                var method            = (MethodInfo)memberDescription;

                foundMethod = foundMethod ?? method;

                if (method.IsStatic != isStatic || method.IsGenericMethod != hasGenericParameters)
                {
                    continue;
                }

                if (hasGenericParameters && memberDescription.GenericArgumentsCount != methodRef.TypeArguments.Count)
                {
                    continue;
                }

                if (hasGenericParameters)
                {
                    try
                    {
                        methodDescription = methodDescription.MakeGenericMethod(genericArguments);
                        method            = methodDescription;
                    }
                    catch (ArgumentException exception)
                    {
                        bindingError = exception;
                        continue;                         /* An element of typeArguments does not satisfy the constraints specified for the corresponding type parameter of the current generic method definition. */
                    }
                }


                var methodQuality        = 0.0f;
                var methodCallExpression = default(Expression);
                if (methodDescription.TryMakeCall(target, arguments, bindingContext, out methodCallExpression, out methodQuality) == false)
                {
                    continue;
                }

                if (float.IsNaN(methodQuality) || methodQuality <= selectedMethodQuality)
                {
                    continue;
                }

                boundExpression       = methodCallExpression;
                selectedMethodQuality = methodQuality;

                if (Math.Abs(methodQuality - MemberDescription.QUALITY_EXACT_MATCH) < float.Epsilon)
                {
                    break;                     // best match
                }
            }

            if (bindingError != null)
            {
                return(false);
            }

            if (boundExpression == null)
            {
                if (foundMethod != null)
                {
                    bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETOBINDMETHOD, methodRef.Name, targetType, arguments.Count), node);
                }
                else
                {
                    bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETOBINDCALL, methodRef.Name, targetType, arguments.Count), node);
                }
                return(false);
            }

            if (useNullPropagation && target == null)
            {
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETOAPPLYNULLCONDITIONALOPERATORONTYPEREF, targetType));
                return(false);
            }

            if (useNullPropagation && targetTypeDescription.CanBeNull)
            {
                bindingContext.RegisterNullPropagationTarget(target);
            }

            if (targetTypeDescription.IsAssignableFrom(typeof(Type)) &&
                bindingContext.IsKnownType(typeof(Type)) == false &&
                (bindingContext.IsKnownType(targetType) == false || methodRef.Name.Equals("InvokeMember", StringComparison.Ordinal)))
            {
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_RESTRICTED_MEMBER_INVOCATION, methodName, targetType, typeof(ITypeResolver)), node);
                return(false);
            }

            return(true);
        }
        public static bool TryBind(SyntaxTreeNode node, BindingContext bindingContext, TypeDescription expectedType, out Expression boundExpression, out Exception bindingError)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (bindingContext == null)
            {
                throw new ArgumentNullException("bindingContext");
            }
            if (expectedType == null)
            {
                throw new ArgumentNullException("expectedType");
            }

            boundExpression = null;
            bindingError    = null;

            var newNode       = node.GetNewExpression(throwOnError: true);
            var newExpression = default(Expression);

            if (AnyBinder.TryBind(newNode, bindingContext, TypeDescription.ObjectType, out newExpression, out bindingError) == false ||
                newExpression is NewExpression == false)
            {
                if (bindingError == null)
                {
                    bindingError = new ExpressionParserException(Properties.Resources.EXCEPTION_BIND_FAILEDTOBINDNEWEXPRESSION, node);
                }
                return(false);
            }

            var bindings = default(MemberBinding[]);

            if (TryGetBindings(node, bindingContext, out bindings, out bindingError) == false)
            {
                return(false);
            }

            boundExpression = Expression.MemberInit((NewExpression)newExpression, bindings);
            return(true);
        }
        public static bool TryBind(SyntaxTreeNode node, BindingContext bindingContext, TypeDescription expectedType, out Expression boundExpression, out Exception bindingError)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (bindingContext == null)
            {
                throw new ArgumentNullException("bindingContext");
            }
            if (expectedType == null)
            {
                throw new ArgumentNullException("expectedType");
            }

            boundExpression = null;
            bindingError    = null;

            // try get type of lambda from node
            var lambdaTypeName = node.GetTypeName(throwOnError: false);
            var lambdaType     = default(Type);

            if (lambdaTypeName != null)
            {
                if (bindingContext.TryResolveType(lambdaTypeName, out lambdaType) == false)
                {
                    bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETORESOLVETYPE, lambdaTypeName), node);
                    return(false);
                }
                else
                {
                    expectedType = TypeDescription.GetTypeDescription(lambdaType);
                }
            }

            if (expectedType.HasGenericParameters || !expectedType.IsDelegate)
            {
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_VALIDDELEGATETYPEISEXPECTED, expectedType.ToString()));
                return(false);
            }

            var expressionType     = node.GetExpressionType(throwOnError: true);
            var bodyNode           = node.GetExpression(throwOnError: true);
            var argumentsTree      = node.GetArguments(throwOnError: false);
            var lambdaInvokeMethod = expectedType.GetMembers(Constants.DELEGATE_INVOKE_NAME).FirstOrDefault(m => m.IsMethod && !m.IsStatic);

            if (lambdaInvokeMethod == null)
            {
                bindingError = new MissingMethodException(string.Format(Resources.EXCEPTION_BIND_MISSINGMETHOD, expectedType.ToString(), Constants.DELEGATE_INVOKE_NAME));
                return(false);
            }

            if (lambdaInvokeMethod.GetParametersCount() != argumentsTree.Count)
            {
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_INVALIDLAMBDAARGUMENTS, expectedType));
                return(false);
            }

            var argumentNames = new string[argumentsTree.Count];

            for (var i = 0; i < argumentNames.Length; i++)
            {
                var argumentNameTree     = default(SyntaxTreeNode);
                var argumentNameTreeType = default(string);
                if (argumentsTree.TryGetValue(i, out argumentNameTree) == false ||
                    argumentNameTree == null ||
                    (argumentNameTreeType = argumentNameTree.GetExpressionType(throwOnError: true)) == null ||
                    (argumentNameTreeType == Constants.EXPRESSION_TYPE_PROPERTY_OR_FIELD ||
                     argumentNameTreeType == Constants.EXPRESSION_TYPE_MEMBER_RESOLVE ||
                     argumentNameTreeType == Constants.EXPRESSION_TYPE_PARAMETER) == false)
                {
                    bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_MISSINGATTRONNODE, Constants.EXPRESSION_ATTRIBUTE, expressionType), node);
                    return(false);
                }
                argumentNames[i] = argumentNameTree.GetMemberName(throwOnError: true);
            }

            var lambdaParameters = new ParameterExpression[argumentsTree.Count];

            for (var i = 0; i < argumentsTree.Count; i++)
            {
                lambdaParameters[i] = Expression.Parameter(lambdaInvokeMethod.GetParameter(i).ParameterType, argumentNames[i]);
            }

            var currentParameters = bindingContext.Parameters;
            var newParameters     = new List <ParameterExpression>(lambdaParameters.Length + currentParameters.Count);

            // add all lambda's parameters
            newParameters.AddRange(lambdaParameters);
            // add closure parameters
            foreach (var parameterExpr in currentParameters)
            {
                if (Array.IndexOf(argumentNames, parameterExpr.Name) < 0)
                {
                    newParameters.Add(parameterExpr);
                }
            }

            var nestedBindingContext = bindingContext.CreateNestedContext(newParameters.AsReadOnly(), lambdaInvokeMethod.ResultType);
            var body = default(Expression);

            if (AnyBinder.TryBindInNewScope(bodyNode, nestedBindingContext, TypeDescription.GetTypeDescription(lambdaInvokeMethod.ResultType), out body, out bindingError) == false)
            {
                return(false);
            }

            Debug.Assert(body != null, "body != null");

            boundExpression = Expression.Lambda(expectedType, body, lambdaParameters);
            return(true);
        }
Exemple #6
0
        public static bool TryBind(SyntaxTreeNode node, BindingContext bindingContext, TypeDescription expectedType, out Expression boundExpression, out Exception bindingError)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (bindingContext == null)
            {
                throw new ArgumentNullException("bindingContext");
            }
            if (expectedType == null)
            {
                throw new ArgumentNullException("expectedType");
            }

            boundExpression = null;
            bindingError    = null;

            var valueObj = node.GetValue(throwOnError: true);
            var typeName = node.GetTypeName(throwOnError: true);
            var type     = default(Type);

            if (bindingContext.TryResolveType(typeName, out type) == false)
            {
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETORESOLVETYPE, typeName), node);
                return(false);
            }

            var value = ChangeType(valueObj, type);

            boundExpression = Expression.Constant(value);
            return(true);
        }
Exemple #7
0
        public static bool TryBind(SyntaxTreeNode node, BindingContext bindingContext, TypeDescription expectedType, out Expression boundExpression, out Exception bindingError)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (bindingContext == null)
            {
                throw new ArgumentNullException("bindingContext");
            }
            if (expectedType == null)
            {
                throw new ArgumentNullException("expectedType");
            }

            boundExpression = null;
            bindingError    = null;

            var expressionType = node.GetExpressionType(throwOnError: true);
            var operandNode    = node.GetExpression(throwOnError: true);
            var operand        = default(Expression);
            var methodName     = node.GetMethodName(throwOnError: false);
            var methodMember   = default(MemberDescription);

            if (AnyBinder.TryBindInNewScope(operandNode, bindingContext, TypeDescription.ObjectType, out operand, out bindingError) == false)
            {
                return(false);
            }

            if (methodName != null)
            {
                bindingContext.TryResolveMember(methodName, out methodMember);
            }

            Debug.Assert(operand != null, "operand != null");

            switch (expressionType)
            {
            case Constants.EXPRESSION_TYPE_NEGATE:
                if (ExpressionUtils.TryPromoteUnaryOperation(ref operand, ExpressionType.Negate, out boundExpression) == false)
                {
                    // fixing b_u_g in mono expression compiler: Negate on float or double = exception
                    if (operand.Type == typeof(double) || operand.Type == typeof(float))
                    {
                        boundExpression = Expression.Multiply(operand, operand.Type == typeof(float) ? ExpressionUtils.NegativeSingle : ExpressionUtils.NegativeDouble);
                    }
                    else
                    {
                        boundExpression = Expression.Negate(operand, methodMember);
                    }
                }
                break;

            case Constants.EXPRESSION_TYPE_NEGATE_CHECKED:
                if (ExpressionUtils.TryPromoteUnaryOperation(ref operand, ExpressionType.NegateChecked, out boundExpression) == false)
                {
                    // fixing b_u_g in mono expression compiler: Negate on float or double = exception
                    if (operand.Type == typeof(double) || operand.Type == typeof(float))
                    {
                        boundExpression = Expression.Multiply(operand, operand.Type == typeof(float) ? ExpressionUtils.NegativeSingle : ExpressionUtils.NegativeDouble);
                    }
                    else
                    {
                        boundExpression = Expression.NegateChecked(operand, methodMember);
                    }
                }
                break;

            case Constants.EXPRESSION_TYPE_COMPLEMENT:
            case Constants.EXPRESSION_TYPE_NOT:
                if (ExpressionUtils.TryPromoteUnaryOperation(ref operand, ExpressionType.Not, out boundExpression) == false)
                {
                    boundExpression = Expression.Not(operand, methodMember);
                }
                break;

            case Constants.EXPRESSION_TYPE_UNARY_PLUS:
                if (ExpressionUtils.TryPromoteUnaryOperation(ref operand, ExpressionType.UnaryPlus, out boundExpression) == false)
                {
                    boundExpression = Expression.UnaryPlus(operand, methodMember);
                }
                break;

            case Constants.EXPRESSION_TYPE_ARRAY_LENGTH:
                boundExpression = Expression.ArrayLength(operand);
                break;

            default:
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNKNOWNEXPRTYPE, expressionType), node);
                return(false);
            }
            return(true);
        }
        public static bool TryBind(SyntaxTreeNode node, BindingContext bindingContext, TypeDescription expectedType, out Expression boundExpression, out Exception bindingError)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (bindingContext == null)
            {
                throw new ArgumentNullException("bindingContext");
            }
            if (expectedType == null)
            {
                throw new ArgumentNullException("expectedType");
            }


            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            var arguments  = node.GetArguments(throwOnError: false);
            var methodName = node.GetMethodName(throwOnError: false);

            if (methodName != null)
            {
                return(TryBindToMethod(node, methodName, arguments, bindingContext, expectedType, out boundExpression, out bindingError));
            }
            else
            {
                var typeName = node.GetTypeName(throwOnError: true);
                return(TryBindToType(node, typeName, arguments, bindingContext, expectedType, out boundExpression, out bindingError));
            }
        }
        public bool TryMakeCall(Expression target, ArgumentsTree argumentsTree, BindingContext bindingContext, out Expression expression, out float expressionQuality)
        {
            if (argumentsTree == null)
            {
                throw new ArgumentNullException("argumentsTree");
            }
            if (bindingContext == null)
            {
                throw new ArgumentNullException("bindingContext");
            }
            if (!this.IsStatic && !this.IsConstructor && target == null)
            {
                throw new ArgumentNullException("target");
            }

            expression        = null;
            expressionQuality = QUALITY_INCOMPATIBLE;

            if (this.parameters == null)             // not a method, constructor, indexer
            {
                return(false);
            }

            // check argument count
            if (argumentsTree.Count > this.parameters.Length)
            {
                return(false);                // not all arguments are bound to parameters
            }
            var requiredParametersCount = this.parameters.Length - this.parameters.Count(p => p.IsOptional);

            if (argumentsTree.Count < requiredParametersCount)
            {
                return(false);                // not all required parameters has values
            }
            // bind arguments
            var parametersQuality = 0.0f;
            var arguments         = default(Expression[]);

            foreach (var argumentName in argumentsTree.Keys)
            {
                var parameter      = default(ParameterInfo);
                var parameterIndex = 0;
                if (HasDigitsOnly(argumentName))
                {
                    parameterIndex = int.Parse(argumentName, Constants.DefaultFormatProvider);
                    if (parameterIndex >= this.parameters.Length)
                    {
                        return(false);                        // position out of range
                    }
                    parameter = this.parameters[parameterIndex];

                    if (argumentsTree.ContainsKey(parameter.Name))
                    {
                        return(false);                        // positional intersects named
                    }
                }
                else
                {
                    if (this.parametersByName.TryGetValue(argumentName, out parameter) == false)
                    {
                        return(false);                        // parameter is not found
                    }
                    parameterIndex = parameter.Position;
                }

                var expectedType = TypeDescription.GetTypeDescription(parameter.ParameterType);
                var argValue     = default(Expression);
                var bindingError = default(Exception);
                if (AnyBinder.TryBindInNewScope(argumentsTree[argumentName], bindingContext, expectedType, out argValue, out bindingError) == false)
                {
                    return(false);
                }

                Debug.Assert(argValue != null, "argValue != null");

                var quality = 0.0f;
                if (ExpressionUtils.TryMorphType(ref argValue, expectedType, out quality) == false || quality <= 0)
                {
                    return(false);                   // failed to bind parameter
                }
                parametersQuality += quality;        // casted
                if (arguments == null)
                {
                    arguments = new Expression[this.parameters.Length];
                }
                arguments[parameterIndex] = argValue;
            }

            if (this.parameters.Length > 0)
            {
                if (arguments == null)
                {
                    arguments = new Expression[this.parameters.Length];
                }
                for (var i = 0; i < arguments.Length; i++)
                {
                    if (arguments[i] != null)
                    {
                        continue;
                    }
                    var parameter = this.parameters[i];
                    if (parameter.IsOptional == false)
                    {
                        return(false);                        // missing required parameter
                    }
                    var typeDescription = TypeDescription.GetTypeDescription(parameter.ParameterType);
                    arguments[i]       = typeDescription.DefaultExpression;
                    parametersQuality += TypeConversion.QUALITY_SAME_TYPE;
                }

                expressionQuality = parametersQuality / this.parameters.Length;
            }
            else
            {
                expressionQuality = QUALITY_EXACT_MATCH;
            }


            if (this.member is MethodInfo)
            {
                if (this.IsStatic)
                {
                    expression = Expression.Call(this, arguments);
                }
                else if (this.Name == Constants.DELEGATE_INVOKE_NAME && this.DeclaringType.IsDelegate)
                {
                    expression = Expression.Invoke(target, arguments);
                }
                else
                {
                    expression = Expression.Call(target, this, arguments);
                }
                return(true);
            }
            else if (this.member is ConstructorInfo)
            {
                expression = Expression.New((ConstructorInfo)this.member, arguments);
                return(true);
            }
            else if (this.member is PropertyInfo)
            {
                if (this.IsStatic)
                {
                    expression = Expression.Call(this, arguments);
                }
                else
                {
                    expression = Expression.Call(target, this, arguments);
                }
                return(true);
            }
            expressionQuality = QUALITY_INCOMPATIBLE;
            return(false);
        }
Exemple #10
0
        public static bool TryBind(SyntaxTreeNode node, BindingContext bindingContext, TypeDescription expectedType, out Expression boundExpression, out Exception bindingError)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (bindingContext == null)
            {
                throw new ArgumentNullException("bindingContext");
            }
            if (expectedType == null)
            {
                throw new ArgumentNullException("expectedType");
            }

            boundExpression = null;
            bindingError    = null;

            var expressionNode = node.GetExpression(throwOnError: true);

            return(AnyBinder.TryBindInNewScope(expressionNode, bindingContext, expectedType, out boundExpression, out bindingError));
        }
Exemple #11
0
        public static bool TryBindInNewScope(SyntaxTreeNode node, BindingContext bindingContext, TypeDescription expectedType, out Expression boundExpression, out Exception bindingError)
        {
            bindingContext = bindingContext.CreateNestedContext();
            var result = TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError);

            bindingContext.CompleteNullPropagation(ref boundExpression);
            return(result);
        }
Exemple #12
0
        public static bool TryBind(SyntaxTreeNode node, BindingContext bindingContext, TypeDescription expectedType, out Expression boundExpression, out Exception bindingError)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (bindingContext == null)
            {
                throw new ArgumentNullException("bindingContext");
            }
            if (expectedType == null)
            {
                throw new ArgumentNullException("expectedType");
            }

            try
            {
                var expressionType = node.GetExpressionType(throwOnError: true);
                switch (expressionType)
                {
                case Constants.EXPRESSION_TYPE_PROPERTY_OR_FIELD:
                    return(PropertyOrFieldBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_CONSTANT:
                    return(ConstantBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_CALL:
                    return(CallBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case "Enclose":
                case Constants.EXPRESSION_TYPE_UNCHECKED_SCOPE:
                case Constants.EXPRESSION_TYPE_CHECKED_SCOPE:
                case Constants.EXPRESSION_TYPE_GROUP:
                    return(GroupBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_INVOKE:
                    return(InvokeBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_LAMBDA:
                    return(LambdaBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_INDEX:
                    return(IndexBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_TYPEOF:
                    return(TypeOfBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_CONVERT:
                case Constants.EXPRESSION_TYPE_CONVERTCHECKED:
                case Constants.EXPRESSION_TYPE_TYPEIS:
                case Constants.EXPRESSION_TYPE_TYPEAS:
                    return(TypeBinaryBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_DEFAULT:
                    return(DefaultBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_NEW:
                    return(NewBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_NEW_ARRAY_BOUNDS:
                    return(NewArrayBoundsBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_ADD:
                case Constants.EXPRESSION_TYPE_ADD_CHECKED:
                case Constants.EXPRESSION_TYPE_SUBTRACT:
                case Constants.EXPRESSION_TYPE_SUBTRACT_CHECKED:
                case Constants.EXPRESSION_TYPE_LEFTSHIFT:
                case Constants.EXPRESSION_TYPE_RIGHTSHIFT:
                case Constants.EXPRESSION_TYPE_GREATERTHAN:
                case Constants.EXPRESSION_TYPE_GREATERTHAN_OR_EQUAL:
                case Constants.EXPRESSION_TYPE_LESSTHAN:
                case Constants.EXPRESSION_TYPE_LESSTHAN_OR_EQUAL:
                case Constants.EXPRESSION_TYPE_POWER:
                case Constants.EXPRESSION_TYPE_DIVIDE:
                case Constants.EXPRESSION_TYPE_MULTIPLY:
                case Constants.EXPRESSION_TYPE_MULTIPLY_CHECKED:
                case Constants.EXPRESSION_TYPE_MODULO:
                case Constants.EXPRESSION_TYPE_EQUAL:
                case Constants.EXPRESSION_TYPE_NOTEQUAL:
                case Constants.EXPRESSION_TYPE_AND:
                case Constants.EXPRESSION_TYPE_OR:
                case Constants.EXPRESSION_TYPE_EXCLUSIVEOR:
                case Constants.EXPRESSION_TYPE_ANDALSO:
                case Constants.EXPRESSION_TYPE_ORELSE:
                case Constants.EXPRESSION_TYPE_COALESCE:
                    return(BinaryBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_NEGATE:
                case Constants.EXPRESSION_TYPE_NEGATE_CHECKED:
                case Constants.EXPRESSION_TYPE_COMPLEMENT:
                case Constants.EXPRESSION_TYPE_NOT:
                case Constants.EXPRESSION_TYPE_UNARYPLUS:
                    return(UnaryBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_CONDITION:
                    return(ConditionBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                default:
                    boundExpression = null;
                    bindingError    = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNKNOWNEXPRTYPE, expressionType), node);
                    return(false);
                }
            }
            catch (ExpressionParserException error)
            {
                boundExpression = null;
                bindingError    = error;
                return(false);
            }
            catch (Exception error)
            {
                boundExpression = null;
                bindingError    = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_FAILEDTOBIND, node.GetExpressionType(throwOnError: false) ?? "<unknown>", error.Message), error, node);
                return(false);
            }
        }
Exemple #13
0
        public static bool TryBind(SyntaxTreeNode node, BindingContext bindingContext, TypeDescription expectedType, out Expression boundExpression, out Exception bindingError)
        {
            bindingError = null;

            boundExpression = null;
            bindingError    = null;

            var operandNode = node.GetExpression(throwOnError: true);
            var operand     = default(Expression);

            if (AnyBinder.TryBindInNewScope(operandNode, bindingContext, TypeDescription.ObjectType, out operand, out bindingError) == false)
            {
                return(false);
            }

            boundExpression = Expression.Quote(operand);
            return(true);
        }
Exemple #14
0
        public static bool TryBind(SyntaxTreeNode node, BindingContext bindingContext, TypeDescription expectedType, out Expression boundExpression, out Exception bindingError)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (bindingContext == null)
            {
                throw new ArgumentNullException("bindingContext");
            }
            if (expectedType == null)
            {
                throw new ArgumentNullException("expectedType");
            }

            boundExpression = null;
            bindingError    = null;

            var expressionType = node.GetExpressionType(throwOnError: true);
            var targetNode     = node.GetExpression(throwOnError: true);
            var typeName       = node.GetTypeName(throwOnError: true);
            var type           = default(Type);

            if (bindingContext.TryResolveType(typeName, out type) == false)
            {
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETORESOLVETYPE, typeName), node);
                return(false);
            }

            var target = default(Expression);

            if (AnyBinder.TryBindInNewScope(targetNode, bindingContext, TypeDescription.ObjectType, out target, out bindingError) == false)
            {
                return(false);
            }

            Debug.Assert(target != null, "target != null");

            switch (expressionType)
            {
            case Constants.EXPRESSION_TYPE_TYPE_IS:
                boundExpression = Expression.TypeIs(target, type);
                break;

            case Constants.EXPRESSION_TYPE_TYPE_AS:
                boundExpression = Expression.TypeAs(target, type);
                break;

            case Constants.EXPRESSION_TYPE_CONVERT:
                boundExpression = Expression.Convert(target, type);
                break;

            case Constants.EXPRESSION_TYPE_CONVERT_CHECKED:
                boundExpression = Expression.ConvertChecked(target, type);
                break;

            default:
                boundExpression = null;
                bindingError    = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNKNOWNEXPRTYPE, expressionType), node);
                return(false);
            }
            return(true);
        }
        public static bool TryBind(SyntaxTreeNode node, BindingContext bindingContext, TypeDescription expectedType, out Expression boundExpression, out Exception bindingError)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (bindingContext == null)
            {
                throw new ArgumentNullException("bindingContext");
            }
            if (expectedType == null)
            {
                throw new ArgumentNullException("expectedType");
            }

            bindingError    = null;
            boundExpression = null;

            var test           = node.GetTestExpression(throwOnError: true);
            var ifTrue         = node.GetIfTrueExpression(throwOnError: true);
            var ifFalse        = node.GetIfFalseExpression(throwOnError: true);
            var testExpression = default(Expression);
            var ifTrueBranch   = default(Expression);
            var ifFalseBranch  = default(Expression);

            if (AnyBinder.TryBindInNewScope(test, bindingContext, TypeDescription.GetTypeDescription(typeof(bool)), out testExpression, out bindingError) == false)
            {
                return(false);
            }
            if (AnyBinder.TryBindInNewScope(ifTrue, bindingContext, TypeDescription.ObjectType, out ifTrueBranch, out bindingError) == false)
            {
                return(false);
            }
            if (AnyBinder.TryBindInNewScope(ifFalse, bindingContext, TypeDescription.ObjectType, out ifFalseBranch, out bindingError) == false)
            {
                return(false);
            }

            Debug.Assert(testExpression != null, "testExpression != null");
            Debug.Assert(ifTrueBranch != null, "ifTrueBranch != null");
            Debug.Assert(ifFalseBranch != null, "ifFalseBranch != null");

            if (ExpressionUtils.TryPromoteBinaryOperation(ref ifTrueBranch, ref ifFalseBranch, ExpressionType.Conditional, out boundExpression) == false)
            {
                if (ifTrueBranch.Type != ifFalseBranch.Type)
                {
                    float quality;
                    ExpressionUtils.TryMorphType(ref ifTrueBranch, ifFalseBranch.Type, out quality);
                }

                boundExpression = Expression.Condition(testExpression, ifTrueBranch, ifFalseBranch);
            }
            return(true);
        }
        private static bool TryGetBinding(object bindingNode, BindingContext bindingContext, out MemberBinding memberBinding, out Exception bindingError)
        {
            bindingError  = null;
            memberBinding = null;
            var bindingNodeTree = bindingNode as SyntaxTreeNode;

            if (bindingNodeTree == null)
            {
                return(false);
            }

            var bindingType = (string)bindingNodeTree.GetTypeName(throwOnError: true);
            var memberObj   = bindingNodeTree.GetMember(throwOnError: true);
            var member      = default(MemberDescription);

            if (bindingContext.TryResolveMember(memberObj, out member) == false)
            {
                return(false);
            }
            var memberValueType = TypeDescription.GetTypeDescription(member.ResultType);

            // ReSharper disable once SwitchStatementMissingSomeCases
            switch (bindingType)
            {
            case "Assignment":
                var expressionNode = bindingNodeTree.GetExpression(throwOnError: true);
                var expression     = default(Expression);
                if (AnyBinder.TryBindInNewScope(expressionNode, bindingContext, memberValueType, out expression, out bindingError) == false)
                {
                    return(false);                            // file to bind member's value
                }

                if (member.IsMethod)
                {
                    memberBinding = Expression.Bind((MethodInfo)member, expression);
                }
                else
                {
                    memberBinding = Expression.Bind((MemberInfo)member, expression);
                }
                return(true);

            case "MemberBinding":
                var bindings = default(MemberBinding[]);
                if (TryGetBindings(bindingNodeTree, bindingContext, out bindings, out bindingError) == false)
                {
                    return(false);                            // failed to resolve bindings
                }
                if (member.IsMethod)
                {
                    memberBinding = Expression.MemberBind((MethodInfo)member, bindings);
                }
                else
                {
                    memberBinding = Expression.MemberBind((MemberInfo)member, bindings);
                }
                return(true);

            case "ListBinding":
                var initializers = default(ElementInit[]);
                if (ListInitBinder.TryGetListInitializers(bindingNodeTree, bindingContext, out initializers, out bindingError) == false)
                {
                    return(false);                            // failed to resolve list initializers
                }
                if (member.IsMethod)
                {
                    memberBinding = Expression.ListBind((MethodInfo)member, initializers);
                }
                else
                {
                    memberBinding = Expression.ListBind((MemberInfo)member, initializers);
                }
                return(true);
            }

            return(false);
        }
Exemple #17
0
        public static bool TryBind(SyntaxTreeNode node, BindingContext bindingContext, TypeDescription expectedType, out Expression boundExpression, out Exception bindingError)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (bindingContext == null)
            {
                throw new ArgumentNullException("bindingContext");
            }
            if (expectedType == null)
            {
                throw new ArgumentNullException("expectedType");
            }

            boundExpression = null;
            bindingError    = null;

            var expressionType = node.GetExpressionType(throwOnError: true);
            var left           = node.GetLeftExpression(throwOnError: true);
            var right          = node.GetRightExpression(throwOnError: true);
            var leftOperand    = default(Expression);
            var rightOperand   = default(Expression);

            if (AnyBinder.TryBindInNewScope(left, bindingContext, TypeDescription.ObjectType, out leftOperand, out bindingError) == false)
            {
                return(false);
            }
            if (AnyBinder.TryBindInNewScope(right, bindingContext, TypeDescription.ObjectType, out rightOperand, out bindingError) == false)
            {
                return(false);
            }

            Debug.Assert(leftOperand != null, "leftOperand != null");
            Debug.Assert(rightOperand != null, "rightOperand != null");

            switch (expressionType)
            {
            case Constants.EXPRESSION_TYPE_ADD:
                if (leftOperand.Type == typeof(string) || rightOperand.Type == typeof(string))
                {
                    boundExpression = Expression.Call
                                      (
                        StringConcat.Method,
                        Expression.Convert(leftOperand, typeof(object)),
                        Expression.Convert(rightOperand, typeof(object))
                                      );
                    break;
                }
                else
                {
                    if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.Add, out boundExpression) == false)
                    {
                        boundExpression = Expression.Add(leftOperand, rightOperand);
                    }
                    break;
                }

            case Constants.EXPRESSION_TYPE_ADD_CHECKED:
                if (leftOperand.Type == typeof(string) || rightOperand.Type == typeof(string))
                {
                    boundExpression = Expression.Call
                                      (
                        StringConcat.Method,
                        Expression.Convert(leftOperand, typeof(object)),
                        Expression.Convert(rightOperand, typeof(object))
                                      );
                    break;
                }
                else
                {
                    if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.AddChecked, out boundExpression) == false)
                    {
                        boundExpression = Expression.AddChecked(leftOperand, rightOperand);
                    }
                    break;
                }

            case Constants.EXPRESSION_TYPE_SUBTRACT:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.Subtract, out boundExpression) == false)
                {
                    boundExpression = Expression.Subtract(leftOperand, rightOperand);
                }
                break;

            case Constants.EXPRESSION_TYPE_SUBTRACT_CHECKED:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.SubtractChecked, out boundExpression) == false)
                {
                    boundExpression = Expression.SubtractChecked(leftOperand, rightOperand);
                }
                break;

            case Constants.EXPRESSION_TYPE_LEFTSHIFT:
                ExpressionUtils.TryPromoteUnaryOperation(ref leftOperand, ExpressionType.LeftShift, out boundExpression);
                ExpressionUtils.TryPromoteUnaryOperation(ref rightOperand, ExpressionType.LeftShift, out boundExpression);
                boundExpression = Expression.LeftShift(leftOperand, rightOperand);
                break;

            case Constants.EXPRESSION_TYPE_RIGHTSHIFT:
                ExpressionUtils.TryPromoteUnaryOperation(ref leftOperand, ExpressionType.RightShift, out boundExpression);
                ExpressionUtils.TryPromoteUnaryOperation(ref rightOperand, ExpressionType.RightShift, out boundExpression);
                boundExpression = Expression.RightShift(leftOperand, rightOperand);
                break;

            case Constants.EXPRESSION_TYPE_GREATERTHAN:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.GreaterThan, out boundExpression) == false)
                {
                    boundExpression = Expression.GreaterThan(leftOperand, rightOperand);
                }
                break;

            case Constants.EXPRESSION_TYPE_GREATERTHAN_OR_EQUAL:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.GreaterThanOrEqual, out boundExpression) == false)
                {
                    boundExpression = Expression.GreaterThanOrEqual(leftOperand, rightOperand);
                }
                break;

            case Constants.EXPRESSION_TYPE_LESSTHAN:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.LessThan, out boundExpression) == false)
                {
                    boundExpression = Expression.LessThan(leftOperand, rightOperand);
                }
                break;

            case Constants.EXPRESSION_TYPE_LESSTHAN_OR_EQUAL:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.LessThanOrEqual, out boundExpression) == false)
                {
                    boundExpression = Expression.LessThanOrEqual(leftOperand, rightOperand);
                }
                break;

            case Constants.EXPRESSION_TYPE_POWER:
                var resultType       = TypeDescription.GetTypeDescription(leftOperand.Type);
                var resultTypeUnwrap = resultType.IsNullable ? resultType.UnderlyingType : resultType;
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.Power, out boundExpression) == false)
                {
                    var operandsType      = TypeDescription.GetTypeDescription(leftOperand.Type);
                    var operandTypeUnwrap = operandsType.IsNullable ? operandsType.UnderlyingType : operandsType;
                    var promoteToNullable = resultType.IsNullable || operandsType.IsNullable;
                    if (operandTypeUnwrap != typeof(double) && leftOperand.Type == rightOperand.Type)
                    {
                        leftOperand  = Expression.ConvertChecked(leftOperand, promoteToNullable ? typeof(double?) : typeof(double));
                        rightOperand = Expression.ConvertChecked(rightOperand, promoteToNullable ? typeof(double?) : typeof(double));
                    }
                    boundExpression = Expression.Power(leftOperand, rightOperand);

                    if (resultType != typeof(double))
                    {
                        boundExpression = Expression.ConvertChecked(boundExpression, promoteToNullable ? resultTypeUnwrap.GetNullableType() : resultTypeUnwrap);
                    }
                }
                break;

            case Constants.EXPRESSION_TYPE_DIVIDE:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.Divide, out boundExpression) == false)
                {
                    boundExpression = Expression.Divide(leftOperand, rightOperand);
                }
                break;

            case Constants.EXPRESSION_TYPE_MULTIPLY:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.Multiply, out boundExpression) == false)
                {
                    boundExpression = Expression.Multiply(leftOperand, rightOperand);
                }
                break;

            case Constants.EXPRESSION_TYPE_MULTIPLY_CHECKED:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.MultiplyChecked, out boundExpression) == false)
                {
                    boundExpression = Expression.MultiplyChecked(leftOperand, rightOperand);
                }
                break;

            case Constants.EXPRESSION_TYPE_MODULO:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.Modulo, out boundExpression) == false)
                {
                    boundExpression = Expression.Modulo(leftOperand, rightOperand);
                }
                break;

            case Constants.EXPRESSION_TYPE_EQUAL:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.Equal, out boundExpression) == false)
                {
                    boundExpression = Expression.Equal(leftOperand, rightOperand);
                }
                break;

            case Constants.EXPRESSION_TYPE_NOTEQUAL:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.NotEqual, out boundExpression) == false)
                {
                    boundExpression = Expression.NotEqual(leftOperand, rightOperand);
                }
                break;

            case Constants.EXPRESSION_TYPE_AND:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.And, out boundExpression) == false)
                {
                    boundExpression = Expression.And(leftOperand, rightOperand);
                }
                break;

            case Constants.EXPRESSION_TYPE_OR:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.Or, out boundExpression) == false)
                {
                    boundExpression = Expression.Or(leftOperand, rightOperand);
                }
                break;

            case Constants.EXPRESSION_TYPE_EXCLUSIVEOR:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.ExclusiveOr, out boundExpression) == false)
                {
                    boundExpression = Expression.ExclusiveOr(leftOperand, rightOperand);
                }
                break;

            case Constants.EXPRESSION_TYPE_ANDALSO:
                boundExpression = Expression.AndAlso(leftOperand, rightOperand);
                break;

            case Constants.EXPRESSION_TYPE_ORELSE:
                boundExpression = Expression.OrElse(leftOperand, rightOperand);
                break;

            case Constants.EXPRESSION_TYPE_COALESCE:
                if (ExpressionUtils.TryPromoteBinaryOperation(ref leftOperand, ref rightOperand, ExpressionType.Coalesce, out boundExpression) == false)
                {
                    boundExpression = Expression.Coalesce(leftOperand, rightOperand);
                }

                break;

            default:
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNKNOWNEXPRTYPE, expressionType), node);
                return(false);
            }
            return(true);
        }
Exemple #18
0
        public static bool TryBind(SyntaxTreeNode node, BindingContext bindingContext, TypeDescription expectedType, out Expression boundExpression, out Exception bindingError)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (bindingContext == null)
            {
                throw new ArgumentNullException("bindingContext");
            }
            if (expectedType == null)
            {
                throw new ArgumentNullException("expectedType");
            }

            if (TryBindMethodCall(node, bindingContext, expectedType, out boundExpression, out bindingError))
            {
                return(true);
            }

            var targetNode = node.GetExpression(throwOnError: true);
            var arguments  = node.GetArguments(throwOnError: false);
            var target     = default(Expression);

            if (AnyBinder.TryBind(targetNode, bindingContext, TypeDescription.ObjectType, out target, out bindingError) == false)
            {
                return(false);
            }

            Debug.Assert(target != null, "target != null");

            var typeDescription = TypeDescription.GetTypeDescription(target.Type);

            if (typeDescription.IsDelegate == false)
            {
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETOINVOKENONDELEG, target.Type), node);
                return(false);
            }

            var methodDescription = typeDescription.GetMembers(Constants.DELEGATE_INVOKE_NAME).FirstOrDefault(m => m.IsMethod && !m.IsStatic);

            if (methodDescription == null)
            {
                throw new MissingMethodException(string.Format(Properties.Resources.EXCEPTION_BIND_MISSINGMETHOD, target.Type.FullName, Constants.DELEGATE_INVOKE_NAME));
            }

            var expressionQuality = 0.0f;

            if (methodDescription.TryMakeCall(target, arguments, bindingContext, out boundExpression, out expressionQuality) == false)
            {
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETOBINDDELEG, target.Type, methodDescription), node);
                return(false);
            }

            return(true);
        }
        private static bool TryBindToType(SyntaxTreeNode node, object typeName, ArgumentsTree arguments, BindingContext bindingContext, TypeDescription expectedType, out Expression boundExpression, out Exception bindingError)
        {
            boundExpression = null;
            bindingError    = null;

            var type = default(Type);

            if (bindingContext.TryResolveType(typeName, out type) == false)
            {
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETORESOLVETYPE, typeName), node);
                return(false);
            }

            var typeDescription = TypeDescription.GetTypeDescription(type);

            // feature: lambda building via new Func()
            var lambdaArgument = default(SyntaxTreeNode);

            if (typeDescription.IsDelegate && arguments.Count == 1 && (lambdaArgument = arguments.Values.Single()).GetExpressionType(throwOnError: true) == Constants.EXPRESSION_TYPE_LAMBDA)
            {
                return(LambdaBinder.TryBind(lambdaArgument, bindingContext, typeDescription, out boundExpression, out bindingError));
            }

            var selectedConstructorQuality = MemberDescription.QUALITY_INCOMPATIBLE;

            foreach (var constructorDescription in typeDescription.Constructors)
            {
                var constructorQuality = MemberDescription.QUALITY_INCOMPATIBLE;
                var constructorCall    = default(Expression);
                if (constructorDescription.TryMakeCall(null, arguments, bindingContext, out constructorCall, out constructorQuality) == false)
                {
                    continue;
                }

                if (float.IsNaN(constructorQuality) || constructorQuality <= selectedConstructorQuality)
                {
                    continue;
                }

                boundExpression            = constructorCall;
                selectedConstructorQuality = constructorQuality;

                if (Math.Abs(constructorQuality - MemberDescription.QUALITY_EXACT_MATCH) < float.Epsilon)
                {
                    break;                     // best match
                }
            }

            if (boundExpression == null)
            {
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETOBINDCONSTRUCTOR, type), node);
                return(false);
            }

            return(true);
        }
Exemple #20
0
        public static bool TryBindMethodCall(SyntaxTreeNode node, BindingContext bindingContext, TypeDescription expectedType, out Expression boundExpression, out Exception bindingError)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (bindingContext == null)
            {
                throw new ArgumentNullException("bindingContext");
            }
            if (expectedType == null)
            {
                throw new ArgumentNullException("expectedType");
            }

            // bindingError could return null from this method
            bindingError    = null;
            boundExpression = null;

            var methodNameNode     = node.GetExpression(throwOnError: true);
            var methodNameNodeType = methodNameNode.GetExpressionType(throwOnError: true);

            if (methodNameNodeType != Constants.EXPRESSION_TYPE_PROPERTY_OR_FIELD &&
                methodNameNodeType != Constants.EXPRESSION_TYPE_MEMBER_RESOLVE)
            {
                return(false);
            }
            var methodTargetNode = methodNameNode.GetExpression(throwOnError: false);
            var methodTarget     = default(Expression);

            var type          = default(Type);
            var typeReference = default(TypeReference);
            var isStatic      = false;

            if (methodTargetNode == null && bindingContext.Global != null)
            {
                methodTarget = bindingContext.Global;
                type         = methodTarget.Type;
                isStatic     = false;
            }
            else if (methodTargetNode == null)
            {
                var methodName = methodNameNode.GetMemberName(throwOnError: false);
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETORESOLVENAME, methodName ?? "<unknown>"), node);
                return(false);
            }
            else if (BindingContext.TryGetTypeReference(methodTargetNode, out typeReference) && bindingContext.TryResolveType(typeReference, out type))
            {
                isStatic = true;
            }
            else if (AnyBinder.TryBind(methodTargetNode, bindingContext, TypeDescription.ObjectType, out methodTarget, out bindingError))
            {
                Debug.Assert(methodTarget != null, "methodTarget != null");

                isStatic = false;
                type     = methodTarget.Type;
            }
            else
            {
                if (typeReference != null && bindingError == null)
                {
                    bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETORESOLVETYPE, typeReference), node);
                }
                return(false);
            }

            var methodRef = default(TypeReference);

            if (type == null || BindingContext.TryGetMethodReference(methodNameNode, out methodRef) == false)
            {
                return(false);
            }

            var typeDescription = TypeDescription.GetTypeDescription(type);

            foreach (var member in typeDescription.GetMembers(methodRef.Name))
            {
                if (member.IsMethod == false || member.IsStatic != isStatic)
                {
                    continue;
                }

                var callNode = new SyntaxTreeNode(new Dictionary <string, object>
                {
                    { Constants.EXPRESSION_ATTRIBUTE, methodTarget ?? (object)type },
                    { Constants.ARGUMENTS_ATTRIBUTE, node.GetValueOrDefault(Constants.ARGUMENTS_ATTRIBUTE, default(object)) },
                    { Constants.METHOD_ATTRIBUTE, methodRef },
                    { Constants.USE_NULL_PROPAGATION_ATTRIBUTE, methodNameNode.GetValueOrDefault(Constants.USE_NULL_PROPAGATION_ATTRIBUTE, default(object)) },
                    { Constants.EXPRESSION_POSITION, methodNameNode.GetPosition(throwOnError: false) }
                });

                return(CallBinder.TryBind(callNode, bindingContext, expectedType, out boundExpression, out bindingError));
            }

            return(false);
        }
Exemple #21
0
        public static bool TryBind(SyntaxTreeNode node, BindingContext bindingContext, TypeDescription expectedType, out Expression boundExpression, out Exception bindingError)
        {
            boundExpression = null;
            bindingError    = null;

            var useNullPropagation = node.GetUseNullPropagation(throwOnError: false);
            var arguments          = node.GetArguments(throwOnError: true);
            var targetNode         = node.GetExpression(throwOnError: true);
            var target             = default(Expression);

            if (AnyBinder.TryBind(targetNode, bindingContext, TypeDescription.ObjectType, out target, out bindingError) == false)
            {
                return(false);
            }

            Debug.Assert(target != null, "target != null");

            var targetTypeDescription = TypeDescription.GetTypeDescription(target.Type);

            if (target.Type.IsArray)
            {
                var indexType           = TypeDescription.Int32Type;
                var indexingExpressions = new Expression[arguments.Count];
                for (var i = 0; i < indexingExpressions.Length; i++)
                {
                    var argument = default(SyntaxTreeNode);
                    if (arguments.TryGetValue(i, out argument) == false)
                    {
                        bindingError = new ExpressionParserException(string.Format(Resources.EXCEPTION_BIND_MISSINGMETHODPARAMETER, i), node);
                        return(false);
                    }

                    if (AnyBinder.TryBindInNewScope(argument, bindingContext, indexType, out indexingExpressions[i], out bindingError) == false)
                    {
                        return(false);
                    }

                    Debug.Assert(indexingExpressions[i] != null, "indexingExpressions[i] != null");
                }

                try
                {
                    if (indexingExpressions.Length == 1)
                    {
                        boundExpression = Expression.ArrayIndex(target, indexingExpressions[0]);
                    }
                    else
                    {
                        boundExpression = Expression.ArrayIndex(target, indexingExpressions);
                    }
                }
                catch (Exception exception)
                {
                    bindingError = new ExpressionParserException(exception.Message, exception, node);
                    return(false);
                }
            }
            else
            {
                var selectedIndexerQuality = MemberDescription.QUALITY_INCOMPATIBLE;
                foreach (var indexer in targetTypeDescription.Indexers)
                {
                    var indexerQuality = MemberDescription.QUALITY_INCOMPATIBLE;
                    var indexerCall    = default(Expression);
                    if (indexer.TryMakeCall(target, arguments, bindingContext, out indexerCall, out indexerQuality) == false)
                    {
                        continue;
                    }
                    if (indexerQuality <= selectedIndexerQuality)
                    {
                        continue;
                    }

                    boundExpression        = indexerCall;
                    selectedIndexerQuality = indexerQuality;
                }
            }
            if (boundExpression == null)
            {
                bindingError = new ExpressionParserException(string.Format(Resources.EXCEPTION_BIND_UNABLETOBINDINDEXER, target.Type), node);
                return(false);
            }

            if (useNullPropagation && targetTypeDescription.CanBeNull)
            {
                bindingContext.RegisterNullPropagationTarget(target);
            }

            return(true);
        }
Exemple #22
0
        public bool TryResolveMember(object memberName, out MemberDescription member)
        {
            member = null;
            if (memberName is SyntaxTreeNode == false)
            {
                return(false);
            }

            var memberNode = (SyntaxTreeNode)memberName;
            var typeName   = memberNode.GetTypeName(throwOnError: false);
            var type       = default(Type);

            if (typeName == null || this.TryResolveType(typeName, out type) == false)
            {
                return(false);
            }

            var name    = memberNode.GetName(throwOnError: false);
            var nameRef = default(TypeReference);

            if (name == null || TryGetTypeReference(name, out nameRef) == false)
            {
                return(false);
            }

            var genericArguments = default(Type[]);

            if (nameRef.IsGenericType)
            {
                genericArguments = new Type[nameRef.TypeArguments.Count];
                for (var i = 0; i < genericArguments.Length; i++)
                {
                    var typeArgument = nameRef.TypeArguments[i];
                    if (this.TryResolveType(typeArgument, out genericArguments[i]) == false)
                    {
                        return(false);
                    }
                }
            }

            var typeDescription = TypeDescription.GetTypeDescription(type);
            var argumentNames   = memberNode.GetArgumentNames(throwOnError: false);
            var members         = nameRef.Name == ".ctor" ? typeDescription.Constructors : typeDescription.GetMembers(nameRef.Name);

            foreach (var declaredMember in members)
            {
                if ((declaredMember.IsMethod || declaredMember.IsConstructor) && argumentNames != null)
                {
                    var paramsCount = declaredMember.GetParametersCount();
                    if (paramsCount != argumentNames.Count)
                    {
                        continue;
                    }
                    else if (paramsCount > 0)
                    {
                        for (var i = 0; i < paramsCount; i++)
                        {
                            var parameter      = declaredMember.GetParameter(i);
                            var parameterIndex = Constants.GetIndexAsString(parameter.Position);

                            var argumentName = default(string);
                            if (argumentNames.TryGetValue(parameterIndex, out argumentName) == false)
                            {
                                break;
                            }

                            if (string.Equals(argumentName, parameter.Name, StringComparison.OrdinalIgnoreCase) == false &&
                                string.Equals(argumentName, parameterIndex, StringComparison.OrdinalIgnoreCase) == false)
                            {
                                break;
                            }

                            if (i == paramsCount - 1)
                            {
                                member = declaredMember;
                                return(TryMakeGenericMethod(ref member, genericArguments));
                            }
                        }
                    }
                    else
                    {
                        member = declaredMember;
                        return(TryMakeGenericMethod(ref member, genericArguments));
                    }
                }
                else if (nameRef.TypeArguments.Count == declaredMember.GenericArgumentsCount)
                {
                    member = declaredMember;
                    return(TryMakeGenericMethod(ref member, genericArguments));
                }
            }
            return(false);
        }
Exemple #23
0
        public static bool TryBind(SyntaxTreeNode node, BindingContext bindingContext, TypeDescription expectedType, out Expression boundExpression, out Exception bindingError)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (bindingContext == null)
            {
                throw new ArgumentNullException("bindingContext");
            }
            if (expectedType == null)
            {
                throw new ArgumentNullException("expectedType");
            }

            boundExpression = null;
            bindingError    = null;

            var typeName = node.GetTypeName(throwOnError: true);
            var type     = default(Type);

            if (bindingContext.TryResolveType(typeName, out type) == false)
            {
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETORESOLVETYPE, typeName), node);
                return(false);
            }

            var elementType         = TypeDescription.GetTypeDescription(type);
            var arguments           = node.GetArguments(throwOnError: true);
            var argumentExpressions = new Expression[arguments.Count];

            for (var i = 0; i < arguments.Count; i++)
            {
                var argument = default(SyntaxTreeNode);
                if (arguments.TryGetValue(i, out argument) == false)
                {
                    bindingError = new ExpressionParserException(Properties.Resources.EXCEPTION_BOUNDEXPR_ARGSDOESNTMATCHPARAMS, node);
                    return(false);
                }

                if (AnyBinder.TryBindInNewScope(argument, bindingContext, elementType, out argumentExpressions[i], out bindingError) == false)
                {
                    return(false);
                }
            }

            boundExpression = Expression.NewArrayInit(type, argumentExpressions);
            return(true);
        }
        public static bool TryBind(SyntaxTreeNode node, BindingContext bindingContext, TypeDescription expectedType, out Expression boundExpression, out Exception bindingError)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (bindingContext == null)
            {
                throw new ArgumentNullException("bindingContext");
            }
            if (expectedType == null)
            {
                throw new ArgumentNullException("expectedType");
            }

            boundExpression = null;
            bindingError    = null;

            var target              = default(Expression);
            var targetNode          = node.GetExpression(throwOnError: false);
            var propertyOrFieldName = node.GetPropertyOrFieldName(throwOnError: true);
            var useNullPropagation  = node.GetUseNullPropagation(throwOnError: false);

            var targetType = default(Type);
            var isStatic   = false;

            if (bindingContext.TryResolveType(targetNode, out targetType))
            {
                target   = null;
                isStatic = true;
            }
            else if (targetNode == null)
            {
                target     = bindingContext.Global;
                targetType = target != null ? target.Type : null;
                isStatic   = false;

                switch (propertyOrFieldName)
                {
                case Constants.VALUE_NULL_STRING:
                    boundExpression = ExpressionUtils.NullConstant;
                    return(true);

                case Constants.VALUE_TRUE_STRING:
                    boundExpression = ExpressionUtils.TrueConstant;
                    return(true);

                case Constants.VALUE_FALSE_STRING:
                    boundExpression = ExpressionUtils.TrueConstant;
                    return(false);

                default:
                    if (bindingContext.TryGetParameter(propertyOrFieldName, out boundExpression))
                    {
                        return(true);
                    }
                    break;
                }
            }
            else if (AnyBinder.TryBind(targetNode, bindingContext, TypeDescription.ObjectType, out target, out bindingError))
            {
                Debug.Assert(target != null, "target != null");

                targetType = target.Type;
                isStatic   = false;
            }
            else
            {
                target     = null;
                targetType = null;
            }

            if (target == null && targetType == null)
            {
                if (bindingError == null)
                {
                    bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETORESOLVENAME, propertyOrFieldName), node);
                }
                return(false);
            }

            Debug.Assert(targetType != null, "type != null");

            var targetTypeDescription = TypeDescription.GetTypeDescription(targetType);

            if (isStatic && targetTypeDescription.IsEnum)
            {
                var fieldMemberDescription = targetTypeDescription.GetMembers(propertyOrFieldName).FirstOrDefault(m => m.IsStatic);
                if (fieldMemberDescription == null)
                {
                    bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETORESOLVEMEMBERONTYPE, propertyOrFieldName, targetType), node);
                    return(false);
                }
                boundExpression = fieldMemberDescription.ConstantValueExpression;
            }
            else
            {
                foreach (var member in targetTypeDescription.GetMembers(propertyOrFieldName))
                {
                    if (member.IsStatic != isStatic || member.IsPropertyOrField == false)
                    {
                        continue;
                    }

                    if (member.TryMakeAccessor(target, out boundExpression))
                    {
                        break;
                    }
                }
            }

            if (boundExpression == null)
            {
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETORESOLVEMEMBERONTYPE, propertyOrFieldName, targetType), node);
                return(false);
            }

            if (useNullPropagation && isStatic)
            {
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETOAPPLYNULLCONDITIONALOPERATORONTYPEREF, targetType));
                return(false);
            }

            if (useNullPropagation && targetTypeDescription.CanBeNull)
            {
                bindingContext.RegisterNullPropagationTarget(target);
            }

            return(true);
        }
        private static bool TryBindToMethod(SyntaxTreeNode node, object methodName, ArgumentsTree arguments, BindingContext bindingContext, TypeDescription expectedType, out Expression boundExpression, out Exception bindingError)
        {
            boundExpression = null;
            bindingError    = null;

            var constructorDescription = default(MemberDescription);

            if (bindingContext.TryResolveMember(methodName, out constructorDescription) == false || constructorDescription.IsConstructor == false)
            {
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETOBINDCONSTRUCTOR, methodName), node);
                return(false);
            }

            var typeDescription = TypeDescription.GetTypeDescription(constructorDescription.DeclaringType);

            // feature: lambda building via new Func()
            var lambdaArgument = default(SyntaxTreeNode);

            if (typeDescription.IsDelegate && arguments.Count == 1 && (lambdaArgument = arguments.Values.Single()).GetExpressionType(throwOnError: true) == Constants.EXPRESSION_TYPE_LAMBDA)
            {
                return(LambdaBinder.TryBind(lambdaArgument, bindingContext, typeDescription, out boundExpression, out bindingError));
            }

            var constructorQuality = MemberDescription.QUALITY_INCOMPATIBLE;

            if (constructorDescription.TryMakeCall(null, arguments, bindingContext, out boundExpression, out constructorQuality))
            {
                return(true);
            }

            bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETOBINDCONSTRUCTOR, constructorDescription.DeclaringType), node);
            return(false);
        }