Debian Codegraph @

Include/internal/pycore_compile.h

raw
1 #ifndef Py_INTERNAL_COMPILE_H
2 #define Py_INTERNAL_COMPILE_H
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6
7 #ifndef Py_BUILD_CORE
8 # error "this header requires Py_BUILD_CORE define"
9 #endif
10
11 #include <stdbool.h>
12
13 #include "pycore_ast.h" // mod_ty
14 #include "pycore_symtable.h" // _Py_SourceLocation
15 #include "pycore_instruction_sequence.h"
16
17 /* A soft limit for stack use, to avoid excessive
18 * memory use for large constants, etc.
19 *
20 * The value 30 is plucked out of thin air.
21 * Code that could use more stack than this is
22 * rare, so the exact value is unimportant.
23 */
24 #define _PY_STACK_USE_GUIDELINE 30
25
26 struct _arena; // Type defined in pycore_pyarena.h
27 struct _mod; // Type defined in pycore_ast.h
28
29 // Export for 'test_peg_generator' shared extension
30 PyAPI_FUNC(PyCodeObject*) _PyAST_Compile(
31 struct _mod *mod,
32 PyObject *filename,
33 PyCompilerFlags *flags,
34 int optimize,
35 struct _arena *arena);
36
37 /* AST preprocessing */
38 extern int _PyCompile_AstPreprocess(
39 struct _mod *mod,
40 PyObject *filename,
41 PyCompilerFlags *flags,
42 int optimize,
43 struct _arena *arena,
44 int syntax_check_only);
45
46 extern int _PyAST_Preprocess(
47 struct _mod *,
48 struct _arena *arena,
49 PyObject *filename,
50 int optimize,
51 int ff_features,
52 int syntax_check_only,
53 int enable_warnings);
54
55
56 typedef struct {
57 PyObject *u_name;
58 PyObject *u_qualname; /* dot-separated qualified name (lazy) */
59
60 /* The following fields are dicts that map objects to
61 the index of them in co_XXX. The index is used as
62 the argument for opcodes that refer to those collections.
63 */
64 PyObject *u_consts; /* all constants */
65 PyObject *u_names; /* all names */
66 PyObject *u_varnames; /* local variables */
67 PyObject *u_cellvars; /* cell variables */
68 PyObject *u_freevars; /* free variables */
69 PyObject *u_fasthidden; /* dict; keys are names that are fast-locals only
70 temporarily within an inlined comprehension. When
71 value is True, treat as fast-local. */
72
73 Py_ssize_t u_argcount; /* number of arguments for block */
74 Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */
75 Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */
76
77 int u_firstlineno; /* the first lineno of the block */
78 } _PyCompile_CodeUnitMetadata;
79
80 struct _PyCompiler;
81
82 typedef enum {
83 COMPILE_OP_FAST,
84 COMPILE_OP_GLOBAL,
85 COMPILE_OP_DEREF,
86 COMPILE_OP_NAME,
87 } _PyCompile_optype;
88
89 /* _PyCompile_FBlockInfo tracks the current frame block.
90 *
91 * A frame block is used to handle loops, try/except, and try/finally.
92 * It's called a frame block to distinguish it from a basic block in the
93 * compiler IR.
94 */
95
96 enum _PyCompile_FBlockType {
97 COMPILE_FBLOCK_WHILE_LOOP,
98 COMPILE_FBLOCK_FOR_LOOP,
99 COMPILE_FBLOCK_TRY_EXCEPT,
100 COMPILE_FBLOCK_FINALLY_TRY,
101 COMPILE_FBLOCK_FINALLY_END,
102 COMPILE_FBLOCK_WITH,
103 COMPILE_FBLOCK_ASYNC_WITH,
104 COMPILE_FBLOCK_HANDLER_CLEANUP,
105 COMPILE_FBLOCK_POP_VALUE,
106 COMPILE_FBLOCK_EXCEPTION_HANDLER,
107 COMPILE_FBLOCK_EXCEPTION_GROUP_HANDLER,
108 COMPILE_FBLOCK_ASYNC_COMPREHENSION_GENERATOR,
109 COMPILE_FBLOCK_STOP_ITERATION,
110 };
111
112 typedef struct {
113 enum _PyCompile_FBlockType fb_type;
114 _PyJumpTargetLabel fb_block;
115 _Py_SourceLocation fb_loc;
116 /* (optional) type-specific exit or cleanup block */
117 _PyJumpTargetLabel fb_exit;
118 /* (optional) additional information required for unwinding */
119 void *fb_datum;
120 } _PyCompile_FBlockInfo;
121
122
123 int _PyCompile_PushFBlock(struct _PyCompiler *c, _Py_SourceLocation loc,
124 enum _PyCompile_FBlockType t,
125 _PyJumpTargetLabel block_label,
126 _PyJumpTargetLabel exit, void *datum);
127 void _PyCompile_PopFBlock(struct _PyCompiler *c, enum _PyCompile_FBlockType t,
128 _PyJumpTargetLabel block_label);
129 _PyCompile_FBlockInfo *_PyCompile_TopFBlock(struct _PyCompiler *c);
130
131 int _PyCompile_EnterScope(struct _PyCompiler *c, identifier name, int scope_type,
132 void *key, int lineno, PyObject *private,
133 _PyCompile_CodeUnitMetadata *umd);
134 void _PyCompile_ExitScope(struct _PyCompiler *c);
135 Py_ssize_t _PyCompile_AddConst(struct _PyCompiler *c, PyObject *o);
136 _PyInstructionSequence *_PyCompile_InstrSequence(struct _PyCompiler *c);
137 int _PyCompile_StartAnnotationSetup(struct _PyCompiler *c);
138 int _PyCompile_EndAnnotationSetup(struct _PyCompiler *c);
139 int _PyCompile_FutureFeatures(struct _PyCompiler *c);
140 void _PyCompile_DeferredAnnotations(
141 struct _PyCompiler *c, PyObject **deferred_annotations,
142 PyObject **conditional_annotation_indices);
143 PyObject *_PyCompile_Mangle(struct _PyCompiler *c, PyObject *name);
144 PyObject *_PyCompile_MaybeMangle(struct _PyCompiler *c, PyObject *name);
145 int _PyCompile_MaybeAddStaticAttributeToClass(struct _PyCompiler *c, expr_ty e);
146 int _PyCompile_GetRefType(struct _PyCompiler *c, PyObject *name);
147 int _PyCompile_LookupCellvar(struct _PyCompiler *c, PyObject *name);
148 int _PyCompile_ResolveNameop(struct _PyCompiler *c, PyObject *mangled, int scope,
149 _PyCompile_optype *optype, Py_ssize_t *arg);
150
151 int _PyCompile_IsInteractiveTopLevel(struct _PyCompiler *c);
152 int _PyCompile_IsInInlinedComp(struct _PyCompiler *c);
153 int _PyCompile_ScopeType(struct _PyCompiler *c);
154 int _PyCompile_OptimizationLevel(struct _PyCompiler *c);
155 int _PyCompile_LookupArg(struct _PyCompiler *c, PyCodeObject *co, PyObject *name);
156 PyObject *_PyCompile_Qualname(struct _PyCompiler *c);
157 _PyCompile_CodeUnitMetadata *_PyCompile_Metadata(struct _PyCompiler *c);
158 PyObject *_PyCompile_StaticAttributesAsTuple(struct _PyCompiler *c);
159
160 struct symtable *_PyCompile_Symtable(struct _PyCompiler *c);
161 PySTEntryObject *_PyCompile_SymtableEntry(struct _PyCompiler *c);
162
163 enum {
164 COMPILE_SCOPE_MODULE,
165 COMPILE_SCOPE_CLASS,
166 COMPILE_SCOPE_FUNCTION,
167 COMPILE_SCOPE_ASYNC_FUNCTION,
168 COMPILE_SCOPE_LAMBDA,
169 COMPILE_SCOPE_COMPREHENSION,
170 COMPILE_SCOPE_ANNOTATIONS,
171 };
172
173
174 typedef struct {
175 PyObject *pushed_locals;
176 PyObject *temp_symbols;
177 PyObject *fast_hidden;
178 _PyJumpTargetLabel cleanup;
179 } _PyCompile_InlinedComprehensionState;
180
181 int _PyCompile_TweakInlinedComprehensionScopes(struct _PyCompiler *c, _Py_SourceLocation loc,
182 PySTEntryObject *entry,
183 _PyCompile_InlinedComprehensionState *state);
184 int _PyCompile_RevertInlinedComprehensionScopes(struct _PyCompiler *c, _Py_SourceLocation loc,
185 _PyCompile_InlinedComprehensionState *state);
186 int _PyCompile_AddDeferredAnnotation(struct _PyCompiler *c, stmt_ty s,
187 PyObject **conditional_annotation_index);
188 void _PyCompile_EnterConditionalBlock(struct _PyCompiler *c);
189 void _PyCompile_LeaveConditionalBlock(struct _PyCompiler *c);
190
191 int _PyCodegen_AddReturnAtEnd(struct _PyCompiler *c, int addNone);
192 int _PyCodegen_EnterAnonymousScope(struct _PyCompiler* c, mod_ty mod);
193 int _PyCodegen_Expression(struct _PyCompiler *c, expr_ty e);
194 int _PyCodegen_Module(struct _PyCompiler *c, _Py_SourceLocation loc, asdl_stmt_seq *stmts,
195 bool is_interactive);
196
197 int _PyCompile_ConstCacheMergeOne(PyObject *const_cache, PyObject **obj);
198
199 PyCodeObject *_PyCompile_OptimizeAndAssemble(struct _PyCompiler *c, int addNone);
200
201 Py_ssize_t _PyCompile_DictAddObj(PyObject *dict, PyObject *o);
202 int _PyCompile_Error(struct _PyCompiler *c, _Py_SourceLocation loc, const char *format, ...);
203 int _PyCompile_Warn(struct _PyCompiler *c, _Py_SourceLocation loc, const char *format, ...);
204
205 // Export for '_opcode' extension module
206 PyAPI_FUNC(PyObject*) _PyCompile_GetUnaryIntrinsicName(int index);
207 PyAPI_FUNC(PyObject*) _PyCompile_GetBinaryIntrinsicName(int index);
208
209 /* Access compiler internals for unit testing */
210
211 // Export for '_testinternalcapi' shared extension
212 PyAPI_FUNC(PyObject*) _PyCompile_CleanDoc(PyObject *doc);
213
214 // Export for '_testinternalcapi' shared extension
215 PyAPI_FUNC(PyObject*) _PyCompile_CodeGen(
216 PyObject *ast,
217 PyObject *filename,
218 PyCompilerFlags *flags,
219 int optimize,
220 int compile_mode);
221
222 // Export for '_testinternalcapi' shared extension
223 PyAPI_FUNC(PyCodeObject*)
224 _PyCompile_Assemble(_PyCompile_CodeUnitMetadata *umd, PyObject *filename,
225 PyObject *instructions);
226
227 #ifdef __cplusplus
228 }
229 #endif
230 #endif /* !Py_INTERNAL_COMPILE_H */