Open dylon opened 1 year ago
In my opinion, pass by reference should be implemented for all the types like list, dict and not just tuples. It should only be done for the arguments specified by the user. Doing it by an automatic ASR pass might create problems when the function call is a part of bigger expression. In fact, we faced this problem when implementing ASR to Julia code generation in LFortran.
Unlike Python, C does not support unpacking tuple return values. To work around this and avoid the overhead of using tuples, most C programmers pass parameters by reference that would otherwise be returned as tuple parameters in Python. Conversely, Python does not support passing by reference, so values are returned as tuple parameters that would otherwise be passed by reference in C. We may take advantage of these by specifying lpython function signatures that return tuples to maintain Python coherence while transforming them into references in the generated C functions.
As a thought, we could provide square bracket notation on the ltype hints to give them parameter names that may be referenced in the return type hints and thusly understood to be references. For example:
The generated C function and call might look something like the following:
Then, we could do other things like pass parameters by reference only into the function while passing others with the intent to mutate them. For example, the following Python code:
might be transformed into the following C code:
EDIT: 17-Dec-2022
An alternative syntax might look like the following: