Beispiel #1
0
        static MethodDeclaration GetMethod(RefactoringContext context, LambdaResolveResult lambda, BlockStatement body,
                                           bool noReturnValue = false)
        {
            var method = new MethodDeclaration {
                Name = "Method"
            };

            if (noReturnValue)
            {
                method.ReturnType = new PrimitiveType("void");
            }
            else
            {
                var type = lambda.GetInferredReturnType(lambda.Parameters.Select(p => p.Type).ToArray());
                method.ReturnType = type.Name == "?" ? new PrimitiveType("void") : context.CreateShortType(type);
            }

            foreach (var param in lambda.Parameters)
            {
                method.Parameters.Add(new ParameterDeclaration(context.CreateShortType(param.Type), param.Name));
            }

            method.Body = body;
            if (lambda.IsAsync)
            {
                method.Modifiers |= Modifiers.Async;
            }

            return(method);
        }
Beispiel #2
0
        public JsNode VisitLambdaResolveResult(LambdaResolveResult res)
        {
            //var prmTypes = res.Parameters.Select(t => t.Type).ToArray();
            //var retType = res.GetInferredReturnType(prmTypes);
            //var conv = res.IsValid(prmTypes, retType, CSharpConversions.Get(Project.Compilation));
            //return Visit(conv);
            var func = new JsFunction {
                Parameters = res.Parameters.Select(t => JsIdentifier(t.Name)).ToList()
            };
            var    body = res.Body;
            JsNode body2;
            var    info = res.GetInfo();

            if (body.GetType() == typeof(ResolveResult) && body.Type.IsVoid())
            {
                var x       = res.GetType().GetProperty("BodyExpression", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(res, null);
                var astNode = (AstNode)x;
                body2 = Visit(astNode);
            }
            else
            {
                body2 = Visit(res.Body);
            }
            var currentMember = res.GetCurrentMember();
            var delType       = info.ConversionTargetType;

            if (body2 is JsExpression)
            {
                var         delMethod = delType.GetDelegateInvokeMethod();
                JsStatement st        = new JsReturnStatement {
                    Expression = (JsExpression)body2
                };
                if (delMethod != null && delMethod.ReturnType.IsVoid())
                {
                    st = new JsExpressionStatement {
                        Expression = (JsExpression)body2
                    }
                }
                ;

                func.Block = new JsBlock {
                    Statements = new List <JsStatement> {
                        st
                    }
                };
            }
            else if (body2 is JsBlock)
            {
                func.Block = (JsBlock)body2;
            }
            if (delType == null || currentMember == null)
            {
                Log.Warn(res.GetFirstNode(), "Cannot resolve delegate type / parent method");
                return(func);
            }
            else
            {
                return(CreateJsDelegateIfNeeded(func, currentMember, delType, true));
            }
        }
        static MethodDeclaration GetMethod(RefactoringContext context, LambdaResolveResult lambda, BlockStatement body,
                                           bool noReturnValue = false)
        {
            var method = new MethodDeclaration {
                Name = "Method"
            };

            if (noReturnValue)
            {
                method.ReturnType = new PrimitiveType("sub");
            }
            else
            {
                var type = lambda.ReturnType;
                method.ReturnType = type.Kind == TypeKind.Unknown ? new PrimitiveType("sub") : context.CreateShortType(type);
            }

            foreach (var param in lambda.Parameters)
            {
                method.Parameters.Add(new ParameterDeclaration(context.CreateShortType(param.Type), param.Name));
            }

            method.Body = body;
            if (lambda.IsAsync)
            {
                method.Modifiers |= Modifiers.Async;
            }

            return(method);
        }
		public override JsExpression VisitLambdaResolveResult(LambdaResolveResult rr, object data) {
			var parameters = new JsExpression[rr.Parameters.Count];
			for (int i = 0; i < rr.Parameters.Count; i++) {
				var temp = _createTemporaryVariable(rr.Parameters[i].Type);
				_allParameters[rr.Parameters[i]] = temp;
				_additionalStatements.Add(new JsVariableDeclarationStatement(temp, CompileFactoryCall("Parameter", new[] { typeof(Type), typeof(string) }, new[] { _instantiateType(rr.Parameters[i].Type), JsExpression.String(rr.Parameters[i].Name) })));
				parameters[i] = JsExpression.Identifier(temp);
			}

			var body = VisitResolveResult(rr.Body, null);
			return CompileFactoryCall("Lambda", new[] { typeof(Expression), typeof(ParameterExpression[]) }, new[] { body, JsExpression.ArrayLiteral(parameters) });
		}
Beispiel #5
0
        public static IType GetLambdaReturnType(RefactoringContext context, LambdaExpression lambda)
        {
            LambdaResolveResult rr = context.Resolve(lambda) as LambdaResolveResult;

            if (rr == null)
            {
                return(SpecialType.UnknownType);
            }
            if (rr.IsAsync)
            {
                // Unpack Task<T>
                if (rr.ReturnType.IsKnownType(KnownTypeCode.Task))
                {
                    return(context.Compilation.FindType(KnownTypeCode.Void));
                }
                else if (rr.ReturnType.IsKnownType(KnownTypeCode.TaskOfT) && rr.ReturnType.IsParameterized)
                {
                    return(rr.ReturnType.TypeArguments[0]);
                }
            }
            return(rr.ReturnType);
        }
Beispiel #6
0
        public override string VisitLambdaResolveResult(LambdaResolveResult rr, object data)
        {
            var parameters = new JRaw[rr.Parameters.Count];
            var map        = new Dictionary <string, string>();

            for (int i = 0; i < rr.Parameters.Count; i++)
            {
                var temp = this._block.GetTempVarName();
                _allParameters[rr.Parameters[i]] = temp;
                parameters[i] = new JRaw(temp);

                map.Add(temp, CompileFactoryCall("Parameter", new[] { typeof(Type), typeof(string) }, new[] { ExpressionTreeBuilder.GetTypeName(rr.Parameters[i].Type, this._emitter), this._emitter.ToJavaScript(rr.Parameters[i].Name) }));
            }

            var body   = VisitResolveResult(rr.Body, null);
            var lambda = CompileFactoryCall("Lambda", new[] { typeof(Expression), typeof(ParameterExpression[]) }, new[] { body, this._emitter.ToJavaScript(parameters) });

            if (map.Count > 0)
            {
                StringBuilder sb = new StringBuilder();

                sb.Append("(");

                foreach (var item in map)
                {
                    sb.Append(item.Key);
                    sb.Append(" = ");
                    sb.Append(item.Value);
                    sb.Append(", ");
                }

                sb.Append(lambda);
                sb.Append(")");

                return(sb.ToString());
            }

            return(lambda);
        }
 public virtual TResult VisitLambdaResolveResult(LambdaResolveResult rr, TData data)
 {
     VisitChildResolveResults(rr, data);
     return(default(TResult));
 }
Beispiel #8
0
 public string BuildExpressionTree(LambdaResolveResult lambda)
 {
     return(this.VisitLambdaResolveResult(lambda, null));
 }
Beispiel #9
0
 public virtual object VisitLambdaResolveResult(LambdaResolveResult rr, object data)
 {
     VisitChildResolveResults(rr, data);
     return(null);
 }
Beispiel #10
0
        public JNode VisitLambdaResolveResult(LambdaResolveResult res)
        {
            //var prmTypes = res.Parameters.Select(t => t.Type).ToArray();
            //var retType = res.GetInferredReturnType(prmTypes);
            //var conv = res.IsValid(prmTypes, retType, CSharpConversions.Get(Compiler.Project.Compilation));
            //return Visit(conv);
            var func = new JFunction {
                Parameters = res.Parameters.Select(t => JsIdentifier(t.Name)).ToList()
            };
            var   body = res.Body;
            JNode body2;
            var   info = res.GetInfo();

            if (body.GetType() == typeof(ResolveResult) && body.Type.IsVoid())
            {
                var x       = res.GetType().GetProperty("BodyExpression", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(res, null);
                var astNode = (AstNode)x;
                body2 = Visit(astNode);
            }
            else
            {
                body2 = Visit(res.Body);
            }


            var currentMember = res.GetCurrentMember();
            var delType       = info.ConversionTargetType;
            var delMethod     = delType.GetDelegateInvokeMethod();
            var returnType    = delMethod.ReturnType;

            if (returnType.IsVoid())
            {
                returnType = null;
            }
            if (body2 is JExpression)
            {
                JStatement st = new JReturnStatement {
                    Expression = (JExpression)body2
                };
                if (delMethod != null && delMethod.ReturnType.IsVoid())
                {
                    st = new JExpressionStatement {
                        Expression = (JExpression)body2
                    }
                }
                ;

                func.Block = new JBlock {
                    Statements = new List <JStatement> {
                        st
                    }
                };
            }
            else if (body2 is JBlock)
            {
                func.Block = (JBlock)body2;
            }
            else
            {
                throw new Exception();
            }
            var del2 = CreateDelegate(delType, res.Parameters, returnType, func.Block);

            return(del2);
        }

        JNewAnonymousClassExpression CreateDelegate(IType delType, IList <IParameter> prms, IType returnType, JBlock block)
        {
            var del2 = J.CreateDelegate(delType.JAccess(), prms.Select(t => new JParameterDeclaration {
                Name = t.Name, Type = t.Type.JAccessNonPrimitive()
            }).ToArray(), returnType.JAccessNonPrimitive(), block);

            return(del2);
        }
		public ExpressionCompileResult BuildExpressionTree(LambdaResolveResult lambda) {
			var expr = VisitLambdaResolveResult(lambda, null);
			return new ExpressionCompileResult(expr, _additionalStatements);
		}
 public object VisitLambdaResolveResult(LambdaResolveResult res)
 {
     throw new NotImplementedException();
 }