summaryrefslogtreecommitdiff
path: root/Util/VS2010/DafnyExtension/DafnyExtension/DafnyDriver.cs
blob: c691d9dabfcc51284b05b360701e4657199b42fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Diagnostics.Contracts;
// Here come the Dafny/Boogie specific imports
//using PureCollections;
using Bpl = Microsoft.Boogie;
using Dafny = Microsoft.Dafny;
using Microsoft.Boogie.AbstractInterpretation;
using VC;
// using AI = Microsoft.AbstractInterpretationFramework;


namespace DafnyLanguage
{
  class DafnyDriver
  {
    readonly string _programText;
    readonly string _filename;
    Dafny.Program _program;
    List<DafnyError> _errors = new List<DafnyError>();
    public List<DafnyError> Errors { get { return _errors; } }

    public DafnyDriver(string programText, string filename) {
      _programText = programText;
      _filename = filename;
    }

    void RecordError(int line, int col, ErrorCategory cat, string msg) {
      _errors.Add(new DafnyError(line, col, cat, msg));
    }

    static DafnyDriver() {
      Initialize();
    }

    static void Initialize() {
      if (Dafny.DafnyOptions.O == null) {
        var options = new Dafny.DafnyOptions();
        options.ProverKillTime = 10;
        options.ErrorTrace = 0;
        Dafny.DafnyOptions.Install(options);
        options.ApplyDefaultOptions();
      }
    }

    public Dafny.Program ProcessResolution() {
      if (!ParseAndTypeCheck()) {
        return null;
      }
      return _program;
    }

    bool ParseAndTypeCheck() {
      Dafny.ModuleDecl module = new Dafny.LiteralModuleDecl(new Dafny.DefaultModuleDecl(), null);
      Dafny.BuiltIns builtIns = new Dafny.BuiltIns();
      int errorCount = Dafny.Parser.Parse(_programText, _filename, module, builtIns, new VSErrors(this));
      if (errorCount != 0)
        return false;
      Dafny.Program program = new Dafny.Program(_filename, module, builtIns);

      Dafny.Resolver r = new VSResolver(program, this);
      r.ResolveProgram(program);
      if (r.ErrorCount != 0)
        return false;

      _program = program;
      return true;  // success
    }

    class VSErrors : Dafny.Errors
    {
      DafnyDriver dd;
      public VSErrors(DafnyDriver dd) {
        this.dd = dd;
      }
      public override void SynErr(string filename, int line, int col, string msg) {
        dd.RecordError(line - 1, col - 1, ErrorCategory.ParseError, msg);
        count++;
      }
      public override void SemErr(string filename, int line, int col, string msg) {
        dd.RecordError(line - 1, col - 1, ErrorCategory.ResolveError, msg);
        count++;
      }
      public override void Warning(string filename, int line, int col, string msg) {
        dd.RecordError(line - 1, col - 1, ErrorCategory.ParseWarning, msg);
      }
    }

    class VSResolver : Dafny.Resolver
    {
      DafnyDriver dd;
      public VSResolver(Dafny.Program program, DafnyDriver dd)
        : base(program) {
        this.dd = dd;
      }
      public override void Error(Bpl.IToken tok, string msg, params object[] args) {
        string s = string.Format(msg, args);
        dd.RecordError(tok.line - 1, tok.col - 1, ErrorCategory.ResolveError, s);
        ErrorCount++;
      }
    }

    public static bool Verify(Dafny.Program dafnyProgram, ErrorReporterDelegate er) {
      Dafny.Translator translator = new Dafny.Translator();
      Bpl.Program boogieProgram = translator.Translate(dafnyProgram);

      PipelineOutcome oc = BoogiePipeline(boogieProgram, er);
      switch (oc) {
        case PipelineOutcome.Done:
        case PipelineOutcome.VerificationCompleted:
          // TODO:  This would be the place to proceed to compile the program, if desired
          return true;
        case PipelineOutcome.FatalError:
        default:
          return false;
      }
    }

    enum PipelineOutcome { Done, ResolutionError, TypeCheckingError, ResolvedAndTypeChecked, FatalError, VerificationCompleted }

