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

619
  if(ret > 0 || env->context->global)
104
413
    vector_add(&env->scope->known_ctx, (vtype)ctx);
105
  else //nspc_rollback(env->global_nspc);
106
206
    REM_REF(ctx, env->gwion);
107
619
  unload_context(ctx, env);
108
619
  return ret;
109
}