Class PythonLikeMatchingStrategy
- java.lang.Object
-
- it.unive.lisa.program.language.resolution.PythonLikeMatchingStrategy
-
- All Implemented Interfaces:
ParameterMatchingStrategy
public class PythonLikeMatchingStrategy extends java.lang.Object implements ParameterMatchingStrategy
A Python-like matching strategy. Specifically, actual parameters are rearranged following the specification:
If keyword arguments are present, they are first converted to positional arguments, as follows. First, a list of unfilled slots is created for the formal parameters. If there are N positional arguments, they are placed in the first N slots. Next, for each keyword argument, the identifier is used to determine the corresponding slot (if the identifier is the same as the first formal parameter name, the first slot is used, and so on). If the slot is already filled, a TypeError exception is raised. Otherwise, the value of the argument is placed in the slot, filling it (even if the expression is None, it fills the slot). When all arguments have been processed, the slots that are still unfilled are filled with the corresponding default value from the function definition. [...] If there are any unfilled slots for which no default value is specified, a TypeError exception is raised. Otherwise, the list of filled slots is used as the argument list for the call. [...] If there are more positional arguments than there are formal parameter slots, a TypeError exception is raised [...]. If any keyword argument does not correspond to a formal parameter name, a TypeError exception is raised [...].
The only difference w.r.t. this specification is that whenever a TypeError exception should be raised, the matching interrupts andfalse
is returned.
Keyword parameters are identified by actuals (i.e.Expression
s) that are instance ofNamedParameterExpression
.
After the reordering is completed, the type-based matching of each parameter happens by delegation to a specifiedFixedOrderMatchingStrategy
.- See Also:
- Python Language Reference: calls
-
-
Constructor Summary
Constructors Constructor Description PythonLikeMatchingStrategy(FixedOrderMatchingStrategy delegate)
Builds the strategy.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
matches(Call call, Parameter[] formals, Expression[] actuals, java.util.Set<Type>[] types)
Yieldstrue
if and only if the parameter list of a cfg is matched by the given actual parameters, according to this strategy.static <T,F>
FpythonLogic(Parameter[] formals, Expression[] actuals, T[] given, java.util.Set<Type>[] givenTypes, T[] defaults, java.util.Set<Type>[] defaultTypes, T[] slots, java.util.Set<Type>[] slotTypes, F failure)
Python logic for preparing an argument list for a method call.
-
-
-
Constructor Detail
-
PythonLikeMatchingStrategy
public PythonLikeMatchingStrategy(FixedOrderMatchingStrategy delegate)
Builds the strategy.- Parameters:
delegate
- the strategy to delegate the match after the actual parameters have been shuffled
-
-
Method Detail
-
matches
public boolean matches(Call call, Parameter[] formals, Expression[] actuals, java.util.Set<Type>[] types)
Description copied from interface:ParameterMatchingStrategy
Yieldstrue
if and only if the parameter list of a cfg is matched by the given actual parameters, according to this strategy.- Specified by:
matches
in interfaceParameterMatchingStrategy
- Parameters:
call
- the call where the parameters are being matchedformals
- the parameters definition of the cfgactuals
- the expression that are used as call parameterstypes
- the runtime types of the actual parameters- Returns:
true
if and only if that condition holds
-
pythonLogic
public static <T,F> F pythonLogic(Parameter[] formals, Expression[] actuals, T[] given, java.util.Set<Type>[] givenTypes, T[] defaults, java.util.Set<Type>[] defaultTypes, T[] slots, java.util.Set<Type>[] slotTypes, F failure)
Python logic for preparing an argument list for a method call. If the preparation fails, that is, if the signatures are incompatible,failure
is returned. Otherwise,null
is returned.slots
is filled with the positional parameters fromgiven
first (that is, ones corresponding to an actual inactuals
that is not an instance ofNamedParameterExpression
), then named parameters fromgiven
, and lastly with default values fromdefaults
.- Type Parameters:
T
- the type of elements inslots
F
- the type of the element returned if the preparation fails- Parameters:
formals
- the formal parametersactuals
- the actual parametersgiven
- the value to use for each parameter, positional or namedgivenTypes
- the types of the value to use for each parameter, positional or nameddefaults
- the default values for each parameter if no explicit value is provideddefaultTypes
- the types of the default values for each parameter if no explicit value is providedslots
- the slots that represent final values to use as parametersslotTypes
- the types of the slots that represent final values to use as parametersfailure
- what to return in case of failure- Returns:
failure
if the preparation fails,null
otherwise- See Also:
- Python Language Reference: calls
-
-