Skip to content

Commit d0e01e6

Browse files
committed
add flow_tree
1 parent a2d62d3 commit d0e01e6

File tree

7 files changed

+108
-61
lines changed

7 files changed

+108
-61
lines changed

crates/emmylua_code_analysis/src/compilation/analyzer/doc/type_ref_tags.rs

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -209,47 +209,47 @@ pub fn analyze_return_cast(analyzer: &mut DocAnalyzer, tag: LuaDocTagReturnCast)
209209
};
210210

211211
if cast_op_type.is_nullable() {
212-
// match action {
213-
// CastAction::Add => {
214-
// analyzer.db.get_flow_index_mut().add_call_cast(
215-
// signature_id,
216-
// name,
217-
// TypeAssertion::Add(LuaType::Nil),
218-
// );
219-
// }
220-
// CastAction::Remove => {
221-
// analyzer.db.get_flow_index_mut().add_call_cast(
222-
// signature_id,
223-
// name,
224-
// TypeAssertion::Remove(LuaType::Nil),
225-
// );
226-
// }
227-
// _ => {}
228-
// }
229-
// } else if let Some(doc_type) = cast_op_type.get_type() {
230-
// let typ = infer_type(analyzer, doc_type.clone());
231-
// match action {
232-
// CastAction::Add => {
233-
// analyzer.db.get_flow_index_mut().add_call_cast(
234-
// signature_id,
235-
// name,
236-
// TypeAssertion::Add(typ),
237-
// );
238-
// }
239-
// CastAction::Remove => {
240-
// analyzer.db.get_flow_index_mut().add_call_cast(
241-
// signature_id,
242-
// name,
243-
// TypeAssertion::Remove(typ),
244-
// );
245-
// }
246-
// CastAction::Force => {
247-
// analyzer.db.get_flow_index_mut().add_call_cast(
248-
// signature_id,
249-
// name,
250-
// TypeAssertion::Force(typ),
251-
// );
252-
// }
212+
// match action {
213+
// CastAction::Add => {
214+
// analyzer.db.get_flow_index_mut().add_call_cast(
215+
// signature_id,
216+
// name,
217+
// TypeAssertion::Add(LuaType::Nil),
218+
// );
219+
// }
220+
// CastAction::Remove => {
221+
// analyzer.db.get_flow_index_mut().add_call_cast(
222+
// signature_id,
223+
// name,
224+
// TypeAssertion::Remove(LuaType::Nil),
225+
// );
226+
// }
227+
// _ => {}
228+
// }
229+
// } else if let Some(doc_type) = cast_op_type.get_type() {
230+
// let typ = infer_type(analyzer, doc_type.clone());
231+
// match action {
232+
// CastAction::Add => {
233+
// analyzer.db.get_flow_index_mut().add_call_cast(
234+
// signature_id,
235+
// name,
236+
// TypeAssertion::Add(typ),
237+
// );
238+
// }
239+
// CastAction::Remove => {
240+
// analyzer.db.get_flow_index_mut().add_call_cast(
241+
// signature_id,
242+
// name,
243+
// TypeAssertion::Remove(typ),
244+
// );
245+
// }
246+
// CastAction::Force => {
247+
// analyzer.db.get_flow_index_mut().add_call_cast(
248+
// signature_id,
249+
// name,
250+
// TypeAssertion::Force(typ),
251+
// );
252+
// }
253253
// }
254254
}
255255
}

crates/emmylua_code_analysis/src/compilation/analyzer/flow/binder.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use internment::ArcIntern;
55
use smol_str::SmolStr;
66

77
use crate::{
8-
AnalyzeError, DbIndex, FileId, FlowAntecedent, FlowId, FlowNode, FlowNodeKind, LuaClosureId,
9-
LuaDeclId,
8+
AnalyzeError, DbIndex, FileId, FlowAntecedent, FlowId, FlowNode, FlowNodeKind, FlowTree,
9+
LuaClosureId, LuaDeclId,
1010
};
1111

1212
#[derive(Debug)]
@@ -159,6 +159,16 @@ impl<'a> FlowBinder<'a> {
159159
.get_diagnostic_index_mut()
160160
.add_diagnostic(self.file_id, error);
161161
}
162+
163+
pub fn finish(self) -> FlowTree {
164+
FlowTree::new(
165+
self.decl_bind_flow_ref,
166+
self.flow_nodes,
167+
self.multiple_antecedents,
168+
self.labels,
169+
self.bindings,
170+
)
171+
}
162172
}
163173

164174
#[derive(Debug, Clone, PartialEq, Eq)]

