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 |
|
65842 |
ANN m_bool env_access(const Env env, const ae_flag flag, const loc_t pos) { |
14 |
✓✓ |
65842 |
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 |
✓✓✓✓ ✓✓ |
130877 |
if((GET(flag, ae_flag_static) || GET(flag, ae_flag_private) || |
21 |
✓✓✓✓
|
65842 |
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 |
|
65826 |
return GW_OK; |
24 |
|
|
} |
25 |
|
|
|
26 |
|
65022 |
ANN m_bool env_storage(const Env env, ae_flag flag, const loc_t pos) { |
27 |
✓✓ |
65022 |
CHECK_BB(env_access(env, flag, pos)) |
28 |
✓✓✓✗
|
65009 |
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 |
|
886035 |
ANN Type _find_type(const Env env, const Symbol xid) { |
43 |
|
886035 |
const Type type = nspc_lookup_type1(env->curr, xid); |
44 |
✓✓✓✓
|
886035 |
if(type || !env->class_def) |
45 |
|
886030 |
return type; |
46 |
|
5 |
return __find_type(env->class_def, xid); |
47 |
|
|
} |
48 |
|
|
|
49 |
|
886035 |
ANN Type find_type(const Env env, Type_Decl *path) { |
50 |
✓✓ |
886035 |
DECL_OO(Type, type, = _find_type(env, path->xid)) |
51 |
✓✓✓✗ ✓✓ |
1772039 |
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 |
|
886008 |
return type; |
58 |
|
|
} |
59 |
|
|
|
60 |
|
27099 |
ANN m_bool already_defined(const Env env, const Symbol s, const loc_t pos) { |
61 |
|
27099 |
const Value v = nspc_lookup_value0(env->curr, s); |
62 |
✓✓✗✓
|
27099 |
if(!v || is_class(env->gwion, v->type)) |
63 |
|
27098 |
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 |
|
5634 |
ANN static Type class_type(const Env env, const Type base) { |
71 |
|
5634 |
const Type t_class = env->gwion->type[et_class]; |
72 |
|
5634 |
const Type t = type_copy(env->gwion->mp, t_class); |
73 |
|
5634 |
t->e->parent = t_class; |
74 |
|
5634 |
t->e->ctx = base->e->ctx; |
75 |
|
5634 |
t->e->d.base_type = base; |
76 |
|
5634 |
SET_FLAG(t, infer); |
77 |
|
5634 |
return t; |
78 |
|
|
} |
79 |
|
|
|
80 |
|
5634 |
ANN Value mk_class(const Env env, const Type base) { |
81 |
|
5634 |
const Type t = class_type(env, base); |
82 |
|
5634 |
const Symbol sym = insert_symbol(base->name); |
83 |
|
5634 |
const Value v = new_value(env->gwion->mp, t, s_name(sym)); |
84 |
|
5634 |
valuefrom(env, v->from); |
85 |
|
5634 |
SET_FLAG(v, const | ae_flag_valid); |
86 |
|
5634 |
nspc_add_value_front(base->e->owner, sym, v); |
87 |
|
5634 |
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 |
|
45561 |
ANN m_bool isres(const Env env, const Symbol xid, const loc_t pos) { |
103 |
✓✓ |
45561 |
if(vector_find(&env->gwion->data->reserved, (vtype)xid) > -1) |
104 |
|
1 |
ERR_B(pos, _("%s is reserved."), s_name(xid)); |
105 |
|
45560 |
return GW_OK; |
106 |
|
|
} |