public virtual void Visit(AlternativeLoopStmt alternativeLoopStatement)
 {
     VisitNullableAttributes(alternativeLoopStatement.Attributes);
     Visit(alternativeLoopStatement.Decreases);
     Visit(alternativeLoopStatement.Mod);
     foreach (var guardedAlternative in alternativeLoopStatement.Alternatives)
     {
         Visit(guardedAlternative);
     }
 }
Ejemplo n.º 2
0
        void WhileStmt(out Statement stmt)
        {
            Contract.Ensures(Contract.ValueAtReturn(out stmt) != null); IToken x;
            Expression guard = null;  IToken guardEllipsis = null;

            List<MaybeFreeExpression> invariants = new List<MaybeFreeExpression>();
            List<Expression> decreases = new List<Expression>();
            Attributes decAttrs = null;
            Attributes modAttrs = null;
            List<FrameExpression> mod = null;

            BlockStmt body = null;  IToken bodyEllipsis = null;
            IToken bodyStart = null, bodyEnd = null, endTok = Token.NoToken;
            List<GuardedAlternative> alternatives;
            stmt = dummyStmt;  // to please the compiler
            bool isDirtyLoop = true;

            Expect(99);
            x = t;
            if (IsLoopSpec() || IsAlternative()) {
            while (StartOf(22)) {
                LoopSpec(invariants, decreases, ref mod, ref decAttrs, ref modAttrs);
            }
            AlternativeBlock(false, out alternatives, out endTok);
            stmt = new AlternativeLoopStmt(x, endTok, invariants, new Specification<Expression>(decreases, decAttrs), new Specification<FrameExpression>(mod, modAttrs), alternatives);
            } else if (StartOf(20)) {
            if (StartOf(21)) {
                Guard(out guard);
                Contract.Assume(guard == null || cce.Owner.None(guard));
            } else {
                Get();
                guardEllipsis = t;
            }
            while (StartOf(22)) {
                LoopSpec(invariants, decreases, ref mod, ref decAttrs, ref modAttrs);
            }
            if (la.kind == _lbrace) {
                BlockStmt(out body, out bodyStart, out bodyEnd);
                endTok = body.EndTok; isDirtyLoop = false;
            } else if (la.kind == _ellipsis) {
                Expect(59);
                bodyEllipsis = t; endTok = t; isDirtyLoop = false;
            } else if (StartOf(23)) {
            } else SynErr(187);
            if (guardEllipsis != null || bodyEllipsis != null) {
             if (mod != null) {
               SemErr(mod[0].E.tok, "'modifies' clauses are not allowed on refining loops");
             }
             if (body == null && !isDirtyLoop) {
               body = new BlockStmt(x, endTok, new List<Statement>());
             }
             stmt = new WhileStmt(x, endTok, guard, invariants, new Specification<Expression>(decreases, decAttrs), new Specification<FrameExpression>(null, null), body);
             stmt = new SkeletonStatement(stmt, guardEllipsis, bodyEllipsis);
            } else {
             // The following statement protects against crashes in case of parsing errors
             if (body == null && !isDirtyLoop) {
               body = new BlockStmt(x, endTok, new List<Statement>());
             }
             stmt = new WhileStmt(x, endTok, guard, invariants, new Specification<Expression>(decreases, decAttrs), new Specification<FrameExpression>(mod, modAttrs), body);
            }

            } else SynErr(188);
        }
