Example #1
0
def create_store_return_from_function(f_name, lineno, col_offset):
    """
    Creates the return source code of any type inference function
    :param f_name:
    :param lineno:
    :param col_offset:
    :return:
    """
    set_type_of_comment = create_src_comment(
        "Storing the return type of function '{0}' in the type store".format(
            f_name), )
    set_type_of_method = core_language.create_attribute(
        default_module_type_store_var_name,
        "store_return_type_of_current_context")

    return_val_instr, val_var = create_get_type_of(
        default_function_ret_var_name, lineno, col_offset)
    set_type_of_call = functions.create_call_expression(
        set_type_of_method, [val_var])

    return_comment = create_src_comment(
        "Return type of the function '{0}'".format(f_name))

    return_ = ast.Return()
    return_.value = val_var

    context_unset = functions.create_context_unset()

    return flatten_lists(create_blank_line(), set_type_of_comment,
                         return_val_instr,
                         set_type_of_call, create_blank_line(), context_unset,
                         create_blank_line(), return_comment, return_)
Example #2
0
def create_set_type_of_member(owner_var, member_name, value, lineno,
                              col_offset):
    """
    Creates code to set the type of an object member
    :param owner_var:
    :param member_name:
    :param value:
    :param lineno:
    :param col_offset:
    :return:
    """
    comment = create_src_comment(
        "Setting the type of the member '{0}' of a type".format(member_name),
        lineno)
    localization = create_localization(lineno, col_offset)

    set_type_of_member_func = core_language.create_attribute(
        default_module_type_store_var_name, 'set_type_of_member')
    set_type_of_member_call = functions.create_call_expression(
        set_type_of_member_func, [
            localization, owner_var,
            core_language.create_str(member_name), value
        ])

    return flatten_lists(comment, set_type_of_member_call)
Example #3
0
def create_open_ssa_branch(branch_name):
    """
    Creates code to open a new SSA branch
    :param branch_name:
    :return:
    """
    attribute = core_language.create_attribute(
        default_module_type_store_var_name, "open_ssa_branch")
    clone_call = functions.create_call_expression(
        attribute, [core_language.create_str(branch_name)])

    return clone_call
Example #4
0
def create_unsupported_feature_call(localization, feature_name, feature_desc,
                                    lineno, col_offset):
    """
    Creates AST nodes to call to the unsupported_python_feature function
    """
    unsupported_feature_func = core_language.create_Name(
        'unsupported_python_feature', line=lineno, column=col_offset)
    unsupported_feature = core_language.create_str(feature_name)
    unsupported_description = core_language.create_str(feature_desc)
    return functions.create_call_expression(
        unsupported_feature_func,
        [localization, unsupported_feature, unsupported_description])
Example #5
0
def assign_as_return_type(value, lineno, col_offset):
    """
    Creates AST nodes to store in default_function_ret_var_name a possible return type
    """
    localization = create_localization(lineno, col_offset)
    default_function_ret_var = core_language.create_str(
        default_function_ret_var_name)
    set_type_of_member_func = core_language.create_attribute(
        default_module_type_store_var_name, 'set_type_of')

    return functions.create_call_expression(
        set_type_of_member_func,
        [localization, default_function_ret_var, value])
Example #6
0
def create_set_unreferenced_var_check(state):
    """
    Creates code to check for unreferenced variables
    :param state:
    :return:
    """
    if ENABLE_CODING_ADVICES:
        attribute = core_language.create_attribute(
            default_module_type_store_var_name, "set_check_unreferenced_vars")
        call_ = functions.create_call_expression(
            attribute, [core_language.create_Name(str(state))])

        return call_
    else:
        return []
Example #7
0
def update_localization(line, col):
    """
    Creates AST Nodes that updates the current localization
    """
    current_loc_stmts = create_localization(line, col)

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

    loc_call = functions.create_call_expression(set_current_call,
                                                [current_loc_stmts])

    return loc_call
Example #8
0
def create_add_stored_type(owner_var, index, value, lineno, col_offset):
    """
    Create code to add an element to a container
    :param owner_var:
    :param index:
    :param value:
    :param lineno:
    :param col_offset:
    :return:
    """
    comment = create_src_comment("Storing an element on a container", lineno)
    localization = create_localization(lineno, col_offset)

    add_type_func = core_language.create_Name('set_contained_elements_type')
    param_tuple = ast.Tuple()
    param_tuple.elts = [index, value]
    set_type_of_member_call = functions.create_call_expression(
        add_type_func, [localization, owner_var, param_tuple])

    return flatten_lists(comment, set_type_of_member_call)
Example #9
0
def create_add_alias(alias_name, var_name, lineno, col_offset):
    """
    Creates code to add an alias to the type store
    :param alias_name:
    :param var_name:
    :param lineno:
    :param col_offset:
    :return:
    """
    get_type_of_comment = create_src_comment("Adding an alias")
    get_type_of_method = core_language.create_attribute(
        default_module_type_store_var_name,
        "add_alias",
        line=lineno,
        column=col_offset)

    get_type_of_call = functions.create_call_expression(
        get_type_of_method, [alias_name, var_name])

    return flatten_lists(get_type_of_comment, get_type_of_call)
Example #10
0
def create_set_type_of(var_name, new_value, lineno, col_offset):
    """
    Creates code to set the type of a variable
    :param var_name:
    :param new_value:
    :param lineno:
    :param col_offset:
    :return:
    """
    set_type_of_comment = create_src_comment(
        "Assigning a type to the variable '{0}'".format(var_name), lineno)
    set_type_of_method = core_language.create_attribute(
        default_module_type_store_var_name, "set_type_of")

    localization = create_localization(lineno, col_offset)

    set_type_of_call = functions.create_call_expression(
        set_type_of_method, [
            localization,
            core_language.create_str(var_name, lineno, col_offset), new_value
        ])

    return flatten_lists(set_type_of_comment, set_type_of_call)