GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/env/nspc.c Lines: 61 62 98.4 %
Date: 2020-08-07 19:15:19 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
401
ANN void nspc_commit(const Nspc nspc) {
10
401
  scope_commit(nspc->info->value);
11
401
  scope_commit(nspc->info->func);
12
401
  scope_commit(nspc->info->type);
13
401
}
14
15
4231
ANN static inline void nspc_release_object(const Nspc a, Value value, Gwion gwion) {
16


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



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

11
    (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
12
}
38
39
15685
ANN static void free_nspc_value(const Nspc a, Gwion gwion) {
40
15685
  struct scope_iter iter = { a->info->value, 0, 0 };
41
  Value v;
42
164013
  while(scope_iter(&iter, &v) > 0) {
43
132643
    if(isa(v->type, gwion->type[et_object]) > 0)
44
4231
      nspc_release_object(a, v, gwion);
45
128412
    else if(GET_FLAG(v->type, struct))
46
12
      nspc_release_struct(a, v, gwion);
47
132643
    REM_REF(v, gwion);
48
  }
49
15685
  free_scope(gwion->mp, a->info->value);
50
15685
}
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
15685
describe_nspc_free(Func, func)
62
15685
describe_nspc_free(Type, type)
63
64
15685
ANN static void free_nspc(Nspc a, Gwion gwion) {
65
15685
  free_nspc_value(a, gwion);
66
15685
  nspc_free_func(a, gwion);
67
15685
  if(a->info->op_map.ptr)
68
765
    free_op_map(&a->info->op_map, gwion);
69
15685
  nspc_free_type(a, gwion);
70

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