Skip to content

Commit 1bb9262

Browse files
committed
stash
1 parent 218f168 commit 1bb9262

File tree

9 files changed

+235
-396
lines changed

9 files changed

+235
-396
lines changed

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
LuaDeclId, LuaDocParamInfo, LuaDocReturnInfo, LuaMemberId, LuaOperator, LuaSemanticDeclId,
1212
LuaSignatureId, LuaType,
1313
},
14-
InFiled, InferFailReason, LuaOperatorMetaMethod, LuaTypeCache, OperatorFunction,
14+
InFiled, InferFailReason, LuaOperatorMetaMethod, LuaTypeCache, LuaTypeOwner, OperatorFunction,
1515
SignatureReturnStatus, TypeAssertion, TypeOps,
1616
};
1717

@@ -358,13 +358,12 @@ pub fn analyze_cast(analyzer: &mut DocAnalyzer, tag: LuaDocTagCast) -> Option<()
358358
for op in tag.get_op_types() {
359359
if let Some(doc_type) = op.get_type() {
360360
let typ = infer_type(analyzer, doc_type.clone());
361-
analyzer.context.cast_flow.insert(
362-
InFiled {
363-
file_id: analyzer.file_id,
364-
value: doc_type.get_syntax_id(),
365-
},
366-
typ,
367-
);
361+
let type_owner =
362+
LuaTypeOwner::SyntaxId(InFiled::new(analyzer.file_id, doc_type.get_syntax_id()));
363+
analyzer
364+
.db
365+
.get_type_index_mut()
366+
.bind_type(type_owner, LuaTypeCache::DocType(typ));
368367
}
369368
}
370369
Some(())
Lines changed: 11 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
use emmylua_parser::{BinaryOperator, LuaAstNode, LuaAstToken, LuaComment, LuaDocTag};
1+
use emmylua_parser::{LuaAstNode, LuaComment, LuaDocTag};
22

