Ejemplo n.º 1
0
 public virtual void VisitAnonymousMethodExpression(IAnonymousMethodExpression value)
 {
     this.VisitType(value.DelegateType);
     this.VisitParameterDeclarationCollection(value.Parameters);
     this.VisitMethodReturnType(value.ReturnType);
     this.VisitStatement(value.Body);
 }
Ejemplo n.º 2
0
        public override IAssignableExpression VisitAnonymousMethodExpression(IAnonymousMethodExpression expr,
                                                                             IList <IStatement> body)
        {
            var lambdaName = expr.GetName();
            var lambdaBody = new KaVEList <IStatement>();

            var isCompletionTarget = expr == _marker.HandlingNode && CompletionCase.InBody == _marker.Case;

            if (isCompletionTarget)
            {
                var stmt = new ExpressionStatement {
                    Expression = new CompletionExpression()
                };
                lambdaBody.Add(stmt);
            }

            var bodyVisitor = new StatementVisitor(_nameGen, _marker);

            expr.Body.Accept(bodyVisitor, lambdaBody);

            return(new LambdaExpression
            {
                Name = lambdaName,
                Body = lambdaBody
            });
        }
Ejemplo n.º 3
0
 public virtual void VisitAnonymousMethodExpression(IAnonymousMethodExpression value)
 {
     VisitType(value.DelegateType);
     VisitParameterDeclarationCollection(value.Parameters);
     VisitMethodReturnType(value.ReturnType);
     VisitStatement(value.Body);
 }
 public virtual void VisitAnonymousMethodExpression <TParameter, TStatement>(
     IAnonymousMethodExpression <TParameter, TStatement> anonymousMethod)
     where TParameter : IAnonymousMethodParameter
     where TStatement : IStatement
 {
     Visit(anonymousMethod);
 }
 public virtual IExpression TransformAnonymousMethodExpression(IAnonymousMethodExpression value)
 {
     value.DelegateType = this.TransformType(value.DelegateType);
     value.Body = (IBlockStatement)this.TransformStatement(value.Body);
     value.ReturnType = this.TransformMethodReturnType(value.ReturnType);
     this.InsituTransformParameterDeclarationCollection(value.Parameters);
     return value;
 }
 public static void VisitAnonymousMethodExpressionChildren<TParameter, TStatement>(
     IAnonymousMethodExpression<TParameter, TStatement> anonymousMethodExpression,
     IGenericExpressionVisitor visitor)
     where TParameter : IAnonymousMethodParameter
     where TStatement : IStatement
 {
     VisitCollection(anonymousMethodExpression.Parameters, visitor);
 }
