Skip to content

Commit 11d8cd5

Browse files
committed
py, compiler: Turn id_info_t.param into a set of flags.
So we can add more flags.
1 parent b140bff commit 11d8cd5

3 files changed

Lines changed: 13 additions & 9 deletions

File tree

py/compile.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2208,7 +2208,7 @@ STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_ar
22082208
// get first argument to function
22092209
bool found = false;
22102210
for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
2211-
if (comp->scope_cur->id_info[i].param) {
2211+
if (comp->scope_cur->id_info[i].flags && ID_FLAG_IS_PARAM) {
22122212
EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].local_num);
22132213
found = true;
22142214
break;
@@ -2761,8 +2761,8 @@ void compile_scope_func_lambda_param(compiler_t *comp, mp_parse_node_t pn, pn_ki
27612761
compile_syntax_error(comp, pn, "same name used for parameter");
27622762
return;
27632763
}
2764-
id_info->param = true;
27652764
id_info->kind = ID_INFO_KIND_LOCAL;
2765+
id_info->flags |= ID_FLAG_IS_PARAM;
27662766
}
27672767
}
27682768

@@ -3097,7 +3097,7 @@ void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
30973097
id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
30983098
}
30993099
// note: params always count for 1 local, even if they are a cell
3100-
if (id->param || id->kind == ID_INFO_KIND_LOCAL) {
3100+
if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
31013101
id->local_num = scope->num_locals;
31023102
scope->num_locals += 1;
31033103
}
@@ -3119,7 +3119,7 @@ void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
31193119
// in Micro Python the cells come right after the fast locals
31203120
// parameters are not counted here, since they remain at the start
31213121
// of the locals, even if they are cell vars
3122-
if (!id->param && id->kind == ID_INFO_KIND_CELL) {
3122+
if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
31233123
id->local_num = scope->num_locals;
31243124
scope->num_locals += 1;
31253125
}
@@ -3136,7 +3136,7 @@ void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
31363136
for (int j = 0; j < scope->id_info_len; j++) {
31373137
id_info_t *id2 = &scope->id_info[j];
31383138
if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
3139-
assert(!id2->param); // free vars should not be params
3139+
assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
31403140
#if MICROPY_EMIT_CPYTHON
31413141
// in CPython the frees are numbered after the cells
31423142
id2->local_num = num_cell + num_free;
@@ -3154,7 +3154,7 @@ void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
31543154
if (num_free > 0) {
31553155
for (int i = 0; i < scope->id_info_len; i++) {
31563156
id_info_t *id = &scope->id_info[i];
3157-
if (id->param || id->kind != ID_INFO_KIND_FREE) {
3157+
if (id->kind != ID_INFO_KIND_FREE || (id->flags && ID_FLAG_IS_PARAM)) {
31583158
id->local_num += num_free;
31593159
}
31603160
}

py/scope.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ id_info_t *scope_find_or_add_id(scope_t *scope, qstr qstr, bool *added) {
7373
// handled by the compiler because it adds arguments before compiling the body
7474
id_info_t *id_info = &scope->id_info[scope->id_info_len++];
7575

76-
id_info->param = false;
7776
id_info->kind = 0;
78-
id_info->qstr = qstr;
77+
id_info->flags = 0;
7978
id_info->local_num = 0;
79+
id_info->qstr = qstr;
8080
*added = true;
8181
return id_info;
8282
}

py/scope.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ enum {
66
ID_INFO_KIND_FREE, // in a function f, belongs to the parent of f
77
};
88

9+
enum {
10+
ID_FLAG_IS_PARAM = 0x01,
11+
};
12+
913
typedef struct _id_info_t {
10-
uint8_t param;
1114
uint8_t kind;
15+
uint8_t flags;
1216
// when it's an ID_INFO_KIND_LOCAL this is the unique number of the local
1317
// whet it's an ID_INFO_KIND_CELL/FREE this is the unique number of the closed over variable
1418
uint16_t local_num;

0 commit comments

Comments
 (0)