From 85d40cea9197c6a9babe6a5626582c39d1352fec Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Tue, 22 Oct 2019 12:49:58 +0200 Subject: [PATCH] :art: Start templating Union import --- include/gwi.h | 3 ++- include/import.h | 2 +- src/lib/import.c | 22 +++++++++++++++++----- src/lib/shred.c | 2 +- src/lib/vararg.c | 2 +- tests/import/empty_union.c | 2 +- tests/import/typedef_tmpl.c | 18 ++++++++++++++++++ tests/import/union.c | 2 +- tests/import/union_fail_exp.c | 2 +- tests/import/union_member.c | 2 +- tests/sh/import.sh | 2 +- 11 files changed, 45 insertions(+), 14 deletions(-) create mode 100644 tests/import/typedef_tmpl.c diff --git a/include/gwi.h b/include/gwi.h index b9c6f5b1..d638a806 100644 --- a/include/gwi.h +++ b/include/gwi.h @@ -40,7 +40,8 @@ typedef struct { } DL_Enum; typedef struct { - Symbol xid; + m_str type_name; + m_str name; Decl_List list; } DL_Union; diff --git a/include/import.h b/include/import.h index db21018f..8a30e93c 100644 --- a/include/import.h +++ b/include/import.h @@ -54,7 +54,7 @@ ANN Type gwi_typedef_end(const Gwi gwi, const ae_flag flag); ANN m_int gwi_tmpl_ini(const Gwi gwi, const m_uint n, const m_str *name); ANN m_int gwi_tmpl_end(const Gwi gwi); -ANN2(1) m_int gwi_union_ini(const Gwi gwi, const m_str name); +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); ANN Type gwi_union_end(const Gwi gwi, const ae_flag flag); diff --git a/src/lib/import.c b/src/lib/import.c index 4df6d1e2..9fdbf0c4 100644 --- a/src/lib/import.c +++ b/src/lib/import.c @@ -734,9 +734,9 @@ ANN static Exp make_exp(const Gwi gwi, const m_str type, const m_str name) { return new_exp_decl(env->gwion->mp, type_decl, var_decl_list); } -ANN2(1) m_int gwi_union_ini(const Gwi gwi, const m_str name) { - if(name) - gwi->union_data.xid = insert_symbol(gwi->gwion->st, name); +ANN2(1) m_int gwi_union_ini(const Gwi gwi, const m_str type, const m_str name) { + gwi->union_data.type_name = type; + gwi->union_data.name = name; return GW_OK; } @@ -766,12 +766,24 @@ ANN static Type union_type(const Gwi gwi, const Union_Def udef) { ANN Type gwi_union_end(const Gwi gwi, const ae_flag flag) { if(!gwi->union_data.list) GWI_ERR_O(_("union is empty")); + if(gwi->union_data.name) + CHECK_BO(name_valid(gwi, gwi->union_data.name)) + struct func_checker ck = { .name=gwi->union_data.type_name, .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 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; const Type t = union_type(gwi, udef); - free_union_def(gwi->gwion->mp, udef); + if(!SAFE_FLAG(t, template)) + free_union_def(gwi->gwion->mp, udef); gwi->union_data.list = NULL; - gwi->union_data.xid = NULL; + gwi->union_data.name = NULL; + gwi->union_data.type_name = NULL; return t; } diff --git a/src/lib/shred.c b/src/lib/shred.c index ba8bd843..b8f773ee 100644 --- a/src/lib/shred.c +++ b/src/lib/shred.c @@ -322,7 +322,7 @@ GWION_IMPORT(shred) { gwi_item_ini(gwi, "int", "@orig"); GWI_BB((o_fork_orig = gwi_item_end(gwi, ae_flag_const, NULL))) o_fork_retval = t_fork->nspc->info->offset; - GWI_BB(gwi_union_ini(gwi, NULL)) + GWI_BB(gwi_union_ini(gwi, NULL, NULL)) GWI_BB(gwi_union_add(gwi, "int", "i")) GWI_BB(gwi_union_add(gwi, "float", "f")) GWI_BB(gwi_union_add(gwi, "Vec3", "v")) diff --git a/src/lib/vararg.c b/src/lib/vararg.c index e6996c21..204dd87b 100644 --- a/src/lib/vararg.c +++ b/src/lib/vararg.c @@ -100,7 +100,7 @@ GWION_IMPORT(vararg) { GWI_BB(gwi_set_global_type(gwi, t_varloop, et_varloop)) GWI_BB(gwi_class_ini(gwi, t_vararg, NULL, NULL)) gwi->gwion->type[et_vararg] = t_vararg; - GWI_BB(gwi_union_ini(gwi, NULL)) + GWI_BB(gwi_union_ini(gwi, NULL, NULL)) GWI_BB(gwi_union_add(gwi, "@VarLoop", "start")) GWI_BB(gwi_union_add(gwi, "@VarLoop", "end")) GWI_BB(gwi_union_add(gwi, "int", "i")) diff --git a/tests/import/empty_union.c b/tests/import/empty_union.c index 1f20d554..bb1a9346 100644 --- a/tests/import/empty_union.c +++ b/tests/import/empty_union.c @@ -12,7 +12,7 @@ #include "import.h" GWION_IMPORT(empty_union_test) { - GWI_BB(gwi_union_ini(gwi, NULL)) + GWI_BB(gwi_union_ini(gwi, NULL, NULL)) GWI_OB(gwi_union_end(gwi, 0)) return GW_OK; } diff --git a/tests/import/typedef_tmpl.c b/tests/import/typedef_tmpl.c new file mode 100644 index 00000000..2a48bedd --- /dev/null +++ b/tests/import/typedef_tmpl.c @@ -0,0 +1,18 @@ +#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(typedef_test) { + GWI_OB(gwi_typedef_ini(gwi, "int", "<~A~>Typedef")) + GWI_OB(gwi_typedef_end(gwi, ae_flag_none)) + return GW_OK; +} diff --git a/tests/import/union.c b/tests/import/union.c index f86a986e..b76c3250 100644 --- a/tests/import/union.c +++ b/tests/import/union.c @@ -12,7 +12,7 @@ #include "import.h" GWION_IMPORT(union_test) { - GWI_BB(gwi_union_ini(gwi, NULL)) + GWI_BB(gwi_union_ini(gwi, NULL, NULL)) GWI_BB(gwi_union_add(gwi,"float", "f")) GWI_BB(gwi_union_add(gwi,"int", "i")) GWI_OB(gwi_union_end(gwi, 0)) diff --git a/tests/import/union_fail_exp.c b/tests/import/union_fail_exp.c index 7ecf58e4..32d43199 100644 --- a/tests/import/union_fail_exp.c +++ b/tests/import/union_fail_exp.c @@ -12,7 +12,7 @@ #include "import.h" GWION_IMPORT(union_test) { - GWI_BB(gwi_union_ini(gwi, NULL)) + GWI_BB(gwi_union_ini(gwi, NULL, NULL)) GWI_BB(gwi_union_add(gwi,"Float", "f")) GWI_BB(gwi_union_add(gwi,"int", "i")) GWI_OB(gwi_union_end(gwi, 0)) diff --git a/tests/import/union_member.c b/tests/import/union_member.c index a0028944..7fcdb934 100644 --- a/tests/import/union_member.c +++ b/tests/import/union_member.c @@ -14,7 +14,7 @@ GWION_IMPORT(union_member) { const Type t_unionmember = gwi_mk_type(gwi, "UnionMember", SZ_INT, "Object"); GWI_BB(gwi_class_ini(gwi, t_unionmember, NULL, NULL)) - GWI_BB(gwi_union_ini(gwi, "U")) + GWI_BB(gwi_union_ini(gwi, "U", NULL)) GWI_BB(gwi_union_add(gwi,"float", "f")) GWI_BB(gwi_union_add(gwi,"int[]", "i")) GWI_OB(gwi_union_end(gwi, ae_flag_none)) diff --git a/tests/sh/import.sh b/tests/sh/import.sh index 6edaf1a9..0d4e0450 100644 --- a/tests/sh/import.sh +++ b/tests/sh/import.sh @@ -1,5 +1,5 @@ #!/bin/bash -# [test] #60 +# [test] #61 n=0 [ "$1" ] && n="$1" -- 2.43.0