GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/env/nspc.c Lines: 61 62 98.4 %
Date: 2020-09-22 13:02:15 Branches: 47 60 78.3 %

Line Branch Exec Source
1
#include "gwion_util.h"
2
#include "gwion_ast.h"
3
#include "gwion_env.h"
4
#include "vm.h"
5
#include "object.h"
6
#include "gwion.h"
7
#include "operator.h"
8
9
418
ANN void nspc_commit(const Nspc nspc) {
10
418
  scope_commit(nspc->info->value);
11
418
  scope_commit(nspc->info->func);
12
418
  scope_commit(nspc->info->type);
13
418
}
14
15
3438
ANN static inline void nspc_release_object(const Nspc a, Value value, Gwion gwion) {
16


6852
  if(!GET_FLAG(value, pure) && ((GET_FLAG(value, static) && a->info->class_data) ||
17
5794
    (value->d.ptr && GET_FLAG(value, builtin)))) {
18
2141
    const M_Object obj = value->d.ptr ? (M_Object)value->d.ptr :
19
3
        *(M_Object*)(a->info->class_data + value->from->offset);
20
2138
       release(obj, gwion->vm->cleaner_shred);
21
  }
22
3438
}
23
24
10
ANN2(1,3) static inline void nspc_release_struct(const Nspc a, Value value, Gwion gwion) {
25



10
  if(!SAFE_FLAG(value, pure) && ((SAFE_FLAG(value, static) && a->info->class_data) ||
26

9
    (SAFE_FLAG(value, builtin) && value->d.ptr))) {
27

2
    const m_bit *ptr = (value && value->d.ptr) ? (m_bit*)value->d.ptr:
28
1
        (m_bit*)(a->info->class_data + value->from->offset);
29
4
    for(m_uint i = 0; i < vector_size(&value->type->e->tuple->types); ++i) {
30
3
      const Type t = (Type)vector_at(&value->type->e->tuple->types, i);
31
3
      if(isa(t, gwion->type[et_object]) > 0)
32
1
        release(*(M_Object*)(ptr + vector_at(&value->type->e->tuple->offset, i)), gwion->vm->cleaner_shred);
33
2
      else if(GET_FLAG(t, struct))
34
        nspc_release_struct(t->nspc, NULL, gwion);
35
    }
36
  }
37
10
}
38
39
16023
ANN static void free_nspc_value(const Nspc a, Gwion gwion) {
40
16023
  struct scope_iter iter = { a->info->value, 0, 0 };
41
  Value v;
42
161498
  while(scope_iter(&iter, &v) > 0) {
43
129452
    if(isa(v->type, gwion->type[et_object]) > 0)
44
3438
      nspc_release_object(a, v, gwion);
45
126014
    else if(GET_FLAG(v->type, struct))
46
10
      nspc_release_struct(a, v, gwion);
47
129452
    REM_REF(v, gwion);
48
  }
49
16023
  free_scope(gwion->mp, a->info->value);
50
16023
}
51
52
#define describe_nspc_free(A, b) \
53
ANN static void nspc_free_##b(Nspc n, Gwion gwion) {\
54
  struct scope_iter iter = { n->info->b, 0, 0 };\
55
  A a;\
56
  while(scope_iter(&iter, &a) > 0) \
57
    REM_REF(a, gwion);\
58
  free_scope(gwion->mp, n->info->b);\
59
}
60
61
16023
describe_nspc_free(Func, func)
62
16023
describe_nspc_free(Type, type)
63
64
16023
ANN static void free_nspc(Nspc a, Gwion gwion) {
65
16023
  free_nspc_value(a, gwion);
66
16023
  nspc_free_func(a, gwion);
67
16023
  if(a->info->op_map.ptr)
68
748
    free_op_map(&a->info->op_map, gwion);
69
16023
  nspc_free_type(a, gwion);
70

16023
  if(a->info->class_data && a->info->class_data_size)
71
57
    mp_free2(gwion->mp, a->info->class_data_size, a->info->class_data);
72
16023
  if(a->info->vtable.ptr)
73
10824
    vector_release(&a->info->vtable);
74
16023
  mp_free(gwion->mp, NspcInfo, a->info);
75
16023
  if(a->pre_ctor)
76
7259
    REM_REF(a->pre_ctor, gwion);
77
16023
  if(a->dtor)
78
9253
    REM_REF(a->dtor, gwion);
79
16023
  mp_free(gwion->mp, Nspc, a);
80
16023
}
81
82
16044
ANN Nspc new_nspc(MemPool p, const m_str name) {
83
16044
  const Nspc a = mp_calloc(p, Nspc);
84
16044
  a->name = name;
85
16044
  a->info = mp_calloc(p, NspcInfo);
86
16044
  a->info->value = new_scope(p);
87
16044
  a->info->type = new_scope(p);
88
16044
  a->info->func = new_scope(p);
89
16044
  a->ref = new_refcount(p, free_nspc);
90
16044
  return a;
91
}