Ejemplo n.º 3
0
    public virtual Statement CloneStmt(Statement stmt) {
      if (stmt == null) {
        return null;
      }

      Statement r;
      if (stmt is AssertStmt) {
        var s = (AssertStmt)stmt;
        r = new AssertStmt(Tok(s.Tok), Tok(s.EndTok), CloneExpr(s.Expr), null);

      } else if (stmt is AssumeStmt) {
        var s = (AssumeStmt)stmt;
        r = new AssumeStmt(Tok(s.Tok), Tok(s.EndTok), CloneExpr(s.Expr), null);
      } else if (stmt is TacticInvariantStmt) {
        var s = (TacticInvariantStmt)stmt;
        r = new TacticInvariantStmt(Tok(s.Tok), Tok(s.EndTok), CloneExpr(s.Expr), null, s.IsObjectLevel);
      } else if (stmt is TacticAssertStmt) {
        var s = (TacticAssertStmt)stmt;
        r = new TacticAssertStmt(Tok(s.Tok), Tok(s.EndTok), CloneExpr(s.Expr), null, s.IsObjectLevel);
      } else if (stmt is PrintStmt) {
        var s = (PrintStmt)stmt;
        r = new PrintStmt(Tok(s.Tok), Tok(s.EndTok), s.Args.ConvertAll(CloneExpr));
      } else if (stmt is BreakStmt) {
        var s = (BreakStmt)stmt;
        if (s.TargetLabel != null) {
          r = new BreakStmt(Tok(s.Tok), Tok(s.EndTok), s.TargetLabel);
        } else {
          r = new BreakStmt(Tok(s.Tok), Tok(s.EndTok), s.BreakCount);
        }

      } else if (stmt is ReturnStmt) {
        var s = (ReturnStmt)stmt;
        r = new ReturnStmt(Tok(s.Tok), Tok(s.EndTok), s.rhss == null ? null : s.rhss.ConvertAll(CloneRHS));

      } else if (stmt is YieldStmt) {
        var s = (YieldStmt)stmt;
        r = new YieldStmt(Tok(s.Tok), Tok(s.EndTok), s.rhss == null ? null : s.rhss.ConvertAll(CloneRHS));

      } else if (stmt is AssignStmt) {
        var s = (AssignStmt)stmt;
        r = new AssignStmt(Tok(s.Tok), Tok(s.EndTok), CloneExpr(s.Lhs), CloneRHS(s.Rhs));

      } else if (stmt is BlockStmt) {
        r = CloneBlockStmt((BlockStmt)stmt);

      } else if (stmt is IfStmt) {
        var s = (IfStmt)stmt;
        r = new IfStmt(Tok(s.Tok), Tok(s.EndTok), s.IsExistentialGuard, CloneExpr(s.Guard), CloneBlockStmt(s.Thn), CloneStmt(s.Els));

      } else if (stmt is AlternativeStmt) {
        var s = (AlternativeStmt)stmt;
        r = new AlternativeStmt(Tok(s.Tok), Tok(s.EndTok), s.Alternatives.ConvertAll(CloneGuardedAlternative));

      } else if (stmt is WhileStmt) {
        var s = (WhileStmt)stmt;
        r = new WhileStmt(Tok(s.Tok), Tok(s.EndTok), CloneExpr(s.Guard), s.Invariants.ConvertAll(CloneMayBeFreeExpr), CloneSpecExpr(s.Decreases), CloneSpecFrameExpr(s.Mod), CloneBlockStmt(s.Body));
        ((WhileStmt)r).TacAps = s.TacAps;
      } else if (stmt is AlternativeLoopStmt) {
        var s = (AlternativeLoopStmt)stmt;
        r = new AlternativeLoopStmt(Tok(s.Tok), Tok(s.EndTok), s.Invariants.ConvertAll(CloneMayBeFreeExpr), CloneSpecExpr(s.Decreases), CloneSpecFrameExpr(s.Mod), s.Alternatives.ConvertAll(CloneGuardedAlternative));

      } else if (stmt is ForallStmt) {
        var s = (ForallStmt)stmt;
        r = new ForallStmt(Tok(s.Tok), Tok(s.EndTok), s.BoundVars.ConvertAll(CloneBoundVar), null, CloneExpr(s.Range), s.Ens.ConvertAll(CloneMayBeFreeExpr), CloneStmt(s.Body));
        if (s.ForallExpressions != null) {
          ((ForallStmt)r).ForallExpressions = s.ForallExpressions.ConvertAll(CloneExpr);
        }
      } else if (stmt is CalcStmt) {
        var s = (CalcStmt)stmt;
        // calc statements have the unusual property that the last line is duplicated.  If that is the case (which
        // we expect it to be here), we share the clone of that line as well.
        var lineCount = s.Lines.Count;
        var lines = new List<Expression>(lineCount);
        for (int i = 0; i < lineCount; i++) {
          lines.Add(i == lineCount - 1 && 2 <= lineCount && s.Lines[i] == s.Lines[i - 1] ? lines[i - 1] : CloneExpr(s.Lines[i]));
        }
        Contract.Assert(lines.Count == lineCount);
        r = new CalcStmt(Tok(s.Tok), Tok(s.EndTok), CloneCalcOp(s.Op), lines, s.Hints.ConvertAll(CloneBlockStmt), s.StepOps.ConvertAll(CloneCalcOp), CloneCalcOp(s.ResultOp), CloneAttributes(s.Attributes));

      } else if (stmt is MatchStmt) {
        var s = (MatchStmt)stmt;
        r = new MatchStmt(Tok(s.Tok), Tok(s.EndTok), CloneExpr(s.Source),
        s.Cases.ConvertAll(CloneMatchCaseStmt), s.UsesOptionalBraces);

      } else if (stmt is AssignSuchThatStmt) {
        var s = (AssignSuchThatStmt)stmt;
        r = new AssignSuchThatStmt(Tok(s.Tok), Tok(s.EndTok), s.Lhss.ConvertAll(CloneExpr), CloneExpr(s.Expr), s.AssumeToken == null ? null : Tok(s.AssumeToken), null);

      } else if (stmt is UpdateStmt) {
        var s = (UpdateStmt)stmt;
        r = new UpdateStmt(Tok(s.Tok), Tok(s.EndTok), s.Lhss.ConvertAll(CloneExpr), s.Rhss.ConvertAll(CloneRHS), s.CanMutateKnownState);

      } else if (stmt is VarDeclStmt) {
        var s = (VarDeclStmt)stmt;
        var lhss = s.Locals.ConvertAll(c => new LocalVariable(Tok(c.Tok), Tok(c.EndTok), c.Name, CloneType(c.OptionalType), c.IsGhost));
        r = new VarDeclStmt(Tok(s.Tok), Tok(s.EndTok), lhss, (ConcreteUpdateStatement)CloneStmt(s.Update));
      } else if (stmt is LetStmt) {
        var s = (LetStmt)stmt;
        r = new LetStmt(Tok(s.Tok), Tok(s.EndTok), s.LHSs.ConvertAll(CloneCasePattern), s.RHSs.ConvertAll(CloneExpr));
      } else if (stmt is ModifyStmt) {
        var s = (ModifyStmt)stmt;
        var mod = CloneSpecFrameExpr(s.Mod);
        var body = s.Body == null ? null : CloneBlockStmt(s.Body);
        r = new ModifyStmt(Tok(s.Tok), Tok(s.EndTok), mod.Expressions, mod.Attributes, body);
      } else if (stmt is TacnyCasesBlockStmt) {
        var s = (TacnyCasesBlockStmt)stmt;
        var guard = CloneExpr(s.Guard);
        var body = s.Body == null ? null : CloneBlockStmt(s.Body);
        r = new TacnyCasesBlockStmt(Tok(s.Tok), Tok(s.EndTok), guard, body);
      } else if (stmt is TacnyChangedBlockStmt) {
        var s = (TacnyChangedBlockStmt)stmt;
        var body = s.Body == null ? null : CloneBlockStmt(s.Body);
        r = new TacnyChangedBlockStmt(Tok(s.Tok), Tok(s.EndTok), body);
      } else if (stmt is TacnySolvedBlockStmt) {
        var s = (TacnySolvedBlockStmt)stmt;
        var body = s.Body == null ? null : CloneBlockStmt(s.Body);
        r = new TacnySolvedBlockStmt(Tok(s.Tok), Tok(s.EndTok), body);
      } else if (stmt is TacnyTryCatchBlockStmt) {
        var s = (TacnyTryCatchBlockStmt)stmt;
        var body = s.Body == null ? null : CloneBlockStmt(s.Body);
        var c = s.Ctch == null ? null : CloneBlockStmt(s.Ctch);
        r = new TacnyTryCatchBlockStmt(Tok(s.Tok), Tok(s.EndTok), body, c);
      } else if (stmt is TacticVarDeclStmt) {
        var s = (TacticVarDeclStmt)stmt;
        var lhss = s.Locals.ConvertAll(c => new LocalVariable(Tok(c.Tok), Tok(c.EndTok), c.Name, CloneType(c.OptionalType), c.IsGhost));
        r = new TacticVarDeclStmt(Tok(s.Tok), Tok(s.EndTok), lhss, (ConcreteUpdateStatement)CloneStmt(s.Update));
      } else {
        Contract.Assert(false); throw new cce.UnreachableException();  // unexpected statement
      }

      // add labels to the cloned statement
      AddStmtLabels(r, stmt.Labels);
      r.Attributes = CloneAttributes(stmt.Attributes);

      return r;
    }