Beispiel #1
0
        public TacticReplacerActor(ITextBuffer tb, int position = -1)
        {
            Contract.Requires(tb != null);
            string currentFileName;

            LoadStatus = RefactoringUtil.LoadAndCheckDocument(tb, out currentFileName) ? TacticReplaceStatus.Success : TacticReplaceStatus.NoDocumentPersistence;
            if (LoadStatus != TacticReplaceStatus.Success)
            {
                return;
            }
            var program = RefactoringUtil.GetReparsedProgram(tb, currentFileName, true);

            _tld        = RefactoringUtil.GetTld(program);
            _tldMembers = _tld?.Members.GetEnumerator();
            LoadStatus  = _tld != null ? TacticReplaceStatus.Success : TacticReplaceStatus.NotResolved;
            if (LoadStatus != TacticReplaceStatus.Success)
            {
                return;
            }
            if (position == -1)
            {
                return;
            }
            SetMember(position);
            SetTacticCall(position);
        }
Beispiel #2
0
        public static Program GenerateResolvedProg(ProofState state)
        {
            var prog = state.GetDafnyProgram();

            var result = TacnyDriver.GetResultList().Where(
                kvp => kvp.Key.Tok.pos != state.TopLevelTacApp.Tok.pos).ToDictionary(c => c.Key, c => c.Value);

            result.Add(state.TopLevelTacApp, state.GetGeneratedCode().Copy());

            var body = InsertCode(state, result);

            Method           destMd           = null;
            DefaultClassDecl defaultClassDecl = null;

            foreach (var m in prog.DefaultModuleDef.TopLevelDecls)
            {
                if (m.WhatKind == "class")
                {
                    var classDecl = m as DefaultClassDecl;
                    if (classDecl != null)
                    {
                        foreach (var method in classDecl.Members)
                        {
                            if (method.Name == state.TargetMethod.Name)
                            {
                                destMd           = (method as Method);
                                defaultClassDecl = classDecl;
                            }
                            else if (!(method is Tactic))
                            {
                                method.CallsTactic = 0;
                                var o = method as Method;
                                if (o != null && o.Body != null)
                                {
                                    o?.Body.Body.Clear();
                                }
                                SetVerifyFalseAttr(method);
                            }
                        }
                    }
                }
            }


            destMd.CallsTactic = 0;
            destMd.Body.Body.Clear();
            destMd.Body.Body.AddRange(body.Body);

            var r = new Resolver(prog);

            r.ResolveProgram(prog);

            if (prog.reporter.Count(ErrorLevel.Error) != 0)
            {
                state.GetErrHandler().Reporter = prog.reporter;
#if _TACTIC_DEBUG_L1
                Console.Write("Fail to resolve prog, skip verifier ! \n");
#endif
                return(null);
            }
            else
            {
                return(prog);
            }
        }
