GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/env/env_utils.c Lines: 71 71 100.0 %
Date: 2020-09-14 20:46:08 Branches: 47 52 90.4 %

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
15
ANN Map env_label(const Env env) {
9
15
  return &env->context->lbls;
10
}
11
12
#define GET(a,b) ((a) & (b)) == (b)
13
65832
ANN m_bool env_access(const Env env, const ae_flag flag, const loc_t pos) {
14
65832
  if(env->scope->depth) {
15
264
   if(GET(flag, ae_flag_global))
16

2
      ERR_B(pos, _("'global' can only be used at %s scope."),
17
          GET(flag, ae_flag_global) && !env->class_def ?
18
           "file" : "class")
19
  }
20

130857
  if((GET(flag, ae_flag_static) || GET(flag, ae_flag_private) ||
21

65832
      GET(flag, ae_flag_protect)) && (!env->class_def || env->scope->depth))
22
14
      ERR_B(pos, _("static/private/protect can only be used at class scope."))
23
65816
  return GW_OK;
24
}
25
26
65016
ANN m_bool env_storage(const Env env, ae_flag flag, const loc_t pos) {
27
65016
  CHECK_BB(env_access(env, flag, pos))
28

65003
  return !(env->class_def && GET(flag, ae_flag_global)) ? GW_OK :GW_ERROR;
29
}
30
31
27
ANN Type __find_type(const Type type, const Symbol xid) {
32
27
  Type base = type;
33

64
  while(base && base->nspc) {
34
34
    const Type t = nspc_lookup_type1(base->nspc, xid);
35
34
    if(t)
36
24
      return t;
37
10
    base = base->e->parent;
38
  }
39
3
  return NULL;
40
}
41
42
885934
ANN Type _find_type(const Env env, const Symbol xid) {
43
885934
  const Type type = nspc_lookup_type1(env->curr, xid);
44

885934
  if(type || !env->class_def)
45
885929
    return type;
46
5
  return __find_type(env->class_def, xid);
47
}
48
49
885934
ANN Type find_type(const Env env, Type_Decl *path) {
50
885934
  DECL_OO(Type, type, = _find_type(env, path->xid))
51

1771837
  while((path = path->next) && type && type->nspc) {
52
22
    const Nspc nspc = type->nspc;
53
22
    type = __find_type(type, path->xid);
54
22
    if(!type)
55
1
      ERR_O(path->pos, _("...(cannot find class '%s' in nspc '%s')"), s_name(path->xid), nspc->name)
56
  }
57
885907
  return type;
58
}
59
60
27091
ANN m_bool already_defined(const Env env, const Symbol s, const loc_t pos) {
61
27091
  const Value v = nspc_lookup_value0(env->curr, s);
62

27091
  if(!v || is_class(env->gwion, v->type))
63
27090
    return GW_OK;
64
1
  env_err(env, pos,
65
1
      _("'%s' already declared as variable of type '%s'."), s_name(s), v->type->name);
66
1
  return GW_ERROR;
67
}
68
69
70
5624
ANN static Type class_type(const Env env, const Type base) {
71
5624
  const Type t_class = env->gwion->type[et_class];
72
5624
  const Type t = type_copy(env->gwion->mp, t_class);
73
5624
  t->e->parent = t_class;
74
5624
  t->e->ctx = base->e->ctx;
75
5624
  t->e->d.base_type = base;
76
5624
  SET_FLAG(t, infer);
77
5624
  return t;
78
}
79
80
5624
ANN Value mk_class(const Env env, const Type base) {
81
5624
  const Type t = class_type(env, base);
82
5624
  const Symbol sym = insert_symbol(base->name);
83
5624
  const Value v = new_value(env->gwion->mp, t, s_name(sym));
84
5624
  valuefrom(env, v->from);
85
5624
  SET_FLAG(v, const | ae_flag_valid);
86
5624
  nspc_add_value_front(base->e->owner, sym, v);
87
5624
  return v;
88
}
89
90
326
ANN Value global_string(const Env env, const m_str str) {
91
326
  char c[strlen(str) + 8];
92
326
  sprintf(c, "%s:string", str);
93
326
  const Symbol sym = insert_symbol(c);
94
326
  const Value v = nspc_lookup_value0(env->global_nspc, sym);
95
326
  if(v)
96
66
    return v;
97
260
  const Value value = new_value(env->gwion->mp, env->gwion->type[et_string], s_name(sym));
98
260
  nspc_add_value_front(env->global_nspc, sym, value);
99
260
  return value;
100
}
101
102
45555
ANN m_bool isres(const Env env, const Symbol xid, const loc_t pos) {
103
45555
  if(vector_find(&env->gwion->data->reserved, (vtype)xid) > -1)
104
1
    ERR_B(pos, _("%s is reserved."), s_name(xid));
105
45554
  return GW_OK;
106
}