<commit_before>﻿from .irvisitor import IRVisitor
from .ir import *
from .scope import Scope
from .type import Type
from .builtin import builtin_return_type_table
from .symbol import function_name
from .common import error_info
from .env import env
import logging
logger = logging.getLogger(__name__)

def type_error(ir, msg):
    print(error_info(ir.lineno))
    raise TypeError(msg)

class TypePropagation(IRVisitor):
    def __init__(self):
        super().__init__()
        self.check_error = True

    def propagate_global_function_type(self):
        self.check_error = False
        scopes = Scope.get_scopes(bottom_up=True, contain_global=True, contain_class=True)
        for s in scopes:
            if s.is_class():
                s.return_type = Type.object(None, s)
            else:
                s.return_type = Type.none_t

        while True:
            for s in scopes:
                self.process(s)
            untyped = [s for s in scopes if s.is_returnable() and s.return_type is Type.none_t]
            if untyped:
                continue
            break

    def visit_UNOP(self, ir):
        return self.visit(ir.exp)

    def visit_BINOP(self, ir):
        ltype = self.visit(ir.left)
        self.visit(ir.right)
        return ltype

    def visit_RELOP(self, ir):
        self.visit(ir.left)
        self.visit(ir.right)
        return Type.bool_t

    def visit_CALL(self, ir):
        self.visit(ir.func)

        if ir.func.is_a(TEMP):
            func_sym = ir.func.sym<commit_msg>Add bug check for the type inference<commit_after>﻿from .irvisitor import IRVisitor
from .ir import *
from .scope import Scope
from .type import Type
from .builtin import builtin_return_type_table
from .symbol import function_name
from .common import error_info
from .env import env
import logging
logger = logging.getLogger(__name__)

def type_error(ir, msg):
    print(error_info(ir.lineno))
    raise TypeError(msg)

class TypePropagation(IRVisitor):
    def __init__(self):
        super().__init__()
        self.check_error = True

    def propagate_global_function_type(self):
        self.check_error = False
        scopes = Scope.get_scopes(bottom_up=True, contain_global=True, contain_class=True)
        for s in scopes:
            if s.is_class():
                s.return_type = Type.object(None, s)
            else:
                s.return_type = Type.none_t

        prev_untyped = []
        while True:
            for s in scopes:
                self.process(s)
            untyped = [s for s in scopes if s.is_returnable() and s.return_type is Type.none_t]
            if untyped:
                if len(prev_untyped) == len(untyped):
                    str_untypes = ', '.join([s.name[len('@top.'):] for s in untyped])
                    raise TypeError('BUG: can not complete the type inference process for ' + str_untypes)
                prev_untyped = untyped[:]
                continue
            break

    def visit_UNOP(self, ir):
        return self.visit(ir.exp)

    def visit_BINOP(self, ir):
        ltype = self.visit(ir.left)
        self.visit(ir.right)
        return ltype

    def visit_RELOP(self, ir):
        self.visit(ir.left)
        self.visit(ir.right)
        return Type.bool_t

    def visit_CALL(self, ir):
        self.visit(ir.func)

        if ir.func.is_a(TEMP):
            func_sym = ir.func.sym
{'fraction_non_alphanumeric': 0.0907210401891253, 'fraction_numerical': 0.0, 'mean_word_length': 2.1488372093023256, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '222347', 'n_tokens_mistral': 1071, 'n_tokens_neox': 1044, 'n_words': 266}