GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/env/env.c Lines: 88 88 100.0 %
Date: 2020-09-21 18:02:52 Branches: 10 10 100.0 %

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
715
ANN static struct Env_Scope_ *new_envscope(MemPool p) {
12
715
  struct Env_Scope_ *a = mp_calloc(p, Env_Scope);
13
715
  vector_init(&a->breaks);
14
715
  vector_init(&a->conts);
15
715
  vector_init(&a->class_stack);
16
715
  vector_init(&a->nspc_stack);
17
715
  vector_init(&a->known_ctx);
18
715
  return a;
19
}
20
21
715
Env new_env(MemPool p) {
22
715
  const Env env = (Env)xmalloc(sizeof(struct Env_));
23
715
  env->global_nspc = new_nspc(p, "global_nspc");
24
715
  env->context = NULL;
25
715
  env->scope = new_envscope(p);
26
715
  env_reset(env);
27
715
  return env;
28
}
29
30
1374
ANN void env_reset(const Env env) {
31
1374
  vector_clear(&env->scope->breaks);
32
1374
  vector_clear(&env->scope->conts);
33
1374
  vector_clear(&env->scope->nspc_stack);
34
1374
  vector_add(&env->scope->nspc_stack, (vtype)env->global_nspc);
35
1374
  vector_clear(&env->scope->class_stack);
36
1374
  vector_add(&env->scope->class_stack, (vtype)NULL);
37
1374
  env->curr = env->global_nspc;
38
1374
  env->class_def = NULL;
39
1374
  env->func = NULL;
40
1374
  env->scope->depth = 0;
41
1374
}
42
43
714
ANN void release_ctx(struct Env_Scope_ *a, struct Gwion_ *gwion) {
44
714
  const m_uint size = vector_size(&a->known_ctx);
45
1850
  for(m_uint i = size + 1; --i;)
46
422
    REM_REF((Context)vector_at(&a->known_ctx, i - 1), gwion);
47
714
}
48
49
714
ANN static void free_env_scope(struct Env_Scope_  *a, Gwion gwion) {
50
714
  release_ctx(a, gwion);
51
714
  vector_release(&a->known_ctx);
52
714
  vector_release(&a->nspc_stack);
53
714
  vector_release(&a->class_stack);
54
714
  vector_release(&a->breaks);
55
714
  vector_release(&a->conts);
56
714
  mp_free(gwion->mp, Env_Scope, a);
57
714
}
58
59
714
ANN void free_env(const Env a) {
60
714
  free_env_scope(a->scope, a->gwion);
61
714
  while(pop_global(a->gwion));
62
714
  xfree(a);
63
714
}
64
65
14189
ANN2(1,3) m_uint env_push(const Env env, const Type type, const Nspc nspc) {
66
14189
  const m_uint scope = env->scope->depth;
67
14189
  vector_add(&env->scope->class_stack, (vtype)env->class_def);
68
14189
  env->class_def = type;
69
14189
  vector_add(&env->scope->nspc_stack, (vtype)env->curr);
70
14189
  env->curr = nspc;
71
14189
  env->scope->depth = 0;
72
14189
  return scope;
73
}
74
75
14182
ANN void env_pop(const Env env, const m_uint scope) {
76
14182
  env->class_def = (Type)vector_pop(&env->scope->class_stack);
77
14182
  env->curr = (Nspc)vector_pop(&env->scope->nspc_stack);
78
14182
  env->scope->depth = scope;
79
14182
}
80
81
27077
ANN void env_add_type(const Env env, const Type type) {
82
27077
  const Type v_type = type_copy(env->gwion->mp, env->gwion->type[et_class]);
83
27077
  ADD_REF(v_type);
84
27077
  v_type->e->d.base_type = type;
85
27077
  SET_FLAG(type, builtin);
86
27077
  const Symbol sym = insert_symbol(type->name);
87
27077
  nspc_add_type_front(env->curr, sym, type);
88
27077
  const Value v = new_value(env->gwion->mp, v_type, s_name(sym));
89
27077
  SET_FLAG(v, valid | ae_flag_const | ae_flag_global | ae_flag_builtin);
90
27077
  nspc_add_value(env->curr, insert_symbol(type->name), v);
91
27077
  v->from->owner = type->e->owner = env->curr;
92
27077
  v->from->owner_class = type->e->owner_class = env->class_def; // t owner_class ?
93
27077
  type->xid = ++env->scope->type_xid;
94
27077
}
95
96
623
ANN m_bool type_engine_check_prog(const Env env, const Ast ast) {
97
623
  const Context ctx = new_context(env->gwion->mp, ast, env->name);
98
623
  env_reset(env);
99
623
  load_context(ctx, env);
100
623
  return traverse_ast(env, ast);
101
}
102
103
623
ANN m_bool type_engine_clean_prog(const Env env, m_bool *r) {
104
623
  const m_bool ret = (m_bool)*r;
105
623
  const Context ctx = env->context;
106
623
  if(ret > 0) //{
107
418
    nspc_commit(env->curr);
108

623
  if(ret > 0 || env->context->global)
109
422
    vector_add(&env->scope->known_ctx, (vtype)ctx);
110
  else //nspc_rollback(env->global_nspc);
111
201
    REM_REF(ctx, env->gwion);
112
623
  unload_context(ctx, env);
113
623
  return ret;
114
}