    /// <summary>
    /// Resolve, type check, infer invariants for, and verify the given Boogie program.
    /// The intention is that this Boogie program has been produced by translation from something
    /// else.  Hence, any resolution errors and type checking errors are due to errors in
    /// the translation.
    /// </summary>
    static PipelineOutcome BoogiePipeline(Bpl.Program/*!*/ program, ErrorReporterDelegate er) {
      Contract.Requires(program != null);

      PipelineOutcome oc = BoogieResolveAndTypecheck(program);
      if (oc == PipelineOutcome.ResolvedAndTypeChecked) {
        EliminateDeadVariablesAndInline(program);
        return BoogieInferAndVerify(program, er);
      }
      return oc;
    }

    static void EliminateDeadVariablesAndInline(Bpl.Program program) {
      Contract.Requires(program != null);
      // Eliminate dead variables
      Microsoft.Boogie.UnusedVarEliminator.Eliminate(program);

      // Collect mod sets
      if (Bpl.CommandLineOptions.Clo.DoModSetAnalysis) {
        Microsoft.Boogie.ModSetCollector.DoModSetAnalysis(program);
      }

      // Coalesce blocks
      if (Bpl.CommandLineOptions.Clo.CoalesceBlocks) {
        Microsoft.Boogie.BlockCoalescer.CoalesceBlocks(program);
      }

      // Inline
      var TopLevelDeclarations = program.TopLevelDeclarations;

      if (Bpl.CommandLineOptions.Clo.ProcedureInlining != Bpl.CommandLineOptions.Inlining.None) {
        bool inline = false;
        foreach (var d in TopLevelDeclarations) {
          if (d.FindExprAttribute("inline") != null) {
            inline = true;
          }
        }
        if (inline && Bpl.CommandLineOptions.Clo.StratifiedInlining == 0) {
          foreach (var d in TopLevelDeclarations) {
            var impl = d as Bpl.Implementation;
            if (impl != null) {
              impl.OriginalBlocks = impl.Blocks;
              impl.OriginalLocVars = impl.LocVars;
            }
          }
          foreach (var d in TopLevelDeclarations) {
            var impl = d as Bpl.Implementation;
            if (impl != null && !impl.SkipVerification) {
              Bpl.Inliner.ProcessImplementation(program, impl);
            }
          }
          foreach (var d in TopLevelDeclarations) {
            var impl = d as Bpl.Implementation;
            if (impl != null) {
              impl.OriginalBlocks = null;
              impl.OriginalLocVars = null;
            }
          }
        }
      }
    }

    /// <summary>
    /// Resolves and type checks the given Boogie program.
    /// Returns:
    ///  - Done if no errors occurred, and command line specified no resolution or no type checking.
    ///  - ResolutionError if a resolution error occurred
    ///  - TypeCheckingError if a type checking error occurred
    ///  - ResolvedAndTypeChecked if both resolution and type checking succeeded
    /// </summary>
    static PipelineOutcome BoogieResolveAndTypecheck(Bpl.Program program) {
      Contract.Requires(program != null);
      // ---------- Resolve ------------------------------------------------------------
      int errorCount = program.Resolve();
      if (errorCount != 0) {
        return PipelineOutcome.ResolutionError;
      }

      // ---------- Type check ------------------------------------------------------------
      errorCount = program.Typecheck();
      if (errorCount != 0) {
        return PipelineOutcome.TypeCheckingError;
      }

      return PipelineOutcome.ResolvedAndTypeChecked;
    }

