Пример #1
0
 public bool isNonPreRequire(bpl.Requires ens)
 {
     if (ens == null)
     {
         return(false);
     }
     return(!hasAttrKey(ens.Attributes, preCondAttr));
 }
    public IEnumerable<Bpl.Requires> getPreconditionTranslation(IMethodContract contract) {
      ICollection<Bpl.Requires> translatedPres = new List<Bpl.Requires>();
      foreach (IPrecondition pre in contract.Preconditions) {
        var stmtTraverser = this.Factory.MakeStatementTraverser(sink, null, true);
        ExpressionTraverser exptravers = this.Factory.MakeExpressionTraverser(sink, stmtTraverser, true);
        exptravers.Traverse(pre.Condition); // TODO
        // Todo: Deal with Descriptions
        var req = new Bpl.Requires(pre.Token(), false, exptravers.TranslatedExpressions.Pop(), "");
        translatedPres.Add(req);
      }

      return translatedPres;
    }
Пример #3
0
        public IEnumerable <Bpl.Requires> getPreconditionTranslation(IMethodContract contract)
        {
            ICollection <Bpl.Requires> translatedPres = new List <Bpl.Requires>();

            foreach (IPrecondition pre in contract.Preconditions)
            {
                var stmtTraverser = this.Factory.MakeStatementTraverser(sink, null, true);
                ExpressionTraverser exptravers = this.Factory.MakeExpressionTraverser(sink, stmtTraverser, true);
                exptravers.Traverse(pre.Condition); // TODO
                // Todo: Deal with Descriptions
                var req = new Bpl.Requires(pre.Token(), false, exptravers.TranslatedExpressions.Pop(), "");
                translatedPres.Add(req);
            }

            return(translatedPres);
        }
Пример #4
0
 public override Requires VisitRequires(Requires requires)
 {
     return requires;
 }
Пример #5
0
 public override Requires VisitRequires(Requires requires)
 {
     Contract.Ensures(Contract.Result<Requires>() == requires);
     this.VisitExpr(requires.Condition);
     return requires;
 }
Пример #6
0
 public virtual Requires VisitRequires(Requires @requires) {
   Contract.Requires(@requires != null);
   Contract.Ensures(Contract.Result<Requires>() != null);
   @requires.Condition = this.VisitExpr(@requires.Condition);
   return @requires;
 }
Пример #7
0
 public override Requires VisitRequires(Requires node)
 {
     CivlAttributes.RemoveLayerAttribute(node);
     return base.VisitRequires(node);
 }
Пример #8
0
 public override Requires VisitRequires(Requires requires)
 {
     sharedVarsAccessed = new HashSet<Variable>();
     Debug.Assert(introducedLocalVarsUpperBound == int.MinValue);
     base.VisitRequires(requires);
     CheckAndAddLayers(requires, requires.Attributes, procToActionInfo[enclosingProc].createdAtLayerNum);
     if (introducedLocalVarsUpperBound > Least(FindLayers(requires.Attributes)))
     {
         Error(requires, "An introduced local variable is accessed but not available");
     }
     introducedLocalVarsUpperBound = int.MinValue;
     sharedVarsAccessed = null;
     return requires;
 }
Пример #9
0
 private Cmd InlinedRequires(CallCmd callCmd, Requires req) {
   Requires/*!*/ reqCopy = (Requires/*!*/)cce.NonNull(req.Clone());
   if (req.Free)
     reqCopy.Condition = Expr.True;
   else 
     reqCopy.Condition = codeCopier.CopyExpr(req.Condition);
   AssertCmd/*!*/ a = new AssertRequiresCmd(callCmd, reqCopy);
   a.ErrorDataEnhanced = reqCopy.ErrorDataEnhanced;
   return a;
 }
Пример #10
0
 public override Requires VisitRequires(Requires requires)
 {
     minLayerNum = int.MaxValue;
     maxLayerNum = -1;
     canAccessSharedVars = true;
     Requires ret = base.VisitRequires(requires);
     canAccessSharedVars = false;
     CheckAndAddLayers(requires, requires.Attributes, procToActionInfo[enclosingProc].createdAtLayerNum);
     return ret;
 }
Пример #11
0
 public override Requires VisitRequires(Requires node)
 {
     node.Attributes = RemoveLayerAttribute(node.Attributes);
     return base.VisitRequires(node);
 }
