GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/env/nspc.c Lines: 61 62 98.4 %
Date: 2020-09-12 17:36:58 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
412
ANN void nspc_commit(const Nspc nspc) {
10
412
  scope_commit(nspc->info->value);
11
412
  scope_commit(nspc->info->func);
12
412
  scope_commit(nspc->info->type);
13
412
}
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
5793
    (value->d.ptr && GET_FLAG(value, builtin)))) {
18
2138
    const M_Object obj = value->d.ptr ? (M_Object)value->d.ptr :
19
3
        *(M_Object*)(a->info->class_data + value->from->offset);
20
2135
       release(obj, gwion->vm->cleaner_shred);
21
  }
22
3438
}
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
16004
ANN static void free_nspc_value(const Nspc a, Gwion gwion) {
40
16004
  struct scope_iter iter = { a->info->value, 0, 0 };
41
  Value v;
42
161301
  while(scope_iter(&iter, &v) > 0) {
43
129293
    if(isa(v->type, gwion->type[et_object]) > 0)
44
3438
      nspc_release_object(a, v, gwion);
45
125855
    else if(GET_FLAG(v->type, struct))
46
12
      nspc_release_struct(a, v, gwion);
47
129293
    REM_REF(v, gwion);
48
  }
49
16004
  free_scope(gwion->mp, a->info->value);
50
16004
}
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
16004
describe_nspc_free(Func, func)
62
16004
describe_nspc_free(Type, type)
63
64
16004
ANN static void free_nspc(Nspc a, Gwion gwion) {
65
16004
  free_nspc_value(a, gwion);
66
16004
  nspc_free_func(a, gwion);
67
16004
  if(a->info->op_map.ptr)
68
747
    free_op_map(&a->info->op_map, gwion);
69
16004
  nspc_free_type(a, gwion);
70

16004
  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
16004
  if(a->info->vtable.ptr)
73
10816
    vector_release(&a->info->vtable);
74
16004
  mp_free(gwion->mp, NspcInfo, a->info);
75
16004
  if(a->pre_ctor)
76
7253
    REM_REF(a->pre_ctor, gwion);
77
16004
  if(a->dtor)
78
9240
    REM_REF(a->dtor, gwion);
79
16004
  mp_free(gwion->mp, Nspc, a);
80
16004
}
81
82
16025
ANN Nspc new_nspc(MemPool p, const m_str name) {
83
16025
  const Nspc a = mp_calloc(p, Nspc);
84
16025
  a->name = name;
85
16025
  a->info = mp_calloc(p, NspcInfo);
86
16025
  a->info->value = new_scope(p);
87
16025
  a->info->type = new_scope(p);
88
16025
  a->info->func = new_scope(p);
89
16025
  a->ref = new_refcount(p, free_nspc);
90
16025
  return a;
91
}