diff --git a/src/ast_analyzer.c b/src/ast_analyzer.c index b1befe597..6fe40a4e5 100644 --- a/src/ast_analyzer.c +++ b/src/ast_analyzer.c @@ -19,6 +19,7 @@ typedef struct kxana_context_ { int depth; int in_catch; int in_native; + int in_function; int class_id; int arg_index; int anon_arg; @@ -665,7 +666,7 @@ LOOP_HEAD:; } break; } - if (is_anon_var(actx, node)) { + if (actx->in_function && is_anon_var(actx, node)) { break; } int enumv = 0; @@ -1604,6 +1605,8 @@ LOOP_HEAD:; enum_map_t enval = kh_init(enum_value); kv_push(enum_map_t, actx->enval, enval); + int in_function = actx->in_function; + actx->in_function = 1; kx_object_t *class_node = actx->class_node; actx->class_node = node; int depth = actx->depth; @@ -1671,6 +1674,7 @@ LOOP_HEAD:; kv_remove_last(actx->symbols); actx->depth = depth; actx->class_node = class_node; + actx->in_function = in_function; kh_destroy(enum_value, enval); kv_pop(actx->enval); @@ -1727,6 +1731,8 @@ LOOP_HEAD:; enum_map_t enval = kh_init(enum_value); kv_push(enum_map_t, actx->enval, enval); + int in_function = actx->in_function; + actx->in_function = 1; if (node->type == KXST_NATIVE) { actx->in_native = 1; node->var_type = KX_NFNC_T; @@ -1785,6 +1791,7 @@ LOOP_HEAD:; kv_remove_last(actx->symbols); actx->depth = depth; actx->in_native = 0; + actx->in_function = in_function; kh_destroy(enum_value, enval); kv_pop(actx->enval); diff --git a/src/optimizer/opt_cfold.c b/src/optimizer/opt_cfold.c index affaf4385..6fd5d062e 100644 --- a/src/optimizer/opt_cfold.c +++ b/src/optimizer/opt_cfold.c @@ -21,6 +21,7 @@ typedef struct folding_context_ { int anon_check; int anon_arg; int exprlist_r2l; + int in_function; int in_case_when; } folding_context_t; @@ -53,7 +54,7 @@ static void opt_ast_constant_folding_impl(kx_context_t *ctx, kx_object_t *node, break; case KXOP_VAR: { - if (cctx->anon_check) { + if (cctx->in_function && cctx->anon_check) { const char *name = node->value.s; if (!cctx->in_case_when && name && name[0] == '_' && name[1] == 0) { node->lexical = 0; @@ -438,22 +439,28 @@ static void opt_ast_constant_folding_impl(kx_context_t *ctx, kx_object_t *node, break; case KXST_SYSCLASS: case KXST_CLASS: { /* s: name, lhs: arglist, rhs: block: ex: expr (inherit) */ + int in_function = cctx->in_function; + cctx->in_function = 1; int anon_arg = cctx->anon_arg; cctx->anon_arg = 0; opt_ast_constant_folding_impl(ctx, node->lhs, cctx); opt_ast_constant_folding_impl(ctx, node->rhs, cctx); opt_ast_constant_folding_impl(ctx, node->ex, cctx); cctx->anon_arg = anon_arg; + cctx->in_function = in_function; break; } case KXST_COROUTINE: /* s: name, lhs: arglist, rhs: block: optional: public/private/protected */ case KXST_FUNCTION: /* s: name, lhs: arglist, rhs: block: optional: public/private/protected */ case KXST_NATIVE: { /* s: name, lhs: arglist, rhs: block: ret_type: return type */ + int in_function = cctx->in_function; + cctx->in_function = 1; int anon_arg = cctx->anon_arg; cctx->anon_arg = 0; opt_ast_constant_folding_impl(ctx, node->lhs, cctx); opt_ast_constant_folding_impl(ctx, node->rhs, cctx); cctx->anon_arg = anon_arg; + cctx->in_function = in_function; break; } default: