示例#1
0
def create_get_type_of(var_name, lineno, col_offset, test_unreferenced=True):
    """
    Creates code to get the type of a variable
    :param var_name:
    :param lineno:
    :param col_offset:
    :param test_unreferenced:
    :return:
    """
    get_type_of_comment = create_src_comment(
        "Getting the type of '{0}'".format(var_name), lineno)
    get_type_of_method = core_language.create_attribute(
        default_module_type_store_var_name,
        "get_type_of",
        line=lineno,
        column=col_offset)
    localization = create_localization(lineno, col_offset)
    if test_unreferenced:
        get_type_of_call = functions.create_call(
            get_type_of_method,
            [localization, core_language.create_str(var_name)])
    else:
        get_type_of_call = functions.create_call(get_type_of_method, [
            localization,
            core_language.create_str(var_name),
            core_language.create_Name('False')
        ])

    assign_stmts, temp_assign = create_temp_Assign(
        get_type_of_call, lineno, col_offset, descriptive_var_name=var_name)

    return flatten_lists(get_type_of_comment, assign_stmts), temp_assign
示例#2
0
def __remove_not_subtype_from_union_implementation(if_test, type_, lineno,
                                                   col_offset):
    """
    Implements the call to remove_not_subtype_from_union when the idiom is recognized
    :param if_test:
    :param type_:
    :param lineno:
    :param col_offset:
    :return:
    """
    param = __get_idiom_type_param(if_test)
    if type(param) is ast.Name:
        # obj_type, obj_var = stypy_functions.create_get_type_of(param.id, lineno, col_offset)
        remove_type_call = functions.create_call(
            core_language.create_Name("remove_not_subtype_from_union"),
            [type_, if_test.args[1]],
            line=lineno,
            column=col_offset)
        set_type = stypy_functions.create_set_type_of(param.id,
                                                      remove_type_call, lineno,
                                                      col_offset)

        return stypy_functions.flatten_lists(set_type)  # obj_type, set_type)
    if type(param) is ast.Attribute:
        # try:
        # Get the owner of the attribute
        if type(param.value) is ast.Name:
            obj_type_stmts, obj_var = stypy_functions.create_get_type_of(
                param.value.id, lineno, col_offset)
        else:
            obj_type_stmts, obj_var = stypy_functions.create_get_type_of_member(
                param.value, param.value.attr, lineno, col_offset)
        # except Exception as ex:
        #     print ex

        # Get the current type of the owner of the attribute
        att_type_stmts, att_var = stypy_functions.create_get_type_of_member(
            obj_var, param.attr, lineno, col_offset)
        remove_type_call = functions.create_call(
            core_language.create_Name("remove_not_subtype_from_union"),
            [type_, if_test.args[1]],
            line=lineno,
            column=col_offset)
        set_member = stypy_functions.create_set_type_of_member(
            obj_var, param.attr, remove_type_call, lineno, col_offset)
        return stypy_functions.flatten_lists(obj_type_stmts, att_type_stmts,
                                             set_member)

    return []
示例#3
0
def create_join_ssa_context():
    """
    Creates code to close a SSA context
    :return:
    """
    attribute = core_language.create_attribute(
        default_module_type_store_var_name, "join_ssa_context")
    clone_call = functions.create_call(attribute, [])

    return create_temp_type_store_Assign(clone_call)
示例#4
0
def create_print_errors():
    """
    Creates AST Nodes that encode "ErrorType.print_error_msgs()"
    """
    attribute = core_language.create_attribute("StypyTypeError",
                                               "print_error_msgs")
    expr = ast.Expr()
    expr.value = functions.create_call(attribute, [])

    return expr
