GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/env/env_utils.c Lines: 71 71 100.0 %
Date: 2020-09-12 17:36:58 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
65769
ANN m_bool env_access(const Env env, const ae_flag flag, const loc_t pos) {
14
65769
  if(env->scope->depth) {
15
267
   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

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

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

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

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

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

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