From e6c09f9d528e8aaf407bf7405676774d35aaa8ed Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Tue, 22 Oct 2019 16:20:01 +0200 Subject: [PATCH] :art: More import union --- src/lib/import.c | 15 +++++++++++---- tests/import/union_tmpl.c | 21 +++++++++++++++++++++ tests/import/union_tmpl.gw | 2 ++ tests/import/union_tmpl_fail.c | 21 +++++++++++++++++++++ tests/sh/import.sh | 2 +- 5 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 tests/import/union_tmpl.c create mode 100644 tests/import/union_tmpl.gw create mode 100644 tests/import/union_tmpl_fail.c diff --git a/src/lib/import.c b/src/lib/import.c index a9c4fce8..b61f37b9 100644 --- a/src/lib/import.c +++ b/src/lib/import.c @@ -742,13 +742,15 @@ ANN2(1) m_int gwi_union_ini(const Gwi gwi, const m_str type, const m_str name) { ANN m_int gwi_union_add(const Gwi gwi, const restrict m_str type, const restrict m_str name) { DECL_OB(const Exp, exp, = make_exp(gwi, type, name)) +/* const Type t = known_type(gwi->gwion->env, exp->d.exp_decl.td); if(!t) { free_exp(gwi->gwion->mp, exp); return GW_ERROR; } if(isa(t, gwi->gwion->type[et_object]) > 0) - SET_FLAG(exp->d.exp_decl.td, ref); +*/ + SET_FLAG(exp->d.exp_decl.td, ref); gwi->union_data.list = new_decl_list(gwi->gwion->mp, exp, gwi->union_data.list); return GW_OK; } @@ -756,7 +758,8 @@ ANN m_int gwi_union_add(const Gwi gwi, const restrict m_str type, const restrict ANN static Type union_type(const Gwi gwi, const Union_Def udef) { CHECK_BO(scan0_union_def(gwi->gwion->env, udef)) CHECK_BO(traverse_union_def(gwi->gwion->env, udef)) - emit_union_offset(udef->l, udef->o); + if(!udef->tmpl) + emit_union_offset(udef->l, udef->o); if(gwi->gwion->env->class_def && !GET_FLAG(udef, static)) gwi->gwion->env->class_def->nspc->info->offset = udef->o + udef->s; @@ -778,12 +781,16 @@ ANN Type gwi_union_end(const Gwi gwi, const ae_flag flag) { if(gwi->union_data.type_name) CHECK_BO(check_typename_def(gwi, &ck)) const Symbol xid = gwi->union_data.name ? insert_symbol(gwi->gwion->st, gwi->union_data.name) : NULL; - const Symbol type_xid = gwi->union_data.type_name ? insert_symbol(gwi->gwion->st, gwi->union_data.type_name) : NULL; + const Symbol type_xid = gwi->union_data.type_name ? insert_symbol(gwi->gwion->st, ck.name) : NULL; const Union_Def udef = new_union_def(gwi->gwion->mp, gwi->union_data.list, loc_cpy(gwi->gwion->mp, gwi->loc)); udef->flag = flag; udef->xid = xid; udef->type_xid = type_xid; - udef->tmpl = ck.tmpl ? new_tmpl(gwi->gwion->mp, ck.tmpl, -1) : NULL; + if(ck.tmpl) { + if(udef->xid) + GWI_ERR_O(_("Template union type can't declare union")); + udef->tmpl = new_tmpl(gwi->gwion->mp, ck.tmpl, -1); + } const Type t = union_type(gwi, udef); if(!SAFE_FLAG(t, template)) free_union_def(gwi->gwion->mp, udef); diff --git a/tests/import/union_tmpl.c b/tests/import/union_tmpl.c new file mode 100644 index 00000000..0de2647a --- /dev/null +++ b/tests/import/union_tmpl.c @@ -0,0 +1,21 @@ +#include "gwion_util.h" +#include "gwion_ast.h" +#include "oo.h" +#include "vm.h" +#include "env.h" +#include "type.h" +#include "object.h" +#include "instr.h" +#include "gwion.h" +#include "value.h" +#include "operator.h" +#include "import.h" + +GWION_IMPORT(union_test) { + GWI_BB(gwi_union_ini(gwi, "<~A~>U", NULL)) + GWI_BB(gwi_union_add(gwi,"float", "f")) + GWI_BB(gwi_union_add(gwi,"int", "i")) + GWI_BB(gwi_union_add(gwi,"A", "a")) + GWI_OB(gwi_union_end(gwi, 0)) + return GW_OK; +} diff --git a/tests/import/union_tmpl.gw b/tests/import/union_tmpl.gw new file mode 100644 index 00000000..3650e773 --- /dev/null +++ b/tests/import/union_tmpl.gw @@ -0,0 +1,2 @@ +<<< <~Event~>U u>>>; +<<< u.a >>>; diff --git a/tests/import/union_tmpl_fail.c b/tests/import/union_tmpl_fail.c new file mode 100644 index 00000000..cebc664e --- /dev/null +++ b/tests/import/union_tmpl_fail.c @@ -0,0 +1,21 @@ +#include "gwion_util.h" +#include "gwion_ast.h" +#include "oo.h" +#include "vm.h" +#include "env.h" +#include "type.h" +#include "object.h" +#include "instr.h" +#include "gwion.h" +#include "value.h" +#include "operator.h" +#include "import.h" + +GWION_IMPORT(union_test) { + GWI_BB(gwi_union_ini(gwi, "<~A~>U", "My")) + GWI_BB(gwi_union_add(gwi,"float", "f")) + GWI_BB(gwi_union_add(gwi,"int", "i")) + GWI_BB(gwi_union_add(gwi,"A", "a")) + GWI_OB(gwi_union_end(gwi, 0)) + return GW_OK; +} diff --git a/tests/sh/import.sh b/tests/sh/import.sh index ca0a55f6..3c7baa29 100644 --- a/tests/sh/import.sh +++ b/tests/sh/import.sh @@ -1,5 +1,5 @@ #!/bin/bash -# [test] #62 +# [test] #64 n=0 [ "$1" ] && n="$1" -- 2.43.0