示例#5
0
def create_get_type_of_member(owner_var,
                              member_name,
                              lineno,
                              col_offset,
                              test_unreferenced=True):
    """
    Creates code to get the type of an object member
    :param owner_var:
    :param member_name:
    :param lineno:
    :param col_offset:
    :param test_unreferenced:
    :return:
    """
    comment = create_src_comment(
        "Obtaining the member '{0}' of a type".format(member_name), lineno)
    localization = create_localization(lineno, col_offset)

    get_type_of_member_func = core_language.create_attribute(
        default_module_type_store_var_name, 'get_type_of_member')
    if not test_unreferenced:
        get_type_of_member_call = functions.create_call(
            get_type_of_member_func, [
                localization, owner_var,
                core_language.create_str(member_name),
                core_language.create_Name('False')
            ])
    else:
        get_type_of_member_call = functions.create_call(
            get_type_of_member_func,
            [localization, owner_var,
             core_language.create_str(member_name)])

    member_stmts, member_var = create_temp_Assign(get_type_of_member_call,
                                                  lineno, col_offset,
                                                  member_name)

    return flatten_lists(comment, member_stmts), member_var
示例#6
0
def create_type_store(type_store_name=default_module_type_store_var_name):
    """
    Creates code to create a new type store
    :param type_store_name:
    :return:
    """
    call_arg = core_language.create_Name("None")
    call_arg2 = core_language.create_Name("__file__")
    call_func = core_language.create_Name("Context")
    call = functions.create_call(call_func, [call_arg, call_arg2])
    assign_target = core_language.create_Name(type_store_name, False)
    assign = core_language.create_Assign(assign_target, call)

    return assign
示例#7
0
def create_get_type_instance_of(type_name, lineno, col_offset):
    """
    Create code to get an instance of the passed type
    :param type_name:
    :param lineno:
    :param col_offset:
    :return:
    """
    get_func = core_language.create_Name("get_builtin_python_type_instance")
    param1 = core_language.create_str(type_name)
    localization = create_localization(lineno, col_offset)
    get_list_call = functions.create_call(get_func, [localization, param1])
    return create_temp_Assign(get_list_call, lineno, col_offset,
                              type_name.replace(".", "_"))
示例#8
0
def create_open_ssa_context(context_name):
    """
    Creates code to open a new SSA context
    :param context_name:
    :return:
    """
    ssa_context_class = core_language.create_Name("SSAContext")
    attribute = core_language.create_attribute(ssa_context_class,
                                               "create_ssa_context")
    clone_call = functions.create_call(
        attribute,
        core_language.create_Name(default_module_type_store_var_name),
        [core_language.create_str(context_name)])

    return create_temp_type_store_Assign(clone_call)
示例#9
0
def create_localization(line, col):
    """
    Creates AST Nodes that creates a new Localization instance
    """
    linen = core_language.create_num(line)
    coln = core_language.create_num(col)
    file_namen = core_language.create_Name('__file__')

    module1 = core_language.create_attribute('stypy', 'reporting')
    module2 = core_language.create_attribute(module1, 'localization')
    loc_namen = core_language.create_attribute(module2, 'Localization')

    loc_call = functions.create_call(loc_namen, [file_namen, linen, coln])

    return loc_call
示例#10
0
def create_unary_operator(op_name, op, line=0, column=0):
    """
    Creates a binary operator
    :param op_name:
    :param op:
    :param line:
    :param column:
    :return:
    """
    localization = stypy_functions.create_localization(line, column)

    unop_func = core_language.create_Name(stypy_functions.default_operator_call_name)
    unop = functions.create_call(unop_func, [localization, op_name, op])

    return unop
示例#11
0
def __set_type_implementation(if_test, type_, lineno, col_offset):
    """
    Assigns the type of the condition when the idiom is recognized
    :param if_test:
    :param type_:
    :param lineno:
    :param col_offset:
    :return:
    """
    param = __get_idiom_type_param(if_test)
    type_ = functions.create_call(type_, [], line=lineno, column=col_offset)
    if type(param) is ast.Name:
        return stypy_functions.create_set_type_of(param.id, type_, lineno,
                                                  col_offset)
    if type(param) is ast.Attribute:
        obj_type, obj_var = stypy_functions.create_get_type_of(
            param.value.id, lineno, col_offset)
        set_member = stypy_functions.create_set_type_of_member(
            obj_var, param.attr, type_, lineno, col_offset)
        return stypy_functions.flatten_lists(obj_type, set_member)

    return []