cairo_lang_lowering/analysis/
mod.rs1pub mod backward;
7pub mod def_site;
8pub use backward::{BackAnalysis, DataflowBackAnalysis};
9
10pub mod core;
11pub use core::{DataflowAnalyzer, Direction, Edge, StatementLocation};
12
13pub mod dominator;
14pub mod equality_analysis;
15pub mod forward;
16pub mod use_sites;
17pub use forward::ForwardDataflowAnalysis;
18pub mod topological_order;
19
20#[cfg(test)]
21mod equality_analysis_test;
22#[cfg(test)]
23mod test;
24
25use crate::{Block, BlockId, MatchInfo, Statement, VarRemapping, VarUsage};
26
27#[derive(Clone, Copy, PartialEq, Eq, Hash)]
29pub enum DefLocation {
30 Statement(StatementLocation),
32 BlockEntry(BlockId),
34}
35
36impl DefLocation {
37 pub fn block(&self) -> BlockId {
39 match *self {
40 DefLocation::Statement((b, _)) => b,
41 DefLocation::BlockEntry(b) => b,
42 }
43 }
44}
45
46impl std::fmt::Debug for DefLocation {
47 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48 match self {
49 DefLocation::Statement((block, stmt_idx)) => write!(f, "stmt({block:?}, {stmt_idx})"),
50 DefLocation::BlockEntry(block) => write!(f, "entry({block:?})"),
51 }
52 }
53}
54
55#[derive(Clone, Copy, PartialEq, Eq, Hash)]
57pub enum UseLocation {
58 Statement(StatementLocation),
60 BlockEnd(BlockId),
62}
63
64impl UseLocation {
65 pub fn block(&self) -> BlockId {
67 match *self {
68 UseLocation::Statement((b, _)) => b,
69 UseLocation::BlockEnd(b) => b,
70 }
71 }
72}
73
74impl std::fmt::Debug for UseLocation {
75 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76 match self {
77 UseLocation::Statement((block, stmt_idx)) => write!(f, "stmt({block:?}, {stmt_idx})"),
78 UseLocation::BlockEnd(block) => write!(f, "end({block:?})"),
79 }
80 }
81}
82
83#[allow(unused_variables)]
85pub trait Analyzer<'db, 'a> {
86 type Info: Clone;
87 fn visit_block_start(&mut self, info: &mut Self::Info, block_id: BlockId, block: &Block<'db>) {}
88 fn visit_stmt(
89 &mut self,
90 info: &mut Self::Info,
91 statement_location: StatementLocation,
92 stmt: &'a Statement<'db>,
93 ) {
94 }
95 fn visit_goto(
96 &mut self,
97 info: &mut Self::Info,
98 statement_location: StatementLocation,
99 target_block_id: BlockId,
100 remapping: &'a VarRemapping<'db>,
101 ) {
102 }
103 fn merge_match(
104 &mut self,
105 statement_location: StatementLocation,
106 match_info: &'a MatchInfo<'db>,
107 infos: impl Iterator<Item = Self::Info>,
108 ) -> Self::Info;
109 fn info_from_return(
110 &mut self,
111 statement_location: StatementLocation,
112 vars: &'a [VarUsage<'db>],
113 ) -> Self::Info;
114
115 fn info_from_panic(
118 &mut self,
119 statement_location: StatementLocation,
120 var: &VarUsage<'db>,
121 ) -> Self::Info {
122 unreachable!("Panics should have been stripped in the `lower_panics` phase.");
123 }
124}