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

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