From: fennecdjay Date: Sun, 25 Feb 2024 22:23:51 +0000 (+0100) Subject: :art: add a few helper functions X-Git-Tag: nightly~57 X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=9f404dd6f178613efeea2f1321bee5583100cf5c;p=gwion.git :art: add a few helper functions --- diff --git a/include/env/env.h b/include/env/env.h index 27cea8ea..9a3b4aeb 100644 --- a/include/env/env.h +++ b/include/env/env.h @@ -44,9 +44,13 @@ ANEW Env new_env(MemPool); ANN void env_reset(const Env); ANN void free_env(Env); ANN2(1, 3) m_uint env_push(const Env, const Type, const Nspc); +ANN static inline m_uint env_push_nspc(const Env env, const Nspc nspc) { + return env_push(env, NULL, nspc); +} ANN static inline m_uint env_push_global(const Env env) { - return env_push(env, NULL, env->global_nspc); + return env_push_nspc(env, env->global_nspc); } + ANN void env_pop(const Env, const m_uint); ANN Type scan_type(const Env, const Type, Type_Decl *); ANN Value mk_class(const Env env, const Type base, const loc_t); diff --git a/include/env/value.h b/include/env/value.h index a03191f0..10c29fd4 100644 --- a/include/env/value.h +++ b/include/env/value.h @@ -60,4 +60,9 @@ ANN static inline void valid_value(const Env env, const Symbol xid, const Value set_vflag(v, vflag_valid); nspc_add_value(env->curr, xid, v); } + +ANN static inline m_uint env_pushv(const Env env, const Value value) { + return env_push(env, value->from->owner_class, value->from->owner); +} + #endif diff --git a/plug b/plug index 748e6a8c..0a0c846c 160000 --- a/plug +++ b/plug @@ -1 +1 @@ -Subproject commit 748e6a8c493dbb14c918558ac254acce7ea84bd1 +Subproject commit 0a0c846ccd7efe3cac7007e1561f760da0a6bff5 diff --git a/src/emit/emit.c b/src/emit/emit.c index 1475e4d5..6b7356f6 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -47,9 +47,8 @@ typedef struct Local_ { static inline void emit_pop(const Emitter emit, const m_uint scope) { env_pop(emit->env, scope); } -static inline m_uint emit_push(const Emitter emit, const Type type, - const Nspc nspc) { - return env_push(emit->env, type, nspc); +static inline m_uint emit_pushv(const Emitter emit, const Value value) { + return env_pushv(emit->env, value); } static inline m_uint emit_push_global(const Emitter emit) { @@ -837,7 +836,7 @@ ANN bool emit_ensure_func(const Emitter emit, const Func f) { if(from->owner_class) CHECK_B(ensure_emit(emit, from->owner_class)); if(f->code) return true; - const m_uint scope = emit_push(emit, from->owner_class, from->owner); + const m_uint scope = emit_pushv(emit, f->value_ref); const bool ret = emit_func_def(emit, f->def); emit_pop(emit, scope); return ret; @@ -1318,7 +1317,7 @@ ANN static bool _emit_exp_call(const Emitter emit, const Exp_Call *call) { if(_f) { const Func base = emit->env->func; emit->env->func = _f; - const m_uint scope = emit_push(emit, _f->value_ref->from->owner_class, _f->value_ref->from->owner); + const m_uint scope = emit_pushv(emit, _f->value_ref); const bool ret = emit_inline(emit, _f, call); emit_pop(emit, scope); emit->env->func = base; @@ -1435,7 +1434,7 @@ ANN bool traverse_dot_tmpl(const Emitter emit, const Func_Def fdef, const Value .scope = scope, .flag = tflag_emit}; CHECK_B(envset_pushv(&es, v)); - (void)emit_push(emit, v->from->owner_class, v->from->owner); + (void)emit_pushv(emit, v); const bool ret = traverse_emit_func_def(emit, fdef); emit_pop(emit, scope); envset_pop(&es, v->from->owner_class); @@ -1481,7 +1480,7 @@ ANN static bool emit_template_code(const Emitter emit, const Func f) { .scope = scope, .flag = tflag_emit}; CHECK_B(envset_pushv(&es, v)); - (void)emit_push(emit, v->from->owner_class, v->from->owner); + (void)emit_pushv(emit, v); const bool ret = emit_func_def(emit, f->def); envset_pop(&es, v->from->owner_class); emit_pop(emit, scope); diff --git a/src/env/envset.c b/src/env/envset.c index 994230b7..ecda1d7a 100644 --- a/src/env/envset.c +++ b/src/env/envset.c @@ -25,7 +25,7 @@ ANN static bool push(struct EnvSet *es, const Type t) { if (owner_class) CHECK_B(push(es, owner_class)); else - env_push(es->env, NULL, + env_push_nspc(es->env, t->info->value->from->ctx ? t->info->value->from->ctx->nspc : es->env->curr); env_push_type((void *)es->env, t); // do not push if is a function? @@ -45,7 +45,7 @@ bool envset_push(struct EnvSet *es, const Type t, const Nspc nspc) { return es->run ? push(es, t) : true; } if (nspc != es->env->curr) { - env_push(es->env, NULL, nspc); + env_push_nspc(es->env, nspc); es->run = 1; } return true; diff --git a/src/env/value.c b/src/env/value.c index a67c31ba..63a4a785 100644 --- a/src/env/value.c +++ b/src/env/value.c @@ -27,6 +27,7 @@ ANN Value new_value(const Env env, const Type type, const Tag tag) { } ANN void valuefrom(const Env env, ValueFrom *from) { + from->filename = env->name; from->owner = env->curr; from->owner_class = env->scope->depth ? NULL : env->class_def; from->ctx = env->context; diff --git a/src/lib/array.c b/src/lib/array.c index 14d0f170..bf2e11fc 100644 --- a/src/lib/array.c +++ b/src/lib/array.c @@ -807,13 +807,13 @@ static OP_CHECK(opck_array_scan) { = ts->t != t_array ? ts->t : known_type(env, mp_vector_at(ts->td->types, TmplArg, 0)->d.td)); if (base->size == 0) { gwerr_basic("Can't use type of size 0 as array base", NULL, NULL, - env->context->name, ts->td->tag.loc, 0); + env->name, ts->td->tag.loc, 0); env_set_error(env, true); return env->gwion->type[et_error]; } if (tflag(base, tflag_ref)) { gwerr_basic("Can't use ref types as array base", NULL, NULL, - env->context->name, ts->td->tag.loc, 0); + env->name, ts->td->tag.loc, 0); env_set_error(env, true); return env->gwion->type[et_error]; } @@ -828,8 +828,7 @@ static OP_CHECK(opck_array_scan) { mp_vector_set(cdef->base.tmpl->call, TmplArg, 0, arg); const Context ctx = env->context; env->context = base->info->value->from->ctx; - const m_uint scope = env_push(env, base->info->value->from->owner_class, - base->info->value->from->owner); + const m_uint scope = env_pushv(env, base->info->value); CHECK_ON(scan0_class_def(env, cdef)); const Type t = cdef->base.type; if (GET_FLAG(base, abstract) && !tflag(base, tflag_union)) diff --git a/src/lib/closure.c b/src/lib/closure.c index de590c95..932bd8ea 100644 --- a/src/lib/closure.c +++ b/src/lib/closure.c @@ -572,7 +572,7 @@ static OP_CHECK(opck_op_impl) { // s_name(impl->e->d.prim.d.var), func->name); const Value v = nspc_lookup_value0(opi.nspc, impl->e->d.prim.d.var); if (v) { - const m_uint scope = env_push(env, NULL, opi.nspc); + const m_uint scope = env_push_nspc(env, opi.nspc); _lhs.next = &_rhs; Exp_Call call = {.args = &_lhs}; const Func exists = (Func)find_func_match(env, v->d.func_ref, &call); @@ -614,7 +614,7 @@ static OP_CHECK(opck_op_impl) { def->base->tag.sym = impl->e->d.prim.d.var; // use envset // or better, some function with envset and traverse - const m_uint scope = env_push(env, NULL, opi.nspc); + const m_uint scope = env_push_nspc(env, opi.nspc); // we assume succes here /*const bool ret = */ traverse_func_def(env, def); env_pop(env, scope); diff --git a/src/lib/object_op.c b/src/lib/object_op.c index 4c961f41..25821d23 100644 --- a/src/lib/object_op.c +++ b/src/lib/object_op.c @@ -328,7 +328,7 @@ ANN Type scan_class(const Env env, const Type t, const Type_Decl *td) { const Type owner = t->info->value->from->owner_class; CHECK_O(envset_pushv(&es, t->info->value)); const bool local = !owner && !tmpl_global(env, td->types) && from_global_nspc(env, env->curr); - if(local && env->context) env_push(env, NULL, env->context->nspc); + if(local && env->context) env_push_nspc(env, env->context->nspc); const Type ret = _scan_class(env, &info); if(local && env->context)env_pop(env, es.scope); envset_pop(&es, owner); diff --git a/src/lib/ref.c b/src/lib/ref.c index 622931bf..d09bb8be 100644 --- a/src/lib/ref.c +++ b/src/lib/ref.c @@ -162,8 +162,7 @@ static OP_CHECK(opck_ref_scan) { set_tflag(t, tflag_check); set_tflag(t, tflag_emit); set_tflag(t, tflag_ref); - const m_uint scope = env_push(env, base->info->value->from->owner_class, - base->info->value->from->owner); + const m_uint scope = env_pushv(env, base->info->value); mk_class(env, t, ts->td->tag.loc); base2ref(env, base, t); ref2base(env, t, base); diff --git a/src/parse/check.c b/src/parse/check.c index 37a047bc..89188f4f 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -676,8 +676,10 @@ ANN static bool check_func_args(const Env env, Arg_List args) { Arg *arg = mp_vector_at(args, Arg, i); const Var_Decl *decl = &arg->var.vd; const Value v = decl->value; - if(decl->tag.sym && !can_define(env, decl->tag.sym, decl->tag.loc)) + if(decl->tag.sym && !can_define(env, decl->tag.sym, decl->tag.loc)) { POISON(ok, env); + continue; + } valid_value(env, decl->tag.sym, v); } return ok; @@ -775,8 +777,10 @@ ANN static TmplArg_List check_template_args(const Env env, Exp_Call *exp, for(uint32_t i = 0; i < len; i++) { Specialized *spec = mp_vector_at(sl, Specialized, i); if (tmplarg_match(env, spec->tag.sym, fdef->base->td->tag.sym, fdef->base->ret_type)) { - if(!check_exp(env, exp->other)) + if(!check_exp(env, exp->other)) { POISON_NODE(ok, env, exp->other); + continue; + } if(!is_func(env->gwion, exp->other->type)) { TmplArg targ = { .type = tmplarg_td, @@ -2056,8 +2060,10 @@ ANN static bool _check_trait_def(const Env env, const Trait_Def pdef) { for(m_uint i = 0; i < l->len; i++) { const Stmt* stmt = mp_vector_at(l, Stmt, i); if (stmt->stmt_type == ae_stmt_exp) { - if(!traverse_exp(env, stmt->d.stmt_exp.val)) + if(!traverse_exp(env, stmt->d.stmt_exp.val)) { POISON_NODE(ok, env, stmt->d.stmt_exp.val); + continue; + } Var_Decl vd = stmt->d.stmt_exp.val->d.exp_decl.var.vd; const Value value = vd.value; valuefrom(env, value->from); diff --git a/src/parse/func_resolve_tmpl.c b/src/parse/func_resolve_tmpl.c index 7eb29378..3bae63ea 100644 --- a/src/parse/func_resolve_tmpl.c +++ b/src/parse/func_resolve_tmpl.c @@ -157,7 +157,7 @@ ANN static Func find_tmpl(const Env env, const Value v, Exp_Call *const exp, struct ResolverArgs ra = { .v = v, .e = exp, .tmpl_name = tmpl_name, .types = types}; CHECK_O(envset_pushv(&es, v)); - (void)env_push(env, v->from->owner_class, v->from->owner); + (void)env_pushv(env, v); const Tmpl *tmpl = v->from->owner_class && v->from->owner_class->info->cdef ? get_tmpl(v->from->owner_class) : NULL; if(tmpl) diff --git a/src/parse/scan1.c b/src/parse/scan1.c index 5158d85e..8a1d6cf9 100644 --- a/src/parse/scan1.c +++ b/src/parse/scan1.c @@ -439,8 +439,10 @@ ANN static bool scan1_args(const Env env, Arg_List args) { } if (arg->var.td) { SET_FLAG(arg->var.td, late); - if(!(arg->type = void_type(env, arg->var.td))) + if(!(arg->type = void_type(env, arg->var.td))) { POISON(ok, env); + continue; + } if (GET_FLAG(env->func->def->base, global) && !type_global(env, arg->type)) ERR_OK(ok, arg->var.td->tag.loc, "is not global"); } @@ -546,8 +548,10 @@ ANN static inline bool scan1_union_def_inner_loop(const Env env, bool ok = true; for(uint32_t i = 0; i < l->len; i++) { Variable *um = mp_vector_at(l, Variable, i); - if (nspc_lookup_value0(env->curr, um->vd.tag.sym)) + if (nspc_lookup_value0(env->curr, um->vd.tag.sym)) { ERR_OK(ok, um->vd.tag.loc, _("'%s' already declared in union"), s_name(um->vd.tag.sym)); + continue; + } const Type t = known_type(env, um->td); if(t) { if(tflag(t, tflag_ref)) { @@ -641,8 +645,10 @@ ANN static bool scan1_stmt_list(const Env env, Stmt_List l) { bool ok = true; for(i = 0; i < l->len; i++) { Stmt* stmt = mp_vector_at(l, Stmt, i); - if(!scan1_stmt(env, stmt)) + if(!scan1_stmt(env, stmt)) { POISON_NODE(ok, env, stmt); + continue; + } if(end_flow(stmt)) break; } if(++i < l->len) dead_code(env, l, i); diff --git a/src/parse/template.c b/src/parse/template.c index 369d19b5..348aa745 100644 --- a/src/parse/template.c +++ b/src/parse/template.c @@ -285,7 +285,7 @@ ANN Type scan_type(const Env env, const Type t, Type_Decl *td) { const Context ctx = env->context; const m_str name = env->name; envset_push(&es, owner, owner->nspc); - (void)env_push(env, owner, owner->nspc); // TODO: is this needed? + (void)env_push_type(env, owner); // TODO: is this needed? env->context = owner->info->value->from->ctx; env->name = owner->info->value->from->filename; const Type ret = known_type(env, td->next); diff --git a/src/parse/type_decl.c b/src/parse/type_decl.c index e79884c1..73f04703 100644 --- a/src/parse/type_decl.c +++ b/src/parse/type_decl.c @@ -86,7 +86,7 @@ ANN static inline Type find(const Env env, Type_Decl *td) { const Type exists = find_type(env, td); if(exists) return exists; const m_uint scope = env->context - ? env_push(env, NULL, env->context->nspc) + ? env_push_nspc(env, env->context->nspc) : env_push_global(env); const bool ret = traverse_fptr_def(env, fptr); env_pop(env, scope); diff --git a/src/plug.c b/src/plug.c index 7d9b292b..7035eded 100644 --- a/src/plug.c +++ b/src/plug.c @@ -191,7 +191,7 @@ ANN static bool start(const Plug plug, const Gwion gwion, const m_str iname, con plug->nspc = new_nspc(gwion->mp, iname); vector_add(&gwion->data->plugs->vec, (m_uint)plug->nspc); set_parent(plug->nspc, gwion); - const m_uint scope = env_push(gwion->env, NULL, plug->nspc); + const m_uint scope = env_push_nspc(gwion->env, plug->nspc); const m_str name = gwion->env->name; gwion->env->name = iname; const bool ret = gwi_run(gwion, plug->plugin);