Ejemplo n.º 1
0
def infer_constructor_application(classtype, argtypes):
    """
    Resolve the free type variables of a constructor given the argument types
    we instantiate the class with.

    This means we need to match up argument types with variables from the
    class layout. In the most general case this means we need to fixpoint
    infer all methods called from the constructor.

    For now, we do the dumb thing: Match up argument names with variable names
    from the layout.
    """
    # Figure out the list of argtypes
    cls = classtype.impl
    init = cls.__init__.py_func
    argtypes = fill_missing_argtypes(init, tuple(argtypes))

    # Determine __init__ argnames
    argspec = inspect.getargspec(init)
    assert not argspec.varargs
    assert not argspec.keywords
    argnames = argspec.args
    assert len(argtypes) == len(argnames)

    return infer_type_from_layout(classtype, zip(argnames, argtypes))
Ejemplo n.º 2
0
def infer_constructor_application(classtype, argtypes):
    """
    Resolve the free type variables of a constructor given the argument types
    we instantiate the class with.

    This means we need to match up argument types with variables from the
    class layout. In the most general case this means we need to fixpoint
    infer all methods called from the constructor.

    For now, we do the dumb thing: Match up argument names with variable names
    from the layout.
    """
    # Figure out the list of argtypes
    cls = classtype.impl
    init = cls.__init__.py_func
    argtypes = fill_missing_argtypes(init, tuple(argtypes))

    # Determine __init__ argnames
    argspec = inspect.getargspec(init)
    assert not argspec.varargs
    assert not argspec.keywords
    argnames = argspec.args
    assert len(argtypes) == len(argnames)

    return infer_type_from_layout(classtype, zip(argnames, argtypes))
Ejemplo n.º 3
0
def simplify_argtypes(func, env):
    """
    Simplify the argtypes for non-opaque functions:

        *args    -> tuple
        **kwargs -> dict
    """
    from flypy.compiler.signature import fill_missing_argtypes, flatargs

    argtypes = env["flypy.typing.argtypes"]

    if func.opaque:
        return argtypes

    # We make the simlifying assumption that `py_func` is the right overload,
    # which has not been determined yet. This means all overloads must have
    # the same types for defaults...
    py_func = func.py_func

    called_flags = env['flypy.state.called_flags']
    called_with_varargs = called_flags['varargs']
    called_with_keywords = called_flags['keywords']

    if called_with_varargs:
        argtypes = handle_unpacking_varargs(func, py_func, argtypes)
    else:
        # Fill out missing argtypes for defaults
        argtypes = fill_missing_argtypes(py_func, argtypes)

    # Handle varargs/keywords (*args, **kwargs)
    argtypes = flatargs(py_func, argtypes, {})
    result = list(argtypes)

    varargs, keywords = [], []
    if argtypes.have_keywords:
        #keywords = [result.pop()]
        raise TypeError("Keyword arguments are not yet supported")
    if argtypes.have_varargs:
        varargs = [make_tuple_type(result.pop())]

    argtypes = result + varargs + keywords
    #print(func, argtypes)
    return argtypes
Ejemplo n.º 4
0
def simplify_argtypes(func, env):
    """
    Simplify the argtypes for non-opaque functions:

        *args    -> tuple
        **kwargs -> dict
    """
    from flypy.compiler.signature import fill_missing_argtypes, flatargs

    argtypes = env["flypy.typing.argtypes"]

    if func.opaque:
        return argtypes

    # We make the simlifying assumption that `py_func` is the right overload,
    # which has not been determined yet. This means all overloads must have
    # the same types for defaults...
    py_func = func.py_func

    called_flags = env['flypy.state.called_flags']
    called_with_varargs = called_flags['varargs']
    called_with_keywords = called_flags['keywords']

    if called_with_varargs:
        argtypes = handle_unpacking_varargs(func, py_func, argtypes)
    else:
        # Fill out missing argtypes for defaults
        argtypes = fill_missing_argtypes(py_func, argtypes)

    # Handle varargs/keywords (*args, **kwargs)
    argtypes = flatargs(py_func, argtypes, {})
    result = list(argtypes)

    varargs, keywords = [], []
    if argtypes.have_keywords:
        #keywords = [result.pop()]
        raise TypeError("Keyword arguments are not yet supported")
    if argtypes.have_varargs:
        varargs = [make_tuple_type(result.pop())]

    argtypes = result + varargs + keywords
    #print(func, argtypes)
    return argtypes