Exemplo n.º 1
0
		public override ExpressionNode VisitMethodInvocationExpression(MethodInvocationExpression expression)
		{
			base.VisitMethodInvocationExpression(expression);

			bool isReflectionBinding = expression.Method is ReflectionMethodBinding;
			if (!isReflectionBinding)
			{
				_ilEmitContext.AddParameter(expression, expression.Method, typeof (MethodBinding));
				object[] args = new object[expression.Arguments.Length];
				_ilEmitContext.AddParameter(expression, args, typeof(object[]));
			}

			return expression;
		}
Exemplo n.º 2
0
		public override AstElement Clone(Dictionary<AstElement, AstElement> alreadyClonedElements)
		{
			MethodInvocationExpression result = new MethodInvocationExpression();

			result.Target = (ExpressionNode)_target.Clone(alreadyClonedElements);
			result.Name = _name;
			result.NameSourceRange = _nameSourceRange;
			
			result._arguments = new ExpressionNode[_arguments.Length];
			for (int i = 0; i < _arguments.Length; i++)
				result._arguments[i] = (ExpressionNode)_arguments[i].Clone(alreadyClonedElements);
			
			result._method = _method;
			
			return result;
		}
Exemplo n.º 3
0
		private static bool VisitMethodInvocationExpression(MethodInvocationExpression node1, MethodInvocationExpression node2)
		{
			if (node2 == null)
				return false;
			
			if (node1.Arguments.Length != node2.Arguments.Length ||
				node1.Name != node2.Name)
				return false;

			if (!Visit(node1.Target, node2.Target))
				return false;

			for (int i = 0; i < node1.Arguments.Length; i++)
				if (!Visit(node1.Arguments[i], node2.Arguments[i]))
					return false;

			return true;
		}
Exemplo n.º 4
0
		public override ExpressionNode VisitMethodInvocationExpression(MethodInvocationExpression expression)
		{
			bool parenthesesNeeded =
				expression.Target is OperatorExpression ||
				expression.Target is CaseExpression ||
				expression.Target is IsNullExpression;

			if (parenthesesNeeded)
				_writer.Write("(");

			Visit(expression.Target);

			if (parenthesesNeeded)
				_writer.Write(")");

			_writer.Write(".");
			_writer.WriteIdentifier(expression.Name);
			_writer.Write("(");

			for (int i = 0; i < expression.Arguments.Length; i++)
			{
				if (i > 0)
					_writer.Write(",");

				Visit(expression.Arguments[i]);
			}

			_writer.Write(")");

			return expression;
		}
Exemplo n.º 5
0
        private ExpressionNode ParseMemberExpression()
        {
            ExpressionNode leftSide = ParsePrimaryExpression();

            while (_token.Id == TokenId.Dot)
            {
                NextToken();

                _rangeRecorder.Begin();
                Identifier memberName = ParseIdentifier();
                SourceRange memberNameSourceRange = _rangeRecorder.End();

                if (_token.Id != TokenId.LeftParentheses)
                {
                    PropertyAccessExpression result = new PropertyAccessExpression();
                    result.Target = leftSide;
                    result.Name = memberName;
                    result.NameSourceRange = memberNameSourceRange;
                    leftSide = result;
                }
                else
                {
                    ExpressionNode[] arguments = ParseExpressionList();
                    MethodInvocationExpression result = new MethodInvocationExpression();
                    result.Target = leftSide;
                    result.Name = memberName;
                    result.NameSourceRange = memberNameSourceRange;
                    result.Arguments = arguments;
                    leftSide = result;
                }
            }

            return leftSide;
        }
Exemplo n.º 6
0
		public virtual ExpressionNode VisitMethodInvocationExpression(MethodInvocationExpression expression)
		{
			expression.Target = VisitExpression(expression.Target);
			for (int i = 0; i < expression.Arguments.Length; i++)
				expression.Arguments[i] = VisitExpression(expression.Arguments[i]);
			return expression;
		}
