Skip to content

Commit f63ac11

Browse files
committed
update
1 parent 05a66a9 commit f63ac11

6 files changed

Lines changed: 121 additions & 80 deletions

File tree

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

Lines changed: 15 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use emmylua_parser::{
2-
BinaryOperator, LuaAst, LuaAstNode, LuaAstToken, LuaBlock, LuaDocDescriptionOwner, LuaDocTagAs,
3-
LuaDocTagCast, LuaDocTagModule, LuaDocTagOther, LuaDocTagOverload, LuaDocTagParam,
4-
LuaDocTagReturn, LuaDocTagReturnCast, LuaDocTagSee, LuaDocTagType, LuaExpr, LuaLocalName,
5-
LuaTokenKind, LuaVarExpr,
2+
LuaAst, LuaAstNode, LuaAstToken, LuaBlock, LuaDocDescriptionOwner, LuaDocTagAs, LuaDocTagCast,
3+
LuaDocTagModule, LuaDocTagOther, LuaDocTagOverload, LuaDocTagParam, LuaDocTagReturn,
4+
LuaDocTagReturnCast, LuaDocTagSee, LuaDocTagType, LuaExpr, LuaLocalName, LuaTokenKind,
5+
LuaVarExpr,
66
};
77

88
use crate::{
@@ -186,73 +186,24 @@ pub fn analyze_return(analyzer: &mut DocAnalyzer, tag: LuaDocTagReturn) -> Optio
186186
Some(())
187187
}
188188

