Beispiel #1
0
        public override LiteralExpression VisitLiteralValue(LiteralExpression expression)
        {
            ILParameterDeclaration ilParameterDeclaration = _ilEmitContext.GetParameters(expression)[0];

            EmitLoadParameter(ilParameterDeclaration);

            return(expression);
        }
Beispiel #2
0
        public override ExpressionNode VisitRowBufferEntryExpression(RowBufferEntryExpression expression)
        {
            ILParameterDeclaration rowBufferParameter = _ilEmitContext.GetParameters(expression)[0];

            EmitLoadParameter(rowBufferParameter);
            _ilEmitContext.ILGenerator.Emit(OpCodes.Castclass, typeof(object[]));
            _ilEmitContext.ILGenerator.Emit(OpCodes.Ldc_I4, expression.RowBufferIndex);
            _ilEmitContext.ILGenerator.Emit(OpCodes.Ldelem_Ref);
            return(expression);
        }
Beispiel #3
0
        public override ExpressionNode VisitParameterExpression(ParameterExpression expression)
        {
            ILParameterDeclaration parameterBinding = _ilEmitContext.GetParameters(expression)[0];

            EmitLoadParameter(parameterBinding);
            EmitThisArgumentPointer(typeof(ParameterBinding));
            _ilEmitContext.ILGenerator.EmitCall(OpCodes.Call, _parameterBindingGetValueMethod, null);

            return(expression);
        }
Beispiel #4
0
        public override ExpressionNode VisitPropertyAccessExpression(PropertyAccessExpression expression)
        {
            ReflectionPropertyBinding reflectionPropertyBinding = expression.Property as ReflectionPropertyBinding;
            ReflectionFieldBinding    reflectionFieldBinding    = expression.Property as ReflectionFieldBinding;

            if (reflectionPropertyBinding != null)
            {
                EmitCall(reflectionPropertyBinding.PropertyInfo.GetGetMethod(), expression.Target);
                _ilEmitContext.ILGenerator.EmitCall(OpCodes.Call, _unifyNullsMethod, null);
            }
            else if (reflectionFieldBinding != null)
            {
                Label finishLabel   = _ilEmitContext.ILGenerator.DefineLabel();
                Label loadNullLabel = _ilEmitContext.ILGenerator.DefineLabel();
                int   instanceIndex = DeclareLocal();

                Visit(expression.Target);
                _ilEmitContext.ILGenerator.Emit(OpCodes.Stloc, instanceIndex);
                _ilEmitContext.ILGenerator.Emit(OpCodes.Ldloc, instanceIndex);
                _ilEmitContext.ILGenerator.Emit(OpCodes.Brfalse, loadNullLabel);
                _ilEmitContext.ILGenerator.Emit(OpCodes.Ldloc, instanceIndex);
                EmitThisArgumentPointer(expression.Target.ExpressionType);
                _ilEmitContext.ILGenerator.Emit(OpCodes.Ldfld, reflectionFieldBinding.FieldInfo);
                _ilEmitContext.ILGenerator.Emit(OpCodes.Br, finishLabel);
                _ilEmitContext.ILGenerator.MarkLabel(loadNullLabel);
                _ilEmitContext.ILGenerator.Emit(OpCodes.Ldnull);
                _ilEmitContext.ILGenerator.MarkLabel(finishLabel);
            }
            else
            {
                Label finishLabel   = _ilEmitContext.ILGenerator.DefineLabel();
                Label loadNullLabel = _ilEmitContext.ILGenerator.DefineLabel();

                int argIndex = DeclareLocal();
                Visit(expression.Target);
                _ilEmitContext.ILGenerator.Emit(OpCodes.Stloc, argIndex);
                _ilEmitContext.ILGenerator.Emit(OpCodes.Ldloc, argIndex);
                _ilEmitContext.ILGenerator.Emit(OpCodes.Brfalse, loadNullLabel);

                ILParameterDeclaration customPropertyBinding = _ilEmitContext.GetParameters(expression)[0];
                EmitLoadParameter(customPropertyBinding);
                EmitThisArgumentPointer(typeof(PropertyBinding));
                _ilEmitContext.ILGenerator.Emit(OpCodes.Ldloc, argIndex);
                _ilEmitContext.ILGenerator.EmitCall(OpCodes.Callvirt, _propertyBindingGetValueMethod, 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);
        }
Beispiel #5
0
		public void AddParameter(AstNode target, object value, Type type)
		{
			ILParameterDeclaration ilParameterDeclaration = new ILParameterDeclaration();
			ilParameterDeclaration.Index = _parameters.Count;
			ilParameterDeclaration.Value = value;
			ilParameterDeclaration.Type = type;
			_parameters.Add(ilParameterDeclaration);

			List<ILParameterDeclaration> targetParameters;
			if (!_parameterDictionary.TryGetValue(target, out targetParameters))
			{
				targetParameters = new List<ILParameterDeclaration>();
				_parameterDictionary.Add(target, targetParameters);
			}

			targetParameters.Add(ilParameterDeclaration);
		}
Beispiel #6
0
        public void AddParameter(AstNode target, object value, Type type)
        {
            ILParameterDeclaration ilParameterDeclaration = new ILParameterDeclaration();

            ilParameterDeclaration.Index = _parameters.Count;
            ilParameterDeclaration.Value = value;
            ilParameterDeclaration.Type  = type;
            _parameters.Add(ilParameterDeclaration);

            List <ILParameterDeclaration> targetParameters;

            if (!_parameterDictionary.TryGetValue(target, out targetParameters))
            {
                targetParameters = new List <ILParameterDeclaration>();
                _parameterDictionary.Add(target, targetParameters);
            }

            targetParameters.Add(ilParameterDeclaration);
        }
Beispiel #7
0
		private void EmitLoadParameter(ILParameterDeclaration ilParameterDeclaration)
		{
			_ilEmitContext.ILGenerator.Emit(OpCodes.Ldarg_0);
			_ilEmitContext.ILGenerator.Emit(OpCodes.Ldc_I4, ilParameterDeclaration.Index);
			_ilEmitContext.ILGenerator.Emit(OpCodes.Ldelem_Ref);
		}
Beispiel #8
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);
        }
