Exemplo n.º 1
0
        public static List <Tuple <Function, ExistsExpr> > Collect(Expr expr)
        {
            var fv = new FunctionCollector();

            fv.VisitExpr(expr);
            return(fv.functionsUsed);
        }
Exemplo n.º 2
0
 public static List<Tuple<Function, ExistsExpr>> Collect(Expr expr)
 {
     var fv = new FunctionCollector();
     fv.VisitExpr(expr);
     return fv.functionsUsed;
 }
Exemplo n.º 3
0
        /// <summary>
        /// Computes the wlp for an assert or assume command "cmd".
        /// </summary>
        public static VCExpr P_Cmd(Block b, HashSet <string> constantsAssumed, Cmd cmd, VCExpr N, VCContext ctxt)
        {
            Contract.Requires(cmd != null);
            Contract.Requires(N != null);
            Contract.Requires(ctxt != null);
            Contract.Ensures(Contract.Result <VCExpr>() != null);

            VCExpressionGenerator gen = ctxt.Ctxt.ExprGen;

            Contract.Assert(gen != null);
            if (cmd is AssertCmd)
            {
                AssertCmd ac = (AssertCmd)cmd;
                ctxt.Ctxt.BoogieExprTranslator.isPositiveContext = !ctxt.Ctxt.BoogieExprTranslator.isPositiveContext;
                VCExpr C = ctxt.Ctxt.BoogieExprTranslator.Translate(ac.Expr);
                ctxt.Ctxt.BoogieExprTranslator.isPositiveContext = !ctxt.Ctxt.BoogieExprTranslator.isPositiveContext;
                if (CommandLineOptions.Clo.vcVariety == CommandLineOptions.VCVariety.Doomed)
                {
                    return(gen.Implies(C, N));
                }
                else
                {
                    int id = ac.UniqueId;
                    if (ctxt.Label2absy != null)
                    {
                        ctxt.Label2absy[id] = ac;
                    }

                    switch (Subsumption(ac))
                    {
                    case CommandLineOptions.SubsumptionOption.Never:
                        break;

                    case CommandLineOptions.SubsumptionOption.Always:
                        N = gen.Implies(C, N);
                        break;

                    case CommandLineOptions.SubsumptionOption.NotForQuantifiers:
                        if (!(C is VCExprQuantifier))
                        {
                            N = gen.Implies(C, N);
                        }
                        break;

                    default:
                        Contract.Assert(false); throw new cce.UnreachableException();  // unexpected case
                    }

                    // (MSchaef) Hack: This line might be useless, but at least it is not harmful
                    // need to test it
                    if (CommandLineOptions.Clo.vcVariety == CommandLineOptions.VCVariety.Doomed)
                    {
                        return(gen.Implies(C, N));
                    }

                    ctxt.AssertionCount++;
                    if (ctxt.ControlFlowVariableExpr == null)
                    {
                        Contract.Assert(ctxt.Label2absy != null);
                        return(gen.AndSimp(gen.LabelNeg(cce.NonNull(id.ToString()), C), N));
                    }
                    else
                    {
                        VCExpr controlFlowFunctionAppl = gen.ControlFlowFunctionApplication(ctxt.ControlFlowVariableExpr, gen.Integer(BigNum.FromInt(b.UniqueId)));
                        Contract.Assert(controlFlowFunctionAppl != null);
                        VCExpr assertFailure = gen.Eq(controlFlowFunctionAppl, gen.Integer(BigNum.FromInt(-ac.UniqueId)));
                        if (ctxt.Label2absy == null)
                        {
                            return(gen.AndSimp(gen.Implies(assertFailure, C), N));
                        }
                        else
                        {
                            return(gen.AndSimp(gen.LabelNeg(cce.NonNull(id.ToString()), gen.Implies(assertFailure, C)), N));
                        }
                    }
                }
            }
            else if (cmd is AssumeCmd)
            {
                AssumeCmd ac = (AssumeCmd)cmd;

                if (CommandLineOptions.Clo.StratifiedInlining > 0)
                {
                    Contract.Assert(false);
                    throw new System.Exception(string.Format("VC Generation in ICE-learning does not handle stratified inlining"));

                    var pname = QKeyValue.FindStringAttribute(ac.Attributes, "candidate");
                    if (pname != null)
                    {
                        return(gen.ImpliesSimp(gen.LabelPos("candidate_" + pname.ToString(), ctxt.Ctxt.BoogieExprTranslator.Translate(ac.Expr)), N));
                    }

                    // Label the assume if it is a procedure call
                    NAryExpr naryExpr = ac.Expr as NAryExpr;
                    if (naryExpr != null)
                    {
                        if (naryExpr.Fun is FunctionCall)
                        {
                            int id = ac.UniqueId;
                            ctxt.Label2absy[id] = ac;
                            return(gen.ImpliesSimp(gen.LabelPos(cce.NonNull("si_fcall_" + id.ToString()), ctxt.Ctxt.BoogieExprTranslator.Translate(ac.Expr)), N));
                        }
                    }
                }
                //return gen.ImpliesSimp(ctxt.Ctxt.BoogieExprTranslator.Translate(ac.Expr), N);
                else
                {
                    if (constantsAssumed == null)
                    {
                        return(gen.ImpliesSimp(ctxt.Ctxt.BoogieExprTranslator.Translate(ac.Expr), N));
                    }

                    // check if the assume command contains any of the constantsAssumed (corresponding to existential function calls)?
                    var  funcsUsed             = FunctionCollector.Collect(ac.Expr);
                    bool hasAnyConstantAssumed = false;

                    foreach (var tup in funcsUsed)
                    {
                        var constantFunc = tup.Item1;
                        if (constantsAssumed.Contains(constantFunc.Name))
                        {
                            hasAnyConstantAssumed = true;
                            break;
                        }
                    }

                    if (hasAnyConstantAssumed)
                    {
                        int id = ac.UniqueId;
                        if (ctxt.Label2absy != null)
                        {
                            ctxt.Label2absy[id] = ac;
                        }

                        //ctxt.AssertionCount++;
                        if (ctxt.ControlFlowVariableExpr == null)
                        {
                            Contract.Assert(ctxt.Label2absy != null);
                            return(gen.ImpliesSimp(gen.LabelPos(cce.NonNull(id.ToString()), ctxt.Ctxt.BoogieExprTranslator.Translate(ac.Expr)), N));
                        }
                        else
                        {
                            return(gen.ImpliesSimp(ctxt.Ctxt.BoogieExprTranslator.Translate(ac.Expr), N));

                            /*VCExpr controlFlowFunctionAppl = gen.ControlFlowFunctionApplication(ctxt.ControlFlowVariableExpr, gen.Integer(BigNum.FromInt(b.UniqueId)));
                             * Contract.Assert(controlFlowFunctionAppl != null);
                             * VCExpr assumeFailure = gen.Eq(controlFlowFunctionAppl, gen.Integer(BigNum.FromInt(-ac.UniqueId)));
                             * if (ctxt.Label2absy == null)
                             * {
                             *  throw new System.Exception(string.Format("Dont know how to handle labels for the assume cmd"));
                             *  //return gen.ImpliesSimp(gen.LabelPos(cce.NonNull(id.ToString()), gen.Implies(assumeFailure, ctxt.Ctxt.BoogieExprTranslator.Translate(ac.Expr))), N);
                             * }
                             * else
                             * {
                             *  return gen.ImpliesSimp(gen.LabelPos(cce.NonNull(id.ToString()), gen.Implies(assumeFailure, ctxt.Ctxt.BoogieExprTranslator.Translate(ac.Expr))), N);
                             * }
                             */
                        }
                    }
                    else
                    {
                        return(gen.ImpliesSimp(ctxt.Ctxt.BoogieExprTranslator.Translate(ac.Expr), N));
                    }
                }
            }
            else
            {
                Console.WriteLine(cmd.ToString());
                Contract.Assert(false); throw new cce.UnreachableException(); // unexpected command
            }
        }