@@ -95,6 +95,7 @@ static PySTEntryObject *compiler_symtable_entry(struct compiler *c);
95
95
#define SCOPE_TYPE (C ) compiler_scope_type(C)
96
96
#define QUALNAME (C ) compiler_qualname(C)
97
97
#define METADATA (C ) compiler_unit_metadata(C)
98
+ #define ARENA (C ) compiler_arena(C)
98
99
99
100
typedef _Py_SourceLocation location ;
100
101
typedef struct _PyCfgBuilder cfg_builder ;
@@ -131,6 +132,7 @@ static void compiler_exit_scope(struct compiler *c);
131
132
static Py_ssize_t compiler_add_const (struct compiler * c , PyObject * o );
132
133
static int compiler_maybe_add_static_attribute_to_class (struct compiler * c , expr_ty e );
133
134
static _PyCompile_CodeUnitMetadata * compiler_unit_metadata (struct compiler * c );
135
+ static PyArena * compiler_arena (struct compiler * c );
134
136
135
137
#define LOCATION (LNO , END_LNO , COL , END_COL ) \
136
138
((const _Py_SourceLocation){(LNO), (END_LNO), (COL), (END_COL)})
@@ -250,60 +252,6 @@ _PyCompile_EnsureArrayLargeEnough(int idx, void **array, int *alloc,
250
252
}
251
253
252
254
253
- /* The following items change on entry and exit of code blocks.
254
- They must be saved and restored when returning to a block.
255
- */
256
- struct compiler_unit {
257
- PySTEntryObject * u_ste ;
258
-
259
- int u_scope_type ;
260
-
261
- PyObject * u_private ; /* for private name mangling */
262
- PyObject * u_static_attributes ; /* for class: attributes accessed via self.X */
263
- PyObject * u_deferred_annotations ; /* AnnAssign nodes deferred to the end of compilation */
264
-
265
- instr_sequence * u_instr_sequence ; /* codegen output */
266
-
267
- int u_nfblocks ;
268
- int u_in_inlined_comp ;
269
-
270
- struct fblockinfo u_fblock [CO_MAXBLOCKS ];
271
-
272
- _PyCompile_CodeUnitMetadata u_metadata ;
273
- };
274
-
275
- /* This struct captures the global state of a compilation.
276
-
277
- The u pointer points to the current compilation unit, while units
278
- for enclosing blocks are stored in c_stack. The u and c_stack are
279
- managed by compiler_enter_scope() and compiler_exit_scope().
280
-
281
- Note that we don't track recursion levels during compilation - the
282
- task of detecting and rejecting excessive levels of nesting is
283
- handled by the symbol analysis pass.
284
-
285
- */
286
-
287
- struct compiler {
288
- PyObject * c_filename ;
289
- struct symtable * c_st ;
290
- _PyFutureFeatures c_future ; /* module's __future__ */
291
- PyCompilerFlags c_flags ;
292
-
293
- int c_optimize ; /* optimization level */
294
- int c_interactive ; /* true if in interactive mode */
295
- PyObject * c_const_cache ; /* Python dict holding all constants,
296
- including names tuple */
297
- struct compiler_unit * u ; /* compiler state for current block */
298
- PyObject * c_stack ; /* Python list holding compiler_unit ptrs */
299
- PyArena * c_arena ; /* pointer to memory allocation arena */
300
-
301
- bool c_save_nested_seqs ; /* if true, construct recursive instruction sequences
302
- * (including instructions for nested code objects)
303
- */
304
- };
305
-
306
-
307
255
typedef struct {
308
256
// A list of strings corresponding to name captures. It is used to track:
309
257
// - Repeated name assignments in the same pattern.
@@ -917,6 +865,21 @@ codegen_unwind_fblock_stack(struct compiler *c, location *ploc,
917
865
return SUCCESS ;
918
866
}
919
867
868
+ static int
869
+ codegen_enter_scope (struct compiler * c , identifier name , int scope_type ,
870
+ void * key , int lineno , PyObject * private ,
871
+ _PyCompile_CodeUnitMetadata * umd )
872
+ {
873
+ RETURN_IF_ERROR (
874
+ compiler_enter_scope (c , name , scope_type , key , lineno , private , umd ));
875
+ location loc = LOCATION (lineno , lineno , 0 , 0 );
876
+ if (scope_type == COMPILER_SCOPE_MODULE ) {
877
+ loc .lineno = 0 ;
878
+ }
879
+ ADDOP_I (c , loc , RESUME , RESUME_AT_FUNC_START );
880
+ return SUCCESS ;
881
+ }
882
+
920
883
static int
921
884
codegen_setup_annotations_scope (struct compiler * c , location loc ,
922
885
void * key , PyObject * name )
@@ -925,8 +888,8 @@ codegen_setup_annotations_scope(struct compiler *c, location loc,
925
888
.u_posonlyargcount = 1 ,
926
889
};
927
890
RETURN_IF_ERROR (
928
- compiler_enter_scope (c , name , COMPILER_SCOPE_ANNOTATIONS ,
929
- key , loc .lineno , NULL , & umd ));
891
+ codegen_enter_scope (c , name , COMPILER_SCOPE_ANNOTATIONS ,
892
+ key , loc .lineno , NULL , & umd ));
930
893
931
894
// if .format != 1: raise NotImplementedError
932
895
_Py_DECLARE_STR (format , ".format" );
@@ -1006,6 +969,14 @@ codegen_process_deferred_annotations(struct compiler *c, location loc)
1006
969
return SUCCESS ;
1007
970
}
1008
971
972
+ /* Compile an expression */
973
+ static int
974
+ codegen_expression (struct compiler * c , expr_ty e )
975
+ {
976
+ VISIT (c , expr , e );
977
+ return SUCCESS ;
978
+ }
979
+
1009
980
/* Compile a sequence of statements, checking for a docstring
1010
981
and for annotations. */
1011
982
@@ -1073,8 +1044,8 @@ codegen_enter_anonymous_scope(struct compiler* c, mod_ty mod)
1073
1044
{
1074
1045
_Py_DECLARE_STR (anon_module , "<module>" );
1075
1046
RETURN_IF_ERROR (
1076
- compiler_enter_scope (c , & _Py_STR (anon_module ), COMPILER_SCOPE_MODULE ,
1077
- mod , 1 , NULL , NULL ));
1047
+ codegen_enter_scope (c , & _Py_STR (anon_module ), COMPILER_SCOPE_MODULE ,
1048
+ mod , 1 , NULL , NULL ));
1078
1049
return SUCCESS ;
1079
1050
}
1080
1051
@@ -1504,7 +1475,7 @@ codegen_function_body(struct compiler *c, stmt_ty s, int is_async, Py_ssize_t fu
1504
1475
.u_kwonlyargcount = asdl_seq_LEN (args -> kwonlyargs ),
1505
1476
};
1506
1477
RETURN_IF_ERROR (
1507
- compiler_enter_scope (c , name , scope_type , (void * )s , firstlineno , NULL , & umd ));
1478
+ codegen_enter_scope (c , name , scope_type , (void * )s , firstlineno , NULL , & umd ));
1508
1479
1509
1480
Py_ssize_t first_instr = 0 ;
1510
1481
PyObject * docstring = _PyAST_GetDocString (body );
@@ -1617,8 +1588,8 @@ codegen_function(struct compiler *c, stmt_ty s, int is_async)
1617
1588
_PyCompile_CodeUnitMetadata umd = {
1618
1589
.u_argcount = num_typeparam_args ,
1619
1590
};
1620
- int ret = compiler_enter_scope (c , type_params_name , COMPILER_SCOPE_ANNOTATIONS ,
1621
- (void * )type_params , firstlineno , NULL , & umd );
1591
+ int ret = codegen_enter_scope (c , type_params_name , COMPILER_SCOPE_ANNOTATIONS ,
1592
+ (void * )type_params , firstlineno , NULL , & umd );
1622
1593
Py_DECREF (type_params_name );
1623
1594
RETURN_IF_ERROR (ret );
1624
1595
RETURN_IF_ERROR_IN_SCOPE (c , codegen_type_params (c , type_params ));
@@ -1696,8 +1667,8 @@ codegen_class_body(struct compiler *c, stmt_ty s, int firstlineno)
1696
1667
1697
1668
/* 1. compile the class body into a code object */
1698
1669
RETURN_IF_ERROR (
1699
- compiler_enter_scope (c , s -> v .ClassDef .name , COMPILER_SCOPE_CLASS ,
1700
- (void * )s , firstlineno , s -> v .ClassDef .name , NULL ));
1670
+ codegen_enter_scope (c , s -> v .ClassDef .name , COMPILER_SCOPE_CLASS ,
1671
+ (void * )s , firstlineno , s -> v .ClassDef .name , NULL ));
1701
1672
1702
1673
location loc = LOCATION (firstlineno , firstlineno , 0 , 0 );
1703
1674
/* load (global) __name__ ... */
@@ -1805,8 +1776,8 @@ codegen_class(struct compiler *c, stmt_ty s)
1805
1776
if (!type_params_name ) {
1806
1777
return ERROR ;
1807
1778
}
1808
- int ret = compiler_enter_scope (c , type_params_name , COMPILER_SCOPE_ANNOTATIONS ,
1809
- (void * )type_params , firstlineno , s -> v .ClassDef .name , NULL );
1779
+ int ret = codegen_enter_scope (c , type_params_name , COMPILER_SCOPE_ANNOTATIONS ,
1780
+ (void * )type_params , firstlineno , s -> v .ClassDef .name , NULL );
1810
1781
Py_DECREF (type_params_name );
1811
1782
RETURN_IF_ERROR (ret );
1812
1783
RETURN_IF_ERROR_IN_SCOPE (c , codegen_type_params (c , type_params ));
@@ -1833,7 +1804,7 @@ codegen_class(struct compiler *c, stmt_ty s)
1833
1804
1834
1805
Py_ssize_t original_len = asdl_seq_LEN (s -> v .ClassDef .bases );
1835
1806
asdl_expr_seq * bases = _Py_asdl_expr_seq_new (
1836
- original_len + 1 , c -> c_arena );
1807
+ original_len + 1 , ARENA ( c ) );
1837
1808
if (bases == NULL ) {
1838
1809
compiler_exit_scope (c );
1839
1810
return ERROR ;
@@ -1843,7 +1814,7 @@ codegen_class(struct compiler *c, stmt_ty s)
1843
1814
}
1844
1815
expr_ty name_node = _PyAST_Name (
1845
1816
& _Py_STR (generic_base ), Load ,
1846
- loc .lineno , loc .col_offset , loc .end_lineno , loc .end_col_offset , c -> c_arena
1817
+ loc .lineno , loc .col_offset , loc .end_lineno , loc .end_col_offset , ARENA ( c )
1847
1818
);
1848
1819
if (name_node == NULL ) {
1849
1820
compiler_exit_scope (c );
@@ -1920,8 +1891,8 @@ codegen_typealias(struct compiler *c, stmt_ty s)
1920
1891
if (!type_params_name ) {
1921
1892
return ERROR ;
1922
1893
}
1923
- int ret = compiler_enter_scope (c , type_params_name , COMPILER_SCOPE_ANNOTATIONS ,
1924
- (void * )type_params , loc .lineno , NULL , NULL );
1894
+ int ret = codegen_enter_scope (c , type_params_name , COMPILER_SCOPE_ANNOTATIONS ,
1895
+ (void * )type_params , loc .lineno , NULL , NULL );
1925
1896
Py_DECREF (type_params_name );
1926
1897
RETURN_IF_ERROR (ret );
1927
1898
ADDOP_LOAD_CONST_IN_SCOPE (c , loc , name );
@@ -2183,8 +2154,8 @@ codegen_lambda(struct compiler *c, expr_ty e)
2183
2154
};
2184
2155
_Py_DECLARE_STR (anon_lambda , "<lambda>" );
2185
2156
RETURN_IF_ERROR (
2186
- compiler_enter_scope (c , & _Py_STR (anon_lambda ), COMPILER_SCOPE_LAMBDA ,
2187
- (void * )e , e -> lineno , NULL , & umd ));
2157
+ codegen_enter_scope (c , & _Py_STR (anon_lambda ), COMPILER_SCOPE_LAMBDA ,
2158
+ (void * )e , e -> lineno , NULL , & umd ));
2188
2159
2189
2160
/* Make None the first constant, so the lambda can't have a
2190
2161
docstring. */
@@ -4765,7 +4736,6 @@ static int
4765
4736
pop_inlined_comprehension_state (struct compiler * c , location loc ,
4766
4737
inlined_comprehension_state * state )
4767
4738
{
4768
- c -> u -> u_in_inlined_comp -- ;
4769
4739
RETURN_IF_ERROR (codegen_pop_inlined_comprehension_locals (c , loc , state ));
4770
4740
RETURN_IF_ERROR (compiler_revert_inlined_comprehension_scopes (c , loc , state ));
4771
4741
return SUCCESS ;
@@ -4819,8 +4789,8 @@ codegen_comprehension(struct compiler *c, expr_ty e, int type,
4819
4789
_PyCompile_CodeUnitMetadata umd = {
4820
4790
.u_argcount = 1 ,
4821
4791
};
4822
- if (compiler_enter_scope (c , name , COMPILER_SCOPE_COMPREHENSION ,
4823
- (void * )e , e -> lineno , NULL , & umd ) < 0 ) {
4792
+ if (codegen_enter_scope (c , name , COMPILER_SCOPE_COMPREHENSION ,
4793
+ (void * )e , e -> lineno , NULL , & umd ) < 0 ) {
4824
4794
goto error ;
4825
4795
}
4826
4796
}
@@ -6442,8 +6412,84 @@ codegen_add_return_at_end(struct compiler *c, int addNone)
6442
6412
return SUCCESS ;
6443
6413
}
6444
6414
6415
+ #undef ADDOP_I
6416
+ #undef ADDOP_I_IN_SCOPE
6417
+ #undef ADDOP
6418
+ #undef ADDOP_IN_SCOPE
6419
+ #undef ADDOP_LOAD_CONST
6420
+ #undef ADDOP_LOAD_CONST_IN_SCOPE
6421
+ #undef ADDOP_LOAD_CONST_NEW
6422
+ #undef ADDOP_N
6423
+ #undef ADDOP_N_IN_SCOPE
6424
+ #undef ADDOP_NAME
6425
+ #undef ADDOP_JUMP
6426
+ #undef ADDOP_COMPARE
6427
+ #undef ADDOP_BINARY
6428
+ #undef ADDOP_INPLACE
6429
+ #undef ADD_YIELD_FROM
6430
+ #undef POP_EXCEPT_AND_RERAISE
6431
+ #undef ADDOP_YIELD
6432
+ #undef VISIT
6433
+ #undef VISIT_IN_SCOPE
6434
+ #undef VISIT_SEQ
6435
+ #undef VISIT_SEQ_IN_SCOPE
6436
+
6445
6437
/*** end of CODEGEN, start of compiler implementation ***/
6446
6438
6439
+ /* The following items change on entry and exit of code blocks.
6440
+ They must be saved and restored when returning to a block.
6441
+ */
6442
+ struct compiler_unit {
6443
+ PySTEntryObject * u_ste ;
6444
+
6445
+ int u_scope_type ;
6446
+
6447
+ PyObject * u_private ; /* for private name mangling */
6448
+ PyObject * u_static_attributes ; /* for class: attributes accessed via self.X */
6449
+ PyObject * u_deferred_annotations ; /* AnnAssign nodes deferred to the end of compilation */
6450
+
6451
+ instr_sequence * u_instr_sequence ; /* codegen output */
6452
+
6453
+ int u_nfblocks ;
6454
+ int u_in_inlined_comp ;
6455
+
6456
+ struct fblockinfo u_fblock [CO_MAXBLOCKS ];
6457
+
6458
+ _PyCompile_CodeUnitMetadata u_metadata ;
6459
+ };
6460
+
6461
+ /* This struct captures the global state of a compilation.
6462
+
6463
+ The u pointer points to the current compilation unit, while units
6464
+ for enclosing blocks are stored in c_stack. The u and c_stack are
6465
+ managed by compiler_enter_scope() and compiler_exit_scope().
6466
+
6467
+ Note that we don't track recursion levels during compilation - the
6468
+ task of detecting and rejecting excessive levels of nesting is
6469
+ handled by the symbol analysis pass.
6470
+
6471
+ */
6472
+
6473
+ struct compiler {
6474
+ PyObject * c_filename ;
6475
+ struct symtable * c_st ;
6476
+ _PyFutureFeatures c_future ; /* module's __future__ */
6477
+ PyCompilerFlags c_flags ;
6478
+
6479
+ int c_optimize ; /* optimization level */
6480
+ int c_interactive ; /* true if in interactive mode */
6481
+ PyObject * c_const_cache ; /* Python dict holding all constants,
6482
+ including names tuple */
6483
+ struct compiler_unit * u ; /* compiler state for current block */
6484
+ PyObject * c_stack ; /* Python list holding compiler_unit ptrs */
6485
+ PyArena * c_arena ; /* pointer to memory allocation arena */
6486
+
6487
+ bool c_save_nested_seqs ; /* if true, construct recursive instruction sequences
6488
+ * (including instructions for nested code objects)
6489
+ */
6490
+ };
6491
+
6492
+
6447
6493
static int
6448
6494
compiler_setup (struct compiler * c , mod_ty mod , PyObject * filename ,
6449
6495
PyCompilerFlags * flags , int optimize , PyArena * arena )
@@ -6801,10 +6847,7 @@ compiler_enter_scope(struct compiler *c, identifier name, int scope_type,
6801
6847
void * key , int lineno , PyObject * private ,
6802
6848
_PyCompile_CodeUnitMetadata * umd )
6803
6849
{
6804
- location loc = LOCATION (lineno , lineno , 0 , 0 );
6805
-
6806
6850
struct compiler_unit * u ;
6807
-
6808
6851
u = (struct compiler_unit * )PyMem_Calloc (1 , sizeof (struct compiler_unit ));
6809
6852
if (!u ) {
6810
6853
PyErr_NoMemory ();
@@ -6918,15 +6961,9 @@ compiler_enter_scope(struct compiler *c, identifier name, int scope_type,
6918
6961
u -> u_private = Py_XNewRef (private );
6919
6962
6920
6963
c -> u = u ;
6921
-
6922
- if (u -> u_scope_type == COMPILER_SCOPE_MODULE ) {
6923
- loc .lineno = 0 ;
6924
- }
6925
- else {
6964
+ if (scope_type != COMPILER_SCOPE_MODULE ) {
6926
6965
RETURN_IF_ERROR (compiler_set_qualname (c ));
6927
6966
}
6928
- ADDOP_I (c , loc , RESUME , RESUME_AT_FUNC_START );
6929
-
6930
6967
return SUCCESS ;
6931
6968
}
6932
6969
@@ -7032,7 +7069,7 @@ compiler_codegen(struct compiler *c, mod_ty mod)
7032
7069
break ;
7033
7070
}
7034
7071
case Expression_kind : {
7035
- VISIT ( c , expr , mod -> v .Expression .body );
7072
+ RETURN_IF_ERROR ( codegen_expression ( c , mod -> v .Expression .body ) );
7036
7073
break ;
7037
7074
}
7038
7075
default : {
@@ -7283,6 +7320,7 @@ static int
7283
7320
compiler_revert_inlined_comprehension_scopes (struct compiler * c , location loc ,
7284
7321
inlined_comprehension_state * state )
7285
7322
{
7323
+ c -> u -> u_in_inlined_comp -- ;
7286
7324
if (state -> temp_symbols ) {
7287
7325
PyObject * k , * v ;
7288
7326
Py_ssize_t pos = 0 ;
@@ -7478,6 +7516,12 @@ compiler_unit_metadata(struct compiler *c)
7478
7516
return & c -> u -> u_metadata ;
7479
7517
}
7480
7518
7519
+ static PyArena *
7520
+ compiler_arena (struct compiler * c )
7521
+ {
7522
+ return c -> c_arena ;
7523
+ }
7524
+
7481
7525
#ifndef NDEBUG
7482
7526
static int
7483
7527
compiler_is_top_level_await (struct compiler * c )
0 commit comments