예제 #1
0
        public static bool Only(ICode ast, params Stmt.NodeType[] stmtTypes)
        {
            var v = new VisitorOnlyStatements(stmtTypes);

            v.Visit(ast);
            return(v.IsOnlyRequestedTypes);
        }
        protected override ICode VisitIf(StmtIf s)
        {
            if (!VisitorFindContinuations.Any(s))
            {
                // 'If' contains no continuations, so no distribution can be done
                return(s);
            }
            if (VisitorOnlyStatements.Only(s, Stmt.NodeType.If, Stmt.NodeType.Continuation) && this.ifInfo == null)
            {
                // 'If' only contains continuations, so no distribution can be done
                // Must visit base method to find contained continuations
                return(base.VisitIf(s));
            }
            bool finalise = false;

            if (this.ifInfo == null)
            {
                finalise    = true;
                this.ifInfo = new IfInfo();
            }
            this.ifInfo.Conditions.Push(s.Condition);
            var then = this.Visit(s.Then);

            this.ifInfo.Conditions.Pop();
            this.ifInfo.Conditions.Push(this.ctx.ExprGen.NotAutoSimplify(s.Condition));
            var @else = this.Visit(s.Else);

            this.ifInfo.Conditions.Pop();
            if (then != s.Then || @else != s.Else)
            {
                var @if = new StmtIf(s.Ctx, s.Condition, (Stmt)then, (Stmt)@else);
                if (finalise && this.ifInfo.AddToIf.Any())
                {
                    var ifStmts = this.ifInfo.AddToIf.GroupBy(x => x.Item1.To, x => x.Item2).Select(x =>
                                                                                                    new StmtIf(s.Ctx, x.Aggregate((a, b) => this.ctx.ExprGen.Or(a, b)),
                                                                                                               this.ifInfo.AddToIf.First(y => y.Item1.To == x.Key).Item1, null)
                                                                                                    );
                    var stmts = new Stmt[] { @if }.Concat(ifStmts).ToArray();
                    this.ifInfo = null;
                    return(new StmtBlock(s.Ctx, stmts));
                }
                else
                {
                    if (finalise)
                    {
                        this.ifInfo = null;
                    }
                    return(@if);
                }
            }
            else
            {
                // In this case, no continuations will have been found, so there cannot be any conditions to add
                if (finalise)
                {
                    this.ifInfo = null;
                }
                return(s);
            }
        }
 public static bool Only(ICode ast, params Stmt.NodeType[] stmtTypes) {
     var v = new VisitorOnlyStatements(stmtTypes);
     v.Visit(ast);
     return v.IsOnlyRequestedTypes;
 }