Example #1
0
def infer_foreign_call(func, func_type, argtypes, flags):
    """
    Higher-order or foreign function call.
    """
    if isinstance(func_type, type(ForeignFunction.type)):
        restype = func_type.parameters[-1]
    else:
        restype = func_type.restype
    assert restype

    expected_argtypes = func_type.parameters[:-1]

    if len(argtypes) != len(expected_argtypes):
        if not (func_type.varargs and len(argtypes) >= len(expected_argtypes)):
            raise TypeError("Function %s requires %d argument(s), got %d" %
                            (func, len(expected_argtypes), len(argtypes)))

    try:
        # Make sure we have compatible types
        unify(zip(argtypes, expected_argtypes))
    except UnificationError:
        raise TypeError(
            "Mismatching signature for function %s with argument types %s" %
            (func, ", ".join(map(str, argtypes))))

    return func, Function[expected_argtypes + (restype, )], restype
Example #2
0
def infer_foreign_call(func, func_type, argtypes, flags):
    """
    Higher-order or foreign function call.
    """
    if isinstance(func_type, type(ForeignFunction.type)):
        restype = func_type.parameters[-1]
    else:
        restype = func_type.restype
    assert restype

    expected_argtypes = func_type.parameters[:-1]

    if len(argtypes) != len(expected_argtypes):
        if not (func_type.varargs and len(argtypes) >= len(expected_argtypes)):
            raise TypeError("Function %s requires %d argument(s), got %d" % (
                                func, len(expected_argtypes), len(argtypes)))

    try:
        # Make sure we have compatible types
        unify(zip(argtypes, expected_argtypes))
    except UnificationError:
        raise TypeError(
            "Mismatching signature for function %s with argument types %s" % (
                func, ", ".join(map(str, argtypes))))

    return func, Function[expected_argtypes + (restype,)], restype
Example #3
0
def resolve_restype(func, env):
    """Figure out the return type and update the context and environment"""
    context = env['flypy.typing.context']
    restype = env['flypy.typing.restype']
    signature = env['flypy.typing.signature']

    typeset = context['return']
    inferred_restype = signature.restype

    if restype is None:
        restype = inferred_restype
    elif inferred_restype != restype:
        try:
            [restype] = unify([(inferred_restype, restype)])
        except UnificationError as e:
            raise TypeError(
                "Annotated result type %s does not match inferred "
                "type %s for function %r: %s" % (
                    restype, inferred_restype, func.name, e))

    if isinstance(restype, set):
        raise TypeError(
            "Undetermined return type for function %s" % (func.name,))

    env['flypy.typing.restype'] = restype

    if restype == void or env['flypy.state.generator']:
        _, argtypes, varargs = func.type
        func.type = types.Function(types.Void, argtypes, varargs)
Example #4
0
def resolve_restype(func, env):
    """Figure out the return type and update the context and environment"""
    context = env['flypy.typing.context']
    restype = env['flypy.typing.restype']
    signature = env['flypy.typing.signature']

    typeset = context['return']
    inferred_restype = signature.restype

    if restype is None:
        restype = inferred_restype
    elif inferred_restype != restype:
        try:
            [restype] = unify([(inferred_restype, restype)])
        except UnificationError, e:
            raise TypeError("Annotated result type %s does not match inferred "
                            "type %s for function %r: %s" %
                            (restype, inferred_restype, func.name, e))
Example #5
0
def resolve_restype(func, env):
    """Figure out the return type and update the context and environment"""
    context = env['flypy.typing.context']
    restype = env['flypy.typing.restype']
    signature = env['flypy.typing.signature']

    typeset = context['return']
    inferred_restype = signature.restype

    if restype is None:
        restype = inferred_restype
    elif inferred_restype != restype:
        try:
            [restype] = unify([(inferred_restype, restype)])
        except UnificationError, e:
            raise TypeError(
                "Annotated result type %s does not match inferred "
                "type %s for function %r: %s" % (
                    restype, inferred_restype, func.name, e))