Skip to main content

cairo_lang_lowering/analysis/
mod.rs

1//! Dataflow analysis utilities for the lowering IR.
2//!
3//! This module provides generic analysis frameworks that can be used by various
4//! optimization passes and semantic checks.
5
6mod backward;
7mod core;
8
9pub use backward::BackAnalysis;
10
11use crate::{Block, BlockId, MatchInfo, Statement, VarRemapping, VarUsage};
12
13/// Location of a lowering statement inside a block.
14pub type StatementLocation = (BlockId, usize);
15
16/// Analyzer trait to implement for each specific analysis.
17#[allow(unused_variables)]
18pub trait Analyzer<'db, 'a> {
19    type Info: Clone;
20    fn visit_block_start(&mut self, info: &mut Self::Info, block_id: BlockId, block: &Block<'db>) {}
21    fn visit_stmt(
22        &mut self,
23        info: &mut Self::Info,
24        statement_location: StatementLocation,
25        stmt: &'a Statement<'db>,
26    ) {
27    }
28    fn visit_goto(
29        &mut self,
30        info: &mut Self::Info,
31        statement_location: StatementLocation,
32        target_block_id: BlockId,
33        remapping: &'a VarRemapping<'db>,
34    ) {
35    }
36    fn merge_match(
37        &mut self,
38        statement_location: StatementLocation,
39        match_info: &'a MatchInfo<'db>,
40        infos: impl Iterator<Item = Self::Info>,
41    ) -> Self::Info;
42    fn info_from_return(
43        &mut self,
44        statement_location: StatementLocation,
45        vars: &'a [VarUsage<'db>],
46    ) -> Self::Info;
47
48    /// Default `info_from_panic` implementation for post 'lower_panics' phases.
49    /// Earlier phases need to override this implementation.
50    fn info_from_panic(
51        &mut self,
52        statement_location: StatementLocation,
53        var: &VarUsage<'db>,
54    ) -> Self::Info {
55        unreachable!("Panics should have been stripped in the `lower_panics` phase.");
56    }
57}