Пример #1
0
        public List <string> getNames(string filename)
        {
            var program = getProgram(filename);
            List <TopLevelDecl> decls = new List <TopLevelDecl>();

            foreach (var module in program.Modules())
            {
                decls.AddRange(module.TopLevelDecls);
            }
            List <string> lst       = new List <string>();
            var           callables = ModuleDefinition.AllCallables(decls);

            foreach (var decl in callables)
            {
                if (decl is Method)
                {
                    lst.Add(((Method)decl).Name);
                }
                else if (decl is Predicate)
                {
                    lst.Add(((Predicate)decl).Name);
                }
            }

            return(lst);
        }
        public static Method FindMethod(string filename, string methodName, Microsoft.Dafny.Type[] InTypes, Microsoft.Dafny.Type[] retTypes)
        {
            // This was commented since it was causing verification to crash. (Boogie is likely to be the source)
            // DafnyOptions.Install(new DafnyOptions());

            var program   = GetProgram(filename);
            var decls     = program.Modules().SelectMany(m => m.TopLevelDecls).ToList();
            var callables = ModuleDefinition.AllCallables(decls);


            foreach (var currMethod in callables)
            {
                if (currMethod is Method)
                {
                    Boolean isFit = true;
                    if (((Method)currMethod).Name.Equals(methodName) && ((Method)currMethod).Ins.Count == InTypes.Length && ((Method)currMethod).Outs.Count == retTypes.Length)
                    {
                        for (int i = 0; i < InTypes.Length && isFit; i++)
                        {
                            if (!InTypes[i].Equals(((Method)currMethod).Ins[i].Type))
                            {
                                isFit = false;
                            }
                        }
                        for (int i = 0; i < retTypes.Length && isFit; i++)
                        {
                            if (!retTypes[i].Equals(((Method)currMethod).Outs[i].Type))
                            {
                                isFit = false;
                            }
                        }
                    }
                    else
                    {
                        isFit = false;
                    }

                    if (isFit)
                    {
                        return((Method)currMethod);
                    }
                }
            }
            return(null);

            //var method = callables.Where(c => c is Method
            //    && ((Method)c).Name.Equals(methodName));


            //if (method.Any())
            //{
            //    return method.First() as Method;
            //}
            //else
            //{
            //    return null;
            //}
        }