Beispiel #3
0
        /// <summary>
        /// an optimised version for verifying prog
        /// </summary>
        /// <param name="states"></param>
        /// <returns></returns>
        public static Program GenerateResolvedProg(List <ProofState> states)
        {
            var state = states[0];
            var prog  = states[0].GetDafnyProgram();

            List <BlockStmt> bodies = new List <BlockStmt>();

            for (var i = 0; i < states.Count; i++)
            {
                var state0 = states[i];
                var result = TacnyDriver.GetResultList().Where(
                    kvp => kvp.Key.Tok.pos != state0.TopLevelTacApp.Tok.pos).ToDictionary(c => c.Key, c => c.Value);
                result.Add(state0.TopLevelTacApp, state0.GetGeneratedCode().Copy());

                var body0 = InsertCode(state0, result);
                SetStatementTokLine(TacnyDriver.TacticCodeTokLine - i - 1, body0);
                SetStatementName(body0, state0.TargetMethod.Name, state0.TargetMethod.Name + "_tacny_code_" + i);
                bodies.Add(body0);
            }

            Method           destMd           = null;
            DefaultClassDecl defaultClassDecl = null;
            var body = bodies[0];

            foreach (var m in prog.DefaultModuleDef.TopLevelDecls)
            {
                if (m.WhatKind == "class")
                {
                    var classDecl = m as DefaultClassDecl;
                    if (classDecl != null)
                    {
                        foreach (var method in classDecl.Members)
                        {
                            if (method.Name == state.TargetMethod.Name)
                            {
                                destMd           = (method as Method);
                                defaultClassDecl = classDecl;
                            }
                            else if (!(method is Tactic))
                            {
                                method.CallsTactic = 0;
                                var o = method as Method;
                                if (o != null && o.Body != null)
                                {
                                    o?.Body.Body.Clear();
                                }
                                SetVerifyFalseAttr(method);
                            }
                        }
                    }
                }
            }

            for (var i = 0; i < bodies.Count - 1; i++)
            {
                var dest = destMd.Copy();
                dest.CallsTactic = 0;
                dest.Body.Body.Clear();
                dest.Body.Body.AddRange(bodies[i].Body);
                dest.Name = dest.Name + "_tacny_code_" + i;
                defaultClassDecl.Members.Add(dest);
                dest.Body.Tok.line = TacnyDriver.TacticCodeTokLine - i - 1;
            }
            destMd.CallsTactic = 0;
            destMd.Body.Body.Clear();
            destMd.Body.Body.AddRange(bodies[bodies.Count - 1].Body);
            destMd.Name          = destMd.Name + "_tacny_code_" + (bodies.Count - 1);
            destMd.Body.Tok.line = TacnyDriver.TacticCodeTokLine - bodies.Count;

            var r = new Resolver(prog);

            r.ResolveProgram(prog);

            return(prog);
        }
Beispiel #4
0
        private void Flush()
        {
            var c = new DefaultClassDecl(module.ArmadaTranslation, newDefaultClassDecls);

            AddTopLevelDecl(c);
        }
Beispiel #5
0
        void Dafny()
        {
            ClassDecl/*!*/ c; DatatypeDecl/*!*/ dt; TopLevelDecl td; IteratorDecl iter;
            List<MemberDecl/*!*/> membersDefaultClass = new List<MemberDecl/*!*/>();
            ModuleDecl submodule;
            // to support multiple files, create a default module only if theModule is null
            DefaultModuleDecl defaultModule = (DefaultModuleDecl)((LiteralModuleDecl)theModule).ModuleDef;
            // theModule should be a DefaultModuleDecl (actually, the singular DefaultModuleDecl)
            TraitDecl/*!*/ trait;
            Contract.Assert(defaultModule != null);

            while (la.kind == 60) {
            Get();
            Expect(20);
            {
             string parsedFile = scanner.FullFilename;
             bool isVerbatimString;
             string includedFile = Util.RemoveParsedStringQuotes(t.val, out isVerbatimString);
             includedFile = Util.RemoveEscaping(includedFile, isVerbatimString);
             string fullPath = includedFile;
             if (!Path.IsPathRooted(includedFile)) {
               string basePath = Path.GetDirectoryName(parsedFile);
               includedFile = Path.Combine(basePath, includedFile);
               fullPath = Path.GetFullPath(includedFile);
             }
             defaultModule.Includes.Add(new Include(t, includedFile, fullPath));
            }

            }
            while (StartOf(1)) {
            switch (la.kind) {
            case 61: case 62: case 65: {
                SubModuleDecl(defaultModule, out submodule);
                defaultModule.TopLevelDecls.Add(submodule);
                break;
            }
            case 70: {
                ClassDecl(defaultModule, out c);
                defaultModule.TopLevelDecls.Add(c);
                break;
            }
            case 76: case 77: {
                DatatypeDecl(defaultModule, out dt);
                defaultModule.TopLevelDecls.Add(dt);
                break;
            }
            case 79: {
                NewtypeDecl(defaultModule, out td);
                defaultModule.TopLevelDecls.Add(td);
                break;
            }
            case 80: {
                OtherTypeDecl(defaultModule, out td);
                defaultModule.TopLevelDecls.Add(td);
                break;
            }
            case 81: {
                IteratorDecl(defaultModule, out iter);
                defaultModule.TopLevelDecls.Add(iter);
                break;
            }
            case 72: {
                TraitDecl(defaultModule, out trait);
                defaultModule.TopLevelDecls.Add(trait);
                break;
            }
            case 38: case 39: case 40: case 41: case 42: case 73: case 74: case 75: case 78: case 84: case 85: case 86: case 87: {
                ClassMemberDecl(membersDefaultClass, false, !DafnyOptions.O.AllowGlobals, false);
                break;
            }
            }
            }
            DefaultClassDecl defaultClass = null;
            foreach (TopLevelDecl topleveldecl in defaultModule.TopLevelDecls) {
             defaultClass = topleveldecl as DefaultClassDecl;
             if (defaultClass != null) {
               defaultClass.Members.AddRange(membersDefaultClass);
               break;
             }
            }
            if (defaultClass == null) { // create the default class here, because it wasn't found
             defaultClass = new DefaultClassDecl(defaultModule, membersDefaultClass);
             defaultModule.TopLevelDecls.Add(defaultClass);
            }
            Expect(0);
        }
