1
- /* AST Optimizer */
1
+ /* AST pre-processing */
2
2
#include "Python.h"
3
3
#include "pycore_ast.h" // _PyAST_GetDocString()
4
4
#include "pycore_c_array.h" // _Py_CArray_EnsureCapacity()
@@ -22,7 +22,7 @@ typedef struct {
22
22
23
23
_Py_c_array_t cf_finally ; /* context for PEP 765 check */
24
24
int cf_finally_used ;
25
- } _PyASTOptimizeState ;
25
+ } _PyASTPreprocessState ;
26
26
27
27
#define ENTER_RECURSIVE () \
28
28
if (Py_EnterRecursiveCall(" during compilation")) { \
@@ -32,14 +32,14 @@ if (Py_EnterRecursiveCall(" during compilation")) { \
32
32
#define LEAVE_RECURSIVE () Py_LeaveRecursiveCall();
33
33
34
34
static ControlFlowInFinallyContext *
35
- get_cf_finally_top (_PyASTOptimizeState * state )
35
+ get_cf_finally_top (_PyASTPreprocessState * state )
36
36
{
37
37
int idx = state -> cf_finally_used ;
38
38
return ((ControlFlowInFinallyContext * )state -> cf_finally .array ) + idx ;
39
39
}
40
40
41
41
static int
42
- push_cf_context (_PyASTOptimizeState * state , stmt_ty node , bool finally , bool funcdef , bool loop )
42
+ push_cf_context (_PyASTPreprocessState * state , stmt_ty node , bool finally , bool funcdef , bool loop )
43
43
{
44
44
if (_Py_CArray_EnsureCapacity (& state -> cf_finally , state -> cf_finally_used + 1 ) < 0 ) {
45
45
return 0 ;
@@ -55,14 +55,14 @@ push_cf_context(_PyASTOptimizeState *state, stmt_ty node, bool finally, bool fun
55
55
}
56
56
57
57
static void
58
- pop_cf_context (_PyASTOptimizeState * state )
58
+ pop_cf_context (_PyASTPreprocessState * state )
59
59
{
60
60
assert (state -> cf_finally_used > 0 );
61
61
state -> cf_finally_used -- ;
62
62
}
63
63
64
64
static int
65
- control_flow_in_finally_warning (const char * kw , stmt_ty n , _PyASTOptimizeState * state )
65
+ control_flow_in_finally_warning (const char * kw , stmt_ty n , _PyASTPreprocessState * state )
66
66
{
67
67
PyObject * msg = PyUnicode_FromFormat ("'%s' in a 'finally' block" , kw );
68
68
if (msg == NULL ) {
@@ -76,7 +76,7 @@ control_flow_in_finally_warning(const char *kw, stmt_ty n, _PyASTOptimizeState *
76
76
}
77
77
78
78
static int
79
- before_return (_PyASTOptimizeState * state , stmt_ty node_ )
79
+ before_return (_PyASTPreprocessState * state , stmt_ty node_ )
80
80
{
81
81
if (state -> cf_finally_used > 0 ) {
82
82
ControlFlowInFinallyContext * ctx = get_cf_finally_top (state );
@@ -90,7 +90,7 @@ before_return(_PyASTOptimizeState *state, stmt_ty node_)
90
90
}
91
91
92
92
static int
93
- before_loop_exit (_PyASTOptimizeState * state , stmt_ty node_ , const char * kw )
93
+ before_loop_exit (_PyASTPreprocessState * state , stmt_ty node_ , const char * kw )
94
94
{
95
95
if (state -> cf_finally_used > 0 ) {
96
96
ControlFlowInFinallyContext * ctx = get_cf_finally_top (state );
@@ -365,7 +365,7 @@ optimize_format(expr_ty node, PyObject *fmt, asdl_expr_seq *elts, PyArena *arena
365
365
}
366
366
367
367
static int
368
- fold_binop (expr_ty node , PyArena * arena , _PyASTOptimizeState * state )
368
+ fold_binop (expr_ty node , PyArena * arena , _PyASTPreprocessState * state )
369
369
{
370
370
if (state -> syntax_check_only ) {
371
371
return 1 ;
@@ -389,18 +389,18 @@ fold_binop(expr_ty node, PyArena *arena, _PyASTOptimizeState *state)
389
389
return 1 ;
390
390
}
391
391
392
- static int astfold_mod (mod_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state );
393
- static int astfold_stmt (stmt_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state );
394
- static int astfold_expr (expr_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state );
395
- static int astfold_arguments (arguments_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state );
396
- static int astfold_comprehension (comprehension_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state );
397
- static int astfold_keyword (keyword_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state );
398
- static int astfold_arg (arg_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state );
399
- static int astfold_withitem (withitem_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state );
400
- static int astfold_excepthandler (excepthandler_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state );
401
- static int astfold_match_case (match_case_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state );
402
- static int astfold_pattern (pattern_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state );
403
- static int astfold_type_param (type_param_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state );
392
+ static int astfold_mod (mod_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state );
393
+ static int astfold_stmt (stmt_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state );
394
+ static int astfold_expr (expr_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state );
395
+ static int astfold_arguments (arguments_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state );
396
+ static int astfold_comprehension (comprehension_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state );
397
+ static int astfold_keyword (keyword_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state );
398
+ static int astfold_arg (arg_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state );
399
+ static int astfold_withitem (withitem_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state );
400
+ static int astfold_excepthandler (excepthandler_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state );
401
+ static int astfold_match_case (match_case_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state );
402
+ static int astfold_pattern (pattern_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state );
403
+ static int astfold_type_param (type_param_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state );
404
404
405
405
#define CALL (FUNC , TYPE , ARG ) \
406
406
if (!FUNC((ARG), ctx_, state)) \
@@ -436,7 +436,7 @@ stmt_seq_remove_item(asdl_stmt_seq *stmts, Py_ssize_t idx)
436
436
}
437
437
438
438
static int
439
- astfold_body (asdl_stmt_seq * stmts , PyArena * ctx_ , _PyASTOptimizeState * state )
439
+ astfold_body (asdl_stmt_seq * stmts , PyArena * ctx_ , _PyASTPreprocessState * state )
440
440
{
441
441
int docstring = _PyAST_GetDocString (stmts ) != NULL ;
442
442
if (docstring && (state -> optimize >= 2 )) {
@@ -466,7 +466,7 @@ astfold_body(asdl_stmt_seq *stmts, PyArena *ctx_, _PyASTOptimizeState *state)
466
466
}
467
467
468
468
static int
469
- astfold_mod (mod_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
469
+ astfold_mod (mod_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state )
470
470
{
471
471
switch (node_ -> kind ) {
472
472
case Module_kind :
@@ -488,7 +488,7 @@ astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
488
488
}
489
489
490
490
static int
491
- astfold_expr (expr_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
491
+ astfold_expr (expr_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state )
492
492
{
493
493
ENTER_RECURSIVE ();
494
494
switch (node_ -> kind ) {
@@ -613,14 +613,14 @@ astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
613
613
}
614
614
615
615
static int
616
- astfold_keyword (keyword_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
616
+ astfold_keyword (keyword_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state )
617
617
{
618
618
CALL (astfold_expr , expr_ty , node_ -> value );
619
619
return 1 ;
620
620
}
621
621
622
622
static int
623
- astfold_comprehension (comprehension_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
623
+ astfold_comprehension (comprehension_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state )
624
624
{
625
625
CALL (astfold_expr , expr_ty , node_ -> target );
626
626
CALL (astfold_expr , expr_ty , node_ -> iter );
@@ -629,7 +629,7 @@ astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTOptimizeState
629
629
}
630
630
631
631
static int
632
- astfold_arguments (arguments_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
632
+ astfold_arguments (arguments_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state )
633
633
{
634
634
CALL_SEQ (astfold_arg , arg , node_ -> posonlyargs );
635
635
CALL_SEQ (astfold_arg , arg , node_ -> args );
@@ -642,7 +642,7 @@ astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
642
642
}
643
643
644
644
static int
645
- astfold_arg (arg_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
645
+ astfold_arg (arg_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state )
646
646
{
647
647
if (!(state -> ff_features & CO_FUTURE_ANNOTATIONS )) {
648
648
CALL_OPT (astfold_expr , expr_ty , node_ -> annotation );
@@ -651,7 +651,7 @@ astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
651
651
}
652
652
653
653
static int
654
- astfold_stmt (stmt_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
654
+ astfold_stmt (stmt_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state )
655
655
{
656
656
ENTER_RECURSIVE ();
657
657
switch (node_ -> kind ) {
@@ -806,7 +806,7 @@ astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
806
806
}
807
807
808
808
static int
809
- astfold_excepthandler (excepthandler_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
809
+ astfold_excepthandler (excepthandler_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state )
810
810
{
811
811
switch (node_ -> kind ) {
812
812
case ExceptHandler_kind :
@@ -820,15 +820,15 @@ astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTOptimizeState
820
820
}
821
821
822
822
static int
823
- astfold_withitem (withitem_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
823
+ astfold_withitem (withitem_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state )
824
824
{
825
825
CALL (astfold_expr , expr_ty , node_ -> context_expr );
826
826
CALL_OPT (astfold_expr , expr_ty , node_ -> optional_vars );
827
827
return 1 ;
828
828
}
829
829
830
830
static int
831
- fold_const_match_patterns (expr_ty node , PyArena * ctx_ , _PyASTOptimizeState * state )
831
+ fold_const_match_patterns (expr_ty node , PyArena * ctx_ , _PyASTPreprocessState * state )
832
832
{
833
833
if (state -> syntax_check_only ) {
834
834
return 1 ;
@@ -869,7 +869,7 @@ fold_const_match_patterns(expr_ty node, PyArena *ctx_, _PyASTOptimizeState *stat
869
869
}
870
870
871
871
static int
872
- astfold_pattern (pattern_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
872
+ astfold_pattern (pattern_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state )
873
873
{
874
874
// Currently, this is really only used to form complex/negative numeric
875
875
// constants in MatchValue and MatchMapping nodes
@@ -911,7 +911,7 @@ astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
911
911
}
912
912
913
913
static int
914
- astfold_match_case (match_case_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
914
+ astfold_match_case (match_case_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state )
915
915
{
916
916
CALL (astfold_pattern , expr_ty , node_ -> pattern );
917
917
CALL_OPT (astfold_expr , expr_ty , node_ -> guard );
@@ -920,7 +920,7 @@ astfold_match_case(match_case_ty node_, PyArena *ctx_, _PyASTOptimizeState *stat
920
920
}
921
921
922
922
static int
923
- astfold_type_param (type_param_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
923
+ astfold_type_param (type_param_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state )
924
924
{
925
925
switch (node_ -> kind ) {
926
926
case TypeVar_kind :
@@ -942,11 +942,11 @@ astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTOptimizeState *stat
942
942
#undef CALL_SEQ
943
943
944
944
int
945
- _PyAST_Optimize (mod_ty mod , PyArena * arena , PyObject * filename , int optimize ,
946
- int ff_features , int syntax_check_only )
945
+ _PyAST_Preprocess (mod_ty mod , PyArena * arena , PyObject * filename , int optimize ,
946
+ int ff_features , int syntax_check_only )
947
947
{
948
- _PyASTOptimizeState state ;
949
- memset (& state , 0 , sizeof (_PyASTOptimizeState ));
948
+ _PyASTPreprocessState state ;
949
+ memset (& state , 0 , sizeof (_PyASTPreprocessState ));
950
950
state .filename = filename ;
951
951
state .optimize = optimize ;
952
952
state .ff_features = ff_features ;
0 commit comments