Beispiel #9
0
        public override ExpressionNode VisitFunctionInvocationExpression(FunctionInvocationExpression expression)
        {
            ReflectionFunctionBinding reflectionFunctionBinding = expression.Function as ReflectionFunctionBinding;

            if (reflectionFunctionBinding != null)
            {
                MethodInfo             method         = reflectionFunctionBinding.Method;
                ParameterInfo[]        parameterInfos = method.GetParameters();
                ILParameterDeclaration instance;

                if (reflectionFunctionBinding.Instance != null)
                {
                    instance = _ilEmitContext.GetParameters(expression)[0];
                }
                else
                {
                    instance = null;
                }

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

                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();

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

                if (instance != null)
                {
                    EmitLoadParameter(instance);
                    EmitThisArgumentPointer(instance.Type);
                }

                for (int i = 0; i < expression.Arguments.Length; i++)
                {
                    _ilEmitContext.ILGenerator.Emit(OpCodes.Ldloc, argLocalIndexes[i]);
                    if (parameterInfos[i].ParameterType != typeof(object))
                    {
                        EmitConversion(parameterInfos[i].ParameterType);
                    }
                }

                EmitRawCall(method, instance == null ? null : instance.Type);

                if (method.ReturnType.IsValueType)
                {
                    _ilEmitContext.ILGenerator.Emit(OpCodes.Box, method.ReturnType);
                }

                _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);
            }
            else
            {
                int[] argLocalIndexes = new int[expression.Arguments.Length];

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

                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();

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

                ILParameterDeclaration customFunctionBinding = _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(customFunctionBinding);
                EmitThisArgumentPointer(typeof(FunctionBinding));
                _ilEmitContext.ILGenerator.Emit(OpCodes.Ldloc, argsArrayIndex);
                _ilEmitContext.ILGenerator.EmitCall(OpCodes.Callvirt, _functionBindingInvokeMethod, 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);
        }
Beispiel #10
0
 private void EmitLoadParameter(ILParameterDeclaration ilParameterDeclaration)
 {
     _ilEmitContext.ILGenerator.Emit(OpCodes.Ldarg_0);
     _ilEmitContext.ILGenerator.Emit(OpCodes.Ldc_I4, ilParameterDeclaration.Index);
     _ilEmitContext.ILGenerator.Emit(OpCodes.Ldelem_Ref);
 }