Ejemplo n.º 1
0
 def assert_map(self, caller_kinds, callee_kinds, expected):
     caller_kinds, caller_names = expand_caller_kinds(caller_kinds)
     callee_kinds, callee_names = expand_callee_kinds(callee_kinds)
     result = map_actuals_to_formals(caller_kinds, caller_names,
                                     callee_kinds, callee_names,
                                     lambda i: AnyType())
     assert_equal(result, expected)
Ejemplo n.º 2
0
 def assert_vararg_map(self, caller_kinds, callee_kinds, expected,
                        vararg_type):
     result = map_actuals_to_formals(
         caller_kinds,
         [],
         callee_kinds,
         [],
         lambda i: vararg_type)
     assert_equal(result, expected)
Ejemplo n.º 3
0
 def assert_vararg_map(self, caller_kinds, callee_kinds, expected,
                       vararg_type):
     result = map_actuals_to_formals(
         caller_kinds,
         [],
         callee_kinds,
         [],
         lambda i: vararg_type)
     assert_equal(result, expected)
Ejemplo n.º 4
0
 def assert_map(self, caller_kinds, callee_kinds, expected):
     caller_kinds, caller_names = expand_caller_kinds(caller_kinds)
     callee_kinds, callee_names = expand_callee_kinds(callee_kinds)
     result = map_actuals_to_formals(
         caller_kinds,
         caller_names,
         callee_kinds,
         callee_names,
         lambda i: Any())
     assert_equal(result, expected)
Ejemplo n.º 5
0
 def assert_vararg_map(
     self,
     caller_kinds: List[int],
     callee_kinds: List[int],
     expected: List[List[int]],
     vararg_type: Type,
 ) -> None:
     result = map_actuals_to_formals(caller_kinds, [], callee_kinds, [],
                                     lambda i: vararg_type)
     assert_equal(result, expected)
Ejemplo n.º 6
0
 def assert_map(
     self,
     caller_kinds_: List[Union[int, str]],
     callee_kinds_: List[Union[int, Tuple[int, str]]],
     expected: List[List[int]],
 ) -> None:
     caller_kinds, caller_names = expand_caller_kinds(caller_kinds_)
     callee_kinds, callee_names = expand_callee_kinds(callee_kinds_)
     result = map_actuals_to_formals(
         caller_kinds, caller_names, callee_kinds, callee_names,
         lambda i: AnyType(TypeOfAny.special_form))
     assert_equal(result, expected)
Ejemplo n.º 7
0
 def assert_vararg_map(self,
                       caller_kinds: List[int],
                       callee_kinds: List[int],
                       expected: List[List[int]],
                       vararg_type: Type,
                       ) -> None:
     result = map_actuals_to_formals(
         caller_kinds,
         [],
         callee_kinds,
         [],
         lambda i: vararg_type)
     assert_equal(result, expected)
Ejemplo n.º 8
0
 def assert_map(self,
                caller_kinds_: List[Union[int, str]],
                callee_kinds_: List[Union[int, Tuple[int, str]]],
                expected: List[List[int]],
                ) -> None:
     caller_kinds, caller_names = expand_caller_kinds(caller_kinds_)
     callee_kinds, callee_names = expand_callee_kinds(callee_kinds_)
     result = map_actuals_to_formals(
         caller_kinds,
         caller_names,
         callee_kinds,
         callee_names,
         lambda i: AnyType(TypeOfAny.special_form))
     assert_equal(result, expected)
Ejemplo n.º 9
0
    def visit_call_expr(self, o: CallExpr) -> None:
        if not any(isinstance(e, RefExpr) and e.node in self.arg_types for e in o.args):
            return

        typ = get_proper_type(self.typemap.get(o.callee))
        if not isinstance(typ, CallableType):
            return

        formal_to_actual = map_actuals_to_formals(
            o.arg_kinds, o.arg_names, typ.arg_kinds, typ.arg_names,
            lambda n: AnyType(TypeOfAny.special_form))

        for i, args in enumerate(formal_to_actual):
            for arg_idx in args:
                arg = o.args[arg_idx]
                if isinstance(arg, RefExpr) and arg.node in self.arg_types:
                    self.arg_types[arg.node].append(typ.arg_types[i])
Ejemplo n.º 10
0
    def native_args_to_positional(self, args: Sequence[Value],
                                  arg_kinds: List[int],
                                  arg_names: Sequence[Optional[str]],
                                  sig: FuncSignature,
                                  line: int) -> List[Value]:
        """Prepare arguments for a native call.

        Given args/kinds/names and a target signature for a native call, map
        keyword arguments to their appropriate place in the argument list,
        fill in error values for unspecified default arguments,
        package arguments that will go into *args/**kwargs into a tuple/dict,
        and coerce arguments to the appropriate type.
        """

        sig_arg_kinds = [arg.kind for arg in sig.args]
        sig_arg_names = [arg.name for arg in sig.args]
        formal_to_actual = map_actuals_to_formals(
            arg_kinds, arg_names, sig_arg_kinds, sig_arg_names,
            lambda n: AnyType(TypeOfAny.special_form))

        # Flatten out the arguments, loading error values for default
        # arguments, constructing tuples/dicts for star args, and
        # coercing everything to the expected type.
        output_args = []
        for lst, arg in zip(formal_to_actual, sig.args):
            output_arg = None
            if arg.kind == ARG_STAR:
                output_arg = self.primitive_op(new_tuple_op,
                                               [args[i] for i in lst], line)
            elif arg.kind == ARG_STAR2:
                dict_entries = [
                    (self.load_static_unicode(cast(str,
                                                   arg_names[i])), args[i])
                    for i in lst
                ]
                output_arg = self.make_dict(dict_entries, line)
            elif not lst:
                output_arg = self.add(
                    LoadErrorValue(arg.type, is_borrowed=True))
            else:
                output_arg = args[lst[0]]
            output_args.append(self.coerce(output_arg, arg.type, line))

        return output_args