crates/emmylua_code_analysis/src/compilation/analyzer/flow/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@ pub(crate) fn analyze(db: &mut DbIndex, context: &mut AnalyzeContext) {
1818
let file_id = in_filed_tree.file_id;
1919
let mut binder = FlowBinder::new(db, file_id);
2020
bind_analyze(&mut binder, chunk);
21+
let flow_tree = binder.finish();
22+
db.get_flow_index_mut().add_flow_tree(file_id, flow_tree);
2123
}
2224
}

crates/emmylua_code_analysis/src/db_index/flow/flow_node.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,6 @@ impl FlowNodeKind {
8888
pub fn is_unreachable(&self) -> bool {
8989
matches!(self, FlowNodeKind::Unreachable)
9090
}
91-
92-
pub fn get_negation_condition(&self) -> Option<FlowNodeKind> {
93-
match self {
94-
FlowNodeKind::TrueCondition(expr) => Some(FlowNodeKind::FalseCondition(expr.clone())),
95-
FlowNodeKind::FalseCondition(expr) => Some(FlowNodeKind::TrueCondition(expr.clone())),
96-
_ => None,
97-
}
98-
}
9991
}
10092

10193
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use std::collections::HashMap;
2+
3+
use emmylua_parser::LuaSyntaxId;
4+
use smol_str::SmolStr;
5+
6+
use crate::{FlowId, FlowNode, LuaClosureId, LuaDeclId};
7+
8+
#[derive(Debug)]
9+
pub struct FlowTree {
10+
decl_bind_flow_ref: HashMap<LuaDeclId, FlowId>,
11+
flow_nodes: Vec<FlowNode>,
12+
multiple_antecedents: Vec<Vec<FlowId>>,
13+
labels: HashMap<LuaClosureId, HashMap<SmolStr, FlowId>>,
14+
bindings: HashMap<LuaSyntaxId, FlowId>,
15+
}
16+
17+
impl FlowTree {
18+
pub fn new(
19+
decl_bind_flow_ref: HashMap<LuaDeclId, FlowId>,
20+
flow_nodes: Vec<FlowNode>,
21+
multiple_antecedents: Vec<Vec<FlowId>>,
22+
labels: HashMap<LuaClosureId, HashMap<SmolStr, FlowId>>,
23+
bindings: HashMap<LuaSyntaxId, FlowId>,
24+
) -> Self {
25+
Self {
26+
decl_bind_flow_ref,
27+
flow_nodes,
28+
multiple_antecedents,
29+
labels,
30+
bindings,
31+
}
32+
}
33+
}
Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,41 @@
11
mod flow_node;
2+
mod flow_tree;
23

34
use std::collections::HashMap;
45

5-
pub use flow_node::*;
66
use crate::FileId;
7+
pub use flow_node::*;
8+
pub use flow_tree::FlowTree;
79

8-
use super::{traits::LuaIndex, LuaSignatureId};
10+
use super::traits::LuaIndex;
911

1012
#[derive(Debug)]
1113
pub struct LuaFlowIndex {
12-
// chains_map: HashMap<FileId, HashMap<LuaVarRefId, LuaFlowChain>>,
13-
// call_cast: HashMap<FileId, HashMap<LuaSignatureId, HashMap<String, TypeAssertion>>>,
14+
file_flow_tree: HashMap<FileId, FlowTree>,
1415
}
1516

1617
impl LuaFlowIndex {
1718
pub fn new() -> Self {
1819
Self {
19-
// chains_map: HashMap::new(),
20-
// call_cast: HashMap::new(),
20+
file_flow_tree: HashMap::new(),
2121
}
2222
}
23+
24+
pub fn add_flow_tree(&mut self, file_id: FileId, flow_tree: FlowTree) {
25+
self.file_flow_tree.insert(file_id, flow_tree);
26+
}
27+
28+
pub fn get_flow_tree(&self, file_id: &FileId) -> Option<&FlowTree> {
29+
self.file_flow_tree.get(file_id)
30+
}
2331
}
2432

2533
impl LuaIndex for LuaFlowIndex {
26-
fn remove(&mut self, file_id: crate::FileId) {
27-
// self.chains_map.remove(&file_id);
28-
// self.call_cast.remove(&file_id);
34+
fn remove(&mut self, file_id: FileId) {
35+
self.file_flow_tree.remove(&file_id);
2936
}
3037

3138
fn clear(&mut self) {
32-
// self.chains_map.clear();
39+
self.file_flow_tree.clear();
3340
}
3441
}

crates/emmylua_parser/src/syntax/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,6 @@ impl<T: LuaAstNode> LuaAstPtr<T> {
248248
}
249249
}
250250
}
251+
252+
unsafe impl<T: LuaAstNode> Send for LuaAstPtr<T> {}
253+
unsafe impl<T: LuaAstNode> Sync for LuaAstPtr<T> {}

0 commit comments

Comments
 (0)