Beispiel #6
0
 public static MemberDecl GetMemberFromPosition(DefaultClassDecl tld, int position) => 
   (from m in tld.Members where m.tok.pos <= position && position <= m.BodyEndTok.pos + 1 select m).FirstOrDefault();
Beispiel #7
0
 public static TacticReplaceStatus GetMemberFromPosition(DefaultClassDecl tld, int position, out MemberDecl member) {
   Contract.Requires(tld != null);
   member = GetMemberFromPosition(tld, position);
   return member == null ? TacticReplaceStatus.NoTactic : TacticReplaceStatus.Success;
 }
Beispiel #8
0
 public static MemberDecl GetMemberFromPosition(DefaultClassDecl tld, int position) =>
 (from m in tld.Members where m.tok.pos <= position && position <= m.BodyEndTok.pos + 1 select m).FirstOrDefault();
Beispiel #9
0
 public static TacticReplaceStatus GetMemberFromPosition(DefaultClassDecl tld, int position, out MemberDecl member)
 {
     Contract.Requires(tld != null);
     member = GetMemberFromPosition(tld, position);
     return(member == null ? TacticReplaceStatus.NoTactic : TacticReplaceStatus.Success);
 }
Beispiel #10
0
 public TacticReplacerActor(ITextBuffer tb, int position = -1)
 {
   Contract.Requires(tb != null);
   string currentFileName;
   LoadStatus = RefactoringUtil.LoadAndCheckDocument(tb, out currentFileName) ? TacticReplaceStatus.Success : TacticReplaceStatus.NoDocumentPersistence;
   if(LoadStatus!=TacticReplaceStatus.Success) return;
   var program = RefactoringUtil.GetReparsedProgram(tb, currentFileName, true);
   _tld = RefactoringUtil.GetTld(program);
   _tldMembers = _tld?.Members.GetEnumerator();
   LoadStatus = _tld != null ? TacticReplaceStatus.Success : TacticReplaceStatus.NotResolved;
   if (LoadStatus != TacticReplaceStatus.Success) return;
   if (position == -1) return;
   SetMember(position);
   SetTacticCall(position);
 }
