Example #1
0
        public bpl.Expr guardWithExistConst(bpl.Expr expr)
        {
            var existsConst = new bpl.Constant(bpl.Token.NoToken, new bpl.TypedIdent(bpl.Token.NoToken, existsConstName + (numExistsConst++), bpl.Type.Bool));

            existsConsts.Add(existsConst);
            return(bpl.NAryExpr.Imp(new bpl.IdentifierExpr(bpl.Token.NoToken, existsConst), expr));
        }
        /// <summary>
        /// Creates a fresh BPL variable to represent <paramref name="type"/>, deciding
        /// on its type based on the heap representation. I.e., the value of this
        /// variable represents the value of the expression "typeof(type)".
        /// </summary>
        public Bpl.Variable CreateTypeVariable(ITypeReference type, List <Bpl.ConstantParent> parents)
        {
            string typename = TypeHelper.GetTypeName(type, NameFormattingOptions.DocumentationId);

            typename = TranslationHelper.TurnStringIntoValidIdentifier(typename);
            Bpl.IToken     tok    = type.Token();
            Bpl.TypedIdent tident = new Bpl.TypedIdent(tok, typename, this.TypeType);
            Bpl.Constant   v      = new Bpl.Constant(tok, tident, true /*unique*/, parents, false, null);
            return(v);
        }
Example #3
0
        public bpl.Declaration getExistConstDeclaration(bpl.Variable vbl)
        {
            var res       = new bpl.Constant(bpl.Token.NoToken, new bpl.TypedIdent(bpl.Token.NoToken, vbl.Name, vbl.TypedIdent.Type), false);
            var paramList = new List <object>();

            paramList.Add(new bpl.LiteralExpr(bpl.Token.NoToken, true));
            var attr = new bpl.QKeyValue(bpl.Token.NoToken, existsConstAttr, paramList, null);

            attr.Next      = res.Attributes;
            res.Attributes = attr;
            return(res);
        }
Example #4
0
 private void trackPhonePageNameVariableName(ITypeDefinition typeDef)
 {
     if (PhoneCodeHelper.instance().PhonePlugin != null && typeDef.isPhoneApplicationPageClass(sink.host))
     {
         INamespaceTypeDefinition namedTypeDef = typeDef as INamespaceTypeDefinition;
         string fullyQualifiedName             = namedTypeDef.ToString();
         string xamlForClass = PhoneCodeHelper.instance().getXAMLForPage(fullyQualifiedName);
         if (xamlForClass != null) // if not it is possibly an abstract page
         {
             string       uriName     = UriHelper.getURIBase(xamlForClass);
             Bpl.Constant uriConstant = sink.FindOrCreateConstant(uriName);
             PhoneCodeHelper.instance().setBoogieStringPageNameForPageClass(fullyQualifiedName, uriConstant.Name);
         }
     }
 }
Example #5
0
        public override Bpl.Variable CreateEventVariable(IEventDefinition e)
        {
            Bpl.Variable v;
            string       fieldname = MemberHelper.GetMemberSignature(e, NameFormattingOptions.DocumentationId);

            fieldname = TranslationHelper.TurnStringIntoValidIdentifier(fieldname);
            Bpl.IToken tok = e.Token();

            if (e.Adder.ResolvedMethod.IsStatic)
            {
                Bpl.Type       t      = this.sink.CciTypeToBoogie(e.Type.ResolvedType);
                Bpl.TypedIdent tident = new Bpl.TypedIdent(tok, fieldname, t);
                v = new Bpl.GlobalVariable(tok, tident);
            }
            else
            {
                Bpl.Type       t      = this.FieldType;
                Bpl.TypedIdent tident = new Bpl.TypedIdent(tok, fieldname, t);
                v = new Bpl.Constant(tok, tident, true);
            }
            return(v);
        }
Example #6
0
 public virtual Constant VisitConstant(Constant node) {
   Contract.Requires(node != null);
   Contract.Ensures(Contract.Result<Constant>() != null);
   return node;
 }
