|
| 1 | +package viper.silver.frontend |
| 2 | + |
| 3 | +import viper.silver.ast.Program |
| 4 | +import viper.silver.verifier.VerificationResult |
| 5 | + |
| 6 | + |
| 7 | +/** |
| 8 | + * Trait that can be mixed into SilFrontends to make them easily usable by actual Viper frontends that do not use |
| 9 | + * Viper's parser, type checker, etc., but instead directly consistency-check and verify a Viper AST. |
| 10 | + * Compared to working directly with an instance of [[viper.silver.verifier.Verifier]], SilFrontend manages plugins |
| 11 | + * automatically. |
| 12 | + * To use it: |
| 13 | + * - create an instance f of a specific SilFrontend with ViperFrontendAPI mixed in |
| 14 | + * - call f.initialize(args), where args are the command line arguments without any file name. |
| 15 | + * - (potentially repeatedly) call f.verify(p), where p is the program to verify |
| 16 | + * - call f.stop() when done |
| 17 | + * Plugin usage must be managed via command line arguments. |
| 18 | + * Existing implementations are SiliconFrontendAPI and CarbonFrontendAPI |
| 19 | + */ |
| 20 | +trait ViperFrontendAPI extends SilFrontend { |
| 21 | + |
| 22 | + private var initialized = false |
| 23 | + override val phases: Seq[Phase] = Seq(ConsistencyCheck, Verification) |
| 24 | + val dummyInputFilename = "dummy-file-to-prevent-cli-parser-from-complaining-about-missing-file-name.silver" |
| 25 | + |
| 26 | + def initialize(args: Seq[String]): Unit = { |
| 27 | + // create verifier |
| 28 | + // parse args on frontend, set on verifier |
| 29 | + val v = createVerifier(args.mkString(" ")) |
| 30 | + setVerifier(v) |
| 31 | + _verifier = Some(v) |
| 32 | + parseCommandLine(args :+ "--ignoreFile" :+ dummyInputFilename) |
| 33 | + resetPlugins() |
| 34 | + initialized = true |
| 35 | + } |
| 36 | + |
| 37 | + protected def setStartingState() = { |
| 38 | + _state = DefaultStates.ConsistencyCheck |
| 39 | + } |
| 40 | + |
| 41 | + def verify(p: Program): VerificationResult = { |
| 42 | + if (!initialized) |
| 43 | + throw new IllegalStateException("Not initialized") |
| 44 | + setStartingState() |
| 45 | + _program = Some(p) |
| 46 | + runAllPhases() |
| 47 | + result |
| 48 | + } |
| 49 | + |
| 50 | + def stop(): Unit = { |
| 51 | + if (!initialized) |
| 52 | + throw new IllegalStateException("Not initialized") |
| 53 | + _verifier.foreach(_.stop()) |
| 54 | + } |
| 55 | + |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Version of ViperFrontendAPI that skips the consistency check phase. |
| 60 | + */ |
| 61 | +trait MinimalViperFrontendAPI extends ViperFrontendAPI { |
| 62 | + override val phases: Seq[Phase] = Seq(Verification) |
| 63 | + |
| 64 | + override protected def setStartingState() = { |
| 65 | + _state = DefaultStates.ConsistencyCheck |
| 66 | + } |
| 67 | +} |
0 commit comments