Skip to content

Commit 172fc04

Browse files
committed
py/parse: Make mp_parse_node_extract_list return size_t instead of int.
Because this function can only return non-negative values, and having the correct return type gives more information to the caller.
1 parent 035059e commit 172fc04

File tree

3 files changed

+33
-33
lines changed

3 files changed

+33
-33
lines changed

py/compile.c

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pn
822822
}
823823

824824
// returns true if it was a built-in decorator (even if the built-in had an error)
825-
STATIC bool compile_built_in_decorator(compiler_t *comp, int name_len, mp_parse_node_t *name_nodes, uint *emit_options) {
825+
STATIC bool compile_built_in_decorator(compiler_t *comp, size_t name_len, mp_parse_node_t *name_nodes, uint *emit_options) {
826826
if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
827827
return false;
828828
}
@@ -874,20 +874,20 @@ STATIC bool compile_built_in_decorator(compiler_t *comp, int name_len, mp_parse_
874874
STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
875875
// get the list of decorators
876876
mp_parse_node_t *nodes;
877-
int n = mp_parse_node_extract_list(&pns->nodes[0], PN_decorators, &nodes);
877+
size_t n = mp_parse_node_extract_list(&pns->nodes[0], PN_decorators, &nodes);
878878

879879
// inherit emit options for this function/class definition
880880
uint emit_options = comp->scope_cur->emit_options;
881881

882882
// compile each decorator
883-
int num_built_in_decorators = 0;
884-
for (int i = 0; i < n; i++) {
883+
size_t num_built_in_decorators = 0;
884+
for (size_t i = 0; i < n; i++) {
885885
assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
886886
mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t *)nodes[i];
887887

888888
// nodes[0] contains the decorator function, which is a dotted name
889889
mp_parse_node_t *name_nodes;
890-
int name_len = mp_parse_node_extract_list(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
890+
size_t name_len = mp_parse_node_extract_list(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
891891

892892
// check for built-in decorators
893893
if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
@@ -899,7 +899,7 @@ STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
899899

900900
// compile the decorator function
901901
compile_node(comp, name_nodes[0]);
902-
for (int j = 1; j < name_len; j++) {
902+
for (size_t j = 1; j < name_len; j++) {
903903
assert(MP_PARSE_NODE_IS_ID(name_nodes[j])); // should be
904904
EMIT_ARG(attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[j]), MP_EMIT_ATTR_LOAD);
905905
}
@@ -931,7 +931,7 @@ STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
931931
}
932932

933933
// call each decorator
934-
for (int i = 0; i < n - num_built_in_decorators; i++) {
934+
for (size_t i = 0; i < n - num_built_in_decorators; i++) {
935935
EMIT_ARG(call_function, 1, 0, 0);
936936
}
937937

@@ -1185,10 +1185,10 @@ STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
11851185

11861186
// get the list of . and/or ...'s
11871187
mp_parse_node_t *nodes;
1188-
int n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
1188+
size_t n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
11891189

11901190
// count the total number of .'s
1191-
for (int i = 0; i < n; i++) {
1191+
for (size_t i = 0; i < n; i++) {
11921192
if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
11931193
import_level++;
11941194
} else {
@@ -1222,8 +1222,8 @@ STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
12221222

12231223
// build the "fromlist" tuple
12241224
mp_parse_node_t *pn_nodes;
1225-
int n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes);
1226-
for (int i = 0; i < n; i++) {
1225+
size_t n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes);
1226+
for (size_t i = 0; i < n; i++) {
12271227
assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
12281228
mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t *)pn_nodes[i];
12291229
qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
@@ -1234,7 +1234,7 @@ STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
12341234
// do the import
12351235
qstr dummy_q;
12361236
do_import_name(comp, pn_import_source, &dummy_q);
1237-
for (int i = 0; i < n; i++) {
1237+
for (size_t i = 0; i < n; i++) {
12381238
assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
12391239
mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t *)pn_nodes[i];
12401240
qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
@@ -1285,8 +1285,8 @@ STATIC void compile_global_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_
12851285
}
12861286

12871287
mp_parse_node_t *nodes;
1288-
int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
1289-
for (int i = 0; i < n; i++) {
1288+
size_t n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes);
1289+
for (size_t i = 0; i < n; i++) {
12901290
qstr qst = MP_PARSE_NODE_LEAF_ARG(nodes[i]);
12911291
id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, ID_INFO_KIND_UNDECIDED);
12921292
if (is_global) {
@@ -1346,8 +1346,8 @@ STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
13461346

13471347
// compile elif blocks (if any)
13481348
mp_parse_node_t *pn_elif;
1349-
int n_elif = mp_parse_node_extract_list(&pns->nodes[2], PN_if_stmt_elif_list, &pn_elif);
1350-
for (int i = 0; i < n_elif; i++) {
1349+
size_t n_elif = mp_parse_node_extract_list(&pns->nodes[2], PN_if_stmt_elif_list, &pn_elif);
1350+
for (size_t i = 0; i < n_elif; i++) {
13511351
assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be
13521352
mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t *)pn_elif[i];
13531353

@@ -1524,7 +1524,7 @@ STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
15241524
&& MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t *)pns_it->nodes[1]) == PN_trailer_paren) {
15251525
mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t *)pns_it->nodes[1])->nodes[0];
15261526
mp_parse_node_t *args;
1527-
int n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args);
1527+
size_t n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args);
15281528
mp_parse_node_t pn_range_start;
15291529
mp_parse_node_t pn_range_end;
15301530
mp_parse_node_t pn_range_step;
@@ -1720,7 +1720,7 @@ STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
17201720
} else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
17211721
// try-except and possibly else and/or finally
17221722
mp_parse_node_t *pn_excepts;
1723-
int n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
1723+
size_t n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
17241724
if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
17251725
// no finally
17261726
compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
@@ -1731,13 +1731,13 @@ STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
17311731
} else {
17321732
// just try-except
17331733
mp_parse_node_t *pn_excepts;
1734-
int n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
1734+
size_t n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
17351735
compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
17361736
}
17371737
}
17381738
}
17391739

