Beispiel #1
0
        bool ComputeOutliningRegions(Dafny.Program program, ITextSnapshot snapshot)
        {
            Contract.Requires(snapshot != null);

            if (program == _program)
            {
                return(false); // no new regions
            }
            List <OutliningRegion> newRegions = new List <OutliningRegion>();

            foreach (var module in program.Modules())
            {
                if (!module.IsDefaultModule)
                {
                    var nm = "module";
                    if (module.IsAbstract)
                    {
                        nm = "abstract " + nm;
                    }
                    OutliningRegion.Add(newRegions, program, module, nm);
                }
                foreach (Dafny.TopLevelDecl d in module.TopLevelDecls)
                {
                    if (!HasBodyTokens(d) && !(d is Dafny.ClassDecl))
                    {
                        continue;
                    }
                    if (d is Dafny.OpaqueTypeDecl)
                    {
                        OutliningRegion.Add(newRegions, program, d, "type");
                    }
                    else if (d is Dafny.CoDatatypeDecl)
                    {
                        OutliningRegion.Add(newRegions, program, d, "codatatype");
                    }
                    else if (d is Dafny.DatatypeDecl)
                    {
                        OutliningRegion.Add(newRegions, program, d, "datatype");
                    }
                    else if (d is Dafny.ModuleDecl)
                    {
                        // do nothing here, since the outer loop handles modules
                    }
                    else
                    {
                        var cl = (Dafny.ClassDecl)d;
                        if (cl.IsDefaultClass)
                        {
                            // do nothing
                        }
                        else if (cl is Dafny.IteratorDecl)
                        {
                            OutliningRegion.Add(newRegions, program, cl, "iterator");
                        }
                        else
                        {
                            OutliningRegion.Add(newRegions, program, cl, "class");
                        }
                        // do the class members (in particular, functions and methods)
                        foreach (Dafny.MemberDecl m in cl.Members)
                        {
                            if (!HasBodyTokens(m))
                            {
                                continue;
                            }
                            if (m is Dafny.Function && ((Dafny.Function)m).Body != null)
                            {
                                var nm =
                                    m is Dafny.InductivePredicate ? "inductive predicate" :
                                    m is Dafny.CoPredicate ? "copredicate" :
                                    // m is Dafny.PrefixPredicate ? "prefix predicate" :  // this won't ever occur here
                                    m is Dafny.Predicate ? "predicate" :
                                    "function";
                                if (!m.IsGhost)
                                {
                                    nm += " method";
                                }
                                OutliningRegion.Add(newRegions, program, m, nm);
                            }
                            else if (m is Dafny.Method && ((Dafny.Method)m).Body != null)
                            {
                                var nm =
                                    m is Dafny.Constructor ? "constructor" :
                                    m is Dafny.CoLemma ? "colemma" :
                                    m is Dafny.Lemma ? "lemma" :
                                    // m is Dafny.PrefixLemma ? "prefix lemma" :  // this won't ever occur here
                                    "method";
                                if (m.IsGhost && !(m is Dafny.CoLemma))
                                {
                                    nm = "ghost " + nm;
                                }
                                OutliningRegion.Add(newRegions, program, m, nm);
                            }
                        }
                    }
                }
            }
            _snapshot = snapshot;
            _regions  = newRegions;
            _program  = program;
            return(true);
        }
