Пример #1
0
 static void ExprRegions(Microsoft.Dafny.Expression expr, List <IdRegion> regions, ModuleDefinition module)
 {
     Contract.Requires(expr != null);
     Contract.Requires(regions != null);
     if (expr is AutoGeneratedExpression)
     {
         // do nothing
         return;
     }
     else if (expr is IdentifierExpr)
     {
         var e = (IdentifierExpr)expr;
         IdRegion.Add(regions, e.tok, e.Var, false, module);
     }
     else if (expr is MemberSelectExpr)
     {
         var e     = (MemberSelectExpr)expr;
         var field = e.Member as Field;
         if (field != null)
         {
             IdRegion.Add(regions, e.tok, field, e.Type, "field", false, module);
         }
     }
     else if (expr is LetExpr)
     {
         var e = (LetExpr)expr;
         foreach (var bv in e.BoundVars)
         {
             IdRegion.Add(regions, bv.tok, bv, true, module);
         }
     }
     else if (expr is ComprehensionExpr)
     {
         var e = (ComprehensionExpr)expr;
         foreach (var bv in e.BoundVars)
         {
             IdRegion.Add(regions, bv.tok, bv, true, module);
         }
     }
     else if (expr is MatchExpr)
     {
         var e = (MatchExpr)expr;
         foreach (var kase in e.Cases)
         {
             kase.Arguments.ForEach(bv => IdRegion.Add(regions, bv.tok, bv, true, module));
         }
     }
     else if (expr is ChainingExpression)
     {
         var e = (ChainingExpression)expr;
         // Do the subexpressions only once (that is, avoid the duplication that occurs in the desugared form of the ChainingExpression)
         e.Operands.ForEach(ee => ExprRegions(ee, regions, module));
         return; // return here, so as to avoid doing the subexpressions below
     }
     foreach (var ee in expr.SubExpressions)
     {
         ExprRegions(ee, regions, module);
     }
 }
 public static void renameBody(Microsoft.Dafny.Expression expr, Dictionary <string, string> rename)
 {
     if (expr == null)
     {
         return;
     }
     if (expr is ApplySuffix)
     {
         List <Microsoft.Dafny.Expression> x = new List <Microsoft.Dafny.Expression>(((ApplySuffix)expr).Args);
         x.ForEach(m => renameBody(m, rename));
         expr = new ApplySuffix(expr.tok, ((ApplySuffix)expr).Lhs, x);
     }
     else if (expr is NameSegment)
     {
         //if (rename.ContainsKey(((NameSegment)expr).Name)) ((NameSegment)expr).Name = rename[((NameSegment)expr).Name];
     }
     else if (expr is BinaryExpr)
     {
         renameBody(((BinaryExpr)expr).E0, rename);
         renameBody(((BinaryExpr)expr).E1, rename);
     }
     else if (expr is UnaryExpr)
     {
         renameBody(((UnaryExpr)expr).E, rename);
     }
     else if (expr is ChainingExpression)
     {
         renameBody(((ChainingExpression)expr).E, rename);
     }
     else if (expr is SeqSelectExpr)
     {
         renameBody(((SeqSelectExpr)expr).Seq, rename);
         renameBody(((SeqSelectExpr)expr).E0, rename);
         renameBody(((SeqSelectExpr)expr).E1, rename);
     }
     else if (expr is ParensExpression)
     {
         renameBody(((ParensExpression)expr).E, rename);
     }
     else if (expr is SeqDisplayExpr)
     {
         foreach (Microsoft.Dafny.Expression x in ((SeqDisplayExpr)expr).Elements)
         {
             renameBody(x, rename);
         }
     }
 }
Пример #3
0
 public static void traverse(Microsoft.Dafny.Expression expr, Method me)
 {
     if (expr == null)
     {
         return;
     }
     if (expr is ApplySuffix)
     {
         List <Microsoft.Dafny.Expression> x = new List <Microsoft.Dafny.Expression>(((ApplySuffix)expr).Args);
         x.ForEach(m => traverse(m, me));
         expr = new ApplySuffix(expr.tok, ((ApplySuffix)expr).Lhs, x);
     }
     else if (expr is NameSegment)
     {
     }
     else if (expr is BinaryExpr)
     {
         traverse(((BinaryExpr)expr).E0, me);
         traverse(((BinaryExpr)expr).E1, me);
     }
     else if (expr is UnaryExpr)
     {
         traverse(((UnaryExpr)expr).E, me);
     }
     else if (expr is ChainingExpression)
     {
         traverse(((ChainingExpression)expr).E, me);
     }
     else if (expr is SeqSelectExpr)
     {
         traverse(((SeqSelectExpr)expr).Seq, me);
         traverse(((SeqSelectExpr)expr).E0, me);
         traverse(((SeqSelectExpr)expr).E1, me);
     }
     else if (expr is ParensExpression)
     {
         traverse(((ParensExpression)expr).E, me);
     }
     else if (expr is SeqDisplayExpr)
     {
         foreach (Microsoft.Dafny.Expression x in ((SeqDisplayExpr)expr).Elements)
         {
             traverse(x, me);
         }
     }
 }
