Gwion coverage report


Directory: src/
File: src/import/import_cdef.c
Date: 2023-01-30 18:32:28
Exec Total Coverage
Lines: 92 102 90.2%
Functions: 9 9 100.0%
Branches: 44 64 68.8%

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 "mpool.h"
17 #include "specialid.h"
18 #include "template.h"
19
20 10208 ANN static m_bool mk_dtor(MemPool p, const Type t, const m_uint d) {
21 10208 VM_Code code = t->nspc->dtor = new_vmcode(p, NULL, NULL, t->name, SZ_INT, true, false);
22 10208 code->native_func = (m_uint)d;
23 10208 set_tflag(t, tflag_dtor);
24 10208 return GW_OK;
25 }
26
27 12122 ANN2(1, 2) static void import_class_ini(const Env env, const Type t) {
28 12122 t->nspc = new_nspc(env->gwion->mp, t->name);
29 12122 t->nspc->parent = env->curr;
30
2/2
✓ Branch 0 taken 11484 times.
✓ Branch 1 taken 638 times.
12122 if (isa(t, env->gwion->type[et_object]) > 0) inherit(t);
31 12122 env_push_type(env, t);
32 12122 }
33
34 2552 ANN2(1) void add_template(const Env env, const Type t) {
35 2552 nspc_push_type(env->gwion->mp, env->curr); //
36 2552 Specialized_List sl = t->info->cdef->base.tmpl->list;
37
2/2
✓ Branch 0 taken 3190 times.
✓ Branch 1 taken 2552 times.
5742 for(uint32_t i = 0; i < sl->len; i++) {
38 3190 Specialized *spec = mp_vector_at(sl, Specialized, i);
39 3190 nspc_add_type(env->curr, spec->xid, env->gwion->type[et_auto]);
40 }
41 2552 }
42
43 ANN2(1)
44 10208 void gwi_class_xtor(const Gwi gwi, const f_xtor ctor, const f_xtor dtor) {
45 10208 const Type t = gwi->gwion->env->class_def;
46
1/2
✓ Branch 0 taken 10208 times.
✗ Branch 1 not taken.
10208 if (dtor) mk_dtor(gwi->gwion->mp, t, (m_uint)dtor);
47
2/2
✓ Branch 0 taken 7656 times.
✓ Branch 1 taken 2552 times.
10208 if (ctor) {
48 7656 gwi_func_ini(gwi, "void", "@ctor");
49 7656 gwi_func_end(gwi, ctor, ae_flag_none);
50
2/2
✓ Branch 0 taken 7018 times.
✓ Branch 1 taken 638 times.
7656 if(t->nspc->vtable.ptr) set_tflag(t, tflag_ctor);
51 }
52 10208 }
53
54 9570 ANN static inline void gwi_type_flag(const Type t) {
55 9570 set_tflag(t,
56 tflag_scan0 | tflag_scan1 | tflag_scan2 | tflag_check | tflag_emit);
57 9570 }
58
59 12122 ANN static Type type_finish(const Gwi gwi, const Type t) {
60 12122 tflag(t, tflag_compound);
61 12122 gwi_add_type(gwi, t);
62 12122 import_class_ini(gwi->gwion->env, t);
63
3/4
✓ Branch 0 taken 12122 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2552 times.
✓ Branch 3 taken 9570 times.
12122 if (t->info->cdef && t->info->cdef->base.tmpl) {
64 2552 gwi->tmpls++;
65 2552 add_template(gwi->gwion->env, t);
66 2552 set_tflag(t, tflag_cdef);
67 }
68
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 12122 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
12122 if (gwi->gwion->data->cdoc && t->info->cdef) {
69 gwfmt_indent(gwi->gwfmt);
70 gwi->gwfmt->indent++;
71 gwfmt_class_def(gwi->gwfmt, t->info->cdef);
72 }
73
3/4
✓ Branch 0 taken 12122 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11484 times.
✓ Branch 3 taken 638 times.
12122 if(t->info->cdef && t->info->cdef->base.ext &&
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11484 times.
11484 t->info->cdef->base.ext->array)
75 set_tflag(t, tflag_typedef);
76 12122 set_tflag(t, tflag_compound);
77 12122 return t;
78 }
79
80 ANN2(1, 2)
81 11500 Type gwi_class_ini(const Gwi gwi, const m_str name, const m_str parent) {
82 11500 struct ImportCK ck = {.name = name};
83
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 11498 times.
11500 CHECK_BO(check_typename_def(gwi, &ck));
84
3/4
✓ Branch 0 taken 9572 times.
✓ Branch 1 taken 1926 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11498 times.
11498 DECL_OO(Type_Decl *, td, = gwi_str2td(gwi, parent ?: "Object"));
85
2/2
✓ Branch 0 taken 1915 times.
✓ Branch 1 taken 9583 times.
11498 Tmpl *tmpl = ck.sl ? new_tmpl(gwi->gwion->mp, ck.sl) : NULL;
86
3/4
✓ Branch 0 taken 1915 times.
✓ Branch 1 taken 9583 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1915 times.
11498 if (tmpl) CHECK_BO(template_push_types(gwi->gwion->env, tmpl));
87
2/2
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 11484 times.
11498 DECL_OO(const Type, base, = known_type(gwi->gwion->env, td));
88 11484 const Type_List tl = td->types;
89
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11484 times.
11484 if (tflag(base, tflag_ntmpl)) td->types = NULL;
90
1/2
✓ Branch 0 taken 11484 times.
✗ Branch 1 not taken.
11484 const Type p = !td->types ? known_type(gwi->gwion->env, td) : NULL;
91 11484 td->types = tl;
92
2/2
✓ Branch 0 taken 1914 times.
✓ Branch 1 taken 9570 times.
11484 if (tmpl) nspc_pop_type(gwi->gwion->mp, gwi->gwion->env->curr);
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11484 times.
11484 CHECK_OO(p);
94 11484 const Type t = new_type(gwi->gwion->mp, s_name(ck.sym), p);
95 11484 t->info->cdef = new_class_def(gwi->gwion->mp, 0, ck.sym, td, NULL, gwi->loc);
96 11484 t->info->cdef->base.tmpl = tmpl;
97 11484 t->info->cdef->base.type = t;
98 11484 t->info->tuple = new_tupleform(gwi->gwion->mp, p);
99 11484 t->info->parent = p;
100
2/2
✓ Branch 0 taken 1914 times.
✓ Branch 1 taken 9570 times.
11484 if (ck.sl)
101 1914 set_tflag(t, tflag_tmpl | tflag_ntmpl);
102 else
103 9570 gwi_type_flag(t);
104 11484 return type_finish(gwi, t);
105 }
106
107 638 ANN Type gwi_struct_ini(const Gwi gwi, const m_str name) {
108 638 struct ImportCK ck = {.name = name};
109
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 638 times.
638 CHECK_BO(check_typename_def(gwi, &ck));
110 const Type t =
111 638 new_type(gwi->gwion->mp, s_name(ck.sym), gwi->gwion->type[et_compound]);
112 638 t->size = 0;
113 638 set_tflag(t, tflag_struct);
114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 638 times.
638 if (!ck.sl)
115 gwi_type_flag(t);
116 else {
117 1276 t->info->cdef =
118 638 new_class_def(gwi->gwion->mp, 0, ck.sym, NULL, NULL, gwi->loc);
119 638 t->info->cdef->base.type = t;
120 638 t->info->cdef->base.tmpl = new_tmpl(gwi->gwion->mp, ck.sl);
121 638 t->info->tuple = new_tupleform(gwi->gwion->mp, NULL);
122 638 t->info->parent = NULL;
123 638 t->info->cdef->cflag |= cflag_struct;
124 638 set_tflag(t, tflag_tmpl | tflag_ntmpl);
125 }
126 638 return type_finish(gwi, t);
127 }
128
129 12123 ANN m_int gwi_class_end(const Gwi gwi) {
130
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 12123 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
12123 if (gwi->gwion->data->cdoc && gwi->gwion->env->class_def->info->cdef) {
131 gwi->gwfmt->indent--;
132 gwfmt_indent(gwi->gwfmt);
133 gwfmt_rbrace(gwi->gwfmt);
134 gwfmt_nl(gwi->gwfmt);
135 }
136
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 12122 times.
12123 if (!gwi->gwion->env->class_def)
137 1 GWI_ERR_B(_("import: too many class_end called."))
138 12122 const Type t = gwi->gwion->env->class_def;
139
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 12122 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
12122 if (t->nspc->class_data_size && !t->nspc->class_data)
140 nspc_allocdata(gwi->gwion->mp, t->nspc);
141
2/2
✓ Branch 1 taken 2552 times.
✓ Branch 2 taken 9570 times.
12122 if (tflag(t, tflag_tmpl)) {
142 2552 --gwi->tmpls;
143 2552 nspc_pop_type(gwi->gwion->mp, gwi->gwion->env->curr);
144 }
145 /*
146 // we need to use @ctor
147 if (gwi->effects.ptr) {
148 vector_init(&t->effects);
149 vector_copy2(&gwi->effects, &t->effects);
150 vector_release(&gwi->effects);
151 }
152 */
153 12122 env_pop(gwi->gwion->env, 0);
154 12122 return GW_OK;
155 }
156