Beispiel #2
0
        bool ComputeIdentifierRegions(Microsoft.Dafny.Program program, ITextSnapshot snapshot)
        {
            Contract.Requires(snapshot != null);

            if (program == _program)
            {
                return(false); // no new regions
            }
            _snapshot = snapshot;
            List <IdRegion> newRegions = new List <IdRegion>();

            foreach (var info in program.reporter.AllMessages[ErrorLevel.Info])
            {
                IdRegion.Add(newRegions, program, info.token, info.message, info.token.val.Length);
            }

            foreach (var module in program.Modules())
            {
                if (module.IsFacade)
                {
                    continue;
                }
                foreach (var d in module.TopLevelDecls)
                {
                    if (d is IteratorDecl)
                    {
                        // iterators already get a hover text that shows the class desugaring, so that hover text shows the type parameters
                    }
                    else if (d is OpaqueTypeDecl)
                    {
                        IdRegion.Add(newRegions, program, d.tok,
                                     string.Format("{0} {1}{2}", d.WhatKind, d.Name, Printer.TPCharacteristicsSuffix(((OpaqueTypeDecl)d).Characteristics)),
                                     d.TypeArgs);
                    }
                    else if (d is TypeSynonymDecl) // also covers SubsetTypeDecl
                    {
                        IdRegion.Add(newRegions, program, d.tok,
                                     string.Format("{0} {1}{2}", d.WhatKind, d.Name, Printer.TPCharacteristicsSuffix(((TypeSynonymDecl)d).Characteristics)),
                                     d.TypeArgs);
                    }
                    else
                    {
                        IdRegion.Add(newRegions, program, d.tok, string.Format("{0} {1}", d.WhatKind, d.Name), d.TypeArgs);
                    }
                    IdRegion.AddRecognizedAttributes(d.Attributes, newRegions, program);
                    if (d is DatatypeDecl)
                    {
                        var dt = (DatatypeDecl)d;
                        foreach (var ctor in dt.Ctors)
                        {
                            foreach (var dtor in ctor.Destructors)
                            {
                                var i      = dtor.EnclosingCtors.IndexOf(ctor);
                                var formal = dtor.CorrespondingFormals[i];
                                if (formal.HasName)
                                {
                                    IdRegion.Add(newRegions, program, formal.tok, dtor, null, "destructor", true, module);
                                }
                            }
                        }
                    }
                    else if (d is IteratorDecl)
                    {
                        var iter = (IteratorDecl)d;
                        foreach (var p in iter.Ins)
                        {
                            IdRegion.Add(newRegions, program, p.tok, p, true, iter, module);
                        }
                        foreach (var p in iter.Outs)
                        {
                            IdRegion.Add(newRegions, program, p.tok, p, true, "yield-parameter", iter, module);
                        }
                        iter.Reads.Expressions.ForEach(fe => FrameExprRegions(fe, newRegions, true, program, module));
                        iter.Modifies.Expressions.ForEach(fe => FrameExprRegions(fe, newRegions, true, program, module));
                        iter.Requires.ForEach(e => ExprRegions(e.E, newRegions, program, module));
                        iter.YieldRequires.ForEach(e => ExprRegions(e.E, newRegions, program, module));
                        iter.YieldEnsures.ForEach(e => ExprRegions(e.E, newRegions, program, module));
                        iter.Ensures.ForEach(e => ExprRegions(e.E, newRegions, program, module));
                        if (!((ICallable)iter).InferredDecreases)
                        {
                            iter.Decreases.Expressions.ForEach(e => ExprRegions(e, newRegions, program, module));
                        }
                        if (iter.Body != null)
                        {
                            StatementRegions(iter.Body, newRegions, program, module);
                        }
                    }
                    else if (d is ClassDecl)
                    {
                        var cl = (ClassDecl)d;
                        foreach (var member in cl.Members)
                        {
                            IdRegion.AddRecognizedAttributes(member.Attributes, newRegions, program);
                            if (Attributes.Contains(member.Attributes, "auto_generated"))
                            {
                                // do nothing
                            }
                            else if (member is Function)
                            {
                                var f    = (Function)member;
                                var kind = f.WhatKind;
                                var name = f.Name;
                                if (!(f.EnclosingClass is DefaultClassDecl))
                                {
                                    if (f.IsStatic)
                                    {
                                        kind = "static " + kind;
                                    }
                                    name = f.EnclosingClass.Name + "." + name;
                                }
                                IdRegion.Add(newRegions, program, f.tok, string.Format("{0} {1}", kind, name), f.TypeArgs);
                                foreach (var p in f.Formals)
                                {
                                    IdRegion.Add(newRegions, program, p.tok, p, true, f, module);
                                }
                                if (f.Result != null)
                                {
                                    IdRegion.Add(newRegions, program, f.Result.tok, f.Result, true, f, module);
                                }
                                f.Req.ForEach(e => ExprRegions(e.E, newRegions, program, module));
                                f.Reads.ForEach(fe => FrameExprRegions(fe, newRegions, true, program, module));
                                f.Ens.ForEach(e => ExprRegions(e.E, newRegions, program, module));
                                f.Decreases.Expressions.ForEach(e => ExprRegions(e, newRegions, program, module));
                                if (f.Body != null)
                                {
                                    ExprRegions(f.Body, newRegions, program, module);
                                }
                            }
                            else if (member is Method)
                            {
                                var m    = (Method)member;
                                var kind = m.WhatKind;
                                var name = m.Name;
                                if (!(m.EnclosingClass is DefaultClassDecl))
                                {
                                    if (m.IsStatic)
                                    {
                                        kind = "static " + kind;
                                    }
                                    name = m.EnclosingClass.Name + "." + name;
                                }
                                IdRegion.Add(newRegions, program, m.tok, string.Format("{0} {1}", kind, name), m.TypeArgs);
                                foreach (var p in m.Ins)
                                {
                                    IdRegion.Add(newRegions, program, p.tok, p, true, m, module);
                                }
                                foreach (var p in m.Outs)
                                {
                                    IdRegion.Add(newRegions, program, p.tok, p, true, m, module);
                                }
                                m.Req.ForEach(e => ExprRegions(e.E, newRegions, program, module));
                                m.Mod.Expressions.ForEach(fe => FrameExprRegions(fe, newRegions, true, program, module));
                                m.Ens.ForEach(e => ExprRegions(e.E, newRegions, program, module));
                                m.Decreases.Expressions.ForEach(e => ExprRegions(e, newRegions, program, module));
                                if (m.Body != null)
                                {
                                    StatementRegions(m.Body, newRegions, program, module);
                                }
                            }
                            else if (member is ConstantField)
                            {
                                var cf = (ConstantField)member;
                                IdRegion.Add(newRegions, program, cf.tok, cf, null, cf.IsStatic && !(cf.EnclosingClass is DefaultClassDecl) ? "static const" : "const", true, module);
                                if (cf.Rhs != null)
                                {
                                    ExprRegions(cf.Rhs, newRegions, program, module);
                                }
                            }
                            else if (member is SpecialField)
                            {
                                // do nothing
                            }
                            else if (member is Field)
                            {
                                var fld = (Field)member;
                                IdRegion.Add(newRegions, program, fld.tok, fld, null, "field", true, module);
                            }
                        }
                    }
                    else if (d is NewtypeDecl)
                    {
                        var dd = (NewtypeDecl)d;
                        if (dd.Var != null)
                        {
                            IdRegion.Add(newRegions, program, dd.Var.tok, dd.Var, true, (ICallable)null, module);
                            ExprRegions(dd.Constraint, newRegions, program, module);
                        }
                    }
                    else if (d is SubsetTypeDecl)
                    {
                        var dd = (SubsetTypeDecl)d;
                        if (dd.Var != null)
                        {
                            IdRegion.Add(newRegions, program, dd.Var.tok, dd.Var, true, (ICallable)null, module);
                            ExprRegions(dd.Constraint, newRegions, program, module);
                        }
                    }
                }
            }
            _regions = newRegions;
            _program = program;
            return(true);
        }
