Skip to content

Commit 6607b0d

Browse files
authored
Simplify function call creation (pyccel#2020)
Add the magic method `__call__` to `FunctionDef`, `Interface` and `PyccelFunctionDef` to create `FunctionCall` nodes. This makes the code more readable as the verbose `FunctionCall(func, [arg1, arg2])` can now be written as `func(arg1, arg2)`.
1 parent 7974ec8 commit 6607b0d

File tree

6 files changed

+85
-75
lines changed

6 files changed

+85
-75
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ All notable changes to this project will be documented in this file.
4646
- Add a warning about containers in lists.
4747
- \[INTERNALS\] Add abstract class `SetMethod` to handle calls to various set methods.
4848
- \[INTERNALS\] Added `container_rank` property to `ast.datatypes.PyccelType` objects.
49+
- \[INTERNALS\] Add a `__call__` method to `FunctionDef` to create `FunctionCall` instances.
4950
- \[DEVELOPER\] Added an improved traceback to the developer-mode errors for errors in function calls.
5051

5152
### Fixed

pyccel/ast/core.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2765,6 +2765,11 @@ def result_pointer_map(self):
27652765
"""
27662766
return self._result_pointer_map
27672767

2768+
def __call__(self, *args, **kwargs):
2769+
arguments = [a if isinstance(a, FunctionCallArgument) else FunctionCallArgument(a) for a in args]
2770+
arguments += [FunctionCallArgument(a, keyword=key) for key, a in kwargs.items()]
2771+
return FunctionCall(self, arguments)
2772+
27682773
class InlineFunctionDef(FunctionDef):
27692774
"""
27702775
Represents a function definition for an inline function.
@@ -2955,6 +2960,9 @@ def __getnewargs__(self):
29552960
'argument_description':self._argument_description}
29562961
return args, kwargs
29572962

2963+
def __call__(self, *args, **kwargs):
2964+
return self._cls_name(*args, **kwargs)
2965+
29582966
class Interface(PyccelAstNode):
29592967
"""
29602968
Class representing an interface function.
@@ -3173,6 +3181,11 @@ def type_match(call_arg, func_arg):
31733181
severity='fatal')
31743182
return self._functions[j]
31753183

3184+
def __call__(self, *args, **kwargs):
3185+
arguments = [a if isinstance(a, FunctionCallArgument) else FunctionCallArgument(a) for a in args]
3186+
arguments += [FunctionCallArgument(a, keyword=key) for key, a in kwargs.items()]
3187+
return FunctionCall(self, arguments)
3188+
31763189
class FunctionAddress(FunctionDef):
31773190
"""
31783191
Represents a function address.

pyccel/codegen/printing/fcode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ def _get_comparison_operator(self, element_type, imports_and_macros):
553553
compare_func = FunctionDef('complex_comparison',
554554
[FunctionDefArgument(tmpVar_x), FunctionDefArgument(tmpVar_y)],
555555
[FunctionDefResult(Variable(PythonNativeBool(), 'c'))], [])
556-
lt_def = FunctionCall(compare_func, [tmpVar_x, tmpVar_y])
556+
lt_def = compare_func(tmpVar_x, tmpVar_y)
557557
else:
558558
lt_def = PyccelAssociativeParenthesis(PyccelLt(tmpVar_x, tmpVar_y))
559559

0 commit comments

Comments
 (0)