1 |
|
|
#include <stdlib.h> |
2 |
|
|
#include <string.h> |
3 |
|
|
#include "gwion_util.h" |
4 |
|
|
#include "gwion_ast.h" |
5 |
|
|
#include "gwion_env.h" |
6 |
|
|
#include "vm.h" |
7 |
|
|
#include "gwion.h" |
8 |
|
|
#include "instr.h" |
9 |
|
|
#include "object.h" |
10 |
|
|
#include "operator.h" |
11 |
|
|
#include "import.h" |
12 |
|
|
#include "emit.h" |
13 |
|
|
#include "traverse.h" |
14 |
|
|
#include "template.h" |
15 |
|
|
#include "parse.h" |
16 |
|
|
#include "specialid.h" |
17 |
|
|
|
18 |
|
|
#include "gwi.h" |
19 |
|
|
|
20 |
|
|
#undef insert_symbol |
21 |
|
|
|
22 |
|
|
#define describe_logical(name, op) \ |
23 |
|
|
static INSTR(name##Object) { \ |
24 |
|
|
POP_REG(shred, SZ_INT); \ |
25 |
|
|
const M_Object lhs = *(M_Object*)REG(-SZ_INT); \ |
26 |
|
|
const M_Object rhs = *(M_Object*)REG(0); \ |
27 |
|
|
*(m_uint*)REG(-SZ_INT) = (lhs op rhs); \ |
28 |
|
|
release(lhs, shred); \ |
29 |
|
|
release(rhs, shred); \ |
30 |
|
|
} |
31 |
|
|
|
32 |
|
4 |
describe_logical(Eq, ==) |
33 |
|
4 |
describe_logical(Neq, !=) |
34 |
|
54 |
static inline m_bool nonnull_check(const Type l, const Type r) { |
35 |
✓✓✓✓
|
54 |
return !GET_FLAG(l, nonnull) && GET_FLAG(r, nonnull); |
36 |
|
|
} |
37 |
|
|
|
38 |
|
77 |
static inline Type check_nonnull(const Env env, const Type l, const Type r, |
39 |
|
|
const m_str action, const loc_t pos) { |
40 |
✓✓ |
77 |
if(GET_FLAG(r, nonnull)) { |
41 |
✓✓ |
24 |
if(isa(l, env->gwion->type[et_null]) > 0) |
42 |
|
6 |
ERR_N(pos, _("can't %s '%s' to '%s'"), action, l->name, r->name); |
43 |
✓✓ |
18 |
if(isa(l, r) < 0) |
44 |
|
1 |
ERR_N(pos, _("can't %s '%s' to '%s'"), action, l->name, r->name); |
45 |
|
17 |
return r->e->parent; |
46 |
|
|
} |
47 |
✓✓✓✓
|
53 |
if(l != env->gwion->type[et_null] && isa(l, r) < 0) |
48 |
|
4 |
ERR_N(pos, _("can't %s '%s' to '%s'"), action, l->name, r->name); |
49 |
|
49 |
return r; |
50 |
|
|
} |
51 |
|
|
|
52 |
|
34 |
static OP_CHECK(at_object) { |
53 |
|
34 |
const Exp_Binary* bin = (Exp_Binary*)data; |
54 |
|
34 |
const Type l = bin->lhs->info->type; |
55 |
|
34 |
const Type r = bin->rhs->info->type; |
56 |
✗✓ |
34 |
if(opck_rassign(env, data, mut) == env->gwion->type[et_null]) |
57 |
|
|
return env->gwion->type[et_null]; |
58 |
✓✓ |
34 |
if(check_nonnull(env, l, r, "assign", exp_self(bin)->pos) == env->gwion->type[et_null]) |
59 |
|
2 |
return env->gwion->type[et_null]; |
60 |
✓✓ |
32 |
if(bin->rhs->exp_type == ae_exp_decl) { |
61 |
|
23 |
SET_FLAG(bin->rhs->d.exp_decl.td, ref); |
62 |
|
23 |
SET_FLAG(bin->rhs->d.exp_decl.list->self->value, ref); |
63 |
|
|
} |
64 |
|
32 |
exp_setvar(bin->rhs, 1); |
65 |
|
32 |
return r; |
66 |
|
|
} |
67 |
|
|
|
68 |
|
32 |
static OP_EMIT(opem_at_object) { |
69 |
|
32 |
const Exp_Binary* bin = (Exp_Binary*)data; |
70 |
|
32 |
const Type l = bin->lhs->info->type; |
71 |
|
32 |
const Type r = bin->rhs->info->type; |
72 |
✓✓ |
32 |
if(nonnull_check(l, r)) { |
73 |
|
6 |
const Instr instr = emit_add_instr(emit, GWOP_EXCEPT); |
74 |
|
6 |
instr->m_val = SZ_INT; |
75 |
|
|
} |
76 |
|
32 |
return emit_add_instr(emit, ObjectAssign); |
77 |
|
|
} |
78 |
|
|
|
79 |
|
16 |
static OP_CHECK(opck_object_cast) { |
80 |
|
16 |
const Exp_Cast* cast = (Exp_Cast*)data; |
81 |
|
16 |
const Type l = cast->exp->info->type; |
82 |
|
16 |
const Type r = exp_self(cast)->info->type; |
83 |
✓✓ |
16 |
if(check_nonnull(env, l, r, "cast", exp_self(cast)->pos) == env->gwion->type[et_null]) |
84 |
|
4 |
return env->gwion->type[et_null]; |
85 |
|
12 |
return force_type(env, r); |
86 |
|
|
} |
87 |
|
|
|
88 |
|
10 |
static OP_EMIT(opem_object_cast) { |
89 |
|
10 |
const Exp_Cast* cast = (Exp_Cast*)data; |
90 |
|
10 |
const Type l = cast->exp->info->type; |
91 |
|
10 |
const Type r = exp_self(cast)->info->type; |
92 |
✓✓ |
10 |
if(nonnull_check(l, r)) |
93 |
|
4 |
emit_add_instr(emit, GWOP_EXCEPT); |
94 |
|
10 |
return (Instr)GW_OK; |
95 |
|
|
} |
96 |
|
|
|
97 |
|
27 |
static OP_CHECK(opck_implicit_null2obj) { |
98 |
|
27 |
const struct Implicit* imp = (struct Implicit*)data; |
99 |
|
27 |
const Type l = imp->e->info->type; |
100 |
|
27 |
const Type r = imp->t; |
101 |
✓✓ |
27 |
if(check_nonnull(env, l, r, "implicitly cast", imp->e->pos) == env->gwion->type[et_null]) |
102 |
|
5 |
return env->gwion->type[et_null]; |
103 |
|
22 |
imp->e->info->cast_to = r; |
104 |
|
22 |
return imp->t; |
105 |
|
|
} |
106 |
|
|
|
107 |
|
12 |
static OP_EMIT(opem_implicit_null2obj) { |
108 |
|
12 |
const struct Implicit* imp = (struct Implicit*)data; |
109 |
|
12 |
const Type l = imp->e->info->type; |
110 |
|
12 |
const Type r = imp->t; |
111 |
✓✓ |
12 |
if(nonnull_check(l, r)) |
112 |
|
4 |
emit_add_instr(emit, GWOP_EXCEPT); |
113 |
|
12 |
return (Instr)GW_OK; |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
ANN /*static*/ Type scan_class(const Env env, const Type t, const Type_Decl* td); |
117 |
|
|
|
118 |
|
45 |
static Type opck_object_scan(const Env env, const struct TemplateScan *ts) { |
119 |
✓✓ |
45 |
if(ts->td->types) |
120 |
✓✓ |
44 |
return scan_class(env, ts->t, ts->td) ?: env->gwion->type[et_null]; |
121 |
|
1 |
ERR_N(td_pos(ts->td), _("you must provide template types for type '%s'"), ts->t->name) |
122 |
|
|
} |
123 |
|
|
|
124 |
|
45 |
static OP_CHECK(opck_struct_scan) { |
125 |
|
45 |
struct TemplateScan *ts = (struct TemplateScan*)data; |
126 |
|
45 |
return opck_object_scan(env, ts); |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
static const f_instr dotstatic[] = { DotStatic, DotStatic2, DotStatic3, RegPushImm }; |
130 |
|
|
static const f_instr structmember[] = { StructMember, StructMemberFloat, StructMemberOther, StructMemberAddr }; |
131 |
|
|
|
132 |
|
|
ANN Instr emit_kind(Emitter emit, const m_uint size, const uint addr, const f_instr func[]); |
133 |
|
42 |
ANN static void emit_dot_static_data(const Emitter emit, const Value v, const uint emit_var) { |
134 |
|
42 |
const m_uint size = v->type->size; |
135 |
|
42 |
const Instr instr = emit_kind(emit, size, emit_var, dotstatic); |
136 |
|
42 |
instr->m_val = (m_uint)(v->from->owner->info->class_data + v->from->offset); |
137 |
|
42 |
instr->m_val2 = size; |
138 |
|
42 |
} |
139 |
|
|
|
140 |
|
|
static const f_instr regpushimm[] = { RegPushImm, RegPushImm2, RegPushImm3, RegPushImm4 }; |
141 |
|
62 |
ANN static void emit_dot_static_import_data(const Emitter emit, const Value v, const uint emit_addr) { |
142 |
✓✓✓✗ ✓✓ |
82 |
if(v->d.ptr && GET_FLAG(v, builtin) && GET_FLAG(v, const)) { |
143 |
|
20 |
const m_uint size = v->type->size; |
144 |
|
20 |
const Instr instr = emit_kind(emit, size, emit_addr, regpushimm); |
145 |
|
20 |
instr->m_val = (m_uint)v->d.ptr; |
146 |
|
20 |
instr->m_val2 = size; |
147 |
|
|
} else |
148 |
|
42 |
emit_dot_static_data(emit, v, emit_addr); |
149 |
|
62 |
} |
150 |
|
|
static const f_instr dotmember[] = { DotMember, DotMember2, DotMember3, DotMember4 }; |
151 |
|
|
|
152 |
|
305 |
ANN static void emit_member_func(const Emitter emit, const Exp_Dot* member) { |
153 |
|
305 |
const Func f = exp_self(member)->info->type->e->d.func; |
154 |
✓✓ |
305 |
if(f->def->base->tmpl) |
155 |
|
50 |
emit_add_instr(emit, DotTmplVal); |
156 |
|
|
else |
157 |
✓✓✗✓
|
255 |
if(is_class(emit->gwion, member->t_base) || GET_FLAG(member->base->info->type, force)) { |
158 |
✓✓ |
33 |
const Instr func_i = emit_add_instr(emit, f->code ? RegPushImm : SetFunc); |
159 |
✓✓ |
33 |
func_i->m_val = (m_uint)f->code ?: (m_uint)f; |
160 |
|
33 |
return; |
161 |
|
|
} |
162 |
|
|
// if(f->def->base->tmpl) |
163 |
|
|
// emit_add_instr(emit, DotTmplVal); |
164 |
|
|
else { |
165 |
✓✓ |
222 |
if(GET_FLAG(member->t_base, struct)) { |
166 |
✓✓ |
4 |
if(!GET_FLAG(f->def, static)) { |
167 |
|
3 |
exp_setvar(member->base, 1); |
168 |
|
3 |
emit_exp(emit, member->base); |
169 |
|
|
} |
170 |
✗✓ |
4 |
const Instr func_i = emit_add_instr(emit, f->code ? RegPushImm : SetFunc); |
171 |
✓✗ |
4 |
func_i->m_val = (m_uint)f->code ?: (m_uint)f; |
172 |
|
4 |
return; |
173 |
|
|
} |
174 |
✓✓ |
218 |
const Instr instr = emit_add_instr(emit, GET_FLAG(f, member) ? DotFunc : DotStaticFunc); |
175 |
|
218 |
instr->m_val = f->vt_index; |
176 |
|
|
} |
177 |
|
268 |
return; |
178 |
|
|
} |
179 |
|
|
|
180 |
|
178 |
ANN static inline void emit_member(const Emitter emit, const Value v, const uint emit_addr) { |
181 |
|
178 |
const m_uint size = v->type->size; |
182 |
|
178 |
const Instr instr = emit_kind(emit, size, emit_addr, dotmember); |
183 |
|
178 |
instr->m_val = v->from->offset; |
184 |
|
178 |
instr->m_val2 = size; |
185 |
|
178 |
} |
186 |
|
|
|
187 |
|
|
ANN static inline void emit_struct_addr(const Emitter emit, const Value v) { |
188 |
|
|
const Instr set = emit_add_instr(emit, StructMemberAddr); |
189 |
|
|
set->m_val = v->from->offset; |
190 |
|
|
} |
191 |
|
|
|
192 |
|
|
ANN static inline void emit_struct_var(const Emitter emit, const Value v) { |
193 |
|
|
for(m_uint i = 0; i < v->type->size; i += SZ_INT) { |
194 |
|
|
const Instr set = emit_add_instr(emit, Reg2Reg); |
195 |
|
|
set->m_val2 = -v->type->size + i; |
196 |
|
|
set->m_val = -v->type->size + v->from->offset + i; |
197 |
|
|
} |
198 |
|
|
} |
199 |
|
|
|
200 |
|
14 |
ANN static inline void emit_struct_data(const Emitter emit, const Value v, const uint emit_addr) { |
201 |
|
14 |
const Instr instr = emit_kind(emit, v->type->size, emit_addr, structmember); |
202 |
|
14 |
instr->m_val = v->from->offset; |
203 |
✓✓ |
14 |
if(!emit_addr) { |
204 |
|
10 |
const Instr instr = emit_add_instr(emit, RegPush); |
205 |
|
10 |
instr->m_val = v->type->size -SZ_INT; |
206 |
|
|
} |
207 |
|
14 |
} |
208 |
|
|
|
209 |
|
|
ANN m_bool not_from_owner_class(const Env env, const Type t, const Value v, const loc_t pos); |
210 |
|
745 |
OP_CHECK(opck_object_dot) { |
211 |
|
745 |
const Exp_Dot *member = (Exp_Dot*)data; |
212 |
|
745 |
const m_str str = s_name(member->xid); |
213 |
|
745 |
const m_bool base_static = is_class(env->gwion, member->t_base); |
214 |
✓✓ |
745 |
const Type the_base = base_static ? member->t_base->e->d.base_type : member->t_base; |
215 |
✓✓ |
745 |
if(!the_base->nspc) |
216 |
|
1 |
ERR_O(member->base->pos, |
217 |
|
|
_("type '%s' does not have members - invalid use in dot expression of %s"), |
218 |
|
|
the_base->name, str) |
219 |
✓✓✓✗
|
744 |
if(member->xid == insert_symbol(env->gwion->st, "this") && base_static) |
220 |
|
2 |
ERR_O(exp_self(member)->pos, |
221 |
|
|
_("keyword 'this' must be associated with object instance...")) |
222 |
|
742 |
const Value value = find_value(the_base, member->xid); |
223 |
✓✓ |
742 |
if(!value) { |
224 |
|
12 |
env_err(env, exp_self(member)->pos, |
225 |
|
|
_("class '%s' has no member '%s'"), the_base->name, str); |
226 |
✓✓ |
12 |
if(member->t_base->nspc) |
227 |
✓✗ |
10 |
did_you_mean_type(the_base, str); |
228 |
|
12 |
return NULL; |
229 |
|
|
} |
230 |
✗✓ |
730 |
CHECK_BO(not_from_owner_class(env, the_base, value, exp_self(member)->pos)) |
231 |
✓✓✓✓
|
730 |
if(!env->class_def || isa(env->class_def, value->from->owner_class) < 0) { |
232 |
✓✓ |
544 |
if(GET_FLAG(value, private)) |
233 |
|
12 |
ERR_O(exp_self(member)->pos, |
234 |
|
|
_("can't access private '%s' outside of class..."), value->name) |
235 |
✓✓ |
532 |
else if(GET_FLAG(value, protect)) |
236 |
|
2 |
exp_setprot(exp_self(member), 1); |
237 |
|
|
} |
238 |
✓✓✓✓
|
718 |
if(base_static && GET_FLAG(value, member)) |
239 |
|
2 |
ERR_O(exp_self(member)->pos, |
240 |
|
|
_("cannot access member '%s.%s' without object instance..."), |
241 |
|
|
the_base->name, str) |
242 |
✓✓ |
716 |
if(GET_FLAG(value, const)) |
243 |
|
471 |
exp_setmeta(exp_self(member), 1); |
244 |
|
716 |
return value->type; |
245 |
|
|
} |
246 |
|
|
|
247 |
|
559 |
OP_EMIT(opem_object_dot) { |
248 |
|
559 |
const Exp_Dot *member = (Exp_Dot*)data; |
249 |
|
559 |
const Type t_base = actual_type(emit->gwion, member->t_base); |
250 |
|
559 |
const Value value = find_value(t_base, member->xid); |
251 |
✓✓✓✓ ✓✓ |
572 |
if(!is_class(emit->gwion, member->t_base) && (GET_FLAG(value, member) || |
252 |
✓✓ |
21 |
(isa(exp_self(member)->info->type, emit->gwion->type[et_function]) > 0 && |
253 |
|
8 |
!is_fptr(emit->gwion, exp_self(member)->info->type)))) { |
254 |
✓✓ |
462 |
if(!GET_FLAG(t_base, struct)) |
255 |
✗✓ |
444 |
CHECK_BO(emit_exp(emit, member->base)) |
256 |
✓✓ |
462 |
if(isa(member->t_base, emit->env->gwion->type[et_object]) > 0) |
257 |
|
444 |
emit_except(emit, member->t_base); |
258 |
|
|
} |
259 |
✓✓✓✓
|
559 |
if(isa(exp_self(member)->info->type, emit->gwion->type[et_function]) > 0 && !is_fptr(emit->gwion, exp_self(member)->info->type)) |
260 |
|
305 |
emit_member_func(emit, member); |
261 |
✓✓ |
254 |
else if(GET_FLAG(value, member)) { |
262 |
✓✓ |
192 |
if(!GET_FLAG(t_base, struct)) |
263 |
|
178 |
emit_member(emit, value, exp_getvar(exp_self(member))); |
264 |
|
|
else { |
265 |
|
|
// exp_setvar(member->base, exp_getvar(exp_self(member))); |
266 |
|
14 |
exp_setvar(member->base, 1); |
267 |
✗✓ |
14 |
CHECK_BO(emit_exp(emit, member->base)) |
268 |
|
14 |
emit_struct_data(emit, value, exp_getvar(exp_self(member))); |
269 |
|
|
} |
270 |
✓✗ |
62 |
} else if(GET_FLAG(value, static)) |
271 |
|
62 |
emit_dot_static_import_data(emit, value, exp_getvar(exp_self(member))); |
272 |
|
|
else { // member type |
273 |
|
|
const Instr instr = emit_add_instr(emit, RegPushImm); |
274 |
|
|
instr->m_val = (m_uint)value->type; |
275 |
|
|
} |
276 |
|
559 |
return (Instr)GW_OK; |
277 |
|
|
} |
278 |
|
|
|
279 |
|
|
struct tmpl_info { |
280 |
|
|
const Class_Def cdef; |
281 |
|
|
Type_List call; |
282 |
|
|
struct Vector_ type; |
283 |
|
|
struct Vector_ size; |
284 |
|
|
uint8_t index; |
285 |
|
|
}; |
286 |
|
|
|
287 |
|
148 |
ANN static inline size_t tmpl_set(struct tmpl_info* info, const Type t) { |
288 |
|
148 |
vector_add(&info->type, (vtype)t); |
289 |
|
148 |
const size_t len = strlen(t->name); |
290 |
|
148 |
vector_add(&info->size, len); |
291 |
|
148 |
return len; |
292 |
|
|
} |
293 |
|
|
|
294 |
|
69 |
ANN static ssize_t template_size(const Env env, struct tmpl_info* info) { |
295 |
|
69 |
ID_List base = info->cdef->base.tmpl->list; |
296 |
|
69 |
Type_List call = info->call; |
297 |
|
69 |
size_t size = 0; |
298 |
|
|
do { |
299 |
✗✓ |
79 |
DECL_OB(const Type, t, = known_type(env, call->td)) |
300 |
|
79 |
size += tmpl_set(info, t); |
301 |
✓✓✓✗ ✓✗ |
79 |
} while((call = call->next) && (base = base->next) && ++size); |
302 |
|
69 |
size += tmpl_set(info, info->cdef->base.type); |
303 |
|
69 |
return size + 16 + 3; |
304 |
|
|
} |
305 |
|
|
|
306 |
|
148 |
ANN static inline m_str tmpl_get(struct tmpl_info* info, m_str str) { |
307 |
|
148 |
const Type t = (Type)vector_at(&info->type, info->index); |
308 |
|
148 |
strcpy(str, t->name); |
309 |
|
148 |
return str += vector_at(&info->size, info->index); |
310 |
|
|
} |
311 |
|
|
|
312 |
|
69 |
ANN static void template_name(struct tmpl_info* info, m_str s) { |
313 |
|
69 |
m_str str = s; |
314 |
|
69 |
*str++ = '<'; |
315 |
|
69 |
*str++ = '~'; |
316 |
|
69 |
const m_uint size = vector_size(&info->type) -1; |
317 |
✓✓ |
148 |
for(info->index = 0; info->index < size; ++info->index) { |
318 |
|
79 |
str = tmpl_get(info, str); |
319 |
✓✓ |
79 |
if(info->index < size - 1) |
320 |
|
10 |
*str++ = ','; |
321 |
|
|
else { |
322 |
|
69 |
*str++ = '~'; |
323 |
|
69 |
*str++ = '>'; |
324 |
|
|
} |
325 |
|
|
} |
326 |
|
69 |
str = tmpl_get(info, str); |
327 |
|
69 |
*str = '\0'; |
328 |
|
69 |
} |
329 |
|
|
|
330 |
|
69 |
ANEW ANN static Symbol template_id(const Env env, const Class_Def c, const Type_List call) { |
331 |
|
69 |
struct tmpl_info info = { .cdef=c, .call=call }; |
332 |
|
69 |
vector_init(&info.type); |
333 |
|
69 |
vector_init(&info.size); |
334 |
|
69 |
ssize_t sz = template_size(env, &info); |
335 |
|
69 |
char name[sz]; |
336 |
✓✗ |
69 |
if(sz > GW_ERROR) |
337 |
|
69 |
template_name(&info, name); |
338 |
|
69 |
vector_release(&info.type); |
339 |
|
69 |
vector_release(&info.size); |
340 |
✓✗ |
69 |
return sz > GW_ERROR ? insert_symbol(env->gwion->st, name) : NULL; |
341 |
|
|
} |
342 |
|
|
|
343 |
|
71 |
ANN static m_bool template_match(ID_List base, Type_List call) { |
344 |
✓✓✓✓
|
71 |
while((call = call->next) && (base = base->next)); |
345 |
✓✓ |
71 |
return !call ? GW_OK : GW_ERROR; |
346 |
|
|
} |
347 |
|
|
|
348 |
|
69 |
ANN static Class_Def template_class(const Env env, const Class_Def def, const Type_List call) { |
349 |
✗✓ |
69 |
DECL_OO(const Symbol, name, = template_id(env, def, call)) |
350 |
✓✓✓✓
|
69 |
if(env->class_def && name == insert_symbol(env->gwion->st, env->class_def->name)) |
351 |
|
1 |
return env->class_def->e->def; |
352 |
|
68 |
const Type t = nspc_lookup_type1(env->curr, name); |
353 |
✓✓ |
68 |
if(t) |
354 |
|
23 |
return t->e->def; |
355 |
|
45 |
const Class_Def c = cpy_class_def(env->gwion->mp, def); |
356 |
|
45 |
c->base.xid = name; |
357 |
|
45 |
SET_FLAG(c, template | ae_flag_ref); |
358 |
|
45 |
UNSET_FLAG(c, scan0 | ae_flag_scan1 | ae_flag_scan2 | |
359 |
|
|
ae_flag_check | ae_flag_emit | ae_flag_valid); |
360 |
|
45 |
return c; |
361 |
|
|
} |
362 |
|
|
|
363 |
|
3 |
ANN static m_bool class2udef(const Env env, const Class_Def a, const Type t) { |
364 |
|
6 |
a->union_def = new_union_def(env->gwion->mp, cpy_decl_list(env->gwion->mp, a->list), |
365 |
|
6 |
loc_cpy(env->gwion->mp, t->e->def->pos)); |
366 |
|
3 |
a->union_def->type_xid = a->base.xid; |
367 |
✗✓ |
3 |
if(GET_FLAG(t, global)) |
368 |
|
|
SET_FLAG(a->union_def, global); |
369 |
✗✓ |
3 |
CHECK_BB(scan0_union_def(env, a->union_def)) |
370 |
|
3 |
a->base.type = a->union_def->type; |
371 |
|
3 |
a->base.type->e->def = a; |
372 |
|
3 |
a->union_def->tmpl = cpy_tmpl(env->gwion->mp, a->base.tmpl); |
373 |
|
3 |
return GW_OK; |
374 |
|
|
} |
375 |
|
|
|
376 |
|
45 |
ANN static m_bool _scan_class(const Env env, const Type t, const Class_Def a) { |
377 |
✓✓ |
45 |
if(t->e->parent != env->gwion->type[et_union]) |
378 |
✗✓ |
42 |
CHECK_BB(scan0_class_def(env, a)) |
379 |
|
|
else |
380 |
✗✓ |
3 |
CHECK_BB(class2udef(env, a, t)) |
381 |
|
45 |
SET_FLAG(a->base.type, template); |
382 |
✓✓ |
45 |
if(GET_FLAG(t, builtin)) |
383 |
|
19 |
SET_FLAG(a->base.type, builtin); |
384 |
|
45 |
return GW_OK; |
385 |
|
|
} |
386 |
|
|
|
387 |
|
71 |
ANN Type scan_class(const Env env, const Type t, const Type_Decl* td) { |
388 |
✓✓ |
71 |
if(template_match(t->e->def->base.tmpl->list, td->types) < 0) |
389 |
|
2 |
ERR_O(td->pos, _("invalid template types number")) |
390 |
✗✓ |
69 |
DECL_OO(const Class_Def, a, = template_class(env, t->e->def, td->types)) |
391 |
✓✓ |
69 |
if(a->base.type) |
392 |
|
24 |
return a->base.type; |
393 |
|
90 |
struct EnvSet es = { .env=env, .data=env, .func=(_exp_func)scan0_cdef, |
394 |
|
45 |
.scope=env->scope->depth, .flag=ae_flag_scan0 }; |
395 |
|
|
// CHECK_BO(envset_push(&es, t->e->owner_class, env->context ? env->context->nspc : env->curr)) |
396 |
✓✓✗✓
|
45 |
CHECK_BO(envset_push(&es, t->e->owner_class, t->e->ctx ? t->e->ctx->nspc : env->curr)) |
397 |
|
45 |
a->base.tmpl = mk_tmpl(env, t->e->def->base.tmpl, td->types); |
398 |
|
45 |
const m_bool ret = _scan_class(env, t, a); |
399 |
✓✓ |
45 |
if(es.run) |
400 |
|
1 |
envset_pop(&es, t->e->owner_class); |
401 |
✓✗ |
45 |
if(ret > 0) |
402 |
|
45 |
return a->base.type; |
403 |
|
|
if(!a->base.type) |
404 |
|
|
free_class_def(env->gwion->mp, a); |
405 |
|
|
return NULL; |
406 |
|
|
} |
407 |
|
|
|
408 |
|
326 |
ANN static inline Symbol dot_symbol(SymTable *st, const Value v) { |
409 |
✓✓ |
326 |
const m_str name = !GET_FLAG(v, static) ? "this" : v->from->owner_class->name; |
410 |
|
326 |
return insert_symbol(st, name); |
411 |
|
|
} |
412 |
|
|
|
413 |
|
326 |
ANN Exp symbol_owned_exp(const Gwion gwion, const Symbol *data) { |
414 |
|
326 |
const Value v = prim_self(data)->value; |
415 |
|
326 |
const Exp base = new_prim_id(gwion->mp, dot_symbol(gwion->st, v), loc_cpy(gwion->mp, prim_pos(data))); |
416 |
|
326 |
const Exp dot = new_exp_dot(gwion->mp, base, *data); |
417 |
|
326 |
const Type owner = v->from->owner_class; |
418 |
|
652 |
dot->d.exp_dot.t_base = dot->d.exp_dot.base->info->type = !GET_FLAG(v, static) ? |
419 |
✓✓ |
326 |
owner : type_class(gwion, owner); |
420 |
|
326 |
dot->info->type = prim_exp(data)->info->type; |
421 |
|
326 |
exp_setvar(dot, exp_getvar(prim_exp(data))); |
422 |
|
326 |
return dot; |
423 |
|
|
} |
424 |
|
|
|
425 |
|
|
ANN void struct_release(const VM_Shred shred, const Type base, const m_bit *ptr) { |
426 |
|
|
const Vector types = &base->e->tuple->types; |
427 |
|
|
const Vector offsets = &base->e->tuple->offset; |
428 |
|
|
for(m_uint i = 0; i < vector_size(types); ++i) { |
429 |
|
|
const Type t = (Type)vector_at(types, i); |
430 |
|
|
if(isa(t, shred->info->vm->gwion->type[et_compound]) < 0) |
431 |
|
|
continue; |
432 |
|
|
const m_uint offset = vector_at(offsets, i); |
433 |
|
|
if(!GET_FLAG(t, struct)) |
434 |
|
|
release(*(M_Object*)(ptr + offset), shred); |
435 |
|
|
else |
436 |
|
|
struct_release(shred, t, *(m_bit**)(ptr + offset)); |
437 |
|
|
} |
438 |
|
|
} |
439 |
|
|
|
440 |
|
711 |
GWION_IMPORT(object_op) { |
441 |
|
711 |
const Type t_null = gwi_mk_type(gwi, "@null", SZ_INT, NULL); |
442 |
|
711 |
gwi->gwion->type[et_null] = t_null; |
443 |
|
711 |
GWI_BB(gwi_set_global_type(gwi, t_null, et_null)) |
444 |
|
711 |
GWI_BB(gwi_oper_cond(gwi, "Object", BranchEqInt, BranchNeqInt)) |
445 |
|
711 |
GWI_BB(gwi_oper_ini(gwi, "Object", "Object", NULL)) |
446 |
|
711 |
GWI_BB(gwi_oper_add(gwi, at_object)) |
447 |
|
711 |
GWI_BB(gwi_oper_emi(gwi, opem_at_object)) |
448 |
|
711 |
GWI_BB(gwi_oper_end(gwi, "@=>", NULL)) |
449 |
|
711 |
GWI_BB(gwi_oper_ini(gwi, "@null", "Object", NULL)) |
450 |
|
711 |
GWI_BB(gwi_oper_add(gwi, at_object)) |
451 |
|
711 |
GWI_BB(gwi_oper_emi(gwi, opem_at_object)) |
452 |
|
711 |
GWI_BB(gwi_oper_end(gwi, "@=>", NULL)) |
453 |
|
711 |
GWI_BB(gwi_oper_ini(gwi, "Object", "Object", "int")) |
454 |
|
711 |
GWI_BB(gwi_oper_end(gwi, "==", EqObject)) |
455 |
|
711 |
GWI_BB(gwi_oper_end(gwi, "!=", NeqObject)) |
456 |
|
711 |
GWI_BB(gwi_oper_add(gwi, opck_object_cast)) |
457 |
|
711 |
GWI_BB(gwi_oper_emi(gwi, opem_object_cast)) |
458 |
|
711 |
GWI_BB(gwi_oper_end(gwi, "$", NULL)) |
459 |
|
711 |
GWI_BB(gwi_oper_add(gwi, opck_implicit_null2obj)) |
460 |
|
711 |
GWI_BB(gwi_oper_emi(gwi, opem_implicit_null2obj)) |
461 |
|
711 |
GWI_BB(gwi_oper_end(gwi, "@implicit", NULL)) |
462 |
|
711 |
GWI_BB(gwi_oper_ini(gwi, "@null", "Object", NULL)) |
463 |
|
711 |
GWI_BB(gwi_oper_add(gwi, opck_implicit_null2obj)) |
464 |
|
711 |
GWI_BB(gwi_oper_end(gwi, "@implicit", NULL)) |
465 |
|
711 |
GWI_BB(gwi_oper_ini(gwi, "@null", "Object", NULL)) |
466 |
|
711 |
GWI_BB(gwi_oper_add(gwi, opck_object_cast)) |
467 |
|
711 |
GWI_BB(gwi_oper_emi(gwi, opem_object_cast)) |
468 |
|
711 |
GWI_BB(gwi_oper_end(gwi, "$", NULL)) |
469 |
|
711 |
GWI_BB(gwi_oper_ini(gwi, NULL, "Object", "bool")) |
470 |
|
711 |
GWI_BB(gwi_oper_add(gwi, opck_unary_meta2)) |
471 |
|
711 |
GWI_BB(gwi_oper_end(gwi, "!", IntNot)) |
472 |
|
711 |
GWI_BB(gwi_oper_ini(gwi, "@Compound", NULL, NULL)) |
473 |
|
711 |
GWI_BB(gwi_oper_add(gwi, opck_struct_scan)) |
474 |
|
711 |
GWI_BB(gwi_oper_end(gwi, "@scan", NULL)) |
475 |
|
711 |
gwi_item_ini(gwi, "@null", "null"); |
476 |
|
711 |
gwi_item_end(gwi, 0, NULL); |
477 |
|
711 |
return GW_OK; |
478 |
|
|
} |