Beispiel #3
0
        bool ComputeIdentifierRegions(Microsoft.Dafny.Program program, ITextSnapshot snapshot)
        {
            Contract.Requires(snapshot != null);

            if (program == _program)
            {
                return(false); // no new regions
            }
            _snapshot = snapshot;
            List <IdRegion> newRegions = new List <IdRegion>();

            foreach (var info in program.reporter.AllMessages[ErrorLevel.Info])
            {
                IdRegion.Add(newRegions, program, info.token, info.message, info.token.val.Length);
            }

            foreach (var module in program.Modules())
            {
                if (module.IsFacade)
                {
                    continue;
                }
                foreach (var d in module.TopLevelDecls)
                {
                    IdRegion.AddRecognizedAttributes(d.Attributes, newRegions, program);
                    if (d is DatatypeDecl)
                    {
                        var dt = (DatatypeDecl)d;
                        foreach (var ctor in dt.Ctors)
                        {
                            foreach (var dtor in ctor.Destructors)
                            {
                                var i      = dtor.EnclosingCtors.IndexOf(ctor);
                                var formal = dtor.CorrespondingFormals[i];
                                if (formal.HasName)
                                {
                                    IdRegion.Add(newRegions, program, formal.tok, dtor, null, "destructor", true, module);
                                }
                            }
                        }
                    }
                    else if (d is IteratorDecl)
                    {
                        var iter = (IteratorDecl)d;
                        foreach (var p in iter.Ins)
                        {
                            IdRegion.Add(newRegions, program, p.tok, p, true, iter, module);
                        }
                        foreach (var p in iter.Outs)
                        {
                            IdRegion.Add(newRegions, program, p.tok, p, true, "yield-parameter", iter, module);
                        }
                        iter.Reads.Expressions.ForEach(fe => FrameExprRegions(fe, newRegions, true, program, module));
                        iter.Modifies.Expressions.ForEach(fe => FrameExprRegions(fe, newRegions, true, program, module));
                        iter.Requires.ForEach(e => ExprRegions(e.E, newRegions, program, module));
                        iter.YieldRequires.ForEach(e => ExprRegions(e.E, newRegions, program, module));
                        iter.YieldEnsures.ForEach(e => ExprRegions(e.E, newRegions, program, module));
                        iter.Ensures.ForEach(e => ExprRegions(e.E, newRegions, program, module));
                        if (!((ICallable)iter).InferredDecreases)
                        {
                            iter.Decreases.Expressions.ForEach(e => ExprRegions(e, newRegions, program, module));
                        }
                        if (iter.Body != null)
                        {
                            StatementRegions(iter.Body, newRegions, program, module);
                        }
                    }
                    else if (d is ClassDecl)
                    {
                        var cl = (ClassDecl)d;
                        foreach (var member in cl.Members)
                        {
                            IdRegion.AddRecognizedAttributes(member.Attributes, newRegions, program);
                            if (Attributes.Contains(member.Attributes, "auto_generated"))
                            {
                                // do nothing
                            }
                            else if (member is Function)
                            {
                                var f = (Function)member;
                                foreach (var p in f.Formals)
                                {
                                    IdRegion.Add(newRegions, program, p.tok, p, true, f, module);
                                }
                                f.Req.ForEach(e => ExprRegions(e, newRegions, program, module));
                                f.Reads.ForEach(fe => FrameExprRegions(fe, newRegions, true, program, module));
                                f.Ens.ForEach(e => ExprRegions(e, newRegions, program, module));
                                f.Decreases.Expressions.ForEach(e => ExprRegions(e, newRegions, program, module));
                                if (f.Body != null)
                                {
                                    ExprRegions(f.Body, newRegions, program, module);
                                }
                            }
                            else if (member is Method)
                            {
                                var m = (Method)member;
                                foreach (var p in m.Ins)
                                {
                                    IdRegion.Add(newRegions, program, p.tok, p, true, m, module);
                                }
                                foreach (var p in m.Outs)
                                {
                                    IdRegion.Add(newRegions, program, p.tok, p, true, m, module);
                                }
                                m.Req.ForEach(e => ExprRegions(e.E, newRegions, program, module));
                                m.Mod.Expressions.ForEach(fe => FrameExprRegions(fe, newRegions, true, program, module));
                                m.Ens.ForEach(e => ExprRegions(e.E, newRegions, program, module));
                                m.Decreases.Expressions.ForEach(e => ExprRegions(e, newRegions, program, module));
                                if (m.Body != null)
                                {
                                    StatementRegions(m.Body, newRegions, program, module);
                                }
                            }
                            else if (member is SpecialField)
                            {
                                // do nothing
                            }
                            else if (member is Field)
                            {
                                var fld = (Field)member;
                                IdRegion.Add(newRegions, program, fld.tok, fld, null, "field", true, module);
                            }
                        }
                    }
                    else if (d is NewtypeDecl)
                    {
                        var dd = (NewtypeDecl)d;
                        if (dd.Var != null)
                        {
                            IdRegion.Add(newRegions, program, dd.Var.tok, dd.Var, true, (ICallable)null, module);
                            ExprRegions(dd.Constraint, newRegions, program, module);
                        }
                    }
                }
            }
            _regions = newRegions;
            _program = program;
            return(true);
        }