]> Nishi Git Mirror - gwion.git/commitdiff
:art: More import union
authorfennecdjay <astor.jeremie@wanadoo.fr>
Tue, 22 Oct 2019 14:20:01 +0000 (16:20 +0200)
committerfennecdjay <astor.jeremie@wanadoo.fr>
Tue, 22 Oct 2019 14:20:01 +0000 (16:20 +0200)
src/lib/import.c
tests/import/union_tmpl.c [new file with mode: 0644]
tests/import/union_tmpl.gw [new file with mode: 0644]
tests/import/union_tmpl_fail.c [new file with mode: 0644]
tests/sh/import.sh

index a9c4fce83cc41d37d34c8297eebcbdca57dc1bff..b61f37b9cb2555f8b431d23986b3a557cf0c5128 100644 (file)
@@ -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 (file)
index 0000000..0de2647
--- /dev/null
@@ -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 (file)
index 0000000..3650e77
--- /dev/null
@@ -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 (file)
index 0000000..cebc664
--- /dev/null
@@ -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;
+}
index ca0a55f6db3e1a9c1d9f63e5f2caad3832dee36c..3c7baa29fb0b41a4e7a56c47a70137c320520a93 100644 (file)
@@ -1,5 +1,5 @@
 #!/bin/bash
-# [test] #62
+# [test] #64
 
 n=0
 [ "$1" ] && n="$1"