]> Nishi Git Mirror - gwion.git/commitdiff
:art: Refactor nspc
authorfennecdjay <astor.jeremie@wanadoo.fr>
Wed, 6 Mar 2019 17:05:50 +0000 (18:05 +0100)
committerfennecdjay <astor.jeremie@wanadoo.fr>
Wed, 6 Mar 2019 17:05:50 +0000 (18:05 +0100)
include/nspc.h
src/emit/emit.c
src/lib/import.c
src/lib/object.c
src/oo/env.c
src/oo/nspc.c
src/parse/check.c
src/parse/operator.c

index 07c2d5dedd379d54de07d205421fcc1b93d06768..e7f575245fe322f4dbace39fddd8bd697658cfd6 100644 (file)
@@ -1,10 +1,6 @@
 #ifndef __NSPC
 #define __NSPC
-struct Nspc_ {
-  m_str     name;
-  Nspc parent;
-  struct VM_Code_*   pre_ctor;
-  struct VM_Code_*   dtor;
+struct NspcInfo_ {
   m_bit* class_data;
   struct Vector_    vtable;
   struct Map_          op_map;
@@ -13,6 +9,14 @@ struct Nspc_ {
   struct Scope_     func;
   size_t offset;
   size_t class_data_size;
+};
+
+struct Nspc_ {
+  m_str     name;
+  Nspc parent;
+  struct VM_Code_*   pre_ctor;
+  struct VM_Code_*   dtor;
+  struct NspcInfo_* info;
   HAS_OBJ
 };
 
@@ -23,12 +27,12 @@ extern ANN void nspc_commit(const Nspc);
 
 #define describe_lookup0(A, b)                                                 \
 static inline ANN A nspc_lookup_##b##0(const Nspc n, const Symbol s){          \
-  return (A)scope_lookup0(&n->b, (vtype)s);                                    \
+  return (A)scope_lookup0(&n->info->b, (vtype)s);                              \
 }
 
 #define describe_lookup1(A, b)                                                 \
 static inline ANN A nspc_lookup_##b##1(const Nspc n, const Symbol s) {         \
-  const A a = (A)scope_lookup1(&n->b, (vtype)s);                               \
+  const A a = (A)scope_lookup1(&n->info->b, (vtype)s);                         \
   if(!a && n->parent)                                                          \
     return nspc_lookup_##b##1(n->parent, s);                                   \
   return a;                                                                    \
@@ -40,10 +44,10 @@ describe_lookup1(A, b)                                                         \
 
 #define describe_nspc_func(A, b)                                               \
 static inline ANN void nspc_add_##b(const Nspc n, const Symbol s, const A a) { \
-  scope_add(&n->b, (vtype)s, (vtype)a);                                        \
+  scope_add(&n->info->b, (vtype)s, (vtype)a);                                  \
 }                                                                              \
-ANN static inline void nspc_push_##b(const Nspc n) { scope_push(&n->b); }      \
-ANN inline static void nspc_pop_##b (const Nspc n) { scope_pop (&n->b); }      \
+ANN static inline void nspc_push_##b(const Nspc n) { scope_push(&n->info->b); }\
+ANN inline static void nspc_pop_##b (const Nspc n) { scope_pop (&n->info->b); }\
 describe_lookups(A, b)
 
 describe_nspc_func(Value, value)
index d52bb5ac2ba6f16b3a8aa9e99055a97861ef722d..3ce744d69a44fbd5a5922479c6bbb24e7f44990f 100644 (file)
@@ -451,7 +451,7 @@ ANN static m_bool emit_exp_primary(const Emitter emit, const Exp_Primary* prim)
 ANN static m_bool emit_dot_static_data(const Emitter emit, const Value v, const uint emit_var) { GWDEBUG_EXE
   const m_uint size = v->type->size;
   const Instr instr = emit_kind(emit, size, emit_var, dotstatic);
-  instr->m_val =  (m_uint)(v->owner_class->nspc->class_data + v->offset);
+  instr->m_val =  (m_uint)(v->owner_class->nspc->info->class_data + v->offset);
   instr->m_val2 = size;
   return GW_OK;
 }
