bpo-41746: Add type information to asdl_seq objects (GH-22223)

* Add new capability to the PEG parser to type variable assignments. For instance:
```
       | a[asdl_stmt_seq*]=';'.small_stmt+ [';'] NEWLINE { a }
```

* Add new sequence types from the asdl definition (automatically generated)
* Make `asdl_seq` type a generic aliasing pointer type.
* Create a new `asdl_generic_seq` for the generic case using `void*`.
* The old `asdl_seq_GET`/`ast_seq_SET` macros now are typed.
* New `asdl_seq_GET_UNTYPED`/`ast_seq_SET_UNTYPED` macros for dealing with generic sequences.
* Changes all possible `asdl_seq` types to use specific versions everywhere.
This commit is contained in:
Pablo Galindo 2020-09-16 19:42:00 +01:00 committed by GitHub
parent 5c1b46d897
commit a5634c4067
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 1338 additions and 1086 deletions

507
Python/Python-ast.c generated

File diff suppressed because it is too large Load diff

View file

@ -1,64 +1,6 @@
#include "Python.h"
#include "asdl.h"
asdl_seq *
_Py_asdl_seq_new(Py_ssize_t size, PyArena *arena)
{
asdl_seq *seq = NULL;
size_t n;
/* check size is sane */
if (size < 0 ||
(size && (((size_t)size - 1) > (SIZE_MAX / sizeof(void *))))) {
PyErr_NoMemory();
return NULL;
}
n = (size ? (sizeof(void *) * (size - 1)) : 0);
/* check if size can be added safely */
if (n > SIZE_MAX - sizeof(asdl_seq)) {
PyErr_NoMemory();
return NULL;
}
n += sizeof(asdl_seq);
seq = (asdl_seq *)PyArena_Malloc(arena, n);
if (!seq) {
PyErr_NoMemory();
return NULL;
}
memset(seq, 0, n);
seq->size = size;
return seq;
}
asdl_int_seq *
_Py_asdl_int_seq_new(Py_ssize_t size, PyArena *arena)
{
asdl_int_seq *seq = NULL;
size_t n;
/* check size is sane */
if (size < 0 ||
(size && (((size_t)size - 1) > (SIZE_MAX / sizeof(void *))))) {
PyErr_NoMemory();
return NULL;
}
n = (size ? (sizeof(void *) * (size - 1)) : 0);
/* check if size can be added safely */
if (n > SIZE_MAX - sizeof(asdl_seq)) {
PyErr_NoMemory();
return NULL;
}
n += sizeof(asdl_seq);
seq = (asdl_int_seq *)PyArena_Malloc(arena, n);
if (!seq) {
PyErr_NoMemory();
return NULL;
}
memset(seq, 0, n);
seq->size = size;
return seq;
}
GENERATE_ASDL_SEQ_CONSTRUCTOR(generic, void*);
GENERATE_ASDL_SEQ_CONSTRUCTOR(identifier, PyObject*);
GENERATE_ASDL_SEQ_CONSTRUCTOR(int, int);

View file