Example #7
0
    // Generate the axiom ensuring that the sub-dags underneath unique
    // child-parent edges are all disjoint
    private VCExpr GenUniqueParentConstraint(Constant child, Constant parent) {
      Contract.Requires(child != null);
      Contract.Requires(parent != null);
      Contract.Requires(child.TypedIdent.Type.Equals(parent.TypedIdent.Type));
      Contract.Ensures(Contract.Result<VCExpr>() != null);



      VCExprVar w = Gen.Variable("w", child.TypedIdent.Type);
      Contract.Assert(w != null);
      VCExpr antecedent =
        Gen.AtMost(w, Translator.LookupVariable(child));
      Contract.Assert(antecedent != null);
      VCExpr succedent =
        Gen.Eq(Gen.Function(OneStepFunFor(child.TypedIdent.Type),
                            Translator.LookupVariable(parent), w),
               Translator.LookupVariable(child));
      Contract.Assert(succedent != null);

      return Gen.Forall(w,
                        Gen.Trigger(true, antecedent),
                        Gen.Implies(antecedent, succedent));
    }
Example #8
0
    // Generate axioms that state that all direct children of c are
    // specified; this is the dual of the axiom stating that all direct
    // ancestors of a constant are known
    private VCExpr GenCompleteChildrenConstraints(Constant c) {
      Contract.Requires(c != null);
      Contract.Requires(c.ChildrenComplete);
      Contract.Ensures(Contract.Result<VCExpr>() != null);


      VCExprVar cAsVar = Translator.LookupVariable(c);
      VCExprVar w = Gen.Variable("w", c.TypedIdent.Type);

      VCExpr maxDescendants = Gen.Eq(cAsVar, w);
      foreach (Constant d in Constants) {
        Contract.Assert(d != null);
        if (d.Parents != null && d.Parents.Any(p => c.Equals(p.Parent.Decl)))
          maxDescendants = Gen.Or(maxDescendants,
                                  Gen.AtMost(w, Translator.LookupVariable(d)));
      }

      VCExpr antecedent = Gen.AtMost(w, cAsVar);
      Contract.Assert(antecedent != null);
      return Gen.Forall(w,
                        Gen.Trigger(true, antecedent),
                        Gen.Implies(antecedent, maxDescendants));
    }
Example #9
0
    // Generate the constraints telling that parents of a constant are
    // strictly greater than the constant itself, and are the minimal
    // elements with this property
    private VCExpr GenParentConstraints(Constant c) {
      Contract.Requires(c != null);
      Contract.Ensures(Contract.Result<VCExpr>() != null);

      VCExpr res = VCExpressionGenerator.True;

      if (c.Parents == null)
        return res;

      VCExprVar cAsVar = Translator.LookupVariable(c);
      VCExprVar w = Gen.Variable("w", c.TypedIdent.Type);

      // Parents of c are proper ancestors of c
      foreach (ConstantParent p in c.Parents) {
        Contract.Assert(p != null);
        VCExprVar par = Translator.LookupVariable(cce.NonNull(p.Parent.Decl));
        res = Gen.AndSimp(res, Gen.Neq(cAsVar, par));
        res = Gen.AndSimp(res, Gen.AtMost(cAsVar, par));
      }

      // Parents are direct ancestors of c (no other elements are in
      // between c and a parent)
      foreach (ConstantParent p in c.Parents) {
        Contract.Assert(p != null);
        VCExprVar par = Translator.LookupVariable(cce.NonNull(p.Parent.Decl));
        Contract.Assert(par != null);
        VCExpr antecedent1 = Gen.AtMost(cAsVar, w);
        Contract.Assert(antecedent1 != null);
        VCExpr antecedent2 = Gen.AtMost(w, par);
        Contract.Assert(antecedent2 != null);
        VCExpr body = Gen.Implies(Gen.And(antecedent1, antecedent2),
                                   Gen.Or(Gen.Eq(cAsVar, w), Gen.Eq(par, w)));
        Contract.Assert(body != null);
        res = Gen.AndSimp(res,
                          Gen.Forall(w,
                                     Gen.Trigger(true, antecedent1, antecedent2),
                                     body));
      }

      // Ancestors of c are only c itself and the ancestors of the
      // parents of c
      VCExpr minAncestors = Gen.Eq(cAsVar, w);
      Contract.Assert(minAncestors != null);
      foreach (ConstantParent p in c.Parents) {
        Contract.Assert(p != null);
        minAncestors =
          Gen.Or(minAncestors,
                 Gen.AtMost(Translator.LookupVariable(cce.NonNull(p.Parent.Decl)), w));
      }
      VCExpr antecedent = Gen.AtMost(cAsVar, w);
      Contract.Assert(antecedent != null);
      res = Gen.AndSimp(res,
                        Gen.Forall(w,
                                   Gen.Trigger(true, antecedent),
                                   Gen.Implies(antecedent, minAncestors)));

      // Constraints for unique child-parent edges
      foreach (ConstantParent p in c.Parents) {
        Contract.Assert(p != null);
        if (p.Unique)
          res =
            Gen.AndSimp(res,
                        GenUniqueParentConstraint(c, cce.NonNull((Constant)p.Parent.Decl)));
      }

      return res;
    }