Ejemplo n.º 7
0
 public override void VisitAnonymousMethodExpression <TParameter, TStatement>(
     IAnonymousMethodExpression <TParameter, TStatement> anonymousMethod)
 {
     Value = new Expression()
     {
         AnonymousMethodExpression = new AnonymousMethodExpressionFactory(anonymousMethod).Value
     };
 }
        public static ILambdaName GetName([NotNull] this IAnonymousMethodExpression anonymousMethodDecl)
        {
            var seen = new Dictionary <DeclaredElementInstance, IName>();

            var identifier = new StringBuilder();

            identifier.AppendType(anonymousMethodDecl.ReturnType, seen);
            identifier.Append(' ');
            identifier.AppendParameters(anonymousMethodDecl.DeclaredParametersOwner, EmptySubstitution.INSTANCE, seen);
            return(Names.Lambda(identifier.ToString()));
        }
        private IForStatement ConvertForWithParallelSchedule(IForStatement ifs, IVariableDeclaration loopVar, ParallelScheduleExpression pse)
        {
            // Convert this loop into for(block) Parallel.For(thread) for(indexInBlock)
            IVariableDeclaration loopVarBlock   = VariableInformation.GenerateLoopVar(context, loopVar.Name + "_Block");
            Sequential           sequentialAttr = new Sequential();

            context.OutputAttributes.Set(loopVarBlock, sequentialAttr);
            IVariableDeclaration loopVarInBlock = VariableInformation.GenerateLoopVar(context, loopVar.Name + "_inBlock");

            context.OutputAttributes.Set(loopVarInBlock, sequentialAttr);
            string paramName   = VariableInformation.GenerateName(context, loopVar.Name + "_thread");
            var    threadParam = Builder.Param(paramName, typeof(int));

            if (!pse.scheduleExpression.GetExpressionType().Equals(typeof(int[][][])))
            {
                Error("argument to ParallelSchedule attribute is not of type int[][][]");
            }
            IExpression   itemsInThread    = Builder.ArrayIndex(pse.scheduleExpression, Builder.ParamRef(threadParam));
            IExpression   itemsInBlock     = Builder.ArrayIndex(itemsInThread, Builder.VarRefExpr(loopVarBlock));
            IExpression   itemCountInBlock = Builder.PropRefExpr(itemsInBlock, typeof(int[]), "Length");
            IExpression   threadCount      = Builder.PropRefExpr(pse.scheduleExpression, typeof(int[][][]), "Length");
            IExpression   zero             = Builder.LiteralExpr(0);
            IExpression   blockCount       = Builder.PropRefExpr(Builder.ArrayIndex(pse.scheduleExpression, zero), typeof(int[][]), "Length");
            IForStatement loopInBlock      = Builder.ForStmt(loopVarInBlock, itemCountInBlock);
            bool          isBackwardLoop   = !Recognizer.IsForwardLoop(ifs);

            if (isBackwardLoop)
            {
                Recognizer.ReverseLoopDirection(loopInBlock);
            }
            var assignLoopVar = Builder.AssignStmt(Builder.VarDeclExpr(loopVar), Builder.ArrayIndex(itemsInBlock, Builder.VarRefExpr(loopVarInBlock)));

            loopInBlock.Body.Statements.Add(assignLoopVar);
            ConvertStatements(loopInBlock.Body.Statements, ifs.Body.Statements);
            //loopInBlock.Body.Statements.AddRange(ifs.Body.Statements);
            IAnonymousMethodExpression bodyDelegate = Builder.AnonMethodExpr(typeof(Action <int>));

            bodyDelegate.Body = Builder.BlockStmt();
            bodyDelegate.Body.Statements.Add(loopInBlock);
            bodyDelegate.Parameters.Add(threadParam);
            Delegate d = new Func <int, int, Action <int>, ParallelLoopResult>(Parallel.For);
            IMethodInvokeExpression parallelFor = Builder.StaticMethod(d, zero, threadCount, bodyDelegate);
            IStatement    loopThread            = Builder.ExprStatement(parallelFor);
            IForStatement loopBlock             = Builder.ForStmt(loopVarBlock, blockCount);

            loopBlock.Body.Statements.Add(loopThread);
            if (isBackwardLoop)
            {
                Recognizer.ReverseLoopDirection(loopBlock);
            }
            return(loopBlock);
        }