Пример #3
0
        /// <summary>
        /// This function is the callback used to execute the command when the menu item is clicked.
        /// See the constructor to see how the menu item is associated with this function using
        /// OleMenuCommandService service and MenuCommand class.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event args.</param>
        private void MenuItemCallback(object sender, EventArgs e)
        {
            DTE dte = (DTE)this.ServiceProvider.GetService(typeof(DTE));

            dte.ActiveDocument.Save();
            var fileName           = dte.ActiveDocument.FullName;
            var selectedStatements = (List <Statement>)HelpFunctions.GetSelectedStatements();

            if (selectedStatements.Count == 1 && selectedStatements[0] is UpdateStmt)
            {
                UpdateStmt             curr    = (UpdateStmt)selectedStatements[0];
                var                    arg     = ((ApplySuffix)((ExprRhs)curr.Rhss[0]).Expr).Args;
                Microsoft.Dafny.Type[] InTypes = getTypes(arg);

                var rets = curr.Lhss;
                Microsoft.Dafny.Type[] retTypes = getTypes(rets);

                Method m = HelpFunctions.FindMethod(fileName, ((NameSegment)((ApplySuffix)((ExprRhs)curr.Rhss[0]).Expr).Lhs).Name, InTypes, retTypes);
                edit = HelpFunctions.GetWpfView().TextBuffer.CreateEdit();
                inline(curr, fileName, m);
                edit.Apply();
                ////////////////////////////////////
            }
            else if (selectedStatements.Count == 0 && HelpFunctions.GetCurrentMethod() != null)
            {
                Method currMethod = HelpFunctions.GetCurrentMethod();

                var program   = HelpFunctions.GetProgram(fileName);
                var decls     = program.Modules().SelectMany(m => m.TopLevelDecls).ToList();
                var callables = ModuleDefinition.AllCallables(decls);
                edit = HelpFunctions.GetWpfView().TextBuffer.CreateEdit();
                foreach (var curr in callables)
                {
                    if (curr is Method)
                    {
                        var m = curr as Method;
                        traverse(m.Body, currMethod);
                    }
                }
                var textView = HelpFunctions.GetWpfView();
                var start    = currMethod.tok.pos;
                if (currMethod is Lemma)
                {
                    start -= 6;
                }
                else
                {
                    start -= 7;
                }
                var end = currMethod.BodyEndTok.pos + 1;
                edit.Delete(start, end - start);
                edit.Apply();
            }
            //HelpFunctions.prettyPrint(fileName);
        }
        public Boolean parse_file(string filename, string methodname)
        {
            // init parser arguments
            DafnyFile usedFile = new DafnyFile(filename);
            var       files    = new List <DafnyFile>();

            files.Add(usedFile);
            ErrorReporter r = new ConsoleErrorReporter();

            Microsoft.Dafny.Program ret;
            DafnyOptions.Install(new DafnyOptions(r));

            // run parser
            string err = Main.ParseCheck(files, filename, r, out ret);

            //if (err != null)
            //{
            //    return false;
            //}

            // program parsed well
            dafny_program = ret;

            // list of all methods
            var decls             = ret.Modules();
            List <TopLevelDecl> a = new List <TopLevelDecl>();

            foreach (Microsoft.Dafny.ModuleDefinition dec in decls)
            {
                a.AddRange(dec.TopLevelDecls);
            }

            // look for methodname
            var callables = ModuleDefinition.AllCallables(a);

            foreach (Microsoft.Dafny.ICallable method in callables)
            {
                if (method is Microsoft.Dafny.Method)
                {
                    Microsoft.Dafny.Method m = (Microsoft.Dafny.Method)method;
                    if (m.Name == methodname)
                    {
                        currentMethod = m;
                        return(true);
                    }
                }
            }

            return(false);
        }
        //laddon
        public static Method FindMethod(string filename, int offset)
        {
            // This was commented since it was causing verification to crash. (Boogie is likely to be the source)
            // DafnyOptions.Install(new DafnyOptions());
            var program   = GetProgram(filename);
            var decls     = program.Modules().SelectMany(m => m.TopLevelDecls).ToList();
            var callables = ModuleDefinition.AllCallables(decls);

            var method = callables.Where(c => c is Method &&
                                         ((Method)c).tok.pos <= offset &&
                                         ((Method)c).BodyEndTok.pos >= offset);

            if (method.Any())
            {
                return(method.First() as Method);
            }
            else
            {
                return(null);
            }
        }
Пример #6
0
        public Method FindMethod(string filename, int line)
        {
            var dafnyProgram          = getProgram(filename);
            List <TopLevelDecl> decls = new List <TopLevelDecl>();

            foreach (var module in dafnyProgram.Modules())
            {
                decls.AddRange(module.TopLevelDecls);
            }
            var callables = ModuleDefinition.AllCallables(decls);

//            var method = callables.Where(c => c is Method
//                && ((((Method)c).tok.line <= offset
//                && ((Method)c).BodyEndTok.line >= offset) || ((Method)c).tok.line == offset));

            foreach (var m in callables)
            {
                if (m is Method && m.Tok.line <= line)
                {
                    var method      = m as Method;
                    int lastTokLine = m.Tok.line;
                    if (method.Ens.Count > 0)
                    {
                        lastTokLine = method.Ens.Last().E.tok.line;
                    }
                    else if (method.Req.Count > 0)
                    {
                        lastTokLine = method.Req.Last().E.tok.line;
                    }
                    if (lastTokLine >= line)
                    {
                        return(method);
                    }
                }
            }
            //           if (method.Any())
            //              return method.First() as Method;
            //          else return null;
            return(null);
        }