    /// <summary>
    /// Given a resolved and type checked Boogie program, infers invariants for the program
    /// and then attempts to verify it.  Returns:
    ///  - Done if command line specified no verification
    ///  - FatalError if a fatal error occurred
    ///  - VerificationCompleted if inference and verification completed, in which the out
    ///    parameters contain meaningful values
    /// </summary>
    static PipelineOutcome BoogieInferAndVerify(Bpl.Program program, ErrorReporterDelegate er) {
      Contract.Requires(program != null);

      // ---------- Infer invariants --------------------------------------------------------

      // Abstract interpretation -> Always use (at least) intervals, if not specified otherwise (e.g. with the "/noinfer" switch)
      if (Bpl.CommandLineOptions.Clo.UseAbstractInterpretation) {
        if (Bpl.CommandLineOptions.Clo.Ai.J_Intervals || Bpl.CommandLineOptions.Clo.Ai.J_Trivial) {
          Microsoft.Boogie.AbstractInterpretation.NativeAbstractInterpretation.RunAbstractInterpretation(program);
        } else {
          // use /infer:j as the default
          Bpl.CommandLineOptions.Clo.Ai.J_Intervals = true;
          Microsoft.Boogie.AbstractInterpretation.NativeAbstractInterpretation.RunAbstractInterpretation(program);
        }
      }

      if (Bpl.CommandLineOptions.Clo.LoopUnrollCount != -1) {
        program.UnrollLoops(Bpl.CommandLineOptions.Clo.LoopUnrollCount);
      }

      if (Bpl.CommandLineOptions.Clo.ExpandLambdas) {
        Bpl.LambdaHelper.ExpandLambdas(program);
        //PrintBplFile ("-", program, true);
      }

      // ---------- Verify ------------------------------------------------------------

      if (!Bpl.CommandLineOptions.Clo.Verify) { return PipelineOutcome.Done; }

      #region Verify each implementation

      ConditionGeneration vcgen = null;
      try {
        if (Bpl.CommandLineOptions.Clo.vcVariety == Bpl.CommandLineOptions.VCVariety.Doomed) {
          vcgen = new DCGen(program, Bpl.CommandLineOptions.Clo.SimplifyLogFilePath, Bpl.CommandLineOptions.Clo.SimplifyLogFileAppend);
        } else {
          vcgen = new VCGen(program, Bpl.CommandLineOptions.Clo.SimplifyLogFilePath, Bpl.CommandLineOptions.Clo.SimplifyLogFileAppend);
        }
      } catch (Bpl.ProverException) {
        return PipelineOutcome.FatalError;
      }

      var decls = program.TopLevelDeclarations.ToArray();
      foreach (var decl in decls) {
        Contract.Assert(decl != null);
        Bpl.Implementation impl = decl as Bpl.Implementation;
        if (impl != null && Bpl.CommandLineOptions.Clo.UserWantsToCheckRoutine(impl.Name) && !impl.SkipVerification) {
          List<Bpl.Counterexample>/*?*/ errors;

          ConditionGeneration.Outcome outcome;
          int prevAssertionCount = vcgen.CumulativeAssertionCount;
          try {
            outcome = vcgen.VerifyImplementation(impl, out errors);
          } catch (VCGenException) {
            errors = null;
            outcome = VCGen.Outcome.Inconclusive;
          } catch (Bpl.UnexpectedProverOutputException) {
            errors = null;
            outcome = VCGen.Outcome.Inconclusive;
          }

          switch (outcome) {
            default:
              Contract.Assert(false); throw new Exception();  // unexpected outcome
            case VCGen.Outcome.Correct:
              break;
            case VCGen.Outcome.TimedOut:
              er(new DafnyErrorInformation(impl.tok, "Verification timed out (" + impl.Name + ")"));
              break;
            case VCGen.Outcome.OutOfMemory:
              er(new DafnyErrorInformation(impl.tok, "Verification out of memory (" + impl.Name + ")"));
              break;
            case VCGen.Outcome.Inconclusive:
              er(new DafnyErrorInformation(impl.tok, "Verification inconclusive (" + impl.Name + ")"));
              break;
            case VCGen.Outcome.Errors:
              Contract.Assert(errors != null);  // guaranteed by postcondition of VerifyImplementation

              errors.Sort(new Bpl.CounterexampleComparer());
              foreach (var error in errors) {
                DafnyErrorInformation errorInfo;

                if (error is Bpl.CallCounterexample) {
                  var err = (Bpl.CallCounterexample)error;
                  errorInfo = new DafnyErrorInformation(err.FailingCall.tok, err.FailingCall.ErrorData as string ?? "A precondition for this call might not hold.");
                  errorInfo.AddAuxInfo(err.FailingRequires.tok, err.FailingRequires.ErrorData as string ?? "Related location: This is the precondition that might not hold.");

                } else if (error is Bpl.ReturnCounterexample) {
                  var err = (Bpl.ReturnCounterexample)error;
                  errorInfo = new DafnyErrorInformation(err.FailingReturn.tok, "A postcondition might not hold on this return path.");
                  errorInfo.AddAuxInfo(err.FailingEnsures.tok, err.FailingEnsures.ErrorData as string ?? "Related location: This is the postcondition that might not hold.");
                  errorInfo.AddAuxInfo(err.FailingEnsures.Attributes);

                } else { // error is AssertCounterexample
                  var err = (Bpl.AssertCounterexample)error;
                  if (err.FailingAssert is Bpl.LoopInitAssertCmd) {
                    errorInfo = new DafnyErrorInformation(err.FailingAssert.tok, "This loop invariant might not hold on entry.");
                  } else if (err.FailingAssert is Bpl.LoopInvMaintainedAssertCmd) {
                    // this assertion is a loop invariant which is not maintained
                    errorInfo = new DafnyErrorInformation(err.FailingAssert.tok, "This loop invariant might not be maintained by the loop.");
                  } else {
                    string msg = err.FailingAssert.ErrorData as string;
                    if (msg == null) {
                      msg = "This assertion might not hold.";
                    }
                    errorInfo = new DafnyErrorInformation(err.FailingAssert.tok, msg);
                    errorInfo.AddAuxInfo(err.FailingAssert.Attributes);
                  }
                }
                if (Bpl.CommandLineOptions.Clo.ErrorTrace > 0) {
                  foreach (Bpl.Block b in error.Trace) {
                    // for ErrorTrace == 1 restrict the output;
                    // do not print tokens with -17:-4 as their location because they have been
                    // introduced in the translation and do not give any useful feedback to the user
                    if (!(Bpl.CommandLineOptions.Clo.ErrorTrace == 1 && b.tok.line == -17 && b.tok.col == -4) &&
                        b.tok.line != 0 && b.tok.col != 0) {
                      errorInfo.AddAuxInfo(b.tok, "Execution trace: " + b.Label);
                    }
                  }
                }
                // if (Bpl.CommandLineOptions.Clo.ModelViewFile != null) {
                //   error.PrintModel();
                // }
                er(errorInfo);
              }
              break;
          }
        }
      }
      vcgen.Close();
      Bpl.CommandLineOptions.Clo.TheProverFactory.Close();

      #endregion

      return PipelineOutcome.VerificationCompleted;
    }

