Skip to content
Prev Previous commit
Next Next commit
_PyASTProcessState -> _PyASTPreprocessState
  • Loading branch information
WolframAlph committed May 4, 2025
commit 72cca4fc50672a2cb42cf88a23f6c17cfba0a4d0
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Move constant folding to the peephole optimizer. Rename AST optimization
Comment thread
AA-Turner marked this conversation as resolved.
related files (Python/ast_opt.c -> Python/ast_preprocess.c), structs (_PyASTOptimizeState -> _PyASTProcessState)
related files (Python/ast_opt.c -> Python/ast_preprocess.c), structs (_PyASTOptimizeState -> _PyASTPreprocessState)
and functions (_PyAST_Optimize -> _PyAST_Preprocess, _PyCompile_AstOptimize -> _PyCompile_AstPreprocess).
72 changes: 36 additions & 36 deletions Python/ast_preprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ typedef struct {

_Py_c_array_t cf_finally; /* context for PEP 765 check */
int cf_finally_used;
} _PyASTProcessState;
} _PyASTPreprocessState;

#define ENTER_RECURSIVE() \
if (Py_EnterRecursiveCall(" during compilation")) { \
Expand All @@ -31,14 +31,14 @@ if (Py_EnterRecursiveCall(" during compilation")) { \
#define LEAVE_RECURSIVE() Py_LeaveRecursiveCall();

static ControlFlowInFinallyContext*
get_cf_finally_top(_PyASTProcessState *state)
get_cf_finally_top(_PyASTPreprocessState *state)
{
int idx = state->cf_finally_used;
return ((ControlFlowInFinallyContext*)state->cf_finally.array) + idx;
}

static int
push_cf_context(_PyASTProcessState *state, stmt_ty node, bool finally, bool funcdef, bool loop)
push_cf_context(_PyASTPreprocessState *state, stmt_ty node, bool finally, bool funcdef, bool loop)
{
if (_Py_CArray_EnsureCapacity(&state->cf_finally, state->cf_finally_used+1) < 0) {
return 0;
Expand All @@ -54,14 +54,14 @@ push_cf_context(_PyASTProcessState *state, stmt_ty node, bool finally, bool func
}

static void
pop_cf_context(_PyASTProcessState *state)
pop_cf_context(_PyASTPreprocessState *state)
{
assert(state->cf_finally_used > 0);
state->cf_finally_used--;
}

static int
control_flow_in_finally_warning(const char *kw, stmt_ty n, _PyASTProcessState *state)
control_flow_in_finally_warning(const char *kw, stmt_ty n, _PyASTPreprocessState *state)
{
PyObject *msg = PyUnicode_FromFormat("'%s' in a 'finally' block", kw);
if (msg == NULL) {
Expand All @@ -75,7 +75,7 @@ control_flow_in_finally_warning(const char *kw, stmt_ty n, _PyASTProcessState *s
}

static int
before_return(_PyASTProcessState *state, stmt_ty node_)
before_return(_PyASTPreprocessState *state, stmt_ty node_)
{
if (state->cf_finally_used > 0) {
ControlFlowInFinallyContext *ctx = get_cf_finally_top(state);
Expand All @@ -89,7 +89,7 @@ before_return(_PyASTProcessState *state, stmt_ty node_)
}

static int
before_loop_exit(_PyASTProcessState *state, stmt_ty node_, const char *kw)
before_loop_exit(_PyASTPreprocessState *state, stmt_ty node_, const char *kw)
{
if (state->cf_finally_used > 0) {
ControlFlowInFinallyContext *ctx = get_cf_finally_top(state);
Expand Down Expand Up @@ -364,7 +364,7 @@ optimize_format(expr_ty node, PyObject *fmt, asdl_expr_seq *elts, PyArena *arena
}

static int
fold_binop(expr_ty node, PyArena *arena, _PyASTProcessState *state)
fold_binop(expr_ty node, PyArena *arena, _PyASTPreprocessState *state)
{
if (state->syntax_check_only) {
return 1;
Expand All @@ -388,18 +388,18 @@ fold_binop(expr_ty node, PyArena *arena, _PyASTProcessState *state)
return 1;
}

static int astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTProcessState *state);
static int astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTProcessState *state);
static int astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTProcessState *state);
static int astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTProcessState *state);
static int astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTProcessState *state);
static int astfold_keyword(keyword_ty node_, PyArena *ctx_, _PyASTProcessState *state);
static int astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTProcessState *state);
static int astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTProcessState *state);
static int astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTProcessState *state);
static int astfold_match_case(match_case_ty node_, PyArena *ctx_, _PyASTProcessState *state);
static int astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTProcessState *state);
static int astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTProcessState *state);
static int astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
static int astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
static int astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
static int astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
static int astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
static int astfold_keyword(keyword_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
static int astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
static int astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
static int astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
static int astfold_match_case(match_case_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
static int astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
static int astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);

#define CALL(FUNC, TYPE, ARG) \
if (!FUNC((ARG), ctx_, state)) \
Expand Down Expand Up @@ -435,7 +435,7 @@ stmt_seq_remove_item(asdl_stmt_seq *stmts, Py_ssize_t idx)
}

