Edit

kc3-lang/kc3/libkc3/struct.c

Branch :

  • libkc3/struct.c
  • /* kc3
     * Copyright from 2022 to 2025 kmx.io <contact@kmx.io>
     *
     * Permission is hereby granted to use this software granted the above
     * copyright notice and this permission paragraph are included in all
     * copies and substantial portions of this software.
     *
     * THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
     * PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
     * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
     * THIS SOFTWARE.
     */
    #include <string.h>
    #include "alloc.h"
    #include "assert.h"
    #include "buf.h"
    #include "buf_parse.h"
    #include "data.h"
    #include "env.h"
    #include "kc3_main.h"
    #include "list.h"
    #include "map.h"
    #include "mutex.h"
    #include "pstruct.h"
    #include "pstruct_type.h"
    #include "struct.h"
    #include "struct_type.h"
    #include "sym.h"
    #include "tag.h"
    #include "tag_type.h"
    
    s_tag * struct_access (s_struct *s, s_list *key, s_tag *dest)
    {
      s_tag *first;
      s_list *next;
      s_tag *r;
      s_tag tag;
      assert(s);
      assert(key);
      assert(dest);
      first = &key->tag;
      next = list_next(key);
      if (first->type != TAG_PSYM) {
        err_write_1("struct_access: key is not a Sym: (");
        err_inspect_struct(s);
        err_write_1(", ");
        err_inspect_tag(first);
        err_write_1(")\n");
        assert(! "struct_access: key is not a Sym");
        return NULL;
      }
      if (! struct_access_sym(s, first->data.psym, &tag)) {
        err_write_1("struct_access: struct_access_sym(");
        err_inspect_struct(s);
        err_write_1(", ");
        err_inspect_tag(first);
        err_write_1(")\n");
        err_stacktrace();
        assert(! "struct_access: struct_access_sym");
        return NULL;
      }
      if (! next) {
        *dest = tag;
        return dest;
      }
      r = kc3_access(&tag, &next, dest);
      tag_clean(&tag);
      return r;
    }
    
    s_tag * struct_access_sym (s_struct *s, const s_sym *key, s_tag *dest)
    {
      void *data;
      s_struct_type *st;
      const s_sym *type;
      s_tag tmp = {0};
      void *tmp_data;
      if (! struct_get_var_type(s, key, &type))
        return NULL;    
      data = struct_get_w(s, key);
      if (! data)
        return NULL;
      if (type != &g_sym_Tag) {
        if (! sym_to_tag_type(type, &tmp.type))
          return NULL;
        if (type != &g_sym_Time) {
          if (! struct_type_find(type, &st))
            return NULL;
          if (st) {
            tag_init_pstruct_with_type(&tmp, st);
            if (! struct_allocate(tmp.data.pstruct))
              return NULL;
          }
        }
      }
      if (! tag_to_pointer(&tmp, type, &tmp_data))
        return NULL;
      if (! data_init_copy(type, tmp_data, data))
        return NULL;
      *dest = tmp;
      return dest;
    }
    
    s_struct * struct_allocate (s_struct *s)
    {
      s_struct tmp = {0};
      assert(s);
      assert(s->pstruct_type);
      if (s->data) {
        err_puts("struct_allocate: data != NULL");
        assert(! "struct_allocate: data != NULL");
        return NULL;
      }
      tmp = *s;
      tmp.data = alloc(tmp.pstruct_type->size);
      if (! tmp.data)
        return NULL;
      tmp.free_data = true;
      *s = tmp;
      return s;
    }
    
    void struct_clean (s_struct *s)
    {
      uw i;
      const s_sym *type;
      assert(s);
      assert(s->pstruct_type);
    #if HAVE_PTHREAD
      if (s->mutex_ready)
        mutex_clean(&s->mutex);
    #endif
      if (s->data) {
        if (s->pstruct_type->clean)
          s->pstruct_type->clean(s->data);
        if (s->pstruct_type->must_clean) {
          i = 0;
          while (i < s->pstruct_type->map.count) {
            if (s->pstruct_type->map.value[i].type == TAG_PVAR)
              type = s->pstruct_type->map.value[i].data.pvar->type;
            else if (! tag_type(s->pstruct_type->map.value + i, &type))
              goto ko;
            data_clean(type, (s8 *) s->data + s->pstruct_type->offset[i]);
          ko:
            i++;
          }
        }
        if (s->free_data)
          free(s->data);
      }
      if (s->tag) {
        i = 0;
        while (i < s->pstruct_type->map.count) {
          tag_clean(s->tag + i);
          i++;
        }
        free(s->tag);
      }
      struct_type_delete(s->pstruct_type);
    }
    
    void struct_delete (s_struct *s)
    {
      assert(s);
      if (env_global()->pass_by_copy)
        assert(s->ref_count == 1);
      else {
    #if HAVE_PTHREAD
        mutex_lock(&s->mutex);
    #endif
        if (s->ref_count <= 0) {
          err_puts("struct_delete: invalid reference count");
          assert(! "struct_delete: invalid reference count");
    #if HAVE_PTHREAD
          mutex_unlock(&s->mutex);
    #endif
          return;
        }
        if (--s->ref_count) {
    #if HAVE_PTHREAD
          mutex_unlock(&s->mutex);
    #endif
          return;
        }
    #if HAVE_PTHREAD
        mutex_unlock(&s->mutex);
    #endif
      }
      struct_clean(s);
      free(s);
    }
    
    uw * struct_find_key_index (const s_struct *s, const s_sym *key,
                                uw *dest)
    {
      return struct_type_find_key_index(s->pstruct_type, key, dest);
    }
    
    const void * struct_get (const s_struct *s, const s_sym *key)
    {
      uw i = 0;
      assert(s);
      assert(key);
      if (! struct_find_key_index(s, key, &i))
        return NULL;
      assert(i < s->pstruct_type->map.count);
      return (u8 *) s->data + s->pstruct_type->offset[i];
    }
    
    const s_sym ** struct_get_sym (const s_struct *s, const s_sym *key)
    {
      return (const s_sym **) struct_get(s, key);
    }
    
    s_tag * struct_get_tag (s_struct *s, const s_sym *key)
    {
      return (s_tag *) struct_get(s, key);
    }
    
    const s_sym ** struct_get_type (const s_struct *s, const s_sym *key,
                                    const s_sym **dest)
    {
      s_tag tag_key;
      const s_sym *tmp = NULL;
      tag_init_psym(&tag_key, key);
      if (! map_get_type(&s->pstruct_type->map, &tag_key, &tmp))
        return NULL;
      *dest = tmp;
      return NULL;
    }
    
    u8 struct_get_u8 (const s_struct *s, const s_sym *key)
    {
      return *(u8 *) struct_get(s, key);
    }
    
    uw struct_get_uw (const s_struct *s, const s_sym *key)
    {
      return *(uw *) struct_get(s, key);
    }
    
    const s_sym ** struct_get_var_type (const s_struct *s, const s_sym *key,
                                        const s_sym **dest)
    {
      s_tag tag_key;
      tag_init_psym(&tag_key, key);
      return map_get_var_type(&s->pstruct_type->map, &tag_key, dest);
    }
    
    void * struct_get_w (s_struct *s, const s_sym *key)
    {
      uw i = 0;
      assert(s);
      assert(key);
      if (! s->data) {
        err_puts("struct_get_w: struct is not allocated");
        assert(! "struct_get_w: struct is not allocated");
        return NULL;
      }
      if (! struct_find_key_index(s, key, &i))
        return NULL;
      assert(i < s->pstruct_type->map.count);
      return (u8 *) s->data + s->pstruct_type->offset[i];
    }
    
    s_struct * struct_init (s_struct *s, const s_sym *module)
    {
      p_struct_type st = NULL;
      s_struct tmp = {0};
      assert(s);
      if (module) {
        if (! struct_type_find(module, &st)) {
          err_write_1("struct_init: struct_type_find(");
          err_inspect_sym(module);
          err_puts(")");
          assert("struct_init: struct_type_find");
          return NULL;
        }
        if (! st) {
          err_write_1("struct_init: struct_type not found: ");
          err_inspect_sym(module);
          err_write_1("\n");
          assert(! "struct_init: struct_type not found");
          return NULL;
        }
        if (! pstruct_type_init_copy(&tmp.pstruct_type, &st)) {
          err_write_1("struct_init: pstruct_type_init_copy: ");
          err_inspect_sym(module);
          err_write_1("\n");
          assert("struct_init: pstruct_type_init_copy");
          return NULL;
        }
      }
      tmp.ref_count = 1;
    #if HAVE_PTHREAD
      mutex_init(&tmp.mutex);
      tmp.mutex_ready = true;
    #endif
      *s = tmp;
      return s;
    }
    
    s_struct * struct_init_copy (s_struct *s, s_struct *src)
    {
      uw i;
      s_tag *key = NULL;
      s_struct tmp = {0};
      const s_sym *type;
      s_tag *value = NULL;
      assert(s);
      assert(src);
      assert(src->pstruct_type);
      tmp.ref_count = 1;
      if (! pstruct_type_init_copy(&tmp.pstruct_type, &src->pstruct_type))
        return NULL;
      if (src->data) {
        tmp.data = alloc(tmp.pstruct_type->size);
        if (! tmp.data)
          return NULL;
        tmp.free_data = true;
        i = 0;
        while (i < tmp.pstruct_type->map.count) {
          key = tmp.pstruct_type->map.key + i;
          value = tmp.pstruct_type->map.value + i;
          if (key->data.psym->str.ptr.pchar[0] != '_') {
            if (value->type == TAG_PVAR)
              type = value->data.pvar->type;
            else if (! tag_type(value, &type))
              goto ko;
            if (! data_init_copy(type, (s8 *) tmp.data +
                                 tmp.pstruct_type->offset[i],
                                 (s8 *) src->data +
                                 tmp.pstruct_type->offset[i]))
              goto ko;
          }
          i++;
        }
      }
      else if (src->tag) {
        tmp.tag = alloc(tmp.pstruct_type->map.count * sizeof(s_tag));
        if (! tmp.tag)
          return NULL;
        i = 0;
        while (i < tmp.pstruct_type->map.count) {
          if (! tag_init_copy(tmp.tag + i, src->tag + i))
            goto ko;
          i++;
        }
      }
    #if HAVE_PTHREAD
      mutex_init(&tmp.mutex);
      tmp.mutex_ready = true;
    #endif
      *s = tmp;
      return s;
     ko:
      struct_clean(&tmp);
      return NULL;
    }
    
    s_struct * struct_init_from_lists (s_struct *s, const s_sym *module,
                                       s_list *keys,
                                       s_list *values)
    {
      uw i;
      s_list *k;
      s_struct tmp = {0};
      s_list *v;
      assert(s);
      assert(module);
      assert(list_length(keys) == list_length(values));
      if (! struct_init(&tmp, module))
        return NULL;
      tmp.tag = alloc(tmp.pstruct_type->map.count * sizeof(s_tag));
      if (! tmp.tag)
        return NULL;
      i = 0;
      while (i < tmp.pstruct_type->map.count) {
        k = keys;
        v = values;
        while (k && v) {
          if (k->tag.type != TAG_PSYM) {
            err_write_1("struct_init_from_lists: key that is not a"
                        " symbol: ");
            err_puts(tag_type_to_string(k->tag.type));
            assert(! "struct_init_from_lists: key that is not a symbol");
            goto ko;
          }
          if (k->tag.data.psym->str.ptr.pchar[0] != '_') {
            if (k->tag.data.psym ==
                tmp.pstruct_type->map.key[i].data.psym) {
              if (! tag_init_copy(tmp.tag + i, &v->tag))
                goto ko;
              goto next;
            }
          }
          k = list_next(k);
          v = list_next(v);
        }
        if (! tag_init_copy(tmp.tag + i, tmp.pstruct_type->map.value + i))
          goto ko;
      next:
        i++;
      }
      *s = tmp;
      return s;
     ko:
      struct_clean(&tmp);
      return NULL;
    }
    
    s_struct * struct_init_with_data (s_struct *s, const s_sym *module,
                                      void *data, bool free_data)
    {
      p_struct_type st;
      s_struct tmp = {0};
      assert(s);
      assert(module);
      if (! struct_type_find(module, &st))
        return NULL;
      if (! st) {
        err_write_1("struct_init_with_data: struct_type not found: ");
        err_inspect_sym(module);
        err_write_1("\n");
        assert(! "struct_init_with_data: struct_type not found");
        return NULL;
      }
      if (! pstruct_type_init_copy(&tmp.pstruct_type, &st))
        return NULL;
      tmp.data = data;
      tmp.free_data = free_data;
      tmp.ref_count = 1;
    #if HAVE_PTHREAD
      mutex_init(&tmp.mutex);
      tmp.mutex_ready = true;
    #endif
      *s = tmp;
      return s;
    }
    
    s_struct * struct_init_with_type (s_struct *s, s_struct_type *st)
    {
      s_struct tmp = {0};
      assert(s);
      assert(st);
      pstruct_type_init_copy(&tmp.pstruct_type, &st);
      tmp.ref_count = 1;
    #if HAVE_PTHREAD
      mutex_init(&tmp.mutex);
      tmp.mutex_ready = true;
    #endif
      *s = tmp;
      return s;
    }
    
    s_struct * struct_new (const s_sym *module)
    {
      s_struct *s;
      s = alloc(sizeof(s_struct));
      if (! s)
        return NULL;
      if (! struct_init(s, module)) {
        free(s);
        return NULL;
      }
      return s;
    }
    
    s_struct * struct_new_copy (s_struct *src)
    {
      s_struct *s;
      assert(src);
      s = alloc(sizeof(s_struct));
      if (! s)
        return NULL;
      if (! struct_init_copy(s, src)) {
        free(s);
        return NULL;
      }
      return s;
    }
    
    s_struct * struct_new_ref (s_struct *s)
    {
      assert(s);
    #if HAVE_PTHREAD
      mutex_lock(&s->mutex);
    #endif
      if (s->ref_count <= 0) {
        err_puts("struct_new_ref: invalid reference count");
        assert(! "struct_new_ref: invalid reference count");
        return NULL;
      }
      s->ref_count++;
    #if HAVE_PTHREAD
      mutex_unlock(&s->mutex);
    #endif
      return s;
    }
    
    s_struct * struct_new_with_data (const s_sym *module, void *data,
                                     bool free_data)
    {
      s_struct *s;
      assert(module);
      assert(data);
      s = alloc(sizeof(s_struct));
      if (! s)
        return NULL;
      if (! struct_init_with_data(s, module, data, free_data)) {
        free(s);
        return NULL;
      }
      return s;
    }
    
    s_struct * struct_new_with_type (s_struct_type *st)
    {
      s_struct *s;
      assert(st);
      s = alloc(sizeof(s_struct));
      if (! s)
        return NULL;
      if (! struct_init_with_type(s, st)) {
        free(s);
        return NULL;
      }
      return s;
    }
    
    uw * struct_offset (const s_struct *s, const s_sym * const *key,
                        uw *dest)
    {
      uw i = 0;
      assert(s);
      assert(s->pstruct_type);
      assert(key);
      if (! struct_find_key_index(s, *key, &i)) {
        err_write_1("struct_offset: key not found: ");
        err_inspect_psym(key);
        err_write_1("\n");
        return NULL;
      }
      assert(i < s->pstruct_type->map.count);
      *dest = s->pstruct_type->offset[i];
      return dest;
    }
    
    s_struct * struct_set (s_struct *s, const s_sym *key,
                           s_tag *value)
    {
      void *data;
      void *data_src;
      uw i;
      const s_sym *type_sym;
      assert(s);
      assert(s->pstruct_type->map.count);
      assert(key);
      assert(value);
      i = 0;
      while (i < s->pstruct_type->map.count) {
        if (s->pstruct_type->map.key[i].type != TAG_PSYM) {
          err_puts("struct_set: struct type map key is not a Sym");
          assert(! "struct_set: struct type map key is not a Sym");
          return NULL;
        }
        if (s->pstruct_type->map.key[i].data.psym == key) {
          if (s->pstruct_type->map.key[i].data.psym->str.ptr.pchar[0] ==
              '_') {
            err_write_1("struct_set: cannot set read only member ");
            err_inspect_sym(s->pstruct_type->map.key[i].data.psym);
            err_write_1("\n");
            assert(! "struct_set: cannot set read only member");
            return NULL;
          }
          if (! tag_type(s->pstruct_type->map.value + i, &type_sym)) {
            err_puts("struct_set: tag_type");
            assert(! "struct_set: tag_type");
            return NULL;
          }
          if (type_sym == &g_sym_Var)
            type_sym = s->pstruct_type->map.value[i].data.pvar->type;
          data = (s8 *) s->data + s->pstruct_type->offset[i];
          if (! tag_to_const_pointer(value, type_sym, &data_src)) {
            err_puts("struct_set: tag_to_const_pointer");
            assert(! "struct_set: tag_to_const_pointer");
            return NULL;
          }
          data_clean(type_sym, data);
          if (! data_init_copy(type_sym, data, data_src)) {
            err_puts("struct_set: data_init_copy");
            assert(! "struct_set: data_init_copy");
            return NULL;
          }
          return s;
        }
        i++;
      }
      err_write_1("struct_set: key not found: ");
      err_inspect_sym(key);
      err_write_1("\n");
      assert(! "struct_set: key not found");
      return NULL;
    }