From a8b0ca8763bdef8e492f1b870cccbbbc73d7f250 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Mon, 6 Apr 2020 14:58:24 +0200 Subject: [PATCH] :art: Simplify free func_def --- ast | 2 +- src/emit/emit.c | 5 +++-- src/lib/instr.c | 2 +- src/parse/check.c | 9 +++++---- src/parse/scan0.c | 6 +++--- src/parse/scan1.c | 5 +++-- src/parse/scan2.c | 12 +++++++----- 7 files changed, 23 insertions(+), 18 deletions(-) diff --git a/ast b/ast index 20493d2b..db6a920a 160000 --- a/ast +++ b/ast @@ -1 +1 @@ -Subproject commit 20493d2bc523d9c28f3b0ce7271bfe6c788b16ad +Subproject commit db6a920ab506312eabe2835f41ca8b02ecec153f diff --git a/src/emit/emit.c b/src/emit/emit.c index 2308b3d7..153a2ab8 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -1860,8 +1860,9 @@ ANN static void emit_fdef_finish(const Emitter emit, const Func_Def fdef) { func->code->memoize = memoize_ini(emit, func); } -ANN static m_bool emit_func_def(const Emitter emit, const Func_Def fdef) { - const Func func = fdef->base->func; +ANN static m_bool emit_func_def(const Emitter emit, const Func_Def f) { + const Func func = f->base->func; + const Func_Def fdef = func->def; const Func former = emit->env->func; if(tmpl_base(fdef->base->tmpl)) return GW_OK; diff --git a/src/lib/instr.c b/src/lib/instr.c index 4b64d823..6ed0c88b 100644 --- a/src/lib/instr.c +++ b/src/lib/instr.c @@ -36,7 +36,7 @@ ANN static Func_Def from_base(const Env env, struct dottmpl_ *const dt, const Ns "template", dt->vt_index); DECL_OO(const Value, v, = nspc_lookup_value0(nspc, sym)) const Func_Def def = cpy_func_def(env->gwion->mp, v->d.func_ref->def); - def->base->tmpl->call = dt->tl; + def->base->tmpl->call = cpy_type_list(env->gwion->mp, dt->tl); def->base->tmpl->base = dt->vt_index; dt->def = def; dt->owner = v->from->owner; diff --git a/src/parse/check.c b/src/parse/check.c index 05130dac..de18b019 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -506,7 +506,7 @@ ANN static Func _find_template_match(const Env env, const Value v, const Exp_Cal Func_Base *fbase = cpy_func_base(env->gwion->mp, base->base); fbase->xid = sym; fbase->tmpl->base = 0; - fbase->tmpl->call = types; + fbase->tmpl->call = cpy_type_list(env->gwion->mp, types); if(template_push_types(env, fbase->tmpl) > 0) { const Fptr_Def fptr = new_fptr_def(env->gwion->mp, fbase, base->flag); if(traverse_fptr_def(env, fptr) > 0 && @@ -543,7 +543,7 @@ ANN static Func _find_template_match(const Env env, const Value v, const Exp_Cal } const Func_Def fdef = (Func_Def)cpy_func_def(env->gwion->mp, value->d.func_ref->def); SET_FLAG(fdef, template); - fdef->base->tmpl->call = types; + fdef->base->tmpl->call = cpy_type_list(env->gwion->mp, types); fdef->base->tmpl->base = i; if((m_func = ensure_tmpl(env, fdef, exp))) break; @@ -1284,8 +1284,9 @@ ANN m_bool check_fdef(const Env env, const Func_Def fdef) { return GW_OK; } -ANN m_bool check_func_def(const Env env, const Func_Def fdef) { - const Func func = fdef->base->func; +ANN m_bool check_func_def(const Env env, const Func_Def f) { + const Func func = f->base->func; + const Func_Def fdef = func->def; assert(func == fdef->base->func); if(env->class_def) // tmpl ? CHECK_BB(check_parent_match(env, fdef)) diff --git a/src/parse/scan0.c b/src/parse/scan0.c index fe8befdf..d35c3760 100644 --- a/src/parse/scan0.c +++ b/src/parse/scan0.c @@ -57,13 +57,13 @@ ANN static void fptr_assign(const Env env, const Fptr_Def fptr) { } static void fptr_def(const Env env, const Fptr_Def fptr) { - const Func_Def def = new_func_def(env->gwion->mp, new_func_base(env->gwion->mp, fptr->base->td, fptr->base->xid, fptr->base->args), - NULL,fptr->base->td->flag, loc_cpy(env->gwion->mp, td_pos(fptr->base->td))); + const Func_Def def = new_func_def(env->gwion->mp, + cpy_func_base(env->gwion->mp, fptr->base), + NULL, fptr->base->td->flag, loc_cpy(env->gwion->mp, td_pos(fptr->base->td))); fptr->base->func = new_func(env->gwion->mp, s_name(fptr->base->xid), def); fptr->value->d.func_ref = fptr->base->func; fptr->base->func->value_ref = fptr->value; fptr->type->e->d.func = fptr->base->func; - def->base->tmpl = fptr->base->tmpl; def->base->func = fptr->base->func; } diff --git a/src/parse/scan1.c b/src/parse/scan1.c index 41535add..4b177e7e 100644 --- a/src/parse/scan1.c +++ b/src/parse/scan1.c @@ -303,8 +303,9 @@ ANN static m_bool scan1_args(const Env env, Arg_List list) { ANN m_bool scan1_fptr_def(const Env env, const Fptr_Def fptr) { if(tmpl_base(fptr->base->tmpl)) return GW_OK; - CHECK_OB((fptr->base->ret_type = known_type(env, fptr->base->td))) - return fptr->base->args ? scan1_args(env, fptr->base->args) : GW_OK; + const Func_Def fdef = fptr->base->func->def; + CHECK_OB((fdef->base->ret_type = known_type(env, fdef->base->td))) + return fdef->base->args ? scan1_args(env, fdef->base->args) : GW_OK; } ANN m_bool scan1_type_def(const Env env, const Type_Def tdef) { diff --git a/src/parse/scan2.c b/src/parse/scan2.c index 16558b78..7ac3000f 100644 --- a/src/parse/scan2.c +++ b/src/parse/scan2.c @@ -70,13 +70,10 @@ ANN static Value scan2_func_assign(const Env env, const Func_Def d, ANN m_bool scan2_fptr_def(const Env env, const Fptr_Def fptr) { if(!tmpl_base(fptr->base->tmpl)) { const Func_Def def = fptr->type->e->d.func->def; - def->base->ret_type = fptr->base->ret_type; - if(fptr->base->args) + if(def->base->args) CHECK_BB(scan2_args(env, def)) - } else { - CHECK_OB(fptr->type) // should happen before + } else SET_FLAG(fptr->type, func); - } return GW_OK; } @@ -332,6 +329,7 @@ ANN static Type func_type(const Env env, const Func func) { t->e->d.func = func; return t; } + ANN2(1,2) static Value func_value(const Env env, const Func f, const Value overload) { const Type t = func_type(env, f); @@ -526,6 +524,10 @@ ANN m_bool scan2_func_def(const Env env, const Func_Def f) { const m_bool ret = scanx_fdef(env, env, f, (_exp_func)scan2_fdef); if(GET_FLAG(f, global)) env_pop(env, scope); + if(GET_FLAG(f, global) || (f->base->tmpl && !f->base->tmpl->call)) { + f->base->func->def = cpy_func_def(env->gwion->mp, f); + f->base->func->def->base->func = f->base->func; + } return ret; } -- 2.43.0