Ejemplo n.º 10
0
        public override IAssignableExpression VisitAnonymousMethodExpression(IAnonymousMethodExpression expr,
                                                                             IList <IStatement> body)
        {
            var lambdaName  = expr.GetName();
            var lambdaBody  = new KaVEList <IStatement>();
            var bodyVisitor = new BodyVisitor(_nameGen, _marker);

            expr.Body.Accept(bodyVisitor, lambdaBody);

            return(new LambdaExpression
            {
                Name = lambdaName,
                Body = lambdaBody
            });
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Converts for loops into parallel for loops.
        /// </summary>
        /// <param name="ifs">The for loop to convert</param>
        /// <returns>The converted statement</returns>
        protected override IStatement ConvertFor(IForStatement ifs)
        {
            if (context.InputAttributes.Has <ConvergenceLoop>(ifs) || context.InputAttributes.Has <HasOffsetIndices>(ifs) || (ifs is IBrokenForStatement))
            {
                return(base.ConvertFor(ifs));
            }
            IVariableDeclaration loopVar = Recognizer.LoopVariable(ifs);

            if (context.InputAttributes.Has <Sequential>(loopVar))
            {
                return(base.ConvertFor(ifs));
            }
            // Convert this loop to a parallel for loop
            IExpression loopSize  = null;
            IExpression loopStart = null;

            if (Recognizer.IsForwardLoop(ifs))
            {
                loopStart = Recognizer.LoopStartExpression(ifs);
                loopSize  = Recognizer.LoopSizeExpression(ifs);
            }
            else if (ifs.Condition is IBinaryExpression)
            {
                IBinaryExpression ibe = (IBinaryExpression)ifs.Condition;
                if (ibe.Operator == BinaryOperator.GreaterThanOrEqual)
                {
                    // loop is "for(int i = end; i >= start; i--)"
                    loopStart = ibe.Right;
                    loopSize  = Builder.BinaryExpr(Recognizer.LoopStartExpression(ifs), BinaryOperator.Add, Builder.LiteralExpr(1));
                }
            }
            if (loopSize == null)
            {
                return(base.ConvertFor(ifs));
            }
            IAnonymousMethodExpression bodyDelegate = Builder.AnonMethodExpr(typeof(Action <int>));

            bodyDelegate.Body = ifs.Body;
            bodyDelegate.Parameters.Add(Builder.Param(loopVar.Name, loopVar.VariableType));
            Delegate d = new Func <int, int, Action <int>, ParallelLoopResult>(Parallel.For);
            IMethodInvokeExpression parallelFor = Builder.StaticMethod(d, loopStart, loopSize, bodyDelegate);
            IStatement st = Builder.ExprStatement(parallelFor);

            return(st);
        }
Ejemplo n.º 12
0
        static void Analyze([NotNull] IAnonymousMethodExpression anonymousMethodExpression, [NotNull] IHighlightingConsumer consumer)
        {
            if (!anonymousMethodExpression.IsAsync || !anonymousMethodExpression.ReturnType.IsVoid())
            {
                return; // not an "async delegate (...) { ... }" that returns void
            }

            if (anonymousMethodExpression.Parent is IAssignmentExpression assignmentExpression &&
                assignmentExpression.IsEventSubscriptionOrUnSubscription())
            {
                return; // direct event target
            }

            Debug.Assert(anonymousMethodExpression.AsyncKeyword != null);

            consumer.AddHighlighting(
                new AsyncVoidFunctionExpressionHighlighting(
                    "'async void' anonymous method expression not used as a direct event handler.",
                    anonymousMethodExpression.AsyncKeyword,
                    () => anonymousMethodExpression.SetAsync(false)));
        }
Ejemplo n.º 13
0
        private void CheckIssuesForReferencedLocalOrField(CheckCodeIssuesEventArgs ea, IForEachStatement loop, IAnonymousMethodExpression lambda, IElement element)
        {
            if (element.ElementType == LanguageElementType.ElementReferenceExpression)
            {
                IElement declaration = element.GetDeclaration();
                if (declaration.ElementType == LanguageElementType.Variable || declaration.ElementType == LanguageElementType.InitializedVariable)
                {
                    bool isLoopVariable = loop.LoopVariable == declaration;
                    bool declarationIsParentedByLoop = declaration.IsParentedBy(loop);
                    if (isLoopVariable || !declarationIsParentedByLoop)
                    {
                        ea.AddWarning(element.ToLanguageElement().Range, "Possible unintended closure scope misuse");
                    }
                }
                return;
            }

            foreach (IElement child in element.Children)
            {
                CheckIssuesForReferencedLocalOrField(ea, loop, lambda, child);
            }
        }
        public override bool Equals(object obj)
        {
            if (this == obj)
            {
                return(true);
            }

            IAnonymousMethodExpression expression = obj as IAnonymousMethodExpression;

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

            // Check that the parameter count is the same
            if (expression.Parameters.Count != this.Parameters.Count)
            {
                return(false);
            }

            // Check that the parameter types are the same
            for (int i = 0; i < this.Parameters.Count; i++)
            {
                if (!this.Parameters[i].ParameterType.Equals(expression.Parameters[i].ParameterType))
                {
                    return(false);
                }
            }

            // Check that the body is the same
            if (!this.Body.Equals(expression.Body))
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 15
0
        // 匿名メソッドは、ラムダ式文法で代替する事にする。
        // 実際には、変数のキャプチャなどを行うと C++/CLI ではコンパイル出来ない。
        private static void WriteAnonymousMethod(LanguageWriter w, IAnonymousMethodExpression exp)
        {
            w.Write("[&]");
            w.WriteMethodParameterCollection(exp.Parameters);

            // 戻り型
            TypeRef return_type = new TypeRef(exp.ReturnType.Type);

            if (!return_type.IsVoid)
            {
                w.Write(" -> ");
                return_type.WriteNameWithRef(w);
            }

            // 中身
            w.PushScope();
            w.Write(" {");
            w.WriteLine();
            w.WriteIndent();
            w.WriteStatement(exp.Body);
            w.WriteOutdent();
            w.Write("}");
            w.PopScope();
        }
            private void WriteAnonymousMethodExpression(IAnonymousMethodExpression value, IFormatter formatter)
            {
                bool parameters = false;

                for (int i = 0; i < value.Parameters.Count; i++)
                {
                    if ((value.Parameters[i].Name != null) && (value.Parameters[i].Name.Length > 0))
                    {
                        parameters = true;
                    }
                }

                formatter.WriteKeyword("function");
                formatter.Write("(");
                if (parameters)
                {
                    this.WriteParameterDeclarationList(value.Parameters, formatter, this.configuration);
                }
                formatter.Write(") {");

                formatter.WriteLine();
                formatter.WriteIndent();
                this.WriteBlockStatement(value.Body, formatter);
                formatter.WriteOutdent();
                formatter.WriteLine();
                formatter.Write("}");
            }
Ejemplo n.º 17
0
 public abstract IExpression Transform(IAnonymousMethodExpression expression);
Ejemplo n.º 18
0
 void IExpressionVisitor.Visit(IAnonymousMethodExpression expression)
 {
     this.Translate(expression);
 }
Ejemplo n.º 19
0
        private void CheckIssuesForReferencedLocalOrField(CheckCodeIssuesEventArgs ea, IForEachStatement loop, IAnonymousMethodExpression lambda, IElement element)
        {
            if (element.ElementType == LanguageElementType.ElementReferenceExpression)
            {
                IElement declaration = element.GetDeclaration();
                if (declaration.ElementType == LanguageElementType.Variable || declaration.ElementType == LanguageElementType.InitializedVariable)
                {
                    bool isLoopVariable = loop.LoopVariable == declaration;
                    bool declarationIsParentedByLoop = declaration.IsParentedBy(loop);
                    if (isLoopVariable || !declarationIsParentedByLoop)
                    {
                        ea.AddWarning(element.ToLanguageElement().Range, "Possible unintended closure scope misuse");
                    }
                }
                return;
            }

            foreach (IElement child in element.Children)
            {
                CheckIssuesForReferencedLocalOrField(ea, loop, lambda, child);
            }
        }
Ejemplo n.º 20
0
 TransformationImpact IExpressionVisitor <TransformationImpact> .Visit(IAnonymousMethodExpression expression)
 {
     return(CalculateRefactorImpact(expression));
 }
Ejemplo n.º 21
0
 public abstract void Translate(IAnonymousMethodExpression expression);
Ejemplo n.º 22
0
 public TransformationKind Visit(IAnonymousMethodExpression expression, ITransformationContext context)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 23
0
 public abstract TransformationImpact CalculateRefactorImpact(IAnonymousMethodExpression expression);
Ejemplo n.º 24
0
 public TestLinkerResult Visit(IAnonymousMethodExpression expression, ICompilationContext context)
 {
     throw new NotImplementedException();
 }