Gwion coverage report


Directory: src/
File: src/env/env.c
Date: 2023-01-30 18:32:28
Exec Total Coverage
Lines: 90 90 100.0%
Functions: 10 10 100.0%
Branches: 8 12 66.7%

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 "gwion.h"
6 #include "operator.h"
7 #include "traverse.h"
8 #include "vm.h"
9 #include "parse.h"
10
11 707 ANN static struct Env_Scope_ *new_envscope(MemPool p) {
12 707 struct Env_Scope_ *a = mp_calloc(p, Env_Scope);
13 707 vector_init(&a->breaks);
14 707 vector_init(&a->conts);
15 707 vector_init(&a->class_stack);
16 707 vector_init(&a->nspc_stack);
17 707 vector_init(&a->known_ctx);
18 707 vector_init(&a->effects);
19 707 return a;
20 }
21
22 707 Env new_env(MemPool p) {
23 707 const Env env = (Env)mp_malloc(p, Env);
24 707 env->global_nspc = new_nspc(p, "global_nspc");
25 707 env->context = NULL;
26 707 env->scope = new_envscope(p);
27 707 vector_add(&env->scope->nspc_stack, (vtype)env->global_nspc);
28 707 env_reset(env);
29 707 return env;
30 }
31
32 1390 ANN void env_reset(const Env env) {
33 1390 const Nspc nspc = (Nspc)vector_front(&env->scope->nspc_stack);
34 1390 vector_clear(&env->scope->breaks);
35 1390 vector_clear(&env->scope->conts);
36 1390 vector_clear(&env->scope->nspc_stack);
37 1390 vector_add(&env->scope->nspc_stack, (vtype)nspc);
38 1390 vector_add(&env->scope->nspc_stack, (vtype)env->global_nspc);
39 1390 vector_clear(&env->scope->class_stack);
40 1390 vector_add(&env->scope->class_stack, (vtype)NULL);
41 1390 vector_add(&env->scope->effects, 0);
42 1390 env->curr = env->global_nspc;
43 1390 env->class_def = NULL;
44 1390 env->func = NULL;
45 1390 env->scope->depth = 0;
46 1390 env->scope->in_try = false;
47 1390 }
48
49 707 ANN void release_ctx(struct Env_Scope_ *a, struct Gwion_ *gwion) {
50 707 const m_uint size = vector_size(&a->known_ctx);
51
2/2
✓ Branch 0 taken 350 times.
✓ Branch 1 taken 707 times.
1057 for (m_uint i = size + 1; --i;) {
52 350 const Context ctx = (Context)vector_at(&a->known_ctx, i - 1);
53 350 context_remref(ctx, gwion);
54 }
55 707 }
56
57 707 ANN static void free_env_scope(struct Env_Scope_ *a, Gwion gwion) {
58 707 vector_release(&a->known_ctx);
59 707 vector_release(&a->nspc_stack);
60 707 vector_release(&a->class_stack);
61 707 vector_release(&a->breaks);
62 707 vector_release(&a->conts);
63 707 const Vector v = &a->effects;
64
2/2
✓ Branch 1 taken 1392 times.
✓ Branch 2 taken 707 times.
2099 for (m_uint i = 0; i < vector_size(v); i++) {
65 1392 MP_Vector *eff = (MP_Vector*)vector_at(v, i);
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1392 times.
1392 if (eff) free_mp_vector(gwion->mp, struct ScopeEffect, eff);
67 }
68 707 vector_release(&a->effects);
69 707 mp_free(gwion->mp, Env_Scope, a);
70 707 }
71
72 4 ANN void env_add_effect(const Env a, const Symbol effect, const loc_t pos) {
73 4 const Vector v = &a->scope->effects;
74 4 MP_Vector *w = (MP_Vector*)vector_back(v);
75
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!w) {
76 4 w = new_mp_vector(a->gwion->mp, struct ScopeEffect, 0);
77 4 VPTR(v, VLEN(v) - 1) = (vtype)w;
78 }
79 4 struct ScopeEffect eff = {effect, pos};
80
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 mp_vector_add(a->gwion->mp, (MP_Vector**)&(VPTR(v, VLEN(v) - 1)) , struct ScopeEffect, eff);
81 4 }
82
83 707 ANN void free_env(const Env a) {
84 707 pop_global(a->gwion);
85 707 free_env_scope(a->scope, a->gwion);
86 707 mp_free(a->gwion->mp, Env, a);
87 707 }
88
89 41844 ANN2(1, 3) m_uint env_push(const Env env, const Type type, const Nspc nspc) {
90 41844 const m_uint scope = env->scope->depth;
91 41844 vector_add(&env->scope->class_stack, (vtype)env->class_def);
92 41844 env->class_def = type;
93 41844 vector_add(&env->scope->nspc_stack, (vtype)env->curr);
94 41844 vector_add(&env->scope->effects, 0);
95 41844 env->curr = nspc;
96 41844 env->scope->depth = 0;
97 41844 return scope;
98 }
99
100 41844 ANN void env_pop(const Env env, const m_uint scope) {
101 41844 env->class_def = (Type)vector_pop(&env->scope->class_stack);
102 41844 env->curr = (Nspc)vector_pop(&env->scope->nspc_stack);
103 41844 MP_Vector *const v = (MP_Vector*)vector_pop(&env->scope->effects);
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41844 times.
41844 if (v) free_mp_vector(env->gwion->mp, struct ScopeEffect, v);
105 41844 env->scope->depth = scope;
106 41844 }
107
108 24882 ANN void env_add_type(const Env env, const Type type, const loc_t loc) {
109 24882 const Symbol sym = insert_symbol(type->name);
110 24882 nspc_add_type_front(env->curr, sym, type);
111 24882 const Value v = mk_class(env, type, loc);
112 24882 SET_FLAG(v, global);
113 24882 set_vflag(v, vflag_builtin);
114 24882 set_tflag(type,
115 tflag_scan0 | tflag_scan1 | tflag_scan2 | tflag_check | tflag_emit);
116 24882 }
117