@@ -1211,7 +1211,7 @@ ANN static m_bool emit_stmt_switch(const Emitter emit, const Stmt_Switch stmt) {
 ANN m_bool value_value(const Value v, m_int *value) {
   if((!GET_FLAG(v, builtin) && !GET_FLAG(v, enum)) || GET_FLAG(v, member))
      return 0;
-  *value = v->d.ptr ? *(m_int*)v->d.ptr : v->owner->class_data[v->offset];
+  *value = v->d.ptr ? *(m_int*)v->d.ptr : v->owner->info->class_data[v->offset];
   return GW_OK;
 }
 
@@ -1256,7 +1256,7 @@ ANN static m_bool emit_stmt_enum(const Emitter emit, const Stmt_Enum stmt) { GWD
       v->offset = emit_local(emit, SZ_INT, 0);
       v->d.ptr = addr;
     } else
-      *(m_uint*)(emit->env->class_def->nspc->class_data + v->offset) = i;
+      *(m_uint*)(emit->env->class_def->nspc->info->class_data + v->offset) = i;
   }
   return GW_OK;
 }
@@ -1274,10 +1274,11 @@ ANN static m_bool emit_stmt_union(const Emitter emit, const Stmt_Union stmt) { G
   m_uint scope = emit->env->scope->depth;
   const m_bool global = GET_FLAG(stmt, global);
   if(stmt->xid) {
-    if(stmt->value->type->nspc->class_data_size && !stmt->value->type->nspc->class_data)
-      stmt->value->type->nspc->class_data =
-        (m_bit*)xcalloc(1, stmt->value->type->nspc->class_data_size);
-    stmt->value->type->nspc->offset = stmt->s;
+    if(stmt->value->type->nspc->info->class_data_size &&
+      !stmt->value->type->nspc->info->class_data)
+      stmt->value->type->nspc->info->class_data =
+        (m_bit*)xcalloc(1, stmt->value->type->nspc->info->class_data_size);
+    stmt->value->type->nspc->info->offset = stmt->s;
     if(!stmt->value->type->p)
       stmt->value->type->p = mp_ini((uint32_t)stmt->value->type->size);
     Type_Decl *type_decl = new_type_decl(new_id_list(stmt->xid, stmt->self->pos),
@@ -1298,10 +1299,11 @@ ANN static m_bool emit_stmt_union(const Emitter emit, const Stmt_Union stmt) { G
     }
     scope = emit_push_type(emit, stmt->value->type);
   } else if(stmt->type_xid) {
-    if(stmt->type->nspc->class_data_size && !stmt->type->nspc->class_data)
-      stmt->type->nspc->class_data =
-        (m_bit*)xcalloc(1, stmt->type->nspc->class_data_size);
-    stmt->type->nspc->offset = stmt->s;
+    if(stmt->type->nspc->info->class_data_size &&
+        !stmt->type->nspc->info->class_data)
+      stmt->type->nspc->info->class_data =
+        (m_bit*)xcalloc(1, stmt->type->nspc->info->class_data_size);
+    stmt->type->nspc->info->offset = stmt->s;
     if(!stmt->type->p)
       stmt->type->p = mp_ini((uint32_t)stmt->type->size);
     scope = emit_push_type(emit, stmt->type);
@@ -1657,8 +1659,8 @@ ANN static m_bool emit_class_def(const Emitter emit, const Class_Def class_def)
     if(!base->nspc->pre_ctor)
       CHECK_BB(emit_class_def(emit, base->def))
   }
-  if(nspc->class_data_size)
-    nspc->class_data = (m_bit*)xcalloc(1, nspc->class_data_size);
+  if(nspc->info->class_data_size)
+    nspc->info->class_data = (m_bit*)xcalloc(1, nspc->info->class_data_size);
   emit_class_push(emit, type);
   emit_class_code(emit, type->name);
   if(class_def->ext && class_def->ext->array)
index 397e6d3d64a37b6ea714e8bab018ca453c198945..7a46aa61a61953cde85b9acf74d7be9b747d2250 100644 (file)
@@ -194,9 +194,9 @@ ANN2(1,2) static void import_class_ini(const Env env, const Type type,
   if(dtor)
     mk_xtor(type, (m_uint)dtor, ae_flag_dtor);
   if(type->parent) {
-    type->nspc->offset = type->parent->nspc->offset;
-    if(type->parent->nspc->vtable.ptr)
-      vector_copy2(&type->parent->nspc->vtable, &type->nspc->vtable);
+    type->nspc->info->offset = type->parent->nspc->info->offset;
+    if(type->parent->nspc->info->vtable.ptr)
+      vector_copy2(&type->parent->nspc->info->vtable, &type->nspc->info->vtable);
   }
   type->owner = env->curr;
   SET_FLAG(type, checked);
@@ -234,9 +234,9 @@ ANN m_int gwi_class_ext(const Gwi gwi, Type_Decl* td) {
     if(td->array)
       SET_FLAG(gwi->env->class_def, typedef);
     gwi->env->class_def->parent = t;
-    gwi->env->class_def->nspc->offset = t->nspc->offset;
-    if(t->nspc->vtable.ptr)
-      vector_copy2(&t->nspc->vtable, &gwi->env->class_def->nspc->vtable);
+    gwi->env->class_def->nspc->info->offset = t->nspc->info->offset;
+    if(t->nspc->info->vtable.ptr)
+      vector_copy2(&t->nspc->info->vtable, &gwi->env->class_def->nspc->info->vtable);
     CHECK_OB((gwi->emit->code = emit_class_code(gwi->emit,
           gwi->env->class_def->name)))
     if(td->array)
@@ -256,8 +256,8 @@ ANN static m_int import_class_end(const Env env) {
   if(!env->class_def)
     ERR_B(0, "import: too many class_end called.")
   const Nspc nspc = env->class_def->nspc;
-  if(nspc->class_data_size && !nspc->class_data)
-    nspc->class_data = (m_bit*)xcalloc(1, nspc->class_data_size);
+  if(nspc->info->class_data_size && !nspc->info->class_data)
+    nspc->info->class_data = (m_bit*)xcalloc(1, nspc->info->class_data_size);
   env_pop(env, 0);
   return GW_OK;
 }
@@ -266,8 +266,8 @@ ANN static m_int import_class_end(const Env env) {
 ANN m_int gwi_class_end(const Gwi gwi) {
   if(!gwi->env->class_def)return GW_ERROR;
   const Type t = gwi->env->class_def;
-  if(t->nspc && t->nspc->offset)
-    t->p = mp_ini((uint32_t)t->nspc->offset);
+  if(t->nspc && t->nspc->info->offset)
+    t->p = mp_ini((uint32_t)t->nspc->info->offset);
   return import_class_end(gwi->env);
 }
 
@@ -582,7 +582,7 @@ ANN m_int gwi_union_end(const Gwi gwi, const ae_flag flag) {
   CHECK_BB(traverse_stmt_union(gwi->env, &stmt->d.stmt_union))
   emit_union_offset(stmt->d.stmt_union.l, stmt->d.stmt_union.o);
   if(GET_FLAG((&stmt->d.stmt_union), member))
-    gwi->env->class_def->nspc->offset =
+    gwi->env->class_def->nspc->info->offset =
       stmt->d.stmt_union.o + stmt->d.stmt_union.s;
   free_stmt(stmt);
   gwi->union_data.list = NULL;
index 1094b00306bac2430a2caa7c94ba5930b1694b62..e84be8f013b8668b0375279e95dab7b3aa7dffa2 100644 (file)
@@ -23,8 +23,8 @@ M_Object new_object(const VM_Shred shred, const Type t) {
   const M_Object a = mp_alloc(M_Object);
   a->ref = 1;
   a->type_ref = t;
-  a->vtable = &t->nspc->vtable;
-  if(t->nspc->offset) {
+  a->vtable = &t->nspc->info->vtable;
+  if(t->nspc->info->offset) {
     Type type = t;
     while(!type->p)
       type = type->parent;
@@ -71,7 +71,7 @@ __attribute__((hot))
 ANN void __release(const M_Object obj, const VM_Shred shred) {
   Type t = obj->type_ref;
   while(t->parent) {
-    struct scope_iter iter = { &t->nspc->value, 0, 0 };\
+    struct scope_iter iter = { &t->nspc->info->value, 0, 0 };\
     Value v;
     while(scope_iter(&iter, &v) > 0) {
       if(!GET_FLAG(v, static) && isa(v->type, t_object) > 0)
index 7c319ad3d6ba94ac6cac8752bc769df3dda2df03..2b88b720327aca6b475bd96d387c89e7b6787ba7 100644 (file)
@@ -112,7 +112,7 @@ ANN m_bool type_engine_check_prog(const Env env, const Ast ast) {
 
 ANN m_bool env_add_op(const Env env, const struct Op_Import* opi) {
   const Nspc nspc = env->curr;
-  if(!nspc->op_map.ptr)
-    map_init(&nspc->op_map);
+  if(!nspc->info->op_map.ptr)
+    map_init(&nspc->info->op_map);
   return add_op(nspc, opi);
 }
index 7d15ef3739bed87741c5cac41114ee261296c8db..8b39aa1a06c4d14c2c7d5042a90ef751fb361023 100644 (file)
 #include "gwion.h"
 
 ANN void nspc_commit(const Nspc nspc) {
-  scope_commit(&nspc->value);
-  scope_commit(&nspc->func);
-  scope_commit(&nspc->type);
+  scope_commit(&nspc->info->value);
+  scope_commit(&nspc->info->func);
+  scope_commit(&nspc->info->type);
 }
 
 ANN static void nspc_release_object(const Nspc a, Value value) {
-  if(value->d.ptr || (GET_FLAG(value, static) && a->class_data) ||
+  if(value->d.ptr || (GET_FLAG(value, static) && a->info->class_data) ||
     (value->d.ptr && GET_FLAG(value, builtin))) {
     const VM_Code code = new_vm_code(NULL, 0, ae_flag_builtin, "in code dtor");
     const VM_Shred s = new_vm_shred(code);
     const M_Object obj = value->d.ptr ? (M_Object)value->d.ptr :
-        *(M_Object*)(a->class_data + value->offset);
+        *(M_Object*)(a->info->class_data + value->offset);
     s->info->vm = value->gwion->vm;
     release(obj, s);
     free_vm_shred(s);
@@ -32,7 +32,7 @@ ANN static void nspc_release_object(const Nspc a, Value value) {
 }
 
 ANN static void free_nspc_value(const Nspc a) {
-  struct scope_iter iter = { &a->value, 0, 0 };
+  struct scope_iter iter = { &a->info->value, 0, 0 };
   Value v;
   while(scope_iter(&iter, &v) > 0) {
     if(isa(v->type, t_object) > 0  ||
@@ -42,16 +42,16 @@ ANN static void free_nspc_value(const Nspc a) {
     }
     REM_REF(v);
   }
-  scope_release(&a->value);
+  scope_release(&a->info->value);
 }
 
 #define describe_nspc_free(A, b) \
 ANN static void nspc_free_##b(Nspc n) {\
-  struct scope_iter iter = { &n->b, 0, 0 };\
+  struct scope_iter iter = { &n->info->b, 0, 0 };\
   A a;\
   while(scope_iter(&iter, &a) > 0) \
     REM_REF(a);\
-  scope_release(&n->b);\
+  scope_release(&n->info->b);\
 }
 
 describe_nspc_free(Func, func)
@@ -62,25 +62,27 @@ ANN static void free_nspc(Nspc a) {
   nspc_free_type(a);
   free_nspc_value(a);
 
-  if(a->class_data)
-    free(a->class_data);
-  if(a->vtable.ptr)
-    vector_release(&a->vtable);
+  if(a->info->class_data)
+    free(a->info->class_data);
+  if(a->info->vtable.ptr)
+    vector_release(&a->info->vtable);
+  if(a->info->op_map.ptr)
+    free_op_map(&a->info->op_map);
+  mp_free(NspcInfo, a->info);
   if(a->pre_ctor)
     REM_REF(a->pre_ctor);
   if(a->dtor)
     REM_REF(a->dtor);
-  if(a->op_map.ptr)
-    free_op_map(&a->op_map);
   mp_free(Nspc, a);
 }
 
 ANN Nspc new_nspc(const m_str name) {
   const Nspc a = mp_alloc(Nspc);
   a->name = name;
-  scope_init(&a->value);
-  scope_init(&a->type);
-  scope_init(&a->func);
+  a->info = mp_alloc(NspcInfo);
+  scope_init(&a->info->value);
+  scope_init(&a->info->type);
+  scope_init(&a->info->func);
   INIT_OO(a, free_nspc);
   return a;
 }
index 359b2a21f37b6a4f726dfcde96f304eeeb4f3a0e..060338f1c59a91f1a1cbd71a223b8b7d9fe1f144 100644 (file)
@@ -49,8 +49,8 @@ ANN static inline m_bool check_exp_decl_parent(const Env env, const Var_Decl var
 #define describe_check_decl(a, b)                                 \
 ANN static inline void decl_##a(const Nspc nspc, const Value v) { \
   SET_FLAG(v, a);                                                 \
-  v->offset = nspc->b;                                            \
-  nspc->b += v->type->size;                                       \
+  v->offset = nspc->info->b;                                      \
+  nspc->info->b += v->type->size;                                 \
 }
 describe_check_decl(member, offset)
 describe_check_decl(static, class_data_size)
@@ -960,7 +960,7 @@ ANN m_bool check_stmt_union(const Env env, const Stmt_Union stmt) { GWDEBUG_EXE
     }
   } else if(env->class_def)  {
     if(!GET_FLAG(stmt, static))
-      stmt->o = env->class_def->nspc->offset;
+      stmt->o = env->class_def->nspc->info->offset;
     else
       decl_static(env->curr, stmt->value);
   }
@@ -1022,7 +1022,7 @@ ANN static m_bool parent_match_actual(const Env env, const restrict Func_Def f,
       CHECK_BB(check_signature_match(f, parent_func))
       if(!f->tmpl) {
         f->func->vt_index = parent_func->vt_index;
-        vector_set(&env->curr->vtable, f->func->vt_index, (vtype)f->func);
+        vector_set(&env->curr->info->vtable, f->func->vt_index, (vtype)f->func);
       }
       return GW_OK;
     }
@@ -1041,10 +1041,10 @@ ANN static m_bool check_parent_match(const Env env, const Func_Def f) { GWDEBUG_
         return match;
     }
   }
-  if(!env->curr->vtable.ptr)
-    vector_init(&env->curr->vtable);
-  func->vt_index = vector_size(&env->curr->vtable);
-  vector_add(&env->curr->vtable, (vtype)func);
+  if(!env->curr->info->vtable.ptr)
+    vector_init(&env->curr->info->vtable);
+  func->vt_index = vector_size(&env->curr->info->vtable);
+  vector_add(&env->curr->info->vtable, (vtype)func);
   return GW_OK;
 }
 
@@ -1183,9 +1183,9 @@ ANN static m_bool check_class_body(const Env env, const Class_Def class_def) {
 
 ANN static inline void inherit(const Type t) {
   const Nspc nspc = t->nspc, parent = t->parent->nspc;
-  nspc->offset = parent->offset;
-  if(parent->vtable.ptr)
-    vector_copy2(&parent->vtable, &nspc->vtable);
+  nspc->info->offset = parent->info->offset;
+  if(parent->info->vtable.ptr)
+    vector_copy2(&parent->info->vtable, &nspc->info->vtable);
 }
 
 ANN m_bool check_class_def(const Env env, const Class_Def class_def) { GWDEBUG_EXE
@@ -1199,8 +1199,8 @@ ANN m_bool check_class_def(const Env env, const Class_Def class_def) { GWDEBUG_E
   inherit(the_class);
   if(class_def->body)
     CHECK_BB(check_class_body(env, class_def))
-  if(!the_class->p && the_class->nspc->offset)
-    the_class->p = mp_ini((uint32_t)the_class->nspc->offset);
+  if(!the_class->p && the_class->nspc->info->offset)
+    the_class->p = mp_ini((uint32_t)the_class->nspc->info->offset);
   SET_FLAG(the_class, checked | ae_flag_check);
   return GW_OK;
 }
index fb59741d1dca5ad22c316231b04355d659b0b5ef..fbf24e09166001cc519816bf96877370af582da9 100644 (file)
@@ -77,11 +77,11 @@ ANN2(1) static M_Operator* operator_find(const Vector v, const restrict Type lhs
 }
 
 ANN m_bool add_op(const Nspc nspc, const struct Op_Import* opi) {
-  Vector v = (Vector)map_get(&nspc->op_map, (vtype)opi->op);
+  Vector v = (Vector)map_get(&nspc->info->op_map, (vtype)opi->op);
   M_Operator* mo;
   if(!v) {
     v = new_vector();
-    map_set(&nspc->op_map, (vtype)opi->op, (vtype)v);
+    map_set(&nspc->info->op_map, (vtype)opi->op, (vtype)v);
   }
   if((mo = operator_find(v, opi->lhs, opi->rhs)))
     CHECK_BB((err_msg(0, "operator '%s', for type '%s' and '%s' already imported",
@@ -139,11 +139,11 @@ static m_str type_name(const Type t) {
 ANN Type op_check(const Env env, struct Op_Import* opi) {
   Nspc nspc = env->curr;
   do {
-    if(nspc->op_map.ptr) {
+    if(nspc->info->op_map.ptr) {
       Type l = opi->lhs;
       do {
         struct Op_Import opi2 = { .op=opi->op, .lhs=l, .rhs=opi->rhs, .data=opi->data };
-        Type ret = op_check_inner(env, &nspc->op_map, &opi2);
+        Type ret = op_check_inner(env, &nspc->info->op_map, &opi2);
         if(ret) {
           if(ret == t_null)
             break;
@@ -163,7 +163,7 @@ ANN Type op_check(const Env env, struct Op_Import* opi) {
 
 ANN m_bool operator_set_func(const struct Op_Import* opi) {
   const Nspc nspc = ((Func)opi->data)->value_ref->owner;
-  const Vector v = (Vector)map_get(&nspc->op_map, opi->op);
+  const Vector v = (Vector)map_get(&nspc->info->op_map, opi->op);
   M_Operator* mo = operator_find(v, opi->lhs, opi->rhs);
   CHECK_OB(mo)
   mo->func = (Func)opi->data;
@@ -200,9 +200,9 @@ ANN m_bool op_emit(const Emitter emit, const struct Op_Import* opi) {
   do {
     Type r = opi->rhs;
     do {
-      if(!nspc->op_map.ptr)
+      if(!nspc->info->op_map.ptr)
         continue;
-      const Vector v = (Vector)map_get(&nspc->op_map, (vtype)opi->op);
+      const Vector v = (Vector)map_get(&nspc->info->op_map, (vtype)opi->op);
       const M_Operator* mo = operator_find(v, l, r);
       if(mo) {
         if(mo->em)