From 79d106658cf6be5e1b9a925a124e585d04f20274 Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Wed, 11 Nov 2020 20:02:00 +0100 Subject: [PATCH] :art: Rewrite ensure_tmpl --- include/env/func.h | 5 +++++ src/parse/func_resolve_tmpl.c | 31 ++++++++++++++++--------------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/include/env/func.h b/include/env/func.h index b0b6203c..6196de7a 100644 --- a/include/env/func.h +++ b/include/env/func.h @@ -29,6 +29,11 @@ ANN static inline void func_remref(const Func f, struct Gwion_ *const gwion) { i static inline int fflag(const Func f, const enum fflag flag) { return (f->fflag & flag) == flag; } + +static inline int safe_fflag(const Func f, const enum fflag flag) { + return f ? ((f->fflag & flag) == flag) : 0; +} + #ifndef __cplusplus static inline void set_fflag(const Func f, const enum fflag flag) { f->fflag |= flag; diff --git a/src/parse/func_resolve_tmpl.c b/src/parse/func_resolve_tmpl.c index 5a5b6a37..2f164e41 100644 --- a/src/parse/func_resolve_tmpl.c +++ b/src/parse/func_resolve_tmpl.c @@ -39,21 +39,22 @@ ANN static m_bool check_call(const Env env, const Exp_Call* exp) { return GW_OK; } +ANN static inline m_bool tmpl_valid(const Env env, const Func_Def fdef) { + return safe_fflag(fdef->base->func, fflag_valid) || + check_traverse_fdef(env, fdef) > 0; +} + ANN static Func ensure_tmpl(const Env env, const Func_Def fdef, const Exp_Call *exp) { - const m_bool ret = (fdef->base->func && fflag(fdef->base->func, fflag_valid)) || check_traverse_fdef(env, fdef) > 0; - if(ret) { - const Func f = fdef->base->func; - const Func next = f->next; - f->next = NULL; - const Func func = find_func_match(env, f, exp->args); - f->next = next; - if(func) { - set_fflag(func, fflag_tmpl); - set_fflag(func, fflag_valid); - return func; - } - } - return NULL; + if(!tmpl_valid(env, fdef)) + return NULL; + const Func f = fdef->base->func; + const Func next = f->next; + f->next = NULL; + const Func func = find_func_match(env, f, exp->args); + f->next = next; + if(func) + set_fflag(func, fflag_tmpl | fflag_valid); + return func; } ANN static Func fptr_match(const Env env, struct ResolverArgs* ra) { @@ -146,7 +147,7 @@ ANN Func find_template_match(const Env env, const Value value, const Exp_Call* e return f; Type t = value->from->owner_class; while(t && t->nspc) { - Func_Def fdef = value->d.func_ref ? value->d.func_ref->def : value->type->info->func->def; + const Func_Def fdef = value->d.func_ref ? value->d.func_ref->def : value->type->info->func->def; const Value v = nspc_lookup_value0(t->nspc, fdef->base->xid); if(v) { const Func f = _find_template_match(env, v, exp); -- 2.43.0