189-
enum CastAction {
190-
Add,
191-
Remove,
192-
Force,
193-
}
194-
195-
#[allow(unused)]
196189
pub fn analyze_return_cast(analyzer: &mut DocAnalyzer, tag: LuaDocTagReturnCast) -> Option<()> {
197190
if let Some(LuaSemanticDeclId::Signature(signature_id)) = get_owner_id(analyzer) {
198191
let name_token = tag.get_name_token()?;
199192
let name = name_token.get_name_text();
200193
let cast_op_type = tag.get_op_type()?;
201-
let action = match cast_op_type.get_op() {
202-
Some(op) => {
203-
if op.get_op() == BinaryOperator::OpAdd {
204-
CastAction::Add
205-
} else {
206-
CastAction::Remove
207-
}
208-
}
209-
None => CastAction::Force,
194+
if let Some(node_type) = cast_op_type.get_type() {
195+
let typ = infer_type(analyzer, node_type.clone());
196+
let infiled_syntax_id = InFiled::new(analyzer.file_id, node_type.get_syntax_id());
197+
let type_owner = LuaTypeOwner::SyntaxId(infiled_syntax_id);
198+
bind_type(analyzer.db, type_owner, LuaTypeCache::DocType(typ));
210199
};
211200

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

258209
Some(())

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
mod flow_node;
22
mod flow_tree;
3+
mod signature_cast;
34

45
use std::collections::HashMap;
56

6-
use crate::FileId;
7+
use crate::{db_index::flow::signature_cast::LuaSignatureCast, FileId, LuaSignatureId};
8+
use emmylua_parser::{LuaAstPtr, LuaDocOpType};
79
pub use flow_node::*;
810
pub use flow_tree::FlowTree;
911

@@ -12,12 +14,14 @@ use super::traits::LuaIndex;
1214
#[derive(Debug)]
1315
pub struct LuaFlowIndex {
1416
file_flow_tree: HashMap<FileId, FlowTree>,
17+
signature_cast_cache: HashMap<FileId, HashMap<LuaSignatureId, LuaSignatureCast>>,
1518
}
1619

1720
impl LuaFlowIndex {
1821
pub fn new() -> Self {
1922
Self {
2023
file_flow_tree: HashMap::new(),
24+
signature_cast_cache: HashMap::new(),
2125
}
2226
}
2327

@@ -28,14 +32,37 @@ impl LuaFlowIndex {
2832
pub fn get_flow_tree(&self, file_id: &FileId) -> Option<&FlowTree> {
2933
self.file_flow_tree.get(file_id)
3034
}
35+
36+
pub fn get_signature_cast(
37+
&self,
38+
file_id: &FileId,
39+
signature_id: &LuaSignatureId,
40+
) -> Option<&LuaSignatureCast> {
41+
self.signature_cast_cache.get(file_id)?.get(signature_id)
42+
}
43+
44+
pub fn add_signature_cast(
45+
&mut self,
46+
file_id: FileId,
47+
signature_id: LuaSignatureId,
48+
name: String,
49+
cast: LuaAstPtr<LuaDocOpType>,
50+
) {
51+
self.signature_cast_cache
52+
.entry(file_id)
53+
.or_insert_with(HashMap::new)
54+
.insert(signature_id, LuaSignatureCast { name, cast });
55+
}
3156
}
3257

3358
impl LuaIndex for LuaFlowIndex {
3459
fn remove(&mut self, file_id: FileId) {
3560
self.file_flow_tree.remove(&file_id);
61+
self.signature_cast_cache.remove(&file_id);
3662
}
3763

3864
fn clear(&mut self) {
3965
self.file_flow_tree.clear();
66+
self.signature_cast_cache.clear();
4067
}
4168
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use emmylua_parser::{LuaAstPtr, LuaDocOpType};
2+
3+
#[derive(Debug, Clone)]
4+
pub struct LuaSignatureCast {
5+
pub name: String,
6+
pub cast: LuaAstPtr<LuaDocOpType>,
7+
}

crates/emmylua_code_analysis/src/semantic/infer/narrow/condition_flow/call_flow.rs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
use emmylua_parser::{LuaCallExpr, LuaChunk};
22

33
use crate::{
4-
semantic::infer::{
5-
narrow::{condition_flow::InferConditionFlow, ResultTypeOrContinue},
6-
VarRefId,
4+
infer_call_expr_func,
5+
semantic::{
6+
infer::{
7+
infer_call::infer_call_expr,
8+
narrow::{condition_flow::InferConditionFlow, ResultTypeOrContinue},
9+
VarRefId,
10+
},
11+
semantic_info::{infer_expr_semantic_decl, SemanticDeclGuard},
712
},
8-
DbIndex, FlowNode, FlowTree, InferFailReason, LuaInferCache,
13+
DbIndex, FlowNode, FlowTree, InferFailReason, LuaInferCache, LuaSemanticDeclId,
14+
SemanticDeclLevel,
915
};
1016

1117
#[allow(unused)]
@@ -19,9 +25,35 @@ pub fn get_type_at_call_expr(
1925
call_expr: LuaCallExpr,
2026
condition_flow: InferConditionFlow,
2127
) -> Result<ResultTypeOrContinue, InferFailReason> {
22-
// let Some(call_prefix) = call_expr.get_prefix_expr() else {
23-
// return Ok(ResultTypeOrContinue::Continue);
24-
// };
28+
let Some(prefix_expr) = call_expr.get_prefix_expr() else {
29+
return Ok(ResultTypeOrContinue::Continue);
30+
};
31+
32+
let Some(semantic_decl_id) = infer_expr_semantic_decl(
33+
db,
34+
cache,
35+
prefix_expr,
36+
SemanticDeclGuard::default(),
37+
SemanticDeclLevel::NoTrace,
38+
) else {
39+
return Ok(ResultTypeOrContinue::Continue);
40+
};
41+
42+
let LuaSemanticDeclId::Signature(signature_id) = semantic_decl_id else {
43+
return Ok(ResultTypeOrContinue::Continue);
44+
};
45+
46+
let Some(signature_cast) = db
47+
.get_flow_index()
48+
.get_signature_cast(&cache.get_file_id(), &signature_id)
49+
else {
50+
return Ok(ResultTypeOrContinue::Continue);
51+
};
52+
53+
match signature_cast.name.as_str() {
54+
"self" => {}
55+
_ => {}
56+
}
2557

2658
Ok(ResultTypeOrContinue::Continue)
2759
}

crates/emmylua_code_analysis/src/semantic/infer/narrow/get_type_at_flow.rs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use emmylua_parser::{LuaAssignStat, LuaCallExpr, LuaChunk};
1+
use emmylua_parser::{LuaAssignStat, LuaAstNode, LuaCallExpr, LuaChunk, LuaVarExpr};
22

33
use crate::{
4-
infer_expr,
5-
semantic::infer::{
4+
infer_expr, semantic::infer::{
65
narrow::{
76
condition_flow::{get_type_at_condition_flow, InferConditionFlow},
87
get_multi_antecedents, get_single_antecedent,
@@ -13,9 +12,7 @@ use crate::{
1312
ResultTypeOrContinue,
1413
},
1514
InferResult, VarRefId,
16-
},
17-
CacheEntry, DbIndex, FlowId, FlowNode, FlowNodeKind, FlowTree, InferFailReason, LuaInferCache,
18-
LuaType, TypeOps,
15+
}, CacheEntry, DbIndex, FlowId, FlowNode, FlowNodeKind, FlowTree, InferFailReason, LuaDeclId, LuaInferCache, LuaMemberId, LuaType, TypeOps
1916
};
2017

2118
pub fn get_type_at_flow(
@@ -187,6 +184,33 @@ fn get_type_at_assign_stat(
187184
continue;
188185
}
189186

187+
// maybe use type force type
188+
let var_type = match var {
189+
LuaVarExpr::NameExpr(name_expr) => {
190+
let decl_id = LuaDeclId::new(cache.get_file_id(), name_expr.get_position());
191+
let type_cache = db.get_type_index().get_type_cache(&decl_id.into());
192+
if let Some(typ_cache) = type_cache {
193+
Some(typ_cache.as_type().clone())
194+
} else {
195+
None
196+
}
197+
}
198+
LuaVarExpr::IndexExpr(index_expr) => {
199+
let member_id = LuaMemberId::new(index_expr.get_syntax_id(), cache.get_file_id());
200+
let type_cache = db.get_type_index().get_type_cache(&member_id.into());
201+
if let Some(typ_cache) = type_cache {
202+
Some(typ_cache.as_type().clone())
203+
} else {
204+
None
205+
}
206+
}
207+
};
208+
209+
if let Some(var_type) = var_type {
210+
return Ok(ResultTypeOrContinue::Result(var_type));
211+
}
212+
213+
// infer from expr
190214
let expr_type = match exprs.get(i) {
191215
Some(expr) => {
192216
let expr_type = infer_expr(db, cache, expr.clone())?;

crates/emmylua_code_analysis/src/semantic/semantic_info/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ use emmylua_parser::{
1010
LuaAstNode, LuaAstToken, LuaDocNameType, LuaDocTag, LuaExpr, LuaLocalName, LuaSyntaxKind,
1111
LuaSyntaxNode, LuaSyntaxToken, LuaTableField,
1212
};
13-
use infer_expr_semantic_decl::infer_expr_semantic_decl;
13+
pub use infer_expr_semantic_decl::infer_expr_semantic_decl;
1414
pub use semantic_decl_level::SemanticDeclLevel;
15-
use semantic_guard::SemanticDeclGuard;
15+
pub use semantic_guard::SemanticDeclGuard;
1616

1717
use super::{infer_expr, LuaInferCache};
1818

0 commit comments

Comments
 (0)