Пример #7
0
        private List <ReferenceInformation> FindMethodReferencesInternal(string methodToFind)
        {
            var information = new List <ReferenceInformation>();

            foreach (var module in _dafnyProgram.Modules())
            {
                foreach (var clbl in ModuleDefinition.AllCallables(module.TopLevelDecls).Where(e => !(e.Tok is IncludeToken)))
                {
                    if (!(clbl is Method))
                    {
                        continue;
                    }

                    var m = (Method)clbl;
                    if (m.Body != null)
                    {
                        information.AddRange(ParseBodyForMethodReferences(m.Body.SubStatements, methodToFind, m.Name));
                    }
                }
            }
            return(information);
        }
Пример #8
0
        public Method FindMethod(string filename, int offset)
        {
            var dafnyProgram          = getProgram(filename);
            List <TopLevelDecl> decls = new List <TopLevelDecl>();

            foreach (var module in dafnyProgram.Modules())
            {
                decls.AddRange(module.TopLevelDecls);
            }
            var callables = ModuleDefinition.AllCallables(decls);
            var method    = callables.Where(c => c is Method &&
                                            ((Method)c).BodyStartTok.pos <= offset &&
                                            ((Method)c).BodyEndTok.pos >= offset);

            ModuleDefinition.AllCallables(decls);
            if (method.Any())
            {
                return(method.First() as Method);
            }
            else
            {
                return(null);
            }
        }
Пример #9
0
 private void AddMethods(ModuleDefinition module, List <SymbolInformation> information)
 {
     foreach (
         var clbl in
         ModuleDefinition.AllCallables(module.TopLevelDecls).Where(e => e != null && !(e.Tok is IncludeToken)))
     {
         if (clbl is Predicate)
         {
             var predicate       = clbl as Predicate;
             var predicateSymbol = new SymbolInformation {
                 Module      = predicate.EnclosingClass.Module.Name,
                 Name        = predicate.Name,
                 ParentClass = predicate.EnclosingClass.Name,
                 SymbolType  = SymbolInformation.Type.Predicate,
                 StartToken  = predicate.tok,
                 EndToken    = predicate.BodyEndTok
             };
             information.Add(predicateSymbol);
         }
         else if (clbl is Function)
         {
             var fn             = (Function)clbl;
             var functionSymbol = new SymbolInformation {
                 Module      = fn.EnclosingClass.Module.Name,
                 Name        = fn.Name,
                 ParentClass = fn.EnclosingClass.Name,
                 SymbolType  = SymbolInformation.Type.Function,
                 StartToken  = fn.tok,
                 EndColumn   = fn.BodyEndTok.col,
                 EndLine     = fn.BodyEndTok.line,
                 EndPosition = fn.BodyEndTok.pos,
                 EndToken    = fn.BodyEndTok
             };
             information.Add(functionSymbol);
         }
         else
         {
             var m = (Method)clbl;
             if (m.Body != null && m.Body.Body != null)
             {
                 information.AddRange(ResolveCallStatements(m.Body.Body));
                 information.AddRange(ResolveLocalDefinitions(m.Body.Body, m));
             }
             var methodSymbol = new SymbolInformation {
                 Module      = m.EnclosingClass.Module.Name,
                 Name        = m.Name,
                 ParentClass = m.EnclosingClass.Name,
                 SymbolType  = SymbolInformation.Type.Method,
                 StartToken  = m.tok,
                 Ensures     = ParseContracts(m.Ens),
                 Requires    = ParseContracts(m.Req),
                 References  =
                     FindMethodReferencesInternal(m.EnclosingClass.Module.Name + "." + m.EnclosingClass.Name + "." +
                                                  m.Name),
                 EndColumn   = m.BodyEndTok.col,
                 EndLine     = m.BodyEndTok.line,
                 EndPosition = m.BodyEndTok.pos,
                 EndToken    = m.BodyEndTok
             };
             information.Add(methodSymbol);
         }
     }
 }