From b5a096368f96569aef550043132de627cdd060eb Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Tue, 15 Dec 2020 15:48:32 +0100 Subject: [PATCH] :art: More 2str or str2 functions --- include/env/env.h | 5 ++++- src/lib/tmpl_info.c | 17 ++++++++++++----- src/parse/func_resolve_tmpl.c | 11 ++++++++--- src/parse/type_decl.c | 19 ++++++++++++++++++- 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/include/env/env.h b/include/env/env.h index 41c76f8b..67c26ddc 100644 --- a/include/env/env.h +++ b/include/env/env.h @@ -35,8 +35,11 @@ ANN void env_pop(const Env, const m_uint); ANN Map env_label(const Env); ANN Type scan_type(const Env, const Type, Type_Decl*); ANN Value mk_class(const Env env, const Type base); -// tl2str returns a mp_alloced string + +// those functions return a mp_alloced string ANEW ANN m_str tl2str(const Env, const Type_List); // in type_decl.c +ANEW ANN m_str type2str(const Env env, const Type t); + ANN m_bool compat_func(const __restrict__ Func_Def, const __restrict__ Func_Def); ANN Type known_type(const Env env, Type_Decl*); ANN Type prim_ref(const Env env, const Type t, const Type_Decl* td); diff --git a/src/lib/tmpl_info.c b/src/lib/tmpl_info.c index 47cd61f5..f9297e7e 100644 --- a/src/lib/tmpl_info.c +++ b/src/lib/tmpl_info.c @@ -40,19 +40,26 @@ ANN static inline size_t tmpl_set(struct tmpl_info* info, const m_str str) { ANN static ssize_t template_size(const Env env, struct tmpl_info* info) { DECL_OB(const m_str, str, = tl2str(env, info->td->types)) - return tmpl_set(info, str) + tmpl_set(info, info->base->name) + 4; + const m_str base = type2str(env, info->base); + return tmpl_set(info, str) + tmpl_set(info, base) + 4; +} + +ANEW ANN static Symbol _template_id(const Env env, struct tmpl_info *const info, const size_t sz) { + char name[sz]; + template_name(info, name); + return insert_symbol(name); } ANEW ANN static Symbol template_id(const Env env, struct tmpl_info *const info) { vector_init(&info->type); vector_init(&info->size); ssize_t sz = template_size(env, info); - char name[sz]; - if(sz > GW_ERROR) - template_name(info, name); + const Symbol sym = sz > GW_ERROR ? _template_id(env, info, sz) : NULL; + for(m_uint i = 0; i < vector_size(&info->type); ++i) + mp_free2(env->gwion->mp, vector_at(&info->size, i), (m_str)vector_at(&info->type, i)); vector_release(&info->type); vector_release(&info->size); - return sz > GW_ERROR ? insert_symbol(name) : NULL; + return sym; } ANN static m_bool template_match(ID_List base, Type_List call) { diff --git a/src/parse/func_resolve_tmpl.c b/src/parse/func_resolve_tmpl.c index 46a61bc4..1d4bf397 100644 --- a/src/parse/func_resolve_tmpl.c +++ b/src/parse/func_resolve_tmpl.c @@ -107,10 +107,9 @@ ANN static Func func_match(const Env env, struct ResolverArgs* ra) { return NULL; } -ANN static Func _find_template_match(const Env env, const Value v, const Exp_Call* exp) { +ANN static Func find_tmpl(const Env env, const Value v, const Exp_Call* exp, const m_str tmpl_name) { const Type_List types = exp->tmpl->call; const Func former = env->func; - DECL_OO(const m_str, tmpl_name, = tl2str(env, types)) const m_uint scope = env->scope->depth; struct EnvSet es = { .env=env, .data=env, .func=(_exp_func)check_cdef, .scope=scope, .flag=tflag_check }; @@ -119,7 +118,6 @@ ANN static Func _find_template_match(const Env env, const Value v, const Exp_Cal (void)env_push(env, v->from->owner_class, v->from->owner); const Func m_func = !is_fptr(env->gwion, v->type) ? func_match(env, &ra) :fptr_match(env, &ra); - free_mstr(env->gwion->mp, tmpl_name); if(es.run) envset_pop(&es, v->from->owner_class); env_pop(env, scope); @@ -127,6 +125,13 @@ ANN static Func _find_template_match(const Env env, const Value v, const Exp_Cal return m_func; } +ANN static Func _find_template_match(const Env env, const Value v, const Exp_Call* exp) { + DECL_OO(const m_str, tmpl_name, = tl2str(env, exp->tmpl->call)) + const Func f = find_tmpl(env, v, exp, tmpl_name); + free_mstr(env->gwion->mp, tmpl_name); + return f; +} + ANN static inline m_bool check_call(const Env env, const Exp_Call* exp) { const ae_exp_t et = exp->func->exp_type; if(et != ae_exp_primary && et != ae_exp_dot && et != ae_exp_cast) diff --git a/src/parse/type_decl.c b/src/parse/type_decl.c index becf93b1..a024e196 100644 --- a/src/parse/type_decl.c +++ b/src/parse/type_decl.c @@ -33,17 +33,34 @@ struct td_info { GwText text; }; +ANN static void td_fullname(const Env env, GwText *text, const Type t) { + const Type owner = t->info->owner_class; + if(owner) { + td_fullname(env, text, owner); + text_add(text, "."); + } + text_add(text, t->name); +} + ANN static m_bool td_info_run(const Env env, struct td_info* info) { Type_List tl = info->tl; do { DECL_OB(const Type, t, = known_type(env, tl->td)) - text_add(&info->text, t->name); + td_fullname(env, &info->text, t); if(tl->next) text_add(&info->text, ","); } while((tl = tl->next)); return GW_OK; } +ANEW ANN m_str type2str(const Env env, const Type t) { + GwText text = { .mp=env->gwion->mp }; + if(t->info->owner_class) + td_fullname(env, &text, t->info->owner_class); + text_add(&text, t->name); + return text.str; +} + ANEW ANN m_str tl2str(const Env env, Type_List tl) { struct td_info info = { .tl=tl, { .mp=env->gwion->mp} }; CHECK_BO(td_info_run(env, &info)) -- 2.43.0