Gwion coverage report


Directory: src/
File: src/env/env_utils.c
Date: 2023-01-30 18:32:28
Exec Total Coverage
Lines: 74 74 100.0%
Functions: 10 10 100.0%
Branches: 55 60 91.7%

Line Branch Exec Source
1 #include "gwion_util.h"
2 #include "gwion_ast.h"
3 #include "gwion_env.h"
4 #include "traverse.h"
5 #include "vm.h"
6 #include "parse.h"
7
8 #define GET(a, b) ((a) & (b)) == (b)
9 121505 ANN m_bool env_access(const Env env, const ae_flag flag, const loc_t pos) {
10
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 121305 times.
121505 if (env->scope->depth) {
11
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 198 times.
200 if (GET(flag, ae_flag_global))
12
3/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
2 ERR_B(pos, _("`{G}global{0}` can only be used at %s scope."),
13 GET(flag, ae_flag_global) && !env->class_def ? "file" : "class")
14 }
15
4/4
✓ Branch 0 taken 102539 times.
✓ Branch 1 taken 18964 times.
✓ Branch 2 taken 102527 times.
✓ Branch 3 taken 12 times.
121503 if ((GET(flag, ae_flag_static) || GET(flag, ae_flag_private) ||
16
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 102525 times.
102527 GET(flag, ae_flag_protect)) &&
17
3/4
✓ Branch 0 taken 18969 times.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18969 times.
18978 (!env->class_def || env->scope->depth))
18 9 ERR_B(pos, _("`{G}static/private/protect{0}` can only be used at class scope."))
19 121494 return GW_OK;
20 }
21
22 112102 ANN m_bool env_storage(const Env env, ae_flag flag, const loc_t pos) {
23
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 112094 times.
112102 CHECK_BB(env_access(env, flag, pos));
24
4/4
✓ Branch 0 taken 91745 times.
✓ Branch 1 taken 20349 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 91742 times.
112094 if(env->class_def && GET(flag, ae_flag_global))
25 3 ERR_B(pos, _("`{G}global{0}` at class scope only valid for function pointers"));
26 112091 return GW_OK;
27 }
28 #undef GET
29
30 #define RETURN_TYPE(a) \
31 do { \
32 const Type t = (a); \
33 if (t) return t; \
34 } while (0)
35
36 265811 ANN static Type find_in_parent(const Type type, const Symbol xid) {
37 265811 Type base = type;
38
3/4
✓ Branch 0 taken 265832 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 265827 times.
✓ Branch 3 taken 5 times.
265832 while (base && base->nspc) {
39
2/2
✓ Branch 1 taken 265806 times.
✓ Branch 2 taken 21 times.
265827 RETURN_TYPE(nspc_lookup_type1(base->nspc, xid));
40 21 base = base->info->parent;
41 }
42 5 return NULL;
43 }
44
45 1096243 ANN Type find_initial(const Env env, const Symbol xid) {
46
4/4
✓ Branch 0 taken 265791 times.
✓ Branch 1 taken 830452 times.
✓ Branch 3 taken 265787 times.
✓ Branch 4 taken 4 times.
1096243 if (env->class_def) RETURN_TYPE(find_in_parent(env->class_def, xid));
47
2/2
✓ Branch 1 taken 829132 times.
✓ Branch 2 taken 1324 times.
830456 RETURN_TYPE(nspc_lookup_type1(env->curr, xid));
48 1324 const Vector v = &env->scope->nspc_stack;
49
2/2
✓ Branch 1 taken 1443 times.
✓ Branch 2 taken 46 times.
1489 for (m_uint i = vector_size(v) + 1; --i;) {
50 1443 const Nspc nspc = (Nspc)vector_at(v, i - 1);
51
2/2
✓ Branch 1 taken 1278 times.
✓ Branch 2 taken 165 times.
1443 RETURN_TYPE(nspc_lookup_type1(nspc, xid));
52 }
53 46 return NULL;
54 }
55 #undef RETURN_TYPE
56
57 1096243 ANN Type find_type(const Env env, Type_Decl *path) {
58
2/2
✓ Branch 1 taken 46 times.
✓ Branch 2 taken 1096197 times.
1096243 DECL_OO(Type, type, = find_initial(env, path->xid));
59
5/6
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 1096195 times.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 20 times.
✓ Branch 5 taken 1 times.
1096216 while ((path = path->next) && type && type->nspc) {
60 20 const Nspc nspc = type->nspc;
61
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 19 times.
20 if(!(type = find_in_parent(type, path->xid)))
62 1 ERR_O(path->pos, _("...(cannot find class '%s' in nspc '%s')"),
63 s_name(path->xid), nspc->name)
64 }
65 1096196 return type;
66 }
67
68 56381 ANN m_bool already_defined(const Env env, const Symbol s, const loc_t pos) {
69 56381 const Value v = nspc_lookup_value0(env->curr, s);
70
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 56380 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
56381 if (!v || is_class(env->gwion, v->type)) return GW_OK;
71 1 gwerr_basic(_("already declared as variable"), NULL, NULL, env->name, pos, 0);
72 1 declared_here(v);
73 1 env_error_footer(env);
74 1 return GW_ERROR;
75 }
76
77 38500 ANN static Type class_type(const Env env, const Type base) {
78 38500 const Type t_class = env->gwion->type[et_class];
79 38500 const Type t = type_copy(env->gwion->mp, t_class);
80 38500 t->info->parent = t_class;
81 38500 t->info->base_type = base;
82 38500 set_tflag(t, tflag_infer);
83 38500 return t;
84 }
85
86 38500 ANN Value mk_class(const Env env, const Type base, const loc_t loc) {
87 38500 const Type t = class_type(env, base);
88 38500 const Symbol sym = insert_symbol(base->name);
89 38500 const Value v = new_value(env, t, s_name(sym), loc);
90 38500 valuefrom(env, v->from);
91 38500 SET_FLAG(v, const);
92 38500 set_vflag(v, vflag_valid);
93 38500 nspc_add_value_front(env->curr, sym, v);
94 38500 t->info->value = base->info->value = v;
95 38500 return v;
96 }
97
98 370 ANN Value global_string(const Env env, const m_str str, const loc_t loc) {
99 370 char c[strlen(str) + 8];
100 370 sprintf(c, "%s:string", str);
101 370 const Symbol sym = insert_symbol(c);
102 370 const Value v = nspc_lookup_value0(env->global_nspc, sym);
103
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 262 times.
370 if (v) return v;
104 const Value value =
105 262 new_value(env, env->gwion->type[et_string], s_name(sym), loc);
106 262 _nspc_add_value_front(env->global_nspc, sym, value);
107 262 return value;
108 }
109
110 57257 ANN m_bool isres(const Env env, const Symbol xid, const loc_t pos) {
111 57257 const Map map = &env->gwion->data->id;
112
2/2
✓ Branch 1 taken 383077 times.
✓ Branch 2 taken 57256 times.
440333 for (m_uint i = 0; i < map_size(map); i++) {
113
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 383076 times.
383077 if (xid == (Symbol)VKEY(map, i))
114 1 ERR_B(pos, _("%s is reserved."), s_name(xid));
115 }
116 57256 return GW_OK;
117 }
118