Example #10
0
    ////////////////////////////////////////////////////////////////////////////

    public void AddConstant(Constant c) {
      Contract.Requires(c != null);
      AddAxiom(GenParentConstraints(c));
      Constants.Add(c);
      if (c.ChildrenComplete)
        CompleteConstantsOpen.Add(c);

      // ensure that no further children are added to closed
      // children-complete constants
      Contract.Assert(!(c.Parents != null && Contract.Exists(c.Parents, p => cce.NonNull((Constant)p.Parent.Decl).ChildrenComplete && !CompleteConstantsOpen.Contains((Constant)p.Parent.Decl))));
    }
Example #11
0
            public LazyInliningInfo(Implementation impl, Program program, ProverContext ctxt, int uniqueId, GlobalVariable errorVariable)
            {
                Contract.Requires(impl != null);
                Contract.Requires(program != null);
                Procedure proc = cce.NonNull(impl.Proc);

                this.impl = impl;
                this.uniqueId = uniqueId;
                this.controlFlowVariable = new LocalVariable(Token.NoToken, new TypedIdent(Token.NoToken, "cfc", Microsoft.Boogie.Type.Int));
                impl.LocVars.Add(controlFlowVariable);

                List<Variable> interfaceVars = new List<Variable>();
                Expr assertExpr = new LiteralExpr(Token.NoToken, true);
                Contract.Assert(assertExpr != null);
                foreach (Variable v in program.GlobalVariables())
                {
                    Contract.Assert(v != null);
                    interfaceVars.Add(v);
                    if (v.Name == "error")
                        inputErrorVariable = v;
                }
                // InParams must be obtained from impl and not proc
                foreach (Variable v in impl.InParams)
                {
                    Contract.Assert(v != null);
                    interfaceVars.Add(v);
                }
                // OutParams must be obtained from impl and not proc
                foreach (Variable v in impl.OutParams)
                {
                    Contract.Assert(v != null);
                    Constant c = new Constant(Token.NoToken,
                                              new TypedIdent(Token.NoToken, impl.Name + "_" + v.Name, v.TypedIdent.Type));
                    interfaceVars.Add(c);
                    Expr eqExpr = Expr.Eq(new IdentifierExpr(Token.NoToken, c), new IdentifierExpr(Token.NoToken, v));
                    assertExpr = Expr.And(assertExpr, eqExpr);
                }
                if (errorVariable != null)
                {
                    proc.Modifies.Add(new IdentifierExpr(Token.NoToken, errorVariable));
                }
                foreach (IdentifierExpr e in proc.Modifies)
                {
                    Contract.Assert(e != null);
                    if (e.Decl == null)
                        continue;
                    Variable v = e.Decl;
                    Constant c = new Constant(Token.NoToken, new TypedIdent(Token.NoToken, impl.Name + "_" + v.Name, v.TypedIdent.Type));
                    interfaceVars.Add(c);
                    if (v.Name == "error")
                    {
                        outputErrorVariable = c;
                        continue;
                    }
                    Expr eqExpr = Expr.Eq(new IdentifierExpr(Token.NoToken, c), new IdentifierExpr(Token.NoToken, v));
                    assertExpr = Expr.And(assertExpr, eqExpr);
                }

                this.interfaceVars = interfaceVars;
                this.assertExpr = Expr.Not(assertExpr);
                List<Variable> functionInterfaceVars = new List<Variable>();
                foreach (Variable v in interfaceVars)
                {
                    Contract.Assert(v != null);
                    functionInterfaceVars.Add(new Formal(Token.NoToken, new TypedIdent(Token.NoToken, v.Name, v.TypedIdent.Type), true));
                }
                TypedIdent ti = new TypedIdent(Token.NoToken, "", Microsoft.Boogie.Type.Bool);
                Contract.Assert(ti != null);
                Formal returnVar = new Formal(Token.NoToken, ti, false);
                Contract.Assert(returnVar != null);
                this.function = new Function(Token.NoToken, proc.Name, functionInterfaceVars, returnVar);
                ctxt.DeclareFunction(this.function, "");

                interfaceVarCopies = new List<List<Variable>>();
                int temp = 0;
                for (int i = 0; i < /* CommandLineOptions.Clo.ProcedureCopyBound */ 0; i++)
                {
                    interfaceVarCopies.Add(new List<Variable>());
                    foreach (Variable v in interfaceVars)
                    {
                        Constant constant = new Constant(Token.NoToken, new TypedIdent(Token.NoToken, v.Name + temp++, v.TypedIdent.Type));
                        interfaceVarCopies[i].Add(constant);
                        //program.TopLevelDeclarations.Add(constant);
                    }
                }
            }