Exemplo n.º 7
0
		public override ExpressionNode VisitMethodInvocationExpression(MethodInvocationExpression expression)
		{
			Visit(expression.Target);
			
			// Since arguments with null values don't answer the question whether the whole method invocation
			// is null we must not visit the arguments.			
			return expression;
		}
Exemplo n.º 8
0
        public override ExpressionNode VisitMethodInvocationExpression(MethodInvocationExpression expression)
        {
            // First the target and all parameters must be resolved.

            base.VisitMethodInvocationExpression(expression);

            // If the type of the target could not be resolved we cannot resolve the method.

            if (expression.Target.ExpressionType == null)
                return expression;

            // If the type of any argument could not be resolved we cannot resolve the method.

            foreach (ExpressionNode argument in expression.Arguments)
            {
                if (argument.ExpressionType == null)
                    return expression;
            }

            // Build argument type array.

            Type[] argumentTypes = new Type[expression.Arguments.Length];
            for (int i = 0; i < argumentTypes.Length; i++)
                argumentTypes[i] = expression.Arguments[i].ExpressionType;

            expression.Method = ResolveMethod(expression.Target.ExpressionType, expression.Name, argumentTypes);

            if (expression.Method == null)
            {
                ErrorReporter.UndeclaredMethod(expression.NameSourceRange, expression.Target.ExpressionType, expression.Name, argumentTypes);
            }
            else
            {
                // Convert all arguments if necessary

                Type[] parameterTypes = expression.Method.GetParameterTypes();

                for (int i = 0; i < expression.Arguments.Length; i++)
                    expression.Arguments[i] = Binder.ConvertExpressionIfRequired(expression.Arguments[i], parameterTypes[i]);
            }

            return expression;
        }
Exemplo n.º 9
0
		public override ExpressionNode VisitMethodInvocationExpression(MethodInvocationExpression expression)
		{
			ReflectionMethodBinding reflectionMethodBinding = expression.Method as ReflectionMethodBinding;
			if (reflectionMethodBinding != null)
			{
				EmitCall(reflectionMethodBinding.Method, expression.Target, expression.Arguments);
				_ilEmitContext.ILGenerator.EmitCall(OpCodes.Call, _unifyNullsMethod, null);
			}
			else
			{
				int instanceArgIndex = DeclareLocal();
				int[] argLocalIndexes = new int[expression.Arguments.Length];

				for (int i = 0; i < expression.Arguments.Length; i++)
					argLocalIndexes[i] = DeclareLocal();

				Visit(expression.Target);
				_ilEmitContext.ILGenerator.Emit(OpCodes.Stloc, instanceArgIndex);

				for (int i = 0; i < expression.Arguments.Length; i++)
				{
					Visit(expression.Arguments[i]);
					_ilEmitContext.ILGenerator.Emit(OpCodes.Stloc, argLocalIndexes[i]);
				}

				Label loadNullLabel = _ilEmitContext.ILGenerator.DefineLabel();
				Label finishLabel = _ilEmitContext.ILGenerator.DefineLabel();

				_ilEmitContext.ILGenerator.Emit(OpCodes.Ldloc, instanceArgIndex);
				_ilEmitContext.ILGenerator.Emit(OpCodes.Brfalse, loadNullLabel);

				for (int i = 0; i < expression.Arguments.Length; i++)
				{
					_ilEmitContext.ILGenerator.Emit(OpCodes.Ldloc, argLocalIndexes[i]);
					_ilEmitContext.ILGenerator.Emit(OpCodes.Brfalse, loadNullLabel);
				}

				ILParameterDeclaration customMethodBinding = _ilEmitContext.GetParameters(expression)[0];
				ILParameterDeclaration argsArray = _ilEmitContext.GetParameters(expression)[1];

				int argsArrayIndex = DeclareLocal();
				EmitLoadParameter(argsArray);
				_ilEmitContext.ILGenerator.Emit(OpCodes.Stloc, argsArrayIndex);

				for (int i = 0; i < expression.Arguments.Length; i++)
				{
					_ilEmitContext.ILGenerator.Emit(OpCodes.Ldloc, argsArrayIndex);
					_ilEmitContext.ILGenerator.Emit(OpCodes.Ldc_I4, i);
					_ilEmitContext.ILGenerator.Emit(OpCodes.Ldloc, argLocalIndexes[i]);
					_ilEmitContext.ILGenerator.Emit(OpCodes.Stelem_Ref);
				}

				EmitLoadParameter(customMethodBinding);
				EmitThisArgumentPointer(typeof(MethodBinding));
				_ilEmitContext.ILGenerator.Emit(OpCodes.Ldloc, instanceArgIndex);
				_ilEmitContext.ILGenerator.Emit(OpCodes.Ldloc, argsArrayIndex);
				_ilEmitContext.ILGenerator.EmitCall(OpCodes.Callvirt, _methodBindingInvokeMethod, null);
				_ilEmitContext.ILGenerator.EmitCall(OpCodes.Call, _unifyNullsMethod, null);
				_ilEmitContext.ILGenerator.Emit(OpCodes.Br, finishLabel);

				_ilEmitContext.ILGenerator.MarkLabel(loadNullLabel);
				_ilEmitContext.ILGenerator.Emit(OpCodes.Ldnull);

				_ilEmitContext.ILGenerator.MarkLabel(finishLabel);
			}

			return expression;
		}
