circomspect_program_analysis/
analysis_context.rs

1use thiserror::Error;
2
3use program_structure::{
4    file_definition::{FileID, FileLocation},
5    cfg::Cfg,
6};
7
8/// Errors returned by the analysis context.
9#[derive(Debug, Error)]
10pub enum AnalysisError {
11    /// This function has no corresponding AST.
12    #[error("Unknown function `{name}`.")]
13    UnknownFunction { name: String },
14    /// This template has no corresponding AST.
15    #[error("Unknown template `{name}`.")]
16    UnknownTemplate { name: String },
17    /// This function has an AST, but we failed to lift it to a corresponding
18    /// CFG.
19    #[error("Failed to lift the function `{name}`.")]
20    FailedToLiftFunction { name: String },
21    /// This template has an AST, but we failed to lift it to a corresponding
22    /// CFG.
23    #[error("Failed to lift the template `{name}`.")]
24    FailedToLiftTemplate { name: String },
25    /// The file ID does not correspond to a known file.
26    #[error("Unknown file ID `{file_id}`.")]
27    UnknownFile { file_id: FileID },
28    /// The location does not exist in the file with the given ID.
29    #[error("The location `{}:{}` is not valid for the file with file ID `{file_id}`.", file_location.start, file_location.end)]
30    InvalidLocation { file_id: FileID, file_location: FileLocation },
31}
32
33/// Context passed to each analysis pass.
34pub trait AnalysisContext {
35    /// Returns true if the context knows of a function with the given name.
36    /// This method does not compute the CFG of the function which saves time
37    /// compared to `AnalysisContext::function`.
38    fn is_function(&self, name: &str) -> bool;
39
40    /// Returns true if the context knows of a template with the given name.
41    /// This method does not compute the CFG of the template which saves time
42    /// compared to `AnalysisContext::template`.
43    fn is_template(&self, name: &str) -> bool;
44
45    /// Returns the CFG for the function with the given name.
46    fn function(&mut self, name: &str) -> Result<&Cfg, AnalysisError>;
47
48    /// Returns the CFG for the template with the given name.
49    fn template(&mut self, name: &str) -> Result<&Cfg, AnalysisError>;
50
51    /// Returns the string corresponding to the given file ID and location.
52    fn underlying_str(
53        &self,
54        file_id: &FileID,
55        file_location: &FileLocation,
56    ) -> Result<String, AnalysisError>;
57}