Example #12
0
 public Constant MakeExistentialBoolean(int StageId) {
   Constant ExistentialBooleanConstant = new Constant(Token.NoToken, new TypedIdent(tok, "_b" + invariantGenerationCounter, Microsoft.Boogie.Type.Bool), false);
   invariantGenerationCounter++;
   ExistentialBooleanConstant.AddAttribute("existential", new object[] { Expr.True });
   ExistentialBooleanConstant.AddAttribute("stage_id", new object[] { new LiteralExpr(Token.NoToken, Microsoft.Basetypes.BigNum.FromInt(StageId)) });
   TopLevelDeclarations.Add(ExistentialBooleanConstant);
   return ExistentialBooleanConstant;
 }
Example #13
0
 public virtual void DeclareConstant(Constant c, bool uniq, string attributes)
 {
     Contract.Requires(c != null); ProcessDeclaration(c);
 }
Example #14
0
 public override Constant VisitConstant(Constant node)
 {
     Contract.Ensures(Contract.Result<Constant>() == node);
     return node;
 }
Example #15
0
 public override Constant VisitConstant(Constant node)
 {
     node.tok = Token.NoToken;
     TokenCount++; 
     return base.VisitConstant(node);
 }
Example #16
0
    public override Bpl.Variable CreateEventVariable(IEventDefinition e) {
      Bpl.Variable v;
      string fieldname = MemberHelper.GetMemberSignature(e, NameFormattingOptions.DocumentationId);
      fieldname = TranslationHelper.TurnStringIntoValidIdentifier(fieldname);
      Bpl.IToken tok = e.Token();

      if (e.Adder.ResolvedMethod.IsStatic) {
        Bpl.Type t = this.sink.CciTypeToBoogie(e.Type.ResolvedType);
        Bpl.TypedIdent tident = new Bpl.TypedIdent(tok, fieldname, t);
        v = new Bpl.GlobalVariable(tok, tident);
      }
      else {
        Bpl.Type t = this.FieldType;
        Bpl.TypedIdent tident = new Bpl.TypedIdent(tok, fieldname, t);
        v = new Bpl.Constant(tok, tident, true);
      }
      return v;
    }
