Skip to content

Commit 4e0f5ed

Browse files
committed
pythongh-121303: more compiler_* -> codegen_*, class_body and comprehensions
1 parent 5fce482 commit 4e0f5ed

File tree

1 file changed

+81
-57
lines changed

1 file changed

+81
-57
lines changed

Python/compile.c

Lines changed: 81 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ static PySTEntryObject *compiler_symtable_entry(struct compiler *c);
9696
#define IS_INTERACTIVE(C) compiler_is_interactive(C)
9797
#define IS_NESTED_SCOPE(C) compiler_is_nested_scope(C)
9898
#define SCOPE_TYPE(C) compiler_scope_type(C)
99+
#define QUALNAME(C) compiler_qualname(C)
99100

100101
typedef _Py_SourceLocation location;
101102
typedef struct _PyCfgBuilder cfg_builder;
@@ -106,6 +107,7 @@ static int compiler_optimization_level(struct compiler *c);
106107
static int compiler_is_interactive(struct compiler *c);
107108
static int compiler_is_nested_scope(struct compiler *c);
108109
static int compiler_scope_type(struct compiler *c);
110+
static PyObject *compiler_qualname(struct compiler *c);
109111

110112
#define LOCATION(LNO, END_LNO, COL, END_COL) \
111113
((const _Py_SourceLocation){(LNO), (END_LNO), (COL), (END_COL)})
@@ -336,14 +338,14 @@ static int codegen_call_helper(struct compiler *c, location loc,
336338
static int codegen_try_except(struct compiler *, stmt_ty);
337339
static int codegen_try_star_except(struct compiler *, stmt_ty);
338340

339-
static int compiler_sync_comprehension_generator(
341+
static int codegen_sync_comprehension_generator(
340342
struct compiler *c, location loc,
341343
asdl_comprehension_seq *generators, int gen_index,
342344
int depth,
343345
expr_ty elt, expr_ty val, int type,
344346
int iter_on_stack);
345347

346-
static int compiler_async_comprehension_generator(
348+
static int codegen_async_comprehension_generator(
347349
struct compiler *c, location loc,
348350
asdl_comprehension_seq *generators, int gen_index,
349351
int depth,
@@ -1714,6 +1716,12 @@ dict_lookup_arg(PyObject *dict, PyObject *name)
17141716
return PyLong_AsLong(v);
17151717
}
17161718

1719+
static int
1720+
compiler_lookup_cellvar(struct compiler *c, PyObject *name)
1721+
{
1722+
return dict_lookup_arg(c->u->u_metadata.u_cellvars, name);
1723+
}
1724+
17171725
static int
17181726
compiler_lookup_arg(struct compiler *c, PyCodeObject *co, PyObject *name)
17191727
{
@@ -2367,8 +2375,15 @@ codegen_set_type_params_in_class(struct compiler *c, location loc)
23672375
return SUCCESS;
23682376
}
23692377

2378+
static PyObject *
2379+
compiler_static_attributes_tuple(struct compiler *c)
2380+
{
2381+
assert(c->u->u_static_attributes);
2382+
return PySequence_Tuple(c->u->u_static_attributes);
2383+
}
2384+
23702385
static int
2371-
compiler_class_body(struct compiler *c, stmt_ty s, int firstlineno)
2386+
codegen_class_body(struct compiler *c, stmt_ty s, int firstlineno)
23722387
{
23732388
/* ultimately generate code for:
23742389
<name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
@@ -2391,8 +2406,7 @@ compiler_class_body(struct compiler *c, stmt_ty s, int firstlineno)
23912406
RETURN_IF_ERROR_IN_SCOPE(c, compiler_nameop(c, loc, &_Py_ID(__name__), Load));
23922407
/* ... and store it as __module__ */
23932408
RETURN_IF_ERROR_IN_SCOPE(c, compiler_nameop(c, loc, &_Py_ID(__module__), Store));
2394-
assert(c->u->u_metadata.u_qualname);
2395-
ADDOP_LOAD_CONST(c, loc, c->u->u_metadata.u_qualname);
2409+
ADDOP_LOAD_CONST(c, loc, QUALNAME(c));
23962410
RETURN_IF_ERROR_IN_SCOPE(c, compiler_nameop(c, loc, &_Py_ID(__qualname__), Store));
23972411
ADDOP_LOAD_CONST_NEW(c, loc, PyLong_FromLong(c->u->u_metadata.u_firstlineno));
23982412
RETURN_IF_ERROR_IN_SCOPE(c, compiler_nameop(c, loc, &_Py_ID(__firstlineno__), Store));
@@ -2410,8 +2424,7 @@ compiler_class_body(struct compiler *c, stmt_ty s, int firstlineno)
24102424
}
24112425
/* compile the body proper */
24122426
RETURN_IF_ERROR_IN_SCOPE(c, codegen_body(c, loc, s->v.ClassDef.body));
2413-
assert(c->u->u_static_attributes);
2414-
PyObject *static_attributes = PySequence_Tuple(c->u->u_static_attributes);
2427+
PyObject *static_attributes = compiler_static_attributes_tuple(c);
24152428
if (static_attributes == NULL) {
24162429
compiler_exit_scope(c);
24172430
return ERROR;
@@ -2424,7 +2437,7 @@ compiler_class_body(struct compiler *c, stmt_ty s, int firstlineno)
24242437
/* Set __classdictcell__ if necessary */
24252438
if (SYMTABLE_ENTRY(c)->ste_needs_classdict) {
24262439
/* Store __classdictcell__ into class namespace */
2427-
int i = dict_lookup_arg(c->u->u_metadata.u_cellvars, &_Py_ID(__classdict__));
2440+
int i = compiler_lookup_cellvar(c, &_Py_ID(__classdict__));
24282441
RETURN_IF_ERROR_IN_SCOPE(c, i);
24292442
ADDOP_I(c, NO_LOCATION, LOAD_CLOSURE, i);
24302443
RETURN_IF_ERROR_IN_SCOPE(
@@ -2433,7 +2446,7 @@ compiler_class_body(struct compiler *c, stmt_ty s, int firstlineno)
24332446
/* Return __classcell__ if it is referenced, otherwise return None */
24342447
if (SYMTABLE_ENTRY(c)->ste_needs_class_closure) {
24352448
/* Store __classcell__ into class namespace & return it */
2436-
int i = dict_lookup_arg(c->u->u_metadata.u_cellvars, &_Py_ID(__class__));
2449+
int i = compiler_lookup_cellvar(c, &_Py_ID(__class__));
24372450
RETURN_IF_ERROR_IN_SCOPE(c, i);
24382451
ADDOP_I(c, NO_LOCATION, LOAD_CLOSURE, i);
24392452
ADDOP_I(c, NO_LOCATION, COPY, 1);
@@ -2503,7 +2516,7 @@ codegen_class(struct compiler *c, stmt_ty s)
25032516
RETURN_IF_ERROR_IN_SCOPE(c, compiler_nameop(c, loc, &_Py_STR(type_params), Store));
25042517
}
25052518

2506-
int ret = compiler_class_body(c, s, firstlineno);
2519+
int ret = codegen_class_body(c, s, firstlineno);
25072520
if (is_generic) {
25082521
RETURN_IF_ERROR_IN_SCOPE(c, ret);
25092522
}
@@ -5107,31 +5120,31 @@ codegen_call_helper(struct compiler *c, location loc,
51075120

51085121

51095122
static int
5110-
compiler_comprehension_generator(struct compiler *c, location loc,
5111-
asdl_comprehension_seq *generators, int gen_index,
5112-
int depth,
5113-
expr_ty elt, expr_ty val, int type,
5114-
int iter_on_stack)
5123+
codegen_comprehension_generator(struct compiler *c, location loc,
5124+
asdl_comprehension_seq *generators, int gen_index,
5125+
int depth,
5126+
expr_ty elt, expr_ty val, int type,
5127+
int iter_on_stack)
51155128
{
51165129
comprehension_ty gen;
51175130
gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
51185131
if (gen->is_async) {
5119-
return compiler_async_comprehension_generator(
5132+
return codegen_async_comprehension_generator(
51205133
c, loc, generators, gen_index, depth, elt, val, type,
51215134
iter_on_stack);
51225135
} else {
5123-
return compiler_sync_comprehension_generator(
5136+
return codegen_sync_comprehension_generator(
51245137
c, loc, generators, gen_index, depth, elt, val, type,
51255138
iter_on_stack);
51265139
}
51275140
}
51285141

51295142
static int
5130-
compiler_sync_comprehension_generator(struct compiler *c, location loc,
5131-
asdl_comprehension_seq *generators,
5132-
int gen_index, int depth,
5133-
expr_ty elt, expr_ty val, int type,
5134-
int iter_on_stack)
5143+
codegen_sync_comprehension_generator(struct compiler *c, location loc,
5144+
asdl_comprehension_seq *generators,
5145+
int gen_index, int depth,
5146+
expr_ty elt, expr_ty val, int type,
5147+
int iter_on_stack)
51355148
{
51365149
/* generate code for the iterator, then each of the ifs,
51375150
and then write to the element */
@@ -5146,7 +5159,7 @@ compiler_sync_comprehension_generator(struct compiler *c, location loc,
51465159
if (!iter_on_stack) {
51475160
if (gen_index == 0) {
51485161
/* Receive outermost iter as an implicit argument */
5149-
c->u->u_metadata.u_argcount = 1;
5162+
assert(c->u->u_metadata.u_argcount == 1);
51505163
ADDOP_I(c, loc, LOAD_FAST, 0);
51515164
}
51525165
else {
@@ -5195,9 +5208,9 @@ compiler_sync_comprehension_generator(struct compiler *c, location loc,
51955208

51965209
if (++gen_index < asdl_seq_LEN(generators)) {
51975210
RETURN_IF_ERROR(
5198-
compiler_comprehension_generator(c, loc,
5199-
generators, gen_index, depth,
5200-
elt, val, type, 0));
5211+
codegen_comprehension_generator(c, loc,
5212+
generators, gen_index, depth,
5213+
elt, val, type, 0));
52015214
}
52025215

52035216
location elt_loc = LOC(elt);
@@ -5252,7 +5265,7 @@ compiler_sync_comprehension_generator(struct compiler *c, location loc,
52525265
}
52535266

52545267
static int
5255-
compiler_async_comprehension_generator(struct compiler *c, location loc,
5268+
codegen_async_comprehension_generator(struct compiler *c, location loc,
52565269
asdl_comprehension_seq *generators,
52575270
int gen_index, int depth,
52585271
expr_ty elt, expr_ty val, int type,
@@ -5268,7 +5281,7 @@ compiler_async_comprehension_generator(struct compiler *c, location loc,
52685281
if (!iter_on_stack) {
52695282
if (gen_index == 0) {
52705283
/* Receive outermost iter as an implicit argument */
5271-
c->u->u_metadata.u_argcount = 1;
5284+
assert(c->u->u_metadata.u_argcount == 1);
52725285
ADDOP_I(c, loc, LOAD_FAST, 0);
52735286
}
52745287
else {
@@ -5300,9 +5313,9 @@ compiler_async_comprehension_generator(struct compiler *c, location loc,
53005313
depth++;
53015314
if (++gen_index < asdl_seq_LEN(generators)) {
53025315
RETURN_IF_ERROR(
5303-
compiler_comprehension_generator(c, loc,
5304-
generators, gen_index, depth,
5305-
elt, val, type, 0));
5316+
codegen_comprehension_generator(c, loc,
5317+
generators, gen_index, depth,
5318+
elt, val, type, 0));
53065319
}
53075320

53085321
location elt_loc = LOC(elt);
@@ -5622,15 +5635,15 @@ codegen_comprehension_iter(struct compiler *c, location loc,
56225635
}
56235636

56245637
static int
5625-
compiler_comprehension(struct compiler *c, expr_ty e, int type,
5626-
identifier name, asdl_comprehension_seq *generators, expr_ty elt,
5627-
expr_ty val)
5638+
codegen_comprehension(struct compiler *c, expr_ty e, int type,
5639+
identifier name, asdl_comprehension_seq *generators, expr_ty elt,
5640+
expr_ty val)
56285641
{
56295642
PyCodeObject *co = NULL;
56305643
inlined_comprehension_state inline_state = {NULL, NULL, NULL, NO_LABEL};
56315644
comprehension_ty outermost;
56325645
#ifndef NDEBUG
5633-
int scope_type = c->u->u_scope_type;
5646+
int scope_type = SCOPE_TYPE(c);
56345647
int is_top_level_await = IS_TOP_LEVEL_AWAIT(c);
56355648
#endif
56365649
PySTEntryObject *entry = _PySymtable_Lookup(SYMTABLE(c), (void *)e);
@@ -5652,8 +5665,12 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
56525665
}
56535666
}
56545667
else {
5668+
/* Receive outermost iter as an implicit argument */
5669+
_PyCompile_CodeUnitMetadata umd = {
5670+
.u_argcount = 1,
5671+
};
56555672
if (compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
5656-
(void *)e, e->lineno, NULL, NULL) < 0) {
5673+
(void *)e, e->lineno, NULL, &umd) < 0) {
56575674
goto error;
56585675
}
56595676
}
@@ -5689,8 +5706,8 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
56895706
}
56905707
}
56915708

5692-
if (compiler_comprehension_generator(c, loc, generators, 0, 0,
5693-
elt, val, type, is_inlined) < 0) {
5709+
if (codegen_comprehension_generator(c, loc, generators, 0, 0,
5710+
elt, val, type, is_inlined) < 0) {
56945711
goto error_in_scope;
56955712
}
56965713

@@ -5753,29 +5770,29 @@ codegen_genexp(struct compiler *c, expr_ty e)
57535770
{
57545771
assert(e->kind == GeneratorExp_kind);
57555772
_Py_DECLARE_STR(anon_genexpr, "<genexpr>");
5756-
return compiler_comprehension(c, e, COMP_GENEXP, &_Py_STR(anon_genexpr),
5757-
e->v.GeneratorExp.generators,
5758-
e->v.GeneratorExp.elt, NULL);
5773+
return codegen_comprehension(c, e, COMP_GENEXP, &_Py_STR(anon_genexpr),
5774+
e->v.GeneratorExp.generators,
5775+
e->v.GeneratorExp.elt, NULL);
57595776
}
57605777

57615778
static int
57625779
codegen_listcomp(struct compiler *c, expr_ty e)
57635780
{
57645781
assert(e->kind == ListComp_kind);
57655782
_Py_DECLARE_STR(anon_listcomp, "<listcomp>");
5766-
return compiler_comprehension(c, e, COMP_LISTCOMP, &_Py_STR(anon_listcomp),
5767-
e->v.ListComp.generators,
5768-
e->v.ListComp.elt, NULL);
5783+
return codegen_comprehension(c, e, COMP_LISTCOMP, &_Py_STR(anon_listcomp),
5784+
e->v.ListComp.generators,
5785+
e->v.ListComp.elt, NULL);
57695786
}
57705787

57715788
static int
57725789
codegen_setcomp(struct compiler *c, expr_ty e)
57735790
{
57745791
assert(e->kind == SetComp_kind);
57755792
_Py_DECLARE_STR(anon_setcomp, "<setcomp>");
5776-
return compiler_comprehension(c, e, COMP_SETCOMP, &_Py_STR(anon_setcomp),
5777-
e->v.SetComp.generators,
5778-
e->v.SetComp.elt, NULL);
5793+
return codegen_comprehension(c, e, COMP_SETCOMP, &_Py_STR(anon_setcomp),
5794+
e->v.SetComp.generators,
5795+
e->v.SetComp.elt, NULL);
57795796
}
57805797

57815798

@@ -5784,9 +5801,9 @@ codegen_dictcomp(struct compiler *c, expr_ty e)
57845801
{
57855802
assert(e->kind == DictComp_kind);
57865803
_Py_DECLARE_STR(anon_dictcomp, "<dictcomp>");
5787-
return compiler_comprehension(c, e, COMP_DICTCOMP, &_Py_STR(anon_dictcomp),
5788-
e->v.DictComp.generators,
5789-
e->v.DictComp.key, e->v.DictComp.value);
5804+
return codegen_comprehension(c, e, COMP_DICTCOMP, &_Py_STR(anon_dictcomp),
5805+
e->v.DictComp.generators,
5806+
e->v.DictComp.key, e->v.DictComp.value);
57905807
}
57915808

57925809

@@ -6264,7 +6281,7 @@ codegen_check_ann_expr(struct compiler *c, expr_ty e)
62646281
}
62656282

62666283
static int
6267-
compiler_check_annotation(struct compiler *c, stmt_ty s)
6284+
codegen_check_annotation(struct compiler *c, stmt_ty s)
62686285
{
62696286
/* Annotations of complex targets does not produce anything
62706287
under annotations future */
@@ -6273,8 +6290,8 @@ compiler_check_annotation(struct compiler *c, stmt_ty s)
62736290
}
62746291

62756292
/* Annotations are only evaluated in a module or class. */
6276-
if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
6277-
c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
6293+
if (SCOPE_TYPE(c) == COMPILER_SCOPE_MODULE ||
6294+
SCOPE_TYPE(c) == COMPILER_SCOPE_CLASS) {
62786295
return codegen_check_ann_expr(c, s->v.AnnAssign.annotation);
62796296
}
62806297
return SUCCESS;
@@ -6384,7 +6401,7 @@ codegen_annassign(struct compiler *c, stmt_ty s)
63846401
return ERROR;
63856402
}
63866403
/* Annotation is evaluated last. */
6387-
if (future_annotations && !s->v.AnnAssign.simple && compiler_check_annotation(c, s) < 0) {
6404+
if (future_annotations && !s->v.AnnAssign.simple && codegen_check_annotation(c, s) < 0) {
63886405
return ERROR;
63896406
}
63906407
return SUCCESS;
@@ -6604,7 +6621,7 @@ emit_and_reset_fail_pop(struct compiler *c, location loc,
66046621
}
66056622

66066623
static int
6607-
compiler_error_duplicate_store(struct compiler *c, location loc, identifier n)
6624+
codegen_error_duplicate_store(struct compiler *c, location loc, identifier n)
66086625
{
66096626
return compiler_error(c, loc,
66106627
"multiple assignments to name %R in pattern", n);
@@ -6632,7 +6649,7 @@ codegen_pattern_helper_store_name(struct compiler *c, location loc,
66326649
int duplicate = PySequence_Contains(pc->stores, n);
66336650
RETURN_IF_ERROR(duplicate);
66346651
if (duplicate) {
6635-
return compiler_error_duplicate_store(c, loc, n);
6652+
return codegen_error_duplicate_store(c, loc, n);
66366653
}
66376654
// Rotate this object underneath any items we need to preserve:
66386655
Py_ssize_t rotations = pc->on_top + PyList_GET_SIZE(pc->stores) + 1;
@@ -7125,7 +7142,7 @@ codegen_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
71257142
goto error;
71267143
}
71277144
if (dupe) {
7128-
compiler_error_duplicate_store(c, LOC(p), name);
7145+
codegen_error_duplicate_store(c, LOC(p), name);
71297146
goto error;
71307147
}
71317148
if (PyList_Append(pc->stores, name)) {
@@ -7436,6 +7453,13 @@ compiler_scope_type(struct compiler *c)
74367453
return c->u->u_scope_type;
74377454
}
74387455

7456+
static PyObject *
7457+
compiler_qualname(struct compiler *c)
7458+
{
7459+
assert(c->u->u_metadata.u_qualname);
7460+
return c->u->u_metadata.u_qualname;
7461+
}
7462+
74397463
static int
74407464
compute_code_flags(struct compiler *c)
74417465
{

0 commit comments

Comments
 (0)