|
| 1 | +use emmylua_parser::{ |
| 2 | + BinaryOperator, LuaAstNode, LuaBinaryExpr, LuaCallExpr, LuaChunk, LuaExpr, LuaLiteralToken, |
| 3 | + LuaNameExpr, |
| 4 | +}; |
| 5 | + |
| 6 | +use crate::{ |
| 7 | + semantic::infer::infer_name::narrow::{ |
| 8 | + condition_flow::InferConditionFlow, get_single_antecedent, |
| 9 | + get_type_at_flow::get_type_at_flow, ResultTypeOrContinue, |
| 10 | + }, |
| 11 | + DbIndex, FlowNode, FlowTree, InferFailReason, LuaDeclId, LuaInferCache, LuaType, TypeOps, |
| 12 | +}; |
| 13 | + |
| 14 | +pub fn get_type_at_binary_expr( |
| 15 | + db: &DbIndex, |
| 16 | + tree: &FlowTree, |
| 17 | + cache: &mut LuaInferCache, |
| 18 | + root: &LuaChunk, |
| 19 | + decl_id: LuaDeclId, |
| 20 | + flow_node: &FlowNode, |
| 21 | + binary_expr: LuaBinaryExpr, |
| 22 | + condition_flow: InferConditionFlow, |
| 23 | +) -> Result<ResultTypeOrContinue, InferFailReason> { |
| 24 | + let Some(op_token) = binary_expr.get_op_token() else { |
| 25 | + return Ok(ResultTypeOrContinue::Continue); |
| 26 | + }; |
| 27 | + |
| 28 | + let Some((left_expr, right_expr)) = binary_expr.get_exprs() else { |
| 29 | + return Ok(ResultTypeOrContinue::Continue); |
| 30 | + }; |
| 31 | + |
| 32 | + match op_token.get_op() { |
| 33 | + BinaryOperator::OpLt |
| 34 | + | BinaryOperator::OpLe |
| 35 | + | BinaryOperator::OpGt |
| 36 | + | BinaryOperator::OpGe => { |
| 37 | + // todo check number range |
| 38 | + } |
| 39 | + BinaryOperator::OpEq => { |
| 40 | + let result_type = maybe_type_guard_bianry( |
| 41 | + db, |
| 42 | + tree, |
| 43 | + cache, |
| 44 | + root, |
| 45 | + decl_id, |
| 46 | + flow_node, |
| 47 | + left_expr, |
| 48 | + right_expr, |
| 49 | + condition_flow, |
| 50 | + )?; |
| 51 | + if let ResultTypeOrContinue::Result(result_type) = result_type { |
| 52 | + return Ok(ResultTypeOrContinue::Result(result_type)); |
| 53 | + } |
| 54 | + } |
| 55 | + BinaryOperator::OpNe => { |
| 56 | + let result_type = maybe_type_guard_bianry( |
| 57 | + db, |
| 58 | + tree, |
| 59 | + cache, |
| 60 | + root, |
| 61 | + decl_id, |
| 62 | + flow_node, |
| 63 | + left_expr, |
| 64 | + right_expr, |
| 65 | + condition_flow.get_negated(), |
| 66 | + )?; |
| 67 | + if let ResultTypeOrContinue::Result(result_type) = result_type { |
| 68 | + return Ok(ResultTypeOrContinue::Result(result_type)); |
| 69 | + } |
| 70 | + } |
| 71 | + _ => {} |
| 72 | + } |
| 73 | + |
| 74 | + Ok(ResultTypeOrContinue::Continue) |
| 75 | +} |
| 76 | + |
| 77 | +fn maybe_type_guard_bianry( |
| 78 | + db: &DbIndex, |
| 79 | + tree: &FlowTree, |
| 80 | + cache: &mut LuaInferCache, |
| 81 | + root: &LuaChunk, |
| 82 | + decl_id: LuaDeclId, |
| 83 | + flow_node: &FlowNode, |
| 84 | + left_expr: LuaExpr, |
| 85 | + right_expr: LuaExpr, |
| 86 | + condition_flow: InferConditionFlow, |
| 87 | +) -> Result<ResultTypeOrContinue, InferFailReason> { |
| 88 | + let mut type_guard_expr: Option<LuaCallExpr> = None; |
| 89 | + let mut literal_string = String::new(); |
| 90 | + if let LuaExpr::CallExpr(call_expr) = left_expr { |
| 91 | + if call_expr.is_type() { |
| 92 | + type_guard_expr = Some(call_expr); |
| 93 | + if let LuaExpr::LiteralExpr(literal_expr) = right_expr { |
| 94 | + match literal_expr.get_literal() { |
| 95 | + Some(LuaLiteralToken::String(s)) => { |
| 96 | + literal_string = s.get_value(); |
| 97 | + } |
| 98 | + _ => return Ok(ResultTypeOrContinue::Continue), |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } else if let LuaExpr::CallExpr(call_expr) = right_expr { |
| 103 | + if call_expr.is_type() { |
| 104 | + type_guard_expr = Some(call_expr); |
| 105 | + if let LuaExpr::LiteralExpr(literal_expr) = left_expr { |
| 106 | + match literal_expr.get_literal() { |
| 107 | + Some(LuaLiteralToken::String(s)) => { |
| 108 | + literal_string = s.get_value(); |
| 109 | + } |
| 110 | + _ => return Ok(ResultTypeOrContinue::Continue), |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if type_guard_expr.is_none() || literal_string.is_empty() { |
| 117 | + return Ok(ResultTypeOrContinue::Continue); |
| 118 | + } |
| 119 | + |
| 120 | + let Some(arg_list) = type_guard_expr.unwrap().get_args_list() else { |
| 121 | + return Ok(ResultTypeOrContinue::Continue); |
| 122 | + }; |
| 123 | + |
| 124 | + let Some(arg) = arg_list.get_args().next() else { |
| 125 | + return Ok(ResultTypeOrContinue::Continue); |
| 126 | + }; |
| 127 | + |
| 128 | + let LuaExpr::NameExpr(name_expr) = arg else { |
| 129 | + return Ok(ResultTypeOrContinue::Continue); |
| 130 | + }; |
| 131 | + |
| 132 | + let maybe_ref_decl_id = db |
| 133 | + .get_reference_index() |
| 134 | + .get_var_reference_decl(&cache.get_file_id(), name_expr.get_range()); |
| 135 | + |
| 136 | + let Some(ref_decl_id) = maybe_ref_decl_id else { |
| 137 | + // If we found a reference declaration ID, we can use it |
| 138 | + return Ok(ResultTypeOrContinue::Continue); |
| 139 | + }; |
| 140 | + |
| 141 | + if ref_decl_id != decl_id { |
| 142 | + return Ok(ResultTypeOrContinue::Continue); |
| 143 | + } |
| 144 | + |
| 145 | + let anatecedent_flow_id = get_single_antecedent(tree, flow_node)?; |
| 146 | + let antecedent_type = |
| 147 | + get_type_at_flow(db, tree, cache, root, ref_decl_id, anatecedent_flow_id)?; |
| 148 | + |
| 149 | + let narrow = match literal_string.as_str() { |
| 150 | + "number" => LuaType::Number, |
| 151 | + "string" => LuaType::String, |
| 152 | + "boolean" => LuaType::Boolean, |
| 153 | + "table" => LuaType::Table, |
| 154 | + "function" => LuaType::Function, |
| 155 | + "thread" => LuaType::Thread, |
| 156 | + "userdata" => LuaType::Userdata, |
| 157 | + "nil" => LuaType::Nil, |
| 158 | + _ => { |
| 159 | + // If the type is not recognized, we cannot narrow it |
| 160 | + return Ok(ResultTypeOrContinue::Continue); |
| 161 | + } |
| 162 | + }; |
| 163 | + |
| 164 | + let result_type = match condition_flow { |
| 165 | + InferConditionFlow::TrueCondition => TypeOps::Narrow.apply(db, &antecedent_type, &narrow), |
| 166 | + InferConditionFlow::FalseCondition => TypeOps::Remove.apply(db, &antecedent_type, &narrow), |
| 167 | + }; |
| 168 | + |
| 169 | + Ok(ResultTypeOrContinue::Result(result_type)) |
| 170 | +} |
| 171 | + |
| 172 | +fn maybe_var_eq( |
| 173 | + db: &DbIndex, |
| 174 | + tree: &FlowTree, |
| 175 | + cache: &mut LuaInferCache, |
| 176 | + root: &LuaChunk, |
| 177 | + decl_id: LuaDeclId, |
| 178 | + flow_node: &FlowNode, |
| 179 | + left_expr: LuaExpr, |
| 180 | + right_expr: LuaExpr, |
| 181 | +) -> Result<ResultTypeOrContinue, InferFailReason> { |
| 182 | + Ok(ResultTypeOrContinue::Continue) |
| 183 | +} |
0 commit comments