Example #17
0
        private static void CreateDispatchMethod(Sink sink, ITypeDefinition type, HashSet <IMethodDefinition> delegates)
        {
            Contract.Assert(type.IsDelegate);
            IMethodDefinition invokeMethod = null;

            foreach (IMethodDefinition m in type.Methods)
            {
                if (m.Name.Value == "Invoke")
                {
                    invokeMethod = m;
                    break;
                }
            }

            try {
                IMethodDefinition  unspecializedInvokeMethod = Sink.Unspecialize(invokeMethod).ResolvedMethod;
                Sink.ProcedureInfo invokeProcedureInfo       = sink.FindOrCreateProcedure(unspecializedInvokeMethod);
                Bpl.Procedure      invokeProcedure           = (Bpl.Procedure)invokeProcedureInfo.Decl;
                invokeProcedure.AddAttribute("inline", Bpl.Expr.Literal(1));
                Bpl.Formal delegateVariable = invokeProcedureInfo.ThisVariable;
                Bpl.IToken token            = invokeMethod.Token();

                List <Bpl.Variable> dispatchProcInExprs = new List <Bpl.Variable>();
                for (int i = 1; i < invokeProcedure.InParams.Count; i++)
                {
                    Bpl.Variable v = invokeProcedure.InParams[i];
                    dispatchProcInExprs.Add(v);
                }
                List <Bpl.Variable> dispatchProcOutExprs = new List <Bpl.Variable>();
                foreach (Bpl.Variable v in invokeProcedure.OutParams)
                {
                    dispatchProcOutExprs.Add(v);
                }

                List <Bpl.Variable> localVariables = new List <Bpl.Variable>();
                Bpl.StmtListBuilder stmtBuilder    = new Bpl.StmtListBuilder();
                int localCounter = 0;
                foreach (IMethodDefinition defn in delegates)
                {
                    Bpl.Constant       c = sink.FindOrCreateDelegateMethodConstant(defn);
                    Sink.ProcedureInfo delegateProcedureInfo = sink.FindOrCreateProcedure(defn);
                    Bpl.Procedure      delegateProcedure     = (Bpl.Procedure)delegateProcedureInfo.Decl;
                    Bpl.Formal         thisVariable          = delegateProcedureInfo.ThisVariable;
                    int numArguments = defn.ParameterCount;

                    List <Bpl.Variable> tempInputs  = new List <Bpl.Variable>();
                    List <Bpl.Variable> tempOutputs = new List <Bpl.Variable>();

                    for (int i = 0; i < defn.ParameterCount; i++)
                    {
                        Bpl.Variable      v             = delegateProcedure.InParams[(thisVariable == null ? 0 : 1) + i];
                        Bpl.LocalVariable localVariable = new Bpl.LocalVariable(Bpl.Token.NoToken,
                                                                                new Bpl.TypedIdent(Bpl.Token.NoToken, "local" + localCounter++, v.TypedIdent.Type));
                        localVariables.Add(localVariable);
                        tempInputs.Add(localVariable);
                    }

                    for (int i = 0; i < delegateProcedure.OutParams.Count; i++)
                    {
                        Bpl.Variable      v             = delegateProcedure.OutParams[i];
                        Bpl.LocalVariable localVariable = new Bpl.LocalVariable(Bpl.Token.NoToken,
                                                                                new Bpl.TypedIdent(Bpl.Token.NoToken, "local" + localCounter++, v.TypedIdent.Type));
                        localVariables.Add(localVariable);
                        tempOutputs.Add(localVariable);
                    }

                    List <Bpl.Expr>           ins  = new List <Bpl.Expr>();
                    List <Bpl.IdentifierExpr> outs = new List <Bpl.IdentifierExpr>();
                    if (!defn.IsStatic)
                    {
                        ins.Add(sink.ReadReceiver(Bpl.Expr.Ident(c), Bpl.Expr.Ident(delegateVariable)));
                    }
                    for (int i = 0; i < tempInputs.Count; i++)
                    {
                        ins.Add(Bpl.Expr.Ident(tempInputs[i]));
                    }
                    if (defn.IsGeneric)
                    {
                        for (int i = 0; i < defn.GenericParameterCount; i++)
                        {
                            ins.Add(new Bpl.NAryExpr(Bpl.Token.NoToken,
                                                     new Bpl.FunctionCall(sink.FindOrCreateTypeParameterFunction(i)),
                                                     new List <Bpl.Expr>(new Bpl.Expr[] { sink.ReadTypeParameters(Bpl.Expr.Ident(c), Bpl.Expr.Ident(delegateVariable)) })));
                        }
                    }
                    if (defn.IsStatic)
                    {
                        int numTypeParameters = Sink.ConsolidatedGenericParameterCount(defn.ContainingType);
                        for (int i = 0; i < numTypeParameters; i++)
                        {
                            ins.Add(new Bpl.NAryExpr(Bpl.Token.NoToken,
                                                     new Bpl.FunctionCall(sink.FindOrCreateTypeParameterFunction(i)),
                                                     new List <Bpl.Expr>(new Bpl.Expr[] { sink.ReadTypeParameters(Bpl.Expr.Ident(c), Bpl.Expr.Ident(delegateVariable)) })));
                        }
                    }
                    for (int i = 0; i < tempOutputs.Count; i++)
                    {
                        outs.Add(Bpl.Expr.Ident(tempOutputs[i]));
                    }

                    Bpl.Expr            bexpr         = sink.ReadMethod(Bpl.Expr.Ident(c), Bpl.Expr.Ident(delegateVariable));
                    Bpl.StmtListBuilder ifStmtBuilder = new Bpl.StmtListBuilder();
                    System.Diagnostics.Debug.Assert(tempInputs.Count == dispatchProcInExprs.Count);
                    if (tempInputs.Count > 0)
                    {
                        BuildAssignment(sink, ifStmtBuilder, tempInputs, dispatchProcInExprs);
                    }
                    ifStmtBuilder.Add(EmitDummySourceContext());
                    ifStmtBuilder.Add(new Bpl.CallCmd(token, delegateProcedure.Name, ins, outs));
                    System.Diagnostics.Debug.Assert(tempOutputs.Count == dispatchProcOutExprs.Count);
                    if (tempOutputs.Count > 0)
                    {
                        BuildAssignment(sink, ifStmtBuilder, dispatchProcOutExprs, tempOutputs);
                    }
                    stmtBuilder.Add(new Bpl.IfCmd(bexpr.tok, bexpr, ifStmtBuilder.Collect(bexpr.tok), null, null));
                }

                Bpl.Implementation dispatchImpl =
                    new Bpl.Implementation(token,
                                           invokeProcedure.Name,
                                           new List <Bpl.TypeVariable>(),
                                           invokeProcedure.InParams,
                                           invokeProcedure.OutParams,
                                           localVariables,
                                           stmtBuilder.Collect(token)
                                           );
                dispatchImpl.Proc = invokeProcedure;
                dispatchImpl.AddAttribute("inline", Bpl.Expr.Literal(1));
                sink.TranslatedProgram.AddTopLevelDeclaration(dispatchImpl);
            } 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
            }
        }
