ty_python_core/
statement.rs1use crate::ast_node_ref::AstNodeRef;
2use crate::db::Db;
3use crate::definition::Definition;
4use crate::expression::Expression;
5use crate::node_key::NodeKey;
6use crate::scope::{FileScopeId, ScopeId};
7use crate::{Program, ProgramFile};
8use ruff_db::PythonFile;
9use ruff_db::files::File;
10use ruff_python_ast as ast;
11use salsa;
12
13#[derive(
18 Clone, Copy, Debug, Eq, Hash, PartialEq, salsa::Supertype, get_size2::GetSize, salsa::SalsaValue,
19)]
20pub enum Statement<'db> {
21 Expression(Expression<'db>),
22 Definition(Definition<'db>),
23 Other(StatementInner<'db>),
24}
25
26#[salsa::tracked(debug, heap_size=ruff_memory_usage::heap_size)]
40pub struct StatementInner<'db> {
41 #[returns(copy)]
43 pub program_file: ProgramFile<'db>,
44
45 #[returns(copy)]
47 pub file_scope: FileScopeId,
48
49 #[no_eq]
51 #[tracked]
52 #[returns(ref)]
53 pub node_ref: AstNodeRef<ast::Stmt>,
54}
55
56impl get_size2::GetSize for StatementInner<'_> {}
58
59impl<'db> StatementInner<'db> {
60 pub fn file(self, db: &'db dyn Db) -> File {
61 self.program_file(db).file(db)
62 }
63
64 pub fn python_file(self, db: &'db dyn Db) -> PythonFile<'db> {
65 self.program_file(db).python_file(db)
66 }
67
68 pub fn scope(self, db: &'db dyn Db) -> ScopeId<'db> {
69 self.file_scope(db).to_scope_id(db, self.program_file(db))
70 }
71
72 pub fn program(self, db: &'db dyn Db) -> Program<'db> {
73 self.scope(db).program(db)
74 }
75}
76
77#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, get_size2::GetSize, salsa::SalsaValue)]
78pub struct StatementNodeKey(NodeKey);
79
80impl From<&ast::Stmt> for StatementNodeKey {
81 fn from(node: &ast::Stmt) -> Self {
82 Self(NodeKey::from_node(node))
83 }
84}