1- /* AST Optimizer */
1+ /* AST pre-processing */
22#include "Python.h"
33#include "pycore_ast.h" // _PyAST_GetDocString()
44#include "pycore_c_array.h" // _Py_CArray_EnsureCapacity()
@@ -22,7 +22,7 @@ typedef struct {
2222
2323 _Py_c_array_t cf_finally ; /* context for PEP 765 check */
2424 int cf_finally_used ;
25- } _PyASTOptimizeState ;
25+ } _PyASTPreprocessState ;
2626
2727#define ENTER_RECURSIVE () \
2828if (Py_EnterRecursiveCall(" during compilation")) { \
@@ -32,14 +32,14 @@ if (Py_EnterRecursiveCall(" during compilation")) { \
3232#define LEAVE_RECURSIVE () Py_LeaveRecursiveCall();
3333
3434static ControlFlowInFinallyContext *
35- get_cf_finally_top (_PyASTOptimizeState * state )
35+ get_cf_finally_top (_PyASTPreprocessState * state )
3636{
3737 int idx = state -> cf_finally_used ;
3838 return ((ControlFlowInFinallyContext * )state -> cf_finally .array ) + idx ;
3939}
4040
4141static 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 )
4343{
4444 if (_Py_CArray_EnsureCapacity (& state -> cf_finally , state -> cf_finally_used + 1 ) < 0 ) {
4545 return 0 ;
@@ -55,14 +55,14 @@ push_cf_context(_PyASTOptimizeState *state, stmt_ty node, bool finally, bool fun
5555}
5656
5757static void
58- pop_cf_context (_PyASTOptimizeState * state )
58+ pop_cf_context (_PyASTPreprocessState * state )
5959{
6060 assert (state -> cf_finally_used > 0 );
6161 state -> cf_finally_used -- ;
6262}
6363
6464static 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 )
6666{
6767 PyObject * msg = PyUnicode_FromFormat ("'%s' in a 'finally' block" , kw );
6868 if (msg == NULL ) {
@@ -76,7 +76,7 @@ control_flow_in_finally_warning(const char *kw, stmt_ty n, _PyASTOptimizeState *
7676}
7777
7878static int
79- before_return (_PyASTOptimizeState * state , stmt_ty node_ )
79+ before_return (_PyASTPreprocessState * state , stmt_ty node_ )
8080{
8181 if (state -> cf_finally_used > 0 ) {
8282 ControlFlowInFinallyContext * ctx = get_cf_finally_top (state );
@@ -90,7 +90,7 @@ before_return(_PyASTOptimizeState *state, stmt_ty node_)
9090}
9191
9292static 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 )
9494{
9595 if (state -> cf_finally_used > 0 ) {
9696 ControlFlowInFinallyContext * ctx = get_cf_finally_top (state );
@@ -365,7 +365,7 @@ optimize_format(expr_ty node, PyObject *fmt, asdl_expr_seq *elts, PyArena *arena
365365}
366366
367367static int
368- fold_binop (expr_ty node , PyArena * arena , _PyASTOptimizeState * state )
368+ fold_binop (expr_ty node , PyArena * arena , _PyASTPreprocessState * state )
369369{
370370 if (state -> syntax_check_only ) {
371371 return 1 ;
@@ -389,18 +389,18 @@ fold_binop(expr_ty node, PyArena *arena, _PyASTOptimizeState *state)
389389 return 1 ;
390390}
391391
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 );
404404
405405#define CALL (FUNC , TYPE , ARG ) \
406406 if (!FUNC((ARG), ctx_, state)) \
@@ -436,7 +436,7 @@ stmt_seq_remove_item(asdl_stmt_seq *stmts, Py_ssize_t idx)
436436}
437437
438438static int
439- astfold_body (asdl_stmt_seq * stmts , PyArena * ctx_ , _PyASTOptimizeState * state )
439+ astfold_body (asdl_stmt_seq * stmts , PyArena * ctx_ , _PyASTPreprocessState * state )
440440{
441441 int docstring = _PyAST_GetDocString (stmts ) != NULL ;
442442 if (docstring && (state -> optimize >= 2 )) {
@@ -466,7 +466,7 @@ astfold_body(asdl_stmt_seq *stmts, PyArena *ctx_, _PyASTOptimizeState *state)
466466}
467467
468468static int
469- astfold_mod (mod_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
469+ astfold_mod (mod_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state )
470470{
471471 switch (node_ -> kind ) {
472472 case Module_kind :
@@ -488,7 +488,7 @@ astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
488488}
489489
490490static int
491- astfold_expr (expr_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
491+ astfold_expr (expr_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state )
492492{
493493 ENTER_RECURSIVE ();
494494 switch (node_ -> kind ) {
@@ -613,14 +613,14 @@ astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
613613}
614614
615615static int
616- astfold_keyword (keyword_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
616+ astfold_keyword (keyword_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state )
617617{
618618 CALL (astfold_expr , expr_ty , node_ -> value );
619619 return 1 ;
620620}
621621
622622static int
623- astfold_comprehension (comprehension_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
623+ astfold_comprehension (comprehension_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state )
624624{
625625 CALL (astfold_expr , expr_ty , node_ -> target );
626626 CALL (astfold_expr , expr_ty , node_ -> iter );
@@ -629,7 +629,7 @@ astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTOptimizeState
629629}
630630
631631static int
632- astfold_arguments (arguments_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
632+ astfold_arguments (arguments_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state )
633633{
634634 CALL_SEQ (astfold_arg , arg , node_ -> posonlyargs );
635635 CALL_SEQ (astfold_arg , arg , node_ -> args );
@@ -642,7 +642,7 @@ astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
642642}
643643
644644static int
645- astfold_arg (arg_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
645+ astfold_arg (arg_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state )
646646{
647647 if (!(state -> ff_features & CO_FUTURE_ANNOTATIONS )) {
648648 CALL_OPT (astfold_expr , expr_ty , node_ -> annotation );
@@ -651,7 +651,7 @@ astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
651651}
652652
653653static int
654- astfold_stmt (stmt_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
654+ astfold_stmt (stmt_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state )
655655{
656656 ENTER_RECURSIVE ();
657657 switch (node_ -> kind ) {
@@ -806,7 +806,7 @@ astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
806806}
807807
808808static int
809- astfold_excepthandler (excepthandler_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
809+ astfold_excepthandler (excepthandler_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state )
810810{
811811 switch (node_ -> kind ) {
812812 case ExceptHandler_kind :
@@ -820,15 +820,15 @@ astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTOptimizeState
820820}
821821
822822static int
823- astfold_withitem (withitem_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
823+ astfold_withitem (withitem_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state )
824824{
825825 CALL (astfold_expr , expr_ty , node_ -> context_expr );
826826 CALL_OPT (astfold_expr , expr_ty , node_ -> optional_vars );
827827 return 1 ;
828828}
829829
830830static 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 )
832832{
833833 if (state -> syntax_check_only ) {
834834 return 1 ;
@@ -869,7 +869,7 @@ fold_const_match_patterns(expr_ty node, PyArena *ctx_, _PyASTOptimizeState *stat
869869}
870870
871871static int
872- astfold_pattern (pattern_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
872+ astfold_pattern (pattern_ty node_ , PyArena * ctx_ , _PyASTPreprocessState * state )
873873{
874874 // Currently, this is really only used to form complex/negative numeric
875875 // constants in MatchValue and MatchMapping nodes
@@ -911,7 +911,7 @@ astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
911911}
912912
913913static 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 )
915915{
916916 CALL (astfold_pattern , expr_ty , node_ -> pattern );
917917 CALL_OPT (astfold_expr , expr_ty , node_ -> guard );
@@ -920,7 +920,7 @@ astfold_match_case(match_case_ty node_, PyArena *ctx_, _PyASTOptimizeState *stat
920920}
921921
922922static 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 )
924924{
925925 switch (node_ -> kind ) {
926926 case TypeVar_kind :
@@ -942,11 +942,11 @@ astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTOptimizeState *stat
942942#undef CALL_SEQ
943943
944944int
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 )
947947{
948- _PyASTOptimizeState state ;
949- memset (& state , 0 , sizeof (_PyASTOptimizeState ));
948+ _PyASTPreprocessState state ;
949+ memset (& state , 0 , sizeof (_PyASTPreprocessState ));
950950 state .filename = filename ;
951951 state .optimize = optimize ;
952952 state .ff_features = ff_features ;
0 commit comments