static int
astfold_body(asdl_stmt_seq *stmts, PyArena *ctx_, _PyASTProcessState *state)
astfold_body(asdl_stmt_seq *stmts, PyArena *ctx_, _PyASTPreprocessState *state)
{
int docstring = _PyAST_GetDocString(stmts) != NULL;
if (docstring && (state->optimize >= 2)) {
Expand Down Expand Up @@ -465,7 +465,7 @@ astfold_body(asdl_stmt_seq *stmts, PyArena *ctx_, _PyASTProcessState *state)
}

static int
astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTProcessState *state)
astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
{
switch (node_->kind) {
case Module_kind:
Expand All @@ -487,7 +487,7 @@ astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTProcessState *state)
}

static int
astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTProcessState *state)
astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
{
ENTER_RECURSIVE();
switch (node_->kind) {
Expand Down Expand Up @@ -612,14 +612,14 @@ astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTProcessState *state)
}

static int
astfold_keyword(keyword_ty node_, PyArena *ctx_, _PyASTProcessState *state)
astfold_keyword(keyword_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
{
CALL(astfold_expr, expr_ty, node_->value);
return 1;
}

static int
astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTProcessState *state)
astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
{
CALL(astfold_expr, expr_ty, node_->target);
CALL(astfold_expr, expr_ty, node_->iter);
Expand All @@ -628,7 +628,7 @@ astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTProcessState
}

static int
astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTProcessState *state)
astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
{
CALL_SEQ(astfold_arg, arg, node_->posonlyargs);
CALL_SEQ(astfold_arg, arg, node_->args);
Expand All @@ -641,7 +641,7 @@ astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTProcessState *state)
}

static int
astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTProcessState *state)
astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
{
if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) {
CALL_OPT(astfold_expr, expr_ty, node_->annotation);
Expand All @@ -650,7 +650,7 @@ astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTProcessState *state)
}

static int
astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTProcessState *state)
astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
{
ENTER_RECURSIVE();
switch (node_->kind) {
Expand Down Expand Up @@ -805,7 +805,7 @@ astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTProcessState *state)
}

static int
astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTProcessState *state)
astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
{
switch (node_->kind) {
case ExceptHandler_kind:
Expand All @@ -819,15 +819,15 @@ astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTProcessState
}

static int
astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTProcessState *state)
astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
{
CALL(astfold_expr, expr_ty, node_->context_expr);
CALL_OPT(astfold_expr, expr_ty, node_->optional_vars);
return 1;
}

static int
fold_const_match_patterns(expr_ty node, PyArena *ctx_, _PyASTProcessState *state)
fold_const_match_patterns(expr_ty node, PyArena *ctx_, _PyASTPreprocessState *state)
{
if (state->syntax_check_only) {
return 1;
Expand Down Expand Up @@ -868,7 +868,7 @@ fold_const_match_patterns(expr_ty node, PyArena *ctx_, _PyASTProcessState *state
}

static int
astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTProcessState *state)
astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
{
// Currently, this is really only used to form complex/negative numeric
// constants in MatchValue and MatchMapping nodes
Expand Down Expand Up @@ -910,7 +910,7 @@ astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTProcessState *state)
}

static int
astfold_match_case(match_case_ty node_, PyArena *ctx_, _PyASTProcessState *state)
astfold_match_case(match_case_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
{
CALL(astfold_pattern, expr_ty, node_->pattern);
CALL_OPT(astfold_expr, expr_ty, node_->guard);
Expand All @@ -919,7 +919,7 @@ astfold_match_case(match_case_ty node_, PyArena *ctx_, _PyASTProcessState *state
}

static int
astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTProcessState *state)
astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
{
switch (node_->kind) {
case TypeVar_kind:
Expand All @@ -944,8 +944,8 @@ int
_PyAST_Preprocess(mod_ty mod, PyArena *arena, PyObject *filename, int optimize,
int ff_features, int syntax_check_only)
{
_PyASTProcessState state;
memset(&state, 0, sizeof(_PyASTProcessState));
_PyASTPreprocessState state;
memset(&state, 0, sizeof(_PyASTPreprocessState));
state.filename = filename;
state.optimize = optimize;
state.ff_features = ff_features;
Expand Down
Loading