Пример #12
0
        private void ReportOutcomePreconditionFailed(IToken callTok, Requires req, string addComment)
        {
            string/*?*/ comment = req.Comment;
              IToken reqTok = req.tok;
              if (comment != null) comment = ": " + comment; else comment = "";
              comment += addComment;

              // in case of testsuite, don't print the full paths to prelude
              // also skip line numbers as they change
              bool isPrelude = reqTok.filename.EndsWith(".bpl") || reqTok.filename.EndsWith(".bpl>");
              if (isPrelude && reqTok.line > 1) {
            string line = VccCommandLineHost.StandardPreludeLines[reqTok.line - 2];
            int idx = line.IndexOf("TOKEN:");
            if (idx > 0) {
              reqTok.val = line.Substring(idx + 7);
            } else {
              line = VccCommandLineHost.StandardPreludeLines[reqTok.line - 1];
              idx = line.IndexOf("requires");
              reqTok.val = idx >= 0 ? line.Substring(idx + 8) : line;
            }
              }
              if (commandLineOptions != null && commandLineOptions.RunTestSuite && isPrelude) {
            reqTok.filename = "Vcc3Prelude.bpl";
            reqTok.line = 0;
            reqTok.col = 0;
              }

              string reqMsg = reqTok.val;
              ErrorCode errNo = GetErrorNumber(ref reqMsg, ErrorCode.PreconditionFailed);

              if (IsStandaloneError(errNo)) {
            ReportError(callTok, errNo, "{0} (in call '{1}'){2}.", reqMsg, RemoveWhiteSpace(callTok.val), comment);
              } else {
            if (ReportError(callTok, errNo, "Call '{0}' did not verify{1}.", RemoveWhiteSpace(callTok.val), comment)) {
              ReportRelated(reqTok, "Precondition: '{0}'.", reqMsg);
              ReportAllRelated(reqTok);
            }
              }
        }
Пример #13
0
 public AssertRequiresCmd(CallCmd/*!*/ call, Requires/*!*/ requires)
     : base(call.tok, requires.Condition)
 {
     Contract.Requires(call != null);
       Contract.Requires(requires != null);
       this.Call = call;
       this.Requires = requires;
 }
Пример #14
0
 public override Requires VisitRequires(Requires requires)
 {
     int phaseNum = QKeyValue.FindIntAttribute(requires.Attributes, "phase", int.MaxValue);
     assertionPhaseNums.Add(phaseNum);
     if (phaseNum > enclosingProcPhaseNum)
     {
         Error(requires, "The phase of requires clause cannot be greater than the phase of enclosing procedure");
     }
     return requires;
 }