Exemplo n.º 10
0
		public override ExpressionNode VisitMethodInvocationExpression(MethodInvocationExpression expression)
		{
			base.VisitMethodInvocationExpression(expression);

			// Constant folding must not be applied if the method is non-deterministic.
			if (!expression.Method.IsDeterministic)
				return expression;
			
			// Check if target is a constant or even null.
			
			ConstantExpression targetAsConstant = expression.Target as ConstantExpression;
			
			if (targetAsConstant == null)
				return expression;
			
			if (targetAsConstant.IsNullValue)
                return LiteralExpression.FromTypedNull(expression.ExpressionType);
			
			// Check if all arguments are constants or at least one argument
			// is null.

			ConstantExpression[] constantArguments = new ConstantExpression[expression.Arguments.Length];
			bool allArgumentsAreConstants = true;

			for (int i = 0; i < constantArguments.Length; i++)
			{
				constantArguments[i] = expression.Arguments[i] as ConstantExpression;

				if (constantArguments[i] == null)
				{
					// Ok, one argument is not a constant
					// But don't stop: If an argument is null we can still calculate the result!
					allArgumentsAreConstants = false;
				}
				else if (constantArguments[i].IsNullValue)
				{
					// We found a null. That means the invocation will also yield null.
                    return LiteralExpression.FromTypedNull(expression.ExpressionType);
				}
			}

			if (allArgumentsAreConstants)
			{
				try
				{
                    return LiteralExpression.FromTypedValue(expression.GetValue(), expression.ExpressionType);
				}
				catch (RuntimeException ex)
				{
					_errorReporter.CannotFoldConstants(ex);
				}
			}

			return expression;
		}