    public delegate void ErrorReporterDelegate(DafnyErrorInformation errInfo);

    public class DafnyErrorInformation
    {
      public readonly Bpl.IToken Tok;
      public readonly string Msg;
      public readonly List<DafnyErrorAuxInfo> Aux = new List<DafnyErrorAuxInfo>();

      public class DafnyErrorAuxInfo
      {
        public readonly Bpl.IToken Tok;
        public readonly string Msg;
        public DafnyErrorAuxInfo(Bpl.IToken tok, string msg) {
          Tok = tok;
          Msg = CleanUp(msg);
        }
      }

      public DafnyErrorInformation(Bpl.IToken tok, string msg) {
        Contract.Requires(tok != null);
        Contract.Requires(1 <= tok.line && 1 <= tok.col);
        Contract.Requires(msg != null);
        Tok = tok;
        Msg = CleanUp(msg);
        AddNestingsAsAux(tok);
      }
      public void AddAuxInfo(Bpl.IToken tok, string msg) {
        Contract.Requires(tok != null);
        Contract.Requires(1 <= tok.line && 1 <= tok.col);
        Contract.Requires(msg != null);
        Aux.Add(new DafnyErrorAuxInfo(tok, msg));
        AddNestingsAsAux(tok);
      }
      void AddNestingsAsAux(Bpl.IToken tok) {
        while (tok is Dafny.NestedToken) {
          var nt = (Dafny.NestedToken)tok;
          tok = nt.Inner;
          Aux.Add(new DafnyErrorAuxInfo(tok, "Related location"));
        }
      }
      public void AddAuxInfo(Bpl.QKeyValue attr) {
        while (attr != null) {
          if (attr.Key == "msg" && attr.Params.Count == 1 && attr.tok.line != 0 && attr.tok.col != 0) {
            var str = attr.Params[0] as string;
            if (str != null) {
              AddAuxInfo(attr.tok, str);
            }
          }
          attr = attr.Next;
        }
      }

      public static string CleanUp(string msg) {
        if (msg.ToLower().StartsWith("error: ")) {
          return msg.Substring(7);
        } else {
          return msg;
        }
      }
    }
  }
}