From 5167986ddfd8abee52d8e9ca6007274c7a9622e2 Mon Sep 17 00:00:00 2001 From: ProgramSnail Date: Tue, 9 May 2023 15:01:11 +0300 Subject: [PATCH] operator typecheck refactoring --- src/type_check_visitor.cpp | 87 +++++++++++++++----------------------- 1 file changed, 35 insertions(+), 52 deletions(-) diff --git a/src/type_check_visitor.cpp b/src/type_check_visitor.cpp index 0b0bab8..7f7bbd2 100644 --- a/src/type_check_visitor.cpp +++ b/src/type_check_visitor.cpp @@ -574,42 +574,12 @@ void TypeCheckVisitor::Visit(BinaryOperatorExpression* node) { auto maybe_operator_id = namespace_visitor_.FindFunction(std::nullopt, node->operator_name); const info::definition::Function* operator_info = nullptr; + bool is_method = false; + if (maybe_operator_id.has_value()) { operator_info = &namespace_visitor_.GetGlobalInfo()->GetFunctionInfo(maybe_operator_id.value()); - - if (!operator_info->declaration.has_value()) { - error_handling::HandleTypecheckError("Operator declaration not found", node->base); - } - - if (!operator_info->definition.has_value()) { // TODO: builtin, etc. - error_handling::HandleTypecheckError("Operator definition not found", node->base); - } - - if (operator_info->argument_count != 3) { // 2 + return type - error_handling::HandleTypecheckError("Operator wrong argument count", node->base); - } - - if (operator_info->declaration->parameters.size() > 0) { - error_handling::HandleTypecheckError("Operator with parameters", node->base); - } - - Visitor::Visit(*operator_info->declaration.value().argument_types[0]); - utils::IdType left_expression_type = current_type_; // TODO: type in context of deduced types - - Visitor::Visit(node->left_expression); - if (!context_manager_.AddTypeRequirement(current_type_, left_expression_type)) { - error_handling::HandleTypecheckError("Operator left expression has wrong type", node->base); - } - - Visitor::Visit(*operator_info->declaration.value().argument_types[1]); - utils::IdType right_expression_type = current_type_; // TODO: type in context of deduced types - - Visitor::Visit(node->right_expression); - if (!context_manager_.AddTypeRequirement(current_type_, right_expression_type)) { - error_handling::HandleTypecheckError("Operator right expression has wrong type", node->base); - } + is_method = false; } else { - Visitor::Visit(node->left_expression); auto maybe_left_type = context_manager_.GetType(current_type_); @@ -648,30 +618,43 @@ void TypeCheckVisitor::Visit(BinaryOperatorExpression* node) { } operator_info = &namespace_visitor_.GetGlobalInfo()->GetFunctionInfo(maybe_operator_id.value()); + is_method = true; + } - if (!operator_info->declaration.has_value()) { - error_handling::HandleTypecheckError("Operator declaration not found", node->base); + if (!operator_info->declaration.has_value()) { + error_handling::HandleTypecheckError("Operator declaration not found", node->base); + } + + if (!operator_info->definition.has_value()) { // TODO: builtin, etc. + error_handling::HandleTypecheckError("Operator definition not found", node->base); + } + + if (operator_info->argument_count != (is_method ? 2 : 3)) { // 2 + return type + error_handling::HandleTypecheckError("Operator wrong argument count", node->base); + } + + if (operator_info->declaration->parameters.size() > 0) { + error_handling::HandleTypecheckError("Operator with parameters", node->base); + } + + if (!is_method) { + + Visitor::Visit(*operator_info->declaration.value().argument_types[0]); + utils::IdType left_expression_type = current_type_; // TODO: type in context of deduced types + + Visitor::Visit(node->left_expression); + if (!context_manager_.AddTypeRequirement(current_type_, left_expression_type)) { + error_handling::HandleTypecheckError("Operator left expression has wrong type", node->base); } - if (!operator_info->definition.has_value()) { // TODO: builtin, etc. - error_handling::HandleTypecheckError("Operator definition not found", node->base); - } + } - if (operator_info->argument_count != 2) { // argument + return type - error_handling::HandleTypecheckError("Operator wrong argument count", node->base); - } + Visitor::Visit(*operator_info->declaration.value().argument_types[is_method ? 0 : 1]); + utils::IdType right_expression_type = current_type_; // TODO: type in context of deduced types - if (operator_info->declaration->parameters.size() > 0) { - error_handling::HandleTypecheckError("Operator with parameters", node->base); - } - - Visitor::Visit(*operator_info->declaration.value().argument_types[9]); - utils::IdType right_expression_type = current_type_; // TODO: type in context of deduced types - - Visitor::Visit(node->right_expression); - if (!context_manager_.AddTypeRequirement(current_type_, right_expression_type)) { - error_handling::HandleTypecheckError("Operator right expression has wrong type", node->base); - } + Visitor::Visit(node->right_expression); + if (!context_manager_.AddTypeRequirement(current_type_, right_expression_type)) { + error_handling::HandleTypecheckError("Operator right expression has wrong type", node->base); } node->function_id_ = maybe_operator_id.value(); // IMPORTANT