Exemplo n.º 11
0
        public override ExpressionNode VisitMethodInvocationExpression(MethodInvocationExpression expression)
        {
            ReflectionMethodBinding reflectionMethodBinding = expression.Method as ReflectionMethodBinding;

            if (reflectionMethodBinding != null)
            {
                EmitCall(reflectionMethodBinding.Method, expression.Target, expression.Arguments);
                _ilEmitContext.ILGenerator.EmitCall(OpCodes.Call, _unifyNullsMethod, null);
            }
            else
            {
                int   instanceArgIndex = DeclareLocal();
                int[] argLocalIndexes  = new int[expression.Arguments.Length];

                for (int i = 0; i < expression.Arguments.Length; i++)
                {
                    argLocalIndexes[i] = DeclareLocal();
                }

                Visit(expression.Target);
                _ilEmitContext.ILGenerator.Emit(OpCodes.Stloc, instanceArgIndex);

                for (int i = 0; i < expression.Arguments.Length; i++)
                {
                    Visit(expression.Arguments[i]);
                    _ilEmitContext.ILGenerator.Emit(OpCodes.Stloc, argLocalIndexes[i]);
                }

                Label loadNullLabel = _ilEmitContext.ILGenerator.DefineLabel();
                Label finishLabel   = _ilEmitContext.ILGenerator.DefineLabel();

                _ilEmitContext.ILGenerator.Emit(OpCodes.Ldloc, instanceArgIndex);
                _ilEmitContext.ILGenerator.Emit(OpCodes.Brfalse, loadNullLabel);

                for (int i = 0; i < expression.Arguments.Length; i++)
                {
                    _ilEmitContext.ILGenerator.Emit(OpCodes.Ldloc, argLocalIndexes[i]);
                    _ilEmitContext.ILGenerator.Emit(OpCodes.Brfalse, loadNullLabel);
                }

                ILParameterDeclaration customMethodBinding = _ilEmitContext.GetParameters(expression)[0];
                ILParameterDeclaration argsArray           = _ilEmitContext.GetParameters(expression)[1];

                int argsArrayIndex = DeclareLocal();
                EmitLoadParameter(argsArray);
                _ilEmitContext.ILGenerator.Emit(OpCodes.Stloc, argsArrayIndex);

                for (int i = 0; i < expression.Arguments.Length; i++)
                {
                    _ilEmitContext.ILGenerator.Emit(OpCodes.Ldloc, argsArrayIndex);
                    _ilEmitContext.ILGenerator.Emit(OpCodes.Ldc_I4, i);
                    _ilEmitContext.ILGenerator.Emit(OpCodes.Ldloc, argLocalIndexes[i]);
                    _ilEmitContext.ILGenerator.Emit(OpCodes.Stelem_Ref);
                }

                EmitLoadParameter(customMethodBinding);
                EmitThisArgumentPointer(typeof(MethodBinding));
                _ilEmitContext.ILGenerator.Emit(OpCodes.Ldloc, instanceArgIndex);
                _ilEmitContext.ILGenerator.Emit(OpCodes.Ldloc, argsArrayIndex);
                _ilEmitContext.ILGenerator.EmitCall(OpCodes.Callvirt, _methodBindingInvokeMethod, null);
                _ilEmitContext.ILGenerator.EmitCall(OpCodes.Call, _unifyNullsMethod, null);
                _ilEmitContext.ILGenerator.Emit(OpCodes.Br, finishLabel);

                _ilEmitContext.ILGenerator.MarkLabel(loadNullLabel);
                _ilEmitContext.ILGenerator.Emit(OpCodes.Ldnull);

                _ilEmitContext.ILGenerator.MarkLabel(finishLabel);
            }

            return(expression);
        }
Exemplo n.º 12
0
		public override ExpressionNode VisitMethodInvocationExpression(MethodInvocationExpression expression)
		{
			_xmlWriter.WriteStartElement("functionInvocationExpression");
			_xmlWriter.WriteAttributeString("identifier", expression.Name.ToSource());
			WriteTypeAttribute(expression.ExpressionType);

			WriteAstNode("target", expression.Target);

			for (int i = 0; i < expression.Arguments.Length; i++)
			{
				_xmlWriter.WriteStartElement("argument");
				_xmlWriter.WriteAttributeString("index", XmlConvert.ToString(i));
				Visit(expression.Arguments[i]);
				_xmlWriter.WriteEndElement();
			}

			_xmlWriter.WriteEndElement();

			return expression;
		}