3-
use crate::{
4-
compilation::analyzer::flow::{
5-
binder::FlowBinder,
6-
flow_node::{FlowAssertion, FlowId, FlowNodeKind},
7-
},
8-
InFiled, LuaType, LuaVarRefId,
3+
use crate::compilation::analyzer::flow::{
4+
binder::FlowBinder,
5+
flow_node::{FlowId, FlowNodeKind},
96
};
107

118
enum CastAction {
@@ -14,84 +11,18 @@ enum CastAction {
1411
Force,
1512
}
1613

17-
pub fn bind_comment(
18-
binder: &mut FlowBinder,
19-
lua_comment: LuaComment,
20-
mut current: FlowId,
21-
) -> FlowId {
14+
pub fn bind_comment(binder: &mut FlowBinder, lua_comment: LuaComment, current: FlowId) -> FlowId {
2215
let cast_tags = lua_comment.get_doc_tags().filter_map(|it| match it {
2316
LuaDocTag::Cast(cast) => Some(cast),
2417
_ => None,
2518
});
2619

27-
for cast_tag in cast_tags {
28-
let Some(name_token) = cast_tag.get_name_token() else {
29-
continue;
30-
};
31-
let name = name_token.get_name_text();
32-
let decl = binder
33-
.db
34-
.get_decl_index()
35-
.get_decl_tree(&binder.file_id)
36-
.and_then(|decl_tree| decl_tree.find_local_decl(&name, name_token.get_position()));
37-
let var_ref_id = match decl {
38-
Some(decl) => LuaVarRefId::DeclId(decl.get_id()),
39-
None => LuaVarRefId::Name(name.into()),
40-
};
41-
42-
let mut assertions = vec![];
43-
for cast_op_type in cast_tag.get_op_types() {
44-
let action = match cast_op_type.get_op() {
45-
Some(op) => {
46-
if op.get_op() == BinaryOperator::OpAdd {
47-
CastAction::Add
48-
} else {
49-
CastAction::Remove
50-
}
51-
}
52-
None => CastAction::Force,
53-
};
54-
55-
if cast_op_type.is_nullable() {
56-
match action {
57-
CastAction::Add => {
58-
assertions.push(FlowAssertion::TypeAdd(var_ref_id.clone(), LuaType::Nil));
59-
}
60-
CastAction::Remove => {
61-
assertions
62-
.push(FlowAssertion::TypeRemove(var_ref_id.clone(), LuaType::Nil));
63-
}
64-
_ => {}
65-
}
66-
} else if let Some(doc_typ) = cast_op_type.get_type() {
67-
let file_id = binder.file_id;
68-
let key = InFiled::new(file_id, doc_typ.get_syntax_id());
69-
let typ = match binder.context.cast_flow.get(&key) {
70-
Some(t) => t.clone(),
71-
None => continue,
72-
};
73-
74-
match action {
75-
CastAction::Add => {
76-
assertions.push(FlowAssertion::TypeAdd(var_ref_id.clone(), typ.clone()));
77-
}
78-
CastAction::Remove => {
79-
assertions.push(FlowAssertion::TypeRemove(var_ref_id.clone(), typ.clone()));
80-
}
81-
CastAction::Force => {
82-
assertions.push(FlowAssertion::TypeForce(var_ref_id.clone(), typ.clone()));
83-
}
84-
}
85-
}
86-
}
87-
88-
for assertion in assertions {
89-
let flow_assertion = assertion.clone();
90-
let flow_id = binder.create_node(FlowNodeKind::Assertion(flow_assertion.into()));
91-
binder.add_antecedent(flow_id, current);
92-
current = flow_id;
93-
}
20+
let mut parent = current;
21+
for cast in cast_tags {
22+
let flow_id = binder.create_node(FlowNodeKind::TagCast(cast.to_ptr()));
23+
binder.add_antecedent(flow_id, parent);
24+
parent = flow_id;
9425
}
9526

96-
current
27+
parent
9728
}

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

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,24 @@ use crate::{
1010
compilation::analyzer::flow::{
1111
bind_analyze::bind_each_child,
1212
binder::FlowBinder,
13-
flow_node::{FlowAssertion, FlowId, FlowNodeKind},
13+
flow_node::{FlowId, FlowNodeKind},
1414
},
1515
LuaVarRefId,
1616
};
1717
pub use bind_binary_expr::bind_binary_expr;
1818
pub use bind_call_expr::bind_call_expr;
1919

20+
pub fn bind_condition_expr(
21+
binder: &mut FlowBinder,
22+
condition_expr: LuaExpr,
23+
current: FlowId,
24+
true_target: FlowId,
25+
false_target: FlowId,
26+
) {
27+
let flow_id = bind_expr(binder, condition_expr, current);
28+
// todo
29+
}
30+
2031
pub fn bind_expr(binder: &mut FlowBinder, expr: LuaExpr, current: FlowId) -> FlowId {
2132
match expr {
2233
LuaExpr::NameExpr(name_expr) => bind_name_expr(binder, name_expr, current),
@@ -32,27 +43,10 @@ pub fn bind_expr(binder: &mut FlowBinder, expr: LuaExpr, current: FlowId) -> Flo
3243
}
3344

3445
pub fn bind_name_expr(binder: &mut FlowBinder, name_expr: LuaNameExpr, current: FlowId) -> FlowId {
35-
let Some(name_text) = name_expr.get_name_text() else {
36-
return current;
37-
};
38-
let position = name_expr.get_position();
39-
40-
let Some(decl_tree) = binder.db.get_decl_index().get_decl_tree(&binder.file_id) else {
41-
return current;
42-
};
43-
let var_ref_id = match decl_tree.find_local_decl(&name_text, position) {
44-
Some(decl) => LuaVarRefId::DeclId(decl.get_id()),
45-
None => LuaVarRefId::Name(name_text.into()),
46-
};
47-
48-
let flow_id = binder.create_node(FlowNodeKind::Assertion(
49-
FlowAssertion::Truthy(var_ref_id).into(),
50-
));
51-
52-
binder.add_antecedent(flow_id, current);
53-
binder.bind_syntax_node(name_expr.get_syntax_id(), flow_id);
54-
55-
flow_id
46+
let name_node = binder.create_node(FlowNodeKind::Variable(name_expr.to_ptr()));
47+
binder.add_antecedent(name_node, current);
48+
binder.bind_syntax_node(name_expr.get_syntax_id(), name_node);
49+
name_node
5650
}
5751

5852
pub fn bind_table_expr(

0 commit comments

Comments
 (0)