Nuitka
The Python compiler
Loading...
Searching...
No Matches
compiled_function.h
1// Copyright 2025, Kay Hayen, mailto:kay.hayen@gmail.com find license text at end of file
2
3#ifndef __NUITKA_COMPILED_FUNCTION_H__
4#define __NUITKA_COMPILED_FUNCTION_H__
5
6#ifdef __IDE_ONLY__
7#include "nuitka/prelude.h"
8#endif
9
10// Compiled function type.
11
12// The backbone of the integration into CPython. Try to behave as well as normal
13// functions and built-in functions, or even better.
14
16
17// The actual function code with arguments as an array.
18typedef PyObject *(*function_impl_code)(PyThreadState *tstate, struct Nuitka_FunctionObject const *, PyObject **);
19
20// The Nuitka_FunctionObject is the storage associated with a compiled function
21// instance of which there can be many for each code.
23 /* Python object folklore: */
24 PyObject_VAR_HEAD
25
26 PyObject *m_name;
27
28 PyObject *m_module;
29 PyObject *m_doc;
30
31 PyCodeObject *m_code_object;
32 Py_ssize_t m_args_overall_count;
33 Py_ssize_t m_args_positional_count;
34 Py_ssize_t m_args_keywords_count;
35 bool m_args_simple;
36 Py_ssize_t m_args_star_list_index;
37 Py_ssize_t m_args_star_dict_index;
38
39#if PYTHON_VERSION >= 0x380
40 Py_ssize_t m_args_pos_only_count;
41#endif
42
43 // Same as code_object->co_varnames
44 PyObject **m_varnames;
45
46 // C implementation of the function
47 function_impl_code m_c_code;
48
49#if PYTHON_VERSION >= 0x380
50 vectorcallfunc m_vectorcall;
51#endif
52
53 PyObject *m_dict;
54 PyObject *m_weakrefs;
55
56 // Tuple of defaults, for use in __defaults__ and parameter parsing.
57 PyObject *m_defaults;
58 Py_ssize_t m_defaults_given;
59
60#if PYTHON_VERSION >= 0x300
61 // List of keyword only defaults, for use in __kwdefaults__ and parameter
62 // parsing.
63 PyObject *m_kwdefaults;
64
65 // Annotations to the function arguments and return value.
66 PyObject *m_annotations;
67#endif
68
69#if PYTHON_VERSION >= 0x300
70 PyObject *m_qualname;
71#endif
72
73#if PYTHON_VERSION >= 0x3c0
74 PyObject *m_type_params;
75#endif
76
77 // Constant return value to use.
78 PyObject *m_constant_return_value;
79
80 // A kind of uuid for the function object, used in comparisons.
81 long m_counter;
82
83 // Closure taken objects, for use in __closure__ and for accessing it.
84 Py_ssize_t m_closure_given;
85 struct Nuitka_CellObject *m_closure[1];
86};
87
88extern PyTypeObject Nuitka_Function_Type;
89
90// Make a function with context.
91#if PYTHON_VERSION < 0x300
92extern struct Nuitka_FunctionObject *Nuitka_Function_New(function_impl_code c_code, PyObject *name,
93 PyCodeObject *code_object, PyObject *defaults,
94 PyObject *module, PyObject *doc,
95 struct Nuitka_CellObject **closure, Py_ssize_t closure_given);
96#else
97extern struct Nuitka_FunctionObject *Nuitka_Function_New(function_impl_code c_code, PyObject *name, PyObject *qualname,
98 PyCodeObject *code_object, PyObject *defaults,
99 PyObject *kw_defaults, PyObject *annotations, PyObject *module,
100 PyObject *doc, struct Nuitka_CellObject **closure,
101 Py_ssize_t closure_given);
102#endif
103
104extern void Nuitka_Function_EnableConstReturnTrue(struct Nuitka_FunctionObject *function);
105
106extern void Nuitka_Function_EnableConstReturnFalse(struct Nuitka_FunctionObject *function);
107
108extern void Nuitka_Function_EnableConstReturnGeneric(struct Nuitka_FunctionObject *function, PyObject *value);
109
110#ifdef _NUITKA_PLUGIN_DILL_ENABLED
111extern PyObject *Nuitka_Function_GetFunctionState(struct Nuitka_FunctionObject *function,
112 function_impl_code const *function_table);
113extern struct Nuitka_FunctionObject *Nuitka_Function_CreateFunctionViaCodeIndex(
114 PyObject *module, PyObject *function_qualname, PyObject *function_index, PyObject *code_object_desc,
115 PyObject *constant_return_value, PyObject *defaults, PyObject *kw_defaults, PyObject *doc, PyObject *closure,
116 PyObject *annotations, PyObject *func_dict, function_impl_code const *function_table, int function_table_size);
117extern PyObject *Nuitka_Function_ExtractCodeObjectDescription(PyThreadState *tstate,
118 struct Nuitka_FunctionObject *function);
119#endif
120
121static inline bool Nuitka_Function_Check(PyObject *object) { return Py_TYPE(object) == &Nuitka_Function_Type; }
122
123static inline PyObject *Nuitka_Function_GetName(PyObject *object) {
124 return ((struct Nuitka_FunctionObject *)object)->m_name;
125}
126
127PyObject *Nuitka_CallFunctionNoArgs(PyThreadState *tstate, struct Nuitka_FunctionObject const *function);
128
129PyObject *Nuitka_CallFunctionPosArgs(PyThreadState *tstate, struct Nuitka_FunctionObject const *function,
130 PyObject *const *args, Py_ssize_t args_size);
131
132PyObject *Nuitka_CallFunctionVectorcall(PyThreadState *tstate, struct Nuitka_FunctionObject const *function,
133 PyObject *const *args, Py_ssize_t args_size, PyObject *const *kw_names,
134 Py_ssize_t kw_size);
135PyObject *Nuitka_CallFunctionPosArgsKwArgs(PyThreadState *tstate, struct Nuitka_FunctionObject const *function,
136 PyObject *const *args, Py_ssize_t args_size, PyObject *kw);
137PyObject *Nuitka_CallFunctionPosArgsKwSplit(PyThreadState *tstate, struct Nuitka_FunctionObject const *function,
138 PyObject *const *args, Py_ssize_t args_size, PyObject *const *kw_values,
139 PyObject *kw_names);
140
141PyObject *Nuitka_CallMethodFunctionNoArgs(PyThreadState *tstate, struct Nuitka_FunctionObject const *function,
142 PyObject *object);
143PyObject *Nuitka_CallMethodFunctionPosArgs(PyThreadState *tstate, struct Nuitka_FunctionObject const *function,
144 PyObject *object, PyObject *const *args, Py_ssize_t args_size);
145PyObject *Nuitka_CallMethodFunctionPosArgsKwArgs(PyThreadState *tstate, struct Nuitka_FunctionObject const *function,
146 PyObject *object, PyObject *const *args, Py_ssize_t args_size,
147 PyObject *kw);
148
149#if _DEBUG_REFCOUNTS
150extern int count_active_Nuitka_Function_Type;
151extern int count_allocated_Nuitka_Function_Type;
152extern int count_released_Nuitka_Function_Type;
153#endif
154
155#endif
156
157// Part of "Nuitka", an optimizing Python compiler that is compatible and
158// integrates with CPython, but also works on its own.
159//
160// Licensed under the Apache License, Version 2.0 (the "License");
161// you may not use this file except in compliance with the License.
162// You may obtain a copy of the License at
163//
164// http://www.apache.org/licenses/LICENSE-2.0
165//
166// Unless required by applicable law or agreed to in writing, software
167// distributed under the License is distributed on an "AS IS" BASIS,
168// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
169// See the License for the specific language governing permissions and
170// limitations under the License.
Definition compiled_cell.h:14
Definition compiled_function.h:22