1740-
STATIC void compile_with_stmt_helper(compiler_t *comp, int n, mp_parse_node_t *nodes, mp_parse_node_t body) {
1740+
STATIC void compile_with_stmt_helper(compiler_t *comp, size_t n, mp_parse_node_t *nodes, mp_parse_node_t body) {
17411741
if (n == 0) {
17421742
// no more pre-bits, compile the body of the with
17431743
compile_node(comp, body);
@@ -1767,7 +1767,7 @@ STATIC void compile_with_stmt_helper(compiler_t *comp, int n, mp_parse_node_t *n
17671767
STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
17681768
// get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
17691769
mp_parse_node_t *nodes;
1770-
int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
1770+
size_t n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
17711771
assert(n > 0);
17721772

17731773
// compile in a nested fashion
@@ -1839,7 +1839,7 @@ STATIC void compile_async_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns
18391839
EMIT_ARG(label_assign, break_label);
18401840
}
18411841

1842-
STATIC void compile_async_with_stmt_helper(compiler_t *comp, int n, mp_parse_node_t *nodes, mp_parse_node_t body) {
1842+
STATIC void compile_async_with_stmt_helper(compiler_t *comp, size_t n, mp_parse_node_t *nodes, mp_parse_node_t body) {
18431843
if (n == 0) {
18441844
// no more pre-bits, compile the body of the with
18451845
compile_node(comp, body);
@@ -1954,7 +1954,7 @@ STATIC void compile_async_with_stmt_helper(compiler_t *comp, int n, mp_parse_nod
19541954
STATIC void compile_async_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
19551955
// get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
19561956
mp_parse_node_t *nodes;
1957-
int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
1957+
size_t n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes);
19581958
assert(n > 0);
19591959

19601960
// compile in a nested fashion
@@ -2325,7 +2325,7 @@ STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_ar
23252325

23262326
// get the list of arguments
23272327
mp_parse_node_t *args;
2328-
int n_args = mp_parse_node_extract_list(&pn_arglist, PN_arglist, &args);
2328+
size_t n_args = mp_parse_node_extract_list(&pn_arglist, PN_arglist, &args);
23292329

23302330
// compile the arguments
23312331
// Rather than calling compile_node on the list, we go through the list of args
@@ -2335,7 +2335,7 @@ STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_ar
23352335
uint n_keyword = 0;
23362336
uint star_flags = 0;
23372337
mp_parse_node_struct_t *star_args_node = NULL, *dblstar_args_node = NULL;
2338-
for (int i = 0; i < n_args; i++) {
2338+
for (size_t i = 0; i < n_args; i++) {
23392339
if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
23402340
mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t *)args[i];
23412341
if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
@@ -2527,7 +2527,7 @@ STATIC void compile_atom_brace_helper(compiler_t *comp, mp_parse_node_struct_t *
25272527

25282528
// get tail elements (2nd, 3rd, ...)
25292529
mp_parse_node_t *nodes;
2530-
int n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2530+
size_t n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
25312531

25322532
// first element sets whether it's a dict or set
25332533
bool is_dict;
@@ -2546,7 +2546,7 @@ STATIC void compile_atom_brace_helper(compiler_t *comp, mp_parse_node_struct_t *
25462546
}
25472547

25482548
// process rest of elements
2549-
for (int i = 0; i < n; i++) {
2549+
for (size_t i = 0; i < n; i++) {
25502550
mp_parse_node_t pn_i = nodes[i];
25512551
bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item);
25522552
compile_node(comp, pn_i);
@@ -3197,7 +3197,7 @@ STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind
31973197
// parameters are in pns->nodes[1]
31983198
if (comp->pass == MP_PASS_CODE_SIZE) {
31993199
mp_parse_node_t *pn_params;
3200-
int n_params = mp_parse_node_extract_list(&pns->nodes[1], PN_typedargslist, &pn_params);
3200+
size_t n_params = mp_parse_node_extract_list(&pns->nodes[1], PN_typedargslist, &pn_params);
32013201
scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
32023202
if (comp->compile_error != MP_OBJ_NULL) {
32033203
goto inline_asm_error;
@@ -3235,9 +3235,9 @@ STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind
32353235

32363236
mp_parse_node_t pn_body = pns->nodes[3]; // body
32373237
mp_parse_node_t *nodes;
3238-
int num = mp_parse_node_extract_list(&pn_body, PN_suite_block_stmts, &nodes);
3238+
size_t num = mp_parse_node_extract_list(&pn_body, PN_suite_block_stmts, &nodes);
32393239

3240-
for (int i = 0; i < num; i++) {
3240+
for (size_t i = 0; i < num; i++) {
32413241
assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
32423242
mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t *)nodes[i];
32433243
if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
@@ -3271,7 +3271,7 @@ STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind
32713271
qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
32723272
pns2 = (mp_parse_node_struct_t *)pns2->nodes[1]; // PN_trailer_paren
32733273
mp_parse_node_t *pn_arg;
3274-
int n_args = mp_parse_node_extract_list(&pns2->nodes[0], PN_arglist, &pn_arg);
3274+
size_t n_args = mp_parse_node_extract_list(&pns2->nodes[0], PN_arglist, &pn_arg);
32753275

32763276
// emit instructions
32773277
if (op == MP_QSTR_label) {

py/parse.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ bool mp_parse_node_get_int_maybe(mp_parse_node_t pn, mp_obj_t *o) {
348348
}
349349
}
350350

351-
int mp_parse_node_extract_list(mp_parse_node_t *pn, size_t pn_kind, mp_parse_node_t **nodes) {
351+
size_t mp_parse_node_extract_list(mp_parse_node_t *pn, size_t pn_kind, mp_parse_node_t **nodes) {
352352
if (MP_PARSE_NODE_IS_NULL(*pn)) {
353353
*nodes = NULL;
354354
return 0;

py/parse.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ static inline mp_parse_node_t mp_parse_node_new_leaf(size_t kind, mp_int_t arg)
8585
bool mp_parse_node_is_const_false(mp_parse_node_t pn);
8686
bool mp_parse_node_is_const_true(mp_parse_node_t pn);
8787
bool mp_parse_node_get_int_maybe(mp_parse_node_t pn, mp_obj_t *o);
88-
int mp_parse_node_extract_list(mp_parse_node_t *pn, size_t pn_kind, mp_parse_node_t **nodes);
88+
size_t mp_parse_node_extract_list(mp_parse_node_t *pn, size_t pn_kind, mp_parse_node_t **nodes);
8989
void mp_parse_node_print(mp_parse_node_t pn, size_t indent);
9090

9191
typedef enum {

0 commit comments

Comments
 (0)