Beispiel #11
0
    public TopLevelDecl CloneDeclaration(TopLevelDecl d, ModuleDefinition m) {
      Contract.Requires(d != null);
      Contract.Requires(m != null);

      if (d is OpaqueTypeDecl) {
        var dd = (OpaqueTypeDecl)d;
        return new OpaqueTypeDecl(Tok(dd.tok), dd.Name, m, dd.EqualitySupport, dd.TypeArgs.ConvertAll(CloneTypeParam), CloneAttributes(dd.Attributes), d);
      } else if (d is TypeSynonymDecl) {
        var dd = (TypeSynonymDecl)d;
        var tps = dd.TypeArgs.ConvertAll(CloneTypeParam);
        return new TypeSynonymDecl(Tok(dd.tok), dd.Name, tps, m, CloneType(dd.Rhs), CloneAttributes(dd.Attributes), dd);
      } else if (d is NewtypeDecl) {
        var dd = (NewtypeDecl)d;
        if (dd.Var == null) {
          return new NewtypeDecl(Tok(dd.tok), dd.Name, m, CloneType(dd.BaseType), CloneAttributes(dd.Attributes), dd);
        } else {
          return new NewtypeDecl(Tok(dd.tok), dd.Name, m, CloneBoundVar(dd.Var), CloneExpr(dd.Constraint), CloneAttributes(dd.Attributes), dd);
        }
      } else if (d is TupleTypeDecl) {
        var dd = (TupleTypeDecl)d;
        return new TupleTypeDecl(dd.Dims, dd.Module);
      } else if (d is IndDatatypeDecl) {
        var dd = (IndDatatypeDecl)d;
        var tps = dd.TypeArgs.ConvertAll(CloneTypeParam);
        var ctors = dd.Ctors.ConvertAll(CloneCtor);
        var dt = new IndDatatypeDecl(Tok(dd.tok), dd.Name, m, tps, ctors, CloneAttributes(dd.Attributes), dd);
        return dt;
      } else if (d is CoDatatypeDecl) {
        var dd = (CoDatatypeDecl)d;
        var tps = dd.TypeArgs.ConvertAll(CloneTypeParam);
        var ctors = dd.Ctors.ConvertAll(CloneCtor);
        var dt = new CoDatatypeDecl(Tok(dd.tok), dd.Name, m, tps, ctors, CloneAttributes(dd.Attributes), dd);
        return dt;
      } else if (d is IteratorDecl) {
        var dd = (IteratorDecl)d;
        var tps = dd.TypeArgs.ConvertAll(CloneTypeParam);
        var ins = dd.Ins.ConvertAll(CloneFormal);
        var outs = dd.Outs.ConvertAll(CloneFormal);
        var reads = CloneSpecFrameExpr(dd.Reads);
        var mod = CloneSpecFrameExpr(dd.Modifies);
        var decr = CloneSpecExpr(dd.Decreases);
        var req = dd.Requires.ConvertAll(CloneMayBeFreeExpr);
        var yreq = dd.YieldRequires.ConvertAll(CloneMayBeFreeExpr);
        var ens = dd.Ensures.ConvertAll(CloneMayBeFreeExpr);
        var yens = dd.YieldEnsures.ConvertAll(CloneMayBeFreeExpr);
        var body = CloneBlockStmt(dd.Body);
        var iter = new IteratorDecl(Tok(dd.tok), dd.Name, dd.Module,
          tps, ins, outs, reads, mod, decr,
          req, ens, yreq, yens,
          body, CloneAttributes(dd.Attributes), dd.SignatureEllipsis);
        return iter;
      } else if (d is TraitDecl) {
        if (d is DefaultClassDecl) {
          var dd = (TraitDecl)d;
          var tps = dd.TypeArgs.ConvertAll(CloneTypeParam);
          var mm = dd.Members.ConvertAll(CloneMember);
          var cl = new DefaultClassDecl(m, mm);
          return cl;
        } else {
          var dd = (TraitDecl)d;
          var tps = dd.TypeArgs.ConvertAll(CloneTypeParam);
          var mm = dd.Members.ConvertAll(CloneMember);
          var cl = new TraitDecl(Tok(dd.tok), dd.Name, m, tps, mm, CloneAttributes(dd.Attributes), dd);
          return cl;
        }
      } else if (d is ClassDecl) {
        var dd = (ClassDecl)d;
        var tps = dd.TypeArgs.ConvertAll(CloneTypeParam);
        var mm = dd.Members.ConvertAll(CloneMember);
        if (d is DefaultClassDecl) {
          return new DefaultClassDecl(m, mm, ((DefaultClassDecl)d));
        } else {
          return new ClassDecl(Tok(dd.tok), dd.Name, m, tps, mm, CloneAttributes(dd.Attributes), dd.TraitsTyp.ConvertAll(CloneType), dd);
        }
      } else if (d is ModuleDecl) {
        if (d is LiteralModuleDecl) {
          var l = new LiteralModuleDecl(((LiteralModuleDecl)d).ModuleDef, m);
          l.Signature = ((ModuleDecl)d).Signature;
          return l;
        } else if (d is AliasModuleDecl) {
          var a = (AliasModuleDecl)d;
          var alias = new AliasModuleDecl(a.Path, a.tok, m, a.Opened);
          alias.Signature = a.Signature;
          return alias;
        } else if (d is ModuleFacadeDecl) {
          var a = (ModuleFacadeDecl)d;
          var abs = new ModuleFacadeDecl(a.Path, a.tok, m, a.CompilePath, a.Opened);
          abs.Signature = a.Signature;
          abs.OriginalSignature = a.OriginalSignature;
          return abs;
        } else if (d is ModuleExportDecl) {
          var a = (ModuleExportDecl)d;
          var export = new ModuleExportDecl(a.tok, m, a.IsDefault, a.Exports, a.Extends);
          export.Signature = a.Signature;
          return export;
        } else {
          Contract.Assert(false);  // unexpected declaration
          return null;  // to please compiler
        }
      } else {
        Contract.Assert(false);  // unexpected declaration
        return null;  // to please compiler
      }
    }
