From 154c8725f669bf3fee97376125230ae52fe00c4e Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Sat, 18 May 2019 23:16:36 +0200 Subject: [PATCH] :art: Func cleaning --- src/parse/scan2.c | 92 +++++++++++++++++++++++++---------------------- src/parse/scanx.c | 24 ++++++------- 2 files changed, 62 insertions(+), 54 deletions(-) diff --git a/src/parse/scan2.c b/src/parse/scan2.c index 01809a5b..c1860089 100644 --- a/src/parse/scan2.c +++ b/src/parse/scan2.c @@ -347,8 +347,8 @@ ANN2(1,2) static Value func_value(const Env env, const Func f, } ANN2(1, 2) static m_bool scan2_func_def_template(const Env env, const Func_Def f, const Value overload) { - const m_str func_name = s_name(f->base->xid); - const Func func = scan_new_func(env, f, func_name); + const m_str name = s_name(f->base->xid); + const Func func = scan_new_func(env, f, name); const Value value = func_value(env, func, overload); SET_FLAG(value, checked | ae_flag_template); SET_FLAG(value->type, func); // the only types with func flag, name could be better @@ -366,7 +366,7 @@ ANN2(1, 2) static m_bool scan2_func_def_template(const Env env, const Func_Def f } m_bool ret = compat_func(ff->def, f); if(ret > 0) { - const Symbol sym = func_symbol(env, env->curr->name, func_name, + const Symbol sym = func_symbol(env, env->curr->name, name, "template", ff->vt_index); nspc_add_value(env->curr, sym, value); if(!overload) { @@ -380,7 +380,7 @@ ANN2(1, 2) static m_bool scan2_func_def_template(const Env env, const Func_Def f } } while(type && (type = type->e->parent) && (nspc = type->nspc)); --i; - const Symbol sym = func_symbol(env, env->curr->name, func_name, "template", i); + const Symbol sym = func_symbol(env, env->curr->name, name, "template", i); nspc_add_value(env->curr, sym, value); if(!overload) { func->vt_index = i; @@ -436,7 +436,7 @@ ANN static void scan2_func_def_flag(const Env env, const Func_Def f) { } ANN static m_str func_tmpl_name(const Env env, const Func_Def f) { - const m_str func_name = s_name(f->base->xid); + const m_str name = s_name(f->base->xid); struct Vector_ v; ID_List id = f->tmpl->list; m_uint tlen = 0; @@ -461,14 +461,14 @@ ANN static m_str func_tmpl_name(const Env env, const Func_Def f) { } tmpl_name[tlen+1] = '\0'; vector_release(&v); - const Symbol sym = func_symbol(env, env->curr->name, func_name, tmpl_name, (m_uint)f->tmpl->base); + const Symbol sym = func_symbol(env, env->curr->name, name, tmpl_name, (m_uint)f->tmpl->base); return s_name(sym); } ANN2(1,2,4) static Value func_create(const Env env, const Func_Def f, - const Value overload, const m_str func_name) { - const Func func = scan_new_func(env, f, func_name); + const Value overload, const m_str name) { + const Func func = scan_new_func(env, f, name); nspc_add_func(env->curr, insert_symbol(func->name), func); const Value v = func_value(env, func, overload); scan2_func_def_flag(env, f); @@ -482,56 +482,65 @@ ANN2(1,2,4) static Value func_create(const Env env, const Func_Def f, return v; } + +ANN static m_str template_helper(const Env env, const Func_Def f) { + const m_str name = f->base->func ? f->base->func->name : func_tmpl_name(env, f); + if(!name) + return(m_str)GW_ERROR; + const Func func = nspc_lookup_func1(env->curr, insert_symbol(name)); + if(func) { + if(GET_FLAG(func, member)) + f->stack_depth += SZ_INT; + if(GET_FLAG(func->def, variadic)) + f->stack_depth += SZ_INT; + f->base->ret_type = known_type(env, f->base->td); + return (m_str)(m_uint)((f->base->args && f->base->args->type) ? scan2_args(env, f) : GW_OK); + } + return name; +} + +ANN2(1,2) static m_str func_name(const Env env, const Func_Def f, const Value v) { + if(!f->tmpl) { + const Symbol sym = func_symbol(env, env->curr->name, s_name(f->base->xid), NULL, v ? ++v->offset : 0); + return s_name(sym); + } + return template_helper(env, f); +} + ANN m_bool scan2_func_def(const Env env, const Func_Def f) { - Value value = NULL; - f->stack_depth = 0; - m_uint scope = env->scope->depth; - if(GET_FLAG(f, global)) - scope = env_push_global(env); + const m_uint scope = !GET_FLAG(f, global) ? env->scope->depth : env_push_global(env); const Value overload = nspc_lookup_value0(env->curr, f->base->xid); const Value res = nspc_lookup_value1(env->global_nspc, f->base->xid); - m_str func_name = s_name(f->base->xid); if(res) - ERR_B(f->pos, "'%s' already declared as type", func_name) + ERR_B(f->pos, "'%s' already declared as type", s_name(f->base->xid)) + f->stack_depth = 0; if(overload) CHECK_BB(scan2_func_def_overload(env, f, overload)) if(tmpl_base(f->tmpl)) return scan2_func_def_template(env, f, overload); - if(!f->tmpl) { - const Symbol sym = func_symbol(env, env->curr->name, func_name, NULL, overload ? ++overload->offset : 0); - func_name = s_name(sym); - } else { - if(f->base->func) - func_name = f->base->func->name; - else - CHECK_OB((func_name = func_tmpl_name(env, f))) - const Func func = nspc_lookup_func1(env->curr, insert_symbol(func_name)); - if(func) { - if(GET_FLAG(func, member)) - f->stack_depth += SZ_INT; - if(GET_FLAG(func->def, variadic)) - f->stack_depth += SZ_INT; - f->base->ret_type = known_type(env, f->base->td); - return (f->base->args && f->base->args->type) ? scan2_args(env, f) : GW_OK; - } - } + const m_str name = func_name(env, f, overload); + if((m_int)name <= GW_OK) + return (m_bool)(m_uint)name; +// scan2 prelude const Func base = get_func(env, f); - if(!base) { - CHECK_OB((value = func_create(env, f, overload, func_name))) - } else { + if(!base) + CHECK_OB(func_create(env, f, overload, name)) + else f->base->func = base; -} +// body if(f->base->args) CHECK_BB(scan2_args(env, f)) if(!GET_FLAG(f, builtin) && f->d.code->d.stmt_code.stmt_list) CHECK_BB(scan2_func_def_code(env, f)) +// gpop + if(GET_FLAG(f, global)) + env_pop(env, scope); + if(!base) { if(GET_FLAG(f, op)) CHECK_BB(scan2_func_def_op(env, f)) - SET_FLAG(value, checked); + SET_FLAG(f->base->func->value_ref, checked); } - if(GET_FLAG(f, global)) - env_pop(env, scope); return GW_OK; } @@ -540,9 +549,8 @@ DECL_SECTION_FUNC(scan2) ANN static m_bool scan2_class_parent(const Env env, const Class_Def cdef) { const Type t = cdef->base.type->e->parent->array_depth ? array_base(cdef->base.type->e->parent) : cdef->base.type->e->parent; - if(!GET_FLAG(t, scan2) && GET_FLAG(cdef->base.ext, typedef)) { + if(!GET_FLAG(t, scan2) && GET_FLAG(cdef->base.ext, typedef)) CHECK_BB(scan2_class_def(env, t->e->def)) - } if(cdef->base.ext->array) CHECK_BB(scan2_exp(env, cdef->base.ext->array->exp)) return GW_OK; diff --git a/src/parse/scanx.c b/src/parse/scanx.c index b8723954..50aa1277 100644 --- a/src/parse/scanx.c +++ b/src/parse/scanx.c @@ -15,25 +15,25 @@ ANN static inline m_bool _body(const Env e, Class_Body b, const _exp_func f) { return GW_OK; } +ANN static inline m_bool tmpl_push(const Env env, const Tmpl* tmpl) { + if(tmpl->call && tmpl->call != (Type_List)1) { + CHECK_BB(template_push_types(env, tmpl->list, tmpl->call)) + return GW_OK; + } + return GW_ERROR; +} + ANN static inline m_int _push(const Env env, const Class_Def c) { const m_uint scope = env_push_type(env, c->base.type); - if(c->tmpl) { - if(!c->tmpl->call) - ERR_B(c->pos, - "you must provide template types for type '%s'", s_name(c->base.xid)) - if(c->tmpl->call != (Type_List)1) - CHECK_BB(template_push_types(env, c->tmpl->list, c->tmpl->call)) - } + if(c->tmpl && tmpl_push(env, c->tmpl) < 0) + ERR_B(c->pos, "you must provide template types for type '%s'", + s_name(c->base.xid)) return scope; } ANN static inline void _pop(const Env e, const Class_Def c, const m_uint s) { - if(c->tmpl) { - if(c->tmpl->call != (Type_List)1) + if(c->tmpl && c->tmpl->call != (Type_List)1) nspc_pop_type(e->gwion->mp, e->curr); - else - c->tmpl->call = NULL; - } env_pop(e, s); } -- 2.43.0