@@ -8,6 +8,7 @@ extern "C" {
8
8
# error "this header requires Py_BUILD_CORE define"
9
9
#endif
10
10
11
+ #include "pycore_ast.h" // mod_ty
11
12
#include "pycore_symtable.h" // _Py_SourceLocation
12
13
#include "pycore_instruction_sequence.h"
13
14
@@ -63,6 +64,120 @@ typedef struct {
63
64
int u_firstlineno ; /* the first lineno of the block */
64
65
} _PyCompile_CodeUnitMetadata ;
65
66
67
+ struct _PyCompiler ;
68
+
69
+ typedef enum {
70
+ COMPILE_OP_FAST ,
71
+ COMPILE_OP_GLOBAL ,
72
+ COMPILE_OP_DEREF ,
73
+ COMPILE_OP_NAME ,
74
+ } _PyCompile_optype ;
75
+
76
+ /* _PyCompile_FBlockInfo tracks the current frame block.
77
+ *
78
+ * A frame block is used to handle loops, try/except, and try/finally.
79
+ * It's called a frame block to distinguish it from a basic block in the
80
+ * compiler IR.
81
+ */
82
+
83
+ enum _PyCompile_FBlockType {
84
+ COMPILE_FBLOCK_WHILE_LOOP ,
85
+ COMPILE_FBLOCK_FOR_LOOP ,
86
+ COMPILE_FBLOCK_TRY_EXCEPT ,
87
+ COMPILE_FBLOCK_FINALLY_TRY ,
88
+ COMPILE_FBLOCK_FINALLY_END ,
89
+ COMPILE_FBLOCK_WITH ,
90
+ COMPILE_FBLOCK_ASYNC_WITH ,
91
+ COMPILE_FBLOCK_HANDLER_CLEANUP ,
92
+ COMPILE_FBLOCK_POP_VALUE ,
93
+ COMPILE_FBLOCK_EXCEPTION_HANDLER ,
94
+ COMPILE_FBLOCK_EXCEPTION_GROUP_HANDLER ,
95
+ COMPILE_FBLOCK_ASYNC_COMPREHENSION_GENERATOR ,
96
+ COMPILE_FBLOCK_STOP_ITERATION ,
97
+ };
98
+
99
+ typedef struct {
100
+ enum _PyCompile_FBlockType fb_type ;
101
+ _PyJumpTargetLabel fb_block ;
102
+ _Py_SourceLocation fb_loc ;
103
+ /* (optional) type-specific exit or cleanup block */
104
+ _PyJumpTargetLabel fb_exit ;
105
+ /* (optional) additional information required for unwinding */
106
+ void * fb_datum ;
107
+ } _PyCompile_FBlockInfo ;
108
+
109
+
110
+ int _PyCompile_PushFBlock (struct _PyCompiler * c , _Py_SourceLocation loc ,
111
+ enum _PyCompile_FBlockType t ,
112
+ _PyJumpTargetLabel block_label ,
113
+ _PyJumpTargetLabel exit , void * datum );
114
+ void _PyCompile_PopFBlock (struct _PyCompiler * c , enum _PyCompile_FBlockType t ,
115
+ _PyJumpTargetLabel block_label );
116
+ _PyCompile_FBlockInfo * _PyCompile_TopFBlock (struct _PyCompiler * c );
117
+
118
+ int _PyCompile_EnterScope (struct _PyCompiler * c , identifier name , int scope_type ,
119
+ void * key , int lineno , PyObject * private ,
120
+ _PyCompile_CodeUnitMetadata * umd );
121
+ void _PyCompile_ExitScope (struct _PyCompiler * c );
122
+ Py_ssize_t _PyCompile_AddConst (struct _PyCompiler * c , PyObject * o );
123
+ _PyInstructionSequence * _PyCompile_InstrSequence (struct _PyCompiler * c );
124
+ int _PyCompile_FutureFeatures (struct _PyCompiler * c );
125
+ PyObject * _PyCompile_DeferredAnnotations (struct _PyCompiler * c );
126
+ PyObject * _PyCompile_Mangle (struct _PyCompiler * c , PyObject * name );
127
+ PyObject * _PyCompile_MaybeMangle (struct _PyCompiler * c , PyObject * name );
128
+ int _PyCompile_MaybeAddStaticAttributeToClass (struct _PyCompiler * c , expr_ty e );
129
+ int _PyCompile_GetRefType (struct _PyCompiler * c , PyObject * name );
130
+ int _PyCompile_LookupCellvar (struct _PyCompiler * c , PyObject * name );
131
+ int _PyCompile_ResolveNameop (struct _PyCompiler * c , PyObject * mangled , int scope ,
132
+ _PyCompile_optype * optype , Py_ssize_t * arg );
133
+
134
+ int _PyCompile_IsInteractive (struct _PyCompiler * c );
135
+ int _PyCompile_IsNestedScope (struct _PyCompiler * c );
136
+ int _PyCompile_IsInInlinedComp (struct _PyCompiler * c );
137
+ int _PyCompile_ScopeType (struct _PyCompiler * c );
138
+ int _PyCompile_OptimizationLevel (struct _PyCompiler * c );
139
+ PyArena * _PyCompile_Arena (struct _PyCompiler * c );
140
+ int _PyCompile_LookupArg (struct _PyCompiler * c , PyCodeObject * co , PyObject * name );
141
+ PyObject * _PyCompile_Qualname (struct _PyCompiler * c );
142
+ _PyCompile_CodeUnitMetadata * _PyCompile_Metadata (struct _PyCompiler * c );
143
+ PyObject * _PyCompile_StaticAttributesAsTuple (struct _PyCompiler * c );
144
+
145
+ #ifndef NDEBUG
146
+ int _PyCompile_IsTopLevelAwait (struct _PyCompiler * c );
147
+ #endif
148
+
149
+ struct symtable * _PyCompile_Symtable (struct _PyCompiler * c );
150
+ PySTEntryObject * _PyCompile_SymtableEntry (struct _PyCompiler * c );
151
+
152
+ enum {
153
+ COMPILE_SCOPE_MODULE ,
154
+ COMPILE_SCOPE_CLASS ,
155
+ COMPILE_SCOPE_FUNCTION ,
156
+ COMPILE_SCOPE_ASYNC_FUNCTION ,
157
+ COMPILE_SCOPE_LAMBDA ,
158
+ COMPILE_SCOPE_COMPREHENSION ,
159
+ COMPILE_SCOPE_ANNOTATIONS ,
160
+ };
161
+
162
+
163
+ typedef struct {
164
+ PyObject * pushed_locals ;
165
+ PyObject * temp_symbols ;
166
+ PyObject * fast_hidden ;
167
+ _PyJumpTargetLabel cleanup ;
168
+ } _PyCompile_InlinedComprehensionState ;
169
+
170
+ int _PyCompile_TweakInlinedComprehensionScopes (struct _PyCompiler * c , _Py_SourceLocation loc ,
171
+ PySTEntryObject * entry ,
172
+ _PyCompile_InlinedComprehensionState * state );
173
+ int _PyCompile_RevertInlinedComprehensionScopes (struct _PyCompiler * c , _Py_SourceLocation loc ,
174
+ _PyCompile_InlinedComprehensionState * state );
175
+ int _PyCompile_AddDeferredAnnotaion (struct _PyCompiler * c , stmt_ty s );
176
+
177
+ int _PyCodegen_AddReturnAtEnd (struct _PyCompiler * c , int addNone );
178
+ int _PyCodegen_EnterAnonymousScope (struct _PyCompiler * c , mod_ty mod );
179
+ int _PyCodegen_Expression (struct _PyCompiler * c , expr_ty e );
180
+ int _PyCodegen_Body (struct _PyCompiler * c , _Py_SourceLocation loc , asdl_stmt_seq * stmts );
66
181
67
182
/* Utility for a number of growing arrays used in the compiler */
68
183
int _PyCompile_EnsureArrayLargeEnough (
@@ -74,6 +189,11 @@ int _PyCompile_EnsureArrayLargeEnough(
74
189
75
190
int _PyCompile_ConstCacheMergeOne (PyObject * const_cache , PyObject * * obj );
76
191
192
+ PyCodeObject * _PyCompile_OptimizeAndAssemble (struct _PyCompiler * c , int addNone );
193
+
194
+ Py_ssize_t _PyCompile_DictAddObj (PyObject * dict , PyObject * o );
195
+ int _PyCompile_Error (struct _PyCompiler * c , _Py_SourceLocation loc , const char * format , ...);
196
+ int _PyCompile_Warn (struct _PyCompiler * c , _Py_SourceLocation loc , const char * format , ...);
77
197
78
198
// Export for '_opcode' extension module
79
199
PyAPI_FUNC (PyObject * ) _PyCompile_GetUnaryIntrinsicName (int index );
0 commit comments