GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/env/env_utils.c Lines: 71 71 100.0 %
Date: 2020-10-03 10:30:04 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
65934
ANN m_bool env_access(const Env env, const ae_flag flag, const loc_t pos) {
14
65934
  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

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

65934
      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
65918
  return GW_OK;
24
}
25
26
65112
ANN m_bool env_storage(const Env env, ae_flag flag, const loc_t pos) {
27
65112
  CHECK_BB(env_access(env, flag, pos))
28

65099
  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
887274
ANN Type _find_type(const Env env, const Symbol xid) {
43
887274
  const Type type = nspc_lookup_type1(env->curr, xid);
44

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

1774515
  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
887246
  return type;
58
}
59
60
27138
ANN m_bool already_defined(const Env env, const Symbol s, const loc_t pos) {
61
27138
  const Value v = nspc_lookup_value0(env->curr, s);
62

27138
  if(!v || is_class(env->gwion, v->type))
63
27137
    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
5641
ANN static Type class_type(const Env env, const Type base) {
71
5641
  const Type t_class = env->gwion->type[et_class];
72
5641
  const Type t = type_copy(env->gwion->mp, t_class);
73
5641
  t->e->parent = t_class;
74
5641
  t->e->ctx = base->e->ctx;
75
5641
  t->e->d.base_type = base;
76
5641
  SET_FLAG(t, infer);
77
5641
  return t;
78
}
79
80
5641
ANN Value mk_class(const Env env, const Type base) {
81
5641
  const Type t = class_type(env, base);
82
5641
  const Symbol sym = insert_symbol(base->name);
83
5641
  const Value v = new_value(env->gwion->mp, t, s_name(sym));
84
5641
  valuefrom(env, v->from);
85
5641
  SET_FLAG(v, const | ae_flag_valid);
86
5641
  nspc_add_value_front(base->e->owner, sym, v);
87
5641
  return v;
88
}
89
90
327
ANN Value global_string(const Env env, const m_str str) {
91
327
  char c[strlen(str) + 8];
92
327
  sprintf(c, "%s:string", str);
93
327
  const Symbol sym = insert_symbol(c);
94
327
  const Value v = nspc_lookup_value0(env->global_nspc, sym);
95
327
  if(v)
96
66
    return v;
97
261
  const Value value = new_value(env->gwion->mp, env->gwion->type[et_string], s_name(sym));
98
261
  nspc_add_value_front(env->global_nspc, sym, value);
99
261
  return value;
100
}
101
102
45624
ANN m_bool isres(const Env env, const Symbol xid, const loc_t pos) {
103
45624
  if(vector_find(&env->gwion->data->reserved, (vtype)xid) > -1)
104
1
    ERR_B(pos, _("%s is reserved."), s_name(xid));
105
45623
  return GW_OK;
106
}