Exemplo n.º 1
0
    def visit_function_call(self, node):
        """
        Check consistency for a single function call: check if the called function has been declared, whether the number and types of arguments correspond to the declaration, etc.

        :param node: a single function call.
        :type node: ASTFunctionCall
        """
        func_name = node.get_name()
        if func_name == 'convolve' or func_name == 'cond_sum' or func_name == 'curr_sum':
            return

        symbol = node.get_scope().resolve_to_symbol(node.get_name(),
                                                    SymbolKind.FUNCTION)

        # first check if the function has been declared
        if symbol is None:
            code, message = Messages.get_function_not_declared(node.get_name())
            Logger.log_message(error_position=node.get_source_position(),
                               log_level=LoggingLevel.ERROR,
                               code=code,
                               message=message)
            return

        # check if the number of arguments is the same as in the symbol
        if len(node.get_args()) != len(symbol.get_parameter_types()):
            code, message = Messages.get_wrong_number_of_args(
                str(node), len(symbol.get_parameter_types()),
                len(node.get_args()))
            Logger.log_message(code=code,
                               message=message,
                               log_level=LoggingLevel.ERROR,
                               error_position=node.get_source_position())
            return

        # finally check if the call is correctly typed
        expected_types = symbol.get_parameter_types()
        actual_args = node.get_args()
        actual_types = [arg.type for arg in actual_args]
        for actual_arg, actual_type, expected_type in zip(
                actual_args, actual_types, expected_types):
            if isinstance(actual_type, ErrorTypeSymbol):
                code, message = Messages.get_type_could_not_be_derived(
                    actual_arg)
                Logger.log_message(
                    code=code,
                    message=message,
                    log_level=LoggingLevel.ERROR,
                    error_position=actual_arg.get_source_position())
                return

            if not actual_type.equals(expected_type) and not isinstance(
                    expected_type, TemplateTypeSymbol):
                TypeCaster.try_to_recover_or_error(expected_type, actual_type,
                                                   actual_arg)
 def visit_function_call(self, node):
     """
     Checks the coco.
     :param node: a single function call.
     :type node: ast_function_call
     """
     func_name = node.get_name()
     if func_name == 'convolve' or func_name == 'cond_sum' or func_name == 'curr_sum':
         return
     # now, for all expressions, check for all function calls, the corresponding function is declared.
     symbol = node.get_scope().resolve_to_symbol(node.get_name(),
                                                 SymbolKind.FUNCTION)
     # first check if the function has been declared
     if symbol is None:
         code, message = Messages.get_function_not_declared(node.get_name())
         Logger.log_message(error_position=node.get_source_position(),
                            log_level=LoggingLevel.ERROR,
                            code=code,
                            message=message)
     # now check if the number of arguments is the same as in the symbol
     if symbol is not None and len(node.get_args()) != len(
             symbol.get_parameter_types()):
         code, message = Messages.get_wrong_number_of_args(
             str(node), len(symbol.get_parameter_types()),
             len(node.get_args()))
         Logger.log_message(code=code,
                            message=message,
                            log_level=LoggingLevel.ERROR,
                            error_position=node.get_source_position())
     # finally check if the call is correctly typed
     elif symbol is not None:
         expected_types = symbol.get_parameter_types()
         actual_types = node.get_args()
         for i in range(0, len(actual_types)):
             expected_type = expected_types[i]
             actual_type = actual_types[i].type
             if isinstance(actual_type, ErrorTypeSymbol):
                 code, message = Messages.get_type_could_not_be_derived(
                     actual_types[i])
                 Logger.log_message(
                     code=code,
                     message=message,
                     log_level=LoggingLevel.ERROR,
                     error_position=actual_types[i].get_source_position())
                 return
             if not actual_type.equals(expected_type):
                 TypeCaster.try_to_recover_or_error(expected_type,
                                                    actual_type,
                                                    actual_types[i])
 def visit_function_call(self, node):
     """
     Checks the coco.
     :param node: a single function call.
     :type node: ast_function_call
     """
     func_name = node.get_name()
     if func_name == 'convolve' or func_name == 'cond_sum' or func_name == 'curr_sum':
         return
     # now, for all expressions, check for all function calls, the corresponding function is declared.
     symbol = node.get_scope().resolve_to_symbol(node.get_name(), SymbolKind.FUNCTION)
     # first check if the function has been declared
     if symbol is None:
         code, message = Messages.get_function_not_declared(node.get_name())
         Logger.log_message(error_position=node.get_source_position(), log_level=LoggingLevel.ERROR,
                            code=code, message=message)
     # now check if the number of arguments is the same as in the symbol
     if symbol is not None and len(node.get_args()) != len(symbol.get_parameter_types()):
         code, message = Messages.get_wrong_number_of_args(str(node), len(symbol.get_parameter_types()),
                                                           len(node.get_args()))
         Logger.log_message(code=code, message=message, log_level=LoggingLevel.ERROR,
                            error_position=node.get_source_position())
     # finally check if the call is correctly typed
     elif symbol is not None:
         expected_types = symbol.get_parameter_types()
         actual_types = node.get_args()
         for i in range(0, len(actual_types)):
             expected_type = expected_types[i]
             actual_type = actual_types[i].type
             if isinstance(actual_type, ErrorTypeSymbol):
                 code, message = Messages.get_type_could_not_be_derived(actual_types[i])
                 Logger.log_message(code=code, message=message, log_level=LoggingLevel.ERROR,
                                    error_position=actual_types[i].get_source_position())
                 return
             if not actual_type.equals(expected_type):
                 TypeCaster.try_to_recover_or_error(expected_type, actual_type, actual_types[i])