149149from mypyc .primitives .generic_ops import (
150150 generic_len_op ,
151151 generic_ssize_t_len_op ,
152+ method_wrapper_op ,
152153 py_call_op ,
153154 py_call_with_kwargs_op ,
154155 py_call_with_posargs_op ,
@@ -1365,12 +1366,36 @@ def call(
13651366 arguments used to track the presence of values for certain arguments. By
13661367 default, the values of the bitmap arguments are inferred from args.
13671368 """
1369+ if decl .class_name is not None and not self .can_pass_args_natively (
1370+ arg_kinds , arg_names , decl .sig
1371+ ):
1372+ return self .call_method_wrapper (decl , args , arg_kinds , arg_names , line )
13681373 # Normalize args to positionals.
13691374 args = self .native_args_to_positional (
13701375 args , arg_kinds , arg_names , decl .sig , line , bitmap_args = bitmap_args
13711376 )
13721377 return self .add (Call (decl , args , line ))
13731378
1379+ def call_method_wrapper (
1380+ self ,
1381+ decl : FuncDecl ,
1382+ args : Sequence [Value ],
1383+ arg_kinds : list [ArgKind ],
1384+ arg_names : Sequence [str | None ],
1385+ line : int ,
1386+ ) -> Value :
1387+ """Call a native method through its wrapper function, which parses the arguments.
1388+
1389+ This is slower than a native call, but like a native call it calls the given
1390+ method without looking it up. A lookup could find an override, or the __init__
1391+ slot wrapper of a native class, which usually does nothing (__init__ is called
1392+ when the object is created instead).
1393+ """
1394+ typ = self .load_native_type_object (f"{ decl .module_name } .{ decl .class_name } " )
1395+ wrapper = self .call_c (method_wrapper_op , [typ , self .load_str (decl .name )], line )
1396+ result = self .py_call (wrapper , list (args ), line , arg_kinds , arg_names )
1397+ return self .coerce (result , decl .sig .ret_type , line )
1398+
13741399 def native_args_to_positional (
13751400 self ,
13761401 args : Sequence [Value ],
@@ -1390,22 +1415,9 @@ def native_args_to_positional(
13901415 and coerce arguments to the appropriate type.
13911416 """
13921417
1393- sig_args = sig .args
1418+ sig_args = sig .real_args ()
13941419 n = sig .num_bitmap_args
1395- if n :
1396- sig_args = sig_args [:- n ]
1397-
1398- sig_arg_kinds = [arg .kind for arg in sig_args ]
1399- sig_arg_names = [arg .name for arg in sig_args ]
1400-
1401- concrete_kinds = [concrete_arg_kind (arg_kind ) for arg_kind in arg_kinds ]
1402- formal_to_actual = map_actuals_to_formals (
1403- concrete_kinds ,
1404- arg_names ,
1405- sig_arg_kinds ,
1406- sig_arg_names ,
1407- lambda n : AnyType (TypeOfAny .special_form ),
1408- )
1420+ formal_to_actual = self .map_native_call_args (arg_kinds , arg_names , sig )
14091421
14101422 # First scan for */** and construct those
14111423 has_star = has_star2 = False
@@ -1465,6 +1477,41 @@ def native_args_to_positional(
14651477
14661478 return output_args
14671479
1480+ def map_native_call_args (
1481+ self , arg_kinds : list [ArgKind ], arg_names : Sequence [str | None ], sig : FuncSignature
1482+ ) -> list [list [int ]]:
1483+ """Map the arguments of a call to the arguments of a native function.
1484+
1485+ Return the indexes of the call arguments that each argument of the function
1486+ (excluding bitmap arguments) may get its value from.
1487+ """
1488+ sig_args = sig .real_args ()
1489+ return map_actuals_to_formals (
1490+ [concrete_arg_kind (arg_kind ) for arg_kind in arg_kinds ],
1491+ arg_names ,
1492+ [arg .kind for arg in sig_args ],
1493+ [arg .name for arg in sig_args ],
1494+ lambda n : AnyType (TypeOfAny .special_form ),
1495+ )
1496+
1497+ def can_pass_args_natively (
1498+ self , arg_kinds : list [ArgKind ], arg_names : Sequence [str | None ], sig : FuncSignature
1499+ ) -> bool :
1500+ """Can native_args_to_positional() prepare the arguments of a call?
1501+
1502+ It can't unpack a *args or **kwargs argument, since we don't know at compile
1503+ time which arguments of the function the items are for. It can only pass one
1504+ on as a whole to the *args or **kwargs argument of the function.
1505+ """
1506+ if not any (kind .is_star () for kind in arg_kinds ):
1507+ return True
1508+ formal_to_actual = self .map_native_call_args (arg_kinds , arg_names , sig )
1509+ for formal , actuals in zip (sig .real_args (), formal_to_actual ):
1510+ for actual in actuals :
1511+ if arg_kinds [actual ].is_star () and arg_kinds [actual ] != formal .kind :
1512+ return False
1513+ return True
1514+
14681515 def gen_method_call (
14691516 self ,
14701517 base : Value ,
0 commit comments