Exemplo n.º 1
0
        private HashSet <DVariable> GetVars(Expression exp, HashSet <DVariable> declaredVars)
        {
            DVariableComparer   comparer = new DVariableComparer();
            HashSet <DVariable> vars     = new HashSet <DVariable>(comparer);

            if (exp is SeqSelectExpr)
            {
                var expr = exp as SeqSelectExpr;
                vars.UnionWith(GetVars(expr.Seq, declaredVars));
                vars.UnionWith(GetVars(expr.E0, declaredVars));
                vars.UnionWith(GetVars(expr.E1, declaredVars));
            }
            else if (exp is NameSegment)
            {
                var       expr = exp as NameSegment;
                DVariable var  = new DVariable(expr.Name, expr.Type);
                if (!declaredVars.Contains(var))
                {
                    vars.Add(var);
                }
            }
            else if (exp is ApplySuffix)
            {
                var expr = exp as ApplySuffix;
                foreach (var arg in expr.Args)
                {
                    vars.UnionWith(GetVars(arg, declaredVars));
                }
            }
            else if (exp is BinaryExpr)
            {
                var expr = exp as BinaryExpr;
                vars.UnionWith(GetVars(expr.E0, declaredVars));
                vars.UnionWith(GetVars(expr.E1, declaredVars));
            }
            else if (exp is UnaryOpExpr)
            {
                var expr = exp as UnaryOpExpr;
                vars.UnionWith(GetVars(expr.E, declaredVars));
            }
            else if (exp is ParensExpression)
            {
                var expr = exp as ParensExpression;
                vars.UnionWith(GetVars(expr.E, declaredVars));
            }
            else if (exp is ChainingExpression)
            {
                var expr = exp as ChainingExpression;
                vars.UnionWith(GetVars(expr.E, declaredVars));
            }
            else if (exp is SeqDisplayExpr)
            {
                var expr = exp as SeqDisplayExpr;
                foreach (var arg in expr.Elements)
                {
                    vars.UnionWith(GetVars(arg, declaredVars));
                }
            }
            else if (exp is ForallExpr)
            {
                var expr       = exp as ForallExpr;
                var newDecVars = new HashSet <DVariable>(declaredVars, comparer);
                if (expr.BoundVars != null)
                {
                    foreach (var bvar in expr.BoundVars)
                    {
                        DVariable dvar = new DVariable(bvar.DisplayName, bvar.Type);
                        newDecVars.Add(dvar);
                    }
                }
                vars.UnionWith(GetVars(expr.Term, newDecVars));
            }
            return(vars);
        }
Exemplo n.º 2
0
        private HashSet <DVariable> GetVars(Statement stmt, HashSet <DVariable> declaredVars, bool containNonModified)
        {
            DVariableComparer   comparer = new DVariableComparer();
            HashSet <DVariable> usedVars = new HashSet <DVariable>(comparer);

            if (stmt is UpdateStmt)
            {
                var ustmt = (UpdateStmt)stmt;
                foreach (var ls in ustmt.Lhss)
                {
                    usedVars.UnionWith(GetVars(ls, declaredVars));
                }
                if (containNonModified)
                {
                    foreach (var rs in ustmt.Rhss)
                    {
                        var exp = rs as ExprRhs;
                        usedVars.UnionWith(GetVars(exp.Expr, declaredVars));
                    }
                }
            }
            else if (stmt is AssertStmt && containNonModified)
            {
                var asrt = stmt as AssertStmt;
                usedVars.UnionWith(GetVars(asrt.Expr, declaredVars));
                usedVars.UnionWith(GetVars(asrt.Proof, declaredVars, containNonModified));
            }
            else if (stmt is WhileStmt)
            {
                var wstmt = stmt as WhileStmt;
                usedVars.UnionWith(GetVars(wstmt.Body, declaredVars, containNonModified));
                foreach (var exp in wstmt.Decreases.Expressions)
                {
                    usedVars.UnionWith(GetVars(exp, declaredVars));
                }
                usedVars.UnionWith(GetVars(wstmt.Guard, declaredVars));
                foreach (var exp in wstmt.Invariants)
                {
                    usedVars.UnionWith(GetVars(exp.E, declaredVars));
                }
            }
            else if (stmt is BlockStmt)
            {
                var stmts = stmt as BlockStmt;
                foreach (var bodyStmt in stmts.Body)
                {
                    usedVars.UnionWith(GetVars(bodyStmt, declaredVars, containNonModified));
                }
            }
            else if (stmt is VarDeclStmt)
            {
                var decl = stmt as VarDeclStmt;
                usedVars.UnionWith(GetVars(decl.Update, declaredVars, containNonModified));
                if (decl.Locals != null)
                {
                    foreach (var v in decl.Locals)
                    {
                        DVariable dvar = new DVariable(v.DisplayName, v.Type);
                        declaredVars.Add(dvar);
                    }
                }
            }
            else if (stmt is IfStmt)
            {
                var ifstmt = stmt as IfStmt;
                usedVars.UnionWith(GetVars(ifstmt.Guard, declaredVars));
                usedVars.UnionWith(GetVars(ifstmt.Thn, declaredVars, containNonModified));
                usedVars.UnionWith(GetVars(ifstmt.Els, declaredVars, containNonModified));
            }
            else if (stmt is PrintStmt)
            {
                var pstmt = stmt as PrintStmt;
                foreach (var arg in pstmt.Args)
                {
                    usedVars.UnionWith(GetVars(arg, declaredVars));
                }
            }

            return(usedVars);
        }