예제 #1
0
        public static Program GenerateDafnyProgram(ProofState state, List <MemberDecl> newMembers)
        {
            var prog = state.GetDafnyProgram();
            var tld  = prog.DefaultModuleDef.TopLevelDecls.FirstOrDefault(x => x.Name == state.TargetMethod.EnclosingClass.Name) as ClassDecl;

            Contract.Assert(tld != null);
            var member = tld.Members.FirstOrDefault(x => x.Name == state.TargetMethod.Name);

            Contract.Assert(member != null);
            // we can safely remove the tactics
            tld.Members.RemoveAll(x => x is Tactic); //remove before else index will be wrong
            int index = tld.Members.IndexOf(member);

            tld.Members.RemoveAt(index);
            tld.Members.InsertRange(index, newMembers);
            var filePath = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
            var tw       = new StreamWriter(filePath);
            var printer  = new Printer(tw);

            printer.PrintTopLevelDecls(prog.DefaultModuleDef.TopLevelDecls, 0, filePath);
            tw.Close();
            Parser.ParseOnly(new List <string>()
            {
                filePath
            }, prog.Name, out prog);
            return(prog);
        }
예제 #2
0
파일: Util.cs 프로젝트: ggrov/tacny
        /// <summary>
        /// Insert generated code into a method
        /// </summary>
        /// <param name="state"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static BlockStmt InsertCode(ProofState state, Dictionary <UpdateStmt, List <Statement> > code)
        {
            Contract.Requires <ArgumentNullException>(state != null, "state");
            Contract.Requires <ArgumentNullException>(code != null, "code");
            var prog = state.GetDafnyProgram();
            var tld  = prog.DefaultModuleDef.TopLevelDecls.FirstOrDefault(x => x.Name == state.ActiveClass.Name) as ClassDecl;

            Contract.Assert(tld != null);
            var member = tld.Members.FirstOrDefault(x => x.Name == state.TargetMethod.Name) as Method;
            var body   = member?.Body;

            foreach (var kvp in code)
            {
                body = InsertCodeInternal(body, kvp.Value, kvp.Key);
            }
            var r = new Resolver(prog);

            r.ResolveProgram(prog);
            return(body);
        }
예제 #3
0
        public static Program GenerateResolvedProg(ProofState state)
        {
            var prog = state.GetDafnyProgram();
            var r    = new Resolver(prog);

            r.ResolveProgram(prog);
            //get the generated code
            var results = new Dictionary <UpdateStmt, List <Statement> >();

            results.Add(state.TacticApplication, state.GetGeneratedCode().Copy());
            var body = Util.InsertCode(state, results);
            // find the membcl in the resoved prog
            Method dest_md = null;

            foreach (var m in prog.DefaultModuleDef.TopLevelDecls)
            {
                if (m.WhatKind == "class")
                {
                    foreach (var method in (m as DefaultClassDecl).Members)
                    {
                        if (method.FullName == state.TargetMethod.FullName)
                        {
                            dest_md = (method as Method);
                            dest_md.Body.Body.Clear();
                            dest_md.Body.Body.AddRange(body.Body);
                        }// if some other method has tactic call, then empty the body
                        else if (method.CallsTactic)
                        {
                            method.CallsTactic = false;
                            (method as Method).Body.Body.Clear();
                            SetVerifyFalseAttr(method);
                        }
                        else
                        {
                            //set other memberdecl as verify false
                            SetVerifyFalseAttr(method);
                        }
                    }
                }
            }

#if _TACTIC_DEBUG
            Console.WriteLine("*********************Verifying Tacny Generated Prog*****************");
            var printer = new Printer(Console.Out);
            //printer.PrintProgram(prog, false);
            foreach (var stmt in state.GetGeneratedCode())
            {
                printer.PrintStatement(stmt, 0);
            }
            Console.WriteLine("\n*********************Prog END*****************");
#endif

            dest_md.CallsTactic = false;
            r.SetCurClass(dest_md.EnclosingClass as ClassDecl);
            r.ResolveMethodBody(dest_md);

            if (prog.reporter.Count(ErrorLevel.Error) != 0)
            {
#if _TACTIC_DEBUG
                Console.Write("Fail to resolve prog, skip verifier ! \n");
#endif
                return(null);
            }
            else
            {
                return(prog);
            }
        }