Gwion coverage report


Directory: src/
File: src/parse/type_decl.c
Date: 2023-01-30 18:32:28
Exec Total Coverage
Lines: 87 91 95.6%
Functions: 10 10 100.0%
Branches: 36 58 62.1%

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 "traverse.h"
6 #include "parse.h"
7 #include "object.h"
8 #include "operator.h"
9 #include "instr.h"
10 #include "import.h"
11
12 7 ANN static Type _option(const Env env, Type_Decl *td, const uint8_t n) {
13 7 const Array_Sub array = td->array;
14 7 td->array = NULL;
15 7 Type_List tl = new_mp_vector(env->gwion->mp, Type_Decl*, 1);
16 7 mp_vector_set(tl, Type_Decl*, 0, td);
17 7 Type_Decl tmp = {
18 7 .xid = insert_symbol("Option"), .types = tl, .pos = td->pos};
19
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 const Type t = !(n - 1) ? known_type(env, &tmp) : _option(env, &tmp, n - 1);
20 7 free_mp_vector(env->gwion->mp, Type_Decl*, tl);
21 7 td->array = array;
22 7 return t;
23 }
24
25 7 ANN static Type option(const Env env, Type_Decl *td) {
26 7 const uint8_t option = td->option;
27 7 td->option = 0;
28 7 const Type t = _option(env, td, option);
29 7 td->option = option;
30 7 return t;
31 }
32
33 641 ANN static Type _ref(const Env env, Type_Decl *td) {
34 641 Type_List tl = new_mp_vector(env->gwion->mp, Type_Decl*, 1);
35 641 mp_vector_set(tl, Type_Decl*, 0, td);
36 641 Type_Decl tmp = {.xid = insert_symbol("Ref"), .types = tl, .pos = td->pos};
37 641 const Type t = known_type(env, &tmp);
38 641 free_mp_vector(env->gwion->mp, Type_Decl*, tl);
39 641 return t;
40 }
41
42 641 ANN static inline Type ref(const Env env, Type_Decl *td) {
43 641 td->ref = false;
44 641 const Type t = _ref(env, td);
45 641 td->ref = true;
46 641 return t;
47 }
48
49 1 ANN static Symbol symname(const Env env, Func_Base *const base, bool *global) {
50 GwText text;
51 1 text_init(&text, env->gwion->mp);
52 1 text_add(&text, "(");
53
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 DECL_OO(const Type, t, = known_type(env, base->td));
54
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 DECL_OO(const m_str, name, = type2str(env->gwion, t, base->td->pos));
55 1 text_add(&text, name);
56 1 free_mstr(env->gwion->mp, name);
57 1 text_add(&text, "(");
58 1 *global = type_global(env, t);
59
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if(base->args) {
60
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for(uint32_t i = 0; i < base->args->len; i++) {
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if(i) text_add(&text, ",");
62 1 Arg *arg = mp_vector_at(base->args, Arg, i);
63
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 DECL_OO(const Type, t, = known_type(env, arg->td));
64
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 DECL_OO(const m_str, name, = type2str(env->gwion, t, arg->td->pos));
65 1 text_add(&text, name);
66 1 free_mstr(env->gwion->mp, name);
67
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if(*global)
68 1 *global = type_global(env, t);
69 }
70 }
71 1 text_add(&text, ")");
72 1 text_add(&text, ")");
73 1 base->xid = insert_symbol(text.str);
74 1 text_release(&text);
75 1 return base->xid;
76 }
77
78 557957 ANN static inline Type find(const Env env, Type_Decl *td) {
79
2/2
✓ Branch 0 taken 557956 times.
✓ Branch 1 taken 1 times.
557957 if (!td->fptr) return find_type(env, td);
80 1 bool global = false;
81
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 CHECK_OO((td->xid = symname(env, td->fptr->base, &global)));
82 1 const Fptr_Def fptr = td->fptr;
83 1 td->fptr = NULL;
84 1 const Type exists = find_type(env, td);
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if(exists) return exists;
86 2 const m_uint scope = env->context
87 1 ? env_push(env, NULL, env->context->nspc)
88
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 : env_push_global(env);
89 1 const m_bool ret = traverse_fptr_def(env, fptr);
90 1 env_pop(env, scope);
91 1 const Type t = fptr->cdef->base.type;
92 1 free_fptr_def(env->gwion->mp, fptr);
93
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 return ret > 0 ? t : NULL;
94 }
95
96 557913 ANN static inline Type find1(const Env env, const Type base, Type_Decl *td) {
97
1/2
✓ Branch 0 taken 557913 times.
✗ Branch 1 not taken.
557913 if (!td->fptr) return scan_type(env, base, td);
98 if (!td->fptr->cdef->base.type) {
99 CHECK_BO(scan0_fptr_def(env, td->fptr));
100 CHECK_BO(traverse_fptr_def(env, td->fptr));
101 }
102 return td->fptr->cdef->base.type;
103 }
104
105 557957 ANN static Type resolve(const Env env, Type_Decl *td) {
106 557957 Type_Decl *last = td;
107
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 557957 times.
557980 while (last->next) last = last->next;
108
2/2
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 557913 times.
557957 DECL_OO(const Type, base, = find(env, td));
109 557913 const Context ctx = base->info->value->from->ctx;
110
3/4
✓ Branch 0 taken 548 times.
✓ Branch 1 taken 557365 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 548 times.
557913 if (ctx && ctx->error) ERR_O(td->pos, _("type '%s' is invalid"), base->name)
111
2/2
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 557901 times.
557913 DECL_OO(const Type, type, = find1(env, base, td));
112
3/4
✓ Branch 0 taken 557260 times.
✓ Branch 1 taken 641 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 557901 times.
557901 DECL_OO(const Type, t, = !td->ref ? type : ref(env, td));
113
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 557894 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 557901 times.
557901 DECL_OO(const Type, ret, = !td->option ? t : option(env, td));
114 557901 const Array_Sub array = last->array;
115
2/2
✓ Branch 0 taken 22249 times.
✓ Branch 1 taken 535652 times.
557901 return !array ? ret : array_type(env, ret, array->depth);
116 }
117
118 56 ANN static inline void *type_unknown(const Env env, const Type_Decl *td) {
119 56 env_err(env, td->pos, _("unknown type '%s'"), s_name(td->xid));
120 56 return NULL;
121 }
122
123 557957 ANN Type known_type(const Env env, Type_Decl *td) {
124
2/2
✓ Branch 1 taken 557901 times.
✓ Branch 2 taken 56 times.
557957 return resolve(env, td) ?: type_unknown(env, td);
125 }
126