Пример #15
0
    /// <summary>
    /// 
    /// </summary>
    public override void Visit(IMethodDefinition method) {

      Dictionary<IParameterDefinition, MethodParameter> formalMap = new Dictionary<IParameterDefinition, MethodParameter>();
      this.sink.BeginMethod();

      try {
        #region Create in- and out-parameters

        int in_count = 0;
        int out_count = 0;
        MethodParameter mp;
        foreach (IParameterDefinition formal in method.ResolvedMethod.Parameters) {

          mp = new MethodParameter(formal);
          if (mp.inParameterCopy != null) in_count++;
          if (mp.outParameterCopy != null && (formal.IsByReference || formal.IsOut))
            out_count++;
          formalMap.Add(formal, mp);
        }
        this.sink.FormalMap = formalMap;

        #region Look for Returnvalue

        // This is just a hack, should be replaced with something more robust
        if (method.Type.TypeCode != PrimitiveTypeCode.Void) {
          Bpl.Type rettype = Bpl.Type.Int;
          out_count++;
          this.sink.RetVariable = new Bpl.Formal(method.Token(),
              new Bpl.TypedIdent(method.Type.Token(),
                  "$result", rettype), false);
        } else {
          this.sink.RetVariable = null;
        }

        #endregion

        #region Create 'this' parameter
        in_count++;
        Bpl.Type selftype = Bpl.Type.Int;
        Bpl.Formal self = new Bpl.Formal(method.Token(),
            new Bpl.TypedIdent(method.Type.Token(),
                "this", selftype), true);

        #endregion

        Bpl.Variable[] invars = new Bpl.Formal[in_count];
        Bpl.Variable[] outvars = new Bpl.Formal[out_count];

        int i = 0;
        int j = 0;

        #region Add 'this' parameter as first in parameter
        invars[i++] = self;
        #endregion

        foreach (MethodParameter mparam in formalMap.Values) {
          if (mparam.inParameterCopy != null) {
            invars[i++] = mparam.inParameterCopy;
          }
          if (mparam.outParameterCopy != null) {
            if (mparam.underlyingParameter.IsByReference || mparam.underlyingParameter.IsOut)
              outvars[j++] = mparam.outParameterCopy;
          }
        }

        #region add the returnvalue to out if there is one
        if (this.sink.RetVariable != null) outvars[j] = this.sink.RetVariable;
        #endregion

        #endregion

        #region Check The Method Contracts
        Bpl.RequiresSeq boogiePrecondition = new Bpl.RequiresSeq();
        Bpl.EnsuresSeq boogiePostcondition = new Bpl.EnsuresSeq();
        Bpl.IdentifierExprSeq boogieModifies = new Bpl.IdentifierExprSeq();

        IMethodContract contract = ContractProvider.GetMethodContractFor(method);

        if (contract != null) {
          try {
            foreach (IPrecondition pre in contract.Preconditions) {
              ExpressionTraverser exptravers = this.factory.MakeExpressionTraverser(this.sink, null);
              exptravers.Visit(pre.Condition); // TODO
              // Todo: Deal with Descriptions


              Bpl.Requires req
                  = new Bpl.Requires(pre.Token(),
                      true, exptravers.TranslatedExpressions.Pop(), "");
              boogiePrecondition.Add(req);
            }

            foreach (IPostcondition post in contract.Postconditions) {
              ExpressionTraverser exptravers = this.factory.MakeExpressionTraverser(this.sink, null);

              exptravers.Visit(post.Condition);
              // Todo: Deal with Descriptions

              Bpl.Ensures ens =
                  new Bpl.Ensures(post.Token(),
                      true, exptravers.TranslatedExpressions.Pop(), "");
              boogiePostcondition.Add(ens);
            }

            foreach (IAddressableExpression mod in contract.ModifiedVariables) {
              ExpressionTraverser exptravers = this.factory.MakeExpressionTraverser(this.sink, null);
              exptravers.Visit(mod);

              Bpl.IdentifierExpr idexp = exptravers.TranslatedExpressions.Pop() as Bpl.IdentifierExpr;

              if (idexp == null) {
                throw new TranslationException(String.Format("Cannot create IdentifierExpr for Modifyed Variable {0}", mod.ToString()));
              }
              boogieModifies.Add(idexp);
            }
          } catch (TranslationException te) {
            throw new NotImplementedException("Cannot Handle Errors in Method Contract: " + te.ToString());
          } catch {
            throw;
          }
        }

        #endregion

        string MethodName = TranslationHelper.CreateUniqueMethodName(method);

        Bpl.Procedure proc = new Bpl.Procedure(method.Token(),
            MethodName, // make it unique!
            new Bpl.TypeVariableSeq(),
            new Bpl.VariableSeq(invars), // in
            new Bpl.VariableSeq(outvars), // out
            boogiePrecondition,
            boogieModifies,
            boogiePostcondition);

        this.sink.TranslatedProgram.TopLevelDeclarations.Add(proc);

        if (method.IsAbstract) {
          throw new NotImplementedException("abstract methods are not yet implemented");
        }

        StatementTraverser stmtTraverser = this.factory.MakeStatementTraverser(this.sink);

        #region Add assignements from In-Params to local-Params

        foreach (MethodParameter mparam in formalMap.Values) {
          if (mparam.inParameterCopy != null) {
            Bpl.IToken tok = method.Token();
            stmtTraverser.StmtBuilder.Add(Bpl.Cmd.SimpleAssign(tok,
              new Bpl.IdentifierExpr(tok, mparam.outParameterCopy),
              new Bpl.IdentifierExpr(tok, mparam.inParameterCopy)));
          }
        }

        #endregion

        try {
          method.ResolvedMethod.Body.Dispatch(stmtTraverser);
        } catch (TranslationException te) {
          throw new NotImplementedException("No Errorhandling in Methodvisitor / " + te.ToString());
        } catch {
          throw;
        }

        #region Create Local Vars For Implementation
        List<Bpl.Variable> vars = new List<Bpl.Variable>();
        foreach (MethodParameter mparam in formalMap.Values) {
          if (!(mparam.underlyingParameter.IsByReference || mparam.underlyingParameter.IsOut))
            vars.Add(mparam.outParameterCopy);
        }
        foreach (Bpl.Variable v in this.sink.LocalVarMap.Values) {
          vars.Add(v);
        }

        Bpl.VariableSeq vseq = new Bpl.VariableSeq(vars.ToArray());
        #endregion

        Bpl.Implementation impl =
            new Bpl.Implementation(method.Token(),
                MethodName, // make unique
                new Microsoft.Boogie.TypeVariableSeq(),
                new Microsoft.Boogie.VariableSeq(invars),
                new Microsoft.Boogie.VariableSeq(outvars),
                vseq,
                stmtTraverser.StmtBuilder.Collect(Bpl.Token.NoToken));

        impl.Proc = proc;
        this.sink.TranslatedProgram.TopLevelDeclarations.Add(impl);
      } catch (TranslationException te) {
        throw new NotImplementedException(te.ToString());
      } catch {
        throw;
      } finally {
        // Maybe this is a good place to add the procedure to the toplevel declarations
      }
    }
Пример #16
0
 public override Requires VisitRequires(Requires node)
 {
     Requires requires = base.VisitRequires(node);
     if (node.Free)
         return requires;
     if (!civlTypeChecker.absyToLayerNums[node].Contains(layerNum))
         requires.Condition = Expr.True;
     return requires;
 }
Пример #17
0
 public override Requires VisitRequires(Requires node)
 {
     //Contract.Requires(node != null);
     Contract.Ensures(Contract.Result<Requires>() != null);
     return base.VisitRequires((Requires)node.Clone());
 }
Пример #18
0
 public override Requires VisitRequires(Requires node)
 {
     Requires requires = base.VisitRequires(node);
     if (node.Free)
         return requires;
     if (QKeyValue.FindIntAttribute(requires.Attributes, "phase", int.MaxValue) != phaseNum)
         requires.Condition = Expr.True;
     return requires;
 }