Skip to content

Commit ea5b59b

Browse files
committed
py/compile: Only compile function annotations if really needed.
Function annotations are only needed when the native emitter is enabled and when the current scope is emitted in viper mode. All other times the annotations can be skipped completely.
1 parent 8d8fdcb commit ea5b59b

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

py/compile.c

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2786,6 +2786,7 @@ STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
27862786
compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star);
27872787
}
27882788

2789+
#if MICROPY_EMIT_NATIVE
27892790
STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) {
27902791
if (!MP_PARSE_NODE_IS_STRUCT(pn)) {
27912792
// no annotation
@@ -2816,22 +2817,19 @@ STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn)
28162817
mp_parse_node_t pn_annotation = pns->nodes[1];
28172818

28182819
if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
2819-
#if MICROPY_EMIT_NATIVE
28202820
qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
28212821
id_info_t *id_info = scope_find(comp->scope_cur, param_name);
28222822
assert(id_info != NULL);
28232823

2824-
if (comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER) {
2825-
if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
2826-
qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
2827-
EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
2828-
} else {
2829-
compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
2830-
}
2824+
if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
2825+
qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
2826+
EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type);
2827+
} else {
2828+
compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier");
28312829
}
2832-
#endif // MICROPY_EMIT_NATIVE
28332830
}
28342831
}
2832+
#endif // MICROPY_EMIT_NATIVE
28352833

28362834
STATIC void compile_scope_comp_iter(compiler_t *comp, mp_parse_node_t pn_iter, mp_parse_node_t pn_inner_expr, int l_top, int for_depth) {
28372835
tail_recursion:
@@ -2954,28 +2952,28 @@ STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
29542952
if (comp->pass == MP_PASS_SCOPE) {
29552953
comp->have_star = false;
29562954
apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2957-
} else {
2955+
}
2956+
#if MICROPY_EMIT_NATIVE
2957+
else if (scope->emit_options == MP_EMIT_OPT_VIPER) {
29582958
// compile annotations; only needed on latter compiler passes
2959+
// only needed for viper emitter
29592960

29602961
// argument annotations
29612962
apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations);
29622963

29632964
// pns->nodes[2] is return/whole function annotation
29642965
mp_parse_node_t pn_annotation = pns->nodes[2];
29652966
if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
2966-
#if MICROPY_EMIT_NATIVE
2967-
if (scope->emit_options == MP_EMIT_OPT_VIPER) {
2968-
// nodes[2] can be null or a test-expr
2969-
if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
2970-
qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
2971-
EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
2972-
} else {
2973-
compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
2974-
}
2967+
// nodes[2] can be null or a test-expr
2968+
if (MP_PARSE_NODE_IS_ID(pn_annotation)) {
2969+
qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation);
2970+
EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type);
2971+
} else {
2972+
compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier");
29752973
}
2976-
#endif // MICROPY_EMIT_NATIVE
29772974
}
29782975
}
2976+
#endif // MICROPY_EMIT_NATIVE
29792977

29802978
compile_node(comp, pns->nodes[3]); // 3 is function body
29812979
// emit return if it wasn't the last opcode

0 commit comments

Comments
 (0)