Beispiel #12
0
	void Dafny() {
		List<MemberDecl/*!*/> membersDefaultClass = new List<MemberDecl/*!*/>();
		// to support multiple files, create a default module only if theModule is null
		DefaultModuleDecl defaultModule = (DefaultModuleDecl)((LiteralModuleDecl)theModule).ModuleDef;
		// theModule should be a DefaultModuleDecl (actually, the singular DefaultModuleDecl)
		Contract.Assert(defaultModule != null);
		
		while (la.kind == 63) {
			Get();
			Expect(20);
			{
			 string parsedFile = scanner.FullFilename;
			 bool isVerbatimString;
			 string includedFile = Util.RemoveParsedStringQuotes(t.val, out isVerbatimString);
			 includedFile = Util.RemoveEscaping(includedFile, isVerbatimString);
			 string fullPath = includedFile;
			 if (!Path.IsPathRooted(includedFile)) {
			   string basePath = Path.GetDirectoryName(parsedFile);
			   includedFile = Path.Combine(basePath, includedFile);
			   fullPath = Path.GetFullPath(includedFile);
			 }
			 defaultModule.Includes.Add(new Include(t, includedFile, fullPath));
			}
			
		}
		while (StartOf(1)) {
			TopDecl(defaultModule, membersDefaultClass, /* isTopLevel */ true, /* isAbstract */ false);
		}
		DefaultClassDecl defaultClass = null;
		foreach (TopLevelDecl topleveldecl in defaultModule.TopLevelDecls) {
		 defaultClass = topleveldecl as DefaultClassDecl;
		 if (defaultClass != null) {
		   defaultClass.Members.AddRange(membersDefaultClass);
		   break;
		 }
		}
		if (defaultClass == null) { // create the default class here, because it wasn't found
		 defaultClass = new DefaultClassDecl(defaultModule, membersDefaultClass);
		 defaultModule.TopLevelDecls.Add(defaultClass);
		} 
		Expect(0);
	}
Beispiel #13
0
 public TacticReplacerActor(ITextBuffer tb, int position = -1)
 {
   Contract.Requires(tb != null);
   string currentFileName;
   LoadStatus = Util.LoadAndCheckDocument(tb, out currentFileName) ? TacticReplaceStatus.Success : TacticReplaceStatus.NoDocumentPersistence;
   if(LoadStatus!=TacticReplaceStatus.Success) return;
   _program = Util.GetProgram(tb, currentFileName, true);
   _unresolvedProgram = Util.GetProgram(tb, currentFileName, false);
   _tld = (DefaultClassDecl)_program?.DefaultModuleDef.TopLevelDecls.FirstOrDefault();
   _tldMembers = _tld?.Members.GetEnumerator();
   LoadStatus = _tld != null ? TacticReplaceStatus.Success : TacticReplaceStatus.NotResolved;
   if (LoadStatus != TacticReplaceStatus.Success) return;
   if (position == -1) return;
   SetMember(position);
   SetTacticCall(position);
 }
Beispiel #14
0
 public static TacticReplaceStatus GetMemberFromPosition(DefaultClassDecl tld, int position, out MemberDecl member)
 {
   Contract.Requires(tld != null);
   member = (from m in tld.Members
             where m.tok.pos <= position && position <= m.BodyEndTok.pos + 1
             select m).FirstOrDefault();
   return member == null ? TacticReplaceStatus.NoTactic : TacticReplaceStatus.Success;
 }