public HashSet <DVariable> GetVars(List <Statement> statements, out HashSet <DVariable> declVars, bool containNonModified) { DVariableComparer comparer = new DVariableComparer(); HashSet <DVariable> vars = new HashSet <DVariable>(comparer); declVars = new HashSet <DVariable>(comparer); foreach (var stmt in statements) { vars.UnionWith(GetVars(stmt, declVars, containNonModified)); } return(vars); }
public void GetVariables(List <Statement> statements, List <Statement> otherStatements, out List <DVariable> ins, out List <DVariable> outs, out List <DVariable> toDeclare) { DVariableComparer comparer = new DVariableComparer(); HashSet <DVariable> varsDeclaredInSelectedScope; var varsUsedInSelectedScope = GetVars(statements, out varsDeclaredInSelectedScope); HashSet <DVariable> varsDeclaredAfterScope; var varsUsedAfterScope = GetVars(otherStatements, out varsDeclaredAfterScope); ins = varsUsedInSelectedScope.ToList(); outs = varsUsedInSelectedScope.Intersect(varsUsedAfterScope, comparer).ToList(); toDeclare = varsDeclaredInSelectedScope.Intersect(varsUsedAfterScope, comparer).ToList(); //ins.AddRange(toDeclare); }
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); }
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); }