Пример #4
0
 void ExprRegions(Microsoft.Dafny.Expression expr, List <IdRegion> regions, Microsoft.Dafny.Program prog, ModuleDefinition module)
 {
     Contract.Requires(expr != null);
     Contract.Requires(regions != null);
     Contract.Requires(prog != null);
     if (expr is AutoGeneratedExpression)
     {
         // do nothing
         return;
     }
     else if (expr is IdentifierExpr)
     {
         var e = (IdentifierExpr)expr;
         IdRegion.Add(regions, prog, e.tok, e.Var, false, (ICallable)null, module);
     }
     else if (expr is MemberSelectExpr)
     {
         var e     = (MemberSelectExpr)expr;
         var field = e.Member as Field;
         if (field != null)
         {
             var kind = !(field is ConstantField) ? "field" : field.IsStatic && !(field.EnclosingClass is DefaultClassDecl) ? "static const" : "const";
             IdRegion.Add(regions, prog, e.tok, field, e.Type, kind, false, module);
         }
         if (e.Member != null)
         {
             RecordUseAndDef(prog, e.tok, e.Member.Name.Length, e.Member.tok);
         }
     }
     else if (expr is FunctionCallExpr)
     {
         var e    = (FunctionCallExpr)expr;
         var func = e.Function;
         if (func != null)
         {
             RecordUseAndDef(prog, e.tok, func.Name.Length, func.tok);
         }
     }
     else if (expr is ApplySuffix)
     {
         // No need to call ExprRegions on the Lhs field because the for loop at the end of this function will do that.
     }
     else if (expr is LetExpr)
     {
         var e = (LetExpr)expr;
         IdRegion.AddRecognizedAttributes(e.Attributes, regions, prog);
         foreach (var bv in e.BoundVars)
         {
             IdRegion.Add(regions, prog, bv.tok, bv, true, (ICallable)null, module);
         }
     }
     else if (expr is ComprehensionExpr)
     {
         var e = (ComprehensionExpr)expr;
         IdRegion.AddRecognizedAttributes(e.Attributes, regions, prog);
         foreach (var bv in e.BoundVars)
         {
             IdRegion.Add(regions, prog, bv.tok, bv, true, (ICallable)null, module);
         }
         if (expr is QuantifierExpr)
         {
             // If the quantifier has a SplitQuantifier, then its .SubExpressions routine goes over each split.
             // That's not what we want here, because it would cause duplicated hover texts.  Instead, we just
             // do what a QuantifierExpr would do without a SplitQuantifier, which is as follows:
             foreach (var ee in Attributes.SubExpressions(e.Attributes))
             {
                 ExprRegions(ee, regions, prog, module);
             }
             if (e.Range != null)
             {
                 ExprRegions(e.Range, regions, prog, module);
             }
             ExprRegions(e.Term, regions, prog, module);
             return;
         }
     }
     else if (expr is MatchExpr)
     {
         var e = (MatchExpr)expr;
         foreach (var kase in e.Cases)
         {
             kase.Arguments.ForEach(bv => {
                 IdRegion.Add(regions, prog, bv.tok, bv, true, (ICallable)null, module);
                 // if the arguments is an encapsulation of different boundvars from nested match cases,
                 // add the boundvars so that they can show up in the IDE correctly
                 if (bv.tok is MatchCaseToken)
                 {
                     MatchCaseToken mt = (MatchCaseToken)bv.tok;
                     foreach (Tuple <Bpl.IToken, BoundVar, bool> entry in mt.varList)
                     {
                         IdRegion.Add(regions, prog, entry.Item1, entry.Item2, entry.Item3, (ICallable)null, module);
                     }
                 }
             });
         }
     }
     else if (expr is ChainingExpression)
     {
         var e = (ChainingExpression)expr;
         // Do the subexpressions only once (that is, avoid the duplication that occurs in the desugared form of the ChainingExpression)
         e.Operands.ForEach(ee => ExprRegions(ee, regions, prog, module));
         return; // return here, so as to avoid doing the subexpressions below
     }
     foreach (var ee in expr.SubExpressions)
     {
         ExprRegions(ee, regions, prog, module);
     }
 }
