{#-  Copyright 2026, Kay Hayen, mailto:kay.hayen@gmail.com find license text at end of file -#}

{% from 'HelperSlotsCommon.c.j2' import goto_exit %}

{% macro unicode_slot(props, operator, nb_slot, target, left, right, result, operand1, operand2, exit_result_ok, exit_result_exception) %}
    // Not every code path will make use of all possible results.
    NUITKA_MAY_BE_UNUSED PyObject *obj_result;

    {# TODO: Could and should in-line and specialize this per slot too. #}
{% if nb_slot == "nb_add" %}
{% if left.type_name == "unicode" == right.type_name %}
    // TODO: Have this more globally passed in
    PyThreadState *tstate = PyThreadState_GET();

    PyObject *x = UNICODE_CONCAT(tstate, {{ operand1 }}, {{ operand2 }});
{% else %}
    PyObject *x = {{ left.getSlotCallExpression("sq_concat", "PyUnicode_Type.tp_as_sequence->sq_concat", operand1, operand2) }};
    assert(x != Py_NotImplemented);
{% endif %}
{% elif nb_slot == "nb_multiply" %}
    PyObject *x = {{ left.getSlotCallExpression("sq_repeat", "PyUnicode_Type.tp_as_sequence->sq_repeat", operand1, operand2) }};
    assert(x != Py_NotImplemented);
{% elif nb_slot == "nb_remainder" %}
    PyObject *x = PyUnicode_Format({{ operand1 }}, {{ operand2 }});
{% else %}
    PyObject *x = {{ left.getSlotCallExpression(nb_slot, "PyUnicode_Type.tp_as_number->" + nb_slot, operand1, operand2) }};
    assert(x != Py_NotImplemented);
{% endif %}

    {{ goto_exit(props, "exit_result_object", "x") }}

exit_result_object:
    if (unlikely(obj_result == NULL)) {
        {{ goto_exit(props, exit_result_exception) }}
    }
{% if target %}
    {{ target.getAssignFromObjectExpressionCode(result, "obj_result") }}
{% else %}
    {# TODO: Check the reference we were handed down and do it in-place really. #}
    // We got an object handed, that we have to release.
    Py_DECREF({{ operand1 }});

    {{ operand1 }} = obj_result;
{% endif %}
    {{ goto_exit(props, exit_result_ok) }}

{% endmacro %}

{#-  Part of "Nuitka", an optimizing Python compiler that is compatible and           -#}
{#-  integrates with CPython, but also works on its own.                              -#}
{#-                                                                                   -#}
{#-  Licensed under the GNU Affero General Public License, Version 3 (the "License"); -#}
{#-  you may not use this file except in compliance with the License.                 -#}
{#-  You may obtain a copy of the License at                                          -#}
{#-                                                                                   -#}
{#-  https://www.gnu.org/licenses/agpl-3.0.txt                                        -#}
{#-                                                                                   -#}
{#-  See also: "Nuitka Runtime Library Exception, Version 1.0" in file                -#}
{#-  "LICENSE-RUNTIME.txt" for additional permissions granted under Section 7.        -#}
{#-                                                                                   -#}
{#-  Unless required by applicable law or agreed to in writing, software              -#}
{#-  distributed under the License is distributed on an "AS IS" BASIS,                -#}
{#-  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.         -#}
{#-  See the License for the specific language governing permissions and              -#}
{#-  limitations under the License.                                                   -#}
