Gwion coverage report


Directory: src/
File: src/import/import_fdef.c
Date: 2023-01-30 18:32:28
Exec Total Coverage
Lines: 79 93 84.9%
Functions: 14 14 100.0%
Branches: 28 42 66.7%

Line Branch Exec Source
1 #include <stdlib.h>
2 #include <string.h>
3 #include <ctype.h>
4 #include "gwion_util.h"
5 #include "gwion_ast.h"
6 #include "gwion_env.h"
7 #include "vm.h"
8 #include "traverse.h"
9 #include "instr.h"
10 #include "emit.h"
11 #include "gwion.h"
12 #include "object.h"
13 #include "operator.h"
14 #include "import.h"
15 #include "gwi.h"
16 #include "clean.h"
17
18 ANN2(1, 2, 3)
19 63174 static m_bool dl_func_init(const Gwi gwi, const restrict m_str t,
20 const restrict m_str n) {
21
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 63173 times.
63174 CHECK_BB(ck_ini(gwi, ck_fdef));
22 63173 gwi->ck->name = n;
23
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 63173 times.
63173 CHECK_BB(check_typename_def(gwi, gwi->ck));
24
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 63173 times.
63173 CHECK_OB((gwi->ck->td = gwi_str2td(gwi, t)));
25 63173 gwi->ck->mpv = new_mp_vector(gwi->gwion->mp, Arg, 0);
26 63173 return GW_OK;
27 }
28
29 59984 ANN m_int gwi_func_ini(const Gwi gwi, const restrict m_str t,
30 const restrict m_str n) {
31 59984 return dl_func_init(gwi, t, n);
32 }
33
34 63170 ANEW ANN static Func_Base *gwi_func_base(const Gwi gwi, ImportCK *ck) {
35
2/2
✓ Branch 0 taken 29992 times.
✓ Branch 1 taken 33178 times.
63170 Arg_List args = gwi->ck->mpv->len ? cpy_arg_list(gwi->gwion->mp, gwi->ck->mpv) : NULL;
36 63170 Func_Base * base = new_func_base(gwi->gwion->mp, ck->td, ck->sym, args,
37 63170 ck->flag, gwi->loc);
38 63170 ck->td = NULL;
39
2/2
✓ Branch 0 taken 5106 times.
✓ Branch 1 taken 58064 times.
63170 if (ck->tmpl) {
40 5106 base->tmpl = gwi_tmpl(gwi);
41 5106 ck->tmpl = NULL;
42 }
43 63170 return base;
44 }
45
46 59980 ANEW ANN static Func_Def import_fdef(const Gwi gwi, ImportCK *ck) {
47 59980 Func_Base * base = gwi_func_base(gwi, ck);
48 59980 const Func_Def fdef = new_func_def(gwi->gwion->mp, base, NULL);
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59980 times.
59980 if (gwi->gwion->data->cdoc) {
50 gwfmt_indent(gwi->gwfmt);
51 gwfmt_func_def(gwi->gwfmt, fdef);
52 }
53
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59980 times.
59980 if (gwi->effects.ptr) {
54 vector_init(&fdef->base->effects);
55 vector_copy2(&gwi->effects, &fdef->base->effects);
56 vector_release(&gwi->effects);
57 }
58 59980 return fdef;
59 }
60
61 9570 ANN static m_bool section_fdef(const Gwi gwi, const Func_Def fdef) {
62 9570 Section section = (Section) {
63 .section_type = ae_section_func,
64 .d = { .func_def = fdef }
65 };
66 9570 gwi_body(gwi, &section);
67 9570 return GW_OK;
68 }
69
70 8 ANN static m_bool error_fdef(const Gwi gwi, const Func_Def fdef) {
71 8 func_def_cleaner(gwi->gwion, fdef);
72 8 return GW_ERROR;
73 }
74
75 59980 ANN m_int gwi_func_valid(const Gwi gwi, ImportCK *ck) {
76 59980 const Func_Def fdef = import_fdef(gwi, ck);
77 59980 fdef->builtin = true;
78
2/2
✓ Branch 1 taken 9570 times.
✓ Branch 2 taken 50410 times.
59980 if (safe_tflag(gwi->gwion->env->class_def, tflag_tmpl)) {
79 9570 section_fdef(gwi, fdef);
80 9570 fdef->d.dl_func_ptr = ck->addr;
81 9570 return GW_OK;
82 }
83
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 50402 times.
50410 if (traverse_func_def(gwi->gwion->env, fdef) < 0)
84 8 return error_fdef(gwi, fdef);
85 50402 builtin_func(gwi->gwion, fdef->base->func, ck->addr);
86 50402 return GW_OK;
87 }
88
89 59980 ANN m_int gwi_func_end(const Gwi gwi, const f_xfun addr, const ae_flag flag) {
90
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 59980 times.
59980 CHECK_BB(ck_ok(gwi, ck_fdef));
91 59980 gwi->ck->addr = addr;
92 59980 gwi->ck->flag = flag;
93 59980 const m_bool ret = gwi_func_valid(gwi, gwi->ck);
94 59980 ck_end(gwi);
95 59980 return ret;
96 }
97
98 39584 ANN m_int gwi_func_arg(const Gwi gwi, const restrict m_str t,
99 const restrict m_str n) {
100
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 39584 times.
39584 CHECK_BB(ck_ok(gwi, ck_fdef));
101
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 39583 times.
39584 DECL_OB(Type_Decl *, td, = gwi_str2td(gwi, t));
102 struct Var_Decl_ var;
103
1/2
✓ Branch 1 taken 39583 times.
✗ Branch 2 not taken.
39583 if(gwi_str2var(gwi, &var, n) > 0) {
104 39583 Arg arg = { .td = td, .var_decl = var };
105
2/2
✓ Branch 0 taken 38294 times.
✓ Branch 1 taken 1289 times.
39583 mp_vector_add(gwi->gwion->mp, &gwi->ck->mpv, Arg, arg);
106 39583 return GW_OK;
107 }
108 free_type_decl(gwi->gwion->mp, td);
109 return GW_ERROR;
110 }
111
112 3190 ANN m_int gwi_fptr_ini(const Gwi gwi, const restrict m_str type,
113 const restrict m_str name) {
114 3190 return dl_func_init(gwi, type, name);
115 }
116
117 3190 ANN static Fptr_Def import_fptr(const Gwi gwi) {
118 3190 Func_Base *base = gwi_func_base(gwi, gwi->ck);
119 3190 return new_fptr_def(gwi->gwion->mp, base);
120 }
121
122 3190 ANN static m_bool section_fptr(const Gwi gwi, const Fptr_Def fdef) {
123 3190 Section section = (Section) {
124 .section_type = ae_section_fptr,
125 .d = { .fptr_def = fdef }
126 };
127 3190 gwi_body(gwi, &section);
128 3190 return GW_OK;
129 }
130
131 3190 ANN Type gwi_fptr_end(const Gwi gwi, const ae_flag flag) {
132
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3190 times.
3190 CHECK_BO(ck_ok(gwi, ck_fdef));
133
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3190 times.
3190 DECL_OO(const Fptr_Def, fptr, = import_fptr(gwi));
134 3190 fptr->base->flag |= flag;
135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3190 times.
3190 if (gwi->gwion->data->cdoc) {
136 gwfmt_indent(gwi->gwfmt);
137 gwfmt_fptr_def(gwi->gwfmt, fptr);
138 }
139
1/2
✓ Branch 1 taken 3190 times.
✗ Branch 2 not taken.
3190 if (safe_tflag(gwi->gwion->env->class_def,
140 tflag_tmpl) /*&& !fptr->base->tmpl*/) {
141 3190 section_fptr(gwi, fptr);
142 3190 ck_end(gwi);
143 3190 return (Type)GW_OK;
144 }
145 const m_bool ret = traverse_fptr_def(gwi->gwion->env, fptr);
146 // if (fptr->base->func) // is it needed ?
147 // set_vflag(fptr->base->func->value_ref, vflag_builtin);
148 const Type t = ret > 0 ? fptr->cdef->base.type : NULL;
149 free_fptr_def(gwi->gwion->mp, fptr);
150 ck_end(gwi);
151 return t;
152 }
153
154 3 ANN void ck_clean_fdef(MemPool mp, ImportCK *ck) {
155
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (ck->td) free_type_decl(mp, ck->td);
156 3 free_arg_list(mp, ck->mpv);
157
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (ck->tmpl) free_id_list(mp, ck->tmpl);
158 3 }
159