/// <summary> /// Clear local variables, and fill them with tactic arguments. Use with caution. /// </summary> public void FillTacticInputs() { localDeclarations.Clear(); ExprRhs er = (ExprRhs)tac_call.Rhss[0]; List <Expression> exps = ((ApplySuffix)er.Expr).Args; Contract.Assert(exps.Count == tactic.Ins.Count); for (int i = 0; i < exps.Count; i++) { localDeclarations.Add(tactic.Ins[i], exps[i]); } }
public static string GetSignature(UpdateStmt us) { ExprRhs er = us.Rhss[0] as ExprRhs; if (er == null) { return(null); } ApplySuffix asx = er.Expr as ApplySuffix; return(GetSignature(asx)); }
/// <summary> /// Deep copy updateStmt /// </summary> /// <param name="stmt"></param> /// <returns></returns> public static UpdateStmt CopyUpdateStmt(UpdateStmt stmt) { ExprRhs old_exp = stmt.Rhss[0] as ExprRhs; ApplySuffix old_aps = old_exp.Expr as ApplySuffix; LiteralExpr literal = old_exp.Expr as LiteralExpr; if (old_aps != null) { ApplySuffix aps = CopyExpression(old_aps) as ApplySuffix; return(new UpdateStmt(stmt.Tok, stmt.EndTok, CopyExpressionList(stmt.Lhss), new List <AssignmentRhs> { new ExprRhs(aps) })); } if (literal != null) { return(new UpdateStmt(stmt.Tok, stmt.EndTok, CopyExpressionList(stmt.Lhss), new List <AssignmentRhs> { new ExprRhs(CopyExpression(literal)) })); } return(null); }
public virtual void Visit(ExprRhs expressionRhs) { VisitNullableAttributes(expressionRhs.Attributes); Visit(expressionRhs.Expr); }
void Rhs(out AssignmentRhs r) { Contract.Ensures(Contract.ValueAtReturn<AssignmentRhs>(out r) != null); IToken/*!*/ x, newToken; Expression/*!*/ e; Type ty = null; List<Expression> ee = null; List<Expression> args = null; r = dummyRhs; // to please compiler Attributes attrs = null; if (la.kind == 97) { Get(); newToken = t; TypeAndToken(out x, out ty); if (la.kind == 48 || la.kind == 50) { if (la.kind == 48) { Get(); ee = new List<Expression>(); Expressions(ee); Expect(49); var tmp = theBuiltIns.ArrayType(ee.Count, new IntType(), true); } else { x = null; args = new List<Expression/*!*/>(); Get(); if (StartOf(7)) { Expressions(args); } Expect(51); } } if (ee != null) { r = new TypeRhs(newToken, ty, ee); } else if (args != null) { r = new TypeRhs(newToken, ty, args, false); } else { r = new TypeRhs(newToken, ty); } } else if (la.kind == 57) { Get(); r = new HavocRhs(t); } else if (StartOf(7)) { Expression(out e, false, true); r = new ExprRhs(e); } else SynErr(197); while (la.kind == 46) { Attribute(ref attrs); } r.Attributes = attrs; }
public override AssignmentRhs CloneRHS(AssignmentRhs rhs) { var r = rhs as ExprRhs; if (r != null && r.Expr is ApplySuffix) { var apply = (ApplySuffix)r.Expr; var mse = apply.Lhs.Resolved as MemberSelectExpr; if (mse != null && mse.Member is FixpointLemma && ModuleDefinition.InSameSCC(context, (FixpointLemma)mse.Member)) { // we're looking at a recursive call to a fixpoint lemma Contract.Assert(apply.Lhs is NameSegment || apply.Lhs is ExprDotName); // this is the only way a call statement can have been parsed // clone "apply.Lhs", changing the inductive/co lemma to the prefix lemma; then clone "apply", adding in the extra argument Expression lhsClone; if (apply.Lhs is NameSegment) { var lhs = (NameSegment)apply.Lhs; lhsClone = new NameSegment(Tok(lhs.tok), lhs.Name + "#", lhs.OptTypeArguments == null ? null : lhs.OptTypeArguments.ConvertAll(CloneType)); } else { var lhs = (ExprDotName)apply.Lhs; lhsClone = new ExprDotName(Tok(lhs.tok), CloneExpr(lhs.Lhs), lhs.SuffixName + "#", lhs.OptTypeArguments == null ? null : lhs.OptTypeArguments.ConvertAll(CloneType)); } var args = new List<Expression>(); args.Add(k); apply.Args.ForEach(arg => args.Add(CloneExpr(arg))); var applyClone = new ApplySuffix(Tok(apply.tok), lhsClone, args); var c = new ExprRhs(applyClone); reporter.Info(MessageSource.Cloner, apply.Lhs.tok, mse.Member.Name + suffix); return c; } } return base.CloneRHS(rhs); }
public virtual AssignmentRhs CloneRHS(AssignmentRhs rhs) { AssignmentRhs c; if (rhs is ExprRhs) { var r = (ExprRhs)rhs; c = new ExprRhs(CloneExpr(r.Expr)); } else if (rhs is HavocRhs) { c = new HavocRhs(Tok(rhs.Tok)); } else { var r = (TypeRhs)rhs; if (r.ArrayDimensions != null) { c = new TypeRhs(Tok(r.Tok), CloneType(r.EType), r.ArrayDimensions.ConvertAll(CloneExpr)); } else if (r.Arguments == null) { c = new TypeRhs(Tok(r.Tok), CloneType(r.EType)); } else { c = new TypeRhs(Tok(r.Tok), CloneType(r.Path), r.Arguments.ConvertAll(CloneExpr), false); } } c.Attributes = CloneAttributes(rhs.Attributes); return c; }
private void BuildBlock(IWpfTextView text, Method method) { string newMethodName = GetNewName(text); UpdateStmt firstStmt; int i = 0; AssertStmt pre = null, post = null; Expression invariant = null; Expression guard = ExtractGuard(method.Ens.Last().E); if (method.Ens.Last().E is BinaryExpr) { var bexp = method.Ens.Last().E as BinaryExpr; if (bexp.Op == BinaryExpr.Opcode.And) { guard = InvertExp(bexp.E1); post = new AssertStmt(null, null, bexp, null, null); pre = new AssertStmt(null, null, bexp.E0, null, null); invariant = bexp.E0; } } //new UnaryOpExpr(null, UnaryOpExpr.Opcode.Not,ExtractGuard(method.Ens.Last().E)); List <Expression> decress = new List <Expression>(); var decExp = new BinaryExpr(null, BinaryExpr.Opcode.Sub, ((BinaryExpr)guard).E0, ((BinaryExpr)guard).E1); decress.Add(decExp); List <MaybeFreeExpression> invs = new List <MaybeFreeExpression>(); invs.Add(new MaybeFreeExpression(invariant)); var newStmts = new List <Statement>(); var whileBodystmts = new List <Statement>(); if (BuildFirstStatement(method.Ins, method.Outs, out firstStmt)) { newStmts.Add(firstStmt); } List <Expression> args = new List <Expression>(); foreach (var v in GetAllVars(method.Ins, method.Outs)) { args.Add(new NameSegment(null, v.Name, null)); } AssignmentRhs arhs = new ExprRhs(new ApplySuffix(null, new NameSegment(null, newMethodName, null) , args)); var lstRhs = new List <AssignmentRhs>(); lstRhs.Add(arhs); whileBodystmts.Add(new UpdateStmt(null, null, firstStmt.Lhss, lstRhs)); BlockStmt whileBody = new BlockStmt(null, null, whileBodystmts); WhileStmt wstmt = new WhileStmt(null, null, guard, invs, new Specification <Expression>(decress, null), new Specification <FrameExpression>(null, null), whileBody); newStmts.Add(pre); newStmts.Add(wstmt); newStmts.Add(post); BlockStmt funcBody = new BlockStmt(null, null, newStmts); List <MaybeFreeExpression> newMethodreq = new List <MaybeFreeExpression>(); List <MaybeFreeExpression> newMethodens = new List <MaybeFreeExpression>(); newMethodreq.Add(new MaybeFreeExpression(new BinaryExpr(null, BinaryExpr.Opcode.And, ReplaceOutsWithIns(invariant, method.Ins, method.Outs), ReplaceOutsWithIns(guard, method.Ins, method.Outs)))); newMethodens.Add(new MaybeFreeExpression(new BinaryExpr(null, BinaryExpr.Opcode.And, invariant, new BinaryExpr(null, BinaryExpr.Opcode.Le, decExp, ReplaceOutsWithIns(decExp, method.Ins, method.Outs))))); Method m = new Method(null, newMethodName, false, false, new List <TypeParameter>(), method.Ins, method.Outs, newMethodreq, new Specification <FrameExpression>(null, null), newMethodens, new Specification <Expression>(null, null), null, null, null); int selection = text.Selection.End.Position; selection = GetPositionOFLastToken(method, text); string s = Printer.StatementToString(funcBody) + "\n\n"; string f = "\n\n" + Printer.MethodSignatureToString(m) + "\n"; WriteToTextViewer(f, text, selection); WriteToTextViewer(s, text, selection); }
public virtual void AddResolvedGhostStatement(Statement stmt) { BlockStmt block = stmt as BlockStmt; IfStmt ifStmt = stmt as IfStmt; AssertStmt assertStmt = stmt as AssertStmt; AssignStmt assignStmt = stmt as AssignStmt; CallStmt callStmt = stmt as CallStmt; VarDecl varDecl = stmt as VarDecl; CalcStmt calcStmt = stmt as CalcStmt; ForallStmt forallStmt = stmt as ForallStmt; AssignSuchThatStmt existsStmt = stmt as AssignSuchThatStmt; if (block != null) { var oldRenamer = PushRename(); block.Body.ForEach(AddGhostStatement); PopRename(oldRenamer); } else if (varDecl != null) { AddGhostVarDecl(varDecl.Name, varDecl.Type, varDecl.IsGhost); } else if (minVerify) { return; } else if (assignStmt != null) { ExprRhs expRhs = assignStmt.Rhs as ExprRhs; if (expRhs != null) { FieldSelectExpr fieldSelect = assignStmt.Lhs as FieldSelectExpr; RtlVar destVar; if (fieldSelect != null) { destVar = new RtlVar(GhostVar(fieldSelect.FieldName), true, fieldSelect.Type); } else { destVar = AsVar(assignStmt.Lhs); Util.Assert(destVar != null); } stmts.Add(new RtlGhostMove(new RtlVar[] { destVar }, new RtlExp[] { GhostExpression(expRhs.Expr) })); } else { throw new Exception("not implemented: " + assignStmt.Rhs); } } else if (callStmt != null) { AddGhostCall(callStmt.Lhs.ConvertAll(AsVar), callStmt.Args, dafnySpec.Compile_Method(callStmt.Method, callStmt.TypeArgumentSubstitutions.ToDictionary(p => p.Key, p => AppType(p.Value))), DafnySpec.IsHeapMethod(callStmt.Method)); SymdiffLinearityPoint(); } else if (ifStmt != null) { stmts.Add(new RtlGhostStmtComputed(s => "if (" + s.args[0] + ") {", new RtlExp[] { GhostExpression(ifStmt.Guard) })); Indent(); AddGhostStatement(ifStmt.Thn); Unindent(); stmts.Add(new RtlGhostStmtComputed(s => "}", new RtlExp[0])); if (ifStmt.Els != null) { stmts.Add(new RtlGhostStmtComputed(s => "if (" + s.args[0] + ") {", new RtlExp[] { GhostExpression(new UnaryExpr(Bpl.Token.NoToken, UnaryExpr.Opcode.Not, ifStmt.Guard)) })); Indent(); AddGhostStatement(ifStmt.Els); Unindent(); stmts.Add(new RtlGhostStmtComputed(s => "}", new RtlExp[0])); } } else if (assertStmt != null) { stmts.Add(new RtlAssert(GhostExpression(assertStmt.Expr))); } else if (forallStmt != null) { var oldRenamer = PushRename(forallStmt.BoundVars.Select(v => v.Name)); RtlExp ens = new RtlLiteral("true"); foreach (var e in forallStmt.Ens) { ens = new RtlBinary("&&", ens, GhostExpression(e.E)); } RtlExp range = (forallStmt.Range == null) ? new RtlLiteral("true") : GhostExpression(forallStmt.Range); List <RtlExp> wellFormed = GetTypeWellFormed(forallStmt.BoundVars. Select(x => Tuple.Create(GhostVar(x.Name), x.IsGhost, x.Type)).ToList()); wellFormed.ForEach(e => range = new RtlBinary("&&", e, range)); ens = new RtlBinary("==>", range, ens); string vars = String.Join(", ", forallStmt.BoundVars.Select(x => GhostVar(x.Name) + ":" + TypeString(AppType(x.Type)))); stmts.Add(new RtlGhostStmtComputed(s => "forall " + vars + "::(" + s.args[0] + ")", new List <RtlExp> { ens })); stmts.Add(new RtlGhostStmtComputed(s => "{", new RtlExp[0])); Indent(); stmts.Add(PushForall()); stmts.Add(new RtlGhostStmtComputed(s => "if (" + s.args[0] + ")", new List <RtlExp> { range })); stmts.Add(new RtlGhostStmtComputed(s => "{", new RtlExp[0])); Indent(); AddGhostStatement(forallStmt.Body); foreach (var e in forallStmt.Ens) { stmts.Add(new RtlAssert(GhostExpression(e.E))); } PopForall(); Unindent(); stmts.Add(new RtlGhostStmtComputed(s => "}", new RtlExp[0])); Unindent(); stmts.Add(new RtlGhostStmtComputed(s => "}", new RtlExp[0])); PopRename(oldRenamer); } else if (existsStmt != null) { List <RtlStmt> assigns = new List <RtlStmt>(); List <RtlVar> tmps = new List <RtlVar>(); List <Tuple <string, bool, Type> > varTuples = new List <Tuple <string, bool, Type> >(); var oldRenamer = PushRename(); foreach (var lhs in existsStmt.Lhss) { IdentifierExpr idExp = lhs.Resolved as IdentifierExpr; RtlVar origVar = AsVar(lhs); AddRename(idExp.Name); RtlVar renameVar = AsVar(lhs); tmps.Add(renameVar); varTuples.Add(Tuple.Create(renameVar.ToString(), true, idExp.Type)); assigns.Add(new RtlGhostMove(new RtlVar[] { origVar }, new RtlExp[] { renameVar })); } string vars = String.Join(", ", tmps.Select(x => x.getName() + ":" + TypeString(AppType(x.type)))); stmts.Add(new RtlGhostStmtComputed(s => "exists " + vars + "::(" + s.args[0] + ");", new List <RtlExp> { GetTypeWellFormedExp(varTuples.ToList(), "&&", GhostExpression(existsStmt.Expr)) })); stmts.AddRange(assigns); PopRename(oldRenamer); } else if (calcStmt != null) { Util.Assert(calcStmt.Steps.Count == calcStmt.Hints.Count); CalcStmt.BinaryCalcOp binOp = calcStmt.Op as CalcStmt.BinaryCalcOp; bool isImply = binOp != null && binOp.Op == BinaryExpr.Opcode.Imp && calcStmt.Steps.Count > 0; if (isImply) { stmts.Add(new RtlGhostStmtComputed(s => "if (" + s.args[0] + ")", new RtlExp[] { GhostExpression(CalcStmt.Lhs(calcStmt.Steps[0])) })); stmts.Add(new RtlGhostStmtComputed(s => "{", new RtlExp[0])); Indent(); } var stepCount = calcStmt.Hints.Last().Body.Count == 0 ? calcStmt.Steps.Count - 1 : calcStmt.Steps.Count; for (int i = 0; i < stepCount; i++) { if (calcStmt.Hints[i] == null) { stmts.Add(new RtlAssert(GhostExpression(calcStmt.Steps[i]))); } else { stmts.Add(new RtlGhostStmtComputed(s => "forall::(" + s.args[0] + ")", new List <RtlExp> { GhostExpression(calcStmt.Steps[i]) })); stmts.Add(new RtlGhostStmtComputed(s => "{", new RtlExp[0])); Indent(); var dict = new Dictionary <string, RtlVar>(); stmts.Add(new RtlGhostStmtComputed(s => String.Concat(dict.Values.Select( x => "var " + x.x + ":" + TypeString(x.type) + ";")), new RtlExp[0])); forallVars.Add(dict); AddGhostStatement(calcStmt.Hints[i]); forallVars.RemoveAt(forallVars.Count - 1); Unindent(); stmts.Add(new RtlGhostStmtComputed(s => "}", new RtlExp[0])); } } if (isImply) { Unindent(); stmts.Add(new RtlGhostStmtComputed(s => "}", new RtlExp[0])); } } else { throw new Exception("not implemented in ghost methods: " + stmt); } }