GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/env/nspc.c Lines: 61 62 98.4 %
Date: 2020-10-03 11:19:27 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
3442
ANN static inline void nspc_release_object(const Nspc a, Value value, Gwion gwion) {
16


6860
  if(!GET_FLAG(value, pure) && ((GET_FLAG(value, static) && a->info->class_data) ||
17
5801
    (value->d.ptr && GET_FLAG(value, builtin)))) {
18
2144
    const M_Object obj = value->d.ptr ? (M_Object)value->d.ptr :
19
3
        *(M_Object*)(a->info->class_data + value->from->offset);
20
2141
       release(obj, gwion->vm->cleaner_shred);
21
  }
22
3442
}
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
16043
ANN static void free_nspc_value(const Nspc a, Gwion gwion) {
40
16043
  struct scope_iter iter = { a->info->value, 0, 0 };
41
  Value v;
42
161717
  while(scope_iter(&iter, &v) > 0) {
43
129631
    if(isa(v->type, gwion->type[et_object]) > 0)
44
3442
      nspc_release_object(a, v, gwion);
45
126189
    else if(GET_FLAG(v->type, struct))
46
10
      nspc_release_struct(a, v, gwion);
47
129631
    REM_REF(v, gwion);
48
  }
49
16043
  free_scope(gwion->mp, a->info->value);
50
16043
}
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
16043
describe_nspc_free(Func, func)
62
16043
describe_nspc_free(Type, type)
63
64
16043
ANN static void free_nspc(Nspc a, Gwion gwion) {
65
16043
  free_nspc_value(a, gwion);
66
16043
  nspc_free_func(a, gwion);
67
16043
  if(a->info->op_map.ptr)
68
749
    free_op_map(&a->info->op_map, gwion);
69
16043
  nspc_free_type(a, gwion);
70

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