@ -14,9 +14,9 @@
#define MAXLEVEL 200 /* Max parentheses level */
static int validate_stmts(asdl_seq *);
static int validate_exprs(asdl_seq *, expr_context_ty, int);
static int validate_nonempty_seq(asdl_seq *, const char *, const char *);
static int validate_stmts(asdl_stmt_seq *);
static int validate_exprs(asdl_expr_seq*, expr_context_ty, int);
static int _validate_nonempty_seq(asdl_seq *, const char *, const char *);
static int validate_stmt(stmt_ty);
static int validate_expr(expr_ty, expr_context_ty);
@ -40,7 +40,7 @@ validate_name(PyObject *name)
}
static int
validate_comprehension(asdl_seq *gens)
validate_comprehension(asdl_comprehension_seq *gens)
{
Py_ssize_t i;
if (!asdl_seq_LEN(gens)) {
@ -58,7 +58,7 @@ validate_comprehension(asdl_seq *gens)
}
static int
validate_keywords(asdl_seq *keywords)
validate_keywords(asdl_keyword_seq *keywords)
{
Py_ssize_t i;
for (i = 0; i < asdl_seq_LEN(keywords); i++)
@ -68,7 +68,7 @@ validate_keywords(asdl_seq *keywords)
}
static int
validate_args(asdl_seq *args)
validate_args(asdl_arg_seq *args)
{
Py_ssize_t i;
for (i = 0; i < asdl_seq_LEN(args); i++) {
@ -324,23 +324,24 @@ validate_expr(expr_ty exp, expr_context_ty ctx)
}
static int
validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
_validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
{
if (asdl_seq_LEN(seq))
return 1;
PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
return 0;
}
#define validate_nonempty_seq(seq, what, owner) _validate_nonempty_seq((asdl_seq*)seq, what, owner)
static int
validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
validate_assignlist(asdl_expr_seq *targets, expr_context_ty ctx)
{
return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
validate_exprs(targets, ctx, 0);
}
static int
validate_body(asdl_seq *body, const char *owner)
validate_body(asdl_stmt_seq *body, const char *owner)
{
return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
}
@ -488,7 +489,7 @@ validate_stmt(stmt_ty stmt)
}
static int
validate_stmts(asdl_seq *seq)
validate_stmts(asdl_stmt_seq *seq)
{
Py_ssize_t i;
for (i = 0; i < asdl_seq_LEN(seq); i++) {
@ -507,7 +508,7 @@ validate_stmts(asdl_seq *seq)
}
static int
validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
validate_exprs(asdl_expr_seq *exprs, expr_context_ty ctx, int null_ok)
{
Py_ssize_t i;
for (i = 0; i < asdl_seq_LEN(exprs); i++) {
@ -550,7 +551,7 @@ PyAST_Validate(mod_ty mod)
}
PyObject *
_PyAST_GetDocString(asdl_seq *body)
_PyAST_GetDocString(asdl_stmt_seq *body)
{
if (!asdl_seq_LEN(body)) {
return NULL;

View file

@ -271,7 +271,7 @@ fold_binop(expr_ty node, PyArena *arena, _PyASTOptimizeState *state)
}
static PyObject*
make_const_tuple(asdl_seq *elts)
make_const_tuple(asdl_expr_seq *elts)
{
for (int i = 0; i < asdl_seq_LEN(elts); i++) {
expr_ty e = (expr_ty)asdl_seq_GET(elts, i);
@ -337,7 +337,7 @@ fold_iter(expr_ty arg, PyArena *arena, _PyASTOptimizeState *state)
PyObject *newval;
if (arg->kind == List_kind) {
/* First change a list into tuple. */
asdl_seq *elts = arg->v.List.elts;
asdl_expr_seq *elts = arg->v.List.elts;
Py_ssize_t n = asdl_seq_LEN(elts);
for (Py_ssize_t i = 0; i < n; i++) {
expr_ty e = (expr_ty)asdl_seq_GET(elts, i);
@ -368,7 +368,7 @@ static int
fold_compare(expr_ty node, PyArena *arena, _PyASTOptimizeState *state)
{
asdl_int_seq *ops;
asdl_seq *args;
asdl_expr_seq *args;
Py_ssize_t i;
ops = node->v.Compare.ops;
@ -405,9 +405,9 @@ static int astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTOp
#define CALL_SEQ(FUNC, TYPE, ARG) { \
int i; \
asdl_seq *seq = (ARG); /* avoid variable capture */ \
asdl_ ## TYPE ## _seq *seq = (ARG); /* avoid variable capture */ \
for (i = 0; i < asdl_seq_LEN(seq); i++) { \
TYPE elt = (TYPE)asdl_seq_GET(seq, i); \
TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
if (elt != NULL && !FUNC(elt, ctx_, state)) \
return 0; \
} \
@ -424,13 +424,13 @@ static int astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTOp
}
static int
astfold_body(asdl_seq *stmts, PyArena *ctx_, _PyASTOptimizeState *state)
astfold_body(asdl_stmt_seq *stmts, PyArena *ctx_, _PyASTOptimizeState *state)
{
int docstring = _PyAST_GetDocString(stmts) != NULL;
CALL_SEQ(astfold_stmt, stmt_ty, stmts);
CALL_SEQ(astfold_stmt, stmt, stmts);
if (!docstring && _PyAST_GetDocString(stmts) != NULL) {
stmt_ty st = (stmt_ty)asdl_seq_GET(stmts, 0);
asdl_seq *values = _Py_asdl_seq_new(1, ctx_);
asdl_expr_seq *values = _Py_asdl_expr_seq_new(1, ctx_);
if (!values) {
return 0;
}
@ -453,7 +453,7 @@ astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
CALL(astfold_body, asdl_seq, node_->v.Module.body);
break;
case Interactive_kind:
CALL_SEQ(astfold_stmt, stmt_ty, node_->v.Interactive.body);
CALL_SEQ(astfold_stmt, stmt, node_->v.Interactive.body);
break;
case Expression_kind:
CALL(astfold_expr, expr_ty, node_->v.Expression.body);
@ -469,7 +469,7 @@ astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
{
switch (node_->kind) {
case BoolOp_kind:
CALL_SEQ(astfold_expr, expr_ty, node_->v.BoolOp.values);
CALL_SEQ(astfold_expr, expr, node_->v.BoolOp.values);
break;
case BinOp_kind:
CALL(astfold_expr, expr_ty, node_->v.BinOp.left);
@ -490,28 +490,28 @@ astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
CALL(astfold_expr, expr_ty, node_->v.IfExp.orelse);
break;
case Dict_kind:
CALL_SEQ(astfold_expr, expr_ty, node_->v.Dict.keys);
CALL_SEQ(astfold_expr, expr_ty, node_->v.Dict.values);
CALL_SEQ(astfold_expr, expr, node_->v.Dict.keys);
CALL_SEQ(astfold_expr, expr, node_->v.Dict.values);
break;
case Set_kind:
CALL_SEQ(astfold_expr, expr_ty, node_->v.Set.elts);
CALL_SEQ(astfold_expr, expr, node_->v.Set.elts);
break;
case ListComp_kind:
CALL(astfold_expr, expr_ty, node_->v.ListComp.elt);
CALL_SEQ(astfold_comprehension, comprehension_ty, node_->v.ListComp.generators);
CALL_SEQ(astfold_comprehension, comprehension, node_->v.ListComp.generators);
break;
case SetComp_kind:
CALL(astfold_expr, expr_ty, node_->v.SetComp.elt);
CALL_SEQ(astfold_comprehension, comprehension_ty, node_->v.SetComp.generators);
CALL_SEQ(astfold_comprehension, comprehension, node_->v.SetComp.generators);
break;
case DictComp_kind:
CALL(astfold_expr, expr_ty, node_->v.DictComp.key);
CALL(astfold_expr, expr_ty, node_->v.DictComp.value);
CALL_SEQ(astfold_comprehension, comprehension_ty, node_->v.DictComp.generators);
CALL_SEQ(astfold_comprehension, comprehension, node_->v.DictComp.generators);
break;
case GeneratorExp_kind:
CALL(astfold_expr, expr_ty, node_->v.GeneratorExp.elt);
CALL_SEQ(astfold_comprehension, comprehension_ty, node_->v.GeneratorExp.generators);
CALL_SEQ(astfold_comprehension, comprehension, node_->v.GeneratorExp.generators);
break;
case Await_kind:
CALL(astfold_expr, expr_ty, node_->v.Await.value);
@ -524,20 +524,20 @@ astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
break;
case Compare_kind:
CALL(astfold_expr, expr_ty, node_->v.Compare.left);
CALL_SEQ(astfold_expr, expr_ty, node_->v.Compare.comparators);
CALL_SEQ(astfold_expr, expr, node_->v.Compare.comparators);
CALL(fold_compare, expr_ty, node_);
break;
case Call_kind:
CALL(astfold_expr, expr_ty, node_->v.Call.func);
CALL_SEQ(astfold_expr, expr_ty, node_->v.Call.args);
CALL_SEQ(astfold_keyword, keyword_ty, node_->v.Call.keywords);
CALL_SEQ(astfold_expr, expr, node_->v.Call.args);
CALL_SEQ(astfold_keyword, keyword, node_->v.Call.keywords);
break;
case FormattedValue_kind:
CALL(astfold_expr, expr_ty, node_->v.FormattedValue.value);
CALL_OPT(astfold_expr, expr_ty, node_->v.FormattedValue.format_spec);
break;
case JoinedStr_kind:
CALL_SEQ(astfold_expr, expr_ty, node_->v.JoinedStr.values);
CALL_SEQ(astfold_expr, expr, node_->v.JoinedStr.values);
break;
case Attribute_kind:
CALL(astfold_expr, expr_ty, node_->v.Attribute.value);
@ -556,10 +556,10 @@ astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.step);
break;
case List_kind:
CALL_SEQ(astfold_expr, expr_ty, node_->v.List.elts);
CALL_SEQ(astfold_expr, expr, node_->v.List.elts);
break;
case Tuple_kind:
CALL_SEQ(astfold_expr, expr_ty, node_->v.Tuple.elts);
CALL_SEQ(astfold_expr, expr, node_->v.Tuple.elts);
CALL(fold_tuple, expr_ty, node_);
break;
case Name_kind:
@ -586,7 +586,7 @@ astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTOptimizeState
{
CALL(astfold_expr, expr_ty, node_->target);
CALL(astfold_expr, expr_ty, node_->iter);
CALL_SEQ(astfold_expr, expr_ty, node_->ifs);
CALL_SEQ(astfold_expr, expr, node_->ifs);
CALL(fold_iter, expr_ty, node_->iter);
return 1;
@ -595,13 +595,13 @@ astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTOptimizeState
static int
astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
{
CALL_SEQ(astfold_arg, arg_ty, node_->posonlyargs);
CALL_SEQ(astfold_arg, arg_ty, node_->args);
CALL_SEQ(astfold_arg, arg, node_->posonlyargs);
CALL_SEQ(astfold_arg, arg, node_->args);
CALL_OPT(astfold_arg, arg_ty, node_->vararg);
CALL_SEQ(astfold_arg, arg_ty, node_->kwonlyargs);
CALL_SEQ(astfold_expr, expr_ty, node_->kw_defaults);
CALL_SEQ(astfold_arg, arg, node_->kwonlyargs);
CALL_SEQ(astfold_expr, expr, node_->kw_defaults);
CALL_OPT(astfold_arg, arg_ty, node_->kwarg);
CALL_SEQ(astfold_expr, expr_ty, node_->defaults);
CALL_SEQ(astfold_expr, expr, node_->defaults);
return 1;
}
@ -621,7 +621,7 @@ astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
case FunctionDef_kind:
CALL(astfold_arguments, arguments_ty, node_->v.FunctionDef.args);
CALL(astfold_body, asdl_seq, node_->v.FunctionDef.body);
CALL_SEQ(astfold_expr, expr_ty, node_->v.FunctionDef.decorator_list);
CALL_SEQ(astfold_expr, expr, node_->v.FunctionDef.decorator_list);
if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) {
CALL_OPT(astfold_expr, expr_ty, node_->v.FunctionDef.returns);
}
@ -629,25 +629,25 @@ astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
case AsyncFunctionDef_kind:
CALL(astfold_arguments, arguments_ty, node_->v.AsyncFunctionDef.args);
CALL(astfold_body, asdl_seq, node_->v.AsyncFunctionDef.body);
CALL_SEQ(astfold_expr, expr_ty, node_->v.AsyncFunctionDef.decorator_list);
CALL_SEQ(astfold_expr, expr, node_->v.AsyncFunctionDef.decorator_list);
if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) {
CALL_OPT(astfold_expr, expr_ty, node_->v.AsyncFunctionDef.returns);
}
break;
case ClassDef_kind:
CALL_SEQ(astfold_expr, expr_ty, node_->v.ClassDef.bases);
CALL_SEQ(astfold_keyword, keyword_ty, node_->v.ClassDef.keywords);
CALL_SEQ(astfold_expr, expr, node_->v.ClassDef.bases);
CALL_SEQ(astfold_keyword, keyword, node_->v.ClassDef.keywords);
CALL(astfold_body, asdl_seq, node_->v.ClassDef.body);
CALL_SEQ(astfold_expr, expr_ty, node_->v.ClassDef.decorator_list);
CALL_SEQ(astfold_expr, expr, node_->v.ClassDef.decorator_list);
break;
case Return_kind:
CALL_OPT(astfold_expr, expr_ty, node_->v.Return.value);
break;
case Delete_kind:
CALL_SEQ(astfold_expr, expr_ty, node_->v.Delete.targets);
CALL_SEQ(astfold_expr, expr, node_->v.Delete.targets);
break;
case Assign_kind:
CALL_SEQ(astfold_expr, expr_ty, node_->v.Assign.targets);
CALL_SEQ(astfold_expr, expr, node_->v.Assign.targets);
CALL(astfold_expr, expr_ty, node_->v.Assign.value);
break;
case AugAssign_kind:
@ -664,44 +664,44 @@ astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
case For_kind:
CALL(astfold_expr, expr_ty, node_->v.For.target);
CALL(astfold_expr, expr_ty, node_->v.For.iter);
CALL_SEQ(astfold_stmt, stmt_ty, node_->v.For.body);
CALL_SEQ(astfold_stmt, stmt_ty, node_->v.For.orelse);
CALL_SEQ(astfold_stmt, stmt, node_->v.For.body);
CALL_SEQ(astfold_stmt, stmt, node_->v.For.orelse);
CALL(fold_iter, expr_ty, node_->v.For.iter);
break;
case AsyncFor_kind:
CALL(astfold_expr, expr_ty, node_->v.AsyncFor.target);
CALL(astfold_expr, expr_ty, node_->v.AsyncFor.iter);
CALL_SEQ(astfold_stmt, stmt_ty, node_->v.AsyncFor.body);
CALL_SEQ(astfold_stmt, stmt_ty, node_->v.AsyncFor.orelse);
CALL_SEQ(astfold_stmt, stmt, node_->v.AsyncFor.body);
CALL_SEQ(astfold_stmt, stmt, node_->v.AsyncFor.orelse);
break;
case While_kind:
CALL(astfold_expr, expr_ty, node_->v.While.test);
CALL_SEQ(astfold_stmt, stmt_ty, node_->v.While.body);
CALL_SEQ(astfold_stmt, stmt_ty, node_->v.While.orelse);
CALL_SEQ(astfold_stmt, stmt, node_->v.While.body);
CALL_SEQ(astfold_stmt, stmt, node_->v.While.orelse);
break;
case If_kind:
CALL(astfold_expr, expr_ty, node_->v.If.test);
CALL_SEQ(astfold_stmt, stmt_ty, node_->v.If.body);
CALL_SEQ(astfold_stmt, stmt_ty, node_->v.If.orelse);
CALL_SEQ(astfold_stmt, stmt, node_->v.If.body);
CALL_SEQ(astfold_stmt, stmt, node_->v.If.orelse);
break;
case With_kind:
CALL_SEQ(astfold_withitem, withitem_ty, node_->v.With.items);
CALL_SEQ(astfold_stmt, stmt_ty, node_->v.With.body);
CALL_SEQ(astfold_withitem, withitem, node_->v.With.items);
CALL_SEQ(astfold_stmt, stmt, node_->v.With.body);
break;
case AsyncWith_kind:
CALL_SEQ(astfold_withitem, withitem_ty, node_->v.AsyncWith.items);
CALL_SEQ(astfold_stmt, stmt_ty, node_->v.AsyncWith.body);
CALL_SEQ(astfold_withitem, withitem, node_->v.AsyncWith.items);
CALL_SEQ(astfold_stmt, stmt, node_->v.AsyncWith.body);
break;
case Raise_kind:
CALL_OPT(astfold_expr, expr_ty, node_->v.Raise.exc);
CALL_OPT(astfold_expr, expr_ty, node_->v.Raise.cause);
break;
case Try_kind:
CALL_SEQ(astfold_stmt, stmt_ty, node_->v.Try.body);
CALL_SEQ(astfold_excepthandler, excepthandler_ty, node_->v.Try.handlers);
CALL_SEQ(astfold_stmt, stmt_ty, node_->v.Try.orelse);
CALL_SEQ(astfold_stmt, stmt_ty, node_->v.Try.finalbody);
CALL_SEQ(astfold_stmt, stmt, node_->v.Try.body);
CALL_SEQ(astfold_excepthandler, excepthandler, node_->v.Try.handlers);
CALL_SEQ(astfold_stmt, stmt, node_->v.Try.orelse);
CALL_SEQ(astfold_stmt, stmt, node_->v.Try.finalbody);
break;
case Assert_kind:
CALL(astfold_expr, expr_ty, node_->v.Assert.test);
@ -722,7 +722,7 @@ astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTOptimizeState
switch (node_->kind) {
case ExceptHandler_kind:
CALL_OPT(astfold_expr, expr_ty, node_->v.ExceptHandler.type);
CALL_SEQ(astfold_stmt, stmt_ty, node_->v.ExceptHandler.body);
CALL_SEQ(astfold_stmt, stmt, node_->v.ExceptHandler.body);
break;
default:
break;

View file

@ -117,7 +117,7 @@ static int
append_ast_boolop(_PyUnicodeWriter *writer, expr_ty e, int level)
{
Py_ssize_t i, value_count;
asdl_seq *values;
asdl_expr_seq *values;
const char *op = (e->v.BoolOp.op == And) ? " and " : " or ";
int pr = (e->v.BoolOp.op == And) ? PR_AND : PR_OR;
@ -398,7 +398,7 @@ append_ast_comprehension(_PyUnicodeWriter *writer, comprehension_ty gen)
}
static int
append_ast_comprehensions(_PyUnicodeWriter *writer, asdl_seq *comprehensions)
append_ast_comprehensions(_PyUnicodeWriter *writer, asdl_comprehension_seq *comprehensions)
{
Py_ssize_t i, gen_count;
gen_count = asdl_seq_LEN(comprehensions);
@ -453,7 +453,7 @@ append_ast_compare(_PyUnicodeWriter *writer, expr_ty e, int level)
{
const char *op;
Py_ssize_t i, comparator_count;
asdl_seq *comparators;
asdl_expr_seq *comparators;
asdl_int_seq *ops;
APPEND_STR_IF(level > PR_CMP, "(");
@ -612,7 +612,7 @@ append_fstring_element(_PyUnicodeWriter *writer, expr_ty e, bool is_format_spec)
/* Build body separately to enable wrapping the entire stream of Strs,
Constants and FormattedValues in one opening and one closing quote. */
static PyObject *
build_fstring_body(asdl_seq *values, bool is_format_spec)
build_fstring_body(asdl_expr_seq *values, bool is_format_spec)
{
Py_ssize_t i, value_count;
_PyUnicodeWriter body_writer;

View file

@ -222,27 +222,27 @@ static int compiler_subscript(struct compiler *, expr_ty);
static int compiler_slice(struct compiler *, expr_ty);
static int inplace_binop(operator_ty);
static int are_all_items_const(asdl_seq *, Py_ssize_t, Py_ssize_t);
static int are_all_items_const(asdl_expr_seq *, Py_ssize_t, Py_ssize_t);
static int expr_constant(expr_ty);
static int compiler_with(struct compiler *, stmt_ty, int);
static int compiler_async_with(struct compiler *, stmt_ty, int);
static int compiler_async_for(struct compiler *, stmt_ty);
static int compiler_call_helper(struct compiler *c, int n,
asdl_seq *args,
asdl_seq *keywords);
asdl_expr_seq *args,
asdl_keyword_seq *keywords);
static int compiler_try_except(struct compiler *, stmt_ty);
static int compiler_set_qualname(struct compiler *);
static int compiler_sync_comprehension_generator(
struct compiler *c,
asdl_seq *generators, int gen_index,
asdl_comprehension_seq *generators, int gen_index,
int depth,
expr_ty elt, expr_ty val, int type);
static int compiler_async_comprehension_generator(
struct compiler *c,
asdl_seq *generators, int gen_index,
asdl_comprehension_seq *generators, int gen_index,
int depth,
expr_ty elt, expr_ty val, int type);
@ -1525,7 +1525,7 @@ compiler_addop_j(struct compiler *c, int opcode, basicblock *b)
#define VISIT_SEQ(C, TYPE, SEQ) { \
int _i; \
asdl_seq *seq = (SEQ); /* avoid variable capture */ \
asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
if (!compiler_visit_ ## TYPE((C), elt)) \
@ -1535,7 +1535,7 @@ compiler_addop_j(struct compiler *c, int opcode, basicblock *b)
#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
int _i; \
asdl_seq *seq = (SEQ); /* avoid variable capture */ \
asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
if (!compiler_visit_ ## TYPE((C), elt)) { \
@ -1559,7 +1559,7 @@ compiler_addop_j(struct compiler *c, int opcode, basicblock *b)
/* Search if variable annotations are present statically in a block. */
static int
find_ann(asdl_seq *stmts)
find_ann(asdl_stmt_seq *stmts)
{
int i, j, res = 0;
stmt_ty st;
@ -1778,7 +1778,7 @@ compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblock
and for annotations. */
static int
compiler_body(struct compiler *c, asdl_seq *stmts)
compiler_body(struct compiler *c, asdl_stmt_seq *stmts)
{
int i = 0;
stmt_ty st;
@ -1841,8 +1841,7 @@ compiler_mod(struct compiler *c, mod_ty mod)
ADDOP(c, SETUP_ANNOTATIONS);
}
c->c_interactive = 1;
VISIT_SEQ_IN_SCOPE(c, stmt,
mod->v.Interactive.body);
VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
break;
case Expression_kind:
VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
@ -1945,7 +1944,7 @@ compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, Py
}
static int
compiler_decorators(struct compiler *c, asdl_seq* decos)
compiler_decorators(struct compiler *c, asdl_expr_seq* decos)
{
int i;
@ -1959,8 +1958,8 @@ compiler_decorators(struct compiler *c, asdl_seq* decos)
}
static int
compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
asdl_seq *kw_defaults)
compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs,
asdl_expr_seq *kw_defaults)
{
/* Push a dict of keyword-only default values.
@ -2047,7 +2046,7 @@ compiler_visit_argannotation(struct compiler *c, identifier id,
}
static int
compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args,
PyObject *names)
{
int i;
@ -2173,7 +2172,7 @@ compiler_check_debug_one_arg(struct compiler *c, arg_ty arg)
}
static int
compiler_check_debug_args_seq(struct compiler *c, asdl_seq *args)
compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args)
{
if (args != NULL) {
for (Py_ssize_t i = 0, n = asdl_seq_LEN(args); i < n; i++) {
@ -2208,8 +2207,8 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
arguments_ty args;
expr_ty returns;
identifier name;
asdl_seq* decos;
asdl_seq *body;
asdl_expr_seq* decos;
asdl_stmt_seq *body;
Py_ssize_t i, funcflags;
int annotations;
int scope_type;
@ -2306,7 +2305,7 @@ compiler_class(struct compiler *c, stmt_ty s)
PyCodeObject *co;
PyObject *str;
int i, firstlineno;
asdl_seq* decos = s->v.ClassDef.decorator_list;
asdl_expr_seq *decos = s->v.ClassDef.decorator_list;
if (!compiler_decorators(c, decos))
return 0;
@ -2418,9 +2417,7 @@ compiler_class(struct compiler *c, stmt_ty s)
ADDOP_LOAD_CONST(c, s->v.ClassDef.name);
/* 5. generate the rest of the code for the call */
if (!compiler_call_helper(c, 2,
s->v.ClassDef.bases,
s->v.ClassDef.keywords))
if (!compiler_call_helper(c, 2, s->v.ClassDef.bases, s->v.ClassDef.keywords))
return 0;
/* 6. apply decorators */
@ -2528,7 +2525,7 @@ compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)
/* fallback to general implementation */
break;
case BoolOp_kind: {
asdl_seq *s = e->v.BoolOp.values;
asdl_expr_seq *s = e->v.BoolOp.values;
Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
assert(n >= 0);
int cond2 = e->v.BoolOp.op == Or;
@ -3645,7 +3642,7 @@ compiler_boolop(struct compiler *c, expr_ty e)
basicblock *end;
int jumpi;
Py_ssize_t i, n;
asdl_seq *s;
asdl_expr_seq *s;
assert(e->kind == BoolOp_kind);
if (e->v.BoolOp.op == And)
@ -3673,7 +3670,7 @@ compiler_boolop(struct compiler *c, expr_ty e)
}
static int
starunpack_helper(struct compiler *c, asdl_seq *elts, int pushed,
starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed,
int build, int add, int extend, int tuple)
{
Py_ssize_t n = asdl_seq_LEN(elts);
@ -3750,7 +3747,7 @@ starunpack_helper(struct compiler *c, asdl_seq *elts, int pushed,
}
static int
assignment_helper(struct compiler *c, asdl_seq *elts)
assignment_helper(struct compiler *c, asdl_expr_seq *elts)
{
Py_ssize_t n = asdl_seq_LEN(elts);
Py_ssize_t i;
@ -3784,7 +3781,7 @@ assignment_helper(struct compiler *c, asdl_seq *elts)
static int
compiler_list(struct compiler *c, expr_ty e)
{
asdl_seq *elts = e->v.List.elts;
asdl_expr_seq *elts = e->v.List.elts;
if (e->v.List.ctx == Store) {
return assignment_helper(c, elts);
}
@ -3800,7 +3797,7 @@ compiler_list(struct compiler *c, expr_ty e)
static int
compiler_tuple(struct compiler *c, expr_ty e)
{
asdl_seq *elts = e->v.Tuple.elts;
asdl_expr_seq *elts = e->v.Tuple.elts;
if (e->v.Tuple.ctx == Store) {
return assignment_helper(c, elts);
}
@ -3821,7 +3818,7 @@ compiler_set(struct compiler *c, expr_ty e)
}
static int
are_all_items_const(asdl_seq *seq, Py_ssize_t begin, Py_ssize_t end)
are_all_items_const(asdl_expr_seq *seq, Py_ssize_t begin, Py_ssize_t end)
{
Py_ssize_t i;
for (i = begin; i < end; i++) {
@ -4084,7 +4081,7 @@ maybe_optimize_method_call(struct compiler *c, expr_ty e)
{
Py_ssize_t argsl, i;
expr_ty meth = e->v.Call.func;
asdl_seq *args = e->v.Call.args;
asdl_expr_seq *args = e->v.Call.args;
/* Check that the call node is an attribute access, and that
the call doesn't have keyword parameters. */
@ -4110,7 +4107,7 @@ maybe_optimize_method_call(struct compiler *c, expr_ty e)
}
static int
validate_keywords(struct compiler *c, asdl_seq *keywords)
validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
{
Py_ssize_t nkeywords = asdl_seq_LEN(keywords);
for (Py_ssize_t i = 0; i < nkeywords; i++) {
@ -4210,7 +4207,7 @@ compiler_formatted_value(struct compiler *c, expr_ty e)
}
static int
compiler_subkwargs(struct compiler *c, asdl_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
compiler_subkwargs(struct compiler *c, asdl_keyword_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
{
Py_ssize_t i, n = end - begin;
keyword_ty kw;
@ -4249,8 +4246,8 @@ compiler_subkwargs(struct compiler *c, asdl_seq *keywords, Py_ssize_t begin, Py_
static int
compiler_call_helper(struct compiler *c,
int n, /* Args already pushed */
asdl_seq *args,
asdl_seq *keywords)
asdl_expr_seq *args,
asdl_keyword_seq *keywords)
{
Py_ssize_t i, nseen, nelts, nkwelts;
@ -4375,7 +4372,7 @@ ex_call:
static int
compiler_comprehension_generator(struct compiler *c,
asdl_seq *generators, int gen_index,
asdl_comprehension_seq *generators, int gen_index,
int depth,
expr_ty elt, expr_ty val, int type)
{
@ -4392,7 +4389,7 @@ compiler_comprehension_generator(struct compiler *c,
static int
compiler_sync_comprehension_generator(struct compiler *c,
asdl_seq *generators, int gen_index,
asdl_comprehension_seq *generators, int gen_index,
int depth,
expr_ty elt, expr_ty val, int type)
{
@ -4424,7 +4421,7 @@ compiler_sync_comprehension_generator(struct compiler *c,
/* Fast path for the temporary variable assignment idiom:
for y in [f(x)]
*/
asdl_seq *elts;
asdl_expr_seq *elts;
switch (gen->iter->kind) {
case List_kind:
elts = gen->iter->v.List.elts;
@ -4511,7 +4508,7 @@ compiler_sync_comprehension_generator(struct compiler *c,
static int
compiler_async_comprehension_generator(struct compiler *c,
asdl_seq *generators, int gen_index,
asdl_comprehension_seq *generators, int gen_index,
int depth,
expr_ty elt, expr_ty val, int type)
{
@ -4602,7 +4599,7 @@ compiler_async_comprehension_generator(struct compiler *c,
static int
compiler_comprehension(struct compiler *c, expr_ty e, int type,
identifier name, asdl_seq *generators, expr_ty elt,
identifier name, asdl_comprehension_seq *generators, expr_ty elt,
expr_ty val)
{
PyCodeObject *co = NULL;
@ -5226,7 +5223,7 @@ check_ann_subscr(struct compiler *c, expr_ty e)
return 1;
case Tuple_kind: {
/* extended slice */
asdl_seq *elts = e->v.Tuple.elts;
asdl_expr_seq *elts = e->v.Tuple.elts;
Py_ssize_t i, n = asdl_seq_LEN(elts);
for (i = 0; i < n; i++) {
if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) {

View file

@ -13,11 +13,10 @@ static int
future_check_features(PyFutureFeatures *ff, stmt_ty s, PyObject *filename)
{
int i;
asdl_seq *names;
assert(s->kind == ImportFrom_kind);
names = s->v.ImportFrom.names;
asdl_alias_seq *names = s->v.ImportFrom.names;
for (i = 0; i < asdl_seq_LEN(names); i++) {
alias_ty name = (alias_ty)asdl_seq_GET(names, i);
const char *feature = PyUnicode_AsUTF8(name->name);

View file

@ -202,8 +202,8 @@ static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
static int symtable_visit_alias(struct symtable *st, alias_ty);
static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
static int symtable_visit_keyword(struct symtable *st, keyword_ty);
static int symtable_visit_params(struct symtable *st, asdl_seq *args);
static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
static int symtable_visit_params(struct symtable *st, asdl_arg_seq *args);
static int symtable_visit_argannotations(struct symtable *st, asdl_arg_seq *args);
static int symtable_implicit_arg(struct symtable *st, int pos);
static int symtable_visit_annotations(struct symtable *st, arguments_ty, expr_ty);
static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
@ -261,7 +261,7 @@ struct symtable *
PySymtable_BuildObject(mod_ty mod, PyObject *filename, PyFutureFeatures *future)
{
struct symtable *st = symtable_new();
asdl_seq *seq;
asdl_stmt_seq *seq;
int i;
PyThreadState *tstate;
int recursion_limit = Py_GetRecursionLimit();
@ -1116,7 +1116,7 @@ symtable_add_def(struct symtable *st, PyObject *name, int flag) {
#define VISIT_SEQ(ST, TYPE, SEQ) { \
int i; \
asdl_seq *seq = (SEQ); /* avoid variable capture */ \
asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
for (i = 0; i < asdl_seq_LEN(seq); i++) { \
TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
if (!symtable_visit_ ## TYPE((ST), elt)) \
@ -1126,7 +1126,7 @@ symtable_add_def(struct symtable *st, PyObject *name, int flag) {
#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
int i; \
asdl_seq *seq = (SEQ); /* avoid variable capture */ \
asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
for (i = (START); i < asdl_seq_LEN(seq); i++) { \
TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
if (!symtable_visit_ ## TYPE((ST), elt)) \
@ -1136,7 +1136,7 @@ symtable_add_def(struct symtable *st, PyObject *name, int flag) {
#define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) { \
int i = 0; \
asdl_seq *seq = (SEQ); /* avoid variable capture */ \
asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
for (i = 0; i < asdl_seq_LEN(seq); i++) { \
TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
if (!elt) continue; /* can be NULL */ \
@ -1318,7 +1318,7 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
break;
case Global_kind: {
int i;
asdl_seq *seq = s->v.Global.names;
asdl_identifier_seq *seq = s->v.Global.names;
for (i = 0; i < asdl_seq_LEN(seq); i++) {
identifier name = (identifier)asdl_seq_GET(seq, i);
long cur = symtable_lookup(st, name);
@ -1351,7 +1351,7 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
}
case Nonlocal_kind: {
int i;
asdl_seq *seq = s->v.Nonlocal.names;
asdl_identifier_seq *seq = s->v.Nonlocal.names;
for (i = 0; i < asdl_seq_LEN(seq); i++) {
identifier name = (identifier)asdl_seq_GET(seq, i);
long cur = symtable_lookup(st, name);
@ -1683,7 +1683,7 @@ symtable_implicit_arg(struct symtable *st, int pos)
}
static int
symtable_visit_params(struct symtable *st, asdl_seq *args)
symtable_visit_params(struct symtable *st, asdl_arg_seq *args)
{
int i;
@ -1700,7 +1700,7 @@ symtable_visit_params(struct symtable *st, asdl_seq *args)
}
static int
symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
symtable_visit_argannotations(struct symtable *st, asdl_arg_seq *args)
{
int i;
@ -1850,7 +1850,7 @@ symtable_visit_keyword(struct symtable *st, keyword_ty k)
static int
symtable_handle_comprehension(struct symtable *st, expr_ty e,
identifier scope_name, asdl_seq *generators,
identifier scope_name, asdl_comprehension_seq *generators,
expr_ty elt, expr_ty value)
{
int is_generator = (e->kind == GeneratorExp_kind);