Пример #5
0
 static void ExprRegions(Microsoft.Dafny.Expression expr, List <IdRegion> regions, Microsoft.Dafny.Program prog, ModuleDefinition module)
 {
     Contract.Requires(expr != null);
     Contract.Requires(regions != null);
     Contract.Requires(prog != null);
     if (expr is AutoGeneratedExpression)
     {
         // do nothing
         return;
     }
     else if (expr is IdentifierExpr)
     {
         var e = (IdentifierExpr)expr;
         IdRegion.Add(regions, prog, e.tok, e.Var, false, module);
     }
     else if (expr is MemberSelectExpr)
     {
         var e     = (MemberSelectExpr)expr;
         var field = e.Member as Field;
         if (field != null)
         {
             IdRegion.Add(regions, prog, e.tok, field, e.Type, "field", false, module);
         }
     }
     else if (expr is LetExpr)
     {
         var e = (LetExpr)expr;
         IdRegion.AddRecognizedAttributes(e.Attributes, regions, prog);
         foreach (var bv in e.BoundVars)
         {
             IdRegion.Add(regions, prog, bv.tok, bv, true, module);
         }
     }
     else if (expr is ComprehensionExpr)
     {
         var e = (ComprehensionExpr)expr;
         IdRegion.AddRecognizedAttributes(e.Attributes, regions, prog);
         foreach (var bv in e.BoundVars)
         {
             IdRegion.Add(regions, prog, bv.tok, bv, true, module);
         }
     }
     else if (expr is MatchExpr)
     {
         var e = (MatchExpr)expr;
         foreach (var kase in e.Cases)
         {
             kase.Arguments.ForEach(bv => {
                 IdRegion.Add(regions, prog, bv.tok, bv, true, module);
                 // if the arguments is an encapsulation of different boundvars from nested match cases,
                 // add the boundvars so that they can show up in the IDE correctly
                 if (bv.tok is MatchCaseToken)
                 {
                     MatchCaseToken mt = (MatchCaseToken)bv.tok;
                     foreach (Tuple <Bpl.IToken, BoundVar, bool> entry in mt.varList)
                     {
                         IdRegion.Add(regions, prog, entry.Item1, entry.Item2, entry.Item3, module);
                     }
                 }
             });
         }
     }
     else if (expr is ChainingExpression)
     {
         var e = (ChainingExpression)expr;
         // Do the subexpressions only once (that is, avoid the duplication that occurs in the desugared form of the ChainingExpression)
         e.Operands.ForEach(ee => ExprRegions(ee, regions, prog, module));
         return; // return here, so as to avoid doing the subexpressions below
     }
     foreach (var ee in expr.SubExpressions)
     {
         ExprRegions(ee, regions, prog, module);
     }
 }
        public static Microsoft.Dafny.Expression renameB(Microsoft.Dafny.Expression expr, Dictionary <string, string> rename)
        {
            if (expr == null)
            {
                return(null);
            }
            if (expr is ApplySuffix)
            {
                var x = expr as ApplySuffix;
                List <Microsoft.Dafny.Expression> R = new List <Microsoft.Dafny.Expression>();
                foreach (var exp in x.Args)
                {
                    R.Add(renameB(exp, rename));
                }

                //List<Microsoft.Dafny.Expression> x = new List<Microsoft.Dafny.Expression>(((ApplySuffix)expr).Args);
                //x.ForEach(m => renameBody(m, rename));
                //expr = new ApplySuffix(expr.tok, ((ApplySuffix)expr).Lhs, x);
                return(new ApplySuffix(null, renameB(x.Lhs, rename), R));
            }
            else if (expr is NameSegment)
            {
                var x = expr as NameSegment;
                if (rename.ContainsKey(x.Name))
                {
                    return(new NameSegment(null, rename[x.Name], x.OptTypeArguments));
                }
                else
                {
                    return(expr);
                }
            }
            else if (expr is BinaryExpr)
            {
                var x = expr as BinaryExpr;
                return(new BinaryExpr(null, x.Op, renameB(x.E0, rename), renameB(x.E1, rename)));
            }
            else if (expr is UnaryOpExpr)
            {
                var x = expr as UnaryOpExpr;
                return(new UnaryOpExpr(null, x.Op, renameB(x.E, rename)));
            }
            else if (expr is ConversionExpr)
            {
                var x = expr as ConversionExpr;
                return(new ConversionExpr(null, renameB(x.E, rename), x.ToType));
            }
            else if (expr is ChainingExpression)
            {
                var x = expr as ChainingExpression;
                List <Microsoft.Dafny.Expression> R = new List <Microsoft.Dafny.Expression>();
                List <Microsoft.Dafny.Expression> L = new List <Microsoft.Dafny.Expression>();
                foreach (var exp in x.Operands)
                {
                    R.Add(renameB(exp, rename));
                }
                foreach (var exp in x.PrefixLimits)
                {
                    L.Add(renameB(exp, rename));
                }
                return(new ChainingExpression(null, R, x.Operators, x.OperatorLocs, L));
            }
            else if (expr is SeqSelectExpr)
            {
                var x = expr as SeqSelectExpr;
                return(new SeqSelectExpr(null, x.SelectOne, renameB(x.Seq, rename), renameB(x.E0, rename), renameB(x.E1, rename)));
            }
            else if (expr is ParensExpression)
            {
                var x = expr as ParensExpression;
                return(new ParensExpression(null, renameB(x.E, rename)));
            }
            else if (expr is SeqDisplayExpr)
            {
                var x = expr as SeqDisplayExpr;
                List <Microsoft.Dafny.Expression> R = new List <Microsoft.Dafny.Expression>();
                foreach (var exp in x.Elements)
                {
                    R.Add(renameB(exp, rename));
                }
                return(new SeqDisplayExpr(null, R));
            }
            else
            {
                //throw new NotImplementedException("implement this!.");
                //return null;
                return(expr);
            }
        }
        //biadsy
        private static HashSet <DVariable> GetVars(Microsoft.Dafny.Expression exp, HashSet <DVariable> declaredVars)
        {
            DVariableComparer   comparer = new DVariableComparer();
            HashSet <DVariable> vars     = new HashSet <DVariable>(comparer);

            if (exp is SeqSelectExpr)
            {
                var expr = exp as SeqSelectExpr;
                vars.UnionWith(GetVars(expr.Seq, declaredVars));
                vars.UnionWith(GetVars(expr.E0, declaredVars));
                vars.UnionWith(GetVars(expr.E1, declaredVars));
            }
            else if (exp is NameSegment)
            {
                var       expr = exp as NameSegment;
                DVariable var  = new DVariable(expr.Name, expr.Type);
                if (!declaredVars.Contains(var))
                {
                    vars.Add(var);
                }
            }
            else if (exp is ApplySuffix)
            {
                var expr = exp as ApplySuffix;
                foreach (var arg in expr.Args)
                {
                    vars.UnionWith(GetVars(arg, declaredVars));
                }
            }
            else if (exp is BinaryExpr)
            {
                var expr = exp as BinaryExpr;
                vars.UnionWith(GetVars(expr.E0, declaredVars));
                vars.UnionWith(GetVars(expr.E1, declaredVars));
            }
            else if (exp is UnaryOpExpr)
            {
                var expr = exp as UnaryOpExpr;
                vars.UnionWith(GetVars(expr.E, declaredVars));
            }
            else if (exp is ParensExpression)
            {
                var expr = exp as ParensExpression;
                vars.UnionWith(GetVars(expr.E, declaredVars));
            }
            else if (exp is ChainingExpression)
            {
                var expr = exp as ChainingExpression;
                vars.UnionWith(GetVars(expr.E, declaredVars));
            }
            else if (exp is SeqDisplayExpr)
            {
                var expr = exp as SeqDisplayExpr;
                foreach (var arg in expr.Elements)
                {
                    vars.UnionWith(GetVars(arg, declaredVars));
                }
            }
            else if (exp is Microsoft.Dafny.ForallExpr)
            {
                var expr       = exp as Microsoft.Dafny.ForallExpr;
                var newDecVars = new HashSet <DVariable>(declaredVars, comparer);
                if (expr.BoundVars != null)
                {
                    foreach (var bvar in expr.BoundVars)
                    {
                        DVariable dvar = new DVariable(bvar.DisplayName, bvar.Type);
                        newDecVars.Add(dvar);
                    }
                }
                vars.UnionWith(GetVars(expr.Term, newDecVars));
            }
            return(vars);
        }
 public static bool IsExpressionInRange(Microsoft.Dafny.Expression e, int start, int end)
 {
     return(e.tok.pos >= start && e.tok.pos <= end);
 }