GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/env/env_utils.c Lines: 77 77 100.0 %
Date: 2020-08-07 19:15:19 Branches: 53 58 91.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
68323
ANN m_bool env_access(const Env env, const ae_flag flag, const loc_t pos) {
14
68323
  if(env->scope->depth) {
15
398
   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

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

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

67477
  return !(env->class_def && GET(flag, ae_flag_global)) ? GW_OK :GW_ERROR;
29
}
30
31
459413
ANN Type _find_type(const Env env, const Symbol xid) {
32
459413
  Type type = nspc_lookup_type1(env->curr, xid);
33

459413
  if(!type && env->class_def) {
34
3
    Type base = env->class_def->e->parent;
35

9
    while(base && base->nspc) {
36
4
      if((type = nspc_lookup_type1(base->nspc, xid)))
37
1
       break;
38
3
      base = base->e->parent;
39
    }
40
  }
41
459413
  return type;
42
}
43
44
459413
ANN Type find_type(const Env env, Type_Decl *path) {
45
459413
  DECL_OO(Type, type, = _find_type(env, path->xid))
46
459372
  Nspc nspc = type->nspc;
47
459372
  path = path->next;
48
918765
  while(path) {
49
22
    const Symbol xid = path->xid;
50
22
    if(nspc) {
51
21
      Type t = nspc_lookup_type1(nspc, xid);
52

45
      while(!t && type && type->e->parent) {
53
3
        if(type->e->parent->nspc) // should we break sooner ?
54
1
          t = nspc_lookup_type1(type->e->parent->nspc, xid); // was lookup2
55
3
        type = type->e->parent;
56
      }
57
21
      if(!t)
58
1
        ERR_O(path->pos, _("...(cannot find class '%s' in nspc '%s')"), s_name(xid), nspc->name)
59
20
      type = t;
60
    }
61
21
    nspc = type->nspc;
62
21
    path = path->next;
63
  }
64
459371
  return type;
65
}
66
67
27731
ANN m_bool already_defined(const Env env, const Symbol s, const loc_t pos) {
68
27731
  const Value v = nspc_lookup_value0(env->curr, s);
69

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