Example #18
0
        public override void DeclareConstant(Constant c, bool uniq, string attributes)
        {
            //Contract.Requires(c != null);
              base.DeclareConstant(c, uniq, attributes);
              orderingAxiomBuilder.AddConstant(c);

              // TODO: make separate distinct lists for names coming from different types
              // e.g., one for strings, one for ints, one for program types.
              if (uniq){
            distincts.Add(c);
              }
        }
 /// <summary>
 /// Creates a fresh BPL variable to represent <paramref name="type"/>, deciding
 /// on its type based on the heap representation. I.e., the value of this
 /// variable represents the value of the expression "typeof(type)".
 /// </summary>
 public Bpl.Variable CreateTypeVariable(ITypeReference type, List<Bpl.ConstantParent> parents)
 {
   string typename = TypeHelper.GetTypeName(type, NameFormattingOptions.DocumentationId);
     typename = TranslationHelper.TurnStringIntoValidIdentifier(typename);
     Bpl.IToken tok = type.Token();
     Bpl.TypedIdent tident = new Bpl.TypedIdent(tok, typename, this.TypeType);
     Bpl.Constant v = new Bpl.Constant(tok, tident, true /*unique*/, parents, false, null);
     return v;
 }
Example #20
0
 public override Constant VisitConstant(Constant node) {
   //Contract.Requires(node != null);
   Contract.Ensures(Contract.Result<Constant>() != null);
   return base.VisitConstant((Constant)node.Clone());
 }
Example #21
0
        private Tuple<Variable, Variable> createNewExplainConstants(Variable v)
        {
            Contract.Assert(impl != null);
            Contract.Assert(CommandLineOptions.Clo.ExplainHoudini);
            Variable v1 = new Constant(Token.NoToken, new TypedIdent(Token.NoToken, string.Format("{0}_{1}_{2}", v.Name, impl.Name, "pos"), Microsoft.Boogie.BasicType.Bool));
            Variable v2 = new Constant(Token.NoToken, new TypedIdent(Token.NoToken, string.Format("{0}_{1}_{2}", v.Name, impl.Name, "neg"), Microsoft.Boogie.BasicType.Bool));

            return Tuple.Create(v1, v2);
        }