From 3ec3d7947401f5dd717d6ee147674ca9e28cf8b2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Fri, 18 Mar 2022 00:35:38 +0100 Subject: [PATCH] :art: more mp_vectors --- include/env/env.h | 2 -- src/env/env.c | 18 +++++++----- src/lib/lib_func.c | 26 ++++++++--------- src/parse/check.c | 70 +++++++++++++++++++++------------------------- 4 files changed, 55 insertions(+), 61 deletions(-) diff --git a/include/env/env.h b/include/env/env.h index 0e550a0a..a93cab94 100644 --- a/include/env/env.h +++ b/include/env/env.h @@ -72,6 +72,4 @@ struct ScopeEffect { ANN void env_add_effect(const Env a, const Symbol effect, const loc_t pos); ANN void call_add_effect(const Env env, const Func func, const loc_t pos); -ANN bool check_effect_overload(const Vector base, const Func override); - #endif diff --git a/src/env/env.c b/src/env/env.c index b22310c6..539eeba1 100644 --- a/src/env/env.c +++ b/src/env/env.c @@ -62,8 +62,8 @@ ANN static void free_env_scope(struct Env_Scope_ *a, Gwion gwion) { vector_release(&a->conts); const Vector v = &a->effects; for (m_uint i = 0; i < vector_size(v); i++) { - m_uint _v = vector_at(v, i); - if (_v) m_vector_release((M_Vector)&_v); + MP_Vector *eff = (MP_Vector*)vector_at(v, i); + if (eff) free_mp_vector(gwion->mp, sizeof(struct ScopeEffect), eff); } vector_release(&a->effects); mp_free(gwion->mp, Env_Scope, a); @@ -71,10 +71,14 @@ ANN static void free_env_scope(struct Env_Scope_ *a, Gwion gwion) { ANN void env_add_effect(const Env a, const Symbol effect, const loc_t pos) { const Vector v = &a->scope->effects; - const M_Vector w = (M_Vector)&VPTR(v, VLEN(v) - 1); - if (!w->ptr) m_vector_init(w, sizeof(struct ScopeEffect), 0); + MP_Vector *w = (MP_Vector*)vector_back(v); + if (!w) { + w = new_mp_vector(a->gwion->mp, sizeof(struct ScopeEffect), 0); + VPTR(v, VLEN(v) - 1) = (vtype)w; + } struct ScopeEffect eff = {effect, pos}; - m_vector_add(w, &eff); +// mp_vector_add(a->gwion->mp, &w, struct ScopeEffect, eff); + mp_vector_add(a->gwion->mp, (MP_Vector**)&(VPTR(v, VLEN(v) - 1)) , struct ScopeEffect, eff); } ANN void free_env(const Env a) { @@ -98,8 +102,8 @@ ANN2(1, 3) m_uint env_push(const Env env, const Type type, const Nspc nspc) { ANN void env_pop(const Env env, const m_uint scope) { env->class_def = (Type)vector_pop(&env->scope->class_stack); env->curr = (Nspc)vector_pop(&env->scope->nspc_stack); - const m_uint _v = vector_pop(&env->scope->effects); - if (_v) m_vector_release((M_Vector)&_v); + MP_Vector *const v = (MP_Vector*)vector_pop(&env->scope->effects); + if (v) free_mp_vector(env->gwion->mp, sizeof(struct ScopeEffect), v); env->scope->depth = scope; } diff --git a/src/lib/lib_func.c b/src/lib/lib_func.c index b8c33a10..f0af5559 100644 --- a/src/lib/lib_func.c +++ b/src/lib/lib_func.c @@ -550,6 +550,7 @@ static inline void op_impl_ensure_types(const Env env, const Func func) { } #include "tmp_resolve.h" + static OP_CHECK(opck_op_impl) { struct Implicit *impl = (struct Implicit *)data; const Func func = impl->t->info->func; @@ -583,10 +584,10 @@ static OP_CHECK(opck_op_impl) { vector_add(&env->scope->effects, 0); DECL_ON(const Type, t, = op_check(env, &opi)); CHECK_BN(isa(t, func->def->base->ret_type)); // error message? - const m_uint _eff = vector_back(&env->scope->effects); - if (!check_effect_overload((Vector)&_eff, func)) - ERR_N(impl->pos, _("`{+Y}%s{0}` has effects not present in `{+G}%s{0}`\n"), - s_name(impl->e->d.prim.d.var), func->name); + MP_Vector *const eff = (MP_Vector*)vector_back(&env->scope->effects); +// if (eff && !check_effect_overload(eff, func)) +// ERR_N(impl->pos, _("`{+Y}%s{0}` has effects not present in `{+G}%s{0}`\n"), +// s_name(impl->e->d.prim.d.var), func->name); 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); @@ -595,9 +596,8 @@ static OP_CHECK(opck_op_impl) { const Func exists = (Func)find_func_match(env, v->d.func_ref, &call); env_pop(env, scope); if (exists) { // improve me - if (_eff) { - const M_Vector eff = (M_Vector)&_eff; - m_vector_release(eff); + if (eff) { + free_mp_vector(env->gwion->mp, sizeof(struct ScopeEffect), eff); ERR_N(impl->pos, _("`{+Y}%s{0}` has effects not present in `{+G}%s{0}`\n"), s_name(impl->e->d.prim.d.var), func->name); @@ -613,14 +613,12 @@ static OP_CHECK(opck_op_impl) { Func_Base *base = new_func_base(env->gwion->mp, type2td(env->gwion, t, impl->e->pos), impl->e->d.prim.d.var, args, ae_flag_none, impl->e->pos); - if (_eff) { - const M_Vector eff = (M_Vector)&_eff; - for (m_uint i = 0; i < m_vector_size(eff); i++) { - struct ScopeEffect effect; - m_vector_get(eff, i, &effect); - vector_add(&base->effects, (m_uint)effect.sym); + if (eff) { + for (m_uint i = 0; i < eff->len; i++) { + struct ScopeEffect *effect = mp_vector_at(eff, struct ScopeEffect, i); + vector_add(&base->effects, (m_uint)effect->sym); } - m_vector_release(eff); + free_mp_vector(env->gwion->mp, sizeof(struct ScopeEffect), eff); } const Exp lhs = new_prim_id(env->gwion->mp, larg0->var_decl.xid, impl->e->pos); diff --git a/src/parse/check.c b/src/parse/check.c index cadf1f17..f28b5641 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -1440,16 +1440,14 @@ ANN static inline m_bool _check_stmt_try(const restrict Env env, const Stmt_Try CHECK_BB(check_handler_list(env, stmt->handler)); vector_add(&env->scope->effects, 0); const m_bool ret = check_stmt_try_start(env, stmt); - const m_uint _v = vector_pop(&env->scope->effects); - if (_v) { - const M_Vector v = (M_Vector)&_v; - for (m_uint i = 0; i < m_vector_size(v); i++) { - struct ScopeEffect eff; - m_vector_get(v, i, &eff); - bool found = find_handler(stmt->handler, eff.sym); - if (!found) env_add_effect(env, eff.sym, eff.pos); + MP_Vector *const v = (MP_Vector*)vector_pop(&env->scope->effects); + if (v) { + for (m_uint i = 0; i < v->len; i++) { + struct ScopeEffect *eff = mp_vector_at(v, struct ScopeEffect, i); + bool found = find_handler(stmt->handler, eff->sym); + if (!found) env_add_effect(env, eff->sym, eff->pos); } - m_vector_release(v); + free_mp_vector(env->gwion->mp, sizeof(struct ScopeEffect), v); } return ret; } @@ -1596,12 +1594,12 @@ ANN static m_bool check_func_overload(const Env env, const Func_Def fdef) { return GW_OK; } -ANN bool check_effect_overload(const Vector base, const Func override) { +ANN static bool check_effect_overload(const Vector base, const Func override) { if (!base->ptr) return true; - if (!override->def->base->effects.ptr) return false; // TODO: error message + if (!override->def->base->effects.ptr) return false; const Vector v = &override->def->base->effects; for (m_uint i = 0; i < vector_size(v); i++) { - if (vector_find((Vector)base, vector_at(v, i)) == -1) return false; + if (vector_find(base, vector_at(v, i)) == -1) return false; } return true; } @@ -1643,9 +1641,9 @@ ANN m_bool check_fdef(const Env env, const Func_Def fdef) { return GW_OK; } -ANN static bool effect_find(const M_Vector v, const Symbol sym) { - for(m_uint i = 0; i < m_vector_size(v); i++) { - struct ScopeEffect *eff = (struct ScopeEffect *)(ARRAY_PTR(v) + ARRAY_SIZE(v) * i); +ANN static bool effect_find(const MP_Vector *v, const Symbol sym) { + for(m_uint i = 0; i < v->len; i++) { + struct ScopeEffect *eff = mp_vector_at(v, struct ScopeEffect, i); if(eff->sym == sym) return true; } return false; @@ -1677,19 +1675,19 @@ ANN m_bool _check_func_def(const Env env, const Func_Def f) { } vector_add(&env->scope->effects, 0); const m_bool ret = scanx_fdef(env, env, fdef, (_exp_func)check_fdef); - const m_uint _v = vector_back(&env->scope->effects); - if (_v) { + MP_Vector *v = (MP_Vector*)vector_back(&env->scope->effects); + if (v) { if (fdef->base->xid == insert_symbol("@dtor")) ERR_B(fdef->base->pos, _("can't use effects in destructors")); - const M_Vector v = (M_Vector)&_v; const Vector base = &fdef->base->effects; if (!base->ptr) vector_init(base); - for (m_uint i = 0; i < m_vector_size(v); i++) { - struct ScopeEffect *eff = (struct ScopeEffect *)(ARRAY_PTR(v) + ARRAY_SIZE(v) * i); + for (uint32_t i = 0; i < v->len; i++) { + struct ScopeEffect *eff = mp_vector_at(v, struct ScopeEffect, i); +//(struct ScopeEffect *)(ARRAY_PTR(v) + ARRAY_SIZE(v) * i); if(!effect_find(v, eff->sym)) vector_add(base, (m_uint)eff->sym); } - m_vector_release(v); + free_mp_vector(env->gwion->mp, sizeof(struct ScopeEffect), v); } vector_pop(&env->scope->effects); if (fbflag(fdef->base, fbflag_op)) operator_resume(&opi); @@ -1809,16 +1807,14 @@ ANN m_bool check_abstract(const Env env, const Class_Def cdef) { ANN static inline void ctor_effects(const Env env) { const Vector v = &env->scope->effects; - const m_uint _w = vector_back(v); - if (!_w) return; + MP_Vector *const w = (MP_Vector*)vector_back(v); + if (!w) return; vector_init(&env->class_def->effects); - const M_Vector w = (M_Vector)&_w; - for (m_uint j = 0; j < m_vector_size(w); j++) { - struct ScopeEffect eff; - m_vector_get(w, j, &eff); - vector_add(&env->class_def->effects, (m_uint)eff.sym); + for (uint32_t j = 0; j < w->len; j++) { + struct ScopeEffect *eff = mp_vector_at(w, struct ScopeEffect, j); + vector_add(&env->class_def->effects, (m_uint)eff->sym); } - m_vector_release(w); + free_mp_vector(env->gwion->mp, sizeof(struct ScopeEffect), w); vector_pop(v); } @@ -1981,18 +1977,16 @@ ANN m_bool check_class_def(const Env env, const Class_Def cdef) { ANN static inline void check_unhandled(const Env env) { const Vector v = &env->scope->effects; - const m_uint _w = vector_back(v); - if (!_w) return; - const M_Vector w = (M_Vector)&_w; - for (m_uint j = 0; j < m_vector_size(w); j++) { - struct ScopeEffect eff; - m_vector_get(w, j, &eff); - if(s_name(eff.sym)[0] == '!') + MP_Vector *const w = (MP_Vector*)vector_back(v); + if(!w) return; + for (m_uint j = 0; j < w->len; j++) { + struct ScopeEffect *eff = mp_vector_at(w, struct ScopeEffect, j); + if(s_name(eff->sym)[0] == '!') continue; - gwerr_secondary("Unhandled effect", env->name, eff.pos); + gwerr_secondary("Unhandled effect", env->name, eff->pos); env->context->error = false; } - m_vector_release(w); + free_mp_vector(env->gwion->mp, sizeof(struct ScopeEffect), w); vector_pop(v); } -- 2.43.0